| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Trie, basic type and operations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- See README.Data.Trie.NonDependent for an example of using a trie to |
| 8 |
-- build a lexer. |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --sized-types #-} |
| 11 |
|
| 12 |
open import Relation.Binary.Core using (Rel) |
| 13 |
open import Relation.Binary.Bundles using (StrictTotalOrder) |
| 14 |
|
| 15 |
module Data.Trie {k e r} (S : StrictTotalOrder k e r) where |
| 16 |
|
| 17 |
open import Level |
| 18 |
open import Size |
| 19 |
open import Data.List.Base using (List; []; _∷_; _++_) |
| 20 |
import Data.List.NonEmpty as List⁺ |
| 21 |
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing; maybe′) |
| 22 |
open import Data.Product.Base using (∃) |
| 23 |
open import Data.These.Base as These using (These) |
| 24 |
open import Function.Base using (_∘′_; const) |
| 25 |
open import Relation.Unary using (IUniversal; _⇒_) |
| 26 |
|
| 27 |
open StrictTotalOrder S |
| 28 |
using (module Eq) |
| 29 |
renaming (Carrier to Key) |
| 30 |
|
| 31 |
open import Data.List.Relation.Binary.Equality.Setoid Eq.setoid |
| 32 |
open import Data.Tree.AVL.Value ≋-setoid using (Value) |
| 33 |
|
| 34 |
------------------------------------------------------------------------ |
| 35 |
-- Definition |
| 36 |
|
| 37 |
-- Trie is defined in terms of Trie⁺, the type of non-empty trie. This |
| 38 |
-- guarantees that the trie is minimal: each path in the tree leads to |
| 39 |
-- either a value or a number of non-empty sub-tries. |
| 40 |
|
| 41 |
open import Data.Trie.NonEmpty S as Trie⁺ public |
| 42 |
using (Trie⁺; Tries⁺; Word; eat) |
| 43 |
|
| 44 |
Trie : ∀ {v} (V : Value v) → Size → Set (v ⊔ k ⊔ e ⊔ r) |
| 45 |
Trie V i = Maybe (Trie⁺ V i) |
| 46 |
|
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- Operations |
| 49 |
|
| 50 |
-- Functions acting on Trie are wrappers for functions acting on Tries. |
| 51 |
-- Sometimes the empty case is handled in a special way (e.g. insertWith |
| 52 |
-- calls singleton when faced with an empty Trie). |
| 53 |
|
| 54 |
module _ {v} {V : Value v} where |
| 55 |
|
| 56 |
private Val = Value.family V |
| 57 |
|
| 58 |
------------------------------------------------------------------------ |
| 59 |
-- Lookup |
| 60 |
|
| 61 |
lookup : Trie V ∞ → ∀ ks → Maybe (These (Val ks) (Tries⁺ (eat V ks) ∞)) |
| 62 |
lookup t ks = t Maybe.>>= λ ts → Trie⁺.lookup ts ks |
| 63 |
|
| 64 |
lookupValue : Trie V ∞ → ∀ ks → Maybe (Val ks) |
| 65 |
lookupValue t ks = t Maybe.>>= λ ts → Trie⁺.lookupValue ts ks |
| 66 |
|
| 67 |
lookupTries⁺ : Trie V ∞ → ∀ ks → Maybe (Tries⁺ (eat V ks) ∞) |
| 68 |
lookupTries⁺ t ks = t Maybe.>>= λ ts → Trie⁺.lookupTries⁺ ts ks |
| 69 |
|
| 70 |
lookupTrie : Trie V ∞ → ∀ k → Trie (eat V (k ∷ [])) ∞ |
| 71 |
lookupTrie t k = t Maybe.>>= λ ts → Trie⁺.lookupTrie⁺ ts k |
| 72 |
|
| 73 |
------------------------------------------------------------------------ |
| 74 |
-- Construction |
| 75 |
|
| 76 |
empty : Trie V ∞ |
| 77 |
empty = nothing |
| 78 |
|
| 79 |
singleton : ∀ ks → Val ks → Trie V ∞ |
| 80 |
singleton ks v = just (Trie⁺.singleton ks v) |
| 81 |
|
| 82 |
insertWith : ∀ ks → (Maybe (Val ks) → Val ks) → Trie V ∞ → Trie V ∞ |
| 83 |
insertWith ks f (just t) = just (Trie⁺.insertWith ks f t) |
| 84 |
insertWith ks f nothing = singleton ks (f nothing) |
| 85 |
|
| 86 |
insert : ∀ ks → Val ks → Trie V ∞ → Trie V ∞ |
| 87 |
insert ks = insertWith ks ∘′ const |
| 88 |
|
| 89 |
fromList : List (∃ Val) → Trie V ∞ |
| 90 |
fromList = Maybe.map Trie⁺.fromList⁺ ∘′ List⁺.fromList |
| 91 |
|
| 92 |
toList : Trie V ∞ → List (∃ Val) |
| 93 |
toList (just t) = List⁺.toList (Trie⁺.toList⁺ t) |
| 94 |
toList nothing = [] |
| 95 |
|
| 96 |
------------------------------------------------------------------------ |
| 97 |
-- Modification |
| 98 |
|
| 99 |
module _ {v w} {V : Value v} {W : Value w} where |
| 100 |
|
| 101 |
private |
| 102 |
Val = Value.family V |
| 103 |
Wal = Value.family W |
| 104 |
|
| 105 |
map : ∀ {i} → ∀[ Val ⇒ Wal ] → Trie V i → Trie W i |
| 106 |
map = Maybe.map ∘′ Trie⁺.map V W |
| 107 |
|
| 108 |
-- Deletion |
| 109 |
|
| 110 |
module _ {v} {V : Value v} where |
| 111 |
|
| 112 |
-- Use a function to decide how to modify the sub-Trie⁺ whose root is |
| 113 |
-- at the end of path ks. |
| 114 |
deleteWith : ∀ {i} (ks : Word) → |
| 115 |
(∀ {i} → Trie⁺ (eat V ks) i → Maybe (Trie⁺ (eat V ks) i)) → |
| 116 |
Trie V i → Trie V i |
| 117 |
deleteWith ks f t = t Maybe.>>= Trie⁺.deleteWith ks f |
| 118 |
|
| 119 |
-- Remove the whole node |
| 120 |
deleteTrie⁺ : ∀ {i} (ks : Word) → Trie V i → Trie V i |
| 121 |
deleteTrie⁺ ks t = t Maybe.>>= Trie⁺.deleteTrie⁺ ks |
| 122 |
|
| 123 |
-- Remove the value and keep the sub-Tries (if any) |
| 124 |
deleteValue : ∀ {i} (ks : Word) → Trie V i → Trie V i |
| 125 |
deleteValue ks t = t Maybe.>>= Trie⁺.deleteValue ks |
| 126 |
|
| 127 |
-- Remove the sub-Tries and keep the value (if any) |
| 128 |
deleteTries⁺ : ∀ {i} (ks : Word) → Trie V i → Trie V i |
| 129 |
deleteTries⁺ ks t = t Maybe.>>= Trie⁺.deleteTries⁺ ks |