ó <¿CVc@s¤ddlmZmZddlmZddlmZmZddlm Z m Z eƒZ de fd„ƒYZ de fd„ƒYZed e fd „ƒYƒZed efd „ƒYƒZd efd„ƒYZdefd„ƒYZedefd„ƒYƒZedefd„ƒYƒZede fd„ƒYƒZdefd„ƒYZdefd„ƒYZdefd„ƒYZd„Zedkr eƒndS( iÿÿÿÿ(tprint_functiontunicode_literals(tCounter(t string_typestpython_2_unicode_compatible(t LogicParsertAPPtTokenscBs3eZdZdZdZeegZeegZRS(u(u)u-o(t__name__t __module__tOPENtCLOSEtIMPtPUNCTtTOKENS(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRs  tLinearLogicParsercBsMeZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z RS(u!A linear logic expression parser.cCsGtj|ƒidt6dtj6dd6|_|jtjg7_dS(Niii(Rt__init__RRR tNonetoperator_precedencetright_associated_operations(tself((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRs !cCstjS(N(RR(R((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytget_all_symbols#scCsB|tjkr|j||ƒS|tjkr>|j||ƒSdS(N(RRthandle_variableR t handle_open(Rttoktcontext((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pythandle&scCs|tjkrtSdSdS(N(RR t ImpExpressionR(RR((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytget_BooleanExpression_factory,scCs |||ƒS(N((Rtfactorytfirsttsecond((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytmake_BooleanExpression2scCs~|jt|ƒrz|jdƒrz|jdƒtjkrz|jƒ|jtƒ}|jtjƒt ||dƒ}qzn|S(uèAttempt 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.iN( t has_priorityRtinRangettokenRR tprocess_next_expressiontassertNextTokenR tApplicationExpressionR(Rt expressionRtargument((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytattempt_ApplicationExpression5s' cCs(|djƒrt|ƒSt|ƒSdS(Ni(tisuppertVariableExpressiontConstantExpression(Rtname((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytmake_VariableExpressionBs ( RR t__doc__RRRRR R)R.(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRs      t ExpressioncBs>eZeƒZed„ƒZdd„Zd„Zd„Z RS(cCs|jj|ƒS(N(t_linear_logic_parsertparse(tclsts((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt fromstringNscCst|||ƒS(N(R&(Rtothert other_indices((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytapplytoRscCs |j|ƒS(N(R8(RR6((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt__call__UscCsd|jj|fS(Nu<%s %s>(t __class__R(R((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt__repr__XsN( RR RR1t classmethodR5RR8R9R;(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR0Is    tAtomicExpressioncBs_eZd d„Zd d„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z RS( cCs:t|tƒst‚||_|s-g}n||_dS(u‘ :param name: str for the constant name :param dependencies: list of int for the indices on which this atom is dependent N(t isinstanceRtAssertionErrorR-t dependencies(RR-R@((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR^s   cCs"|r||kr||S|SdS(uõ 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`` N((Rtbindings((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytsimplifyjscCsg|_|gfS(u3 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@(Rt index_countertglueFormulaFactory((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt compile_posws cCsg|_|gfS(u3 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@(RRCRD((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt compile_neg‚s cCs|j|jjƒƒ|_dS(N(tinitialize_labelR-tlower(Rtfstruct((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytinitialize_labelsscCs"|j|jko!|j|jkS(N(R:R-(RR6((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt__eq__scCs ||k S(N((RR6((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt__ne__“scCs*|j}|jr&|d|j7}n|S(Nu%s(R-R@(Rtaccum((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt__str__–s  cCs t|jƒS(N(thashR-(R((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt__hash__œsN( RR RRRBRERFRJRKRLRNRP(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR=\s    R,cBseZd„ZRS(cCs|t|tƒst‚t|tƒrVy|t||fgƒSWqftk rRqfXn||krf|St|||ƒ‚dS(uæ 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' N(R>R0R?R+t BindingDicttVariableBindingExceptiontUnificationException(RR6RA((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytunify s   (RR RT(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR,ŸsR+cBseZd„ZRS(cCsjt|tƒst‚y+||kr(|S|t||fgƒSWn#tk ret|||ƒ‚nXdS(uˆ '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>R0R?RQRRRS(RR6RA((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRTµs   (RR RT(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR+´sRcBseeZd„Zd d„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z RS( cCs@t|tƒst‚t|tƒs*t‚||_||_dS(uƒ :param antecedent: ``Expression`` for the antecedent :param consequent: ``Expression`` for the consequent N(R>R0R?t antecedentt consequent(RRURV((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRÉs cCs(|j|jj|ƒ|jj|ƒƒS(N(R:RURBRV(RRA((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRBÓscCsst|tƒst‚y4||jj|j|ƒ|jj|j|ƒSWn#tk rnt|||ƒ‚nXdS(u˜ 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>RR?RURTRVRRRS(RR6RA((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRTÖs 4 cCsM|jj||ƒ\}}|jj||ƒ\}}t||ƒ||fS(u3 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 (RURFRVRER(RRCRDtata_newtctc_new((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyREåsc Cs†|jj||ƒ\}}|jj||ƒ\}}|jƒ}|jj|ƒ|d||t|gƒƒ}||||gfS(uG 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 uv%s(RURERVRFtgetR@tappendtset( RRCRDRWRXRYRZt fresh_indextnew_v((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRFñs  cCs$|jj|ƒ|jj|ƒdS(N(RURJRV(RRI((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRJscCs4|j|jko3|j|jko3|j|jkS(N(R:RURV(RR6((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRKscCs ||k S(N((RR6((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRLscCs&dtj|jtj|jtjfS(Nu %s%s %s %s%s(RR RUR RVR (R((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRN scCs,tdt|jƒtjt|jƒfƒS(Nu%s%s%s(RORURR RV(R((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRPsN( RR RRRBRTRERFRJRKRLRNRP(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRÇs       R&cBsDeZdd„Zdd„Zd„Zd„Zd„Zd„ZRS(cCs_|jƒ}|jƒ}t|tƒs-t‚t|tƒsBt‚tƒ}y[t|tƒrm||j7}nt|tƒrŒ||j7}n||jj ||ƒ7}Wn,t k rÔ}t d|||fƒ‚nX|r@t |jj ƒ|ks t d||fƒ‚nt |jj ƒ|kr@t d||fƒ‚q@n||_||_||_dS(uY :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'. uCannot apply %s to %s. %suODependencies unfulfilled when attempting to apply Linear Logic formula %s to %subDependencies not a proper subset of indices when attempting to apply Linear Logic formula %s to %sN(RBR>RR?R0RQR&RARURTRStLinearLogicApplicationExceptionR]R@tfunctionR((RRaR(targument_indicest function_simpt argument_simpRAte((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRs*     cCs%|s|j}n|jj|ƒjS(u= 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`` (RARaRBRV(RRA((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRB8s  cCs4|j|jko3|j|jko3|j|jkS(N(R:RaR((RR6((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRKFscCs ||k S(N((RR6((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRLJscCs$d|jtjd|jtjS(Nu%s(RaRR R(R (R((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRNMscCs,tdt|jƒtjt|jƒfƒS(Nu%s%s%s(RORURR RV(R((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRPPsN( RR RRRBRKRLRNRP(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR&s  $    RQcBs\eZd d„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z RS( cCsUi|_t|tƒr'|jƒ}n|rQx!|D]\}}|||tdicttitems(RRAtvtb((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRUs  cCsˆt|tƒst‚t|tƒs*t‚||ks<t‚|jj|dƒ}| sd||krt||j|R+R?R0RfR[RRR(Rtvariabletbindingtexisting((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt __setitem__ds cCsYt|tƒst‚|j|}x0|rTy|j|}Wq%tk rP|SXq%WdS(uD Return the expression to which 'variable' is bound N(R>R+R?RftKeyError(RRkt intermediate((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt __getitem__ys   cCs ||jkS(N(Rf(Rtitem((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyt __contains__†scCs‰y[tƒ}x"|jD]}|j|||RQt TypeErrorRf(RR6((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRKs cs(ddj‡fd†ˆjDƒƒdS(Nu{u, c3s&|]}d|ˆj|fVqdS(u%s: %sN(Rf(t.0Ri(R(sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pys £su}(tjoinRf(R((Rsf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRN¢scCsd|S(NuBindingDict: %s((R((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR;¥sN( RR RRRnRqRsRuRLRKRNR;(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRQSs       RRcBseZRS((RR (((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRR¨sRScBseZd„ZRS(cCs!tj|d|||fƒdS(Nu Cannot unify %s with %s given %s(t ExceptionR(RRWRjRA((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR¬s(RR R(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyRS«sR`cBseZRS((RR (((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyR`¯scCs¥tj}t|dƒƒt|dƒƒt|dƒƒt|dƒƒt|dƒjƒƒt|dƒjƒƒt|dƒjƒƒt|dƒjƒƒdS( Nufu(g -o f)u((g -o G) -o G)u g -o h -o fu (g -o f)(g)u (H -o f)(g)u((g -o G) -o G)((g -o f))u(H -o H)((g -o f))(R0R5tprintRB(tlexpr((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pytdemo³s u__main__N(t __future__RRtnltk.internalsRt nltk.compatRRtnltk.sem.logicRRt_countertobjectRRR0R=R,R+RR&RQRyRRRSR`R|R(((sf/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/linearlogic.pyts0  .BJ@T