
    gd                     ,   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e      Z G d de      Z G d	 d
e      Z G d d      Z G d d      Z G d d      Zd Zd Z ddZ!d Z"e#dk(  r e"        yy)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                       e Zd Zy)ProverParseErrorN)__name__
__module____qualname__     K/var/www/openai/venv/lib/python3.12/site-packages/nltk/inference/tableau.pyr   r   %   s    r   r   c                       e Zd ZdZd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 Zed        Zy)TableauProverFNc                    |sg }d }	 t               }|r|j                  |        |j                  |       t        |      }| j	                  |t               t               |      }|dj                  j                        fS # t        $ rC}| j                  rt        |      j                  d      rd}n|rt        |       n|Y d }~dd }~ww xY w)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agendadebuggeres           r   _provezTableauProver._prove,   s    K	XF

D5!NN;'W~H((xHF 		(..122  		!!c!f&7&72' !HG		s   AB 	C9C

Cc           	      
   |j                         \  \  }}}|s|j                  d       y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.                  | j0                  t        j2                  | j4                  t        j6                  | j8                  t        j:                  | j<                  t        j>                  | j@                  t        jB                  | jD                  t        jF                  | jH                  t        jJ                  | jL                  t        jN                  | jP                  t        jR                  | jT                  t        jV                  | jX                  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/   r4   accessible_varsatomsdebugcurrentcontextcategoryproof_methods	            r   r&   zTableauProver._attempt_proofD   s0   '-'7'7'9$'H JJ~&
OOT55
OOT55
 t99
 t99	

 NND33
 d77
 OOT55
 d77
 d77
 !;!;
 NND33
 OOT55
 d77
 MM411
 NND33
  d77!
" NND33#
$ d77MM411t77NND33+
, -0 	

GW%&GWfoueTTr   c                    |df|v r|j                  dd       y|rft        |j                  t              r|j	                         }|j                   ||      j                                | j                  ||||dz         S |j                          | j                  ||t        |j                        z  ||dfhz  |dz         S NTCLOSED   F)r:   
isinstancetermr   negater#   simplifyr&   mark_alls_freshr'   argsr/   ri   rj   r4   rf   rg   rh   s          r   r=   z!TableauProver._attempt_proof_atomg   s     T?e#JJx#',,(9:!..*JJww'0023&&vuqyQQ ""$&&#gll"33'5)**		 r   c                    |j                   df|v r|j                  dd       y|rft        |j                   t              r|j	                         }|j                   ||      j                                | j                  ||||dz         S |j                          | j                  ||t        |j                   j                        z  ||j                   dfhz  |dz         S NFro   rp   T)rr   r:   rq   r   rs   r#   rt   r&   ru   r'   rv   rw   s          r   rA   z#TableauProver._attempt_proof_n_atom~   s     LL% E)JJx#',,(9:!..*JJww'0023&&vuqyQQ ""$&&#gll&7&7"88',,-..		 r   c                     |df|v r|j                  dd       y|j                          | j                  ||||dfhz  |dz         S rn   )r:   ru   r&   rw   s          r   r?   z!TableauProver._attempt_proof_prop   s\     T?e#JJx# 	 ""OUw.>-?%?
 	
r   c                     |j                   df|v r|j                  dd       y|j                          | j                  ||||j                   dfhz  |dz         S ry   )rr   r:   ru   r&   rw   s          r   rC   z#TableauProver._attempt_proof_n_prop   se     LL% E)JJx# 	 ""OUw||T.B-C%CUQY
 	
r   c                    |j                         \  }}t        |      D ]  \  }	}
t        j                  |
      r|}t	        dt
        j                         z        }t        |      D ]#  \  }}|	|k(  r |t        |            n ||      }% |r ||      j                         }t        ||      }|j                  |
|       | j                  ||||dz         c S  t        d      NzX%srp   z=If this method is called, there must be a non-atomic argument)uncurry	enumerater   is_atomr   _countergetr   rt   r   r#   r&   	Exceptionr/   ri   rj   r4   rf   rg   rh   frv   iargctxnvjas                  r   rE   z TableauProver._attempt_proof_app   s     //#4oFAs ((-ehlln45%dODAq9:a#045SVC ,!#,//1C&r3/

