o
    rZŽh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 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   úE/var/www/auris/lib/python3.10/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 n( tyN } z| jr;t|ƒ 	d¡r;d}n	|rBt
|ƒ n|‚W Y d }~nd }~ww |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 i 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 |r-t|jtƒr| ¡ }| ||ƒ ¡ ¡ |  ||||d ¡S | ¡  |  ||t	|j
ƒB ||dfhB |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ƒr| ¡ }| ||ƒ ¡ ¡ |  ||||d ¡S | ¡  |  ||t	|j j
ƒB ||j dfhB |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 ]I\}	}
t |
¡sS|}tdt ¡  ƒ}t|ƒD ]\}}|	|kr/|t|ƒƒn||ƒ}q!|r<||ƒ ¡ }t	||ƒ}| 
|
|¡ |  ||||d ¡  S q
tdƒ‚©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 ]K\}	}
t |
¡sV|}tdt ¡  ƒ}t|ƒD ]\}}|	|kr0|t|ƒƒn||ƒ}q"|r=||ƒ 	¡ }t
|| ƒ}| |
 |¡ |  ||||d ¡  S qtdƒ‚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   ó8   |t j  t|jj|jj ƒ|f¡ |  ||||d ¡S rƒ   )r4   r[   r   r
   ri   Úvariabler!   rn   r   r   r   rF   í   ó   
ÿz"TableauProver._attempt_proof_n_allc                 C   r„   rƒ   )r4   r]   r   r   ri   r…   r!   rn   r   r   r   rH   õ   r†   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 ¡o%|  ||||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 ¡o&|  ||||d ¡S rƒ   rˆ   rŠ   r   r   r   rR     s   ÿþz TableauProver._attempt_proof_impc                 C   sT   |  ¡ }| |jj |¡ | |jj |¡ |  ||||d ¡o)|  ||||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 ¡o5|  ||||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 ¡o9|  ||||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   sV  z|j  W n ty   tƒ |_ Y nw |ro||j  }|rRt|ƒd }| d| d¡ | j |hO  _ | |j |j|¡|¡ |t	j
  ||f¡ |  ||||d ¡S | dd¡ d|_|t	j
  ||f¡ |  ||||d ¡S ttƒ ƒ}	| d|	 d¡ | j |	hO  _ | |j |j|	¡|¡ |t	j
  ||f¡ | ¡  |  |||	hB ||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   sV   t | tƒr| j} t | tƒr| jD ]
}t |¡s dS qdS t | tƒs't | tƒr)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   )   s6    
#



+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>¯  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ƒ }|tj D ]+\}}t|j|jƒ}zdd„ |jD ƒ|_W n t	y6   tƒ |_Y nw | 
|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>³  s    z Agenda.clone.<locals>.<listcomp>c                 S   ó   h | ]}|’qS r   r   ©r›   Úusedr   r   r   Ú	<setcomp>¹  ó    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   sl   t |tƒr%t|j|jƒ}zdd„ |jD ƒ|_W n ty$   tƒ |_Y nw |}| j|  |¡  	||f¡ d S )Nc                 S   r¤   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 ]\}}|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 ]G\}}|rL|tjtjfv rD|D ]-}z|d js+| |¡ ||fW     S W q tyB   | |¡ ||f Y     S w 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 ]}|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   ó    | j tj D ]\}}d|_qd S ©NF)r    r4   r]   r‚   )r*   Úur©   r   r   r   rl   ø  ó   ÿzAgenda.mark_alls_freshc                 C   r´   rµ   )r    r4   rA   r‚   )r*   Zneqr©   r   r   r   r   ü  r·   zAgenda.mark_neqs_freshc                 C   sÄ   t |tƒr
|  |¡S t |tƒrtjS t |¡rtjS t |t	ƒr"tj
S t |tƒr*tjS t |tƒr2tjS t |tƒr:tjS t |tƒrBtjS t |tƒrJtjS t |tƒrRtjS t |tƒrZtjS td|jj ƒ‚©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 |¡rtj	S t|t
ƒr#tjS t|tƒr+tjS t|tƒr3tjS t|tƒr;tjS t|tƒrCtjS t|tƒrKtjS t|tƒrStjS t|tƒr[tjS td|jj ƒ‚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_NegatedExpressionrš   )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w d	 d
| j|  |¡}| j 	|¡ | j
rYt|ƒ d S 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  s   € 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£   ¹  s    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__rµ   )$Ú__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

ÿ