1 open import Relation.Binary.Core using (Decidable ; _≡_)
3 module Bidir (Carrier : Set) (deq : Decidable {A = Carrier} _≡_) where
5 open import Data.Nat using (ℕ)
6 open import Data.Fin using (Fin)
7 open import Data.Fin.Props using (_≟_)
8 open import Data.Maybe using (Maybe ; nothing ; just ; maybe′)
9 open import Data.List using (List)
10 open import Data.List.Any using (Any ; any ; here ; there)
11 open import Data.List.All using (All)
12 open Data.List.Any.Membership-≡ using (_∈_ ; _∉_)
13 open import Data.Vec using (Vec ; [] ; _∷_ ; toList ; fromList ; map ; tabulate) renaming (lookup to lookupVec)
14 open import Data.Vec.Properties using (tabulate-∘ ; lookup∘tabulate ; map-cong ; map-∘)
15 open import Data.Product using (∃ ; _×_ ; _,_ ; proj₁ ; proj₂)
16 open import Data.Empty using (⊥-elim)
17 open import Function using (id ; _∘_ ; flip)
18 open import Relation.Nullary using (yes ; no)
19 open import Relation.Nullary.Negation using (contradiction)
20 open import Relation.Binary.Core using (refl)
21 open import Relation.Binary.PropositionalEquality using (cong ; sym ; inspect ; [_] ; _≗_ ; trans)
22 open Relation.Binary.PropositionalEquality.≡-Reasoning using (begin_ ; _≡⟨_⟩_ ; _∎)
25 open FreeTheorems.VecVec using (get-type ; free-theorem)
28 open CheckInsert Carrier deq
29 open import BFF using (_>>=_ ; fmap)
30 open BFF.VecBFF Carrier deq using (assoc ; enumerate ; denumerate ; bff)
32 lemma-1 : {m n : ℕ} → (f : Fin n → Carrier) → (is : Vec (Fin n) m) → assoc is (map f is) ≡ just (restrict f (toList is))
34 lemma-1 f (i ∷ is′) = begin
35 assoc (i ∷ is′) (map f (i ∷ is′))
37 assoc is′ (map f is′) >>= checkInsert i (f i)
38 ≡⟨ cong (λ m → m >>= checkInsert i (f i)) (lemma-1 f is′) ⟩
39 just (restrict f (toList is′)) >>= (checkInsert i (f i))
41 checkInsert i (f i) (restrict f (toList is′))
42 ≡⟨ lemma-checkInsert-restrict f i (toList is′) ⟩
43 just (restrict f (toList (i ∷ is′))) ∎
45 lemma-lookupM-assoc : {m n : ℕ} → (i : Fin n) → (is : Vec (Fin n) m) → (x : Carrier) → (xs : Vec Carrier m) → (h : FinMapMaybe n Carrier) → assoc (i ∷ is) (x ∷ xs) ≡ just h → lookupM i h ≡ just x
46 lemma-lookupM-assoc i is x xs h p with assoc is xs
47 lemma-lookupM-assoc i is x xs h () | nothing
48 lemma-lookupM-assoc i is x xs h p | just h' = apply-checkInsertProof i x h' record
49 { same = λ lookupM≡justx → begin
51 ≡⟨ cong (lookupM i) (just-injective (trans (sym p) (lemma-checkInsert-same i x h' lookupM≡justx))) ⟩
55 ; new = λ lookupM≡nothing → begin
57 ≡⟨ cong (lookupM i) (just-injective (trans (sym p) (lemma-checkInsert-new i x h' lookupM≡nothing))) ⟩
58 lookupM i (insert i x h')
59 ≡⟨ lemma-lookupM-insert i x h' ⟩
61 ; wrong = λ x' x≢x' lookupM≡justx' → lemma-just≢nothing (trans (sym p) (lemma-checkInsert-wrong i x h' x' x≢x' lookupM≡justx'))
64 lemma-∉-lookupM-assoc : {m n : ℕ} → (i : Fin n) → (is : Vec (Fin n) m) → (xs : Vec Carrier m) → (h : FinMapMaybe n Carrier) → assoc is xs ≡ just h → (i ∉ toList is) → lookupM i h ≡ nothing
65 lemma-∉-lookupM-assoc i [] [] h ph i∉is = begin
67 ≡⟨ cong (lookupM i) (sym (just-injective ph)) ⟩
69 ≡⟨ lemma-lookupM-empty i ⟩
71 lemma-∉-lookupM-assoc i (i' ∷ is') (x' ∷ xs') h ph i∉is with assoc is' xs' | inspect (assoc is') xs'
72 lemma-∉-lookupM-assoc i (i' ∷ is') (x' ∷ xs') h () i∉is | nothing | [ ph' ]
73 lemma-∉-lookupM-assoc i (i' ∷ is') (x' ∷ xs') h ph i∉is | just h' | [ ph' ] = apply-checkInsertProof i' x' h' record {
74 same = λ lookupM-i'-h'≡just-x' → begin
76 ≡⟨ cong (lookupM i) (just-injective (trans (sym ph) (lemma-checkInsert-same i' x' h' lookupM-i'-h'≡just-x'))) ⟩
78 ≡⟨ lemma-∉-lookupM-assoc i is' xs' h' ph' (i∉is ∘ there) ⟩
80 ; new = λ lookupM-i'-h'≡nothing → begin
82 ≡⟨ cong (lookupM i) (just-injective (trans (sym ph) (lemma-checkInsert-new i' x' h' lookupM-i'-h'≡nothing))) ⟩
83 lookupM i (insert i' x' h')
84 ≡⟨ sym (lemma-lookupM-insert-other i i' x' h' (i∉is ∘ here)) ⟩
86 ≡⟨ lemma-∉-lookupM-assoc i is' xs' h' ph' (i∉is ∘ there) ⟩
88 ; wrong = λ x'' x'≢x'' lookupM-i'-h'≡just-x'' → lemma-just≢nothing (trans (sym ph) (lemma-checkInsert-wrong i' x' h' x'' x'≢x'' lookupM-i'-h'≡just-x''))
91 _in-domain-of_ : {n : ℕ} {A : Set} → (is : List (Fin n)) → (FinMapMaybe n A) → Set
92 _in-domain-of_ is h = All (λ i → ∃ λ x → lookupM i h ≡ just x) is
94 lemma-assoc-domain : {m n : ℕ} → (is : Vec (Fin n) m) → (xs : Vec Carrier m) → (h : FinMapMaybe n Carrier) → assoc is xs ≡ just h → (toList is) in-domain-of h
95 lemma-assoc-domain [] [] h ph = Data.List.All.[]
96 lemma-assoc-domain (i' ∷ is') (x' ∷ xs') h ph with assoc is' xs' | inspect (assoc is') xs'
97 lemma-assoc-domain (i' ∷ is') (x' ∷ xs') h () | nothing | [ ph' ]
98 lemma-assoc-domain (i' ∷ is') (x' ∷ xs') h ph | just h' | [ ph' ] = apply-checkInsertProof i' x' h' record {
99 same = λ lookupM-i'-h'≡just-x' → Data.List.All._∷_
100 (x' , (trans (cong (lookupM i') (just-injective (trans (sym ph) (lemma-checkInsert-same i' x' h' lookupM-i'-h'≡just-x')))) lookupM-i'-h'≡just-x'))
101 (lemma-assoc-domain is' xs' h (trans ph' (trans (sym (lemma-checkInsert-same i' x' h' lookupM-i'-h'≡just-x')) ph)))
102 ; new = λ lookupM-i'-h'≡nothing → Data.List.All._∷_
103 (x' , (trans (cong (lookupM i') (just-injective (trans (sym ph) (lemma-checkInsert-new i' x' h' lookupM-i'-h'≡nothing)))) (lemma-lookupM-insert i' x' h')))
105 (λ {i} p → proj₁ p , lemma-lookupM-checkInsert i i' (proj₁ p) x' h' h (proj₂ p) ph)
106 (lemma-assoc-domain is' xs' h' ph'))
107 ; wrong = λ x'' x'≢x'' lookupM-i'-h'≡just-x'' → lemma-just≢nothing (trans (sym ph) (lemma-checkInsert-wrong i' x' h' x'' x'≢x'' lookupM-i'-h'≡just-x''))
110 lemma-map-lookupM-insert : {m n : ℕ} → (i : Fin n) → (is : Vec (Fin n) m) → (x : Carrier) → (h : FinMapMaybe n Carrier) → i ∉ (toList is) → (toList is) in-domain-of h → map (flip lookupM (insert i x h)) is ≡ map (flip lookupM h) is
111 lemma-map-lookupM-insert i [] x h i∉is ph = refl
112 lemma-map-lookupM-insert i (i' ∷ is') x h i∉is ph = begin
113 lookupM i' (insert i x h) ∷ map (flip lookupM (insert i x h)) is'
114 ≡⟨ cong (flip _∷_ (map (flip lookupM (insert i x h)) is')) (sym (lemma-lookupM-insert-other i' i x h (i∉is ∘ here ∘ sym))) ⟩
115 lookupM i' h ∷ map (flip lookupM (insert i x h)) is'
116 ≡⟨ cong (_∷_ (lookupM i' h)) (lemma-map-lookupM-insert i is' x h (i∉is ∘ there) (Data.List.All.tail ph)) ⟩
117 lookupM i' h ∷ map (flip lookupM h) is' ∎
119 lemma-map-lookupM-assoc : {m n : ℕ} → (i : Fin n) → (is : Vec (Fin n) m) → (x : Carrier) → (xs : Vec Carrier m) → (h : FinMapMaybe n Carrier) → (h' : FinMapMaybe n Carrier) → assoc is xs ≡ just h' → checkInsert i x h' ≡ just h → map (flip lookupM h) is ≡ map (flip lookupM h') is
120 lemma-map-lookupM-assoc i [] x [] h h' ph' ph = refl
121 lemma-map-lookupM-assoc i (i' ∷ is') x (x' ∷ xs') h h' ph' ph with any (_≟_ i) (toList (i' ∷ is'))
122 lemma-map-lookupM-assoc i (i' ∷ is') x (x' ∷ xs') h h' ph' ph | yes p with Data.List.All.lookup (lemma-assoc-domain (i' ∷ is') (x' ∷ xs') h' ph') p
123 lemma-map-lookupM-assoc i (i' ∷ is') x (x' ∷ xs') h h' ph' ph | yes p | (x'' , p') with lookupM i h'
124 lemma-map-lookupM-assoc i (i' ∷ is') x (x' ∷ xs') h h' ph' ph | yes p | (x'' , refl) | .(just x'') with deq x x''
125 lemma-map-lookupM-assoc i (i' ∷ is') x (x' ∷ xs') h .h ph' refl | yes p | (.x , refl) | .(just x) | yes refl = refl
126 lemma-map-lookupM-assoc i (i' ∷ is') x (x' ∷ xs') h h' ph' () | yes p | (x'' , refl) | .(just x'') | no ¬p
127 lemma-map-lookupM-assoc i (i' ∷ is') x (x' ∷ xs') h h' ph' ph | no ¬p rewrite lemma-∉-lookupM-assoc i (i' ∷ is') (x' ∷ xs') h' ph' ¬p = begin
128 map (flip lookupM h) (i' ∷ is')
129 ≡⟨ map-cong (λ i'' → cong (lookupM i'') (just-injective (sym ph))) (i' ∷ is') ⟩
130 map (flip lookupM (insert i x h')) (i' ∷ is')
131 ≡⟨ lemma-map-lookupM-insert i (i' ∷ is') x h' ¬p (lemma-assoc-domain (i' ∷ is') (x' ∷ xs') h' ph') ⟩
132 map (flip lookupM h') (i' ∷ is') ∎
134 lemma-2 : {m n : ℕ} → (is : Vec (Fin n) m) → (v : Vec Carrier m) → (h : FinMapMaybe n Carrier) → assoc is v ≡ just h → map (flip lookupM h) is ≡ map just v
135 lemma-2 [] [] h p = refl
136 lemma-2 (i ∷ is) (x ∷ xs) h p with assoc is xs | inspect (assoc is) xs
137 lemma-2 (i ∷ is) (x ∷ xs) h () | nothing | _
138 lemma-2 (i ∷ is) (x ∷ xs) h p | just h' | [ ir ] = begin
139 map (flip lookupM h) (i ∷ is)
141 lookupM i h ∷ map (flip lookupM h) is
142 ≡⟨ cong (flip _∷_ (map (flip lookupM h) is)) (lemma-lookupM-assoc i is x xs h (begin
143 assoc (i ∷ is) (x ∷ xs)
144 ≡⟨ cong (flip _>>=_ (checkInsert i x)) ir ⟩
148 just x ∷ map (flip lookupM h) is
149 ≡⟨ cong (_∷_ (just x)) (lemma-map-lookupM-assoc i is x xs h h' ir p) ⟩
150 just x ∷ map (flip lookupM h') is
151 ≡⟨ cong (_∷_ (just x)) (lemma-2 is xs h' ir) ⟩
156 lemma-map-denumerate-enumerate : {m : ℕ} → (as : Vec Carrier m) → map (denumerate as) (enumerate as) ≡ as
157 lemma-map-denumerate-enumerate [] = refl
158 lemma-map-denumerate-enumerate (a ∷ as) = cong (_∷_ a) (begin
159 map (flip lookupVec (a ∷ as)) (tabulate Fin.suc)
160 ≡⟨ cong (map (flip lookupVec (a ∷ as))) (tabulate-∘ Fin.suc id) ⟩
161 map (flip lookupVec (a ∷ as)) (map Fin.suc (tabulate id))
163 map (flip lookupVec (a ∷ as)) (map Fin.suc (enumerate as))
164 ≡⟨ sym (map-∘ _ _ (enumerate as)) ⟩
165 map (flip lookupVec (a ∷ as) ∘ Fin.suc) (enumerate as)
167 map (denumerate as) (enumerate as)
168 ≡⟨ lemma-map-denumerate-enumerate as ⟩
171 theorem-1 : {getlen : ℕ → ℕ} → (get : get-type getlen) → {m : ℕ} → (s : Vec Carrier m) → bff get s (get s) ≡ just s
172 theorem-1 get s = begin
174 ≡⟨ cong (bff get s ∘ get) (sym (lemma-map-denumerate-enumerate s)) ⟩
175 bff get s (get (map (denumerate s) (enumerate s)))
176 ≡⟨ cong (bff get s) (free-theorem get (denumerate s) (enumerate s)) ⟩
177 bff get s (map (denumerate s) (get (enumerate s)))
179 fmap (flip map (enumerate s) ∘ flip lookup) (fmap (flip union (fromFunc (denumerate s))) (assoc (get (enumerate s)) (map (denumerate s) (get (enumerate s)))))
180 ≡⟨ cong (fmap (flip map (enumerate s) ∘ flip lookup) ∘ fmap (flip union (fromFunc (denumerate s)))) (lemma-1 (denumerate s) (get (enumerate s))) ⟩
181 fmap (flip map (enumerate s) ∘ flip lookup) (fmap (flip union (fromFunc (flip lookupVec s))) (just (restrict (denumerate s) (toList (get (enumerate s))))))
183 just ((flip map (enumerate s) ∘ flip lookup) (union (restrict (denumerate s) (toList (get (enumerate s)))) (fromFunc (denumerate s))))
184 ≡⟨ cong just (cong (flip map (enumerate s) ∘ flip lookup) (lemma-union-restrict (denumerate s) (toList (get (enumerate s))))) ⟩
185 just ((flip map (enumerate s) ∘ flip lookup) (fromFunc (denumerate s)))
187 just (map (flip lookup (fromFunc (denumerate s))) (enumerate s))
188 ≡⟨ cong just (map-cong (lookup∘tabulate (denumerate s)) (enumerate s)) ⟩
189 just (map (denumerate s) (enumerate s))
190 ≡⟨ cong just (lemma-map-denumerate-enumerate s) ⟩
193 lemma-fmap-just : {A B : Set} → {f : A → B} {b : B} → (ma : Maybe A) → fmap f ma ≡ just b → ∃ λ a → ma ≡ just a
194 lemma-fmap-just (just x) fmap-f-ma≡just-b = x , refl
195 lemma-fmap-just nothing ()
197 ∷-injective : {A : Set} {n : ℕ} {x y : A} {xs ys : Vec A n} → (x ∷ xs) ≡ (y ∷ ys) → x ≡ y × xs ≡ ys
198 ∷-injective refl = refl , refl
200 lemma-from-map-just : {A : Set} {n : ℕ} → {xs ys : Vec A n} → map Maybe.just xs ≡ map Maybe.just ys → xs ≡ ys
201 lemma-from-map-just {xs = []} {ys = []} p = refl
202 lemma-from-map-just {xs = x ∷ xs'} {ys = y ∷ ys'} p with ∷-injective p
203 lemma-from-map-just {xs = x ∷ xs'} {ys = .x ∷ ys'} p | refl , p' = cong (_∷_ x) (lemma-from-map-just p')
205 lemma-union-not-used : {m n : ℕ} {A : Set} (h : FinMapMaybe n A) → (h' : FinMap n A) → (is : Vec (Fin n) m) → (toList is) in-domain-of h → map just (map (flip lookup (union h h')) is) ≡ map (flip lookupM h) is
206 lemma-union-not-used h h' [] p = refl
207 lemma-union-not-used h h' (i ∷ is') p with Data.List.All.head p
208 lemma-union-not-used h h' (i ∷ is') p | x , lookupM-i-h≡just-x = begin
209 just (lookup i (union h h')) ∷ map just (map (flip lookup (union h h')) is')
210 ≡⟨ cong (flip _∷_ (map just (map (flip lookup (union h h')) is'))) (begin
211 just (lookup i (union h h'))
212 ≡⟨ cong just (lookup∘tabulate (λ j → maybe′ id (lookup j h') (lookupM j h)) i) ⟩
213 just (maybe′ id (lookup i h') (lookupM i h))
214 ≡⟨ cong just (cong (maybe′ id (lookup i h')) lookupM-i-h≡just-x) ⟩
215 just (maybe′ id (lookup i h') (just x))
218 ≡⟨ sym lookupM-i-h≡just-x ⟩
220 lookupM i h ∷ map just (map (flip lookup (union h h')) is')
221 ≡⟨ cong (_∷_ (lookupM i h)) (lemma-union-not-used h h' is' (Data.List.All.tail p)) ⟩
222 lookupM i h ∷ map (flip lookupM h) is' ∎
224 theorem-2 : {getlen : ℕ → ℕ} (get : get-type getlen) → {m : ℕ} → (v : Vec Carrier (getlen m)) → (s u : Vec Carrier m) → bff get s v ≡ just u → get u ≡ v
225 theorem-2 get v s u p with lemma-fmap-just (assoc (get (enumerate s)) v) (proj₂ (lemma-fmap-just (fmap (flip union (fromFunc (denumerate s))) (assoc (get (enumerate s)) v)) p))
226 theorem-2 get v s u p | h , ph = begin
228 ≡⟨ just-injective (begin
232 ≡⟨ cong (fmap get) (sym p) ⟩
233 fmap get (bff get s v)
234 ≡⟨ cong (fmap get ∘ fmap (flip map (enumerate s) ∘ flip lookup) ∘ fmap (flip union (fromFunc (denumerate s)))) ph ⟩
235 fmap get (fmap (flip map (enumerate s) ∘ flip lookup) (fmap (flip union (fromFunc (denumerate s))) (just h)))
237 just (get (map (flip lookup (union h (fromFunc (denumerate s)))) (enumerate s)))
239 get (map (flip lookup (union h (fromFunc (denumerate s)))) (enumerate s))
240 ≡⟨ free-theorem get (flip lookup (union h (fromFunc (denumerate s)))) (enumerate s) ⟩
241 map (flip lookup (union h (fromFunc (denumerate s)))) (get (enumerate s))
242 ≡⟨ lemma-from-map-just (begin
243 map just (map (flip lookup (union h (fromFunc (denumerate s)))) (get (enumerate s)))
244 ≡⟨ lemma-union-not-used h (fromFunc (denumerate s)) (get (enumerate s)) (lemma-assoc-domain (get (enumerate s)) v h ph) ⟩
245 map (flip lookupM h) (get (enumerate s))
246 ≡⟨ lemma-2 (get (enumerate s)) v h ph ⟩