ó
ù`]c           @  s®  d  d l  m Z m Z d  d l m Z d  d l m Z d  d l m Z d  d l	 m
 Z
 m Z e ƒ  Z d e f d „  ƒ  YZ d e
 f d	 „  ƒ  YZ e d
 e f d „  ƒ  Yƒ Z e d e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d e f d „  ƒ  Yƒ Z e d e f d „  ƒ  Yƒ Z e d e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z e d k rªe ƒ  n  d  S(!   iÿÿÿÿ(   t   print_functiont   unicode_literals(   t   string_types(   t   Counter(   t   python_2_unicode_compatible(   t   LogicParsert   APPt   Tokensc           B  s3   e  Z d  Z d Z d Z e e g Z e e g Z RS(   u   (u   )u   -o(   t   __name__t
   __module__t   OPENt   CLOSEt   IMPt   PUNCTt   TOKENS(    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR      s
   t   LinearLogicParserc           B  sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   u!   A linear logic expression parser.c         C  sG   t  j |  ƒ i d t 6d t j 6d d  6|  _ |  j t j g 7_ d  S(   Ni   i   i   (   R   t   __init__R   R   R   t   Nonet   operator_precedencet   right_associated_operations(   t   self(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR   "   s    !c         C  s   t  j S(   N(   R   R   (   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   get_all_symbols(   s    c         C  sB   | t  j k r |  j | | ƒ S| t  j k r> |  j | | ƒ Sd  S(   N(   R   R   t   handle_variableR
   t   handle_open(   R   t   tokt   context(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   handle+   s    c         C  s   | t  j k r t Sd  Sd  S(   N(   R   R   t   ImpExpressionR   (   R   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   get_BooleanExpression_factory1   s    c         C  s   | | | ƒ S(   N(    (   R   t   factoryt   firstt   second(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   make_BooleanExpression7   s    c         C  s~   |  j  t | ƒ rz |  j d ƒ rz |  j d ƒ t j k rz |  j ƒ  |  j t ƒ } |  j t j ƒ t	 | | d ƒ } qz n  | 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.i    N(   t   has_priorityR   t   inRanget   tokenR   R
   t   process_next_expressiont   assertNextTokenR   t   ApplicationExpressionR   (   R   t
   expressionR   t   argument(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   attempt_ApplicationExpression:   s    '
c         C  s(   | d j  ƒ  r t | ƒ St | ƒ Sd  S(   Ni    (   t   isuppert   VariableExpressiont   ConstantExpression(   R   t   name(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   make_VariableExpressionG   s    
(
   R   R	   t   __doc__R   R   R   R   R    R)   R.   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR      s   						t
   Expressionc           B  s>   e  Z e ƒ  Z e d  „  ƒ Z d d „ Z d „  Z d „  Z	 RS(   c         C  s   |  j  j | ƒ S(   N(   t   _linear_logic_parsert   parse(   t   clst   s(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt
   fromstringS   s    c         C  s   t  |  | | ƒ S(   N(   R&   (   R   t   othert   other_indices(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   applytoW   s    c         C  s   |  j  | ƒ S(   N(   R8   (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __call__Z   s    c         C  s   d |  j  j |  f S(   Nu   <%s %s>(   t	   __class__R   (   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __repr__]   s    N(
   R   R	   R   R1   t   classmethodR5   R   R8   R9   R;   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR0   N   s
   		t   AtomicExpressionc           B  s_   e  Z d	 d  „ Z d	 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(
   c         C  s:   t  | t ƒ s t ‚ | |  _ | s- g  } n  | |  _ d S(   u‘   
        :param name: str for the constant name
        :param dependencies: list of int for the indices on which this atom is dependent
        N(   t
   isinstanceR   t   AssertionErrorR-   t   dependencies(   R   R-   R@   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR   c   s
    		c         C  s"   | r |  | k r | |  S|  Sd S(   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(    (   R   t   bindings(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   simplifyo   s    c         C  s   g  |  _  |  g  f S(   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@   (   R   t   index_countert   glueFormulaFactory(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   compile_pos|   s    	c         C  s   g  |  _  |  g  f S(   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@   (   R   RC   RD   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   compile_neg‡   s    	c         C  s   | j  |  j j ƒ  ƒ |  _ d  S(   N(   t   initialize_labelR-   t   lower(   R   t   fstruct(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   initialize_labels’   s    c         C  s"   |  j  | j  k o! |  j | j k S(   N(   R:   R-   (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __eq__•   s    c         C  s   |  | k S(   N(    (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __ne__˜   s    c         C  s*   |  j  } |  j r& | d |  j 7} n  | S(   Nu   %s(   R-   R@   (   R   t   accum(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __str__›   s    		c         C  s   t  |  j ƒ S(   N(   t   hashR-   (   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __hash__¡   s    N(   R   R	   R   R   RB   RE   RF   RJ   RK   RL   RN   RP   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR=   a   s   						R,   c           B  s   e  Z d  „  Z RS(   c         C  s|   t  | t ƒ s t ‚ t  | t ƒ rV y | t | |  f g ƒ SWqf t k
 rR qf Xn |  | k rf | St |  | | ƒ ‚ d S(   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>   R0   R?   R+   t   BindingDictt   VariableBindingExceptiont   UnificationException(   R   R6   RA   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   unify¦   s    
(   R   R	   RT   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR,   ¥   s   R+   c           B  s   e  Z d  „  Z RS(   c         C  sj   t  | t ƒ s t ‚ y+ |  | k r( | S| t |  | f g ƒ SWn# t k
 re t |  | | ƒ ‚ n Xd S(   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>   R0   R?   RQ   RR   RS   (   R   R6   RA   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRT   ¼   s    	(   R   R	   RT   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR+   »   s   R   c           B  se   e  Z d  „  Z d
 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z RS(   c         C  s@   t  | t ƒ s t ‚ t  | t ƒ s* t ‚ | |  _ | |  _ d S(   uƒ   
        :param antecedent: ``Expression`` for the antecedent
        :param consequent: ``Expression`` for the consequent
        N(   R>   R0   R?   t
   antecedentt
   consequent(   R   RU   RV   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR   Ñ   s    	c         C  s(   |  j  |  j j | ƒ |  j j | ƒ ƒ S(   N(   R:   RU   RB   RV   (   R   RA   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRB   Û   s    c         C  ss   t  | t ƒ s t ‚ y4 | |  j j | j | ƒ |  j j | j | ƒ SWn# t k
 rn t |  | | ƒ ‚ n Xd S(   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>   R   R?   RU   RT   RV   RR   RS   (   R   R6   RA   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRT   à   s
    	4c         C  sM   |  j  j | | ƒ \ } } |  j j | | ƒ \ } } t | | ƒ | | f S(   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
        (   RU   RF   RV   RE   R   (   R   RC   RD   t   at   a_newt   ct   c_new(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRE   ó   s    c   	      C  s†   |  j  j | | ƒ \ } } |  j j | | ƒ \ } } | j ƒ  } | j j | ƒ | d | | t | g ƒ ƒ } | | | | g f S(   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
        u   v%s(   RU   RE   RV   RF   t   getR@   t   appendt   set(	   R   RC   RD   RW   RX   RY   RZ   t   fresh_indext   new_v(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRF   ÿ   s    c         C  s$   |  j  j | ƒ |  j j | ƒ d  S(   N(   RU   RJ   RV   (   R   RI   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRJ     s    c         C  s4   |  j  | j  k o3 |  j | j k o3 |  j | j k S(   N(   R:   RU   RV   (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRK     s    c         C  s   |  | k S(   N(    (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRL     s    c         C  s&   d t  j |  j t  j |  j t  j f S(   Nu   %s%s %s %s%s(   R   R
   RU   R   RV   R   (   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRN     s    c         C  s,   t  d t  |  j ƒ t j t  |  j ƒ f ƒ S(   Nu   %s%s%s(   RO   RU   R   R   RV   (   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRP   %  s    N(   R   R	   R   R   RB   RT   RE   RF   RJ   RK   RL   RN   RP   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR   Ï   s   	
								R&   c           B  sD   e  Z d d  „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s_  | j  ƒ  } | j  ƒ  } t | t ƒ s- t ‚ t | t ƒ sB t ‚ t ƒ  } y[ t | t ƒ rm | | j 7} n  t | t ƒ rŒ | | j 7} n  | | j j	 | | ƒ 7} Wn, t
 k
 rÔ } t d | | | f ƒ ‚ n X| r@t | j j ƒ | k  st d | | f ƒ ‚ n  t | j j ƒ | k r@t d | | f ƒ ‚ q@n  | |  _ | |  _ | |  _ d S(   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'.
        u   Cannot apply %s to %s. %suO   Dependencies unfulfilled when attempting to apply Linear Logic formula %s to %sub   Dependencies not a proper subset of indices when attempting to apply Linear Logic formula %s to %sN(   RB   R>   R   R?   R0   RQ   R&   RA   RU   RT   RS   t   LinearLogicApplicationExceptionR]   R@   t   functionR(   (   R   Ra   R(   t   argument_indicest   function_simpt   argument_simpRA   t   e(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR   -  s4    			c         C  s%   | s |  j  } n  |  j j | ƒ j S(   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``
        (   RA   Ra   RB   RV   (   R   RA   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRB   Y  s    	c         C  s4   |  j  | j  k o3 |  j | j k o3 |  j | j k S(   N(   R:   Ra   R(   (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRK   g  s    c         C  s   |  | k S(   N(    (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRL   n  s    c         C  s$   d |  j  t j d |  j t j S(   Nu   %s(   Ra   R   R
   R(   R   (   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRN   q  s    c         C  s,   t  d t  |  j ƒ t j t  |  j ƒ f ƒ S(   Nu   %s%s%s(   RO   RU   R   R
   RV   (   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRP   t  s    N(	   R   R	   R   R   RB   RK   RL   RN   RP   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR&   +  s   ,			RQ   c           B  s\   e  Z d	 d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(
   c         C  sU   i  |  _  t | t ƒ r' | j ƒ  } n  | rQ x! | D] \ } } | |  | <q4 Wn  d S(   uÞ   
        :param bindings:
            list [(``VariableExpression``, ``AtomicExpression``)] to initialize the dictionary
            dict {``VariableExpression``: ``AtomicExpression``} to initialize the dictionary
        N(   t   dR>   t   dictt   items(   R   RA   t   vt   b(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR   |  s    	c         C  sˆ   t  | t ƒ s t ‚ t  | t ƒ s* t ‚ | | k s< t ‚ |  j j | d ƒ } | sd | | k rt | |  j | <n t d | ƒ ‚ d S(   u“  
        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
        u*   Variable %s already bound to another valueN(   R>   R+   R?   R0   Rf   R[   R   RR   (   R   t   variablet   bindingt   existing(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __setitem__‹  s    	c         C  sY   t  | t ƒ s t ‚ |  j | } x0 | rT y |  j | } Wq% t k
 rP | SXq% Wd S(   uD   
        Return the expression to which 'variable' is bound
        N(   R>   R+   R?   Rf   t   KeyError(   R   Rk   t   intermediate(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __getitem__¢  s    	c         C  s   | |  j  k S(   N(   Rf   (   R   t   item(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __contains__¯  s    c         C  s‰   y[ t  ƒ  } x" |  j D] } |  j | | | <q Wx" | j D] } | j | | | <q; W| SWn' t k
 r„ t d |  | f ƒ ‚ n Xd S(   u  
        :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
        uA   Attempting to add two contradicting VariableBindingsLists: %s, %sN(   RQ   Rf   RR   (   R   R6   t   combinedRi   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   __add__²  s    	c         C  s   |  | k S(   N(    (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRL   Å  s    c         C  s(   t  | t ƒ s t ‚ n  |  j | j k S(   N(   R>   RQ   t	   TypeErrorRf   (   R   R6   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRK   È  s    	c           s(   d d j  ‡  f d †  ˆ  j Dƒ ƒ d S(   Nu   {u   , c         3  s&   |  ] } d  | ˆ  j  | f Vq d S(   u   %s: %sN(   Rf   (   t   .0Ri   (   R   (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pys	   <genexpr>Î  s    u   }(   t   joinRf   (   R   (    (   R   s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRN   Í  s    c         C  s   d |  S(   Nu   BindingDict: %s(    (   R   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR;   Ð  s    N(   R   R	   R   R   Rn   Rq   Rs   Ru   RL   RK   RN   R;   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRQ   z  s   							RR   c           B  s   e  Z RS(    (   R   R	   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRR   Ô  s   RS   c           B  s   e  Z d  „  Z RS(   c         C  s!   t  j |  d | | | f ƒ d  S(   Nu    Cannot unify %s with %s given %s(   t	   ExceptionR   (   R   RW   Rj   RA   (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR   Ù  s    (   R   R	   R   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyRS   Ø  s   R`   c           B  s   e  Z RS(    (   R   R	   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyR`   Ý  s   c          C  s¥   t  j }  t |  d ƒ ƒ t |  d ƒ ƒ t |  d ƒ ƒ t |  d ƒ ƒ t |  d ƒ j ƒ  ƒ t |  d ƒ j ƒ  ƒ t |  d ƒ j ƒ  ƒ t |  d ƒ j ƒ  ƒ d  S(	   Nu   fu   (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))(   R0   R5   t   printRB   (   t   lexpr(    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   demoá  s    	u   __main__N(   t
   __future__R    R   t   sixR   t   nltk.internalsR   t   nltk.compatR   t   nltk.sem.logicR   R   t   _countert   objectR   R   R0   R=   R,   R+   R   R&   RQ   Ry   RR   RS   R`   R|   R   (    (    (    s3   lib/python2.7/site-packages/nltk/sem/linearlogic.pyt   <module>   s2   	/C[NY	