annotate nat.agda @ 956:bfc7007177d0 default tip

safe and cubical compatible with no warning done
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 19 Oct 2024 09:48:48 +0900
parents 057d3309ed9d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
949
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 {-# OPTIONS --cubical-compatible --safe #-}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 module nat where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.Nat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 open import Data.Nat.Properties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 open import Data.Empty
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
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 open import Relation.Binary.Core
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 open import Relation.Binary.Definitions
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 open import logic
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 open import Level hiding ( zero ; suc )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 =→¬< : {x : ℕ } → ¬ ( x < x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 =→¬< {x} x<x with <-cmp x x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a x<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 ... | tri> ¬a ¬b c = ⊥-elim ( ¬b refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 >→¬< : {x y : ℕ } → (x < y ) → ¬ ( y < x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 >→¬< {x} {y} x<y y<x with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c y<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c y<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a x<y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 nat-<> : { x y : ℕ } → x < y → y < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 nat-<> {x} {y} x<y y<x with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c y<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c y<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a x<y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 a<sa : {la : ℕ} → la < suc la
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 a<sa {zero} = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 a<sa {suc la} = s≤s a<sa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 refl-≤s : {x : ℕ } → x ≤ suc x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 refl-≤s {zero} = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 refl-≤s {suc x} = s≤s (refl-≤s {x})
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 a≤sa : {x : ℕ } → x ≤ suc x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 a≤sa = refl-≤s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 nat-<≡ : { x : ℕ } → x < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 nat-<≡ {x} x<x with <-cmp x x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬a x<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a x<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 nat-≡< : { x y : ℕ } → x ≡ y → x < y → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 nat-≡< refl lt = nat-<≡ lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 nat-≤> {x} {y} x≤y y<x with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 ... | tri< a ¬b ¬c = ⊥-elim ( ¬c y<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 ... | tri≈ ¬a b ¬c = ⊥-elim ( ¬c y<x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 ... | tri> ¬a ¬b c = ⊥-elim (nat-<≡ (≤-trans c x≤y))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 ≤-∨ : { x y : ℕ } → x ≤ y → ( (x ≡ y ) ∨ (x < y) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 ≤-∨ {x} {y} x≤y with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 ... | tri< a ¬b ¬c = case2 a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 ... | tri≈ ¬a b ¬c = case1 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 ... | tri> ¬a ¬b c = ⊥-elim ( nat-<≡ (≤-trans c x≤y))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 <-∨ : { x y : ℕ } → x < suc y → ( (x ≡ y ) ∨ (x < y) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 <-∨ {x} {y} x<sy with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 ... | tri< a ¬b ¬c = case2 a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 ... | tri≈ ¬a b ¬c = case1 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 ... | tri> ¬a ¬b c = ⊥-elim ( nat-<≡ (≤-trans x<sy c ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 ¬a≤a : {la : ℕ} → suc la ≤ la → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 ¬a≤a {x} sx≤x = ⊥-elim ( nat-≤> sx≤x a<sa )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 max : (x y : ℕ) → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 max zero zero = zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 max zero (suc x) = (suc x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 max (suc x) zero = (suc x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 max (suc x) (suc y) = suc ( max x y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 x≤max : (x y : ℕ) → x ≤ max x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 x≤max zero zero = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 x≤max zero (suc x) = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 x≤max (suc x) zero = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 x≤max (suc x) (suc y) = s≤s( x≤max x y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 y≤max : (x y : ℕ) → y ≤ max x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 y≤max zero zero = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 y≤max zero (suc x) = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 y≤max (suc x) zero = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 y≤max (suc x) (suc y) = s≤s( y≤max x y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 x≤y→max=y : (x y : ℕ) → x ≤ y → max x y ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 x≤y→max=y zero zero x≤y = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 x≤y→max=y zero (suc y) x≤y = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 x≤y→max=y (suc x) (suc y) lt with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 ... | tri< a ¬b ¬c = cong suc (x≤y→max=y x y (≤-trans a≤sa a))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 ... | tri≈ ¬a refl ¬c = cong suc (x≤y→max=y x y ≤-refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 y≤x→max=x : (x y : ℕ) → y ≤ x → max x y ≡ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 y≤x→max=x zero zero y≤x = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 y≤x→max=x zero (suc y) ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 y≤x→max=x (suc x) zero lt = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 y≤x→max=x (suc x) (suc y) lt with <-cmp y x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 ... | tri< a ¬b ¬c = cong suc (y≤x→max=x x y (≤-trans a≤sa a))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 ... | tri≈ ¬a refl ¬c = cong suc (y≤x→max=x x y ≤-refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 -- _*_ : ℕ → ℕ → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 -- _*_ zero _ = zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 -- _*_ (suc n) m = m + ( n * m )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 -- x ^ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 exp : ℕ → ℕ → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 exp _ zero = 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 exp n (suc m) = n * ( exp n m )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 div2 : ℕ → (ℕ ∧ Bool )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 div2 zero = ⟪ 0 , false ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 div2 (suc zero) = ⟪ 0 , true ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 div2 (suc (suc n)) = ⟪ suc (proj1 (div2 n)) , proj2 (div2 n) ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 open _∧_
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 div2-rev : (ℕ ∧ Bool ) → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 div2-rev ⟪ x , true ⟫ = suc (x + x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 div2-rev ⟪ x , false ⟫ = x + x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 div2-eq : (x : ℕ ) → div2-rev ( div2 x ) ≡ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 div2-eq zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 div2-eq (suc zero) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 div2-eq (suc (suc x)) with div2 x in eq1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 ... | ⟪ x1 , true ⟫ = begin -- eq1 : div2 x ≡ ⟪ x1 , true ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 div2-rev ⟪ suc x1 , true ⟫ ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 suc (suc (x1 + suc x1)) ≡⟨ cong (λ k → suc (suc k )) (+-comm x1 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 suc (suc (suc (x1 + x1))) ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 suc (suc (div2-rev ⟪ x1 , true ⟫)) ≡⟨ cong (λ k → suc (suc (div2-rev k ))) (sym eq1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 suc (suc (div2-rev (div2 x))) ≡⟨ cong (λ k → suc (suc k)) (div2-eq x) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 suc (suc x) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 ... | ⟪ x1 , false ⟫ = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 div2-rev ⟪ suc x1 , false ⟫ ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 suc (x1 + suc x1) ≡⟨ cong (λ k → (suc k )) (+-comm x1 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 suc (suc (x1 + x1)) ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 suc (suc (div2-rev ⟪ x1 , false ⟫)) ≡⟨ cong (λ k → suc (suc (div2-rev k ))) (sym eq1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 suc (suc (div2-rev (div2 x))) ≡⟨ cong (λ k → suc (suc k)) (div2-eq x) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 suc (suc x) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 sucprd : {i : ℕ } → 0 < i → suc (pred i) ≡ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 sucprd {suc i} 0<i = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 0<s : {x : ℕ } → zero < suc x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 0<s {_} = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 px<py : {x y : ℕ } → pred x < pred y → x < y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 px<py {zero} {suc y} lt = 0<s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 px<py {suc x} {suc y} lt with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 ... | tri< a ¬b ¬c = s≤s a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 ... | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< b lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 ... | tri> ¬a ¬b c = ⊥-elim ( nat-<> c lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 minus : (a b : ℕ ) → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 minus a zero = a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 minus zero (suc b) = zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 minus (suc a) (suc b) = minus a b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 _-_ = minus
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 sn-m=sn-m : {m n : ℕ } → m ≤ n → suc n - m ≡ suc ( n - m )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 sn-m=sn-m {0} {n} m≤n = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 sn-m=sn-m {suc m} {suc n} le with <-cmp m n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 ... | tri< a ¬b ¬c = sm00 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 sm00 : suc n - m ≡ suc ( n - m )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 sm00 = sn-m=sn-m {m} {n} (≤-trans a≤sa a )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 ... | tri≈ ¬a refl ¬c = sn-m=sn-m {m} {n} ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c le )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 si-sn=i-n : {i n : ℕ } → n < i → suc (i - suc n) ≡ (i - n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 si-sn=i-n {i} {n} n<i = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 suc (i - suc n) ≡⟨ sym (sn-m=sn-m n<i ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 suc i - suc n ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 i - n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 n-m<n : (n m : ℕ ) → n - m ≤ n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 n-m<n zero zero = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 n-m<n (suc n) zero = s≤s (n-m<n n zero)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 n-m<n zero (suc m) = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 n-m<n (suc n) (suc m) = ≤-trans (n-m<n n m ) refl-≤s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 m-0=m : {m : ℕ } → m - zero ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 m-0=m {zero} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 m-0=m {suc m} = cong suc (m-0=m {m})
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 m-m=0 : {m : ℕ } → m - m ≡ zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 m-m=0 {zero} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 m-m=0 {suc m} = m-m=0 {m}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 refl-≤ : {x : ℕ } → x ≤ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 refl-≤ {zero} = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 refl-≤ {suc x} = s≤s (refl-≤ {x})
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 refl-≤≡ : {x y : ℕ } → x ≡ y → x ≤ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 refl-≤≡ refl = refl-≤
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 px≤x : {x : ℕ } → pred x ≤ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 px≤x {zero} = refl-≤
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 px≤x {suc x} = refl-≤s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 px≤py : {x y : ℕ } → x ≤ y → pred x ≤ pred y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 px≤py {zero} {zero} x≤y = refl-≤
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 px≤py {suc x} {zero} ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 px≤py {zero} {suc y} le = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 px≤py {suc x} {suc y} x≤y with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 ... | tri< a ¬b ¬c = ≤-trans a≤sa a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 ... | tri≈ ¬a b ¬c = refl-≤≡ b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c x≤y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 n-n-m=m : {m n : ℕ } → m ≤ n → m ≡ (n - (n - m))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 n-n-m=m {0} {zero} le = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 n-n-m=m {0} {suc n} lt = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 0 ≡⟨ sym (m-m=0 {suc n}) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 suc n - suc n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 n-n-m=m {suc m} {suc n} le = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 suc m ≡⟨ cong suc ( n-n-m=m (px≤py le)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 suc (n - (n - m)) ≡⟨ sym (sn-m=sn-m (n-m<n n m)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 suc n - (n - m) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 m+= : {i j m : ℕ } → m + i ≡ m + j → i ≡ j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 m+= {i} {j} {zero} refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 m+= {i} {j} {suc m} eq = m+= {i} {j} {m} ( cong (λ k → pred k ) eq )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 +m= : {i j m : ℕ } → i + m ≡ j + m → i ≡ j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 +m= {i} {j} {m} eq = m+= ( subst₂ (λ j k → j ≡ k ) (+-comm i _ ) (+-comm j _ ) eq )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 less-1 : { n m : ℕ } → suc n < m → n < m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 less-1 sn<m = <-trans a<sa sn<m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 sa=b→a<b : { n m : ℕ } → suc n ≡ m → n < m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 sa=b→a<b {n} {m} sn=m = subst (λ k → n < k ) sn=m a<sa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 minus+n : {x y : ℕ } → suc x > y → minus x y + y ≡ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 minus+n {x} {zero} _ = trans (sym (+-comm zero _ )) refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 minus+n {zero} {suc y} lt = ⊥-elim ( nat-≤> lt (≤-trans a<sa (s≤s (s≤s z≤n)) ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 minus+n {suc x} {suc y} y<sx with <-cmp y (suc x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 ... | tri< a ¬b ¬c = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 minus (suc x) (suc y) + suc y ≡⟨ +-comm _ (suc y) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 suc y + minus x y ≡⟨ cong ( λ k → suc k ) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 y + minus x y ≡⟨ +-comm y _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 minus x y + y ≡⟨ minus+n {x} {y} a ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 x ∎ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 suc x ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 ... | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< b (px≤py y<sx ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 ... | tri> ¬a ¬b c = ⊥-elim ( nat-<> c (px≤py y<sx ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 +<-cong : {x y z : ℕ } → x < y → z + x < z + y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 +<-cong {x} {y} {zero} x<y = x<y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 +<-cong {x} {y} {suc z} x<y = s≤s (+<-cong {x} {y} {z} x<y)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 <-minus-0 : {x y z : ℕ } → z + x < z + y → x < y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 <-minus-0 {x} {y} {z} x<y with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 ... | tri< a ¬b ¬c = a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 ... | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< (cong (λ k → z + k ) b) x<y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 ... | tri> ¬a ¬b c = ⊥-elim ( nat-<> x<y (+<-cong {y} {x} {z} c))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 <-minus : {x y z : ℕ } → x + z < y + z → x < y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 <-minus {x} {y} {z} lt = <-minus-0 ( subst₂ ( λ j k → j < k ) (+-comm x _) (+-comm y _ ) lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 x≤x+y : {z y : ℕ } → z ≤ z + y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 x≤x+y {zero} {y} = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 x≤x+y {suc z} {y} = s≤s (x≤x+y {z} {y})
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
273
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 x≤y+x : {z y : ℕ } → z ≤ y + z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 x≤y+x {z} {y} = subst (λ k → z ≤ k ) (+-comm _ y ) x≤x+y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 x≤x+sy : {x y : ℕ} → x < x + suc y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 x≤x+sy {x} {y} = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 suc x ≤⟨ x≤x+y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 suc x + y ≡⟨ cong (λ k → k + y) (+-comm 1 x ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 (x + 1) + y ≡⟨ (+-assoc x 1 _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 x + suc y ∎ where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 <-plus-0 : {x y z : ℕ } → x < y → z + x < z + y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 <-plus-0 = +<-cong
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
287
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 <-plus : {x y z : ℕ } → x < y → x + z < y + z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 <-plus {x} {y} {z} x<y = subst₂ (λ j k → j < k ) (+-comm z x ) (+-comm z y ) ( <-plus-0 x<y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
290
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 ≤-plus-0 : {x y z : ℕ } → x ≤ y → z + x ≤ z + y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 ≤-plus-0 {x} {y} {zero} lt = lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 ≤-plus-0 {x} {y} {suc z} lt = s≤s ( ≤-plus-0 {x} {y} {z} lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
294
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 ≤-plus : {x y z : ℕ } → x ≤ y → x + z ≤ y + z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 ≤-plus {x} {y} {z} x≤y = subst₂ (λ j k → j ≤ k ) (+-comm z x ) (+-comm z y ) ( ≤-plus-0 x≤y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 x+y<z→x<z : {x y z : ℕ } → x + y < z → x < z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 x+y<z→x<z {x} {zero} {z} xy<z = subst (λ k → k < z ) (+-comm x zero ) xy<z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 x+y<z→x<z {x} {suc y} {z} xy<z = <-minus {x} {z} {suc y} (<-trans xy<z x≤x+sy )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 *≤ : {x y z : ℕ } → x ≤ y → x * z ≤ y * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 *≤ lt = *-mono-≤ lt ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 <to≤ : {x y : ℕ } → x < y → x ≤ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 <to≤ {x} {y} x<y with <-cmp x (suc y)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 ... | tri< a ¬b ¬c = px≤py a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 ... | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< b (≤-trans x<y a≤sa ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 ... | tri> ¬a ¬b c = ⊥-elim ( nat-<> c (≤-trans x<y a≤sa ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 <sto≤ : {x y : ℕ } → x < suc y → x ≤ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 <sto≤ {x} {y} x<sy with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 ... | tri< a ¬b ¬c = <to≤ a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 ... | tri≈ ¬a refl ¬c = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c x<sy )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 *< : {x y z : ℕ } → x < y → x * suc z < y * suc z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 *< {x} {zero} {z} ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 *< {x} {suc y} {z} x<y = s≤s ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 x * suc z ≤⟨ lem01 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 y * suc z ≤⟨ x≤x+y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 y * suc z + z ≡⟨ +-comm _ z ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 z + y * suc z ∎ ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 lem01 : x * suc z ≤ y * suc z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 lem01 = *-mono-≤ {x} {y} {suc z} (<sto≤ x<y) ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
327
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 <to<s : {x y : ℕ } → x < y → x < suc y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 <to<s x<y = <-trans x<y a<sa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 <tos<s : {x y : ℕ } → x < y → suc x < suc y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 <tos<s x<y = s≤s x<y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
333
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 <∨≤ : ( x y : ℕ ) → (x < y ) ∨ (y ≤ x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 <∨≤ x y with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 ... | tri< a ¬b ¬c = case1 a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 ... | tri≈ ¬a refl ¬c = case2 ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 ... | tri> ¬a ¬b c = case2 (<to≤ c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 x<y→≤ : {x y : ℕ } → x < y → x ≤ suc y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 x<y→≤ {x} {y} x<y with <-cmp x (suc y)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 ... | tri< a ¬b ¬c = <to≤ a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 ... | tri≈ ¬a b ¬c = refl-≤≡ b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 ... | tri> ¬a ¬b c = ⊥-elim ( ¬a ( ≤-trans x<y a≤sa ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 ≤→= : {i j : ℕ} → i ≤ j → j ≤ i → i ≡ j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 ≤→= {i} {j} i≤j j≤i with <-cmp i j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> j≤i a )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 ... | tri≈ ¬a b ¬c = b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> i≤j c )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 sx≤py→x≤y : {x y : ℕ } → suc x ≤ suc y → x ≤ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 sx≤py→x≤y = px≤py
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
354
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 sx<py→x<y : {x y : ℕ } → suc x < suc y → x < y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 sx<py→x<y {x} {y} sx<sy with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 ... | tri< a ¬b ¬c = a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
358 ... | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< (cong suc b) sx<sy )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 ... | tri> ¬a ¬b c = ⊥-elim ( nat-<> (s≤s c) sx<sy )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
360
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 sx≤y→x≤y : {x y : ℕ } → suc x ≤ y → x ≤ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 sx≤y→x≤y sx≤y = ≤-trans a≤sa sx≤y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
363
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 x<sy→x≤y : {x y : ℕ } → x < suc y → x ≤ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 x<sy→x≤y = <sto≤
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
366
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 x≤y→x<sy : {x y : ℕ } → x ≤ y → x < suc y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 x≤y→x<sy {.zero} {y} z≤n = ≤-trans a<sa (s≤s z≤n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 x≤y→x<sy {.(suc _)} {.(suc _)} (s≤s le) = s≤s ( x≤y→x<sy le)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
370
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 sx≤y→x<y : {x y : ℕ } → suc x ≤ y → x < y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 sx≤y→x<y sx≤y = sx≤y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
373
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 open import Data.Product
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
375
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 i-j=0→i=j : {i j : ℕ } → j ≤ i → i - j ≡ 0 → i ≡ j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 i-j=0→i=j {i} {j} le j=0 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 i ≡⟨ sym (m-0=m) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 i - 0 ≡⟨ cong (λ k → i - k ) (sym j=0) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 i - (i - j ) ≡⟨ sym (n-n-m=m le) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 j ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
382
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
383 m*n=0⇒m=0∨n=0 : {i j : ℕ} → i * j ≡ 0 → (i ≡ 0) ∨ ( j ≡ 0 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
384 m*n=0⇒m=0∨n=0 {zero} {j} eq = case1 refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 m*n=0⇒m=0∨n=0 {suc i} {zero} eq = case2 refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
386
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
387
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
388 minus+1 : {x y : ℕ } → y ≤ x → suc (minus x y) ≡ minus (suc x) y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 minus+1 {zero} {zero} y≤x = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 minus+1 {suc x} {zero} y≤x = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 minus+1 {suc x} {suc y} y≤x = minus+1 {x} {y} (sx≤py→x≤y y≤x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
392
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 minus+yz : {x y z : ℕ } → z ≤ y → x + minus y z ≡ minus (x + y) z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 minus+yz {zero} {y} {z} _ = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 minus+yz {suc x} {y} {z} z≤y = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 suc x + minus y z ≡⟨ cong suc ( minus+yz z≤y ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 suc (minus (x + y) z) ≡⟨ minus+1 {x + y} {z} (≤-trans z≤y (subst (λ g → y ≤ g) (+-comm y x) x≤x+y) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
398 minus (suc x + y) z ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
399
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 minus<=0 : {x y : ℕ } → x ≤ y → minus x y ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 minus<=0 {0} {zero} le = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 minus<=0 {0} {suc y} le = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
403 minus<=0 {suc x} {suc y} le = minus<=0 {x} {y} (sx≤py→x≤y le)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
404
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
405 minus>0 : {x y : ℕ } → x < y → 0 < minus y x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
406 minus>0 {zero} {suc _} lt = lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
407 minus>0 {suc x} {suc y} lt = minus>0 {x} {y} (sx<py→x<y lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
408
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 minus>0→x<y : {x y : ℕ } → 0 < minus y x → x < y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 minus>0→x<y {x} {y} lt with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 ... | tri< a ¬b ¬c = a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
412 ... | tri≈ ¬a refl ¬c = ⊥-elim ( nat-≡< (sym (minus<=0 {x} ≤-refl)) lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym (minus<=0 {y} (≤-trans refl-≤s c ))) lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
414
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
415 minus+y-y : {x y : ℕ } → (x + y) - y ≡ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
416 minus+y-y {zero} {y} = minus<=0 {zero + y} {y} ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
417 minus+y-y {suc x} {y} = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
418 (suc x + y) - y ≡⟨ sym (minus+1 {_} {y} x≤y+x) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
419 suc ((x + y) - y) ≡⟨ cong suc (minus+y-y {x} {y}) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
420 suc x ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
421
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 minus+yx-yz : {x y z : ℕ } → (y + x) - (y + z) ≡ x - z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
423 minus+yx-yz {x} {zero} {z} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
424 minus+yx-yz {x} {suc y} {z} = minus+yx-yz {x} {y} {z}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
425
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
426 minus+xy-zy : {x y z : ℕ } → (x + y) - (z + y) ≡ x - z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
427 minus+xy-zy {x} {y} {z} = subst₂ (λ j k → j - k ≡ x - z ) (+-comm y x) (+-comm y z) (minus+yx-yz {x} {y} {z})
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
428
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
429 +cancel<l : (x z : ℕ ) {y : ℕ} → y + x < y + z → x < z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
430 +cancel<l x z {zero} lt = lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
431 +cancel<l x z {suc y} lt = +cancel<l x z {y} (sx<py→x<y lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
432
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
433 +cancel<r : (x z : ℕ ) {y : ℕ} → x + y < z + y → x < z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
434 +cancel<r x z {y} lt = +cancel<l x z (subst₂ (λ j k → j < k ) (+-comm x _) (+-comm z _) lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
435
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
436 minus<z : {x y z : ℕ } → x < y → z ≤ x → x - z < y - z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
437 minus<z {x} {y} {z} x<y z≤x = +cancel<r _ _ ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
438 suc ( (x - z) + z ) ≡⟨ cong suc (minus+n (s≤s z≤x) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
439 suc x ≤⟨ x<y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
440 y ≡⟨ sym ( minus+n (<-trans (s≤s z≤x) (s≤s x<y) )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
441 (y - z ) + z ∎ ) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
442
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
443
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
444 y-x<y : {x y : ℕ } → 0 < x → 0 < y → y - x < y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
445 y-x<y {x} {y} 0<x 0<y with <-cmp x (suc y)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
446 ... | tri< a ¬b ¬c = +cancel<r (y - x) _ ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
447 suc ((y - x) + x) ≡⟨ cong suc (minus+n {y} {x} a ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
448 suc y ≡⟨ +-comm 1 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
449 y + suc 0 ≤⟨ +-mono-≤ ≤-refl 0<x ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
450 y + x ∎ ) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
451 ... | tri≈ ¬a refl ¬c = subst ( λ k → k < y ) (sym (minus<=0 {y} {x} refl-≤s )) 0<y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
452 ... | tri> ¬a ¬b c = subst ( λ k → k < y ) (sym (minus<=0 {y} {x} (≤-trans (≤-trans refl-≤s refl-≤s) c))) 0<y -- suc (suc y) ≤ x → y ≤ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
453
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
454 open import Relation.Binary.Definitions
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
455
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 distr-minus-* : {x y z : ℕ } → (minus x y) * z ≡ minus (x * z) (y * z)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 distr-minus-* {x} {zero} {z} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 distr-minus-* {x} {suc y} {z} with <-cmp x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
459 distr-minus-* {x} {suc y} {z} | tri< a ¬b ¬c = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 minus x (suc y) * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 ≡⟨ cong (λ k → k * z ) (minus<=0 {x} {suc y} (x<y→≤ a)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
462 0 * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
463 ≡⟨ sym (minus<=0 {x * z} {z + y * z} le ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
464 minus (x * z) (z + y * z)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
465 ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
466 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
467 le : x * z ≤ z + y * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
468 le = ≤-trans lemma (subst (λ k → y * z ≤ k ) (+-comm _ z ) (x≤x+y {y * z} {z} ) ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
469 lemma : x * z ≤ y * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
470 lemma = *≤ {x} {y} {z} (<to≤ a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
471 distr-minus-* {x} {suc y} {z} | tri≈ ¬a refl ¬c = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
472 minus x (suc y) * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
473 ≡⟨ cong (λ k → k * z ) (minus<=0 {x} {suc y} refl-≤s ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
474 0 * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
475 ≡⟨ sym (minus<=0 {x * z} {z + y * z} (lt {x} {z} )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
476 minus (x * z) (z + y * z)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
477 ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
478 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
479 lt : {x z : ℕ } → x * z ≤ z + x * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
480 lt {zero} {zero} = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
481 lt {suc x} {zero} = lt {x} {zero}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
482 lt {x} {suc z} = ≤-trans lemma refl-≤s where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
483 lemma : x * suc z ≤ z + x * suc z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
484 lemma = subst (λ k → x * suc z ≤ k ) (+-comm _ z) (x≤x+y {x * suc z} {z})
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
485 distr-minus-* {x} {suc y} {z} | tri> ¬a ¬b c = +m= {_} {_} {suc y * z} ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
486 minus x (suc y) * z + suc y * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
487 ≡⟨ sym (proj₂ *-distrib-+ z (minus x (suc y) ) _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
488 ( minus x (suc y) + suc y ) * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
489 ≡⟨ cong (λ k → k * z) (minus+n {x} {suc y} (s≤s c)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
490 x * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
491 ≡⟨ sym (minus+n {x * z} {suc y * z} (s≤s (lt c))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
492 minus (x * z) (suc y * z) + suc y * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 ∎ ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
494 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
495 lt : {x y z : ℕ } → suc y ≤ x → z + y * z ≤ x * z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
496 lt {x} {y} {z} le = *≤ le
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
497
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
498 distr-minus-*' : {z x y : ℕ } → z * (minus x y) ≡ minus (z * x) (z * y)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
499 distr-minus-*' {z} {x} {y} = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
500 z * (minus x y) ≡⟨ *-comm _ (x - y) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
501 (minus x y) * z ≡⟨ distr-minus-* {x} {y} {z} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
502 minus (x * z) (y * z) ≡⟨ cong₂ (λ j k → j - k ) (*-comm x z ) (*-comm y z) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
503 minus (z * x) (z * y) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
504
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 minus- : {x y z : ℕ } → suc x > z + y → minus (minus x y) z ≡ minus x (y + z)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
506 minus- {x} {y} {z} gt = +m= {_} {_} {z} ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
507 minus (minus x y) z + z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
508 ≡⟨ minus+n {_} {z} lemma ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
509 minus x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
510 ≡⟨ +m= {_} {_} {y} ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
511 minus x y + y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
512 ≡⟨ minus+n {_} {y} lemma1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
513 x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
514 ≡⟨ sym ( minus+n {_} {z + y} gt ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
515 minus x (z + y) + (z + y)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
516 ≡⟨ sym ( +-assoc (minus x (z + y)) _ _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
517 minus x (z + y) + z + y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
518 ∎ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
519 minus x (z + y) + z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
520 ≡⟨ cong (λ k → minus x k + z ) (+-comm _ y ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 minus x (y + z) + z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
522 ∎ ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
523 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
524 lemma1 : suc x > y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
525 lemma1 = x+y<z→x<z (subst (λ k → k < suc x ) (+-comm z _ ) gt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
526 lemma : suc (minus x y) > z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
527 lemma = <-minus {_} {_} {y} ( subst ( λ x → z + y < suc x ) (sym (minus+n {x} {y} lemma1 )) gt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
528
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
529 sn≤1→n=0 : {n : ℕ } → suc n ≤ 1 → n ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
530 sn≤1→n=0 {n} sn≤1 with <-cmp n 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
531 ... | tri≈ ¬a b ¬c = b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
532 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c sn≤1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
533
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 minus-* : {M k n : ℕ } → n < k → minus k (suc n) * M ≡ minus (minus k n * M ) M
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
535 minus-* {zero} {k} {n} lt = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 minus k (suc n) * zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
537 ≡⟨ *-comm (minus k (suc n)) zero ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
538 zero * minus k (suc n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
539 ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
540 0 * minus k n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
541 ≡⟨ *-comm 0 (minus k n) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
542 minus (minus k n * 0 ) 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
543 ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
544 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
545 minus-* {suc m} {k} {n} lt with <-cmp k 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
546 minus-* {suc m} {k} {n} lt | tri< a ¬b ¬c = ⊥-elim ( nat-≤> (sx≤py→x≤y a) (≤-trans (s≤s z≤n) lt) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
547 minus-* {suc m} {k} {n} lt | tri≈ ¬a refl ¬c = subst (λ k → minus 0 k * suc m ≡ minus (minus 1 k * suc m) (suc m)) (sym n=0) lem where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
548 n=0 : n ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
549 n=0 = sn≤1→n=0 lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
550 lem : minus 0 0 * suc m ≡ minus (minus 1 0 * suc m) (suc m)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
551 lem = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
552 minus 0 0 * suc m ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
553 0 ≡⟨ sym ( minus<=0 {suc m} {suc m} ≤-refl ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
554 minus (suc m) (suc m) ≡⟨ cong (λ k → minus k (suc m)) (+-comm 0 (suc m) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
555 minus (suc m + 0) (suc m) ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
556 minus (minus 1 0 * suc m) (suc m) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
557 minus-* {suc m} {k} {n} lt | tri> ¬a ¬b c = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
558 minus k (suc n) * M
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
559 ≡⟨ distr-minus-* {k} {suc n} {M} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
560 minus (k * M ) ((suc n) * M)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
561 ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
562 minus (k * M ) (M + n * M )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
563 ≡⟨ cong (λ x → minus (k * M) x) (+-comm M _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
564 minus (k * M ) ((n * M) + M )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
565 ≡⟨ sym ( minus- {k * M} {n * M} (lemma lt) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
566 minus (minus (k * M ) (n * M)) M
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
567 ≡⟨ cong (λ x → minus x M ) ( sym ( distr-minus-* {k} {n} )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
568 minus (minus k n * M ) M
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 M = suc m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
571 lemma : {n k m : ℕ } → n < k → suc (k * suc m) > suc m + n * suc m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
572 lemma {n} {k} {m} lt = ≤-plus-0 {_} {_} {1} (*≤ lt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
573 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
574
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
575 x=y+z→x-z=y : {x y z : ℕ } → x ≡ y + z → x - z ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
576 x=y+z→x-z=y {x} {zero} {.x} refl = minus<=0 {x} {x} refl-≤ -- x ≡ suc (y + z) → (x ≡ y + z → x - z ≡ y) → (x - z) ≡ suc y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
577 x=y+z→x-z=y {suc x} {suc y} {zero} eq = begin -- suc x ≡ suc (y + zero) → (suc x - zero) ≡ suc y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
578 suc x - zero ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
579 suc x ≡⟨ eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
580 suc y + zero ≡⟨ +-comm _ zero ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
581 suc y ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
582 x=y+z→x-z=y {suc x} {suc y} {suc z} eq = x=y+z→x-z=y {x} {suc y} {z} ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
583 x ≡⟨ cong pred eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
584 pred (suc y + suc z) ≡⟨ +-comm _ (suc z) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
585 suc z + y ≡⟨ cong suc ( +-comm _ y ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
586 suc y + z ∎ ) where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
587
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
588 m*1=m : {m : ℕ } → m * 1 ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
589 m*1=m {zero} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
590 m*1=m {suc m} = cong suc m*1=m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
591
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
592 +-cancel-1 : (x y z : ℕ ) → x + y ≡ x + z → y ≡ z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
593 +-cancel-1 zero y z eq = eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
594 +-cancel-1 (suc x) y z eq = +-cancel-1 x y z (cong pred eq )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
595
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
596 +-cancel-0 : (x y z : ℕ ) → y + x ≡ z + x → y ≡ z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
597 +-cancel-0 x y z eq = +-cancel-1 x y z (trans (+-comm x y) (trans eq (sym (+-comm x z)) ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
598
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
599 *-cancel-left : {x y z : ℕ } → x > 0 → x * y ≡ x * z → y ≡ z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
600 *-cancel-left {suc x} {zero} {zero} lt eq = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
601 *-cancel-left {suc x} {zero} {suc z} lt eq = ⊥-elim ( nat-≡< eq (s≤s (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
602 x * zero ≡⟨ *-comm x _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
603 zero ≤⟨ z≤n ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
604 z + x * suc z ∎ ))) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
605 *-cancel-left {suc x} {suc y} {zero} lt eq = ⊥-elim ( nat-≡< (sym eq) (s≤s (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
606 x * zero ≡⟨ *-comm x _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
607 zero ≤⟨ z≤n ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
608 _ ∎ ))) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
609 *-cancel-left {suc x} {suc y} {suc z} lt eq with cong pred eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
610 ... | eq1 = cong suc (*-cancel-left {suc x} {y} {z} lt (+-cancel-0 x _ _ (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
611 y + x * y + x ≡⟨ +-assoc y _ _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
612 y + (x * y + x) ≡⟨ cong (λ k → y + (k + x)) (*-comm x _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
613 y + (y * x + x) ≡⟨ cong (_+_ y) (+-comm _ x) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
614 y + (x + y * x ) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
615 y + suc y * x ≡⟨ cong (_+_ y) (*-comm (suc y) _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
616 y + x * suc y ≡⟨ eq1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
617 z + x * suc z ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
618 _ ≡⟨ sym ( cong (_+_ z) (*-comm (suc z) _) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
619 _ ≡⟨ sym ( cong (_+_ z) (+-comm _ x)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
620 z + (z * x + x) ≡⟨ sym ( cong (λ k → z + (k + x)) (*-comm x _) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
621 z + (x * z + x) ≡⟨ sym ( +-assoc z _ _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
622 z + x * z + x ∎ ))) where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
623
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
624 record Finduction {n m : Level} (P : Set n ) (Q : P → Set m ) (f : P → ℕ) : Set (n Level.⊔ m) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
625 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
626 fzero : {p : P} → f p ≡ zero → Q p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
627 pnext : (p : P ) → P
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
628 decline : {p : P} → 0 < f p → f (pnext p) < f p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
629 ind : {p : P} → Q (pnext p) → Q p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
630
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
631 y<sx→y≤x : {x y : ℕ} → y < suc x → y ≤ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
632 y<sx→y≤x = x<sy→x≤y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
633
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
634 fi0 : (x : ℕ) → x ≤ zero → x ≡ zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
635 fi0 .0 z≤n = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
636
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
637 f-induction : {n m : Level} {P : Set n } → {Q : P → Set m }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
638 → (f : P → ℕ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
639 → Finduction P Q f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
640 → (p : P ) → Q p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
641 f-induction {n} {m} {P} {Q} f I p with <-cmp 0 (f p)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
642 ... | tri> ¬a ¬b ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
643 ... | tri≈ ¬a b ¬c = Finduction.fzero I (sym b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
644 ... | tri< lt _ _ = f-induction0 p (f p) (<to≤ (Finduction.decline I lt)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
645 f-induction0 : (p : P) → (x : ℕ) → (f (Finduction.pnext I p)) ≤ x → Q p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
646 f-induction0 p zero le = Finduction.ind I (Finduction.fzero I (fi0 _ le))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
647 f-induction0 p (suc x) le with <-cmp (f (Finduction.pnext I p)) (suc x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
648 ... | tri< a ¬b ¬c = f-induction0 p x (px≤py a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
649 ... | tri≈ ¬a b ¬c = Finduction.ind I (f-induction0 (Finduction.pnext I p) x (y<sx→y≤x f1)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
650 f1 : f (Finduction.pnext I (Finduction.pnext I p)) < suc x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
651 f1 = subst (λ k → f (Finduction.pnext I (Finduction.pnext I p)) < k ) b ( Finduction.decline I {Finduction.pnext I p}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
652 (subst (λ k → 0 < k ) (sym b) (s≤s z≤n ) ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
653 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> le c )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
654
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
655
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
656 record Ninduction {n m : Level} (P : Set n ) (Q : P → Set m ) (f : P → ℕ) : Set (n Level.⊔ m) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
657 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
658 pnext : (p : P ) → P
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
659 fzero : {p : P} → f (pnext p) ≡ zero → Q p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
660 decline : {p : P} → 0 < f p → f (pnext p) < f p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
661 ind : {p : P} → Q (pnext p) → Q p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
662
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
663 s≤s→≤ : { i j : ℕ} → suc i ≤ suc j → i ≤ j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
664 s≤s→≤ = sx≤py→x≤y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
665
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
666 n-induction : {n m : Level} {P : Set n } → {Q : P → Set m }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
667 → (f : P → ℕ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
668 → Ninduction P Q f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
669 → (p : P ) → Q p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
670 n-induction {n} {m} {P} {Q} f I p = f-induction0 p (f (Ninduction.pnext I p)) ≤-refl where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
671 f-induction0 : (p : P) → (x : ℕ) → (f (Ninduction.pnext I p)) ≤ x → Q p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
672 f-induction0 p zero lt = Ninduction.fzero I {p} (fi0 _ lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
673 f-induction0 p (suc x) le with <-cmp (f (Ninduction.pnext I p)) (suc x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
674 ... | tri< a ¬b ¬c = f-induction0 p x (px≤py a)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
675 ... | tri≈ ¬a b ¬c = Ninduction.ind I (f-induction0 (Ninduction.pnext I p) x (s≤s→≤ nle) ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
676 f>0 : 0 < f (Ninduction.pnext I p)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
677 f>0 = subst (λ k → 0 < k ) (sym b) ( s≤s z≤n )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
678 nle : suc (f (Ninduction.pnext I (Ninduction.pnext I p))) ≤ suc x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
679 nle = subst (λ k → suc (f (Ninduction.pnext I (Ninduction.pnext I p))) ≤ k) b (Ninduction.decline I {Ninduction.pnext I p} f>0 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
680 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> le c )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
681
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
682
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
683 record Factor (n m : ℕ ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
684 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
685 factor : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
686 remain : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
687 is-factor : factor * n + remain ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
688
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
689 record Factor< (n m : ℕ ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
690 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
691 factor : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
692 remain : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
693 is-factor : factor * n + remain ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
694 remain<n : remain < n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
695
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
696 record Dividable (n m : ℕ ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
697 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
698 factor : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
699 is-factor : factor * n + 0 ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
700
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
701 open Factor
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
702
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
703 DtoF : {n m : ℕ} → Dividable n m → Factor n m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
704 DtoF {n} {m} record { factor = f ; is-factor = fa } = record { factor = f ; remain = 0 ; is-factor = fa }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
705
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
706 FtoD : {n m : ℕ} → (fc : Factor n m) → remain fc ≡ 0 → Dividable n m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
707 FtoD {n} {m} record { factor = f ; remain = r ; is-factor = fa } refl = record { factor = f ; is-factor = fa }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
708
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
709 --divdable^2 : ( n k : ℕ ) → Dividable k ( n * n ) → Dividable k n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
710 --divdable^2 n k dn2 = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
711
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
712 decf : { n k : ℕ } → ( x : Factor k (suc n) ) → Factor k n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
713 decf {n} {k} record { factor = f ; remain = r ; is-factor = fa } =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
714 decf1 {n} {k} f r fa where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
715 decf1 : { n k : ℕ } → (f r : ℕ) → (f * k + r ≡ suc n) → Factor k n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
716 decf1 {n} {k} f (suc r) fa = -- this case must be the first
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
717 record { factor = f ; remain = r ; is-factor = ( begin -- fa : f * k + suc r ≡ suc n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
718 f * k + r ≡⟨ cong pred ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
719 suc ( f * k + r ) ≡⟨ +-comm _ r ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
720 r + suc (f * k) ≡⟨ sym (+-assoc r 1 _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
721 (r + 1) + f * k ≡⟨ cong (λ t → t + f * k ) (+-comm r 1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
722 (suc r ) + f * k ≡⟨ +-comm (suc r) _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
723 f * k + suc r ≡⟨ fa ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
724 suc n ∎ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
725 n ∎ ) } where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
726 decf1 {n} {zero} (suc f) zero fa = ⊥-elim ( nat-≡< fa (
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
727 begin suc (suc f * zero + zero) ≡⟨ cong suc (+-comm _ zero) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
728 suc (f * 0) ≡⟨ cong suc (*-comm f zero) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
729 suc zero ≤⟨ s≤s z≤n ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
730 suc n ∎ )) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
731 decf1 {n} {suc k} (suc f) zero fa =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
732 record { factor = f ; remain = k ; is-factor = ( begin -- fa : suc (k + f * suc k + zero) ≡ suc n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
733 f * suc k + k ≡⟨ +-comm _ k ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
734 k + f * suc k ≡⟨ +-comm zero _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
735 (k + f * suc k) + zero ≡⟨ cong pred fa ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
736 n ∎ ) } where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
737
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
738 div0 : {k : ℕ} → Dividable k 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
739 div0 {k} = record { factor = 0; is-factor = refl }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
740
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
741 div= : {k : ℕ} → Dividable k k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
742 div= {k} = record { factor = 1; is-factor = ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
743 k + 0 * k + 0 ≡⟨ trans ( +-comm _ 0) ( +-comm _ 0) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
744 k ∎ ) } where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
745
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
746 div1 : { k : ℕ } → k > 1 → ¬ Dividable k 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
747 div1 {k} k>1 record { factor = f1 ; is-factor = fa } = ⊥-elim ( nat-≡< (sym fa) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
748 2 ≤⟨ k>1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
749 k ≡⟨ +-comm 0 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
750 k + 0 ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
751 1 * k ≤⟨ *-mono-≤ {1} {f1} (lem1 _ fa) ≤-refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
752 f1 * k ≡⟨ +-comm 0 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
753 f1 * k + 0 ∎ )) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
754 open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
755 lem1 : (f1 : ℕ) → f1 * k + 0 ≡ 1 → 1 ≤ f1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
756 lem1 zero ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
757 lem1 (suc f1) eq = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
758
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
759 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:
diff changeset
760 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:
diff changeset
761 fki = Dividable.factor di
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
762 fkj = Dividable.factor dj
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
763 div+div1 : Dividable k (i + j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
764 div+div1 = record { factor = fki + fkj ; is-factor = ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
765 (fki + fkj) * k + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
766 (fki + fkj) * k ≡⟨ *-distribʳ-+ k fki _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
767 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:
diff changeset
768 (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:
diff changeset
769 i + j ∎ ) } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
770 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
771
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
772 div-div : { i j k : ℕ } → k > 1 → Dividable k i → Dividable k j → Dividable k (i - j) ∧ Dividable k (j - i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
773 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:
diff changeset
774 div-div1 : {i j : ℕ } → Dividable k i → Dividable k j → Dividable k (i - j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
775 div-div1 {i} {j} di dj = record { factor = fki - fkj ; is-factor = ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
776 (fki - fkj) * k + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
777 (fki - fkj) * k ≡⟨ distr-minus-* {fki} {fkj} ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
778 (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:
diff changeset
779 (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:
diff changeset
780 i - j ∎ ) } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
781 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
782 fki = Dividable.factor di
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
783 fkj = Dividable.factor dj
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
784
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
785 open _∧_
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
786
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
787 div+1 : { i k : ℕ } → k > 1 → Dividable k i → ¬ Dividable k (suc i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
788 div+1 {i} {k} k>1 d d1 = div1 k>1 div+11 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
789 div+11 : Dividable k 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
790 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:
diff changeset
791
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
792 div<k : { m k : ℕ } → k > 1 → m > 0 → m < k → ¬ Dividable k m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
793 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:
diff changeset
794 div<k1 : (f : ℕ ) → f * k + 0 ≡ m → k ≤ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
795 div<k1 zero eq = ⊥-elim (nat-≡< eq m>0 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
796 div<k1 (suc f) eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
797 k ≤⟨ x≤x+y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
798 k + (f * k + 0) ≡⟨ sym (+-assoc k _ _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
799 k + f * k + 0 ≡⟨ eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
800 m ∎ where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
801
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
802 0<factor : { m k : ℕ } → k > 0 → m > 0 → (d : Dividable k m ) → Dividable.factor d > 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
803 0<factor {m} {k} k>0 m>0 d with Dividable.factor d in eq1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
804 ... | zero = ⊥-elim ( nat-≡< ff1 m>0 ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
805 ff1 : 0 ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
806 ff1 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
807 0 ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
808 0 * k + 0 ≡⟨ cong (λ j → j * k + 0) (sym eq1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
809 Dividable.factor d * k + 0 ≡⟨ Dividable.is-factor d ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
810 m ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
811 ... | suc t = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
812
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
813 div→k≤m : { m k : ℕ } → k > 1 → m > 0 → Dividable k m → m ≥ k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
814 div→k≤m {m} {k} k>1 m>0 d with <-cmp m k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
815 ... | tri< a ¬b ¬c = ⊥-elim ( div<k k>1 m>0 a d )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
816 ... | tri≈ ¬a refl ¬c = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
817 ... | tri> ¬a ¬b c = <to≤ c
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
818
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
819 div1*k+0=k : {k : ℕ } → 1 * k + 0 ≡ k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
820 div1*k+0=k {k} = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
821 1 * k + 0 ≡⟨ cong (λ g → g + 0) (+-comm _ 0) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
822 k + 0 ≡⟨ +-comm _ 0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
823 k ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
824
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
825
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
826 factor< : {k m : ℕ} → k > 1 → Factor< k m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
827 factor< {k} {m} k>1 = n-induction {_} {_} {ℕ} {λ m → Factor< k m} F I m where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
828 F : ℕ → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
829 F m = m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
830 F0 : ( m : ℕ ) → F (m - k) ≡ 0 → Factor< k m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
831 F0 0 eq = record { factor = 0 ; remain = 0 ; is-factor = refl ; remain<n = <-trans a<sa k>1 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
832 F0 (suc m) eq with <-cmp k (suc m)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
833 ... | tri< a ¬b ¬c = record { factor = 1 ; remain = 0 ; is-factor = lem00 ; remain<n = <-trans a<sa k>1 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
834 lem00 : k + zero + 0 ≡ suc m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
835 lem00 = begin -- minus (suc m) k ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
836 k + zero + 0 ≡⟨ +-comm (k + 0) _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
837 k + 0 ≡⟨ +-comm k _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
838 k ≡⟨ sym ( i-j=0→i=j (≤-trans a≤sa a) eq ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
839 suc m ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
840 ... | tri≈ ¬a b ¬c = record { factor = 1 ; remain = 0 ; is-factor = trans (trans (+-comm (k + 0) _) (+-comm k 0)) b ; remain<n = <-trans a<sa k>1 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
841 ... | tri> ¬a ¬b c = record { factor = 0 ; remain = suc m ; is-factor = refl ; remain<n = c }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
842 ind : {m : ℕ} → Factor< k (m - k) → Factor< k m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
843 ind {m} record { factor = f ; remain = r ; is-factor = isf ; remain<n = r<n } with <-cmp k (suc m)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
844 ... | tri≈ ¬a b ¬c = record { factor = 0 ; remain = m ; is-factor = refl ; remain<n = subst (λ j → m < j) (sym b) a<sa }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
845 ... | tri> ¬a ¬b c = record { factor = 0 ; remain = m ; is-factor = refl ; remain<n = <-trans a<sa c }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
846 ... | tri< a ¬b ¬c = record { factor = suc f ; remain = r ; is-factor = lem00 ; remain<n = r<n } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
847 k<sm : k < suc m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
848 k<sm = a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
849 lem00 : k + f * k + r ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
850 lem00 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
851 k + f * k + r ≡⟨ +-assoc k _ _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
852 k + (f * k + r) ≡⟨ +-comm k _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
853 (f * k + r) + k ≡⟨ cong (λ i → i + k ) isf ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
854 (m - k) + k ≡⟨ minus+n k<sm ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
855 m ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
856 decl : {m : ℕ } → 0 < m → m - k < m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
857 decl {m} 0<m = y-x<y (<-trans a<sa k>1 ) 0<m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
858 I : Ninduction ℕ _ F
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
859 I = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
860 pnext = λ p → p - k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
861 ; fzero = λ {m} eq → F0 m eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
862 ; decline = λ {m} lt → decl lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
863 ; ind = λ {p} prev → ind prev
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
864 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
865
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
866 Factor<→¬k≤m : {k m : ℕ} → k ≤ m → (x : Factor< k m ) → Factor<.factor x > 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
867 Factor<→¬k≤m {k} {m} k≤m x with Factor<.factor x in eqx
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
868 ... | zero = ⊥-elim ( nat-≤> k≤m (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
869 suc m ≡⟨ cong suc (sym (Factor<.is-factor x)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
870 suc (Factor<.factor x * k + Factor<.remain x) ≡⟨ cong (λ j → suc (j * k + _)) eqx ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
871 suc (0 * k + Factor<.remain x) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
872 suc (Factor<.remain x) ≤⟨ Factor<.remain<n x ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
873 k ∎ ) ) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
874 ... | suc fa = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
875
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
876 Factor<-inject : {k m : ℕ} → k > 1 → (x y : Factor< k m) → (Factor<.factor x ≡ Factor<.factor y ) ∧ (Factor<.remain x ≡ Factor<.remain y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
877 Factor<-inject {k} {m} k>1 x y = n-induction {_} {_} {ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
878 {λ m → (x y : Factor< k m) → (Factor<.factor x ≡ Factor<.factor y ) ∧ (Factor<.remain x ≡ Factor<.remain y ) } F I m x y where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
879 F : ℕ → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
880 F m = m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
881 f00 : (m : ℕ ) → ( k ≡ suc m ) → (x y : Factor< k (suc m)) → (Factor<.factor x ≡ Factor<.factor y ) ∧ (Factor<.remain x ≡ Factor<.remain y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
882 f00 m lem00 x y = ⟪ trans (lem02 x) (sym (lem02 y)) , trans (lem01 x) (sym (lem01 y)) ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
883 lem02 : (f : Factor< k (suc m)) → Factor<.factor f ≡ 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
884 lem02 f with <-cmp (Factor<.factor f) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
885 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≡< (Factor<.is-factor f) (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
886 suc (Factor<.factor f * k + Factor<.remain f) ≤⟨ s≤s (≤-plus {_} {_} {Factor<.remain f} (*≤ (px≤py a)) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
887 suc (0 * k + Factor<.remain f) ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
888 suc (0 + Factor<.remain f) ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
889 suc (Factor<.remain f) ≤⟨ Factor<.remain<n f ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
890 k ≡⟨ lem00 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
891 suc m ∎ )) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
892 ... | tri≈ ¬a b ¬c = b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
893 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (sym (Factor<.is-factor f)) (begin -- 1 < Factor<. factor f, fa * k + r > k ≡ suc m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
894 suc (suc m) ≡⟨ cong suc (sym lem00) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
895 suc k ≡⟨ sym (+-comm k 1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
896 k + 1 <⟨ <-plus-0 k>1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
897 k + k ≡⟨ cong (λ j → k + j) (+-comm 0 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
898 k + (k + 0) ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
899 k + (k + 0 * k) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
900 2 * k ≤⟨ *≤ c ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
901 Factor<.factor f * k ≤⟨ x≤x+y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
902 Factor<.factor f * k + Factor<.remain f ∎ ) ) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
903 lem03 : k ≡ 1 * k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
904 lem03 = +-comm 0 k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
905 lem01 : (f : Factor< k (suc m)) → Factor<.remain f ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
906 lem01 f = +-cancel-1 _ _ _ ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
907 Factor<.factor f * k + Factor<.remain f ≡⟨ Factor<.is-factor f ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
908 suc m ≡⟨ sym lem00 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
909 k ≡⟨ +-comm 0 k ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
910 k + 0 ≡⟨ cong (λ j → j + 0) lem03 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
911 1 * k + 0 ≡⟨ cong (λ j → j * k + 0) (sym (lem02 f)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
912 Factor<.factor f * k + 0 ∎ ) where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
913 F0 : ( m : ℕ ) → F (m - k) ≡ 0 → (x y : Factor< k m) → (Factor<.factor x ≡ Factor<.factor y ) ∧ (Factor<.remain x ≡ Factor<.remain y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
914 F0 0 eq x y = ⟪ trans (lem00 x) (sym (lem00 y)) , trans (lem01 x) (sym (lem01 y)) ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
915 lem01 : (f : Factor< k 0) → Factor<.remain f ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
916 lem01 f with Factor<.remain f in eq1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
917 ... | zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
918 ... | suc n = ⊥-elim ( nat-≡< (sym (Factor<.is-factor f)) (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
919 suc 0 ≤⟨ s≤s z≤n ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
920 suc n ≡⟨ sym eq1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
921 Factor<.remain f ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
922 0 + Factor<.remain f ≤⟨ x≤y+x ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
923 Factor<.factor f * k + Factor<.remain f ∎ ) ) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
924 lem00 : (f : Factor< k 0) → Factor<.factor f ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
925 lem00 f with m*n=0⇒m=0∨n=0 {Factor<.factor f} {k} (trans (+-comm 0 (Factor<.factor f * k) ) (subst (λ j → _ + j ≡ 0) (lem01 f) (Factor<.is-factor f)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
926 ... | case1 fa=0 = fa=0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
927 ... | case2 k=0 = ⊥-elim (nat-≡< (sym k=0) (<-trans a<sa k>1) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
928 F0 (suc m) eq x y with <-cmp k (suc m)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
929 ... | tri< a ¬b ¬c = ⊥-elim ( ¬b lem00 ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
930 lem00 : k ≡ suc m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
931 lem00 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
932 k ≡⟨ sym ( i-j=0→i=j (≤-trans a≤sa a) eq ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
933 suc m ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
934 ... | tri≈ ¬a b ¬c = f00 m b x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
935 ... | tri> ¬a ¬b c = ⟪ trans ( lem00 x ) (sym (lem00 y)) , trans (lem01 x) (sym (lem01 y)) ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
936 lem00 : (f : Factor< k (suc m)) → Factor<.factor f ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
937 lem00 f with Factor<.factor f in eq1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
938 ... | zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
939 ... | suc fa = ⊥-elim ( nat-≡< (sym (Factor<.is-factor f)) (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
940 suc (suc m) ≤⟨ c ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
941 k ≤⟨ x≤x+y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
942 k + Factor<.remain f ≡⟨ cong (λ j → j + _) (+-comm 0 k) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
943 suc 0 * k + Factor<.remain f ≤⟨ ≤-plus {_} {_} {Factor<.remain f} (*≤ {suc 0} {suc fa} {k} (s≤s z≤n)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
944 suc fa * k + Factor<.remain f ≡⟨ cong (λ j → j * k + Factor<.remain f) (sym eq1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
945 Factor<.factor f * k + Factor<.remain f ∎ ) ) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
946 lem01 : (f : Factor< k (suc m)) → Factor<.remain f ≡ (suc m)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
947 lem01 f = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
948 Factor<.remain f ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
949 0 * k + Factor<.remain f ≡⟨ cong (λ j → j * k + Factor<.remain f) (sym (lem00 f)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
950 Factor<.factor f * k + Factor<.remain f ≡⟨ Factor<.is-factor f ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
951 suc m ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
952 ind : {m : ℕ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
953 → ( (x y : Factor< k (m - k)) → (Factor<.factor x ≡ Factor<.factor y ) ∧ (Factor<.remain x ≡ Factor<.remain y ) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
954 → (x y : Factor< k m) → (Factor<.factor x ≡ Factor<.factor y ) ∧ (Factor<.remain x ≡ Factor<.remain y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
955 ind {m} prev x y with <∨≤ m k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
956 ... | case1 m<k = ⟪ trans (lem00 x) (sym (lem00 y)) , trans (lem01 x) (sym (lem01 y)) ⟫ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
957 lem00 : (x : Factor< k m ) → Factor<.factor x ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
958 lem00 x with Factor<.factor x in eqx
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
959 ... | zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
960 ... | suc fa = ⊥-elim ( nat-≤> (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
961 k ≤⟨ x≤x+y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
962 k + (fa * k + Factor<.remain x) ≡⟨ sym (+-assoc k _ _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
963 ( k + fa * k ) + Factor<.remain x ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
964 suc fa * k + Factor<.remain x ≡⟨ cong (λ j → j * k + Factor<.remain x) (sym eqx) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
965 Factor<.factor x * k + Factor<.remain x ≡⟨ Factor<.is-factor x ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
966 m ∎ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
967 m<k ) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
968 lem01 : (f : Factor< k m) → Factor<.remain f ≡ m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
969 lem01 f = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
970 Factor<.remain f ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
971 0 * k + Factor<.remain f ≡⟨ cong (λ j → j * k + Factor<.remain f) (sym (lem00 f)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
972 Factor<.factor f * k + Factor<.remain f ≡⟨ Factor<.is-factor f ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
973 m ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
974 ... | case2 k≤m = px=py x y k≤m where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
975 lem07 : (x : Factor< k m ) → {fa : ℕ } → suc fa ≡ Factor<.factor x → fa * k + Factor<.remain x ≡ m - k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
976 lem07 x {fa} eq1 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
977 fa * k + Factor<.remain x ≡⟨ sym (minus+y-y {_} {k} ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
978 (fa * k + Factor<.remain x + k ) - k ≡⟨ cong (λ j → j - k) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
979 fa * k + Factor<.remain x + k ≡⟨ +-assoc (fa * k) _ _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
980 fa * k + (Factor<.remain x + k) ≡⟨ cong (λ j → fa * k + j) (+-comm _ k) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
981 fa * k + (k + Factor<.remain x ) ≡⟨ sym (+-assoc (fa * k) k _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
982 (fa * k + k) + Factor<.remain x ≡⟨ cong (λ j → j + Factor<.remain x ) (+-comm (fa * k) k) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
983 suc fa * k + Factor<.remain x ≡⟨ cong (λ j → j * k + _) (eq1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
984 Factor<.factor x * k + Factor<.remain x ∎ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
985 (Factor<.factor x * k + Factor<.remain x) - k ≡⟨ cong (λ j → j - k ) (Factor<.is-factor x) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
986 m - k ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
987 px=py : (x y : Factor< k m) → k ≤ m → (Factor<.factor x ≡ Factor<.factor y ) ∧ (Factor<.remain x ≡ Factor<.remain y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
988 px=py x y k≤m with Factor<.factor x in eqx | Factor<.factor y in eqy
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
989 ... | zero | _ = ⊥-elim ( nat-≡< (sym eqx) (Factor<→¬k≤m k≤m x) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
990 ... | _ | zero = ⊥-elim ( nat-≡< (sym eqy) (Factor<→¬k≤m k≤m y) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
991 ... | suc fx | suc fy with prev
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
992 record { factor = fx ; remain = Factor<.remain x ; is-factor = lem07 x (sym eqx) ; remain<n = Factor<.remain<n x }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
993 record { factor = fy ; remain = Factor<.remain y ; is-factor = lem07 y (sym eqy) ; remain<n = Factor<.remain<n y }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
994 ... | ⟪ eqf , eqr ⟫ = ⟪ cong suc eqf , eqr ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
995 decl : {m : ℕ } → 0 < m → m - k < m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
996 decl {m} 0<m = y-x<y (<-trans a<sa k>1 ) 0<m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
997 I : Ninduction ℕ _ F
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
998 I = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
999 pnext = λ p → p - k
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1000 ; fzero = λ {m} eq → F0 m eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1001 ; decline = λ {m} lt → decl lt
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1002 ; ind = λ {p} prev → ind prev
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1003 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1004
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1005 F<toD : {n m : ℕ} → (fc : Factor< n m) → Factor<.remain fc ≡ 0 → Dividable n m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1006 F<toD {n} {m} record { factor = f ; remain = r ; is-factor = fa ; remain<n = _ } refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1007 = record { factor = f ; is-factor = fa }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1008
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1009 DtoF< : {n m : ℕ} → Dividable n m → 0 < n → Factor< n m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1010 DtoF< {n} {m} record { factor = f ; is-factor = fa } 0<n = record { factor = f ; is-factor = fa ; remain = 0 ; remain<n = 0<n }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1011
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1012 F<to¬D : {n m nx : ℕ} → (fc : Factor< n m) → 1 < n → Factor<.remain fc ≡ suc nx → ¬ Dividable n m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1013 F<to¬D {n} {m} fc 1<n eq div = ⊥-elim ( nat-≡< (sym (proj2 ( Factor<-inject {n} {m} 1<n fc (DtoF< div (<-trans a<sa 1<n) )))) 0<r ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1014 0<r : 0 < Factor<.remain fc
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1015 0<r = subst ( λ k → 0 < k ) (sym eq) (s≤s z≤n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1016
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1017 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1018 -- we can use factor< and check Factor.remain ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1019 -- Factor.remain ≡ 0 → Dividable k m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1020 -- ¬ Factor.remain ≡ 0 → ¬ Dividable k m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1021 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1022 decD : {k m : ℕ} → k > 1 → Dec0 (Dividable k m )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1023 decD {k} {m} k>1 = dec0 (factor< {k} {m} k>1) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1024 dec0 : Factor< k m → Dec0 (Dividable k m)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1025 dec0 fc with Factor<.remain fc in eq1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1026 ... | zero = yes0 record { factor = Factor<.factor fc ; is-factor = trans (cong (λ j → Factor<.factor fc * k + j) (sym eq1)) (Factor<.is-factor fc) }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1027 ... | suc t = no0 ( λ dv → F<to¬D fc k>1 eq1 dv )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1028
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1029
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1030