a
     ˜ëj‹=  ã                   @   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�rže"ƒ  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n4|  ¡ dkrLt|  ¡ ƒD ]}t|ƒ q<ntd| ƒ‚dS )z<
        Print the list of the current assumptions.
        r   Úprover9z*Unrecognized value for 'output_format': %sN)ÚlowerÚassumptionsÚprintÚconvert_to_prover9Ú	NameError)ÚselfZoutput_formatÚa© r   úU/mnt/bigstor/www/usecases/.venv/lib/python3.9/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   ÚtimeoutZproverr   r   r    r*   M   s    zProver9Command.__init__Tc                 C   s(   |r| j  |dg¡d  ¡ S | ¡ S dS )z9
        :see BaseProverCommand.decorate_proof()
        Zstriplabelsr   N)Z_proverÚ_call_prooftransÚrstrip)r   Zproof_stringÚsimplifyr   r   r    Údecorate_proofd   s
    ÿzProver9Command.decorate_proof)NNr'   N)T)r"   r#   r$   r%   r*   r0   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   sV   |d u rd | _ d | _n<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)Zpath_to_binÚenv_varsÚurlÚbinary_namesÚverboser   )	Ú_binary_locationÚ_prover9_binr   Ú	internalsÚfind_binaryÚrsplitÚosÚpathÚsep)r   Zbinary_locationr8   Únamer   r   r    Úconfig_prover9y   s    
úzProver9Parent.config_prover9c                 C   sZ   d}|r2|d7 }t |ƒD ]}|d| 7 }q|d7 }|rV|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   ÚsZ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 )Nr2   r3   r4   )Z
searchpathr5   r6   r7   r8   )rF   r9   r   r;   r<   )r   rA   r8   rF   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yN   Y n0 tj|tjtjtjd�}|j|d�\}}|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Úargsr8   ÚcmdÚprJ   rK   r   r   r    Ú_callº   s(    



ÿzProver9Parent._call)F)F)
r"   r#   r$   r%   r9   rB   rE   rF   rG   r\   r   r   r   r    r1   p   s   

r1   c                 C   s|   t | tƒrNg }| D ]6}z| t| ¡ ƒ¡ W q   td|  ƒ ‚ Y q0 q|S zt|  ¡ ƒW S    td|  ƒ ‚ Y n0 dS )z;
    Convert a ``logic.Expression`` to Prover9 format.
    z4input %s cannot be converted to Prover9 input syntaxN)r(   ÚlistÚappendÚ_convert_to_prover9r/   r   )rM   ÚresultrD   r   r   r    r   Þ   s    

r   c                 C   sJ  t | tƒr&dt| jƒ d t| jƒ S t | tƒrLdt| jƒ d t| jƒ S t | tƒrhdt| jƒ d S t | tƒr’dt| j	ƒ d t| j
ƒ d S t | tƒr¼dt| j	ƒ d t| j
ƒ d S t | tƒrædt| j	ƒ d	 t| j
ƒ d S t | tƒ�rdt| j	ƒ d
 t| j
ƒ d S t | tƒ�r>dt| j	ƒ d t| j
ƒ d S t| ƒS dS )zC
    Convert ``logic.Expression`` to Prover9 formatted string.
    zexists ú zall z-(ú)ú(z & z | z -> z <-> z = N)r(   r   ÚstrÚvariabler_   Ztermr   r   r   ÚfirstÚsecondr   r
   r	   r   )Z
expressionr   r   r    r_   ó   s˜    
ÿþýÿ
ÿþýÿ

ÿþýüÿ
ÿþýüÿ
ÿþýüÿÿþýüÿÿþýüÿr_   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.
        )r8   r   )Ú_call_prover9rE   )r   r+   r   r8   rJ   rU   r   r   r    Ú_prove=  s    ÿ
zProver9._provec                 C   s   d}|t  | ||¡ S )z3
        :see: Prover9Parent.prover9_input
        zclear(auto_denials).
)r1   rE   )r   r+   r   rD   r   r   r    rE   L  s    zProver9.prover9_inputc           
      C   s¬   | j du r|  d|¡| _ d}| jdkr4|d| j 7 }||7 }|  || j ||¡\}}|dvr¤d}||v r‚| |¡}||d…  ¡ }	nd}	|dv ršt||	ƒ‚n
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   rC   r   zassign(max_seconds, %d).

)r   r   z%%ERROR:)r   r   r   r   )r:   rG   rh   r\   ÚindexÚstripÚProver9LimitExceededExceptionÚProver9FatalException)
r   rW   rY   r8   Zupdated_input_strrJ   rU   ZerrormsgprefixZmsgstartZerrormsgr   r   r    ri   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``
        NZ
prooftrans)Ú_prooftrans_binrG   r\   )r   rW   rY   r8   r   r   r    r-   v  s    	
zProver9._call_prooftrans)r'   )NNF)
r"   r#   r$   r:   ro   r*   rj   rE   ri   r-   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   rU   ÚmessageÚmsgr   r   r    r*   †  s    zProver9Exception.__init__N)r"   r#   r$   r*   r   r   r   r    rp   …  s   rp   c                   @   s   e Zd ZdS )rn   N©r"   r#   r$   r   r   r   r    rn   �  s   rn   c                   @   s   e Zd ZdS )rm   Nru   r   r   r   r    rm   ‘  s   rm   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&   Z_executable_pathZprover9_searchÚprover   Zproof)r   Úgr[   r   r   r    Útest_configš  s    

rz   c                 C   s$   | D ]}t  |¡}tt|ƒƒ qdS )z%
    Test that parsing works OK.
    N)r   rw   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| ƒ q8td|› d|› d�ƒ qdS )	z2
    Try some proofs and exhibit the results.
    c                 S   s   g | ]}t  |¡‘qS r   )r   rw   )Ú.0r   r   r   r    Ú
<listcomp>µ  ó    ztest_prove.<locals>.<listcomp>rv   z   %sz|- z: rH   N)r   rw   r&   rx   r   )Ú	argumentsr+   r   ry   Úalistr[   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ˆ   rz   r~   Úexpressionsr„   r‚   r   r   r   r    Údemoì  s    rŠ   Ú__main__)r…   )$r%   r>   rP   r   Znltk.inference.apir   r   Znltk.sem.logicr   r   r   r   r   r	   r
   r   r   rq   r   r&   r1   r   r_   r)   rr   rp   rn   rm   rz   r~   r„   r‚   r‰   rˆ   rŠ   r"   r   r   r   r    Ú<module>	   sn   ,ö*n@R		
ÿþÿþë

