3
df                 @   s   d Z ddlmZmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZ e ZG dd deZG dd deZG d	d
 d
eZG dd dZG dd dZG dd dZdd Zdd Z dddZ!dd Z"e#dkre"  dS )z8
Module for a tableau-based First Order theorem prover.
    )BaseProverCommandProver)Counter)AbstractVariableExpressionAllExpressionAndExpressionApplicationExpressionEqualityExpressionExistsExpression
ExpressionFunctionVariableExpressionIffExpressionImpExpressionLambdaExpressionNegatedExpressionOrExpressionVariableVariableExpressionunique_variablec               @   s   e Zd ZdS )ProverParseErrorN)__name__
__module____qualname__ r   r   6/tmp/pip-build-v9q4h5k9/nltk/nltk/inference/tableau.pyr   %   s   r   c               @   s   e Zd ZdZd3ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zed1d2 ZdS )4TableauProverFNc             C   s   |sg }d }y@t  }|r$|j|  |j| t|}| j|t t |}W nN tk
r } z2| jrxt|j	drxd}n|rt
| n|W Y d d }~X nX |dj|jfS )Nz maximum recursion depth exceededF
)Agendaputput_allDebug_attempt_proofsetRuntimeError_assume_falsestr
startswithprintjoinlines)selfgoalassumptionsverboseresultagendaZdebuggerer   r   r   _prove,   s$    

zTableauProver._provec       	   *   C   s   |j  \\}}}|s"|jd dS tj| jtj| jtj| jtj	| j
tj| jtj| jtj| jtj| jtj| jtj| jtj| jtj| jtj| jtj| jtj| j tj!| j"tj#| j$tj%| j&tj'| j(tj)| j*tj+| j,i| }|j||f |||||||S )NzAGENDA EMPTYF)-	pop_firstline
CategoriesATOM_attempt_proof_atomPROP_attempt_proof_propN_ATOM_attempt_proof_n_atomN_PROP_attempt_proof_n_propAPP_attempt_proof_appN_APP_attempt_proof_n_appN_EQ_attempt_proof_n_eqD_NEG_attempt_proof_d_negN_ALL_attempt_proof_n_allN_EXISTS_attempt_proof_n_someAND_attempt_proof_andN_OR_attempt_proof_n_orN_IMP_attempt_proof_n_impOR_attempt_proof_orIMP_attempt_proof_impN_AND_attempt_proof_n_andIFF_attempt_proof_iffN_IFF_attempt_proof_n_iffEQ_attempt_proof_eqEXISTS_attempt_proof_someALL_attempt_proof_all)	r*   r/   accessible_varsatomsdebugcurrentcontextcategoryZproof_methodr   r   r   r!   D   s8    

zTableauProver._attempt_proofc             C   s   |df|kr|j dd dS |rZt|jtr4|j }|j||j  | j||||d S |j  | j||t	|j
B ||dfhB |d S d S )NTCLOSED   F)r3   
isinstancetermr   negater   simplifyr!   mark_alls_freshr"   args)r*   rb   rc   r/   r_   r`   ra   r   r   r   r6   g   s    z!TableauProver._attempt_proof_atomc             C   s   |j df|kr|jdd dS |r\t|j tr6|j }|j||j  | j||||d S |j  | j||t	|j j
B ||j dfhB |d S d S )NFre   rf   T)rh   r3   rg   r   ri   r   rj   r!   rk   r"   rl   )r*   rb   rc   r/   r_   r`   ra   r   r   r   r:   ~   s    z#TableauProver._attempt_proof_n_atomc             C   sB   |df|kr|j dd dS |j  | j||||dfhB |d S )NTre   rf   F)r3   rk   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   r8      s    z!TableauProver._attempt_proof_propc             C   sF   |j df|kr|jdd dS |j  | j||||j dfhB |d S )NFre   rf   T)rh   r3   rk   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   r<      s    z#TableauProver._attempt_proof_n_propc             C   s   |j  \}}xt|D ]\}	}
tj|
s|}tdtj  }x0t|D ]$\}}|	|krb|t|n||}qFW |r~||j }t	||}|j
|
| | j||||d S qW tdd S )NzX%srf   z=If this method is called, there must be a non-atomic argument)uncurry	enumerater   is_atomr   _countergetr   rj   r   r   r!   	Exception)r*   rb   rc   r/   r_   r`   ra   frl   iargctxnvjar   r   r   r>      s    
 
z TableauProver._attempt_proof_appc             C   s   |j j \}}xt|D ]\}	}
tj|
s|}tdtj  }x0t|D ]$\}}|	|krd|t|n||}qHW |r||j	 }t
|| }|j|
 | | j||||d S qW tdd S )NzX%srf   z=If this method is called, there must be a non-atomic argument)rh   rm   rn   r   ro   r   rp   rq   r   rj   r   r   r!   rr   )r*   rb   rc   r/   r_   r`   ra   rs   rl   rt   ru   rv   rw   rx   ry   r   r   r   r@      s    
 z"TableauProver._attempt_proof_n_appc             C   s^   |j j|j jkr |jdd dS |tj j||f d|_| j|||j j|j jhB ||d S )Nre   rf   T)	rh   firstsecondr3   r4   rA   add
