forked from joelvaneenwyk/language84
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.84
238 lines (231 loc) · 7.62 KB
/
search.84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
{
: MAP
: SET
}
Where
Define (MAP compare item_key)
(make compare item_key)
Let SET
Define (item_key item) item
In
Func compare
Let MAP (make compare item_key)
In
Define (union s t)
(LIST.reduce (MAP.list s) t MAP.insert)
Define (diff s t)
(LIST.reduce (MAP.list s) MAP.empty
Func {u item}
Match (MAP.search t item) {
| 'nothing (MAP.insert u item)
| 'just._ u
})
In
{
: compare
: empty MAP.empty
: new MAP.new
: search MAP.search
: insert MAP.insert
: list MAP.list
: union
: diff
}
Where
Define (make compare item_key)
Define (search tree key)
Iterate {depth t} From {tree.depth tree.root}
Cond {
| [depth = 0] 'nothing
| [depth = 1]
Match t {
| 'one.a
Match (compare key (item_key a)) {
| 'equal 'just.a
| _ 'nothing
}
| 'two.{a b}
Match (compare key (item_key a)) {
| 'equal 'just.a
| _
Match (compare key (item_key b)) {
| 'equal 'just.b
| _ 'nothing
}
}
}
| True
Match t {
| 'one.{u a v}
Match (compare key (item_key a)) {
| 'less (Continue [depth - 1] u)
| 'equal 'just.a
| 'greater (Continue [depth - 1] v)
}
| 'two.{u a v b w}
Match (compare key (item_key a)) {
| 'less (Continue [depth - 1] u)
| 'equal 'just.a
| 'greater
Match (compare key (item_key b)) {
| 'less (Continue [depth - 1] v)
| 'equal 'just.b
| 'greater (Continue [depth - 1] w)
}
}
}
}
In
Let empty
{
: depth 0
: size 0
: root 'zero
}
Define (insert tree a)
Define (insert depth tree a)
Unfold {depth tree a}
Cond {
| [depth = 1]
Match tree {
| 'one.b
Match (compare (item_key a) (item_key b)) {
| 'less 'no_split.'two.{a b}
| 'greater 'no_split.'two.{b a}
| 'equal 'no_split.'one.a
}
| 'two.{b c}
Let key_a (item_key a)
Let key_b (item_key b)
Let key_c (item_key c)
In
Match (compare key_a key_b) {
| 'less 'split.{'one.a b 'one.c}
| 'greater
Match (compare key_a key_c) {
| 'less 'split.{'one.b a 'one.c}
| 'greater 'split.{'one.b c 'one.a}
| 'equal 'no_split.'two.{b a}
}
| 'equal 'no_split.'two.{a c}
}
}
| True
Match tree {
| 'one.{u b v}
Match (compare (item_key a) (item_key b)) {
| 'less
Match (Fold [depth - 1] u a) {
| 'no_split.u
'no_split.'one.{u b v}
| 'split.{t a u}
'no_split.'two.{t a u b v}
}
| 'greater
Match (Fold [depth - 1] v a) {
| 'no_split.v
'no_split.'one.{u b v}
| 'split.{v c w}
'no_split.'two.{u b v c w}
}
| 'equal 'no_split.'one.{u a v}
}
| 'two.{t b u c v}
Let key_a (item_key a)
Let key_b (item_key b)
Let key_c (item_key c)
In
Match (compare key_a key_b) {
| 'less
Match (Fold [depth - 1] t a) {
| 'no_split.t
'no_split.'two.{t b u c v}
| 'split.{tt a tu}
'split.{'one.{tt a tu} b 'one.{u c v}}
}
| 'greater
Match (compare key_a key_c) {
| 'less
Match (Fold [depth - 1] u a) {
| 'no_split.u
'no_split.'two.{t b u c v}
| 'split.{ut a uv}
'split.{'one.{t b ut} a 'one.{uv c v}}
}
| 'greater
Match (Fold [depth - 1] v a) {
| 'no_split.v
'no_split.'two.{t b u c v}
| 'split.{vu a vv}
'split.{'one.{t b u} c 'one.{vu a vv}}
}
| 'equal 'no_split.'two.{t b u a v}
}
| 'equal 'no_split.'two.{t a u c v}
}
}
}
In
If [tree.depth = 0]
{
: depth 1
: size 1
: root 'one.a
}
Match (insert tree.depth tree.root a) {
| 'no_split.t
{
: depth tree.depth
: size [tree.size + 1]
: root t
}
| 'split.{u a v}
{
: depth [tree.depth + 1]
: size [tree.size + 1]
: root 'one.{u a v}
}
}
Define (list tree)
If [tree.depth = 0]
'nil
Unfold {depth t list} From {tree.depth tree.root 'nil}
Cond {
| [depth > 1]
Let depth [depth - 1]
In
Match t {
| 'one.{u a v}
(Fold depth u [a & (Fold depth v list)])
| 'two.{u a v b w}
Let list
(Fold depth v
[b & (Fold depth w list)])
In
(Fold depth u [a & list])
}
| [depth = 1]
Match t {
| 'one.a [a & list]
| 'two.{a b} [a & b & list]
}
}
In
Define (size tree)
tree.size
Define (new items)
(LIST.reduce items empty insert)
In
{
: compare
: item_key
: size
: new
: empty
: insert
: search
: list
}
Where
Let STDIO Package "stdio"
Let LIST Package "list"