JL i=dZddlZddlZddlZddlmZmZddlmZm Z m Z m Z m Z m Z mZmZmZddddd d d d d d ZGddZGddeeZGddZdZdZGddeeZGddeZGddeZGddeZdZd Zd!Zd"gfd#gfd$gfd%gfd&gfd#gfd$gfd&gfd'gfd(gfd)d*d+gfd,gfd-gfd.gfd/d0gfd1d0gfgZgd2Z d6d3Z!d4Z"e#d5k(re"yy)7zD A theorem prover that makes use of the external 'Prover9' package. N)BaseProverCommandProver) AllExpression AndExpressionEqualityExpressionExistsExpression Expression IffExpression ImpExpressionNegatedExpression OrExpressionTz(FATAL)Fz (MAX_MEGS)z (MAX_SECONDS)z (MAX_GIVEN)z (MAX_KEPT)z(ACTION)z (SIGSEGV)) receZdZdZddZy)Prover9CommandParentz A common base class used by both ``Prover9Command`` and ``MaceCommand``, which is responsible for maintaining a goal and a set of assumptions, and generating prover9-style input files from them. c|jdk(r!|jD] }t|y|jdk(r*t|jD] }t|yt d|z)z< Print the list of the current assumptions. nltkprover9z*Unrecognized value for 'output_format': %sN)lower assumptionsprintconvert_to_prover9 NameError)self output_formatas \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/inference/prover9.pyprint_assumptionsz&Prover9CommandParent.print_assumptions6s    F *%%' a   "i /'(8(8(:; a <}L N)r)__name__ __module__ __qualname____doc__r$r%r#rr/s  r%rc eZdZdZddZddZy)Prover9Commandz A ``ProverCommand`` specific to the ``Prover9`` prover. It contains the a print_assumptions() method that is used to print the list of assumptions in multiple formats. Ncz|sg}|t|ts Jt|}tj||||y)a :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) :param timeout: number of seconds before timeout; set to 0 for no timeout. :type timeout: int :param prover: a prover. If not set, one will be created. :type prover: Prover9 N) isinstanceProver9r__init__)r goalrtimeoutprovers r#r0zProver9Command.__init__Ms@K  fg. ..W%F""4{Cr%c|r.|jj|dgdjS|jS)z9 :see BaseProverCommand.decorate_proof() striplabelsr)_prover_call_prooftransrstrip)r proof_stringsimplifys r#decorate_proofzProver9Command.decorate_proofdsB <<00 Ofh  &&( (r%)NN<N)T)r&r'r(r)r0r;r*r%r#r,r,Fs D. )r%r,c<eZdZdZdZd dZdZdZd dZgdfdZ y) Prover9Parentz A common class extended by both ``Prover9`` and ``Mace ``. It contains the functionality required to convert NLTK-style expressions into Prover9-style expressions. NFc |d|_d|_yd}tjj ||dgd||dzg||_|jj t jjd|_y)NrPROVER9'https://www.cs.unm.edu/~mccune/prover9/.exe) path_to_binenv_varsurl binary_namesverboser) _binary_location _prover9_binr internals find_binaryrsplitospathsep)r binary_locationrGnames r#config_prover9zProver9Parent.config_prover9ys{  "$(D ! $D D $ : :+#="D6M2 !;!D %)$5$5$<$r>ps0 M *    -/!6r%r>ct|tr4g}|D]+} |jt|j -|S t|j S#t d|zxYw#t d|zxYw)z; Convert a ``logic.Expression`` to Prover9 format. z4input %s cannot be converted to Prover9 input syntax)r.listappend_convert_to_prover9r:r)rcresultrUs r#rrs% A  1!**,?@    &u~~'78 8  LuTU   H5P Q s(AA3A03Bct|tr1dt|jzdzt |j zSt|t r1dt|jzdzt |j zSt|trdt |j zdzSt|tr4dt |jzdzt |jzdzSt|tr4dt |jzdzt |jzdzSt|tr4dt |jzd zt |jzdzSt|tr4dt |jzd zt |jzdzSt|tr4dt |jzd zt |jzdzSt|S) zC Convert ``logic.Expression`` to Prover9 formatted string. zexists  zall z-()(z & z | z -> z <-> z = )r.rstrvariablervtermrr rfirstsecondr r r r) expressions r#rvrvs?*./ *%%& ' "*//2 3 J . *%%& ' "*//2 3 J 1 2)*//::S@@ J . !*"2"23 4 "*"3"34 5    J - !*"2"23 4 "*"3"34 5    J . !*"2"23 4 "*"3"34 5    J . !*"2"23 4 "*"3"34 5    J 2 3 !*"2"23 4 "*"3"34 5   :r%cBeZdZdZdZddZd dZdZgdfdZgdfdZ y) r/Nc||_y)N)_timeout)r r2s r#r0zProver9.__init__7s  &r%Fcd|sg}|j|j|||\}}|dk(|fS)z Use Prover9 to prove a theorem. :return: A pair whose first element is a boolean indicating if the proof was successful (i.e. returns value of 0) and whose second element is the output of the prover. )rGr) _call_prover9rW)r r1rrGr`rks r#_provezProver9._prove=sIK!//   t[ 170  a((r%c:d}|tj|||zS)z3 :see: Prover9Parent.prover9_input zclear(auto_denials). )r>rW)r r1rrUs r#rWzProver9.prover9_inputLs# %=..tT;GGGr%c||j|jd||_d}|jdkDr|d|jzz }||z }|j||j||\}}|dvrId}||vr%|j |}||dj } nd} |dvr t || t|| ||fS) a Call the ``prover9`` binary with the given input. :param input_str: A string whose contents are used as stdin. :param args: A list of command-line arguments. :return: A tuple (stdout, returncode) :see: ``config_prover9`` NrrTrzassign(max_seconds, %d). )rrz%%ERROR:)rrrr)rIr\rrrindexstripProver9LimitExceededExceptionProver9FatalException) r rmrorGupdated_input_strr`rkerrormsgprefixmsgstarterrormsgs r#rzProver9._call_prover9Ss    $ $ 1 1)W ED  ==1  !?$--!O O Y&!ZZ t00$   V #'N'!<<7!(),224\)3JII+JAAz!!r%c|j|jd||_|j||j||S)a Call the ``prooftrans`` binary with the given input. :param input_str: A string whose contents are used as stdin. :param args: A list of command-line arguments. :return: A tuple (stdout, returncode) :see: ``config_prover9`` prooftrans)_prooftrans_binr\rr)r rmrorGs r#r7zProver9._call_prooftransvs@    '#'#4#4\7#KD zz)T%9%94IIr%)r<)NNF) r&r'r(rIrr0rrWrr7r*r%r#r/r/3s6LO& )H-/!"F025 Jr%r/ceZdZdZy)Prover9ExceptioncVt|}|r|d|zz }tj||y)Nz %s)p9_return_codes Exceptionr0)r rkmessagemsgs r#r0zProver9Exception.__init__s.j)  6G# #C4%r%N)r&r'r(r0r*r%r#rrs&r%rc eZdZy)rNr&r'r(r*r%r#rrr%rc eZdZy)rNrr*r%r#rrrr%rctjd}tjd}t||g}d|_g|_|j t |j t |jy)Nz(walk(j) & sing(j))zwalk(j)r)r fromstringr,_executable_pathprover9_searchproverproof)r"grqs r# test_configrsf34Ai(Aqqc*AAAGGI !'') !'')r%cd|D]+}tj|}tt|-y)z% Test that parsing works OK. N)r rrr)exprtes r#test_convert_to_prover9rs1%  ! !! $  #$%r%c|D]~\}}tj|}|Dcgc]}tj|}}t||j}|D]}t d|zt d|d|dycc}w)z2 Try some proofs and exhibit the results. rz %sz|- z: r^N)r rr,rr) argumentsr1rrr"alistrqs r# test_provers' k  ! !$ '3>?a&&q)?? 1% 0 6 6 8 A 'A+   A3b2  ?sBz(man(x) <-> (not (not man(x))))z(not (man(x) & (not man(x))))z(man(x) | (not man(x)))z(man(x) & (not man(x)))z(man(x) -> man(x))z(man(x) <-> man(x))z(not (man(x) <-> (not man(x))))zmortal(Socrates)zall x.(man(x) -> mortal(x))z man(Socrates)zA((all x.(man(x) -> walks(x)) & man(Socrates)) -> some y.walks(y))z(all x.man(x) -> all x.man(x))zsome x.all y.sees(x,y)z#some e3.(walk(e3) & subj(e3, mary))zWsome e1.(see(e1) & subj(e1, john) & some e2.(pred(e1, e2) & walk(e2) & subj(e2, mary)))zVsome x e1.(see(e1) & subj(e1, x) & some e2.(pred(e1, e2) & walk(e2) & subj(e2, mary)))) zsome x y.sees(x,y)zsome x.(man(x) & walks(x))z\x.(man(x) & walks(x))z\x y.sees(x,y)z walks(john)z\x.big(x, \y.mouse(y))z/(walks(x) & (runs(x) & (threes(x) & fours(x))))z(walks(x) -> runs(x))zsome x.(PRO(x) & sees(John, x))z some x.(man(x) & (not walks(x)))zall x.(man(x) -> walks(x))c td|zy)N-)r)nums r#spacerrs #)r%ctdttttdtttttdtt t y)NzTesting configurationz$Testing conversion to Prover9 formatzTesting proofs)rrrr expressionsrrr*r%r#demorsK !" HM G 01 HK( G  Hyr%__main__)-)$r)rMrfrnltk.inference.apirrnltk.sem.logicrrrrr r r r r rrr,r>rrvr/rrrrrrrrrrrr&r*r%r#rs  8        .'))+<')Tk6k6\*=@OJmVOJd&y& ,  $4  %  '+$b)##2$b)#2B&+7IJH"M%r*r"- e  a e +  :    zFr%