_exhaustedr!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rB      s    z!TableauProver._attempt_proof_n_eqc             C   s$   |j |jj| | j||||d S )Nrf   )r   rh   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rD      s    z"TableauProver._attempt_proof_d_negc             C   s8   |t j jt|jj|jj |f | j||||d S )Nrf   )r4   r[   r|   r
   rh   variabler!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rF      s    
z"TableauProver._attempt_proof_n_allc             C   s8   |t j jt|jj|jj |f | j||||d S )Nrf   )r4   r]   r|   r   rh   r~   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rH      s    
z#TableauProver._attempt_proof_n_somec             C   s0   |j |j| |j |j| | j||||d S )Nrf   )r   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rJ      s    z TableauProver._attempt_proof_andc             C   s8   |j |jj | |j |jj | | j||||d S )Nrf   )r   rh   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rL     s    z!TableauProver._attempt_proof_n_orc             C   s6   |j |jj| |j |jj | | j||||d S )Nrf   )r   rh   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rN     s    z"TableauProver._attempt_proof_n_impc             C   sL   |j  }|j|j| |j|j| | j||||d oJ| j||||d S )Nrf   )cloner   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   
new_agendar   r   r   rP     s    zTableauProver._attempt_proof_orc             C   sN   |j  }|j|j | |j|j| | j||||d oL| j||||d S )Nrf   )r   r   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   r   rR     s    z TableauProver._attempt_proof_impc             C   sT   |j  }|j|jj | |j|jj | | j||||d oR| j||||d S )Nrf   )r   r   rh   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   r   rT   &  s    z"TableauProver._attempt_proof_n_andc             C   sl   |j  }|j|j| |j|j| |j|j | |j|j | | j||||d oj| j||||d S )Nrf   )r   r   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   r   rV   0  s    z TableauProver._attempt_proof_iffc             C   st   |j  }|j|jj| |j|jj | |j|jj | |j|jj| | j||||d or| j||||d S )Nrf   )r   r   rh   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   r   r   r   r   rX   <  s    z"TableauProver._attempt_proof_n_iffc             C   sD   |j | |j|j|j |j|j |j  | j||t |d S )Nrf   )	put_atomsreplace_allrz   r{   discardmark_neqs_freshr!   r"   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rZ   H  s
    
zTableauProver._attempt_proof_eqc             C   sD   t t }|j|jj|j|| |j  | j|||hB ||d S )Nrf   )r   r   r   rh   replacer~   rk   r!   )r*   rb   rc   r/   r_   r`   ra   new_unique_variabler   r   r   r\   U  s
    
z!TableauProver._attempt_proof_somec       
      C   s^  y
|j  W n tk
r&   t |_ Y nX |r||j  }|rt|d }|jd| d | j |hO  _ |j|jj|j|| |t	j
 j||f | j||||d S |jdd d|_|t	j
 j||f | j||||d S nxtt }	|jd|	 d | j |	hO  _ |j|jj|j|	| |t	j
 j||f |j  | j|||	hB ||d S d S )Nr   z--> Using '%s'   rf   z--> Variables ExhaustedT)
_used_varsAttributeErrorr"   listr3   r   rh   r   r~   r4   r]   r|   r!   r}   r   r   rk   )
r*   rb   rc   r/   r_   r`   ra   Zbv_availableZvariable_to_user   r   r   r   r^   _  s6    


z TableauProver._attempt_proof_allc             C   s\   t | tr| j} t | tr<x| jD ]}tj|s"dS q"W dS t | tsPt | trTdS dS d S )NFT)	rg   r   rh   r   rl   r   ro   r   r   )r0   ru   r   r   r   ro     s    


zTableauProver.is_atom)NNF)r   r   r   r$   r1   r!   r6   r:   r8   r<   r>   r@   rB   rD   rF   rH   rJ   rL   rN   rP   rR   rT   rV   rX   rZ   r\   r^   staticmethodro   r   r   r   r   r   )   s2   
#



