a
     ˜ëj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 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røe"ƒ  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   úU/mnt/bigstor/www/usecases/.venv/lib/python3.9/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 }z@t ƒ }|r$| | ¡ | |¡ t|ƒ}|  |tƒ tƒ |¡}W nP tyœ } z8| jrvt|ƒ 	d¡rvd}n|r„t
|ƒ n|‚W Y d }~n
d }~0 0 |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!   D   s<    
ëêzTableauProver._attempt_proofc                 C   sŽ   |df|v r|  dd¡ dS |rZt|jtƒr4| ¡ }| ||ƒ ¡ ¡ |  ||||d ¡S | ¡  |  ||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|v r| dd¡ dS |r\t|j tƒr6| ¡ }| ||ƒ ¡ ¡ |  ||||d ¡S | ¡  |  ||t	|j j
ƒB ||j dfhB |d ¡S d S ©NFrf   rg   T)ri   r3   rh   r   rj   r   rk   r!   rl   r"   rm   rn   r   r   r   r:   ~   s    üz#TableauProver._attempt_proof_n_atomc                 C   sB   |df|v r|  dd¡ dS | ¡  |  ||||dfhB |d ¡S re   )r3   rl   r!   rn   r   r   r   r8   •   s    ÿz!TableauProver._attempt_proof_propc                 C   sF   |j df|v r| dd¡ dS | ¡  |  ||||j dfhB |d ¡S ro   )ri   r3   rl   r!   rn   r   r   r   r<   £   s    ÿz#TableauProver._attempt_proof_n_propc                 C   s´   |  ¡ \}}t|ƒD ]’\}	}
t |
¡s|}tdt ¡  ƒ}t|ƒD ]$\}}|	|kr^|t|ƒƒn||ƒ}qB|rx||ƒ ¡ }t	||ƒ}| 
|
|¡ |  ||||d ¡  S qtdƒ‚d S ©NzX%srg   z=If this method is called, there must be a non-atomic argument)ÚuncurryÚ	enumerater   Úis_atomr   Ú_counterÚgetr   rk   r   r   r!   Ú	Exception©r*   rb   rc   r/   r_   r`   ra   Úfrm   ÚiÚargÚctxÚnvÚjÚar   r   r   r>   ±   s    

z TableauProver._attempt_proof_appc                 C   sº   |j  ¡ \}}t|ƒD ]–\}	}
t |
¡s|}tdt ¡  ƒ}t|ƒD ]$\}}|	|kr`|t|ƒƒn||ƒ}qD|rz||ƒ 	¡ }t
|| ƒ}| |
 |¡ |  ||||d ¡  S qtdƒ‚d S rp   )ri   rq   rr   r   rs   r   rt   ru   r   rk   r   r   r!   rv   rw   r   r   r   r@   Â   s    
z"TableauProver._attempt_proof_n_appc                 C   s^   |j j|j jkr | dd¡ dS |tj  ||f¡ d|_|  |||j j|j jhB ||d ¡S )Nrf   rg   T)	ri   ÚfirstÚsecondr3   r4   rA   ÚaddÚ
_exhaustedr!   rn   r   r   r   rB   Ô   s    üz!TableauProver._attempt_proof_n_eqc                 C   s$   |  |jj|¡ |  ||||d ¡S ©Nrg   )r   ri   r!   rn   r   r   r   rD   ç   s    z"TableauProver._attempt_proof_d_negc                 C   s8   |t j  t|jj|jj ƒ|f¡ |  ||||d ¡S rƒ   )r4   r[   r�   r
   ri   Úvariabler!   rn   r   r   r   rF   í   s    
