In an earlier post the monoid laws showed up as an OCaml fold. Here they are again as theorems in Lean 4 — and this time the proofs are interactive.
Each proof below is rendered with Alectryon driven by LeanInk. Hover over a tactic (or the goal markers in the gutter) to see the proof state at that point: the hypotheses in context and the goal that remains.
/-! # The monoid laws for `Nat`, proved in Lean The proofs below are processed by LeanInk + Alectryon, so each tactic step shows the intermediate proof state when you hover or step through it. -/ /- Associativity of addition. We go by induction on the last argument. -/ theorem nat_add_assoc (a b c : Nat) : (a + b) + c = a + (b + c) :=Goals accomplished! 🐙a, b, c: Nata + b + c = a + (b + c)a, b, c: Nata + b + c = a + (b + c)Goals accomplished! 🐙a, b, c: Nata + b + c = a + (b + c)Goals accomplished! 🐙a, b, c: Nata + b + c = a + (b + c)a, b, c: Nat
ih: a + b + c = a + (b + c)
succa + b + (c + 1) = a + (b + (c + 1))a, b, c: Nata + b + c = a + (b + c)/- `0` is a left identity. (It is a right identity definitionally.) -/ theorem nat_zero_add (a : Nat) : 0 + a = a :=a, b, c: Nat
ih: a + b + c = a + (b + c)
succa + b + (c + 1) = a + (b + (c + 1))Goals accomplished! 🐙a: Nat0 + a = aa: Nat0 + a = aGoals accomplished! 🐙a: Nat0 + a = aGoals accomplished! 🐙a: Nat0 + a = aa: Nat
ih: 0 + a = a
succ0 + (a + 1) = a + 1a: Nat0 + a = aa: Nat
ih: 0 + a = a
succ0 + (a + 1) = a + 1
The associativity proof goes by induction on c: the zero case closes by rfl, and the succ case rewrites with the inductive hypothesis. Step through it above to watch the goal shrink.