B
    P?ð[“d  ã               @   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 ddl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eƒZ G dd„ deƒZ!G dd„ deƒZ"dd„ Z#dd„ Z$ddd„Z%dd„ Z&e'dkre&ƒ  dS )z8
Module for a tableau-based First Order theorem prover.
é    )Úprint_functionÚunicode_literals)ÚCounter)ÚVariableExpressionÚEqualityExpressionÚApplicationExpressionÚ
ExpressionÚAbstractVariableExpressionÚAllExpressionÚNegatedExpressionÚExistsExpressionÚVariableÚImpExpressionÚAndExpressionÚunique_variableÚLambdaExpressionÚIffExpressionÚOrExpressionÚFunctionVariableExpression)ÚProverÚBaseProverCommandc               @   s   e Zd ZdS )ÚProverParseErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú5lib/python3.7/site-packages/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$| | ¡ | |¡ t|ƒ}|  |tƒ tƒ |¡}W nN tk
rš } z0| jrxt|ƒ 	d¡rxd}n|r†t
|ƒ n|‚W d d }~X Y nX |d |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ò   |  ¡ \\}}}|s"| 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| }| ||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,   r1   Úaccessible_varsÚatomsÚdebugÚcurrentÚcontextÚcategoryZproof_methodr   r   r   r#   G   s8    

zTableauProver._attempt_proofc             C   s’   |df|kr|  dd¡ dS |rZt|jtƒr4| ¡ }| ||ƒ ¡ ¡ |  ||||d ¡S | ¡  |  ||t	|j
ƒB |t	|dfgƒB |d ¡S d S )NTÚCLOSEDé   F)r5   Ú
isinstanceÚtermr   Únegater    Úsimplifyr#   Úmark_alls_freshr$   Úargs)r,   rd   re   r1   ra   rb   rc   r   r   r   r8   j   s    z!TableauProver._attempt_proof_atomc             C   s˜   |j df|kr| dd¡ dS |r\t|j tƒr6| ¡ }| ||ƒ ¡ ¡ |  ||||d ¡S | ¡  |  ||t	|j j
ƒB |t	|j dfgƒB |d ¡S d S )NFrg   rh   T)rj   r5   ri   r   rk   r    rl   r#   rm   r$   rn   )r,   rd   re   r1   ra   rb   rc   r   r   r   r<      s    z#TableauProver._attempt_proof_n_atomc             C   sF   |df|kr|  dd¡ dS | ¡  |  |||t|dfgƒB |d ¡S )NTrg   rh   F)r5   rm   r#   r$   )r,   rd   re   r1   ra   rb   rc   r   r   r   r:   ˜   s    z!TableauProver._attempt_proof_propc             C   sJ   |j df|kr| dd¡ dS | ¡  |  |||t|j dfgƒB |d ¡S )NFrg   rh   T)rj   r5   rm   r#   r$   )r,   rd   re   r1   ra   rb   rc   r   r   r   r>   ¦   s    z#TableauProver._attempt_proof_n_propc             C   s¸   |  ¡ \}}xžt|ƒD ]’\}	}
t |
¡s|}tdt ¡  ƒ}x0t|ƒD ]$\}}|	|krb|t|ƒƒn||ƒ}qFW |r~||ƒ ¡ }t	||ƒ}| 
|
|¡ |  ||||d ¡S qW tdƒ‚d S )NzX%srh   z=If this method is called, there must be a non-atomic argument)ÚuncurryÚ	enumerater   Úis_atomr   Ú_counterÚgetr   rl   r   r    r#   Ú	Exception)r,   rd   re   r1   ra   rb   rc   Úfrn   ÚiÚargÚctxÚnvÚjÚar   r   r   r@   ´   s    
 
