a
     ˜ëjmA  ã                   @   sö   d dl mZ d dlmZmZ eƒ ZG dd„ dƒZG dd„ deƒZG dd„ dƒZG d	d
„ d
eƒZ	G dd„ de	ƒZ
G dd„ de	ƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zedkròeƒ  dS )é    )ÚCounter)ÚAPPÚLogicParserc                   @   s*   e Zd ZdZdZdZeegZeeg ZdS )ÚTokensú(ú)z-oN)Ú__name__Ú
__module__Ú__qualname__ÚOPENÚCLOSEÚIMPZPUNCTÚTOKENS© r   r   úS/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/sem/linearlogic.pyr      s
   r   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚLinearLogicParserz!A linear logic expression parser.c                 C   s4   t  | ¡ tdtjdd di| _|  jtjg7  _d S )Né   é   é   )r   Ú__init__r   r   r   Úoperator_precedenceZright_associated_operations©Úselfr   r   r   r      s    
zLinearLogicParser.__init__c                 C   s   t jS ©N)r   r   r   r   r   r   Úget_all_symbols$   s    z!LinearLogicParser.get_all_symbolsc                 C   s0   |t jvr|  ||¡S |t jkr,|  ||¡S d S r   )r   r   Zhandle_variabler   Úhandle_open)r   ÚtokÚcontextr   r   r   Úhandle'   s    

zLinearLogicParser.handlec                 C   s   |t jkrtS d S d S r   )r   r   ÚImpExpression)r   r   r   r   r   Úget_BooleanExpression_factory-   s    
z/LinearLogicParser.get_BooleanExpression_factoryc                 C   s
   |||ƒS r   r   )r   ÚfactoryÚfirstÚsecondr   r   r   Úmake_BooleanExpression3   s    z(LinearLogicParser.make_BooleanExpressionc                 C   sT   |   t|¡rP|  d¡rP|  d¡tjkrP|  ¡  |  t¡}|  tj¡ t	||dƒ}|S )zè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.r   N)
Zhas_priorityr   ZinRangeÚtokenr   r   Zprocess_next_expressionZassertNextTokenr   ÚApplicationExpression)r   Z
expressionr   Úargumentr   r   r   Úattempt_ApplicationExpression6   s    
z/LinearLogicParser.attempt_ApplicationExpressionc                 C   s    |d   ¡ rt|ƒS t|ƒS d S )Nr   )ÚisupperÚVariableExpressionÚConstantExpression)r   Únamer   r   r   Úmake_VariableExpressionC   s    z)LinearLogicParser.make_VariableExpressionN)r   r	   r
   Ú__doc__r   r   r   r    r$   r(   r-   r   r   r   r   r      s   r   c                   @   s8   e Zd Zeƒ Zedd„ ƒZd
dd„Zdd„ Zdd	„ Z	dS )Ú
Expressionc                 C   s   | j  |¡S r   )Ú_linear_logic_parserÚparse)ÚclsÚsr   r   r   Ú
fromstringM   s    zExpression.fromstringNc                 C   s   t | ||ƒS r   )r&   )r   ÚotherZother_indicesr   r   r   ÚapplytoQ   s    zExpression.applytoc                 C   s
   |   |¡S r   )r6   ©r   r5   r   r   r   Ú__call__T   s    zExpression.__call__c                 C   s   d| j j› d| › d�S )Nú<ú ú>)Ú	__class__r   r   r   r   r   Ú__repr__W   s    zExpression.__repr__)N)
r   r	   r
   r   r0   Úclassmethodr4   r6   r8   r=   r   r   r   r   r/   J   s   

r/   c                   @   sX   e Zd Zddd„Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚAtomicExpressionNc                 C   s&   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,   rB   r   r   r   r   \   s
    zAtomicExpression.__init__c                 C   s   |r| |v r||  S | S d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``
        Nr   ©r   Úbindingsr   r   r   Úsimplifyh   s    zAtomicExpression.simplifyc                 C   s   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
        ©rB   ©r   Úindex_counterÚglueFormulaFactoryr   r   r   Úcompile_posu   s    zAtomicExpression.compile_posc                 C   s   g | _ | g fS rF   rG   rH   r   r   r   Úcompile_neg€   s    zAtomicExpression.compile_negc                 C   s   |  | j ¡ ¡| _d S r   )Zinitialize_labelr,   Úlower©r   Zfstructr   r   r   Úinitialize_labels‹   s    z"AtomicExpression.initialize_labelsc                 C   s   | j |j ko| j|jkS r   )r<   r,   r7   r   r   r   Ú__eq__Ž   s    zAtomicExpression.__eq__c                 C   s
   | |k S r   r   r7   r   r   r   Ú__ne__‘   s    zAtomicExpression.__ne__c                 C   s   | j }| jr|d| j 7 }|S ©Nz%s)r,   rB   )r   Úaccumr   r   r   Ú__str__”   s    zAtomicExpression.__str__c                 C   s
   t | jƒS r   )Úhashr,   r   r   r   r   Ú__hash__š   s    zAtomicExpression.__hash__)N)N)r   r	   r
   r   rE   rK   rL   rO   rP   rQ   rT   rV   r   r   r   r   r?   [   s   

r?   c                   @   s   e Zd Zdd„ ZdS )r+   c                 C   s^   t |tƒsJ ‚t |tƒrBz|t|| fgƒ W S  ty>   Y qN0 n| |krN|S t| ||ƒ‚dS )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'
        N)r@   r/   r*   ÚBindingDictÚVariableBindingExceptionÚUnificationException)r   r5   rD   r   r   r   ÚunifyŸ   s    

