Use inferred variables for proofs where possible

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2023-07-30 13:19:00 -07:00
parent 4033a1b33d
commit b066db9829

120
Map.agda
View File

@ -119,129 +119,128 @@ private module ImplInsert (f : B → B → B) where
merge : List (A × B) List (A × B) List (A × B) merge : List (A × B) List (A × B) List (A × B)
merge m₁ m₂ = foldr insert m₂ m₁ merge m₁ m₂ = foldr insert m₂ m₁
insert-keys-∈ : (k : A) (v : B) (l : List (A × B)) insert-keys-∈ : {k : A} {v : B} {l : List (A × B)}
k ∈k l keys l keys (insert k v l) k ∈k l keys l keys (insert k v l)
insert-keys-∈ k v ((k' , v') xs) (here k≡k') insert-keys-∈ {k} {v} {(k' , v') xs} (here k≡k')
with (≡-dec-A k k') with (≡-dec-A k k')
... | yes _ = refl ... | yes _ = refl
... | no k≢k' = absurd (k≢k' k≡k') ... | no k≢k' = absurd (k≢k' k≡k')
insert-keys-∈ k v ((k' , _) xs) (there k∈kxs) insert-keys-∈ {k} {v} {(k' , _) xs} (there k∈kxs)
with (≡-dec-A k k') with (≡-dec-A k k')
... | yes _ = refl ... | yes _ = refl
... | no _ = cong (λ xs' k' xs') (insert-keys-∈ k v xs k∈kxs) ... | no _ = cong (λ xs' k' xs') (insert-keys-∈ k∈kxs)
insert-keys-∉ : (k : A) (v : B) (l : List (A × B)) insert-keys-∉ : {k : A} {v : B} {l : List (A × B)}
¬ (k ∈k l) (keys l ++ (k [])) keys (insert k v l) ¬ (k ∈k l) (keys l ++ (k [])) keys (insert k v l)
insert-keys-∉ k v [] _ = refl insert-keys-∉ {k} {v} {[]} _ = refl
insert-keys-∉ k v ((k' , v') xs) k∉kl insert-keys-∉ {k} {v} {(k' , v') xs} k∉kl
with (≡-dec-A k k') with (≡-dec-A k k')
... | yes k≡k' = absurd (k∉kl (here k≡k')) ... | yes k≡k' = absurd (k∉kl (here k≡k'))
... | no _ = cong (λ xs' k' xs') ... | no _ = cong (λ xs' k' xs')
(insert-keys-∉ k v xs (λ k∈kxs k∉kl (there k∈kxs))) (insert-keys-∉ (λ k∈kxs k∉kl (there k∈kxs)))
insert-preserves-Unique : (k : A) (v : B) (l : List (A × B)) insert-preserves-Unique : {k : A} {v : B} {l : List (A × B)}
Unique (keys l) Unique (keys (insert k v l)) Unique (keys l) Unique (keys (insert k v l))
insert-preserves-Unique k v l u insert-preserves-Unique {k} {v} {l} u
with (∈k-dec k l) with (∈k-dec k l)
... | yes k∈kl rewrite insert-keys-∈ k v l k∈kl = u ... | yes k∈kl rewrite insert-keys-∈ {v = v} k∈kl = u
... | no k∉kl rewrite sym (insert-keys-∉ k v l k∉kl) = Unique-append k∉kl u ... | no k∉kl rewrite sym (insert-keys-∉ {v = v} k∉kl) = Unique-append k∉kl u
merge-preserves-Unique : (l₁ l₂ : List (A × B)) merge-preserves-Unique : (l₁ l₂ : List (A × B))
Unique (keys l₂) Unique (keys (merge l₁ l₂)) Unique (keys l₂) Unique (keys (merge l₁ l₂))
merge-preserves-Unique [] l₂ u₂ = u₂ merge-preserves-Unique [] l₂ u₂ = u₂
merge-preserves-Unique ((k₁ , v₁) xs₁) l₂ u₂ = merge-preserves-Unique ((k₁ , v₁) xs₁) l₂ u₂ =
insert-preserves-Unique k₁ v₁ (merge xs₁ l₂) insert-preserves-Unique (merge-preserves-Unique xs₁ l₂ u₂)
(merge-preserves-Unique xs₁ l₂ u₂)
insert-preserves-∈-right : (k k' : A) (v v' : B) (l : List (A × B)) insert-preserves-∈-right : {k k' : A} {v v' : B} {l : List (A × B)}
¬ k k' (k , v) l (k , v) insert k' v' l ¬ k k' (k , v) l (k , v) insert k' v' l
insert-preserves-∈-right k k' v v' (x xs) k≢k' (here k,v=x) insert-preserves-∈-right {k} {k'} {l = x xs} k≢k' (here k,v=x)
rewrite sym k,v=x with ≡-dec-A k' k rewrite sym k,v=x with ≡-dec-A k' k
... | yes k'≡k = absurd (k≢k' (sym k'≡k)) ... | yes k'≡k = absurd (k≢k' (sym k'≡k))
... | no _ = here refl ... | no _ = here refl
insert-preserves-∈-right k k' v v' ((k'' , _) xs) k≢k' (there k,v∈xs) insert-preserves-∈-right {k} {k'} {l = (k'' , _) xs} k≢k' (there k,v∈xs)
with ≡-dec-A k' k'' with ≡-dec-A k' k''
... | yes _ = there k,v∈xs ... | yes _ = there k,v∈xs
... | no _ = there (insert-preserves-∈-right k k' v v' xs k≢k' k,v∈xs) ... | no _ = there (insert-preserves-∈-right k≢k' k,v∈xs)
insert-preserves-∈k-right : (k k' : A) (v' : B) (l : List (A × B)) insert-preserves-∈k-right : {k k' : A} {v' : B} {l : List (A × B)}
¬ k k' k ∈k l k ∈k insert k' v' l ¬ k k' k ∈k l k ∈k insert k' v' l
insert-preserves-∈k-right k k' v' l k≢k' k∈kl = insert-preserves-∈k-right k≢k' k∈kl =
let (v , k,v∈l) = locate k∈kl let (v , k,v∈l) = locate k∈kl
in ∈-cong proj₁ (insert-preserves-∈-right k k' v v' l k≢k' k,v∈l) in ∈-cong proj₁ (insert-preserves-∈-right k≢k' k,v∈l)
insert-preserves-∉-right : (k k' : A) (v' : B) (l : List (A × B)) insert-preserves-∉-right : {k k' : A} {v' : B} {l : List (A × B)}
¬ k k' ¬ k ∈k l ¬ k ∈k insert k' v' l ¬ k k' ¬ k ∈k l ¬ k ∈k insert k' v' l
insert-preserves-∉-right k k' v' [] k≢k' k∉kl (here k≡k') = k≢k' k≡k' insert-preserves-∉-right {l = []} k≢k' k∉kl (here k≡k') = k≢k' k≡k'
insert-preserves-∉-right k k' v' [] k≢k' k∉kl (there ()) insert-preserves-∉-right {l = []} k≢k' k∉kl (there ())
insert-preserves-∉-right k k' v' ((k'' , v'') xs) k≢k' k∉kl k∈kil insert-preserves-∉-right {k} {k'} {v'} {(k'' , v'') xs} k≢k' k∉kl k∈kil
with ≡-dec-A k k'' with ≡-dec-A k k''
... | yes k≡k'' = k∉kl (here k≡k'') ... | yes k≡k'' = k∉kl (here k≡k'')
... | no k≢k'' with ≡-dec-A k' k'' | k∈kil ... | no k≢k'' with ≡-dec-A k' k'' | k∈kil
... | yes k'≡k'' | here k≡k'' = k≢k'' k≡k'' ... | yes k'≡k'' | here k≡k'' = k≢k'' k≡k''
... | yes k'≡k'' | there k∈kxs = k∉kl (there k∈kxs) ... | yes k'≡k'' | there k∈kxs = k∉kl (there k∈kxs)
... | no k'≢k'' | here k≡k'' = k∉kl (here k≡k'') ... | no k'≢k'' | here k≡k'' = k∉kl (here k≡k'')
... | no k'≢k'' | there k∈kxs = insert-preserves-∉-right k k' v' xs k≢k' ... | no k'≢k'' | there k∈kxs = insert-preserves-∉-right k≢k'
(λ k∈kxs k∉kl (there k∈kxs)) k∈kxs (λ k∈kxs k∉kl (there k∈kxs)) k∈kxs
merge-preserves-∉ : (k : A) (l₁ l₂ : List (A × B)) merge-preserves-∉ : {k : A} {l₁ l₂ : List (A × B)}
¬ k ∈k l₁ ¬ k ∈k l₂ ¬ k ∈k merge l₁ l₂ ¬ k ∈k l₁ ¬ k ∈k l₂ ¬ k ∈k merge l₁ l₂
merge-preserves-∉ k [] l₂ _ k∉kl₂ = k∉kl₂ merge-preserves-∉ {l₁ = []} _ k∉kl₂ = k∉kl₂
merge-preserves-∉ k ((k' , v') xs₁) l₂ k∉kl₁ k∉kl₂ merge-preserves-∉ {k} {(k' , v') xs₁} k∉kl₁ k∉kl₂
with ≡-dec-A k k' with ≡-dec-A k k'
... | yes k≡k' = absurd (k∉kl₁ (here k≡k')) ... | yes k≡k' = absurd (k∉kl₁ (here k≡k'))
... | no k≢k' = insert-preserves-∉-right k k' v' _ k≢k' (merge-preserves-∉ k xs₁ l₂ (λ k∈kxs₁ k∉kl₁ (there k∈kxs₁)) k∉kl₂) ... | no k≢k' = insert-preserves-∉-right k≢k' (merge-preserves-∉ (λ k∈kxs₁ k∉kl₁ (there k∈kxs₁)) k∉kl₂)
merge-preserves-keys₁ : (k : A) (v : B) (l₁ l₂ : List (A × B)) merge-preserves-keys₁ : {k : A} {v : B} {l₁ l₂ : List (A × B)}
¬ k ∈k l₁ (k , v) l₂ (k , v) merge l₁ l₂ ¬ k ∈k l₁ (k , v) l₂ (k , v) merge l₁ l₂
merge-preserves-keys₁ k v [] l₂ _ k,v∈l₂ = k,v∈l₂ merge-preserves-keys₁ {l₁ = []} _ k,v∈l₂ = k,v∈l₂
merge-preserves-keys₁ k v ((k' , v') xs₁) l₂ k∉kl₁ k,v∈l₂ = merge-preserves-keys₁ {l₁ = (k' , v') xs₁} k∉kl₁ k,v∈l₂ =
let recursion = merge-preserves-keys₁ k v xs₁ l₂ (λ k∈xs₁ k∉kl₁ (there k∈xs₁)) k,v∈l₂ let recursion = merge-preserves-keys₁ (λ k∈xs₁ k∉kl₁ (there k∈xs₁)) k,v∈l₂
in insert-preserves-∈-right k k' v v' _ (λ k≡k' k∉kl₁ (here k≡k')) recursion in insert-preserves-∈-right (λ k≡k' k∉kl₁ (here k≡k')) recursion
insert-fresh : (k : A) (v : B) (l : List (A × B)) insert-fresh : {k : A} {v : B} {l : List (A × B)}
¬ k ∈k l (k , v) insert k v l ¬ k ∈k l (k , v) insert k v l
insert-fresh k v [] k∉kl = here refl insert-fresh {l = []} k∉kl = here refl
insert-fresh k v ((k' , v') xs) k∉kl insert-fresh {k} {l = (k' , v') xs} k∉kl
with ≡-dec-A k k' with ≡-dec-A k k'
... | yes k≡k' = absurd (k∉kl (here k≡k')) ... | yes k≡k' = absurd (k∉kl (here k≡k'))
... | no _ = there (insert-fresh k v xs (λ k∈kxs k∉kl (there k∈kxs))) ... | no _ = there (insert-fresh (λ k∈kxs k∉kl (there k∈kxs)))
merge-preserves-keys₂ : (k : A) (v : B) (l₁ l₂ : List (A × B)) merge-preserves-keys₂ : {k : A} {v : B} {l₁ l₂ : List (A × B)}
Unique (keys l₁) (k , v) l₁ ¬ k ∈k l₂ (k , v) merge l₁ l₂ Unique (keys l₁) (k , v) l₁ ¬ k ∈k l₂ (k , v) merge l₁ l₂
merge-preserves-keys₂ k v ((k' , v') xs₁) l₂ (push k'≢xs₁ uxs₁) (there k,v∈xs₁) k∉kl₂ = merge-preserves-keys₂ {k} {v} {(k' , v') xs₁} (push k'≢xs₁ uxs₁) (there k,v∈xs₁) k∉kl₂ =
insert-preserves-∈-right k k' v v' (merge xs₁ l₂) k≢k' k,v∈mxs₁l insert-preserves-∈-right k≢k' k,v∈mxs₁l
where where
k,v∈mxs₁l = merge-preserves-keys₂ k v xs₁ l₂ uxs₁ k,v∈xs₁ k∉kl₂ k,v∈mxs₁l = merge-preserves-keys₂ uxs₁ k,v∈xs₁ k∉kl₂
k≢k' : ¬ k k' k≢k' : ¬ k k'
k≢k' with ≡-dec-A 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₁)) ... | yes k≡k' rewrite k≡k' = absurd (All¬-¬Any k'≢xs₁ (∈-cong proj₁ k,v∈xs₁))
... | no k≢k' = k≢k' ... | no k≢k' = k≢k'
merge-preserves-keys₂ k v ((k' , v') xs₁) l₂ (push k'≢xs₁ uxs₁) (here k,v≡k',v') k∉kl₂ merge-preserves-keys₂ {l₁ = (k' , v') xs₁} (push k'≢xs₁ uxs₁) (here k,v≡k',v') k∉kl₂
rewrite cong proj₁ k,v≡k',v' rewrite cong proj₂ k,v≡k',v' = rewrite cong proj₁ k,v≡k',v' rewrite cong proj₂ k,v≡k',v' =
insert-fresh k' v' _ (merge-preserves-∉ k' xs₁ l₂ (All¬-¬Any k'≢xs₁) k∉kl₂) insert-fresh (merge-preserves-∉ (All¬-¬Any k'≢xs₁) k∉kl₂)
insert-combines : (k : A) (v v' : B) (l : List (A × B)) insert-combines : {k : A} {v v' : B} {l : List (A × B)}
Unique (keys l) (k , v') l (k , f v v') (insert k v l) Unique (keys l) (k , v') l (k , f v v') (insert k v l)
insert-combines k v v' ((k' , v'') xs) _ (here k,v'≡k',v'') insert-combines {l = (k' , v'') xs} _ (here k,v'≡k',v'')
rewrite cong proj₁ k,v'≡k',v'' rewrite cong proj₂ k,v'≡k',v'' rewrite cong proj₁ k,v'≡k',v'' rewrite cong proj₂ k,v'≡k',v''
with ≡-dec-A k' k' with ≡-dec-A k' k'
... | yes _ = here refl ... | yes _ = here refl
... | no k≢k' = absurd (k≢k' refl) ... | no k≢k' = absurd (k≢k' refl)
insert-combines k v v' ((k' , v'') xs) (push k'≢xs uxs) (there k,v'∈xs) insert-combines {k} {l = (k' , v'') xs} (push k'≢xs uxs) (there k,v'∈xs)
with ≡-dec-A k k' with ≡-dec-A k k'
... | yes k≡k' rewrite k≡k' = absurd (All¬-¬Any k'≢xs (∈-cong proj₁ k,v'∈xs)) ... | yes k≡k' rewrite k≡k' = absurd (All¬-¬Any k'≢xs (∈-cong proj₁ k,v'∈xs))
... | no k≢k' = there (insert-combines k v v' xs uxs k,v'∈xs) ... | no k≢k' = there (insert-combines uxs k,v'∈xs)
merge-combines : forall (k : A) (v₁ v₂ : B) (l₁ l₂ : List (A × B)) merge-combines : forall {k : A} {v₁ v₂ : B} {l₁ l₂ : List (A × B)}
Unique (keys l₁) Unique (keys l₂) Unique (keys l₁) Unique (keys l₂)
(k , v₁) l₁ (k , v₂) l₂ (k , f v₁ v₂) merge l₁ l₂ (k , v₁) l₁ (k , v₂) l₂ (k , f v₁ v₂) merge l₁ l₂
merge-combines k v₁ v₂ ((k' , v) xs₁) l₂ (push k'≢xs₁ uxs₁) ul₂ (here k,v₁≡k',v) k,v₂∈l₂ merge-combines {l₁ = (k' , v) xs₁} {l₂} (push k'≢xs₁ uxs₁) ul₂ (here k,v₁≡k',v) k,v₂∈l₂
rewrite cong proj₁ (sym (k,v₁≡k',v)) rewrite cong proj₂ (sym (k,v₁≡k',v)) = rewrite cong proj₁ (sym (k,v₁≡k',v)) rewrite cong proj₂ (sym (k,v₁≡k',v)) =
insert-combines k v₁ v₂ _ (merge-preserves-Unique xs₁ l₂ ul₂) (merge-preserves-keys₁ k v₂ xs₁ l₂ (All¬-¬Any k'≢xs₁) k,v₂∈l₂) insert-combines (merge-preserves-Unique xs₁ l₂ ul₂) (merge-preserves-keys₁ (All¬-¬Any k'≢xs₁) k,v₂∈l₂)
merge-combines k v₁ v₂ ((k' , v) xs₁) l₂ (push k'≢xs₁ uxs₁) ul₂ (there k,v₁∈xs₁) k,v₂∈l₂ = merge-combines {k} {l₁ = (k' , v) xs₁} (push k'≢xs₁ uxs₁) ul₂ (there k,v₁∈xs₁) k,v₂∈l₂ =
insert-preserves-∈-right k k' (f v₁ v₂) v _ k≢k' (merge-combines k v₁ v₂ xs₁ l₂ uxs₁ ul₂ k,v₁∈xs₁ k,v₂∈l₂) insert-preserves-∈-right k≢k' (merge-combines uxs₁ ul₂ k,v₁∈xs₁ k,v₂∈l₂)
where where
k≢k' : ¬ k k' k≢k' : ¬ k k'
k≢k' with ≡-dec-A k k' k≢k' with ≡-dec-A k k'
@ -273,7 +272,7 @@ module _ (f : B → B → B) where
) )
insert : A B Map Map 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 uks)
merge : Map Map Map 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₂)
@ -291,18 +290,18 @@ module _ (f : B → B → B) where
(v₁ , k,v₁∈l₁) = locate k∈kl₁ (v₁ , k,v₁∈l₁) = locate k∈kl₁
(v₂ , k,v₂∈l₂) = locate k∈kl₂ (v₂ , k,v₂∈l₂) = locate k∈kl₂
in in
(both v₁ v₂ k,v₁∈l₁ k,v₂∈l₂ , merge-combines k v₁ v₂ l₁ l₂ u₁ u₂ k,v₁∈l₁ k,v₂∈l₂) (both v₁ v₂ k,v₁∈l₁ k,v₂∈l₂ , merge-combines u₁ u₂ k,v₁∈l₁ k,v₂∈l₂)
... | yes k∈kl₁ | no k∉kl₂ = ... | yes k∈kl₁ | no k∉kl₂ =
let let
(v₁ , k,v₁∈l₁) = locate k∈kl₁ (v₁ , k,v₁∈l₁) = locate k∈kl₁
in in
(in v₁ k,v₁∈l₁ k∉kl₂ , merge-preserves-keys₂ k v₁ l₁ l₂ u₁ k,v₁∈l₁ k∉kl₂) (in v₁ k,v₁∈l₁ k∉kl₂ , merge-preserves-keys₂ u₁ k,v₁∈l₁ k∉kl₂)
... | no k∉kl₁ | yes k∈kl₂ = ... | no k∉kl₁ | yes k∈kl₂ =
let let
(v₂ , k,v₂∈l₂) = locate k∈kl₂ (v₂ , k,v₂∈l₂) = locate k∈kl₂
in in
(in v₂ k∉kl₁ k,v₂∈l₂ , merge-preserves-keys₁ k v₂ l₁ l₂ k∉kl₁ k,v₂∈l₂) (in v₂ k∉kl₁ k,v₂∈l₂ , merge-preserves-keys₁ k∉kl₁ k,v₂∈l₂)
... | no k∉kl₁ | no k∉kl₂ = absurd (merge-preserves-∉ k l₁ l₂ k∉kl₁ k∉kl₂ k∈km₁m₂) ... | no k∉kl₁ | no k∉kl₂ = absurd (merge-preserves-∉ k∉kl₁ k∉kl₂ k∈km₁m₂)
module _ (_≈_ : B B Set b) where module _ (_≈_ : B B Set b) where
open ImplRelation _≈_ renaming (subset to subset-impl) open ImplRelation _≈_ renaming (subset to subset-impl)
@ -319,8 +318,7 @@ module _ (f : B → B → B) where
merge-comm m₁ m₂ = (merge-comm-subset m₁ m₂ , merge-comm-subset m₂ m₁) merge-comm m₁ m₂ = (merge-comm-subset m₁ m₂ , merge-comm-subset m₂ m₁)
where where
merge-comm-subset : (m₁ m₂ : Map) subset (_≡_) (merge f m₁ m₂) (merge f m₂ m₁) merge-comm-subset : (m₁ m₂ : Map) subset (_≡_) (merge f m₁ m₂) (merge f m₂ m₁)
merge-comm-subset m₁ m₂ k v k,v∈m₁m₂ merge-comm-subset m₁ m₂ k v k,v∈m₁m₂ with ∈k-dec k (proj₁ (merge f m₂ m₁) )
with ∈k-dec k (proj₁ (merge f m₂ m₁) )
... | no k∉km₂m₁ = {!!} ... | 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₁ ... | 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₁) ... | (both v₁ v₂ v₁∈m₁ v₂∈m₂ , v₁v₂∈m₁m₂) | (both v₂' v₁' v₂'∈m₂ v₁'∈m₁ , v₂'v₁'∈m₂m₁)