annotate automaton-in-agda/src/bijection.agda @ 256:5aff0067b194

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 05 Jul 2021 11:10:15 +0900
parents 3fa72793620b
children 246da8456ad1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
176
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 175
diff changeset
1 module bijection where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Level renaming ( zero to Zero ; suc to Suc )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 open import Data.Nat
142
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
5 open import Data.Maybe
256
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
6 open import Data.List hiding ([_] ; sum )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 open import Data.Nat.Properties
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Relation.Nullary
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 open import Data.Empty
256
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
10 open import Data.Unit hiding ( _≤_ )
142
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
11 open import Relation.Binary.Core hiding (_⇔_)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 open import Relation.Binary.Definitions
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 open import Relation.Binary.PropositionalEquality
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 open import logic
256
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
16 open import nat
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 record Bijection {n m : Level} (R : Set n) (S : Set m) : Set (n Level.⊔ m) where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 field
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 fun← : S → R
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 fun→ : R → S
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 fiso← : (x : R) → fun← ( fun→ x ) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 fiso→ : (x : S ) → fun→ ( fun← x ) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
25 injection : {n m : Level} (R : Set n) (S : Set m) (f : R → S ) → Set (n Level.⊔ m)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
26 injection R S f = (x y : R) → f x ≡ f y → x ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
27
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 open Bijection
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
30 b→injection0 : {n m : Level} (R : Set n) (S : Set m) → (b : Bijection R S) → injection R S (fun→ b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
31 b→injection0 R S b x y eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
32 x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
33 ≡⟨ sym ( fiso← b x ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
34 fun← b ( fun→ b x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
35 ≡⟨ cong (λ k → fun← b k ) eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
36 fun← b ( fun→ b y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
37 ≡⟨ fiso← b y ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
38 y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
39 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
40
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
41 b→injection1 : {n m : Level} (R : Set n) (S : Set m) → (b : Bijection R S) → injection S R (fun← b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
42 b→injection1 R S b x y eq = trans ( sym ( fiso→ b x ) ) (trans ( cong (λ k → fun→ b k ) eq ) ( fiso→ b y ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
43
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
44 -- ¬ A = A → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
45
174
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
46 diag : {S : Set } (b : Bijection ( S → Bool ) S) → S → Bool
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
47 diag b n = not (fun← b n n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
48
174
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
49 diagonal : { S : Set } → ¬ Bijection ( S → Bool ) S
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
50 diagonal {S} b = diagn1 (fun→ b (diag b) ) refl where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
51 diagn1 : (n : S ) → ¬ (fun→ b (diag b) ≡ n )
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
52 diagn1 n dn = ¬t=f (diag b n ) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
53 not (diag b n)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 ≡⟨⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 not (not fun← b n n)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 ≡⟨ cong (λ k → not (k n) ) (sym (fiso← b _)) ⟩
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
57 not (fun← b (fun→ b (diag b)) n)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 ≡⟨ cong (λ k → not (fun← b k n) ) dn ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 not (fun← b n n)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 ≡⟨⟩
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
61 diag b n
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 ∎ ) where open ≡-Reasoning
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63
256
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
64
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
65 open _∧_
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
66
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
67 record NN ( i : ℕ) (nxn→n : ℕ → ℕ → ℕ) (n→nxn : ℕ → ℕ ∧ ℕ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
68 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
69 j k sum stage : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
70 nn : j + k ≡ sum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
71 ni : i ≡ j + stage
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
72 k1 : nxn→n j k ≡ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
73 k0 : n→nxn i ≡ ⟪ j , k ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
74 nn-unique : {j0 k0 : ℕ } → nxn→n j0 k0 ≡ i → ⟪ j , k ⟫ ≡ ⟪ j0 , k0 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
75
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
76 i≤0→i≡0 : {i : ℕ } → i ≤ 0 → i ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
77 i≤0→i≡0 {0} z≤n = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
78
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
79
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
80 nxn : Bijection ℕ (ℕ ∧ ℕ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
81 nxn = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
82 fun← = λ p → nxn→n (proj1 p) (proj2 p)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
83 ; fun→ = n→nxn
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
84 ; fiso← = n-id
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
85 ; fiso→ = λ x → nn-id (proj1 x) (proj2 x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
86 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
87 nxn→n : ℕ → ℕ → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
88 nxn→n zero zero = zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
89 nxn→n zero (suc j) = j + suc (nxn→n zero j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
90 nxn→n (suc i) zero = suc i + suc (nxn→n i zero)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
91 nxn→n (suc i) (suc j) = suc i + suc j + suc (nxn→n i (suc j))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
92 n→nxn : ℕ → ℕ ∧ ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
93 n→nxn zero = ⟪ 0 , 0 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
94 n→nxn (suc i) with n→nxn i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
95 ... | ⟪ x , zero ⟫ = ⟪ zero , suc x ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
96 ... | ⟪ x , suc y ⟫ = ⟪ suc x , y ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
97
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
98 nxn→n0 : { j k : ℕ } → nxn→n j k ≡ 0 → ( j ≡ 0 ) ∧ ( k ≡ 0 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
99 nxn→n0 {zero} {zero} eq = ⟪ refl , refl ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
100 nxn→n0 {zero} {(suc k)} eq = ⊥-elim ( nat-≡< (sym eq) (subst (λ k → 0 < k) (+-comm _ k) (s≤s z≤n)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
101 nxn→n0 {(suc j)} {zero} eq = ⊥-elim ( nat-≡< (sym eq) (s≤s z≤n) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
102 nxn→n0 {(suc j)} {(suc k)} eq = ⊥-elim ( nat-≡< (sym eq) (s≤s z≤n) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
103
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
104 nid20 : (i : ℕ) → i + (nxn→n 0 i) ≡ nxn→n i 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
105 nid20 zero = refl -- suc (i + (i + suc (nxn→n 0 i))) ≡ suc (i + suc (nxn→n i 0))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
106 nid20 (suc i) = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
107 suc (i + (i + suc (nxn→n 0 i))) ≡⟨ cong (λ k → suc (i + k)) (sym (+-assoc i 1 (nxn→n 0 i))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
108 suc (i + ((i + 1) + nxn→n 0 i)) ≡⟨ cong (λ k → suc (i + (k + nxn→n 0 i))) (+-comm i 1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
109 suc (i + suc (i + nxn→n 0 i)) ≡⟨ cong ( λ k → suc (i + suc k)) (nid20 i) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
110 suc (i + suc (nxn→n i 0)) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
112 nid4 : {i j : ℕ} → i + 1 + j ≡ i + suc j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
113 nid4 {zero} {j} = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
114 nid4 {suc i} {j} = cong suc (nid4 {i} {j} )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
115 nid5 : {i j k : ℕ} → i + suc (suc j) + suc k ≡ i + suc j + suc (suc k )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
116 nid5 {zero} {j} {k} = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
117 suc (suc j) + suc k ≡⟨ +-assoc 1 (suc j) _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
118 1 + (suc j + suc k) ≡⟨ +-comm 1 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
119 (suc j + suc k) + 1 ≡⟨ +-assoc (suc j) (suc k) _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
120 suc j + (suc k + 1) ≡⟨ cong (λ k → suc j + k ) (+-comm (suc k) 1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
121 suc j + suc (suc k) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
122 nid5 {suc i} {j} {k} = cong suc (nid5 {i} {j} {k} )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
123
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
124 nid2 : (i j : ℕ) → suc (nxn→n i (suc j)) ≡ nxn→n (suc i) j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
125 nid2 zero zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
126 nid2 zero (suc j) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
127 nid2 (suc i) zero = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
128 suc (nxn→n (suc i) 1) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
129 suc (suc (i + 1 + suc (nxn→n i 1))) ≡⟨ cong (λ k → suc (suc k)) nid4 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
130 suc (suc (i + suc (suc (nxn→n i 1)))) ≡⟨ cong (λ k → suc (suc (i + suc (suc k)))) (nid3 i) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
131 suc (suc (i + suc (suc (i + suc (nxn→n i 0))))) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
132 nxn→n (suc (suc i)) zero ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
133 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
134 nid3 : (i : ℕ) → nxn→n i 1 ≡ i + suc (nxn→n i 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
135 nid3 zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
136 nid3 (suc i) = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
137 suc (i + 1 + suc (nxn→n i 1)) ≡⟨ cong suc nid4 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
138 suc (i + suc (suc (nxn→n i 1))) ≡⟨ cong (λ k → suc (i + suc (suc k))) (nid3 i) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
139 suc (i + suc (suc (i + suc (nxn→n i 0))))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
140
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
141 nid2 (suc i) (suc j) = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
142 suc (nxn→n (suc i) (suc (suc j))) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
143 suc (suc (i + suc (suc j) + suc (nxn→n i (suc (suc j))))) ≡⟨ cong (λ k → suc (suc (i + suc (suc j) + k))) (nid2 i (suc j)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
144 suc (suc (i + suc (suc j) + suc (i + suc j + suc (nxn→n i (suc j))))) ≡⟨ cong ( λ k → suc (suc k)) nid5 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
145 suc (suc (i + suc j + suc (suc (i + suc j + suc (nxn→n i (suc j)))))) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
146 nxn→n (suc (suc i)) (suc j) ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
147 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
148
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
149 nid00 : (i : ℕ) → suc (nxn→n i 0) ≡ nxn→n 0 (suc i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
150 nid00 zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
151 nid00 (suc i) = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
152 suc (suc (i + suc (nxn→n i 0))) ≡⟨ cong (λ k → suc (suc (i + k ))) (nid00 i) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
153 suc (suc (i + (nxn→n 0 (suc i)))) ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
154 suc (suc (i + (i + suc (nxn→n 0 i)))) ≡⟨ cong suc (sym ( +-assoc 1 i (i + suc (nxn→n 0 i)))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
155 suc ((1 + i) + (i + suc (nxn→n 0 i))) ≡⟨ cong (λ k → suc (k + (i + suc (nxn→n 0 i)))) (+-comm 1 i) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
156 suc ((i + 1) + (i + suc (nxn→n 0 i))) ≡⟨ cong suc (+-assoc i 1 (i + suc (nxn→n 0 i))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
157 suc (i + suc (i + suc (nxn→n 0 i))) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
158
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
159 nn : ( i : ℕ) → NN i nxn→n n→nxn
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
160 nn zero = record { j = 0 ; k = 0 ; sum = 0 ; stage = 0 ; nn = refl ; ni = refl ; k1 = refl ; k0 = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
161 ; nn-unique = λ {j0} {k0} eq → cong₂ (λ x y → ⟪ x , y ⟫) (sym (proj1 (nxn→n0 eq))) (sym (proj2 (nxn→n0 {j0} {k0} eq))) }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
162 nn (suc i) with NN.k (nn i) | inspect NN.k (nn i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
163 ... | zero | record { eq = eq } = record { k = suc (NN.sum (nn i)) ; j = 0 ; sum = suc (NN.sum (nn i)) ; stage = suc (NN.sum (nn i)) + (NN.stage (nn i))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
164 ; nn = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
165 ; ni = nn01 ; k1 = nn02 ; k0 = nn03 ; nn-unique = nn04 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
166 sum = NN.sum (nn i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
167 stage = NN.stage (nn i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
168 j = NN.j (nn i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
169 nn01 : suc i ≡ 0 + (suc sum + stage )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
170 nn01 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
171 suc i ≡⟨ cong suc (NN.ni (nn i)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
172 suc ((NN.j (nn i) ) + stage ) ≡⟨ cong (λ k → suc (k + stage )) (+-comm 0 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
173 suc ((NN.j (nn i) + 0 ) + stage ) ≡⟨ cong (λ k → suc ((NN.j (nn i) + k) + stage )) (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
174 suc ((NN.j (nn i) + NN.k (nn i)) + stage ) ≡⟨ cong (λ k → suc ( k + stage )) (NN.nn (nn i)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
175 0 + (suc sum + stage ) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
176 nn02 : nxn→n 0 (suc sum) ≡ suc i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
177 nn02 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
178 sum + suc (nxn→n 0 sum) ≡⟨ sym (+-assoc sum 1 (nxn→n 0 sum)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
179 (sum + 1) + nxn→n 0 sum ≡⟨ cong (λ k → k + nxn→n 0 sum ) (+-comm sum 1 )⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
180 suc (sum + nxn→n 0 sum) ≡⟨ cong suc (nid20 sum ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
181 suc (nxn→n sum 0) ≡⟨ cong (λ k → suc (nxn→n k 0 )) (sym (NN.nn (nn i))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
182 suc (nxn→n (NN.j (nn i) + (NN.k (nn i)) ) 0) ≡⟨ cong₂ (λ j k → suc (nxn→n (NN.j (nn i) + j) k )) eq (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
183 suc (nxn→n (NN.j (nn i) + 0 ) (NN.k (nn i))) ≡⟨ cong (λ k → suc ( nxn→n k (NN.k (nn i)))) (+-comm (NN.j (nn i)) 0) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
184 suc (nxn→n (NN.j (nn i)) (NN.k (nn i))) ≡⟨ cong suc (NN.k1 (nn i) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
185 suc i ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
186 nn03 : n→nxn (suc i) ≡ ⟪ 0 , suc (NN.sum (nn i)) ⟫ -- k0 : n→nxn i ≡ ⟪ NN.j (nn i) = sum , NN.k (nn i) = 0 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
187 nn03 with n→nxn i | inspect n→nxn i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
188 ... | ⟪ x , zero ⟫ | record { eq = eq1 } = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
189 ⟪ zero , suc x ⟫ ≡⟨ cong (λ k → ⟪ zero , suc k ⟫) (sym (cong proj1 eq1)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
190 ⟪ zero , suc (proj1 (n→nxn i)) ⟫ ≡⟨ cong (λ k → ⟪ zero , suc k ⟫) (cong proj1 (NN.k0 (nn i))) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
191 ⟪ zero , suc (NN.j (nn i)) ⟫ ≡⟨ cong (λ k → ⟪ zero , suc k ⟫) (+-comm 0 _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
192 ⟪ zero , suc (NN.j (nn i) + 0) ⟫ ≡⟨ cong (λ k → ⟪ zero , suc (NN.j (nn i) + k) ⟫ ) (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
193 ⟪ zero , suc (NN.j (nn i) + NN.k (nn i)) ⟫ ≡⟨ cong (λ k → ⟪ zero , suc k ⟫ ) (NN.nn (nn i)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
194 ⟪ 0 , suc sum ⟫ ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
195 ... | ⟪ x , suc y ⟫ | record { eq = eq1 } = ⊥-elim ( nat-≡< (sym (cong proj2 (NN.k0 (nn i)))) (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
196 suc (NN.k (nn i)) ≡⟨ cong suc eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
197 suc 0 ≤⟨ s≤s z≤n ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
198 suc y ≡⟨ sym (cong proj2 eq1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
199 proj2 (n→nxn i) ∎ )) where open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
200 -- nid2 : (i j : ℕ) → suc (nxn→n i (suc j)) ≡ nxn→n (suc i) j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
201 nn04 : {j0 k0 : ℕ} → nxn→n j0 k0 ≡ suc i → ⟪ 0 , suc (NN.sum (nn i)) ⟫ ≡ ⟪ j0 , k0 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
202 nn04 {zero} {suc k0} eq1 = cong (λ k → ⟪ 0 , k ⟫ ) (cong suc (sym nn08)) where -- eq : nxn→n zero (suc k0) ≡ suc i --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
203 nn07 : nxn→n k0 0 ≡ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
204 nn07 = cong pred ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
205 suc ( nxn→n k0 0 ) ≡⟨ nid00 k0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
206 nxn→n 0 (suc k0 ) ≡⟨ eq1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
207 suc i ∎ ) where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
208 nn08 : k0 ≡ sum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
209 nn08 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
210 k0 ≡⟨ cong proj1 (sym (NN.nn-unique (nn i) nn07)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
211 NN.j (nn i) ≡⟨ +-comm 0 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
212 NN.j (nn i) + 0 ≡⟨ cong (λ k → NN.j (nn i) + k) (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
213 NN.j (nn i) + NN.k (nn i) ≡⟨ NN.nn (nn i) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
214 sum ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
215 nn04 {suc j0} {k0} eq1 = ⊥-elim ( nat-≡< (cong proj2 (nn06 nn05)) (subst (λ k → k < suc k0) (sym eq) (s≤s z≤n))) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
216 nn05 : nxn→n j0 (suc k0) ≡ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
217 nn05 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
218 nxn→n j0 (suc k0) ≡⟨ cong pred ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
219 suc (nxn→n j0 (suc k0)) ≡⟨ nid2 j0 k0 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
220 nxn→n (suc j0) k0 ≡⟨ eq1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
221 suc i ∎ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
222 i ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
223 nn06 : nxn→n j0 (suc k0) ≡ i → ⟪ NN.j (nn i) , NN.k (nn i) ⟫ ≡ ⟪ j0 , suc k0 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
224 nn06 = NN.nn-unique (nn i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
225 ... | suc k | record {eq = eq} = record { k = k ; j = suc (NN.j (nn i)) ; sum = NN.sum (nn i) ; stage = NN.stage (nn i) ; nn = nn10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
226 ; ni = cong suc (NN.ni (nn i)) ; k1 = nn11 ; k0 = nn12 ; nn-unique = nn13 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
227 nn10 : suc (NN.j (nn i)) + k ≡ NN.sum (nn i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
228 nn10 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
229 suc (NN.j (nn i)) + k ≡⟨ cong (λ x → x + k) (+-comm 1 _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
230 (NN.j (nn i) + 1) + k ≡⟨ +-assoc (NN.j (nn i)) 1 k ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
231 NN.j (nn i) + suc k ≡⟨ cong (λ k → NN.j (nn i) + k) (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
232 NN.j (nn i) + NN.k (nn i) ≡⟨ NN.nn (nn i) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
233 NN.sum (nn i) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
234 nn11 : nxn→n (suc (NN.j (nn i))) k ≡ suc i -- nxn→n ( NN.j (nn i)) (NN.k (nn i) ≡ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
235 nn11 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
236 nxn→n (suc (NN.j (nn i))) k ≡⟨ sym (nid2 (NN.j (nn i)) k) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
237 suc (nxn→n (NN.j (nn i)) (suc k)) ≡⟨ cong (λ k → suc (nxn→n (NN.j (nn i)) k)) (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
238 suc (nxn→n ( NN.j (nn i)) (NN.k (nn i))) ≡⟨ cong suc (NN.k1 (nn i)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
239 suc i ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
240 nn18 : zero < NN.k (nn i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
241 nn18 = subst (λ k → 0 < k ) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
242 suc k ≡⟨ sym eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
243 NN.k (nn i) ∎ ) (s≤s z≤n ) where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
244 nn12 : n→nxn (suc i) ≡ ⟪ suc (NN.j (nn i)) , k ⟫ -- n→nxn i ≡ ⟪ NN.j (nn i) , NN.k (nn i) ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
245 nn12 with n→nxn i | inspect n→nxn i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
246 ... | ⟪ x , zero ⟫ | record { eq = eq1 } = ⊥-elim ( nat-≡< (sym (cong proj2 eq1))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
247 (subst (λ k → 0 < k ) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
248 suc k ≡⟨ sym eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
249 NN.k (nn i) ≡⟨ cong proj2 (sym (NN.k0 (nn i)) ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
250 proj2 (n→nxn i) ∎ ) (s≤s z≤n )) ) where open ≡-Reasoning -- eq1 n→nxn i ≡ ⟪ x , zero ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
251 ... | ⟪ x , suc y ⟫ | record { eq = eq1 } = begin -- n→nxn i ≡ ⟪ x , suc y ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
252 ⟪ suc x , y ⟫ ≡⟨ refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
253 ⟪ suc x , pred (suc y) ⟫ ≡⟨ cong (λ k → ⟪ suc (proj1 k) , pred (proj2 k) ⟫) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
254 ⟪ x , suc y ⟫ ≡⟨ sym eq1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
255 n→nxn i ≡⟨ NN.k0 (nn i) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
256 ⟪ NN.j (nn i) , NN.k (nn i) ⟫ ∎ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
257 ⟪ suc (NN.j (nn i)) , pred (NN.k (nn i)) ⟫ ≡⟨ cong (λ k → ⟪ suc (NN.j (nn i)) , pred k ⟫) eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
258 ⟪ suc (NN.j (nn i)) , k ⟫ ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
259 nn13 : {j0 k0 : ℕ} → nxn→n j0 k0 ≡ suc i → ⟪ suc (NN.j (nn i)) , k ⟫ ≡ ⟪ j0 , k0 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
260 nn13 {zero} {suc k0} eq1 = ⊥-elim ( nat-≡< (sym (cong proj2 nn17)) nn18 ) where -- (nxn→n zero (suc k0)) ≡ suc i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
261 nn16 : nxn→n k0 zero ≡ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
262 nn16 = cong pred ( subst (λ k → k ≡ suc i) (sym ( nid00 k0 )) eq1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
263 nn17 : ⟪ NN.j (nn i) , NN.k (nn i) ⟫ ≡ ⟪ k0 , zero ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
264 nn17 = NN.nn-unique (nn i) nn16
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
265 nn13 {suc j0} {k0} eq1 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
266 ⟪ suc (NN.j (nn i)) , pred (suc k) ⟫ ≡⟨ cong (λ k → ⟪ suc (NN.j (nn i)) , pred k ⟫ ) (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
267 ⟪ suc (NN.j (nn i)) , pred (NN.k (nn i)) ⟫ ≡⟨ cong (λ k → ⟪ suc (proj1 k) , pred (proj2 k) ⟫) ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
268 ⟪ NN.j (nn i) , NN.k (nn i) ⟫ ≡⟨ nn15 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
269 ⟪ j0 , suc k0 ⟫ ∎ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
270 ⟪ suc j0 , k0 ⟫ ∎ where -- nxn→n (suc j0) k0 ≡ suc i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
271 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
272 nn14 : nxn→n j0 (suc k0) ≡ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
273 nn14 = cong pred ( subst (λ k → k ≡ suc i) (sym ( nid2 j0 k0 )) eq1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
274 nn15 : ⟪ NN.j (nn i) , NN.k (nn i) ⟫ ≡ ⟪ j0 , suc k0 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
275 nn15 = NN.nn-unique (nn i) nn14
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
276
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
277 n-id : (i : ℕ) → nxn→n (proj1 (n→nxn i)) (proj2 (n→nxn i)) ≡ i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
278 n-id i = subst (λ k → nxn→n (proj1 k) (proj2 k) ≡ i ) (sym (NN.k0 (nn i))) (NN.k1 (nn i))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
279
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
280 nn-id : (j k : ℕ) → n→nxn (nxn→n j k) ≡ ⟪ j , k ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
281 nn-id j k = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
282 n→nxn (nxn→n j k) ≡⟨ NN.k0 (nn (nxn→n j k)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
283 ⟪ NN.j (nn (nxn→n j k)) , NN.k (nn (nxn→n j k)) ⟫ ≡⟨ NN.nn-unique (nn (nxn→n j k)) refl ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
284 ⟪ j , k ⟫ ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
285
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
286
164
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
287 b1 : (b : Bijection ( ℕ → Bool ) ℕ) → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
288 b1 b = fun→ b (diag b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
289
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
290 b-iso : (b : Bijection ( ℕ → Bool ) ℕ) → fun← b (b1 b) ≡ (diag b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
291 b-iso b = fiso← b _
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
292
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 to1 : {n : Level} {R : Set n} → Bijection ℕ R → Bijection ℕ (⊤ ∨ R )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 to1 {n} {R} b = record {
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 fun← = to11
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 ; fun→ = to12
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 ; fiso← = to13
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 ; fiso→ = to14
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 } where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 to11 : ⊤ ∨ R → ℕ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 to11 (case1 tt) = 0
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 to11 (case2 x) = suc ( fun← b x )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 to12 : ℕ → ⊤ ∨ R
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 to12 zero = case1 tt
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 to12 (suc n) = case2 ( fun→ b n)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 to13 : (x : ℕ) → to11 (to12 x) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 to13 zero = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 to13 (suc x) = cong suc (fiso← b x)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 to14 : (x : ⊤ ∨ R) → to12 (to11 x) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 to14 (case1 x) = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 to14 (case2 x) = cong case2 (fiso→ b x)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 open _∧_
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314
172
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
315 open import nat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
316
173
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
317 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
318
172
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
319 -- [] 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
320 -- 0 → 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
321 -- 1 → 2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
322 -- 01 → 3
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
323 -- 11 → 4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
324 -- ...
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 --
172
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
326 {-# TERMINATING #-}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
327 LBℕ : Bijection ℕ ( List Bool )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
328 LBℕ = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
329 fun← = λ x → lton x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
330 ; fun→ = λ n → ntol n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
331 ; fiso← = lbiso0
173
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
332 ; fiso→ = lbisor
172
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
333 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
334 lton1 : List Bool → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
335 lton1 [] = 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
336 lton1 (true ∷ t) = suc (lton1 t + lton1 t)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
337 lton1 (false ∷ t) = lton1 t + lton1 t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
338 lton : List Bool → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
339 lton [] = 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
340 lton x = suc (lton1 x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
341 ntol1 : ℕ → List Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
342 ntol1 0 = []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
343 ntol1 (suc x) with div2 (suc x)
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
344 ... | ⟪ x1 , true ⟫ = true ∷ ntol1 x1 -- non terminating
172
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
345 ... | ⟪ x1 , false ⟫ = false ∷ ntol1 x1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
346 ntol : ℕ → List Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
347 ntol 0 = []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
348 ntol 1 = false ∷ []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
349 ntol (suc n) = ntol1 n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
350 xx : (x : ℕ ) → List Bool ∧ ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
351 xx x = ⟪ (ntol x) , lton ((ntol x)) ⟫
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
352 add11 : (x1 : ℕ ) → suc x1 + suc x1 ≡ suc (suc (x1 + x1))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
353 add11 zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
354 add11 (suc x) = cong (λ k → suc (suc k)) (trans (+-comm x _) (cong suc (+-comm _ x)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
355 add12 : (x1 x : ℕ ) → suc x1 + x ≡ x1 + suc x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
356 add12 zero x = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
357 add12 (suc x1) x = cong suc (add12 x1 x)
175
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
358 ---- div2-eq : (x : ℕ ) → div2-rev ( div2 x ) ≡ x
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
359 div20 : (x x1 : ℕ ) → div2 (suc x) ≡ ⟪ x1 , false ⟫ → x1 + x1 ≡ suc x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
360 div20 x x1 eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
361 x1 + x1 ≡⟨ cong (λ k → div2-rev k ) (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
362 div2-rev (div2 (suc x)) ≡⟨ div2-eq _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
363 suc x ∎
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
364 div21 : (x x1 : ℕ ) → div2 (suc x) ≡ ⟪ x1 , true ⟫ → suc (x1 + x1) ≡ suc x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
365 div21 x x1 eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
366 suc (x1 + x1) ≡⟨ cong (λ k → div2-rev k ) (sym eq) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
367 div2-rev (div2 (suc x)) ≡⟨ div2-eq _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
368 suc x ∎
172
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
369 lbiso1 : (x : ℕ) → suc (lton1 (ntol1 x)) ≡ suc x
173
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
370 lbiso1 zero = refl
175
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
371 lbiso1 (suc x) with div2 (suc x) | inspect div2 (suc x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 174
diff changeset
372 ... | ⟪ x1 , true ⟫ | record { eq = eq1 } = begin
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
373 suc (suc (lton1 (ntol1 x1) + lton1 (ntol1 x1))) ≡⟨ sym (add11 _) ⟩
173
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
374 suc (lton1 (ntol1 x1)) + suc (lton1 (ntol1 x1)) ≡⟨ cong ( λ k → k + k ) (lbiso1 x1) ⟩
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
375 suc x1 + suc x1 ≡⟨ add11 x1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
376 suc (suc (x1 + x1)) ≡⟨ cong suc (div21 x x1 eq1) ⟩
173
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
377 suc (suc x) ∎
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
378 ... | ⟪ x1 , false ⟫ | record { eq = eq1 } = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
379 suc (lton1 (ntol1 x1) + lton1 (ntol1 x1)) ≡⟨ cong ( λ k → k + lton1 (ntol1 x1) ) (lbiso1 x1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
380 suc x1 + lton1 (ntol1 x1) ≡⟨ add12 _ _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
381 x1 + suc (lton1 (ntol1 x1)) ≡⟨ cong ( λ k → x1 + k ) (lbiso1 x1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
382 x1 + suc x1 ≡⟨ +-comm x1 _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
383 suc (x1 + x1) ≡⟨ cong suc (div20 x x1 eq1) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
384 suc (suc x) ∎
172
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
385 lbiso0 : (x : ℕ) → lton (ntol x) ≡ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
386 lbiso0 zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 164
diff changeset
387 lbiso0 (suc zero) = refl
173
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
388 lbiso0 (suc (suc x)) = subst (λ k → k ≡ suc (suc x)) (hh x) ( lbiso1 (suc x)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
389 hh : (x : ℕ ) → suc (lton1 (ntol1 (suc x))) ≡ lton (ntol (suc (suc x)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
390 hh x with div2 (suc x)
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
391 ... | ⟪ _ , true ⟫ = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
392 ... | ⟪ _ , false ⟫ = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
393 lbisor0 : (x : List Bool) → ntol1 (lton1 (true ∷ x)) ≡ true ∷ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
394 lbisor0 = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
395 lbisor1 : (x : List Bool) → ntol1 (lton1 (false ∷ x)) ≡ false ∷ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
396 lbisor1 = {!!}
173
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
397 lbisor : (x : List Bool) → ntol (lton x) ≡ x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
398 lbisor [] = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
399 lbisor (false ∷ []) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 172
diff changeset
400 lbisor (true ∷ []) = refl
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
401 lbisor (false ∷ t) = trans {!!} ( lbisor1 t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 176
diff changeset
402 lbisor (true ∷ t) = trans {!!} ( lbisor0 t )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
403
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
404