Debian Codegraph @

src/Data/Nat/GCD.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Greatest common divisor
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Nat.GCD where
10
11 open import Data.Nat.Base
12 open import Data.Nat.Divisibility
13 open import Data.Nat.DivMod
14 open import Data.Nat.GCD.Lemmas
15 open import Data.Nat.Properties
16 open import Data.Nat.Induction
17 using (Acc; acc; <′-Rec; <′-recBuilder; <-wellFounded-fast)
18 open import Data.Product.Base
19 using (_×_; _,_; proj₂; proj₁; swap; uncurry′; ∃; map)
20 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
21 open import Function.Base using (_$_; _∘_)
22 open import Induction using (build)
23 open import Induction.Lexicographic using (_⊗_; [_⊗_])
24 open import Relation.Binary.Definitions using (tri<; tri>; tri≈; Symmetric)
25 open import Relation.Binary.PropositionalEquality.Core as ≡
26 using (_≡_; _≢_; subst; cong)
27 open import Relation.Binary.PropositionalEquality.Properties
28 using (module ≡-Reasoning)
29 open import Relation.Nullary.Decidable.Core using (Dec)
30 open import Relation.Nullary.Negation.Core using (contradiction)
31 import Relation.Nullary.Decidable as Dec
32
33 open import Algebra.Definitions {A = ℕ} _≡_ as Algebra
34 using (Associative; Commutative; LeftIdentity; RightIdentity; LeftZero; RightZero; Zero)
35
36 ------------------------------------------------------------------------
37 -- Definition
38
39 -- Calculated via Euclid's algorithm. In order to show progress,
40 -- avoiding the initial step where the first argument may increase, it
41 -- is necessary to first define a version `gcd′` which assumes that the
42 -- second argument is strictly smaller than the first. The full `gcd`
43 -- function then compares the two arguments and applies `gcd′`
44 -- accordingly.
45
46 gcd′ : ∀ m n → Acc _<_ m → n < m → ℕ
47 gcd′ m zero _ _ = m
48 gcd′ m n@(suc _) (acc rec) n<m = gcd′ n (m % n) (rec n<m) (m%n<n m n)
49
50 gcd : ℕ → ℕ → ℕ
51 gcd m n with <-cmp m n
52 ... | tri< m<n _ _ = gcd′ n m (<-wellFounded-fast n) m<n
53 ... | tri≈ _ _ _ = m
54 ... | tri> _ _ n<m = gcd′ m n (<-wellFounded-fast m) n<m
55
56 ------------------------------------------------------------------------
57 -- Core properties of gcd′
58
59 gcd′[m,n]∣m,n : ∀ {m n} rec n<m → gcd′ m n rec n<m ∣ m × gcd′ m n rec n<m ∣ n
60 gcd′[m,n]∣m,n {m} {zero} rec n<m = ∣-refl , m ∣0
61 gcd′[m,n]∣m,n {m} {n@(suc _)} (acc rec) n<m
62 with gcd∣n , gcd∣m%n ← gcd′[m,n]∣m,n (rec n<m) (m%n<n m n)
63 = ∣n∣m%n⇒∣m gcd∣n gcd∣m%n , gcd∣n
64
65 gcd′-greatest : ∀ {m n c} rec n<m → c ∣ m → c ∣ n → c ∣ gcd′ m n rec n<m
66 gcd′-greatest {m} {zero} rec n<m c∣m c∣n = c∣m
67 gcd′-greatest {m} {n@(suc _)} (acc rec) n<m c∣m c∣n =
68 gcd′-greatest (rec n<m) (m%n<n m n) c∣n (%-presˡ-∣ c∣m c∣n)
69
70 ------------------------------------------------------------------------
71 -- Core properties of gcd
72
73 gcd[m,n]∣m : ∀ m n → gcd m n ∣ m
74 gcd[m,n]∣m m n with <-cmp m n
75 ... | tri< n<m _ _ = proj₂ (gcd′[m,n]∣m,n {n} {m} _ _)
76 ... | tri≈ _ _ _ = ∣-refl
77 ... | tri> _ _ m<n = proj₁ (gcd′[m,n]∣m,n {m} {n} _ _)
78
79 gcd[m,n]∣n : ∀ m n → gcd m n ∣ n
80 gcd[m,n]∣n m n with <-cmp m n
81 ... | tri< n<m _ _ = proj₁ (gcd′[m,n]∣m,n {n} {m} _ _)
82 ... | tri≈ _ ≡.refl _ = ∣-refl
83 ... | tri> _ _ m<n = proj₂ (gcd′[m,n]∣m,n {m} {n} _ _)
84
85 gcd-greatest : ∀ {m n c} → c ∣ m → c ∣ n → c ∣ gcd m n
86 gcd-greatest {m} {n} c∣m c∣n with <-cmp m n
87 ... | tri< n<m _ _ = gcd′-greatest _ _ c∣n c∣m
88 ... | tri≈ _ _ _ = c∣m
89 ... | tri> _ _ m<n = gcd′-greatest _ _ c∣m c∣n
90
91 ------------------------------------------------------------------------
92 -- Other properties
93
94 -- Note that all other properties of `gcd` should be inferable from the
95 -- 3 core properties above.
96
97 gcd[0,0]≡0 : gcd 0 0 ≡ 0
98 gcd[0,0]≡0 = ∣-antisym (gcd 0 0 ∣0) (gcd-greatest (0 ∣0) (0 ∣0))
99
100 gcd[m,n]≢0 : ∀ m n → m ≢ 0 ⊎ n ≢ 0 → gcd m n ≢ 0
101 gcd[m,n]≢0 m n (inj₁ m≢0) eq = m≢0 (0∣⇒≡0 (subst (_∣ m) eq (gcd[m,n]∣m m n)))
102 gcd[m,n]≢0 m n (inj₂ n≢0) eq = n≢0 (0∣⇒≡0 (subst (_∣ n) eq (gcd[m,n]∣n m n)))
103
104 gcd[m,n]≡0⇒m≡0 : ∀ {m n} → gcd m n ≡ 0 → m ≡ 0
105 gcd[m,n]≡0⇒m≡0 {zero} {n} eq = ≡.refl
106 gcd[m,n]≡0⇒m≡0 {suc m} {n} eq = contradiction eq (gcd[m,n]≢0 (suc m) n (inj₁ λ()))
107
108 gcd[m,n]≡0⇒n≡0 : ∀ m {n} → gcd m n ≡ 0 → n ≡ 0
109 gcd[m,n]≡0⇒n≡0 m {zero} eq = ≡.refl
110 gcd[m,n]≡0⇒n≡0 m {suc n} eq = contradiction eq (gcd[m,n]≢0 m (suc n) (inj₂ λ()))
111
112 gcd-comm : Commutative gcd
113 gcd-comm m n = ∣-antisym
114 (gcd-greatest (gcd[m,n]∣n m n) (gcd[m,n]∣m m n))
115 (gcd-greatest (gcd[m,n]∣n n m) (gcd[m,n]∣m n m))
116
117 gcd-assoc : Associative gcd
118 gcd-assoc m n p = ∣-antisym
119 (gcd-greatest gcd[gcd[m,n],p]|m (gcd-greatest gcd[gcd[m,n],p]∣n gcd[gcd[m,n],p]∣p))
120 (gcd-greatest (gcd-greatest gcd[m,gcd[n,p]]∣m gcd[m,gcd[n,p]]∣n) gcd[m,gcd[n,p]]∣p)
121 where
122 open ∣-Reasoning
123 gcd[gcd[m,n],p]|m = begin
124 gcd (gcd m n) p ∣⟨ gcd[m,n]∣m (gcd m n) p ⟩
125 gcd m n ∣⟨ gcd[m,n]∣m m n ⟩
126 m ∎
127 gcd[gcd[m,n],p]∣n = begin
128 gcd (gcd m n) p ∣⟨ gcd[m,n]∣m (gcd m n) p ⟩
129 gcd m n ∣⟨ gcd[m,n]∣n m n ⟩
130 n ∎
131 gcd[gcd[m,n],p]∣p = gcd[m,n]∣n (gcd m n) p
132 gcd[m,gcd[n,p]]∣m = gcd[m,n]∣m m (gcd n p)
133 gcd[m,gcd[n,p]]∣n = begin
134 gcd m (gcd n p) ∣⟨ gcd[m,n]∣n m (gcd n p) ⟩
135 gcd n p ∣⟨ gcd[m,n]∣m n p ⟩
136 n ∎
137 gcd[m,gcd[n,p]]∣p = begin
138 gcd m (gcd n p) ∣⟨ gcd[m,n]∣n m (gcd n p) ⟩
139 gcd n p ∣⟨ gcd[m,n]∣n n p ⟩
140 p ∎
141
142 gcd-identityˡ : LeftIdentity 0 gcd
143 gcd-identityˡ zero = ≡.refl
144 gcd-identityˡ (suc _) = ≡.refl
145
146 gcd-identityʳ : RightIdentity 0 gcd
147 gcd-identityʳ zero = ≡.refl
148 gcd-identityʳ (suc _) = ≡.refl
149
150 gcd-identity : Algebra.Identity 0 gcd
151 gcd-identity = gcd-identityˡ , gcd-identityʳ
152
153 gcd-zeroˡ : LeftZero 1 gcd
154 gcd-zeroˡ n = ∣-antisym gcd[1,n]∣1 1∣gcd[1,n]
155 where
156 gcd[1,n]∣1 = gcd[m,n]∣m 1 n
157 1∣gcd[1,n] = 1∣ gcd 1 n
158
159 gcd-zeroʳ : RightZero 1 gcd
160 gcd-zeroʳ n = ∣-antisym gcd[n,1]∣1 1∣gcd[n,1]
161 where
162 gcd[n,1]∣1 = gcd[m,n]∣n n 1
163 1∣gcd[n,1] = 1∣ gcd n 1
164
165 gcd-zero : Zero 1 gcd
166 gcd-zero = gcd-zeroˡ , gcd-zeroʳ
167
168 gcd-universality : ∀ {m n g} →
169 (∀ {d} → d ∣ m × d ∣ n → d ∣ g) →
170 (∀ {d} → d ∣ g → d ∣ m × d ∣ n) →
171 g ≡ gcd m n
172 gcd-universality {m} {n} forwards backwards with back₁ , back₂ ← backwards ∣-refl
173 = ∣-antisym (gcd-greatest back₁ back₂) (forwards (gcd[m,n]∣m m n , gcd[m,n]∣n m n))
174
175 -- This could be simplified with some nice backwards/forwards reasoning
176 -- after the new function hierarchy is up and running.
177 gcd[cm,cn]/c≡gcd[m,n] : ∀ c m n .{{_ : NonZero c}} → gcd (c * m) (c * n) / c ≡ gcd m n
178 gcd[cm,cn]/c≡gcd[m,n] c m n = gcd-universality forwards backwards
179 where
180 forwards : ∀ {d : ℕ} → d ∣ m × d ∣ n → d ∣ gcd (c * m) (c * n) / c
181 forwards {d} (d∣m , d∣n) = m*n∣o⇒n∣o/m c d (gcd-greatest (*-monoʳ-∣ c d∣m) (*-monoʳ-∣ c d∣n))
182
183 backwards : ∀ {d : ℕ} → d ∣ gcd (c * m) (c * n) / c → d ∣ m × d ∣ n
184 backwards {d} d∣gcd[cm,cn]/c
185 with cd∣gcd[cm,n] ← m∣n/o⇒o*m∣n (gcd-greatest (m∣m*n m) (m∣m*n n)) d∣gcd[cm,cn]/c
186 = *-cancelˡ-∣ c (∣-trans cd∣gcd[cm,n] (gcd[m,n]∣m (c * m) _)) ,
187 *-cancelˡ-∣ c (∣-trans cd∣gcd[cm,n] (gcd[m,n]∣n (c * m) _))
188
189 c*gcd[m,n]≡gcd[cm,cn] : ∀ c m n → c * gcd m n ≡ gcd (c * m) (c * n)
190 c*gcd[m,n]≡gcd[cm,cn] zero m n = ≡.sym gcd[0,0]≡0
191 c*gcd[m,n]≡gcd[cm,cn] c@(suc _) m n = begin
192 c * gcd m n ≡⟨ cong (c *_) (≡.sym (gcd[cm,cn]/c≡gcd[m,n] c m n)) ⟩
193 c * (gcd (c * m) (c * n) / c) ≡⟨ m*[n/m]≡n (gcd-greatest (m∣m*n m) (m∣m*n n)) ⟩
194 gcd (c * m) (c * n) ∎
195 where open ≡-Reasoning
196
197 gcd[m,n]≤n : ∀ m n .{{_ : NonZero n}} → gcd m n ≤ n
198 gcd[m,n]≤n m n = ∣⇒≤ (gcd[m,n]∣n m n)
199
200 n/gcd[m,n]≢0 : ∀ m n .{{_ : NonZero n}} .{{gcd≢0 : NonZero (gcd m n)}} →
201 n / gcd m n ≢ 0
202 n/gcd[m,n]≢0 m n = m<n⇒n≢0 (m≥n⇒m/n>0 {n} {gcd m n} (gcd[m,n]≤n m n))
203
204 m/gcd[m,n]≢0 : ∀ m n .{{_ : NonZero m}} .{{gcd≢0 : NonZero (gcd m n)}} →
205 m / gcd m n ≢ 0
206 m/gcd[m,n]≢0 m n rewrite gcd-comm m n = n/gcd[m,n]≢0 n m
207
208 ------------------------------------------------------------------------
209 -- A formal specification of GCD
210
211 module GCD where
212
213 -- Specification of the greatest common divisor (gcd) of two natural
214 -- numbers.
215
216 record GCD (m n gcd : ℕ) : Set where
217 constructor is
218 field
219 -- The gcd is a common divisor.
220 commonDivisor : gcd ∣ m × gcd ∣ n
221
222 -- All common divisors divide the gcd, i.e. the gcd is the
223 -- greatest common divisor according to the partial order _∣_.
224 greatest : ∀ {d} → d ∣ m × d ∣ n → d ∣ gcd
225
226 gcd∣m : gcd ∣ m
227 gcd∣m = proj₁ commonDivisor
228
229 gcd∣n : gcd ∣ n
230 gcd∣n = proj₂ commonDivisor
231
232 open GCD public
233
234
235 -- The gcd is unique.
236
237 unique : ∀ {d₁ d₂ m n} → GCD m n d₁ → GCD m n d₂ → d₁ ≡ d₂
238 unique d₁ d₂ = ∣-antisym (GCD.greatest d₂ (GCD.commonDivisor d₁))
239 (GCD.greatest d₁ (GCD.commonDivisor d₂))
240
241 -- The gcd relation is "symmetric".
242
243 sym : ∀ {d m n} → GCD m n d → GCD n m d
244 sym g = is (swap $ GCD.commonDivisor g) (GCD.greatest g ∘ swap)
245
246 -- The gcd relation is "reflexive".
247
248 refl : ∀ {n} → GCD n n n
249 refl = is (∣-refl , ∣-refl) proj₁
250
251 -- The GCD of 0 and n is n.
252
253 base : ∀ {n} → GCD 0 n n
254 base {n} = is (n ∣0 , ∣-refl) proj₂
255
256 -- If d is the gcd of n and k, then it is also the gcd of n and
257 -- n + k.
258
259 step : ∀ {n k d} → GCD n k d → GCD n (n + k) d
260 step {n} {k} {d} g with d₁ , d₂ ← GCD.commonDivisor g
261 = is (d₁ , ∣m∣n⇒∣m+n d₁ d₂) greatest′
262 where
263 greatest′ : ∀ {d′} → d′ ∣ n × d′ ∣ n + k → d′ ∣ d
264 greatest′ (d₁ , d₂) = GCD.greatest g (d₁ , ∣m+n∣m⇒∣n d₂ d₁)
265
266 open GCD public using (GCD) hiding (module GCD)
267
268 -- The function gcd fulfils the conditions required of GCD
269
270 gcd-GCD : ∀ m n → GCD m n (gcd m n)
271 gcd-GCD m n = record
272 { commonDivisor = gcd[m,n]∣m m n , gcd[m,n]∣n m n
273 ; greatest = uncurry′ gcd-greatest
274 }
275
276 -- Calculates the gcd of the arguments.
277
278 mkGCD : ∀ m n → ∃ λ d → GCD m n d
279 mkGCD m n = gcd m n , gcd-GCD m n
280
281 -- gcd as a proposition is decidable
282
283 gcd? : (m n d : ℕ) → Dec (GCD m n d)
284 gcd? m n d =
285 Dec.map′ (λ { ≡.refl → gcd-GCD m n }) (GCD.unique (gcd-GCD m n))
286 (gcd m n ≟ d)
287
288 GCD-* : ∀ {m n d c} .{{_ : NonZero c}} → GCD (m * c) (n * c) (d * c) → GCD m n d
289 GCD-* {c = suc _} (GCD.is (dc∣nc , dc∣mc) dc-greatest) =
290 GCD.is (*-cancelʳ-∣ _ dc∣nc , *-cancelʳ-∣ _ dc∣mc)
291 λ {_} → *-cancelʳ-∣ _ ∘ dc-greatest ∘ map (*-monoˡ-∣ _) (*-monoˡ-∣ _)
292
293 GCD-/ : ∀ {m n d c} .{{_ : NonZero c}} → c ∣ m → c ∣ n → c ∣ d →
294 GCD m n d → GCD (m / c) (n / c) (d / c)
295 GCD-/ {m} {n} {d} {c} {{x}}
296 (divides-refl p) (divides-refl q) (divides-refl r) gcd
297 rewrite m*n/n≡m p c {{x}} | m*n/n≡m q c {{x}} | m*n/n≡m r c {{x}} = GCD-* gcd
298
299 GCD-/gcd : ∀ m n .{{_ : NonZero (gcd m n)}} → GCD (m / gcd m n) (n / gcd m n) 1
300 GCD-/gcd m n rewrite ≡.sym (n/n≡1 (gcd m n)) =
301 GCD-/ (gcd[m,n]∣m m n) (gcd[m,n]∣n m n) ∣-refl (gcd-GCD m n)
302
303 ------------------------------------------------------------------------
304 -- Calculating the gcd
305
306 -- The calculation also proves Bézout's lemma.
307
308 module Bézout where
309
310 module Identity where
311
312 -- If m and n have greatest common divisor d, then one of the
313 -- following two equations is satisfied, for some numbers x and y.
314 -- The proof is "lemma" below (Bézout's lemma).
315 --
316 -- (If this identity was stated using integers instead of natural
317 -- numbers, then it would not be necessary to have two equations.)
318
319 data Identity (d m n : ℕ) : Set where
320 +- : (x y : ℕ) (eq : d + y * n ≡ x * m) → Identity d m n
321 -+ : (x y : ℕ) (eq : d + x * m ≡ y * n) → Identity d m n
322
323 -- Various properties about Identity.
324
325 sym : ∀ {d} → Symmetric (Identity d)
326 sym (+- x y eq) = -+ y x eq
327 sym (-+ x y eq) = +- y x eq
328
329 refl : ∀ {d} → Identity d d d
330 refl = -+ 0 1 ≡.refl
331
332 base : ∀ {d} → Identity d 0 d
333 base = -+ 0 1 ≡.refl
334
335 private
336 infixl 7 _⊕_
337
338 _⊕_ : ℕ → ℕ → ℕ
339 m ⊕ n = 1 + m + n
340
341 step : ∀ {d n k} → Identity d n k → Identity d n (n + k)
342 step {d} {n} (+- x y eq) with compare x y
343 ... | equal x = +- (2 * x) x (lem₂ d x eq)
344 ... | less x i = +- (2 * x ⊕ i) (x ⊕ i) (lem₃ d x eq)
345 ... | greater y i = +- (2 * y ⊕ i) y (lem₄ d y n eq)
346 step {d} {n} (-+ x y eq) with compare x y
347 ... | equal x = -+ (2 * x) x (lem₅ d x eq)
348 ... | less x i = -+ (2 * x ⊕ i) (x ⊕ i) (lem₆ d x eq)
349 ... | greater y i = -+ (2 * y ⊕ i) y (lem₇ d y n eq)
350
351 open Identity public using (Identity; +-; -+) hiding (module Identity)
352
353 module Lemma where
354
355 -- This type packs up the gcd, the proof that it is a gcd, and the
356 -- proof that it satisfies Bézout's identity.
357
358 data Lemma (m n : ℕ) : Set where
359 result : (d : ℕ) (g : GCD m n d) (b : Identity d m n) → Lemma m n
360
361 -- Various properties about Lemma.
362
363 sym : Symmetric Lemma
364 sym (result d g b) = result d (GCD.sym g) (Identity.sym b)
365
366 base : ∀ d → Lemma 0 d
367 base d = result d GCD.base Identity.base
368
369 refl : ∀ d → Lemma d d
370 refl d = result d GCD.refl Identity.refl
371
372 stepˡ : ∀ {n k} → Lemma n (suc k) → Lemma n (suc (n + k))
373 stepˡ {n} {k} (result d g b) =
374 subst (Lemma n) (+-suc n k) $
375 result d (GCD.step g) (Identity.step b)
376
377 stepʳ : ∀ {n k} → Lemma (suc k) n → Lemma (suc (n + k)) n
378 stepʳ = sym ∘ stepˡ ∘ sym
379
380 open Lemma public using (Lemma; result) hiding (module Lemma)
381
382 -- Bézout's lemma proved using some variant of the extended
383 -- Euclidean algorithm.
384
385 lemma : (m n : ℕ) → Lemma m n
386 lemma m n = build [ <′-recBuilder ⊗ <′-recBuilder ] P gcd″ (m , n)
387 where
388 P : ℕ × ℕ → Set
389 P (m , n) = Lemma m n
390
391 gcd″ : ∀ p → (<′-Rec ⊗ <′-Rec) P p → P p
392 gcd″ (zero , n) rec = Lemma.base n
393 gcd″ (m@(suc _) , zero) rec = Lemma.sym (Lemma.base m)
394 gcd″ (m′@(suc m) , n′@(suc n)) rec with compare m n
395 ... | equal m = Lemma.refl m′
396 ... | less m k = Lemma.stepˡ $ proj₁ rec (lem₁ k m)
397 -- "gcd (suc m) (suc k)"
398 ... | greater n k = Lemma.stepʳ $ proj₂ rec (lem₁ k n) n′
399 -- "gcd (suc k) (suc n)"
400
401 -- Bézout's identity can be recovered from the GCD.
402
403 identity : ∀ {m n d} → GCD m n d → Identity d m n
404 identity {m} {n} g with result d g′ b ← lemma m n rewrite GCD.unique g g′ = b