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.
| Connective | Accepted | Printed |
|---|---|---|
| not | ¬ ~ ! not | |
| and | ∧ & && /\ ^ and | |
| or | ∨ | || \/ or | |
| if … then | → -> => ⊃ implies | |
| if and only if | ↔ <-> <=> ≡ iff | |
| exclusive or | ⊕ xor | |
| true, false | ⊤ true 1, ⊥ false 0 _|_ | , |
An atom is a letter followed by letters, digits or underscores: p, q2, rain, door_open. Without brackets, binds tightest, then , , , , ; groups to the right, so p -> q -> r is .
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 or of 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: is false only when is true and false; is true when the two agree; 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
| Call | Returns |
|---|---|
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, FALSE | Formulas built in code. |
f.kind, f.name, f.arg, f.left, f.right, f.parts | The shape of the tree. |
f.atoms(), f.subformulas(), f.size, f.depth | What 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". |
@ratmath/logic and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.