annotate automaton-in-agda/src/root2.agda @ 186:08f4ec41ea93

even→gcd
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 14 Jun 2021 02:17:58 +0900
parents f9473f83f6e7
children 1402c5b17160
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module root2 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
3 open import Data.Nat
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
4 open import Data.Nat.Properties
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.Empty
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
6 open import Data.Unit using (⊤ ; tt)
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 open import Relation.Nullary
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Relation.Binary.PropositionalEquality
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
9 open import Relation.Binary.Definitions
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
148
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 147
diff changeset
11 open import gcd
159
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
12 open import even
142
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
13 open import nat
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
14
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
15 record Rational : Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
16 field
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
17 i j : ℕ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
18 coprime : gcd i j ≡ 1
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
159
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
20 even→gcd=2 : {n : ℕ} → even n → n > 0 → gcd n 2 ≡ 2
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
21 even→gcd=2 {suc (suc zero)} en (s≤s z≤n) = refl
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
22 even→gcd=2 {suc (suc (suc (suc n)))} en (s≤s z≤n) = begin
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
23 gcd (suc (suc (suc (suc n)))) 2 ≡⟨⟩
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
24 gcd (suc (suc n)) 2 ≡⟨ even→gcd=2 {suc (suc n)} en (s≤s z≤n) ⟩
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
25 2 ∎ where open ≡-Reasoning
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
26
186
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
27 even→gcd : (n m : ℕ) → even n → even m → even (gcd n m)
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
28 even→gcd zero zero en em = tt
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
29 even→gcd zero (suc (suc m)) en em = em
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
30 even→gcd (suc (suc n)) zero en em = en
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
31 even→gcd (suc (suc n)) (suc (suc m)) en em = ee2 n n (suc (suc m)) (suc (suc m)) refl refl
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
32 (subst (λ k → even k) (gcdsym {suc (suc m)} {n}) (ee2 m m n n refl refl (subst (λ k → even k) (gcdsym {n} {m}) ee1))) where
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
33 ee1 : even (gcd n m)
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
34 ee1 = even→gcd n m en em
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
35 ee2 : (i i0 j j0 : ℕ) → i ≡ i0 → j ≡ j0 → even (gcd1 i i j0 j0) → even (gcd1 (suc (suc i)) (suc (suc i)) j0 j0)
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
36 ee2 i i0 j j0 i=i0 j=j0 e = {!!}
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
37
159
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
38 even^2 : {n : ℕ} → even ( n * n ) → even n
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
39 even^2 {n} en with even? n
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
40 ... | yes y = y
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
41 ... | no ne = ⊥-elim ( odd4 ((2 * m) + 2 * m * suc (2 * m)) (n+even {2 * m} {2 * m * suc (2 * m)} ee3 ee4) (subst (λ k → even k) ee2 en )) where
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
42 m : ℕ
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
43 m = Odd.j ( odd3 n ne )
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
44 ee3 : even (2 * m)
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
45 ee3 = subst (λ k → even k ) (*-comm m 2) (n*even {m} {2} tt )
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
46 ee4 : even ((2 * m) * suc (2 * m))
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
47 ee4 = even*n {(2 * m)} {suc (2 * m)} (even*n {2} {m} tt )
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
48 ee2 : n * n ≡ suc (2 * m) + ((2 * m) * (suc (2 * m) ))
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
49 ee2 = begin n * n ≡⟨ cong ( λ k → k * k) (Odd.is-twice (odd3 n ne)) ⟩
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
50 suc (2 * m) * suc (2 * m) ≡⟨ *-distribʳ-+ (suc (2 * m)) 1 ((2 * m) ) ⟩
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
51 (1 * suc (2 * m)) + 2 * m * suc (2 * m) ≡⟨ cong (λ k → k + 2 * m * suc (2 * m)) (begin
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
52 suc m + 1 * m + 0 * (suc m + 1 * m ) ≡⟨ +-comm (suc m + 1 * m) 0 ⟩
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
53 suc m + 1 * m ≡⟨⟩
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
54 suc (2 * m)
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
55 ∎) ⟩ suc (2 * m) + 2 * m * suc (2 * m) ∎ where open ≡-Reasoning
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
56
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
57 e3 : {i j : ℕ } → 2 * i ≡ 2 * j → i ≡ j
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
58 e3 {zero} {zero} refl = refl
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
59 e3 {suc x} {suc y} eq with <-cmp x y
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
60 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≡< eq (s≤s (<-trans (<-plus a) (<-plus-0 (s≤s (<-plus a ))))))
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
61 ... | tri≈ ¬a b ¬c = cong suc b
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
62 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym eq) (s≤s (<-trans (<-plus c) (<-plus-0 (s≤s (<-plus c ))))))
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
63
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
64 open Factor
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 159
diff changeset
65
186
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
66 -- gcd-div : ( i j k : ℕ ) → (if : Factor k i) (jf : Factor k j )
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
67 -- → remain if ≡ 0 → remain jf ≡ 0
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
68 -- → Dividable k ( gcd i j )
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
69
142
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
70 root2-irrational : ( n m : ℕ ) → n > 1 → m > 1 → 2 * n * n ≡ m * m → ¬ (gcd n m ≡ 1)
186
08f4ec41ea93 even→gcd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 185
diff changeset
71 root2-irrational n m n>1 m>1 2nm = rot13 ( gcd-div n m 2 f2 fm (f3 n rot7) refl ) where
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
72 rot13 : {m : ℕ } → Dividable 2 m → m ≡ 1 → ⊥
185
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
73 rot13 d refl with Dividable.factor d | Dividable.is-factor d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
74 ... | zero | ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
75 ... | suc n | ()
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
76 rot11 : {m : ℕ } → even m → Factor 2 m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
77 rot11 {zero} em = record { factor = 0 ; remain = 0 ; is-factor = refl }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
78 rot11 {suc zero} ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
79 rot11 {suc (suc m) } em = record { factor = suc (factor fc ) ; remain = remain fc ; is-factor = isfc } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
80 fc : Factor 2 m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
81 fc = rot11 {m} em
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
82 isfc : suc (factor fc) * 2 + remain fc ≡ suc (suc m)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
83 isfc = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
84 suc (factor fc) * 2 + remain fc ≡⟨ cong (λ k → k + remain fc) (*-distribʳ-+ 2 1 (factor fc)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
85 ((1 * 2) + (factor fc)* 2 ) + remain fc ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
86 ((1 + 1) + (factor fc)* 2 ) + remain fc ≡⟨ cong (λ k → k + remain fc) (+-assoc 1 1 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
87 (1 + (1 + (factor fc)* 2 )) + remain fc ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
88 suc (suc ((factor fc * 2) + remain fc )) ≡⟨ cong (λ x → suc (suc x)) (is-factor fc) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
89 suc (suc m) ∎ where open ≡-Reasoning
142
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
90 rot5 : {n : ℕ} → n > 1 → n > 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
91 rot5 {n} lt = <-trans a<sa lt
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
92 rot1 : even ( m * m )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
93 rot1 = subst (λ k → even k ) rot4 (n*even {n * n} {2} tt ) where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
94 rot4 : (n * n) * 2 ≡ m * m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
95 rot4 = begin
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
96 (n * n) * 2 ≡⟨ *-comm (n * n) 2 ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
97 2 * ( n * n ) ≡⟨ sym (*-assoc 2 n n) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
98 2 * n * n ≡⟨ 2nm ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
99 m * m ∎ where open ≡-Reasoning
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
100 E : Even m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
101 E = e2 m ( even^2 {m} ( rot1 ))
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
102 rot2 : 2 * n * n ≡ 2 * Even.j E * m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
103 rot2 = subst (λ k → 2 * n * n ≡ k * m ) (Even.is-twice E) 2nm
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
104 rot3 : n * n ≡ Even.j E * m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
105 rot3 = e3 ( begin
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
106 2 * (n * n) ≡⟨ sym (*-assoc 2 n _) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
107 2 * n * n ≡⟨ rot2 ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
108 2 * Even.j E * m ≡⟨ *-assoc 2 (Even.j E) m ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
109 2 * (Even.j E * m) ∎ ) where open ≡-Reasoning
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
110 rot7 : even n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
111 rot7 = even^2 {n} (subst (λ k → even k) (sym rot3) ((n*even {Even.j E} {m} ( even^2 {m} ( rot1 )))))
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
112 f2 : Factor 2 n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
113 f2 = rot11 rot7
185
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
114 f3 : ( n : ℕ) → (e : even n ) → remain (rot11 {n} e) ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
115 f3 zero e = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
116 f3 (suc (suc n)) e = f3 n e
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
117 fm : Factor 2 m
185
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
118 fm = record { factor = Even.j E ; remain = 0 ; is-factor = fm1 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
119 fm1 : Even.j E * 2 + 0 ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
120 fm1 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
121 Even.j E * 2 + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
122 Even.j E * 2 ≡⟨ *-comm (Even.j E) 2 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
123 2 * Even.j E ≡⟨ sym ( Even.is-twice E ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
124 m ∎ where open ≡-Reasoning
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125
185
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
126