1-- The principle of mathematical induction, proved from the least counterexample2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Nat.Induction8import Least9import Foundation1011-- The natural numbers that a set S leaves out.12def counterexamples (S : Set) : Set:= { n ∈ ℕ |¬(n ∈ S) }1314theorem counterexamples.subset_naturals (S : Set)15shows counterexamples(S) ⊆ ℕ16proof17simp1819-- A set that contains 0 and the successor of each of its members contains20-- every natural number. If it left some number out, the least such number21-- could not be 0, so it would be the successor of a number inside the set,22-- which puts it inside too.23theorem natural.induction (S : Set)24requires base : 0∈ S25requires step : ∀ n ∈ ℕ, n ∈ S → n +1∈ S26shows∀ n ∈ ℕ, n ∈ S27proof28intros n nMember29by_contra absent30have nIn : n ∈ counterexamples(S) bysimp31have inhabited : Nonempty(counterexamples(S)) :=⟨n, nIn⟩32have naturals := counterexamples.subset_naturals at S33have least := leastNatural.member with*34obtain⟨leastMember, leastAbsent⟩:= counterexamples.membership.forward with least35have leastNat := nat.omega.backward with leastMember36have shape := natural.zero_or_successor with leastNat37cases shape with zero | successor38 . rw zero at leastAbsent39exact leastAbsent with base40 . obtain⟨k, kNat, equation⟩:= successor41have kMember := nat.omega.forward with kNat42have kIn : k ∈ S43by_contra kAbsent44have kCounter : k ∈ counterexamples(S) bysimp [counterexamples.membership]45have bound := leastNatural.least at counterexamples(S), k with*46rw equation at bound47arith48have next := step with kMember, kIn49have bound := add.right_one at k50rw [bound, ← equation] at next51exact leastAbsent with next52