
    /h=                        S r SSKrSSKrSSKrSSKJrJr  SSKJrJ	r	J
r
JrJrJrJrJrJr  SSSSS	S
SSSS.	r " S S5      r " S S\\5      r " S S5      rS rS r " S S\\5      r " S S\5      r " S S\5      r " S S\5      rS rS  rS! rS"/ 4S#/ 4S$/ 4S%/ 4S&/ 4S#/ 4S$/ 4S&/ 4S'/ 4S(/ 4S)S*S+/4S,/ 4S-/ 4S./ 4S/S0/4S1S0/4/r/ S2Qr S6S3 jr!S4 r"\#S5:X  a  \"" 5         gg)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                   "    \ rS rSrSrSS jrSrg)Prover9CommandParent/   z
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                    UR                  5       S:X  a#  U R                  5        H  n[        U5        M     gUR                  5       S:X  a,  [        U R                  5       5       H  n[        U5        M     g[	        SU-  5      e)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      N/var/www/auris/envauris/lib/python3.13/site-packages/nltk/inference/prover9.pyprint_assumptions&Prover9CommandParent.print_assumptions6   sv      F*%%'a (  "i/'(8(8(:;a < <}L      N)r   )__name__
__module____qualname____firstlineno____doc__r%   __static_attributes__r(   r'   r$   r   r   /   s    r'   r   c                   ,    \ rS rSrSrSS jrSS jrSrg)	Prover9CommandF   z
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                     U(       d  / nUb  [        U[        5      (       d   eO[        U5      n[        R                  " XX5        g)al  
: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$   r5   Prover9Command.__init__M   s?     Kfg....W%F""4Cr'   c                     U(       a.  U R                   R                  US/5      S   R                  5       $ UR                  5       $ )z)
:see BaseProverCommand.decorate_proof()
striplabelsr   )_prover_call_prooftransrstrip)r!   proof_stringsimplifys      r$   decorate_proofProver9Command.decorate_proofd   sB     <<00Ofh  &&((r'   r(   )NN<   N)T)r)   r*   r+   r,   r-   r5   rA   r.   r(   r'   r$   r0   r0   F   s    D.	)r'   r0   c                   J    \ rS rSrSrSrSS jrS rS rSS jr	/ S4S	 jr
S
rg)Prover9Parentp   z
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           	          Uc  S U l         S U l        g Sn[        R                  R	                  UUS/SX3S-   /US9U l        U R                  R                  [        R                  R                  S5      U l         g )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_locationrO   names       r$   config_prover9Prover9Parent.config_prover9y   sy    "$(D! $DD $ : :+#="6M2 !; !D %)$5$5$<$<RWW[[!$LD!r'   c                     SnU(       a$  US-  n[        U5       H  nUSU-  -  nM     US-  nU(       a  US-  nUS[        U5      -  -  nUS-  nU$ )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!   r6   r   sp9_assumptions        r$   prover9_inputProver9Parent.prover9_input   st     ++A!3K!@[=00 "A##A%%A1$777A##Ar'   c                 
    / SQ$ )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_locationsProver9Parent.binary_locations   s    
 	
r'   c           	          U R                  5       nU R                  b  X0R                  /-  n[        R                  R	                  UUS/SXS-   /US9$ )NrH   rI   rJ   )
searchpathrL   rM   rN   rO   )rc   rP   r   rR   rS   )r!   rY   rO   rc   s       r$   _find_binaryProver9Parent._find_binary   sf    002  ,!6!6 77~~))'[9v. * 
 	
r'   c                    U(       a%  [        SU5        [        SU5        [        SUS5        U/U-   n UR                  S5      n[        R                  " U[        R
                  [        R                  [        R
                  S9nUR                  US9u  pxU(       a>  [        SUR                  5        U(       a  [        S	US5        U(       a  [        S
US5        UR                  S5      UR                  4$ ! [         a     Nf = f)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argsrO   cmdprl   rm   s	            r$   _callProver9Parent._call   s     *f%'4 *i. ho	!((0I 

0A0A
 ==y=9.!,,/k640k640g&55  		s   C9 9
DD)rP   rQ   )F)r)   r*   r+   r,   r-   rP   rZ   r`   rc   rg   r~   r.   r(   r'   r$   rE   rE   p   s0     M *

 -/ !6r'   rE   c                    [        U [        5      (       a6  / nU  H,  n UR                  [        UR	                  5       5      5        M.     U$  [        U R	                  5       5      $ !   [        SU -  5        e = f!   [        SU -  5        e = f)z3
Convert a ``logic.Expression`` to Prover9 format.
z4input %s cannot be converted to Prover9 input syntax)r3   listappend_convert_to_prover9r@   r   )ro   resultr^   s      r$   r   r      s     %A1!**,?@  	&u~~'788LuTU
	H5PQs   (A&A: &A7:Bc                 j   [        U [        5      (       a1  S[        U R                  5      -   S-   [	        U R
                  5      -   $ [        U [        5      (       a1  S[        U R                  5      -   S-   [	        U R
                  5      -   $ [        U [        5      (       a  S[	        U R
                  5      -   S-   $ [        U [        5      (       a4  S[	        U R                  5      -   S-   [	        U R                  5      -   S-   $ [        U [        5      (       a4  S[	        U R                  5      -   S-   [	        U R                  5      -   S-   $ [        U [        5      (       a4  S[	        U R                  5      -   S	-   [	        U R                  5      -   S-   $ [        U [        5      (       a4  S[	        U R                  5      -   S
-   [	        U R                  5      -   S-   $ [        U [        5      (       a4  S[	        U R                  5      -   S-   [	        U R                  5      -   S-   $ [        U 5      $ )z;
Convert ``logic.Expression`` to Prover9 formatted string.
zexists  zall z-()(z & z | z -> z <-> z = )r3   r   strvariabler   termr   r   r   firstsecondr   r   r
   r   )
expressions    r$   r   r      sW    *.//*%%&' "*//23	
 
J	.	.*%%&' "*//23	
 
J 1	2	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	3!*"2"234 "*"3"345 		
 :r'   c                   R    \ rS rSrSrSrS
S jrSS jrS r/ S4S jr	/ S4S jr
S	rg)r4   i3  Nc                     Xl         g )N)_timeout)r!   r7   s     r$   r5   Prover9.__init__7  s    	&r'   Fc                 d    U(       d  / nU R                  U R                  X5      US9u  pEUS:H  U4$ )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.
)rO   r   )_call_prover9r`   )r!   r6   r   rO   rl   rw   s         r$   _proveProver9._prove=  sE     K!//t17 0 
 a((r'   c                 8    SnU[         R                  XU5      -   $ )z#
:see: Prover9Parent.prover9_input
zclear(auto_denials).
)rE   r`   )r!   r6   r   r^   s       r$   r`   Prover9.prover9_inputL  s!     %=..t;GGGr'   c                 z   U R                   c  U R                  SU5      U l         SnU R                  S:  a  USU R                  -  -  nXA-  nU R                  X@R                   X#5      u  pVUS;  aI  SnXu;   a$  UR	                  U5      nXXS R                  5       n	OSn	US;   a  [        Xi5      e[        Xi5      eXV4$ )	z
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   r]   r   zassign(max_seconds, %d).

)r   r   z%%ERROR:)r   r   r   r   )rQ   rg   r   r~   indexstripProver9LimitExceededExceptionProver9FatalException)
r!   ry   r{   rO   updated_input_strrl   rw   errormsgprefixmsgstarterrormsgs
             r$   r   Prover9._call_prover9S  s     $ $ 1 1)W ED==1!?$--!OO&!ZZ00$
 V#'N'!<<7!),224\)3JII+JAA!!r'   c                     U R                   c  U R                  SU5      U l         U R                  XR                   X#5      $ )z
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_binrg   r~   )r!   ry   r{   rO   s       r$   r=   Prover9._call_prooftransv  s<     '#'#4#4\7#KD zz)%9%94IIr'   )r   rQ   r   )rC   )NNF)r)   r*   r+   r,   rQ   r   r5   r   r`   r   r=   r.   r(   r'   r$   r4   r4   3  s6    LO&)H -/ !"F 025 Jr'   r4   c                       \ rS rSrS rSrg)Prover9Exceptioni  c                 ^    [         U   nU(       a  USU-  -  n[        R                  X5        g )Nz
%s)p9_return_codes	Exceptionr5   )r!   rw   messagemsgs       r$   r5   Prover9Exception.__init__  s,    j)6G##C4%r'   r(   N)r)   r*   r+   r,   r5   r.   r(   r'   r$   r   r     s    &r'   r   c                       \ rS rSrSrg)r   i  r(   Nr)   r*   r+   r,   r.   r(   r'   r$   r   r         r'   r   c                       \ rS rSrSrg)r   i  r(   Nr   r(   r'   r$   r   r     r   r'   r   c                     [         R                  " S5      n [         R                  " S5      n[        X/S9nS Ul        / Ul        UR                  5         [        UR                  5       5        [        UR                  5       5        g )Nz(walk(j) & sing(j))zwalk(j)r   )r	   
fromstringr0   _executable_pathprover9_searchprover   proof)r#   gr}   s      r$   test_configr     sd    34Ai(Aqc*AAAGGI	!'')	!'')r'   c                 j    U  H-  n[         R                  " U5      n[        [        U5      5        M/     g)z
Test that parsing works OK.
N)r	   r   r   r   )exprtes      r$   test_convert_to_prover9r     s,     !!!$ #$ r'   c                    U  H  u  p[         R                  " U5      nU Vs/ s H  n[         R                  " U5      PM     nn[        X5S9R                  5       nU H  n[	        SU-  5        M     [	        SU SU S35        M     gs  snf )z*
Try some proofs and exhibit the results.
r   z   %sz|- z: rj   N)r	   r   r0   r   r   )	argumentsr6   r   r   r#   alistr}   s          r$   
test_prover     s     '!!$'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                      [        SU -  5        g )N-)r   )nums    r$   spacerr     s    	#)r'   c                      [        S5        [        5         [        5         [        5         [        S5        [        5         [        [        5        [        5         [        S5        [        5         [        [        5        g )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-   rU   rr   r   nltk.inference.apir   r   nltk.sem.logicr   r   r   r   r	   r
   r   r   r   r   r   r0   rE   r   r   r4   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'   