B
    P?[=                 @   s  d Z ddlm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/d0d1gfd2g fd3g fd4g fd5d6gfd7d6gfgZ"d8d9d:d;d<d=d>d?d@dAdBgZ#dIdDdEZ$dFdG Z%e&dHkre%  dS )JzD
A theorem prover that makes use of the external 'Prover9' package.
    )print_functionN)	
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    5lib/python3.7/site-packages/nltk/inference/prover9.pyprint_assumptions7   s    z&Prover9CommandParent.print_assumptionsN)r   )__name__
__module____qualname____doc__r"   r    r    r    r!   r   0   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,   N   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_proofe   s    
zProver9Command.decorate_proof)NNr(   N)T)r#   r$   r%   r&   r,   r2   r    r    r    r!   r'   G   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_locationr8   namer    r    r!   config_prover9z   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 )Nr4   z&http://www.cs.unm.edu/~mccune/prover9/z.exe)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| }y|d}W n tk
rP   Y nX tj|tjtjtjd}|j|d\}}|rt d|j |rt d	|d |rt 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PIPEZSTDOUTZcommunicate
returncodedecode)	r   	input_strZbinaryargsr8   cmdprJ   rK   r    r    r!   _call   s&    



zProver9Parent._call)F)F)
r#   r$   r%   r&   r9   rB   rE   rF   rG   rY   r    r    r    r!   r3   q   s   

r3   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_prover9r1   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rd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 | 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   strZ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,   8  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   rS   r    r    r!   _prove>  s
    zProver9._provec             C   s   d}|t | || S )z3
        :see: Prover9Parent.prover9_input
        zclear(auto_denials).
)r3   rE   )r   r-   r   rD   r    r    r!   rE   M  s    zProver9.prover9_inputc       
      C   s   | j dkr| d|| _ d}| jdkr4|d| j 7 }||7 }| || j ||\}}|dkrd}||kr||}||d  }	nd}	|dkrt||	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   rd   rY   indexstripProver9LimitExceededExceptionProver9FatalException)
r   rU   rV   r8   Zupdated_input_strrJ   rS   ZerrormsgprefixZmsgstartZerrormsgr    r    r!   re   T  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_binrG   rY   )r   rU   rV   r8   r    r    r!   r/   w  s    	
zProver9._call_prooftrans)r(   )NNF)
r#   r$   r%   r:   rk   r,   rf   rE   re   r/   r    r    r    r!   r*   4  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   rS   messagemsgr    r    r!   r,     s    zProver9Exception.__init__N)r#   r$   r%   r,   r    r    r    r!   rl     s   rl   c               @   s   e Zd ZdS )rj   N)r#   r$   r%   r    r    r    r!   rj     s   rj   c               @   s   e Zd ZdS )ri   N)r#   r$   r%   r    r    r    r!   ri     s   ri   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   grX   r    r    r!   test_config  s    

rt   c             C   s(   x"| D ]}t |}tt| qW dS )z%
    Test that parsing works OK.
    N)r   rq   r   r   )exprter    r    r!   test_convert_to_prover9  s    

rx   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   rq   ).0r   r    r    r!   
<listcomp>  s    ztest_prove.<locals>.<listcomp>)r   z   %sz
|- %s: %s
N)r   rq   r'   rr   r   )	argumentsr-   r   rs   ZalistrX   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   )Z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   rt   rx   expressionsr|   r{   r    r    r    r!   demo  s    r   __main__)r}   )'r&   Z
__future__r   r>   rP   r   Znltk.sem.logicr   r   r   r   r   r   r	   r
   r   Znltk.inference.apir   r   rm   objectr   r'   r3   r   r\   r*   rn   rl   rj   ri   rt   rx   r|   r{   r   r   r   r#   r    r    r    r!   <module>   sx   ,*n@R		



