
    gmA                     P   d dl mZ d dlmZmZ  e       Z G d d      Z G d de      Z G d d      Z G d	 d
e      Z	 G d de	      Z
 G d de	      Z G d de      Z G d de      Z G d d      Z G d de      Z G d de      Z G d de      Zd Zedk(  r e        yy)    )Counter)APPLogicParserc                   ,    e Zd ZdZdZdZeegZeegz   Zy)Tokens()z-oN)__name__
__module____qualname__OPENCLOSEIMPPUNCTTOKENS     I/var/www/openai/venv/lib/python3.12/site-packages/nltk/sem/linearlogic.pyr   r      s'    DE C5MEcU]Fr   r   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
LinearLogicParserz!A linear logic expression parser.c                     t        j                  |        t        dt        j                  dd di| _        | xj                  t        j                  gz  c_        y )N         )r   __init__r   r   r   operator_precedenceright_associated_operationsselfs    r   r   zLinearLogicParser.__init__   s@    T"$'FJJ4#C ((VZZL8(r   c                 "    t         j                  S N)r   r   r   s    r   get_all_symbolsz!LinearLogicParser.get_all_symbols$   s    }}r   c                     |t         j                  vr| j                  ||      S |t         j                  k(  r| j	                  ||      S y r!   )r   r   handle_variabler   handle_open)r   tokcontexts      r   handlezLinearLogicParser.handle'   sE    fmm#''W55FKK##C11  r   c                 6    |t         j                  k(  rt        S y r!   )r   r   ImpExpression)r   r&   s     r   get_BooleanExpression_factoryz/LinearLogicParser.get_BooleanExpression_factory-   s    &**  r   c                      |||      S r!   r   )r   factoryfirstseconds       r   make_BooleanExpressionz(LinearLogicParser.make_BooleanExpression3   s    uf%%r   c                 :   | j                  t        |      r| j                  d      rs| j                  d      t        j
                  k(  rQ| j                          | j                  t              }| j                  t        j                         t        ||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.r   N)
has_priorityr   inRangetokenr   r   process_next_expressionassertNextTokenr   ApplicationExpression)r   
expressionr'   arguments       r   attempt_ApplicationExpressionz/LinearLogicParser.attempt_ApplicationExpression6   sq    
 S'*||A4::a=FKK#?

77<$$V\\22:xN
r   c                 T    |d   j                         rt        |      S t        |      S )Nr   )isupperVariableExpressionConstantExpression)r   names     r   make_VariableExpressionz)LinearLogicParser.make_VariableExpressionC   s&    7??%d++%d++r   N)r
   r   r   __doc__r   r"   r(   r+   r0   r:   r@   r   r   r   r   r      s(    +92&,r   r   c                   >    e Zd Z e       Zed        ZddZd Zd Z	y)
Expressionc                 8    | j                   j                  |      S r!   )_linear_logic_parserparse)clsss     r   
fromstringzExpression.fromstringM   s    ''--a00r   Nc                     t        | ||      S r!   )r7   )r   otherother_indicess      r   applytozExpression.applytoQ   s    $T5-@@r   c                 $    | j                  |      S r!   )rM   r   rK   s     r   __call__zExpression.__call__T   s    ||E""r   c                 <    d| j                   j                   d|  dS )N< >)	__class__r
   r   s    r   __repr__zExpression.__repr__W   s"    4>>**+1TF!44r   r!   )
r
   r   r   r   rE   classmethodrI   rM   rP   rV   r   r   r   rC   rC   J   s-    ,.1 1A#5r   rC   c                   F    e Zd ZddZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zy)AtomicExpressionNc                 L    t        |t              sJ || _        |sg }|| _        y)z
        :param name: str for the constant name
        :param dependencies: list of int for the indices on which this atom is dependent
        N)
