JL id,dZddlmZmZddlmZddlmZmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZmZeZGddeZGddeZGd d eZGd d ZGd dZGddZdZdZ ddZ!dZ"e#dk(re"yy)z8 Module for a tableau-based First Order theorem prover. )BaseProverCommandProver)Counter)AbstractVariableExpression AllExpression AndExpressionApplicationExpressionEqualityExpressionExistsExpression ExpressionFunctionVariableExpression IffExpression ImpExpressionLambdaExpressionNegatedExpression OrExpressionVariableVariableExpressionunique_variablec eZdZy)ProverParseErrorN)__name__ __module__ __qualname__\/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/inference/tableau.pyrr%srrceZdZdZddZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZdZdZdZdZdZdZdZdZedZy) TableauProverFNc|sg}d} t}|r|j| |j|t|}|j |t t |}|djjfS#t $rC}|jrt|jdrd}n|r t|n|Yd}~dd}~wwxYw)Nz maximum recursion depth exceededF ) Agendaputput_allDebug_attempt_proofset RuntimeError _assume_falsestr startswithprintjoinlines)selfgoal assumptionsverboseresultagendadebuggeres r_provezTableauProver._prove,sK XF D5! NN; 'W~H((xHF (..122 !!c!f&7&72'!HG sAB C 9C  Cc  |j\\}}}|s|jdyitj|jtj |j tj|jtj|jtj|jtj|jtj|j tj"|j$tj&|j(tj*|j,tj.|j0tj2|j4tj6|j8tj:|j<tj>|j@tjB|jDtjF|jHtjJ|jLtjN|jPtjR|jTtjV|jXi|}|j||f|||||||S)Nz AGENDA EMPTYF)- pop_firstline CategoriesATOM_attempt_proof_atomPROP_attempt_proof_propN_ATOM_attempt_proof_n_atomN_PROP_attempt_proof_n_propAPP_attempt_proof_appN_APP_attempt_proof_n_appN_EQ_attempt_proof_n_eqD_NEG_attempt_proof_d_negN_ALL_attempt_proof_n_allN_EXISTS_attempt_proof_n_someAND_attempt_proof_andN_OR_attempt_proof_n_orN_IMP_attempt_proof_n_impOR_attempt_proof_orIMP_attempt_proof_impN_AND_attempt_proof_n_andIFF_attempt_proof_iffN_IFF_attempt_proof_n_iffEQ_attempt_proof_eqEXISTS_attempt_proof_someALL_attempt_proof_all) r/r4accessible_varsatomsdebugcurrentcontextcategory proof_methods rr&zTableauProver._attempt_proofDs0'-'7'7'9$'H JJ~ & OOT55 OOT55    t99    t99  NND33    d77   OOT55    d77    d77    !;!;  NND33  OOT55    d77  MM411  NND33   d77! " NND33# $   d77 MM411   t77 NND33+ , - 0  GW%&GWfoueTTrc|df|vr|jddy|rft|jtr|j }|j ||j |j||||dzS|j|j||t|jz||dfhz|dzSNTCLOSEDF) r: isinstancetermrnegater#simplifyr&mark_alls_freshr'argsr/rirjr4rfrgrhs rr=z!TableauProver._attempt_proof_atomgs T?e # JJx # ',,(9:!..* JJww'002 3&&vuqyQ Q  " " $&&#gll"33'5)**  rc|jdf|vr|jddy|rft|jtr|j }|j ||j |j||||dzS|j|j||t|jjz||jdfhz|dzSNFrorpT) rrr:rqrrsr#rtr&rur'rvrws rrAz#TableauProver._attempt_proof_n_atom~s LL% E ) JJx # ',,(9:!..* JJww'002 3&&vuqyQ Q  " " $&&#gll&7&7"88',,-..  rc|df|vr|jddy|j|j||||dfhz|dzSrn)r:rur&rws rr?z!TableauProver._attempt_proof_props\ T?e # JJx #  "" OUw.>-?%?  rc|jdf|vr|jddy|j|j||||jdfhz|dzSry)rrr:rur&rws rrCz#TableauProver._attempt_proof_n_propse LL% E ) JJx #  "" OUw||T.B-C%CUQY  rc|j\}}t|D]\} } tj| r|} t dt j z} t|D]#\} }| | k(r| t| n| |} %|r|| j} t| | } |j| | |j||||dzcStdNzX%srpz=If this method is called, there must be a non-atomic argument) uncurry enumerateris_atomr_countergetrrtrr#r& Exceptionr/rirjr4rfrgrhfrviargctxnvjas rrEz TableauProver._attempt_proof_apps//#4o VFAs ((-ehlln45%dOLDAq9:a#045SVCL!#,//1C&r3/ 3$**6?E5ST9UU VWXXrc|jj\}}t|D]\} } tj | r|} t dt jz} t|D]#\} }| | k(r| t| n| |} %|r|| j} t| | } |j| | |j||||dzcStdr})rrr~rrrrrrrrtrr#r&rrs rrGz"TableauProver._attempt_proof_n_apps,,&&(4o VFAs ((-ehlln45%dOLDAq9:a#045SVCL!#,//1C&rC40 C4%**6?E5ST9UU VWXXrc`|jj|jjk(r|jddy|tj j ||fd|_|j|||jj|jjhz||dzS)NrorpT) rrfirstsecondr:r;rHadd _exhaustedr&rws rrIz!TableauProver._attempt_proof_n_eqs <<  !4!4 4 JJx #z##Wg$67!""  w||117<<3F3FG G  AI   rc||j|jj||j||||dzSNrp)r#rrr&rws rrKz"TableauProver._attempt_proof_d_negs7  7<<$$g.""6?E519MMrc|tjjt|jj |jj |f|j ||||dzSr)r;rbrr rrvariabler&rws rrMz"TableauProver._attempt_proof_n_alls` z  !%% gll33gll6G6G5G H' R ""6?E519MMrc|tjjt|jj |jj |f|j ||||dzSr)r;rdrrrrrr&rws rrOz#TableauProver._attempt_proof_n_somes^ z~~"" 7<<007<<3D3D2D Ew O ""6?E519MMrc|j|j||j|j||j||||dzSr)r#rrr&rws rrQz TableauProver._attempt_proof_andsC  7=='* 7>>7+""6?E519MMrc|j|jj ||j|jj ||j ||||dzSrr#rrrrr&rws rrSz!TableauProver._attempt_proof_n_orsU  GLL&&&0 GLL'''1""6?E519MMrc|j|jj||j|jj ||j ||||dzSrrrws rrUz"TableauProver._attempt_proof_n_imp sR  7<<%%w/ GLL'''1""6?E519MMrc|j}|j|j||j|j||j ||||dzxr|j ||||dzSrcloner#rrr&r/rirjr4rfrgrh new_agendas rrWzTableauProver._attempt_proof_orsr\\^  7=='*w~~w/"" OUEAI Q!!*oueaiP Qrc|j}|j|j ||j|j||j ||||dzxr|j ||||dzSrrrs rrYz TableauProver._attempt_proof_impst\\^  GMM>7+w~~w/"" OUEAI Q!!*oueaiP Qrc|j}|j|jj ||j|jj ||j ||||dzxr|j ||||dzSrrr#rrrrr&rs rr[z"TableauProver._attempt_proof_n_and&s\\^  GLL&&&0 +++W5"" OUEAI Q!!*oueaiP Qrcf|j}|j|j||j|j||j|j ||j|j ||j ||||dzxr|j ||||dzSrrrs rr]z TableauProver._attempt_proof_iff0s\\^  7=='* 7>>7+ ~w/0"" OUEAI Q!!*oueaiP Qrc|j}|j|jj||j|jj ||j|jj ||j|jj||j ||||dzxr|j ||||dzSrrrs rr_z"TableauProver._attempt_proof_n_iff<s\\^  7<<%%w/ GLL'''1 ***G4w||**G4"" OUEAI Q!!*oueaiP Qrc|j||j|j|j|j |j|j |j ||t|dzSr) put_atoms replace_allrrdiscardmark_neqs_freshr&r'rws rrazTableauProver._attempt_proof_eqHse 7=='..9 . ""6?CE519MMrctt}|j|jj |j |||j |j|||hz||dzSr)rrr#rrreplacerrur&)r/rirjr4rfrgrhnew_unique_variables rrcz!TableauProver._attempt_proof_someUsn11BC 7<<''(8(8:MNPWX "" O':&;;UEAI  rc |j|r||jz }|rt|d}|j d|zd|xj|hzc_|j |j j|j|||tjj||f|j||||dzS|j ddd|_ |tjj||f|j||||dzStt} |j d| zd|xj| hzc_|j |j j|j| ||tjj||f|j!|j||| hz||dzS#t$rt|_YwxYw)Nrz--> Using '%s'rpz--> Variables ExhaustedT) _used_varsAttributeErrorr'listr:r#rrrrr;rdrr&rrrru) r/rirjr4rfrgrh bv_availablevariable_to_users rrez TableauProver._attempt_proof_all_s '    *W-?-??L"&|"4Q"7 +o=qA""&77" LL(()9)9?KWz~~&**GW+=>**6?E5ST9UU 4a8%)"z~~&**GW+=>**6?E5ST9UU#5_5F"G  JJ'*==q A   #6"7 7  JJ $$W%5%57JKW  :>> " & &'9 :  " " $&&+>*??PQ  E '!$G  's G##G?>G?ct|tr |j}t|tr)|jD]}t j |ryyt|tst|tryy)NFT) rqrrrr rvrrrr)r6rs rrzTableauProver.is_atomsj a* +A a. /vv !$,,S1  ! 5 6* ; r)NNF)rrrr)r7r&r=rAr?rCrErGrIrKrMrOrQrSrUrWrYr[r]r_rarcre staticmethodrrrrrr)sM30!UF..    Y"Y$ &N NNNNNQQQ Q Q N )VrrceZdZddZy)TableauProverCommandNcp|t|ts Jt}tj||||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)rqrr__init__)r/r0r1provers rrzTableauProverCommand.__init__s5  fm4 44"_F""4{Cr)NNN)rrrrrrrrrs DrrcVeZdZdZdZdZddZdZdZdZ d Z d Z d Z d Z d Zy)r"cDtdtdD|_y)Nc30K|]}tywN)r').0rs r z"Agenda.__init__..s3A#%3s)tuplerangesets)r/s rrzAgenda.__init__s3r33 rct}|jDcgc]}|j}}t}|tj D]X\}}t |j|j} |jDchc]}|c}|_ |j|dfZ||tj <|tjD chc]\} } t| j| fc} } |tj<t||_|Scc}wcc}w#t$rt|_ YwxYwcc} } wr)r"rcopyr'r;rdrrrrrrrrHrr) r/rsset_list new_allExsallEx_ new_allExusedn_eqrs rrz Agenda.clonesX &*ii0AFFH00U  0 .HE1%ennejjAI -9>9I9I'J'J $ NNIt, -  .$.  ( 8% styy )3 /% !  / %1 (K! -'*u $ - % s/D6D% D  D%!E D%%E?Ec |j|Sr)r)r/indexs r __getitem__zAgenda.__getitem__syyrNcNt|trAt|j|j} |jDchc]}|c}|_n|}|j|j|j||fycc}w#t $rt |_YOwxYwr) rqrrrrrrr'r_categorize_expressionr)r/ expressionrj ex_to_addrs rr#z Agenda.puts j- 0%j&9&9:??KI -9C9N9N'O'O $#I $--i89==y'>RS (P! -'*u $ -s#B B B B B$#B$c4|D]}|j|yr)r#)r/ expressionsrs rr$zAgenda.put_alls% !J HHZ  !rc|D]Q\}}|r&|tjj| df.|tjj|dfSyr)r;r@rr<)r/rgatomnegs rrzAgenda.put_atomssR 8ID#Z&&'++dUDM:Z__%))4,7  8rc`t|jD]n\}}|s |tjtjfvr1|D]+} |dj s|j |||fccS-\|j|fcSy#t$r|j |||fcYccSwxYw)z3Pop the first expression that appears in the agendar))NNN) rrr;rHrdrremoverpop)r/rrexs rr9zAgenda.pop_firstsdii( (DAq*..99++#%a5#3#3 ! (*Aw$4+EEGQ<' ($ .+HHRL$&7N+s#BB-,B-c|jD]G}|D]@\}}|j|j||%|j|j|BIyr)rrr)r/oldnewrrrs rrzAgenda.replace_allsQ 3A 3C 3<<-?KK c2 3 3rc\|jtjD] \}}d|_yNF)rr;rdr)r/urs rruzAgenda.mark_alls_freshs)IIjnn- !DAq AL !rc\|jtjD] \}}d|_yr)rr;rHr)r/neqrs rrzAgenda.mark_neqs_freshs)ii 0 #FC"CN #rct|tr|j|St|trtj St j|rtjSt|trtjSt|trtjSt|trtjSt|trtj St|t"rtj$St|t&rtj(St|t*rtj,St|t.rtj0St3d|j4j6zNzcannot categorize %s)rqr_categorize_NegatedExpressionr r;r>rrr<rrdrrPrrVrrXrr\r r`r rbr rDr __class__r)r/ris rrzAgenda._categorize_expressions  g0 155g> > !; <?? "  " "7 +?? "  />> !  />> !  .==  />> !  />> ! !3 4== !1 2$$ $ !6 7>> !"#9G.Ns QR (8(8!8 Qs#%z: %sz: []z{}{}z ) rqrrr-rrformatrr.appendr2r,)r/datarrr used_varsnewlines rr:z Debug.lineCs dE "GBRu~by"m,% & Q2== QQ!IHy00D--v)= >E '" << 'N  &%H$D%s-B99C  C )rN)r)rrrrrr:rrrr%r%7s@rr%c`eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZy)r;rrpr N)rrrr<r>r@rBrDrFrHrJrLrNrPrRrTrVrXrZr\r^r`rbrdrrrr;r;[sk D D F F C E D E EH C D E B C E C E B F Crr;cPtdtdtdddgtdtdtdtd td tdtd td tdtd td td tdtdd}d}d}t|||gd}d}d}t|||gd}d}t||gd}d}t||gd}d}t||gd}t|y)NzP | -PzP & -PQPz(P -> Q)zman(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))zall x.(man(x) -> mortal(x))z man(Socrates)zmortal(Socrates)zall x.(man(x) -> walks(x))z man(John)zsome y.walks(y)z((x = y) & walks(y))zwalks(x)z((x = y) & ((y = z) & (z = w)))z(x = w)z5some e1.some e2.(believe(e1,john,e2) & walk(e2,mary))zsome 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)p1p2cps rtestTableauProverrss*sJ'(%&'(()&'%&()&'%&&'() 34EF 'B BARH %B BARHAAQC)AAQC?AAQC JAOrctddgtddgtddgtddgtdd gtd dgtd dgy) Nzbelieve(j, -lie(b))zbelieve(j, -lie(b) & -cheat(b))zbelieve(j, lie(b) & cheat(b))zbelieve(j, lie(b))zlie(b)zbelieve(j, know(b, cheat(b)))z;believe(j, know(b, lie(b)) & know(b, steals(b) & cheat(b)))zP(Q(y), R(y) & R(z))zP(Q(x) & Q(y), R(y) & R(z))zbelieve(j, cheat(b) & lie(b))zbelieve(j, -cheat(b) & -lie(b))rrrrtestHigherOrderTableauProverrsq&)J(KL03G2HIxj' FG'*G)HI03R2ST25V4WXrNc tj|}|r#|Dcgc]}tj|c}ng}|sg}tdj|d|dt j |||ycc}w)Nrz |- z: )r2)r fromstringr,r-rprove)rpsr2pcrppss rrrso   q !B46R 0:  # 0BC  99R="mo33BW3M O 1sA>c,ttyr)rrrrrdemor%s "r__main__r)$__doc__nltk.inference.apirrnltk.internalsrnltk.sem.logicrrrr r r r r rrrrrrrrrrrrrr"r%r;rrrr%rrrrr+s9"& 9 y pFpf D,D"GXGXT!!H  0,h Y #  zFr