Topic

Natural deduction

A natural deduction proof is a numbered list of lines, each a premise, an assumption that opens a subproof, or the result of a rule applied to earlier lines. checkProof reads one as it would be written on paper and checks every line against a rule set.

Writing a proof

One line holds an optional number, bars for the depth of the subproof, the formula, and the reason. Bars may be | or │; rule names may be typed as ->E, →E, MP or modus ponens.

import { checkProof, parseProof } from "@ratmath/logic";

const text = `
1. p -> q      premise
2. ~q          premise
3. | p         assumption
4. | q         ->E 1, 3
5. | _|_       ~E 4, 2
6. ~p          ~I 3-5`;

const result = checkProof(text);
result.status;              // "proof"
result.proves;              // "p → q, ¬q ⊢ ¬p"
String(parseProof(text));   // the same proof, set out with │ bars and proper symbols

A line may cite any earlier line that is still in reach: a line of the proof itself, or of a subproof that is still open. A subproof that has ended can be cited only as a whole, as 3-5 above. That restriction is the whole point of the bars: what was derived under an assumption depends on it.

When a line fails

Every error names the line, the rule, and what the rule needed and did not find. Checking does not stop at the first one.

checkProof(`
1. p -> q      premise
2. q           premise
3. p           ->E 1, 2
4. | r         assumption
5. | p         R 3
6. r -> p      ->I 4-5
7. r           R 4`).errors.map((e) => e.message);
// [ 'line 3: to give "p", →E needs a line reading "φ → p"; line 1 reads "p → q"',
//   'line 7: it cites line 4, which is inside the subproof 4-5 that has ended;
//    only the subproof as a whole can be cited now' ]

Give the argument being proved and the checker also says whether the proof is finished. A proof whose lines all check but which has not reached the conclusion is "incomplete", which is a different thing from wrong.

checkProof(text, { premises: "p -> q, ~q", conclusion: "~p" }).status;   // "proof"
checkProof(text, { conclusion: "q" }).note;
// 'every line checks, but the last line is "¬p", not the conclusion "q"'

The rules

Each connective has a rule that introduces it and a rule that eliminates it. Writing A⊢BA \vdash B for a subproof that assumes AA and reaches BB:

RuleFromInfer
∧I, ∧EAA and BB; A∧BA \land BA∧BA \land B; either part
∨IAAA∨BA \lor B or B∨AB \lor A
∨EA∨BA \lor B, A⊢CA \vdash C, B⊢CB \vdash CCC
→IA⊢BA \vdash BA→BA \to B
→EA→BA \to B and AABB
¬IA⊢⊥A \vdash \bot¬A\lnot A
¬EAA and ¬A\lnot A⊥\bot
⊥E⊥\botanything
↔I, ↔EA⊢BA \vdash B and B⊢AB \vdash A; A↔BA \leftrightarrow B and one sideA↔BA \leftrightarrow B; the other side
RAAAA again, inside a subproof
IP¬A⊢⊥\lnot A \vdash \botAA (classical)
DNE, LEM¬¬A\lnot\lnot A; nothingAA; A∨¬AA \lor \lnot A (classical)

The rules above the last two lines are intuitionistic logic: a proof built from them is a construction. Adding any one of IP, DNE or LEM gives classical logic, where every formula true in all rows of its truth table has a proof. The two are kept as separate rule sets, and a classical step in an intuitionistic proof is reported as exactly that:

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

const dne = `1. ~~p   premise
2. p     DNE 1`;
checkProof(dne).status;                                              // "proof": the default is rules.classical
checkProof(dne, { rules: rules.intuitionistic }).errors[0].message;
// "line 2: DNE is a classical rule and is not in this rule set (intuitionistic natural deduction)"

rules.derived is classical logic with the usual shortcuts: MT (modus tollens), DS (disjunctive syllogism), HS (hypothetical syllogism), DNI and DeM (De Morgan's laws).

Rules are data

A rule is a name, some other names it answers to, and one or more forms. A form lists what the rule needs, with A ⊢ B standing for a subproof, and what it gives. The checker has no rule built in: it matches a line against the forms of the rule the line names.

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

rules.intuitionistic.rules.find((r) => r.name === "∨E").forms;
// [ { from: ["A ∨ B", "A ⊢ C", "B ⊢ C"], infer: "C" } ]

// A course that allows modus tollens and the constructive dilemma, and nothing classical:
const course = ruleSet("Logic 101", [
  ...rules.intuitionistic.rules,
  { name: "MT", aliases: ["modus tollens"], forms: [{ from: ["A → B", "¬B"], infer: "¬A" }] },
  { name: "CD", aliases: ["dilemma"], forms: [{ from: ["A ∨ B", "A → C", "B → D"], infer: "C ∨ D" }] },
], { logic: "intuitionistic" });

checkProof(`1. p | q    premise
2. p -> r   premise
3. q -> s   premise
4. r | s    dilemma 1, 2, 3`, { rules: course }).status;   // "proof"

The checker does not ask whether a rule you add is sound. That is a question entails answers: a form is sound when its from entails its infer.

Reference

ExportWhat it does
checkProof(proof, { rules, premises, conclusion }){ status, ok, complete, errors, note, proves, lines, rules }; status is "proof", "incomplete" or "errors".
parseProof(text, { rules })A Proof: lines of { n, depth, formula, rule, cites }, and toString({ style }).
rules.intuitionistic, rules.classical, rules.derivedThe rule sets that come with the library.
ruleSet(name, list, { logic })A rule set from rules written as data; find(name) looks a rule up by any of its names.
proofState(proof)Which subproofs are open and closed after the last line.
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