1-- Greatest common divisors and Euclidean transformations2-- 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.GCD8import Divisibility910-- Greatest in the divisibility order. This includes IsGCD(0, 0, 0).11-- Existence and an executable algorithm are separate from this specification.12predicate IsGCD (a b g : Nat) :=13 g ∣ a ∧ g ∣ b ∧ (∀ d, Nat(d) → d ∣ a → d ∣ b → d ∣ g)1415theorem IsGCD.divides_left (a b g : Nat)16requires gcd : IsGCD(a, b, g)17shows g ∣ a18proof19simp [IsGCD]2021theorem IsGCD.divides_right (a b g : Nat)22requires gcd : IsGCD(a, b, g)23shows g ∣ b24proof25simp [IsGCD]2627theorem IsGCD.greatest (a b g d : Nat)28requires gcd : IsGCD(a, b, g)29requiresleft : d ∣ a30requiresright : d ∣ b31shows d ∣ g32proof33simp [IsGCD]3435theorem IsGCD.intro (a b g : Nat)36requiresleft : g ∣ a37requiresright : g ∣ b38requires greatest : ∀ d, Nat(d) → d ∣ a → d ∣ b → d ∣ g39shows IsGCD(a, b, g)40proof41simp [IsGCD]4243theorem IsGCD.unique (a b g h : Nat)44requires first : IsGCD(a, b, g)45requires second : IsGCD(a, b, h)46shows g = h47proof48have ga := IsGCD.divides_left at a, b, g with*49have gb := IsGCD.divides_right at a, b, g with*50have ha := IsGCD.divides_left at a, b, h with*51have hb := IsGCD.divides_right at a, b, h with*52have gh := IsGCD.greatest at a, b, h, g with*53have hg := IsGCD.greatest at a, b, g, h with*54exact divides.antisymmetric with*5556theorem IsGCD.symmetric (a b g : Nat)57requires gcd : IsGCD(a, b, g)58shows IsGCD(b, a, g)59proof60haveleft:= IsGCD.divides_left with*61haveright:= IsGCD.divides_right with*62apply IsGCD.intro withright, left63intros d dNat db da64exact IsGCD.greatest with*6566theorem IsGCD.zero_right (a : Nat)67shows IsGCD(a, 0, a)68proof69haveself:= divides.reflexive with*70have zero := divides.zero with*71apply IsGCD.intro withself, zero72intros d dNat da dz73exact da7475theorem IsGCD.zero_left (a : Nat)76shows IsGCD(0, a, a)77proof78haveright:= IsGCD.zero_right with*79exact IsGCD.symmetric with*8081theorem IsGCD.of_divides (a b : Nat)82requires divides : a ∣ b83shows IsGCD(a, b, a)84proof85haveself:= divides.reflexive at a with*86apply IsGCD.intro withself, divides87intros d dNat da db88exact da8990-- A single invariant covers subtraction and quotient/remainder algorithms.91-- No remainder bound is needed for preservation, only for termination.92theorem IsGCD.euclidean_forward (a b q r g : Nat)93requires equation : a = b * q + r94requires gcd : IsGCD(a, b, g)95shows IsGCD(b, r, g)96proof97have ga := IsGCD.divides_left with*98have gb := IsGCD.divides_right with*99have equivalence := divides.remainder_iff with*100have gr : g ∣ r101 . tauto102apply IsGCD.intro with gb, gr103intros d dNat db dr104have commonDivisor := divides.remainder_iff at d, a, b, q, r with*105have da : d ∣ a106 . tauto107exact IsGCD.greatest with*108109theorem IsGCD.euclidean_backward (a b q r g : Nat)110requires equation : a = b * q + r111requires gcd : IsGCD(b, r, g)112shows IsGCD(a, b, g)113proof114have gb := IsGCD.divides_left with*115have gr := IsGCD.divides_right with*116have equivalence := divides.remainder_iff with*117have ga : g ∣ a118 . tauto119apply IsGCD.intro with ga, gb120intros d dNat da db121have commonDivisor := divides.remainder_iff at d, a, b, q, r with*122have dr : d ∣ r123 . tauto124exact IsGCD.greatest with*125126theorem IsGCD.euclidean_iff (a b q r g : Nat)127requires equation : a = b * q + r128shows IsGCD(a, b, g) ↔ IsGCD(b, r, g)129proof130constructor131 . intro gcd132exact IsGCD.euclidean_forward with*133 . intro gcd134exact IsGCD.euclidean_backward with*135136theorem IsGCD.add_right_iff (a b g : Nat)137shows IsGCD(a + b, b, g) ↔ IsGCD(a, b, g)138proof139have equation : a + b = b *1+ a140 . ring141have step := IsGCD.euclidean_iff at a + b, b, 1, a, g with*142constructor143 . intro gcd144have reversed : IsGCD(b, a, g)145 . tauto146exact IsGCD.symmetric with*147 . intro gcd148have reversed := IsGCD.symmetric with*149tauto150