isinstancestrr?   dependencies)r   r?   r]   s      r   r   zAtomicExpression.__init__\   s+    
 $$$$	L(r   c                     |r	| |v r||    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``
        r   r   bindingss     r   simplifyzAtomicExpression.simplifyh   s     (D>!Kr   c                     g | _         | g fS a3  
        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_counterglueFormulaFactorys      r   compile_poszAtomicExpression.compile_posu        bzr   c                     g | _         | g fS rc   rd   re   s      r   compile_negzAtomicExpression.compile_neg   ri   r   c                 `    |j                  | j                  j                               | _        y r!   )initialize_labelr?   lowerr   fstructs     r   initialize_labelsz"AtomicExpression.initialize_labels   s    ,,TYY__->?	r   c                 j    | j                   |j                   k(  xr | j                  |j                  k(  S r!   )rU   r?   rO   s     r   __eq__zAtomicExpression.__eq__   s'    ~~0LTYY%**5LLr   c                     | |k(   S r!   r   rO   s     r   __ne__zAtomicExpression.__ne__       5=  r   c                 Z    | j                   }| j                  r|d| j                  z  z  }|S Nz%s)r?   r]   )r   accums     r   __str__zAtomicExpression.__str__   s.    		TD----Er   c                 ,    t        | j                        S r!   )hashr?   r   s    r   __hash__zAtomicExpression.__hash__   s    DIIr   r!   )r
   r   r   r   ra   rh   rk   rq   rs   ru   rz   r}   r   r   r   rY   rY   [   s1    
)		@M!r   rY   c                       e Zd Zd Zy)r>   c                     t        |t              sJ t        |t              r	 |t        || fg      z   S | |k(  r|S t        | ||      # t        $ r Y w xY w)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[   rC   r=   BindingDictVariableBindingExceptionUnificationException)r   rK   r`   s      r   unifyzConstantExpression.unify   sk     %,,,e/0+t}o">>> U]O"499	 , s   A	 		AANr
   r   r   r   r   r   r   r>   r>      s    :r   r>   c                       e Zd Zd Zy)r=   c                     t        |t              sJ 	 | |k(  r|S |t        | |fg      z   S # t        $ r}t	        | ||      |d}~ww xY w)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[   rC   r   r   r   r   rK   r`   es       r   r   zVariableExpression.unify   s_     %,,,	Eu}+e}o">>>' 	E&tUH=1D	Es   , , 	AAANr   r   r   r   r=   r=      s    Er   r=   c                   J    e Zd Zd ZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zy)r*   c                 h    t        |t              sJ t        |t              sJ || _        || _        y)z
        :param antecedent: ``Expression`` for the antecedent
        :param consequent: ``Expression`` for the consequent
        N)r[   rC   
antecedent
consequent)r   r   r   s      r   r   zImpExpression.__init__   s2    
 *j111*j111$$r   Nc                     | j                  | j                  j                  |      | j                  j                  |            S r!   )rU   r   ra   r   r_   s     r   ra   zImpExpression.simplify   s6    ~~OO$$X.0H0H0R
 	
r   c                    t        |t              sJ 	 || j                  j                  |j                  |      z   | j                  j                  |j                  |      z   S # t
        $ r}t        | ||      |d}~ww xY w)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*   r   r   r   r   r   r   s       r   r   zImpExpression.unify   s     %///	E//''(8(8(CD//''(8(8(CD
 ( 	E&tUH=1D	Es   AA$ $	B -A;;B c                     | j                   j                  ||      \  }}| j                  j                  ||      \  }}t	        ||      ||z   fS rc   )r   rk   r   rh   r*   )r   rf   rg   aa_newcc_news          r   rh   zImpExpression.compile_pos   sP     __00@RS
E__00@RS
Ea#UU]33r   c                    | j                   j                  ||      \  }}| j                  j                  ||      \  }}|j	                         }|j
                  j                  |        |d|z  ||h      }|||z   |gz   fS )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
        zv%s)r   rh   r   rk   getr]   append)	r   rf   rg   r   r   r   r   fresh_indexnew_vs	            r   rk   zImpExpression.compile_neg   s     __00@RS
E__00@RS
E#'')	k*"5;#6K=I55=E7*++r   c                 p    | j                   j                  |       | j                  j                  |       y r!   )r   rq   r   ro   s     r   rq   zImpExpression.initialize_labels  s&    ))'2))'2r   c                     | j                   |j                   k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r!   )rU   r   r   rO   s     r   rs   zImpExpression.__eq__
  sE    NNeoo- 45#3#3345#3#33	
r   c                     | |k(   S r!   r   rO   s     r   ru   zImpExpression.__ne__  rv   r   c                     dj                  t        j                  | j                  t        j                  | j
                  t        j                        S )Nz{}{} {} {}{})formatr   r   r   r   r   r   r   s    r   rz   zImpExpression.__str__  s8    $$KKOOJJOOLL
 	
r   c                     t        t        | j                         t        j                   t        | j                               S r!   )r|   r   r   r   r   r   s    r   r}   zImpExpression.__hash__  s2    tDOO,-fjj\$t:O9PQRRr   r!   )r
   r   r   r   ra   r   rh   rk   rq   rs   ru   rz   r}   r   r   r   r*   r*      s6    %