ÿz"TableauProver._attempt_proof_n_allc                 C   s8   |t j  t|jj|jj ƒ|f¡ |  ||||d ¡S rƒ   )r4   r]   r�   r   ri   r„   r!   rn   r   r   r   rH   õ   s    
ÿz#TableauProver._attempt_proof_n_somec                 C   s0   |  |j|¡ |  |j|¡ |  ||||d ¡S rƒ   )r   r   r€   r!   rn   r   r   r   rJ   ý   s    z TableauProver._attempt_proof_andc                 C   s8   |  |jj |¡ |  |jj |¡ |  ||||d ¡S rƒ   ©r   ri   r   r€   r!   rn   r   r   r   rL     s    z!TableauProver._attempt_proof_n_orc                 C   s6   |  |jj|¡ |  |jj |¡ |  ||||d ¡S rƒ   r…   rn   r   r   r   rN     s    z"TableauProver._attempt_proof_n_impc                 C   sL   |  ¡ }| |j|¡ | |j|¡ |  ||||d ¡oJ|  ||||d ¡S rƒ   ©Úcloner   r   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 rƒ   r†   rˆ   r   r   r   rR     s    ÿþz TableauProver._attempt_proof_impc                 C   sT   |  ¡ }| |jj |¡ | |jj |¡ |  ||||d ¡oR|  ||||d ¡S rƒ   ©r‡   r   ri   r   r€   r!   rˆ   r   r   r   rT   &  s    ÿþz"TableauProver._attempt_proof_n_andc                 C   sl   |  ¡ }| |j|¡ | |j|¡ | |j |¡ | |j |¡ |  ||||d ¡oj|  ||||d ¡S rƒ   r†   rˆ   r   r   r   rV   0  s    ÿþz TableauProver._attempt_proof_iffc                 C   st   |  ¡ }| |jj|¡ | |jj |¡ | |jj |¡ | |jj|¡ |  ||||d ¡or|  ||||d ¡S rƒ   rŠ   rˆ   r   r   r   rX   <  s    ÿþz"TableauProver._attempt_proof_n_iffc                 C   sD   |  |¡ | |j|j¡ | |j¡ | ¡  |  ||tƒ |d ¡S rƒ   )Ú	put_atomsÚreplace_allr   r€   ÚdiscardÚmark_neqs_freshr!   r"   rn   r   r   r   rZ   H  s
    
zTableauProver._attempt_proof_eqc                 C   sD   t tƒ ƒ}| |j |j|¡|¡ | ¡  |  |||hB ||d ¡S rƒ   )r   r   r   ri   Úreplacer„   rl   r!   )r*   rb   rc   r/   r_   r`   ra   Únew_unique_variabler   r   r   r\   U  s    
ÿz!TableauProver._attempt_proof_somec           
      C   s\  z
|j  W n ty$   tƒ |_ Y n0 |rà||j  }|r¤t|ƒd }| d| d¡ | j |hO  _ | |j |j|¡|¡ |t	j
  ||f¡ |  ||||d ¡S | dd¡ d|_|t	j
  ||f¡ |  ||||d ¡S nxttƒ ƒ}	| d|	 d¡ | j |	hO  _ | |j |j|	¡|¡ |t	j
  ||f¡ | ¡  |  |||	hB ||d ¡S d S )Nr   z--> Using '%s'é   rg   z--> Variables ExhaustedT)Ú
_used_varsÚAttributeErrorr"   Úlistr3   r   ri   r�   r„   r4   r]   r�   r!   r‚   r   r   rl   )
r*   rb   rc   r/   r_   r`   ra   Zbv_availableZvariable_to_user�   r   r   r   r^   _  s<    

ÿ
ÿÿz TableauProver._attempt_proof_allc                 C   sZ   t | tƒr| j} t | tƒr:| jD ]}t |¡s  dS q dS t | tƒsNt | tƒrRdS dS d S )NFT)	rh   r   ri   r   rm   r   rs   r   r   )r0   rz   r   r   r   rs   Š  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^   Ústaticmethodrs   r   r   r   r   r   )   s4   
#