+r   c               @   s   e Zd ZdddZdS )TableauProverCommandNc             C   s2   |dk	rt |tstnt }tj| ||| dS )z
        :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)
        N)rg   r   AssertionErrorr   __init__)r*   r+   r,   Zproverr   r   r   r     s    zTableauProverCommand.__init__)NNN)r   r   r   r   r   r   r   r   r     s   r   c               @   sn   e Zd Zdd Zdd Zdd Zddd	Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )r   c             C   s   t dd tdD | _d S )Nc             s   s   | ]}t  V  qd S )N)r"   ).0rt   r   r   r   	<genexpr>  s    z"Agenda.__init__.<locals>.<genexpr>   )tuplerangesets)r*   r   r   r   r     s    zAgenda.__init__c             C   s   t  }dd | jD }t }xf|tj D ]X\}}t|j|j}ydd |jD |_W n t	k
rp   t |_Y nX |j
|d f q(W ||tj< dd |tj D |tj< t||_|S )Nc             S   s   g | ]}|j  qS r   )copy)r   sr   r   r   
<listcomp>  s    z Agenda.clone.<locals>.<listcomp>c             S   s   h | ]}|qS r   r   )r   usedr   r   r   	<setcomp>  s    zAgenda.clone.<locals>.<setcomp>c             S   s   h | ]\}}t |j|fqS r   )r   rh   )r   Zn_eqrv   r   r   r   r     s   )r   r   r"   r4   r]   r   r~   rh   r   r   r|   rA   r   )r*   r   Zset_listZ
new_allExsZallEx_Z	new_allExr   r   r   r     s    

zAgenda.clonec             C   s
   | j | S )N)r   )r*   indexr   r   r   __getitem__  s    zAgenda.__getitem__Nc             C   sp   t |trNt|j|j}ydd |jD |_W qR tk
