
    g=                        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 d6d3Z!d4 Z"e#d5k(  r e"        yy)7zD
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y)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.
    c                     |j                         dk(  r!| j                         D ]  }t        |        y|j                         dk(  r*t        | j                               D ]  }t        |        yt	        d|z        )z<
        Print the list of the current assumptions.
        nltkprover9z*Unrecognized value for 'output_format': %sN)lowerassumptionsprintconvert_to_prover9	NameError)selfoutput_formatas      K/var/www/openai/venv/lib/python3.12/site-packages/nltk/inference/prover9.pyprint_assumptionsz&Prover9CommandParent.print_assumptions6   sv      F*%%'a (  "i/'(8(8(:;a < <}L     N)r   )__name__
__module____qualname____doc__r$    r%   r#   r   r   /   s    r%   r   c                        e Zd ZdZddZddZy)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.
    Nc                 z    |sg }|t        |t              sJ t        |      }t        j                  | |||       y)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#   r0   zProver9Command.__init__M   s@     Kfg...W%F""4{Cr%   c                     |r.| j                   j                  |dg      d   j                         S |j                         S )z9
        :see BaseProverCommand.decorate_proof()
        striplabelsr   )_prover_call_prooftransrstrip)r    proof_stringsimplifys      r#   decorate_proofzProver9Command.decorate_proofd   sB     <<00Ofh  &&((r%   )NN<   N)T)r&   r'   r(   r)   r0   r;   r*   r%   r#   r,   r,   F   s    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	y)
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 | _        y d}t        j                  j	                  ||dgd||dz   g|      | _        | j                  j                  t        j                  j                  d      | _         y )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$5$<$<RWW[[!$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    r1   r   sp9_assumptions        r#   prover9_inputzProver9Parent.prover9_input   st     ++A!3K!@[=00 "A##A%%A1$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                  || j                  gz  }t        j                  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  ,!6!6 77~~))'[9v. * 
 	
r%   c                    |r%t        d|       t        d|       t        d|d       |g|z   }	 |j                  d      }t        j                  |t        j
                  t        j                  t        j
                        }|j                  |      \  }}|r4t        d|j                         |rt        d	|d       |rt        d
|d       |j                  d      |j                  fS # t        $ r Y w xY w)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	!((0I 

0A0A
 ==y=9.!,,/k640k640g&55  		s   C+ +	C76C7)F)
r&   r'   r(   r)   rH   rR   rW   rY   r\   rr   r*   r%   r#   r>   r>   p   s0     M *

 -/ !6r%   r>   c                    t        | t              r4g }| D ]+  }	 |j                  t        |j	                                      - |S 	 t        | j	                               S #  t        d| z          xY w#  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!**,?@  	&u~~'788LuTU
	H5PQs   (AA3 A03Bc                    t        | t              r1dt        | j                        z   dz   t	        | j
                        z   S t        | t              r1dt        | j                        z   dz   t	        | j
                        z   S t        | t              rdt	        | j
                        z   dz   S t        | t              r4dt	        | j                        z   dz   t	        | j                        z   dz   S t        | t              r4dt	        | j                        z   dz   t	        | j                        z   dz   S t        | t              r4dt	        | j                        z   d	z   t	        | j                        z   dz   S t        | t              r4dt	        | j                        z   d
z   t	        | j                        z   dz   S t        | t              r4d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?    *./*%%&' "*//23	
 
J	.*%%&' "*//23	
 
J 1	2)*//::S@@	J	.!*"2"234 "*"3"345 		
 
J	-!*"2"234 "*"3"345 		
 
J	.!*"2"234 "*"3"345 		
 
J	.!*"2"234 "*"3"345 		
 
J 2	3!*"2"234 "*"3"345 		
 :r%   c                   B    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	y)
r/   Nc                     || _         y )N)_timeout)r    r2   s     r#   r0   zProver9.__init__7  s    	&r%   Fc                 d    |sg }| j                  | j                  ||      |      \  }}|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    r1   r   rG   r`   rk   s         r#   _provezProver9._prove=  sI     K!//t[17 0 

 a((r%   c                 :    d}|t         j                  | ||      z   S )z3
        :see: Prover9Parent.prover9_input
        zclear(auto_denials).
)r>   rW   )r    r1   r   rU   s       r#   rW   zProver9.prover9_inputL  s#     %=..tT;GGGr%   c                 |   | j                   | j                  d|      | _         d}| j                  dkD  r|d| j                  z  z  }||z  }| j                  || j                   ||      \  }}|dvrId}||v r%|j	                  |      }||d j                         }	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 ED==1!?$--!OOY&!ZZt00$

 V#'N'!<<7!(),224\)3JII+JAAz!!r%   c                     | j                   | j                  d|      | _         | j                  || 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``
        
prooftrans)_prooftrans_binr\   rr   )r    rm   ro   rG   s       r#   r7   zProver9._call_prooftransv  s@     '#'#4#4\7#KD zz)T%9%94IIr%   )r<   )NNF)
r&   r'   r(   rI   r   r0   r   rW   r   r7   r*   r%   r#   r/   r/   3  s6    LO&)H -/ !"F 025 Jr%   r/   c                       e Zd Zd Zy)Prover9Exceptionc                 V    t         |   }|r|d|z  z  }t        j                  | |       y )Nz
%s)p9_return_codes	Exceptionr0   )r    rk   messagemsgs       r#   r0   zProver9Exception.__init__  s.    j)6G##C4%r%   N)r&   r'   r(   r0   r*   r%   r#   r   r     s    &r%   r   c                       e Zd Zy)r   Nr&   r'   r(   r*   r%   r#   r   r         r%   r   c                       e Zd Zy)r   Nr   r*   r%   r#   r   r     r   r%   r   c                     t        j                  d      } t        j                  d      }t        || g      }d |_        g |_        |j                          t        |j                                t        |j                                y )Nz(walk(j) & sing(j))zwalk(j)r   )r	   
fromstringr,   _executable_pathprover9_searchprover   proof)r"   grq   s      r#   test_configr     sf    34Ai(Aqqc*AAAGGI	!'')	!'')r%   c                 d    | D ]+  }t        j                  |      }t        t        |             - y)z%
    Test that parsing works OK.
    N)r	   r   r   r   )exprtes      r#   test_convert_to_prover9r     s,     !!!$ #$ r%   c                    | D ]~  \  }}t        j                  |      }|D cg c]  }t        j                  |       }}t        ||      j                         }|D ]  }t	        d|z          t	        d| d| d        yc c}w )z2
    Try some proofs and exhibit the results.
    r   z   %sz|- z: r^   N)r	   r   r,   r   r   )	argumentsr1   r   r   r"   alistrq   s          r#   
test_prover     s     'k!!$'3>?;a&&q);?1%0668A'A+ A3b2 '?s   B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         y )N-)r   )nums    r#   spacerr     s    	#)r%   c                      t        d       t                t                t                t        d       t                t        t               t                t        d       t                t        t               y )NzTesting configurationz$Testing conversion to Prover9 formatzTesting proofs)r   r   r   r   expressionsr   r   r*   r%   r#   demor     sK    	
!"
HM	G	
01
HK(	G	

Hyr%   __main__)-   )$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   r/   r   r   r   r   r   r   r   r   r   r   r   r&   r*   r%   r#   <module>r      s   
   8
 
 
  	 .'))+< ')Tk6 k6\*=@OJmV OJd&y &	, 		$4 		%
  '+$b)##2$b)#2B&+7IJH"M%r*r"-e	
 	ae	
+	: zF r%   