2023-07-25 18:22:24 -07:00
|
|
|
|
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; sym; trans; cong)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
open import Relation.Binary.Definitions using (Decidable)
|
|
|
|
|
open import Relation.Binary.Core using (Rel)
|
2023-07-26 20:40:28 -07:00
|
|
|
|
open import Relation.Nullary using (Dec; yes; no; Reflects; ofʸ; ofⁿ)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
open import Agda.Primitive using (Level; _⊔_)
|
|
|
|
|
|
|
|
|
|
module Map {a b : Level} (A : Set a) (B : Set b)
|
|
|
|
|
(≡-dec-A : Decidable (_≡_ {a} {A}))
|
|
|
|
|
where
|
|
|
|
|
|
2023-07-24 23:12:04 -07:00
|
|
|
|
import Data.List.Membership.Propositional as MemProp
|
|
|
|
|
|
2023-07-23 17:50:25 -07:00
|
|
|
|
open import Relation.Nullary using (¬_)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
open import Data.Nat using (ℕ)
|
2023-07-25 22:58:42 -07:00
|
|
|
|
open import Data.List using (List; map; []; _∷_; _++_)
|
2023-07-24 22:51:27 -07:00
|
|
|
|
open import Data.List.Relation.Unary.All using (All; []; _∷_)
|
2023-07-23 17:50:25 -07:00
|
|
|
|
open import Data.List.Relation.Unary.Any using (Any; here; there) -- TODO: re-export these with nicer names from map
|
|
|
|
|
open import Data.Product using (_×_; _,_; Σ; proj₁ ; proj₂)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
open import Data.Empty using (⊥)
|
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
keys : List (A × B) → List A
|
2023-07-25 22:58:42 -07:00
|
|
|
|
keys = map proj₁
|
2023-07-24 20:38:34 -07:00
|
|
|
|
|
|
|
|
|
data Unique {c} {C : Set c} : List C → Set c where
|
2023-07-23 21:34:24 -07:00
|
|
|
|
empty : Unique []
|
2023-07-30 13:46:52 -07:00
|
|
|
|
push : ∀ {x : C} {xs : List C}
|
2023-07-24 20:38:34 -07:00
|
|
|
|
→ All (λ x' → ¬ x ≡ x') xs
|
2023-07-23 21:34:24 -07:00
|
|
|
|
→ Unique xs
|
2023-07-24 20:38:34 -07:00
|
|
|
|
→ Unique (x ∷ xs)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
|
2023-07-26 17:31:09 -07:00
|
|
|
|
Unique-append : ∀ {c} {C : Set c} {x : C} {xs : List C} →
|
|
|
|
|
¬ MemProp._∈_ x xs → Unique xs → Unique (xs ++ (x ∷ []))
|
2023-07-24 22:51:27 -07:00
|
|
|
|
Unique-append {c} {C} {x} {[]} _ _ = push [] empty
|
2023-07-26 17:31:09 -07:00
|
|
|
|
Unique-append {c} {C} {x} {x' ∷ xs'} x∉xs (push x'≢ uxs') =
|
|
|
|
|
push (help x'≢) (Unique-append (λ x∈xs' → x∉xs (there x∈xs')) uxs')
|
2023-07-24 22:51:27 -07:00
|
|
|
|
where
|
|
|
|
|
x'≢x : ¬ x' ≡ x
|
|
|
|
|
x'≢x x'≡x = x∉xs (here (sym x'≡x))
|
|
|
|
|
|
|
|
|
|
help : {l : List C} → All (λ x'' → ¬ x' ≡ x'') l → All (λ x'' → ¬ x' ≡ x'') (l ++ (x ∷ []))
|
|
|
|
|
help {[]} _ = x'≢x ∷ []
|
|
|
|
|
help {e ∷ es} (x'≢e ∷ x'≢es) = x'≢e ∷ help x'≢es
|
|
|
|
|
|
2023-07-26 20:40:28 -07:00
|
|
|
|
All¬-¬Any : ∀ {p c} {C : Set c} {P : C → Set p} {l : List C} → All (λ x → ¬ P x) l → ¬ Any P l
|
|
|
|
|
All¬-¬Any {l = x ∷ xs} (¬Px ∷ _) (here Px) = ¬Px Px
|
|
|
|
|
All¬-¬Any {l = x ∷ xs} (_ ∷ ¬Pxs) (there Pxs) = All¬-¬Any ¬Pxs Pxs
|
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
absurd : ∀ {a} {A : Set a} → ⊥ → A
|
|
|
|
|
absurd ()
|
|
|
|
|
|
2023-07-25 19:56:47 -07:00
|
|
|
|
private module _ where
|
|
|
|
|
open MemProp using (_∈_)
|
|
|
|
|
|
2023-07-26 17:31:09 -07:00
|
|
|
|
unique-not-in : ∀ {k : A} {v : B} {l : List (A × B)} →
|
|
|
|
|
¬ (All (λ k' → ¬ k ≡ k') (keys l) × (k , v) ∈ l)
|
|
|
|
|
unique-not-in {l = (k' , _) ∷ xs} (k≢k' ∷ _ , here k',≡x) =
|
|
|
|
|
k≢k' (cong proj₁ k',≡x)
|
|
|
|
|
unique-not-in {l = _ ∷ xs} (_ ∷ rest , there k,v'∈xs) =
|
|
|
|
|
unique-not-in (rest , k,v'∈xs)
|
|
|
|
|
|
|
|
|
|
ListAB-functional : ∀ {k : A} {v v' : B} {l : List (A × B)} →
|
|
|
|
|
Unique (keys l) → (k , v) ∈ l → (k , v') ∈ l → v ≡ v'
|
|
|
|
|
ListAB-functional _ (here k,v≡x) (here k,v'≡x) =
|
|
|
|
|
cong proj₂ (trans k,v≡x (sym k,v'≡x))
|
|
|
|
|
ListAB-functional (push k≢xs _) (here k,v≡x) (there k,v'∈xs)
|
|
|
|
|
rewrite sym k,v≡x = absurd (unique-not-in (k≢xs , k,v'∈xs))
|
|
|
|
|
ListAB-functional (push k≢xs _) (there k,v∈xs) (here k,v'≡x)
|
|
|
|
|
rewrite sym k,v'≡x = absurd (unique-not-in (k≢xs , k,v∈xs))
|
|
|
|
|
ListAB-functional {l = _ ∷ xs } (push _ uxs) (there k,v∈xs) (there k,v'∈xs) =
|
|
|
|
|
ListAB-functional uxs k,v∈xs k,v'∈xs
|
2023-07-25 19:56:47 -07:00
|
|
|
|
|
2023-07-28 00:05:41 -07:00
|
|
|
|
∈k-dec : ∀ (k : A) (l : List (A × B)) → Dec (k ∈ keys l)
|
|
|
|
|
∈k-dec k [] = no (λ ())
|
|
|
|
|
∈k-dec k ((k' , v) ∷ xs)
|
|
|
|
|
with (≡-dec-A k k')
|
|
|
|
|
... | yes k≡k' = yes (here k≡k')
|
|
|
|
|
... | no k≢k' with (∈k-dec k xs)
|
|
|
|
|
... | yes k∈kxs = yes (there k∈kxs)
|
|
|
|
|
... | no k∉kxs = no witness
|
|
|
|
|
where
|
|
|
|
|
witness : ¬ k ∈ keys ((k' , v) ∷ xs)
|
|
|
|
|
witness (here k≡k') = k≢k' k≡k'
|
|
|
|
|
witness (there k∈kxs) = k∉kxs k∈kxs
|
|
|
|
|
|
|
|
|
|
∈-cong : ∀ {c d} {C : Set c} {D : Set d} {c : C} {l : List C} →
|
|
|
|
|
(f : C → D) → c ∈ l → f c ∈ map f l
|
|
|
|
|
∈-cong f (here c≡c') = here (cong f c≡c')
|
|
|
|
|
∈-cong f (there c∈xs) = there (∈-cong f c∈xs)
|
|
|
|
|
|
|
|
|
|
locate : ∀ {k : A} {l : List (A × B)} → k ∈ keys l → Σ B (λ v → (k , v) ∈ l)
|
|
|
|
|
locate {k} {(k' , v) ∷ xs} (here k≡k') rewrite k≡k' = (v , here refl)
|
|
|
|
|
locate {k} {(k' , v) ∷ xs} (there k∈kxs) = let (v , k,v∈xs) = locate k∈kxs in (v , there k,v∈xs)
|
|
|
|
|
|
2023-07-24 23:55:09 -07:00
|
|
|
|
private module ImplRelation (_≈_ : B → B → Set b) where
|
2023-07-25 19:56:47 -07:00
|
|
|
|
open MemProp using (_∈_)
|
|
|
|
|
|
2023-07-24 23:55:09 -07:00
|
|
|
|
subset : List (A × B) → List (A × B) → Set (a ⊔ b)
|
2023-07-26 17:31:09 -07:00
|
|
|
|
subset m₁ m₂ = ∀ (k : A) (v : B) → (k , v) ∈ m₁ →
|
|
|
|
|
Σ B (λ v' → v ≈ v' × ((k , v') ∈ m₂))
|
2023-07-24 23:55:09 -07:00
|
|
|
|
|
|
|
|
|
private module ImplInsert (f : B → B → B) where
|
2023-07-25 22:58:42 -07:00
|
|
|
|
open import Data.List using (map)
|
2023-07-25 19:56:47 -07:00
|
|
|
|
open MemProp using (_∈_)
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
_∈k_ : A → List (A × B) → Set a
|
|
|
|
|
_∈k_ k m = k ∈ (keys m)
|
2023-07-24 23:12:04 -07:00
|
|
|
|
|
2023-07-25 19:56:47 -07:00
|
|
|
|
foldr : ∀ {c} {C : Set c} → (A → B → C → C) -> C -> List (A × B) -> C
|
|
|
|
|
foldr f b [] = b
|
|
|
|
|
foldr f b ((k , v) ∷ xs) = f k v (foldr f b xs)
|
2023-07-25 00:10:57 -07:00
|
|
|
|
|
2023-07-24 23:12:04 -07:00
|
|
|
|
insert : A → B → List (A × B) → List (A × B)
|
|
|
|
|
insert k v [] = (k , v) ∷ []
|
|
|
|
|
insert k v (x@(k' , v') ∷ xs) with ≡-dec-A k k'
|
|
|
|
|
... | yes _ = (k' , f v v') ∷ xs
|
|
|
|
|
... | no _ = x ∷ insert k v xs
|
|
|
|
|
|
|
|
|
|
merge : List (A × B) → List (A × B) → List (A × B)
|
|
|
|
|
merge m₁ m₂ = foldr insert m₂ m₁
|
|
|
|
|
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-keys-∈ : ∀ {k : A} {v : B} {l : List (A × B)} →
|
2023-07-26 17:31:09 -07:00
|
|
|
|
k ∈k l → keys l ≡ keys (insert k v l)
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-keys-∈ {k} {v} {(k' , v') ∷ xs} (here k≡k')
|
2023-07-26 17:31:09 -07:00
|
|
|
|
with (≡-dec-A k k')
|
|
|
|
|
... | yes _ = refl
|
|
|
|
|
... | no k≢k' = absurd (k≢k' k≡k')
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-keys-∈ {k} {v} {(k' , _) ∷ xs} (there k∈kxs)
|
2023-07-26 17:31:09 -07:00
|
|
|
|
with (≡-dec-A k k')
|
|
|
|
|
... | yes _ = refl
|
2023-07-30 13:19:00 -07:00
|
|
|
|
... | no _ = cong (λ xs' → k' ∷ xs') (insert-keys-∈ k∈kxs)
|
2023-07-26 17:31:09 -07:00
|
|
|
|
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-keys-∉ : ∀ {k : A} {v : B} {l : List (A × B)} →
|
2023-07-26 17:31:09 -07:00
|
|
|
|
¬ (k ∈k l) → (keys l ++ (k ∷ [])) ≡ keys (insert k v l)
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-keys-∉ {k} {v} {[]} _ = refl
|
|
|
|
|
insert-keys-∉ {k} {v} {(k' , v') ∷ xs} k∉kl
|
2023-07-26 17:31:09 -07:00
|
|
|
|
with (≡-dec-A k k')
|
|
|
|
|
... | yes k≡k' = absurd (k∉kl (here k≡k'))
|
|
|
|
|
... | no _ = cong (λ xs' → k' ∷ xs')
|
2023-07-30 13:19:00 -07:00
|
|
|
|
(insert-keys-∉ (λ k∈kxs → k∉kl (there k∈kxs)))
|
2023-07-24 23:12:04 -07:00
|
|
|
|
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-preserves-Unique : ∀ {k : A} {v : B} {l : List (A × B)}
|
2023-07-26 17:31:09 -07:00
|
|
|
|
→ Unique (keys l) → Unique (keys (insert k v l))
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-preserves-Unique {k} {v} {l} u
|
2023-07-26 17:31:09 -07:00
|
|
|
|
with (∈k-dec k l)
|
2023-07-30 13:19:00 -07:00
|
|
|
|
... | yes k∈kl rewrite insert-keys-∈ {v = v} k∈kl = u
|
|
|
|
|
... | no k∉kl rewrite sym (insert-keys-∉ {v = v} k∉kl) = Unique-append k∉kl u
|
2023-07-25 22:58:42 -07:00
|
|
|
|
|
2023-07-26 17:31:09 -07:00
|
|
|
|
merge-preserves-Unique : ∀ (l₁ l₂ : List (A × B)) →
|
|
|
|
|
Unique (keys l₂) → Unique (keys (merge l₁ l₂))
|
|
|
|
|
merge-preserves-Unique [] l₂ u₂ = u₂
|
|
|
|
|
merge-preserves-Unique ((k₁ , v₁) ∷ xs₁) l₂ u₂ =
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-preserves-Unique (merge-preserves-Unique xs₁ l₂ u₂)
|
2023-07-26 17:31:09 -07:00
|
|
|
|
|
2023-07-30 13:21:03 -07:00
|
|
|
|
insert-preserves-∈ : ∀ {k k' : A} {v v' : B} {l : List (A × B)} →
|
2023-07-26 17:31:09 -07:00
|
|
|
|
¬ k ≡ k' → (k , v) ∈ l → (k , v) ∈ insert k' v' l
|
2023-07-30 13:21:03 -07:00
|
|
|
|
insert-preserves-∈ {k} {k'} {l = x ∷ xs} k≢k' (here k,v=x)
|
2023-07-26 17:31:09 -07:00
|
|
|
|
rewrite sym k,v=x with ≡-dec-A k' k
|
|
|
|
|
... | yes k'≡k = absurd (k≢k' (sym k'≡k))
|
|
|
|
|
... | no _ = here refl
|
2023-07-30 13:21:03 -07:00
|
|
|
|
insert-preserves-∈ {k} {k'} {l = (k'' , _) ∷ xs} k≢k' (there k,v∈xs)
|
2023-07-26 17:31:09 -07:00
|
|
|
|
with ≡-dec-A k' k''
|
|
|
|
|
... | yes _ = there k,v∈xs
|
2023-07-30 13:21:03 -07:00
|
|
|
|
... | no _ = there (insert-preserves-∈ k≢k' k,v∈xs)
|
2023-07-26 20:40:28 -07:00
|
|
|
|
|
2023-07-30 13:21:03 -07:00
|
|
|
|
insert-preserves-∈k : ∀ {k k' : A} {v' : B} {l : List (A × B)} →
|
2023-07-26 20:40:28 -07:00
|
|
|
|
¬ k ≡ k' → k ∈k l → k ∈k insert k' v' l
|
2023-07-30 13:21:03 -07:00
|
|
|
|
insert-preserves-∈k k≢k' k∈kl =
|
2023-07-26 20:58:41 -07:00
|
|
|
|
let (v , k,v∈l) = locate k∈kl
|
2023-07-30 13:21:03 -07:00
|
|
|
|
in ∈-cong proj₁ (insert-preserves-∈ k≢k' k,v∈l)
|
2023-07-26 20:40:28 -07:00
|
|
|
|
|
2023-07-30 13:46:52 -07:00
|
|
|
|
insert-preserves-∉k : ∀ {k k' : A} {v' : B} {l : List (A × B)} →
|
2023-07-26 20:40:28 -07:00
|
|
|
|
¬ k ≡ k' → ¬ k ∈k l → ¬ k ∈k insert k' v' l
|
2023-07-30 13:46:52 -07:00
|
|
|
|
insert-preserves-∉k {l = []} k≢k' k∉kl (here k≡k') = k≢k' k≡k'
|
|
|
|
|
insert-preserves-∉k {l = []} k≢k' k∉kl (there ())
|
|
|
|
|
insert-preserves-∉k {k} {k'} {v'} {(k'' , v'') ∷ xs} k≢k' k∉kl k∈kil
|
2023-07-26 20:40:28 -07:00
|
|
|
|
with ≡-dec-A k k''
|
|
|
|
|
... | yes k≡k'' = k∉kl (here k≡k'')
|
|
|
|
|
... | no k≢k'' with ≡-dec-A k' k'' | k∈kil
|
|
|
|
|
... | yes k'≡k'' | here k≡k'' = k≢k'' k≡k''
|
|
|
|
|
... | yes k'≡k'' | there k∈kxs = k∉kl (there k∈kxs)
|
|
|
|
|
... | no k'≢k'' | here k≡k'' = k∉kl (here k≡k'')
|
2023-07-30 13:46:52 -07:00
|
|
|
|
... | no k'≢k'' | there k∈kxs = insert-preserves-∉k k≢k'
|
2023-07-26 20:40:28 -07:00
|
|
|
|
(λ k∈kxs → k∉kl (there k∈kxs)) k∈kxs
|
|
|
|
|
|
2023-07-30 13:19:00 -07:00
|
|
|
|
merge-preserves-∉ : ∀ {k : A} {l₁ l₂ : List (A × B)} →
|
2023-07-26 20:40:28 -07:00
|
|
|
|
¬ k ∈k l₁ → ¬ k ∈k l₂ → ¬ k ∈k merge l₁ l₂
|
2023-07-30 13:19:00 -07:00
|
|
|
|
merge-preserves-∉ {l₁ = []} _ k∉kl₂ = k∉kl₂
|
|
|
|
|
merge-preserves-∉ {k} {(k' , v') ∷ xs₁} k∉kl₁ k∉kl₂
|
2023-07-26 20:40:28 -07:00
|
|
|
|
with ≡-dec-A k k'
|
|
|
|
|
... | yes k≡k' = absurd (k∉kl₁ (here k≡k'))
|
2023-07-30 13:46:52 -07:00
|
|
|
|
... | no k≢k' = insert-preserves-∉k k≢k' (merge-preserves-∉ (λ k∈kxs₁ → k∉kl₁ (there k∈kxs₁)) k∉kl₂)
|
2023-07-26 17:31:09 -07:00
|
|
|
|
|
2023-07-30 13:46:52 -07:00
|
|
|
|
merge-preserves-∈₁ : ∀ {k : A} {v : B} {l₁ l₂ : List (A × B)} →
|
2023-07-26 17:31:09 -07:00
|
|
|
|
¬ k ∈k l₁ → (k , v) ∈ l₂ → (k , v) ∈ merge l₁ l₂
|
2023-07-30 13:46:52 -07:00
|
|
|
|
merge-preserves-∈₁ {l₁ = []} _ k,v∈l₂ = k,v∈l₂
|
|
|
|
|
merge-preserves-∈₁ {l₁ = (k' , v') ∷ xs₁} k∉kl₁ k,v∈l₂ =
|
|
|
|
|
let recursion = merge-preserves-∈₁ (λ k∈xs₁ → k∉kl₁ (there k∈xs₁)) k,v∈l₂
|
2023-07-30 13:21:03 -07:00
|
|
|
|
in insert-preserves-∈ (λ k≡k' → k∉kl₁ (here k≡k')) recursion
|
2023-07-25 22:58:42 -07:00
|
|
|
|
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-fresh : ∀ {k : A} {v : B} {l : List (A × B)} →
|
2023-07-26 17:31:09 -07:00
|
|
|
|
¬ k ∈k l → (k , v) ∈ insert k v l
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-fresh {l = []} k∉kl = here refl
|
|
|
|
|
insert-fresh {k} {l = (k' , v') ∷ xs} k∉kl
|
2023-07-26 17:31:09 -07:00
|
|
|
|
with ≡-dec-A k k'
|
|
|
|
|
... | yes k≡k' = absurd (k∉kl (here k≡k'))
|
2023-07-30 13:19:00 -07:00
|
|
|
|
... | no _ = there (insert-fresh (λ k∈kxs → k∉kl (there k∈kxs)))
|
2023-07-25 22:58:42 -07:00
|
|
|
|
|
2023-07-30 13:46:52 -07:00
|
|
|
|
merge-preserves-∈₂ : ∀ {k : A} {v : B} {l₁ l₂ : List (A × B)} →
|
2023-07-26 17:31:09 -07:00
|
|
|
|
Unique (keys l₁) → (k , v) ∈ l₁ → ¬ k ∈k l₂ → (k , v) ∈ merge l₁ l₂
|
2023-07-30 13:46:52 -07:00
|
|
|
|
merge-preserves-∈₂ {k} {v} {(k' , v') ∷ xs₁} (push k'≢xs₁ uxs₁) (there k,v∈xs₁) k∉kl₂ =
|
2023-07-30 13:21:03 -07:00
|
|
|
|
insert-preserves-∈ k≢k' k,v∈mxs₁l
|
2023-07-26 20:40:28 -07:00
|
|
|
|
where
|
2023-07-30 13:46:52 -07:00
|
|
|
|
k,v∈mxs₁l = merge-preserves-∈₂ uxs₁ k,v∈xs₁ k∉kl₂
|
2023-07-26 20:40:28 -07:00
|
|
|
|
|
|
|
|
|
k≢k' : ¬ k ≡ k'
|
|
|
|
|
k≢k' with ≡-dec-A k k'
|
|
|
|
|
... | yes k≡k' rewrite k≡k' = absurd (All¬-¬Any k'≢xs₁ (∈-cong proj₁ k,v∈xs₁))
|
|
|
|
|
... | no k≢k' = k≢k'
|
2023-07-30 13:46:52 -07:00
|
|
|
|
merge-preserves-∈₂ {l₁ = (k' , v') ∷ xs₁} (push k'≢xs₁ uxs₁) (here k,v≡k',v') k∉kl₂
|
2023-07-26 20:40:28 -07:00
|
|
|
|
rewrite cong proj₁ k,v≡k',v' rewrite cong proj₂ k,v≡k',v' =
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-fresh (merge-preserves-∉ (All¬-¬Any k'≢xs₁) k∉kl₂)
|
2023-07-26 20:40:28 -07:00
|
|
|
|
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-combines : ∀ {k : A} {v v' : B} {l : List (A × B)} →
|
2023-07-26 20:40:28 -07:00
|
|
|
|
Unique (keys l) → (k , v') ∈ l → (k , f v v') ∈ (insert k v l)
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-combines {l = (k' , v'') ∷ xs} _ (here k,v'≡k',v'')
|
2023-07-26 20:40:28 -07:00
|
|
|
|
rewrite cong proj₁ k,v'≡k',v'' rewrite cong proj₂ k,v'≡k',v''
|
|
|
|
|
with ≡-dec-A k' k'
|
|
|
|
|
... | yes _ = here refl
|
|
|
|
|
... | no k≢k' = absurd (k≢k' refl)
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert-combines {k} {l = (k' , v'') ∷ xs} (push k'≢xs uxs) (there k,v'∈xs)
|
2023-07-26 20:40:28 -07:00
|
|
|
|
with ≡-dec-A k k'
|
|
|
|
|
... | yes k≡k' rewrite k≡k' = absurd (All¬-¬Any k'≢xs (∈-cong proj₁ k,v'∈xs))
|
2023-07-30 13:19:00 -07:00
|
|
|
|
... | no k≢k' = there (insert-combines uxs k,v'∈xs)
|
2023-07-26 20:40:28 -07:00
|
|
|
|
|
2023-07-30 13:46:52 -07:00
|
|
|
|
merge-combines : ∀ {k : A} {v₁ v₂ : B} {l₁ l₂ : List (A × B)} →
|
2023-07-26 20:40:28 -07:00
|
|
|
|
Unique (keys l₁) → Unique (keys l₂) →
|
|
|
|
|
(k , v₁) ∈ l₁ → (k , v₂) ∈ l₂ → (k , f v₁ v₂) ∈ merge l₁ l₂
|
2023-07-30 13:19:00 -07:00
|
|
|
|
merge-combines {l₁ = (k' , v) ∷ xs₁} {l₂} (push k'≢xs₁ uxs₁) ul₂ (here k,v₁≡k',v) k,v₂∈l₂
|
2023-07-26 20:40:28 -07:00
|
|
|
|
rewrite cong proj₁ (sym (k,v₁≡k',v)) rewrite cong proj₂ (sym (k,v₁≡k',v)) =
|
2023-07-30 13:46:52 -07:00
|
|
|
|
insert-combines (merge-preserves-Unique xs₁ l₂ ul₂) (merge-preserves-∈₁ (All¬-¬Any k'≢xs₁) k,v₂∈l₂)
|
2023-07-30 13:19:00 -07:00
|
|
|
|
merge-combines {k} {l₁ = (k' , v) ∷ xs₁} (push k'≢xs₁ uxs₁) ul₂ (there k,v₁∈xs₁) k,v₂∈l₂ =
|
2023-07-30 13:21:03 -07:00
|
|
|
|
insert-preserves-∈ k≢k' (merge-combines uxs₁ ul₂ k,v₁∈xs₁ k,v₂∈l₂)
|
2023-07-26 20:40:28 -07:00
|
|
|
|
where
|
|
|
|
|
k≢k' : ¬ k ≡ k'
|
|
|
|
|
k≢k' with ≡-dec-A k k'
|
|
|
|
|
... | yes k≡k' rewrite k≡k' = absurd (All¬-¬Any k'≢xs₁ (∈-cong proj₁ k,v₁∈xs₁))
|
|
|
|
|
... | no k≢k' = k≢k'
|
2023-07-25 22:58:42 -07:00
|
|
|
|
|
|
|
|
|
|
2023-07-25 19:56:47 -07:00
|
|
|
|
Map : Set (a ⊔ b)
|
|
|
|
|
Map = Σ (List (A × B)) (λ l → Unique (keys l))
|
|
|
|
|
|
|
|
|
|
_∈_ : (A × B) → Map → Set (a ⊔ b)
|
|
|
|
|
_∈_ p (kvs , _) = MemProp._∈_ p kvs
|
|
|
|
|
|
|
|
|
|
_∈k_ : A → Map → Set a
|
|
|
|
|
_∈k_ k (kvs , _) = MemProp._∈_ k (keys kvs)
|
|
|
|
|
|
|
|
|
|
Map-functional : ∀ {k : A} {v v' : B} {m : Map} → (k , v) ∈ m → (k , v') ∈ m → v ≡ v'
|
|
|
|
|
Map-functional {m = (l , ul)} k,v∈m k,v'∈m = ListAB-functional ul k,v∈m k,v'∈m
|
|
|
|
|
|
|
|
|
|
data Provenance (k : A) (m₁ m₂ : Map) : Set (a ⊔ b) where
|
|
|
|
|
both : (v₁ v₂ : B) → (k , v₁) ∈ m₁ → (k , v₂) ∈ m₂ → Provenance k m₁ m₂
|
|
|
|
|
in₁ : (v₁ : B) → (k , v₁) ∈ m₁ → ¬ k ∈k m₂ → Provenance k m₁ m₂
|
|
|
|
|
in₂ : (v₂ : B) → ¬ k ∈k m₁ → (k , v₂) ∈ m₂ → Provenance k m₁ m₂
|
2023-07-24 23:12:04 -07:00
|
|
|
|
|
|
|
|
|
module _ (f : B → B → B) where
|
2023-07-24 23:55:09 -07:00
|
|
|
|
open ImplInsert f renaming
|
|
|
|
|
( insert to insert-impl
|
|
|
|
|
; merge to merge-impl
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
insert : A → B → Map → Map
|
2023-07-30 13:19:00 -07:00
|
|
|
|
insert k v (kvs , uks) = (insert-impl k v kvs , insert-preserves-Unique uks)
|
2023-07-24 23:55:09 -07:00
|
|
|
|
|
|
|
|
|
merge : Map → Map → Map
|
2023-07-25 19:56:47 -07:00
|
|
|
|
merge (kvs₁ , _) (kvs₂ , uks₂) = (merge-impl kvs₁ kvs₂ , merge-preserves-Unique kvs₁ kvs₂ uks₂)
|
2023-07-24 23:55:09 -07:00
|
|
|
|
|
2023-07-25 19:56:47 -07:00
|
|
|
|
MergeResult : {k : A} {m₁ m₂ : Map} → Provenance k m₁ m₂ → Set (a ⊔ b)
|
|
|
|
|
MergeResult {k} {m₁} {m₂} (both v₁ v₂ _ _) = (k , f v₁ v₂) ∈ merge m₁ m₂
|
|
|
|
|
MergeResult {k} {m₁} {m₂} (in₁ v₁ _ _) = (k , v₁) ∈ merge m₁ m₂
|
|
|
|
|
MergeResult {k} {m₁} {m₂} (in₂ v₂ _ _) = (k , v₂) ∈ merge m₁ m₂
|
|
|
|
|
|
|
|
|
|
merge-provenance : ∀ (m₁ m₂ : Map) (k : A) → k ∈k merge m₁ m₂ → Σ (Provenance k m₁ m₂) MergeResult
|
2023-07-26 20:58:41 -07:00
|
|
|
|
merge-provenance m₁@(l₁ , u₁) m₂@(l₂ , u₂) k k∈km₁m₂
|
|
|
|
|
with ∈k-dec k l₁ | ∈k-dec k l₂
|
|
|
|
|
... | yes k∈kl₁ | yes k∈kl₂ =
|
|
|
|
|
let
|
|
|
|
|
(v₁ , k,v₁∈l₁) = locate k∈kl₁
|
|
|
|
|
(v₂ , k,v₂∈l₂) = locate k∈kl₂
|
|
|
|
|
in
|
2023-07-30 13:19:00 -07:00
|
|
|
|
(both v₁ v₂ k,v₁∈l₁ k,v₂∈l₂ , merge-combines u₁ u₂ k,v₁∈l₁ k,v₂∈l₂)
|
2023-07-26 20:58:41 -07:00
|
|
|
|
... | yes k∈kl₁ | no k∉kl₂ =
|
|
|
|
|
let
|
|
|
|
|
(v₁ , k,v₁∈l₁) = locate k∈kl₁
|
|
|
|
|
in
|
2023-07-30 13:46:52 -07:00
|
|
|
|
(in₁ v₁ k,v₁∈l₁ k∉kl₂ , merge-preserves-∈₂ u₁ k,v₁∈l₁ k∉kl₂)
|
2023-07-26 20:58:41 -07:00
|
|
|
|
... | no k∉kl₁ | yes k∈kl₂ =
|
|
|
|
|
let
|
|
|
|
|
(v₂ , k,v₂∈l₂) = locate k∈kl₂
|
|
|
|
|
in
|
2023-07-30 13:46:52 -07:00
|
|
|
|
(in₂ v₂ k∉kl₁ k,v₂∈l₂ , merge-preserves-∈₁ k∉kl₁ k,v₂∈l₂)
|
2023-07-30 13:19:00 -07:00
|
|
|
|
... | no k∉kl₁ | no k∉kl₂ = absurd (merge-preserves-∉ k∉kl₁ k∉kl₂ k∈km₁m₂)
|
2023-07-26 20:58:41 -07:00
|
|
|
|
|
2023-07-24 23:55:09 -07:00
|
|
|
|
module _ (_≈_ : B → B → Set b) where
|
|
|
|
|
open ImplRelation _≈_ renaming (subset to subset-impl)
|
|
|
|
|
|
|
|
|
|
subset : Map → Map → Set (a ⊔ b)
|
|
|
|
|
subset (kvs₁ , _) (kvs₂ , _) = subset-impl kvs₁ kvs₂
|
|
|
|
|
|
|
|
|
|
lift : Map → Map → Set (a ⊔ b)
|
|
|
|
|
lift m₁ m₂ = subset m₁ m₂ × subset m₂ m₁
|
2023-07-28 00:05:41 -07:00
|
|
|
|
|
|
|
|
|
module _ (f : B → B → B) where
|
|
|
|
|
module _ (f-comm : ∀ (b₁ b₂ : B) → f b₁ b₂ ≡ f b₂ b₁) where
|
2023-07-30 13:46:52 -07:00
|
|
|
|
merge-comm : ∀ (m₁ m₂ : Map) → lift (_≡_) (merge f m₁ m₂) (merge f m₂ m₁)
|
2023-07-28 00:05:41 -07:00
|
|
|
|
merge-comm m₁ m₂ = (merge-comm-subset m₁ m₂ , merge-comm-subset m₂ m₁)
|
|
|
|
|
where
|
|
|
|
|
merge-comm-subset : ∀ (m₁ m₂ : Map) → subset (_≡_) (merge f m₁ m₂) (merge f m₂ m₁)
|
2023-07-30 13:19:00 -07:00
|
|
|
|
merge-comm-subset m₁ m₂ k v k,v∈m₁m₂ with ∈k-dec k (proj₁ (merge f m₂ m₁) )
|
2023-07-28 00:05:41 -07:00
|
|
|
|
... | no k∉km₂m₁ = {!!}
|
|
|
|
|
... | yes k∈km₂m₁ with merge-provenance f m₁ m₂ k (∈-cong proj₁ k,v∈m₁m₂) | merge-provenance f m₂ m₁ k k∈km₂m₁
|
|
|
|
|
... | (both v₁ v₂ v₁∈m₁ v₂∈m₂ , v₁v₂∈m₁m₂) | (both v₂' v₁' v₂'∈m₂ v₁'∈m₁ , v₂'v₁'∈m₂m₁)
|
|
|
|
|
rewrite Map-functional {m = merge f m₁ m₂} k,v∈m₁m₂ v₁v₂∈m₁m₂
|
|
|
|
|
rewrite Map-functional {m = m₁} v₁∈m₁ v₁'∈m₁
|
|
|
|
|
rewrite Map-functional {m = m₂} v₂∈m₂ v₂'∈m₂ = (f v₂' v₁' , (f-comm v₁' v₂' , v₂'v₁'∈m₂m₁))
|
|
|
|
|
... | (in₁ v₁ v₁∈m₁ _ , v₁∈m₁m₂) | (in₂ v₁' _ v₁'∈m₁ , v₁'∈m₂m₁)
|
|
|
|
|
rewrite Map-functional {m = merge f m₁ m₂} k,v∈m₁m₂ v₁∈m₁m₂ =
|
|
|
|
|
(v₁' , (Map-functional {m = m₁} v₁∈m₁ v₁'∈m₁ , v₁'∈m₂m₁))
|
|
|
|
|
... | (in₂ v₂ _ v₂∈m₂ , v₂∈m₁m₂) | (in₁ v₂' v₂'∈m₂ _ , v₂'∈m₂m₁)
|
|
|
|
|
rewrite Map-functional {m = merge f m₁ m₂} k,v∈m₁m₂ v₂∈m₁m₂ =
|
|
|
|
|
(v₂' , (Map-functional {m = m₂} v₂∈m₂ v₂'∈m₂ , v₂'∈m₂m₁))
|
|
|
|
|
-- The rest of the cases are to be dismissed.
|
|
|
|
|
... | (both v₁ v₂ v₁∈m₁ v₂∈m₂ , _) | (in₁ v₂' v₂'∈m₂ k∉km₁ , _) = absurd (k∉km₁ (∈-cong proj₁ v₁∈m₁))
|
|
|
|
|
... | (both v₁ v₂ v₁∈m₁ v₂∈m₂ , _) | (in₂ v₁' k∉km₂ v₁'∈m₁ , _) = absurd (k∉km₂ (∈-cong proj₁ v₂∈m₂))
|
|
|
|
|
... | (in₁ v₁ v₁∈m₁ k∉km₂ , _) | (both v₂' v₁' v₂'∈m₂ v₁'∈m₁ , _) = absurd (k∉km₂ (∈-cong proj₁ v₂'∈m₂))
|
|
|
|
|
... | (in₁ v₁ v₁∈m₁ k∉km₂ , _) | (in₁ v₂' v₂'∈m₂ k∉km₁ , _) = absurd (k∉km₂ (∈-cong proj₁ v₂'∈m₂))
|
|
|
|
|
... | (in₂ v₂ k∉km₁ v₂∈m₂ , v₂∈m₁m₂) | (both v₂' v₁' v₂'∈m₂ v₁'∈m₁ , _) = absurd (k∉km₁ (∈-cong proj₁ v₁'∈m₁))
|
|
|
|
|
... | (in₂ v₂ k∉km₁ v₂∈m₂ , v₂∈m₁m₂) | (in₂ v₁' _ v₁'∈m₁ , v₁'∈m₂m₁) = absurd (k∉km₁ (∈-cong proj₁ v₁'∈m₁))
|