o 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@eZdZdZddZdS)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 rrrr r rc@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)rrrrrr r !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__- z Term.__le__cC td||S)N<rrrrr __lt__0 z Term.__lt__cCr)N>rrrrr __gt__2r!z Term.__gt__cCr)Nz>=rrrrr __ge__4r!z Term.__ge__cCr)N=rrrrr __eq__8r!z Term.__eq__cCr)Nz\=rrrrr __ne__<r!z Term.__ne__cCrN-rrrrr __sub__?r!z Term.__sub__cCr)N+rrrrr __add__Ar!z Term.__add__cCr)N*rrrrr __mul__Cr!z Term.__mul__cCr)N/rrrrr __truediv__Er!zTerm.__truediv__cCr)Nz**rrrrr __pow__Gr!z Term.__pow__cCs |g|_|Sr)r)rcommentrrr __mod__Ksz Term.__mod__cC td|Sr(rrrrr __neg__Prz Term.__neg__cCr4)Nz\+rr5rrr __invert__TrzTerm.__invert__N)r r rrr rrr r#r$r&r'r*r,r.r0r1r3r6r7rrrr rs$ rc@r)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)rheadrrrr r ^s  z Rule.__init__Nr rrrr rXrrc@r)Varzz Represents a prolog variable. These should use a leading uppercase. TODO: allow other naming conventions cCs||_g|_dSr)namer)rr:rrr r os z Var.__init__Nr rrrr r9irr9c@r) 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)rrrr methodzsz)TermGenerator.__getattr__..methodr)rrr=rr<r __getattr__ys zTermGenerator.__getattr__N)r r rrr>rrrr r;srr;c@s eZdZdS)RendererN)r r rrrrr r?~sr?c@r)PrologRendererz| Renders internal prolog term or program representations as strings that can be fed directly to a prolog engine csd}t|trddfdd|jD}nt|trM|jd}|jd}d}t|trAddfdd|D}n|}d ||}n~t|t rYd|j }nrt|t r|t |jdkri|j }qd |j dfd d|jD}nOt|trd dfd d|D}n:t|trddfdd|D}n%t|trd|}td|s|dddd}d|}nd|}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.0xr5rr sz)PrologRenderer.render..rz, cg|]}|qSrrCrFer5rr rHz {} :- {}z{}({})crJrrCrFar5rr rHrMz[{}]crJrrCrKr5rr rHrM({})crJrrCrKr5rr rHrM ^[a-z]\w*$'\' \n'{}'cSg|]}d|qS)z% {} formatrFrrrr rHrM) isinstancerrYjoinrrrDrtupler9r:rlenrrstrrematchreplacerrtsr8ZbodytrZcmtrr5r rDs@ "       &         zPrologRenderer.renderNr r rrrDrrrr r@ r@c@r)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|trddfdd|jD}nt|trM|jd}|jd}d}t|trAddfdd|D}n|}d ||}nst|t rYd |j }ngt|t rqd |j dfd d|jD}nOt|t rd dfdd|D}n:t|trddfdd|D}n%t|trd|}td|s|dddd}d|}nd|}t|t rt|jdkrddd|jD}||}|S)zS Renders a prolog term, program or python structure holding these. rArB crJrrCrEr5rr rHrMz.SExpressionRenderer.render..rrIcrJrrCrKr5rr rHrMz (<= {} {})z?{}z({} {})crJrrCrNr5rr rHrMz (list {})crJrrCrKr5rr rHrMrPcrJrrCrKr5rr rHrMrQrRrSrTrUrVcSrW)z; {} rXrZrrr rHrM)r[rrYr\rrrDrr]r9r:rrrr_r`rarbr^rrcrr5r rDs< "       &         zSExpressionRenderer.renderNrfrrrr rhrgrh) rr` __author__ __version__objectrrrr9r;r?r@rhrrrr s ;  0