³ò
zdhKc           @   sV  d  d k  l Z d  d k l Z e ƒ  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ	 d e f d „  ƒ  YZ
 d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z e d j o e ƒ  n d S(   iÿÿÿÿ(   t   Counter(   t   LogicParsert
   Expressionc           B   s&   e  Z e d  „ Z d „  Z d „  Z RS(   c         C   s   t  |  | | ƒ S(   N(   t   ApplicationExpression(   t   selft   othert   other_indices(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   applyto   s    c         C   s   |  i  | ƒ S(   N(   R   (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __call__   s    c         C   s    d |  i  i d t |  ƒ d S(   Nt   <t    t   >(   t	   __class__t   __name__t   str(   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __repr__   s    (   R   t
   __module__t   NoneR   R   R   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR      s   	t   AtomicExpressionc           B   sV   e  Z e d  „ Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   c         C   s>   t  | t ƒ p t ‚ | |  _ | p
 g  } n | |  _ d S(   sš   
        @param name: C{str} for the constant name
        @param dependencies: C{list} of C{int} for the indices on which this atom is dependent
        N(   t
   isinstanceR   t   AssertionErrort   namet   dependencies(   R   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __init__   s
    	
c         C   s(   | o |  | j o | |  Sn |  Sd S(   sý   
        If 'self' is bound by 'bindings', return the atomic to which it is bound.  
        Otherwise, return self.
        
        @param bindings: C{BindingDict} A dictionary of bindings used to simplify
        @return: C{AtomicExpression}
        N(    (   R   t   bindings(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   simplify%   s    c         C   s   g  |  _  |  g  f S(   s;  
        From Iddo Lev's PhD Dissertation p108-109
        
        @param index_counter: C{Counter} for unique indices
        @param glueFormulaFactory: C{GlueFormula} for creating new glue formulas
        @return: (C{Expression},C{set}) for the compiled linear logic and any newly created glue formulas
        (   R   (   R   t   index_countert   glueFormulaFactory(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   compile_pos2   s    	c         C   s   g  |  _  |  g  f S(   s;  
        From Iddo Lev's PhD Dissertation p108-109
        
        @param index_counter: C{Counter} for unique indices
        @param glueFormulaFactory: C{GlueFormula} for creating new glue formulas
        @return: (C{Expression},C{set}) for the compiled linear logic and any newly created glue formulas
        (   R   (   R   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   compile_neg=   s    	c         C   s   | i  |  i i ƒ  ƒ |  _ d  S(   N(   t   initialize_labelR   t   lower(   R   t   fstruct(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   initialize_labelsH   s    c         C   s#   |  i  | i  j o |  i | i j S(   N(   R   R   (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __eq__K   s    c         C   s.   |  i  } |  i o | t |  i ƒ 7} n | S(   N(   R   R   R   (   R   t   accum(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __str__N   s    	
c         C   s   t  |  i ƒ S(   N(   t   hashR   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __hash__T   s    (   R   R   R   R   R   R   R   R!   R"   R$   R&   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR      s   					t   ConstantExpressionc           B   s   e  Z d  „  Z RS(   c         C   s‡   t  | t ƒ p t ‚ t  | t ƒ o5 y | t | |  f g ƒ SWqq t j
 o qq Xn |  | j o | Sn t |  | | ƒ ‚ d S(   së  
        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: C{Expression}
        @param bindings: C{BindingDict} A dictionary of all current bindings
        @return: C{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   t   VariableExpressiont   BindingDictt   VariableBindingExceptiont   UnificationException(   R   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   unifyX   s    
	(   R   R   R,   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR'   W   s   R(   c           B   s   e  Z d  „  Z RS(   c         C   ss   t  | t ƒ p t ‚ y0 |  | j o | Sn | t |  | f g ƒ SWn% t j
 o t |  | | ƒ ‚ n Xd S(   s  
        'self' must not be bound to anything other than 'other'.
        
        @param other: C{Expression}
        @param bindings: C{BindingDict} A dictionary of all current bindings
        @return: C{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   R   R)   R*   R+   (   R   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR,   m   s    	(   R   R   R,   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR(   l   s   t   ImpExpressionc           B   s\   e  Z d  „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   c         C   sD   t  | t ƒ p t ‚ t  | t ƒ p t ‚ | |  _ | |  _ d S(   s   
        @param antecedent: C{Expression} for the antecedent
        @param consequent: C{Expression} for the consequent
        N(   R   R   R   t
   antecedentt
   consequent(   R   R.   R/   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   €   s    	c         C   s(   |  i  |  i i | ƒ |  i i | ƒ ƒ S(   N(   R   R.   R   R/   (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   Š   s    c         C   sw   t  | t ƒ p t ‚ y4 | |  i i | i | ƒ |  i i | i | ƒ SWn% t j
 o t |  | | ƒ ‚ n Xd S(   s  
        Both the antecedent and consequent of 'self' and 'other' must unify.
        
        @param other: C{ImpExpression}
        @param bindings: C{BindingDict} A dictionary of all current bindings
        @return: C{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+   (   R   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR,      s
    	4c         C   sM   |  i  i | | ƒ \ } } |  i i | | ƒ \ } } t | | ƒ | | f S(   s;  
        From Iddo Lev's PhD Dissertation p108-109
        
        @param index_counter: C{Counter} for unique indices
        @param glueFormulaFactory: C{GlueFormula} for creating new glue formulas
        @return: (C{Expression},C{set}) for the compiled linear logic and any newly created glue formulas
        (   R.   R   R/   R   R-   (   R   R   R   t   at   a_newt   ct   c_new(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   œ   s    c   	      C   s†   |  i  i | | ƒ \ } } |  i i | | ƒ \ } } | i ƒ  } | i i | ƒ | d | | t | g ƒ ƒ } | | | | g f S(   sN  
        From Iddo Lev's PhD Dissertation p108-109
        
        @param index_counter: C{Counter} for unique indices
        @param glueFormulaFactory: C{GlueFormula} for creating new glue formulas
        @return: (C{Expression},C{list} of C{GlueFormula}) for the compiled linear logic and any newly created glue formulas
        s   v%s(   R.   R   R/   R   t   getR   t   appendt   set(	   R   R   R   R0   R1   R2   R3   t   fresh_indext   new_v(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   ¨   s    c         C   s$   |  i  i | ƒ |  i i | ƒ d  S(   N(   R.   R!   R/   (   R   R    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR!   ·   s    c         C   s6   |  i  | i  j o# |  i | i j o |  i | i j S(   N(   R   R.   R/   (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR"   »   s    c         C   s7   t  i t |  i ƒ d t  i d t |  i ƒ t  i S(   NR
   (   t   Tokenst   OPENR   R.   t   IMPR/   t   CLOSE(   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR$   ¿   s    c         C   s,   t  d t  |  i ƒ t i t  |  i ƒ f ƒ S(   Ns   %s%s%s(   R%   R.   R9   R;   R/   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR&   Ã   s    (   R   R   R   R   R   R,   R   R   R!   R"   R$   R&   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR-      s   	
						R   c           B   s;   e  Z e d  „ Z e d „ Z d „  Z d „  Z d „  Z RS(   c         C   sf  | i  ƒ  } | i  ƒ  } t | t ƒ p t ‚ t | t ƒ p t ‚ t ƒ  } y_ t | t ƒ o | | i 7} n t | t ƒ o | | i 7} n | | i i	 | | ƒ 7} Wn+ t
 j
 o } t d | | | f ‚ n X| od t | i i ƒ | j  p t d | | f ‚ n t | i i ƒ | j o t d | | f ‚ qGn | |  _ | |  _ | |  _ d S(   sZ  
        @param function: C{Expression} for the function
        @param argument: C{Expression} for the argument
        @param argument_indices: C{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'.
        s   Cannot apply %s to %s. %ssO   Dependencies unfulfilled when attempting to apply Linear Logic formula %s to %ssb   Dependencies not a proper subset of indices when attempting to apply Linear Logic formula %s to %sN(   R   R   R-   R   R   R)   R   R   R.   R,   R+   t   LinearLogicApplicationExceptionR6   R   t   functiont   argument(   R   R>   R?   t   argument_indicest   function_simpt   argument_simpR   t   e(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   Ç   s*    			c         C   s'   | p |  i  } n |  i i | ƒ i S(   sE  
        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: C{BindingDict} A dictionary of bindings used to simplify
        @return: C{Expression}
        (   R   R>   R   R/   (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   ë   s    	c         C   s6   |  i  | i  j o# |  i | i j o |  i | i j S(   N(   R   R>   R?   (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR"   ù   s    c         C   s(   t  |  i ƒ t i t  |  i ƒ t i S(   N(   R   R>   R9   R:   R?   R<   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR$   ý   s    c         C   s,   t  d t  |  i ƒ t i t  |  i ƒ f ƒ S(   Ns   %s%s%s(   R%   R.   R9   R:   R/   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR&      s    (   R   R   R   R   R   R"   R$   R&   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   Æ   s
   $		R)   c           B   sJ   e  Z e d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s9   h  |  _  | o% x" | D] \ } } | |  | <q Wn d S(   s{   
        @param binding_list: C{list} of (C{VariableExpression}, C{AtomicExpression}) to initialize the dictionary
        N(   t   d(   R   t   binding_listt   vt   b(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR     s
    	 c         C   s¦   t  | t ƒ p t ‚ t  | t ƒ p t ‚ | | j p t ‚ y |  i | } Wn t j
 o d } n X| p | | j o | |  i | <n t d | ‚ d S(   sš  
        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: C{VariableExpression} The variable bind
        @param binding: C{Expression} The expression to which 'variable' should be bound
        @raise VariableBindingException: If the variable cannot be bound in this dictionary
        s*   Variable %s already bound to another valueN(   R   R(   R   R   RD   t   KeyErrorR   R*   (   R   t   variablet   bindingt   existing(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __setitem__  s    	c         C   sb   t  | t ƒ p t ‚ |  i | } x7 | o/ y |  i | } Wq' t j
 o | Sq' Xq' Wd S(   sD   
        Return the expression to which 'variable' is bound
        N(   R   R(   R   RD   RH   (   R   RI   t   intermediate(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __getitem__&  s    
c         C   s   | |  i  j S(   N(   RD   (   R   t   item(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __contains__3  s    c      	   C   sˆ   y[ t  ƒ  } x" |  i D] } |  i | | | <q Wx" | i D] } | i | | | <q; W| SWn& t j
 o t d |  | f ‚ n Xd S(   s  
        @param other: C{BindingDict} The dict with which to combine self
        @return: C{BindingDict} A new dict containing all the elements of both parameters
        @raise VariableBindingException: If the parameter dictionaries are not consistent with each other
        sA   Attempting to add two contradicting VariableBindingsLists: %s, %sN(   R)   RD   R*   (   R   R   t   combinedRF   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   __add__6  s    	
 
 c      	   C   sD   d d i  g  } |  i D] } | d | |  i | f q ~ ƒ d S(   Nt   {s   , s   %s: %st   }(   t   joinRD   (   R   t   _[1]RF   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR$   G  s    c         C   s   d t  |  ƒ S(   Ns   BindingDict: (   R   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   J  s    (
   R   R   R   R   RL   RN   RP   RR   R$   R   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR)     s   
					R*   c           B   s   e  Z RS(    (   R   R   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR*   M  s    R+   c           B   s   e  Z d  „  Z RS(   c         C   s!   t  i |  d | | | f ƒ d  S(   Ns    Cannot unify %s with %s given %s(   t	   ExceptionR   (   R   R0   RG   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   P  s    (   R   R   R   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR+   O  s   R=   c           B   s   e  Z RS(    (   R   R   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR=   S  s    R9   c           B   s3   e  Z d  Z d Z d Z e e g Z e e g Z RS(   t   (t   )s   -o(   R   R   R:   R<   R;   t   PUNCTt   TOKENS(    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR9   U  s
   t   LinearLogicParserc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s!   A linear logic expression parser.c         C   s   t  i |  ƒ d  S(   N(   R   R   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR   b  s    c         C   s   t  i S(   N(   R9   R[   (   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   get_all_symbolse  s    c         C   sF   | t  i j o |  i | ƒ Sn" | t  i j o |  i | ƒ Sn d  S(   N(   R9   R[   t   handle_variableR:   t   handle_open(   R   t   tok(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   handleh  s    c         C   s    | t  i j o t Sn d  Sd  S(   N(   R9   R;   R-   R   (   R   R`   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   get_BooleanExpression_factoryn  s    c         C   s   | | | ƒ S(   N(    (   R   t   factoryt   firstt   second(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   make_BooleanExpressiont  s    c         C   st   |  i  d ƒ o\ |  i d ƒ t i j oC |  i ƒ  |  i ƒ  } |  i |  i ƒ  t i ƒ t | | d ƒ Sn | Sd S(   sé   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   inRanget   tokenR9   R:   t   parse_Expressiont   assertTokenR<   R   R   (   R   t
   expressionR?   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   attempt_ApplicationExpressionw  s    )
c         C   s-   | d i  ƒ  o t | ƒ Sn t | ƒ Sd  S(   Ni    (   t   isupperR(   R'   (   R   R   (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   make_VariableExpression„  s    (
   R   R   t   __doc__R   R]   Ra   Rb   Rf   Rl   Rn   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyR\   `  s   						c          C   s‡   t  ƒ  }  |  i d ƒ GH|  i d ƒ GH|  i d ƒ GH|  i d ƒ i ƒ  GH|  i d ƒ i ƒ  GH|  i d ƒ i ƒ  GH|  i d ƒ i ƒ  GHd  S(   Nt   fs   (g -o f)s   ((g -o G) -o G)s   (g -o f)(g)s   (H -o f)(g)s   ((g -o G) -o G)((g -o f))s   (H -o H)((g -o f))(   R\   t   parseR   (   t   llp(    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pyt   demoŠ  s    	t   __main__N(   t   nltk.internalsR    t   logicR   t   _countert   objectR   R   R'   R(   R-   R   R)   RW   R*   R+   R=   R9   R\   Rs   R   (    (    (    s*   /p/zhu/06/nlp/nltk/nltk/sem/linearlogic.pys   <module>	   s"   	
?G=J*	