Debian Codegraph @

src/Data/Tree/AVL/Map/Membership/Propositional/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of the membership relation for AVL Maps identifying values
5 -- up to propositional equality.
6 ------------------------------------------------------------------------
7
8 {-# OPTIONS --cubical-compatible --safe #-}
9
10 open import Relation.Binary.Bundles using (StrictTotalOrder)
11
12 module Data.Tree.AVL.Map.Membership.Propositional.Properties
13 {a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂)
14 where
15
16 open import Data.Bool.Base using (true; false)
17 open import Data.Maybe.Base using (just; nothing; is-just)
18 open import Data.Product.Base as Product
19 using (_×_; ∃-syntax; _,_; proj₁; proj₂)
20 open import Data.Product.Relation.Binary.Pointwise.NonDependent
21 renaming (Pointwise to _×ᴿ_)
22 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
23 open import Function.Base using (_∘_; _∘′_)
24 open import Level using (Level)
25 open import Relation.Binary.Definitions
26 using (Transitive; Symmetric; _Respectsˡ_)
27 open import Relation.Binary.Core using (Rel)
28 open import Relation.Binary.Construct.Intersection using (_∩_)
29 open import Relation.Binary.PropositionalEquality.Core
30 using (_≡_; cong) renaming (refl to ≡-refl; sym to ≡-sym; trans to ≡-trans)
31 open import Relation.Nullary.Reflects using (Reflects)
32 open import Relation.Nullary.Decidable.Core using (yes; no)
33 open import Relation.Nullary.Negation.Core using (contradiction; ¬_)
34
35 open StrictTotalOrder strictTotalOrder renaming (Carrier to Key) hiding (trans)
36 open Eq using (refl; sym; trans)
37 open import Data.Tree.AVL strictTotalOrder using (tree)
38 open import Data.Tree.AVL.Indexed strictTotalOrder using (key)
39 import Data.Tree.AVL.Indexed.Relation.Unary.Any strictTotalOrder as IAny
40 import Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties strictTotalOrder as IAnyₚ
41 open import Data.Tree.AVL.Key strictTotalOrder using (⊥⁺<[_]<⊤⁺)
42 open import Data.Tree.AVL.Map strictTotalOrder
43 using (Map; empty; insert; lookup; member; singleton)
44 open import Data.Tree.AVL.Map.Relation.Unary.Any strictTotalOrder as Map
45 using (Any)
46 open import Data.Tree.AVL.Map.Membership.Propositional strictTotalOrder
47 open import Data.Tree.AVL.Relation.Unary.Any strictTotalOrder as Any
48 using (tree)
49
50 private
51 variable
52 v p q : Level
53 V : Set v
54 m : Map V
55 k k′ : Key
56 x x′ y y′ : V
57 kx : Key × V
58
59 ≈ₖᵥ-trans : Transitive (_≈ₖᵥ_ {V = V})
60 ≈ₖᵥ-trans {i = i} {k = k} = ×-transitive Eq.trans ≡-trans {i = i} {k = k}
61
62 ≈ₖᵥ-sym : Symmetric (_≈ₖᵥ_ {V = V})
63 ≈ₖᵥ-sym {x = x} {y = y} = ×-symmetric sym ≡-sym {x} {y}
64
65 ∈ₖᵥ-Respectsˡ : _∈ₖᵥ_ {V = V} Respectsˡ _≈ₖᵥ_
66 ∈ₖᵥ-Respectsˡ x~y = Any.map (≈ₖᵥ-trans (≈ₖᵥ-sym x~y))
67
68 ∈ₖᵥ-empty : kx ∉ₖᵥ empty
69 ∈ₖᵥ-empty (tree ())
70
71 ------------------------------------------------------------------------
72 -- singleton
73
74 ∈ₖᵥ-singleton⁺ : (k , x) ∈ₖᵥ singleton k x
75 ∈ₖᵥ-singleton⁺ = tree (IAnyₚ.singleton⁺ _ _ _ (refl , ≡-refl))
76
77 ∈ₖᵥ-singleton⁻ : (k , x) ∈ₖᵥ singleton k′ x′ → k ≈ k′ × x ≡ x′
78 ∈ₖᵥ-singleton⁻ (tree p) = IAnyₚ.singleton⁻ _ _ _ p
79
80 private
81 ≈-lookup : (p : (k , x) ∈ₖᵥ m) → k ≈ Any.lookupKey p
82 ≈-lookup (tree p) = proj₁ (IAnyₚ.lookup-result p)
83
84 ------------------------------------------------------------------------
85 -- insert
86
87 ∈ₖᵥ-insert⁺ : k ≉ k′ → (k , x) ∈ₖᵥ m → (k , x) ∈ₖᵥ insert k′ x′ m
88 ∈ₖᵥ-insert⁺ {k′ = k′} {m = m@(tree t)} k≉k′ (tree p) =
89 tree (IAnyₚ.insert⁺ _ _ _ (⊥⁺<[ _ ]<⊤⁺) p k′≉key-p)
90 where
91 k′≉key-p : k′ ≉ Any.lookupKey (tree p)
92 k′≉key-p k′≈key-p = k≉k′ (Eq.trans (≈-lookup (tree p)) (Eq.sym k′≈key-p))
93
94 ∈ₖᵥ-insert⁺⁺ : (k , x) ∈ₖᵥ insert k x m
95 ∈ₖᵥ-insert⁺⁺ {k = k} {m = tree t} with IAny.any? ((k ≟_) ∘ key) t
96 ... | yes k∈ = tree (IAnyₚ.Any-insert-just _ _ _ _ (λ k′ → _, ≡-refl) k∈)
97 ... | no ¬k∈ = tree (IAnyₚ.Any-insert-nothing _ _ _ _ (refl , ≡-refl) ¬k∈)
98
99 private
100 ≈ₖᵥ-Resp : k ≈ k′ → kx ≈ₖᵥ (k′ , x) → kx ≈ₖᵥ (k , x)
101 ≈ₖᵥ-Resp = (λ{ k′≈l (k≈l , x≡) → (trans k≈l (sym k′≈l) , x≡)})
102
103 ∈ₖᵥ-insert⁻ : (k , x) ∈ₖᵥ insert k′ x′ m → (k ≈ k′ × x ≡ x′) ⊎ (k ≉ k′ × (k , x) ∈ₖᵥ m)
104 ∈ₖᵥ-insert⁻ {m = tree t} (tree kx∈insert)
105 with IAnyₚ.insert⁻ ≈ₖᵥ-Resp _ _ t (⊥⁺<[ _ ]<⊤⁺) kx∈insert
106 ... | inj₁ p = inj₁ p
107 ... | inj₂ p = let k′≉p , k≈p , _ = IAnyₚ.lookup-result p
108 k≉k′ = λ k≈k′ → k′≉p (trans (sym k≈k′) k≈p)
109 in inj₂ (k≉k′ , tree (IAny.map proj₂ p))
110
111 ------------------------------------------------------------------------
112 -- lookup
113
114 ∈ₖᵥ-lookup⁺ : (k , x) ∈ₖᵥ m → lookup m k ≡ just x
115 ∈ₖᵥ-lookup⁺ {k = k} {m = tree t} (tree kx∈m)
116 with IAnyₚ.lookup⁺ t k (⊥⁺<[ _ ]<⊤⁺) kx∈m | IAnyₚ.lookup-result kx∈m
117 ... | inj₁ p≉k | k≈p , x≡p = contradiction (sym k≈p) p≉k
118 ... | inj₂ (p≈k , eq) | k≈p , x≡p = ≡-trans eq (cong just (≡-sym x≡p))
119
120 ∈ₖᵥ-lookup⁻ : lookup m k ≡ just x → (k , x) ∈ₖᵥ m
121 ∈ₖᵥ-lookup⁻ {m = tree t} {k = k} {x = x} eq
122 = tree (IAny.map (Product.map sym ≡-sym) (IAnyₚ.lookup⁻ t k x (⊥⁺<[ _ ]<⊤⁺) eq))
123
124 ∈ₖᵥ-lookup-nothing⁺ : (∀ x → (k , x) ∉ₖᵥ m) → lookup m k ≡ nothing
125 ∈ₖᵥ-lookup-nothing⁺ {k = k} {m = m@(tree t)} k∉m with lookup m k in eq
126 ... | nothing = ≡-refl
127 ... | just x = contradiction (∈ₖᵥ-lookup⁻ eq) (k∉m x)
128
129 ∈ₖᵥ-lookup-nothing⁻ : lookup m k ≡ nothing → (k , x) ∉ₖᵥ m
130 ∈ₖᵥ-lookup-nothing⁻ eq kx∈m with ≡-trans (≡-sym eq) (∈ₖᵥ-lookup⁺ kx∈m)
131 ... | ()
132
133 ------------------------------------------------------------------------
134 -- member
135
136 ∈ₖᵥ-member : (k , x) ∈ₖᵥ m → member k m ≡ true
137 ∈ₖᵥ-member = cong is-just ∘ ∈ₖᵥ-lookup⁺
138
139 ∉ₖᵥ-member : (∀ x → (k , x) ∉ₖᵥ m) → member k m ≡ false
140 ∉ₖᵥ-member = cong is-just ∘ ∈ₖᵥ-lookup-nothing⁺
141
142 member-∈ₖᵥ : member k m ≡ true → ∃[ x ] (k , x) ∈ₖᵥ m
143 member-∈ₖᵥ {k = k} {m = m} ≡true with lookup m k in eq
144 ... | just x = x , ∈ₖᵥ-lookup⁻ eq
145
146 member-∉ₖᵥ : member k m ≡ false → ∀ x → (k , x) ∉ₖᵥ m
147 member-∉ₖᵥ {k = k} {m = m} ≡false x with lookup m k in eq
148 ... | nothing = ∈ₖᵥ-lookup-nothing⁻ eq
149
150 member-Reflects-∈ₖᵥ : Reflects (∃[ x ] (k , x) ∈ₖᵥ m) (member k m)
151 member-Reflects-∈ₖᵥ {k = k} {m = m} with lookup m k in eq
152 ... | just x = Reflects.ofʸ (x , ∈ₖᵥ-lookup⁻ eq)
153 ... | nothing = Reflects.ofⁿ (∈ₖᵥ-lookup-nothing⁻ eq ∘ proj₂)