3$**6?E5ST9UU & WXXr   c                    |j                   j                         \  }}t        |      D ]  \  }	}
t        j	                  |
      r|}t        dt        j                         z        }t        |      D ]#  \  }}|	|k(  r |t        |            n ||      }% |r ||      j                         }t        ||       }|j                  |
 |       | j                  ||||dz         c S  t        d      r}   )rr   r~   r   r   r   r   r   r   r   rt   r   r#   r&   r   r   s                  r   rG   z"TableauProver._attempt_proof_n_app   s     ,,&&(4oFAs ((-ehlln45%dODAq9:a#045SVC ,!#,//1C&rC40

C4%**6?E5ST9UU & WXXr   c                 `   |j                   j                  |j                   j                  k(  r|j                  dd       y|t        j
                     j                  ||f       d|_        | j                  |||j                   j                  |j                   j                  hz  ||dz         S )Nro   rp   T)	rr   firstsecondr:   r;   rH   add
_exhaustedr&   rw   s          r   rI   z!TableauProver._attempt_proof_n_eq   s     <<!4!44JJx#z##Wg$67!""w||117<<3F3FGGAI	
 	
r   c                 |    |j                  |j                  j                  |       | j                  ||||dz         S Nrp   )r#   rr   r&   rw   s          r   rK   z"TableauProver._attempt_proof_d_neg   s7     	

7<<$$g.""6?E519MMr   c                     |t         j                     j                  t        |j                  j
                  |j                  j                         |f       | j                  ||||dz         S r   )r;   rb   r   r   rr   variabler&   rw   s          r   rM   z"TableauProver._attempt_proof_n_all   s`     	z  !%%gll33gll6G6G5GH'R	
 ""6?E519MMr   c                     |t         j                     j                  t        |j                  j
                  |j                  j                         |f       | j                  ||||dz         S r   )r;   rd   r   r   rr   r   r&   rw   s          r   rO   z#TableauProver._attempt_proof_n_some   s^     	z~~""7<<007<<3D3D2DEwO	
 ""6?E519MMr   c                     |j                  |j                  |       |j                  |j                  |       | j                  ||||dz         S r   )r#   r   r   r&   rw   s          r   rQ   z TableauProver._attempt_proof_and   sC     	

7=='*

7>>7+""6?E519MMr   c                     |j                  |j                  j                   |       |j                  |j                  j                   |       | j	                  ||||dz         S r   r#   rr   r   r   r&   rw   s          r   rS   z!TableauProver._attempt_proof_n_or  sU     	

GLL&&&0

GLL'''1""6?E519MMr   c                     |j                  |j                  j                  |       |j                  |j                  j                   |       | j	                  ||||dz         S r   r   rw   s          r   rU   z"TableauProver._attempt_proof_n_imp  sR     	

7<<%%w/

GLL'''1""6?E519MMr   c                     |j                         }|j                  |j                  |       |j                  |j                  |       | j	                  ||||dz         xr | j	                  ||||dz         S r   cloner#   r   r   r&   r/   ri   rj   r4   rf   rg   rh   
new_agendas           r   rW   zTableauProver._attempt_proof_or  sr     \\^


7=='*w~~w/""OUEAI
 Q!!*oueaiP	Qr   c                     |j                         }|j                  |j                   |       |j                  |j                  |       | j	                  ||||dz         xr | j	                  ||||dz         S r   r   r   s           r   rY   z TableauProver._attempt_proof_imp  st     \\^


GMM>7+w~~w/""OUEAI
 Q!!*oueaiP	Qr   c                    |j                         }|j                  |j                  j                   |       |j                  |j                  j                   |       | j                  ||||dz         xr | j                  ||||dz         S r   r   r#   rr   r   r   r&   r   s           r   r[   z"TableauProver._attempt_proof_n_and&  s     \\^


GLL&&&0+++W5""OUEAI
 Q!!*oueaiP	Qr   c                 f   |j                         }|j                  |j                  |       |j                  |j                  |       |j                  |j                   |       |j                  |j                   |       | j	                  ||||dz         xr | j	                  ||||dz         S r   r   r   s           r   r]   z TableauProver._attempt_proof_iff0  s     \\^


7=='*

7>>7+~w/0""OUEAI
 Q!!*oueaiP	Qr   c                    |j                         }|j                  |j                  j                  |       |j                  |j                  j                   |       |j                  |j                  j                   |       |j                  |j                  j                  |       | j                  ||||dz         xr | j                  ||||dz         S r   r   r   s           r   r_   z"TableauProver._attempt_proof_n_iff<  s     \\^


7<<%%w/

GLL'''1***G4w||**G4""OUEAI
 Q!!*oueaiP	Qr   c                    |j                  |       |j                  |j                  |j                         |j	                  |j                         |j                          | j                  ||t               |dz         S r   )	put_atomsreplace_allr   r   discardmark_neqs_freshr&   r'   rw   s          r   ra   zTableauProver._attempt_proof_eqH  se     	7=='..9. ""6?CE519MMr   c                     t        t                     }|j                  |j                  j	                  |j
                  |      |       |j                          | j                  |||hz  ||dz         S r   )r   r   r#   rr   replacer   ru   r&   )r/   ri   rj   r4   rf   rg   rh   new_unique_variables           r   rc   z!TableauProver._attempt_proof_someU  sn     11BC

7<<''(8(8:MNPWX ""O':&;;UEAI
 	
r   c                    	 |j                    |r||j                   z
  }|rt        |      d   }|j	                  d|z  d       |xj                   |hz  c_         |j                  |j                  j                  |j                  |      |       |t        j                     j                  ||f       | j                  ||||dz         S |j	                  dd       d|_        |t        j                     j                  ||f       | j                  ||||dz         S t        t                     }	|j	                  d|	z  d       |xj                   |	hz  c_         |j                  |j                  j                  |j                  |	      |       |t        j                     j                  ||f       |j!                          | j                  |||	hz  ||dz         S # t        $ r t               |_         Y w xY w)Nr   z--> Using '%s'   rp   z--> Variables ExhaustedT)
_used_varsAttributeErrorr'   listr:   r#   rr   r   r   r;   rd   r   r&   r   r   r   ru   )
r/   ri   rj   r4   rf   rg   rh   bv_availablevariable_to_user   s
             r   re   z TableauProver._attempt_proof_all_  s   	'
 *W-?-??L"&|"4Q"7

+o=qA""&77"

LL(()9)9?KW z~~&**GW+=>**6?E5ST9UU 

4a8%)"z~~&**GW+=>**6?E5ST9UU #5_5F"GJJ'*==qA#6"77JJ$$W%5%57JKW :>>"&&'9:""$&&+>*??PQ	 E  	'!$G	's   G# #G?>G?c                     t        | t              r| j                  } t        | t              r)| j                  D ]  }t
        j                  |      r y yt        | t              st        | t              ryy)NFT)	rq   r   rr   r	   rv   r   r   r   r   )r6   r   s     r   r   zTableauProver.is_atom  se    a*+Aa./vv$,,S1   56*;
 r   )NNF)r   r   r   r)   r7   r&   r=   rA   r?   rC   rE   rG   rI   rK   rM   rO   rQ   rS   rU   rW   rY   r[   r]   r_   ra   rc   re   staticmethodr   r   r   r   r   r   )   s    M30!UF..

Y"Y$
&NNNNNNQQQ
Q
QN
)V  r   r   c                       e Zd ZddZy)TableauProverCommandNc                 p    |t        |t              sJ t               }t        j                  | |||       y)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)rq   r   r   __init__)r/   r0   r1   provers       r   r   zTableauProverCommand.__init__  s5     fm444"_F""4{Cr   )NNN)r   r   r   r   r   r   r   r   r     s    Dr   r   c                   V    e Zd Zd Zd Zd ZddZd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zy)r"   c                 D    t        d t        d      D              | _        y )Nc              3   0   K   | ]  }t                 y wN)r'   ).0r   s     r   	<genexpr>z"Agenda.__init__.<locals>.<genexpr>  s     3A#%s      )tuplerangesets)r/   s    r   r   zAgenda.__init__  s    3r33	r   c                    t               }| j                  D cg c]  }|j                          }}t               }|t        j
                     D ]X  \  }}t        |j                  |j                        }	 |j                  D ch c]  }| c}|_	        |j                  |d f       Z ||t        j
                  <   |t        j                     D 	
ch c]  \  }	}
t        |	j                        |
f c}
}	|t        j                  <   t        |      |_        |S c c}w c c}w # t        $ r t               |_	        Y w xY wc c}
}	w r   )r"   r   copyr'   r;   rd   r   r   rr   r   r   r   rH   r   r   )r/   r   sset_list
new_allExsallEx_	new_allExusedn_eqr   s              r   r   zAgenda.clone  s   X
&*ii0iAFFHi0U
 0HE1%ennejjAI-9>9I9I'J9I9I'J	$ NNIt,- 1 $.   (
8%
8s tyy)3/8%
!
  /
% 1 (K! -'*u	$-
%
s/   D6D%	D D%!E D%%E ?E c                      | j                   |   S r   )r   )r/   indexs     r   __getitem__zAgenda.__getitem__  s    yyr   Nc                 N   t        |t              rAt        |j                  |j                        }	 |j                  D ch c]  }| c}|_        n|}| j                  | j                  |         j                  ||f       y c c}w # t
        $ r t               |_        Y Ow xY wr   )
rq   r   r   rr   r   r   r'   r   _categorize_expressionr   )r/   
expressionrj   	ex_to_addr   s        r   r#   z
Agenda.put  s    j-0%j&9&9:??KI-9C9N9N'O9N9N'O	$ #I		$--i89==y'>RS (P! -'*u	$-s#   B	  	B	B	 B	 	B$#B$c                 4    |D ]  }| j                  |        y r   )r#   )r/   expressionsr   s      r   r$   zAgenda.put_all  s    %JHHZ  &r   c                     |D ]Q  \  }}|r&| t         j                     j                  | d f       .| t         j                     j                  |d f       S y r   )r;   r@   r   r<   )r/   rg   atomnegs       r   r   zAgenda.put_atoms  sM    ID#Z&&'++dUDM:Z__%))4,7	 r   c                 `   t        | j                        D ]n  \  }}|s	|t        j                  t        j                  fv r1|D ]+  }	 |d   j
                  s|j                  |       ||fc c S - \|j                         |fc S  y# t        $ r |j                  |       ||fcY c c S w xY w)z3Pop the first expression that appears in the agendar   ))NNN)	r   r   r;   rH   rd   r   remover   pop)r/   r   r   exs       r   r9   zAgenda.pop_first  s    dii(DAq*..99+#%a5#3#3 !(*Aw $4   EEGQ<' ) $  . +HHRL$&7N+s   #BB-,B-c                     | j                   D ]G  }|D ]@  \  }}|j                  |j                  |       |%|j                  |j                  |       B I y r   )r   r   r   )r/   oldnewr   r   r   s         r   r   zAgenda.replace_all  sG    AC

3<<-?KKc2  r   c                 \    | j                   t        j                     D ]  \  }}d|_         y NF)r   r;   rd   r   )r/   ur   s      r   ru   zAgenda.mark_alls_fresh  s$    IIjnn-DAq AL .r   c                 \    | j                   t        j                     D ]  \  }}d|_         y r   )r   r;   rH   r   )r/   neqr   s      r   r   zAgenda.mark_neqs_fresh  s$    ii
0FC"CN 1r   c                    t        |t              r| j                  |      S t        |t              rt        j
                  S t        j                  |      rt        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        |t"              rt        j$                  S t        |t&              rt        j(                  S t        |t*              rt        j,                  S t        |t.              rt        j0                  S t3        d|j4                  j6                  z        Nzcannot categorize %s)rq   r   _categorize_NegatedExpressionr   r;   r>   r   r   r<   r   rd   r   rP   r   rV   r   rX   r   r\   r
   r`   r   rb   r	   rD   r   	__class__r   )r/   ri   s     r   r   zAgenda._categorize_expression   s   g0155g>>!;<??"""7+??"/>>!/>>!.== />>!/>>!!34== !12$$$!67>>!"#9G<M<M<V<V#VWWr   c                 (   |j                   }t        |t              rt        j                  S t        |t
              rt        j                  S t        j                  |      rt        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        |t$              rt        j&                  S t        |t(              rt        j*                  S t        |t,              rt        j.                  S t        |t0              rt        j2                  S t5        d|j6                  j8                  z        r   )rr   rq   r   r;   rJ   r   rB   r   r   r@   r   rL   r   rZ   r   rR   r   rT   r   r^   r
   rH   r   rN   r	   rF   r   r   r   )r/   ri   negateds      r   r   z$Agenda._categorize_NegatedExpression  s   ,,g01###!;<$$$""7+$$$/###/###.??"/###/###!34??"!12&&&!67###"#9G<M<M<V<V#VWWr   r   )r   r   r   r   r   r   r#   r$   r   r9   r   ru   r   r   r   r   r   r   r"   r"     sA    4, 	T!8$"3!#X4Xr   r"   c                   "    e Zd ZddZd ZddZy)r%   Nc                 6    || _         || _        |sg }|| _        y r   )r2   indentr.   )r/   r2   r   r.   s       r   r   zDebug.__init__8  s    E
r   c                 ^    t        | j                  | j                  dz   | j                        S r   )r%   r2   r   r.   )r/   	increments     r   __add__zDebug.__add__@  s!    T\\4;;?DJJ??r   c                    t        |t              rR|\  }}|r| d| }nd|z  }t        |t              r.	 ddj                  d |j                  D              z  }|d|z  z  }dj                  d	| j                  |z   z  |      }| j                  j                  |       | j                  rt        |       y y # t
        $ r |dz  }Y fw xY w)
N, %sz[%s],c              3   N   K   | ]  }d |j                   j                  z    yw)r   N)r   name)r   ves     r   r   zDebug.line.<locals>.<genexpr>N  s       Q=R(8(8!8=s   #%z:   %sz:   []z{}{}z   )rq   r   r   r-   r   r   formatr   r.   appendr2   r,   )r/   datar   r   r   	used_varsnewlines          r   r:   z
Debug.lineC  s    dE"GBRu~by"m,% & Q2== QQ!I Hy00D --v)= >E

'"<<'N  & %H$D%s   -B9 9C
	C
)r   N)r   )r   r   r   r   r   r:   r   r   r   r%   r%   7  s    @r   r%   c                   `    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y)r;   r   rp   r                     	   
                                 N)r   r   r   r<   r>   r@   rB   rD   rF   rH   rJ   rL   rN   rP   rR   rT   rV   rX   rZ   r\   r^   r`   rb   rd   r   r   r   r;   r;   [  sk    DDFF
CEDEEH
CDE	B
CE
CE	BF
Cr   r;   c                  P   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        |       y )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ps       r   testTableauProverr  s  s*   sJ'(%&'(()&'%&()&'%&&'() 34EF 
'B	BARH	%B	BARHAAQC)AAQC?AAQC 	JAOr   c                      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       y )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   testHigherOrderTableauProverr    sq    &)J(KL03G2HIxj '	FG '*G)HI03R2ST25V4WXr   Nc                    t        j                  |       }|r#|D cg c]  }t        j                  |       c}ng }|sg }t        dj                  |      d|dt	               j                  |||             y c c}w )Nr   z |- z: )r2   )r   
fromstringr,   r-   r   prove)r  psr2   pcr  ppss         r   r  r    ss    			q	!B46R
0R:  #R
0BC	99R="mo33BW3M	O 1s   A>c                  ,    t                t                y r   )r  r  r   r   r   demor%    s     "r   __main__r   )$__doc__nltk.inference.apir   r   nltk.internalsr   nltk.sem.logicr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r%   r;   r  r  r  r%  r   r   r   r   <module>r+     s    9 "    & 9	y 	pF pfD, D"GX GXT! !H 0,hY #
 zF r   