
dhKc           @   sO  d  Z  d d k Z d d k Z d d k l Z d d k l Z e   Z d e f d     YZ	 d   Z
 d   Z d	   Z d
 e f d     YZ e e d  Z e d  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 e f d     YZ e   Z e   Z e   Z e   Z d   Z d e f d     YZ d e f d      YZ d! e f d"     YZ  d# e f d$     YZ! e d%  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' d0 e& f d1     YZ( d2 e' f d3     YZ) d4 e& f d5     YZ* d6   Z+ d7 e$ f d8     YZ, d9 e, f d:     YZ- d; e, f d<     YZ. d= e. f d>     YZ/ d? e. f d@     YZ0 dA e$ f dB     YZ1 dC e$ f dD     YZ2 dE e2 f dF     YZ3 dG e3 f dH     YZ4 dI e3 f dJ     YZ5 dK e3 f dL     YZ6 dM e3 f dN     YZ7 dO e2 f dP     YZ8 dQ e f dR     YZ9 dS e f dT     YZ: dU e f dV     YZ; dW e; f dX     YZ< dY   Z= dZ   Z> d[   Z? d\   Z@ d]   ZA eB d^ j o e@   n d S(_   sW   
A version of first order predicate logic, built on 
top of the typed lambda calculus.
iN(   t   defaultdict(   t   Countert   Tokensc           B   sZ  e  Z d  Z d Z d Z d d d g Z d d d g Z d d d g Z d d d g Z d	 Z	 d
 Z
 d Z d d d g Z d d d g Z d d d g Z d d d g Z d d d g Z d d d g Z d d d g Z e e e e Z e e Z e e e	 e
 e g Z e e e e e e e Z e e e e e e e e e e e e e e g Z RS(   i    i   i   s   \t   somet   existst   allt   .t    t   (t   )t   ,t   nott   -t   andt   &t   ort   |t   impliess   ->t   iffs   <->t   =s   !=(   t   __name__t
   __module__t   OLD_NLTKt   NLTKt   PROVER9t   LAMBDAt   EXISTSt   ALLt   DOTt   OPENt   CLOSEt   COMMAt   NOTt   ANDt   ORt   IMPt   IFFt   EQt   NEQt   BINOPSt   QUANTSt   PUNCTt   TOKENSt   SYMBOLS(    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR      s.   
c          C   s}   d d d d d g }  xa t  |  t i t i t i t i t i t i t i t i t i t i g  D] } d | GHqf Wd S(   s   
    Boolean operators
    t   negationt   conjunctiont   disjunctiont   implicationt   equivalences   %-15s	%sN(   t   zipR   R    R   R!   R"   R#   R$   (   t   namest   pair(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   boolean_ops>   s     c          C   sM   d d g }  x: t  |  t i t i t i t i g  D] } d | GHq6 Wd S(   s   
    Equality predicates
    t   equalityt
   inequalitys   %-15s	%sN(   R1   R   R%   R   R&   (   R2   R3   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   equality_predsJ   s
     c          C   s]   d d d g }  xG t  |  t i t i t i t i t i t i g  D] } d | GHqF Wd S(   s   
    Binding operators
    t   existentialt	   universalt   lambdas   %-15s	%sN(   R1   R   R   R   R   R   (   R2   R3   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   binding_opsS   s     t   Variablec           B   sP   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   c         C   s+   t  | t  p t d |  | |  _ d S(   s7   
        @param name: the name of the variable
        s   %s is not a stringN(   t
   isinstancet   strt   AssertionErrort   name(   t   selfR@   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __init___   s    c         C   s    t  | t  o |  i | i j S(   N(   R=   R<   R@   (   RA   t   other(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __eq__f   s    c         C   s   |  | j S(   N(    (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __neq__i   s    c         C   s/   t  t |   t |   p t  |  i | i  S(   N(   t   cmpt   typeR@   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __cmp__l   s    c         C   s   | i  |  |   S(   N(   t   get(   RA   t   bindings(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   substitute_bindingso   s    c         C   s   t  |  i  S(   N(   t   hashR@   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __hash__r   s    c         C   s   |  i  S(   N(   R@   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __str__u   s    c         C   s   d |  i  d S(   Ns
   Variable('s   ')(   R@   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __repr__x   s    (
   R   R   RB   RD   RE   RH   RK   RM   RN   RO   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR<   ^   s   							c         C   s   |  t  j	 oc t |  i  o
 d } qv t |  i  o
 d } qv t |  i  o
 d } qv t p
 t d  n d } t | t t	 i
     } x; | t  j	 o- | | j o  t | t t	 i
     } q W| S(   s  
    Return a new, unique variable.
    param pattern: C{Variable} that is being replaced.  The new variable must
    be the same type.
    param term: a C{set} of C{Variable}s that should not be returned from 
    this function.
    return: C{Variable}
    t   zt   Ft   e0s!   Cannot generate a unique constant(   t   Nonet	   is_indvarR@   t
   is_funcvart   is_eventvart   FalseR?   R<   R>   t   _counterRI   (   t   patternt   ignoret   prefixt   v(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   unique_variable|   s    	


!c         C   sT   t  t d t i     } |  o- x* t |   D] } | t  |   } q0 Wn | S(   sX   
    Return a skolem function over the variables in univ_scope
    param univ_scope
    s   F%s(   t   VariableExpressionR<   RX   RI   t   list(   t
   univ_scopet   skolemR\   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   skolem_function   s     t   Typec           B   s   e  Z d    Z d   Z RS(   c         C   s
   t  |   S(   N(   R>   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRO      s    c         C   s   t  t |    S(   N(   RL   R>   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRM      s    (   R   R   RO   RM   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRc      s   	t   ComplexTypec           B   s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   sR   t  | t  p t d |  t  | t  p t d |  | |  _ | |  _ d  S(   Ns   %s is not a Type(   R=   Rc   R?   t   firstt   second(   RA   Re   Rf   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB      s    	c         C   s3   t  | t  o# |  i | i j o |  i | i j S(   N(   R=   Rd   Re   Rf   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD      s    c         C   sK   t  | t  o- |  i i | i  o |  i i | i  Sn |  t j Sd  S(   N(   R=   Rd   Re   t   matchesRf   t   ANY_TYPE(   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRg      s    c         C   s   | t  j o |  Sn{ t | t  oQ |  i i | i  } |  i i | i  } | o | o t | |  Sq d  Sn |  t  j o | Sn d  Sd  S(   N(   Rh   R=   Rd   Re   t   resolveRf   RS   (   RA   RC   t   ft   s(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRi      s    c         C   s3   |  t  j o t t   Sn d |  i |  i f Sd  S(   Ns   <%s,%s>(   Rh   R>   Re   Rf   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRN      s    c         C   s?   |  t  j o t  i   Sn! d |  i i   |  i i   f Sd  S(   Ns
   (%s -> %s)(   Rh   R>   Re   Rf   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>      s    (   R   R   RB   RD   Rg   Ri   RN   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRd      s   					t	   BasicTypec           B   s#   e  Z d    Z d   Z d   Z RS(   c         C   s&   t  | t  o t |   t |  j S(   N(   R=   Rl   R>   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD      s    c         C   s   | t  j p
 |  | j S(   N(   Rh   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRg      s    c         C   s    |  i  |  o |  Sn d  Sd  S(   N(   Rg   RS   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRi      s    (   R   R   RD   Rg   Ri   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRl      s   		t
   EntityTypec           B   s   e  Z d    Z d   Z RS(   c         C   s   d S(   Nt   e(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRN      s    c         C   s   d S(   Nt   IND(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>      s    (   R   R   RN   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRm      s   	t   TruthValueTypec           B   s   e  Z d    Z d   Z RS(   c         C   s   d S(   Nt   t(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRN      s    c         C   s   d S(   Nt   BOOL(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>      s    (   R   R   RN   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRp      s   	t	   EventTypec           B   s   e  Z d    Z d   Z RS(   c         C   s   d S(   NR\   (    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRN      s    c         C   s   d S(   Nt   EVENT(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>      s    (   R   R   RN   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRs      s   	t   AnyTypec           B   s\   e  Z d    Z e d    Z e d    Z d   Z d   Z d   Z d   Z	 d   Z
 RS(   c         C   s   d  S(   N(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB      s    c         C   s   |  S(    (    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   <lambda>   s    c         C   s   |  S(    (    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv      s    c         C   s   t  | t  p | i |   S(   N(   R=   Ru   RD   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD      s    c         C   s   t  S(   N(   t   True(   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRg     s    c         C   s   | S(   N(    (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRi     s    c         C   s   d S(   Nt   ?(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRN   	  s    c         C   s   d S(   Nt   ANY(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>     s    (   R   R   RB   t   propertyRe   Rf   RD   Rg   Ri   RN   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRu      s   					c      	   C   s  t  |  t  p t  |  i d d  }  |  d d j o |  d d j p t  d } x t |   D]y \ } } | d j o | d 7} qe | d j o" | d 8} | d j p t  qe | d j o | d j o Pq qe qe Wt t |  d | ! t |  | d d !  Snr |  d t t  j o t SnS |  d t t  j o t Sn4 |  d t t	  j o t	 Sn t
 d	 |  d   d  S(
   NR   t    i    t   <it   >i   R
   s   Unexpected character: '%s'.(   R=   R>   R?   t   replacet	   enumerateRd   t
   parse_typet   ENTITY_TYPEt
   TRUTH_TYPERh   t   ParseException(   t   type_stringt   paren_countt   it   char(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s0     
t   TypeExceptionc           B   s   e  Z d    Z RS(   c         C   s   t  i |  |  d  S(   N(   t	   ExceptionRB   (   RA   t   msg(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB   3  s    (   R   R   RB   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   2  s   t"   InconsistentTypeHierarchyExceptionc           B   s   e  Z e d   Z RS(   c         C   s9   | o d | | f } n d | } t  i |  |  d  S(   NsL   The variable '%s' was found in multiple places with different types in '%s'.sD   The variable '%s' was found in multiple places with different types.(   R   RB   (   RA   t   variablet
   expressionR   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB   7  s    (   R   R   RS   RB   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   6  s   t   TypeResolutionExceptionc           B   s   e  Z d    Z RS(   c         C   s$   t  i |  d | | i | f  d  S(   Ns9   The type of '%s', '%s', cannot be resolved with type '%s'(   R   RB   RG   (   RA   R   t
   other_type(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB   A  s    (   R   R   RB   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   @  s   t   IllegalTypeExceptionc           B   s   e  Z d    Z RS(   c         C   s*   t  i |  d | i i | | | f  d  S(   Ns9   Cannot set type of %s '%s' to '%s'; must match type '%s'.(   R   RB   t	   __class__R   (   RA   R   R   t   allowed_type(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB   G  s    (   R   R   RB   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   F  s   c         C   sF   x |  D] } | i  |  } q Wx |  d  D] } | i  |  q+ W| S(   s   
    Ensure correct typing across a collection of C{Expression}s.
    @param expressions: a collection of expressions
    @param signature: C{dict} that maps variable names to types (or string 
    representations of types)
    i(   t	   typecheck(   t   expressionst	   signatureR   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   N  s      t   SubstituteBindingsIc           B   s    e  Z d  Z d   Z d   Z RS(   sT   
    An interface for classes that can perform substitutions for
    variables.
    c         C   s   t     d S(   s   
        @return: The object that is obtained by replacing
        each variable bound by C{bindings} with its values.
        Aliases are already resolved. (maybe?)
        @rtype: (any)
        N(   t   NotImplementedError(   RA   RJ   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRK   c  s    c         C   s   t     d S(   sB   
        @return: A list of all variables in this object.
        N(   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt	   variablesl  s    (   R   R   t   __doc__RK   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   ^  s   		t
   Expressionc           B   s  e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z e d  Z d   Z d   Z e d  Z d   Z e e d  Z e d  Z d   Z d   Z d   Z d   Z d   Z e d  Z d   Z d   Z RS(   s<   This is the base abstract object for all logical expressionsc         G   s0   |  i  |  } x | D] } | |  } q W| S(   N(   t   applyto(   RA   RC   t
   additionalt   accumt   a(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __call__v  s
     c         C   s+   t  | t  p t d |  t |  |  S(   Ns   %s is not an Expression(   R=   R   R?   t   ApplicationExpression(   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   |  s    c         C   s
   t  |   S(   N(   t   NegatedExpression(   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __neg__  s    c         C   s   |  S(   sY   If this is a negated expression, remove the negation.  
        Otherwise add a negation.(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   negate  s    c         C   s+   t  | t  p t d |  t |  |  S(   Ns   %s is not an Expression(   R=   R   R?   t   AndExpression(   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __and__  s    c         C   s+   t  | t  p t d |  t |  |  S(   Ns   %s is not an Expression(   R=   R   R?   t   OrExpression(   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __or__  s    c         C   s+   t  | t  p t d |  t |  |  S(   Ns   %s is not an Expression(   R=   R   R?   t   ImpExpression(   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __gt__  s    c         C   s+   t  | t  p t d |  t |  |  S(   Ns   %s is not an Expression(   R=   R   R?   t   IffExpression(   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   __lt__  s    c         C   s   t     d  S(   N(   R   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD     s    c         C   s   |  | j S(   N(    (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRE     s    c         C   sp   t  | t  p t d |  | t j o d d k l } |   } n t |  i   | i    } | i |  S(   s  
        Pass the expression (self <-> other) to the theorem prover.   
        If the prover says it is valid, then the self and other are equal.
        
        @param other: an C{Expression} to check equality against
        @param prover: a C{nltk.inference.api.Prover}
        s   %s is not an Expressioni(   t   Prover9(	   R=   R   R?   RS   t   nltk.inferenceR   R   t   simplifyt   prove(   RA   RC   t   proverR   t   bicond(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt	   tp_equals  s    c         C   s   t  t |    S(   N(   RL   t   repr(   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRM     s    c         C   s   |  } x | i    D] } | | j ov | | } t | t  o t |  } n( t | t  p t d | f   n | i |  } | i | |  } q q W| i   S(   Ns@   Can not substitute a non-expression value into an expression: %r(	   R   R=   R<   R^   R   t
   ValueErrorRK   R~   R   (   RA   RJ   t   exprt   vart   val(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRK     s     
c      	   C   s   t  t  } | op xm | i   D][ \ } } t t |   } t | t  o | | _ n t |  | _ | | i	 |  q  Wn |  i
 d |  t g  } | i   D]  \ } } | | | d i f q ~  S(   s   
        Infer and check types.  Raise exceptions if necessary.
        @param signature: C{dict} that maps variable names to types (or string 
        representations of types)
        @return: the signature, plus any additional type mappings 
        R   i    (   R    R_   t	   iteritemsR^   R<   R=   Rc   RG   R   t   appendt	   _set_typet   dict(   RA   R   t   sigt   keyR   t   varExt   _[1]t   vars(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s     c         C   s   t     d S(   s   
        Find the type of the given variable as it is used in this expression.
        For example, finding the type of "P" in "P(x) & Q(x,y)" yields "<e,t>"
        @param variable: C{Variable}  
        N(   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   findtype  s    c         C   s   t     d S(   s&  
        Set the type of this expression to be the given type.  Raise type 
        exceptions where applicable.
        @param other_type: C{Type} to set
        @param signature: C{dict<str, list<AbstractVariableExpression>>} store 
        all variable expressions with a given name
        N(   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c            sp   t   t  p t d   t   t  p t d    f d   }  i     f d   | t    S(   s%  
        Replace every instance of 'variable' with 'expression'
        @param variable: C{Variable} The variable to replace
        @param expression: C{Expression} The expression with which to replace it
        @param replace_bound: C{boolean} Should bound variables be replaced?  
        s   %s is not a Variables   %s is not an Expressionc            sS   t  |  d j o   i |   Sn, t  |  d j o   i |  | d  Sn d  S(   Ni    i   (   t   lenR   (   R   R   (   RA   (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt
   combinator  s    c            s   |  i       S(    (   R~   (   Rn   (   R   R   t   replace_bound(    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv     s    (   R=   R<   R?   R   t   visitt   set(   RA   R   R   R   R   (    (   R   RA   R   R   s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR~     s
    c            s     f d     |  } x t  t t   |      D]` \ } } t | i  o d | d } n d | d } | i | |  i t |   t  } q4 W| S(   s&   Rename auto-generated unique variablesc            s~   t  |  t  oK t i d |  i  p t i d |  i  o t |  g  Sqz t g   Sn  d   } |  i   | t    Sd  S(   Ns   ^z\d+$s   ^e0\d+$c          W   s   t  t i |   S(    (   t   reducet   operatort   or_(   t   parts(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv     s    (   R=   R<   t   ret   matchR@   R   R   (   Rn   R   (   Rj   (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRj     s    ,	s   e0%si   s   z%s(	   R   t   sortedR_   RV   R@   R~   t   make_VariableExpressionR<   Rw   (   RA   t   resultR   R\   t   newVar(    (   Rj   s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt	   normalize  s    
 	c         C   s   t     d S(   s   
        Recursively visit sub expressions
        @param function: C{Function} to call on each sub expression
        @param combinator: C{Function} to combine the results of the 
        function calls
        @return: result of combination
        N(   R   (   RA   t   functionR   t   default(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   d |  i  i |  f S(   Ns   <%s %s>(   R   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRO     s    c         C   s
   |  i    S(   N(   R>   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRN     s    c         C   s   |  i  d   d   t    S(   s   
        Return a set of all the variables that are available to be replaced.
        This includes free (non-bound) variables as well as predicates.
        @return: C{set} of C{Variable}s
        c         S   s*   t  |  t  o t |  g  p
 |  i   S(    (   R=   R<   R   R   (   Rn   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv   '  s   c          W   s   t  t i |   S(    (   R   R   R   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv   )  s    (   R   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   !  s    c            s"   |  i    f d   d   t    S(   s   
        Return a set of all the free (non-bound) variables in self.  Variables
        serving as predicates are not included.
        @param indvar_only: C{boolean} only return individual variables?
        @return: C{set} of C{Variable}s
        c            s-   t  |  t  o t |  g  p |  i    S(    (   R=   R<   R   t   free(   Rn   (   t   indvar_only(    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv   2  s   c          W   s   t  t i |   S(    (   R   R   R   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv   4  s    (   R   R   (   RA   R   (    (   R   s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   +  s    c            s(     f d   }   i  d   | t    S(   sD   
        @return: beta-converted version of this expression
        c            sS   t  |  d j o   i |   Sn, t  |  d j o   i |  | d  Sn d  S(   Ni    i   (   R   R   (   R   R   (   RA   (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   :  s    c         S   s!   t  |  t  o |  p
 |  i   S(    (   R=   R<   R   (   Rn   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv   @  s   (   R   R   (   RA   R   (    (   RA   s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   6  s    c         C   s
   t  |  S(   N(   R^   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   C  s    (    R   R   R   R   R   R   R   R   R   R   R   RD   RE   RS   R   RM   RK   R   R   Rh   R   RW   R~   R   R   RO   RN   R   Rw   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   s  s4   													
		
			
	R   c           B   s   e  Z d  Z d   Z d   Z e d  Z d   Z e e  Z	 e
 e d  Z d   Z d   Z d   Z e i d	  Z d
   Z e d   d d Z RS(   so  
    This class is used to represent two related types of logical expressions.
    
    The first is a Predicate Expression, such as "P(x,y)".  A predicate 
    expression is comprised of a C{FunctionVariableExpression} or 
    C{ConstantExpression} as the predicate and a list of Expressions as the 
    arguments.
    
    The second is a an application of one expression to another, such as 
    "(\x.dog(x))(fido)".
    
    The reason Predicate Expressions are treated as Application Expressions is
    that the Variable Expression predicate of the expression may be replaced 
    with another Expression, such as a LambdaExpression, which would mean that 
    the Predicate should be thought of as being applied to the arguments.
    
    The LogicParser will always curry arguments in a application expression.
    So, "\x y.see(x,y)(john,mary)" will be represented internally as 
    "((\x y.(see(x))(y))(john))(mary)".  This simplifies the internals since 
    there will always be exactly one argument in an application.
    
    The str() method will usually print the curried forms of application 
    expressions.  The one exception is when the the application expression is
    really a predicate expression (ie, underlying function is an 
    C{AbstractVariableExpression}).  This means that the example from above  
    will be returned as "(\x y.see(x,y)(john))(mary)".
    c         C   sR   t  | t  p t d |  t  | t  p t d |  | |  _ | |  _ d S(   s   
        @param function: C{Expression}, for the function expression
        @param argument: C{Expression}, for the argument   
        s   %s is not an ExpressionN(   R=   R   R?   R   t   argument(   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB   c  s    	c         C   sb   |  i  i   } |  i i   } t | t  o  | i i | i |  i   Sn |  i | |  Sd  S(   N(	   R   R   R   R=   t   LambdaExpressiont   termR~   R   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   m  s
     c         C   sK   t  |  i t  o |  i i |  Sn! |  i i |  |  i i |  BSd S(   s   @see: Expression.free()N(   R=   R   t   AbstractVariableExpressionR   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   u  s    c         C   s/   t  |  i i t  o |  i i i Sn t Sd  S(   N(   R=   R   RG   Rd   Rf   Rh   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt	   _get_type}  s    c         C   s   t  | t  p t  | d j o t t  } n |  i i t |  y& |  i	 i t
 |  i i |  |  WnO t j
 oC } t d |  i	 |  i	 i |  i |  i i |  i	 i i f   n Xd S(   s   @see Expression._set_type()sq   The function '%s' is of type '%s' and cannot be applied to '%s' of type '%s'.  Its argument must match type '%s'.N(   R=   Rc   R?   RS   R    R_   R   R   Rh   R   Rd   RG   R   R   Re   (   RA   R   R   Rn   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    &c   
      C   s  t  | t  p t d |  |  i   \ } } t  | t  p |  i } |  i g } n g  } | g | D] } | | i |  qk ~ } g  } x] | D]U } | t j oB | o* x4 | D] }	 | i	 |	  o Pq q Wq | i
 |  q q Wt |  d j o t |  d Sn t Sd S(   s   @see Expression.findtype()s   %s is not a Variablei   i    N(   R=   R<   R?   t   uncurryR   R   R   R   Rh   Rg   R   R   R_   (
   RA   R   R   t   argsR   t   argt   foundt   uniqueRj   t   u(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s&    	1  c         C   s   | | |  i   | |  i   S(   s   @see: Expression.visit()(   R   R   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s3   t  | t  o# |  i | i j o |  i | i j S(   N(   R=   R   R   R   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD     s    c   	      C   s@  |  i    \ } } t | t  o7 d i g  } | D] } | | i |  q3 ~  } n |  i } |  i i |  } | i |  } t } t | t  oX t | i	 t
  o$ t | i	 i t  p
 t } q qt | i	 t  p
 t } qn t | t
  o
 t } n | o t i | t i } n | t i | t i S(   NR
   (   R   R=   R   t   joinR>   R   R   RW   R   R   R   Rw   t   BooleanExpressionR   R   R   (	   RA   t   syntaxR   R   R   R   t   arg_strt   function_strt   parenthesize_function(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>     s&    7	

c         C   sS   |  i  } |  i g } x1 t | t  o  | i d | i  | i  } q W| | f S(   sp   
        Uncurry this application expression
        
        return: A tuple (base-function, arg-list)
        i    (   R   R   R=   R   t   insert(   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    	c         C   s   |  i    d  S(   i   (   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv     s    t   docs   uncurried arg-list(   R   R   R   RB   R   Rw   R   R   Rz   RG   Rh   RS   R   R   R   RD   R   R   R>   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   G  s   	
						R   c           B   se   e  Z d  Z d   Z d   Z e d  Z e e d  Z	 d   Z
 d   Z d   Z e i d  Z RS(	   sD   This class represents a variable to be used as a predicate or entityc         C   s+   t  | t  p t d |  | |  _ d S(   s@   
        @param variable: C{Variable}, for the variable
        s   %s is not a VariableN(   R=   R<   R?   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB     s    c         C   s   |  S(   N(    (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s\   t  | t  p t d |  t  | t  p t d |  |  i | j o | Sn |  Sd S(   s   @see: Expression.replace()s   %s is not an Variables   %s is not an ExpressionN(   R=   R<   R?   R   R   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR~     s
    c         C   s   t  | t  p t  | d j o t t  } n | } xA | |  i i D]/ } | i i	 |  } | p t
 |    qK qK W| |  i i i |   x! | |  i i D] } | | _ q Wd S(   s   @see Expression._set_type()N(   R=   Rc   R?   RS   R    R_   R   R@   RG   Ri   R   R   (   RA   R   R   t
   resolutionR   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s      c         C   sA   t  | t  p t d |  |  i | j o |  i Sn t Sd S(   s   @see Expression.findtype()s   %s is not a VariableN(   R=   R<   R?   R   RG   Rh   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   | | |  i    S(   s   @see: Expression.visit()(   R   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s    t  | t  o |  i | i j S(   sT   Allow equality between instances of C{AbstractVariableExpression} 
        subtypes.(   R=   R   R   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD     s    c         C   s   t  |  i  S(   N(   R>   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>     s    (   R   R   R   RB   R   RW   R~   Rh   RS   R   R   R   RD   R   R   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s   						t   IndividualVariableExpressionc           B   s/   e  Z d  Z e e d  Z e d   e  Z RS(   s   This class represents variables that take the form of a single lowercase
    character (other than 'e') followed by zero or more digits.c         C   su   t  | t  p t  | d j o t t  } n | i t  p t |  | t   n | |  i	 i
 i |   d S(   s   @see Expression._set_type()N(   R=   Rc   R?   RS   R    R_   Rg   R   R   R   R@   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   $  s    c         C   s   t  S(    (   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv   0  s    (   R   R   R   Rh   RS   R   Rz   RG   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   !  s   t   FunctionVariableExpressionc           B   s    e  Z d  Z e Z e d  Z RS(   sw   This class represents variables that take the form of a single uppercase
    character followed by zero or more digits.c         C   s&   | p t  |  i g  Sn t    Sd S(   s   @see: Expression.free()N(   R   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   7  s    (   R   R   R   Rh   RG   Rw   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   2  s   t   EventVariableExpressionc           B   s   e  Z d  Z e Z RS(   s{   This class represents variables that take the form of a single lowercase
    'e' character followed by zero or more digits.(   R   R   R   t
   EVENT_TYPERG   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   >  s   t   ConstantExpressionc           B   s/   e  Z d  Z e Z e e d  Z e d  Z	 RS(   st   This class represents variables that do not take the form of a single
    character followed by zero or more digits.c         C   s   t  | t  p t  | d j o t t  } n | t j o
 t } n- | } |  i t j o | i	 |  i  } n xA | |  i
 i D]/ } | i i	 |  } | p t |    q q W| |  i
 i i |   x! | |  i
 i D] } | | _ q Wd S(   s   @see Expression._set_type()N(   R=   Rc   R?   RS   R    R_   Rh   R   RG   Ri   R   R@   R   R   (   RA   R   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   H  s"    
  c         C   s&   | p t  |  i g  Sn t    Sd S(   s   @see: Expression.free()N(   R   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   `  s    (
   R   R   R   R   RG   Rh   RS   R   Rw   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   C  s   c         C   s   t  |  t  p t d |   t |  i  o t |   SnG t |  i  o t |   Sn) t |  i  o t	 |   Sn t
 |   Sd S(   s   
    This is a factory method that instantiates and returns a subtype of 
    C{AbstractVariableExpression} appropriate for the given variable.
    s   %s is not a VariableN(   R=   R<   R?   RT   R@   R   RU   R   RV   R   R   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR^   h  s    t   VariableBinderExpressionc           B   s\   e  Z d  Z d   Z e d  Z d   Z d   Z e d  Z	 d   Z
 d   Z d   Z RS(	   s   This an abstract class for any Expression that binds a variable in an
    Expression.  This includes LambdaExpressions and Quantified Expressionsc         C   sR   t  | t  p t d |  t  | t  p t d |  | |  _ | |  _ d S(   sq   
        @param variable: C{Variable}, for the variable
        @param term: C{Expression}, for the term
        s   %s is not a Variables   %s is not an ExpressionN(   R=   R<   R?   R   R   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB   {  s    	c         C   s   t  | t  p t d |  t  | t  p t d |  |  i | j oV | oG t  | t  p t d |  |  i | i |  i i | | t	   Sq |  Sn[ |  i | i
   j o |  i t d |  i   }  n |  i |  i |  i i | | |   Sd S(   s   @see: Expression.replace()s   %s is not a Variables   %s is not an Expressions&   %s is not a AbstractVariableExpressionRY   N(   R=   R<   R?   R   R   R   R   R   R~   Rw   R   t   alpha_convertR]   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR~     s    c         C   sI   t  | t  p t d |  |  i | |  i i |  i t |  t   S(   s   Rename all occurrences of the variable introduced by this variable
        binder in the expression to @C{newvar}.
        @param newvar: C{Variable}, for the new variable
        s   %s is not a Variable(	   R=   R<   R?   R   R   R~   R   R^   Rw   (   RA   t   newvar(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s
    		c         C   s   |  i  i   t |  i g  S(   s   @see: Expression.variables()(   R   R   R   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s    |  i  i |  t |  i g  S(   s   @see: Expression.free()(   R   R   R   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   sJ   t  | t  p t d |  | |  i j o t Sn |  i i |  Sd S(   s   @see Expression.findtype()s   %s is not a VariableN(   R=   R<   R?   R   Rh   R   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   | | |  i   | |  i   S(   s   @see: Expression.visit()(   R   R   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   t  |  | i  p t  | |  i  oY |  i | i j o |  i | i j Sq t |  i  } |  i | i i | i |  j Sn t Sd S(   s   Defines equality modulo alphabetic variance.  If we are comparing 
        \x.M  and \y.N, then check equality of M and N[x/y].N(   R=   R   R   R   R^   R~   RW   (   RA   RC   t   varex(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD     s    #(   R   R   R   RB   RW   R~   R   R   Rw   R   R   R   RD   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   x  s   	
				R   c           B   s5   e  Z e d     Z e e d  Z e i d  Z	 RS(   c         C   s"   t  |  i i |  i  |  i i  S(    (   Rd   R   R   R   RG   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv     s    c         C   st   t  | t  p t  | d j o t t  } n |  i i | i |  |  i	 i
 |  p t |  |   n d S(   s   @see Expression._set_type()N(   R=   Rc   R?   RS   R    R_   R   R   Rf   RG   Ri   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   |  i  g } |  i } | t i j o8 x5 | i |  i j o | i | i   | i } q( Wn t i | d i d   | D  t i | | i	 |  S(   NR   c         s   s   x |  ] } t  |  Vq Wd  S(   N(   R>   (   t   .0R\   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pys	   <genexpr>  s    (
   R   R   R   R   R   R   R   R   R   R>   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>     s    	(
   R   R   Rz   RG   Rh   RS   R   R   R   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s   t   QuantifiedExpressionc           B   s5   e  Z e d     Z e e d  Z e i d  Z	 RS(   c         C   s   t  S(    (   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv     s    c         C   sq   t  | t  p t  | d j o t t  } n | i t  p t |  | t   n |  i	 i
 t |  d S(   s   @see Expression._set_type()N(   R=   Rc   R?   RS   R    R_   Rg   R   R   R   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   |  i  g } |  i } | t i j o8 x5 | i |  i j o | i | i   | i } q( Wn |  i |  d d i d   | D  t i | | i	 |  S(   NR   c         s   s   x |  ] } t  |  Vq Wd  S(   N(   R>   (   R   R\   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pys	   <genexpr>  s    (
   R   R   R   R   R   R   t   getQuantifierR   R   R>   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>     s    	(
   R   R   Rz   RG   Rh   RS   R   R   R   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s   t   ExistsExpressionc           B   s   e  Z e i d   Z RS(   c         C   s   t  i | S(   N(   R   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    (   R   R   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s   t   AllExpressionc           B   s   e  Z e i d   Z RS(   c         C   s   t  i | S(   N(   R   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    (   R   R   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s   R   c           B   sb   e  Z d    Z e d    Z e e d  Z d   Z d   Z	 d   Z
 d   Z e i d  Z RS(   c         C   s+   t  | t  p t d |  | |  _ d  S(   Ns   %s is not an Expression(   R=   R   R?   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB     s    c         C   s   t  S(    (   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv     s    c         C   sq   t  | t  p t  | d j o t t  } n | i t  p t |  | t   n |  i	 i
 t |  d S(   s   @see Expression._set_type()N(   R=   Rc   R?   RS   R    R_   Rg   R   R   R   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s.   t  | t  p t d |  |  i i |  S(   Ns   %s is not a Variable(   R=   R<   R?   R   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   | | |  i    S(   s   @see: Expression.visit()(   R   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   |  i  S(   s   @see: Expression.negate()(   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   !  s    c         C   s    t  | t  o |  i | i j S(   N(   R=   R   R   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD   %  s    c         C   s\   | t  i j o- t  i | t  i |  i i |  t  i Sn t  i | |  i i |  Sd  S(   N(   R   R   R    R   R   R>   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>   (  s    !(   R   R   RB   Rz   RG   Rh   RS   R   R   R   R   RD   R   R   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s   					t   BinaryExpressionc           B   sJ   e  Z d    Z e d    Z d   Z d   Z d   Z e i	 d  Z
 RS(   c         C   sR   t  | t  p t d |  t  | t  p t d |  | |  _ | |  _ d  S(   Ns   %s is not an Expression(   R=   R   R?   Re   Rf   (   RA   Re   Rf   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB   1  s    	c         C   s   t  S(    (   R   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRv   7  s    c         C   s   t  | t  p t d |  |  i i |  } |  i i |  } | | j p | t j o | Sn | t j o | Sn t Sd S(   s   @see Expression.findtype()s   %s is not a VariableN(   R=   R<   R?   Re   R   Rf   Rh   (   RA   R   Rj   Rk   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   9  s    c         C   s   | | |  i   | |  i   S(   s   @see: Expression.visit()(   Re   Rf   (   RA   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   E  s    c         C   sI   t  |  | i  p t  | |  i  o# |  i | i j o |  i | i j S(   N(   R=   R   Re   Rf   (   RA   RC   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRD   I  s    c         C   sC   t  i |  i i |  d |  i |  d |  i i |  t  i S(   NR   (   R   R   Re   R>   t   getOpRf   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR>   N  s    (   R   R   RB   Rz   RG   R   R   RD   R   R   R>   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   0  s   				R   c           B   s   e  Z e e d   Z RS(   c         C   s   t  | t  p t  | d j o t t  } n | i t  p t |  | t   n |  i	 i
 t |  |  i i
 t |  d S(   s   @see Expression._set_type()N(   R=   Rc   R?   RS   R    R_   Rg   R   R   Re   R   Rf   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   T  s    (   R   R   Rh   RS   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   S  s   R   c           B   s   e  Z d  Z e i d  Z RS(   s"   This class represents conjunctionsc         C   s   t  i | S(   N(   R   R!   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   b  s    (   R   R   R   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   `  s   R   c           B   s   e  Z d  Z e i d  Z RS(   s"   This class represents disjunctionsc         C   s   t  i | S(   N(   R   R"   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   g  s    (   R   R   R   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   e  s   R   c           B   s   e  Z d  Z e i d  Z RS(   s"   This class represents implicationsc         C   s   t  i | S(   N(   R   R#   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   l  s    (   R   R   R   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   j  s   R   c           B   s   e  Z d  Z e i d  Z RS(   s$   This class represents biconditionalsc         C   s   t  i | S(   N(   R   R$   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   q  s    (   R   R   R   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   o  s   t   EqualityExpressionc           B   s,   e  Z d  Z e e d  Z e i d  Z RS(   s:   This class represents equality expressions like "(x = y)".c         C   s   t  | t  p t  | d j o t t  } n | i t  p t |  | t   n |  i	 i
 t |  |  i i
 t |  d S(   s   @see Expression._set_type()N(   R=   Rc   R?   RS   R    R_   Rg   R   R   Re   R   R   Rf   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   w  s    c         C   s   t  i | S(   N(   R   R%   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    (	   R   R   R   Rh   RS   R   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   u  s   t   LogicParserc           B   s(  e  Z d  Z e d  Z e d  Z d   Z d   Z d   Z	 d   Z
 e d  Z d   Z e d	  Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  d   Z! d   Z" d   Z# RS(   s$   A lambda calculus expression parser.c         C   s?   t  | t  p t  d |  _ g  |  _ | |  _ g  |  _ d S(   sj   
        @param type_check: C{boolean} should type checking be performed?
        to their types.
        i    N(   R=   t   boolR?   t   _currentIndext   _buffert
   type_checkt   quote_chars(   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB     s
    			c         C   so   d |  _  |  i |  |  _ |  i   } |  i d  o t |  i d    n |  i o | i |  n | S(   s   
        Parse the expression.

        @param data: C{str} for the input to be parsed
        @param signature: C{dict<str, str>} that maps variable names to type 
        strings
        @returns: a parsed Expression
        i    (	   R   t   processR   t   parse_Expressiont   inRanget   UnexpectedTokenExceptiont   tokenR   R   (   RA   t   dataR   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   parse  s    		
c   	      C   s}  g  } t  |  i    } d } x@| o8|  i |  \ } } | o | | 7} q! n | } | d } d } xT | | j oF | | 7} | | } t |  t |  j o | t |  } qn Pqn Wt  i | j o? | o | i |  d } n | i |  | t |  } q! | d d j o" | o | i |  d } qRn | | d 7} | d } q! W| o | i |  n | S(   s   Split the data into tokensR{   i    s    	
i   (   t
   StringTriet   get_all_symbolst   process_quoted_tokenR   t   LEAFR   (	   RA   R  t   outt	   tokenTrieR  t   quoted_tokent   stt   ct   symbol(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR    s@    





c   	      C   sp  d } | d } d } xI|  i  D]>\ } } } } | | j o| o | | 7} n d } x | | | j o | | | j o\ | o | | | 7} n | d 7} t |  | j o t d |   n | | | 7} n | | | 7} | d 7} t |  | j o t d |   q] q] W| o | | | 7} n | d 7} | p t d   n Pq  q  W| | | f S(   NR{   i    i   s:   End of input reached.  Escape character [%s] found at end.s%   End of input reached.  Expected: [%s]s   Empty quoted token found(   R   R   R   (	   RA   R  R  R  R   t   startt   endt   escapet   incl_quotes(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR
    s<    

 


	c         C   s   t  i S(   s#   This method exists to be overridden(   R   R+   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR	    s    c         C   s   |  i  | t |  i  j  S(   s6   Return TRUE if the given location is within the buffer(   R   R   R   (   RA   t   location(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR    s    c         C   so   yL | d j o# |  i |  i } |  i d 7_ n |  i |  i | } | SWn t j
 o t d  n Xd S(   s   Get the next waiting token.  If a location is given, then 
        return the token at currentIndex+location without advancing
        currentIndex; setting it gives lookahead/lookback capability.i   s   More tokens expected.N(   RS   R   R   t
   IndexErrorR   (   RA   R  t   tok(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR    s    c         C   s   | t  i j S(   N(   R   R*   (   RA   R  (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt
   isvariable  s    c         C   s_   |  i    } |  i |  } | p t |   n | o" |  i |  } |  i |  } n | S(   sA   Parse the next complete expression from the stream and return it.(   R  t   handleR  t   attempt_ApplicationExpressiont   attempt_BooleanExpression(   RA   t   allow_adjunctsR  R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR    s    c         C   s   |  i  |  o |  i |  Sn | t i j o |  i   Snd | t i j o |  i |  SnC | t i j o |  i |  Sn" | t i	 j o |  i
 |  Sn d S(   sh   This method is intended to be overridden for logics that 
        use different operators or expressionsN(   R  t   handle_variableR   R    t   handle_negationR   t   handle_lambdaR(   t   handle_quantR   t   handle_open(   RA   R  (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR  &  s    c         C   s   |  i  |  i t   S(   N(   t   make_NegatedExpressionR  RW   (   RA   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR  8  s    c         C   s
   t  |  S(   N(   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR#  ;  s    c         C   s   |  i  |  } |  i d  o |  i d  t i j o t | t  o t d |   n |  i   |  i | |  i	    } x@ |  i d  t i
 j o& |  i   |  i | |  i	    } q W|  i |  i   t i  n |  i |  S(   Ni    sW   '%s' is an illegal predicate name.  Individual variables may not be used as predicates.(   R   R  R  R   R   R=   R   R   t   make_ApplicationExpressionR  R   t   assertTokenR   t   attempt_EqualityExpression(   RA   R  R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR  >  s    )
	
	c         C   s4   t  t t |   t  o t d |   n d  S(   NsC   '%s' is an illegal variable name.  Constants may not be abstracted.(   R=   R^   R<   R   R   (   RA   R  (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   ensure_abstractableU  s    c         C   s   |  i  |  i d   t |  i    g } xM |  i |  i d   o3 |  i  |  i d   | i t |  i     q. W|  i |  i   t i  |  i t	  } x$ | o |  i
 | i   |  } q W| S(   Ni    (   R'  R  R<   R  R   R%  R   R   R  RW   t   make_LambdaExpressiont   pop(   RA   R  R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR   Z  s    
c         C   s   |  i  |  } |  i   g } x1 |  i |  i d   o | i |  i    q! W|  i |  i   t i  |  i t  } xf | o^ | i	   } |  i
 |  } t | t  o t d |   n |  i | t |  |  } q} W| S(   Ni    sN   '%s' is an illegal variable name.  Constant expressions may not be quantified.(   t    get_QuantifiedExpression_factoryR  R  R   R%  R   R   R  RW   R)  R   R=   R   R   t   make_QuanifiedExpressionR<   (   RA   R  t   factoryR   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR!  i  s    
 c         C   sG   | t  i j o t Sn, | t  i j o t Sn |  i | t  i  d S(   s\   This method serves as a hook for other logic parsers that
        have different quantifiersN(   R   R   R   R   R   R%  R(   (   RA   R  (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR*  ~  s
    c         C   s   | | |  S(   N(    (   RA   R,  R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR+    s    c         C   s)   |  i    } |  i |  i   t i  | S(   N(   R  R%  R  R   R   (   RA   R  R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR"    s    c         C   s   |  i  d  o= |  i d  t i j o$ |  i   |  i | |  i    SnW |  i  d  oF |  i d  t i j o- |  i   |  i |  i | |  i     Sn | S(   s   Attempt to make an equality expression.  If the next token is an 
        equality operator, then an EqualityExpression will be returned.  
        Otherwise, the parameter will be returned.i    (   R  R  R   R%   t   make_EqualityExpressionR  R&   R#  (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR&    s    )
	)
	c         C   s   t  | |  S(   sl   This method serves as a hook for other logic parsers that
        have different equality expression classes(   R   (   RA   Re   Rf   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR-    s    c         C   s^   |  i  d  oJ |  i |  i d   } | o' |  i   |  i | | |  i    SqZ n | S(   s   Attempt to make a boolean expression.  If the next token is a boolean 
        operator, then a BooleanExpression will be returned.  Otherwise, the 
        parameter will be returned.i    (   R  t   get_BooleanExpression_factoryR  t   make_BooleanExpressionR  (   RA   R   R,  (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR    s    
!c         C   sh   | t  i j o t SnM | t  i j o t Sn5 | t  i j o t Sn | t  i j o t Sn d Sd S(   sb   This method serves as a hook for other logic parsers that
        have different boolean operatorsN(
   R   R!   R   R"   R   R#   R   R$   R   RS   (   RA   R  (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR.    s    c         C   s   | | |  S(   N(    (   RA   R,  Re   Rf   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR/    s    c         C   s   |  i  d  o |  i d  t i j o t | t  o/ t | t  o t d t |  d   n |  i   |  i	 | |  i
    } x@ |  i d  t i j o& |  i   |  i	 | |  i
    } q W|  i |  i   t i  |  i |  Sn | Sd S(   s   Attempt to make an application expression.  The next tokens are
        a list of arguments in parens, then the argument expression is a
        function being applied to the arguments.  Otherwise, return the
        argument expression.i    s   The function 'sV    is not a Lambda Expression or an Application Expression, so it may not take argumentsN(   R  R  R   R   R=   R   R   R   R>   R$  R  R   R%  R   R  (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR    s    )
	
	c         C   s   t  | |  S(   N(   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR$    s    c         C   s   t  t |   S(   N(   R^   R<   (   RA   R@   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    c         C   s   t  | |  S(   N(   R   (   RA   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR(    s    c         C   sX   t  | t  o$ | | j o t | |   qT n! | | j o t | |   n d  S(   N(   R=   R_   R  (   RA   R  t   expected(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR%    s
    c         C   sG   |  i  d  o d |  i d  } n d } d |  i i d | d S(   Ni    s   Next token: s   No more tokensR|   s   : R}   (   R  R  R   R   (   RA   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRO     s    ($   R   R   R   RW   RB   RS   R  R  R
  R	  R  R  R  Rw   R  R  R  R#  R  R'  R   R!  R*  R+  R"  R&  R-  R  R.  R/  R  R$  R   R(  R%  RO   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s>   	(	!											
												R  c           B   s#   e  Z d  Z e d  Z d   Z RS(   s   <leaf>c         C   s=   t  i |  t  | o" x | D] } |  i |  q Wn d  S(   N(   R    RB   R  R   (   RA   t   stringst   string(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB     s
     c         C   s;   t  |  o |  | d i | d  n d  |  t i <d  S(   Ni    i   (   R   R   RS   R  R  (   RA   R2  (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s    (   R   R   R  RS   RB   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR    s   R   c           B   s   e  Z d    Z RS(   c         C   s   t  i |  |  d  S(   N(   R   RB   (   RA   t   message(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB     s    (   R   R   RB   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR     s   R  c           B   s   e  Z e d   Z RS(   c         C   s=   | o t  i |  d | | f  n t  i |  d |  d  S(   Ns7   parse error, unexpected token: '%s'. Expected token: %ss#   parse error, unexpected token: '%s'(   R   RB   (   RA   R  R0  (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRB   
  s
    (   R   R   RS   RB   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyR  	  s   c         C   s.   t  |  t  p t d |   t i d |   S(   s   
    An individual variable must be a single lowercase character other than 'e',
    followed by zero or more digits.
    
    @param expr: C{str}
    @return: C{boolean} True if expr is of the correct form 
    s   %s is not a strings   ^[a-df-z]\d*$(   R=   R>   R?   R   R   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRT     s    c         C   s.   t  |  t  p t d |   t i d |   S(   s   
    A function variable must be a single uppercase character followed by
    zero or more digits.
    
    @param expr: C{str}
    @return: C{boolean} True if expr is of the correct form 
    s   %s is not a strings
   ^[A-Z]\d*$(   R=   R>   R?   R   R   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRU     s    c         C   s.   t  |  t  p t d |   t i d |   S(   s   
    An event variable must be a single lowercase 'e' character followed by
    zero or more digits.
    
    @param expr: C{str}
    @return: C{boolean} True if expr is of the correct form 
    s   %s is not a strings   ^e\d*$(   R=   R>   R?   R   R   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyRV   )  s    c          C   s5  t    i }  d d d GH|  d  GH|  d  GH|  d  GH|  d  GH|  d  GH|  d	  GH|  d
  GH|  d  GH|  d  GH|  d  GH|  d  GH|  d  GHd d d GH|  d  i   GH|  d  i   GH|  d  i   GH|  d  i   GHd d d GH|  d  } | GH| i t d   } | GH| | j GHd  S(   NR   i   s   Test parsert   johns   man(x)s   -man(x)s   (man(x) & tall(x) & walks(x))s   exists x.(man(x) & tall(x))s	   \x.man(x)s   \x.man(x)(john)s   \x y.sees(x,y)s   \x y.sees(x,y)(a,b)s   (\x.exists y.walks(x,y))(x)s   exists x.(x = y)s   \P Q.exists x.(P(x) & Q(x))s   Test simplifys   \x.\y.sees(x,y)(john)(mary)s   \x.\y.sees(x,y)(john, mary)s,   all x.(man(x) & (\x.exists y.walks(x,y))(x))s5   (\P.\Q.exists x.(P(x) & Q(x)))(\x.dog(x))(\x.bark(x))s4   Test alpha conversion and binder expression equalitys   exists x.P(x)RP   s   ====================s   ====================s   ====================s   ====================s   ====================s   ====================(   R   R  R   R   R<   (   t   pt   e1t   e2(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt   demo5  s2    c         C   s    |  i    d t  |  i  GHd  S(   Ns    : (   R>   RG   (   t   ex(    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pyt	   printtypeR  s    t   __main__(C   R   R   R   t   nltk.compatR    t   nltk.internalsR   RX   t   objectR   R4   R7   R;   R<   RS   R]   Rb   Rc   Rd   Rl   Rm   Rp   Rs   Ru   R   R   R   Rh   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R^   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R   R  RT   RU   RV   R8  R:  R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/sem/logic.pys   <module>   s~   	(				.					
<%	R)# n
					