57
|
1 module root2 where
|
|
2
|
141
|
3 open import Data.Nat
|
|
4 open import Data.Nat.Properties
|
57
|
5 open import Data.Empty
|
141
|
6 open import Data.Unit using (⊤ ; tt)
|
57
|
7 open import Relation.Nullary
|
|
8 open import Relation.Binary.PropositionalEquality
|
141
|
9 open import Relation.Binary.Definitions
|
57
|
10
|
|
11 even : (n : ℕ ) → Set
|
141
|
12 even zero = ⊤
|
|
13 even (suc zero) = ⊥
|
|
14 even (suc (suc n)) = even n
|
57
|
15
|
|
16 even? : (n : ℕ ) → Dec ( even n )
|
141
|
17 even? zero = yes tt
|
|
18 even? (suc zero) = no (λ ())
|
|
19 even? (suc (suc n)) = even? n
|
|
20
|
|
21 n+even : {n m : ℕ } → even n → even m → even ( n + m )
|
|
22 n+even {zero} {zero} tt tt = tt
|
|
23 n+even {zero} {suc m} tt em = em
|
|
24 n+even {suc (suc n)} {m} en em = n+even {n} {m} en em
|
|
25
|
|
26 n*even : {m n : ℕ } → even n → even ( m * n )
|
|
27 n*even {zero} {n} en = tt
|
|
28 n*even {suc m} {n} en = n+even {n} {m * n} en (n*even {m} {n} en)
|
|
29
|
|
30 even*n : {n m : ℕ } → even n → even ( n * m )
|
|
31 even*n {n} {m} en = subst even (*-comm m n) (n*even {m} {n} en)
|
|
32
|
|
33 gcd1 : ( i i0 j j0 : ℕ ) → ℕ
|
|
34 gcd1 zero i0 zero j0 = i0
|
|
35 gcd1 zero i0 (suc zero) j0 = 1
|
|
36 gcd1 zero zero (suc (suc j)) j0 = j0
|
|
37 gcd1 zero (suc i0) (suc (suc j)) j0 = gcd1 i0 (suc i0) (suc j) (suc (suc j))
|
|
38 gcd1 (suc zero) i0 zero j0 = 1
|
|
39 gcd1 (suc (suc i)) i0 zero zero = i0
|
|
40 gcd1 (suc (suc i)) i0 zero (suc j0) = gcd1 (suc i) (suc (suc i)) j0 (suc j0)
|
|
41 gcd1 (suc i) i0 (suc j) j0 = gcd1 i i0 j j0
|
|
42
|
|
43 gcd : ( i j : ℕ ) → ℕ
|
|
44 gcd i j = gcd1 i i j j
|
57
|
45
|
141
|
46 even→gcd=2 : {n : ℕ} → even n → n > 0 → gcd n 2 ≡ 2
|
|
47 even→gcd=2 {suc (suc zero)} en (s≤s z≤n) = refl
|
|
48 even→gcd=2 {suc (suc (suc (suc n)))} en (s≤s z≤n) = begin
|
|
49 gcd (suc (suc (suc (suc n)))) 2
|
|
50 ≡⟨⟩
|
|
51 gcd (suc (suc n)) 2
|
|
52 ≡⟨ even→gcd=2 {suc (suc n)} en (s≤s z≤n) ⟩
|
|
53 2
|
|
54 ∎ where open ≡-Reasoning
|
57
|
55
|
142
|
56 open import nat
|
141
|
57
|
145
|
58 -- gcd26 : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n m ≡ gcd (n - m) m
|
|
59 -- gcd25 : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n m ≡ gcd m n
|
|
60 -- gcd27 : { n m : ℕ} → n > 1 → m > 1 → n - m > 0 → gcd n k ≡ k → k ≤ n
|
143
|
61
|
142
|
62 gcd24 : { n m k : ℕ} → n > 1 → m > 1 → k > 1 → gcd n k ≡ k → gcd m k ≡ k → ¬ ( gcd n m ≡ 1 )
|
|
63 gcd24 {n} {m} {k} 1<n 1<m 1<k gn gm gnm = gcd21 n n m m k k 1<n 1<m 1<k gn gm gnm where
|
|
64 gcd22 : ( i i0 o o0 : ℕ ) → gcd1 (suc i) i0 (suc o) o0 ≡ gcd1 i i0 o o0
|
|
65 gcd22 zero i0 zero o0 = refl
|
|
66 gcd22 zero i0 (suc o) o0 = refl
|
|
67 gcd22 (suc i) i0 zero o0 = refl
|
|
68 gcd22 (suc i) i0 (suc o) o0 = refl
|
145
|
69 gcd23 : { j j0 : ℕ } → 1 < j → 1 < j0 → ¬ (gcd1 zero zero j j0 ≡ 1)
|
|
70 gcd23 {zero} {suc j0} 1<j 1<j0 ()
|
|
71 gcd23 {suc zero} {suc j0} 1<j 1<j0 gn = nat-<≡ 1<j
|
|
72 gcd23 {suc (suc j)} {suc .0} 1<j 1<j0 refl = nat-<≡ 1<j0
|
143
|
73 1<ss : {j : ℕ} → 1 < suc (suc j)
|
|
74 1<ss = s≤s (s≤s z≤n)
|
142
|
75 gcd21 : ( i i0 j j0 o o0 : ℕ ) → 1 < i0 → 1 < j0 → 1 < o0 → gcd1 i i0 o o0 ≡ k → gcd1 j j0 o o0 ≡ k → gcd1 i i0 j j0 ≡ 1 → ⊥
|
144
|
76 gcd21 zero i0 zero j0 o o0 1<i 1<j 1<o refl gm gnm = nat-≡< (sym gnm) 1<i
|
145
|
77 --- gcd1 zero i0 o o0 ≡ k o = suc zero → k ≡ 1
|
|
78 --- i0 = zero , o = suc (suc o) → o0 = k -> gcd1 zero zero (suc j) j0 ≡ 1
|
|
79 -- i0 = suc i0, o = suc (suc o) → gn = gcd1 i0 (suc i0) (suc o) (suc (suc o))
|
|
80 gcd21 zero i0 (suc j) j0 o o0 1<i 1<j 1<o gn gm gnm = {!!}
|
|
81 gcd21 (suc i) i0 zero j0 o o0 1<i 1<j 1<o gn gm gnm = {!!}
|
|
82 -- ?
|
142
|
83 gcd21 (suc i) i0 (suc j) j0 zero o0 1<i 1<j 1<o gn gm gnm = {!!}
|
|
84 gcd21 (suc i) i0 (suc j) j0 (suc o) o0 1<i 1<j 1<o gn gm gnm =
|
|
85 gcd21 i i0 j j0 o o0 1<i 1<j 1<o (subst (λ m → m ≡ k) (gcd22 i i0 _ _ ) gn)
|
|
86 (subst (λ m → m ≡ k) (gcd22 j j0 _ _ ) gm) (subst (λ k → k ≡ 1) (gcd22 i i0 _ _ ) gnm)
|
141
|
87
|
|
88 record Even (i : ℕ) : Set where
|
|
89 field
|
|
90 j : ℕ
|
|
91 is-twice : i ≡ 2 * j
|
|
92
|
|
93 e2 : (i : ℕ) → even i → Even i
|
|
94 e2 zero en = record { j = 0 ; is-twice = refl }
|
|
95 e2 (suc (suc i)) en = record { j = suc (Even.j (e2 i en )) ; is-twice = e21 } where
|
|
96 e21 : suc (suc i) ≡ 2 * suc (Even.j (e2 i en))
|
|
97 e21 = begin
|
|
98 suc (suc i) ≡⟨ cong (λ k → suc (suc k)) (Even.is-twice (e2 i en)) ⟩
|
|
99 suc (suc (2 * Even.j (e2 i en))) ≡⟨ sym (*-distribˡ-+ 2 1 _) ⟩
|
|
100 2 * suc (Even.j (e2 i en)) ∎ where open ≡-Reasoning
|
|
101
|
|
102 record Odd (i : ℕ) : Set where
|
|
103 field
|
|
104 j : ℕ
|
|
105 is-twice : i ≡ suc (2 * j )
|
57
|
106
|
141
|
107 odd2 : (i : ℕ) → ¬ even i → even (suc i)
|
|
108 odd2 zero ne = ⊥-elim ( ne tt )
|
|
109 odd2 (suc zero) ne = tt
|
|
110 odd2 (suc (suc i)) ne = odd2 i ne
|
|
111
|
|
112 odd3 : (i : ℕ) → ¬ even i → Odd i
|
|
113 odd3 zero ne = ⊥-elim ( ne tt )
|
|
114 odd3 (suc zero) ne = record { j = 0 ; is-twice = refl }
|
|
115 odd3 (suc (suc i)) ne = record { j = Even.j (e2 (suc i) (odd2 i ne)) ; is-twice = odd31 } where
|
|
116 odd31 : suc (suc i) ≡ suc (2 * Even.j (e2 (suc i) (odd2 i ne)))
|
|
117 odd31 = begin
|
|
118 suc (suc i) ≡⟨ cong suc (Even.is-twice (e2 (suc i) (odd2 i ne))) ⟩
|
|
119 suc (2 * (Even.j (e2 (suc i) (odd2 i ne)))) ∎ where open ≡-Reasoning
|
|
120
|
|
121 odd4 : (i : ℕ) → even i → ¬ even ( suc i )
|
|
122 odd4 (suc (suc i)) en en1 = odd4 i en en1
|
|
123
|
|
124 even^2 : {n : ℕ} → even ( n * n ) → even n
|
|
125 even^2 {n} en with even? n
|
|
126 ... | yes y = y
|
|
127 ... | 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
|
|
128 m : ℕ
|
|
129 m = Odd.j ( odd3 n ne )
|
|
130 ee3 : even (2 * m)
|
|
131 ee3 = subst (λ k → even k ) (*-comm m 2) (n*even {m} {2} tt )
|
|
132 ee4 : even ((2 * m) * suc (2 * m))
|
|
133 ee4 = even*n {(2 * m)} {suc (2 * m)} (even*n {2} {m} tt )
|
|
134 ee2 : n * n ≡ suc (2 * m) + ((2 * m) * (suc (2 * m) ))
|
|
135 ee2 = begin n * n ≡⟨ cong ( λ k → k * k) (Odd.is-twice (odd3 n ne)) ⟩
|
|
136 suc (2 * m) * suc (2 * m) ≡⟨ *-distribʳ-+ (suc (2 * m)) 1 ((2 * m) ) ⟩
|
|
137 (1 * suc (2 * m)) + 2 * m * suc (2 * m) ≡⟨ cong (λ k → k + 2 * m * suc (2 * m)) (begin
|
|
138 suc m + 1 * m + 0 * (suc m + 1 * m ) ≡⟨ +-comm (suc m + 1 * m) 0 ⟩
|
|
139 suc m + 1 * m ≡⟨⟩
|
|
140 suc (2 * m)
|
|
141 ∎) ⟩ suc (2 * m) + 2 * m * suc (2 * m) ∎ where open ≡-Reasoning
|
57
|
142
|
141
|
143 open import nat
|
|
144
|
|
145 e3 : {i j : ℕ } → 2 * i ≡ 2 * j → i ≡ j
|
|
146 e3 {zero} {zero} refl = refl
|
|
147 e3 {suc x} {suc y} eq with <-cmp x y
|
|
148 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≡< eq (s≤s (<-trans (<-plus a) (<-plus-0 (s≤s (<-plus a ))))))
|
|
149 ... | tri≈ ¬a b ¬c = cong suc b
|
|
150 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym eq) (s≤s (<-trans (<-plus c) (<-plus-0 (s≤s (<-plus c ))))))
|
|
151
|
|
152 record Rational : Set where
|
|
153 field
|
|
154 i j : ℕ
|
|
155 coprime : gcd i j ≡ 1
|
57
|
156
|
142
|
157 root2-irrational : ( n m : ℕ ) → n > 1 → m > 1 → 2 * n * n ≡ m * m → ¬ (gcd n m ≡ 1)
|
|
158 root2-irrational n m n>1 m>1 2nm = gcd24 {n} {m} n>1 m>1 (s≤s (s≤s z≤n)) (even→gcd=2 rot7 (rot5 n>1)) (even→gcd=2 ( even^2 {m} ( rot1)) (rot5 m>1))where
|
|
159 rot5 : {n : ℕ} → n > 1 → n > 0
|
|
160 rot5 {n} lt = <-trans a<sa lt
|
141
|
161 rot1 : even ( m * m )
|
|
162 rot1 = subst (λ k → even k ) rot4 (n*even {n * n} {2} tt ) where
|
|
163 rot4 : (n * n) * 2 ≡ m * m
|
|
164 rot4 = begin
|
|
165 (n * n) * 2 ≡⟨ *-comm (n * n) 2 ⟩
|
|
166 2 * ( n * n ) ≡⟨ sym (*-assoc 2 n n) ⟩
|
|
167 2 * n * n ≡⟨ 2nm ⟩
|
|
168 m * m ∎ where open ≡-Reasoning
|
|
169 E : Even m
|
|
170 E = e2 m ( even^2 {m} ( rot1 ))
|
|
171 rot2 : 2 * n * n ≡ 2 * Even.j E * m
|
|
172 rot2 = subst (λ k → 2 * n * n ≡ k * m ) (Even.is-twice E) 2nm
|
|
173 rot3 : n * n ≡ Even.j E * m
|
|
174 rot3 = e3 ( begin
|
|
175 2 * (n * n) ≡⟨ sym (*-assoc 2 n _) ⟩
|
|
176 2 * n * n ≡⟨ rot2 ⟩
|
|
177 2 * Even.j E * m ≡⟨ *-assoc 2 (Even.j E) m ⟩
|
|
178 2 * (Even.j E * m) ∎ ) where open ≡-Reasoning
|
|
179 rot7 : even n
|
|
180 rot7 = even^2 {n} (subst (λ k → even k) (sym rot3) ((n*even {Even.j E} {m} ( even^2 {m} ( rot1 )))))
|
57
|
181
|