B
    ²ô`Wd  ã               @   s  d 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.
é    )Ú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   úE/home/dcms/DCMS/lib/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*   r/   Úaccessible_varsÚatomsÚdebugÚcurrentÚcontextÚcategoryZproof_methodr   r   r   r!   F   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)r3   Ú
isinstanceÚtermr	   Únegater   Úsimplifyr!   Úmark_alls_freshr"   Úargs)r*   rb   rc   r/   r_   r`   ra   r   r   r   r6   i   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 )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   sF   |df|kr|  dd¡ dS | ¡  |  |||t|dfgƒB |d ¡S )NTre   rf   F)r3   rk   r!   r"   )r*   rb   rc   r/   r_   r`   ra   r   r   r   r8   —   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 )NFre   rf   T)rh   r3   rk   r!   r"   )r*   rb   rc   r/   r_   r`   ra   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%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  ¡ \}}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%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   sb   |j j|j jkr | dd¡ dS |tj  ||f¡ d|_|  ||t	|j j|j jgƒB ||d ¡S )Nre   rf   T)
rh   ÚfirstÚsecondr3   r4   rA   ÚaddÚ
_exhaustedr!   r"   )r*   rb   rc   r/   r_   r`   ra   r   r   r   rB   Ö   s    z!TableauProver._attempt_proof_n_eqc             C   s$   |  |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  t|jj|jj ƒ|f¡ |  ||||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  t|jj|jj ƒ|f¡ |  ||||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|¡ |  ||||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 |¡ |  ||||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 |¡ |  ||||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|¡ |  ||||d ¡oJ|  ||||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|¡ |  ||||d ¡oL|  ||||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 |¡ |  ||||d ¡oR|  ||||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 |¡ |  ||||d ¡oj|  ||||d ¡S )Nrf   )r   r   rz   r{   r!   )r*   rb   rc   r/   r_   r`   ra   r€   r   r   r   rV   2  s    z TableauProver._attempt_proof_iffc             C   st   |  ¡ }| |jj|¡ | |jj |¡ | |jj |¡ | |jj|¡ |  ||||d ¡or|  ||||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¡ | ¡  |  ||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   J  s
    
zTableauProver._attempt_proof_eqc             C   sH   t tƒ ƒ}| |j |j|¡|¡ | ¡  |  ||t|gƒB ||d ¡S )Nrf   )	r   r   r   rh   Úreplacer~   rk   r!   r"   )r*   rb   rc   r/   r_   r`   ra   Únew_unique_variabler   r   r   r\   W  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'é   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^   a  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)	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 | |||¡ 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ƒ }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	   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   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>)
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 ]}|   |¡ 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)r4   r9   r|   r5   )r*   r`   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)	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|¡ |d k	r| |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|  |¡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)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 |¡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)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Ž   :  s
    zDebug.__init__c             C   s   t | j| jd | jƒS )Nrf   )r    r-   r§   r)   )r*   Ú	incrementr   r   r   Ú__add__B  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   Úver   r   r   r   P  s    zDebug.line.<locals>.<genexpr>z:   %sz:   []z%s%sz   )rg   r’   r   r(   rˆ   r‰   r§   r)   Úappendr-   r'   )r*   Údatar§   r¡   rv   Z	used_varsÚnewliner   r   r   r3   E  s     

z
Debug.line)r   N)r   )r   r   r   rŽ   r©   r3   r   r   r   r   r    9  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   ÚtestTableauProveru  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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   rp   rr   r   r   rŒ   Úobjectr   r    r4   rÉ   rÊ   rÄ   rÌ   r   r   r   r   r   Ú<module>   s&   H  u $4

