module Data.Nat.Divisible.GCD where
Greatest common divisorsð
The greatest common divisor of a pair of natural numbers is the largest number which divides them both. The definition we use is slightly unorthodox, since it requires only that the GCD be the greatest in the divisibility ordering, but the divisibility ordering is finer than the usual ordering, so this will turn out to imply they are greatest in magnitude, as well.
We start by defining what it means for a number to be a GCD â that is, we start by defining the graph of the function Only after will we show that such a function exists.
record is-gcd (x y : Nat) (gcd : Nat) : Type where field gcd-â£l : gcd ⣠x gcd-â£r : gcd ⣠y greatest : â {g'} â g' ⣠x â g' ⣠y â g' ⣠gcd open is-gcd is-gcd-is-prop : â {x y z} â is-prop (is-gcd x y z) is-gcd-is-prop p q i .gcd-â£l = â£-is-prop _ _ (p .gcd-â£l) (q .gcd-â£l) i is-gcd-is-prop p q i .gcd-â£r = â£-is-prop _ _ (p .gcd-â£r) (q .gcd-â£r) i is-gcd-is-prop p q i .greatest a b = â£-is-prop _ _ (p .greatest a b) (q .greatest a b) i instance H-Level-is-gcd : â {x y z n} â H-Level (is-gcd x y z) (suc n) H-Level-is-gcd = prop-instance is-gcd-is-prop GCD : Nat â Nat â Type GCD a b = Σ _ (is-gcd a b) GCD-is-prop : â {a b} â is-prop (GCD a b) GCD-is-prop (_ , p) (_ , q) = Σ-prop-path! $ â£-antisym (q .greatest (p .gcd-â£l) (p .gcd-â£r)) (p .greatest (gcd-â£l q) (gcd-â£r q)) GCD-magnitude : â {x y g : Nat} â is-gcd x y (suc g) â â {g'} â g' ⣠x â g' ⣠y â g' †suc g GCD-magnitude gcd α β = mâ£snâmâ€sn (gcd .greatest α β)
The following observations may seem trivial, but it will come in super handy later: If then the GCD of and is Similarly, the GCD function is âsymmetricâ, meaning
dividesâGCD : â {x y} â x ⣠y â GCD x y dividesâGCD {x} {y} w = x , gcd where gcd : is-gcd x y x gcd .gcd-â£l = â£-refl gcd .gcd-â£r = w gcd .greatest g'â£x g'â£y = g'â£x GCD-sym : â {x y} â GCD x y â GCD y x GCD-sym w .fst = w .fst GCD-sym w .snd .gcd-â£l = w .snd .gcd-â£r GCD-sym w .snd .gcd-â£r = w .snd .gcd-â£l GCD-sym w .snd .greatest g'â£y g'â£x = w .snd .greatest g'â£x g'â£y
Euclidâs algorithmð
To compute greatest common divisors, we use the familiar (recursive) Euclidean algorithm: and otherwise. The difficulty comes in when we want not only to compute the numbers (in which case the definition above is perfectly cromulent), but also to show that they are greatest common divisors.
module Euclid where private variable n m k d d' : Nat
The base case can be established using our existing functions:
is-gcd-0 : is-gcd n 0 n is-gcd-0 .gcd-â£l = â£-refl is-gcd-0 .gcd-â£r = â£-zero is-gcd-0 .greatest g'â£n gâ£n = g'â£n
The inductive step is a bit more complicated. We first have to establish (using the most tedious of arithmetic properties) the following properties of the divisibility relation:
- If is nonzero, and then
- If is nonzero, and then
These two facts, incarnated in the following helper lemmas lemâ
and lemâ
, will allow us to compute
the inductive step for GCD.
private lemâ : fibre (_* d) (suc n) â fibre (_* d) (m % suc n) â fibre (_* d) m lemâ {d = d} {n} {m} (câ , p) (câ , q) = dm.q * câ + câ , r where module dm = DivMod (divide-pos m (suc n)) renaming (quot to q ; rem to r) r = (dm.q * câ + câ) * d â¡âš *-distrib-+r (dm.q * câ) _ _ â©â¡ dm.q * câ * d + â câ * d â â¡âš ap! q â©â¡ â dm.q * câ * d â + dm.r â¡âš ap! (*-associative dm.q câ d) â©â¡ dm.q * â câ * d â + dm.r â¡âš ap! p â©â¡ dm.q * suc n + dm.r â¡Ëâš is-divmod m (suc n) â©â¡Ë m â lemâ : fibre (_* d) (suc n) â fibre (_* d) m â fibre (_* d) (m % suc n) lemâ {d = d} {n} {m} (câ , p) (câ , q) = câ - dm.q * câ , r where module dm = DivMod (divide-pos m (suc n)) renaming (quot to q ; rem to r) r = (câ - dm.q * câ) * d â¡âš monus-distribr câ (dm.q * câ) d â©â¡ câ * d - â dm.q * câ * d â â¡âš ap! (*-associative dm.q câ d â ap (dm.q *_) p) â©â¡ â câ * d â - dm.q * suc n â¡âš ap! (q â is-divmod m (suc n)) â©â¡ â dm.q * suc n + dm.r â - dm.q * suc n â¡âš ap! (+-commutative (dm.q * suc n) dm.r) â©â¡ (dm.r + dm.q * suc n) - dm.q * suc n â¡âš monus-cancelr dm.r 0 (dm.q * suc n) â©â¡ dm.r â
That step is put together here: It says that, if ( is nonzero and) then too â so that it will suffice to compute the former if we want the latter.
is-gcd-step : is-gcd (suc n) (m % suc n) d â is-gcd m (suc n) d is-gcd-step x .gcd-â£l = fibreâ⣠(lemâ (â£âfibre (x .gcd-â£l)) (â£âfibre (x .gcd-â£r))) is-gcd-step x .gcd-â£r = x .gcd-â£l is-gcd-step x .greatest g'â£m g'â£sucn = x .greatest g'â£sucn (fibreâ⣠(lemâ (â£âfibre g'â£sucn) (â£âfibre g'â£m)))
Actually putting this together is a bit indirect, since we are not performing structural induction: Agda canât see that the argument is less than We can, however, turn this into well-founded recursion, which demands only that the recursive call be less (in the sense of than the original input.
euclid-< : â y x â x < y â GCD y x euclid-< = Wf-induction _ <-wf _ λ where x rec zero p â x , is-gcd-0 x rec (suc y) p â let (d , step) = rec (suc y) p (x % suc y) (x%y<y x (suc y)) in d , is-gcd-step step
With a handy wrapper to put the arguments in the order our induction
worker euclid-<
expects, we can
create the euclid
function, together with
its numerical component gcd
, and a proof that the graph of
gcd
is indeed is-gcd
.
euclid : â x y â GCD x y euclid x y with â€-split y x ... | inl y<x = euclid-< x y y<x ... | inr (inl x<y) = GCD-sym (euclid-< y x x<y) ... | inr (inr y=x) = dividesâGCD (subst (x â£_) (sym y=x) â£-refl) gcd : Nat â Nat â Nat gcd x y = Euclid.euclid x y .fst is-gcd-graphs-gcd : â {m n d} â is-gcd m n d â (gcd m n â¡ d) is-gcd-graphs-gcd {m = m} {n} {d} = prop-ext! (λ x â ap fst $ GCD-is-prop (gcd m n , Euclid.euclid m n .snd) (d , x)) (λ p â subst (is-gcd m n) p (Euclid.euclid m n .snd))
is-gcd-factor : â x y n k .⊠_ : Positive k ⊠â is-gcd (x * k) (y * k) (n * k) â is-gcd x y n is-gcd-factor x y n k p .gcd-â£l with (q , α) â â£âfibre (p .gcd-â£l) = fibreâ⣠(q , *-injr k (q * n) x (*-associative q n k â α)) is-gcd-factor x y n k p .gcd-â£r with (q , α) â â£âfibre (p .gcd-â£r) = fibreâ⣠(q , *-injr k (q * n) y (*-associative q n k â α)) is-gcd-factor x y n k p .greatest {g'} α β with (q , α) â â£âfibre α | (r , β) â â£âfibre β = â£-*-cancelr {n = k} (p .greatest (fibreâ⣠(q , sym (*-associative q g' k) â ap (_* k) α)) (fibreâ⣠(r , sym (*-associative r g' k) â ap (_* k) β))) gcd-factor : â x y k â gcd (x * k) (y * k) â¡ gcd x y * k gcd-factor x y zero = apâ gcd (*-zeror x) (*-zeror y) â sym (*-zeror (gcd x y)) gcd-factor x y k@(suc _) = sym (kâ£gcd .snd) â ap (_* k) (sym (Equiv.to is-gcd-graphs-gcd d')) where kâ£gcd = Euclid.euclid (x * k) (y * k) .snd .greatest {k} (â£-*r {k} {x}) (â£-*r {k} {y}) d' : is-gcd x y (kâ£gcd .fst) d' = is-gcd-factor x y (kâ£gcd .fst) k (subst (is-gcd _ _) (sym (kâ£gcd .snd)) (Euclid.euclid (x * k) (y * k) .snd))
Euclidâs lemmað
|-*-coprime-cancel : â n a b .⊠_ : Positive n ⊠.⊠_ : Positive a ⊠â n ⣠a * b â is-gcd n a 1 â n ⣠b |-*-coprime-cancel n a b div coprime = done where E : Nat â Prop lzero E x = el! (0 < x à n ⣠x * b) has : Σ[ x â Nat ] ((x â E) à (â y â (0 < y) à (n ⣠y * b) â x †y)) has = â-well-ordered {P = E} (λ _ â auto) (inc (a , recover auto , div)) instance _ : Positive (has .fst) _ = has .snd .fst .fst step : â x â x â E â has .fst ⣠x step x (xe , d) with divmod q r α β â divide-pos x (has .fst) = let d' : n ⣠(q * has .fst * b + r * b) d' = subst (n â£_) (ap (_* b) (recover α) â *-distrib-+r (q * has .fst) r b) d d'' : n ⣠r * b d'' = â£-+-cancel {n} {q * has .fst * b} {r * b} (subst (n â£_) (sym (*-associative q (has .fst) b)) (|-*l-pres {a = q} (has .snd .fst .snd))) d' in case r â¡? 0 of λ where (yes p) â fibreâ⣠(q , sym (recover α â ap (q * has .fst +_) p â +-zeror (q * has .fst))) (no ¬p) â absurd (<-irrefl (â€-antisym (â€-trans â€-ascend (recover β)) (has .snd .snd r (nonzeroâpositive ¬p , d''))) (recover β)) almost : has .fst â¡ 1 almost = â£-1 $ coprime .greatest {has .fst} (step n (recover auto , â£-*l)) (step a (recover auto , div)) done : n ⣠b done = subst (n â£_) (ap (_* b) almost â *-onel b) (has .snd .fst .snd)