annotate automaton-in-agda/src/root2.agda @ 281:8b437dd616dd

fix gcd and root
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 27 Dec 2021 10:29:59 +0900
parents 4e4e148eb7e5
children 4a00e5f2b793
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
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
11 import gcd as 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
231
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 199
diff changeset
14 open import logic
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
15
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
16 record Rational : Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
17 field
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 57
diff changeset
18 i j : ℕ
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
19 coprime : GCD.gcd i j ≡ 1
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
20
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
21 -- record Dividable (n m : ℕ ) : Set where
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
22 -- field
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
23 -- factor : ℕ
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
24 -- is-factor : factor * n + 0 ≡ m
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
25
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
26 gcd : (i j : ℕ) → ℕ
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
27 gcd = GCD.gcd
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
28
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
29 gcd-euclid : ( p a b : ℕ ) → 1 < p → 0 < a → 0 < b → ((i : ℕ ) → i < p → 0 < i → gcd p i ≡ 1)
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
30 → Dividable p (a * b) → Dividable p a ∨ Dividable p b
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
31 gcd-euclid = GCD.gcd-euclid
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
32
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
33 gcd-div1 : ( i j k : ℕ ) → k > 1 → (if : Dividable k i) (jf : Dividable k j )
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
34 → Dividable k ( gcd i j )
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
35 gcd-div1 = GCD.gcd-div
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36
231
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 199
diff changeset
37 open _∧_
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 199
diff changeset
38
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 199
diff changeset
39 open import prime
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 199
diff changeset
40
233
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 231
diff changeset
41 divdable^2 : ( n k : ℕ ) → 1 < k → 1 < n → Prime k → Dividable k ( n * n ) → Dividable k n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 231
diff changeset
42 divdable^2 zero zero () 1<n pk dn2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 231
diff changeset
43 divdable^2 (suc n) (suc k) 1<k 1<n pk dn2 with decD {suc k} {suc n} 1<k
159
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
44 ... | yes y = y
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 233
diff changeset
45 ... | no non with gcd-euclid (suc k) (suc n) (suc n) 1<k (<-trans a<sa 1<n) (<-trans a<sa 1<n) (Prime.isPrime pk) dn2
233
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 231
diff changeset
46 ... | case1 dn = dn
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 231
diff changeset
47 ... | case2 dm = dm
159
5530b3789e0c add even
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 148
diff changeset
48
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 254
diff changeset
49 -- p * n * n ≡ m * m means (m/n)^2 = p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 254
diff changeset
50 -- rational m/n requires m and n is comprime each other which contradicts the condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 254
diff changeset
51
254
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
52 root-prime-irrational : ( n m p : ℕ ) → n > 1 → Prime p → m > 1 → p * n * n ≡ m * m → ¬ (gcd n m ≡ 1)
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
53 root-prime-irrational n m 0 n>1 pn m>1 pnm = ⊥-elim ( nat-≡< refl (<-trans a<sa (Prime.p>1 pn)))
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
54 root-prime-irrational n m (suc p0) n>1 pn m>1 pnm = rot13 ( gcd-div1 n m (suc p0) 1<sp dn dm ) where
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
55 p = suc p0
254
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
56 1<sp : 1 < p
253
012f79b51dba ... prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 251
diff changeset
57 1<sp = Prime.p>1 pn
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
58 rot13 : {m : ℕ } → Dividable (suc p0) m → m ≡ 1 → ⊥
185
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
59 rot13 d refl with Dividable.factor d | Dividable.is-factor d
253
012f79b51dba ... prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 251
diff changeset
60 ... | zero | () -- gcd 0 m ≡ 1
254
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
61 ... | suc n | x = ⊥-elim ( nat-≡< (sym x) rot17 ) where -- x : (suc n * p + 0) ≡ 1
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
62 rot17 : suc n * (suc p0) + 0 > 1
253
012f79b51dba ... prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 251
diff changeset
63 rot17 = begin
012f79b51dba ... prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 251
diff changeset
64 2 ≡⟨ refl ⟩
254
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
65 suc (1 * 1 ) ≤⟨ 1<sp ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
66 suc p0 ≡⟨ cong suc (+-comm 0 _) ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
67 suc (p0 + 0) ≤⟨ s≤s (+-monoʳ-≤ p0 z≤n) ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
68 suc (p0 + n * p ) ≡⟨ +-comm 0 _ ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
69 suc n * p + 0 ∎ where open ≤-Reasoning
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
70 dm : Dividable p m
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
71 dm = divdable^2 m p 1<sp m>1 pn record { factor = n * n ; is-factor = begin
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
72 (n * n) * p + 0 ≡⟨ +-comm _ 0 ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
73 (n * n) * p ≡⟨ *-comm (n * n) p ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
74 p * (n * n) ≡⟨ sym (*-assoc p n n) ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
75 (p * n) * n ≡⟨ pnm ⟩
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 233
diff changeset
76 m * m ∎ } where open ≡-Reasoning
254
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
77 -- p * n * n = 2m' 2m'
251
9d2cba39b33c root2 done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 250
diff changeset
78 -- n * n = m' 2m'
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 233
diff changeset
79 df = Dividable.factor dm
254
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
80 dn : Dividable p n
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
81 dn = divdable^2 n p 1<sp n>1 pn record { factor = df * df ; is-factor = begin
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
82 df * df * p + 0 ≡⟨ *-cancelʳ-≡ _ _ {p0} ( begin
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
83 (df * df * p + 0) * p ≡⟨ cong (λ k → k * p) (+-comm (df * df * p) 0) ⟩
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
84 ((df * df) * p ) * p ≡⟨ cong (λ k → k * p) (*-assoc df df p ) ⟩
254
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
85 (df * (df * p)) * p ≡⟨ cong (λ k → (df * k ) * p) (*-comm df p) ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
86 (df * (p * df)) * p ≡⟨ sym ( cong (λ k → k * p) (*-assoc df p df ) ) ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
87 ((df * p) * df) * p ≡⟨ *-assoc (df * p) df p ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
88 (df * p) * (df * p) ≡⟨ cong₂ (λ j k → j * k ) (+-comm 0 (df * p)) (+-comm 0 _) ⟩
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
89 (df * p + 0) * (df * p + 0) ≡⟨ cong₂ (λ j k → j * k) (Dividable.is-factor dm ) (Dividable.is-factor dm )⟩
253
012f79b51dba ... prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 251
diff changeset
90 m * m ≡⟨ sym pnm ⟩
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
91 p * n * n ≡⟨ cong (λ k → k * n) (*-comm p n) ⟩
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
92 n * p * n ≡⟨ *-assoc n p n ⟩
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
93 n * (p * n) ≡⟨ cong (λ k → n * k) (*-comm p n) ⟩
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
94 n * (n * p) ≡⟨ sym (*-assoc n n p) ⟩
254
24c721da4f27 prime version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 253
diff changeset
95 n * n * p ∎ ) ⟩
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 233
diff changeset
96 n * n ∎ } where open ≡-Reasoning
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
98 Rational* : (r s : Rational) → Rational
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
99 Rational* r s = record { i = {!!} ; j = {!!} ; coprime = {!!} }
185
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
100
281
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
101 _r=_ : Rational → ℕ → Set
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
102 r r= n = (Rational.i r ≡ n) ∧ (Rational.j r ≡ 1)
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
103
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
104 root-prime-irrational1 : ( p : ℕ ) → Prime p → ¬ ( ( r : Rational ) → Rational* r r r= p )
8b437dd616dd fix gcd and root
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 255
diff changeset
105 root-prime-irrational1 = {!!}