JL iHLdZddlmZddlmZddlmZmZddlm Z m Z ddl m Z m Z mZmZmZmZmZmZmZmZmZmZmZmZGddeZd ZGd d eZGd d eZGddeZ GddeZ!GddZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)e*dk(re)yy)z A module to perform nonmonotonic reasoning. The ideas and demonstrations in this module are based on "Logical Foundations of Artificial Intelligence" by Michael R. Genesereth and Nils J. Nilsson. ) defaultdict)reduce)ProverProverCommandDecorator)Prover9Prover9Command)AbstractVariableExpression AllExpression AndExpressionApplicationExpressionBooleanExpressionEqualityExpressionExistsExpression Expression ImpExpressionNegatedExpressionVariableVariableExpressionoperatorunique_variablec eZdZy)ProverParseErrorN)__name__ __module__ __qualname__a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/inference/nonmonotonic.pyrr&srrcn||}n|| gz}ttjd|DtS)Nc3<K|]}|jywN) constants).0as r zget_domain../s H1 Hs)rror_set)goal assumptionsall_expressionss r get_domainr+*s5 |%%$/ (,, H H#% PPrc"eZdZdZdZdZdZy)ClosedDomainProverz] This is a prover decorator that adds domain closure assumptions before proving. c|jjDcgc]}|}}|jj}t||}|Dcgc]}|j ||c}Scc}wcc}wr!)_commandr)r(r+replace_quants)selfr$r)r(domainexs rr)zClosedDomainProver.assumptions8si"&--";";"=>Qq> >}}!!#D+.:EFB##B/FF?Gs A.A3c|jj}t||jj}|j ||Sr!)r/r(r+r)r0)r1r(r2s rr(zClosedDomainProver.goal>s@}}!!#D$--";";"=>""400rc t|trh|Dcgc]1}|jj|jt |3}}|Dcgc]}|j ||}}td|St|trF|j|j |j||j |j|St|tr|j |j| St|trh|Dcgc]1}|jj|jt |3}}|Dcgc]}|j ||}}td|S|Scc}wcc}wcc}wcc}w)a Apply the closed domain assumption to the expression - Domain = union([e.free()|e.constants() for e in all_expressions]) - translate "exists x.P" to "(z=d1 | z=d2 | ... ) & P.replace(x,z)" OR "P.replace(x, d1) | P.replace(x, d2) | ..." - translate "all x.P" to "P.replace(x, d1) & P.replace(x, d2) & ..." :param ex: ``Expression`` :param domain: set of {Variable}s :return: ``Expression`` c ||zSr!rxys rz3ClosedDomainProver.replace_quants..U q1urc ||zSr!rr7s rr:z3ClosedDomainProver.replace_quants..br;r) isinstancer termreplacevariablerr0rr __class__firstsecondrr)r1r3r2d conjunctsc disjunctss rr0z!ClosedDomainProver.replace_quantsCsi b- (MSHI -?-BCIBKKA,,Q7KIK,i8 8 - .<<##BHHf5##BIIv6 - .''88 8 , -MSHI -?-BCIBKKA,,Q7KIK,i8 8I'LLs6E6E;6F FN)rrr__doc__r)r(r0rrrr-r-2s G 1 !rr-ceZdZdZdZy)UniqueNamesProverz[ This is a prover decorator that adds unique names assumptions before proving. cp|jj}tt|jj |}t }|D]S}t |ts|jj}|jj}||j|Ug}t|D]y\}}||dzdD]i} | ||vs tt|t| } tj| |r||j| X|j!| k{||zS)z - Domain = union([e.free()|e.constants() for e in all_expressions]) - if "d1 = d2" cannot be proven from the premises, then add "d1 != d2" N)r/r)listr+r( SetHolderr=rrBr@rCadd enumeraterrproveappend) r1r)r2eq_setsr$avbvnew_assumptionsibnewEqExs rr)zUniqueNamesProver.assumptionsms& mm//1 j!3!3!5{CD+ $A!/0WW%%XX&& #  $f% 9DAqAEG_ 9GAJ&0*1-/A!/DGyw <  q)(..x8 9 9_,,rN)rrrrHr)rrrrJrJgs  "-rrJceZdZdZdZy)rNz& A list of sets of Variables. cpt|tsJ|D] }||vs|cS|h}|j||S)zV :param item: ``Variable`` :return: the set containing 'item' )r=rrR)r1itemsnews r __getitem__zSetHolder.__getitem__sI $))) Aqy f C rN)rrrrHr_rrrrNrNs  rrNc.eZdZdZdZdZdZdZdZy)ClosedWorldProvera This is a prover decorator that completes predicates before proving. If the assumptions contain "P(A)", then "all x.(P(x) -> (x=A))" is the completion of "P". If the assumptions contain "all x.(ostrich(x) -> bird(x))", then "all x.(bird(x) -> ostrich(x))" is the completion of "bird". If the assumptions don't contain anything that are "P", then "all x.-P(x)" is the completion of "P". walk(Socrates) Socrates != Bill + all x.(walk(x) -> (x=Socrates)) ---------------- -walk(Bill) see(Socrates, John) see(John, Mary) Socrates != John John != Mary + all x.all y.(see(x,y) -> ((x=Socrates & y=John) | (x=John & y=Mary))) ---------------- -see(Socrates, Mary) all x.(ostrich(x) -> bird(x)) bird(Tweety) -ostrich(Sam) Sam != Tweety + all x.(bird(x) -> (ostrich(x) | x=Tweety)) + all x.-ostrich(x) ------------------- -bird(Sam) c (|jj}|j|}g}|D]V}||}|j|}|Dcgc] }t |}}g} |j D]O} g} t || D] \} } | jt| | "| jtd| Q|jD]C}i}t ||dD] \} } | || < | j|dj|E| r,|j||}td| }t||}nt|j||}|dddD]}t||}|j|Y||zScc}w)Nc ||zSr!rr7s rr:z/ClosedWorldProver.assumptions..s QUrrrLc ||zSr!rr7s rr:z/ClosedWorldProver.assumptions..s Qr)r/r)_make_predicate_dict_make_unique_signaturer signaturesziprRrr propertiessubstitute_bindings_make_antecedentrrr )r1r) predicatesrVp predHoldernew_sigv new_sig_exsrGsig equality_exsv1v2propbindings antecedent consequentaccum new_sig_vars rr)zClosedWorldProver.assumptionssmm//1 ..{; # *A#AJ11*=G:ABQ-a0BKBI",, K! !+s3DFB ''(:2r(BCD  (:L!IJ  K#-- H!+tAw7&FB#%HRL&  a! #$6 B %j*=*$*?*?7*KL 'tt} : %k59 :  " "5 )G# *J_,,ECsFcLtdt|jDS)z This method figures out how many arguments the predicate takes and returns a tuple containing that number of unique variables. c30K|]}tywr!)r)r#rWs rr%z;ClosedWorldProver._make_unique_signature..sP1_&Ps)tuplerange signature_len)r1ros rrgz(ClosedWorldProver._make_unique_signatures Pj6N6N0OPPPrc:|}|D]}|t|}|S)z Return an application expression with 'predicate' as the predicate and 'signature' as the list of arguments. )r)r1 predicate signatureryrqs rrlz"ClosedWorldProver._make_antecedents.   ;A#$6q$9:J ;rcVtt}|D]}|j|||S)z Create a dictionary of predicates from the assumptions. :param assumptions: a list of ``Expression``s :return: dict mapping ``AbstractVariableExpression`` to ``PredHolder`` )r PredHolder_map_predicates)r1r)rmr$s rrfz&ClosedWorldProver._make_predicate_dicts3!,  0A  J / 0rcnt|trB|j\}}t|tr||j t |yyt|t r9|j|j||j|j|yt|tr|jg}|j}t|tr8|j|j|j}t|tr8t|tr t|jtrt|jtr|jj\}}|jj\} } t|trt| trz||D cgc]} | jc} k(r\|| D cgc]} | jc} k(r>|| jt ||jf||j!|yyyyyyyyycc} wcc} wr!)r=r uncurryr append_sigrr rrBrCr r@r>rRr append_propvalidate_sig_len) r1 expressionpredDictfuncargsrsr>func1args1func2args2rqs rrz!ClosedWorldProver._map_predicatess j"7 8#++-JD$$ :;))%+6<  M 2  !1!18 <  !2!2H =  M 2&&'C??DT=1 4==)yyT=1$ .djj*?@ZKK!6F$(::#5#5#7LE5#';;#6#6#8LE5"5*DE&u.HI#>1AJJ#>>#>1AJJ#>> 33U3Z4LM 88=??JF F@/3 $?#>s 2H-H2N) rrrrHr)rgrlrfrrrrraras">+-ZQ >rrac4eZdZdZdZdZdZdZdZdZ y) ra This class will be used by a dictionary that will store information about predicates to be used by the ``ClosedWorldProver``. The 'signatures' property is a list of tuples defining signatures for which the predicate is true. For instance, 'see(john, mary)' would be result in the signature '(john,mary)' for 'see'. The second element of the pair is a list of pairs such that the first element of the pair is a tuple of variables and the second element is an expression of those variables that makes the predicate true. For instance, 'all x.all y.(see(x,y) -> know(x,y))' would result in "((x,y),('see(x,y)'))" for 'know'. c.g|_g|_d|_yr!rhrjrr1s r__init__zPredHolder.__init__?s!rc\|j||jj|yr!)rrhrRr1rps rrzPredHolder.append_sigDs" g& w'rcb|j|d|jj|y)Nr)rrjrR)r1new_props rrzPredHolder.append_propHs& hqk* x(rc|jt||_y|jt|k7r tdy)NzSignature lengths do not match)rlen Exceptionrs rrzPredHolder.validate_sig_lenLs=    %!$WD    3w< /<= =0rcVd|jd|jd|jdS)N(,)rrs r__str__zPredHolder.__str__Rs.4??#1T__$5Qt7I7I6J!LLrc d|zS)Nz%srrs r__repr__zPredHolder.__repr__Us d{rN) rrrrHrrrrrrrrrrr/s& " ()> Mrrctj}|d}|d}|d}t|||g}t|j t |}td|j D]}td|td|jt|j |d}|d}|d}|d}t||||g}t|j t |}td|j D]}td|td|jt|j |d}|d}|d}|d}t||||g}t|j t |}td|j D]}td|td|jt|j |d}|d}|d }t|||g}t|j t |}td|j D]}td|td|jt|j |d }|d }|d }|d }|d} |d}t|||||| g}t|j t |}td|j D]}td|td|jt|j y)Nzexists x.walk(x) man(Socrates)walk(Socrates) assumptions: goal: -walk(Bill)z walk(Bill)z all x.walk(x)z girl(mary)z dog(rover)zall x.(girl(x) -> -dog(x))zall x.(dog(x) -> -girl(x))zchase(mary, rover)z1exists y.(dog(y) & all x.(girl(x) -> chase(x,y))))r fromstringrprintrQr-r)r() lexprp1p2rFprovercdpr$p3p4p5s rclosed_domain_demorYs  ! !E " #B  B  A ABx (F &,,. V $C . __  eQ '388: #))+ " #B  B ~ B  A AB| ,F &,,. V $C . __  eQ '388: #))+ " #B  B ~ B  A AB| ,F &,,. V $C . __  eQ '388: #))+  !B } B A ABx (F &,,. V $C . __  eQ '388: #))+ } B } B , -B , -B $ %B BCA ABB3 4F &,,. V $C . __  eQ '388: #))+rctj}|d}|d}|d}t|||g}t|j t |}td|j D]}td|td|jt|j |d}|d}|d }|d }t||||g}t|j t |}td|j D]}td|td|jt|j y) Nrz man(Bill)zexists x.exists y.(x != y)rrrz!all x.(walk(x) -> (x = Socrates))zBill = Williamz Bill = Billyz-walk(William))rrrrrQrJr)r()rrrrFrunpr$rs runique_names_demors4  ! !E  B | B +,A ABx (F &,,. F #C . __  eQ '388: #))+ 3 4B  !B  B  A AB| ,F &,,. F #C . __  eQ '388: #))+rcbtj}|d}|d}|d}t|||g}t|j t |}td|j D]}td|td|jt|j |d}|d}|d }|d }|d }t|||||g}t|j t |}td|j D]}td|td|jt|j |d }|d }|d}|d}|d}t|||||g}t|j t |}td|j D]}td|td|jt|j y)Nrz(Socrates != Bill)rrrrsee(Socrates, John)see(John, Mary)z(Socrates != John)z(John != Mary)-see(Socrates, Mary)zall x.(ostrich(x) -> bird(x))z bird(Tweety)z -ostrich(Sam)z Sam != Tweetyz -bird(Sam))rrrrrQrar)r() rrrrFrcwpr$rrs rclosed_world_demors  ! !E  !B $ %B nA ABx (F &,,. F #C . __  eQ '388: #))+ % &B ! "B $ %B  !B %&A ABB/ 0F &,,. F #C . __  eQ '388: #))+ / 0B  B  B  B mA ABB/ 0F &,,. F #C . __  eQ '388: #))+rcNtj}|d}|d}|d}t|||g}t|j t t t|}|jD] }t|t|j y)Nrrr) rrrrrQr-rJrar))rrrrFrcommandr$s rcombination_prover_demors  ! !E % &B ! "B %&A ABx (F &,,. !23DV3L!MNG  " a '--/rcttj}g}|j|d|j|d|j|d|j|d|j|d|j|d|j|d|j|d|j|d |j|d |j|d |j|d |j|d |j|dtd|}t t |}|j D] }t|td|td|td|y)Nz'all x.(elephant(x) -> animal(x))z'all x.(bird(x) -> animal(x))z%all x.(dove(x) -> bird(x))z%all x.(ostrich(x) -> bird(x))z(all x.(flying_ostrich(x) -> ostrich(x))z)all x.((animal(x) & -Ab1(x)) -> -fly(x))z(all x.((bird(x) & -Ab2(x)) -> fly(x))z)all x.((ostrich(x) & -Ab3(x)) -> -fly(x))z#all x.(bird(x) -> Ab1(x))z#all x.(ostrich(x) -> Ab2(x))z#all x.(flying_ostrich(x) -> Ab3(x))z elephant(E)zdove(D)z ostrich(O)z-fly(E)zfly(D)z-fly(O)) rrrRrrJrar)r print_proof)rpremisesrrr$s rdefault_reasoning_demors}  ! !EH OOEDEF OOEDEF OOEBCD OOEBCD OOEEFG OO :; OO 9: OO :;  OOE@AB OOE@AB OOE@AB OOE.)* OOE*%& OOE-()D( +F 1& 9:G  " a 8$(# 8$rctj}t|||}tt |}t ||j |j yr!)rrrrJrarrQ)r(rrrrs rrr!sE  ! !E E$K 2F 1& 9:G $  0rchttttt yr!)rrrrrrrrdemor(s r__main__N)+rH collectionsr functoolsrnltk.inference.apirrnltk.inference.prover9rrnltk.sem.logicr r r r r rrrrrrrrrrrr+r-rJrMrNrarrrrrrrrrrrrrs $=:$ y Q2/2j(-.(-V(F>.F>R''TBJ:)X )%X1 zFr