E&
4,3
!
Sr   r*   c                   4    e Zd ZddZddZd Zd Zd Zd Zy)	r7   Nc           	         |j                         }|j                         }t        |t              sJ t        |t              sJ t	               }	 t        |t
              r||j                  z  }t        |t
              r||j                  z  }||j                  j                  ||      z  }|rft        |j                  j                        |k  st        d|d|      t        |j                  j                        |k(  rt        d|d|      || _        || _        || _        y# t        $ r}t        d| d| d|       |d}~ww xY w)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 )ra   r[   r*   rC   r   r7   r`   r   r   r   LinearLogicApplicationExceptionsetr]   functionr9   )r   r   r9   argument_indicesfunction_simpargument_simpr`   r   s           r   r   zApplicationExpression.__init__"  sW    !))+ ))+-777-444=		($9:H---($9:H---0066}hOOH }//<<=@PP5$m5  =++889=MM5$m5 
 !  + $ 	1d=/A3G	s   AD+ +	E4E		Ec                 h    |s| j                   }| j                  j                  |      j                  S )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`   r   ra   r   r_   s     r   ra   zApplicationExpression.simplifyN  s+     }}H}}%%h/:::r   c                     | j                   |j                   k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r!   )rU   r   r9   rO   s     r   rs   zApplicationExpression.__eq__\  sA    NNeoo- 0/0/	
r   c                     | |k(   S r!   r   rO   s     r   ru   zApplicationExpression.__ne__c  rv   r   c                     d| j                   z  t        j                  z   d| j                  z  z   t        j                  z   S rx   )r   r   r   r9   r   r   s    r   rz   zApplicationExpression.__str__f  s1    dmm#fkk1D4==4HH6<<WWr   c                     t        t        | j                         t        j                   t        | j                               S r!   )r|   r   r   r   r   r   s    r   r}   zApplicationExpression.__hash__i  s2    tDOO,-fkk]4;P:QRSSr   r!   )	r
   r   r   r   ra   rs   ru   rz   r}   r   r   r   r7   r7   !  s#    *!X;
!XTr   r7   c                   D    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zy)r   Nc                 v    i | _         t        |t              r|j                         }|r|D ]
  \  }}|| |<    yy)z
        :param bindings:
            list [(``VariableExpression``, ``AtomicExpression``)] to initialize the dictionary
            dict {``VariableExpression``: ``AtomicExpression``} to initialize the dictionary
        N)dr[   dictitems)r   r`   vbs       r   r   zBindingDict.__init__n  s@     h%~~'H 1Q ! r   c                     t        |t              sJ t        |t              sJ ||k7  sJ | j                  j	                  |d      }|r||k(  r|| j                  |<   yt        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=   rC   r   r   r   )r   variablebindingexistings       r   __setitem__zBindingDict.__setitem__}  st     ($6777':...7"""66::h-7h.&DFF8*<I r   c                     t        |t              sJ | j                  |   }|r	 | j                  |   }|ryy# t        $ r |cY S w xY w)zD
        Return the expression to which 'variable' is bound
        N)r[   r=   r   KeyError)r   r   intermediates      r   __getitem__zBindingDict.__getitem__  sU     ($6777vvh'$#vvl3   $##$s   9 AAc                     || j                   v S r!   r   )r   items     r   __contains__zBindingDict.__contains__  s    tvv~r   c                     	 t               }| j                  D ]  }| j                  |   ||<    |j                  D ]  }|j                  |   ||<    |S # t        $ r}t        d| d|      |d}~ww xY w)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)r   r   r   )r   rK   combinedr   r   s        r   __add__zBindingDict.__add__  s~    	"}HVV"ffQi WW#ggaj O' 	*48%A 	s   AA 	A4A//A4c                     | |k(   S r!   r   rO   s     r   ru   zBindingDict.__ne__  rv   r   c                 `    t        |t              st        | j                  |j                  k(  S r!   )r[   r   	TypeErrorr   rO   s     r   rs   zBindingDict.__eq__  s$    %-Ovv  r   c                      ddj                   fdt         j                  j                               D              z   dz   S )N{r   c              3   F   K   | ]  }| d j                   |      yw)z: Nr   ).0r   r   s     r   	<genexpr>z&BindingDict.__str__.<locals>.<genexpr>  s'     R<Qq!Btvvayk2<Qs   !})joinsortedr   keysr   s   `r   rz   zBindingDict.__str__  s2    TYYRF466;;=<QRRRUXXXr   c                     d| z  S )NzBindingDict: %sr   r   s    r   rV   zBindingDict.__repr__  s     4''r   r!   )r
   r   r   r   r   r   r   r   ru   rs   rz   rV   r   r   r   r   r   m  s0    .$&!!
Y(r   r   c                       e Zd Zy)r   Nr
   r   r   r   r   r   r   r         r   r   c                       e Zd Zd Zy)r   c           	      B    t         j                  | d| d| d|        y )NzCannot unify z with z given )	Exceptionr   )r   r   r   r`   s       r   r   zUnificationException.__init__  s%    4=6!GH:!NOr   N)r
   r   r   r   r   r   r   r   r     s    Pr   r   c                       e Zd Zy)r   Nr   r   r   r   r   r     r   r   r   c                     t         j                  } t         | d             t         | d             t         | d             t         | d             t         | d      j                                t         | d      j                                t         | d      j                                t         | d      j                                y )	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)))rC   rI   printra   )lexprs    r   demor     s    !!E	%+	%
	%"
#$	%
 	%

(
(
*+	%

(
(
*+	%,
-
6
6
89	%%
&
/
/
12r   __main__N)nltk.internalsr   nltk.sem.logicr   r   _counterr   r   rC   rY   r>   r=   r*   r7   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   