2023-07-23 17:50:25 -07:00
|
|
|
|
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; sym; cong)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
open import Relation.Binary.Definitions using (Decidable)
|
|
|
|
|
open import Relation.Binary.Core using (Rel)
|
|
|
|
|
open import Relation.Nullary using (Dec; yes; no)
|
|
|
|
|
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-23 17:50:25 -07:00
|
|
|
|
open import Relation.Nullary using (¬_)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
open import Data.Nat using (ℕ)
|
|
|
|
|
open import Data.String using (String; _++_)
|
|
|
|
|
open import Data.List using (List; []; _∷_)
|
|
|
|
|
open import Data.List.Membership.Propositional using ()
|
2023-07-23 17:50:25 -07:00
|
|
|
|
open import Data.List.Relation.Unary.All using (All; _∷_)
|
|
|
|
|
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.Unit using (⊤)
|
|
|
|
|
open import Data.Empty using (⊥)
|
|
|
|
|
|
|
|
|
|
Map : Set (a ⊔ b)
|
|
|
|
|
Map = List (A × B)
|
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
keys : List (A × B) → List A
|
|
|
|
|
keys [] = []
|
|
|
|
|
keys ((k , v) ∷ xs) = k ∷ keys xs
|
|
|
|
|
|
|
|
|
|
data Unique {c} {C : Set c} : List C → Set c where
|
2023-07-23 21:34:24 -07:00
|
|
|
|
empty : Unique []
|
2023-07-24 20:38:34 -07:00
|
|
|
|
push : forall {x : C} {xs : List C}
|
|
|
|
|
→ 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-24 20:38:34 -07:00
|
|
|
|
_∈_ : (A × B) → List (A × B) → Set (a ⊔ b)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
_∈_ p m = Data.List.Membership.Propositional._∈_ p m
|
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
_∈k_ : A → List (A × B) → Set a
|
|
|
|
|
_∈k_ k m = Data.List.Membership.Propositional._∈_ k (keys m)
|
|
|
|
|
|
|
|
|
|
subset : ∀ (_≈_ : B → B → Set b) → List (A × B) → List (A × B) → Set (a ⊔ b)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
subset _≈_ m₁ m₂ = ∀ (k : A) (v : B) → (k , v) ∈ m₁ → Σ B (λ v' → v ≈ v' × ((k , v') ∈ m₂))
|
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
lift : ∀ (_≈_ : B → B → Set b) → List (A × B) → List (A × B) → Set (a ⊔ b)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
lift _≈_ m₁ m₂ = (m₁ ⊆ m₂) × (m₂ ⊆ m₁)
|
|
|
|
|
where
|
2023-07-24 20:38:34 -07:00
|
|
|
|
_⊆_ : List (A × B) → List (A × B) → Set (a ⊔ b)
|
2023-07-23 00:51:34 -07:00
|
|
|
|
_⊆_ = subset _≈_
|
2023-07-23 17:50:25 -07:00
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
foldr : ∀ {c} {C : Set c} → (A → B → C → C) -> C -> List (A × B) -> C
|
2023-07-23 17:50:25 -07:00
|
|
|
|
foldr f b [] = b
|
|
|
|
|
foldr f b ((k , v) ∷ xs) = f k v (foldr f b xs)
|
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
insert : (B → B → B) → A → B → List (A × B) → List (A × B)
|
2023-07-23 17:50:25 -07:00
|
|
|
|
insert f k v [] = (k , v) ∷ []
|
|
|
|
|
insert f k v (x@(k' , v') ∷ xs) with ≡-dec-A k k'
|
2023-07-24 20:38:34 -07:00
|
|
|
|
... | yes _ = (k' , f v v') ∷ xs
|
2023-07-23 17:50:25 -07:00
|
|
|
|
... | no _ = x ∷ insert f k v xs
|
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
merge : (B → B → B) → List (A × B) → List (A × B) → List (A × B)
|
2023-07-23 17:50:25 -07:00
|
|
|
|
merge f m₁ m₂ = foldr (insert f) m₂ m₁
|
|
|
|
|
|
2023-07-24 20:38:34 -07:00
|
|
|
|
absurd : ∀ {a} {A : Set a} → ⊥ → A
|
|
|
|
|
absurd ()
|
|
|
|
|
|
|
|
|
|
insert-keys-∈ : ∀ (f : B → B → B) (k : A) (v : B) (l : List (A × B)) → k ∈k l → keys l ≡ keys (insert f k v l)
|
|
|
|
|
insert-keys-∈ f k v ((k' , v') ∷ xs) (here k≡k') with (≡-dec-A k k')
|
|
|
|
|
... | yes _ = refl
|
|
|
|
|
... | no k≢k' = absurd (k≢k' k≡k')
|
|
|
|
|
insert-keys-∈ f k v ((k' , _) ∷ xs) (there k∈kxs) with (≡-dec-A k k')
|
|
|
|
|
... | yes _ = refl
|
|
|
|
|
... | no _ = cong (λ xs' → k' ∷ xs') (insert-keys-∈ f k v xs k∈kxs)
|
|
|
|
|
|
|
|
|
|
Map-functional : ∀ (k : A) (v v' : B) (xs : List (A × B)) → Unique (keys ((k , v) ∷ xs)) → Data.List.Membership.Propositional._∈_ (k , v') ((k , v) ∷ xs) → v ≡ v'
|
2023-07-23 17:50:25 -07:00
|
|
|
|
Map-functional k v v' _ _ (here k,v'≡k,v) = sym (cong proj₂ k,v'≡k,v)
|
2023-07-23 21:34:24 -07:00
|
|
|
|
Map-functional k v v' xs (push k≢ _) (there k,v'∈xs) = absurd (unique-not-in xs v' (k≢ , k,v'∈xs))
|
2023-07-23 17:50:25 -07:00
|
|
|
|
where
|
2023-07-24 20:38:34 -07:00
|
|
|
|
unique-not-in : ∀ (xs : List (A × B)) (v' : B) → ¬ (All (λ k' → ¬ k ≡ k') (keys xs) × (k , v') ∈ xs)
|
2023-07-23 17:50:25 -07:00
|
|
|
|
unique-not-in ((k' , _) ∷ xs) v' (k≢k' ∷ _ , here k',≡x) = k≢k' (cong proj₁ k',≡x)
|
|
|
|
|
unique-not-in (_ ∷ xs) v' (_ ∷ rest , there k,v'∈xs) = unique-not-in xs v' (rest , k,v'∈xs)
|