+r   c                   @   s   e Zd Zddd„ZdS )ÚTableauProverCommandNc                 C   s2   |durt |tƒsJ ‚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)rh   r   r   Ú__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"   )Ú.0ry   r   r   r   Ú	<genexpr>¯  ó    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ƒ }|tj D ]V\}}t|j|jƒ}zdd„ |jD ƒ|_W n t	yl   tƒ |_Y n0 | 
|d f¡ q&||tj< dd„ |tj D ƒ|tj< t|ƒ|_|S )Nc                 S   s   g | ]}|  ¡ ‘qS r   )Úcopy)r™   Úsr   r   r   Ú
<listcomp>³  r›   z Agenda.clone.<locals>.<listcomp>c                 S   s   h | ]}|’qS r   r   ©r™   Úusedr   r   r   Ú	<setcomp>¹  r›   zAgenda.clone.<locals>.<setcomp>c                 S   s   h | ]\}}t |jƒ|f’qS r   )r   ri   )r™   Zn_eqr{   r   r   r   r¥   ¿  s   ÿ)r   rŸ   r"   r4   r]   r   r„   ri   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 r˜   )rŸ   )r*   Úindexr   r   r   Ú__getitem__Ç  s    zAgenda.__getitem__Nc                 C   sn   t |tƒrLt|j|jƒ}zdd„ |jD ƒ|_W qP tyH   tƒ |_Y qP0 n|}| j|  |¡  	||f¡ d S )Nc                 S   s   h | ]}|’qS r   r   r£   r   r   r   r¥   Î  r›   zAgenda.put.<locals>.<setcomp>)
rh   r   r„   ri   r’   r“   r"   rŸ   Ú_categorize_expressionr�   )r*   Ú
expressionrc   Z	ex_to_addr   r   r   r   Ê  s    
z
Agenda.putc                 C   s   |D ]}|   |¡ qd S r˜   )r   )r*   Zexpressionsrª   r   r   r   r   Õ  s    zAgenda.put_allc                 C   sB   |D ]8\}}|r(| t j  | d f¡ q| t j  |d f¡ qd S r˜   )r4   r9   r�   r5   )r*   r`   ZatomÚnegr   r   r   r‹   Ù  s    zAgenda.put_atomsc              
   C   s    t | jƒD ]�\}}|r
|tjtjfv rŠ|D ]\}z*|d jsV| |¡ ||fW     S W q* ty„   | |¡ ||f Y     S 0 q*q
| ¡ |f  S q
dS )z3Pop the first expression that appears in the agendar   ))NNN)	rr   rŸ   r4   rA   r]   r‚   Úremover“   Úpop)r*   ry   r¡   Úexr   r   r   r2   à  s    


zAgenda.pop_firstc                 C   sB   | j D ]6}|D ],\}}| |j|¡ |d ur| |j|¡ qqd S r˜   )rŸ   r�   r„   )r*   ÚoldÚnewr¡   r®   r{   r   r   r   rŒ   ñ  s
    
zAgenda.replace_allc                 C   s    | j tj D ]\}}d|_qd S ©NF)rŸ   r4   r]   r‚   )r*   Úur¦   r   r   r   rl   ø  s    zAgenda.mark_alls_freshc                 C   s    | j tj D ]\}}d|_qd S r±   )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)rh   r   Ú_categorize_NegatedExpressionr   r4   r7   r   rs   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 r³   )ri   rh   r   r4   rC   r   r;   r   rs   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Œ   rl   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 r˜   )r-   Úindentr)   )r*   r-   r¶   r)   r   r   r   r—   8  s
    zDebug.__init__c                 C   s   t | j| jd | jƒS rƒ   )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ƒr~z*dd dd„ |jD ƒ¡ }|d| 7 }W n ty|   |d7 }Y n0 d	 d
| j|  |¡}| j 	|¡ | j
r®t|ƒ d S )Nú, ú%sz[%s]ú,c                 s   s   | ]}d |j j V  qdS )rº   N)r„   Úname)r™   Úver   r   r   rš   N  r›   zDebug.line.<locals>.<genexpr>z:   %sz:   []z{}{}z   )rh   r�   r   r(   r’   r“   Úformatr¶   r)   Úappendr-   r'   )r*   Údatar¶   r®   r{   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   rg   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  | ¡}|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¢   ¹  r›   z tableau_test.<locals>.<listcomp>z%s |- %s: %sr¹   )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 r˜   )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   rt   rv   r   r   r–   r   r    r4   rÜ   rÝ   r×   rß   r   r   r   r   r   Ú<module>	   s&   H  u $4
