4.5 Natural numbers, induction, and primitive recursion¶
Tutorial sequence · Previous: 4.4 Test conjectures numerically
Goal: construct the Peano natural numbers conservatively inside a classical
HOL session using judgment_nat, inspect nat.induct, and define a recursive
function (double) via define_primrec.
(Direct-package Mojo only. Build once from the repository root with
pixi run build-nat.)
Conservative construction from classical HOL¶
Instead of postulating Peano axioms, judgment_nat constructs nat
conservatively from standard classical HOL principles (Hilbert choice
\(\varepsilon\), function extensionality, and an infinite type ind), following
Isabelle/HOL and HOL Light:
recognize(s, budget)locates choice, function extensionality, and infinity by matching their exact logical statements rather than declaration names.build_nat(s, inputs, budget)definesnat.natas the smallest inductive subset ofind, definesnat.zeroandnat.suc, and proves the Peano axioms, mathematical induction (nat.induct), and the primitive recursion combinator without adding axioms.build_arithmetic(s, nat, budget)constructs checked addition, multiplication, order, and standard lemmas onnat.define_primrec(s, nat, name, base, step, budget)defines a recursive function satisfying \(f(0) = \text{base}\) and \(f(\text{suc}(n)) = \text{step}(n, f(n))\), returning checked defining equationszero_eqandsuc_eq.
Primitive recursion example¶
The example builds a classical profile, constructs nat and its arithmetic
theorems, and defines \(\mathrm{double}(0) = 0\),
\(\mathrm{double}(\text{suc}(n)) = \text{suc}(\text{suc}(\mathrm{double}(n)))\):
"""Build the natural numbers by definitions and define a function by primitive recursion."""
from judgment_shell import Session, arrow, exists, forall, implies, lambda_
from judgment_nat import NatName, build_arithmetic, build_nat, define_primrec, recognize
from termbank import Budget
def classical_profile(s: Session) raises:
"""Declare choice, function extensionality, and infinity as axioms."""
var a = s.type_var("demo.a")
var b = s.type_var("demo.b")
var prop = s.prop_type()
_ = s.symbol("demo.epsilon", arrow(arrow(a, prop), a))
var p = s.bound_var("P", arrow(a, prop))
var x = s.bound_var("x", a)
var eps = s.import_symbol("demo.epsilon", [a])
_ = s.axiom("demo.choice", forall(p, forall(x, implies(p(x), p(eps(p))))))
var f = s.bound_var("f", arrow(a, b))
var g = s.bound_var("g", arrow(a, b))
var y = s.bound_var("y", a)
_ = s.axiom("demo.funext", forall(f, forall(g, implies(forall(y, f(y) == g(y)), f == g))))
var ind = s.declare_type("demo.ind")
var sv = s.bound_var("s", arrow(ind, ind))
var z = s.bound_var("z", ind)
var u = s.bound_var("u", ind)
var w = s.bound_var("w", ind)
var injective = forall(u, forall(w, implies(sv(u) == sv(w), u == w)))
var outside = forall(u, ~(sv(u) == z))
_ = s.axiom("demo.infinity", exists(sv, exists(z, injective & outside)))
def main() raises:
var s = Session(work=100000000)
var budget = Budget(UInt64.MAX)
classical_profile(s)
var inputs = recognize(s, budget)
var nat = build_nat(s, inputs, budget)
var arithmetic = build_arithmetic(s, nat, budget)
print("Rows:", len(nat.rows), "then", len(arithmetic.names), "arithmetic declarations")
print("Induction:", nat.get(NatName.INDUCT).name)
# double := rec zero (λn r. suc (suc r)).
var n_type = s.import_type(nat.get(NatName.NAT).name)
var n = s.bound_var("n", n_type)
var r = s.bound_var("r", n_type)
var suc = s.import_symbol(nat.get(NatName.SUC).name)
var zero = s.import_symbol(nat.get(NatName.ZERO).name)
var step = lambda_(n, lambda_(r, suc(suc(r))))
var double = define_primrec(s, nat, "demo.double", zero, step, budget)
print("Zero:", double.zero_eq.statement().format())
print("Suc:", double.suc_eq.statement().format())
print("Axioms in double.zero_eq:", len(double.zero_eq.axioms()))
Run from the repository root pixi run mojo run -I src docs/examples/nat/primrec.mojo
Output
Rows: 36 then 41 arithmetic declarations
Induction: nat.induct
Zero: demo.double(nat.zero) = nat.zero
Suc: ∀demo.double.n.1:nat.nat. demo.double(nat.suc(demo.double.n.1)) = (λn:nat.nat. λr:nat.nat. nat.suc(nat.suc(r)))(demo.double.n.1)(demo.double(demo.double.n.1))
Axioms in double.zero_eq: 2
Axioms in double.zero_eq: 2 confirms that the defining equations depend only
on the pre-existing classical profile axioms (choice, infinity, funext);
neither build_nat nor define_primrec introduces new axioms.
Using induction and defining equations in proofs¶
- Cite
s.fact(nat.get(NatName.INDUCT).name)to prove universal statements \(\forall n:\text{nat}.\ P(n)\) from base and inductive steps. - Cite
double.zero_eqanddouble.suc_eqinrworcalcproofs (for example, to prove \(\forall n.\ \mathrm{double}(n) = n + n\)).
See natural numbers theory and the
judgment_nat API reference.
Exercise¶
What base and step define \(\mathrm{triple}(0) = 0\),
\(\mathrm{triple}(\text{suc}(n)) = \text{suc}(\text{suc}(\text{suc}(\mathrm{triple}(n))))\)?
Answer
base = zero and step = lambda_(n, lambda_(r, suc(suc(suc(r))))).