logic design

The logic package turns the equality calculus of the kernel into propositional logic. It defines the connectives as kernel definitions, derives the natural-deduction rules from the ten primitive rules, and keeps the catalog of theorem names that proof scripts may cite. This page gives the definitions, derives the rules, and explains why the package can do all this without being trusted.

Design goal

The kernel knows only equality and choice. Users want ⊤\top, ⊥\bot, ∧\wedge, ⇒\Rightarrow, ¬\neg and ∨\vee with their usual rules. The goal is to provide them with no new authority: every connective is a definition admitted by the DefOK gate, and every rule is a MoonBit function that calls kernel rules. The package may be wrong in the sense of failing to prove something, but it cannot prove anything false.

Mathematical background

Connectives as definitions

QED follows the definitions of HOL Light, where every connective is reduced to equality.11 J. Harrison, HOL Light Tutorial, section on the logical constants; the definitions go back to Andrews’ type theory Q0. QED’s disjunction differs from HOL Light’s, see below. Let tt be the truth term (λx. x)=(λx. x)(\lambda x.\,x) = (\lambda x.\,x). The prelude defines

⊤:=t⊥:=(λp. p)=(λp. t)and:=λp q.  (λf. f p q)=(λf. f t t)imp:=λp q.  and′ p q=pnot:=λp.  imp′ p ⊥′or:=λp q.  imp′ (not′ p) q\begin{aligned} \top &:= t \\ \bot &:= (\lambda p.\,p) = (\lambda p.\,t) \\ \mathit{and} &:= \lambda p\,q.\;(\lambda f.\,f\,p\,q) = (\lambda f.\,f\,t\,t) \\ \mathit{imp} &:= \lambda p\,q.\;\mathit{and}'\,p\,q = p \\ \mathit{not} &:= \lambda p.\;\mathit{imp}'\,p\,\bot' \\ \mathit{or} &:= \lambda p\,q.\;\mathit{imp}'\,(\mathit{not}'\,p)\,q \end{aligned}

where a primed name stands for the definition body already expanded, so that each right-hand side is a closed term over == alone. The reading of each definition:

  • tt is true because it is an instance of REFL.
  • ⊥\bot says the identity predicate on booleans equals the constantly-true predicate, that is ∀p. p\forall p.\,p with ∀P:=(P=λx. t)\forall P := (P = \lambda x.\,t). It is false in the standard model because the two predicates differ at p=falsep = \text{false}.
  • p∧qp \wedge q says the pair (p,q)(p, q) cannot be told apart from (t,t)(t, t) by any function ff, which holds exactly when pp and qq are both true.
  • p⇒qp \Rightarrow q is (p∧q)=p(p \wedge q) = p: adding qq to pp changes nothing.
  • ¬p\neg p is p⇒⊥p \Rightarrow \bot.
  • p∨qp \vee q is ¬p⇒q\neg p \Rightarrow q.

Basis terms

prop_mk_and and its siblings return the basis form, the expanded right-hand side, rather than the application and p q\mathit{and}\,p\,q of the constant. The rules below work on basis forms, because only on those can the kernel rules act directly; the constants exist so that the definitions are recorded, can be cited, and can be recognised in terms that users write.

Design decisions

Derive, do not postulate

Every rule of the package is a derivation. The derivations below are the ones the code performs; each line is a kernel rule.

Truth. From the definition ⊢⊤=t\vdash \top = t, symmetry gives ⊢t=⊤\vdash t = \top, and ⊢t\vdash t is REFL (logic_prop_truth_thm), so EQ_MP gives ⊢⊤\vdash \top (logic_prop_truth_const_thm). Symmetry (logic_eq_sym) is itself derived:

⊢(=)=(=)REFLΓ⊢(=) s=(=) tMK_COMB(⋅, Γ⊢s=t)Γ⊢(s=s)=(t=s)MK_COMB(⋅, ⊢s=s)Γ⊢t=sEQ_MP(⋅, ⊢s=s)\begin{aligned} &\vdash (=) = (=) && \textsf{REFL} \\ &\Gamma \vdash (=)\,s = (=)\,t && \textsf{MK\_COMB}(\cdot,\ \Gamma \vdash s = t) \\ &\Gamma \vdash (s = s) = (t = s) && \textsf{MK\_COMB}(\cdot,\ \vdash s = s) \\ &\Gamma \vdash t = s && \textsf{EQ\_MP}(\cdot,\ \vdash s = s) \end{aligned}

From a proof to an equation with truth. From Γ⊢p\Gamma \vdash p and ⊢⊤\vdash \top, DEDUCT_ANTISYM_RULE gives (Γ∖{⊤})∪(∅∖{p})=Γ∖{⊤}⊢p=⊤(\Gamma \setminus \{\top\}) \cup (\emptyset \setminus \{p\}) = \Gamma \setminus \{\top\} \vdash p = \top, and Γ∖{⊤}=Γ\Gamma \setminus \{\top\} = \Gamma unless ⊤\top is itself a hypothesis, in which case dropping it is harmless because ⊤\top is provable. This “EQT_INTRO” step is how propositions are put inside terms.

Conjunction introduction. From Γ⊢p\Gamma \vdash p and Δ⊢q\Delta \vdash q, EQT_INTRO gives Γ⊢p=⊤\Gamma \vdash p = \top and Δ⊢q=⊤\Delta \vdash q = \top. Congruence with the constant and\mathit{and} then gives

⊢and=andREFLΓ⊢and p=and ⊤MK_COMBΓ∪Δ⊢and p q=and ⊤ ⊤MK_COMB\begin{aligned} &\vdash \mathit{and} = \mathit{and} && \textsf{REFL} \\ &\Gamma \vdash \mathit{and}\,p = \mathit{and}\,\top && \textsf{MK\_COMB} \\ &\Gamma \cup \Delta \vdash \mathit{and}\,p\,q = \mathit{and}\,\top\,\top && \textsf{MK\_COMB} \end{aligned}

and symmetry with EQ_MP turns a proof of ⊢and ⊤ ⊤\vdash \mathit{and}\,\top\,\top into Γ∪Δ⊢and p q\Gamma \cup \Delta \vdash \mathit{and}\,p\,q. That proof needs no hypotheses: the definition of and\mathit{and}, applied to ⊤,⊤\top, \top and β-normalised, gives ⊢and ⊤ ⊤=((λf. f ⊤ ⊤)=(λf. f t t))\vdash \mathit{and}\,\top\,\top = \big((\lambda f.\,f\,\top\,\top) = (\lambda f.\,f\,t\,t)\big), and the right-hand side is proved for a variable ff by

⊢f=fREFL⊢f ⊤=f tMK_COMB(⋅, ⊢⊤=t)⊢f ⊤ ⊤=f t tMK_COMB(⋅, ⊢⊤=t)⊢(λf. f ⊤ ⊤)=(λf. f t t)ABS\begin{aligned} &\vdash f = f && \textsf{REFL} \\ &\vdash f\,\top = f\,t && \textsf{MK\_COMB}(\cdot,\ \vdash \top = t) \\ &\vdash f\,\top\,\top = f\,t\,t && \textsf{MK\_COMB}(\cdot,\ \vdash \top = t) \\ &\vdash (\lambda f.\,f\,\top\,\top) = (\lambda f.\,f\,t\,t) && \textsf{ABS} \end{aligned}

ABS applies because the hypothesis set is empty, and the abstracted terms are closed apart from ff, so nothing is captured. Finally the definition of and\mathit{and} is unfolded in Γ∪Δ⊢and p q\Gamma \cup \Delta \vdash \mathit{and}\,p\,q and the result β-normalised, which gives the basis form (λf. f p q)=(λf. f t t)(\lambda f.\,f\,p\,q) = (\lambda f.\,f\,t\,t), that is p∧qp \wedge q. This route uses ABS only on closed terms. The textbook route, MK_COMB on Γ⊢p=t\Gamma \vdash p = t and Δ⊢q=t\Delta \vdash q = t followed by ABS on ff, would need ff to be fresh for pp, qq and Γ∪Δ\Gamma \cup \Delta.

Conjunction elimination. Apply both sides of Γ⊢(λf. f p q)=(λf. f t t)\Gamma \vdash (\lambda f.\,f\,p\,q) = (\lambda f.\,f\,t\,t) to the selector λx y. x\lambda x\,y.\,x with MK_COMB and REFL, then reduce both sides with BETA and TRANS:

Γ⊢(λx y. x) p q=(λx y. x) t t⇝Γ⊢p=t\Gamma \vdash (\lambda x\,y.\,x)\,p\,q = (\lambda x\,y.\,x)\,t\,t \quad\leadsto\quad \Gamma \vdash p = t

Symmetry and EQ_MP with ⊢t\vdash t give Γ⊢p\Gamma \vdash p. The selector λx y. y\lambda x\,y.\,y gives qq.

Implication elimination. From Γ⊢(p∧q)=p\Gamma \vdash (p \wedge q) = p and Δ⊢p\Delta \vdash p: symmetry gives Γ⊢p=(p∧q)\Gamma \vdash p = (p \wedge q), EQ_MP gives Γ∪Δ⊢p∧q\Gamma \cup \Delta \vdash p \wedge q, and conjunction elimination gives qq.

Implication introduction. From Γ⊢q\Gamma \vdash q: conjunction introduction with {p}⊢p\{p\} \vdash p gives Γ∪{p}⊢p∧q\Gamma \cup \{p\} \vdash p \wedge q, and elimination from the assumption gives {p∧q}⊢p\{p \wedge q\} \vdash p. Then

Γ∪{p}⊢p∧q{p∧q}⊢p((Γ∪{p})∖{p})∪({p∧q}∖{p∧q})⊢(p∧q)=p  DEDUCT_ANTISYM_RULE\frac{\Gamma \cup \{p\} \vdash p \wedge q \qquad \{p \wedge q\} \vdash p}{\big((\Gamma \cup \{p\}) \setminus \{p\}\big) \cup (\{p \wedge q\} \setminus \{p \wedge q\}) \vdash (p \wedge q) = p}\;\textsf{DEDUCT\_ANTISYM\_RULE}

and the hypothesis set is Γ∖{p}\Gamma \setminus \{p\}. The conclusion is p⇒qp \Rightarrow q by definition. The derivation works whether or not p∈Γp \in \Gamma: when pp is absent, the conjunction step adds it and DEDUCT_ANTISYM_RULE removes it again, giving the vacuous implication Γ⊢p⇒q\Gamma \vdash p \Rightarrow q. logic_prop_imp_intro_thm accepts both cases, as Lean Derivation.impIntro and HOL Light DISCH do: when pp is absent it first weakens the premise with ADD_ASSUM.

Ex falso. From Γ⊢(λp. p)=(λp. t)\Gamma \vdash (\lambda p.\,p) = (\lambda p.\,t) and any proposition qq, MK_COMB with ⊢q=q\vdash q = q and two BETA steps give Γ⊢q=t\Gamma \vdash q = t, hence Γ⊢q\Gamma \vdash q. Negation elimination is implication elimination with conclusion ⊥\bot.

Disjunction introduction. From Γ⊢p\Gamma \vdash p: assume ¬p\neg p, eliminate it against pp to get Γ∪{¬p}⊢⊥\Gamma \cup \{\neg p\} \vdash \bot, derive qq by ex falso, and discharge ¬p\neg p:

Γ∖{¬p}⊢¬p⇒q  =  p∨q.\Gamma \setminus \{\neg p\} \vdash \neg p \Rightarrow q \;=\; p \vee q.

The hypotheses are Γ\Gamma unless ¬p\neg p was already among them. From Δ⊢q\Delta \vdash q the right introduction must discharge a ¬p\neg p that the proof of qq does not use. The code first conjoins {¬p}⊢¬p\{\neg p\} \vdash \neg p with Δ⊢q\Delta \vdash q and eliminates the conjunction again, which gives Δ∪{¬p}⊢q\Delta \cup \{\neg p\} \vdash q, and then discharges ¬p\neg p: Δ∖{¬p}⊢p∨q\Delta \setminus \{\neg p\} \vdash p \vee q.

Quantifiers. The universal quantifier is the HOL Light definition ∀P:=(P=λx. ⊤)\forall P := (P = \lambda x.\,\top) unfolded, so ∀x. φ\forall x.\,\varphi is the term (λx. φ)=(λx. ⊤)(\lambda x.\,\varphi) = (\lambda x.\,\top) with ⊤\top the prelude constant; this is also the shape the tactics layer introduces for a quantified goal. The existential is the impredicative encoding

∃x. φ  :=  ∀q. (∀x. φ⇒q)⇒q\exists x.\,\varphi \;:=\; \forall q.\,\big(\forall x.\,\varphi \Rightarrow q\big) \Rightarrow q

for a boolean qq not free in φ\varphi. HOL Light instead defines ∃P:=P (εP)\exists P := P\,(\varepsilon P), which needs the choice constant; the encoding here needs nothing beyond the propositional rules, so the quantifier rules stay independent of choice. Both sides are definitions of terms only: no constant is added to the state. The four rules follow Lean Derivation.allIntro, allElim, exIntro and exElim:

  • ∀-introduction (GEN). From Γ⊢φ\Gamma \vdash \varphi, EQT_INTRO gives Γ⊢φ=⊤\Gamma \vdash \varphi = \top and ABS on xx gives Γ⊢(λx. φ)=(λx. ⊤)\Gamma \vdash (\lambda x.\,\varphi) = (\lambda x.\,\top). ABS fails when xx is free in Γ\Gamma, which is Lean’s weakening of the premise. EQT_INTRO is DEDUCT_ANTISYM_RULE against ⊢⊤\vdash \top and would drop a hypothesis ⊤\top; the code adds it back, so the hypotheses are exactly Γ\Gamma.
  • ∀-elimination (SPEC). MK_COMB with ⊢t=t\vdash t = t gives Γ⊢(λx. φ) t=(λx. ⊤) t\Gamma \vdash (\lambda x.\,\varphi)\,t = (\lambda x.\,\top)\,t; one BETA step on each side and TRANS give Γ⊢φ[t/x]=⊤\Gamma \vdash \varphi[t/x] = \top, and EQ_MP with ⊢⊤\vdash \top gives Γ⊢φ[t/x]\Gamma \vdash \varphi[t/x]. Only the two outer redexes are contracted, so a redex inside φ\varphi remains, as in Lean’s φ.substitute.
  • ∃-introduction (EXISTS). From Γ⊢φ[t/x]\Gamma \vdash \varphi[t/x], assume ∀x. φ⇒q\forall x.\,\varphi \Rightarrow q, specialise it to tt and apply modus ponens to get qq; discharge the assumption and generalise qq. The qq is chosen fresh for Γ\Gamma, so the generalisation applies.
  • ∃-elimination (CHOOSE). From A⊢∃x. φA \vdash \exists x.\,\varphi and Δ∪{φ[v/x]}⊢B\Delta \cup \{\varphi[v/x]\} \vdash B, where vv is a variable not free in ∃x. φ\exists x.\,\varphi, BB or Δ\Delta: discharge φ[v/x]\varphi[v/x], generalise vv to get Δ⊢∀v. φ[v/x]⇒B\Delta \vdash \forall v.\,\varphi[v/x] \Rightarrow B, which is ∀x. φ⇒B\forall x.\,\varphi \Rightarrow B up to α because vv is fresh; specialise the existential to BB and apply modus ponens: A∪Δ⊢BA \cup \Delta \vdash B. The side conditions are checked before the derivation, so a violation is reported as VarFreeInHyp.

No disjunction elimination

The prelude defines p∨qp \vee q as ¬p⇒q\neg p \Rightarrow q. Introduction is derivable, as shown. Elimination, from p∨qp \vee q, p⇒rp \Rightarrow r and q⇒rq \Rightarrow r infer rr, is not: it needs a case split on pp, that is excluded middle p∨¬pp \vee \neg p. In HOL excluded middle follows from the choice axiom and extensionality (Diaconescu’s theorem),22 R. Diaconescu, “Axiom of choice and complementation”, Proc. AMS 51, 1975. HOL Light derives EXCLUDED_MIDDLE this way in class.ml. but the kernel exposes no theorem for the choice axiom and the package does not derive excluded middle. Rather than ship a rule it cannot justify, the catalog has or_intro_l and or_intro_r and no or_elim, and the tactic layer has left and right but no case analysis.

Trusted recognition of connective constants

Problem. A user can declare a constant named and that means something else. If prop_dest_and recognised any application of a constant called and, a tactic could treat an arbitrary term as a conjunction.

Choice. The destructors accept the basis form, which is checked structurally, and an application of a connective constant only when the state holds the canonical definition theorem for that constant (with the current identity). install_prop_prelude refuses to install over a placeholder constant with the right name and type but no definition.

Why. Recognition errors would not break soundness, because every rule is replayed through the kernel. They would produce confusing failures at replay time instead of clear failures at the tactic, and the specification requires connector recognition to be backed by definitions.

One catalog, two modes

Problem. exact th and apply th mean different things. exact needs a theorem whose conclusion is the goal; apply needs an implication whose consequent is the goal and leaves its antecedent as the new goal. Letting a name be used in the wrong mode silently would either fail late or, worse, make exact quietly behave like apply.

Choice. Each catalog entry records an exact_class and an apply_class. exact consults only the first, apply only the second, and a name that exists but is unusable in a mode resolves to KnownButUnavailable, which the tactics layer reports as a shape or apply mismatch. A local hypothesis name always takes precedence over a catalog name and never falls back to it.

Why. The table is the single source for the tactics layer, the prover’s corpus, the mapping matrix and the documentation, so the theorem names a user may write cannot drift between them.

Errors from the kernel only

Kernel error constructors are read-only outside the kernel. The package obtains the LogicError and SigError values it returns by running small kernel operations that are known to fail in the required way. This keeps the error vocabulary owned by the kernel, at the cost of less specific errors: many helper failures surface as TypeMismatch or AlphaMismatch.

Correctness and invariants

  • Soundness. Every Thm returned by this package is the result of kernel functions, so the kernel’s soundness argument covers it.
  • Coverage, not completeness. The derivations above show that each rule can succeed whenever its premises have the stated shapes. The code can still fail on such premises in corner cases: β-normalisation is bounded (see below), and the connective builders choose fixed binder names, so prop_mk_and captures a free variable of its arguments that is literally named _p_and and has type bool→bool→bool\mathit{bool} \to \mathit{bool} \to \mathit{bool}; the resulting term is not p∧qp \wedge q, and logic_prop_and_intro_thm then fails. Such failures are reported as errors, never as theorems.
  • Conservativity of the prelude. The six constants are admitted by DefOK with closed right-hand sides, no type variables and no cycles, so the prelude is a conservative extension of the empty theory.
  • Idempotence. On a state that already holds the six canonical definitions, install_prop_prelude returns its argument unchanged, so installing twice is the same as installing once.
  • Exact sequents. The replay helpers check hypotheses as sets up to α-equivalence. logic_prop_strengthen_to_hyps only adds hypotheses, so a theorem that needs a hypothesis the goal does not have is rejected rather than accepted with extra assumptions.
  • β-normalisation is proof-producing. logic_beta_normalize_eq and logic_normalize_prop_beta build kernel equations step by step; the term-level logic_beta_nf_* functions return the right-hand side of such an equation. logic_beta_normalize_eq contracts only head redexes, with no step bound; this terminates because simply typed terms are strongly normalising. logic_normalize_prop_beta and logic_beta_nf_bool_term contract leftmost-outermost redexes along application spines, never under an abstraction, and stop after 512 steps even if redexes remain; the deep form repeats such passes up to 32 times. These bounds suffice for the connective encodings.

Alternatives rejected

  • Connectives as kernel primitives. This would add rules to the kernel and to its soundness proof. Definitions cost nothing in trust.
  • HOL Light’s disjunction ∀r. (p⇒r)⇒(q⇒r)⇒r\forall r.\,(p \Rightarrow r) \Rightarrow (q \Rightarrow r) \Rightarrow r. With it, elimination is derivable without excluded middle, at the cost of a universal quantifier over propositions inside every disjunction. The prelude uses the shorter encoding and states its limit instead; switching would change every disjunction term and the corpus built on them.
  • A mixed resolver. logic_prop_resolve_ref resolves names without distinguishing modes; it is kept for tests, while the tactics layer uses the mode-specific resolvers.

Boundaries

  • The package proves propositional facts and the four quantifier rules. It has no rules for Hilbert choice or the axiom of infinity, and the tactics layer does not use the quantifier rules yet.
  • It has no disjunction elimination, no excluded middle and no classical reasoning.
  • It does not store user theorems: the catalog is fixed in the source, and adding a name means adding code and a test.
  • It adds no authority. Any change to it is reviewed for usefulness, not for soundness.
  • It does not parse or print formulas; terms are built by the parser and printed by the kernel’s structural printer.

Footnotes

  1. J. Harrison, HOL Light Tutorial, section on the logical constants; the definitions go back to Andrews’ type theory Q0. QED’s disjunction differs from HOL Light’s, see below. ↩

  2. R. Diaconescu, “Axiom of choice and complementation”, Proc. AMS 51, 1975. HOL Light derives EXCLUDED_MIDDLE this way in class.ml. ↩