annotate agda/finiteSet.agda @ 134:14cf0e1c8d91

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 24 Nov 2019 14:52:20 +0900
parents 65bea0aad363
children 2d70f90565c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
1 {-# OPTIONS --allow-unsolved-metas #-}
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 module finiteSet where
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
4 open import Data.Nat hiding ( _≟_ )
83
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
5 open import Data.Fin renaming ( _<_ to _<<_ ) hiding (_≤_)
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
6 open import Data.Fin.Properties
76
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 70
diff changeset
7 open import Data.Empty
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
8 open import Relation.Nullary
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 open import Relation.Binary.Core
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
10 open import Relation.Binary.PropositionalEquality
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
11 open import logic
78
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
12 open import nat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
13 open import Data.Nat.Properties hiding ( _≟_ )
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
79
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
15 open import Relation.Binary.HeterogeneousEquality as HE using (_≅_ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
16
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
17 record Found ( Q : Set ) (p : Q → Bool ) : Set where
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
18 field
92
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 88
diff changeset
19 found-q : Q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 88
diff changeset
20 found-p : p found-q ≡ true
79
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
21
115
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 114
diff changeset
22 lt0 : (n : ℕ) → n Data.Nat.≤ n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 114
diff changeset
23 lt0 zero = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 114
diff changeset
24 lt0 (suc n) = s≤s (lt0 n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 114
diff changeset
25 lt2 : {m n : ℕ} → m < n → m Data.Nat.≤ n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 114
diff changeset
26 lt2 {zero} lt = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 114
diff changeset
27 lt2 {suc m} {zero} ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 114
diff changeset
28 lt2 {suc m} {suc n} (s≤s lt) = s≤s (lt2 lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 114
diff changeset
29
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
30 record FiniteSet ( Q : Set ) { n : ℕ } : Set where
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 field
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 Q←F : Fin n → Q
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 F←Q : Q → Fin n
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 finiso→ : (q : Q) → Q←F ( F←Q q ) ≡ q
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 finiso← : (f : Fin n ) → F←Q ( Q←F f ) ≡ f
70
702ce92c45ab add concat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
36 finℕ : ℕ
702ce92c45ab add concat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
37 finℕ = n
76
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 70
diff changeset
38 exists1 : (m : ℕ ) → m Data.Nat.≤ n → (Q → Bool) → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 70
diff changeset
39 exists1 zero _ _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 70
diff changeset
40 exists1 ( suc m ) m<n p = p (Q←F (fromℕ≤ {m} {n} m<n)) \/ exists1 m (lt2 m<n) p
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 exists : ( Q → Bool ) → Bool
76
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 70
diff changeset
42 exists p = exists1 n (lt0 n) p
104
fba1cd54581d use exists in cond, nfa example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
43
114
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
44 open import Data.List
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
45 list1 : (m : ℕ ) → m Data.Nat.≤ n → (Q → Bool) → List Q
104
fba1cd54581d use exists in cond, nfa example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
46 list1 zero _ _ = []
fba1cd54581d use exists in cond, nfa example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
47 list1 ( suc m ) m<n p with bool-≡-? (p (Q←F (fromℕ≤ {m} {n} m<n))) true
fba1cd54581d use exists in cond, nfa example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
48 ... | yes _ = Q←F (fromℕ≤ {m} {n} m<n) ∷ list1 m (lt2 m<n) p
114
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
49 ... | no _ = list1 m (lt2 m<n) p
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
50 to-list : ( Q → Bool ) → List Q
104
fba1cd54581d use exists in cond, nfa example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
51 to-list p = list1 n (lt0 n) p
fba1cd54581d use exists in cond, nfa example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
52
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
53 equal? : Q → Q → Bool
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
54 equal? q0 q1 with F←Q q0 ≟ F←Q q1
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
55 ... | yes p = true
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
56 ... | no ¬p = false
95
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
57 equal→refl : { x y : Q } → equal? x y ≡ true → x ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
58 equal→refl {q0} {q1} eq with F←Q q0 ≟ F←Q q1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
59 equal→refl {q0} {q1} refl | yes eq = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
60 q0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
61 ≡⟨ sym ( finiso→ q0) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
62 Q←F (F←Q q0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
63 ≡⟨ cong (λ k → Q←F k ) eq ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
64 Q←F (F←Q q1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
65 ≡⟨ finiso→ q1 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
66 q1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
67 ∎ where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 92
diff changeset
68 equal→refl {q0} {q1} () | no ne
87
217ef727574a reverse direction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 85
diff changeset
69 equal?-refl : {q : Q} → equal? q q ≡ true
217ef727574a reverse direction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 85
diff changeset
70 equal?-refl {q} with F←Q q ≟ F←Q q
217ef727574a reverse direction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 85
diff changeset
71 ... | yes p = refl
217ef727574a reverse direction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 85
diff changeset
72 ... | no ne = ⊥-elim (ne refl)
77
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
73 fin<n : {n : ℕ} {f : Fin n} → toℕ f < n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
74 fin<n {_} {zero} = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
75 fin<n {suc n} {suc f} = s≤s (fin<n {n} {f})
84
29d81bcff049 found done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
76 i=j : {n : ℕ} (i j : Fin n) → toℕ i ≡ toℕ j → i ≡ j
29d81bcff049 found done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
77 i=j {suc n} zero zero refl = refl
29d81bcff049 found done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
78 i=j {suc n} (suc i) (suc j) eq = cong ( λ k → suc k ) ( i=j i j (cong ( λ k → Data.Nat.pred k ) eq) )
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
79 -- ¬∀⟶∃¬ : ∀ n {p} (P : Pred (Fin n) p) → Decidable P → ¬ (∀ i → P i) → (∃ λ i → ¬ P i)
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
80 End : (m : ℕ ) → (p : Q → Bool ) → Set
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
81 End m p = (i : Fin n) → m ≤ toℕ i → p (Q←F i ) ≡ false
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
82 next-end : {m : ℕ } → ( p : Q → Bool ) → End (suc m) p
83
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
83 → (m<n : m < n ) → p (Q←F (fromℕ≤ m<n )) ≡ false
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
84 → End m p
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
85 next-end {m} p prev m<n np i m<i with Data.Nat.Properties.<-cmp m (toℕ i)
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
86 next-end p prev m<n np i m<i | tri< a ¬b ¬c = prev i a
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
87 next-end p prev m<n np i m<i | tri> ¬a ¬b c = ⊥-elim ( nat-≤> m<i c )
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
88 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
83
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
89 m<n=i : {n : ℕ } (i : Fin n) {m : ℕ } → m ≡ (toℕ i) → (m<n : m < n ) → fromℕ≤ m<n ≡ i
84
29d81bcff049 found done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
90 m<n=i i eq m<n = i=j (fromℕ≤ m<n) i (subst (λ k → k ≡ toℕ i) (sym (toℕ-fromℕ≤ m<n)) eq )
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
91 first-end : ( p : Q → Bool ) → End n p
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
92 first-end p i i>n = ⊥-elim (nat-≤> i>n (fin<n {n} {i}) )
88
e7b3a2856ccb clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
93 found : { p : Q → Bool } → (q : Q ) → p q ≡ true → exists p ≡ true
e7b3a2856ccb clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
94 found {p} q pt = found1 n (lt0 n) ( first-end p ) where
83
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
95 found1 : (m : ℕ ) (m<n : m Data.Nat.≤ n ) → ((i : Fin n) → m ≤ toℕ i → p (Q←F i ) ≡ false ) → exists1 m m<n p ≡ true
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
96 found1 0 m<n end = ⊥-elim ( ¬-bool (subst (λ k → k ≡ false ) (cong (λ k → p k) (finiso→ q) ) (end (F←Q q) z≤n )) pt )
84
29d81bcff049 found done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
97 found1 (suc m) m<n end with bool-≡-? (p (Q←F (fromℕ≤ m<n))) true
29d81bcff049 found done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
98 found1 (suc m) m<n end | yes eq = subst (λ k → k \/ exists1 m (lt2 m<n) p ≡ true ) (sym eq) (bool-or-4 {exists1 m (lt2 m<n) p} )
29d81bcff049 found done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
99 found1 (suc m) m<n end | no np = begin
82
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
100 p (Q←F (fromℕ≤ m<n)) \/ exists1 m (lt2 m<n) p
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
101 ≡⟨ bool-or-1 (¬-bool-t np ) ⟩
82
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
102 exists1 m (lt2 m<n) p
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
103 ≡⟨ found1 m (lt2 m<n) (next-end p end m<n (¬-bool-t np )) ⟩
82
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
104 true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 81
diff changeset
105 ∎ where open ≡-Reasoning
83
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
106 not-found : { p : Q → Bool } → ( (q : Q ) → p q ≡ false ) → exists p ≡ false
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
107 not-found {p} pn = not-found2 n (lt0 n) where
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
108 not-found2 : (m : ℕ ) → (m<n : m Data.Nat.≤ n ) → exists1 m m<n p ≡ false
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
109 not-found2 zero _ = refl
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
110 not-found2 ( suc m ) m<n with pn (Q←F (fromℕ≤ {m} {n} m<n))
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
111 not-found2 (suc m) m<n | eq = begin
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
112 p (Q←F (fromℕ≤ m<n)) \/ exists1 m (lt2 m<n) p
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
113 ≡⟨ bool-or-1 eq ⟩
83
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
114 exists1 m (lt2 m<n) p
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
115 ≡⟨ not-found2 m (lt2 m<n) ⟩
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
116 false
92f396c3a1d7 add end function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 82
diff changeset
117 ∎ where open ≡-Reasoning
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
118 open import Level
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
119 postulate f-extensionality : { n : Level} → Relation.Binary.PropositionalEquality.Extensionality n n -- (Level.suc n)
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
120 found← : { p : Q → Bool } → exists p ≡ true → Found Q p
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
121 found← {p} exst = found2 n (lt0 n) (first-end p ) where
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
122 found2 : (m : ℕ ) (m<n : m Data.Nat.≤ n ) → End m p → Found Q p
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
123 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
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
124 lemma : (λ z → p (Q←F (F←Q z))) ≡ p
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
125 lemma = f-extensionality ( λ q → subst (λ k → p k ≡ p q ) (sym (finiso→ q)) refl )
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
126 found2 (suc m) m<n end with bool-≡-? (p (Q←F (fromℕ≤ m<n))) true
92
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 88
diff changeset
127 found2 (suc m) m<n end | yes eq = record { found-q = Q←F (fromℕ≤ m<n) ; found-p = eq }
85
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
128 found2 (suc m) m<n end | no np =
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
129 found2 m (lt2 m<n) (next-end p end m<n (¬-bool-t np ))
9911911b77cb all foundables
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 84
diff changeset
130 not-found← : { p : Q → Bool } → exists p ≡ false → (q : Q ) → p q ≡ false
88
e7b3a2856ccb clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
131 not-found← {p} np q = ¬-bool-t ( contra-position {_} {_} {_} {exists p ≡ true} (found q) (λ ep → ¬-bool np ep ) )
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132
119
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
133 record ISO (A B : Set) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
134 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
135 A←B : B → A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
136 B←A : A → B
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
137 iso← : (q : A) → A←B ( B←A q ) ≡ q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
138 iso→ : (f : B) → B←A ( A←B f ) ≡ f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
139
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
140 iso-fin : {A B : Set} → {n : ℕ } → FiniteSet A {n} → ISO A B → FiniteSet B {n}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
141 iso-fin {A} {B} {n} fin iso = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
142 Q←F = λ f → ISO.B←A iso ( FiniteSet.Q←F fin f )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
143 ; F←Q = λ b → FiniteSet.F←Q fin ( ISO.A←B iso b )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
144 ; finiso→ = finiso→
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
145 ; finiso← = finiso←
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
146 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
147 finiso→ : (q : B) → ISO.B←A iso (FiniteSet.Q←F fin (FiniteSet.F←Q fin (ISO.A←B iso q))) ≡ q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
148 finiso→ q = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
149 ISO.B←A iso (FiniteSet.Q←F fin (FiniteSet.F←Q fin (ISO.A←B iso q)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
150 ≡⟨ cong (λ k → ISO.B←A iso k ) (FiniteSet.finiso→ fin _ ) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
151 ISO.B←A iso (ISO.A←B iso q)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
152 ≡⟨ ISO.iso→ iso _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
153 q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
154 ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
155 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
156 finiso← : (f : Fin n) → FiniteSet.F←Q fin (ISO.A←B iso (ISO.B←A iso (FiniteSet.Q←F fin f))) ≡ f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
157 finiso← f = begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
158 FiniteSet.F←Q fin (ISO.A←B iso (ISO.B←A iso (FiniteSet.Q←F fin f)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
159 ≡⟨ cong (λ k → FiniteSet.F←Q fin k ) (ISO.iso← iso _) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
160 FiniteSet.F←Q fin (FiniteSet.Q←F fin f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
161 ≡⟨ FiniteSet.finiso← fin _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
162 f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
163 ∎ where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
164 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
165
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
166 data One : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
167 one : One
119
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
168
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
169 fin-∨1 : {B : Set} → { b : ℕ } → FiniteSet B {b} → FiniteSet (One ∨ B) {suc b}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
170 fin-∨1 {B} {b} fb = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
171 Q←F = Q←F
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
172 ; F←Q = F←Q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
173 ; finiso→ = finiso→
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
174 ; finiso← = finiso←
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
175 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
176 Q←F : Fin (suc b) → One ∨ B
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
177 Q←F zero = case1 one
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
178 Q←F (suc f) = case2 (FiniteSet.Q←F fb f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
179 F←Q : One ∨ B → Fin (suc b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
180 F←Q (case1 one) = zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
181 F←Q (case2 f ) = suc (FiniteSet.F←Q fb f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
182 finiso→ : (q : One ∨ B) → Q←F (F←Q q) ≡ q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
183 finiso→ (case1 one) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
184 finiso→ (case2 b) = cong (λ k → case2 k ) (FiniteSet.finiso→ fb b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
185 finiso← : (q : Fin (suc b)) → F←Q (Q←F q) ≡ q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
186 finiso← zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
187 finiso← (suc f) = cong ( λ k → suc k ) (FiniteSet.finiso← fb f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
188
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
189
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
190 fin-∨2 : {B : Set} → ( a : ℕ ) → { b : ℕ } → FiniteSet B {b} → FiniteSet (Fin a ∨ B) {a Data.Nat.+ b}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
191 fin-∨2 {B} zero {b} fb = iso-fin fb iso where
119
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
192 iso : ISO B (Fin zero ∨ B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
193 iso = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
194 A←B = A←B
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
195 ; B←A = λ b → case2 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
196 ; iso→ = iso→
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
197 ; iso← = λ _ → refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
198 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
199 A←B : Fin zero ∨ B → B
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
200 A←B (case2 x) = x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
201 iso→ : (f : Fin zero ∨ B ) → case2 (A←B f) ≡ f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
202 iso→ (case2 x) = refl
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
203 fin-∨2 {B} (suc a) {b} fb = iso-fin (fin-∨1 (fin-∨2 a fb) ) iso
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
204 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
205 iso : ISO (One ∨ (Fin a ∨ B) ) (Fin (suc a) ∨ B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
206 ISO.A←B iso (case1 zero) = case1 one
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
207 ISO.A←B iso (case1 (suc f)) = case2 (case1 f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
208 ISO.A←B iso (case2 b) = case2 (case2 b)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
209 ISO.B←A iso (case1 one) = case1 zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
210 ISO.B←A iso (case2 (case1 f)) = case1 (suc f)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
211 ISO.B←A iso (case2 (case2 b)) = case2 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
212 ISO.iso← iso (case1 one) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
213 ISO.iso← iso (case2 (case1 x)) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
214 ISO.iso← iso (case2 (case2 x)) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
215 ISO.iso→ iso (case1 zero) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
216 ISO.iso→ iso (case1 (suc x)) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
217 ISO.iso→ iso (case2 x) = refl
119
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
218
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
219
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
220 FiniteSet→Fin : {A : Set} → { a : ℕ } → (fin : FiniteSet A {a} ) → ISO (Fin a) A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
221 ISO.A←B (FiniteSet→Fin fin) f = FiniteSet.F←Q fin f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
222 ISO.B←A (FiniteSet→Fin fin) f = FiniteSet.Q←F fin f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
223 ISO.iso← (FiniteSet→Fin fin) = FiniteSet.finiso← fin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
224 ISO.iso→ (FiniteSet→Fin fin) = FiniteSet.finiso→ fin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
225
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
226
121
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
227 fin-∨ : {A B : Set} → { a b : ℕ } → FiniteSet A {a} → FiniteSet B {b} → FiniteSet (A ∨ B) {a Data.Nat.+ b}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
228 fin-∨ {A} {B} {a} {b} fa fb = iso-fin (fin-∨2 a fb ) iso2 where
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
229 ia = FiniteSet→Fin fa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
230 iso2 : ISO (Fin a ∨ B ) (A ∨ B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
231 ISO.A←B iso2 (case1 x) = case1 ( ISO.A←B ia x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
232 ISO.A←B iso2 (case2 x) = case2 x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
233 ISO.B←A iso2 (case1 x) = case1 ( ISO.B←A ia x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
234 ISO.B←A iso2 (case2 x) = case2 x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
235 ISO.iso← iso2 (case1 x) = cong ( λ k → case1 k ) (ISO.iso← ia x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
236 ISO.iso← iso2 (case2 x) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
237 ISO.iso→ iso2 (case1 x) = cong ( λ k → case1 k ) (ISO.iso→ ia x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
238 ISO.iso→ iso2 (case2 x) = refl
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
239
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
240 import Data.Nat.DivMod
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
241 import Data.Nat.Properties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
242
112
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 111
diff changeset
243 open _∧_
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 111
diff changeset
244
114
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
245 open import Data.Vec
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
246 import Data.Product
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
247
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
248 exp2 : (n : ℕ ) → exp 2 (suc n) ≡ exp 2 n Data.Nat.+ exp 2 n
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
249 exp2 n = begin
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
250 exp 2 (suc n)
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
251 ≡⟨⟩
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
252 2 * ( exp 2 n )
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
253 ≡⟨ *-comm 2 (exp 2 n) ⟩
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
254 ( exp 2 n ) * 2
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
255 ≡⟨ +-*-suc ( exp 2 n ) 1 ⟩
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
256 (exp 2 n ) Data.Nat.+ ( exp 2 n ) * 1
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
257 ≡⟨ cong ( λ k → (exp 2 n ) Data.Nat.+ k ) (proj₂ *-identity (exp 2 n) ) ⟩
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
258 exp 2 n Data.Nat.+ exp 2 n
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
259 ∎ where
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
260 open ≡-Reasoning
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
261 open Data.Product
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
262
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
263 cast-iso : {n m : ℕ } → (eq : n ≡ m ) → (f : Fin m ) → cast eq ( cast (sym eq ) f) ≡ f
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
264 cast-iso refl zero = refl
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
265 cast-iso refl (suc f) = cong ( λ k → suc k ) ( cast-iso refl f )
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
266
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
267
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
268 fin2List : {n : ℕ } → FiniteSet (Vec Bool n) {exp 2 n }
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
269 fin2List {zero} = record {
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
270 Q←F = λ _ → Vec.[]
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
271 ; F←Q = λ _ → # 0
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
272 ; finiso→ = finiso→
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
273 ; finiso← = finiso←
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
274 } where
114
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
275 Q = Vec Bool zero
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
276 finiso→ : (q : Q) → [] ≡ q
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
277 finiso→ [] = refl
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
278 finiso← : (f : Fin (exp 2 zero)) → # 0 ≡ f
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
279 finiso← zero = refl
121
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
280 fin2List {suc n} = subst (λ k → FiniteSet (Vec Bool (suc n)) {k} ) (sym (exp2 n)) ( iso-fin (fin-∨ (fin2List {n}) (fin2List {n})) iso )
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
281 where
114
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
282 QtoR : Vec Bool (suc n) → Vec Bool n ∨ Vec Bool n
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
283 QtoR ( true ∷ x ) = case1 x
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
284 QtoR ( false ∷ x ) = case2 x
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
285 RtoQ : Vec Bool n ∨ Vec Bool n → Vec Bool (suc n)
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
286 RtoQ ( case1 x ) = true ∷ x
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
287 RtoQ ( case2 x ) = false ∷ x
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
288 isoRQ : (x : Vec Bool (suc n) ) → RtoQ ( QtoR x ) ≡ x
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
289 isoRQ (true ∷ _ ) = refl
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
290 isoRQ (false ∷ _ ) = refl
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
291 isoQR : (x : Vec Bool n ∨ Vec Bool n ) → QtoR ( RtoQ x ) ≡ x
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
292 isoQR (case1 x) = refl
a7364dfcc51e finite-or
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 113
diff changeset
293 isoQR (case2 x) = refl
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
294 iso : ISO (Vec Bool n ∨ Vec Bool n) (Vec Bool (suc n))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
295 iso = record { A←B = QtoR ; B←A = RtoQ ; iso← = isoQR ; iso→ = isoRQ }
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
296
124
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
297 F2L : {Q : Set } {n m : ℕ } → n < suc m → (fin : FiniteSet Q {m} ) → ( (q : Q) → toℕ (FiniteSet.F←Q fin q ) < n → Bool ) → Vec Bool n
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
298 F2L {Q} {zero} fin _ Q→B = []
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
299 F2L {Q} {suc n} (s≤s n<m) fin Q→B = Q→B (FiniteSet.Q←F fin (fromℕ≤ n<m)) lemma6 ∷ F2L {Q} {n} (Data.Nat.Properties.<-trans n<m a<sa ) fin qb1 where
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
300 lemma6 : toℕ (FiniteSet.F←Q fin (FiniteSet.Q←F fin (fromℕ≤ n<m))) < suc n
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
301 lemma6 = subst (λ k → toℕ k < suc n ) (sym (FiniteSet.finiso← fin _ )) (subst (λ k → k < suc n) (sym (toℕ-fromℕ≤ n<m )) a<sa )
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
302 qb1 : (q : Q) → toℕ (FiniteSet.F←Q fin q) < n → Bool
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
303 qb1 q q<n = Q→B q (Data.Nat.Properties.<-trans q<n a<sa)
116
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
304
117
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
305 List2Func : { Q : Set } → {n m : ℕ } → n < suc m → FiniteSet Q {m} → Vec Bool n → Q → Bool
121
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
306 List2Func {Q} {zero} (s≤s z≤n) fin [] q = false
117
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
307 List2Func {Q} {suc n} {m} (s≤s n<m) fin (h ∷ t) q with FiniteSet.F←Q fin q ≟ fromℕ≤ n<m
116
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
308 ... | yes _ = h
117
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
309 ... | no _ = List2Func {Q} {n} {m} (Data.Nat.Properties.<-trans n<m a<sa ) fin t q
116
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
310
124
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
311 F2L-iso : { Q : Set } → {n : ℕ } → (fin : FiniteSet Q {n}) → (x : Vec Bool n ) → F2L a<sa fin (λ q _ → List2Func a<sa fin x q ) ≡ x
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
312 F2L-iso {Q} {m} fin x = f2l m a<sa x where
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
313 f2l : (n : ℕ ) → (n<m : n < suc m )→ (x : Vec Bool n ) → F2L n<m fin (λ q q<n → List2Func n<m fin x q ) ≡ x
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 119
diff changeset
314 f2l zero (s≤s z≤n) [] = refl
123
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
315 f2l (suc n) (s≤s n<m) (h ∷ t ) = lemma1 lemma2 lemma3 where
121
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
316 lemma1 : {n : ℕ } → {h h1 : Bool } → {t t1 : Vec Bool n } → h ≡ h1 → t ≡ t1 → h ∷ t ≡ h1 ∷ t1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
317 lemma1 refl refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
318 lemma2 : List2Func (s≤s n<m) fin (h ∷ t) (FiniteSet.Q←F fin (fromℕ≤ n<m)) ≡ h
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
319 lemma2 with FiniteSet.F←Q fin (FiniteSet.Q←F fin (fromℕ≤ n<m)) ≟ fromℕ≤ n<m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
320 lemma2 | yes p = refl
124
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
321 lemma2 | no ¬p = ⊥-elim ( ¬p (FiniteSet.finiso← fin _) )
123
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
322 lemma4 : (q : Q ) → toℕ (FiniteSet.F←Q fin q ) < n → List2Func (s≤s n<m) fin (h ∷ t) q ≡ List2Func (Data.Nat.Properties.<-trans n<m a<sa) fin t q
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
323 lemma4 q _ with FiniteSet.F←Q fin q ≟ fromℕ≤ n<m
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
324 lemma4 q lt | yes p = ⊥-elim ( nat-≡< (toℕ-fromℕ≤ n<m) (lemma5 n lt (cong (λ k → toℕ k) p))) where
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
325 lemma5 : {j k : ℕ } → ( n : ℕ) → suc j ≤ n → j ≡ k → k < n
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
326 lemma5 {zero} (suc n) (s≤s z≤n) refl = s≤s z≤n
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
327 lemma5 {suc j} (suc n) (s≤s lt) refl = s≤s (lemma5 {j} n lt refl)
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
328 lemma4 q _ | no ¬p = refl
124
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
329 lemma3 : F2L (Data.Nat.Properties.<-trans n<m a<sa) fin (λ q q<n → List2Func (s≤s n<m) fin (h ∷ t) q ) ≡ t
123
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
330 lemma3 = begin
124
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
331 F2L (Data.Nat.Properties.<-trans n<m a<sa) fin (λ q q<n → List2Func (s≤s n<m) fin (h ∷ t) q )
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
332 ≡⟨ cong (λ k → F2L (Data.Nat.Properties.<-trans n<m a<sa) fin ( λ q q<n → k q q<n ))
123
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
333 (FiniteSet.f-extensionality fin ( λ q →
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
334 (FiniteSet.f-extensionality fin ( λ q<n → lemma4 q q<n )))) ⟩
124
0ee5c7f46274 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 123
diff changeset
335 F2L (Data.Nat.Properties.<-trans n<m a<sa) fin (λ q q<n → List2Func (Data.Nat.Properties.<-trans n<m a<sa) fin t q )
123
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
336 ≡⟨ f2l n (Data.Nat.Properties.<-trans n<m a<sa ) t ⟩
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
337 t
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
338 ∎ where
f7f0a994daef F2L-iso done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 122
diff changeset
339 open ≡-Reasoning
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
340
125
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
341
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
342 L2F : {Q : Set } {n m : ℕ } → n < suc m → (fin : FiniteSet Q {m} ) → Vec Bool n → (q : Q ) → toℕ (FiniteSet.F←Q fin q ) < n → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
343 L2F n<m fin x q q<n = List2Func n<m fin x q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
344
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
345 L2F-iso : { Q : Set } → {n : ℕ } → (fin : FiniteSet Q {n}) → (f : Q → Bool ) → (q : Q ) → (L2F a<sa fin (F2L a<sa fin (λ q _ → f q) )) q (toℕ<n _) ≡ f q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
346 L2F-iso {Q} {m} fin f q = l2f m a<sa (toℕ<n _) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
347 lemma11 : {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: 124
diff changeset
348 lemma11 {n} n<m ¬q=n q≤n = lemma13 n<m (contra-position (lemma12 n<m _) ¬q=n ) q≤n where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
349 lemma13 : {n nq : ℕ } → (n<m : n < m ) → ¬ ( nq ≡ n ) → nq ≤ n → nq < n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
350 lemma13 {0} {0} (s≤s z≤n) nt z≤n = ⊥-elim ( nt refl )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
351 lemma13 {suc _} {0} (s≤s (s≤s n<m)) nt z≤n = s≤s z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
352 lemma13 {suc n} {suc nq} n<m nt (s≤s nq≤n) = s≤s (lemma13 {n} {nq} (Data.Nat.Properties.<-trans a<sa n<m ) (λ eq → nt ( cong ( λ k → suc k ) eq )) nq≤n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
353 lemma3 : {a b : ℕ } → (lt : a < b ) → fromℕ≤ (s≤s lt) ≡ suc (fromℕ≤ lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
354 lemma3 (s≤s lt) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
355 lemma12 : {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: 124
diff changeset
356 lemma12 {zero} {suc m} (s≤s z≤n) zero refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
357 lemma12 {suc n} {suc m} (s≤s n<m) (suc f) refl = subst ( λ k → suc f ≡ k ) (sym (lemma3 n<m) ) ( cong ( λ k → suc k ) ( lemma12 {n} {m} n<m f refl ) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
358 l2f : (n : ℕ ) → (n<m : n < suc m ) → (q<n : toℕ (FiniteSet.F←Q fin q ) < n ) → (L2F n<m fin (F2L n<m fin (λ q _ → f q))) q q<n ≡ f q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
359 l2f zero (s≤s z≤n) ()
126
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
360 l2f (suc n) (s≤s n<m) (s≤s n<q) with FiniteSet.F←Q fin q ≟ fromℕ≤ n<m
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
361 l2f (suc n) (s≤s n<m) (s≤s n<q) | yes p with inspect f q
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
362 l2f (suc n) (s≤s n<m) (s≤s n<q) | yes p | record { eq = refl } = begin
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
363 f (FiniteSet.Q←F fin (fromℕ≤ n<m))
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
364 ≡⟨ cong ( λ k → f (FiniteSet.Q←F fin k )) (sym p) ⟩
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
365 f (FiniteSet.Q←F fin ( FiniteSet.F←Q fin q ))
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
366 ≡⟨ cong ( λ k → f k ) (FiniteSet.finiso→ fin _ ) ⟩
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
367 f q
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
368 ∎ where
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
369 open ≡-Reasoning
125
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 124
diff changeset
370 l2f (suc n) (s≤s n<m) (s≤s n<q) | no ¬p = l2f n (Data.Nat.Properties.<-trans n<m a<sa) (lemma11 n<m ¬p n<q)
126
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
371
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
372 fin→ : {A : Set} → { a : ℕ } → FiniteSet A {a} → FiniteSet (A → Bool ) {exp 2 a}
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
373 fin→ {A} {a} fin = iso-fin fin2List iso where
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
374 iso : ISO (Vec Bool a ) (A → Bool)
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
375 ISO.A←B iso x = F2L a<sa fin ( λ q _ → x q )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
376 ISO.B←A iso x = List2Func a<sa fin x
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
377 ISO.iso← iso x = F2L-iso fin x
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
378 ISO.iso→ iso x = lemma where
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
379 lemma : List2Func a<sa fin (F2L a<sa fin (λ q _ → x q)) ≡ x
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
380 lemma = FiniteSet.f-extensionality fin ( λ q → L2F-iso fin x q )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
381
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
382
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
383 open import Data.Product
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
384
128
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
385 record Fin-< { n m : ℕ } (n<m : n < m ) { A : Set } (fa : FiniteSet A {m}) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
386 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
387 elm : A
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
388 elm<n : toℕ (FiniteSet.F←Q fa elm ) < n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
389
130
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
390 open Fin-<
133
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
391
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
392 Fin-<-cong : { n m : ℕ } (n<m : n < m ) { A : Set } (fa : FiniteSet A {m})
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
393 → ( s t : Fin-< n<m fa )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
394 → ( elm s ≡ elm t) → ( elm<n s ≅ elm<n t ) → s ≡ t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
395 Fin-<-cong n<m fa _ _ refl HE.refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
396
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
397 lemma1 : {m n : ℕ } → ( i j : m < n ) → i ≡ j
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
398 lemma1 {zero} {suc n} (s≤s z≤n) (s≤s z≤n) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
399 lemma1 {suc m} {suc n} (s≤s i) (s≤s j) = cong ( λ k → s≤s k ) ( lemma1 {m} {n} i j )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
400
128
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
401 fin-< : {A : Set} → { n m : ℕ } → (n<m : n < m ) → (fa : FiniteSet A {m}) → FiniteSet (Fin-< n<m fa) {n}
129
fb6237e9a98b bad direction on fin-<
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 128
diff changeset
402 fin-< {A} {zero} {m} (s≤s z≤n) fa = record { Q←F = λ () ; F←Q = λ () ; finiso← = λ () ; finiso→ = λ () }
fb6237e9a98b bad direction on fin-<
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 128
diff changeset
403 fin-< {A} {suc n} {m} (s≤s n<m) fa = iso-fin (fin-∨1 (fin-< {A} {n} {m} (Data.Nat.Properties.<-trans n<m a<sa) fa)) iso where
fb6237e9a98b bad direction on fin-<
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 128
diff changeset
404 fin- : FiniteSet (Fin-< (Data.Nat.Properties.<-trans n<m a<sa) fa)
fb6237e9a98b bad direction on fin-<
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 128
diff changeset
405 fin- = fin-< {A} {n} {m} (Data.Nat.Properties.<-trans n<m a<sa) fa
130
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
406 iso : ISO (One ∨ Fin-< (Data.Nat.Properties.<-trans n<m a<sa) fa) (Fin-< (s≤s n<m) fa)
132
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 131
diff changeset
407 lastf = FiniteSet.F←Q fa (FiniteSet.Q←F fa (fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa) ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 131
diff changeset
408 c1 : toℕ lastf ≡ n
130
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
409 c1 = subst (λ k → toℕ k ≡ n ) (sym (FiniteSet.finiso← fa _ )) (subst (λ k → k ≡ n) (sym (toℕ-fromℕ≤ _ )) refl )
132
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 131
diff changeset
410 f<n : toℕ lastf < suc n
130
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
411 f<n = subst ( λ k → k < suc n ) (sym c1) a<sa
133
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
412 last1 = FiniteSet.Q←F fa (fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa))
130
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
413 ISO.A←B iso x with Data.Nat.Properties.<-cmp (toℕ (FiniteSet.F←Q fa (elm x )) ) n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
414 ISO.A←B iso x | tri< a ¬b ¬c = case2 record { elm = elm x ; elm<n = a }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
415 ISO.A←B iso x | tri≈ ¬a b ¬c = case1 one
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
416 ISO.A←B iso x | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c (elm<n x) )
132
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 131
diff changeset
417 ISO.B←A iso (case1 one) = record { elm = last1 ; elm<n = f<n }
130
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
418 ISO.B←A iso (case2 x) = record { elm = elm x ; elm<n = Data.Nat.Properties.<-trans (elm<n x) a<sa }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
419 ISO.iso← iso (case1 one) with Data.Nat.Properties.<-cmp (toℕ (FiniteSet.F←Q fa (elm (ISO.B←A iso (case1 one))))) n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
420 ISO.iso← iso (case1 one) | tri< a ¬b ¬c = ⊥-elim ( ¬b c1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
421 ISO.iso← iso (case1 one) | tri≈ ¬a b ¬c = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
422 ISO.iso← iso (case1 one) | tri> ¬a ¬b c = ⊥-elim ( ¬b c1 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
423 ISO.iso← iso (case2 x) with Data.Nat.Properties.<-cmp (toℕ (FiniteSet.F←Q fa (elm x))) n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
424 ISO.iso← iso (case2 x) | tri< a ¬b ¬c = cong ( λ k → case2 record { elm = elm x ; elm<n = k } ) (lemma1 _ _) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
425 ISO.iso← iso (case2 x) | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< b (elm<n x) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
426 ISO.iso← iso (case2 x) | tri> ¬a ¬b c = ⊥-elim ( nat-<> c (elm<n x) )
132
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 131
diff changeset
427 ISO.iso→ iso x with ISO.A←B iso x
134
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 133
diff changeset
428 ISO.iso→ iso x | case1 one with Data.Nat.Properties.<-cmp (toℕ (FiniteSet.F←Q fa (elm x )) ) n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 133
diff changeset
429 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c (elm<n x) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 133
diff changeset
430 ... | tri< a ¬b ¬c = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 133
diff changeset
431 ... | tri≈ ¬a b ¬c = begin
133
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
432 record { elm = FiniteSet.Q←F fa (fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa)) ; elm<n = lemma5 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
433 ≡⟨ Fin-<-cong (s≤s n<m) fa _ _ (sym (lemma2 b)) lemma7 ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
434 record { elm = elm x ; elm<n = elm<n x }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
435 ≡⟨⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
436 x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
437
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
438 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
439 open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
440 lemma3 : {n m : ℕ } (x : Fin m) → toℕ x ≡ n → (n<m : n < m ) → x ≡ fromℕ≤ n<m
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
441 lemma3 _ refl n<m = sym ( fromℕ≤-toℕ _ n<m )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
442 lemma4 : {x : A } → (x=n : toℕ (FiniteSet.F←Q fa x) ≡ n ) → fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa) ≡ FiniteSet.F←Q fa x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
443 lemma4 {x} refl = sym ( lemma3 _ refl (Data.Nat.Properties.<-trans n<m a<sa))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
444 lemma2 : {x : A} → toℕ (FiniteSet.F←Q fa x) ≡ n → x ≡ FiniteSet.Q←F fa (fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
445 lemma2 {x} refl = sym ( begin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
446 FiniteSet.Q←F fa (fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
447 ≡⟨ cong (λ k → FiniteSet.Q←F fa k) (lemma4 refl) ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
448 FiniteSet.Q←F fa ( FiniteSet.F←Q fa x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
449 ≡⟨ FiniteSet.finiso→ fa _ ⟩
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
450 x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
451 ∎ ) where open ≡-Reasoning
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
452 lemma5 : toℕ (FiniteSet.F←Q fa (FiniteSet.Q←F fa (fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa)))) < suc n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
453 lemma5 = subst (λ k → suc k ≤ suc n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
454 (sym
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
455 (subst (λ k → toℕ k ≡ n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
456 (sym
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
457 (FiniteSet.finiso← fa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
458 (fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa))))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
459 (subst (λ k → k ≡ n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
460 (sym (toℕ-fromℕ≤ (Data.Nat.Properties.<-trans n<m a<sa))) refl)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
461 a<sa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
462 lemma7 : lemma5 ≅ elm<n x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
463 lemma7 with lemma2 b
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
464 ... | refl with lemma1 lemma5 (elm<n x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
465 ... | refl = HE.refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
466
132
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 131
diff changeset
467 ISO.iso→ iso x | case2 x1 = {!!}
131
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 130
diff changeset
468 -- ISO.iso→ iso x | case2 x1 | tri< a ¬b ¬c = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 130
diff changeset
469 -- ISO.iso→ iso x | case2 x1 | tri≈ ¬a b ¬c = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 130
diff changeset
470 -- ISO.iso→ iso x | case2 x1 | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c (elm<n x) )
128
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 126
diff changeset
471
126
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
472 fin-× : {A B : Set} → { a b : ℕ } → FiniteSet A {a} → FiniteSet B {b} → FiniteSet (A × B) {a * b}
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
473 fin-× {A} {B} {a} {b} fa fb with FiniteSet→Fin fa
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
474 ... | a=f = iso-fin (fin-×-f a ) iso-1 where
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
475 iso-1 : ISO (Fin a × B) ( A × B )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
476 ISO.A←B iso-1 x = ( FiniteSet.F←Q fa (proj₁ x) , proj₂ x)
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
477 ISO.B←A iso-1 x = ( FiniteSet.Q←F fa (proj₁ x) , proj₂ x)
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
478 ISO.iso← iso-1 x = lemma where
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
479 lemma : (FiniteSet.F←Q fa (FiniteSet.Q←F fa (proj₁ x)) , proj₂ x) ≡ ( proj₁ x , proj₂ x )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
480 lemma = cong ( λ k → ( k , proj₂ x ) ) (FiniteSet.finiso← fa _ )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
481 ISO.iso→ iso-1 x = cong ( λ k → ( k , proj₂ x ) ) (FiniteSet.finiso→ fa _ )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
482 iso-2 : {a : ℕ } → ISO (B ∨ (Fin a × B)) (Fin (suc a) × B)
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
483 ISO.A←B iso-2 (zero , b ) = case1 b
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
484 ISO.A←B iso-2 (suc fst , b ) = case2 ( fst , b )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
485 ISO.B←A iso-2 (case1 b) = ( zero , b )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
486 ISO.B←A iso-2 (case2 (a , b )) = ( suc a , b )
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
487 ISO.iso← iso-2 (case1 x) = refl
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
488 ISO.iso← iso-2 (case2 x) = refl
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
489 ISO.iso→ iso-2 (zero , b ) = refl
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
490 ISO.iso→ iso-2 (suc a , b ) = refl
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
491 fin-×-f : ( a : ℕ ) → FiniteSet ((Fin a) × B) {a * b}
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
492 fin-×-f zero = record { Q←F = λ () ; F←Q = λ () ; finiso→ = λ () ; finiso← = λ () }
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 125
diff changeset
493 fin-×-f (suc a) = iso-fin ( fin-∨ fb ( fin-×-f a ) ) iso-2