z TableauProver._attempt_proof_appc             C   s¾   |j  ¡ \}}x¢t|ƒD ]–\}	}
t |
¡s|}tdt ¡  ƒ}x0t|ƒD ]$\}}|	|krd|t|ƒƒn||ƒ}qHW |r€||ƒ 	¡ }t
|| ƒ}| |
 |¡ |  ||||d ¡S qW tdƒ‚d S )NzX%srh   z=If this method is called, there must be a non-atomic argument)rj   ro   rp   r   rq   r   rr   rs   r   rl   r   r    r#   rt   )r,   rd   re   r1   ra   rb   rc   ru   rn   rv   rw   rx   ry   rz   r{   r   r   r   rB   Å   s    
 z"TableauProver._attempt_proof_n_appc             C   sb   |j j|j jkr | dd¡ dS |tj  ||f¡ d|_|  ||t	|j j|j jgƒB ||d ¡S )Nrg   rh   T)
rj   ÚfirstÚsecondr5   r6   rC   ÚaddÚ
_exhaustedr#   r$   )r,   rd   re   r1   ra   rb   rc   r   r   r   rD   ×   s    z!TableauProver._attempt_proof_n_eqc             C   s$   |  |jj|¡ |  ||||d ¡S )Nrh   )r    rj   r#   )r,   rd   re   r1   ra   rb   rc   r   r   r   rF   ê   s    z"TableauProver._attempt_proof_d_negc             C   s8   |t j  t|jj|jj ƒ|f¡ |  ||||d ¡S )Nrh   )r6   r]   r~   r   rj   Úvariabler#   )r,   rd   re   r1   ra   rb   rc   r   r   r   rH   ð   s    
z"TableauProver._attempt_proof_n_allc             C   s8   |t j  t|jj|jj ƒ|f¡ |  ||||d ¡S )Nrh   )r6   r_   r~   r
   rj   r€   r#   )r,   rd   re   r1   ra   rb   rc   r   r   r   rJ   ø   s    
z#TableauProver._attempt_proof_n_somec             C   s0   |  |j|¡ |  |j|¡ |  ||||d ¡S )Nrh   )r    r|   r}   r#   )r,   rd   re   r1   ra   rb   rc   r   r   r   rL      s    z TableauProver._attempt_proof_andc             C   s8   |  |jj |¡ |  |jj |¡ |  ||||d ¡S )Nrh   )r    rj   r|   r}   r#   )r,   rd   re   r1   ra   rb   rc   r   r   r   rN     s    z!TableauProver._attempt_proof_n_orc             C   s6   |  |jj|¡ |  |jj |¡ |  ||||d ¡S )Nrh   )r    rj   r|   r}   r#   )r,   rd   re   r1   ra   rb   rc   r   r   r   rP     s    z"TableauProver._attempt_proof_n_impc             C   sL   |  ¡ }| |j|¡ | |j|¡ |  ||||d ¡oJ|  ||||d ¡S )Nrh   )Úcloner    r|   r}   r#   )r,   rd   re   r1   ra   rb   rc   Ú
new_agendar   r   r   rR     s    zTableauProver._attempt_proof_orc             C   sN   |  ¡ }| |j |¡ | |j|¡ |  ||||d ¡oL|  ||||d ¡S )Nrh   )r   r    r|   r}   r#   )r,   rd   re   r1   ra   rb   rc   r‚   r   r   r   rT     s    z TableauProver._attempt_proof_impc             C   sT   |  ¡ }| |jj |¡ | |jj |¡ |  ||||d ¡oR|  ||||d ¡S )Nrh   )r   r    rj   r|   r}   r#   )r,   rd   re   r1   ra   rb   rc   r‚   r   r   r   rV   )  s    z"TableauProver._attempt_proof_n_andc             C   sl   |  ¡ }| |j|¡ | |j|¡ | |j |¡ | |j |¡ |  ||||d ¡oj|  ||||d ¡S )Nrh   )r   r    r|   r}   r#   )r,   rd   re   r1   ra   rb   rc   r‚   r   r   r   rX   3  s    z TableauProver._attempt_proof_iffc             C   st   |  ¡ }| |jj|¡ | |jj |¡ | |jj |¡ | |jj|¡ |  ||||d ¡or|  ||||d ¡S )Nrh   )r   r    rj   r|   r}   r#   )r,   rd   re   r1   ra   rb   rc   r‚   r   r   r   rZ   ?  s    z"TableauProver._attempt_proof_n_iffc             C   sD   |  |¡ | |j|j¡ | |j¡ | ¡  |  ||tƒ |d ¡S )Nrh   )Ú	put_atomsÚreplace_allr|   r}   ÚdiscardÚmark_neqs_freshr#   r$   )r,   rd   re   r1   ra   rb   rc   r   r   r   r\   K  s
    
