Finish all in/not-in proofs.

Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
Danila Fedorin 2023-07-26 20:40:28 -07:00
parent 12217e6928
commit 461732244a

119
Map.agda
View File

@ -1,7 +1,7 @@
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; sym; trans; cong)
open import Relation.Binary.Definitions using (Decidable)
open import Relation.Binary.Core using (Rel)
open import Relation.Nullary using (Dec; yes; no)
open import Relation.Nullary using (Dec; yes; no; Reflects; ofʸ; ofⁿ)
open import Agda.Primitive using (Level; _⊔_)
module Map {a b : Level} (A : Set a) (B : Set b)
@ -41,6 +41,10 @@ Unique-append {c} {C} {x} {x' ∷ xs'} x∉xs (push x'≢ uxs') =
help {[]} _ = x'≢x []
help {e es} (x'≢e x'≢es) = x'≢e help x'≢es
All¬-¬Any : {p c} {C : Set c} {P : C Set p} {l : List C} All (λ x ¬ P x) l ¬ Any P l
All¬-¬Any {l = x xs} (¬Px _) (here Px) = ¬Px Px
All¬-¬Any {l = x xs} (_ ¬Pxs) (there Pxs) = All¬-¬Any ¬Pxs Pxs
absurd : {a} {A : Set a} A
absurd ()
@ -131,6 +135,10 @@ private module ImplInsert (f : B → B → B) where
∈-cong f (here c≡c') = here (cong f c≡c')
∈-cong f (there c∈xs) = there (∈-cong f c∈xs)
locate : (k : A) (l : List (A × B)) k ∈k l Σ B (λ v (k , v) l)
locate k ((k' , v) xs) (here k≡k') rewrite k≡k' = (v , here refl)
locate k ((k' , v) xs) (there k∈kxs) = let (v , k,v∈xs) = locate k xs k∈kxs in (v , there k,v∈xs)
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
@ -145,53 +153,100 @@ private module ImplInsert (f : B → B → B) where
insert-preserves-Unique k₁ v₁ (merge xs₁ l₂)
(merge-preserves-Unique xs₁ l₂ u₂)
insert-preserves-other-keys : (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
insert-preserves-other-keys k k' v v' (x xs) k≢k' (here k,v=x)
insert-preserves-∈-right k k' v v' (x xs) k≢k' (here k,v=x)
rewrite sym k,v=x with ≡-dec-A k' k
... | yes k'≡k = absurd (k≢k' (sym k'≡k))
... | no _ = here refl
insert-preserves-other-keys k k' v v' ((k'' , _) xs) k≢k' (there k,v∈xs)
insert-preserves-∈-right k k' v v' ((k'' , _) xs) k≢k' (there k,v∈xs)
with ≡-dec-A k' k''
... | yes _ = there k,v∈xs
... | no _ = there (insert-preserves-other-keys k k' v v' xs k≢k' k,v∈xs)
... | no _ = there (insert-preserves-∈-right k k' v v' xs k≢k' k,v∈xs)
insert-preserves-∈k-right : (k k' : A) (v' : B) (l : List (A × B))
¬ k k' k ∈k l k ∈k insert k' v' l
insert-preserves-∈k-right k k' v' l k≢k' k∈kl =
let (v , k,v∈l) = locate k l k∈kl
in ∈-cong proj₁ (insert-preserves-∈-right k k' v v' l k≢k' k,v∈l)
insert-preserves-∉-right : (k k' : A) (v' : B) (l : List (A × B))
¬ 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 k k' v' [] k≢k' k∉kl (there ())
insert-preserves-∉-right k k' v' ((k'' , v'') xs) k≢k' k∉kl k∈kil
with ≡-dec-A k k''
... | yes k≡k'' = k∉kl (here k≡k'')
... | no k≢k'' with ≡-dec-A k' k'' | k∈kil
... | yes k'≡k'' | here k≡k'' = k≢k'' k≡k''
... | 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'' | there k∈kxs = insert-preserves-∉-right k k' v' xs k≢k'
(λ k∈kxs k∉kl (there k∈kxs)) k∈kxs
merge-preserves-∉ : (k : A) (l₁ l₂ : List (A × B))
¬ k ∈k l₁ ¬ k ∈k l₂ ¬ k ∈k merge l₁ l₂
merge-preserves-∉ k [] l₂ _ k∉kl₂ = k∉kl₂
merge-preserves-∉ k ((k' , v') xs₁) l₂ k∉kl₁ k∉kl₂
with ≡-dec-A 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₂)
merge-preserves-keys₁ : (k : A) (v : B) (l₁ l₂ : List (A × B))
¬ 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₁ k v ((k' , v') xs₁) l₂ k∉kl₁ k,v∈l₂ =
let recursion = merge-preserves-keys₁ k v xs₁ l₂ (λ k∈xs₁ k∉kl₁ (there k∈xs₁)) k,v∈l₂
in insert-preserves-other-keys k k' v v' _ (λ k≡k' k∉kl₁ (here k≡k')) recursion
in insert-preserves-∈-right k k' v v' _ (λ k≡k' k∉kl₁ (here k≡k')) recursion
insert-preserves-other-key : (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
insert-preserves-other-key k v [] k∉kl = here refl
insert-preserves-other-key k v ((k' , v') xs) k∉kl
insert-fresh k v [] k∉kl = here refl
insert-fresh k v ((k' , v') xs) k∉kl
with ≡-dec-A k k'
... | yes k≡k' = absurd (k∉kl (here k≡k'))
... | no _ = there (insert-preserves-other-key k v xs (λ k∈kxs k∉kl (there k∈kxs)))
... | no _ = there (insert-fresh k v xs (λ k∈kxs k∉kl (there k∈kxs)))
-- prove that ¬ k ∈k m → (k , v) ∈ insert k v m
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₂
merge-preserves-keys₂ k v ((k' , v') xs₁) l₂ (push k'≢xs₁ uxs₁) (here _) k∉kl₂ = {!!} -- hard!
-- where
-- rest : ∀ (l l' : List (A × B)) → All (λ k'' → ¬ k ≡ k'') (keys l) → ¬ k ∈k l' → ¬ k ∈k merge l l'
-- rest [] l' _ k∉kl' = k∉kl'
-- rest l [] (k≢l) _ = help
-- where
-- help : ∀ (l : List (A × B)) → All (λ k'' → ¬ k ≡ k'') (keys l) → ¬ k ∈k l
-- help [] _ ()
-- help ((k'' , _) ∷ xs) (k≢k'' ∷ k≢xs) (here k≡k'') = k≢k'' k≡k''
-- help ((k'' , _) ∷ xs) (k≢k'' ∷ k≢xs) (there k∈kxs) = help xs k≢xs k∈kxs
-- -- rest (x@(k'' , _) ∷ xs) l' (k≢k'' ∷ k≢xs) k∉kl' with (≡-dec-A k'' = (rest xs l' k≢xs k∉kl')
-- -- where
-- -- help : ¬ k ∈k (merge (x ∷ xs) l') -- insert x (merge xs l')
-- -- help (here k≡k'') = {!!}
-- -- help (there k∈) = {!!}
-- -- let nested = (rest xs l' k≢xs k∉kl')
merge-preserves-keys₂ k v ((k' , v') xs₁) l₂ (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
where
k,v∈mxs₁l = merge-preserves-keys₂ k v xs₁ l₂ uxs₁ k,v∈xs₁ k∉kl₂
k≢k' : ¬ 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₁))
... | 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₂
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-combines : (k : A) (v v' : B) (l : List (A × B))
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'')
rewrite cong proj₁ k,v'≡k',v'' rewrite cong proj₂ k,v'≡k',v''
with ≡-dec-A k' k'
... | yes _ = here refl
... | no k≢k' = absurd (k≢k' refl)
insert-combines k v v' ((k' , v'') xs) (push k'≢xs uxs) (there k,v'∈xs)
with ≡-dec-A k k'
... | 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)
merge-combines : forall (k : A) (v₁ v₂ : B) (l₁ l₂ : List (A × B))
Unique (keys l₁) Unique (keys 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₂
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₂)
merge-combines k v₁ v₂ ((k' , v) xs₁) l₂ (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₂)
where
k≢k' : ¬ 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₁))
... | no k≢k' = k≢k'
Map : Set (a b)
@ -237,20 +292,20 @@ module _ (f : B → B → B) where
--
-- prove that ¬ k ∈k m₁ → (k , v) ∈ m₂ → (k , v) ∈ merge m₁ m₂ (done)
-- prove that k ≢ k' → (k , v) ∈ m → (k , v) ∈ insert k' v' m (done)
-- prove that (k , v) ∈ m₁ → ¬ k ∈k m₂ → (k , v) ∈ merge m₁ m₂ (stuck)
-- prove that ¬ k ∈k m → (k , v) ∈ insert k v m
-- prove that (k , v) ∈ m₁ → ¬ k ∈k m₂ → (k , v) ∈ merge m₁ m₂ (done)
-- prove that ¬ k ∈k m → (k , v) ∈ insert k v m (done)
--
-- ------------------------------------------------------------------------
--
-- The following relies on uniqueness, since inserts stops after the first encounter.
--
-- prove that (k , v) ∈ m₁ → (k , v') ∈ m₂ → (k, f v v') ∈ merge m₁ m₂
-- prove that (k , v) ∈ m₁ → (k , v') ∈ m₂ → (k, f v v') ∈ merge m₁ m₂ (done)
--
-- ------------------------------------------------------------------------
--
-- The following can probably be proven via keys.
--
-- prove that k ∉k m₁ → k ∉k m₂ → k ∉k merge m₁ m₂
-- prove that k ∉k m₁ → k ∉k m₂ → k ∉k merge m₁ m₂ (done)
module _ (_≈_ : B B Set b) where
open ImplRelation _≈_ renaming (subset to subset-impl)