
    Qd?                        d Z ddlZddlZddl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 ddddd	d
dddd	Z G d d          Z G d dee          Z G d d          Zd Zd Z G d dee          Z G d de          Z G d de          Z G d de          Z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/d0gfd1d0gfgZg d2Z d7d4Z!d5 Z"e#d6k    r e"             dS dS )8zD
A theorem prover that makes use of the external 'Prover9' package.
    N)BaseProverCommandProver)	AllExpressionAndExpressionEqualityExpressionExistsExpression
ExpressionIffExpressionImpExpressionNegatedExpressionOrExpressionTz(FATAL)Fz
(MAX_MEGS)z(MAX_SECONDS)z(MAX_GIVEN)z
(MAX_KEPT)z(ACTION)z	(SIGSEGV))	r                        e   c                       e Zd ZdZddZdS )Prover9CommandParentz
    A common base class used by both ``Prover9Command`` and ``MaceCommand``,
    which is responsible for maintaining a goal and a set of assumptions,
    and generating prover9-style input files from them.
    nltkc                 @   |                                 dk    r(|                                 D ]}t          |           dS |                                 dk    r5t          |                                           D ]}t          |           dS t	          d|z            )z<
        Print the list of the current assumptions.
        r   prover9z*Unrecognized value for 'output_format': %sN)lowerassumptionsprintconvert_to_prover9	NameError)selfoutput_formatas      6lib/python3.11/site-packages/nltk/inference/prover9.pyprint_assumptionsz&Prover9CommandParent.print_assumptions6   s       F**%%''  a   ""i//'(8(8(:(:;;  a  <}L      N)r   )__name__
__module____qualname____doc__r$    r%   r#   r   r   /   s2              r%   r   c                   "    e Zd ZdZddZddZdS )	Prover9Commandz
    A ``ProverCommand`` specific to the ``Prover9`` prover.  It contains
    the a print_assumptions() method that is used to print the list
    of assumptions in multiple formats.
    N<   c                     |sg }|t          |t                    sJ nt          |          }t          j        | |||           dS )a  
        :param goal: Input expression to prove
        :type goal: sem.Expression
        :param assumptions: Input expressions to use as assumptions in
            the proof.
        :type assumptions: list(sem.Expression)
        :param timeout: number of seconds before timeout; set to 0 for
            no timeout.
        :type timeout: int
        :param prover: a prover.  If not set, one will be created.
        :type prover: Prover9
        N)
isinstanceProver9r   __init__)r    goalr   timeoutprovers        r#   r1   zProver9Command.__init__M   sZ      	Kfg......W%%F"4{CCCCCr%   Tc                     |r4| j                             |dg          d                                         S |                                S )z9
        :see BaseProverCommand.decorate_proof()
        striplabelsr   )_prover_call_prooftransrstrip)r    proof_stringsimplifys      r#   decorate_proofzProver9Command.decorate_proofd   sM      	)<00OOfhh  &&(((r%   )NNr-   N)T)r&   r'   r(   r)   r1   r<   r*   r%   r#   r,   r,   F   sJ         D D D D.	) 	) 	) 	) 	) 	)r%   r,   c                   >    e Zd ZdZdZd	dZd Zd Zd	dZg dfdZ	dS )
Prover9Parentz
    A common class extended by both ``Prover9`` and ``Mace <mace.Mace>``.
    It contains the functionality required to convert NLTK-style
    expressions into Prover9-style expressions.
    NFc           	          |d | _         d | _        d S d}t          j                            ||dgd||dz   g|          | _        | j                            t          j        j        d          | _         d S )Nr   PROVER9'https://www.cs.unm.edu/~mccune/prover9/.exe)path_to_binenv_varsurlbinary_namesverboser   )	_binary_location_prover9_binr   	internalsfind_binaryrsplitospathsep)r    binary_locationrG   names       r#   config_prover9zProver9Parent.config_prover9y   s    "$(D! $DD $ : :+#="D6M2 !; ! !D %)$5$<$<RW[!$L$LD!!!r%   c                     d}|r$|dz  }t          |          D ]
}|d|z  z  }|dz  }|r|dz  }|dt          |          z  z  }|dz  }|S )z
        :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.
         zformulas(assumptions).
z    %s.
zend_of_list.

zformulas(goals).
)r   )r    r2   r   sp9_assumptions        r#   prover9_inputzProver9Parent.prover9_input   s      	$++A!3K!@!@ 1 1[=00##A 	$%%A1$7777A##Ar%   c                 
    g dS )z
        A list of directories that should be searched for the prover9
        executables.  This list is used by ``config_prover9`` when searching
        for the prover9 executables.
        )z/usr/local/bin/prover9z/usr/local/bin/prover9/binz/usr/local/binz/usr/binz/usr/local/prover9z/usr/local/share/prover9r*   )r    s    r#   binary_locationszProver9Parent.binary_locations   s    
 
 
 	
