Debian Codegraph @

doc/README/Data/Tree/AVL.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Some examples showing how the AVL tree module can be used
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module README.Data.Tree.AVL where
10
11 ------------------------------------------------------------------------
12 -- Setup
13
14 -- AVL trees are defined in Data.Tree.AVL.
15
16 import Data.Tree.AVL
17
18 -- This module is parametrised by keys, which have to form a (strict)
19 -- total order, and values, which are indexed by keys. Let us use
20 -- natural numbers as keys and vectors of strings as values.
21
22 open import Data.Nat.Properties using (<-strictTotalOrder)
23 open import Data.Product.Base as Product using (_,_; _,′_)
24 open import Data.String.Base using (String)
25 open import Data.Vec.Base using (Vec; _∷_; [])
26 open import Relation.Binary.PropositionalEquality
27
28 open Data.Tree.AVL <-strictTotalOrder renaming (Tree to Tree′)
29 Tree = Tree′ (MkValue (Vec String) (subst (Vec String)))
30
31 -- The first argument to `MkValue` should be a function from a key
32 -- (a `ℕ` in this case) to a value type (a `String` vector type).
33 -- Since `(Vec String)` is missing `Vec`'s second parameter, a `ℕ`,
34 -- it's equivalent to `λ n → Vec String n`.
35
36 -- The second argument to `MkValue` is a function that can accept
37 -- a proof that two keys are equal, and then substitute a unique key
38 -- for an equivalent key that is passed in. Applying `subst` to the
39 -- key-to-value-type function passed as `MkValue`'s first argument is
40 -- a normal way to create such a function.
41
42 -- Note that there is no need to define the type of keys separately:
43 -- passing a key-to-value-type function to `MkValue` and providing
44 -- the result of `MkValue` to `Data.Tree.AVL.Tree` is enough.
45
46 ------------------------------------------------------------------------
47 -- Construction of trees
48
49 -- Some values.
50
51 v₁ = "cepa" ∷ []
52 v₁′ = "depa" ∷ []
53 v₂ = "apa" ∷ "bepa" ∷ []
54
55 -- Empty and singleton trees.
56
57 t₀ : Tree
58 t₀ = empty
59
60 t₁ : Tree
61 t₁ = singleton 2 v₂
62
63 -- Insertion of a key-value pair into a tree.
64
65 t₂ = insert 1 v₁ t₁
66
67 -- If you insert a key-value pair and the key already exists in the
68 -- tree, then the old value is thrown away.
69
70 t₂′ = insert 1 v₁′ t₂
71
72 -- Deletion of the mapping for a certain key.
73
74 t₃ = delete 2 t₂
75
76 -- Conversion of a list of key-value mappings to a tree.
77
78 open import Data.List.Base using (_∷_; [])
79
80 t₄ : Tree
81 t₄ = fromList ((2 , v₂) ∷ (1 , v₁) ∷ [])
82
83 ------------------------------------------------------------------------
84 -- Queries
85
86 -- Let us formulate queries as unit tests.
87
88 open import Relation.Binary.PropositionalEquality using (_≡_; refl)
89
90 -- Searching for a key.
91
92 open import Data.Bool.Base using (true; false)
93 open import Data.Maybe.Base as Maybe using (just; nothing)
94
95 q₀ : lookup t₂ 2 ≡ just v₂
96 q₀ = refl
97
98 q₁ : lookup t₃ 2 ≡ nothing
99 q₁ = refl
100
101 q₂ : (3 ∈? t₂) ≡ false
102 q₂ = refl
103
104 q₃ : (1 ∈? t₄) ≡ true
105 q₃ = refl
106
107 -- Turning a tree into a sorted list of key-value pairs.
108
109 q₄ : toList t₁ ≡ (2 , v₂) ∷ []
110 q₄ = refl
111
112 q₅ : toList t₂ ≡ (1 , v₁) ∷ (2 , v₂) ∷ []
113 q₅ = refl
114
115 q₅′ : toList t₂′ ≡ (1 , v₁′) ∷ (2 , v₂) ∷ []
116 q₅′ = refl
117
118 ------------------------------------------------------------------------
119 -- Views
120
121 -- Partitioning a tree into the smallest element plus the rest, or the
122 -- largest element plus the rest.
123
124 open import Function.Base using (id)
125
126 v₆ : headTail t₀ ≡ nothing
127 v₆ = refl
128
129 v₇ : Maybe.map (Product.map₂ toList) (headTail t₂) ≡
130 just ((1 , v₁) , ((2 , v₂) ∷ []))
131 v₇ = refl
132
133 v₈ : initLast t₀ ≡ nothing
134 v₈ = refl
135
136 v₉ : Maybe.map (Product.map₁ toList) (initLast t₄) ≡
137 just (((1 , v₁) ∷ []) ,′ (2 , v₂))
138 v₉ = refl
139
140 ------------------------------------------------------------------------
141 -- Further reading
142
143 -- Variations of the AVL tree module are available:
144
145 -- • Finite maps in which types of values don't depend on keys.
146
147 import Data.Tree.AVL.Map
148
149 -- • Finite maps with indexed keys and values.
150
151 import Data.Tree.AVL.IndexedMap
152
153 -- • Finite sets.
154
155 import Data.Tree.AVL.Sets