Normal forms
Every formula can be rewritten into a few standard shapes. Negation normal form pushes down to the atoms; conjunctive and disjunctive forms flatten what is left into two levels. Solvers, circuits and resolution proofs all start from one of these.
Negation normal form
Write , and in terms of , , ; then move each inward with De Morgan's laws, and , cancelling double negations. What remains has only on atoms.
import { nnf, cnf, dnf, clauses, tseitin, isCnf } from "@ratmath/logic";
String(nnf("~(p & (q | ~r))")); // "¬p ∨ (¬q ∧ r)"
String(nnf("~(p -> q)")); // "p ∧ ¬q"
String(nnf("p <-> q")); // "(¬p ∨ q) ∧ (p ∨ ¬q)"
Conjunctive and disjunctive forms
A literal is an atom or its negation. A formula in conjunctive normal form (CNF) is an "and" of clauses, each an "or" of literals; in disjunctive normal form (DNF) it is an "or" of terms, each an "and" of literals. From negation normal form, CNF comes by distributing over : . DNF is the mirror image.
String(cnf("(p & q) | r")); // "(p ∨ r) ∧ (q ∨ r)"
String(dnf("(p | q) & r")); // "(p ∧ r) ∨ (q ∧ r)"
String(cnf("p -> (q & r)")); // "(¬p ∨ q) ∧ (¬p ∨ r)"
String(cnf("p | ~p")); // "⊤": a clause with p and ¬p is always true and is dropped
clauses("(p -> q) & p & ~q"); // [["p"], ["¬p", "q"], ["¬q"]]: the same thing as lists
The result is tidied: a clause that contains both a literal and its negation is dropped, and so is a clause that contains another clause, since the smaller one already implies it. Neither changes the truth table.
With method: "table" the form is read off the truth table instead: one clause for each false row, or one term for each true row. This full form is unique for a given table, which makes it a way to compare formulas, but it is usually much longer.
String(dnf("p -> q", { method: "table" })); // "(¬p ∧ ¬q) ∨ (¬p ∧ q) ∨ (p ∧ q)"
String(dnf("p -> q")); // "¬p ∨ q"
When distribution explodes
Distribution can multiply the size of a formula at every step. has literals, and its CNF has clauses: one for each way of picking or from every part. cnf refuses when the count passes its limit, and says what to use instead.
The Tseitin form avoids the explosion by giving each compound subformula a new atom. For standing for , three short clauses say :
One such group for each connective, and one clause asserting the atom for the whole formula. The result grows in step with the formula. It is not equivalent to the original, since it has more atoms, but it is satisfiable exactly when the original is, and a model of it, restricted to the original atoms, is a model of the original. That is all a solver needs.
const t = tseitin("(p & q) | ~r");
t.definitions.map((d) => `${d.atom} stands for ${d.formula}`);
// ["_1 stands for p ∧ q", "_2 stands for (p ∧ q) ∨ ¬r"]
t.clauses;
// [["p", "¬_1"], ["q", "¬_1"], ["¬p", "¬q", "_1"], ["_1", "¬_2"], ["¬r", "¬_2"], ["¬_1", "r", "_2"], ["_2"]]
The functions
| Call | Returns |
|---|---|
nnf(f) | Negation normal form. |
cnf(f, { method }) | Conjunctive form. method: "distribute" (default), "table" or "tseitin". |
dnf(f, { method }) | Disjunctive form. method: "distribute" (default) or "table". |
clauses(f, { method }) | The clauses as lists of literals, "p" or "¬p". |
tseitin(f) | { formula, clauses, definitions, atoms }. |
foldConstants(f) | The formula with ⊤ and ⊥ absorbed: the result is ⊤, ⊥, or free of both. |
isNnf(f), isCnf(f), isDnf(f) | Whether a formula already has the shape. |
@ratmath/logic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.