r%   c           	          |                                  }| j        || j        gz  }t          j                            ||dgd||dz   g|          S )Nr@   rA   rB   )
searchpathrD   rE   rF   rG   )rY   rH   r   rJ   rK   )r    rQ   rG   rY   s       r#   _find_binaryzProver9Parent._find_binary   sj    0022 ,!6 77~))'[9v. * 
 
 	
r%   c                    |r1t          d|           t          d|           t          d|d           |g|z   }	 |                    d          }n# t          $ r Y nw xY wt          j        |t          j        t          j        t          j                  }|                    |          \  }}|r;t          d|j                   |rt          d	|d           |rt          d
|d           |	                    d          |j        fS )a=  
        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: ``config_prover9``
        zCalling:zArgs:zInput:

utf8)stdoutstderrstdin)inputzReturn code:zstdout:
zstderr:
zutf-8)
r   encodeAttributeError
subprocessPopenPIPESTDOUTcommunicate
returncodedecode)	r    	input_strbinaryargsrG   cmdpr`   ra   s	            r#   _callzProver9Parent._call   s-     	/*f%%%'4   *i... ho	!((00II 	 	 	D	

0A
 
 
 ==y=99 	1.!,/// 1k64000 1k64000g&&55s   A 
AA)F)
r&   r'   r(   r)   rH   rR   rW   rY   r\   rr   r*   r%   r#   r>   r>   p   s          M M M M   *
 
 

 
 
 
 -/ !6 !6 !6 !6 !6 !6r%   r>   c                 L   t          | t                    rVg }| D ]O}	 |                    t          |                                                     8#  t          d| z              xY w|S 	 t          |                                           S #  t          d| z              xY w)z;
    Convert a ``logic.Expression`` to Prover9 format.
    z4input %s cannot be converted to Prover9 input syntax)r/   listappend_convert_to_prover9r;   r   )rc   resultrU   s      r#   r   r      s     %  	 	A1!**,,??@@@@LuTUUU	&u~~'7'7888	H5PQQQs   4AA'- B B#c                 T   t          | t                    r/dt          | j                  z   dz   t	          | j                  z   S t          | t                    r/dt          | j                  z   dz   t	          | j                  z   S t          | t                    rdt	          | j                  z   dz   S t          | t                    r2dt	          | j	                  z   dz   t	          | j
                  z   dz   S t          | t                    r2dt	          | j	                  z   dz   t	          | j
                  z   dz   S t          | t                    r2dt	          | j	                  z   d	z   t	          | j
                  z   dz   S t          | t                    r2dt	          | j	                  z   d
z   t	          | j
                  z   dz   S t          | t                    r2dt	          | j	                  z   dz   t	          | j
                  z   dz   S t          |           S )zC
    Convert ``logic.Expression`` to Prover9 formatted string.
    zexists  zall z-()(z & z | z -> z <-> z = )r/   r   strvariablerv   termr   r   r   firstsecondr   r   r
   r   )
expressions    r#   rv   rv      s    *.// 9*%&&' "*/223	
 
J	.	. 2*%&&' "*/223	
 
J 1	2	2 +)*/:::S@@	J	.	. )!*"2334 "*"3445 		
 
J	-	- !!*"2334 "*"3445 		
 
J	.	. !*"2334 "*"3445 		
 
J	.	. !*"2334 "*"3445 		
 
J 2	3	3 	!*"2334 "*"3445 		
 :r%   c                   D    e Zd ZdZdZd	dZd
dZd Zg dfdZg dfdZ	dS )r0   Nr-   c                     || _         d S )N)_timeout)r    r3   s     r#   r1   zProver9.__init__7  s    	& 	&r%   Fc                 v    |sg }|                      |                     ||          |          \  }}|dk    |fS )z
        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.
        )rG   r   )_call_prover9rW   )r    r2   r   rG   r`   rk   s         r#   _provezProver9._prove=  sV      	K!//t[117 0 
 

 a((r%   c                 D    d}|t                               | ||          z   S )z3
        :see: Prover9Parent.prover9_input
        zclear(auto_denials).
)r>   rW   )r    r2   r   rU   s       r#   rW   zProver9.prover9_inputL  s&     %=..tT;GGGGr%   c                    | j         |                     d|          | _         d}| j        dk    r|d| j        z  z  }||z  }|                     || j         ||          \  }}|dvr^d}||v r2|                    |          }||d                                         }	nd}	|dv rt          ||	          t          ||	          ||fS )	a  
        Call the ``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: ``config_prover9``
        Nr   rT   r   zassign(max_seconds, %d).

)r   r   z%%ERROR:)r   r   r   r   )rI   r\   r   rr   indexstripProver9LimitExceededExceptionProver9FatalException)
r    rm   ro   rG   updated_input_strr`   rk   errormsgprefixmsgstarterrormsgs
             r#   r   zProver9._call_prover9S  s     $ $ 1 1)W E ED=1!?$-!OOY&!ZZt0$
 

 V##'N''!<<77!()),2244\))3JIII+JAAAz!!r%   c                     | j         |                     d|          | _         |                     || j         ||          S )a  
        Call the ``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: ``config_prover9``
        N
prooftrans)_prooftrans_binr\   rr   )r    rm   ro   rG   s       r#   r8   zProver9._call_prooftransv  s@     '#'#4#4\7#K#KD zz)T%94IIIr%   )r-   )NNF)
r&   r'   r(   rI   r   r1   r   rW   r   r8   r*   r%   r#   r0   r0   3  s        LO& & & &) ) ) )H H H -/ !" !" !" !"F 025 J J J J J Jr%   r0   c                       e Zd Zd ZdS )Prover9Exceptionc                 j    t           |         }|r|d|z  z  }t                              | |           d S )Nz
%s)p9_return_codes	Exceptionr1   )r    rk   messagemsgs       r#   r1   zProver9Exception.__init__  s?    j) 	$6G##C4%%%%%r%   N)r&   r'   r(   r1   r*   r%   r#   r   r     s#        & & & & &r%   r   c                       e Zd ZdS )r   Nr&   r'   r(   r*   r%   r#   r   r             Dr%   r   c                       e Zd ZdS )r   Nr   r*   r%   r#   r   r     r   r%   r   c                  B   t          j        d          } t          j        d          }t          || g          }d |_        g |_        |                                 t          |                                           t          |                                           d S )Nz(walk(j) & sing(j))zwalk(j)r   )r	   
fromstringr,   _executable_pathprover9_searchprover   proof)r"   grq   s      r#   test_configr     s    344Ai((Aqqc***AAAGGIII	!''))	!''))r%   c                 p    | D ]2}t          j        |          }t          t          |                     3dS )z%
    Test that parsing works OK.
    N)r	   r   r   r   )exprtes      r#   test_convert_to_prover9r     sG      % %!!$$ ##$$$$% %r%   c                     | D ]u\  }}t          j        |          }d |D             }t          ||                                          }|D ]}t	          d|z             t	          d| d| d           vdS )z2
    Try some proofs and exhibit the results.
    c                 6    g | ]}t          j        |          S r*   )r	   r   ).0r"   s     r#   
<listcomp>ztest_prove.<locals>.<listcomp>  s#    ???a&q))???r%   r   z   %sz|- z: r^   N)r	   r   r,   r   r   )	argumentsr2   r   r   alistrq   r"   s          r#   
test_prover     s      )    {!$''??;???1%0006688 	 	A'A+A   r%   z(man(x) <-> (not (not man(x))))z(not (man(x) & (not man(x))))z(man(x) | (not man(x)))z(man(x) & (not man(x)))z(man(x) -> man(x))z(man(x) <-> man(x))z(not (man(x) <-> (not man(x))))zmortal(Socrates)zall x.(man(x) -> mortal(x))zman(Socrates)zA((all x.(man(x) -> walks(x)) & man(Socrates)) -> some y.walks(y))z(all x.man(x) -> all x.man(x))zsome x.all y.sees(x,y)z#some e3.(walk(e3) & subj(e3, mary))zWsome e1.(see(e1) & subj(e1, john) & some e2.(pred(e1, e2) & walk(e2) & subj(e2, mary)))zVsome x e1.(see(e1) & subj(e1, x) & some e2.(pred(e1, e2) & walk(e2) & subj(e2, mary))))zsome x y.sees(x,y)zsome x.(man(x) & walks(x))z\x.(man(x) & walks(x))z\x y.sees(x,y)zwalks(john)z\x.big(x, \y.mouse(y))z/(walks(x) & (runs(x) & (threes(x) & fours(x))))z(walks(x) -> runs(x))zsome x.(PRO(x) & sees(John, x))z some x.(man(x) & (not walks(x)))zall x.(man(x) -> walks(x))-   c                 *    t          d| z             d S )N-)r   )nums    r#   spacerr     s    	#)r%   c                  X   t          d           t                       t                       t                       t          d           t                       t          t                     t                       t          d           t                       t          t                     d S )NzTesting configurationz$Testing conversion to Prover9 formatzTesting proofs)r   r   r   r   expressionsr   r   r*   r%   r#   demor     s    	
!"""
HHHMMM	GGG	
0111
HHHK(((	GGG	

HHHyr%   __main__)r   )$r)   rM   rf   r   nltk.inference.apir   r   nltk.sem.logicr   r   r   r   r	   r
   r   r   r   r   r   r,   r>   r   rv   r0   r   r   r   r   r   r   r   r   r   r   r   r&   r*   r%   r#   <module>r      si    
			      8 8 8 8 8 8 8 8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  	        .') ') ') ') '))+< ') ') ')Tk6 k6 k6 k6 k6 k6 k6 k6\  *= = =@OJ OJ OJ OJ OJmV OJ OJ OJd& & & & &y & & &	 	 	 	 	, 	 	 		 	 	 	 	$4 	 	 	
 
 
% % %
  
  
  '+$b)##2$b)#2B&+7IJH"M%r*r"-e	
 	ae	
+	:        zDFFFFF r%   