
    /hmA                     R   S SK Jr  S SKJrJr  \" 5       r " S S5      r " S S\5      r " S S5      r " S	 S
\5      r	 " S S\	5      r
 " S S\	5      r " S S\5      r " S S\5      r " S S5      r " S S\5      r " S S\5      r " S S\5      rS r\S:X  a  \" 5         gg)    )Counter)APPLogicParserc                   4    \ rS rSrSrSrSr\\/r\\/-   rSr	g)Tokens   ()z-o N)
__name__
__module____qualname____firstlineno__OPENCLOSEIMPPUNCTTOKENS__static_attributes__r       L/var/www/auris/envauris/lib/python3.13/site-packages/nltk/sem/linearlogic.pyr   r      s'    DE C5MEcU]Fr   r   c                   B    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
rg)LinearLogicParser   z!A linear logic expression parser.c                     [         R                  " U 5        [        S[        R                  SS S0U l        U =R                  [        R                  /-  sl        g )N         )r   __init__r   r   r   operator_precedenceright_associated_operationsselfs    r   r   LinearLogicParser.__init__   s@    T"$'FJJ4#C ((VZZL8(r   c                 "    [         R                  $ N)r   r   r"   s    r   get_all_symbols!LinearLogicParser.get_all_symbols$   s    }}r   c                     U[         R                  ;  a  U R                  X5      $ U[         R                  :X  a  U R	                  X5      $ g r&   )r   r   handle_variabler   handle_open)r#   tokcontexts      r   handleLinearLogicParser.handle'   sA    fmm#''55FKK##C11  r   c                 8    U[         R                  :X  a  [        $ g r&   )r   r   ImpExpression)r#   r,   s     r   get_BooleanExpression_factory/LinearLogicParser.get_BooleanExpression_factory-   s    &**  r   c                     U" X#5      $ r&   r   )r#   factoryfirstseconds       r   make_BooleanExpression(LinearLogicParser.make_BooleanExpression3   s    u%%r   c                 N   U R                  [        U5      (       a  U R                  S5      (       as  U R                  S5      [        R
                  :X  aP  U R                  5         U R                  [        5      nU R                  [        R                  5        [        XS5      nU$ )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.r   N)
has_priorityr   inRangetokenr   r   process_next_expressionassertNextTokenr   ApplicationExpression)r#   
expressionr-   arguments       r   attempt_ApplicationExpression/LinearLogicParser.attempt_ApplicationExpression6   st    
 S'**||A4::a=FKK#?

77<$$V\\22:N
r   c                 ^    US   R                  5       (       a  [        U5      $ [        U5      $ )Nr   )isupperVariableExpressionConstantExpression)r#   names     r   make_VariableExpression)LinearLogicParser.make_VariableExpressionC   s)    7??%d++%d++r   )r    N)r   r   r   r   __doc__r   r'   r.   r2   r8   rC   rJ   r   r   r   r   r   r      s(    +92&,r   r   c                   H    \ rS rSr\" 5       r\S 5       rSS jrS r	S r
Srg)	
ExpressionJ   c                 8    U R                   R                  U5      $ r&   )_linear_logic_parserparse)clsss     r   
fromstringExpression.fromstringM   s    ''--a00r   Nc                     [        XU5      $ r&   )r@   )r#   otherother_indicess      r   applytoExpression.applytoQ   s    $T-@@r   c                 $    U R                  U5      $ r&   )rZ   r#   rX   s     r   __call__Expression.__call__T   s    ||E""r   c                 <    SU R                   R                   SU  S3$ )N< >)	__class__r   r"   s    r   __repr__Expression.__repr__W   s"    4>>**+1TF!44r   r   r&   )r   r   r   r   r   rQ   classmethodrU   rZ   r^   re   r   r   r   r   rN   rN   J   s-    ,.1 1A#5r   rN   c                   R    \ rS rSrSS jrSS jrS rS rS rS r	S	 r
S
 rS rSrg)AtomicExpression[   Nc                 \    [        U[        5      (       d   eXl        U(       d  / nX l        g)zy
:param name: str for the constant name
:param dependencies: list of int for the indices on which this atom is dependent
N)
isinstancestrrI   dependencies)r#   rI   rn   s      r   r   AtomicExpression.__init__\   s*    
 $$$$$	L(r   c                 &    U(       a	  X;   a  X   $ U $ )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``
r   r#   bindingss     r   simplifyAtomicExpression.simplifyh   s     (>!Kr   c                     / U l         U / 4$ a  
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
rn   r#   index_counterglueFormulaFactorys      r   compile_posAtomicExpression.compile_posu        bzr   c                     / U l         U / 4$ rv   rw   rx   s      r   compile_negAtomicExpression.compile_neg   r}   r   c                 `    UR                  U R                  R                  5       5      U l        g r&   )initialize_labelrI   lowerr#   fstructs     r   initialize_labels"AtomicExpression.initialize_labels   s    ,,TYY__->?	r   c                 t    U R                   UR                   :H  =(       a    U R                  UR                  :H  $ r&   )rd   rI   r]   s     r   __eq__AtomicExpression.__eq__   s'    ~~0LTYY%**5LLr   c                     X:X  + $ r&   r   r]   s     r   __ne__AtomicExpression.__ne__         r   c                 d    U R                   nU R                  (       a  USU R                  -  -  nU$ Nz%s)rI   rn   )r#   accums     r   __str__AtomicExpression.__str__   s.    		TD----Er   c                 ,    [        U R                  5      $ r&   )hashrI   r"   s    r   __hash__AtomicExpression.__hash__   s    DIIr   )rn   rI   r&   )r   r   r   r   r   rs   r{   r   r   r   r   r   r   r   r   r   r   ri   ri   [   s1    
)		@M!r   ri   c                       \ rS rSrS rSrg)rH      c                     [        U[        5      (       d   e[        U[        5      (       a   U[        X4/5      -   $ X:X  a  U$ [        XU5      e! [         a     Nf = f)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'
)rl   rN   rG   BindingDictVariableBindingExceptionUnificationException)r#   rX   rr   s      r   unifyConstantExpression.unify   sk     %,,,,e/00+}o">>> ]O"499	 , s   A 
AAr   Nr   r   r   r   r   r   r   r   r   rH   rH      s    :r   rH   c                       \ rS rSrS rSrg)rG      c                     [        U[        5      (       d   e X:X  a  U$ U[        X4/5      -   $ ! [         a  n[	        XU5      UeSnAff = f)aX  
'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)rl   rN   r   r   r   r#   rX   rr   es       r   r   VariableExpression.unify   s\     %,,,,	E}+}o">>>' 	E&tH=1D	Es   0 0 
AAAr   Nr   r   r   r   rG   rG      s    Er   rG   c                   T    \ rS rSrS rSS jrS rS rS rS r	S	 r
S
 rS rS rSrg)r1      c                 x    [        U[        5      (       d   e[        U[        5      (       d   eXl        X l        g)zk
:param antecedent: ``Expression`` for the antecedent
:param consequent: ``Expression`` for the consequent
N)rl   rN   
antecedent
consequent)r#   r   r   s      r   r   ImpExpression.__init__   s4    
 *j1111*j1111$$r   Nc                     U R                  U R                  R                  U5      U R                  R                  U5      5      $ r&   )rd   r   rs   r   rq   s     r   rs   ImpExpression.simplify   s6    ~~OO$$X.0H0H0R
 	
r   c                    [        U[        5      (       d   e UU R                  R                  UR                  U5      -   U R                  R                  UR                  U5      -   $ ! [
         a  n[        XU5      UeSnAff = f)ah  
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)rl   r1   r   r   r   r   r   r   s       r   r   ImpExpression.unify   s     %////	E//''(8(8(CD//''(8(8(CD
 ( 	E&tH=1D	Es   AA) )
B3B  Bc                     U R                   R                  X5      u  p4U R                  R                  X5      u  pV[	        X55      XF-   4$ rv   )r   r   r   r{   r1   )r#   ry   rz   aa_newcc_news          r   r{   ImpExpression.compile_pos   sB     __00S
__00S
a#U]33r   c                     U R                   R                  X5      u  p4U R                  R                  X5      u  pVUR	                  5       nUR
                  R                  U5        U" SU-  X715      nXTU-   U/-   4$ )a  
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
zv%s)r   r{   r   r   getrn   append)	r#   ry   rz   r   r   r   r   fresh_indexnew_vs	            r   r   ImpExpression.compile_neg   sw     __00S
__00S
#'')	k*"5;#6=I5=E7*++r   c                 p    U R                   R                  U5        U R                  R                  U5        g r&   )r   r   r   r   s     r   r   ImpExpression.initialize_labels  s&    ))'2))'2r   c                     U R                   UR                   :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r&   )rd   r   r   r]   s     r   r   ImpExpression.__eq__
  sE    NNeoo- 45#3#3345#3#33	
r   c                     X:X  + $ r&   r   r]   s     r   r   ImpExpression.__ne__  r   r   c                     SR                  [        R                  U R                  [        R                  U R
                  [        R                  5      $ )Nz{}{} {} {}{})formatr   r   r   r   r   r   r"   s    r   r   ImpExpression.__str__  s8    $$KKOOJJOOLL
 	
r   c                     [        [        U R                  5       [        R                   [        U R                  5       35      $ r&   )r   r   r   r   r   r"   s    r   r   ImpExpression.__hash__  s2    tDOO,-fjj\$t:O9PQRRr   )r   r   r&   )r   r   r   r   r   rs   r   r{   r   r   r   r   r   r   r   r   r   r   r1   r1      s6    %

E&
4,3
!
Sr   r1   c                   @    \ rS rSrS
S jrS
S jrS rS rS rS r	S	r
g)r@   i!  Nc           	         UR                  5       nUR                  5       n[        U[        5      (       d   e[        U[        5      (       d   e[	        5       n [        U[
        5      (       a  XaR                  -  n[        U[
        5      (       a  XbR                  -  nXdR                  R                  XV5      -  nU(       al  [        UR                  R                  5      U:  d  [        SU< SU< 35      e[        UR                  R                  5      U:X  a  [        SU< SU< 35      eXl        X l        X`l        g! [         a  n[        SU SU SU 35      UeSnAff = f)a1  
: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 )rs   rl   r1   rN   r   r@   rr   r   r   r   LinearLogicApplicationExceptionsetrn   functionrB   )r#   r   rB   argument_indicesfunction_simpargument_simprr   r   s           r   r   ApplicationExpression.__init__"  sU    !))+ ))+-7777-4444=		($9::---($9::---0066}OOH }//<<=@PP5$m5  =++889=MM5$m5 
 !  + $ 	1d=/A3G	s   A#E 
E'E""E'c                 r    U(       d  U R                   nU R                  R                  U5      R                  $ )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``
)rr   r   rs   r   rq   s     r   rs   ApplicationExpression.simplifyN  s+     }}H}}%%h/:::r   c                     U R                   UR                   :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r&   )rd   r   rB   r]   s     r   r   ApplicationExpression.__eq__\  sA    NNeoo- 0/0/	
r   c                     X:X  + $ r&   r   r]   s     r   r   ApplicationExpression.__ne__c  r   r   c                     SU R                   -  [        R                  -   SU R                  -  -   [        R                  -   $ r   )r   r   r   rB   r   r"   s    r   r   ApplicationExpression.__str__f  s1    dmm#fkk1D4==4HH6<<WWr   c                     [        [        U R                  5       [        R                   [        U R                  5       35      $ r&   )r   r   r   r   r   r"   s    r   r   ApplicationExpression.__hash__i  s2    tDOO,-fkk]4;P:QRSSr   )rB   rr   r   r&   )r   r   r   r   r   rs   r   r   r   r   r   r   r   r   r@   r@   !  s#    *!X;
!XTr   r@   c                   N    \ rS rSrSS jrS rS rS rS rS r	S	 r
S
 rS rSrg)r   im  Nc                     0 U l         [        U[        5      (       a  UR                  5       nU(       a  U H	  u  p#X0U'   M     gg)z
:param bindings:
    list [(``VariableExpression``, ``AtomicExpression``)] to initialize the dictionary
    dict {``VariableExpression``: ``AtomicExpression``} to initialize the dictionary
N)drl   dictitems)r#   rr   vbs       r   r   BindingDict.__init__n  s?     h%%~~'H Q ! r   c                     [        U[        5      (       d   e[        U[        5      (       d   eX:w  d   eU R                  R	                  US5      nU(       a  X#:X  a  X R                  U'   g[        SU-  5      e)ac  
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)rl   rG   rN   r   r   r   )r#   variablebindingexistings       r   __setitem__BindingDict.__setitem__}  st     ($67777':...."""66::h-7.&FF8*<I r   c                     [        U[        5      (       d   eU R                  U   nU(       a   U R                  U   nU(       a  M  gg! [         a    Us $ f = f)z4
Return the expression to which 'variable' is bound
N)rl   rG   r   KeyError)r#   r   intermediates      r   __getitem__BindingDict.__getitem__  sZ     ($67777vvh'$#vvl3 l  $##$s   A	 	AAc                     XR                   ;   $ r&   r   )r#   items     r   __contains__BindingDict.__contains__  s    vv~r   c                      [        5       nU R                   H  nU R                  U   X#'   M     UR                   H  nUR                  U   X#'   M     U$ ! [         a  n[        SU < SU< 35      UeSnAff = f)z
: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)r   r   r   )r#   rX   combinedr   r   s        r   __add__BindingDict.__add__  sz    	"}HVV"ffQi WW#ggaj O' 	*48%A 	s   AA 
A9 A44A9c                     X:X  + $ r&   r   r]   s     r   r   BindingDict.__ne__  r   r   c                 j    [        U[        5      (       d  [        eU R                  UR                  :H  $ r&   )rl   r   	TypeErrorr   r]   s     r   r   BindingDict.__eq__  s'    %--Ovv  r   c                    ^  SSR                  U 4S j[        T R                  R                  5       5       5       5      -   S-   $ )N{r   c              3   J   >#    U  H  o S TR                   U    3v   M     g7f)z: Nr   ).0r   r#   s     r   	<genexpr>&BindingDict.__str__.<locals>.<genexpr>  s$     R<QqBtvvayk2<Qs    #})joinsortedr   keysr"   s   `r   r   BindingDict.__str__  s2    TYYRF466;;=<QRRRUXXXr   c                     SU -  $ )NzBindingDict: %sr   r"   s    r   re   BindingDict.__repr__  s     4''r   r   r&   )r   r   r   r   r   r   r   r   r   r   r   r   re   r   r   r   r   r   r   m  s0    .$&!!
Y(r   r   c                       \ rS rSrSrg)r   i  r   Nr   r   r   r   r   r   r   r   r   r         r   r   c                       \ rS rSrS rSrg)r   i  c           	      B    [         R                  U SU SU SU 35        g )NzCannot unify z with z given )	Exceptionr   )r#   r   r   rr   s       r   r   UnificationException.__init__  s%    4=6!GH:!NOr   r   N)r   r   r   r   r   r   r   r   r   r   r     s    Pr   r   c                       \ rS rSrSrg)r   i  r   Nr  r   r   r   r   r     r  r   r   c                     [         R                  n [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      R                  5       5        [        U " S5      R                  5       5        [        U " S5      R                  5       5        [        U " S5      R                  5       5        g )	Nfz(g -o f)z((g -o G) -o G)zg -o h -o fz(g -o f)(g)z(H -o f)(g)z((g -o G) -o G)((g -o f))z(H -o H)((g -o f)))rN   rU   printrs   )lexprs    r   demor    s    !!E	%+	%
	%"
#$	%
 	%

(
(
*+	%

(
(
*+	%,
-
6
6
89	%%
&
/
/
12r   __main__N)nltk.internalsr   nltk.sem.logicr   r   _counterr   r   rN   ri   rH   rG   r1   r@   r   r  r   r   r   r  r   r   r   r   <module>r     s    # +9	 	,, ,,^5 5"@z @F:) :,E) E(VSJ VSrITJ ITXV( V(r	y 	P9 P
	i 	
3 zF r   