File: //opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyo
�
;�Pc           @   s]   d  Z  d d d �  �  YZ d �  Z d �  Z d d d �  �  YZ d �  Z d �  Z d	 �  Z d
 S(
   s�  
    An implementation of the Knuth-Bendix algorithm,
    as described in (1), p. 143.
    For determining if two paths in a category are equal.
The algorithm as given here,
takes a set of equations in the form of a sequence:
E = [(a, b), (c, d) ...]
where a, b, c, d are 'paths'.
Paths are given as strings, for example:
E = [ ('fhk', 'gh'), ('m', 'kkm') ]
means that the path 'fhk' equals 'gh' and 'm' equals 'kkm'.
Each arrow in the path is here a single character.  If longer arrow
names are required, a delimiter string can be specified as in:
kb(E, delim='.')
The paths must then be given by the delimiter between each arrow;
E = [ ('h_arrow.g_arrow', 'g_arrow.k_arrow') ... ]
The function kb(E) returns an object, say A, which is
o callable: A(a, b)->boolean determines if two paths given by a, b are equal.
o has a method A.reduce(a)->pathstring, which reduces a path to normal form.
An optional parameter to kb, max_iterations, determines the maximum
number of iterations the algorithm should try making the reduction
system 'confluent'. The algorithm is not guaranteed to terminate
with a confluent system in a finite number of iterations, so if the
number of iterations needed exceeds max_iterations an exception
(ValueError) will be raised. The default is 100.
References
(1)
@book{walters91categories,
    title={Categories and Computer Science},
    author={R. F. C. Walters},
    publisher={Cambridge University Press},
    location={Cambridge},
    year=1991}
(2)
@book{grimaldi94discrete,
author="Ralph P. Grimaldi".
title="Discrete and Combinatorial Mathematics: An Applied Introduction",
publisher="Addison-Wesley",
location="Readin, Massachusetts",
year=1994
}
t   KnuthBendixc           B   sb   e  Z d  d d � Z d �  Z d �  Z d �  Z d �  Z d �  Z d d � Z	 d	 �  Z
 d
 �  Z RS(   t    id   c         C   s�   g  |  _  | |  _ xs | D]k \ } } | rL |  j | � } |  j | � } n  |  j | | � rn | | } } n  |  j  j | | f � q W|  j | � |  j �  d  S(   N(   t
   reductionst   delimt
   wrap_delimt   gtt   appendt   make_confluentt   sort(   t   selft   ER   t   max_iterationst   at   b(    (    sH   /opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyt   __init__C   s    		
c         C   s   |  j  | � |  j  | � k S(   N(   t   reduce(   R	   t   xt   y(    (    sH   /opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyt   __call__P   s    c         C   sr   |  j  } | r* t | � } t | � } n | j | � } | j | � } | | k rX d S| | k  rh d S| | k S(   Ni   i    (   R   t   lent   count(   R	   R   R
   R   t   lat   lb(    (    sH   /opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyR   S   s    	c      	      s�  �  f d �  } i  } x�t  | � D]w} d �  _ t �  j � } xx| D]p\ } } xa| D]Y\ } }	 | | | |	 f }
 |
 | k r� qZ n  d | |
 <| | k r&�  j | � } | j | � } xe | d k r"| |  |	 | | t | � } �  j | � }
 | | |
 � | j | | d � } q� Wn  t | � } x~ t  d | t �  j � � D]` } | | |  | | k rO�  j | | | | � } �  j | |  |	 � }
 | | |
 � qOqOWqZ WqG Wg  } �  j �  j f } x� t	 �  j � D]� \ } } | \ } } | �  j | <�  j | � } �  j | � } | | k r�| | k r�| | f } | j
 | � | �  j | <q�q�Wt | � �  j k r�| �  _ n  �  j r"