o
    i=                     @   s  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 dZG dd deeZG dd dZdd Zdd ZG dd deeZG dd deZG dd deZG dd  d eZd!d" Zd#d$ Zd%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/d0gfd1g fd2g fd3g fd4d5gfd6d5gfgZg d7Z d>d9d:Z!d;d< Z"e#d=kre"  dS dS )?zD
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                   @   s   e Zd ZdZd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                 C   s\   |  dkr|  D ]}t| q
dS |  dkr(t|  D ]}t| qdS td| )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a r    Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nltk/inference/prover9.pyprint_assumptions6   s   

z&Prover9CommandParent.print_assumptionsN)r   )__name__
__module____qualname____doc__r"   r    r    r    r!   r   /   s    r   c                   @   s$   e Zd ZdZd	ddZd
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                 C   s<   |sg }|durt |tsJ nt|}t| ||| 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proverr    r    r!   r+   M   s   zProver9Command.__init__Tc                 C   s$   |r| j |dgd  S | S )z9
        :see BaseProverCommand.decorate_proof()
        striplabelsr   )_prover_call_prooftransrstrip)r   proof_stringsimplifyr    r    r!   decorate_proofd   s   zProver9Command.decorate_proof)NNr(   N)T)r#   r$   r%   r&   r+   r5   r    r    r    r!   r'   F   s    
r'   c                   @   sF   e Zd ZdZdZdddZdd Zdd	 Zdd
dZg dfd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                 C   sX   |d u rd | _ d | _d S d}tjj||dgd||d g|d| _| 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_locationr>   namer    r    r!   config_prover9y   s   

zProver9Parent.config_prover9c                 C   sZ   d}|r|d7 }t |D ]}|d| 7 }q|d7 }|r+|d7 }|dt | 7 }|d7 }|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   r,   r   sp9_assumptionr    r    r!   prover9_input   s   zProver9Parent.prover9_inputc                 C   s   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   r    r    r!   binary_locations   s   zProver9Parent.binary_locationsc                 C   s@   |   }| jd ur|| jg7 }tjj||dgd||d g|dS )Nr7   r8   r9   )
searchpathr;   r<   r=   r>   )rN   r?   r   rA   rB   )r   rH   r>   rN   r    r    r!   _find_binary   s   

zProver9Parent._find_binaryc           	      C   s   |rt d| t d| t d|d |g| }z|d}W n	 ty'   Y nw tj|tjtjtjd}|j|d\}}|rTt d|j |rLt d	|d |rTt 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argsr>   cmdprS   rT   r    r    r!   _call   s*   



zProver9Parent._call)F)
r#   r$   r%   r&   r?   rI   rM   rN   rP   re   r    r    r    r!   r6   p   s    

r6   c                 C   sj   t | tr#g }| D ]}z|t|  W q	   td|    |S zt|  W S    td|    )z;
    Convert a ``logic.Expression`` to Prover9 format.
    z4input %s cannot be converted to Prover9 input syntax)r)   listappend_convert_to_prover9r4   r   )rV   resultrK   r    r    r!   r      s   
r   c                 C   sB  t | trdt| j d t| j S t | tr&dt| j d t| j S t | tr4dt| j d S t | trIdt| j	 d t| j
 d S t | tr^dt| j	 d t| j
 d S t | trsdt| j	 d	 t| j
 d S t | trdt| j	 d
 t| j
 d S t | trdt| j	 d t| j
 d S t| S )zC
    Convert ``logic.Expression`` to Prover9 formatted string.
    zexists  zall z-()(z & z | z -> z <-> z = )r)   r   strvariablerh   termr   r   r   firstsecondr   r
   r	   r   )
expressionr    r    r!   rh      s   







rh   c                   @   sL   e Zd ZdZdZdddZdddZdd	 Zg dfd
dZg dfddZ	dS )r*   Nr(   c                 C   s
   || _ d S )N)_timeout)r   r-   r    r    r!   r+   7  s   zProver9.__init__Fc                 C   s.   |sg }| j | |||d\}}|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.
        )r>   r   )_call_prover9rM   )r   r,   r   r>   rS   r^   r    r    r!   _prove=  s   
zProver9._provec                 C   s   d}|t | || S )z3
        :see: Prover9Parent.prover9_input
        zclear(auto_denials).
)r6   rM   )r   r,   r   rK   r    r    r!   rM   L  s   zProver9.prover9_inputc           
      C   s   | j du r| d|| _ d}| jdkr|d| j 7 }||7 }| || j ||\}}|dvrQd}||v rA||}||d  }	nd}	|dv rLt||	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   rJ   r   zassign(max_seconds, %d).

)r   r   z%%ERROR:)r   r   r   r   )r@   rP   rs   re   indexstripProver9LimitExceededExceptionProver9FatalException)
r   r`   rb   r>   updated_input_strrS   r^   errormsgprefixmsgstarterrormsgr    r    r!   rt   S  s&   
	




zProver9._call_prover9c                 C   s*   | j du r| 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_binrP   re   )r   r`   rb   r>   r    r    r!   r1   v  s   
	zProver9._call_prooftrans)r(   )NNF)
r#   r$   r%   r@   r   r+   ru   rM   rt   r1   r    r    r    r!   r*   3  s    

#r*   c                   @   s   e Zd Zdd ZdS )Prover9Exceptionc                 C   s(   t | }|r|d| 7 }t| | d S )Nz
%s)p9_return_codes	Exceptionr+   )r   r^   messagemsgr    r    r!   r+     s   zProver9Exception.__init__N)r#   r$   r%   r+   r    r    r    r!   r     s    r   c                   @      e Zd ZdS )ry   Nr#   r$   r%   r    r    r    r!   ry         ry   c                   @   r   )rx   Nr   r    r    r    r!   rx     r   rx   c                  C   sR   t d} t d}t|| gd}d |_g |_|  t|  t|  d S )Nz(walk(j) & sing(j))zwalk(j)r   )r   
fromstringr'   _executable_pathprover9_searchprover   proof)r   grd   r    r    r!   test_config  s   

r   c                 C   s$   | D ]}t |}tt| qdS )z%
    Test that parsing works OK.
    N)r   r   r   r   )exprter    r    r!   test_convert_to_prover9  s   
r   c                 C   sf   | D ].\}}t |}dd |D }t||d }|D ]}td|  qtd| d| d qdS )	z2
    Try some proofs and exhibit the results.
    c                 S   s   g | ]}t |qS r    )r   r   ).0r   r    r    r!   
<listcomp>  s    ztest_prove.<locals>.<listcomp>r   z   %sz|- z: rQ   N)r   r   r'   r   r   )	argumentsr,   r   r   alistrd   r   r    r    r!   
test_prove  s   
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                 C   s   t d|   d S )N-)r   )numr    r    r!   spacer  s   r   c                   C   sP   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    r!   demo  s   r   __main__)r   )$r&   rD   rY   r   nltk.inference.apir   r   nltk.sem.logicr   r   r   r   r   r	   r
   r   r   r   r   r'   r6   r   rh   r*   r   r   ry   rx   r   r   r   r   r   r   r   r#   r    r    r    r!   <module>   sr   ,*n@R		


