Skip to content

Commit 2f8402f

Browse files
authored
Various AVL goodies (#1371)
1 parent 1c9399b commit 2f8402f

File tree

6 files changed

+229
-64
lines changed

6 files changed

+229
-64
lines changed

CHANGELOG.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,49 @@ Other minor additions
485485
linesBy : Decidable P → String → List String
486486
lines : String → List String
487487
```
488+
489+
* Added new functions to `Data.Tree.AVL`:
490+
```agda
491+
foldr : (∀ {k} → Val k → A → A) → A → Tree V → A
492+
size : Tree V → ℕ
493+
494+
intersectionWith : (∀ {k} → Val k → Wal k → Xal k) → Tree V → Tree W → Tree X
495+
intersection : Tree V → Tree V → Tree V
496+
intersectionsWith : (∀ {k} → Val k → Val k → Val k) → List (Tree V) → Tree V
497+
intersections : List (Tree V) → Tree V
498+
```
499+
500+
* Added new functions to `Data.Tree.AVL.Indexed`:
501+
```agda
502+
foldr : (∀ {k} → Val k → A → A) → A → Tree V l u h → A
503+
size : Tree V → ℕ
504+
```
505+
506+
* Added new functions to `Data.Tree.AVL.IndexedMap` module:
507+
```agda
508+
foldr : (∀ {k} → Value k → A → A) → A → Map → A
509+
size : Map → ℕ
510+
```
511+
512+
* Added new functions to `Data.Tree.AVL.Map`:
513+
```agda
514+
foldr : (Key → V → A → A) → A → Map V → A
515+
size : Map V → ℕ
516+
517+
intersectionWith : (V → W → X) → Map V → Map W → Map X
518+
intersection : Map V → Map V → Map V
519+
intersectionsWith : (V → V → V) → List (Map V) → Map V
520+
intersections : List (Map V) → Map V
521+
```
522+
523+
* Added new functions to `Data.Tree.AVL.Sets`:
524+
```agda
525+
foldr : (A → B → B) → B → ⟨Set⟩ → B
526+
size : ⟨Set⟩ → ℕ
527+
528+
union : ⟨Set⟩ → ⟨Set⟩ → ⟨Set⟩
529+
unions : List ⟨Set⟩ → ⟨Set⟩
530+
531+
intersection : ⟨Set⟩ → ⟨Set⟩ → ⟨Set⟩
532+
intersections : List ⟨Set⟩ → ⟨Set⟩
533+
```

src/Data/Tree/AVL.agda

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ module Data.Tree.AVL
2121

2222
open import Data.Bool.Base using (Bool)
2323
import Data.DifferenceList as DiffList
24-
open import Data.List.Base as List using (List)
24+
open import Data.List.Base as List using (List; []; _∷_)
2525
open import Data.Maybe.Base using (Maybe; nothing; just; is-just)
26-
open import Data.Nat.Base using (suc)
26+
open import Data.Nat.Base using (ℕ; suc)
2727
open import Data.Product hiding (map)
28-
open import Function as F
29-
open import Level using (_⊔_)
30-
open import Relation.Unary
28+
open import Function.Base as F
29+
open import Level using (Level; _⊔_)
30+
open import Relation.Unary using (IUniversal; _⇒_)
31+
32+
private
33+
variable
34+
l : Level
35+
A : Set l
3136

3237
open StrictTotalOrder strictTotalOrder renaming (Carrier to Key)
3338
import Data.Tree.AVL.Indexed strictTotalOrder as Indexed
@@ -84,7 +89,6 @@ module _ {v} {V : Value v} where
8489

8590

8691
infix 4 _∈?_
87-
8892
_∈?_ : Key Tree V Bool
8993
k ∈? t = is-just (lookup k t)
9094

@@ -98,6 +102,9 @@ module _ {v} {V : Value v} where
98102
initLast (tree {h = suc _} t) with Indexed.initLast t
99103
... | (k , _ , _ , t′) = just (tree (Indexed.castʳ t′ [ _ ]<⊤⁺) , k)
100104

105+
foldr : ( {k} Val k A A) A Tree V A
106+
foldr cons nil (tree t) = Indexed.foldr cons nil t
107+
101108
-- The input does not need to be ordered.
102109

103110
fromList : List (K& V) Tree V
@@ -108,32 +115,71 @@ module _ {v} {V : Value v} where
108115
toList : Tree V List (K& V)
109116
toList (tree t) = DiffList.toList (Indexed.toDiffList t)
110117

111-
-- Naive implementations of union.
118+
size : Tree V
119+
size (tree t) = Indexed.size t
120+
121+
------------------------------------------------------------------------
122+
-- Naive implementation of union
112123

113124
module _ {v w} {V : Value v} {W : Value w} where
114125
private
115126
Val = Value.family V
116127
Wal = Value.family W
117128

118129
unionWith : ( {k} Val k Maybe (Wal k) Wal k)
119-
-- Left → right → result.
130+
-- left → right → result.
120131
Tree V Tree W Tree W
121-
unionWith f t₁ t₂ =
122-
List.foldr (uncurry $ λ k v insertWith k (f v)) t₂ (toList t₁)
123-
124-
-- Left-biased.
132+
unionWith f t₁ t₂ = foldr (λ {k} v insertWith k (f v)) t₂ t₁
125133

126134
module _ {v} {V : Value v} where
127135

128136
private Val = Value.family V
129137

138+
-- Left-biased.
130139
union : Tree V Tree V Tree V
131140
union = unionWith F.const
132141

133142
unionsWith : ( {k} Val k Maybe (Val k) Val k) List (Tree V) Tree V
134143
unionsWith f ts = List.foldr (unionWith f) empty ts
135144

136145
-- Left-biased.
137-
138146
unions : List (Tree V) Tree V
139147
unions = unionsWith F.const
148+
149+
------------------------------------------------------------------------
150+
-- Naive implementation of intersection
151+
152+
module _ {v w x} {V : Value v} {W : Value w} {X : Value x} where
153+
private
154+
Val = Value.family V
155+
Wal = Value.family W
156+
Xal = Value.family X
157+
158+
intersectionWith : ( {k} Val k Wal k Xal k)
159+
Tree V Tree W Tree X
160+
intersectionWith f t₁ t₂ = foldr cons empty t₁ where
161+
162+
cons : {k} Val k Tree X Tree X
163+
cons {k} v = case lookup k t₂ of λ where
164+
nothing id
165+
(just w) insert k (f v w)
166+
167+
module _ {v} {V : Value v} where
168+
private
169+
Val = Value.family V
170+
171+
-- Left-biased.
172+
intersection : Tree V Tree V Tree V
173+
intersection = intersectionWith F.const
174+
175+
intersectionsWith : ( {k} Val k Val k Val k)
176+
List (Tree V) Tree V
177+
intersectionsWith f [] = empty
178+
intersectionsWith f (t ∷ ts) = List.foldl (intersectionWith f) t ts
179+
-- We are using foldl so that we are indeed forming t₁ ∩ ⋯ ∩ tₙ for
180+
-- the input list [t₁,⋯,tₙ]. If we were to use foldr, we would form
181+
-- t₂ ∩ ⋯ ∩ tₙ ∩ t₁ instead!
182+
183+
-- Left-biased.
184+
intersections : List (Tree V) Tree V
185+
intersections = intersectionsWith F.const

src/Data/Tree/AVL/Indexed.agda

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ open import Relation.Binary using (StrictTotalOrder)
1111
module Data.Tree.AVL.Indexed
1212
{a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂) where
1313

14-
open import Level using (_⊔_)
14+
open import Level using (Level; _⊔_)
1515
open import Data.Nat.Base using (ℕ; zero; suc; _+_)
1616
open import Data.Product using (Σ; ∃; _×_; _,_; proj₁)
1717
open import Data.Maybe.Base using (Maybe; just; nothing)
@@ -22,6 +22,11 @@ open import Relation.Unary
2222
open import Relation.Binary using (_Respects_; Tri; tri<; tri≈; tri>)
2323
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
2424

25+
private
26+
variable
27+
l : Level
28+
A : Set l
29+
2530
open StrictTotalOrder strictTotalOrder renaming (Carrier to Key)
2631

2732
------------------------------------------------------------------------
@@ -244,13 +249,20 @@ module _ {v} {V : Value v} where
244249
-- Converts the tree to an ordered list. Linear in the size of the
245250
-- tree.
246251

252+
foldr : {l u h} ( {k} Val k A A) A Tree V l u h A
253+
foldr cons nil (leaf l<u) = nil
254+
foldr cons nil (node (_ , v) l r bal) = foldr cons (cons v (foldr cons nil r)) l
255+
247256
toDiffList : {l u h} Tree V l u h DiffList (K& V)
248257
toDiffList (leaf _) = []
249258
toDiffList (node k l r _) = toDiffList l ++ k ∷ toDiffList r
250259

251260
toList : {l u h} Tree V l u h List (K& V)
252261
toList t = toDiffList t List.[]
253262

263+
size : {l u h} Tree V l u h
264+
size = List.length ∘′ toList
265+
254266
module _ {v w} {V : Value v} {W : Value w} where
255267

256268
private

src/Data/Tree/AVL/IndexedMap.agda

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ module Data.Tree.AVL.IndexedMap
1919
where
2020

2121
import Data.Tree.AVL
22-
open import Data.Bool.Base
22+
open import Data.Bool.Base using (Bool)
2323
open import Data.List.Base as List using (List)
24-
open import Data.Maybe.Base as Maybe
25-
open import Function
26-
open import Level
24+
open import Data.Maybe.Base as Maybe using (Maybe)
25+
open import Data.Nat.Base using (ℕ)
26+
open import Function.Base
27+
open import Level using (Level; _⊔_)
28+
29+
private
30+
variable
31+
a : Level
32+
A : Set a
2733

2834
-- Key/value pairs.
2935

@@ -76,8 +82,14 @@ headTail m = Maybe.map (Prod.map toKV id) (AVL.headTail m)
7682
initLast : Map Maybe (Map × KV)
7783
initLast m = Maybe.map (Prod.map id toKV) (AVL.initLast m)
7884

85+
foldr : ( {k} Value k A A) A Map A
86+
foldr cons = AVL.foldr cons
87+
7988
fromList : List KV Map
8089
fromList = AVL.fromList ∘ List.map fromKV
8190

8291
toList : Map List KV
8392
toList = List.map toKV ∘ AVL.toList
93+
94+
size : Map
95+
size = AVL.size

src/Data/Tree/AVL/Map.agda

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,78 +17,100 @@ module Data.Tree.AVL.Map
1717
open import Data.Bool.Base using (Bool)
1818
open import Data.List.Base using (List)
1919
open import Data.Maybe.Base using (Maybe)
20+
open import Data.Nat.Base using (ℕ)
2021
open import Data.Product using (_×_)
21-
open import Level using (_⊔_)
22+
open import Level using (Level; _⊔_)
23+
24+
private
25+
variable
26+
l v w x : Level
27+
A : Set l
28+
V : Set v
29+
W : Set w
30+
X : Set x
2231

2332
import Data.Tree.AVL strictTotalOrder as AVL
2433
open StrictTotalOrder strictTotalOrder renaming (Carrier to Key)
2534

2635
------------------------------------------------------------------------
2736
-- The map type
2837

29-
Map : {v} (V : Set v) Set (a ⊔ v ⊔ ℓ₂)
30-
Map v = AVL.Tree (AVL.const v)
38+
Map : (V : Set v) Set (a ⊔ v ⊔ ℓ₂)
39+
Map V = AVL.Tree (AVL.const V)
3140

3241
------------------------------------------------------------------------
3342
-- Repackaged functions
3443

35-
module _ {v} {V : Set v} where
44+
empty : Map V
45+
empty = AVL.empty
46+
47+
singleton : Key V Map V
48+
singleton = AVL.singleton
3649

37-
empty : Map V
38-
empty = AVL.empty
50+
insert : Key V Map V Map V
51+
insert = AVL.insert
3952

40-
singleton : Key V Map V
41-
singleton = AVL.singleton
53+
insertWith : Key (Maybe V V) Map V Map V
54+
insertWith = AVL.insertWith
4255

43-
insert : Key V Map V Map V
44-
insert = AVL.insert
56+
delete : Key Map V Map V
57+
delete = AVL.delete
4558

46-
insertWith : Key (Maybe V V) Map V Map V
47-
insertWith = AVL.insertWith
59+
lookup : Key Map V Maybe V
60+
lookup = AVL.lookup
4861

49-
delete : Key Map V Map V
50-
delete = AVL.delete
62+
map : (V W) Map V Map W
63+
map f = AVL.map f
5164

52-
lookup : Key Map V Maybe V
53-
lookup = AVL.lookup
65+
infix 4 _∈?_
66+
_∈?_ : Key Map V Bool
67+
_∈?_ = AVL._∈?_
5468

55-
module _ {v w} {V : Set v} {W : Set w} where
69+
headTail : Map V Maybe ((Key × V) × Map V)
70+
headTail = AVL.headTail
5671

57-
map : (V W) Map V Map W
58-
map f = AVL.map f
72+
initLast : Map V Maybe (Map V × (Key × V))
73+
initLast = AVL.initLast
5974

60-
module _ {v} {V : Set v} where
75+
foldr : (Key V A A) A Map V A
76+
foldr cons = AVL.foldr (λ {k} cons k)
6177

62-
infix 4 _∈?_
78+
fromList : List (Key × V) Map V
79+
fromList = AVL.fromList
6380

64-
_∈?_ : Key Map V Bool
65-
_∈?_ = AVL._∈?_
81+
toList : Map V List (Key × V)
82+
toList = AVL.toList
6683

67-
headTail : Map V Maybe ((Key × V) × Map V)
68-
headTail = AVL.headTail
84+
size : Map V
85+
size = AVL.size
6986

70-
initLast : Map V Maybe (Map V × (Key × V))
71-
initLast = AVL.initLast
87+
------------------------------------------------------------------------
88+
-- Naïve implementations of union
7289

73-
fromList : List (Key × V) Map V
74-
fromList = AVL.fromList
90+
unionWith : (V Maybe W W)
91+
Map V Map W Map W
92+
unionWith f = AVL.unionWith f
7593

76-
toList : Map V List (Key × V)
77-
toList = AVL.toList
94+
union : Map V Map V Map V
95+
union = AVL.union
7896

79-
module _ {v w} {V : Set v} {W : Set w} where
97+
unionsWith : (V Maybe V V) List (Map V) Map V
98+
unionsWith f = AVL.unionsWith f
8099

81-
unionWith : (V Maybe W W)
82-
Map V Map W Map W
83-
unionWith f = AVL.unionWith f
100+
unions : List (Map V) Map V
101+
unions = AVL.unions
102+
103+
------------------------------------------------------------------------
104+
-- Naïve implementations of intersection
84105

85-
module _ {v} {V : Set v} where
106+
intersectionWith : (V W X) Map V Map W Map X
107+
intersectionWith f = AVL.intersectionWith f
86108

87-
union : Map V Map V Map V
88-
union = AVL.union
109+
intersection : Map V Map V Map V
110+
intersection = AVL.intersection
89111

90-
unionsWith : (V Maybe V V) List (Map V) Map V
91-
unionsWith f = AVL.unionsWith f
112+
intersectionsWith : (V V V) List (Map V) Map V
113+
intersectionsWith f = AVL.intersectionsWith f
92114

93-
unions : List (Map V) Map V
94-
unions = AVL.unions
115+
intersections : List (Map V) Map V
116+
intersections = AVL.intersections

0 commit comments

Comments
 (0)