1-- The square root of two is irrational2-- 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.NumberTheory.RootTwo8import Nat.Parity9import Nat.Least1011-- The square of an odd number is odd.12theorem Odd.square (n : Set)13requires odd : Odd(n)14shows Odd(n * n)15proof16obtain⟨k, kMember, equation⟩:= odd17have kNat := nat.omega.backward with kMember18have m : Nat(add(add(mul(k, k), mul(k, k)), add(k, k))) bytypecheck19witness add(add(mul(k, k), mul(k, k)), add(k, k))20constructor21 . exact nat.omega.forward at add(add(mul(k, k), mul(k, k)), add(k, k)) with m22 . rw equation23ring2425-- A number whose square is even is even.26theorem Even.of_square (n : Nat)27requires even : Even(n * n)28shows Even(n)29proof30have shape := parity.exhaustive with*31cases shape with evenN | oddN32 . exact evenN33 . exfalso34have oddSquare := Odd.square with oddN35exact Odd.not_even with*, even3637-- The naturals p with p² = 2q² for some positive q.38def rootTwoNumerators : Set:=39 { p ∈ ℕ |∃ q ∈ ℕ, q ≠0∧ p * p =2* (q * q) }4041theorem rootTwoNumerators.subset_naturals42shows rootTwoNumerators ⊆ ℕ43proof44intros p member45obtain⟨natural, rest⟩:= rootTwoNumerators.membership.forward with member46exact natural4748-- From p² = 2q² with q positive, p is even, p = 2j, and then q² = 2j² with49-- j positive and q < p. So q is a smaller member of the same set.50theorem rootTwoNumerators.descent (p q : Nat)51requires positive : q ≠052requires equation : p * p =2* (q * q)53shows q ∈ rootTwoNumerators ∧ q < p54proof55have evenSquare : Even(p * p)56witness q * q57constructor58 . have qq : Nat(q * q)59typecheck60exact nat.omega.forward with qq61 . rw equation62exact mul.two_left with*63have evenP := Even.of_square with*64obtain⟨j, jMember, pShape⟩:= evenP65have jNat := nat.omega.backward with jMember66have halved : q * q =2* (j * j)67have doubled : 2* (q * q) =2* (2* (j * j))68rw [← equation, pShape]69simp [mul.two_left]70ring71have twoNonzero : 2≠072intro bad73have positiveTwo : 0 < 2byarith74rw bad at positiveTwo75exact Less.irreflexive with*, positiveTwo76exact mul.left_cancel_positive with*77have jPositive : j ≠078intro jZero79rw jZero at halved80have qq : q * q =081rw halved82compute83havesplit:= mul.eq_zero with*, qq84casessplitwithleft|right85 . exact positive withleft86 . exact positive withright87have smaller : q < p88have shape := Less.trichotomy at q, p with*89rcases shape with less | equal | greater90 . exact less91 . exfalso92rw [equal, pShape] at halved93have two := mul.two_left at j * j with*94rw two at halved95have expand : (j + j) * (j + j) = (j * j + j * j) + (j * j + j * j) byring96rw expand at halved97have upper : j * j ≤0byarith98have lower := Grace.Cumulus.Nat.Order.LessEqual.zero at j * j with*99have jj := LessEqual.antisymmetric at j * j, 0with*100havesplit:= mul.eq_zero with*, jj101casessplitwith first | second102 . exact jPositive with first103 . exact jPositive with second104 . exfalso105have le := Less.implies_LessEqual with*106have first := LessEqual.mul_left at p, q, p with*107have second := LessEqual.mul_left at p, q, q with*108have commuted := mul.commutative at q, p with*109have two := mul.two_left at q * q with*110rw two at equation111have upper : q * q ≤0byarith112have lower := Grace.Cumulus.Nat.Order.LessEqual.zero at q * q with*113have zero := LessEqual.antisymmetric at q * q, 0with*114havesplit:= mul.eq_zero with*, zero115casessplitwithleft|right116 . exact positive withleft117 . exact positive withright118constructor119 . apply rootTwoNumerators.membership.backward at q120constructor121 . exact nat.omega.forward at q withassumption122 . witness j123refine⟨jMember, jPositive, halved⟩124 . exact smaller125126-- No natural p and positive q satisfy p² = 2q²: the least such p would give127-- a smaller one.128theorem rootTwo.irrational (p q : Nat)129requires positive : q ≠0130shows p * p ≠2* (q * q)131proof132intro equation133have member : p ∈ rootTwoNumerators134apply rootTwoNumerators.membership.backward135constructor136 . exact nat.omega.forward at p withassumption137 . witness q138have qMember := nat.omega.forward at q withassumption139refine⟨qMember, positive, equation⟩140have inhabited : Nonempty(rootTwoNumerators) :=⟨p, member⟩141have naturals := rootTwoNumerators.subset_naturals142have least := leastNatural.member at rootTwoNumerators with*143obtain⟨leastMember, r, rMember, rPositive, rEquation⟩:= rootTwoNumerators.membership.forward with least144have leastNat := nat.omega.backward with leastMember145have rNat := nat.omega.backward with rMember146have descent := rootTwoNumerators.descent at leastNatural(rootTwoNumerators), r with*147rcases descent with⟨rIn, rSmaller⟩148have bound := leastNatural.least at rootTwoNumerators, r with*149have bad : r < r byarith150exact Less.irreflexive with*, bad151