Satisfiability and validity
Can this formula be true? Must it be? Does this follow from that? Are these two the same? All four are one question, and the library answers it with evidence: a valuation you can plug in, or a proof you can check line by line.
The four questions
A formula is satisfiable if some valuation makes it true, and valid if every valuation does. Premises entail a conclusion when every valuation that makes all the premises true makes the conclusion true. Two formulas are equivalent when they agree under every valuation. Each reduces to satisfiability:
- is valid exactly when is unsatisfiable.
- entail exactly when is unsatisfiable.
- and are equivalent exactly when is unsatisfiable.
import { satisfiable, valid, entails, equivalent, classify } from "@ratmath/logic";
satisfiable("(p | q) & ~p");
// { status: "satisfiable", model: { p: false, q: true }, … }
valid("(p -> q) -> (q -> p)");
// { status: "invalid", countermodel: { p: false, q: true }, … }
entails("p -> q, q -> r", "p -> r").status; // "valid"
equivalent("p -> q", "~q -> ~p").status; // "equivalent"
equivalent("p -> q", "q -> p");
// { status: "different", witness: { p: true, q: false }, values: [false, true], … }
classify("p | ~p").kind; // "tautology"
Evidence for "yes": a model
A model is easy to check: evaluate the formula. The solver does that before it answers, against the formulas as you wrote them, so a "satisfiable" or "invalid" answer can never rest on a slip in the clause conversion.
Evidence for "no": a refutation
To show that no valuation works, the solver returns a resolution proof. The formulas are turned into clauses; from two clauses, one with and one with , the rule infers the clause holding everything else from both:
Any valuation that satisfies both premises satisfies the conclusion. So if the empty clause , which nothing satisfies, can be reached, the starting clauses cannot all be true.
const v = valid("((p -> q) & (q -> r)) -> (p -> r)");
String(v.refutation);
// 1. p premise
// 2. ¬p ∨ q premise
// 3. ¬q ∨ r premise
// 4. ¬r premise
// 5. ¬q resolve 4, 3 on r
// 6. ¬p resolve 5, 2 on q
// 7. □ resolve 6, 1 on p
v.refutation.check(); // { ok: true, refutation: true, errors: [] }
The four premises are the clauses of the negated formula. check() runs a checker that knows only the resolution rule: it does not trust the solver that produced the proof.
How the search works
The solver is the Davis–Putnam–Logemann–Loveland procedure. Pick an atom, suppose it true, and simplify; if a clause is left with one literal, that literal is forced; if a clause is left with none, back up and suppose the atom false. Each dead end is a clause that the current guesses falsify, and when both guesses for an atom fail, resolving the two dead-end clauses on that atom gives the dead end for the level above. The refutation is exactly the record of the failed search, with the steps the conclusion does not depend on removed.
Formulas that would blow up under distribution go through the Tseitin form instead; the refutation then says which new atom stands for which subformula, in refutation.definitions.
Unresolved
Satisfiability is the original NP-complete problem, and some formulas are hard for every known method. The search counts its steps and stops at its budget (200,000 by default) with a third answer.
satisfiable(hardFormula, { budget: 1000 });
// { status: "unresolved", budget: 1000, reason: "the search used its budget of 1000 steps without an answer" }
"Unresolved" is not "no". Nothing in the library reports a verdict it cannot back.
The functions
| Call | status and evidence |
|---|---|
satisfiable(f) (a formula or a list) | "satisfiable" with model; "unsatisfiable" with refutation. |
valid(f) | "valid" with refutation (of the negation); "invalid" with countermodel. |
entails(premises, conclusion) | The same; the countermodel makes the premises true and the conclusion false. |
equivalent(a, b) | "equivalent" with refutation; "different" with witness and values. |
classify(f) | kind: "tautology", "contradiction" or "contingent", with trueUnder and falseUnder. |
satisfiableClauses(clauses) | The solver on clauses given directly. |
Every one may also answer "unresolved". Options: budget, and method ("auto", "distribute", "tseitin") for how the clauses are made.
@ratmath/logic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.