Truth tables
A formula with atoms has valuations, and its truth table lists the value under each. truthTable writes the table out; tableBits keeps it as one whole number, which is how the library compares and counts tables without writing them.
The table
import { truthTable } from "@ratmath/logic";
const t = truthTable("p -> q");
t.toString();
// p │ q │ p → q
// ──┼───┼──────
// T │ T │ T
// T │ F │ F
// F │ T │ T
// F │ F │ T
t.kind; // "contingent": true on some rows, false on others
t.rows[1]; // { index: 2, assignment: { p: true, q: false }, values: [false], value: false }
With subformulas: true every compound part gets its own column, which is how a table is worked by hand. Several formulas can share a table, for comparing them row by row.
truthTable("(p | q) & ~p", { subformulas: true }).toString();
// p │ q │ p ∨ q │ ¬p │ (p ∨ q) ∧ ¬p
// ──┼───┼───────┼────┼─────────────
// T │ T │ T │ F │ F
// T │ F │ T │ F │ F
// F │ T │ T │ T │ T
// F │ F │ F │ T │ F
truthTable(["p -> q", "~q -> ~p"]).rows.every((r) => r.values[0] === r.values[1]); // true: contrapositives agree
Rows start with every atom true, as in most logic books; order: "FT" counts upward in binary from all false instead, as in most circuit books. toString({ symbols: ["1", "0"] }) changes what is printed.
Three kinds of formula
A tautology is true on every row, a contradiction on none, and anything else is contingent. t.kind says which. For formulas with many atoms, where the table is too long to write, classify answers the same question by search.
Rows as numbers
Read the atoms of a row as a binary numeral, first atom leading, and each row has a number: with atoms , the row true, false is row . The rows where a formula is true are its minterms; the rows where it is false are its maxterms.
import { minterms, maxterms, fromMinterms, fromMaxterms, countModels, models, tableBits } from "@ratmath/logic";
minterms("p xor q"); // [1, 2]
maxterms("p xor q"); // [0, 3]
String(fromMinterms(["p", "q"], [1, 2])); // "(¬p ∧ q) ∨ (p ∧ ¬q)": one term for each true row
String(fromMaxterms(["p", "q"], [0, 3])); // "(p ∨ q) ∧ (¬p ∨ ¬q)": one clause for each false row
countModels("p | q | r"); // 7n of the 8 valuations
models("p & ~q"); // [{ p: true, q: false }]
tableBits("p -> q"); // 11n = 1011₂: true on rows 3, 1 and 0
Every truth table is the table of some formula: fromMinterms builds one directly, which proves that , and suffice to express any truth function. tableBits packs the whole column into one BigInt, bit for row ; two formulas over the same atoms are equivalent exactly when these numbers are equal.
Limits
A table is written out for up to 16 atoms (65,536 rows), and kept as a number for up to 22. Beyond that, ask satisfiable or valid, which do not go through the table.
The functions
| Call | Returns |
|---|---|
truthTable(f, { subformulas, order, atoms }) | { atoms, columns, rows, kind, trueRows, toString() }. f may be a list of formulas. |
tableBits(f, atoms?) | The table as a BigInt. |
minterms(f), maxterms(f) | The numbers of the true rows, of the false rows. |
fromMinterms(atoms, rows), fromMaxterms(atoms, rows) | The full disjunctive or conjunctive form with that table. |
countModels(f), models(f, { limit }) | How many valuations make it true; the valuations. |
@ratmath/logic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.