rJ   t |_Y qRX n|}| j| j| j	||f d S )Nc             S   s   h | ]}|qS r   r   )r   r   r   r   r   r     s    zAgenda.put.<locals>.<setcomp>)
rg   r   r~   rh   r   r   r"   r   _categorize_expressionr|   )r*   
expressionrc   Z	ex_to_addr   r   r   r     s    
z
Agenda.putc             C   s   x|D ]}| j | qW d S )N)r   )r*   Zexpressionsr   r   r   r   r     s    
zAgenda.put_allc             C   sF   x@|D ]8\}}|r*| t j j| d f q| t j j|d f qW d S )N)r4   r9   r|   r5   )r*   r`   Zatomnegr   r   r   r     s    zAgenda.put_atomsc             C   s   xt | jD ]|\}}|r|tjtjgkr|x^|D ]H}y |d jsP|j| ||fS W q. tk
rt   |j| ||fS X q.W q|j |fS qW dS )z3Pop the first expression that appears in the agendar   NNN)r   N)	rn   r   r4   rA   r]   r}   remover   pop)r*   rt   r   exr   r   r   r2     s    



zAgenda.pop_firstc             C   sJ   xD| j D ]:}x4|D ],\}}|j|j| |d k	r|j|j| qW qW d S )N)r   r   r~   )r*   oldnewr   r   rv   r   r   r   r     s
    zAgenda.replace_allc             C   s$   x| j tj D ]\}}d|_qW d S )NF)r   r4   r]   r}   )r*   ur   r   r   r   rk     s    zAgenda.mark_alls_freshc             C   s$   x| j tj D ]\}}d|_qW d S )NF)r   r4   rA   r}   )r*   Zneqr   r   r   r   r     s    zAgenda.mark_neqs_freshc             C   s   t |tr| j|S t |tr$tjS tj|r4tjS t |t	rDtj
S t |trTtjS t |trdtjS t |trttjS t |trtjS t |trtjS t |trtjS t |trtjS td|jj d S )Nzcannot categorize %s)rg   r   _categorize_NegatedExpressionr   r4   r7   r   ro   r5   r   r]   r   rI   r   rO   r   rQ   r   rU   r	   rY   r
   r[   r   r=   r   	__class__r   )r*   rb   r   r   r   r      s.    











zAgenda._categorize_expressionc             C   s   |j }t|trtjS t|tr&tjS tj|r6tj	S t|t
rFtjS t|trVtjS t|trftjS t|trvtjS t|trtjS t|trtjS t|trtjS t|trtjS td|jj d S )Nzcannot categorize %s)rh   rg   r   r4   rC   r   r;   r   ro   r9   r   rE   r   rS   r   rK   r   rM   r   rW   r	   rA   r
   rG   r   r?   r   r   r   )r*   rb   Znegatedr   r   r   r     s0    










z$Agenda._categorize_NegatedExpression)N)r   r   r   r   r   r   r   r   r   r2   r   rk   r   r   r   r   r   r   r   r     s   
r   c               @   s(   e Zd Zd	ddZdd Zd
ddZdS )r    r   Nc             C   s   || _ || _|sg }|| _d S )N)r-   indentr)   )r*   r-   r   r)   r   r   r   r   8  s
    zDebug.__init__c             C   s   t | j| jd | jS )Nrf   )r    r-   r   r)   )r*   	incrementr   r   r   __add__@  s    zDebug.__add__c             C   s   t |tr|\}}|r&| d| }nd| }t |try*ddjdd |jD  }|d| 7 }W n tk
r~   |d7 }Y nX d	jd
| j|  |}| jj	| | j
rt| d S )Nz, z%sz[%s],c             s   s   | ]}d |j j V  qdS )z%sN)r~   name)r   ver   r   r   r   N  s    zDebug.line.<locals>.<genexpr>z:   %sz:   []z{}{}z   )rg   r   r   r(   r   r   formatr   r)   appendr-   r'   )r*   datar   r   rv   Z	used_varsnewliner   r   r   r3   C  s     

z
Debug.line)r   N)r   )r   r   r   r   r   r3   r   r   r   r   r    7  s   
r    c               @   s`   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdS )r4   r   rf   r                     	   
                                 N)r   r   r   r5   r7   r9   r;   r=   r?   rA   rC   rE   rG   rI   rK   rM   rO   rQ   rS   rU   rW   rY   r[   r]   r   r   r   r   r4   [  s*   r4   c              C   s  t d t d t dddg t d t d t d t d	 t d
 t d t d	 t d
 t d t d t d t d t d t d d} d}d}t || |g d} d}d}t || |g d}d}t ||g d}d}t ||g d}d}t ||g d}t | d S )NzP | -PzP & -PQPz(P -> Q)zman(x)z(man(x) -> man(x))z(man(x) -> --man(x))z-(man(x) and -man(x))z(man(x) or -man(x))z(man(x) iff man(x))z-(man(x) iff -man(x))zall x.man(x)z all x.all y.((x = y) -> (y = x))z2all x.all y.all z.(((x = y) & (y = z)) -> (x = z))zall x.(man(x) -> mortal(x))zman(Socrates)zmortal(Socrates)zall x.(man(x) -> walks(x))z	man(John)zsome y.walks(y)z((x = y) & walks(y))zwalks(x)z((x = y) & ((y = z) & (z = w)))z(x = w)z5some e1.some e2.(believe(e1,john,e2) & walk(e2,mary))zsome e0.walk(e0,mary)z(exists x.exists z3.((x = Mary) & ((z3 = John) & sees(z3,x))) <-> exists x.exists z4.((x = John) & ((z4 = Mary) & sees(x,z4)))))tableau_test)p1p2cpr   r   r   testTableauProvers  sH    r   c               C   sX   t ddg t ddg t ddg t ddg t dd	g t d
dg t ddg d S )Nzbelieve(j, -lie(b))zbelieve(j, -lie(b) & -cheat(b))zbelieve(j, lie(b) & cheat(b))zbelieve(j, lie(b))zlie(b)zbelieve(j, know(b, cheat(b)))z;believe(j, know(b, lie(b)) & know(b, steals(b) & cheat(b)))zP(Q(y), R(y) & R(z))zP(Q(x) & Q(y), R(y) & R(z))zbelieve(j, cheat(b) & lie(b))zbelieve(j, -cheat(b) & -lie(b)))r   r   r   r   r   testHigherOrderTableauProver  s    
r   NFc          	   C   sR   t j| }|rdd |D ng }|s(g }tddj||t j|||df  d S )Nc             S   s   g | ]}t j|qS r   )r   
fromstring)r   r   r   r   r   r     s    z tableau_test.<locals>.<listcomp>z%s |- %s: %sz, )r-   )r   r   r'   r(   r   Zprove)r   Zpsr-   ZpcZppsr   r   r   r     s    
r   c               C   s   t   t  d S )N)r   r   r   r   r   r   demo  s    r   __main__)NF)$__doc__Znltk.inference.apir   r   Znltk.internalsr   Znltk.sem.logicr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   rp   rr   r   r   r   r   r    r4   r   r   r   r   r   r   r   r   r   <module>   s&   H  u $4