zTableauProver._attempt_proof_eqc             C   sH   t tƒ ƒ}| |j |j|¡|¡ | ¡  |  ||t|gƒB ||d ¡S )Nrh   )	r   r   r    rj   Úreplacer€   rm   r#   r$   )r,   rd   re   r1   ra   rb   rc   Únew_unique_variabler   r   r   r^   X  s
    
z!TableauProver._attempt_proof_somec       
      C   sj  y
|j  W n tk
r&   tƒ |_ Y nX |ræ||j  }|rªt|ƒd }| d| d¡ | j t|gƒO  _ | |j |j|¡|¡ |t	j
  ||f¡ |  ||||d ¡S | dd¡ d|_|t	j
  ||f¡ |  ||||d ¡S n€ttƒ ƒ}	| d|	 d¡ | j t|	gƒO  _ | |j |j|	¡|¡ |t	j
  ||f¡ | ¡  |  ||t|	gƒB ||d ¡S d S )Nr   z--> Using '%s'é   rh   z--> Variables ExhaustedT)Ú
_used_varsÚAttributeErrorr$   Úlistr5   r    rj   r‡   r€   r6   r_   r~   r#   r   r   r   rm   )
r,   rd   re   r1   ra   rb   rc   Zbv_availableZvariable_to_userˆ   r   r   r   r`   b  s6    


z TableauProver._attempt_proof_allc             C   s\   t | tƒr| j} t | tƒr<x| jD ]}t |¡s"dS q"W dS t | tƒsPt | tƒrTdS dS d S )NFT)	ri   r   rj   r   rn   r   rq   r	   r   )r2   rw   r   r   r   rq     s    


zTableauProver.is_atom)NNF)r   r   r   r&   r3   r#   r8   r<   r:   r>   r@   rB   rD   rF   rH   rJ   rL   rN   rP   rR   rT   rV   rX   rZ   r\   r^   r`   Ústaticmethodrq   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 | |||¡ 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)ri   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$   )Ú.0rv   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ƒ }xj|tj D ]\\}}t|j|jƒ}ytdd„ |jD ƒƒ|_W n t	k
rt   tƒ |_Y nX | 
|d f¡ q(W ||tj< tdd„ |tj D ƒƒ|tj< t|ƒ|_|S )Nc             S   s   g | ]}|  ¡ ‘qS r   )Úcopy)r‘   Úsr   r   r   ú
<listcomp>¶  s    z Agenda.clone.<locals>.<listcomp>c             s   s   | ]
}|V  qd S )Nr   )r‘   Úusedr   r   r   r’   ¼  s    zAgenda.clone.<locals>.<genexpr>c             s   s    | ]\}}t |jƒ|fV  qd S )N)r   rj   )r‘   Zn_eqrx   r   r   r   r’   Ã  s   )r   r–   r$   r6   r_   r
   r€   rj   rŠ   r‹   r~   rC   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   st   t |tƒrRt|j|jƒ}ytdd„ |jD ƒƒ|_W qV tk
rN   tƒ |_Y qVX n|}| j|  |¡  	||f¡ d S )Nc             s   s   | ]
}|V  qd S )Nr   )r‘   rš   r   r   r   r’   Ñ  s    zAgenda.put.<locals>.<genexpr>)
ri   r
   r€   rj   r$   rŠ   r‹   r–   Ú_categorize_expressionr~   )r,   Ú
expressionre   Z	ex_to_addr   r   r   r    Í  s    
z
Agenda.putc             C   s   x|D ]}|   |¡ 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  | d f¡ q| t j  |d f¡ qW d S )N)r6   r;   r~   r7   )r,   rb   ZatomÚnegr   r   r   rƒ   Ü  s    zAgenda.put_atomsc          
   C   s   xŠt | jƒD ]|\}}|r|tjtjgkr|x^|D ]H}y |d jsP| |¡ ||fS W q. tk
