ó ;œPc@s]dZdd d„ƒYZd„Zd„Zdd d„ƒYZd„Zd„Zd „Zd 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 KnuthBendixcBsbeZddd„Zd„Zd„Zd„Zd„Zd„Zd d„Z d „Z d „Z RS( tidcCs£g|_||_xs|D]k\}}|rL|j|ƒ}|j|ƒ}n|j||ƒrn||}}n|jj||fƒqW|j|ƒ|jƒdS(N(t reductionstdelimt wrap_delimtgttappendtmake_confluenttsort(tselftERtmax_iterationstatb((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyt__init__Cs   cCs|j|ƒ|j|ƒkS(N(treduce(R txty((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyt__call__PscCsr|j}|r*t|ƒ}t|ƒ}n|j|ƒ}|j|ƒ}||krXdS||krhdS||kS(Nii(Rtlentcount(R R R Rtlatlb((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyRSs    c s®‡fd†}i}x’t|ƒD]w}dˆ_tˆjƒ}xx|D]p\}}xa|D]Y\}} |||| f} | |krŠqZnd|| <||kr&ˆj|ƒ} |j|ƒ}xe|dkr"|| | ||t|ƒ} ˆj| ƒ} || | ƒ|j||dƒ}qÁWnt|ƒ}x~td|tˆjƒƒD]`}||| ||krOˆj||||ƒ} ˆj|| | ƒ} || | ƒqOqOWqZWqGWg}ˆjˆjf}x•t ˆjƒD]„\}}|\}}|ˆj|<ˆj|ƒ}ˆj|ƒ}||krã||krã||f}|j |ƒ|ˆj|dSdS(Niiiÿÿÿÿ(R(t.0t.1Rt_Rt__(R (sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pytcmpÊs  (tNoneRR(R R$R:((R sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyRÇs  cCst|jƒdS(N(t printredsR(R ((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pytppÒscCsH|j|jƒs"|j|}n|j|jƒsD||j}n|S(N(t startswithRtendswith(R R((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyRÕs N( t__name__t __module__RRRRRRR;RR=R(((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyRBs   K  cCs\x=t|ƒD]/\}}d|fG|dddkr Hq q W|dddkrXHndS(Ns%s iii(R (R$R#R.((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyR<Üs  cOst|||ŽS(N(R(R R tk((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pytkbåst _GLUECLAMP_cBseZRS((R@RA(((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyRDêsc CsŽddddg}d d ddg}t|d d ƒ}d g|gg|D]9}|gg|D]}|jd ||fƒ^q]^qJ}|S(Nt1s-1R#s-is1.isi.isi.i.isi.i.i.iRt.s%s.%s(s1.iR#(si.is-1(si.i.is-i(si.i.i.iRE(RCR(tGR tRRRtT((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyttest2ñs T cCsdg}t|ddƒ}ddg}t|ƒ}|jd ƒ}|jd ƒ}dg}t|ddƒ}|jd ƒ}|jd ƒ}|jd ƒ}ddg}t|ddƒ}|jdƒ}|jdƒ}d d!g}t|ddƒ}|jdƒ}|jdƒ}dS("Ns.a.s.b.RRFtfhktghtmtkkmt fffghkkkmtffghkmtaas.bb.R s.f.h.k.s.g.h.s.m.s.k.k.m.s.f.f.f.g.h.k.k.k.m.s .f.f.g.h.k.m.s .f.ff.fff.s .ffff.ff.s.fffff.s.fff.fff.fffff.s!.f.f.f.ffff.ff.fff.fff.fff.fffff.s.f.f.ffff.ff.fff.fffff.(s.a.s.b.(RKRL(RMRN(s.a.s.b.(s.f.h.k.s.g.h.(s.m.s.k.k.m.(s .f.ff.fff.s .ffff.ff.(s.fffff.s.fff.fff.fffff.(RCR(R R RR((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyttests(      c!Cs”d,d-d.d/d0g}t|ƒ}d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPg }||_|S(QNtHcctHtaabtbataactcatcccbtabctcacaR tHbtHatHaatHabtHcatHactHcbtabbtbbtcbtacbtbaatbabtbactbbatbcatbcbtbbctcabtcbatcbbtcbctccbtHacatHacctbbbbtbbbctbbcctbbbtbccatccaatcccatcacca(RSRT(RURV(RWRX(RYRZ(R[R (R\R](R^R](R_R](R`Ra(RbRa(RSRT(RURV(RWRX(RcRd(RZRe(RfRe(RgRV(RhRd(RiRe(RjRd(RkRe(RlRm(RnRe(RoRe(RpRm(RqRd(RrRd(RsRa(RtR](RuRd(RvRe(RwRx(RyRd(R[R (RzRV(R{Re(R|Re(RCtcanon(R R R}((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyttest31s           N(((t__doc__RR<RCRDRJRRR~(((sH/opt/alt/python27/lib64/python2.7/site-packages/guppy/etc/KnuthBendix.pyt@sš   )