Use an expression-based provenance to make enumerating cases easier

This should come in handy for the associativity proof.
This commit is contained in:
Danila Fedorin 2023-07-30 16:43:07 -07:00
parent 6039c1dfab
commit de2f202bdf

View File

@ -265,6 +265,10 @@ data Provenance (k : A) (m₁ m₂ : Map) : Set (a ⊔ b) where
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₂
data Expr : Set (a b) where
`_ : Map Expr
__ : Expr Expr Expr
module _ (f : B B B) where
open ImplInsert f renaming
( insert to insert-impl
@ -277,31 +281,32 @@ module _ (f : B → B → B) where
union : Map Map Map
union (kvs₁ , _) (kvs₂ , uks₂) = (union-impl kvs₁ kvs₂ , union-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₂) union m₁ m₂
MergeResult {k} {m₁} {m₂} (in v₁ _ _) = (k , v₁) union m₁ m₂
MergeResult {k} {m₁} {m₂} (in v₂ _ _) = (k , v₂) union m₁ m₂
⟦_⟧ : Expr -> Map
` m = m
e₁ e₂ = union e₁ e₂
data Magic (k : A) : B Expr Set (a b) where
single : {v : B} {m : Map} (k , v) m Magic k v (` m)
in₁ᵘ : {v : B} {e₁ e₂ : Expr} Magic k v e₁ ¬ k ∈k e₂ Magic k v (e₁ e₂)
in₂ᵘ : {v : B} {e₁ e₂ : Expr} ¬ k ∈k e₁ Magic k v e₂ Magic k v (e₁ e₂)
bothᵘ : {v₁ v₂ : B} {e₁ e₂ : Expr} Magic k v₁ e₁ Magic k v₂ e₂ Magic k (f v₁ v₂) (e₁ e₂)
Expr-Magic : (k : A) (e : Expr) k ∈k e Σ B (λ v (Magic k v e × (k , v) e ))
Expr-Magic k (` m) k∈km = let (v , k,v∈m) = locate k∈km in (v , (single k,v∈m , k,v∈m))
Expr-Magic k (e₁ e₂) k∈ke₁e₂
with ∈k-dec k (proj₁ e₁ ) | ∈k-dec k (proj₁ e₂ )
... | yes k∈ke₁ | yes k∈ke₂ =
let (v₁ , (g₁ , k,v₁∈e₁)) = Expr-Magic k e₁ k∈ke₁
(v₂ , (g₂ , k,v₂∈e₂)) = Expr-Magic k e₂ k∈ke₂
in (f v₁ v₂ , (bothᵘ g₁ g₂ , union-combines (proj₂ e₁ ) (proj₂ e₂ ) k,v₁∈e₁ k,v₂∈e₂))
... | yes k∈ke₁ | no k∉ke₂ =
let (v₁ , (g₁ , k,v₁∈e₁)) = Expr-Magic k e₁ k∈ke₁
in (v₁ , (in₁ᵘ g₁ k∉ke₂ , union-preserves-∈₂ (proj₂ e₁ ) k,v₁∈e₁ k∉ke₂))
... | no k∉ke₁ | yes k∈ke₂ =
let (v₂ , (g₂ , k,v₂∈e₂)) = Expr-Magic k e₂ k∈ke₂
in (v₂ , (in₂ᵘ k∉ke₁ g₂ , union-preserves-∈₁ k∉ke₁ k,v₂∈e₂))
... | no k∉ke₁ | no k∉ke₂ = absurd (union-preserves-∉ k∉ke₁ k∉ke₂ k∈ke₁e₂)
union-provenance : (m₁ m₂ : Map) (k : A) k ∈k union m₁ m₂ Σ (Provenance k m₁ m₂) MergeResult
union-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
(both v₁ v₂ k,v₁∈l₁ k,v₂∈l₂ , union-combines u₁ u₂ k,v₁∈l₁ k,v₂∈l₂)
... | yes k∈kl₁ | no k∉kl₂ =
let
(v₁ , k,v₁∈l₁) = locate k∈kl₁
in
(in v₁ k,v₁∈l₁ k∉kl₂ , union-preserves-∈₂ u₁ k,v₁∈l₁ k∉kl₂)
... | no k∉kl₁ | yes k∈kl₂ =
let
(v₂ , k,v₂∈l₂) = locate k∈kl₂
in
(in v₂ k∉kl₁ k,v₂∈l₂ , union-preserves-∈₁ k∉kl₁ k,v₂∈l₂)
... | no k∉kl₁ | no k∉kl₂ = absurd (union-preserves-∉ k∉kl₁ k∉kl₂ k∈km₁m₂)
module _ (_≈_ : B B Set b) where
open ImplRelation _≈_ renaming (subset to subset-impl)
@ -319,14 +324,13 @@ module _ (f : B → B → B) where
where
union-comm-subset : (m₁ m₂ : Map) subset (_≡_) (union f m₁ m₂) (union f m₂ m₁)
union-comm-subset m₁@(l₁ , u₁) m₂@(l₂ , u₂) k v k,v∈m₁m₂
with union-provenance f m₁ m₂ k (∈-cong proj₁ k,v∈m₁m₂)
... | (both v₁ v₂ v₁∈m₁ v₂∈m₂ , v₁v₂∈m₁m₂)
with Expr-Magic f k ((` m₁) (` m₂)) (∈-cong proj₁ k,v∈m₁m₂)
... | (_ , (both {v₁} {v₂} (single v₁∈m₁) (single v₂∈m₂) , v₁v₂∈m₁m₂))
rewrite Map-functional {m = union f m₁ m₂} k,v∈m₁m₂ v₁v₂∈m₁m₂ =
(f v₂ v₁ , (f-comm v₁ v₂ , ImplInsert.union-combines f u₂ u₁ v₂∈m₂ v₁∈m₁))
... | (in v₁ v₁∈m₁ k∉km₂ , v₁∈m₁m₂)
... | (_ , (in₁ᵘ {v₁} (single v₁∈m₁) k∉km₂ , v₁∈m₁m₂))
rewrite Map-functional {m = union f m₁ m₂} k,v∈m₁m₂ v₁∈m₁m₂ =
(v₁ , (refl , ImplInsert.union-preserves-∈₁ f k∉km₂ v₁∈m₁))
... | (in v₂ k∉km₁ v₂∈m₂ , v₂∈m₁m₂)
... | (_ , (in {v₂} k∉km₁ (single v₂∈m₂) , v₂∈m₁m₂))
rewrite Map-functional {m = union f m₁ m₂} k,v∈m₁m₂ v₂∈m₁m₂ =
(v₂ , (refl , ImplInsert.union-preserves-∈₂ f u₂ v₂∈m₂ k∉km₁))