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 is′ (map f is′) >>= checkInsert i (f i)
36 ≡⟨ cong (λ m → m >>= checkInsert i (f i)) (lemma-1 f is′) ⟩
37 checkInsert i (f i) (restrict f (toList is′))
38 ≡⟨ lemma-checkInsert-restrict f i (toList is′) ⟩
39 just (restrict f (toList (i ∷ is′))) ∎
41 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
42 lemma-lookupM-assoc i is x xs h p with assoc is xs
43 lemma-lookupM-assoc i is x xs h () | nothing
44 lemma-lookupM-assoc i is x xs h p | just h' = apply-checkInsertProof i x h' record
45 { same = λ lookupM≡justx → begin
47 ≡⟨ cong (lookupM i) (just-injective (trans (sym p) (lemma-checkInsert-same i x h' lookupM≡justx))) ⟩
51 ; new = λ lookupM≡nothing → begin
53 ≡⟨ cong (lookupM i) (just-injective (trans (sym p) (lemma-checkInsert-new i x h' lookupM≡nothing))) ⟩
54 lookupM i (insert i x h')
55 ≡⟨ lemma-lookupM-insert i x h' ⟩
57 ; wrong = λ x' x≢x' lookupM≡justx' → lemma-just≢nothing (trans (sym p) (lemma-checkInsert-wrong i x h' x' x≢x' lookupM≡justx'))
60 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
61 lemma-∉-lookupM-assoc i [] [] h ph i∉is = begin
63 ≡⟨ cong (lookupM i) (sym (just-injective ph)) ⟩
65 ≡⟨ lemma-lookupM-empty i ⟩
67 lemma-∉-lookupM-assoc i (i' ∷ is') (x' ∷ xs') h ph i∉is with assoc is' xs' | inspect (assoc is') xs'
68 lemma-∉-lookupM-assoc i (i' ∷ is') (x' ∷ xs') h () i∉is | nothing | [ ph' ]
69 lemma-∉-lookupM-assoc i (i' ∷ is') (x' ∷ xs') h ph i∉is | just h' | [ ph' ] = apply-checkInsertProof i' x' h' record {
70 same = λ lookupM-i'-h'≡just-x' → begin
72 ≡⟨ cong (lookupM i) (just-injective (trans (sym ph) (lemma-checkInsert-same i' x' h' lookupM-i'-h'≡just-x'))) ⟩
74 ≡⟨ lemma-∉-lookupM-assoc i is' xs' h' ph' (i∉is ∘ there) ⟩
76 ; new = λ lookupM-i'-h'≡nothing → begin
78 ≡⟨ cong (lookupM i) (just-injective (trans (sym ph) (lemma-checkInsert-new i' x' h' lookupM-i'-h'≡nothing))) ⟩
79 lookupM i (insert i' x' h')
80 ≡⟨ sym (lemma-lookupM-insert-other i i' x' h' (i∉is ∘ here)) ⟩
82 ≡⟨ lemma-∉-lookupM-assoc i is' xs' h' ph' (i∉is ∘ there) ⟩
84 ; 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''))
87 _in-domain-of_ : {n : ℕ} {A : Set} → (is : List (Fin n)) → (FinMapMaybe n A) → Set
88 _in-domain-of_ is h = All (λ i → ∃ λ x → lookupM i h ≡ just x) is
90 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
91 lemma-assoc-domain [] [] h ph = Data.List.All.[]
92 lemma-assoc-domain (i' ∷ is') (x' ∷ xs') h ph with assoc is' xs' | inspect (assoc is') xs'
93 lemma-assoc-domain (i' ∷ is') (x' ∷ xs') h () | nothing | [ ph' ]
94 lemma-assoc-domain (i' ∷ is') (x' ∷ xs') h ph | just h' | [ ph' ] = apply-checkInsertProof i' x' h' record {
95 same = λ lookupM-i'-h'≡just-x' → Data.List.All._∷_
96 (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'))
97 (lemma-assoc-domain is' xs' h (trans ph' (trans (sym (lemma-checkInsert-same i' x' h' lookupM-i'-h'≡just-x')) ph)))
98 ; new = λ lookupM-i'-h'≡nothing → Data.List.All._∷_
99 (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')))
101 (λ {i} p → proj₁ p , lemma-lookupM-checkInsert i i' (proj₁ p) x' h' h (proj₂ p) ph)
102 (lemma-assoc-domain is' xs' h' ph'))
103 ; 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''))
106 lemma-map-lookupM-insert : {m n : ℕ} → (i : Fin n) → (is : Vec (Fin n) m) → (x : Carrier) → (h : FinMapMaybe n Carrier) → i ∉ (toList is) → map (flip lookupM (insert i x h)) is ≡ map (flip lookupM h) is
107 lemma-map-lookupM-insert i [] x h i∉is = refl
108 lemma-map-lookupM-insert i (i' ∷ is') x h i∉is = begin
109 lookupM i' (insert i x h) ∷ map (flip lookupM (insert i x h)) is'
110 ≡⟨ cong (flip _∷_ (map (flip lookupM (insert i x h)) is')) (sym (lemma-lookupM-insert-other i' i x h (i∉is ∘ here ∘ sym))) ⟩
111 lookupM i' h ∷ map (flip lookupM (insert i x h)) is'
112 ≡⟨ cong (_∷_ (lookupM i' h)) (lemma-map-lookupM-insert i is' x h (i∉is ∘ there)) ⟩
113 lookupM i' h ∷ map (flip lookupM h) is' ∎
115 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
116 lemma-map-lookupM-assoc i is x xs h h' ph' ph with any (_≟_ i) (toList is)
117 lemma-map-lookupM-assoc i is x xs h h' ph' ph | yes p with Data.List.All.lookup (lemma-assoc-domain is xs h' ph') p
118 lemma-map-lookupM-assoc i is x xs h h' ph' ph | yes p | (x'' , p') with lookupM i h'
119 lemma-map-lookupM-assoc i is x xs h h' ph' ph | yes p | (x'' , refl) | .(just x'') with deq x x''
120 lemma-map-lookupM-assoc i is x xs h .h ph' refl | yes p | (.x , refl) | .(just x) | yes refl = refl
121 lemma-map-lookupM-assoc i is x xs h h' ph' () | yes p | (x'' , refl) | .(just x'') | no p
122 lemma-map-lookupM-assoc i is x xs h h' ph' ph | no ¬p rewrite lemma-∉-lookupM-assoc i is xs h' ph' ¬p = begin
123 map (flip lookupM h) is
124 ≡⟨ map-cong (λ i'' → cong (lookupM i'') (just-injective (sym ph))) is ⟩
125 map (flip lookupM (insert i x h')) is
126 ≡⟨ lemma-map-lookupM-insert i is x h' ¬p ⟩
127 map (flip lookupM h') is ∎
129 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
130 lemma-2 [] [] h p = refl
131 lemma-2 (i ∷ is) (x ∷ xs) h p with assoc is xs | inspect (assoc is) xs
132 lemma-2 (i ∷ is) (x ∷ xs) h () | nothing | _
133 lemma-2 (i ∷ is) (x ∷ xs) h p | just h' | [ ir ] = begin
134 lookupM i h ∷ map (flip lookupM h) is
135 ≡⟨ cong (flip _∷_ (map (flip lookupM h) is)) (lemma-lookupM-assoc i is x xs h (begin
136 assoc (i ∷ is) (x ∷ xs)
137 ≡⟨ cong (flip _>>=_ (checkInsert i x)) ir ⟩
141 just x ∷ map (flip lookupM h) is
142 ≡⟨ cong (_∷_ (just x)) (lemma-map-lookupM-assoc i is x xs h h' ir p) ⟩
143 just x ∷ map (flip lookupM h') is
144 ≡⟨ cong (_∷_ (just x)) (lemma-2 is xs h' ir) ⟩
145 just x ∷ map just xs ∎
147 lemma-map-denumerate-enumerate : {m : ℕ} → (as : Vec Carrier m) → map (denumerate as) (enumerate as) ≡ as
148 lemma-map-denumerate-enumerate [] = refl
149 lemma-map-denumerate-enumerate (a ∷ as) = cong (_∷_ a) (begin
150 map (flip lookupVec (a ∷ as)) (tabulate Fin.suc)
151 ≡⟨ cong (map (flip lookupVec (a ∷ as))) (tabulate-∘ Fin.suc id) ⟩
152 map (flip lookupVec (a ∷ as)) (map Fin.suc (tabulate id))
154 map (flip lookupVec (a ∷ as)) (map Fin.suc (enumerate as))
155 ≡⟨ sym (map-∘ _ _ (enumerate as)) ⟩
156 map (flip lookupVec (a ∷ as) ∘ Fin.suc) (enumerate as)
158 map (denumerate as) (enumerate as)
159 ≡⟨ lemma-map-denumerate-enumerate as ⟩
162 theorem-1 : {getlen : ℕ → ℕ} → (get : get-type getlen) → {m : ℕ} → (s : Vec Carrier m) → bff get s (get s) ≡ just s
163 theorem-1 get s = begin
165 ≡⟨ cong (bff get s ∘ get) (sym (lemma-map-denumerate-enumerate s)) ⟩
166 bff get s (get (map (denumerate s) (enumerate s)))
167 ≡⟨ cong (bff get s) (free-theorem get (denumerate s) (enumerate s)) ⟩
168 bff get s (map (denumerate s) (get (enumerate s)))
170 (h′↦r ∘ h↦h′) (assoc (get (enumerate s)) (map (denumerate s) (get (enumerate s))))
171 ≡⟨ cong (h′↦r ∘ h↦h′) (lemma-1 (denumerate s) (get (enumerate s))) ⟩
172 (h′↦r ∘ h↦h′ ∘ just) (restrict (denumerate s) (toList (get (enumerate s))))
174 (h′↦r ∘ just) (union (restrict (denumerate s) (toList (get (enumerate s)))) (fromFunc (denumerate s)))
175 ≡⟨ cong (h′↦r ∘ just) (lemma-union-restrict (denumerate s) (toList (get (enumerate s)))) ⟩
176 (h′↦r ∘ just) (fromFunc (denumerate s))
178 just (map (flip lookup (fromFunc (denumerate s))) (enumerate s))
179 ≡⟨ cong just (map-cong (lookup∘tabulate (denumerate s)) (enumerate s)) ⟩
180 just (map (denumerate s) (enumerate s))
181 ≡⟨ cong just (lemma-map-denumerate-enumerate s) ⟩
183 where h↦h′ = fmap (flip union (fromFunc (denumerate s)))
184 h′↦r = fmap (flip map (enumerate s) ∘ flip lookupVec)
186 lemma-fmap-just : {A B : Set} {f : A → B} {b : B} {ma : Maybe A} → fmap f ma ≡ just b → ∃ λ a → ma ≡ just a
187 lemma-fmap-just {ma = just x} fmap-f-ma≡just-b = x , refl
188 lemma-fmap-just {ma = nothing} ()
190 ∷-injective : {A : Set} {n : ℕ} {x y : A} {xs ys : Vec A n} → (x ∷ xs) ≡ (y ∷ ys) → x ≡ y × xs ≡ ys
191 ∷-injective refl = refl , refl
193 map-just-injective : {A : Set} {n : ℕ} {xs ys : Vec A n} → map Maybe.just xs ≡ map Maybe.just ys → xs ≡ ys
194 map-just-injective {xs = []} {ys = []} p = refl
195 map-just-injective {xs = x ∷ xs'} {ys = y ∷ ys'} p with ∷-injective p
196 map-just-injective {xs = x ∷ xs'} {ys = .x ∷ ys'} p | refl , p' = cong (_∷_ x) (map-just-injective p')
198 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
199 lemma-union-not-used h h' [] p = refl
200 lemma-union-not-used h h' (i ∷ is') p with Data.List.All.head p
201 lemma-union-not-used h h' (i ∷ is') p | x , lookupM-i-h≡just-x = begin
202 just (lookup i (union h h')) ∷ map just (map (flip lookup (union h h')) is')
203 ≡⟨ cong (flip _∷_ (map just (map (flip lookup (union h h')) is'))) (begin
204 just (lookup i (union h h'))
205 ≡⟨ cong just (lookup∘tabulate (λ j → maybe′ id (lookup j h') (lookupM j h)) i) ⟩
206 just (maybe′ id (lookup i h') (lookupM i h))
207 ≡⟨ cong just (cong (maybe′ id (lookup i h')) lookupM-i-h≡just-x) ⟩
208 just (maybe′ id (lookup i h') (just x))
211 ≡⟨ sym lookupM-i-h≡just-x ⟩
213 lookupM i h ∷ map just (map (flip lookup (union h h')) is')
214 ≡⟨ cong (_∷_ (lookupM i h)) (lemma-union-not-used h h' is' (Data.List.All.tail p)) ⟩
215 lookupM i h ∷ map (flip lookupM h) is' ∎
217 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
218 theorem-2 get v s u p with lemma-fmap-just (proj₂ (lemma-fmap-just p))
219 theorem-2 get v s u p | h , ph = begin
221 ≡⟨ just-injective (begin
223 ≡⟨ cong (fmap get) (sym p) ⟩
224 fmap get (bff get s v)
225 ≡⟨ cong (fmap get ∘ fmap h′↦r ∘ fmap h↦h′) ph ⟩
226 fmap get (fmap h′↦r (fmap h↦h′ (just h))) ∎) ⟩
227 get (map (flip lookup (h↦h′ h)) s′)
228 ≡⟨ free-theorem get (flip lookup (h↦h′ h)) s′ ⟩
229 map (flip lookup (h↦h′ h)) (get s′)
230 ≡⟨ map-just-injective (begin
231 map just (map (flip lookup (union h g)) (get s′))
232 ≡⟨ lemma-union-not-used h g (get s′) (lemma-assoc-domain (get s′) v h ph) ⟩
233 map (flip lookupM h) (get s′)
234 ≡⟨ lemma-2 (get s′) v h ph ⟩
238 where s′ = enumerate s
239 g = fromFunc (denumerate s)
241 h′↦r = flip map s′ ∘ flip lookupVec