K i#dZddlmZmZmZmZmZddlmZddl m Z ddl m Z dZ ddZd Zdd Zdd ZGd d ZGddeZy)z Inference in propositional logic)AndNot conjunctsto_cnfBooleanFunction)ordered)sympify) import_modulec|dus|dur|S|jr|S|jrt|jdSt d)z The symbol in this literal (without the negation). Examples ======== >>> from sympy.abc import A >>> from sympy.logic.inference import literal_symbol >>> literal_symbol(A) A >>> literal_symbol(~A) A TFrz#Argument must be a boolean literal.) is_Symbolis_Notliteral_symbolargs ValueError)literals [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/logic/inference.pyrr sK $'U*    gll1o..>??Nc|r||dk7rtd|dd}||dk(r"td}|d}n|dk(r tdd}|dk(rtd}|d}|dk(rtd}|d}|d k(rd d lm}||S|dk(rd d lm}|||| S|dk(rd d lm} | ||S|dk(rd dlm } | |||S|dk(rd dl m } | ||St)a Check satisfiability of a propositional sentence. Returns a model when it succeeds. Returns {true: true} for trivially true expressions. On setting all_models to True, if given expr is satisfiable then returns a generator of models. However, if expr is unsatisfiable then returns a generator containing the single element False. Examples ======== >>> from sympy.abc import A, B >>> from sympy.logic.inference import satisfiable >>> satisfiable(A & ~B) {A: True, B: False} >>> satisfiable(A & ~A) False >>> satisfiable(True) {True: True} >>> next(satisfiable(A & ~A, all_models=True)) False >>> models = satisfiable((A >> B) & B, all_models=True) >>> next(models) {A: False, B: True} >>> next(models) {A: True, B: True} >>> def use_models(models): ... for model in models: ... if model: ... # Do something with the model. ... print(model) ... else: ... # Given expr is unsatisfiable. ... print("UNSAT") >>> use_models(satisfiable(A >> ~A, all_models=True)) {A: False} >>> use_models(satisfiable(A ^ A, all_models=True)) UNSAT dpll2z2Currently only dpll2 can handle using lra theory. z is not handled.pycosatzpycosat module is not present minisat22pysatz3dpllr)dpll_satisfiable)use_lra_theory)pycosat_satisfiable)minisat22_satisfiable)z3_satisfiable) rr ImportErrorsympy.logic.algorithms.dpllrsympy.logic.algorithms.dpll2&sympy.logic.algorithms.pycosat_wrapperr(sympy.logic.algorithms.minisat22_wrapperr!sympy.logic.algorithms.z3_wrapperrNotImplementedError) expr algorithm all_modelsminimalrrrrrrrrs r satisfiabler+#s&T  Y'%9QR[Q\\lmn n I2 *  !II%!"ABB I+g& =I$ 4  :IF@%% g AjPP i N"444 k !R$T:w?? d DdJ// rc,tt| S)ax Check validity of a propositional sentence. A valid propositional sentence is True under every assignment. Examples ======== >>> from sympy.abc import A, B >>> from sympy.logic.inference import valid >>> valid(A | ~A) True >>> valid(A | B) False References ========== .. [1] https://en.wikipedia.org/wiki/Validity )r+r)r's rvalidr-zs*3t9% %%rcddlmdfd|vr|St|}|std|z|si}|j Dcic] \}}|vs ||}}}|j |}|vr t |S|rItj|jd}t||r t|ryyt|syycc}}w) a+ Returns whether the given assignment is a model or not. If the assignment does not specify the value for every proposition, this may return None to indicate 'not obvious'. Parameters ========== model : dict, optional, default: {} Mapping of symbols to boolean values to indicate assignment. deep: boolean, optional, default: False Gives the value of the expression under partial assignments correctly. May still return None to indicate 'not obvious'. Examples ======== >>> from sympy.abc import A, B >>> from sympy.logic.inference import pl_true >>> pl_true( A & B, {A: True, B: True}) True >>> pl_true(A & B, {A: False}) False >>> pl_true(A & B, {A: True}) >>> pl_true(A & B, {A: True}, deep=True) >>> pl_true(A >> (B >> A)) >>> pl_true(A >> (B >> A), deep=True) True >>> pl_true(A & ~A) >>> pl_true(A & ~A, deep=True) False >>> pl_true(A & B & (~A | ~B), {A: True}) >>> pl_true(A & B & (~A | ~B), {A: True}, deep=True) False r)Symbol)TFct|s|vryt|tsytfd|jDS)NTFc3.K|] }|ywN).0arg _validates r z-pl_true.._validate..s7c9S>7s) isinstancerallr)r'r/r6booleans rr6zpl_true.._validates8 dF #tw$07TYY777rz$%s is not a valid boolean expressionTFN) sympy.core.symbolr/r ritemssubsbooldictfromkeysatomspl_truer-r+) r'modeldeepkvresultr/r6r:s @@@rrBrBsP)G8 w 4=D T??$FGG #kkm >> from sympy.abc import A, B, C >>> from sympy.logic.inference import entails >>> entails(A, [A >> B, B >> C]) False >>> entails(C, [A >> B, B >> C, A]) True >>> entails(A >> B) False >>> entails(A >> (B >> A)) True References ========== .. [1] https://en.wikipedia.org/wiki/Logical_consequence )listappendrr+r)r' formula_sets rentailsrLs;2;'  s4y!3 ,- --rc:eZdZdZddZdZdZdZedZ y) KBz"Base class for all knowledge basesNcJt|_|r|j|yyr2)setclauses_tellselfsentences r__init__z KB.__init__s  IIh  rctr2r&rSs rrRzKB.tell!!rctr2rXrTquerys raskzKB.askrYrctr2rXrSs rretractz KB.retract rYrc>tt|jSr2)rIrrQ)rTs rclausesz KB.clauses sGDMM*++rr2) __name__ __module__ __qualname____doc__rVrRr]r_propertyrar3rrrNrNs-, """,,rrNc"eZdZdZdZdZdZy)PropKBz=A KB for Propositional Logic. Inefficient, with no indexing.cltt|D]}|jj|y)aiAdd the sentence's clauses to the KB Examples ======== >>> from sympy.logic.inference import PropKB >>> from sympy.abc import x, y >>> l = PropKB() >>> l.clauses [] >>> l.tell(x | y) >>> l.clauses [x | y] >>> l.tell(y) >>> l.clauses [y, x | y] N)rrrQaddrTrUcs rrRz PropKB.tells/*6(+, !A MM  a  !rc.t||jS)a8Checks if the query is true given the set of clauses. Examples ======== >>> from sympy.logic.inference import PropKB >>> from sympy.abc import x, y >>> l = PropKB() >>> l.tell(x & ~y) >>> l.ask(x) True >>> l.ask(y) False )rLrQr[s rr]z PropKB.ask,s udmm,,rcltt|D]}|jj|y)amRemove the sentence's clauses from the KB Examples ======== >>> from sympy.logic.inference import PropKB >>> from sympy.abc import x, y >>> l = PropKB() >>> l.clauses [] >>> l.tell(x | y) >>> l.clauses [x | y] >>> l.retract(x | y) >>> l.clauses [] N)rrrQdiscardrks rr_zPropKB.retract>s/*6(+, %A MM ! !! $ %rN)rbrcrdrerRr]r_r3rrrhrhsG!0-$%rrh)NFFF)NFr2)resympy.logic.boolalgrrrrrsympy.core.sortingrsympy.core.sympifyr sympy.external.importtoolsr rr+r-rBrLrNrhr3rrrtsN&LL&&4@4Tn&0FR.B,,*C%RC%r