Topic

Modal logic

Modal logic adds two operators: □A\Box A, "necessarily AA", and ◊A\Diamond A, "possibly AA". Read them as knowledge, obligation, provability or time, and the same machinery applies. What changes from reading to reading is which axioms hold, and Kripke's semantics ties each axiom to a property of a picture.

Formulas

Write □\Box as □, [], box or nec, and ◊\Diamond as ◇, <>, dia or poss. Everything else is propositional. ◊A\Diamond A means the same as ¬□¬A\lnot\Box\lnot A.

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

const f = parseModal("[]p -> <>p");
String(f);                        // "□p → ◇p"
f.modalDepth;                     // 1
String(parseModal("box p -> dia p"));   // "□p → ◇p"

Kripke models

A Kripke model is a set of worlds, an accessibility relation saying which worlds each world "sees", and a valuation saying where each atom is true. Then □A\Box A is true at ww when AA is true at every world ww sees, and ◊A\Diamond A when AA is true at some world ww sees. A world that sees nothing makes every □A\Box A true, even □⊥\Box\bot.

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

const M = kripke({ worlds: ["a", "b", "c"],
  access: [["a", "b"], ["a", "c"], ["b", "b"]], valuation: { p: ["b"] } });
M.truthAt("◇p ∧ ◇¬p", "a");      // true: a sees b (p) and c (not p)
M.truthAt("□⊥", "c");            // true: c sees nothing
M.trueAt("□p");                  // ["b", "c"]
M.truthAt("□p", "a", { explain: true }).steps[0];
// "□p at a: false (p fails at c, which a sees)"

Axioms and frames

A frame is a Kripke model without the valuation. A formula is valid on a frame when it is true at every world under every valuation, and some formulas are valid exactly on the frames with a property:

AxiomFormulaValid exactly on frames that are
K□(p→q)→(□p→□q)\Box(p \to q) \to (\Box p \to \Box q)all frames
D□p→◊p\Box p \to \Diamond pserial: every world sees some world
T□p→p\Box p \to preflexive
Bp→□◊pp \to \Box\Diamond psymmetric
4□p→□□p\Box p \to \Box\Box ptransitive
5◊p→□◊p\Diamond p \to \Box\Diamond peuclidean

The usual systems are named for their frames: K (any), D (serial), T (reflexive), B (reflexive and symmetric), S4 (reflexive and transitive) and S5 (all three, an equivalence relation). For knowledge, T says that what is known is true; 4 says that what is known is known to be known.

import { validOnFrame, SYSTEMS } from "@ratmath/logic";

validOnFrame("□p → p", { worlds: 2, access: [["w1", "w1"], ["w2", "w2"], ["w1", "w2"]] });
// { valid: true }: the frame is reflexive
const v = validOnFrame("□p → □□p", { worlds: 3, access: [["w1", "w2"], ["w2", "w3"]] });
v.valid, v.valuation, v.world;    // false, { p: ["w2"] }, "w1": the frame is not transitive

Deciding validity

modalValid builds a truth tree with each formula labelled by its world. ¬□A\lnot\Box A at ww creates a new world that ww sees, with ¬A\lnot A there; □A\Box A at ww sends AA to every world ww sees. The frame properties of the system add edges: a loop at each world for T, an edge back for B, edges through for S4. If every branch closes, the formula is valid in the system. If a branch stays open, its worlds and edges are a countermodel, which is checked to be a frame of the system and to make the formula false.

import { modalValid, showModalTree } from "@ratmath/logic";

modalValid("□p → p").status;                   // "invalid" in K
modalValid("□p → p").countermodel.describe();
// { worlds: ["w1"], access: { w1: [] }, valuation: { p: [] } }
modalValid("□p → p", { system: "T" }).status;  // "valid"

const r = modalValid("◇p → □◇p", { system: "S4" });
r.status;                                      // "invalid": axiom 5 needs S5
r.countermodel.describe().access;              // { w1: ["w2", "w3", "w1"], w2: ["w2"], w3: ["w3"] }

showModalTree(modalValid("□(p→q)→(□p→□q)").tree);
// w1: ¬(□(p → q) → (□p → □q))    (the formula is assumed false at w1)
// w1: □(p → q)    …
// w2: w1 sees w2    (for ¬□q at w1)
// w2: ¬q    (from ¬□q at w1)
// …
//   × closed: q and ¬q at w2

In S4 and S5 a tree could keep making worlds forever. A world whose formulas already appear at an earlier world is given no new successors; it points back to that world instead, which keeps every tree finite. The tests check the whole table of axioms against all six systems, and every countermodel against its frame conditions.

Reference

ExportWhat it does
parseModal(text), modal(x)A ModalFormula: atoms(), modalDepth, toString({ style }).
kripke({ worlds, access, valuation })A Kripke model: sees(w), truthAt(f, w, { explain }), trueAt(f), holds(f), properties(), describe().
SYSTEMSThe frame conditions of K, D, T, B, S4 and S5.
validOnFrame(f, frame)Every valuation tried (up to 2202^{20}); { valid }, or a valuation and world where it fails.
modalValid(f, { system, budget })"valid" with the closed tree, "invalid" with a checked countermodel, or "unresolved".
showModalTree(tree)The tree as text.
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