annotate automaton-in-agda/src/gcd.agda @ 255:4e4e148eb7e5

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 01 Jul 2021 19:07:19 +0900
parents 89120ac828b7
children 8b437dd616dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 147
diff changeset
1 {-# OPTIONS --allow-unsolved-metas #-}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 147
diff changeset
2 module gcd where
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
4 open import Data.Nat
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
5 open import Data.Nat.Properties
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 open import Data.Empty
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
7 open import Data.Unit using (⊤ ; tt)
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Relation.Nullary
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 open import Relation.Binary.PropositionalEquality
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
10 open import Relation.Binary.Definitions
149
d3a8572ced9c non terminating GCD
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
11 open import nat
151
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
12 open import logic
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
14 record Factor (n m : ℕ ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
15 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
16 factor : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
17 remain : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
18 is-factor : factor * n + remain ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
19
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
20 record Dividable (n m : ℕ ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
21 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
22 factor : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
23 is-factor : factor * n + 0 ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
24
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
25 open Factor
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
26
193
875eb1fa9694 dividable reorganzaiton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 192
diff changeset
27 DtoF : {n m : ℕ} → Dividable n m → Factor n m
195
373b6e0ec595 ... remove f>1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 194
diff changeset
28 DtoF {n} {m} record { factor = f ; is-factor = fa } = record { factor = f ; remain = 0 ; is-factor = fa }
193
875eb1fa9694 dividable reorganzaiton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 192
diff changeset
29
195
373b6e0ec595 ... remove f>1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 194
diff changeset
30 FtoD : {n m : ℕ} → (fc : Factor n m) → remain fc ≡ 0 → Dividable n m
373b6e0ec595 ... remove f>1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 194
diff changeset
31 FtoD {n} {m} record { factor = f ; remain = r ; is-factor = fa } refl = record { factor = f ; is-factor = fa }
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
32
199
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 198
diff changeset
33 --divdable^2 : ( n k : ℕ ) → Dividable k ( n * n ) → Dividable k n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 198
diff changeset
34 --divdable^2 n k dn2 = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 198
diff changeset
35
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
36 decf : { n k : ℕ } → ( x : Factor k (suc n) ) → Factor k n
191
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
37 decf {n} {k} record { factor = f ; remain = r ; is-factor = fa } =
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
38 decf1 {n} {k} f r fa where
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
39 decf1 : { n k : ℕ } → (f r : ℕ) → (f * k + r ≡ suc n) → Factor k n
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
40 decf1 {n} {k} f (suc r) fa = -- this case must be the first
187
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 186
diff changeset
41 record { factor = f ; remain = r ; is-factor = ( begin -- fa : f * k + suc r ≡ suc n
189
6945d2aeb86a expanding record does not work
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 188
diff changeset
42 f * k + r ≡⟨ cong pred ( begin
6945d2aeb86a expanding record does not work
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 188
diff changeset
43 suc ( f * k + r ) ≡⟨ +-comm _ r ⟩
6945d2aeb86a expanding record does not work
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 188
diff changeset
44 r + suc (f * k) ≡⟨ sym (+-assoc r 1 _) ⟩
6945d2aeb86a expanding record does not work
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 188
diff changeset
45 (r + 1) + f * k ≡⟨ cong (λ t → t + f * k ) (+-comm r 1) ⟩
6945d2aeb86a expanding record does not work
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 188
diff changeset
46 (suc r ) + f * k ≡⟨ +-comm (suc r) _ ⟩
6945d2aeb86a expanding record does not work
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 188
diff changeset
47 f * k + suc r ≡⟨ fa ⟩
187
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 186
diff changeset
48 suc n ∎ ) ⟩
191
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
49 n ∎ ) } where open ≡-Reasoning
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
50 decf1 {n} {zero} (suc f) zero fa = ⊥-elim ( nat-≡< fa (
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
51 begin suc (suc f * zero + zero) ≡⟨ cong suc (+-comm _ zero) ⟩
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
52 suc (f * 0) ≡⟨ cong suc (*-comm f zero) ⟩
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
53 suc zero ≤⟨ s≤s z≤n ⟩
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
54 suc n ∎ )) where open ≤-Reasoning
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
55 decf1 {n} {suc k} (suc f) zero fa =
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
56 record { factor = f ; remain = k ; is-factor = ( begin -- fa : suc (k + f * suc k + zero) ≡ suc n
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
57 f * suc k + k ≡⟨ +-comm _ k ⟩
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
58 k + f * suc k ≡⟨ +-comm zero _ ⟩
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
59 (k + f * suc k) + zero ≡⟨ cong pred fa ⟩
a3a72db6aed3 fix decf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 190
diff changeset
60 n ∎ ) } where open ≡-Reasoning
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
61
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
62 div0 : {k : ℕ} → Dividable k 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
63 div0 {k} = record { factor = 0; is-factor = refl }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
64
209
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
65 div= : {k : ℕ} → Dividable k k
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
66 div= {k} = record { factor = 1; is-factor = ( begin
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
67 k + 0 * k + 0 ≡⟨ trans ( +-comm _ 0) ( +-comm _ 0) ⟩
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
68 k ∎ ) } where open ≡-Reasoning
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
69
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
70 gcd1 : ( i i0 j j0 : ℕ ) → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
71 gcd1 zero i0 zero j0 with <-cmp i0 j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
72 ... | tri< a ¬b ¬c = i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
73 ... | tri≈ ¬a refl ¬c = i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
74 ... | tri> ¬a ¬b c = j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
75 gcd1 zero i0 (suc zero) j0 = 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
76 gcd1 zero zero (suc (suc j)) j0 = j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
77 gcd1 zero (suc i0) (suc (suc j)) j0 = gcd1 i0 (suc i0) (suc j) (suc (suc j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
78 gcd1 (suc zero) i0 zero j0 = 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
79 gcd1 (suc (suc i)) i0 zero zero = i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
80 gcd1 (suc (suc i)) i0 zero (suc j0) = gcd1 (suc i) (suc (suc i)) j0 (suc j0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
81 gcd1 (suc i) i0 (suc j) j0 = gcd1 i i0 j j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
82
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
83 gcd : ( i j : ℕ ) → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
84 gcd i j = gcd1 i i j j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
85
209
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
86 gcd20 : (i : ℕ) → gcd i 0 ≡ i
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
87 gcd20 zero = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
88 gcd20 (suc i) = gcd201 (suc i) where
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
89 gcd201 : (i : ℕ ) → gcd1 i i zero zero ≡ i
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
90 gcd201 zero = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
91 gcd201 (suc zero) = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
92 gcd201 (suc (suc i)) = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
93
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
94 gcd22 : ( i i0 o o0 : ℕ ) → gcd1 (suc i) i0 (suc o) o0 ≡ gcd1 i i0 o o0
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
95 gcd22 zero i0 zero o0 = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
96 gcd22 zero i0 (suc o) o0 = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
97 gcd22 (suc i) i0 zero o0 = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
98 gcd22 (suc i) i0 (suc o) o0 = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
99
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
100 gcdmm : (n m : ℕ) → gcd1 n m n m ≡ m
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
101 gcdmm zero m with <-cmp m m
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
102 ... | tri< a ¬b ¬c = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
103 ... | tri≈ ¬a refl ¬c = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
104 ... | tri> ¬a ¬b c = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
105 gcdmm (suc n) m = subst (λ k → k ≡ m) (sym (gcd22 n m n m )) (gcdmm n m )
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
106
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
107 gcdsym2 : (i j : ℕ) → gcd1 zero i zero j ≡ gcd1 zero j zero i
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
108 gcdsym2 i j with <-cmp i j | <-cmp j i
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
109 ... | tri< a ¬b ¬c | tri< a₁ ¬b₁ ¬c₁ = ⊥-elim (nat-<> a a₁)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
110 ... | tri< a ¬b ¬c | tri≈ ¬a b ¬c₁ = ⊥-elim (nat-≡< (sym b) a)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
111 ... | tri< a ¬b ¬c | tri> ¬a ¬b₁ c = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
112 ... | tri≈ ¬a b ¬c | tri< a ¬b ¬c₁ = ⊥-elim (nat-≡< (sym b) a)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
113 ... | tri≈ ¬a refl ¬c | tri≈ ¬a₁ refl ¬c₁ = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
114 ... | tri≈ ¬a b ¬c | tri> ¬a₁ ¬b c = ⊥-elim (nat-≡< b c)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
115 ... | tri> ¬a ¬b c | tri< a ¬b₁ ¬c = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
116 ... | tri> ¬a ¬b c | tri≈ ¬a₁ b ¬c = ⊥-elim (nat-≡< b c)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
117 ... | tri> ¬a ¬b c | tri> ¬a₁ ¬b₁ c₁ = ⊥-elim (nat-<> c c₁)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
118 gcdsym1 : ( i i0 j j0 : ℕ ) → gcd1 i i0 j j0 ≡ gcd1 j j0 i i0
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
119 gcdsym1 zero zero zero zero = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
120 gcdsym1 zero zero zero (suc j0) = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
121 gcdsym1 zero (suc i0) zero zero = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
122 gcdsym1 zero (suc i0) zero (suc j0) = gcdsym2 (suc i0) (suc j0)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
123 gcdsym1 zero zero (suc zero) j0 = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
124 gcdsym1 zero zero (suc (suc j)) j0 = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
125 gcdsym1 zero (suc i0) (suc zero) j0 = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
126 gcdsym1 zero (suc i0) (suc (suc j)) j0 = gcdsym1 i0 (suc i0) (suc j) (suc (suc j))
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
127 gcdsym1 (suc zero) i0 zero j0 = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
128 gcdsym1 (suc (suc i)) i0 zero zero = refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
129 gcdsym1 (suc (suc i)) i0 zero (suc j0) = gcdsym1 (suc i) (suc (suc i))j0 (suc j0)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
130 gcdsym1 (suc i) i0 (suc j) j0 = subst₂ (λ j k → j ≡ k ) (sym (gcd22 i _ _ _)) (sym (gcd22 j _ _ _)) (gcdsym1 i i0 j j0 )
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
131
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
132 gcdsym : { n m : ℕ} → gcd n m ≡ gcd m n
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
133 gcdsym {n} {m} = gcdsym1 n n m m
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
134
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
135 gcd11 : ( i : ℕ ) → gcd i i ≡ i
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
136 gcd11 i = gcdmm i i
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
137
212
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
139 gcd203 : (i : ℕ) → gcd1 (suc i) (suc i) i i ≡ 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
140 gcd203 zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
141 gcd203 (suc i) = gcd205 (suc i) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
142 gcd205 : (j : ℕ) → gcd1 (suc j) (suc (suc i)) j (suc i) ≡ 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
143 gcd205 zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
144 gcd205 (suc j) = subst (λ k → k ≡ 1) (gcd22 (suc j) (suc (suc i)) j (suc i)) (gcd205 j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
145
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
146 gcd204 : (i : ℕ) → gcd1 1 1 i i ≡ 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
147 gcd204 zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
148 gcd204 (suc zero) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
149 gcd204 (suc (suc zero)) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
150 gcd204 (suc (suc (suc i))) = gcd204 (suc (suc i))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
151
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
152 gcd+j : ( i j : ℕ ) → gcd (i + j) j ≡ gcd i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
153 gcd+j i j = gcd200 i i j j refl refl where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
154 gcd202 : (i j1 : ℕ) → (i + suc j1) ≡ suc (i + j1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
155 gcd202 zero j1 = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
156 gcd202 (suc i) j1 = cong suc (gcd202 i j1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
157 gcd201 : (i i0 j j0 j1 : ℕ) → gcd1 (i + j1) (i0 + suc j) j1 j0 ≡ gcd1 i (i0 + suc j) zero j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
158 gcd201 i i0 j j0 zero = subst (λ k → gcd1 k (i0 + suc j) zero j0 ≡ gcd1 i (i0 + suc j) zero j0 ) (+-comm zero i) refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
159 gcd201 i i0 j j0 (suc j1) = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
160 gcd1 (i + suc j1) (i0 + suc j) (suc j1) j0 ≡⟨ cong (λ k → gcd1 k (i0 + suc j) (suc j1) j0 ) (gcd202 i j1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
161 gcd1 (suc (i + j1)) (i0 + suc j) (suc j1) j0 ≡⟨ gcd22 (i + j1) (i0 + suc j) j1 j0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
162 gcd1 (i + j1) (i0 + suc j) j1 j0 ≡⟨ gcd201 i i0 j j0 j1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
163 gcd1 i (i0 + suc j) zero j0 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
164 gcd200 : (i i0 j j0 : ℕ) → i ≡ i0 → j ≡ j0 → gcd1 (i + j) (i0 + j) j j0 ≡ gcd1 i i j0 j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
165 gcd200 i .i zero .0 refl refl = subst (λ k → gcd1 k k zero zero ≡ gcd1 i i zero zero ) (+-comm zero i) refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
166 gcd200 (suc (suc i)) i0 (suc j) (suc j0) i=i0 j=j0 = gcd201 (suc (suc i)) i0 j (suc j0) (suc j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
167 gcd200 zero zero (suc zero) .1 i=i0 refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
168 gcd200 zero zero (suc (suc j)) .(suc (suc j)) i=i0 refl = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
169 gcd1 (zero + suc (suc j)) (zero + suc (suc j)) (suc (suc j)) (suc (suc j)) ≡⟨ gcdmm (suc (suc j)) (suc (suc j)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
170 suc (suc j) ≡⟨ sym (gcd20 (suc (suc j))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
171 gcd1 zero zero (suc (suc j)) (suc (suc j)) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
172 gcd200 zero (suc i0) (suc j) .(suc j) () refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
173 gcd200 (suc zero) .1 (suc j) .(suc j) refl refl = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
174 gcd1 (1 + suc j) (1 + suc j) (suc j) (suc j) ≡⟨ gcd203 (suc j) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
175 1 ≡⟨ sym ( gcd204 (suc j)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
176 gcd1 1 1 (suc j) (suc j) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
177 gcd200 (suc (suc i)) i0 (suc j) zero i=i0 ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
178
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
179 div1 : { k : ℕ } → k > 1 → ¬ Dividable k 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
180 div1 {k} k>1 record { factor = (suc f) ; is-factor = fa } = ⊥-elim ( nat-≡< (sym fa) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
181 2 ≤⟨ k>1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
182 k ≡⟨ +-comm 0 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
183 k + 0 ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
184 1 * k ≤⟨ *-mono-≤ {1} {suc f} (s≤s z≤n ) ≤-refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
185 suc f * k ≡⟨ +-comm 0 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
186 suc f * k + 0 ∎ )) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
187
213
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
188 div+div : { i j k : ℕ } → Dividable k i → Dividable k j → Dividable k (i + j) ∧ Dividable k (j + i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
189 div+div {i} {j} {k} di dj = ⟪ div+div1 , subst (λ g → Dividable k g) (+-comm i j) div+div1 ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
190 fki = Dividable.factor di
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
191 fkj = Dividable.factor dj
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
192 div+div1 : Dividable k (i + j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
193 div+div1 = record { factor = fki + fkj ; is-factor = ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
194 (fki + fkj) * k + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
195 (fki + fkj) * k ≡⟨ *-distribʳ-+ k fki _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
196 fki * k + fkj * k ≡⟨ cong₂ ( λ i j → i + j ) (+-comm 0 (fki * k)) (+-comm 0 (fkj * k)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
197 (fki * k + 0) + (fkj * k + 0) ≡⟨ cong₂ ( λ i j → i + j ) (Dividable.is-factor di) (Dividable.is-factor dj) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
198 i + j ∎ ) } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
199 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
200
197
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 196
diff changeset
201 div-div : { i j k : ℕ } → k > 1 → Dividable k i → Dividable k j → Dividable k (i - j) ∧ Dividable k (j - i)
213
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
202 div-div {i} {j} {k} k>1 di dj = ⟪ div-div1 di dj , div-div1 dj di ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
203 div-div1 : {i j : ℕ } → Dividable k i → Dividable k j → Dividable k (i - j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
204 div-div1 {i} {j} di dj = record { factor = fki - fkj ; is-factor = ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
205 (fki - fkj) * k + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
206 (fki - fkj) * k ≡⟨ distr-minus-* {fki} {fkj} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
207 (fki * k) - (fkj * k) ≡⟨ cong₂ ( λ i j → i - j ) (+-comm 0 (fki * k)) (+-comm 0 (fkj * k)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
208 (fki * k + 0) - (fkj * k + 0) ≡⟨ cong₂ ( λ i j → i - j ) (Dividable.is-factor di) (Dividable.is-factor dj) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
209 i - j ∎ ) } where
197
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 196
diff changeset
210 open ≡-Reasoning
213
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
211 fki = Dividable.factor di
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
212 fkj = Dividable.factor dj
197
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 196
diff changeset
213
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
214 open _∧_
192
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 191
diff changeset
215
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
216 div+1 : { i k : ℕ } → k > 1 → Dividable k i → ¬ Dividable k (suc i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
217 div+1 {i} {k} k>1 d d1 = div1 k>1 div+11 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
218 div+11 : Dividable k 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
219 div+11 = subst (λ g → Dividable k g) (minus+y-y {1} {i} ) ( proj2 (div-div k>1 d d1 ) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
220
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
221 div<k : { m k : ℕ } → k > 1 → m > 0 → m < k → ¬ Dividable k m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
222 div<k {m} {k} k>1 m>0 m<k d = ⊥-elim ( nat-≤> (div<k1 (Dividable.factor d) (Dividable.is-factor d)) m<k ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
223 div<k1 : (f : ℕ ) → f * k + 0 ≡ m → k ≤ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
224 div<k1 zero eq = ⊥-elim (nat-≡< eq m>0 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
225 div<k1 (suc f) eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
226 k ≤⟨ x≤x+y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
227 k + (f * k + 0) ≡⟨ sym (+-assoc k _ _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
228 k + f * k + 0 ≡⟨ eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
229 m ∎ where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
230
227
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
231 div→k≤m : { m k : ℕ } → k > 1 → m > 0 → Dividable k m → m ≥ k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
232 div→k≤m {m} {k} k>1 m>0 d with <-cmp m k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
233 ... | tri< a ¬b ¬c = ⊥-elim ( div<k k>1 m>0 a d )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
234 ... | tri≈ ¬a refl ¬c = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
235 ... | tri> ¬a ¬b c = <to≤ c
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
236
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
237 div1*k+0=k : {k : ℕ } → 1 * k + 0 ≡ k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
238 div1*k+0=k {k} = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
239 1 * k + 0 ≡⟨ cong (λ g → g + 0) (+-comm _ 0) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
240 k + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
241 k ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
242
220
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
243 decD : {k m : ℕ} → k > 1 → Dec (Dividable k m )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
244 decD {k} {m} k>1 = n-induction {_} {_} {ℕ} {λ m → Dec (Dividable k m ) } F I m where
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
245 F : ℕ → ℕ
216
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 215
diff changeset
246 F m = m
220
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
247 F0 : ( m : ℕ ) → F (m - k) ≡ 0 → Dec (Dividable k m )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
248 F0 0 eq = yes record { factor = 0 ; is-factor = refl }
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
249 F0 (suc m) eq with <-cmp k (suc m)
220
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
250 ... | tri< a ¬b ¬c = yes record { factor = 1 ; is-factor =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
251 subst (λ g → 1 * k + 0 ≡ g ) (sym (i-j=0→i=j (<to≤ a) eq )) div1*k+0=k } where -- (suc m - k) ≡ 0 → k ≡ suc m, k ≤ suc m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
252 ... | tri≈ ¬a refl ¬c = yes record { factor = 1 ; is-factor = div1*k+0=k }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
253 ... | tri> ¬a ¬b c = no ( λ d → ⊥-elim (div<k k>1 (s≤s z≤n ) c d) )
216
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 215
diff changeset
254 decl : {m : ℕ } → 0 < m → m - k < m
217
119ab3f823f1 NInduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 216
diff changeset
255 decl {m} 0<m = y-x<y (<-trans a<sa k>1 ) 0<m
220
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
256 ind : (p : ℕ ) → Dec (Dividable k (p - k) ) → Dec (Dividable k p )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
257 ind p (yes y) with <-cmp p k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
258 ... | tri≈ ¬a refl ¬c = yes (subst (λ g → Dividable k g) (minus+n ≤-refl ) (proj1 ( div+div y div= )))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
259 ... | tri> ¬a ¬b k<p = yes (subst (λ g → Dividable k g) (minus+n (<-trans k<p a<sa)) (proj1 ( div+div y div= )))
221
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
260 ... | tri< a ¬b ¬c with <-cmp p 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
261 ... | tri≈ ¬a refl ¬c₁ = yes div0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
262 ... | tri> ¬a ¬b₁ c = no (λ d → not-div p (Dividable.factor d) a c (Dividable.is-factor d) ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
263 not-div : (p f : ℕ) → p < k → 0 < p → f * k + 0 ≡ p → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
264 not-div (suc p) (suc f) p<k 0<p eq = nat-≡< (sym eq) ( begin -- ≤-trans p<k {!!}) -- suc p ≤ k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
265 suc (suc p) ≤⟨ p<k ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
266 k ≤⟨ x≤x+y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
267 k + (f * k + 0) ≡⟨ sym (+-assoc k _ _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 220
diff changeset
268 suc f * k + 0 ∎ ) where open ≤-Reasoning
220
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 219
diff changeset
269 ind p (no n) = no (λ d → n (proj1 (div-div k>1 d div=)) )
216
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 215
diff changeset
270 I : Ninduction ℕ _ F
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
271 I = record {
216
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 215
diff changeset
272 pnext = λ p → p - k
217
119ab3f823f1 NInduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 216
diff changeset
273 ; fzero = λ {m} eq → F0 m eq
219
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 218
diff changeset
274 ; decline = λ {m} lt → decl lt
217
119ab3f823f1 NInduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 216
diff changeset
275 ; ind = λ {p} prev → ind p prev
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
276 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 214
diff changeset
277
193
875eb1fa9694 dividable reorganzaiton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 192
diff changeset
278 gcd-gt : ( i i0 j j0 k : ℕ ) → k > 1 → (if : Factor k i) (i0f : Dividable k i0 ) (jf : Factor k j ) (j0f : Dividable k j0)
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
279 → Dividable k (i - j) ∧ Dividable k (j - i)
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
280 → Dividable k ( gcd1 i i0 j j0 )
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
281 gcd-gt zero i0 zero j0 k k>1 if i0f jf j0f i-j with <-cmp i0 j0
194
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 193
diff changeset
282 ... | tri< a ¬b ¬c = i0f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 193
diff changeset
283 ... | tri≈ ¬a refl ¬c = i0f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 193
diff changeset
284 ... | tri> ¬a ¬b c = j0f
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
285 gcd-gt zero i0 (suc zero) j0 k k>1 if i0f jf j0f i-j = ⊥-elim (div1 k>1 (proj2 i-j)) -- can't happen
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
286 gcd-gt zero zero (suc (suc j)) j0 k k>1 if i0f jf j0f i-j = j0f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
287 gcd-gt zero (suc i0) (suc (suc j)) j0 k k>1 if i0f jf j0f i-j =
197
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 196
diff changeset
288 gcd-gt i0 (suc i0) (suc j) (suc (suc j)) k k>1 (decf (DtoF i0f)) i0f (decf jf) (proj2 i-j) (div-div k>1 i0f (proj2 i-j))
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
289 gcd-gt (suc zero) i0 zero j0 k k>1 if i0f jf j0f i-j = ⊥-elim (div1 k>1 (proj1 i-j)) -- can't happen
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
290 gcd-gt (suc (suc i)) i0 zero zero k k>1 if i0f jf j0f i-j = i0f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
291 gcd-gt (suc (suc i)) i0 zero (suc j0) k k>1 if i0f jf j0f i-j = --
197
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 196
diff changeset
292 gcd-gt (suc i) (suc (suc i)) j0 (suc j0) k k>1 (decf if) (proj1 i-j) (decf (DtoF j0f)) j0f (div-div k>1 (proj1 i-j) j0f )
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
293 gcd-gt (suc zero) i0 (suc j) j0 k k>1 if i0f jf j0f i-j =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
294 gcd-gt zero i0 j j0 k k>1 (decf if) i0f (decf jf) j0f i-j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
295 gcd-gt (suc (suc i)) i0 (suc j) j0 k k>1 if i0f jf j0f i-j =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 195
diff changeset
296 gcd-gt (suc i) i0 j j0 k k>1 (decf if) i0f (decf jf) j0f i-j
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 162
diff changeset
297
194
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 193
diff changeset
298 gcd-div : ( i j k : ℕ ) → k > 1 → (if : Dividable k i) (jf : Dividable k j )
186
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
299 → Dividable k ( gcd i j )
197
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 196
diff changeset
300 gcd-div i j k k>1 if jf = gcd-gt i i j j k k>1 (DtoF if) if (DtoF jf) jf (div-div k>1 if jf)
143
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
301
235
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
302 di-next : {i i0 j j0 : ℕ} → Dividable i0 ((j0 + suc i) - suc j ) ∧ Dividable j0 ((i0 + suc j) - suc i) →
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
303 Dividable i0 ((j0 + i) - j ) ∧ Dividable j0 ((i0 + j) - i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
304 di-next {i} {i0} {j} {j0} x =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
305 ⟪ ( subst (λ k → Dividable i0 (k - suc j)) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
306 j0 + suc i ≡⟨ sym (+-assoc j0 1 i ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
307 (j0 + 1) + i ≡⟨ cong (λ k → k + i) (+-comm j0 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
308 suc (j0 + i) ∎ ) (proj1 x) ) ,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
309 ( subst (λ k → Dividable j0 (k - suc i)) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
310 i0 + suc j ≡⟨ sym (+-assoc i0 1 j ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
311 (i0 + 1) + j ≡⟨ cong (λ k → k + j) (+-comm i0 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
312 suc (i0 + j) ∎ ) (proj2 x) ) ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
313 where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
314
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
315 di-next1 : {i0 j j0 : ℕ} → Dividable (suc i0) ((j0 + 0) - (suc (suc j))) ∧ Dividable j0 (suc (i0 + suc (suc j)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
316 → Dividable (suc i0) ((suc (suc j) + i0) - suc j) ∧ Dividable (suc (suc j)) ((suc i0 + suc j) - i0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
317 di-next1 {i0} {j} {j0} x =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
318 ⟪ record { factor = 1 ; is-factor = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
319 1 * suc i0 + 0 ≡⟨ cong suc ( trans (+-comm _ 0) (+-comm _ 0) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
320 suc i0 ≡⟨ sym (minus+y-y {suc i0} {j}) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
321 (suc i0 + j) - j ≡⟨ cong (λ k → k - j ) (+-comm (suc i0) _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
322 (suc j + suc i0 ) - suc j ≡⟨ cong (λ k → k - suc j) (sym (+-assoc (suc j) 1 i0 )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
323 ((suc j + 1) + i0) - suc j ≡⟨ cong (λ k → (k + i0) - suc j) (+-comm _ 1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
324 (suc (suc j) + i0) - suc j ∎ } ,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
325 subst (λ k → Dividable (suc (suc j)) k) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
326 suc (suc j) ≡⟨ sym ( minus+y-y {suc (suc j)}{i0} ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
327 (suc (suc j) + i0 ) - i0 ≡⟨ cong (λ k → (k + i0) - i0) (cong suc (+-comm 1 _ )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
328 ((suc j + 1) + i0 ) - i0 ≡⟨ cong (λ k → k - i0) (+-assoc (suc j) 1 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
329 (suc j + suc i0 ) - i0 ≡⟨ cong (λ k → k - i0) (+-comm (suc j) _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
330 ((suc i0 + suc j) - i0) ∎ ) div= ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
331 where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
332
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
333 gcd>0 : ( i j : ℕ ) → 0 < i → 0 < j → 0 < gcd i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
334 gcd>0 i j 0<i 0<j = gcd>01 i i j j 0<i 0<j where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
335 gcd>01 : ( i i0 j j0 : ℕ ) → 0 < i0 → 0 < j0 → gcd1 i i0 j j0 > 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
336 gcd>01 zero i0 zero j0 0<i 0<j with <-cmp i0 j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
337 ... | tri< a ¬b ¬c = 0<i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
338 ... | tri≈ ¬a refl ¬c = 0<i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
339 ... | tri> ¬a ¬b c = 0<j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
340 gcd>01 zero i0 (suc zero) j0 0<i 0<j = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
341 gcd>01 zero zero (suc (suc j)) j0 0<i 0<j = 0<j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
342 gcd>01 zero (suc i0) (suc (suc j)) j0 0<i 0<j = gcd>01 i0 (suc i0) (suc j) (suc (suc j)) 0<i (s≤s z≤n) -- 0 < suc (suc j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
343 gcd>01 (suc zero) i0 zero j0 0<i 0<j = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
344 gcd>01 (suc (suc i)) i0 zero zero 0<i 0<j = 0<i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
345 gcd>01 (suc (suc i)) i0 zero (suc j0) 0<i 0<j = gcd>01 (suc i) (suc (suc i)) j0 (suc j0) (s≤s z≤n) 0<j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
346 gcd>01 (suc i) i0 (suc j) j0 0<i 0<j = subst (λ k → 0 < k ) (sym (gcd033 i i0 j j0 )) (gcd>01 i i0 j j0 0<i 0<j ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
347 gcd033 : (i i0 j j0 : ℕ) → gcd1 (suc i) i0 (suc j) j0 ≡ gcd1 i i0 j j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
348 gcd033 zero zero zero zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
349 gcd033 zero zero (suc j) zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
350 gcd033 (suc i) zero j zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
351 gcd033 zero zero zero (suc j0) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
352 gcd033 (suc i) zero zero (suc j0) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
353 gcd033 zero zero (suc j) (suc j0) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
354 gcd033 (suc i) zero (suc j) (suc j0) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
355 gcd033 zero (suc i0) j j0 = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
356 gcd033 (suc i) (suc i0) j j0 = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
357
238
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
358 -- gcd loop invariant
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
359 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
360 record GCD ( i i0 j j0 : ℕ ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
361 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
362 i<i0 : i ≤ i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
363 j<j0 : j ≤ j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
364 div-i : Dividable i0 ((j0 + i) - j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
365 div-j : Dividable j0 ((i0 + j) - i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
366
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
367 div-11 : {i j : ℕ } → Dividable i ((j + i) - j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
368 div-11 {i} {j} = record { factor = 1 ; is-factor = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
369 1 * i + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
370 i + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
371 i ≡⟨ sym (minus+y-y {i} {j}) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
372 (i + j ) - j ≡⟨ cong (λ k → k - j ) (+-comm i j ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
373 (j + i) - j ∎ } where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
374
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
375 div→gcd : {n k : ℕ } → k > 1 → Dividable k n → gcd n k ≡ k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
376 div→gcd {n} {k} k>1 = n-induction {_} {_} {ℕ} {λ m → Dividable k m → gcd m k ≡ k } (λ x → x) I n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
377 decl : {m : ℕ } → 0 < m → m - k < m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
378 decl {m} 0<m = y-x<y (<-trans a<sa k>1 ) 0<m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
379 ind : (m : ℕ ) → (Dividable k (m - k) → gcd (m - k) k ≡ k) → Dividable k m → gcd m k ≡ k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
380 ind m prev d with <-cmp (suc m) k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
381 ... | tri≈ ¬a refl ¬c = ⊥-elim ( div+1 k>1 d div= )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
382 ... | tri> ¬a ¬b c = subst (λ g → g ≡ k) ind1 ( prev (proj2 (div-div k>1 div= d))) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
383 ind1 : gcd (m - k) k ≡ gcd m k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
384 ind1 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
385 gcd (m - k) k ≡⟨ sym (gcd+j (m - k) _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
386 gcd (m - k + k) k ≡⟨ cong (λ g → gcd g k) (minus+n {m} {k} c) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
387 gcd m k ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
388 ... | tri< a ¬b ¬c with <-cmp 0 m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
389 ... | tri< a₁ ¬b₁ ¬c₁ = ⊥-elim ( div<k k>1 a₁ (<-trans a<sa a) d )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
390 ... | tri≈ ¬a refl ¬c₁ = subst (λ g → g ≡ k ) (gcdsym {k} {0} ) (gcd20 k)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
391 fzero : (m : ℕ) → (m - k) ≡ zero → Dividable k m → gcd m k ≡ k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
392 fzero 0 eq d = trans (gcdsym {0} {k} ) (gcd20 k)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
393 fzero (suc m) eq d with <-cmp (suc m) k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
394 ... | tri< a ¬b ¬c = ⊥-elim ( div<k k>1 (s≤s z≤n) a d )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
395 ... | tri≈ ¬a refl ¬c = gcdmm k k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
396 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym eq) (minus>0 c) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
397 I : Ninduction ℕ _ (λ x → x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
398 I = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
399 pnext = λ p → p - k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
400 ; fzero = λ {m} eq → fzero m eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
401 ; decline = λ {m} lt → decl lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
402 ; ind = λ {p} prev → ind p prev
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
403 }
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
404
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
405 GCDi : {i j : ℕ } → GCD i i j j
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
406 GCDi {i} {j} = record { i<i0 = refl-≤ ; j<j0 = refl-≤ ; div-i = div-11 {i} {j} ; div-j = div-11 {j} {i} }
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
407
238
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
408 GCD-sym : {i i0 j j0 : ℕ} → GCD i i0 j j0 → GCD j j0 i i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
409 GCD-sym g = record { i<i0 = GCD.j<j0 g ; j<j0 = GCD.i<i0 g ; div-i = GCD.div-j g ; div-j = GCD.div-i g }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
410
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
411 pred-≤ : {i i0 : ℕ } → suc i ≤ suc i0 → i ≤ suc i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
412 pred-≤ {i} {i0} (s≤s lt) = ≤-trans lt refl-≤s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
414 gcd-next : {i i0 j j0 : ℕ} → GCD (suc i) i0 (suc j) j0 → GCD i i0 j j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
415 gcd-next {i} {0} {j} {0} ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
416 gcd-next {i} {suc i0} {j} {suc j0} g = record { i<i0 = pred-≤ (GCD.i<i0 g) ; j<j0 = pred-≤ (GCD.j<j0 g)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
417 ; div-i = proj1 (di-next {i} {suc i0} {j} {suc j0} ⟪ GCD.div-i g , GCD.div-j g ⟫ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
418 ; div-j = proj2 (di-next {i} {suc i0} {j} {suc j0} ⟪ GCD.div-i g , GCD.div-j g ⟫ ) }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
419
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
420 gcd-next1 : {i0 j j0 : ℕ} → GCD 0 (suc i0) (suc (suc j)) j0 → GCD i0 (suc i0) (suc j) (suc (suc j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
421 gcd-next1 {i0} {j} {j0} g = record { i<i0 = refl-≤s ; j<j0 = refl-≤s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
422 ; div-i = proj1 (di-next1 ⟪ GCD.div-i g , GCD.div-j g ⟫ ) ; div-j = proj2 (di-next1 ⟪ GCD.div-i g , GCD.div-j g ⟫ ) }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
423
243
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 242
diff changeset
424
231
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
425 -- gcd-dividable1 : ( i i0 j j0 : ℕ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
426 -- → Dividable i0 (j0 + i - j ) ∨ Dividable j0 (i0 + j - i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
427 -- → Dividable ( gcd1 i i0 j j0 ) i0 ∧ Dividable ( gcd1 i i0 j j0 ) j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
428 -- gcd-dividable1 zero i0 zero j0 with <-cmp i0 j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
429 -- ... | tri< a ¬b ¬c = ⟪ div= , {!!} ⟫ -- Dividable i0 (j0 + i - j ) ∧ Dividable j0 (i0 + j - i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
430 -- ... | tri≈ ¬a refl ¬c = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
431 -- ... | tri> ¬a ¬b c = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
432 -- gcd-dividable1 zero i0 (suc zero) j0 = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
433 -- gcd-dividable1 i i0 j j0 = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 230
diff changeset
434
227
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
435 gcd-dividable : ( i j : ℕ )
212
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
436 → Dividable ( gcd i j ) i ∧ Dividable ( gcd i j ) j
227
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
437 gcd-dividable i j = f-induction {_} {_} {ℕ ∧ ℕ}
209
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
438 {λ p → Dividable ( gcd (proj1 p) (proj2 p) ) (proj1 p) ∧ Dividable ( gcd (proj1 p) (proj2 p) ) (proj2 p)} F I ⟪ i , j ⟫ where
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
439 F : ℕ ∧ ℕ → ℕ
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
440 F ⟪ 0 , 0 ⟫ = 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
441 F ⟪ 0 , suc j ⟫ = 0
222
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
442 F ⟪ suc i , 0 ⟫ = 0
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
443 F ⟪ suc i , suc j ⟫ with <-cmp i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
444 ... | tri< a ¬b ¬c = suc j
209
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
445 ... | tri≈ ¬a b ¬c = 0
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
446 ... | tri> ¬a ¬b c = suc i
209
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
447 F0 : { i j : ℕ } → F ⟪ i , j ⟫ ≡ 0 → (i ≡ j) ∨ (i ≡ 0 ) ∨ (j ≡ 0)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
448 F0 {zero} {zero} p = case1 refl
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
449 F0 {zero} {suc j} p = case2 (case1 refl)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
450 F0 {suc i} {zero} p = case2 (case2 refl)
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
451 F0 {suc i} {suc j} p with <-cmp i j
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
452 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≡< (sym p) (s≤s z≤n ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
453 ... | tri≈ ¬a refl ¬c = case1 refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
454 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym p) (s≤s z≤n ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
455 F00 : {p : ℕ ∧ ℕ} → F p ≡ zero → Dividable (gcd (proj1 p) (proj2 p)) (proj1 p) ∧ Dividable (gcd (proj1 p) (proj2 p)) (proj2 p)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
456 F00 {⟪ i , j ⟫} eq with F0 {i} {j} eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
457 ... | case1 refl = ⟪ subst (λ k → Dividable k i) (sym (gcdmm i i)) div= , subst (λ k → Dividable k i) (sym (gcdmm i i)) div= ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
458 ... | case2 (case1 refl) = ⟪ subst (λ k → Dividable k i) (sym (trans (gcdsym {0} {j} ) (gcd20 j)))div0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
459 , subst (λ k → Dividable k j) (sym (trans (gcdsym {0} {j}) (gcd20 j))) div= ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
460 ... | case2 (case2 refl) = ⟪ subst (λ k → Dividable k i) (sym (gcd20 i)) div=
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
461 , subst (λ k → Dividable k j) (sym (gcd20 i)) div0 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
462 Fsym : {i j : ℕ } → F ⟪ i , j ⟫ ≡ F ⟪ j , i ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
463 Fsym {0} {0} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
464 Fsym {0} {suc j} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
465 Fsym {suc i} {0} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
466 Fsym {suc i} {suc j} with <-cmp i j | <-cmp j i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
467 ... | tri< a ¬b ¬c | tri< a₁ ¬b₁ ¬c₁ = ⊥-elim (nat-<> a a₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
468 ... | tri< a ¬b ¬c | tri≈ ¬a b ¬c₁ = ⊥-elim (¬b (sym b))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
469 ... | tri< a ¬b ¬c | tri> ¬a ¬b₁ c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
470 ... | tri≈ ¬a refl ¬c | tri< a ¬b ¬c₁ = ⊥-elim (¬b refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
471 ... | tri≈ ¬a refl ¬c | tri≈ ¬a₁ refl ¬c₁ = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
472 ... | tri≈ ¬a refl ¬c | tri> ¬a₁ ¬b c = ⊥-elim (¬b refl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
473 ... | tri> ¬a ¬b c | tri< a ¬b₁ ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
474 ... | tri> ¬a ¬b c | tri≈ ¬a₁ b ¬c = ⊥-elim (¬b (sym b))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
475 ... | tri> ¬a ¬b c | tri> ¬a₁ ¬b₁ c₁ = ⊥-elim (nat-<> c c₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
476
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
477 record Fdec ( i j : ℕ ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
478 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
479 ni : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
480 nj : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
481 fdec : 0 < F ⟪ i , j ⟫ → F ⟪ ni , nj ⟫ < F ⟪ i , j ⟫
214
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
482
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
483 fd1 : ( i j k : ℕ ) → i < j → k ≡ j - i → F ⟪ suc i , k ⟫ < F ⟪ suc i , suc j ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
484 fd1 i j 0 i<j eq = ⊥-elim ( nat-≡< eq (minus>0 {i} {j} i<j ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
485 fd1 i j (suc k) i<j eq = fd2 i j k i<j eq where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
486 fd2 : ( i j k : ℕ ) → i < j → suc k ≡ j - i → F ⟪ suc i , suc k ⟫ < F ⟪ suc i , suc j ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
487 fd2 i j k i<j eq with <-cmp i k | <-cmp i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
488 ... | tri< a ¬b ¬c | tri< a₁ ¬b₁ ¬c₁ = fd3 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
489 fd3 : suc k < suc j -- suc j - suc i < suc j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
490 fd3 = subst (λ g → g < suc j) (sym eq) (y-x<y {suc i} {suc j} (s≤s z≤n) (s≤s z≤n))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
491 ... | tri< a ¬b ¬c | tri≈ ¬a b ¬c₁ = ⊥-elim (⊥-elim (¬a i<j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
492 ... | tri< a ¬b ¬c | tri> ¬a ¬b₁ c = ⊥-elim (⊥-elim (¬a i<j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
493 ... | tri≈ ¬a b ¬c | tri< a ¬b ¬c₁ = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
494 ... | tri≈ ¬a b ¬c | tri≈ ¬a₁ b₁ ¬c₁ = ⊥-elim (¬a₁ i<j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
495 ... | tri≈ ¬a b ¬c | tri> ¬a₁ ¬b c = s≤s z≤n -- i > j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
496 ... | tri> ¬a ¬b c | tri< a ¬b₁ ¬c = fd4 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
497 fd4 : suc i < suc j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
498 fd4 = s≤s a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
499 ... | tri> ¬a ¬b c | tri≈ ¬a₁ b ¬c = ⊥-elim (¬a₁ i<j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
500 ... | tri> ¬a ¬b c | tri> ¬a₁ ¬b₁ c₁ = ⊥-elim (¬a₁ i<j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
501
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
502 fedc0 : (i j : ℕ ) → Fdec i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
503 fedc0 0 0 = record { ni = 0 ; nj = 0 ; fdec = λ () }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
504 fedc0 (suc i) 0 = record { ni = suc i ; nj = 0 ; fdec = λ () }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
505 fedc0 0 (suc j) = record { ni = 0 ; nj = suc j ; fdec = λ () }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
506 fedc0 (suc i) (suc j) with <-cmp i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
507 ... | tri< i<j ¬b ¬c = record { ni = suc i ; nj = j - i ; fdec = λ lt → fd1 i j (j - i) i<j refl }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
508 ... | tri≈ ¬a refl ¬c = record { ni = suc i ; nj = suc j ; fdec = λ lt → ⊥-elim (nat-≡< fd0 lt) } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
509 fd0 : {i : ℕ } → 0 ≡ F ⟪ suc i , suc i ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
510 fd0 {i} with <-cmp i i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
511 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
512 ... | tri≈ ¬a b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
513 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
514 ... | tri> ¬a ¬b c = record { ni = i - j ; nj = suc j ; fdec = λ lt →
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
515 subst₂ (λ s t → s < t) (Fsym {suc j} {i - j}) (Fsym {suc j} {suc i}) (fd1 j i (i - j) c refl ) }
214
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
516
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
517 ind3 : {i j : ℕ } → i < j
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
518 → Dividable (gcd (suc i) (j - i)) (suc i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
519 → Dividable (gcd (suc i) (suc j)) (suc i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
520 ind3 {i} {j} a prev =
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
521 subst (λ k → Dividable k (suc i)) ( begin
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
522 gcd (suc i) (j - i) ≡⟨ gcdsym {suc i} {j - i} ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
523 gcd (j - i ) (suc i) ≡⟨ sym (gcd+j (j - i) (suc i)) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
524 gcd ((j - i) + suc i) (suc i) ≡⟨ cong (λ k → gcd k (suc i)) ( begin
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
525 (suc j - suc i) + suc i ≡⟨ minus+n {suc j} {suc i} (<-trans ( s≤s a) a<sa ) ⟩ -- i ≤ n → suc (suc i) ≤ suc (suc (suc n))
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
526 suc j ∎ ) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
527 gcd (suc j) (suc i) ≡⟨ gcdsym {suc j} {suc i} ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
528 gcd (suc i) (suc j) ∎ ) prev where open ≡-Reasoning
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
529 ind7 : {i j : ℕ } → (i < j ) → (j - i) + suc i ≡ suc j
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
530 ind7 {i} {j} a = begin (suc j - suc i) + suc i ≡⟨ minus+n {suc j} {suc i} (<-trans (s≤s a) a<sa) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
531 suc j ∎ where open ≡-Reasoning
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
532 ind6 : {i j k : ℕ } → i < j
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
533 → Dividable k (j - i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
534 → Dividable k (suc i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
535 → Dividable k (suc j)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
536 ind6 {i} {j} {k} i<j dj di = subst (λ g → Dividable k g ) (ind7 i<j) (proj1 (div+div dj di))
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
537 ind4 : {i j : ℕ } → i < j
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
538 → Dividable (gcd (suc i) (j - i)) (j - i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
539 → Dividable (gcd (suc i) (suc j)) (j - i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
540 ind4 {i} {j} i<j prev = subst (λ k → k) ( begin
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
541 Dividable (gcd (suc i) (j - i)) (j - i) ≡⟨ cong (λ k → Dividable k (j - i)) (gcdsym {suc i} ) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
542 Dividable (gcd (j - i ) (suc i) ) (j - i) ≡⟨ cong (λ k → Dividable k (j - i)) ( sym (gcd+j (j - i) (suc i))) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
543 Dividable (gcd ((j - i) + suc i) (suc i)) (j - i) ≡⟨ cong (λ k → Dividable (gcd k (suc i)) (j - i)) (ind7 i<j ) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
544 Dividable (gcd (suc j) (suc i)) (j - i) ≡⟨ cong (λ k → Dividable k (j - i)) (gcdsym {suc j} ) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
545 Dividable (gcd (suc i) (suc j)) (j - i) ∎ ) prev where open ≡-Reasoning
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
546
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
547 ind : ( i j : ℕ ) →
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
548 Dividable (gcd (Fdec.ni (fedc0 i j)) (Fdec.nj (fedc0 i j))) (Fdec.ni (fedc0 i j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
549 ∧ Dividable (gcd (Fdec.ni (fedc0 i j)) (Fdec.nj (fedc0 i j))) (Fdec.nj (fedc0 i j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
550 → Dividable (gcd i j) i ∧ Dividable (gcd i j) j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
551 ind zero zero prev = ind0 where
210
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 209
diff changeset
552 ind0 : Dividable (gcd zero zero) zero ∧ Dividable (gcd zero zero) zero
212
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
553 ind0 = ⟪ div0 , div0 ⟫
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
554 ind zero (suc j) prev = ind1 where
210
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 209
diff changeset
555 ind1 : Dividable (gcd zero (suc j)) zero ∧ Dividable (gcd zero (suc j)) (suc j)
212
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
556 ind1 = ⟪ div0 , subst (λ k → Dividable k (suc j)) (sym (trans (gcdsym {zero} {suc j}) (gcd20 (suc j)))) div= ⟫
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
557 ind (suc i) zero prev = ind2 where
210
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 209
diff changeset
558 ind2 : Dividable (gcd (suc i) zero) (suc i) ∧ Dividable (gcd (suc i) zero) zero
212
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
559 ind2 = ⟪ subst (λ k → Dividable k (suc i)) (sym (trans refl (gcd20 (suc i)))) div= , div0 ⟫
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
560 ind (suc i) (suc j) prev with <-cmp i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
561 ... | tri< a ¬b ¬c = ⟪ ind3 a (proj1 prev) , ind6 a (ind4 a (proj2 prev)) (ind3 a (proj1 prev) ) ⟫ where
213
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
562 ... | tri≈ ¬a refl ¬c = ⟪ ind5 , ind5 ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
563 ind5 : Dividable (gcd (suc i) (suc i)) (suc i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 212
diff changeset
564 ind5 = subst (λ k → Dividable k (suc j)) (sym (gcdmm (suc i) (suc i))) div=
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
565 ... | tri> ¬a ¬b c = ⟪ ind8 c (proj1 prev) (proj2 prev) , ind10 c (proj2 prev) ⟫ where
214
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
566 ind9 : {i j : ℕ} → i < j → gcd (j - i) (suc i) ≡ gcd (suc j) (suc i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
567 ind9 {i} {j} i<j = begin
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
568 gcd (j - i ) (suc i) ≡⟨ sym (gcd+j (j - i ) (suc i) ) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
569 gcd (j - i + suc i) (suc i) ≡⟨ cong (λ k → gcd k (suc i)) (ind7 i<j ) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
570 gcd (suc j) (suc i) ∎ where open ≡-Reasoning
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
571 ind8 : { i j : ℕ } → i < j
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
572 → Dividable (gcd (j - i) (suc i)) (j - i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
573 → Dividable (gcd (j - i) (suc i)) (suc i)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
574 → Dividable (gcd (suc j) (suc i)) (suc j)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
575 ind8 {i} {j} i<j dji di = ind6 i<j (subst (λ k → Dividable k (j - i)) (ind9 i<j) dji) (subst (λ k → Dividable k (suc i)) (ind9 i<j) di)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
576 ind10 : { i j : ℕ } → j < i
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
577 → Dividable (gcd (i - j) (suc j)) (suc j)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
578 → Dividable (gcd (suc i) (suc j)) (suc j)
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
579 ind10 {i} {j} j<i dji = subst (λ g → Dividable g (suc j) ) (begin
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
580 gcd (i - j) (suc j) ≡⟨ sym (gcd+j (i - j) (suc j)) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
581 gcd (i - j + suc j) (suc j) ≡⟨ cong (λ k → gcd k (suc j)) (ind7 j<i ) ⟩
906b43b94228 gcd-dividable done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 213
diff changeset
582 gcd (suc i) (suc j) ∎ ) dji where open ≡-Reasoning
210
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 209
diff changeset
583
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
584 I : Finduction (ℕ ∧ ℕ) _ F
209
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
585 I = record {
224
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
586 fzero = F00
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
587 ; pnext = λ p → ⟪ Fdec.ni (fedc0 (proj1 p) (proj2 p)) , Fdec.nj (fedc0 (proj1 p) (proj2 p)) ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
588 ; decline = λ {p} lt → Fdec.fdec (fedc0 (proj1 p) (proj2 p)) lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 223
diff changeset
589 ; ind = λ {p} prev → ind (proj1 p ) ( proj2 p ) prev
209
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
590 }
1c537e2b8f69 ... f-induction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 208
diff changeset
591
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
592 f-div>0 : { k i : ℕ } → (d : Dividable k i ) → 0 < i → 0 < Dividable.factor d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
593 f-div>0 {k} {i} d 0<i with <-cmp 0 (Dividable.factor d)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
594 ... | tri< a ¬b ¬c = a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
595 ... | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
596 0 * k + 0 ≡⟨ cong (λ g → g * k + 0) b ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
597 Dividable.factor d * k + 0 ≡⟨ Dividable.is-factor d ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
598 i ∎ ) 0<i ) where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
599
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
600 gcd-≤i : ( i j : ℕ ) → 0 < i → i ≤ j → gcd i j ≤ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
601 gcd-≤i zero _ () z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
602 gcd-≤i (suc i) (suc j) _ (s≤s i<j) = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
603 gcd (suc i) (suc j) ≡⟨ sym m*1=m ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
604 gcd (suc i) (suc j) * 1 ≤⟨ *-monoʳ-≤ (gcd (suc i) (suc j)) (f-div>0 d (s≤s z≤n)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
605 gcd (suc i) (suc j) * f ≡⟨ +-comm 0 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
606 gcd (suc i) (suc j) * f + 0 ≡⟨ cong (λ k → k + 0) (*-comm (gcd (suc i) (suc j)) _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
607 Dividable.factor (proj1 (gcd-dividable (suc i) (suc j))) * gcd (suc i) (suc j) + 0 ≡⟨ Dividable.is-factor (proj1 (gcd-dividable (suc i) (suc j))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
608 suc i ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
609 d = proj1 (gcd-dividable (suc i) (suc j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
610 f = Dividable.factor (proj1 (gcd-dividable (suc i) (suc j)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
611 open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
612
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
613 gcd-≤ : { i j : ℕ } → 0 < i → 0 < j → gcd i j ≤ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
614 gcd-≤ {i} {j} 0<i 0<j with <-cmp i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
615 ... | tri< a ¬b ¬c = gcd-≤i i j 0<i (<to≤ a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
616 ... | tri≈ ¬a refl ¬c = gcd-≤i i j 0<i refl-≤
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
617 ... | tri> ¬a ¬b c = ≤-trans (subst (λ k → k ≤ j) (gcdsym {j} {i}) (gcd-≤i j i 0<j (<to≤ c))) (<to≤ c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
618
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
619 record Euclid (i j gcd : ℕ ) : Set where
233
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 232
diff changeset
620 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 232
diff changeset
621 eqa : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 232
diff changeset
622 eqb : ℕ
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
623 is-equ< : eqa * i > eqb * j → (eqa * i) - (eqb * j) ≡ gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
624 is-equ> : eqb * j > eqa * i → (eqb * j) - (eqa * i) ≡ gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
625 is-equ= : eqa * i ≡ eqb * j → 0 ≡ gcd
234
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 233
diff changeset
626
235
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
627 ge3 : {a b c d : ℕ } → b > a → b - a ≡ d - c → d > c
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 234
diff changeset
628 ge3 {a} {b} {c} {d} b>a eq = minus>0→x<y (subst (λ k → 0 < k ) eq (minus>0 b>a))
234
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 233
diff changeset
629
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
630 ge01 : ( i0 j j0 ea eb : ℕ )
239
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 238
diff changeset
631 → ( di : GCD 0 (suc i0) (suc (suc j)) j0 )
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
632 → (((ea + eb * (Dividable.factor (GCD.div-i di))) * suc i0) ≡ (ea * suc i0) + (eb * (Dividable.factor (GCD.div-i di)) ) * suc i0 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
633 ∧ ( (eb * j0) ≡ (eb * suc (suc j) + (eb * (Dividable.factor (GCD.div-i di)) ) * suc i0) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
634 ge01 i0 j j0 ea eb di = ⟪ ge011 , ge012 ⟫ where
239
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 238
diff changeset
635 f = Dividable.factor (GCD.div-i di)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 238
diff changeset
636 ge4 : suc (j0 + 0) > suc (suc j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 238
diff changeset
637 ge4 = subst (λ k → k > suc (suc j)) (+-comm 0 _ ) ( s≤s (GCD.j<j0 di ))
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
638 ge011 : (ea + eb * f) * suc i0 ≡ ea * suc i0 + eb * f * suc i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
639 ge011 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
640 (ea + eb * f) * suc i0 ≡⟨ *-distribʳ-+ (suc i0) ea _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
641 ea * suc i0 + eb * f * suc i0 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
642 ge012 : eb * j0 ≡ eb * suc (suc j) + eb * f * suc i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
643 ge012 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
644 eb * j0 ≡⟨ cong (λ k → eb * k) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
645 j0 ≡⟨ +-comm 0 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
646 j0 + 0 ≡⟨ sym (minus+n {j0 + 0} {suc (suc j)} ge4) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
647 ((j0 + 0) - (suc (suc j))) + suc (suc j) ≡⟨ +-comm _ (suc (suc j)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
648 suc (suc j) + ((j0 + 0) - suc (suc j)) ≡⟨ cong (λ k → suc (suc j) + k ) (sym (Dividable.is-factor (GCD.div-i di))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
649 suc (suc j) + (f * suc i0 + 0) ≡⟨ cong (λ k → suc (suc j) + k ) ( +-comm _ 0 ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
650 suc (suc j) + (f * suc i0 ) ∎ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
651 eb * (suc (suc j) + (f * suc i0 ) ) ≡⟨ *-distribˡ-+ eb (suc (suc j)) (f * suc i0) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
652 eb * suc (suc j) + eb * (f * suc i0) ≡⟨ cong (λ k → eb * suc (suc j) + k ) ((sym (*-assoc eb _ _ )) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
653 eb * suc (suc j) + eb * f * suc i0 ∎ where open ≡-Reasoning
239
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 238
diff changeset
654
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
655 ge20 : {i0 j0 : ℕ } → i0 ≡ 0 → 0 ≡ gcd1 zero i0 zero j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
656 ge20 {i0} {zero} refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
657 ge20 {i0} {suc j0} refl = refl
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
658
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
659 gcd-euclid1 : ( i i0 j j0 : ℕ ) → GCD i i0 j j0 → Euclid i0 j0 (gcd1 i i0 j j0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
660 gcd-euclid1 zero i0 zero j0 di with <-cmp i0 j0
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
661 ... | tri< a' ¬b ¬c = record { eqa = 1 ; eqb = 0 ; is-equ< = λ _ → +-comm _ 0 ; is-equ> = λ () ; is-equ= = ge21 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
662 ge21 : 1 * i0 ≡ 0 * j0 → 0 ≡ i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
663 ge21 eq = trans (sym eq) (+-comm i0 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
664 ... | tri≈ ¬a refl ¬c = record { eqa = 1 ; eqb = 0 ; is-equ< = λ _ → +-comm _ 0 ; is-equ> = λ () ; is-equ= = λ eq → trans (sym eq) (+-comm i0 0) }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
665 ... | tri> ¬a ¬b c = record { eqa = 0 ; eqb = 1 ; is-equ< = λ () ; is-equ> = λ _ → +-comm _ 0 ; is-equ= = ge22 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
666 ge22 : 0 * i0 ≡ 1 * j0 → 0 ≡ j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
667 ge22 eq = trans eq (+-comm j0 0)
243
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 242
diff changeset
668 -- i<i0 : zero ≤ i0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 242
diff changeset
669 -- j<j0 : 1 ≤ j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 242
diff changeset
670 -- div-i : Dividable i0 ((j0 + zero) - 1) -- fi * i0 ≡ (j0 + zero) - 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 242
diff changeset
671 -- div-j : Dividable j0 ((i0 + 1) - zero) -- fj * j0 ≡ (i0 + 1) - zero
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
672 gcd-euclid1 zero i0 (suc zero) j0 di = record { eqa = 1 ; eqb = Dividable.factor (GCD.div-j di) ; is-equ< = λ lt → ⊥-elim ( ge7 lt) ; is-equ> = λ _ → ge6
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
673 ; is-equ= = λ eq → ⊥-elim (nat-≡< (sym (minus<=0 (subst (λ k → k ≤ 1 * i0) eq refl-≤ ))) (subst (λ k → 0 < k) (sym ge6) a<sa )) } where
244
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
674 ge6 : (Dividable.factor (GCD.div-j di) * j0) - (1 * i0) ≡ gcd1 zero i0 1 j0
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
675 ge6 = begin
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
676 (Dividable.factor (GCD.div-j di) * j0) - (1 * i0) ≡⟨ cong (λ k → k - (1 * i0)) (+-comm 0 _) ⟩
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
677 (Dividable.factor (GCD.div-j di) * j0 + 0) - (1 * i0) ≡⟨ cong (λ k → k - (1 * i0)) (Dividable.is-factor (GCD.div-j di) ) ⟩
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
678 ((i0 + 1) - zero) - (1 * i0) ≡⟨ refl ⟩
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
679 (i0 + 1) - (i0 + 0) ≡⟨ minus+yx-yz {_} {i0} {0} ⟩
243
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 242
diff changeset
680 1 ∎ where open ≡-Reasoning
244
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
681 ge7 : ¬ ( 1 * i0 > Dividable.factor (GCD.div-j di) * j0 )
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
682 ge7 lt = ⊥-elim ( nat-≡< (sym ( minus<=0 (<to≤ lt))) (subst (λ k → 0 < k) (sym ge6) (s≤s z≤n)))
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
683 gcd-euclid1 zero zero (suc (suc j)) j0 di = record { eqa = 0 ; eqb = 1 ; is-equ< = λ () ; is-equ> = λ _ → +-comm _ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
684 ; is-equ= = λ eq → subst (λ k → 0 ≡ k) (+-comm _ 0) eq }
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
685 gcd-euclid1 zero (suc i0) (suc (suc j)) j0 di with gcd-euclid1 i0 (suc i0) (suc j) (suc (suc j)) ( gcd-next1 di )
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
686 ... | e = record { eqa = ea + eb * f ; eqb = eb
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
687 ; is-equ= = λ eq → Euclid.is-equ= e (ge23 eq)
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
688 ; is-equ< = λ lt → subst (λ k → ((ea + eb * f) * suc i0) - (eb * j0) ≡ k ) (Euclid.is-equ< e (ge3 lt (ge1 ))) (ge1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
689 ; is-equ> = λ lt → subst (λ k → (eb * j0) - ((ea + eb * f) * suc i0) ≡ k ) (Euclid.is-equ> e (ge3 lt (ge2 ))) (ge2 ) } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
690 ea = Euclid.eqa e
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
691 eb = Euclid.eqb e
238
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 237
diff changeset
692 f = Dividable.factor (GCD.div-i di)
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
693 ge1 : ((ea + eb * f) * suc i0) - (eb * j0) ≡ (ea * suc i0) - (eb * suc (suc j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
694 ge1 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
695 ((ea + eb * f) * suc i0) - (eb * j0) ≡⟨ cong₂ (λ j k → j - k ) (proj1 (ge01 i0 j j0 ea eb di)) (proj2 (ge01 i0 j j0 ea eb di)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
696 (ea * suc i0 + (eb * f ) * suc i0 ) - ( eb * suc (suc j) + ((eb * f) * (suc i0)) ) ≡⟨ minus+xy-zy {ea * suc i0} {(eb * f ) * suc i0} {eb * suc (suc j)} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
697 (ea * suc i0) - (eb * suc (suc j)) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
698 ge2 : (eb * j0) - ((ea + eb * f) * suc i0) ≡ (eb * suc (suc j)) - (ea * suc i0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
699 ge2 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
700 (eb * j0) - ((ea + eb * f) * suc i0) ≡⟨ cong₂ (λ j k → j - k ) (proj2 (ge01 i0 j j0 ea eb di)) (proj1 (ge01 i0 j j0 ea eb di)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
701 ( eb * suc (suc j) + ((eb * f) * (suc i0)) ) - (ea * suc i0 + (eb * f ) * suc i0 ) ≡⟨ minus+xy-zy {eb * suc (suc j)}{(eb * f ) * suc i0} {ea * suc i0} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
702 (eb * suc (suc j)) - (ea * suc i0) ∎ where open ≡-Reasoning
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
703 ge23 : (ea + eb * f) * suc i0 ≡ eb * j0 → ea * suc i0 ≡ eb * suc (suc j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
704 ge23 eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
705 ea * suc i0 ≡⟨ sym (minus+y-y {_} {(eb * f ) * suc i0} ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
706 (ea * suc i0 + ((eb * f ) * suc i0 )) - ((eb * f ) * suc i0 ) ≡⟨ cong (λ k → k - ((eb * f ) * suc i0 )) (sym ( proj1 (ge01 i0 j j0 ea eb di))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
707 ((ea + eb * f) * suc i0) - ((eb * f ) * suc i0 ) ≡⟨ cong (λ k → k - ((eb * f ) * suc i0 )) eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
708 (eb * j0) - ((eb * f ) * suc i0 ) ≡⟨ cong (λ k → k - ((eb * f ) * suc i0 )) ( proj2 (ge01 i0 j j0 ea eb di)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
709 (eb * suc (suc j) + ((eb * f ) * suc i0 )) - ((eb * f ) * suc i0 ) ≡⟨ minus+y-y {_} {(eb * f ) * suc i0 } ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
710 eb * suc (suc j) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
711 gcd-euclid1 (suc zero) i0 zero j0 di = record { eqb = 1 ; eqa = Dividable.factor (GCD.div-i di) ; is-equ> = λ lt → ⊥-elim ( ge7' lt) ; is-equ< = λ _ → ge6'
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
712 ; is-equ= = λ eq → ⊥-elim (nat-≡< (sym (minus<=0 (subst (λ k → k ≤ 1 * j0) (sym eq) refl-≤ ))) (subst (λ k → 0 < k) (sym ge6') a<sa )) } where
244
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
713 ge6' : (Dividable.factor (GCD.div-i di) * i0) - (1 * j0) ≡ gcd1 (suc zero) i0 zero j0
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
714 ge6' = begin
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
715 (Dividable.factor (GCD.div-i di) * i0) - (1 * j0) ≡⟨ cong (λ k → k - (1 * j0)) (+-comm 0 _) ⟩
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
716 (Dividable.factor (GCD.div-i di) * i0 + 0) - (1 * j0) ≡⟨ cong (λ k → k - (1 * j0)) (Dividable.is-factor (GCD.div-i di) ) ⟩
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
717 ((j0 + 1) - zero) - (1 * j0) ≡⟨ refl ⟩
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
718 (j0 + 1) - (j0 + 0) ≡⟨ minus+yx-yz {_} {j0} {0} ⟩
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
719 1 ∎ where open ≡-Reasoning
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
720 ge7' : ¬ ( 1 * j0 > Dividable.factor (GCD.div-i di) * i0 )
08994de7c82f gcd-euclid1 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 243
diff changeset
721 ge7' lt = ⊥-elim ( nat-≡< (sym ( minus<=0 (<to≤ lt))) (subst (λ k → 0 < k) (sym ge6') (s≤s z≤n)))
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
722 gcd-euclid1 (suc (suc i)) i0 zero zero di = record { eqb = 0 ; eqa = 1 ; is-equ> = λ () ; is-equ< = λ _ → +-comm _ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
723 ; is-equ= = λ eq → subst (λ k → 0 ≡ k) (+-comm _ 0) (sym eq) }
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
724 gcd-euclid1 (suc (suc i)) i0 zero (suc j0) di with gcd-euclid1 (suc i) (suc (suc i)) j0 (suc j0) (GCD-sym (gcd-next1 (GCD-sym di)))
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
725 ... | e = record { eqa = ea ; eqb = eb + ea * f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
726 ; is-equ= = λ eq → Euclid.is-equ= e (ge24 eq)
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
727 ; is-equ< = λ lt → subst (λ k → ((ea * i0) - ((eb + ea * f) * suc j0)) ≡ k ) (Euclid.is-equ< e (ge3 lt ge4)) ge4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
728 ; is-equ> = λ lt → subst (λ k → (((eb + ea * f) * suc j0) - (ea * i0)) ≡ k ) (Euclid.is-equ> e (ge3 lt ge5)) ge5 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
729 ea = Euclid.eqa e
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
730 eb = Euclid.eqb e
242
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
731 f = Dividable.factor (GCD.div-j di)
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
732 ge5 : (((eb + ea * f) * suc j0) - (ea * i0)) ≡ ((eb * suc j0) - (ea * suc (suc i)))
242
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
733 ge5 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
734 ((eb + ea * f) * suc j0) - (ea * i0) ≡⟨ cong₂ (λ j k → j - k ) (proj1 (ge01 j0 i i0 eb ea (GCD-sym di) )) (proj2 (ge01 j0 i i0 eb ea (GCD-sym di) )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
735 ( eb * suc j0 + (ea * f )* suc j0) - (ea * suc (suc i) + (ea * f )* suc j0) ≡⟨ minus+xy-zy {_} {(ea * f )* suc j0} {ea * suc (suc i)} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
736 (eb * suc j0) - (ea * suc (suc i)) ∎ where open ≡-Reasoning
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
737 ge4 : ((ea * i0) - ((eb + ea * f) * suc j0)) ≡ ((ea * suc (suc i)) - (eb * suc j0))
242
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
738 ge4 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
739 (ea * i0) - ((eb + ea * f) * suc j0) ≡⟨ cong₂ (λ j k → j - k ) (proj2 (ge01 j0 i i0 eb ea (GCD-sym di) )) (proj1 (ge01 j0 i i0 eb ea (GCD-sym di) )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
740 (ea * suc (suc i) + (ea * f )* suc j0) - ( eb * suc j0 + (ea * f )* suc j0) ≡⟨ minus+xy-zy {ea * suc (suc i)} {(ea * f )* suc j0} { eb * suc j0} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 241
diff changeset
741 (ea * suc (suc i)) - (eb * suc j0) ∎ where open ≡-Reasoning
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
742 ge24 : ea * i0 ≡ (eb + ea * f) * suc j0 → ea * suc (suc i) ≡ eb * suc j0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
743 ge24 eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
744 ea * suc (suc i) ≡⟨ sym ( minus+y-y {_} {(ea * f ) * suc j0 }) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
745 (ea * suc (suc i) + (ea * f ) * suc j0 ) - ((ea * f ) * suc j0) ≡⟨ cong (λ k → k - ((ea * f ) * suc j0 )) (sym (proj2 (ge01 j0 i i0 eb ea (GCD-sym di) ))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
746 (ea * i0) - ((ea * f ) * suc j0) ≡⟨ cong (λ k → k - ((ea * f ) * suc j0 )) eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
747 ((eb + ea * f) * suc j0) - ((ea * f ) * suc j0) ≡⟨ cong (λ k → k - ((ea * f ) * suc j0 )) ((proj1 (ge01 j0 i i0 eb ea (GCD-sym di)))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
748 ( eb * suc j0 + (ea * f ) * suc j0 ) - ((ea * f ) * suc j0) ≡⟨ minus+y-y {_} {(ea * f ) * suc j0 } ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
749 eb * suc j0 ∎ where open ≡-Reasoning
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
750 gcd-euclid1 (suc zero) i0 (suc j) j0 di =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
751 gcd-euclid1 zero i0 j j0 (gcd-next di)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
752 gcd-euclid1 (suc (suc i)) i0 (suc j) j0 di =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 240
diff changeset
753 gcd-euclid1 (suc i) i0 j j0 (gcd-next di)
233
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 232
diff changeset
754
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
755 ge12 : (p x : ℕ) → 0 < x → 1 < p → ((i : ℕ ) → i < p → 0 < i → gcd p i ≡ 1) → ( gcd p x ≡ 1 ) ∨ ( Dividable p x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
756 ge12 p x 0<x 1<p prime with decD {p} {x} 1<p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
757 ... | yes y = case2 y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
758 ... | no nx with <-cmp (gcd p x ) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
759 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (s≤s (gcd>0 p x (<-trans a<sa 1<p) 0<x) ) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
760 ... | tri≈ ¬a b ¬c = case1 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
761 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym (prime (gcd p x) ge13 (<to≤ c) )) ge18 ) where
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
762 -- 1 < gcd p x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
763 ge13 : gcd p x < p -- gcd p x ≡ p → ¬ nx
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
764 ge13 with <-cmp (gcd p x ) p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
765 ... | tri< a ¬b ¬c = a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
766 ... | tri≈ ¬a b ¬c = ⊥-elim ( nx (subst (λ k → Dividable k x) b (proj2 (gcd-dividable p x ))))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
767 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> (gcd-≤ (<-trans a<sa 1<p) 0<x) c )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
768 ge19 : Dividable (gcd p x) p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
769 ge19 = proj1 (gcd-dividable p x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
770 ge18 : 1 < gcd p (gcd p x) -- Dividable p (gcd p x) → gcd p (gcd p x) ≡ (gcd p x) > 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
771 ge18 = subst (λ k → 1 < k ) (sym (div→gcd {p} {gcd p x} c ge19 )) c
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
772
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
773 gcd-euclid : ( p a b : ℕ ) → 1 < p → 0 < a → 0 < b → ((i : ℕ ) → i < p → 0 < i → gcd p i ≡ 1) → Dividable p (a * b) → Dividable p a ∨ Dividable p b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
774 gcd-euclid p a b 1<p 0<a 0<b prime div-ab with decD {p} {a} 1<p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
775 ... | yes y = case1 y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
776 ... | no np = case2 ge16 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
777 f = Dividable.factor div-ab
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
778 ge10 : gcd p a ≡ 1
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
779 ge10 with ge12 p a 0<a 1<p prime
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
780 ... | case1 x = x
246
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 245
diff changeset
781 ... | case2 x = ⊥-elim ( np x )
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
782 ge11 : Euclid p a (gcd p a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
783 ge11 = gcd-euclid1 p p a a GCDi
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
784 ea = Euclid.eqa ge11
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
785 eb = Euclid.eqb ge11
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
786 ge18 : (f * eb) * p ≡ b * (a * eb )
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
787 ge18 = begin
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
788 (f * eb) * p ≡⟨ *-assoc (f) (eb) p ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
789 f * (eb * p) ≡⟨ cong (λ k → f * k) (*-comm _ p) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
790 f * (p * eb ) ≡⟨ sym (*-assoc (f) p (eb) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
791 (f * p ) * eb ≡⟨ cong (λ k → k * eb ) (+-comm 0 (f * p )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
792 (f * p + 0) * eb ≡⟨ cong (λ k → k * eb) (((Dividable.is-factor div-ab))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
793 (a * b) * eb ≡⟨ cong (λ k → k * eb) (*-comm a b) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
794 (b * a) * eb ≡⟨ *-assoc b a (eb ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
795 b * (a * eb ) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
796 ge19 : ( ea * p ) ≡ ( eb * a ) → ((b * ea) - (f * eb)) * p + 0 ≡ b
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 249
diff changeset
797 ge19 eq = ⊥-elim ( nat-≡< (Euclid.is-equ= ge11 eq) (subst (λ k → 0 < k ) (sym ge10) a<sa ) )
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
798 ge14 : ( ea * p ) > ( eb * a ) → ((b * ea) - (f * eb)) * p + 0 ≡ b
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
799 ge14 lt = begin
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
800 (((b * ea) - (f * eb)) * p) + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
801 ((b * ea) - ((f * eb)) * p) ≡⟨ distr-minus-* {_} {f * eb} {p} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
802 ((b * ea) * p) - (((f * eb) * p)) ≡⟨ cong (λ k → ((b * ea) * p) - k ) ge18 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
803 ((b * ea) * p) - (b * (a * eb )) ≡⟨ cong (λ k → k - (b * (a * eb)) ) (*-assoc b _ p) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
804 (b * (ea * p)) - (b * (a * eb )) ≡⟨ sym ( distr-minus-*' {b} {ea * p} {a * eb} ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
805 b * (( ea * p) - (a * eb) ) ≡⟨ cong (λ k → b * ( ( ea * p) - k)) (*-comm a (eb)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
806 (b * ( (ea * p)) - (eb * a) ) ≡⟨ cong (b *_) (Euclid.is-equ< ge11 lt )⟩
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
807 b * gcd p a ≡⟨ cong (b *_) ge10 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
808 b * 1 ≡⟨ m*1=m ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
809 b ∎ where open ≡-Reasoning
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
810 ge15 : ( ea * p ) < ( eb * a ) → ((f * eb) - (b * ea ) ) * p + 0 ≡ b
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
811 ge15 lt = begin
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
812 ((f * eb) - (b * ea) ) * p + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
813 ((f * eb) - (b * ea) ) * p ≡⟨ distr-minus-* {_} {b * ea} {p} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
814 ((f * eb) * p) - ((b * ea) * p) ≡⟨ cong (λ k → k - ((b * ea) * p) ) ge18 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
815 (b * (a * eb )) - ((b * ea) * p ) ≡⟨ cong (λ k → (b * (a * eb)) - k ) (*-assoc b _ p) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
816 (b * (a * eb )) - (b * (ea * p) ) ≡⟨ sym ( distr-minus-*' {b} {a * eb} {ea * p} ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
817 b * ( (a * eb) - (ea * p) ) ≡⟨ cong (λ k → b * ( k - ( ea * p) )) (*-comm a (eb)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
818 b * ( (eb * a) - (ea * p) ) ≡⟨ cong (b *_) (Euclid.is-equ> ge11 lt) ⟩
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
819 b * gcd p a ≡⟨ cong (b *_) ge10 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
820 b * 1 ≡⟨ m*1=m ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 246
diff changeset
821 b ∎ where open ≡-Reasoning
246
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 245
diff changeset
822 ge17 : (x y : ℕ ) → x ≡ y → x ≤ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 245
diff changeset
823 ge17 x x refl = refl-≤
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
824 ge16 : Dividable p b
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
825 ge16 with <-cmp ( ea * p ) ( eb * a )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
826 ... | tri< a ¬b ¬c = record { factor = (f * eb) - (b * ea) ; is-factor = ge15 a }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
827 ... | tri≈ ¬a eq ¬c = record { factor = (b * ea) - ( f * eb) ; is-factor = ge19 eq }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
828 ... | tri> ¬a ¬b c = record { factor = (b * ea) - (f * eb) ; is-factor = ge14 c }
245
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
829
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 244
diff changeset
830
206
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 205
diff changeset
831
167
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
832 gcdmul+1 : ( m n : ℕ ) → gcd (m * n + 1) n ≡ 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
833 gcdmul+1 zero n = gcd204 n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
834 gcdmul+1 (suc m) n = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
835 gcd (suc m * n + 1) n ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
836 gcd (n + m * n + 1) n ≡⟨ cong (λ k → gcd k n ) (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
837 n + m * n + 1 ≡⟨ cong (λ k → k + 1) (+-comm n _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
838 m * n + n + 1 ≡⟨ +-assoc (m * n) _ _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
839 m * n + (n + 1) ≡⟨ cong (λ k → m * n + k) (+-comm n _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
840 m * n + (1 + n) ≡⟨ sym ( +-assoc (m * n) _ _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
841 m * n + 1 + n ∎
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
842 ) ⟩
212
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 211
diff changeset
843 gcd (m * n + 1 + n) n ≡⟨ gcd+j (m * n + 1) n ⟩
167
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
844 gcd (m * n + 1) n ≡⟨ gcdmul+1 m n ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
845 1 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 166
diff changeset
846
227
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
847 --gcd-dividable : ( i j : ℕ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
848 -- → Dividable ( gcd i j ) i ∧ Dividable ( gcd i j ) j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 224
diff changeset
849
230
a72bcc6eadad prime done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 227
diff changeset
850 m*n=m→n : {m n : ℕ } → 0 < m → m * n ≡ m * 1 → n ≡ 1
a72bcc6eadad prime done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 227
diff changeset
851 m*n=m→n {suc m} {n} (s≤s lt) eq = *-cancelˡ-≡ m eq
a72bcc6eadad prime done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 227
diff changeset
852