Topic

Finite models

A sentence is true or false in a structure: a set of things (the domain) with a meaning for each predicate, function and constant. On a finite domain, truth can simply be computed, and a search over small domains finds models and countermodels.

Structures and truth

∀x A\forall x\, A is true when AA holds for every element put in for xx, and ∃x A\exists x\, A when it holds for at least one. On a finite domain that is a loop, and the loop knows why: which element is the witness to an ∃\exists, and which is the counterexample to a ∀\forall.

import { structure, evaluate } from "@ratmath/logic";

// three people, each loving the next round a circle
const M = structure({ domain: [1, 2, 3], predicates: { L: [[1, 2], [2, 3], [3, 1]] } });

evaluate("∀x ∃y L(x, y)", M);
// { value: true, reason: "holds for every x in the domain" }
evaluate("∃x ∀y L(x, y)", M).value;      // false: nobody loves everyone
evaluate("∀x ∃y L(x, y)", M, { explain: true }).steps.slice(0, 2);
// ["∀x ∃y L(x, y): true (holds for every x in the domain)",
//  "  ∃y L(x, y) with x = 1: true (holds for y = 2)"]

Predicates are given as lists of the elements or tuples where they hold, or as functions. Functions may be tables or JavaScript functions; constants name elements.

const N = structure({ domain: 4, constants: { zero: 0 },
  functions: { s: (x) => (x + 1) % 4 }, predicates: { Even: [0, 2] } });
evaluate("∀x (Even(x) ↔ ¬Even(s(x)))", N).value;   // true
evaluate("Even(s(s(zero)))", N).value;             // true

Searching for a model

On a domain of nn elements, a sentence becomes a propositional formula: ∀x\forall x turns into a conjunction over the nn elements and ∃x\exists x into a disjunction, and each atom such as L(0,2)L(0, 2) or f(1)=0f(1) = 0 into a letter. Function symbols get one letter for each possible value, with clauses saying that exactly one value is taken. The SAT solver then looks for an assignment, and findModel reads the structure back and checks it by evaluation before returning it.

import { findModel, countermodel } from "@ratmath/logic";

const r = findModel("∀x f(f(x)) = x, ∃x f(x) ≠ x");   // a non-trivial involution
r.status, r.size;                  // "found", 2
r.model.describe().functions;      // { f: { "0": 1, "1": 0 } }

findModel("∀x ∀y x = y, ∃x ∃y x ≠ y");   // { status: "none", upTo: 4 }

Countermodels, and what search cannot do

An argument is invalid when some structure makes the premises true and the conclusion false. countermodel looks for one:

const c = countermodel("∀x ∃y L(x, y)", "∃y ∀x L(x, y)");
c.status;                          // "invalid"
c.countermodel.describe().predicates;   // { L: [[0, 0], [1, 1]] }: everyone loves only themselves

Not finding a countermodel proves nothing. First-order validity is undecidable (Church and Turing, 1936), and some sentences have only infinite models: a strict order with no largest element, for example. So an empty search is reported as "unresolved" with the size searched, never as "valid". Validity is shown by a proof or a closed tree; see First-order proofs and trees.

countermodel("∀x (H(x) → M(x)), H(s)", "M(s)");
// { status: "unresolved", reason: "no countermodel with at most 4 elements", upTo: 4 }

// irreflexive, transitive, and everything below something: only infinite models
findModel("∀x ¬R(x, x), ∀x ∀y ∀z (R(x, y) ∧ R(y, z) → R(x, z)), ∀x ∃y R(x, y)", { maxSize: 5 });
// { status: "none", upTo: 5 }

Reference

ExportWhat it does
structure({ domain, constants, functions, predicates })A Structure: domain, size, holds(f, env), term(t, env), describe() as plain data.
evaluate(f, M, { assignment, explain }){ value, reason }, with steps when explained. Free variables need values in assignment.
findModel(sentences, { minSize, maxSize, budget })"found" with a checked model and size; "none" up to maxSize (4 unless you say); or "unresolved" at the solver's budget.
countermodel(premises, conclusion, options)"invalid" with a checked countermodel, or "unresolved".
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