B
    ²ô`•=  ã               @   s¸  d Z ddlZddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZmZ ddlmZmZ ddddd	d
ddddœ	ZG dd„ deƒZG dd„ deeƒZG dd„ deƒ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 d7d8d9d:d;d<d=d>d?d@dAgZ!dHdCdD„Z"dEdF„ Z#e$dGkr´e#ƒ  dS )IzD
A theorem prover that makes use of the external 'Prover9' package.
é    N)	Ú
ExpressionÚExistsExpressionÚAllExpressionÚNegatedExpressionÚAndExpressionÚIffExpressionÚOrExpressionÚEqualityExpressionÚImpExpression)ÚBaseProverCommandÚProverTz(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   sd   |  ¡ dkr(xR|  ¡ D ]}t|ƒ qW n8|  ¡ dkrTx*t|  ¡ ƒD ]}t|ƒ qBW 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   úE/home/dcms/DCMS/lib/python3.7/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k	r t |tƒs(t‚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ÚProver9ÚAssertionErrorr   Ú__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+   r1   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 krd | _ d | _n<d}tjj||dgd||d g|d| _| j tjjd¡| _ d S )Nr   ÚPROVER9z&http://www.cs.unm.edu/~mccune/prover9/z.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_locationr7   Únamer   r   r    Úconfig_prover9y   s    

zProver9Parent.config_prover9c             C   s^   d}|r6|d7 }xt |ƒD ]}|d| 7 }qW |d7 }|rZ|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   ddddddg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 k	r|| jg7 }tjj||dgd||d g|dS )Nr3   z&http://www.cs.unm.edu/~mccune/prover9/z.exe)Z
searchpathr4   r5   r6   r7   )rE   r8   r   r:   r;   )r   r@   r7   rE   r   r   r    Ú_find_binary­   s    

zProver9Parent._find_binaryc       	      C   sº   |r$t d|ƒ t d|ƒ t d|dƒ |g| }y| d¡}W n tk
rP   Y nX 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Úargsr7   ÚcmdÚprI   rJ   r   r   r    Ú_callº   s&    



zProver9Parent._call)F)F)
r"   r#   r$   r%   r8   rA   rD   rE   rF   r[   r   r   r   r    r2   p   s   

r2   c             C   s~   t | tƒrRg }x>| D ]6}y| t| ¡ ƒ¡ W q   td|  ƒ ‚ Y qX qW |S yt|  ¡ ƒS    td|  ƒ ‚ Y nX dS )z;
    Convert a ``logic.Expression`` to Prover9 format.
    z4input %s cannot be converted to Prover9 input syntaxN)r(   ÚlistÚappendÚ_convert_to_prover9r0   r   )rL   ÚresultrC   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.
        )r7   r   )Ú_call_prover9rD   )r   r,   r   r7   rI   rT   r   r   r    Ú_prove=  s
    zProver9._provec             C   s   d}|t  | ||¡ S )z3
        :see: Prover9Parent.prover9_input
        zclear(auto_denials).
)r2   rD   )r   r,   r   rC   r   r   r    rD   L  s    zProver9.prover9_inputc       
      C   s¬   | j dkr|  d|¡| _ d}| jdkr4|d| j 7 }||7 }|  || j ||¡\}}|dkr¤d}||kr‚| |¡}||d…  ¡ }	nd}	|dk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   rB   r   zassign(max_seconds, %d).

)r   r   z%%ERROR:)r   r   r   r   )r9   rF   rg   r[   ÚindexÚstripÚProver9LimitExceededExceptionÚProver9FatalException)
r   rV   rX   r7   Zupdated_input_strrI   rT   ZerrormsgprefixZmsgstartZerrormsgr   r   r    rh   S  s$    	



zProver9._call_prover9c             C   s*   | j dk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_binrF   r[   )r   rV   rX   r7   r   r   r    r.   v  s    	
zProver9._call_prooftrans)r'   )NNF)
r"   r#   r$   r9   rn   r+   ri   rD   rh   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   rT   ÚmessageÚmsgr   r   r    r+   †  s    zProver9Exception.__init__N)r"   r#   r$   r+   r   r   r   r    ro   …  s   ro   c               @   s   e Zd ZdS )rm   N)r"   r#   r$   r   r   r   r    rm     s   rm   c               @   s   e Zd ZdS )rl   N)r"   r#   r$   r   r   r   r    rl   ‘  s   rl   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   ÚgrZ   r   r   r    Útest_configš  s    

rw   c             C   s(   x"| D ]}t  |¡}tt|ƒƒ qW dS )z%
    Test that parsing works OK.
    N)r   rt   r   r   )ÚexprÚtÚer   r   r    Útest_convert_to_prover9§  s    

r{   c             C   sh   xb| D ]Z\}}t  |¡}dd„ |D ƒ}t||d ¡ }x|D ]}td| ƒ q<W td||f ƒ qW dS )z2
    Try some proofs and exhibit the results.
    c             S   s   g | ]}t  |¡‘qS r   )r   rt   )Ú.0r   r   r   r    ú
<listcomp>¶  s    ztest_prove.<locals>.<listcomp>)r   z   %sz
|- %s: %s
N)r   rt   r&   ru   r   )Ú	argumentsr,   r   rv   ÚalistrZ   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„   rw   r{   Úexpressionsr€   r~   r   r   r   r    Údemoí  s    r†   Ú__main__)r   )%r%   r=   rO   r   Znltk.sem.logicr   r   r   r   r   r   r   r	   r
   Znltk.inference.apir   r   rp   Úobjectr   r&   r2   r   r^   r)   rq   ro   rm   rl   rw   r{   r€   r~   r…   r„   r†   r"   r   r   r   r    Ú<module>   sv   ,*n@R		



