File: //opt/alt/python37/lib64/python3.7/site-packages/guppy/etc/__pycache__/KnuthBendix.cpython-37.pyc
B
    �]?#  �               @   sT   d Z ddlZG dd� d�Zdd� Zdd� ZG d	d
� d
�Zdd� Zd
d� Zdd� ZdS )a�  
    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
}
�    Nc               @   sX   e Zd Zddd�Zdd� Zdd� Zd	d
� Zdd� Zd
d� Zddd�Z	dd� Z
dd� ZdS )�KnuthBendix� �d   c             C   sr   g | _ || _xN|D ]F\}}|r2| �|�}| �|�}| �||�rH|| }}| j �||f� qW | �|� | ��  d S )N)�
reductions�delim�
wrap_delim�gt�append�make_confluent�sort)�self�Er   �max_iterations�a�b� r   �H/opt/alt/python37/lib64/python3.7/site-packages/guppy/etc/KnuthBendix.py�__init__D   s    
zKnuthBendix.__init__c             C   s   | � |�| � |�kS )N)�reduce)r   �x�yr   r   r   �__call__Q   s    zKnuthBendix.__call__c             C   sP   | j }|rt|�}t|�}n|�|�}|�|�}||kr<dS ||k rHdS ||kS )N�   r   )r   �len�count)r   r   r   r   ZlaZlbr   r   r   r   T   s    
zKnuthBendix.gtc          
      sN  � fdd�}i }�x6t |�D �]}d� _t� j�}�x8|D �].\}}�x"|D �]\}}	||||	f}
|
|krlqLd||
<