annotate automaton-in-agda/src/finiteSetUtil.agda @ 362:6d5344d3be9c

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 19 Jul 2023 07:08:43 +0900
parents c66d664593e9
children 21aa222b11c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
1 {-# OPTIONS --allow-unsolved-metas #-}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
3 module finiteSetUtil where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.Nat hiding ( _≟_ )
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
6 open import Data.Fin renaming ( _<_ to _<<_ ; _>_ to _f>_ ; _≟_ to _f≟_ ) hiding (_≤_ ; pred )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
7 open import Data.Fin.Properties hiding ( <-trans ; ≤-trans ; ≤-refl ; <-irrelevant ) renaming ( <-cmp to <-fcmp )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Data.Empty
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 open import Relation.Nullary
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 open import Relation.Binary.Definitions
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 open import Relation.Binary.PropositionalEquality
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 open import logic
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 open import nat
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 open import finiteSet
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
15 open import fin
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
16 open import Data.Nat.Properties as NP hiding ( _≟_ )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 open import Relation.Binary.HeterogeneousEquality as HE using (_≅_ )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
19 record Found ( Q : Set ) (p : Q → Bool ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
20 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
21 found-q : Q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
22 found-p : p found-q ≡ true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
23
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
24 open Bijection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
25
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
26 open import Axiom.Extensionality.Propositional
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
27 open import Level hiding (suc ; zero)
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
28 postulate f-extensionality : { n : Level} → Axiom.Extensionality.Propositional.Extensionality n n -- (Level.suc n)
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
29
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
30 module _ {Q : Set } (F : FiniteSet Q) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
31 open FiniteSet F
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
32 equal?-refl : { x : Q } → equal? x x ≡ true
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
33 equal?-refl {x} with F←Q x ≟ F←Q x
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
34 ... | yes refl = refl
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
35 ... | no ne = ⊥-elim (ne refl)
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
36 equal→refl : { x y : Q } → equal? x y ≡ true → x ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
37 equal→refl {q0} {q1} eq with F←Q q0 ≟ F←Q q1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
38 equal→refl {q0} {q1} refl | yes eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
39 q0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
40 ≡⟨ sym ( finiso→ q0) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
41 Q←F (F←Q q0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
42 ≡⟨ cong (λ k → Q←F k ) eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
43 Q←F (F←Q q1)
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
44 ≡⟨ finiso→ q1 ⟩
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
45 q1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
46 ∎ where open ≡-Reasoning
318
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 316
diff changeset
47 eqP : (x y : Q) → Dec ( x ≡ y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 316
diff changeset
48 eqP x y with F←Q x ≟ F←Q y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 316
diff changeset
49 ... | yes eq = yes (subst₂ (λ j k → j ≡ k ) (finiso→ x) (finiso→ y) (cong Q←F eq) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 316
diff changeset
50 ... | no n = no (λ eq → n (cong F←Q eq))
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
51 End : (m : ℕ ) → (p : Q → Bool ) → Set
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
52 End m p = (i : Fin finite) → m ≤ toℕ i → p (Q←F i ) ≡ false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
53 first-end : ( p : Q → Bool ) → End finite p
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
54 first-end p i i>n = ⊥-elim (nat-≤> i>n (fin<n {finite} i) )
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
55 next-end : {m : ℕ } → ( p : Q → Bool ) → End (suc m) p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
56 → (m<n : m < finite ) → p (Q←F (fromℕ< m<n )) ≡ false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
57 → End m p
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
58 next-end {m} p prev m<n np i m<i with NP.<-cmp m (toℕ i)
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
59 next-end p prev m<n np i m<i | tri< a ¬b ¬c = prev i a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
60 next-end p prev m<n np i m<i | tri> ¬a ¬b c = ⊥-elim ( nat-≤> m<i c )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
61 next-end {m} p prev m<n np i m<i | tri≈ ¬a b ¬c = subst ( λ k → p (Q←F k) ≡ false) (m<n=i i b m<n ) np where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
62 m<n=i : {n : ℕ } (i : Fin n) {m : ℕ } → m ≡ (toℕ i) → (m<n : m < n ) → fromℕ< m<n ≡ i
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
63 m<n=i i refl m<n = fromℕ<-toℕ i m<n
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
64 found : { p : Q → Bool } → (q : Q ) → p q ≡ true → exists p ≡ true
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
65 found {p} q pt = found1 finite (NP.≤-refl ) ( first-end p ) where
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
66 found1 : (m : ℕ ) (m<n : m Data.Nat.≤ finite ) → ((i : Fin finite) → m ≤ toℕ i → p (Q←F i ) ≡ false ) → exists1 m m<n p ≡ true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
67 found1 0 m<n end = ⊥-elim ( ¬-bool (subst (λ k → k ≡ false ) (cong (λ k → p k) (finiso→ q) ) (end (F←Q q) z≤n )) pt )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
68 found1 (suc m) m<n end with bool-≡-? (p (Q←F (fromℕ< m<n))) true
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
69 found1 (suc m) m<n end | yes eq = subst (λ k → k \/ exists1 m (<to≤ m<n) p ≡ true ) (sym eq) (bool-or-4 {exists1 m (<to≤ m<n) p} )
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
70 found1 (suc m) m<n end | no np = begin
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
71 p (Q←F (fromℕ< m<n)) \/ exists1 m (<to≤ m<n) p
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
72 ≡⟨ bool-or-1 (¬-bool-t np ) ⟩
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
73 exists1 m (<to≤ m<n) p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
74 ≡⟨ found1 m (<to≤ m<n) (next-end p end m<n (¬-bool-t np )) ⟩
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
75 true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
76 ∎ where open ≡-Reasoning
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
77 not-found : { p : Q → Bool } → ( (q : Q ) → p q ≡ false ) → exists p ≡ false
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
78 not-found {p} pn = not-found2 finite NP.≤-refl where
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
79 not-found2 : (m : ℕ ) → (m<n : m Data.Nat.≤ finite ) → exists1 m m<n p ≡ false
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
80 not-found2 zero _ = refl
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
81 not-found2 ( suc m ) m<n with pn (Q←F (fromℕ< {m} {finite} m<n))
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
82 not-found2 (suc m) m<n | eq = begin
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
83 p (Q←F (fromℕ< m<n)) \/ exists1 m (<to≤ m<n) p
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
84 ≡⟨ bool-or-1 eq ⟩
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
85 exists1 m (<to≤ m<n) p
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
86 ≡⟨ not-found2 m (<to≤ m<n) ⟩
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
87 false
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
88 ∎ where open ≡-Reasoning
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
89 found← : { p : Q → Bool } → exists p ≡ true → Found Q p
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
90 found← {p} exst = found2 finite NP.≤-refl (first-end p ) where
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
91 found2 : (m : ℕ ) (m<n : m Data.Nat.≤ finite ) → End m p → Found Q p
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
92 found2 0 m<n end = ⊥-elim ( ¬-bool (not-found (λ q → end (F←Q q) z≤n ) ) (subst (λ k → exists k ≡ true) (sym lemma) exst ) ) where
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
93 lemma : (λ z → p (Q←F (F←Q z))) ≡ p
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
94 lemma = f-extensionality ( λ q → subst (λ k → p k ≡ p q ) (sym (finiso→ q)) refl )
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
95 found2 (suc m) m<n end with bool-≡-? (p (Q←F (fromℕ< m<n))) true
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
96 found2 (suc m) m<n end | yes eq = record { found-q = Q←F (fromℕ< m<n) ; found-p = eq }
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
97 found2 (suc m) m<n end | no np =
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
98 found2 m (<to≤ m<n) (next-end p end m<n (¬-bool-t np ))
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
99 not-found← : { p : Q → Bool } → exists p ≡ false → (q : Q ) → p q ≡ false
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
100 not-found← {p} np q = ¬-bool-t ( contra-position {_} {_} {_} {exists p ≡ true} (found q) (λ ep → ¬-bool np ep ) )
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
101 Q←F-inject : {x y : Fin finite} → Q←F x ≡ Q←F y → x ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
102 Q←F-inject eq = subst₂ (λ j k → j ≡ k ) (finiso← _) (finiso← _) (cong F←Q eq)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
103 F←Q-inject : {x y : Q } → F←Q x ≡ F←Q y → x ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
104 F←Q-inject eq = subst₂ (λ j k → j ≡ k ) (finiso→ _) (finiso→ _) (cong Q←F eq)
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
105
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
106
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
107
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
108 iso-fin : {A B : Set} → FiniteSet A → Bijection A B → FiniteSet B
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 iso-fin {A} {B} fin iso = record {
330
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
110 Q←F = λ f → fun→ iso ( FiniteSet.Q←F fin f )
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
111 ; F←Q = λ b → FiniteSet.F←Q fin (fun← iso b )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
112 ; finiso→ = finiso→
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
113 ; finiso← = finiso←
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 } where
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
115 finiso→ : (q : B) → fun→ iso (FiniteSet.Q←F fin (FiniteSet.F←Q fin (Bijection.fun← iso q))) ≡ q
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 finiso→ q = begin
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
117 fun→ iso (FiniteSet.Q←F fin (FiniteSet.F←Q fin (Bijection.fun← iso q)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
118 ≡⟨ cong (λ k → fun→ iso k ) (FiniteSet.finiso→ fin _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
119 fun→ iso (Bijection.fun← iso q)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
120 ≡⟨ fiso→ iso _ ⟩
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 q
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
122 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
123 finiso← : (f : Fin (FiniteSet.finite fin ))→ FiniteSet.F←Q fin (Bijection.fun← iso (Bijection.fun→ iso (FiniteSet.Q←F fin f))) ≡ f
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 finiso← f = begin
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
125 FiniteSet.F←Q fin (Bijection.fun← iso (Bijection.fun→ iso (FiniteSet.Q←F fin f)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
126 ≡⟨ cong (λ k → FiniteSet.F←Q fin k ) (Bijection.fiso← iso _) ⟩
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 FiniteSet.F←Q fin (FiniteSet.Q←F fin f)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 ≡⟨ FiniteSet.finiso← fin _ ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 f
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 ∎ where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 open ≡-Reasoning
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 data One : Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 one : One
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 fin-∨1 : {B : Set} → (fb : FiniteSet B ) → FiniteSet (One ∨ B)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 fin-∨1 {B} fb = record {
330
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
138 Q←F = Q←F
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
139 ; F←Q = F←Q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
140 ; finiso→ = finiso→
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
141 ; finiso← = finiso←
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 } where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 b = FiniteSet.finite fb
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 Q←F : Fin (suc b) → One ∨ B
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 Q←F zero = case1 one
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 Q←F (suc f) = case2 (FiniteSet.Q←F fb f)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 F←Q : One ∨ B → Fin (suc b)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 F←Q (case1 one) = zero
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 F←Q (case2 f ) = suc (FiniteSet.F←Q fb f)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 finiso→ : (q : One ∨ B) → Q←F (F←Q q) ≡ q
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 finiso→ (case1 one) = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 finiso→ (case2 b) = cong (λ k → case2 k ) (FiniteSet.finiso→ fb b)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 finiso← : (q : Fin (suc b)) → F←Q (Q←F q) ≡ q
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 finiso← zero = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 finiso← (suc f) = cong ( λ k → suc k ) (FiniteSet.finiso← fb f)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 fin-∨2 : {B : Set} → ( a : ℕ ) → FiniteSet B → FiniteSet (Fin a ∨ B)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 fin-∨2 {B} zero fb = iso-fin fb iso where
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
160 iso : Bijection B (Fin zero ∨ B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
161 iso = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
162 fun← = fun←1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
163 ; fun→ = λ b → case2 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
164 ; fiso→ = fiso→1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
165 ; fiso← = λ _ → refl
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 } where
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
167 fun←1 : Fin zero ∨ B → B
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
168 fun←1 (case2 x) = x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
169 fiso→1 : (f : Fin zero ∨ B ) → case2 (fun←1 f) ≡ f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
170 fiso→1 (case2 x) = refl
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 fin-∨2 {B} (suc a) fb = iso-fin (fin-∨1 (fin-∨2 a fb) ) iso
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 where
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
173 iso : Bijection (One ∨ (Fin a ∨ B) ) (Fin (suc a) ∨ B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
174 fun← iso (case1 zero) = case1 one
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
175 fun← iso (case1 (suc f)) = case2 (case1 f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
176 fun← iso (case2 b) = case2 (case2 b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
177 fun→ iso (case1 one) = case1 zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
178 fun→ iso (case2 (case1 f)) = case1 (suc f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
179 fun→ iso (case2 (case2 b)) = case2 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
180 fiso← iso (case1 one) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
181 fiso← iso (case2 (case1 x)) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
182 fiso← iso (case2 (case2 x)) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
183 fiso→ iso (case1 zero) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
184 fiso→ iso (case1 (suc x)) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
185 fiso→ iso (case2 x) = refl
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
188 FiniteSet→Fin : {A : Set} → (fin : FiniteSet A ) → Bijection (Fin (FiniteSet.finite fin)) A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
189 fun← (FiniteSet→Fin fin) f = FiniteSet.F←Q fin f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
190 fun→ (FiniteSet→Fin fin) f = FiniteSet.Q←F fin f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
191 fiso← (FiniteSet→Fin fin) = FiniteSet.finiso← fin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
192 fiso→ (FiniteSet→Fin fin) = FiniteSet.finiso→ fin
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 fin-∨ : {A B : Set} → FiniteSet A → FiniteSet B → FiniteSet (A ∨ B)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 fin-∨ {A} {B} fa fb = iso-fin (fin-∨2 a fb ) iso2 where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 a = FiniteSet.finite fa
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 ia = FiniteSet→Fin fa
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
199 iso2 : Bijection (Fin a ∨ B ) (A ∨ B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
200 fun← iso2 (case1 x) = case1 (fun← ia x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
201 fun← iso2 (case2 x) = case2 x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
202 fun→ iso2 (case1 x) = case1 (fun→ ia x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
203 fun→ iso2 (case2 x) = case2 x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
204 fiso← iso2 (case1 x) = cong ( λ k → case1 k ) (Bijection.fiso← ia x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
205 fiso← iso2 (case2 x) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
206 fiso→ iso2 (case1 x) = cong ( λ k → case1 k ) (Bijection.fiso→ ia x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
207 fiso→ iso2 (case2 x) = refl
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
209 open import Data.Product hiding ( map )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 fin-× : {A B : Set} → FiniteSet A → FiniteSet B → FiniteSet (A × B)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 fin-× {A} {B} fa fb with FiniteSet→Fin fa
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 ... | a=f = iso-fin (fin-×-f a ) iso-1 where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 a = FiniteSet.finite fa
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 b = FiniteSet.finite fb
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
216 iso-1 : Bijection (Fin a × B) ( A × B )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
217 fun← iso-1 x = ( FiniteSet.F←Q fa (proj₁ x) , proj₂ x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
218 fun→ iso-1 x = ( FiniteSet.Q←F fa (proj₁ x) , proj₂ x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
219 fiso← iso-1 x = lemma where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 lemma : (FiniteSet.F←Q fa (FiniteSet.Q←F fa (proj₁ x)) , proj₂ x) ≡ ( proj₁ x , proj₂ x )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 lemma = cong ( λ k → ( k , proj₂ x ) ) (FiniteSet.finiso← fa _ )
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
222 fiso→ iso-1 x = cong ( λ k → ( k , proj₂ x ) ) (FiniteSet.finiso→ fa _ )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
224 iso-2 : {a : ℕ } → Bijection (B ∨ (Fin a × B)) (Fin (suc a) × B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
225 fun← iso-2 (zero , b ) = case1 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
226 fun← iso-2 (suc fst , b ) = case2 ( fst , b )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
227 fun→ iso-2 (case1 b) = ( zero , b )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
228 fun→ iso-2 (case2 (a , b )) = ( suc a , b )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
229 fiso← iso-2 (case1 x) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
230 fiso← iso-2 (case2 x) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
231 fiso→ iso-2 (zero , b ) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
232 fiso→ iso-2 (suc a , b ) = refl
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 fin-×-f : ( a : ℕ ) → FiniteSet ((Fin a) × B)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 fin-×-f zero = record { Q←F = λ () ; F←Q = λ () ; finiso→ = λ () ; finiso← = λ () ; finite = 0 }
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 fin-×-f (suc a) = iso-fin ( fin-∨ fb ( fin-×-f a ) ) iso-2
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 open _∧_
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 fin-∧ : {A B : Set} → FiniteSet A → FiniteSet B → FiniteSet (A ∧ B)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 fin-∧ {A} {B} fa fb with FiniteSet→Fin fa -- same thing for our tool
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 ... | a=f = iso-fin (fin-×-f a ) iso-1 where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 a = FiniteSet.finite fa
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 b = FiniteSet.finite fb
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
245 iso-1 : Bijection (Fin a ∧ B) ( A ∧ B )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
246 fun← iso-1 x = record { proj1 = FiniteSet.F←Q fa (proj1 x) ; proj2 = proj2 x}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
247 fun→ iso-1 x = record { proj1 = FiniteSet.Q←F fa (proj1 x) ; proj2 = proj2 x}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
248 fiso← iso-1 x = lemma where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 lemma : record { proj1 = FiniteSet.F←Q fa (FiniteSet.Q←F fa (proj1 x)) ; proj2 = proj2 x} ≡ record {proj1 = proj1 x ; proj2 = proj2 x }
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 lemma = cong ( λ k → record {proj1 = k ; proj2 = proj2 x } ) (FiniteSet.finiso← fa _ )
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
251 fiso→ iso-1 x = cong ( λ k → record {proj1 = k ; proj2 = proj2 x } ) (FiniteSet.finiso→ fa _ )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
253 iso-2 : {a : ℕ } → Bijection (B ∨ (Fin a ∧ B)) (Fin (suc a) ∧ B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
254 fun← iso-2 (record { proj1 = zero ; proj2 = b }) = case1 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
255 fun← iso-2 (record { proj1 = suc fst ; proj2 = b }) = case2 ( record { proj1 = fst ; proj2 = b } )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
256 fun→ iso-2 (case1 b) = record {proj1 = zero ; proj2 = b }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
257 fun→ iso-2 (case2 (record { proj1 = a ; proj2 = b })) = record { proj1 = suc a ; proj2 = b }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
258 fiso← iso-2 (case1 x) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
259 fiso← iso-2 (case2 x) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
260 fiso→ iso-2 (record { proj1 = zero ; proj2 = b }) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
261 fiso→ iso-2 (record { proj1 = suc a ; proj2 = b }) = refl
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 fin-×-f : ( a : ℕ ) → FiniteSet ((Fin a) ∧ B)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 fin-×-f zero = record { Q←F = λ () ; F←Q = λ () ; finiso→ = λ () ; finiso← = λ () ; finite = 0 }
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 fin-×-f (suc a) = iso-fin ( fin-∨ fb ( fin-×-f a ) ) iso-2
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 -- import Data.Nat.DivMod
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
269 open import Data.Vec hiding ( map ; length )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 import Data.Product
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 exp2 : (n : ℕ ) → exp 2 (suc n) ≡ exp 2 n Data.Nat.+ exp 2 n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 exp2 n = begin
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 exp 2 (suc n)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 ≡⟨⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 2 * ( exp 2 n )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 ≡⟨ *-comm 2 (exp 2 n) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 ( exp 2 n ) * 2
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 ≡⟨ *-suc ( exp 2 n ) 1 ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 (exp 2 n ) Data.Nat.+ ( exp 2 n ) * 1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 ≡⟨ cong ( λ k → (exp 2 n ) Data.Nat.+ k ) (proj₂ *-identity (exp 2 n) ) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 exp 2 n Data.Nat.+ exp 2 n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 ∎ where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 open ≡-Reasoning
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 open Data.Product
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 cast-iso : {n m : ℕ } → (eq : n ≡ m ) → (f : Fin m ) → cast eq ( cast (sym eq ) f) ≡ f
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 cast-iso refl zero = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 cast-iso refl (suc f) = cong ( λ k → suc k ) ( cast-iso refl f )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
290
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
291
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 fin2List : {n : ℕ } → FiniteSet (Vec Bool n)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 fin2List {zero} = record {
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 Q←F = λ _ → Vec.[]
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 ; F←Q = λ _ → # 0
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 ; finiso→ = finiso→
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 ; finiso← = finiso←
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 } where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 Q = Vec Bool zero
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 finiso→ : (q : Q) → [] ≡ q
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 finiso→ [] = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 finiso← : (f : Fin (exp 2 zero)) → # 0 ≡ f
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 finiso← zero = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 fin2List {suc n} = subst (λ k → FiniteSet (Vec Bool (suc n)) ) (sym (exp2 n)) ( iso-fin (fin-∨ (fin2List ) (fin2List )) iso )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 QtoR : Vec Bool (suc n) → Vec Bool n ∨ Vec Bool n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 QtoR ( true ∷ x ) = case1 x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 QtoR ( false ∷ x ) = case2 x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 RtoQ : Vec Bool n ∨ Vec Bool n → Vec Bool (suc n)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 RtoQ ( case1 x ) = true ∷ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 RtoQ ( case2 x ) = false ∷ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 isoRQ : (x : Vec Bool (suc n) ) → RtoQ ( QtoR x ) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 isoRQ (true ∷ _ ) = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 isoRQ (false ∷ _ ) = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 isoQR : (x : Vec Bool n ∨ Vec Bool n ) → QtoR ( RtoQ x ) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 isoQR (case1 x) = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 isoQR (case2 x) = refl
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
318 iso : Bijection (Vec Bool n ∨ Vec Bool n) (Vec Bool (suc n))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
319 iso = record { fun← = QtoR ; fun→ = RtoQ ; fiso← = isoQR ; fiso→ = isoRQ }
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 F2L : {Q : Set } {n : ℕ } → (fin : FiniteSet Q ) → n < suc (FiniteSet.finite fin) → ( (q : Q) → toℕ (FiniteSet.F←Q fin q ) < n → Bool ) → Vec Bool n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 F2L {Q} {zero} fin _ Q→B = []
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
323 F2L {Q} {suc n} fin (s≤s n<m) Q→B = Q→B (FiniteSet.Q←F fin (fromℕ< n<m)) lemma6 ∷ F2L {Q} fin (NP.<-trans n<m a<sa ) qb1 where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 lemma6 : toℕ (FiniteSet.F←Q fin (FiniteSet.Q←F fin (fromℕ< n<m))) < suc n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 lemma6 = subst (λ k → toℕ k < suc n ) (sym (FiniteSet.finiso← fin _ )) (subst (λ k → k < suc n) (sym (toℕ-fromℕ< n<m )) a<sa )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 qb1 : (q : Q) → toℕ (FiniteSet.F←Q fin q) < n → Bool
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
327 qb1 q q<n = Q→B q (NP.<-trans q<n a<sa)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
328
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 List2Func : { Q : Set } → {n : ℕ } → (fin : FiniteSet Q ) → n < suc (FiniteSet.finite fin) → Vec Bool n → Q → Bool
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 List2Func {Q} {zero} fin (s≤s z≤n) [] q = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 List2Func {Q} {suc n} fin (s≤s n<m) (h ∷ t) q with FiniteSet.F←Q fin q ≟ fromℕ< n<m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 ... | yes _ = h
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
333 ... | no _ = List2Func {Q} fin (NP.<-trans n<m a<sa ) t q
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 open import Level renaming ( suc to Suc ; zero to Zero)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 open import Axiom.Extensionality.Propositional
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 264
diff changeset
337 -- postulate f-extensionality : { n : Level} → Axiom.Extensionality.Propositional.Extensionality n n
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 F2L-iso : { Q : Set } → (fin : FiniteSet Q ) → (x : Vec Bool (FiniteSet.finite fin) ) → F2L fin a<sa (λ q _ → List2Func fin a<sa x q ) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 F2L-iso {Q} fin x = f2l m a<sa x where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 m = FiniteSet.finite fin
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 f2l : (n : ℕ ) → (n<m : n < suc m )→ (x : Vec Bool n ) → F2L fin n<m (λ q q<n → List2Func fin n<m x q ) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 f2l zero (s≤s z≤n) [] = refl
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
344 f2l (suc n) (s≤s n<m) (h ∷ t ) = lemma1 lemma2 lemma3f where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 lemma1 : {n : ℕ } → {h h1 : Bool } → {t t1 : Vec Bool n } → h ≡ h1 → t ≡ t1 → h ∷ t ≡ h1 ∷ t1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 lemma1 refl refl = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 lemma2 : List2Func fin (s≤s n<m) (h ∷ t) (FiniteSet.Q←F fin (fromℕ< n<m)) ≡ h
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 lemma2 with FiniteSet.F←Q fin (FiniteSet.Q←F fin (fromℕ< n<m)) ≟ fromℕ< n<m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 lemma2 | yes p = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 lemma2 | no ¬p = ⊥-elim ( ¬p (FiniteSet.finiso← fin _) )
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
351 lemma4 : (q : Q ) → toℕ (FiniteSet.F←Q fin q ) < n → List2Func fin (s≤s n<m) (h ∷ t) q ≡ List2Func fin (NP.<-trans n<m a<sa) t q
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 lemma4 q _ with FiniteSet.F←Q fin q ≟ fromℕ< n<m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 lemma4 q lt | yes p = ⊥-elim ( nat-≡< (toℕ-fromℕ< n<m) (lemma5 n lt (cong (λ k → toℕ k) p))) where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 lemma5 : {j k : ℕ } → ( n : ℕ) → suc j ≤ n → j ≡ k → k < n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 lemma5 {zero} (suc n) (s≤s z≤n) refl = s≤s z≤n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 lemma5 {suc j} (suc n) (s≤s lt) refl = s≤s (lemma5 {j} n lt refl)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 lemma4 q _ | no ¬p = refl
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
358 lemma3f : F2L fin (NP.<-trans n<m a<sa) (λ q q<n → List2Func fin (s≤s n<m) (h ∷ t) q ) ≡ t
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
359 lemma3f = begin
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
360 F2L fin (NP.<-trans n<m a<sa) (λ q q<n → List2Func fin (s≤s n<m) (h ∷ t) q )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
361 ≡⟨ cong (λ k → F2L fin (NP.<-trans n<m a<sa) ( λ q q<n → k q q<n ))
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 (f-extensionality ( λ q →
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 (f-extensionality ( λ q<n → lemma4 q q<n )))) ⟩
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
364 F2L fin (NP.<-trans n<m a<sa) (λ q q<n → List2Func fin (NP.<-trans n<m a<sa) t q )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
365 ≡⟨ f2l n (NP.<-trans n<m a<sa ) t ⟩
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 t
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 ∎ where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 open ≡-Reasoning
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
369
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
370
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 L2F : {Q : Set } {n : ℕ } → (fin : FiniteSet Q ) → n < suc (FiniteSet.finite fin) → Vec Bool n → (q : Q ) → toℕ (FiniteSet.F←Q fin q ) < n → Bool
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 L2F fin n<m x q q<n = List2Func fin n<m x q
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
373
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 L2F-iso : { Q : Set } → (fin : FiniteSet Q ) → (f : Q → Bool ) → (q : Q ) → (L2F fin a<sa (F2L fin a<sa (λ q _ → f q) )) q (toℕ<n _) ≡ f q
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 L2F-iso {Q} fin f q = l2f m a<sa (toℕ<n _) where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 m = FiniteSet.finite fin
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
377 lemma11f : {n : ℕ } → (n<m : n < m ) → ¬ ( FiniteSet.F←Q fin q ≡ fromℕ< n<m ) → toℕ (FiniteSet.F←Q fin q) ≤ n → toℕ (FiniteSet.F←Q fin q) < n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
378 lemma11f n<m ¬q=n q≤n = lemma13 n<m (contra-position (lemma12 n<m _) ¬q=n ) q≤n where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 lemma13 : {n nq : ℕ } → (n<m : n < m ) → ¬ ( nq ≡ n ) → nq ≤ n → nq < n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 lemma13 {0} {0} (s≤s z≤n) nt z≤n = ⊥-elim ( nt refl )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 lemma13 {suc _} {0} (s≤s (s≤s n<m)) nt z≤n = s≤s z≤n
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
382 lemma13 {suc n} {suc nq} n<m nt (s≤s nq≤n) = s≤s (lemma13 {n} {nq} (NP.<-trans a<sa n<m ) (λ eq → nt ( cong ( λ k → suc k ) eq )) nq≤n)
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
383 lemma3f : {a b : ℕ } → (lt : a < b ) → fromℕ< (s≤s lt) ≡ suc (fromℕ< lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
384 lemma3f (s≤s lt) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
385 lemma12f : {n m : ℕ } → (n<m : n < m ) → (f : Fin m ) → toℕ f ≡ n → f ≡ fromℕ< n<m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
386 lemma12f {zero} {suc m} (s≤s z≤n) zero refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
387 lemma12f {suc n} {suc m} (s≤s n<m) (suc f) refl = subst ( λ k → suc f ≡ k ) (sym (lemma3f n<m) ) ( cong ( λ k → suc k ) ( lemma12f {n} {m} n<m f refl ) )
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
388 l2f : (n : ℕ ) → (n<m : n < suc m ) → (q<n : toℕ (FiniteSet.F←Q fin q ) < n ) → (L2F fin n<m (F2L fin n<m (λ q _ → f q))) q q<n ≡ f q
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 l2f zero (s≤s z≤n) ()
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 l2f (suc n) (s≤s n<m) (s≤s n<q) with FiniteSet.F←Q fin q ≟ fromℕ< n<m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 l2f (suc n) (s≤s n<m) (s≤s n<q) | yes p = begin
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
392 f (FiniteSet.Q←F fin (fromℕ< n<m))
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 ≡⟨ cong ( λ k → f (FiniteSet.Q←F fin k )) (sym p) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 f (FiniteSet.Q←F fin ( FiniteSet.F←Q fin q ))
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 ≡⟨ cong ( λ k → f k ) (FiniteSet.finiso→ fin _ ) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 f q
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 ∎ where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
398 open ≡-Reasoning
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
399 l2f (suc n) (s≤s n<m) (s≤s n<q) | no ¬p = l2f n (NP.<-trans n<m a<sa) (lemma11f n<m ¬p n<q)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
400
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 fin→ : {A : Set} → FiniteSet A → FiniteSet (A → Bool )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 fin→ {A} fin = iso-fin fin2List iso where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
403 a = FiniteSet.finite fin
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
404 iso : Bijection (Vec Bool a ) (A → Bool)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
405 fun← iso x = F2L fin a<sa ( λ q _ → x q )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
406 fun→ iso x = List2Func fin a<sa x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
407 fiso← iso x = F2L-iso fin x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
408 fiso→ iso x = lemma where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 lemma : List2Func fin a<sa (F2L fin a<sa (λ q _ → x q)) ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 lemma = f-extensionality ( λ q → L2F-iso fin x q )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
412
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 Fin2Finite : ( n : ℕ ) → FiniteSet (Fin n)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
414 Fin2Finite n = record { F←Q = λ x → x ; Q←F = λ x → x ; finiso← = λ q → refl ; finiso→ = λ q → refl }
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
415
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
416 data fin-less { n : ℕ } { A : Set } (fa : FiniteSet A ) (n<m : n < FiniteSet.finite fa ) : Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
417 elm1 : (elm : A ) → toℕ (FiniteSet.F←Q fa elm ) < n → fin-less fa n<m
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
418
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
419 get-elm : { n : ℕ } { A : Set } {fa : FiniteSet A } {n<m : n < FiniteSet.finite fa } → fin-less fa n<m → A
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
420 get-elm (elm1 a _ ) = a
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
421
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 get-< : { n : ℕ } { A : Set } {fa : FiniteSet A } {n<m : n < FiniteSet.finite fa }→ (f : fin-less fa n<m ) → toℕ (FiniteSet.F←Q fa (get-elm f )) < n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
423 get-< (elm1 _ b ) = b
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
424
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
425 fin-less-cong : { n : ℕ } { A : Set } (fa : FiniteSet A ) (n<m : n < FiniteSet.finite fa )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
426 → (x y : fin-less fa n<m ) → get-elm {n} {A} {fa} x ≡ get-elm {n} {A} {fa} y → get-< x ≅ get-< y → x ≡ y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
427 fin-less-cong fa n<m (elm1 elm x) (elm1 elm x) refl HE.refl = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
428
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
429 fin-< : {A : Set} → { n : ℕ } → (fa : FiniteSet A ) → (n<m : n < FiniteSet.finite fa ) → FiniteSet (fin-less fa n<m )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
430 fin-< {A} {n} fa n<m = iso-fin (Fin2Finite n) iso where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
431 m = FiniteSet.finite fa
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
432 iso : Bijection (Fin n) (fin-less fa n<m )
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
433 lemma8f : {i j n : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → i<n ≅ j<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
434 lemma8f {zero} {zero} {suc n} refl {s≤s z≤n} {s≤s z≤n} = HE.refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
435 lemma8f {suc i} {suc i} {suc n} refl {s≤s i<n} {s≤s j<n} = HE.cong (λ k → s≤s k ) ( lemma8f {i} {i} refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
436 lemma10f : {n i j : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → fromℕ< i<n ≡ fromℕ< j<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
437 lemma10f refl = HE.≅-to-≡ (HE.cong (λ k → fromℕ< k ) (lemma8f refl ))
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
438 lemma3f : {a b c : ℕ } → { a<b : a < b } { b<c : b < c } { a<c : a < c } → NP.<-trans a<b b<c ≡ a<c
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
439 lemma3f {a} {b} {c} {a<b} {b<c} {a<c} = HE.≅-to-≡ (lemma8f refl)
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
440 lemma11f : {n : ℕ } {x : Fin n } → (n<m : n < m ) → toℕ (fromℕ< (NP.<-trans (toℕ<n x) n<m)) ≡ toℕ x
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 141
diff changeset
441 lemma11f {n} {x} n<m = begin
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
442 toℕ (fromℕ< (NP.<-trans (toℕ<n x) n<m))
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
443 ≡⟨ toℕ-fromℕ< _ ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
444 toℕ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
445 ∎ where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
446 open ≡-Reasoning
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
447 fun← iso (elm1 elm x) = fromℕ< x
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
448 fun→ iso x = elm1 (FiniteSet.Q←F fa (fromℕ< (NP.<-trans x<n n<m ))) to<n where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
449 x<n : toℕ x < n
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
450 x<n = toℕ<n x
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
451 to<n : toℕ (FiniteSet.F←Q fa (FiniteSet.Q←F fa (fromℕ< (NP.<-trans x<n n<m)))) < n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
452 to<n = subst (λ k → toℕ k < n ) (sym (FiniteSet.finiso← fa _ )) (subst (λ k → k < n ) (sym ( toℕ-fromℕ< (NP.<-trans x<n n<m) )) x<n )
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
453 fiso← iso x = lemma2 where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
454 lemma2 : fromℕ< (subst (λ k → toℕ k < n) (sym
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
455 (FiniteSet.finiso← fa (fromℕ< (NP.<-trans (toℕ<n x) n<m)))) (subst (λ k → k < n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
456 (sym (toℕ-fromℕ< (NP.<-trans (toℕ<n x) n<m))) (toℕ<n x))) ≡ x
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 lemma2 = begin
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 fromℕ< (subst (λ k → toℕ k < n) (sym
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
459 (FiniteSet.finiso← fa (fromℕ< (NP.<-trans (toℕ<n x) n<m)))) (subst (λ k → k < n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
460 (sym (toℕ-fromℕ< (NP.<-trans (toℕ<n x) n<m))) (toℕ<n x)))
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 ≡⟨⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
462 fromℕ< ( subst (λ k → toℕ ( k ) < n ) (sym (FiniteSet.finiso← fa _ )) lemma6 )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
463 ≡⟨ lemma10 (cong (λ k → toℕ k) (FiniteSet.finiso← fa _ ) ) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
464 fromℕ< lemma6
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
465 ≡⟨ lemma10 (lemma11 n<m ) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
466 fromℕ< ( toℕ<n x )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
467 ≡⟨ fromℕ<-toℕ _ _ ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
468 x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
469 ∎ where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
470 open ≡-Reasoning
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
471 lemma6 : toℕ (fromℕ< (NP.<-trans (toℕ<n x) n<m)) < n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
472 lemma6 = subst ( λ k → k < n ) (sym (toℕ-fromℕ< (NP.<-trans (toℕ<n x) n<m))) (toℕ<n x )
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
473 fiso→ iso (elm1 elm x) = fin-less-cong fa n<m _ _ lemma (lemma8 (cong (λ k → toℕ (FiniteSet.F←Q fa k) ) lemma ) ) where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
474 lemma13 : toℕ (fromℕ< x) ≡ toℕ (FiniteSet.F←Q fa elm)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
475 lemma13 = begin
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
476 toℕ (fromℕ< x)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
477 ≡⟨ toℕ-fromℕ< _ ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
478 toℕ (FiniteSet.F←Q fa elm)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
479 ∎ where open ≡-Reasoning
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
480 lemma : FiniteSet.Q←F fa (fromℕ< (NP.<-trans (toℕ<n (Bijection.fun← iso (elm1 elm x))) n<m)) ≡ elm
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
481 lemma = begin
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
482 FiniteSet.Q←F fa (fromℕ< (NP.<-trans (toℕ<n (Bijection.fun← iso (elm1 elm x))) n<m))
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
483 ≡⟨⟩
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
484 FiniteSet.Q←F fa (fromℕ< ( NP.<-trans (toℕ<n ( fromℕ< x ) ) n<m))
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
485 ≡⟨ cong (λ k → FiniteSet.Q←F fa k) (lemma10 lemma13 ) ⟩
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
486 FiniteSet.Q←F fa (fromℕ< ( NP.<-trans x n<m))
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
487 ≡⟨ cong (λ k → FiniteSet.Q←F fa (fromℕ< k )) (HE.≅-to-≡ (lemma8 refl)) ⟩
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
488 FiniteSet.Q←F fa (fromℕ< ( toℕ<n (FiniteSet.F←Q fa elm)))
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
489 ≡⟨ cong (λ k → FiniteSet.Q←F fa k ) ( fromℕ<-toℕ _ _ ) ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
490 FiniteSet.Q←F fa (FiniteSet.F←Q fa elm )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
491 ≡⟨ FiniteSet.finiso→ fa _ ⟩
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
492 elm
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 ∎ where open ≡-Reasoning
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
494
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
495 open import Data.List
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
496
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
497 open FiniteSet
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
498
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
499 memberQ : { Q : Set } (finq : FiniteSet Q) (q : Q) (qs : List Q ) → Bool
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
500 memberQ {Q} finq q [] = false
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
501 memberQ {Q} finq q (q0 ∷ qs) with equal? finq q q0
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
502 ... | true = true
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
503 ... | false = memberQ finq q qs
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
504
316
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
505 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
506 -- there is a duplicate element in finite list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
507 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
508
330
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
509 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
510 -- How about this?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
511 -- get list of Q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
512 -- remove one element for each Q from list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
513 -- there must be remaining list > 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
514 -- theses are duplicates
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
515 -- actualy it is duplicate
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 318
diff changeset
516
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
517 phase2 : { Q : Set } (finq : FiniteSet Q) (q : Q) (qs : List Q ) → Bool
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
518 phase2 finq q [] = false
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
519 phase2 finq q (x ∷ qs) with equal? finq q x
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
520 ... | true = true
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
521 ... | false = phase2 finq q qs
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
522 phase1 : { Q : Set } (finq : FiniteSet Q) (q : Q) (qs : List Q ) → Bool
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
523 phase1 finq q [] = false
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
524 phase1 finq q (x ∷ qs) with equal? finq q x
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
525 ... | true = phase2 finq q qs
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
526 ... | false = phase1 finq q qs
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
527
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
528 dup-in-list : { Q : Set } (finq : FiniteSet Q) (q : Q) (qs : List Q ) → Bool
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
529 dup-in-list {Q} finq q qs = phase1 finq q qs
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
530
316
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
531 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
532 -- if length of the list is longer than kinds of a finite set, there is a duplicate
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
533 -- prove this based on the theorem on Data.Fin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
534 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 294
diff changeset
535
283
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
536 dup-in-list+fin : { Q : Set } (finq : FiniteSet Q)
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
537 → (q : Q) (qs : List Q )
283
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
538 → fin-dup-in-list (F←Q finq q) (map (F←Q finq) qs) ≡ true
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
539 → dup-in-list finq q qs ≡ true
283
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
540 dup-in-list+fin {Q} finq q qs p = i-phase1 qs p where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
541 i-phase2 : (qs : List Q) → fin-phase2 (F←Q finq q) (map (F←Q finq) qs) ≡ true
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
542 → phase2 finq q qs ≡ true
294
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
543 i-phase2 (x ∷ qs) p with equal? finq q x | inspect (equal? finq q ) x | <-fcmp (F←Q finq q) (F←Q finq x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
544 ... | true | _ | t = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
545 ... | false | _ | tri< a ¬b ¬c = i-phase2 qs p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
546 ... | false | record { eq = eq } | tri≈ ¬a b ¬c = ⊥-elim (¬-bool eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
547 (subst₂ (λ j k → equal? finq j k ≡ true) (finiso→ finq q) (subst (λ k → Q←F finq k ≡ x) (sym b) (finiso→ finq x)) ( equal?-refl finq )))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
548 ... | false | _ | tri> ¬a ¬b c = i-phase2 qs p
283
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
549 i-phase1 : (qs : List Q) → fin-phase1 (F←Q finq q) (map (F←Q finq) qs) ≡ true
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
550 → phase1 finq q qs ≡ true
294
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
551 i-phase1 (x ∷ qs) p with equal? finq q x | inspect (equal? finq q ) x | <-fcmp (F←Q finq q) (F←Q finq x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
552 ... | true | record { eq = eq } | tri< a ¬b ¬c = ⊥-elim ( nat-≡< (cong (λ x → toℕ (F←Q finq x)) ( equal→refl finq eq )) a )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
553 ... | true | _ | tri≈ ¬a b ¬c = i-phase2 qs p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
554 ... | true | record { eq = eq} | tri> ¬a ¬b c = ⊥-elim ( nat-≡< (cong (λ x → toℕ (F←Q finq x)) (sym ( equal→refl finq eq ))) c )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
555 ... | false | _ | tri< a ¬b ¬c = i-phase1 qs p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
556 ... | false | record {eq = eq} | tri≈ ¬a b ¬c = ⊥-elim (¬-bool eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
557 (subst₂ (λ j k → equal? finq j k ≡ true) (finiso→ finq q) (subst (λ k → Q←F finq k ≡ x) (sym b) (finiso→ finq x)) ( equal?-refl finq )))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
558 ... | false | _ | tri> ¬a ¬b c = i-phase1 qs p
278
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
559
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
560 record Dup-in-list {Q : Set } (finq : FiniteSet Q) (qs : List Q) : Set where
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
561 field
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
562 dup : Q
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
563 is-dup : dup-in-list finq dup qs ≡ true
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
564
e89957b99662 dup in finiteSet in long list
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 268
diff changeset
565 dup-in-list>n : {Q : Set } → (finq : FiniteSet Q) → (qs : List Q) → (len> : length qs > finite finq ) → Dup-in-list finq qs
283
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
566 dup-in-list>n {Q} finq qs lt = record { dup = Q←F finq (FDup-in-list.dup dl)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
567 ; is-dup = dup-in-list+fin finq (Q←F finq (FDup-in-list.dup dl)) qs dl01 } where
294
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
568 maplen : (qs : List Q) → length (map (F←Q finq) qs) ≡ length qs
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
569 maplen [] = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
570 maplen (x ∷ qs) = cong suc (maplen qs)
283
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
571 dl : FDup-in-list (finite finq ) (map (F←Q finq) qs)
294
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
572 dl = fin-dup-in-list>n (map (F←Q finq) qs) (subst (λ k → k > finite finq ) (sym (maplen qs)) lt)
283
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
573 dl01 : fin-dup-in-list (F←Q finq (Q←F finq (FDup-in-list.dup dl))) (map (F←Q finq) qs) ≡ true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
574 dl01 = subst (λ k → fin-dup-in-list k (map (F←Q finq) qs) ≡ true )
294
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 283
diff changeset
575 (sym (finiso← finq _)) ( FDup-in-list.is-dup dl )
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
576
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
577 open import bijection using ( InjectiveF ; Is )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
578
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
579 -- open import Relation.Binary.HeterogeneousEquality as HE using (_≅_ )
345
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 341
diff changeset
580
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
581 inject-fin : {A B : Set} (fa : FiniteSet A )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
582 → (fi : InjectiveF B A)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
583 → (is-B : (a : A ) → Dec (Is B A (InjectiveF.f fi) a) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
584 → FiniteSet B
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
585 inject-fin {A} {B} fa fi is-B with finite fa | inspect finite fa
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
586 ... | zero | record { eq = eq1 } = record {
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
587 finite = 0
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
588 ; Q←F = λ ()
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
589 ; F←Q = λ b → ⊥-elim ( lem00 b)
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
590 ; finiso→ = λ b → ⊥-elim ( lem00 b)
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
591 ; finiso← = λ ()
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
592 } where
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
593 lem00 : ( b : B) → ⊥
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
594 lem00 b with subst (λ k → Fin k ) eq1 (F←Q fa (InjectiveF.f fi b))
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
595 ... | ()
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
596 ... | suc pfa | record { eq = eq1 } = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
597 finite = maxb
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
598 ; Q←F = λ fb → CountB.b (cb00 _ (fin<n {_} fb))
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
599 ; F←Q = λ b → fromℕ< (cb<mb b)
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
600 ; finiso→ = iso1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
601 ; finiso← = iso0
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
602 } where
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
603 f = InjectiveF.f fi
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
604 pfa<fa : pfa < finite fa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
605 pfa<fa = subst (λ k → pfa < k ) (sym eq1) a<sa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
606 0<fa : 0 < finite fa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
607 0<fa = <-transˡ (s≤s z≤n) pfa<fa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
608
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
609 count-B : ℕ → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
610 count-B zero with is-B (Q←F fa ( fromℕ< {0} 0<fa ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
611 ... | yes isb = 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
612 ... | no nisb = 0
350
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 349
diff changeset
613 count-B (suc n) with <-cmp (finite fa) (suc n)
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
614 ... | tri< a ¬b ¬c = count-B n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
615 ... | tri≈ ¬a b ¬c = count-B n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
616 ... | tri> ¬a ¬b c with is-B (Q←F fa (fromℕ< c))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
617 ... | yes isb = suc (count-B n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
618 ... | no nisb = count-B n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
619
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
620 record CountB (n : ℕ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
621 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
622 b : B
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
623 cb : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
624 b=cn : cb ≡ toℕ (F←Q fa (f b))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
625 cb=n : count-B cb ≡ suc n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
626 cb-inject : (cb1 : ℕ) → (c1<a : cb1 < finite fa) → Is B A f (Q←F fa (fromℕ< c1<a)) → count-B cb ≡ count-B cb1 → cb ≡ cb1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
627
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
628 maxb : ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
629 maxb = count-B (finite fa)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
630
352
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
631 count-B-mono : {i j : ℕ} → i ≤ j → count-B i ≤ count-B j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
632 count-B-mono {i} {j} i≤j with ≤-∨ i≤j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
633 ... | case1 refl = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
634 ... | case2 i<j = lem00 _ _ i<j where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
635 lem00 : (i j : ℕ) → i < j → count-B i ≤ count-B j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
636 lem00 i (suc j) (s≤s i<j) = ≤-trans (count-B-mono i<j) (lem01 j) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
637 lem01 : (j : ℕ) → count-B j ≤ count-B (suc j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
638 lem01 zero with <-cmp (finite fa) 1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
639 lem01 zero | tri< a ¬b ¬c = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
640 lem01 zero | tri≈ ¬a b ¬c = ≤-refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
641 lem01 zero | tri> ¬a ¬b c with is-B (Q←F fa (fromℕ< c)) | is-B (Q←F fa ( fromℕ< {0} 0<fa )) | inspect count-B 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
642 ... | yes isb1 | yes isb0 | record { eq = eq0 } = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
643 ... | yes isb1 | no nisb0 | record { eq = eq0 } = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
644 ... | no nisb1 | yes isb0 | record { eq = eq0 } = refl-≤≡ (sym eq0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
645 ... | no nisb1 | no nisb0 | record { eq = eq0 } = z≤n
359
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 358
diff changeset
646 lem01 (suc i) with <-cmp (finite fa) (suc i) | <-cmp (finite fa) (suc (suc i)) | inspect count-B (suc i) | inspect count-B (suc (suc i))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 358
diff changeset
647 ... | tri< a ¬b ¬c | tri< a₁ ¬b₁ ¬c₁ | record { eq = eq0 } | record { eq = eq1 } = refl-≤≡ (sym eq0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 358
diff changeset
648 ... | tri< a ¬b ¬c | tri≈ ¬a b ¬c₁ | _ | _ = ⊥-elim (nat-≡< b (<-trans a a<sa))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 358
diff changeset
649 ... | tri< a ¬b ¬c | tri> ¬a ¬b₁ c | _ | _ = ⊥-elim (nat-<> a (<-trans a<sa c) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 358
diff changeset
650 ... | tri≈ ¬a b ¬c | tri< a ¬b ¬c₁ | record { eq = eq0 } | _ = refl-≤≡ (sym eq0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 358
diff changeset
651 ... | tri≈ ¬a b ¬c | tri≈ ¬a₁ b₁ ¬c₁ | _ | _ = ⊥-elim (nat-≡< (sym b) (subst (λ k → _ < k ) (sym b₁) a<sa) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 358
diff changeset
652 ... | tri≈ ¬a b ¬c | tri> ¬a₁ ¬b c | _ | _ = ⊥-elim (nat-≡< (sym b) (<-trans a<sa c))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 358
diff changeset
653 ... | tri> ¬a ¬b c | tri< a ¬b₁ ¬c | _ | _ = ⊥-elim (nat-≤> a (<-transʳ c a<sa ) )
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
654 ... | tri> ¬a ¬b c | tri≈ ¬a₁ b ¬c | record { eq = eq0 } | record { eq = eq1 } with is-B (Q←F fa (fromℕ< c))
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
655 ... | yes isb = refl-≤≡ (sym eq0)
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
656 ... | no nisb = refl-≤≡ (sym eq0)
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
657 lem01 (suc i) | tri> ¬a ¬b c | tri> ¬a₁ ¬b₁ c₁ | record { eq = eq0 } | record { eq = eq1 }
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
658 with is-B (Q←F fa (fromℕ< c)) | is-B (Q←F fa (fromℕ< c₁))
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
659 ... | yes isb0 | yes isb1 = ≤-trans (refl-≤≡ (sym eq0)) a≤sa
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
660 ... | yes isb0 | no nisb1 = refl-≤≡ (sym eq0)
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
661 ... | no nisb0 | yes isb1 = ≤-trans (refl-≤≡ (sym eq0)) a≤sa
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
662 ... | no nisb0 | no nisb1 = refl-≤≡ (sym eq0)
352
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
663
353
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 352
diff changeset
664 lem31 : (b : B) → 0 < count-B (toℕ (F←Q fa (f b)))
358
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
665 lem31 b = lem32 (toℕ (F←Q fa (f b))) refl where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
666 lem32 : (i : ℕ) → toℕ (F←Q fa (f b)) ≡ i → 0 < count-B i
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
667 lem32 zero eq with is-B (Q←F fa ( fromℕ< {0} 0<fa ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
668 ... | yes isb = s≤s z≤n
354
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 353
diff changeset
669 ... | no nisb = ⊥-elim ( nisb record { a = b ; fa=c = lem33 } ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 353
diff changeset
670 lem33 : f b ≡ Q←F fa ( fromℕ< {0} 0<fa )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 353
diff changeset
671 lem33 = begin
355
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 354
diff changeset
672 f b ≡⟨ sym (finiso→ fa _) ⟩
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
673 Q←F fa ( F←Q fa (f b)) ≡⟨ sym (cong (λ k → Q←F fa k) ( fromℕ<-toℕ _ (fin<n _))) ⟩
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
674 Q←F fa ( fromℕ< (fin<n _) ) ≡⟨ cong (λ k → Q←F fa k) (fromℕ<-cong _ _ eq (fin<n _) 0<fa) ⟩
354
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 353
diff changeset
675 Q←F fa ( fromℕ< {0} 0<fa ) ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 353
diff changeset
676 open ≡-Reasoning
358
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
677 lem32 (suc i) eq with <-cmp (finite fa) (suc i) | inspect count-B (suc i)
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
678 ... | tri< a ¬b ¬c | _ = ⊥-elim ( nat-≡< eq (<-trans (fin<n _) a) )
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
679 ... | tri≈ ¬a eq1 ¬c | _ = ⊥-elim ( nat-≡< eq (subst (λ k → toℕ (F←Q fa (f b)) < k ) eq1 (fin<n _)))
358
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
680 ... | tri> ¬a ¬b c | record { eq = eq1 } with is-B (Q←F fa (fromℕ< c))
356
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 355
diff changeset
681 ... | yes isb = s≤s z≤n
358
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
682 ... | no nisb = ⊥-elim ( nisb record { a = b ; fa=c = lem33 } ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
683 lem33 : f b ≡ Q←F fa ( fromℕ< c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
684 lem33 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
685 f b ≡⟨ sym (finiso→ fa _) ⟩
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
686 Q←F fa ( F←Q fa (f b)) ≡⟨ sym (cong (λ k → Q←F fa k) ( fromℕ<-toℕ _ (fin<n _))) ⟩
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
687 Q←F fa ( fromℕ< (fin<n _) ) ≡⟨ cong (λ k → Q←F fa k) (fromℕ<-cong _ _ eq (fin<n _) c ) ⟩
358
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
688 Q←F fa ( fromℕ< c ) ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
689 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 357
diff changeset
690
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
691 cb<mb : (b : B) → pred (count-B (toℕ (F←Q fa (f b)))) < maxb
353
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 352
diff changeset
692 cb<mb b = sx≤y→x<y ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 352
diff changeset
693 suc ( pred (count-B (toℕ (F←Q fa (f b))))) ≡⟨ sucprd (lem31 b) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 352
diff changeset
694 count-B (toℕ (F←Q fa (f b))) ≤⟨ lem02 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 352
diff changeset
695 count-B (finite fa) ∎ ) where
352
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
696 open ≤-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 351
diff changeset
697 lem02 : count-B (toℕ (F←Q fa (f b))) ≤ count-B (finite fa)
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
698 lem02 = count-B-mono (<to≤ (fin<n {_} (F←Q fa (f b))))
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
699
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
700 cb00 : (n : ℕ) → n < count-B (finite fa) → CountB n
348
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
701 cb00 n n<cb = lem09 (finite fa) (count-B (finite fa)) (<-transˡ a<sa n<cb) refl where
341
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 338
diff changeset
702
361
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
703 lem06 : (i j : ℕ) → (i<fa : i < finite fa) (j<fa : j < finite fa)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
704 → Is B A f (Q←F fa (fromℕ< i<fa)) → Is B A f (Q←F fa (fromℕ< j<fa)) → count-B i ≡ count-B j → i ≡ j
362
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
705 lem06 i j i<fa j<fa bi bj eq = lem08 where
361
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
706 lem20 : (i j : ℕ) → i < j → (i<fa : i < finite fa) (j<fa : j < finite fa)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
707 → Is B A f (Q←F fa (fromℕ< i<fa)) → Is B A f (Q←F fa (fromℕ< j<fa)) → count-B j ≡ count-B i → ⊥
362
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
708 lem20 zero (suc j) i<j i<fa j<fa bi bj le with <-cmp (finite fa) (suc j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
709 ... | tri< a ¬b ¬c = ⊥-elim (¬c j<fa)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
710 ... | tri≈ ¬a b ¬c = ⊥-elim (¬c j<fa)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
711 ... | tri> ¬a ¬b c with is-B (Q←F fa ( fromℕ< 0<fa )) | inspect count-B 0 | is-B (Q←F fa (fromℕ< c)) | inspect count-B (suc j)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
712 ... | no nisc | _ | _ | _ = ⊥-elim (nisc record { a = Is.a bi ; fa=c = lem26 } ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
713 lem26 : f (Is.a bi) ≡ Q←F fa (fromℕ< 0<fa)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
714 lem26 = trans (Is.fa=c bi) (cong (Q←F fa) (fromℕ<-cong _ _ refl i<fa 0<fa) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
715 ... | yes _ | _ | no nisc | _ = ⊥-elim (nisc record { a = Is.a bj ; fa=c = lem26 } ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
716 lem26 : f (Is.a bj) ≡ Q←F fa (fromℕ< c)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
717 lem26 = trans (Is.fa=c bj) (cong (Q←F fa) (fromℕ<-cong _ _ refl j<fa c) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
718 ... | yes _ | record { eq = eq1 } | yes _ | record { eq = eq2 } = ⊥-elim ( nat-≤> lem25 a<sa) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
719 lem24 : count-B j ≡ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
720 lem24 = cong pred le
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
721 lem25 : 1 ≤ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
722 lem25 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
723 1 ≡⟨ sym eq1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
724 count-B 0 ≤⟨ count-B-mono {0} {j} z≤n ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
725 count-B j ≡⟨ lem24 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
726 0 ∎ where open ≤-Reasoning
361
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
727 lem20 (suc i) zero () bi bj le
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
728 lem20 (suc i) (suc j) (s≤s i<j) bi bj le = ?
362
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
729
361
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
730 lem08 : i ≡ j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
731 lem08 with <-cmp i j
362
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
732 ... | tri< a ¬b ¬c = ⊥-elim ? -- ( lem20 i j a i<fa j<fa bi bj (sym eq) )
361
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
733 ... | tri≈ ¬a b ¬c = b
362
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
734 ... | tri> ¬a ¬b c₁ = ⊥-elim ? -- ( lem20 j i c₁ j<fa i<fa bj bi eq )
361
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 360
diff changeset
735
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
736 lem09 : (i j : ℕ) → suc n ≤ j → j ≡ count-B i → CountB n
348
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
737 lem09 0 (suc j) (s≤s le) eq with is-B (Q←F fa (fromℕ< {0} 0<fa )) | inspect count-B 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
738 ... | no nisb | record { eq = eq1 } = ⊥-elim ( nat-≡< (sym eq) (s≤s z≤n) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
739 ... | yes isb | record { eq = eq1 } with ≤-∨ (s≤s le)
349
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
740 ... | case1 eq2 = record { b = Is.a isb ; cb = 0 ; b=cn = lem10 ; cb=n = trans eq1 (sym (trans eq2 eq))
362
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
741 ; cb-inject = λ cb1 c1<fa b1 eq → lem06 0 cb1 0<fa c1<fa isb b1 eq } where
349
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
742 lem10 : 0 ≡ toℕ (F←Q fa (f (Is.a isb)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
743 lem10 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
744 0 ≡⟨ sym ( toℕ-fromℕ< 0<fa ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
745 toℕ (fromℕ< {0} 0<fa ) ≡⟨ cong toℕ (sym (finiso← fa _)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
746 toℕ (F←Q fa (Q←F fa (fromℕ< {0} 0<fa ))) ≡⟨ cong (λ k → toℕ ((F←Q fa k))) (sym (Is.fa=c isb)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
747 toℕ (F←Q fa (f (Is.a isb))) ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
748 ... | case2 (s≤s lt) = ⊥-elim ( nat-≡< (sym eq) (s≤s (<-transʳ z≤n lt) ))
350
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 349
diff changeset
749 lem09 (suc i) (suc j) (s≤s le) eq with <-cmp (finite fa) (suc i) | inspect count-B (suc i)
348
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
750 ... | tri< a ¬b ¬c | _ = lem09 i (suc j) (s≤s le) eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
751 ... | tri≈ ¬a b ¬c | _ = lem09 i (suc j) (s≤s le) eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
752 ... | tri> ¬a ¬b c | record { eq = eq1 } with is-B (Q←F fa (fromℕ< c))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
753 ... | no nisb = lem09 i (suc j) (s≤s le) eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
754 ... | yes isb with ≤-∨ (s≤s le)
350
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 349
diff changeset
755 ... | case1 eq2 = record { b = Is.a isb ; cb = suc i ; b=cn = lem11 ; cb=n = trans eq1 (sym (trans eq2 eq ))
362
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 361
diff changeset
756 ; cb-inject = λ cb1 c1<fa b1 eq → lem06 (suc i) cb1 c c1<fa isb b1 eq } where
350
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 349
diff changeset
757 lem11 : suc i ≡ toℕ (F←Q fa (f (Is.a isb)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 349
diff changeset
758 lem11 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 349
diff changeset
759 suc i ≡⟨ sym ( toℕ-fromℕ< c) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 349
diff changeset
760 toℕ (fromℕ< c) ≡⟨ cong toℕ (sym (finiso← fa _)) ⟩
349
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
761 toℕ (F←Q fa (Q←F fa (fromℕ< c ))) ≡⟨ cong (λ k → toℕ ((F←Q fa k))) (sym (Is.fa=c isb)) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 348
diff changeset
762 toℕ (F←Q fa (f (Is.a isb))) ∎ where open ≡-Reasoning
348
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 347
diff changeset
763 ... | case2 (s≤s lt) = lem09 i j lt (cong pred eq)
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
764
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
765 iso0 : (x : Fin maxb) → fromℕ< (cb<mb (CountB.b (cb00 (toℕ x) (fin<n _)))) ≡ x
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
766 iso0 x = begin
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
767 fromℕ< (cb<mb (CountB.b (cb00 (toℕ x) (fin<n _)))) ≡⟨ fromℕ<-cong _ _ ( begin
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
768 pred (count-B (toℕ (F←Q fa (f (CountB.b (cb00 (toℕ x) (fin<n _))))))) ≡⟨ sym (cong (λ k → pred (count-B k)) (CountB.b=cn CB)) ⟩
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
769 pred (count-B (CountB.cb CB)) ≡⟨ cong pred (CountB.cb=n CB) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
770 pred (suc (toℕ x)) ≡⟨ refl ⟩
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
771 toℕ x ∎ ) (cb<mb (CountB.b CB)) (fin<n _) ⟩
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
772 fromℕ< (fin<n {_} x) ≡⟨ fromℕ<-toℕ _ (fin<n {_} x) ⟩
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
773 x ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
774 open ≡-Reasoning
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
775 CB = cb00 (toℕ x) (fin<n _)
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
776
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
777 iso1 : (b : B) → CountB.b (cb00 (toℕ (fromℕ< (cb<mb b))) (fin<n _)) ≡ b
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
778 iso1 b = begin
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
779 CountB.b CB ≡⟨ InjectiveF.inject fi (F←Q-inject fa (toℕ-injective (begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
780 toℕ (F←Q fa (f (CountB.b CB))) ≡⟨ sym (CountB.b=cn CB) ⟩
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
781 CountB.cb CB ≡⟨ CountB.cb-inject CB _ (fin<n _) isb lem30 ⟩
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
782 toℕ (F←Q fa (f b)) ∎ ) )) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
783 b ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
784 open ≡-Reasoning
360
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
785 CB = cb00 (toℕ (fromℕ< (cb<mb b))) (fin<n _)
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
786 isb : Is B A f (Q←F fa (fromℕ< (fin<n {_} (F←Q fa (f b)) )))
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
787 isb = record { a = b ; fa=c = lem33 } where
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
788 lem33 : f b ≡ Q←F fa (fromℕ< (fin<n (F←Q fa (f b))))
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
789 lem33 = begin
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
790 f b ≡⟨ sym (finiso→ fa _) ⟩
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
791 Q←F fa (F←Q fa (f b)) ≡⟨ cong (Q←F fa) (sym (fromℕ<-toℕ _ (fin<n (F←Q fa (f b))))) ⟩
6ba2836177b4 fix fin<n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 359
diff changeset
792 Q←F fa (fromℕ< (fin<n (F←Q fa (f b)))) ∎
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
793 lem30 : count-B (CountB.cb CB) ≡ count-B (toℕ (F←Q fa (InjectiveF.f fi b)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
794 lem30 = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
795 count-B (CountB.cb CB) ≡⟨ CountB.cb=n CB ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
796 suc (toℕ (fromℕ< (cb<mb b))) ≡⟨ cong suc (toℕ-fromℕ< (cb<mb b)) ⟩
353
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 352
diff changeset
797 suc (pred (count-B (toℕ (F←Q fa (f b))))) ≡⟨ sucprd (lem31 b) ⟩
351
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
798 count-B (toℕ (F←Q fa (f b))) ∎
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
799
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 350
diff changeset
800
347
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 345
diff changeset
801 -- end