rt   | |¡ ||fS X q.W q| ¡ |fS qW dS )z5 Pop the first expression that appears in the agenda r   ))NNN)	rp   r–   r6   rC   r_   r   Úremover‹   Úpop)r,   rv   r˜   Úexr   r   r   r4   ã  s    



zAgenda.pop_firstc             C   sJ   xD| j D ]:}x4|D ],\}}| |j|¡ |d k	r| |j|¡ qW qW d S )N)r–   r‡   r€   )r,   ÚoldÚnewr˜   r£   rx   r   r   r   r„   ô  s
    zAgenda.replace_allc             C   s$   x| j tj D ]\}}d|_qW d S )NF)r–   r6   r_   r   )r,   Úur›   r   r   r   rm   û  s    zAgenda.mark_alls_freshc             C   s$   x| j tj D ]\}}d|_qW d S )NF)r–   r6   rC   r   )r,   Zneqr›   r   r   r   r†   ÿ  s    zAgenda.mark_neqs_freshc             C   sÈ   t |tƒr|  |¡S t |tƒr$tjS t |¡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ƒr„tjS t |tƒr”tjS t |tƒr¤tjS t |tƒr´tjS td|jj ƒ‚d S )Nzcannot categorize %s)ri   r   Ú_categorize_NegatedExpressionr   r6   r9   r   rq   r7   r
   r_   r   rK   r   rQ   r   rS   r   rW   r   r[   r   r]   r   r?   r   Ú	__class__r   )r,   rd   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 |¡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ƒr†tjS t|tƒr–tjS t|tƒr¦tjS t|tƒr¶tjS td|jj ƒ‚d S )Nzcannot categorize %s)rj   ri   r   r6   rE   r   r=   r   rq   r;   r
   rG   r   rU   r   rM   r   rO   r   rY   r   rC   r   rI   r   rA   r   r¨   r   )r,   rd   Znegatedr   r   r   r§     s0    










z$Agenda._categorize_NegatedExpression)N)r   r   r   r   r   r   r    r!   rƒ   r4   r„   rm   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   ;  s
    zDebug.__init__c             C   s   t | j| jd | jƒS )Nrh   )r"   r/   r©   r+   )r,   Z	incrementr   r   r   Ú__add__C  s    zDebug.__add__c             C   s²   t |tƒr~|\}}|r$d||f }nd| }t |tƒr~y*dd dd„ |jD ƒ¡ }|d| 7 }W n tk
r|   |d7 }Y nX d	d
| j|  |f }| j |¡ | j	r®t
|ƒ d S )Nz%s, %sz%sz[%s]ú,c             s   s   | ]}d |j j V  qdS )z%sN)r€   Úname)r‘   Zver   r   r   r’   Q  s    zDebug.line.<locals>.<genexpr>z:   %sz:   []z%s%sz   )ri   r”   r
   r*   rŠ   r‹   r©   r+   Úappendr/   r)   )r,   Údatar©   r£   rx   Z	used_varsÚnewliner   r   r   r5   F  s     

z
Debug.line)r   N)r   )r   r   r   r   rª   r5   r   r   r   r   r"   :  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 )r6   r   rh   r‰   é   é   é   é   é   é   é	   é
   é   é   é   é   é   é   é   é   é   é   N)r   r   r   r7   r9   r;   r=   r?   rA   rC   rE   rG   rI   rK   rM   rO   rQ   rS   rU   rW   rY   r[   r]   r_   r   r   r   r   r6   ^  s*   r6   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)Zp1Zp2ÚcÚpr   r   r   ÚtestTableauProverv  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  | ¡}|rdd„ |D ƒng }|s(g }tdd |¡|tƒ j|||df ƒ d S )Nc             S   s   g | ]}t  |¡‘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
__future__r   r   Znltk.internalsr   Znltk.sem.logicr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   Znltk.inference.apir   r   rr   rt   r   r   rŽ   Úobjectr   r"   r6   rÇ   rÈ   rÄ   rÊ   r   r   r   r   r   Ú<module>   s(   H  u $4