zConstantExpression.unifyN©r   r	   r
   rZ   r   r   r   r   r+   ž   s   r+   c                   @   s   e Zd Zdd„ ZdS )r*   c              
   C   sl   t |tƒsJ ‚z&| |kr|W S |t| |fgƒ W S W n2 tyf } zt| ||ƒ|‚W Y d}~n
d}~0 0 dS )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@   r/   rW   rX   rY   ©r   r5   rD   Úer   r   r   rZ   µ   s    	zVariableExpression.unifyNr[   r   r   r   r   r*   ´   s   r*   c                   @   s^   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )r   c                 C   s,   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@   r/   Ú
antecedentÚ
consequent)r   r^   r_   r   r   r   r   É   s    zImpExpression.__init__Nc                 C   s   |   | j |¡| j |¡¡S r   )r<   r^   rE   r_   rC   r   r   r   rE   Ó   s    ÿzImpExpression.simplifyc              
   C   sl   t |tƒsJ ‚z&|| j |j|¡ | j |j|¡ W S  tyf } zt| ||ƒ|‚W Y d}~n
d}~0 0 dS )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^   rZ   r_   rX   rY   r\   r   r   r   rZ   Ø   s    	ÿþÿzImpExpression.unifyc                 C   s6   | j  ||¡\}}| j ||¡\}}t||ƒ|| fS rF   )r^   rL   r_   rK   r   )r   rI   rJ   ÚaÚa_newÚcÚc_newr   r   r   rK   ë   s    zImpExpression.compile_posc           	      C   s\   | j  ||¡\}}| j ||¡\}}| ¡ }|j |¡ |d| ||hƒ}||| |g 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^   rK   r_   rL   ÚgetrB   Úappend)	r   rI   rJ   r`   ra   rb   rc   Zfresh_indexZnew_vr   r   r   rL   ÷   s    zImpExpression.compile_negc                 C   s   | j  |¡ | j |¡ d S r   )r^   rO   r_   rN   r   r   r   rO     s    zImpExpression.initialize_labelsc                 C   s$   | j |j ko"| j|jko"| j|jkS r   )r<   r^   r_   r7   r   r   r   rP   
  s
    
ÿ
ýzImpExpression.__eq__c                 C   s
   | |k S r   r   r7   r   r   r   rQ     s    zImpExpression.__ne__c                 C   s   d  tj| jtj| jtj¡S )Nz{}{} {} {}{})Úformatr   r   r^   r   r_   r   r   r   r   r   rT     s    ûzImpExpression.__str__c                 C   s"   t t | jƒ› tj› t | jƒ› �ƒS r   )rU   r^   r   r   r_   r   r   r   r   rV     s    zImpExpression.__hash__)N)r   r	   r
   r   rE   rZ   rK   rL   rO   rP   rQ   rT   rV   r   r   r   r   r   È   s   

	r   c                   @   s@   e Zd Zddd„Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )r&   Nc              
   C   s  |  ¡ }|  ¡ }t|tƒsJ ‚t|tƒs,J ‚tƒ }z>t|tƒrH||j7 }t|tƒr\||j7 }||j ||¡7 }W n@ t	y° } z(t
d|› d|› d|› �ƒ|‚W Y d}~n
d}~0 0 |röt|jjƒ|k sÖt
d||f ƒ‚t|jjƒ|kröt
d||f ƒ‚|| _|| _|| _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. NzODependencies unfulfilled when attempting to apply Linear Logic formula %s to %szbDependencies not a proper subset of indices when attempting to apply Linear Logic formula %s to %s)rE   r@   r   r/   rW   r&   rD   r^   rZ   rY   ÚLinearLogicApplicationExceptionÚsetrB   Úfunctionr'   )r   ri   r'   Zargument_indicesZfunction_simpZargument_simprD   r]   r   r   r   r   "  sB    



ÿþÿÿÿÿzApplicationExpression.__init__c                 C   s   |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``
        )rD   ri   rE   r_   rC   r   r   r   rE   N  s    	zApplicationExpression.simplifyc                 C   s$   | j |j ko"| j|jko"| j|jkS r   )r<   ri   r'   r7   r   r   r   rP   \  s
    
ÿ
ýzApplicationExpression.__eq__c                 C   s
   | |k S r   r   r7   r   r   r   rQ   c  s    zApplicationExpression.__ne__c                 C   s    d| j  tj d| j  tj S rR   )ri   r   r   r'   r   r   r   r   r   rT   f  s    zApplicationExpression.__str__c                 C   s"   t t | jƒ› tj› t | jƒ› �ƒS r   )rU   r^   r   r   r_   r   r   r   r   rV   i  s    zApplicationExpression.__hash__)N)N)	r   r	   r
   r   rE   rP   rQ   rT   rV   r   r   r   r   r&   !  s   
,
r&   c                   @   sV   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )rW   Nc                 C   s6   i | _ t|tƒr| ¡ }|r2|D ]\}}|| |< q dS )zÞ
        :param bindings:
            list [(``VariableExpression``, ``AtomicExpression``)] to initialize the dictionary
            dict {``VariableExpression``: ``AtomicExpression``} to initialize the dictionary
        N)Údr@   ÚdictÚitems)r   rD   ÚvÚbr   r   r   r   n  s    
zBindingDict.__init__c                 C   s^   t |tƒsJ ‚t |tƒsJ ‚||ks(J ‚| j |d¡}|rB||krN|| j|< ntd| ƒ‚dS )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*   r/   rj   rd   rX   )r   ÚvariableZbindingÚexistingr   r   r   Ú__setitem__}  s    	ÿzBindingDict.__setitem__c                 C   sH   t |tƒsJ ‚| j| }|rDz| j| }W q ty@   | Y S 0 qdS )zD
        Return the expression to which 'variable' is bound
        N)r@   r*   rj   ÚKeyError)r   ro   Zintermediater   r   r   Ú__getitem__”  s    
zBindingDict.__getitem__c                 C   s
   || j v S r   ©rj   )r   Úitemr   r   r   Ú__contains__¡  s    zBindingDict.__contains__c              
   C   s|   z@t ƒ }| jD ]}| j| ||< q|jD ]}|j| ||< q(|W S  tyv } ztd| |f ƒ|‚W Y d}~n
d}~0 0 dS )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
        zAAttempting to add two contradicting VariableBindingsLists: %s, %sN)rW   rj   rX   )r   r5   Zcombinedrm   r]   r   r   r   Ú__add__¤  s    

ÿÿýzBindingDict.__add__c                 C   s
   | |k S r   r   r7   r   r   r   rQ   ·  s    zBindingDict.__ne__c                 C   s   t |tƒst‚| j|jkS r   )r@   rW   Ú	TypeErrorrj   r7   r   r   r   rP   º  s    
zBindingDict.__eq__c                    s*   dd  ‡ fdd„tˆ j ¡ ƒD ƒ¡ d S )NÚ{z, c                 3   s"   | ]}|› d ˆ j | › �V  qdS )z: Nrt   )Ú.0rm   r   r   r   Ú	<genexpr>À  ó    z&BindingDict.__str__.<locals>.<genexpr>Ú})ÚjoinÚsortedrj   Úkeysr   r   r   r   rT   ¿  s    zBindingDict.__str__c                 C   s   d|  S )NzBindingDict: %sr   r   r   r   r   r=   Â  s    zBindingDict.__repr__)N)r   r	   r
   r   rq   rs   rv   rw   rQ   rP   rT   r=   r   r   r   r   rW   m  s   
rW   c                   @   s   e Zd ZdS )rX   N©r   r	   r
   r   r   r   r   rX   Æ  s   rX   c                   @   s   e Zd Zdd„ ZdS )rY   c              	   C   s"   t  | d|› d|› d|› �¡ d S )NzCannot unify z with z given )Ú	Exceptionr   )r   r`   rn   rD   r   r   r   r   Ë  s    zUnificationException.__init__N)r   r	   r
   r   r   r   r   r   rY   Ê  s   rY   c                   @   s   e Zd ZdS )rg   Nr�   r   r   r   r   rg   Ï  s   rg   c                  C   sz   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)))r/   r4   ÚprintrE   )Zlexprr   r   r   ÚdemoÓ  s    r…   Ú__main__N)Znltk.internalsr   Znltk.sem.logicr   r   Ú_counterr   r   r/   r?   r+   r*   r   r&   rW   r‚   rX   rY   rg   r…   r   r   r   r   r   Ú<module>	   s"   /CYLY