| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- AVL trees where the stored values may depend on their key |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (StrictTotalOrder) |
| 10 |
|
| 11 |
module Data.Tree.AVL.Indexed |
| 12 |
{a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂) where |
| 13 |
|
| 14 |
open import Level using (Level; _⊔_) |
| 15 |
open import Data.Nat.Base using (ℕ; zero; suc; _+_) |
| 16 |
open import Data.Product.Base using (Σ; ∃; _×_; _,_; proj₁) |
| 17 |
open import Data.Maybe.Base using (Maybe; just; nothing) |
| 18 |
open import Data.List.Base as List using (List) |
| 19 |
open import Data.DifferenceList using (DiffList; []; _∷_; _++_) |
| 20 |
open import Function.Base as F hiding (const) |
| 21 |
open import Relation.Unary |
| 22 |
open import Relation.Binary.Definitions using (_Respects_; Tri; tri<; tri≈; tri>) |
| 23 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 24 |
|
| 25 |
private |
| 26 |
variable |
| 27 |
l v : Level |
| 28 |
A : Set l |
| 29 |
|
| 30 |
open StrictTotalOrder strictTotalOrder renaming (Carrier to Key) |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Re-export core definitions publicly |
| 34 |
|
| 35 |
open import Data.Tree.AVL.Key strictTotalOrder public |
| 36 |
open import Data.Tree.AVL.Value Eq.setoid public |
| 37 |
open import Data.Tree.AVL.Height public |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- Definitions of the tree |
| 41 |
|
| 42 |
-- The trees have three parameters/indices: a lower bound on the |
| 43 |
-- keys, an upper bound, and a height. |
| 44 |
-- |
| 45 |
-- (The bal argument is the balance factor.) |
| 46 |
|
| 47 |
data Tree {v} (V : Value v) (l u : Key⁺) : ℕ → Set (a ⊔ v ⊔ ℓ₂) where |
| 48 |
leaf : (l<u : l <⁺ u) → Tree V l u 0 |
| 49 |
node : ∀ {hˡ hʳ h} |
| 50 |
(kv : K& V) |
| 51 |
(lk : Tree V l [ kv .key ] hˡ) |
| 52 |
(ku : Tree V [ kv .key ] u hʳ) |
| 53 |
(bal : hˡ ∼ hʳ ⊔ h) → |
| 54 |
Tree V l u (suc h) |
| 55 |
|
| 56 |
module _ {v} {V : Value v} where |
| 57 |
|
| 58 |
ordered : ∀ {l u n} → Tree V l u n → l <⁺ u |
| 59 |
ordered (leaf l<u) = l<u |
| 60 |
ordered (node kv lk ku bal) = trans⁺ _ (ordered lk) (ordered ku) |
| 61 |
|
| 62 |
private |
| 63 |
Val = Value.family V |
| 64 |
V≈ = Value.respects V |
| 65 |
|
| 66 |
leaf-injective : ∀ {l u} {p q : l <⁺ u} → (Tree V l u 0 ∋ leaf p) ≡ leaf q → p ≡ q |
| 67 |
leaf-injective refl = refl |
| 68 |
|
| 69 |
node-injective-key : |
| 70 |
∀ {hˡ₁ hˡ₂ hʳ₁ hʳ₂ h l u k₁ k₂} |
| 71 |
{lk₁ : Tree V l [ k₁ .key ] hˡ₁} {lk₂ : Tree V l [ k₂ .key ] hˡ₂} |
| 72 |
{ku₁ : Tree V [ k₁ .key ] u hʳ₁} {ku₂ : Tree V [ k₂ .key ] u hʳ₂} |
| 73 |
{bal₁ : hˡ₁ ∼ hʳ₁ ⊔ h} {bal₂ : hˡ₂ ∼ hʳ₂ ⊔ h} → |
| 74 |
node k₁ lk₁ ku₁ bal₁ ≡ node k₂ lk₂ ku₂ bal₂ → k₁ ≡ k₂ |
| 75 |
node-injective-key refl = refl |
| 76 |
|
| 77 |
-- See also node-injectiveˡ, node-injectiveʳ, and node-injective-bal |
| 78 |
-- in Data.Tree.AVL.Indexed.WithK. |
| 79 |
|
| 80 |
-- Cast operations. Logarithmic in the size of the tree, if we don't |
| 81 |
-- count the time needed to construct the new proofs in the leaf |
| 82 |
-- cases. (The same kind of caveat applies to other operations |
| 83 |
-- below.) |
| 84 |
-- |
| 85 |
-- Perhaps it would be worthwhile changing the data structure so |
| 86 |
-- that the casts could be implemented in constant time (excluding |
| 87 |
-- proof manipulation). However, note that this would not change the |
| 88 |
-- worst-case time complexity of the operations below (up to Θ). |
| 89 |
|
| 90 |
castˡ : ∀ {l m u h} → l <⁺ m → Tree V m u h → Tree V l u h |
| 91 |
castˡ {l} l<m (leaf m<u) = leaf (trans⁺ l l<m m<u) |
| 92 |
castˡ l<m (node k mk ku bal) = node k (castˡ l<m mk) ku bal |
| 93 |
|
| 94 |
castʳ : ∀ {l m u h} → Tree V l m h → m <⁺ u → Tree V l u h |
| 95 |
castʳ {l} (leaf l<m) m<u = leaf (trans⁺ l l<m m<u) |
| 96 |
castʳ (node k lk km bal) m<u = node k lk (castʳ km m<u) bal |
| 97 |
|
| 98 |
-- Various constant-time functions which construct trees out of |
| 99 |
-- smaller pieces, sometimes using rotation. |
| 100 |
|
| 101 |
pattern node⁺ k₁ t₁ k₂ t₂ t₃ bal = node k₁ t₁ (node k₂ t₂ t₃ bal) ∼+ |
| 102 |
|
| 103 |
joinˡ⁺ : ∀ {l u hˡ hʳ h} → |
| 104 |
(k : K& V) → |
| 105 |
(∃ λ i → Tree V l [ k .key ] (i ⊕ hˡ)) → |
| 106 |
Tree V [ k .key ] u hʳ → |
| 107 |
(bal : hˡ ∼ hʳ ⊔ h) → |
| 108 |
∃ λ i → Tree V l u (i ⊕ (1 + h)) |
| 109 |
joinˡ⁺ k₂ (0# , t₁) t₃ bal = (0# , node k₂ t₁ t₃ bal) |
| 110 |
joinˡ⁺ k₂ (1# , t₁) t₃ ∼0 = (1# , node k₂ t₁ t₃ ∼-) |
| 111 |
joinˡ⁺ k₂ (1# , t₁) t₃ ∼+ = (0# , node k₂ t₁ t₃ ∼0) |
| 112 |
joinˡ⁺ k₄ (1# , node k₂ t₁ t₃ ∼-) t₅ ∼- = (0# , node k₂ t₁ (node k₄ t₃ t₅ ∼0) ∼0) |
| 113 |
joinˡ⁺ k₄ (1# , node k₂ t₁ t₃ ∼0) t₅ ∼- = (1# , node k₂ t₁ (node k₄ t₃ t₅ ∼-) ∼+) |
| 114 |
joinˡ⁺ k₆ (1# , node⁺ k₂ t₁ k₄ t₃ t₅ bal) t₇ ∼- |
| 115 |
= (0# , node k₄ (node k₂ t₁ t₃ (max∼ bal)) |
| 116 |
(node k₆ t₅ t₇ (∼max bal)) |
| 117 |
∼0) |
| 118 |
|
| 119 |
pattern node⁻ k₁ k₂ t₁ t₂ bal t₃ = node k₁ (node k₂ t₁ t₂ bal) t₃ ∼- |
| 120 |
|
| 121 |
joinʳ⁺ : ∀ {l u hˡ hʳ h} → |
| 122 |
(k : K& V) → |
| 123 |
Tree V l [ k .key ] hˡ → |
| 124 |
(∃ λ i → Tree V [ k .key ] u (i ⊕ hʳ)) → |
| 125 |
(bal : hˡ ∼ hʳ ⊔ h) → |
| 126 |
∃ λ i → Tree V l u (i ⊕ (1 + h)) |
| 127 |
joinʳ⁺ k₂ t₁ (0# , t₃) bal = (0# , node k₂ t₁ t₃ bal) |
| 128 |
joinʳ⁺ k₂ t₁ (1# , t₃) ∼0 = (1# , node k₂ t₁ t₃ ∼+) |
| 129 |
joinʳ⁺ k₂ t₁ (1# , t₃) ∼- = (0# , node k₂ t₁ t₃ ∼0) |
| 130 |
joinʳ⁺ k₂ t₁ (1# , node k₄ t₃ t₅ ∼+) ∼+ = (0# , node k₄ (node k₂ t₁ t₃ ∼0) t₅ ∼0) |
| 131 |
joinʳ⁺ k₂ t₁ (1# , node k₄ t₃ t₅ ∼0) ∼+ = (1# , node k₄ (node k₂ t₁ t₃ ∼+) t₅ ∼-) |
| 132 |
joinʳ⁺ k₂ t₁ (1# , node⁻ k₆ k₄ t₃ t₅ bal t₇) ∼+ |
| 133 |
= (0# , node k₄ (node k₂ t₁ t₃ (max∼ bal)) |
| 134 |
(node k₆ t₅ t₇ (∼max bal)) |
| 135 |
∼0) |
| 136 |
|
| 137 |
joinˡ⁻ : ∀ {l u} hˡ {hʳ h} → |
| 138 |
(k : K& V) → |
| 139 |
(∃ λ i → Tree V l [ k .key ] pred[ i ⊕ hˡ ]) → |
| 140 |
Tree V [ k .key ] u hʳ → |
| 141 |
(bal : hˡ ∼ hʳ ⊔ h) → |
| 142 |
∃ λ i → Tree V l u (i ⊕ h) |
| 143 |
joinˡ⁻ zero k₂ (0# , t₁) t₃ bal = (1# , node k₂ t₁ t₃ bal) |
| 144 |
joinˡ⁻ zero k₂ (1# , t₁) t₃ bal = (1# , node k₂ t₁ t₃ bal) |
| 145 |
joinˡ⁻ (suc _) k₂ (0# , t₁) t₃ ∼+ = joinʳ⁺ k₂ t₁ (1# , t₃) ∼+ |
| 146 |
joinˡ⁻ (suc _) k₂ (0# , t₁) t₃ ∼0 = (1# , node k₂ t₁ t₃ ∼+) |
| 147 |
joinˡ⁻ (suc _) k₂ (0# , t₁) t₃ ∼- = (0# , node k₂ t₁ t₃ ∼0) |
| 148 |
joinˡ⁻ (suc _) k₂ (1# , t₁) t₃ bal = (1# , node k₂ t₁ t₃ bal) |
| 149 |
|
| 150 |
joinʳ⁻ : ∀ {l u hˡ} hʳ {h} → |
| 151 |
(k : K& V) → |
| 152 |
Tree V l [ k .key ] hˡ → |
| 153 |
(∃ λ i → Tree V [ k .key ] u pred[ i ⊕ hʳ ]) → |
| 154 |
(bal : hˡ ∼ hʳ ⊔ h) → |
| 155 |
∃ λ i → Tree V l u (i ⊕ h) |
| 156 |
joinʳ⁻ zero k₂ t₁ (0# , t₃) bal = (1# , node k₂ t₁ t₃ bal) |
| 157 |
joinʳ⁻ zero k₂ t₁ (1# , t₃) bal = (1# , node k₂ t₁ t₃ bal) |
| 158 |
joinʳ⁻ (suc _) k₂ t₁ (0# , t₃) ∼- = joinˡ⁺ k₂ (1# , t₁) t₃ ∼- |
| 159 |
joinʳ⁻ (suc _) k₂ t₁ (0# , t₃) ∼0 = (1# , node k₂ t₁ t₃ ∼-) |
| 160 |
joinʳ⁻ (suc _) k₂ t₁ (0# , t₃) ∼+ = (0# , node k₂ t₁ t₃ ∼0) |
| 161 |
joinʳ⁻ (suc _) k₂ t₁ (1# , t₃) bal = (1# , node k₂ t₁ t₃ bal) |
| 162 |
|
| 163 |
-- Extracts the smallest element from the tree, plus the rest. |
| 164 |
-- Logarithmic in the size of the tree. |
| 165 |
|
| 166 |
headTail : ∀ {l u h} → Tree V l u (1 + h) → |
| 167 |
∃ λ (k : K& V) → l <⁺ [ k .key ] × |
| 168 |
∃ λ i → Tree V [ k .key ] u (i ⊕ h) |
| 169 |
headTail (node k₁ (leaf l<k₁) t₂ ∼+) = (k₁ , l<k₁ , 0# , t₂) |
| 170 |
headTail (node k₁ (leaf l<k₁) t₂ ∼0) = (k₁ , l<k₁ , 0# , t₂) |
| 171 |
headTail (node {hˡ = suc _} k₃ t₁₂ t₄ bal) with headTail t₁₂ |
| 172 |
... | (k₁ , l<k₁ , t₂) = (k₁ , l<k₁ , joinˡ⁻ _ k₃ t₂ t₄ bal) |
| 173 |
|
| 174 |
-- Extracts the largest element from the tree, plus the rest. |
| 175 |
-- Logarithmic in the size of the tree. |
| 176 |
|
| 177 |
initLast : ∀ {l u h} → Tree V l u (1 + h) → |
| 178 |
∃ λ (k : K& V) → [ k .key ] <⁺ u × |
| 179 |
∃ λ i → Tree V l [ k .key ] (i ⊕ h) |
| 180 |
initLast (node k₂ t₁ (leaf k₂<u) ∼-) = (k₂ , k₂<u , (0# , t₁)) |
| 181 |
initLast (node k₂ t₁ (leaf k₂<u) ∼0) = (k₂ , k₂<u , (0# , t₁)) |
| 182 |
initLast (node {hʳ = suc _} k₂ t₁ t₃₄ bal) with initLast t₃₄ |
| 183 |
... | (k₄ , k₄<u , t₃) = (k₄ , k₄<u , joinʳ⁻ _ k₂ t₁ t₃ bal) |
| 184 |
|
| 185 |
-- Another joining function. Logarithmic in the size of either of |
| 186 |
-- the input trees (which need to have almost equal heights). |
| 187 |
|
| 188 |
join : ∀ {l m u hˡ hʳ h} → |
| 189 |
Tree V l m hˡ → Tree V m u hʳ → (bal : hˡ ∼ hʳ ⊔ h) → |
| 190 |
∃ λ i → Tree V l u (i ⊕ h) |
| 191 |
join t₁ (leaf m<u) ∼0 = (0# , castʳ t₁ m<u) |
| 192 |
join t₁ (leaf m<u) ∼- = (0# , castʳ t₁ m<u) |
| 193 |
join {hʳ = suc _} t₁ t₂₃ bal with headTail t₂₃ |
| 194 |
... | (k₂ , m<k₂ , t₃) = joinʳ⁻ _ k₂ (castʳ t₁ m<k₂) t₃ bal |
| 195 |
|
| 196 |
-- An empty tree. |
| 197 |
|
| 198 |
empty : ∀ {l u} → l <⁺ u → Tree V l u 0 |
| 199 |
empty = leaf |
| 200 |
|
| 201 |
-- A singleton tree. |
| 202 |
|
| 203 |
singleton : ∀ {l u} (k : Key) → Val k → l < k < u → Tree V l u 1 |
| 204 |
singleton k v (l<k , k<u) = node (k , v) (leaf l<k) (leaf k<u) ∼0 |
| 205 |
|
| 206 |
-- Inserts a key into the tree, using a function to combine any |
| 207 |
-- existing value with the new value. Logarithmic in the size of the |
| 208 |
-- tree (assuming constant-time comparisons and a constant-time |
| 209 |
-- combining function). |
| 210 |
|
| 211 |
insertWith : ∀ {l u h} (k : Key) → (Maybe (Val k) → Val k) → -- Maybe old → result. |
| 212 |
Tree V l u h → l < k < u → |
| 213 |
∃ λ i → Tree V l u (i ⊕ h) |
| 214 |
insertWith k f (leaf l<u) l<k<u = (1# , singleton k (f nothing) l<k<u) |
| 215 |
insertWith k f (node (k′ , v′) lp pu bal) (l<k , k<u) with compare k k′ |
| 216 |
... | tri< k<k′ _ _ = joinˡ⁺ (k′ , v′) (insertWith k f lp (l<k , [ k<k′ ]ᴿ)) pu bal |
| 217 |
... | tri> _ _ k′<k = joinʳ⁺ (k′ , v′) lp (insertWith k f pu ([ k′<k ]ᴿ , k<u)) bal |
| 218 |
... | tri≈ _ k≈k′ _ = (0# , node (k′ , V≈ k≈k′ (f (just (V≈ (Eq.sym k≈k′) v′)))) lp pu bal) |
| 219 |
|
| 220 |
-- Inserts a key into the tree. If the key already exists, then it |
| 221 |
-- is replaced. Logarithmic in the size of the tree (assuming |
| 222 |
-- constant-time comparisons). |
| 223 |
|
| 224 |
insert : ∀ {l u h} → (k : Key) → Val k → Tree V l u h → l < k < u → |
| 225 |
∃ λ i → Tree V l u (i ⊕ h) |
| 226 |
insert k v = insertWith k (F.const v) |
| 227 |
|
| 228 |
-- Deletes the key/value pair containing the given key, if any. |
| 229 |
-- Logarithmic in the size of the tree (assuming constant-time |
| 230 |
-- comparisons). |
| 231 |
|
| 232 |
delete : ∀ {l u h} (k : Key) → Tree V l u h → l < k < u → |
| 233 |
∃ λ i → Tree V l u pred[ i ⊕ h ] |
| 234 |
delete k (leaf l<u) l<k<u = (0# , leaf l<u) |
| 235 |
delete k (node p@(k′ , v) lp pu bal) (l<k , k<u) with compare k′ k |
| 236 |
... | tri< k′<k _ _ = joinʳ⁻ _ p lp (delete k pu ([ k′<k ]ᴿ , k<u)) bal |
| 237 |
... | tri> _ _ k′>k = joinˡ⁻ _ p (delete k lp (l<k , [ k′>k ]ᴿ)) pu bal |
| 238 |
... | tri≈ _ k′≡k _ = join lp pu bal |
| 239 |
|
| 240 |
-- Looks up a key. Logarithmic in the size of the tree (assuming |
| 241 |
-- constant-time comparisons). |
| 242 |
|
| 243 |
lookup : ∀ {l u h} → Tree V l u h → (k : Key) → l < k < u → Maybe (Val k) |
| 244 |
lookup (leaf _) k l<k<u = nothing |
| 245 |
lookup (node (k′ , v) lk′ k′u _) k (l<k , k<u) with compare k′ k |
| 246 |
... | tri< k′<k _ _ = lookup k′u k ([ k′<k ]ᴿ , k<u) |
| 247 |
... | tri> _ _ k′>k = lookup lk′ k (l<k , [ k′>k ]ᴿ) |
| 248 |
... | tri≈ _ k′≡k _ = just (V≈ k′≡k v) |
| 249 |
|
| 250 |
-- Converts the tree to an ordered list. Linear in the size of the |
| 251 |
-- tree. |
| 252 |
|
| 253 |
foldr : ∀ {l u h} → (∀ {k} → Val k → A → A) → A → Tree V l u h → A |
| 254 |
foldr cons nil (leaf l<u) = nil |
| 255 |
foldr cons nil (node (_ , v) l r bal) = foldr cons (cons v (foldr cons nil r)) l |
| 256 |
|
| 257 |
toDiffList : ∀ {l u h} → Tree V l u h → DiffList (K& V) |
| 258 |
toDiffList (leaf _) = [] |
| 259 |
toDiffList (node k l r _) = toDiffList l ++ k ∷ toDiffList r |
| 260 |
|
| 261 |
toList : ∀ {l u h} → Tree V l u h → List (K& V) |
| 262 |
toList t = toDiffList t List.[] |
| 263 |
|
| 264 |
size : ∀ {l u h} → Tree V l u h → ℕ |
| 265 |
size = List.length ∘′ toList |
| 266 |
|
| 267 |
module _ {v w} {V : Value v} {W : Value w} where |
| 268 |
|
| 269 |
private |
| 270 |
Val = Value.family V |
| 271 |
Wal = Value.family W |
| 272 |
|
| 273 |
-- Maps a function over all values in the tree. |
| 274 |
|
| 275 |
map : ∀[ Val ⇒ Wal ] → ∀ {l u h} → Tree V l u h → Tree W l u h |
| 276 |
map f (leaf l<u) = leaf l<u |
| 277 |
map f (node (k , v) l r bal) = node (k , f v) (map f l) (map f r) bal |