Debian Codegraph @

src/Data/Nat/Divisibility.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Divisibility
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Nat.Divisibility where
10
11 open import Data.Nat.Base
12 open import Data.Nat.DivMod
13 using (m≡m%n+[m/n]*n; m%n≡m∸m/n*n; m*n/n≡m; m*n%n≡0; *-/-assoc)
14 open import Data.Nat.Properties
15 open import Function.Base using (_∘′_; _$_; flip)
16 open import Function.Bundles using (_⇔_; mk⇔)
17 open import Level using (0ℓ)
18 open import Relation.Nullary.Decidable as Dec using (yes; no)
19 open import Relation.Nullary.Negation.Core using (contradiction)
20 open import Relation.Binary.Core using (_⇒_)
21 open import Relation.Binary.Bundles using (Preorder; Poset)
22 open import Relation.Binary.Structures
23 using (IsPreorder; IsPartialOrder)
24 open import Relation.Binary.Definitions
25 using (Reflexive; Transitive; Antisymmetric; Decidable)
26 import Relation.Binary.Reasoning.Preorder as ≲-Reasoning
27 open import Relation.Binary.PropositionalEquality.Core
28 using (_≡_; _≢_; refl; sym; cong; subst)
29 open import Relation.Binary.Reasoning.Syntax
30 open import Relation.Binary.PropositionalEquality.Properties
31 using (isEquivalence; module ≡-Reasoning)
32
33 private
34 variable d m n o p : ℕ
35
36
37 ------------------------------------------------------------------------
38 -- Definition and derived properties
39
40 open import Data.Nat.Divisibility.Core public hiding (*-pres-∣)
41
42 quotient≢0 : (m∣n : m ∣ n) → .{{NonZero n}} → NonZero (quotient m∣n)
43 quotient≢0 m∣n rewrite _∣_.equality m∣n = m*n≢0⇒m≢0 (quotient m∣n)
44
45 m∣n⇒n≡quotient*m : (m∣n : m ∣ n) → n ≡ (quotient m∣n) * m
46 m∣n⇒n≡quotient*m m∣n = _∣_.equality m∣n
47
48 m∣n⇒n≡m*quotient : (m∣n : m ∣ n) → n ≡ m * (quotient m∣n)
49 m∣n⇒n≡m*quotient {m = m} m∣n rewrite _∣_.equality m∣n = *-comm (quotient m∣n) m
50
51 quotient-∣ : (m∣n : m ∣ n) → (quotient m∣n) ∣ n
52 quotient-∣ {m = m} m∣n = divides m (m∣n⇒n≡m*quotient m∣n)
53
54 quotient>1 : (m∣n : m ∣ n) → m < n → 1 < quotient m∣n
55 quotient>1 {m} {n} m∣n m<n = *-cancelˡ-< m 1 (quotient m∣n) $ begin-strict
56 m * 1 ≡⟨ *-identityʳ m ⟩
57 m <⟨ m<n ⟩
58 n ≡⟨ m∣n⇒n≡m*quotient m∣n ⟩
59 m * quotient m∣n ∎
60 where open ≤-Reasoning
61
62 quotient-< : (m∣n : m ∣ n) → .{{NonTrivial m}} → .{{NonZero n}} → quotient m∣n < n
63 quotient-< {m} {n} m∣n = begin-strict
64 quotient m∣n <⟨ m<m*n (quotient m∣n) m (nonTrivial⇒n>1 m) ⟩
65 quotient m∣n * m ≡⟨ _∣_.equality m∣n ⟨
66 n ∎
67 where open ≤-Reasoning; instance _ = quotient≢0 m∣n
68
69 ------------------------------------------------------------------------
70 -- Relating _/_ and quotient
71
72 n/m≡quotient : (m∣n : m ∣ n) .{{_ : NonZero m}} → n / m ≡ quotient m∣n
73 n/m≡quotient {m = m} (divides-refl q) = m*n/n≡m q m
74
75 ------------------------------------------------------------------------
76 -- Relationship with _%_
77
78 m%n≡0⇒n∣m : ∀ m n .{{_ : NonZero n}} → m % n ≡ 0 → n ∣ m
79 m%n≡0⇒n∣m m n eq = divides (m / n) $ begin
80 m ≡⟨ m≡m%n+[m/n]*n m n ⟩
81 m % n + [m/n]*n ≡⟨ cong (_+ [m/n]*n) eq ⟩
82 [m/n]*n ∎
83 where open ≡-Reasoning; [m/n]*n = m / n * n
84
85 n∣m⇒m%n≡0 : ∀ m n .{{_ : NonZero n}} → n ∣ m → m % n ≡ 0
86 n∣m⇒m%n≡0 .(q * n) n (divides-refl q) = m*n%n≡0 q n
87
88 m%n≡0⇔n∣m : ∀ m n .{{_ : NonZero n}} → m % n ≡ 0 ⇔ n ∣ m
89 m%n≡0⇔n∣m m n = mk⇔ (m%n≡0⇒n∣m m n) (n∣m⇒m%n≡0 m n)
90
91 ------------------------------------------------------------------------
92 -- Properties of _∣_ and _≤_
93
94 ∣⇒≤ : .{{_ : NonZero n}} → m ∣ n → m ≤ n
95 ∣⇒≤ {n@.(q * m)} {m} (divides-refl q@(suc p)) = m≤m+n m (p * m)
96
97 >⇒∤ : .{{_ : NonZero n}} → m > n → m ∤ n
98 >⇒∤ {n@(suc _)} n<m@(s<s _) m∣n = contradiction (∣⇒≤ m∣n) (<⇒≱ n<m)
99
100 ------------------------------------------------------------------------
101 -- _∣_ is a partial order
102
103 -- these could/should inherit from Algebra.Properties.Monoid.Divisibility
104
105 ∣-reflexive : _≡_ ⇒ _∣_
106 ∣-reflexive {n} refl = divides 1 (sym (*-identityˡ n))
107
108 ∣-refl : Reflexive _∣_
109 ∣-refl = ∣-reflexive refl
110
111 ∣-trans : Transitive _∣_
112 ∣-trans (divides-refl p) (divides-refl q) =
113 divides (q * p) (sym (*-assoc q p _))
114
115 ∣-antisym : Antisymmetric _≡_ _∣_
116 ∣-antisym {m} {zero} _ q∣p = m∣n⇒n≡m*quotient q∣p
117 ∣-antisym {zero} {n} p∣q _ = sym (m∣n⇒n≡m*quotient p∣q)
118 ∣-antisym {suc m} {suc n} p∣q q∣p = ≤-antisym (∣⇒≤ p∣q) (∣⇒≤ q∣p)
119
120 infix 4 _∣?_
121
122 _∣?_ : Decidable _∣_
123 zero ∣? zero = yes (divides-refl 0)
124 zero ∣? suc m = no ((λ()) ∘′ ∣-antisym (divides-refl 0))
125 n@(suc _) ∣? m = Dec.map (m%n≡0⇔n∣m m n) (m % n ≟ 0)
126
127 ∣-isPreorder : IsPreorder _≡_ _∣_
128 ∣-isPreorder = record
129 { isEquivalence = isEquivalence
130 ; reflexive = ∣-reflexive
131 ; trans = ∣-trans
132 }
133
134 ∣-isPartialOrder : IsPartialOrder _≡_ _∣_
135 ∣-isPartialOrder = record
136 { isPreorder = ∣-isPreorder
137 ; antisym = ∣-antisym
138 }
139
140 ∣-preorder : Preorder 0ℓ 0ℓ 0ℓ
141 ∣-preorder = record
142 { isPreorder = ∣-isPreorder
143 }
144
145 ∣-poset : Poset 0ℓ 0ℓ 0ℓ
146 ∣-poset = record
147 { isPartialOrder = ∣-isPartialOrder
148 }
149
150 ------------------------------------------------------------------------
151 -- A reasoning module for the _∣_ relation
152
153 module ∣-Reasoning where
154 private module Base = ≲-Reasoning ∣-preorder
155
156 open Base public
157 hiding (step-≈; step-≈˘; step-≈-⟩; step-≈-⟨; step-∼; step-≲)
158 renaming (≲-go to ∣-go)
159
160 open ∣-syntax _IsRelatedTo_ _IsRelatedTo_ ∣-go public
161
162
163 ------------------------------------------------------------------------
164 -- Simple properties of _∣_
165
166 infix 10 _∣0 1∣_
167
168 _∣0 : ∀ n → n ∣ 0
169 n ∣0 = divides-refl 0
170
171 0∣⇒≡0 : 0 ∣ n → n ≡ 0
172 0∣⇒≡0 {n} 0∣n = ∣-antisym (n ∣0) 0∣n
173
174 1∣_ : ∀ n → 1 ∣ n
175 1∣ n = divides n (sym (*-identityʳ n))
176
177 ∣1⇒≡1 : n ∣ 1 → n ≡ 1
178 ∣1⇒≡1 {n} n∣1 = ∣-antisym n∣1 (1∣ n)
179
180 n∣n : n ∣ n
181 n∣n = ∣-refl
182
183 ------------------------------------------------------------------------
184 -- Properties of _∣_ and _+_
185
186 ∣m∣n⇒∣m+n : d ∣ m → d ∣ n → d ∣ m + n
187 ∣m∣n⇒∣m+n (divides-refl p) (divides-refl q) =
188 divides (p + q) (sym (*-distribʳ-+ _ p q))
189
190 ∣m+n∣m⇒∣n : d ∣ m + n → d ∣ m → d ∣ n
191 ∣m+n∣m⇒∣n {d} {m} {n} (divides p m+n≡p*d) (divides-refl q) =
192 divides (p ∸ q) $ begin-equality
193 n ≡⟨ m+n∸n≡m n m ⟨
194 n + m ∸ m ≡⟨ cong (_∸ m) (+-comm n m) ⟩
195 m + n ∸ m ≡⟨ cong (_∸ m) m+n≡p*d ⟩
196 p * d ∸ q * d ≡⟨ *-distribʳ-∸ d p q ⟨
197 (p ∸ q) * d ∎
198 where open ∣-Reasoning
199
200 ------------------------------------------------------------------------
201 -- Properties of _∣_ and _*_
202
203 n∣m*n : ∀ m {n} → n ∣ m * n
204 n∣m*n m = divides m refl
205
206 m∣m*n : ∀ {m} n → m ∣ m * n
207 m∣m*n n = divides n (*-comm _ n)
208
209 n∣m*n*o : ∀ m {n} o → n ∣ m * n * o
210 n∣m*n*o m o = ∣-trans (n∣m*n m) (m∣m*n o)
211
212 ∣m⇒∣m*n : ∀ n → d ∣ m → d ∣ m * n
213 ∣m⇒∣m*n n (divides-refl q) = ∣-trans (n∣m*n q) (m∣m*n n)
214
215 ∣n⇒∣m*n : ∀ m {n} → d ∣ n → d ∣ m * n
216 ∣n⇒∣m*n m {n} rewrite *-comm m n = ∣m⇒∣m*n m
217
218 m*n∣⇒m∣ : ∀ m n → m * n ∣ d → m ∣ d
219 m*n∣⇒m∣ m n (divides-refl q) = ∣n⇒∣m*n q (m∣m*n n)
220
221 m*n∣⇒n∣ : ∀ m n → m * n ∣ d → n ∣ d
222 m*n∣⇒n∣ m n rewrite *-comm m n = m*n∣⇒m∣ n m
223
224 *-pres-∣ : o ∣ m → p ∣ n → o * p ∣ m * n
225 *-pres-∣ {o} {m@.(q * o)} {p} {n@.(r * p)} (divides-refl q) (divides-refl r) =
226 divides (q * r) ([m*n]*[o*p]≡[m*o]*[n*p] q o r p)
227
228 *-monoʳ-∣ : ∀ o → m ∣ n → o * m ∣ o * n
229 *-monoʳ-∣ o = *-pres-∣ (∣-refl {o})
230
231 *-monoˡ-∣ : ∀ o → m ∣ n → m * o ∣ n * o
232 *-monoˡ-∣ o = flip *-pres-∣ (∣-refl {o})
233
234 *-cancelˡ-∣ : ∀ o .{{_ : NonZero o}} → o * m ∣ o * n → m ∣ n
235 *-cancelˡ-∣ {m} {n} o o*m∣o*n = divides q $ *-cancelˡ-≡ n (q * m) o $ begin-equality
236 o * n ≡⟨ m∣n⇒n≡m*quotient o*m∣o*n ⟩
237 o * m * q ≡⟨ *-assoc o m q ⟩
238 o * (m * q) ≡⟨ cong (o *_) (*-comm q m) ⟨
239 o * (q * m) ∎
240 where
241 open ∣-Reasoning
242 q = quotient o*m∣o*n
243
244 *-cancelʳ-∣ : ∀ o .{{_ : NonZero o}} → m * o ∣ n * o → m ∣ n
245 *-cancelʳ-∣ {m} {n} o rewrite *-comm m o | *-comm n o = *-cancelˡ-∣ o
246
247 ------------------------------------------------------------------------
248 -- Properties of _∣_ and _∸_
249
250 ∣m∸n∣n⇒∣m : ∀ d → n ≤ m → d ∣ m ∸ n → d ∣ n → d ∣ m
251 ∣m∸n∣n⇒∣m {n} {m} d n≤m (divides p m∸n≡p*d) (divides-refl q) =
252 divides (p + q) $ begin-equality
253 m ≡⟨ m+[n∸m]≡n n≤m ⟨
254 n + (m ∸ n) ≡⟨ +-comm n (m ∸ n) ⟩
255 m ∸ n + n ≡⟨ cong (_+ n) m∸n≡p*d ⟩
256 p * d + q * d ≡⟨ *-distribʳ-+ d p q ⟨
257 (p + q) * d ∎
258 where open ∣-Reasoning
259
260 ------------------------------------------------------------------------
261 -- Properties of _∣_ and _/_
262
263 m/n∣m : .{{_ : NonZero n}} → n ∣ m → m / n ∣ m
264 m/n∣m {n} {m} n∣m = begin
265 m / n ≡⟨ n/m≡quotient n∣m ⟩
266 quotient n∣m ∣⟨ quotient-∣ n∣m ⟩
267 m ∎
268 where open ∣-Reasoning
269
270 m*n∣o⇒m∣o/n : ∀ m n .{{_ : NonZero n}} → m * n ∣ o → m ∣ o / n
271 m*n∣o⇒m∣o/n m n (divides-refl p) = divides p $ begin-equality
272 p * (m * n) / n ≡⟨ *-/-assoc p (n∣m*n m) ⟩
273 p * ((m * n) / n) ≡⟨ cong (p *_) (m*n/n≡m m n) ⟩
274 p * m ∎
275 where open ∣-Reasoning
276
277 m*n∣o⇒n∣o/m : ∀ m n .{{_ : NonZero m}} → m * n ∣ o → n ∣ (o / m)
278 m*n∣o⇒n∣o/m m n rewrite *-comm m n = m*n∣o⇒m∣o/n n m
279
280 m∣n/o⇒m*o∣n : .{{_ : NonZero o}} → o ∣ n → m ∣ n / o → m * o ∣ n
281 m∣n/o⇒m*o∣n {o} {n@.(p * o)} {m} (divides-refl p) m∣p*o/o = begin
282 m * o ∣⟨ *-monoˡ-∣ o (subst (m ∣_) (m*n/n≡m p o) m∣p*o/o) ⟩
283 p * o ∎
284 where open ∣-Reasoning
285
286 m∣n/o⇒o*m∣n : .{{_ : NonZero o}} → o ∣ n → m ∣ n / o → o * m ∣ n
287 m∣n/o⇒o*m∣n {o} {_} {m} rewrite *-comm o m = m∣n/o⇒m*o∣n
288
289 m/n∣o⇒m∣o*n : .{{_ : NonZero n}} → n ∣ m → m / n ∣ o → m ∣ o * n
290 m/n∣o⇒m∣o*n {n} {m@.(p * n)} {o} (divides-refl p) p*n/n∣o = begin
291 p * n ∣⟨ *-monoˡ-∣ n (subst (_∣ o) (m*n/n≡m p n) p*n/n∣o) ⟩
292 o * n ∎
293 where open ∣-Reasoning
294
295 m∣n*o⇒m/n∣o : .{{_ : NonZero n}} → n ∣ m → m ∣ o * n → m / n ∣ o
296 m∣n*o⇒m/n∣o {n} {m@.(p * n)} {o} (divides-refl p) pn∣on = begin
297 m / n ≡⟨⟩
298 p * n / n ≡⟨ m*n/n≡m p n ⟩
299 p ∣⟨ *-cancelʳ-∣ n pn∣on ⟩
300 o ∎
301 where open ∣-Reasoning
302
303 ------------------------------------------------------------------------
304 -- Properties of _∣_ and _%_
305
306 ∣n∣m%n⇒∣m : .{{_ : NonZero n}} → d ∣ n → d ∣ m % n → d ∣ m
307 ∣n∣m%n⇒∣m {n@.(p * d)} {d} {m} (divides-refl p) (divides q m%n≡qd) =
308 divides (q + (m / n) * p) $ begin-equality
309 m ≡⟨ m≡m%n+[m/n]*n m n ⟩
310 m % n + (m / n) * n ≡⟨ cong (_+ (m / n) * n) m%n≡qd ⟩
311 q * d + (m / n) * n ≡⟨⟩
312 q * d + (m / n) * (p * d) ≡⟨ cong (q * d +_) (*-assoc (m / n) p d) ⟨
313 q * d + ((m / n) * p) * d ≡⟨ *-distribʳ-+ d q _ ⟨
314 (q + (m / n) * p) * d ∎
315 where open ∣-Reasoning
316
317 %-presˡ-∣ : .{{_ : NonZero n}} → d ∣ m → d ∣ n → d ∣ m % n
318 %-presˡ-∣ {n} {d} {m@.(p * d)} (divides-refl p) (divides q 1+n≡qd) =
319 divides (p ∸ m / n * q) $ begin-equality
320 m % n ≡⟨ m%n≡m∸m/n*n m n ⟩
321 m ∸ m / n * n ≡⟨ cong (λ v → m ∸ m / n * v) 1+n≡qd ⟩
322 m ∸ m / n * (q * d) ≡⟨ cong (m ∸_) (*-assoc (m / n) q d) ⟨
323 m ∸ (m / n * q) * d ≡⟨⟩
324 p * d ∸ (m / n * q) * d ≡⟨ *-distribʳ-∸ d p (m / n * q) ⟨
325 (p ∸ m / n * q) * d ∎
326 where open ∣-Reasoning
327
328 ------------------------------------------------------------------------
329 -- Properties of _∣_ and !_
330
331 m≤n⇒m!∣n! : m ≤ n → m ! ∣ n !
332 m≤n⇒m!∣n! m≤n = help (≤⇒≤′ m≤n)
333 where
334 help : m ≤′ n → m ! ∣ n !
335 help ≤′-refl = ∣-refl
336 help {n = n} (≤′-step m≤n) = ∣n⇒∣m*n n (help m≤n)
337
338 ------------------------------------------------------------------------
339 -- Properties of _HasNonTrivialDivisorLessThan_
340
341 -- Smart constructor
342
343 hasNonTrivialDivisor-≢ : .{{NonTrivial d}} → .{{NonZero n}} →
344 d ≢ n → d ∣ n → n HasNonTrivialDivisorLessThan n
345 hasNonTrivialDivisor-≢ d≢n d∣n
346 = hasNonTrivialDivisor (≤∧≢⇒< (∣⇒≤ d∣n) d≢n) d∣n
347
348 -- Monotonicity wrt ∣
349
350 hasNonTrivialDivisor-∣ : m HasNonTrivialDivisorLessThan n → m ∣ o →
351 o HasNonTrivialDivisorLessThan n
352 hasNonTrivialDivisor-∣ (hasNonTrivialDivisor d<n d∣m) m∣o
353 = hasNonTrivialDivisor d<n (∣-trans d∣m m∣o)
354
355 -- Monotonicity wrt ≤
356
357 hasNonTrivialDivisor-≤ : m HasNonTrivialDivisorLessThan n → n ≤ o →
358 m HasNonTrivialDivisorLessThan o
359 hasNonTrivialDivisor-≤ (hasNonTrivialDivisor d<n d∣m) n≤o
360 = hasNonTrivialDivisor (<-≤-trans d<n n≤o) d∣m