ó
ù`]c           @  sÂ  d  Z  d d l m Z m Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z d d l m Z d	 e f d
 „  ƒ  YZ  d „  Z! d e f d „  ƒ  YZ" d e f d „  ƒ  YZ# d e$ f d „  ƒ  YZ% d e f d „  ƒ  YZ& e d e' f d „  ƒ  Yƒ Z( d „  Z) d „  Z* d „  Z+ d „  Z, d „  Z- d „  Z. d „  Z/ e0 d k r¾e/ ƒ  n  d S(   uÆ   
A module to perform nonmonotonic reasoning.  The ideas and demonstrations in
this module are based on "Logical Foundations of Artificial Intelligence" by
Michael R. Genesereth and Nils J. Nilsson.
iÿÿÿÿ(   t   print_functiont   unicode_literals(   t   defaultdict(   t   reduce(   t   Prover9t   Prover9Command(   t   VariableExpressiont   EqualityExpressiont   ApplicationExpressiont
   Expressiont   AbstractVariableExpressiont   AllExpressiont   BooleanExpressiont   NegatedExpressiont   ExistsExpressiont   Variablet   ImpExpressiont   AndExpressiont   unique_variablet   operator(   t   Provert   ProverCommandDecorator(   t   python_2_unicode_compatiblet   ProverParseErrorc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR   )   s   c         C  sC   |  d  k r | } n | |  g } t t j d „  | Dƒ t ƒ  ƒ S(   Nc         s  s   |  ] } | j  ƒ  Vq d  S(   N(   t	   constants(   t   .0t   a(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pys	   <genexpr>2   s    (   t   NoneR   R   t   or_t   set(   t   goalt   assumptionst   all_expressions(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt
   get_domain-   s    	t   ClosedDomainProverc           B  s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   u]   
    This is a prover decorator that adds domain closure assumptions before
    proving.
    c         C  sc   g  |  j  j ƒ  D] } | ^ q } |  j  j ƒ  } t | | ƒ } g  | D] } |  j | | ƒ ^ qG S(   N(   t   _commandR!   R    R#   t   replace_quants(   t   selfR   R!   R    t   domaint   ex(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR!   ;   s    "c         C  s7   |  j  j ƒ  } t | |  j  j ƒ  ƒ } |  j | | ƒ S(   N(   R%   R    R#   R!   R&   (   R'   R    R(   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR    A   s    c         C  sR  t  | t ƒ ru g  | D]$ } | j j | j t | ƒ ƒ ^ q } g  | D] } |  j | | ƒ ^ qG } t d „  | ƒ St  | t ƒ r² | j	 |  j | j
 | ƒ |  j | j | ƒ ƒ St  | t ƒ rÕ |  j | j | ƒ St  | t ƒ rJg  | D]$ } | j j | j t | ƒ ƒ ^ që } g  | D] } |  j | | ƒ ^ q} t d „  | ƒ S| Sd S(   uÞ  
        Apply the closed domain assumption to the expression
         - Domain = union([e.free()|e.constants() for e in all_expressions])
         - translate "exists x.P" to "(z=d1 | z=d2 | ... ) & P.replace(x,z)" OR
                     "P.replace(x, d1) | P.replace(x, d2) | ..."
         - translate "all x.P" to "P.replace(x, d1) & P.replace(x, d2) & ..."
        :param ex: ``Expression``
        :param domain: set of {Variable}s
        :return: ``Expression``
        c         S  s   |  | @S(   N(    (   t   xt   y(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   <lambda>V   t    c         S  s   |  | BS(   N(    (   R*   R+   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR,   c   R-   N(   t
   isinstanceR   t   termt   replacet   variableR   R&   R   R   t	   __class__t   firstt   secondR   R   (   R'   R)   R(   t   dt	   conjunctst   ct	   disjuncts(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR&   F   s    1%1%(   R   R   t   __doc__R!   R    R&   (    (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR$   5   s   		t   UniqueNamesProverc           B  s   e  Z d  Z d „  Z RS(   u[   
    This is a prover decorator that adds unique names assumptions before
    proving.
    c         C  s+  |  j  j ƒ  } t t |  j  j ƒ  | ƒ ƒ } t ƒ  } xI | D]A } t | t ƒ r= | j j	 } | j
 j	 } | | j | ƒ q= q= Wg  } x˜ t | ƒ D]Š \ } } x{ | | d D]k }	 |	 | | k r° t t | ƒ t |	 ƒ ƒ }
 t ƒ  j |
 | ƒ r
| | j |	 ƒ q| j |
 ƒ q° q° Wq• W| | S(   u¦   
         - Domain = union([e.free()|e.constants() for e in all_expressions])
         - if "d1 = d2" cannot be proven from the premises, then add "d1 != d2"
        i   (   R%   R!   t   listR#   R    t	   SetHolderR.   R   R3   R1   R4   t   addt	   enumerateR   R   t   provet   append(   R'   R!   R(   t   eq_setsR   t   avt   bvt   new_assumptionst   it   bt   newEqEx(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR!   n   s$    	(   R   R   R9   R!   (    (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR:   h   s   R<   c           B  s   e  Z d  Z d „  Z RS(   u&   
    A list of sets of Variables.
    c         C  sV   t  | t ƒ s t ‚ x |  D] } | | k r | Sq Wt | g ƒ } |  j | ƒ | S(   uV   
        :param item: ``Variable``
        :return: the set containing 'item'
        (   R.   R   t   AssertionErrorR   R@   (   R'   t   itemt   st   new(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   __getitem__˜   s    (   R   R   R9   RL   (    (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR<   “   s   t   ClosedWorldProverc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   u¡  
    This is a prover decorator that completes predicates before proving.

    If the assumptions contain "P(A)", then "all x.(P(x) -> (x=A))" is the completion of "P".
    If the assumptions contain "all x.(ostrich(x) -> bird(x))", then "all x.(bird(x) -> ostrich(x))" is the completion of "bird".
    If the assumptions don't contain anything that are "P", then "all x.-P(x)" is the completion of "P".

    walk(Socrates)
    Socrates != Bill
    + all x.(walk(x) -> (x=Socrates))
    ----------------
    -walk(Bill)

    see(Socrates, John)
    see(John, Mary)
    Socrates != John
    John != Mary
    + all x.all y.(see(x,y) -> ((x=Socrates & y=John) | (x=John & y=Mary)))
    ----------------
    -see(Socrates, Mary)

    all x.(ostrich(x) -> bird(x))
    bird(Tweety)
    -ostrich(Sam)
    Sam != Tweety
    + all x.(bird(x) -> (ostrich(x) | x=Tweety))
    + all x.-ostrich(x)
    -------------------
    -bird(Sam)
    c         C  sÔ  |  j  j ƒ  } |  j | ƒ } g  } x¥| D]} | | } |  j | ƒ } g  | D] } t | ƒ ^ qQ } g  }	 xf | j D][ }
 g  } x3 t | |
 ƒ D]" \ } } | j t | | ƒ ƒ q• W|	 j t	 d „  | ƒ ƒ qy Wx_ | j
 D]T } i  } x+ t | | d ƒ D] \ } } | | | <qW|	 j | d j | ƒ ƒ qâ W|	 rv|  j | | ƒ } t	 d „  |	 ƒ } t | | ƒ } n t |  j | | ƒ ƒ } x* | d  d  d … D] } t | | ƒ } q¢W| j | ƒ q+ W| | S(   Nc         S  s   |  | @S(   N(    (   R*   R+   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR,   Ù   R-   i    i   c         S  s   |  | BS(   N(    (   R*   R+   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR,   ç   R-   iÿÿÿÿ(   R%   R!   t   _make_predicate_dictt   _make_unique_signatureR   t
   signaturest   zipR@   R   R   t
   propertiest   substitute_bindingst   _make_antecedentR   R   R   (   R'   R!   t
   predicatesRD   t   pt
   predHoldert   new_sigt   vt   new_sig_exsR8   t   sigt   equality_exst   v1t   v2t   propt   bindingst
   antecedentt
   consequentt   accumt   new_sig_var(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR!   Ç   s6    
 c         C  s   t  d „  t | j ƒ Dƒ ƒ S(   u˜   
        This method figures out how many arguments the predicate takes and
        returns a tuple containing that number of unique variables.
        c         s  s   |  ] } t  ƒ  Vq d  S(   N(   R   (   R   RE   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pys	   <genexpr>ù   s    (   t   tuplet   ranget   signature_len(   R'   RW   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRO   ô   s    c         C  s-   | } x  | D] } | t  | ƒ ƒ } q W| S(   u†   
        Return an application expression with 'predicate' as the predicate
        and 'signature' as the list of arguments.
        (   R   (   R'   t	   predicatet	   signatureRa   RY   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRT   û   s    c         C  s1   t  t ƒ } x | D] } |  j | | ƒ q W| S(   uÏ   
        Create a dictionary of predicates from the assumptions.

        :param assumptions: a list of ``Expression``s
        :return: dict mapping ``AbstractVariableExpression`` to ``PredHolder``
        (   R   t
   PredHoldert   _map_predicates(   R'   R!   RU   R   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRN     s    c         C  sØ  t  | t ƒ rM | j ƒ  \ } } t  | t ƒ rÔ| | j t | ƒ ƒ qÔn‡t  | t ƒ r… |  j | j | ƒ |  j | j	 | ƒ nOt  | t
 ƒ rÔ| j g } | j } x, t  | t
 ƒ r× | j | j ƒ | j } q¬ Wt  | t ƒ rÔt  | j t ƒ rÑt  | j	 t ƒ rÑ| j j ƒ  \ } } | j	 j ƒ  \ }	 }
 t  | t ƒ rÎt  |	 t ƒ rÎ| g  | D] } | j ^ q]k rÎ| g  |
 D] } | j ^ qk rÎ| |	 j t | ƒ | j f ƒ | | j | ƒ qÎqÑqÔn  d  S(   N(   R.   R   t   uncurryR
   t
   append_sigRe   R   Rk   R3   R4   R   R1   R/   R@   R   t   append_propt   validate_sig_len(   R'   t
   expressiont   predDictt   funct   argsR[   R/   t   func1t   args1t   func2t   args2RY   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRk     s0    	"" (   R   R   R9   R!   RO   RT   RN   Rk   (    (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRM   §   s   	-		
	Rj   c           B  sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   uŸ  
    This class will be used by a dictionary that will store information
    about predicates to be used by the ``ClosedWorldProver``.

    The 'signatures' property is a list of tuples defining signatures for
    which the predicate is true.  For instance, 'see(john, mary)' would be
    result in the signature '(john,mary)' for 'see'.

    The second element of the pair is a list of pairs such that the first
    element of the pair is a tuple of variables and the second element is an
    expression of those variables that makes the predicate true.  For instance,
    'all x.all y.(see(x,y) -> know(x,y))' would result in "((x,y),('see(x,y)'))"
    for 'know'.
    c         C  s   g  |  _  g  |  _ d  |  _ d  S(   N(   RP   RR   R   Rg   (   R'   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   __init__A  s    		c         C  s!   |  j  | ƒ |  j j | ƒ d  S(   N(   Ro   RP   R@   (   R'   RX   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRm   F  s    c         C  s%   |  j  | d ƒ |  j j | ƒ d  S(   Ni    (   Ro   RR   R@   (   R'   t   new_prop(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRn   J  s    c         C  sI   |  j  d  k r! t | ƒ |  _  n$ |  j  t | ƒ k rE t d ƒ ‚ n  d  S(   Nu   Signature lengths do not match(   Rg   R   t   lent	   Exception(   R'   RX   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRo   N  s    c         C  s   d |  j  |  j |  j f S(   Nu
   (%s,%s,%s)(   RP   RR   Rg   (   R'   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   __str__T  s    c         C  s   d |  S(   Nu   %s(    (   R'   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   __repr__W  s    (	   R   R   R9   Rx   Rm   Rn   Ro   R|   R}   (    (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyRj   0  s   					c    
      C  s–  t  j }  |  d ƒ } |  d ƒ } |  d ƒ } t | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ qu Wt d | j ƒ  ƒ t | j ƒ  ƒ |  d ƒ } |  d ƒ } |  d ƒ } |  d ƒ } t | | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ q*Wt d | j ƒ  ƒ t | j ƒ  ƒ |  d ƒ } |  d ƒ } |  d ƒ } |  d ƒ } t | | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ qßWt d | j ƒ  ƒ t | j ƒ  ƒ |  d ƒ } |  d ƒ } |  d	 ƒ } t | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ q…Wt d | j ƒ  ƒ t | j ƒ  ƒ |  d
 ƒ } |  d ƒ } |  d ƒ } |  d ƒ } |  d ƒ }	 |  d ƒ } t | | | | | |	 g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ qXWt d | j ƒ  ƒ t | j ƒ  ƒ d  S(   Nu   exists x.walk(x)u   man(Socrates)u   walk(Socrates)u   assumptions:u      u   goal:u   -walk(Bill)u
   walk(Bill)u   all x.walk(x)u
   girl(mary)u
   dog(rover)u   all x.(girl(x) -> -dog(x))u   all x.(dog(x) -> -girl(x))u   chase(mary, rover)u1   exists y.(dog(y) & all x.(girl(x) -> chase(x,y)))(   R	   t
   fromstringR   t   printR?   R$   R!   R    (
   t   lexprt   p1t   p2R7   t   provert   cdpR   t   p3t   p4t   p5(    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   closed_domain_demo[  sz    	




c          C  sh  t  j }  |  d ƒ } |  d ƒ } |  d ƒ } t | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ qu Wt d | j ƒ  ƒ t | j ƒ  ƒ |  d ƒ } |  d ƒ } |  d	 ƒ } |  d
 ƒ } t | | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ q*Wt d | j ƒ  ƒ t | j ƒ  ƒ d  S(   Nu   man(Socrates)u	   man(Bill)u   exists x.exists y.(x != y)u   assumptions:u      u   goal:u!   all x.(walk(x) -> (x = Socrates))u   Bill = Williamu   Bill = Billyu   -walk(William)(   R	   R~   R   R   R?   R:   R!   R    (   R€   R   R‚   R7   Rƒ   t   unpR   R…   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   unique_names_demo   s0    	

c    	      C  s;  t  j }  |  d ƒ } |  d ƒ } |  d ƒ } t | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ qu Wt d | j ƒ  ƒ t | j ƒ  ƒ |  d ƒ } |  d ƒ } |  d	 ƒ } |  d
 ƒ } |  d ƒ } t | | | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ q9Wt d | j ƒ  ƒ t | j ƒ  ƒ |  d ƒ } |  d ƒ } |  d ƒ } |  d ƒ } |  d ƒ } t | | | | | g ƒ } t | j ƒ  ƒ t | ƒ } t d ƒ x! | j ƒ  D] } t d | ƒ qýWt d | j ƒ  ƒ t | j ƒ  ƒ d  S(   Nu   walk(Socrates)u   (Socrates != Bill)u   -walk(Bill)u   assumptions:u      u   goal:u   see(Socrates, John)u   see(John, Mary)u   (Socrates != John)u   (John != Mary)u   -see(Socrates, Mary)u   all x.(ostrich(x) -> bird(x))u   bird(Tweety)u   -ostrich(Sam)u   Sam != Tweetyu
   -bird(Sam)(   R	   R~   R   R   R?   RM   R!   R    (	   R€   R   R‚   R7   Rƒ   t   cwpR   R…   R†   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   closed_world_demo½  sL    	


c          C  sŸ   t  j }  |  d ƒ } |  d ƒ } |  d ƒ } t | | | g ƒ } t | j ƒ  ƒ t t t | ƒ ƒ ƒ } x | j ƒ  D] } t | ƒ qw Wt | j ƒ  ƒ d  S(   Nu   see(Socrates, John)u   see(John, Mary)u   -see(Socrates, Mary)(	   R	   R~   R   R   R?   R$   R:   RM   R!   (   R€   R   R‚   R7   Rƒ   t   commandR   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   combination_prover_demoé  s    	c          C  s†  t  j }  g  } | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d	 ƒ ƒ | j |  d
 ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ | j |  d ƒ ƒ t d  | ƒ } t t | ƒ ƒ } x | j ƒ  D] } t | ƒ qGWt	 d | ƒ t	 d | ƒ t	 d | ƒ d  S(   Nu'   all x.(elephant(x)        -> animal(x))u'   all x.(bird(x)            -> animal(x))u%   all x.(dove(x)            -> bird(x))u%   all x.(ostrich(x)         -> bird(x))u(   all x.(flying_ostrich(x)  -> ostrich(x))u)   all x.((animal(x)  & -Ab1(x)) -> -fly(x))u(   all x.((bird(x)    & -Ab2(x)) -> fly(x))u)   all x.((ostrich(x) & -Ab3(x)) -> -fly(x))u#   all x.(bird(x)           -> Ab1(x))u#   all x.(ostrich(x)        -> Ab2(x))u#   all x.(flying_ostrich(x) -> Ab3(x))u   elephant(E)u   dove(D)u
   ostrich(O)u   -fly(E)u   fly(D)u   -fly(O)(
   R	   R~   R@   R   R   R:   RM   R!   R   t   print_proof(   R€   t   premisesRƒ   R   R   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   default_reasoning_demo÷  s4    	c         C  sP   t  j } t | |  ƒ | ƒ } t t | ƒ ƒ } t |  | j ƒ  | j ƒ  ƒ d  S(   N(   R	   R~   R   R:   RM   R   R?   (   R    R   R€   Rƒ   R   (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyR   #  s    	c           C  s'   t  ƒ  t ƒ  t ƒ  t ƒ  t ƒ  d  S(   N(   Rˆ   RŠ   RŒ   RŽ   R‘   (    (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   demo*  s
    u   __main__N(1   R9   t
   __future__R    R   t   collectionsR   t	   functoolsR   t   nltk.inference.prover9R   R   t   nltk.sem.logicR   R   R   R	   R
   R   R   R   R   R   R   R   R   R   t   nltk.inference.apiR   R   t   nltk.compatR   R{   R   R#   R$   R:   R;   R<   RM   t   objectRj   Rˆ   RŠ   RŒ   RŽ   R‘   R   R’   R   (    (    (    s:   lib/python2.7/site-packages/nltk/inference/nonmonotonic.pyt   <module>   s0   ^	3+‰*	E		,		,		