Debian Codegraph @

src/Data/Nat/GCD/Lemmas.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Boring lemmas used in Data.Nat.GCD and Data.Nat.Coprimality
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Nat.GCD.Lemmas where
10
11 open import Data.Nat.Base
12 using (ℕ; suc; zero; _+_; _*_; _∸_; _≤_; s≤s; z≤n; _≤′_)
13 open import Data.Nat.Properties
14 open import Function.Base using (_$_)
15 open import Relation.Binary.PropositionalEquality.Core
16 using (_≡_; refl; cong; cong₂; sym)
17 open import Relation.Binary.PropositionalEquality.Properties
18 using (module ≡-Reasoning)
19
20 open ≡-Reasoning
21
22 private
23 comm-factor : ∀ x k n → x * k + x * n ≡ x * (n + k)
24 comm-factor x k n = begin
25 x * k + x * n ≡⟨ +-comm (x * k) _ ⟩
26 x * n + x * k ≡⟨ *-distribˡ-+ x n k ⟨
27 x * (n + k) ∎
28
29 distrib-comm₂ : ∀ d x k n → d + x * (n + k) ≡ d + x * k + x * n
30 distrib-comm₂ d x k n = begin
31 d + x * (n + k) ≡⟨ cong (d +_) (comm-factor x k n) ⟨
32 d + (x * k + x * n) ≡⟨ +-assoc d _ _ ⟨
33 d + x * k + x * n ∎
34
35 -- Other properties
36 -- TODO: Can this proof be simplified? An automatic solver which can
37 -- handle ∸ would be nice...
38 lem₀ : ∀ i j m n → i * m ≡ j * m + n → (i ∸ j) * m ≡ n
39 lem₀ i j m n eq = begin
40 (i ∸ j) * m ≡⟨ *-distribʳ-∸ m i j ⟩
41 (i * m) ∸ (j * m) ≡⟨ cong (_∸ j * m) eq ⟩
42 (j * m + n) ∸ (j * m) ≡⟨ cong (_∸ j * m) (+-comm (j * m) n) ⟩
43 (n + j * m) ∸ (j * m) ≡⟨ m+n∸n≡m n (j * m) ⟩
44 n ∎
45
46 lem₁ : ∀ i j → 2 + i ≤′ 2 + j + i
47 lem₁ i j = ≤⇒≤′ $ s≤s $ s≤s $ m≤n+m i j
48
49 private
50 times2 : ∀ x → x + x ≡ 2 * x
51 times2 x = cong (x +_) (sym (+-identityʳ x))
52
53 times2′ : ∀ x y → x * y + x * y ≡ 2 * x * y
54 times2′ x y = begin
55 x * y + x * y ≡⟨ times2 (x * y) ⟩
56 2 * (x * y) ≡⟨ *-assoc 2 x y ⟨
57 2 * x * y ∎
58
59 lem₂ : ∀ d x {k n} →
60 d + x * k ≡ x * n → d + x * (n + k) ≡ 2 * x * n
61 lem₂ d x {k} {n} eq = begin
62 d + x * (n + k) ≡⟨ distrib-comm₂ d x k n ⟩
63 d + x * k + x * n ≡⟨ cong (_+ x * n) eq ⟩
64 x * n + x * n ≡⟨ times2′ x n ⟩
65 2 * x * n ∎
66
67 private
68 distrib₃ : ∀ a b c x → (a + b + c) * x ≡ a * x + b * x + c * x
69 distrib₃ a b c x = begin
70 (a + b + c) * x ≡⟨ *-distribʳ-+ x (a + b) c ⟩
71 (a + b) * x + c * x ≡⟨ cong (_+ c * x) (*-distribʳ-+ x a b) ⟩
72 a * x + b * x + c * x ∎
73
74 lem₃₁ : ∀ a b c → a + (b + c) ≡ b + a + c
75 lem₃₁ a b c = begin
76 a + (b + c) ≡⟨ +-assoc a b c ⟨
77 (a + b) + c ≡⟨ cong (_+ c) (+-comm a b) ⟩
78 b + a + c ∎
79
80 +-assoc-comm : ∀ a b c d → a + (b + c + d) ≡ (a + c) + (b + d)
81 +-assoc-comm a b c d = begin
82 a + (b + c + d) ≡⟨ cong (a +_) (cong (_+ d) (+-comm b c)) ⟩
83 a + (c + b + d) ≡⟨ cong (a +_) (+-assoc c b d) ⟩
84 a + (c + (b + d)) ≡⟨ +-assoc a c _ ⟨
85 (a + c) + (b + d) ∎
86
87 *-on-right : ∀ a b c {d} → b * c ≡ d → a * b * c ≡ a * d
88 *-on-right a b c {d} eq = begin
89 a * b * c ≡⟨ *-assoc a b c ⟩
90 a * (b * c) ≡⟨ cong (a *_) eq ⟩
91 a * d ∎
92
93 *-on-left : ∀ a b c {d} → a * b ≡ d → a * (b * c) ≡ d * c
94 *-on-left a b c {d} eq = begin
95 a * (b * c) ≡⟨ *-assoc a b c ⟨
96 a * b * c ≡⟨ cong (_* c) eq ⟩
97 d * c ∎
98
99 +-on-right : ∀ a b c {d} → b + c ≡ d → a + b + c ≡ a + d
100 +-on-right a b c {d} eq = begin
101 a + b + c ≡⟨ +-assoc a b c ⟩
102 a + (b + c) ≡⟨ cong (a +_) eq ⟩
103 a + d ∎
104
105 +-on-left : ∀ a b c d → a + b ≡ d → a + (b + c) ≡ d + c
106 +-on-left a b c d eq = begin
107 a + (b + c) ≡⟨ +-assoc a b c ⟨
108 a + b + c ≡⟨ cong (_+ c) eq ⟩
109 d + c ∎
110
111 +-focus-mid : ∀ a b c d → a + b + c + d ≡ a + (b + c) + d
112 +-focus-mid a b c d = begin
113 a + b + c + d ≡⟨ cong (_+ d) (+-assoc a b c) ⟩
114 a + (b + c) + d ∎
115
116 +-assoc-comm′ : ∀ a b c d → a + b + c + d ≡ a + (b + d) + c
117 +-assoc-comm′ a b c d = begin
118 a + b + c + d ≡⟨ +-on-left a (b + c) d (a + b + c) (sym $ +-assoc a b c) ⟨
119 a + (b + c + d) ≡⟨ cong (a +_) (+-on-right b c d (+-comm c d)) ⟩
120 a + (b + (d + c)) ≡⟨ cong (a +_) (+-assoc b d c) ⟨
121 a + (b + d + c) ≡⟨ +-assoc a _ c ⟨
122 a + (b + d) + c ∎
123
124 lem₃₂ : ∀ a b c n → a * n + (b * n + a * n + c * n) ≡ (a + a + (b + c)) * n
125 lem₃₂ a b c n = begin
126 a * n + (b * n + a * n + c * n) ≡⟨ cong (a * n +_) (distrib₃ b a c n) ⟨
127 a * n + (b + a + c) * n ≡⟨ *-distribʳ-+ n a _ ⟨
128 (a + (b + a + c)) * n ≡⟨ cong (_* n) (+-assoc-comm a b a c) ⟩
129 (a + a + (b + c)) * n ∎
130
131 mid-to-right : ∀ a b c → a + 2 * b + c ≡ (a + b + c) + b
132 mid-to-right a b c = begin
133 a + 2 * b + c ≡⟨ cong (λ x → a + x + c) (times2 b) ⟨
134 a + (b + b) + c ≡⟨ +-assoc-comm′ a b c b ⟨
135 a + b + c + b ∎
136
137 mid-to-left : ∀ a b c → a + 2 * b + c ≡ b + (a + b + c)
138 mid-to-left a b c = begin
139 a + 2 * b + c ≡⟨ cong (λ x → a + x + c) (times2 b) ⟨
140 a + (b + b) + c ≡⟨ cong (_+ c) (+-on-left a b b _ (+-comm a b)) ⟩
141 b + a + b + c ≡⟨ +-on-left b (a + b) c (b + a + b) (sym $ +-assoc b a b) ⟨
142 b + (a + b + c) ∎
143
144 lem₃ : ∀ d x {i k n} →
145 d + (1 + x + i) * k ≡ x * n →
146 d + (1 + x + i) * (n + k) ≡ (1 + 2 * x + i) * n
147 lem₃ d x {i} {k} {n} eq = begin
148 d + y * (n + k) ≡⟨ distrib-comm₂ d y k n ⟩
149 d + y * k + y * n ≡⟨ cong (_+ y * n) eq ⟩
150 x * n + y * n ≡⟨ cong (x * n +_) (distrib₃ 1 x i n) ⟩
151 x * n + (1 * n + x * n + i * n) ≡⟨ lem₃₂ x 1 i n ⟩
152 (x + x + (1 + i)) * n ≡⟨ cong (_* n) (cong (_+ (1 + i)) (times2 x)) ⟩
153 (2 * x + (1 + i)) * n ≡⟨ cong (_* n) (lem₃₁ (2 * x) 1 i) ⟩
154 (1 + 2 * x + i) * n ∎
155 where y = 1 + x + i
156
157 lem₄ : ∀ d y {k i} n →
158 d + y * k ≡ (1 + y + i) * n →
159 d + y * (n + k) ≡ (1 + 2 * y + i) * n
160 lem₄ d y {k} {i} n eq = begin
161 d + y * (n + k) ≡⟨ distrib-comm₂ d y k n ⟩
162 d + y * k + y * n ≡⟨ cong (_+ y * n) eq ⟩
163 (1 + y + i) * n + y * n ≡⟨ *-distribʳ-+ n (1 + y + i) y ⟨
164 (1 + y + i + y) * n ≡⟨ cong (_* n) (mid-to-right 1 y i) ⟨
165 (1 + 2 * y + i) * n ∎
166
167 lem₅ : ∀ d x {n k} →
168 d + x * n ≡ x * k →
169 d + 2 * x * n ≡ x * (n + k)
170 lem₅ d x {n} {k} eq = begin
171 d + 2 * x * n ≡⟨ cong (d +_) (times2′ x n) ⟨
172 d + (x * n + x * n) ≡⟨ +-assoc d (x * n) _ ⟨
173 d + x * n + x * n ≡⟨ cong (_+ x * n) eq ⟩
174 x * k + x * n ≡⟨ comm-factor x k n ⟩
175 x * (n + k) ∎
176
177 lem₆ : ∀ d x {n i k} →
178 d + x * n ≡ (1 + x + i) * k →
179 d + (1 + 2 * x + i) * n ≡ (1 + x + i) * (n + k)
180 lem₆ d x {n} {i} {k} eq = begin
181 d + (1 + 2 * x + i) * n ≡⟨ cong (λ z → d + z * n) (mid-to-left 1 x i) ⟩
182 d + (x + y) * n ≡⟨ cong (d +_) (*-distribʳ-+ n x y) ⟩
183 d + (x * n + y * n) ≡⟨ +-on-left d _ _ _ eq ⟩
184 y * k + y * n ≡⟨ comm-factor y k n ⟩
185 y * (n + k) ∎
186 where y = 1 + x + i
187
188 lem₇ : ∀ d y {i} n {k} →
189 d + (1 + y + i) * n ≡ y * k →
190 d + (1 + 2 * y + i) * n ≡ y * (n + k)
191 lem₇ d y {i} n {k} eq = begin
192 d + (1 + 2 * y + i) * n ≡⟨ cong (λ z → d + z * n) (mid-to-right 1 y i) ⟩
193 d + (1 + y + i + y) * n ≡⟨ cong (d +_) (*-distribʳ-+ n (1 + y + i) y) ⟩
194 d + ((1 + y + i) * n + y * n) ≡⟨ +-on-left d _ _ _ eq ⟩
195 y * k + y * n ≡⟨ comm-factor y k n ⟩
196 y * (n + k) ∎
197
198 lem₈ : ∀ {i j k q} x y →
199 1 + y * j ≡ x * i → j * k ≡ q * i →
200 k ≡ (x * k ∸ y * q) * i
201 lem₈ {i} {j} {k} {q} x y eq eq′ =
202 sym (lem₀ (x * k) (y * q) i k lemma)
203 where
204 lemma = begin
205 x * k * i ≡⟨ *-on-right x k i (*-comm k i) ⟩
206 x * (i * k) ≡⟨ *-on-left x i k (sym eq) ⟩
207 (1 + y * j) * k ≡⟨ +-comm k _ ⟩
208 (y * j) * k + k ≡⟨ cong (_+ k) (*-assoc y j k) ⟩
209 y * (j * k) + k ≡⟨ cong (λ n → y * n + k) eq′ ⟩
210 y * (q * i) + k ≡⟨ cong (_+ k) (*-assoc y q i) ⟨
211 y * q * i + k ∎
212
213 lem₉ : ∀ {i j k q} x y →
214 1 + x * i ≡ y * j → j * k ≡ q * i →
215 k ≡ (y * q ∸ x * k) * i
216 lem₉ {i} {j} {k} {q} x y eq eq′ =
217 sym (lem₀ (y * q) (x * k) i k lemma)
218 where
219 lem : ∀ a b c → a * b * c ≡ b * c * a
220 lem a b c = begin
221 a * b * c ≡⟨ *-assoc a b c ⟩
222 a * (b * c) ≡⟨ *-comm a _ ⟩
223 b * c * a ∎
224 lemma = begin
225 y * q * i ≡⟨ lem y q i ⟩
226 q * i * y ≡⟨ cong (λ n → n * y) eq′ ⟨
227 j * k * y ≡⟨ lem y j k ⟨
228 y * j * k ≡⟨ cong (λ n → n * k) eq ⟨
229 (1 + x * i) * k ≡⟨ +-comm k _ ⟩
230 x * i * k + k ≡⟨ cong (_+ k) (*-on-right x i k (*-comm i k)) ⟩
231 x * (k * i) + k ≡⟨ cong (_+ k) (*-assoc x k i) ⟨
232 x * k * i + k ∎
233
234 lem₁₀ : ∀ {a′} b c {d} e f → let a = suc a′ in
235 a + b * (c * d * a) ≡ e * (f * d * a) →
236 d ≡ 1
237 lem₁₀ {a′} b c {d} e f eq =
238 m*n≡1⇒n≡1 (e * f ∸ b * c) d
239 (lem₀ (e * f) (b * c) d 1
240 (*-cancelʳ-≡ (e * f * d) (b * c * d + 1) _ (begin
241 e * f * d * a ≡⟨ *-assoc₄₃ e f d a ⟩
242 e * (f * d * a) ≡⟨ eq ⟨
243 a + b * (c * d * a) ≡⟨ cong (a +_) (*-assoc₄₃ b c d a) ⟨
244 a + b * c * d * a ≡⟨ +-comm a _ ⟩
245 b * c * d * a + a ≡⟨ cong (b * c * d * a +_) (+-identityʳ a) ⟨
246 b * c * d * a + (a + 0) ≡⟨ *-distribʳ-+ a (b * c * d) 1 ⟨
247 (b * c * d + 1) * a ∎)))
248 where a = suc a′
249 *-assoc₄₃ : ∀ w x y z → w * x * y * z ≡ w * (x * y * z)
250 *-assoc₄₃ w x y z = begin
251 w * x * y * z ≡⟨ cong (_* z) (*-assoc w x y) ⟩
252 w * (x * y) * z ≡⟨ *-assoc w _ z ⟩
253 w * (x * y * z) ∎
254
255 lem₁₁ : ∀ {i j m n k d} x y →
256 1 + y * j ≡ x * i → i * k ≡ m * d → j * k ≡ n * d →
257 k ≡ (x * m ∸ y * n) * d
258 lem₁₁ {i} {j} {m} {n} {k} {d} x y eq eq₁ eq₂ =
259 sym (lem₀ (x * m) (y * n) d k (begin
260 x * m * d ≡⟨ *-on-right x m d (sym eq₁) ⟩
261 x * (i * k) ≡⟨ *-on-left x i k (sym eq) ⟩
262 (1 + y * j) * k ≡⟨ +-comm k _ ⟩
263 y * j * k + k ≡⟨ cong (_+ k) (*-assoc y j k) ⟩
264 y * (j * k) + k ≡⟨ cong (λ p → y * p + k) eq₂ ⟩
265 y * (n * d) + k ≡⟨ cong (_+ k) (*-assoc y n d) ⟨
266 y * n * d + k ∎))