Reorganize a bit and start on provenance

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2023-07-25 19:56:47 -07:00
parent 88a712fa98
commit 6b51cd4050

View File

@ -40,26 +40,38 @@ Unique-append {c} {C} {x} {x' ∷ xs'} x∉xs (push x'≢ uxs') = push (help x'
help {[]} _ = x'≢x []
help {e es} (x'≢e x'≢es) = x'≢e help x'≢es
Map : Set (a b)
Map = Σ (List (A × B)) (λ l Unique (keys l))
_∈_ : (A × B) Map Set (a b)
_∈_ p (kvs , _) = MemProp._∈_ p kvs
absurd : {a} {A : Set a} A
absurd ()
private module _ where
open MemProp using (_∈_)
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
private module ImplRelation (_≈_ : B B Set b) where
open MemProp using (_∈_)
subset : List (A × B) List (A × B) Set (a b)
subset m₁ m₂ = (k : A) (v : B) MemProp._∈_ (k , v) m₁ Σ B (λ v' v v' × (MemProp._∈_ (k , v') m₂))
subset m₁ m₂ = (k : A) (v : B) (k , v) m₁ Σ B (λ v' v v' × ((k , v') m₂))
private module ImplInsert (f : B B B) where
_∈k_ : A List (A × B) Set a
_∈k_ k m = MemProp._∈_ k (keys m)
open MemProp using (_∈_)
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)
private
_∈k_ : A List (A × B) Set a
_∈k_ k m = k (keys m)
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)
insert : A B List (A × B) List (A × B)
insert k v [] = (k , v) []
@ -96,19 +108,31 @@ private module ImplInsert (f : B → B → B) where
witness (here k≡k') = k≢k' k≡k'
witness (there k∈kxs) = k∉kxs k∈kxs
insert-preserves-unique : (k : A) (v : B) (l : List (A × B)) Unique (keys l) Unique (keys (insert k v l))
insert-preserves-unique k v l u with (∈k-dec k l)
insert-preserves-Unique : (k : A) (v : B) (l : List (A × B)) Unique (keys l) Unique (keys (insert k v l))
insert-preserves-Unique k v l u with (∈k-dec k l)
... | yes k∈kl rewrite insert-keys-∈ k v l k∈kl = u
... | no k∉kl rewrite sym (insert-keys-∉ k v l k∉kl) = Unique-append k∉kl u
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₂ = insert-preserves-unique k₁ v₁ (merge xs₁ l₂) (merge-preserves-unique xs₁ l₂ u₂)
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₂ = insert-preserves-Unique k₁ v₁ (merge xs₁ l₂) (merge-preserves-Unique xs₁ l₂ u₂)
private
unique-not-in : {k : A} {v : B} {l : List (A × B)} ¬ (All (λ k' ¬ k k') (keys l) × MemProp._∈_ (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)
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₂
module _ (f : B B B) where
open ImplInsert f renaming
@ -117,11 +141,18 @@ module _ (f : B → B → B) where
)
insert : A B Map Map
insert k v (kvs , uks) = (insert-impl k v kvs , insert-preserves-unique k v kvs uks)
insert k v (kvs , uks) = (insert-impl k v kvs , insert-preserves-Unique k v kvs uks)
merge : Map Map Map
merge (kvs₁ , _) (kvs₂ , uks₂) = (merge-impl kvs₁ kvs₂ , merge-preserves-unique kvs₁ kvs₂ uks₂)
merge (kvs₁ , _) (kvs₂ , uks₂) = (merge-impl kvs₁ kvs₂ , merge-preserves-Unique kvs₁ kvs₂ uks₂)
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
merge-provenance = {!!}
module _ (_≈_ : B B Set b) where
open ImplRelation _≈_ renaming (subset to subset-impl)
@ -131,9 +162,3 @@ module _ (_≈_ : B → B → Set b) where
lift : Map Map Set (a b)
lift m₁ m₂ = subset m₁ m₂ × subset m₂ m₁
Map-functional : {k : A} {v v' : B} {m : Map} (k , v) m (k , v') m v v'
Map-functional (here k,v≡x) (here k,v'≡x) = cong proj₂ (trans k,v≡x (sym k,v'≡x))
Map-functional {m = (_ , 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))
Map-functional {m = (_ , 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))
Map-functional {m = (_ xs , push _ uxs)} (there k,v∈xs) (there k,v'∈xs) = Map-functional {m = (xs , uxs)} k,v∈xs k,v'∈xs