Debian Codegraph @

src/Data/Trie/NonEmpty.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Non empty trie, basic type and operations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --sized-types #-}
8
9 open import Relation.Binary.Bundles using (StrictTotalOrder)
10
11 module Data.Trie.NonEmpty {k e r} (S : StrictTotalOrder k e r) where
12
13 open import Data.Product.Base as Product using (∃; uncurry; -,_)
14 open import Data.List.Base as List using (List; []; _∷_; _++_)
15 open import Data.List.NonEmpty as List⁺ using (List⁺; [_]; concatMap)
16 open import Data.Maybe.Base as Maybe
17 using (Maybe; nothing; just; maybe′) hiding (module Maybe)
18 open import Data.These as These using (These; this; that; these)
19 open import Effect.Monad using (RawMonad)
20 open import Function.Base as F
21 using (const; _∘′_; _$_; id; _∘_; _$′_; case_of_)
22 open import Level using (Level; _⊔_)
23 import Function.Identity.Effectful as Identity
24 open import Relation.Unary using (_⇒_; IUniversal)
25 open import Size using (Size; ↑_; ∞)
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 hiding (Value)
33 open import Data.Tree.AVL.Value ≋-setoid using (Value)
34 open import Data.Tree.AVL.NonEmpty S as Tree⁺ using (Tree⁺)
35 open Value
36
37 ------------------------------------------------------------------------
38 -- Definition
39
40 -- A Trie⁺ is a tree branching over an alphabet of Keys. It stores
41 -- values indexed over the Word (i.e. List Key) that was read to reach
42 -- them. Each node in the Trie⁺ contains either a value, a non-empty
43 -- Tree of sub-Trie⁺ reached by reading an extra letter, or both.
44
45 Word : Set k
46 Word = List Key
47
48 eat : ∀ {v} → Value v → Word → Value v
49 family (eat V ks) = family V ∘′ (ks ++_)
50 respects (eat V ks) = respects V ∘′ ++⁺ ≋-refl
51
52 data Trie⁺ {v} (V : Value v) : Size → Set (v ⊔ k ⊔ e ⊔ r)
53 Tries⁺ : ∀ {v} (V : Value v) (i : Size) → Set (v ⊔ k ⊔ e ⊔ r)
54
55 map : ∀ {v w} (V : Value v) (W : Value w) {i} →
56 ∀[ family V ⇒ family W ] →
57 Trie⁺ V i → Trie⁺ W i
58
59 data Trie⁺ V where
60 node : ∀ {i} → These (family V []) (Tries⁺ V i) → Trie⁺ V (↑ i)
61
62 Tries⁺ V j = Tree⁺ $ MkValue (λ k → Trie⁺ (eat V (k ∷ [])) j)
63 $ λ eq → map _ _ (respects V (eq ∷ ≋-refl))
64
65 map V W f (node t) = node $ These.map f (Tree⁺.map (map _ _ f)) t
66
67 ------------------------------------------------------------------------
68 -- Query
69
70 lookup : ∀ {v} {V : Value v} → Trie⁺ V ∞ → ∀ ks →
71 Maybe (These (family V ks) (Tries⁺ (eat V ks) ∞))
72 lookup (node nd) [] = just (These.map₂ (Tree⁺.map id) nd)
73 lookup (node nd) (k ∷ ks) = let open Maybe in do
74 ts ← These.fromThat nd
75 t ← Tree⁺.lookup ts k
76 lookup t ks
77
78 module _ {v} {V : Value v} where
79
80 lookupValue : Trie⁺ V ∞ → (ks : Word) → Maybe (family V ks)
81 lookupValue t ks = lookup t ks Maybe.>>= These.fromThis
82
83 lookupTries⁺ : Trie⁺ V ∞ → ∀ ks → Maybe (Tries⁺ (eat V ks) ∞)
84 lookupTries⁺ t ks = lookup t ks Maybe.>>= These.fromThat
85
86 lookupTrie⁺ : Trie⁺ V ∞ → ∀ k → Maybe (Trie⁺ (eat V (k ∷ [])) ∞)
87 lookupTrie⁺ t k = lookupTries⁺ t [] Maybe.>>= λ ts → Tree⁺.lookup ts k
88
89 ------------------------------------------------------------------------
90 -- Construction
91
92 singleton : ∀ {v} {V : Value v} ks → family V ks → Trie⁺ V ∞
93 singleton [] v = node (this v)
94 singleton (k ∷ ks) v = node (that (Tree⁺.singleton k (singleton ks v)))
95
96 insertWith : ∀ {v} {V : Value v} ks → (Maybe (family V ks) → family V ks) →
97 Trie⁺ V ∞ → Trie⁺ V ∞
98 insertWith [] f (node nd) =
99 node (These.fold (this ∘ f ∘ just) (these (f nothing)) (these ∘ f ∘ just) nd)
100 insertWith {v} {V} (k ∷ ks) f (node {j} nd) = node $
101 These.fold (λ v → these v (Tree⁺.singleton k end))
102 (that ∘′ rec)
103 (λ v → these v ∘′ rec)
104 nd
105 where
106
107 end : Trie⁺ (eat V (k ∷ [])) ∞
108 end = singleton ks (f nothing)
109
110 rec : Tries⁺ V ∞ → Tries⁺ V ∞
111 rec = Tree⁺.insertWith k (maybe′ (insertWith ks f) end)
112
113 module _ {v} {V : Value v} where
114
115 private Val = family V
116
117 insert : ∀ ks → Val ks → Trie⁺ V ∞ → Trie⁺ V ∞
118 insert ks = insertWith ks ∘′ F.const
119
120 fromList⁺ : List⁺ (∃ Val) → Trie⁺ V ∞
121 fromList⁺ = List⁺.foldr (uncurry insert) (uncurry singleton)
122
123 toList⁺ : ∀ {v} {V : Value v} {i} → let Val = Value.family V in
124 Trie⁺ V i → List⁺ (∃ Val)
125 toList⁺ (node nd) = These.mergeThese List⁺._⁺++⁺_
126 $ These.map fromVal fromTries⁺ nd
127 where
128
129 fromVal = [_] ∘′ -,_
130 fromTrie⁺ = λ (k , v) → List⁺.map (Product.map (k ∷_) id) (toList⁺ v)
131 fromTries⁺ = concatMap fromTrie⁺ ∘′ Tree⁺.toList⁺
132
133 ------------------------------------------------------------------------
134 -- Modification
135
136 -- Deletion
137
138 deleteWith : ∀ {v} {V : Value v} {i} ks →
139 (∀ {i} → Trie⁺ (eat V ks) i → Maybe (Trie⁺ (eat V ks) i)) →
140 Trie⁺ V i → Maybe (Trie⁺ V i)
141 deleteWith [] f t = f t
142 deleteWith (k ∷ ks) f t@(node nd) = let open RawMonad Identity.monad in do
143 just ts ← These.fromThat nd where _ → just t
144 -- This would be a lot cleaner if we had
145 -- Tree⁺.updateWith : ∀ k → (Maybe (V k) → Maybe (V k)) → AVL → AVL
146 -- Instead we lookup the subtree, update it and either put it back in
147 -- or delete the corresponding leaf depending on whether the result is successful.
148 just t′ ← Tree⁺.lookup ts k where _ → just t
149 Maybe.map node ∘′ Maybe.align (These.fromThis nd) $′ case deleteWith ks f t′ of λ where
150 nothing → Tree⁺.delete k ts
151 (just u) → just (Tree⁺.insert k u ts)
152
153 module _ {v} {V : Value v} where
154
155 deleteTrie⁺ : ∀ {i} (ks : Word) → Trie⁺ V i → Maybe (Trie⁺ V i)
156 deleteTrie⁺ ks = deleteWith ks (F.const nothing)
157
158 deleteValue : ∀ {i} (ks : Word) → Trie⁺ V i → Maybe (Trie⁺ V i)
159 deleteValue ks = deleteWith ks $ λ where
160 (node nd) → Maybe.map node $′ These.deleteThis nd
161
162 deleteTries⁺ : ∀ {i} (ks : Word) → Trie⁺ V i → Maybe (Trie⁺ V i)
163 deleteTries⁺ ks = deleteWith ks $ λ where
164 (node nd) → Maybe.map node $′ These.deleteThat nd