open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl) 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 open import Data.Nat using (ℕ) open import Data.String using (String; _++_) open import Data.List using (List; []; _∷_) open import Data.List.Membership.Propositional using () open import Data.Product using (_×_; _,_; Σ) open import Data.Unit using (⊤) open import Data.Empty using (⊥) Map : Set (a ⊔ b) Map = List (A × B) insert : (B → B → B) → A → B → Map → Map insert f k v [] = (k , v) ∷ [] insert f k v (x@(k' , v') ∷ xs) with ≡-dec-A k k' ... | yes _ = (k , f v v') ∷ xs ... | no _ = x ∷ insert f k v xs foldr : ∀ {c} {C : Set c} → (A → B → C → C) -> C -> Map -> C foldr f b [] = b foldr f b ((k , v) ∷ xs) = f k v (foldr f b xs) _∈_ : (A × B) → Map → Set (a ⊔ b) _∈_ p m = Data.List.Membership.Propositional._∈_ p m subset : ∀ (_≈_ : B → B → Set b) → Map → Map → Set (a ⊔ b) subset _≈_ m₁ m₂ = ∀ (k : A) (v : B) → (k , v) ∈ m₁ → Σ B (λ v' → v ≈ v' × ((k , v') ∈ m₂)) lift : ∀ (_≈_ : B → B → Set b) → Map → Map → Set (a ⊔ b) lift _≈_ m₁ m₂ = (m₁ ⊆ m₂) × (m₂ ⊆ m₁) where _⊆_ : Map → Map → Set (a ⊔ b) _⊆_ = subset _≈_