Topic

Formulas

A formula is a tree: atoms at the leaves, connectives above them. parse reads one from text typed on any keyboard, and a Formula prints itself back, lists its atoms and parts, and takes a truth value under a valuation.

Reading

import { parse, formula } from "@ratmath/logic";

const f = parse("~p & q -> (r | s)");
String(f);                      // "(¬p ∧ q) → (r ∨ s)"
f.kind;                         // "imp"
String(f.left);                 // "¬p ∧ q"
f.atoms();                      // ["p", "q", "r", "s"]

Every connective has several spellings, and they can be mixed.

ConnectiveAcceptedPrinted
not¬ ~ ! not¬\lnot
and∧ & && /\ ^ and∧\land
or∨ | || \/ or∨\lor
if … then→ -> => ⊃ implies→\to
if and only if↔ <-> <=> ≡ iff↔\leftrightarrow
exclusive or⊕ xor⊕\oplus
true, false⊤ true 1, ⊥ false 0 _|_⊤\top, ⊥\bot

An atom is a letter followed by letters, digits or underscores: p, q2, rain, door_open. Without brackets, ¬\lnot binds tightest, then ∧\land, ⊕\oplus, ∨\lor, →\to, ↔\leftrightarrow; →\to groups to the right, so p -> q -> r is p→(q→r)p \to (q \to r).

A formula that cannot be read says where the trouble is:

parse("p & (q | )");
// LogicError: a formula was expected, but found ")" at position 10 in "p & (q | )"

Writing

Printing never relies on the reader knowing the precedence. Wherever two different connectives meet, the brackets are written; only a run of ∧\land or of ∨\lor is left bare.

String(parse("p & q | r & s"));              // "(p ∧ q) ∨ (r ∧ s)"
String(parse("p & q & r"));                  // "p ∧ q ∧ r"
f.toString({ style: "ascii" });              // "(~p & q) -> (r | s)": reads back with any keyboard
f.toString({ style: "words" });              // "(not p and q) implies (r or s)"
f.toTeX();                                   // "(\lnot p \land q) \to (r \lor s)"

Formulas can also be built in code. and and or take any number of parts, and text is parsed where a formula is expected.

import { atom, not, and, or, implies, iff, xor, TRUE, FALSE } from "@ratmath/logic";

String(implies(and("p", "q", "r"), or(not("p"), iff("q", "r"))));   // "(p ∧ q ∧ r) → (¬p ∨ (q ↔ r))"
and() === TRUE;                                                     // the empty conjunction is true

Meaning

A valuation gives each atom a truth value, and the connectives pass values upward: A→BA \to B is false only when AA is true and BB false; A↔BA \leftrightarrow B is true when the two agree; A⊕BA \oplus B when they differ.

f.evaluate({ p: false, q: true, r: false, s: false });   // false: the left side holds and the right does not
f.subformulas().map(String);
// ["p", "¬p", "q", "¬p ∧ q", "r", "s", "r ∨ s", "(¬p ∧ q) → (r ∨ s)"]
String(f.substitute({ p: "a & b", s: FALSE }));           // "(¬(a ∧ b) ∧ q) → (r ∨ ⊥)"
f.equals("~p & q -> r | s");                              // true: the same tree

Two formulas are equals when they are the same tree. Having the same truth table is a different question, answered by equivalent.

The functions

CallReturns
parse(text)The formula, or a LogicError with position.
formula(x), formulas(x)A formula from a formula or text; a list from an array or from text with commas.
atom not and or implies iff xor, TRUE, FALSEFormulas built in code.
f.kind, f.name, f.arg, f.left, f.right, f.partsThe shape of the tree.
f.atoms(), f.subformulas(), f.size, f.depthWhat it is made of.
f.evaluate(valuation)Its truth value.
f.substitute(map), f.equals(g)Replacing atoms; comparing trees.
f.toString({ style }), f.toTeX()Text in "unicode", "ascii", "words" or "tex".
Try itEdit the code and run it with @ratmath/logic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs