a a@sdZddlZdZdZGdddeZGdddeZGd d d eZGd d d eZGd ddeZ GdddeZ Gddde Z Gddde Z dS)a Prolog Terms All Prolog data structures are called terms. A term is either: - A constant, which can be either an atom or a number (represented using python types) - A variable (represented using a Var object) - A compound term (represented using a Term object) In prolog, structures such as lists and tuples are also terms. Here these can be directly represented by the analogous python structure Nz!Chris Mungall z0.0.6c@seZdZdZddZdS)Programzm A program is a collection of terms note: in future this may also allow for creation of modules cGst||_dSN)listterms)selfrr?/opt/logicmoo_workspace/packs_lib/prologterms-py/prologterms.py__init__szProgram.__init__N__name__ __module__ __qualname____doc__r rrrrrsrc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd"S)#Termzj Compound terms, which consist of a predicate plus one or more arguments, each of which is a term cGs||_t||_g|_dSr)predrargscomments)rrrrrrr !s z Term.__init__cCs|j|dS)z6 Terms can be annotated with comments N)rappend)rcrrr add_comment&szTerm.add_commentcCs t||Sr)Rulerbodyrrr__le__-sz Term.__le__cCs td||S)N<rrrrr__lt__0sz Term.__lt__cCs td||S)N>rrrrr__gt__2sz Term.__gt__cCs td||S)Nz>=rrrrr__ge__4sz Term.__ge__cCs td||S)N=rrrrr__eq__8sz Term.__eq__cCs td||S)Nz\=rrrrr__ne__<sz Term.__ne__cCs td||SN-rrrrr__sub__?sz Term.__sub__cCs td||S)N+rrrrr__add__Asz Term.__add__cCs td||S)N*rrrrr__mul__Csz Term.__mul__cCs td||S)N/rrrrr __truediv__EszTerm.__truediv__cCs td||S)Nz**rrrrr__pow__Gsz Term.__pow__cCs |g|_|Sr)r)rcommentrrr__mod__Ksz Term.__mod__cCs td|Sr#rrrrr__neg__Psz Term.__neg__cCs td|S)Nz\+rr/rrr __invert__TszTerm.__invert__N)r r r rr rrrrrr!r"r%r'r)r+r,r.r0r1rrrrrs"rc@seZdZdZddZdS)rz A Rule is a compound term with 2 args, where the predicate is ":-" the first arg is a compound term representing the head (consequent) the second arg is a tuple of compound terms representing the body (antecedents) cCsd|_||g|_g|_dS)z] Arguments --------- head: Term body: tuple of Terms z:-N)rrr)rheadrrrrr ^s z Rule.__init__Nr rrrrrXsrc@seZdZdZddZdS)Varzz Represents a prolog variable. These should use a leading uppercase. TODO: allow other naming conventions cCs||_g|_dSr)namer)rr4rrrr osz Var.__init__Nr rrrrr3isr3c@seZdZdZddZdS) TermGeneratorz An object for conveniently generating compound terms. After creating a TermGenerator G, call G.PRED(ARG1, ..., ) to generate a compound term csfdd}|S)Ncstg|RSrr)rrrrmethodzsz)TermGenerator.__getattr__..methodr)rrr7rr6r __getattr__ys zTermGenerator.__getattr__N)r r r rr8rrrrr5ssr5c@s eZdZdS)RendererN)r r r rrrrr9~sr9c@seZdZdZddZdS)PrologRendererz| Renders internal prolog term or program representations as strings that can be fed directly to a prolog engine csd}t|tr2ddfdd|jD}npt|tr|jd}|jd}d}t|trddfdd|D}n |}d ||}nt|t rd|j }nt|t rt |jdkr|j }n$d |j dfd d|jD}nt|tr(d dfd d|D}nzt|trTddfdd|D}nNt|trd|}td|s|dddd}d|}n d|}t|t rt |jdkrddd|jD}||}|S)z Renders a prolog term, program or python structure holding these. Note that single terms are rendered without a closing ".". Include terms in a Program object to render with "."s {}csg|]}|dqS)z. render.0xr/rr z)PrologRenderer.render..rz, csg|]}|qSrr=r@er/rrrBrCz {} :- {}z{}({})csg|]}|qSrr=r@ar/rrrBrCz[{}]csg|]}|qSrr=rEr/rrrBrC({})csg|]}|qSrr=rEr/rrrBrC ^[a-z]\w*$'\' \n'{}'cSsg|]}d|qS)z% {} formatr@rrrrrBrC) isinstancerrQjoinrrr>rtupler3r4rlenrrstrrematchreplacerrtsr2ZbodytrZcmtrr/rr>s> $       &        zPrologRenderer.renderNr r r rr>rrrrr:sr:c@seZdZdZddZdS)SExpressionRendererz Renders internal prolog term or program representations as S-Expression strings that can be fed directly to a prolog engine such as kanren csd}t|tr2ddfdd|jD}nXt|tr|jd}|jd}d}t|trddfdd|D}n |}d ||}nt|t rd |j }nt|t rd |j dfd d|jD}nt|t rd dfdd|D}nzt|tr.rrDcsg|]}|qSrr=rEr/rrrBrCz (<= {} {})z?{}z({} {})csg|]}|qSrr=rGr/rrrBrCz (list {})csg|]}|qSrr=rEr/rrrBrCrIcsg|]}|qSrr=rEr/rrrBrCrJrKrLrMrNrOcSsg|]}d|qS)z; {} rPrRrrrrBrC)rSrrQrTrrr>rrUr3r4rrrrWrXrYrZrVrr[rr/rr>s: $       &        zSExpressionRenderer.renderNr^rrrrr_sr_) rrX __author__ __version__objectrrrr3r5r9r:r_rrrrs ;  0