
    NgmA                        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             dS dS )    )Counter)APPLogicParserc                   .    e Zd ZdZdZdZeegZeegz   ZdS )Tokens()z-oN)__name__
__module____qualname__OPENCLOSEIMPPUNCTTOKENS     P/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/sem/linearlogic.pyr   r      s3        DE C5MEcU]FFFr   r   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	S )
LinearLogicParserz!A linear logic expression parser.c                     t          j        |            t          dt          j        dd di| _        | xj        t          j        gz  c_        d S )N         )r   __init__r   r   r   operator_precedenceright_associated_operationsselfs    r   r   zLinearLogicParser.__init__   sH    T"""$'FJ4#C ((VZL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|                     ||          S |t           j        k    r|                     ||          S d S r!   )r   r   handle_variabler   handle_open)r   tokcontexts      r   handlezLinearLogicParser.handle'   sP    fm##''W555FK##C111  r   c                 4    |t           j        k    rt          S d S r!   )r   r   ImpExpression)r   r&   s     r   get_BooleanExpression_factoryz/LinearLogicParser.get_BooleanExpression_factory-   s    &*  4r   c                      |||          S r!   r   )r   factoryfirstseconds       r   make_BooleanExpressionz(LinearLogicParser.make_BooleanExpression3   s    wuf%%%r   c                 h   |                      t          |          r|                     d          r|                     d          t          j        k    r^|                                  |                     t                    }|                     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   s    
 S'** 	O||A O4::a==FK#?#?

77<<$$V\2222:xNN
r   c                 r    |d                                          rt          |          S t          |          S )Nr   )isupperVariableExpressionConstantExpression)r   names     r   make_VariableExpressionz)LinearLogicParser.make_VariableExpressionC   s4    7?? 	,%d+++%d+++r   N)r
   r   r   __doc__r   r"   r(   r+   r0   r:   r@   r   r   r   r   r      s        ++9 9 9  2 2 2  & & &  , , , , ,r   r   c                   L    e Zd Z e            Zed             ZddZd Zd Z	dS )
Expressionc                 6    | j                             |          S r!   )_linear_logic_parserparse)clsss     r   
fromstringzExpression.fromstringM   s    '--a000r   Nc                 $    t          | ||          S r!   )r7   )r   otherother_indicess      r   applytozExpression.applytoQ   s    $T5-@@@r   c                 ,    |                      |          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     44>*44T4444r   r!   )
r
   r   r   r   rE   classmethodrI   rM   rP   rV   r   r   r   rC   rC   J   sr        ,,..1 1 [1A A A A# # #5 5 5 5 5r   rC   c                   H    e Zd ZddZddZd Zd Zd Zd Zd Z	d	 Z
d
 ZdS )AtomicExpressionNc                 X    t          |t                    sJ || _        |sg }|| _        dS )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                 h    |                     | j                                                  | _        d S r!   )initialize_labelr?   lowerr   fstructs     r   initialize_labelsz"AtomicExpression.initialize_labels   s'    ,,TY__->->??			r   c                 B    | j         |j         k    o| j        |j        k    S r!   )rU   r?   rO   s     r   __eq__zAtomicExpression.__eq__   s    ~0LTY%*5LLr   c                     | |k     S r!   r   rO   s     r   __ne__zAtomicExpression.__ne__       5=  r   c                 <    | 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    DIr   r!   )r
   r   r   r   ra   rh   rk   rq   rs   ru   rz   r}   r   r   r   rY   rY   [   s        
) 
) 
) 
)   	 	 		 	 	@ @ @M M M! ! !      r   rY   c                       e Zd Zd ZdS )r>   c                     t          |t                    sJ t          |t                    r&	 |t          || fg          z   S # t          $ r Y nw xY w| |k    r|S t          | ||          )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   s     %,,,,,e/00 	+t}o">">>>+   U]]O"4999s   A 
AANr
   r   r   r   r   r   r   r>   r>      s#        : : : : :r   r>   c                       e Zd Zd ZdS )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 	E 	E&tUH==1D	Es   6 6 
A AANr   r   r   r   r=   r=      s(        E E E E Er   r=   c                   L    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dS )r*   c                 ~    t          |t                    sJ t          |t                    sJ || _        || _        dS )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__   sB    
 *j11111*j11111$$r   Nc                     |                      | j                            |          | j                            |                    S r!   )rU   r   ra   r   r_   s     r   ra   zImpExpression.simplify   s=    ~~O$$X..0H0H0R0R
 
 	
r   c                    t          |t                    sJ 	 || j                            |j        |          z   | 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(CCD/''(8(CCD
 ( 	E 	E 	E&tUH==1D	Es   AA 
A>'A99A>c                     | 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   sU     _00@RSS
E_00@RSS
Ea##UU]33r   c                    | 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S
E_00@RSS
E#''))	k***""5;#6K=II55=E7*++r   c                 n    | j                             |           | j                            |           d S r!   )r   rq   r   ro   s     r   rq   zImpExpression.initialize_labels  s4    ))'222))'22222r   c                 b    | j         |j         k    o| j        |j        k    o| j        |j        k    S r!   )rU   r   r   rO   s     r   rs   zImpExpression.__eq__
  s7    Neo- 45#3345#33	
r   c                     | |k     S r!   r   rO   s     r   ru   zImpExpression.__ne__  rv   r   c                     d                     t          j        | j        t          j        | j        t          j                  S )Nz{}{} {} {}{})formatr   r   r   r   r   r   r   s    r   rz   zImpExpression.__str__  s3    $$KOJOL
 
 	
r   c                     t          t          | j                   t          j         t          | j                             S r!   )r|   r   r   r   r   r   s    r   r}   zImpExpression.__hash__  s6    tDO,,QfjQ$t:O:OQQRRRr   r!   )r
   r   r   r   ra   r   rh   rk   rq   rs   ru   rz   r}   r   r   r   r*   r*      s        % % %
 
 
 

E E E&
4 
4 
4, , ,3 3 3
 
 
! ! !
 
 
S S S S Sr   r*   c                   6    e Zd ZddZddZd Zd Zd Zd ZdS )	r7   Nc           	         |                                 }|                                 }t          |t                    sJ t          |t                    sJ t	                      }	 t          |t
                    r
||j        z  }t          |t
                    r
||j        z  }||j                            ||          z  }n+# t          $ r}t          d| d| d|           |d}~ww xY w|rdt          |j        j                  |k     st          d|d|          t          |j        j                  |k    rt          d|d|          || _        || _        || _        dS )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__"  s    !))++ ))++-77777-44444==		($9:: .H--($9:: .H--066}hOOOHH# 	 	 	1GGG=GGAGG 	  	}/<==@PPP55$}}mm5   =+899=MMM55$}}mm5  
 !  s   &AC 
C+C&&C+c                 R    |s| 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                 b    | j         |j         k    o| j        |j        k    o| j        |j        k    S r!   )rU   r   r9   rO   s     r   rs   zApplicationExpression.__eq__\  s5    Neo- 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  s)    dm#fk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  s6    tDO,,RfkR4;P;PRRSSSr   r!   )	r
   r   r   r   ra   rs   ru   rz   r}   r   r   r   r7   r7   !  s        *! *! *! *!X; ; ; ;
 
 
! ! !X X XT T T T Tr   r7   c                   F    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 ZdS )r   Nc                     i | _         t          |t                    r|                                }|r|D ]\  }}|| |<   dS dS )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  sf     h%% 	(~~''H 	   1Q	 	 r   c                     t          |t                    sJ t          |t                    sJ ||k    sJ | j                            |d          }|r||k    r|| j        |<   dS 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__}  s     ($677777':.....7""""6::h-- 	7h..&DF8*<I  r   c                     t          |t                    sJ | j        |         }|r%	 | j        |         }n# t          $ r |cY S w xY w|#dS dS )zD
        Return the expression to which 'variable' is bound
        N)r[   r=   r   KeyError)r   r   intermediates      r   __getitem__zBindingDict.__getitem__  s     ($677777vh' 	$$#vl3 $ $ $####$  	$ 	$ 	$ 	$ 	$s   6 AAc                     || j         v S r!   r   )r   items     r   __contains__zBindingDict.__contains__  s    tv~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V ( ("fQiW ) )#gajO' 	 	 	**48DD%%A  	s   AA 
A+A&&A+c                     | |k     S r!   r   rO   s     r   ru   zBindingDict.__ne__  rv   r   c                 Z    t          |t                    st          | j        |j        k    S r!   )r[   r   	TypeErrorr   rO   s     r   rs   zBindingDict.__eq__  s(    %-- 	Ov  r   c                      dd                      fdt           j                                                  D                       z   dz   S )N{r   c              3   <   K   | ]}| d j         |          V  dS )z: Nr   ).0r   r   s     r   	<genexpr>z&BindingDict.__str__.<locals>.<genexpr>  s7      RRq!22tvay22RRRRRRr   })joinsortedr   keysr   s   `r   rz   zBindingDict.__str__  sG    TYYRRRRF46;;==<Q<QRRR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  s             .$ $ $    &! ! !! ! !
Y Y Y( ( ( ( (r   r   c                       e Zd ZdS )r   Nr
   r   r   r   r   r   r   r             Dr   r   c                       e Zd Zd ZdS )r   c           	      N    t                               | d| d| d|            d S )NzCannot unify z with z given )	Exceptionr   )r   r   r   r`   s       r   r   zUnificationException.__init__  s7    4!N!N!N!!N!NH!N!NOOOOOr   N)r
   r   r   r   r   r   r   r   r     s(        P P P P Pr   r   c                       e Zd ZdS )r   Nr   r   r   r   r   r     r   r   r   c                  .   t           j        } t           | d                     t           | d                     t           | d                     t           | d                     t           | d                                                     t           | d                                                     t           | d                                                     t           | d                                                     d S )	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
8999	%%%
&
&
/
/
1
12222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   # " " " " " + + + + + + + +799	 	 	 	 	 	 	 	,, ,, ,, ,, ,, ,, ,, ,,^5 5 5 5 5 5 5 5"@ @ @ @ @z @ @ @F: : : : :) : : :,E E E E E) E E E(VS VS VS VS VSJ VS VS VSrIT IT IT IT ITJ IT IT ITXV( V( V( V( V( V( V( V(r	 	 	 	 	y 	 	 	P P P P P9 P P P
	 	 	 	 	i 	 	 	
3 
3 
3 zDFFFFF r   