Topic

Predicate logic

Propositional logic treats "Socrates is mortal" as one unbreakable letter. Predicate logic opens it up: a predicate applied to terms, with quantifiers saying "for every" and "for some". That is enough to write most of mathematics.

Writing formulas

Predicates start with a capital letter (Human(x), Loves(x, y), or a bare P for a sentence letter). Function symbols and constants start with a small letter (f(x), socrates). A name is a variable when a quantifier binds it, or when it is one of u to z (with digits or primes); any other small name is a constant. Identity is = and ≠, and every propositional connective works as before.

import { parseFO } from "@ratmath/logic";

const f = parseFO("forall x. (Human(x) -> Mortal(x))");
String(f);                          // "∀x (Human(x) → Mortal(x))"
f.toString({ style: "ascii" });     // "forall x.(Human(x) -> Mortal(x))"
f.toString({ style: "tex" });       // "\forall x (Human(x) \to Mortal(x))"
f.isSentence;                       // true: no variable is free

String(parseFO("∀x ∃y (f(x) ≠ y ∨ x = a)"));   // "∀x ∃y (f(x) ≠ y ∨ x = a)"

A quantifier reaches as far right as it can, so ∀x P(x) → Q(x) would put the whole implication under ∀x\forall x; use parentheses to stop it. ∀, forall and \A all mean the same, and so do ∃, exists and \E.

Free and bound variables

An occurrence of xx is bound when it sits inside ∀x\forall x or ∃x\exists x, and free otherwise. A formula with no free variables is a sentence: it is true or false in a structure without anything more. With free variables it is true or false only once they are given values.

Putting a term in for a free variable has one trap. In ∃y Loves(x,y)\exists y\, Loves(x, y) ("x loves someone"), putting yy for xx naively gives ∃y Loves(y,y)\exists y\, Loves(y, y) ("someone loves themselves"), a different statement: the new yy was captured by the quantifier. substitute renames the bound variable first:

const g = parseFO("∃y Loves(x, y)");
g.freeVariables();                  // ["x"]
String(g.substitute("x", "y"));     // "∃y1 Loves(y, y1)": still "y loves someone"
parseFO("∀x P(x)").alphaEquals("∀y P(y)");   // true: the same up to the bound name

Normal forms

Every formula is equivalent to one in prenex form, with all its quantifiers at the front. The rules move a quantifier out past a connective: ∀x A→B\forall x\,A \to B becomes ∃x (A→B)\exists x\,(A \to B) when xx is not free in BB (the quantifier flips on the left of an arrow, since A→BA \to B is ¬A∨B\lnot A \lor B). Bound variables are renamed when two would clash.

Skolem form then removes each ∃\exists: in ∀x ∃y Loves(x,y)\forall x\, \exists y\, Loves(x, y) the yy depends on xx, so it is replaced by a new function, Loves(x,sk1(x))Loves(x, sk_1(x)). The result is not equivalent, but it is satisfiable exactly when the original is, which is all that refutation methods need.

import { prenex, skolemize, foNnf } from "@ratmath/logic";

String(prenex("∀x P(x) → ∃x Q(x)"));        // "∃x ∃x1 (¬P(x) ∨ Q(x1))"
String(skolemize("∀x ∃y Loves(x, y)"));     // "∀x Loves(x, sk1(x))"
String(skolemize("∃x ∀y Loves(y, x)"));     // "∀y Loves(y, c1)": no ∀ before it, so a constant
String(foNnf("¬∀x (P(x) → ∃y R(x, y))"));   // "∃x (P(x) ∧ ∀y ¬R(x, y))"

Reference

ExportWhat it does
parseFO(text)A FOFormula. Kinds: pred, eq, top, bot, not, the two-place connectives, all, ex.
fo(x), foList(text | array)Accept a formula or text; a list may be written with commas.
f.freeVariables(), f.isSentence, f.signature()Free variables; whether there are none; the predicates, functions (with arities) and constants used.
f.substitute(x, term)Put a term for the free x, renaming bound variables so nothing is captured.
f.alphaEquals(g)Equal up to the names of bound variables.
f.toString({ style })"unicode", "ascii" or "tex".
parseTerm(text), TermTerms: variables, constants and function applications.
foNnf(f), prenex(f), skolemize(f)Negation normal form; prenex form; Skolem form (equisatisfiable).
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