The Monoid Laws, Proved in Lean

Interactive Lean proofs with hoverable goal states, via LeanInk + Alectryon.
lean
type-theory
Author

Michael Thomas

Published

May 30, 2026

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: Nat

a + b + c = a + (b + c)
a, b, c: Nat

a + b + c = a + (b + c)

Goals accomplished! 🐙
a, b, c: Nat

a + b + c = a + (b + c)

Goals accomplished! 🐙
a, b, c: Nat

a + b + c = a + (b + c)
a, b, c: Nat
ih: a + b + c = a + (b + c)

succ
a + b + (c + 1) = a + (b + (c + 1))
a, b, c: Nat

a + b + c = a + (b + c)
Error: tactic 'simp' failed, nested error: maximum recursion depth has been reached use `set_option maxRecDepth <num>` to increase limit use `set_option diagnostics true` to get diagnostic information
a, b, c: Nat
ih: a + b + c = a + (b + c)

succ
a + b + (c + 1) = a + (b + (c + 1))
/- `0` is a left identity. (It is a right identity definitionally.) -/ theorem nat_zero_add (a : Nat) : 0 + a = a :=

Goals accomplished! 🐙
a: Nat

0 + a = a
a: Nat

0 + a = a

Goals accomplished! 🐙
a: Nat

0 + a = a

Goals accomplished! 🐙
a: Nat

0 + a = a
a: Nat
ih: 0 + a = a

succ
0 + (a + 1) = a + 1
a: Nat

0 + a = a
Error: tactic 'simp' failed, nested error: maximum recursion depth has been reached use `set_option maxRecDepth <num>` to increase limit use `set_option diagnostics true` to get diagnostic information
a: Nat
ih: 0 + a = a

succ
0 + (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.