~ubuntu-branches/ubuntu/utopic/agda-stdlib/utopic-proposed

« back to all changes in this revision

Viewing changes to README/AVL.agda

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2011-11-29 17:00:35 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20111129170035-00v3pq4mmhoo5ulf
Tags: 0.6~darcs20111129t1640-1
* [ef445ab] Imported Upstream version 0.6~darcs20111129t1640
  + Darcs snapshot required for Agda 2.3.0 compatibility
* [f801f83] Update BDs and deps to require Agda 2.3.0
* [c52be90] Use 3.0 (quilt) for bz2 orig

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
------------------------------------------------------------------------
 
2
-- The Agda standard library
 
3
--
 
4
-- Some examples showing how the AVL tree module can be used
 
5
------------------------------------------------------------------------
 
6
 
 
7
module README.AVL where
 
8
 
 
9
------------------------------------------------------------------------
 
10
-- Setup
 
11
 
 
12
-- AVL trees are defined in Data.AVL.
 
13
 
 
14
import Data.AVL
 
15
 
 
16
-- This module is parametrised by keys, which have to form a (strict)
 
17
-- total order, and values, which are indexed by keys. Let us use
 
18
-- natural numbers as keys and vectors of strings as values.
 
19
 
 
20
import Data.Nat.Properties as ℕ
 
21
open import Data.String using (String)
 
22
open import Data.Vec using (Vec; _∷_; [])
 
23
open import Relation.Binary using (module StrictTotalOrder)
 
24
 
 
25
open Data.AVL (Vec String)
 
26
              (StrictTotalOrder.isStrictTotalOrder ℕ.strictTotalOrder)
 
27
 
 
28
------------------------------------------------------------------------
 
29
-- Construction of trees
 
30
 
 
31
-- Some values.
 
32
 
 
33
v₁ = "cepa" ∷ []
 
34
v₂ = "apa" ∷ "bepa" ∷ []
 
35
 
 
36
-- Empty and singleton trees.
 
37
 
 
38
t₀ : Tree
 
39
t₀ = empty
 
40
 
 
41
t₁ : Tree
 
42
t₁ = singleton 2 v₂
 
43
 
 
44
-- Insertion of a key-value pair into a tree.
 
45
 
 
46
t₂ = insert 1 v₁ t₁
 
47
 
 
48
-- Deletion of the mapping for a certain key.
 
49
 
 
50
t₃ = delete 2 t₂
 
51
 
 
52
-- Conversion of a list of key-value mappings to a tree.
 
53
 
 
54
open import Data.List using (_∷_; [])
 
55
open import Data.Product as Prod using (_,_; _,′_)
 
56
 
 
57
t₄ = fromList ((2 , v₂) ∷ (1 , v₁) ∷ [])
 
58
 
 
59
------------------------------------------------------------------------
 
60
-- Queries
 
61
 
 
62
-- Let us formulate queries as unit tests.
 
63
 
 
64
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
 
65
 
 
66
-- Searching for a key.
 
67
 
 
68
open import Data.Bool using (true; false)
 
69
open import Data.Maybe as Maybe using (just; nothing)
 
70
 
 
71
q₀ : lookup 2 t₂ ≡ just v₂
 
72
q₀ = refl
 
73
 
 
74
q₁ : lookup 2 t₃ ≡ nothing
 
75
q₁ = refl
 
76
 
 
77
q₂ : 3 ∈? t₂ ≡ false
 
78
q₂ = refl
 
79
 
 
80
q₃ : 1 ∈? t₄ ≡ true
 
81
q₃ = refl
 
82
 
 
83
-- Turning a tree into a sorted list of key-value pairs.
 
84
 
 
85
q₄ : toList t₁ ≡ (2 , v₂) ∷ []
 
86
q₄ = refl
 
87
 
 
88
q₅ : toList t₂ ≡ (1 , v₁) ∷ (2 , v₂) ∷ []
 
89
q₅ = refl
 
90
 
 
91
------------------------------------------------------------------------
 
92
-- Views
 
93
 
 
94
-- Partitioning a tree into the smallest element plus the rest, or the
 
95
-- largest element plus the rest.
 
96
 
 
97
open import Category.Functor using (module RawFunctor)
 
98
open import Function using (id)
 
99
import Level
 
100
 
 
101
open RawFunctor (Maybe.functor {f = Level.zero}) using (_<$>_)
 
102
 
 
103
v₆ : headTail t₀ ≡ nothing
 
104
v₆ = refl
 
105
 
 
106
v₇ : Prod.map id toList <$> headTail t₂ ≡
 
107
     just ((1 , v₁) , ((2 , v₂) ∷ []))
 
108
v₇ = refl
 
109
 
 
110
v₈ : initLast t₀ ≡ nothing
 
111
v₈ = refl
 
112
 
 
113
v₉ : Prod.map toList id <$> initLast t₄ ≡
 
114
     just (((1 , v₁) ∷ []) ,′ (2 , v₂))
 
115
v₉ = refl
 
116
 
 
117
------------------------------------------------------------------------
 
118
-- Further reading
 
119
 
 
120
-- Variations of the AVL tree module are available:
 
121
 
 
122
-- • Finite maps with indexed keys and values.
 
123
 
 
124
import Data.AVL.IndexedMap
 
125
 
 
126
-- • Finite sets.
 
127
 
 
128
import Data.AVL.Sets