³ò

šiKc           @   s?  d  d k  Z  d  d k Z d  d k Z d  d k l Z d  d k Z d  d k l Z l Z	 d  d k
 l Z l Z h  e d <d d <e d <d	 d
 <d d <d d <d d <d d <d d <Z d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d e e f d „  ƒ  YZ d „  Z d „  Z d  „  Z d! g  f d" g  f d# g  f d$ g  f d% g  f d" g  f d# g  f d% g  f d& g  f d' g  f d( d) d* g f d+ g  f d, g  f d- g  f d. d/ g f d0 d/ g f g Z d1 d2 d3 d4 d5 d6 d7 d8 d9 d: d; g Z d< d= „ Z d> „  Z e d? j o e ƒ  n d S(@   iÿÿÿÿN(   t   join(   t   LogicParsert   Tokens(   t   BaseProverCommandt   Proveri    s   (FATAL)i   i   s
   (MAX_MEGS)i   s   (MAX_SECONDS)i   s   (MAX_GIVEN)i   s
   (MAX_KEPT)i   s   (ACTION)i   s	   (SIGSEGV)ie   t   Prover9CommandParentc           B   s   e  Z d  Z d d „ Z RS(   sé   
    A common base class used by both L{Prover9Command} and L{MaceCommand
    <mace.MaceCommand>}, which is responsible for maintaining a goal and a
    set of assumptions, and generating prover9-style input files from
    them.
    t   nltkc         C   s€   | i  ƒ  d j o  xf |  i ƒ  D] } | GHq  WnJ | i  ƒ  d j o& x3 t |  i ƒ  ƒ D] } | GHqY Wn t d | ƒ ‚ d S(   sC   
        Print the list of the current assumptions.       
        R   t   prover9s*   Unrecognized value for 'output_format': %sN(   t   lowert   assumptionst   convert_to_prover9t	   NameError(   t   selft   output_formatt   a(    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   print_assumptions.   s      (   t   __name__t
   __module__t   __doc__R   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR   '   s   t   Prover9Commandc           B   s/   e  Z d  Z e e d e d „ Z e d „ Z RS(   s¸   
    A L{ProverCommand} specific to the L{Prover9} prover.  It contains
    the a print_assumptions() method that is used to print the list
    of assumptions in multiple formats.
    i<   c         C   s_   | p
 g  } n | d j	 o t | t ƒ p t ‚ n t | ƒ } t i |  | | | ƒ d S(   sÙ  
        @param goal: Input expression to prove
        @type goal: L{logic.Expression}
        @param assumptions: Input expressions to use as assumptions in
            the proof.
        @type assumptions: C{list} of L{logic.Expression}
        @param timeout: number of seconds before timeout; set to 0 for
            no timeout.
        @type timeout: C{int}
        @param prover: a prover.  If not set, one will be created.
        @type prover: C{Prover9}
        N(   t   Nonet
   isinstancet   Prover9t   AssertionErrorR   t   __init__(   R   t   goalR	   t   timeoutt   prover(    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR   B   s    
c         C   s9   | o$ |  i  i | d g ƒ d i ƒ  Sn | i ƒ  Sd S(   s9   
        @see BaseProverCommand.decorate_proof()
        t   striplabelsi    N(   t   _provert   _call_prooftranst   rstrip(   R   t   proof_stringt   simplify(    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   decorate_proofY   s    $(   R   R   R   R   R   t   TrueR"   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR   <   s   t   Prover9Parentc           B   sM   e  Z d  Z e Z e d „ Z d „  Z d „  Z e d „ Z	 g  e d „ Z
 RS(   s¾   
    A common class extended by both L{Prover9} and L{Mace <mace.Mace>}.
    It contains the functionality required to convert NLTK-style
    expressions into Prover9-style expressions.
    c         C   s‹   | d  j o d  |  _ d  |  _ ne d } t i i | d | d d g d d d | | d g d	 | ƒ|  _ |  i i t i i	 d
 ƒ |  _ d  S(   NR   t   path_to_bint   env_varst   PROVER9HOMEt   urls&   http://www.cs.unm.edu/~mccune/prover9/t   binary_namess   .exet   verbosei   (
   R   t   _binary_locationt   _prover9_binR   t	   internalst   find_binaryt   rsplitt   ost   patht   sep(   R   t   binary_locationR*   t   name(    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   config_prover9l   s    			c         C   s   d } | o= | d 7} x" t  | ƒ D] } | d | 7} q$ W| d 7} n | o, | d 7} | d t  | ƒ 7} | d 7} n | S(   sÃ   
        @return: The input string that should be provided to the
        prover9 binary.  This string is formed based on the goal,
        assumptions, and timeout value of this object.
        t    s   formulas(assumptions).
s       %s.
s   end_of_list.

s   formulas(goals).
(   R
   (   R   R   R	   t   st   p9_assumption(    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   prover9_input{   s    
 
c         C   s   d d d d d d g S(   sÀ   
        A list of directories that should be searched for the prover9
        executables.  This list is used by L{config_prover9} when searching
        for the prover9 executables.
        s   /usr/local/bin/prover9s   /usr/local/bin/prover9/bins   /usr/local/bins   /usr/bins   /usr/local/prover9s   /usr/local/share/prover9(    (   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   binary_locations   s    c         C   sk   |  i  ƒ  } |  i d  j	 o | |  i g 7} n t i i | d | d d g d d d | | d g d | ƒS(	   Nt
   searchpathR&   R'   R(   s&   http://www.cs.unm.edu/~mccune/prover9/R)   s   .exeR*   (   R:   R+   R   R   R-   R.   (   R   R4   R*   R:   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   _find_binary   s    	c   	      C   sÊ   | o# d G| GHd G| GHd G| Gd GHn | g | } t  i | d t  i d t  i d t  i ƒ} | i | ƒ \ } } | o@ d G| i GH| o d	 G| Gd GHn | o d
 G| Gd GHq½ n | | i f S(   s@  
        Call the binary with the given input.
    
        @param input_str: A string whose contents are used as stdin.
        @param binary: The location of the binary to call
        @param args: A list of command-line arguments.
        @return: A tuple (stdout, returncode)
        @see: L{config_prover9}
        s   Calling:s   Args:s   Input:
s   
t   stdoutt   stderrt   stdins   Return code:s   stdout:
s   stderr:
(   t
   subprocesst   Popent   PIPEt   STDOUTt   communicatet
   returncode(	   R   t	   input_strt   binaryt   argsR*   t   cmdt   pR=   R>   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   _call¨   s    
			(   R   R   R   R   R+   t   FalseR5   R9   R:   R<   RK   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR$   c   s   		c         C   s®   t  |  t ƒ oa g  } xP |  D]H } y# | i | i ƒ  i t i ƒ ƒ Wq t j
 o d |  GHq Xq W| Sn: y |  i ƒ  i t i ƒ SWn t j
 o d |  GHn Xd S(   s9   
    Convert C{logic.Expression}s to Prover9 format.
    s4   input %s cannot be converted to Prover9 input syntaxN(   R   t   listt   appendR!   t   strt   LogicTokenst   PROVER9R   (   t   inputt   resultR7   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR
   Æ   s     #R   c           B   sY   e  Z e Z e Z d  d „ Z e e e d „ Z d „  Z g  e d „ Z	 g  e d „ Z
 RS(   i<   c         C   s   | |  _  d  S(   N(   t   _timeout(   R   R   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR   Ý   s    	c         C   sH   | p
 g  } n |  i  |  i | | ƒ d | ƒ\ } } | d j | f S(   sú   
        Use Prover9 to prove a theorem.
        @return: A pair whose first element is a boolean indicating if the 
        proof was successful (i.e. returns value of 0) and whose second element
        is the output of the prover.        
        R*   i    (   t   _call_prover9R9   (   R   R   R	   R*   R=   RE   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   _proveã   s
    
c         C   s   d } | t  i |  | | ƒ S(   s3   
        @see: Prover9Parent.prover9_input
        s   clear(auto_denials).
(   R$   R9   (   R   R   R	   R7   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR9   ñ   s    c         C   sw   |  i  t j o |  i d | ƒ |  _  n d } |  i d j o | d |  i 7} n | | 7} |  i | |  i  | | ƒ S(   s  
        Call the C{prover9} binary with the given input.
    
        @param input_str: A string whose contents are used as stdin.
        @param args: A list of command-line arguments.
        @return: A tuple (stdout, returncode)
        @see: L{config_prover9}
        R   R6   i    s   assign(max_seconds, %d).

(   R,   R   R<   RT   RK   (   R   RF   RH   R*   t   updated_input_str(    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyRU   ø   s    	
c         C   sB   |  i  t j o |  i d | ƒ |  _  n |  i | |  i  | | ƒ S(   s  
        Call the C{prooftrans} binary with the given input.
    
        @param input_str: A string whose contents are used as stdin.
        @param args: A list of command-line arguments.
        @return: A tuple (stdout, returncode)
        @see: L{config_prover9}
        t
   prooftrans(   t   _prooftrans_binR   R<   RK   (   R   RF   RH   R*   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR     s    	(   R   R   R   R,   RY   R   RL   RV   R9   RU   R   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyR   Ù   s   	c          C   so   t  ƒ  i d ƒ }  t  ƒ  i d ƒ } t | d |  g ƒ} d  | _ g  | _ | i ƒ  | i ƒ  GH| i ƒ  GHd  S(   Ns   (walk(j) & sing(j))s   walk(j)R	   (   R   t   parseR   R   t   _executable_patht   prover9_searcht   provet   proof(   R   t   gRJ   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   test_config  s    		
c         C   s2   x+ |  D]# } t  ƒ  i | ƒ } t | ƒ GHq Wd S(   s%   
    Test that parsing works OK.
    N(   R   RZ   R
   (   t   exprt   tt   e(    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   test_convert_to_prover9*  s     c         C   s›   x” |  D]Œ \ } } t  ƒ  i | ƒ } g  } | D] } | t  ƒ  i | ƒ q0 ~ } t | d | ƒi ƒ  } x | D] } d | GHqq Wd | | f GHq Wd S(   s2   
    Try some proofs and exhibit the results.
    R	   s      %ss
   |- %s: %s
N(   R   RZ   R   R]   (   t	   argumentsR   R	   R_   t   _[1]R   t   alistRJ   (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt
   test_prove2  s     - s   (man(x) <-> (not (not man(x))))s   (not (man(x) & (not man(x))))s   (man(x) | (not man(x)))s   (man(x) & (not man(x)))s   (man(x) -> man(x))s   (man(x) <-> man(x))s   (not (man(x) <-> (not man(x))))s   mortal(Socrates)s   all x.(man(x) -> mortal(x))s   man(Socrates)sA   ((all x.(man(x) -> walks(x)) & man(Socrates)) -> some y.walks(y))s   (all x.man(x) -> all x.man(x))s   some x.all y.sees(x,y)s#   some e3.(walk(e3) & subj(e3, mary))sW   some e1.(see(e1) & subj(e1, john) & some e2.(pred(e1, e2) & walk(e2) & subj(e2, mary)))sV   some x e1.(see(e1) & subj(e1, x) & some e2.(pred(e1, e2) & walk(e2) & subj(e2, mary)))s   some x y.sees(x,y)s   some x.(man(x) & walks(x))s   \x.(man(x) & walks(x))s   \x y.sees(x,y)s   walks(john)s   \x.big(x, \y.mouse(y))s/   (walks(x) & (runs(x) & (threes(x) & fours(x))))s   (walks(x) -> runs(x))s   some x.(PRO(x) & sees(John, x))s    some x.(man(x) & (not walks(x)))s   all x.(man(x) -> walks(x))i-   c         C   s   d |  GHd  S(   Nt   -(    (   t   num(    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   spacer_  s    c           C   sE   d GHt  ƒ  t ƒ  Hd GHt  ƒ  t t ƒ Hd GHt  ƒ  t t ƒ d  S(   Ns   Testing configurations$   Testing conversion to Prover9 formats   Testing proofs(   Rk   R`   Rd   t   expressionsRh   Re   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pyt   demob  s    
t   __main__(   R0   t   tempfileR@   t   stringR    R   t   nltk.sem.logicR   R   RP   t   apiR   R   R#   RL   t   p9_return_codest   objectR   R   R$   R
   R   R`   Rd   Rh   Re   Rl   Rk   Rm   R   (    (    (    s,   /p/zhu/06/nlp/nltk/nltk/inference/prover9.pys   <module>
   sr   								'c	E																			