JL imAPddlmZddlmZmZeZGddZGddeZGddZGd d eZ Gd d e Z Gd de Z GddeZ GddeZ GddZGddeZGddeZGddeZdZedk(reyy))Counter)APP LogicParserc,eZdZdZdZdZeegZeegzZy)Tokens()z-oN)__name__ __module__ __qualname__OPENCLOSEIMPPUNCTTOKENSZ/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/sem/linearlogic.pyrrs' D E C 5ME cU]Frrc:eZdZdZdZdZdZdZdZdZ dZ y ) LinearLogicParserz!A linear logic expression parser.ctj|tdtjdddi|_|xj tjgz c_y)N)r__init__rrroperator_precedenceright_associated_operationsselfs rrzLinearLogicParser.__init__s@T"$'FJJ4#C  ((VZZL8(rc"tjSN)rrrs rget_all_symbolsz!LinearLogicParser.get_all_symbols$s }}rc|tjvr|j||S|tjk(r|j ||Syr!)rrhandle_variabler handle_open)rtokcontexts rhandlezLinearLogicParser.handle'sE fmm #''W5 5 FKK ##C1 1 rc6|tjk(rtSyr!)rr ImpExpression)rr&s rget_BooleanExpression_factoryz/LinearLogicParser.get_BooleanExpression_factory-s &**  rc|||Sr!r)rfactoryfirstseconds rmake_BooleanExpressionz(LinearLogicParser.make_BooleanExpression3suf%%rc:|jt|r|jdrs|jdtj k(rQ|j|j t}|jtjt||d}|S)zAttempt to make an application expression. If the next tokens are an argument in parens, then the argument expression is a function being applied to the arguments. Otherwise, return the argument expression.rN) has_priorityrinRangetokenrr process_next_expressionassertNextTokenrApplicationExpression)r expressionr'arguments rattempt_ApplicationExpressionz/LinearLogicParser.attempt_ApplicationExpression6sq   S' *||A4::a=FKK#? 77<$$V\\22:xN rcT|djr t|St|S)Nr)isupperVariableExpressionConstantExpression)rnames rmake_VariableExpressionz)LinearLogicParser.make_VariableExpressionCs& 7?? %d+ +%d+ +rN) r r r __doc__rr"r(r+r0r:r@rrrrrs(+9 2  & ,rrc>eZdZeZedZddZdZdZ y) Expressionc8|jj|Sr!)_linear_logic_parserparse)clsss r fromstringzExpression.fromstringMs''--a00rNct|||Sr!)r7)rother other_indicess rapplytozExpression.applytoQs$T5-@@rc$|j|Sr!)rMrrKs r__call__zExpression.__call__Ts||E""rc<d|jjd|dS)N< >) __class__r rs r__repr__zExpression.__repr__Ws"4>>**+1TF!44rr!) r r r rrE classmethodrIrMrPrVrrrrCrCJs-,.11A#5rrCcFeZdZd dZd dZdZdZdZdZdZ d Z d Z y) AtomicExpressionNcLt|tsJ||_|sg}||_y)z :param name: str for the constant name :param dependencies: list of int for the indices on which this atom is dependent N) isinstancestrr? dependencies)rr?r]s rrzAtomicExpression.__init__\s+ $$$$ L(rc|r ||vr||S|S)z If 'self' is bound by 'bindings', return the atomic to which it is bound. Otherwise, return self. :param bindings: ``BindingDict`` A dictionary of bindings used to simplify :return: ``AtomicExpression`` rrbindingss rsimplifyzAtomicExpression.simplifyhs (D> !Krcg|_|gfSa3 From Iddo Lev's PhD Dissertation p108-109 :param index_counter: ``Counter`` for unique indices :param glueFormulaFactory: ``GlueFormula`` for creating new glue formulas :return: (``Expression``,set) for the compiled linear logic and any newly created glue formulas r]r index_counterglueFormulaFactorys r compile_poszAtomicExpression.compile_posubzrcg|_|gfSrcrdres r compile_negzAtomicExpression.compile_negrirc`|j|jj|_yr!)initialize_labelr?lowerrfstructs rinitialize_labelsz"AtomicExpression.initialize_labelss,,TYY__->? rcj|j|jk(xr|j|jk(Sr!)rUr?rOs r__eq__zAtomicExpression.__eq__s'~~0LTYY%**5LLrc||k( Sr!rrOs r__ne__zAtomicExpression.__ne__5=  rcZ|j}|jr|d|jzz }|SNz%s)r?r])raccums r__str__zAtomicExpression.__str__s.     TD--- -E rc,t|jSr!)hashr?rs r__hash__zAtomicExpression.__hash__sDIIrr!) r r r rrarhrkrqrsrurzr}rrrrYrY[s1 )   @M! rrYceZdZdZy)r>ct|tsJt|tr |t||fgzS||k(r|St |||#t$rYwxYw)a If 'other' is a constant, then it must be equal to 'self'. If 'other' is a variable, then it must not be bound to anything other than 'self'. :param other: ``Expression`` :param bindings: ``BindingDict`` A dictionary of all current bindings :return: ``BindingDict`` A new combined dictionary of of 'bindings' and any new binding :raise UnificationException: If 'self' and 'other' cannot be unified in the context of 'bindings' )r[rCr= BindingDictVariableBindingExceptionUnificationException)rrKr`s runifyzConstantExpression.unifysk%,,, e/ 0 +t}o">>>U]O"499 ,  sA AANr r r rrrrr>r>s:rr>ceZdZdZy)r=ct|tsJ ||k(r|S|t||fgzS#t$r}t ||||d}~wwxYw)a 'self' must not be bound to anything other than 'other'. :param other: ``Expression`` :param bindings: ``BindingDict`` A dictionary of all current bindings :return: ``BindingDict`` A new combined dictionary of of 'bindings' and the new binding :raise UnificationException: If 'self' and 'other' cannot be unified in the context of 'bindings' N)r[rCrrrrrKr`es rrzVariableExpression.unifys_%,,, Eu}+e}o">>>' E&tUH=1 D Es,, AAANrrrrr=r=sErr=cJeZdZdZd dZdZdZdZdZdZ d Z d Z d Z y) r*cht|tsJt|tsJ||_||_y)z :param antecedent: ``Expression`` for the antecedent :param consequent: ``Expression`` for the consequent N)r[rC antecedent consequent)rrrs rrzImpExpression.__init__s2 *j111*j111$$rNc|j|jj||jj|Sr!)rUrrarr_s rrazImpExpression.simplifys6~~ OO $ $X .0H0H0R  rct|tsJ ||jj|j|z|jj|j|zS#t $r}t ||||d}~wwxYw)a Both the antecedent and consequent of 'self' and 'other' must unify. :param other: ``ImpExpression`` :param bindings: ``BindingDict`` A dictionary of all current bindings :return: ``BindingDict`` A new combined dictionary of of 'bindings' and any new bindings :raise UnificationException: If 'self' and 'other' cannot be unified in the context of 'bindings' N)r[r*rrrrrrs rrzImpExpression.unifys%/// E//''(8(8(CD//''(8(8(CD  ( E&tUH=1 D EsAA$$ B-A;;Bc|jj||\}}|jj||\}}t ||||zfSrc)rrkrrhr*)rrfrgaa_newcc_news rrhzImpExpression.compile_possP__00@RS E__00@RS Ea#UU]33rc|jj||\}}|jj||\}}|j }|j j ||d|z||h}|||z|gzfS)aG From Iddo Lev's PhD Dissertation p108-109 :param index_counter: ``Counter`` for unique indices :param glueFormulaFactory: ``GlueFormula`` for creating new glue formulas :return: (``Expression``,list of ``GlueFormula``) for the compiled linear logic and any newly created glue formulas zv%s)rrhrrkgetr]append) rrfrgrrrr fresh_indexnew_vs rrkzImpExpression.compile_negs__00@RS E__00@RS E#'')  k*"5;#6K=I55=E7*++rcp|jj||jj|yr!)rrqrros rrqzImpExpression.initialize_labelss& ))'2 ))'2rc|j|jk(xr4|j|jk(xr|j|jk(Sr!)rUrrrOs rrszImpExpression.__eq__ sE NNeoo - 45#3#33 45#3#33 rc||k( Sr!rrOs rruzImpExpression.__ne__rvrcdjtj|jtj|j tj S)Nz {}{} {} {}{})formatrr rrrrrs rrzzImpExpression.__str__s8$$ KK OO JJ OO LL   rctt|jtjt|jSr!)r|rrrrrs rr}zImpExpression.__hash__s2tDOO,-fjj\$t:O9PQRRrr!) r r r rrarrhrkrqrsrurzr}rrrr*r*s6% E& 4 ,3 ! Srr*c4eZdZddZddZdZdZdZdZy) r7Nc |j}|j}t|tsJt|tsJt } t|t r||j z }t|t r||j z }||jj||z }|rft|jj|kstd|d|t|jj|k(rtd|d|||_ ||_||_y#t$r}td|d|d||d}~wwxYw)aY :param function: ``Expression`` for the function :param argument: ``Expression`` for the argument :param argument_indices: set for the indices of the glue formula from which the argument came :raise LinearLogicApplicationException: If 'function' cannot be applied to 'argument' given 'argument_indices'. z Cannot apply z to z. NzGDependencies unfulfilled when attempting to apply Linear Logic formula zZDependencies not a proper subset of indices when attempting to apply Linear Logic formula )rar[r*rCrr7r`rrrLinearLogicApplicationExceptionsetr]functionr9)rrr9argument_indices function_simp argument_simpr`rs rrzApplicationExpression.__init__"sW!))+  ))+ -777-444= ($9:H---($9:H---  0066}hO OH }//<<=@PP5$m5=++889=MM5$m5 !     +$ 1 d=/A3G  sAD++ E4E  Ech|s |j}|jj|jS)a= Since function is an implication, return its consequent. There should be no need to check that the application is valid since the checking is done by the constructor. :param bindings: ``BindingDict`` A dictionary of bindings used to simplify :return: ``Expression`` )r`rrarr_s rrazApplicationExpression.simplifyNs+}}H}}%%h/:::rc|j|jk(xr4|j|jk(xr|j|jk(Sr!)rUrr9rOs rrszApplicationExpression.__eq__\sA NNeoo - 0 / 0 / rc||k( Sr!rrOs rruzApplicationExpression.__ne__crvrcd|jztjzd|jzztjzSrx)rrr r9rrs rrzzApplicationExpression.__str__fs1dmm#fkk1D4==4HH6<<WWrctt|jtjt|jSr!)r|rrr rrs rr}zApplicationExpression.__hash__is2tDOO,-fkk]4;P:QRSSrr!) r r r rrarsrurzr}rrrr7r7!s#*!X ; !XTrr7cDeZdZd dZdZdZdZdZdZdZ d Z d Z y) rNcvi|_t|tr|j}|r|D] \}}|||< yy)z :param bindings: list [(``VariableExpression``, ``AtomicExpression``)] to initialize the dictionary dict {``VariableExpression``: ``AtomicExpression``} to initialize the dictionary N)dr[dictitems)rr`vbs rrzBindingDict.__init__nsE  h %~~'H   1Q  rct|tsJt|tsJ||k7sJ|jj |d}|r||k(r||j|<yt d|z)a A binding is consistent with the dict if its variable is not already bound, OR if its variable is already bound to its argument. :param variable: ``VariableExpression`` The variable bind :param binding: ``Expression`` The expression to which 'variable' should be bound :raise VariableBindingException: If the variable cannot be bound in this dictionary Nz*Variable %s already bound to another value)r[r=rCrrr)rvariablebindingexistings r __setitem__zBindingDict.__setitem__}st($6777':...7"""66::h-7h.&DFF8 *<I rct|tsJ|j|}|r |j|}|ryy#t$r|cYSwxYw)zD Return the expression to which 'variable' is bound N)r[r=rKeyError)rr intermediates r __getitem__zBindingDict.__getitem__sU($6777vvh'  $#vvl3  $## $s9 AAc||jvSr!r)ritems r __contains__zBindingDict.__contains__stvv~rc t}|jD]}|j|||<|jD]}|j|||<|S#t$r}td|d||d}~wwxYw)a :param other: ``BindingDict`` The dict with which to combine self :return: ``BindingDict`` A new dict containing all the elements of both parameters :raise VariableBindingException: If the parameter dictionaries are not consistent with each other z;Attempting to add two contradicting VariableBindingsLists: , N)rrr)rrKcombinedrrs r__add__zBindingDict.__add__s "}HVV ("ffQi  (WW )#ggaj  )O' *48%A  sAA A4A//A4c||k( Sr!rrOs rruzBindingDict.__ne__rvrc`t|tst|j|jk(Sr!)r[r TypeErrorrrOs rrszBindingDict.__eq__s$%-Ovv  rcddjfdtjjDzdzS)N{rc3FK|]}|dj|yw)z: Nr).0rrs r z&BindingDict.__str__..s$Rq!Btvvayk2Rs!})joinsortedrkeysrs`rrzzBindingDict.__str__s2TYYRF466;;=r=r*r7rrrrrrr rrrrs#+ 9  ,, ,,^55"@z@F:):,E)E(VSJVSrITJITXV(V(r y P9P  i  3 zFr