-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.pony
434 lines (353 loc) · 9.27 KB
/
stream.pony
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
interface val Printable
fun string(): String
type Stream[A: Any val] is (SCons[A] | SNext[A] | SNil[A])
primitive SNil[A: Any val]
"""
The empty stream of As.
"""
fun val step(): Stream[A] =>
this
fun mature(): (A, Stream[A]) ? =>
error
fun val force(): Stream[A] =>
this
fun size(): USize =>
0
fun empty(): Bool =>
"""
Returns a Bool indicating if the stream is empty.
"""
true
fun is_non_empty(): Bool =>
"""
Returns a Bool indicating if the stream is non-empty.
"""
false
fun head(): A ? =>
"""
Returns an error, since Nil has no head.
"""
error
fun tail(): Stream[A] ? =>
"""
Returns an error, since Nil has no tail.
"""
error
fun val map[B: Any val](f: {(A): B} val): Stream[B] =>
SNil[B]
fun val flat_map[B: Any val](f: {(A): Stream[B]} val): Stream[B] =>
SNil[B]
fun val filter(pred: {(A): Bool} val): Stream[A] =>
SNil[A]
fun val merge(s2: Stream[A]): Stream[A] =>
s2
fun val delay(): Stream[A] =>
SNil[A]
fun val reverse(): SNil[A] =>
this
fun val prepend(a: A): SCons[A] =>
SCons[A](a, this)
fun take(n: USize): SNil[A] =>
"""
There are no elements to take from the empty stream.
"""
this
fun string(): String =>
"Stream()"
fun _string(): String =>
")"
trait val SNext[A: Any val]
// In order to implement an SNext, you need an implementation
// of mature() (and possibly of step())
fun mature(): (A, Stream[A]) ?
fun val step(): Stream[A] =>
force()
fun val force(): Stream[A] =>
try
// This weird line is because the compiler doesn't see the mature()
// line as possibly raising an error when wrapped in a try block
// TODO: Address and remove
if false then error end
(let h: A, let t: Stream[A]) = mature()?
SCons[A](h, t)
else
SNil[A]
end
fun val size(): USize =>
"""
It's unwise to call size() on an infinite stream
"""
force().size()
fun empty(): Bool =>
"""
Returns a Bool indicating if the stream is empty.
"""
false
fun is_non_empty(): Bool =>
"""
Returns a Bool indicating if the stream is non-empty.
"""
true
fun head(): A ? =>
"""
Returns the head of the stream.
"""
mature()?._1
fun tail(): Stream[A] ? =>
"""
Returns the tail of the stream.
"""
mature()?._2
// TODO: Determine why compiler refuses to accept this use of
// type parameter B: Any val on SNext (but not SCons) for map and
// flat_map.
// fun val map[B: Any val](f: {(A): B} val): SMap[A, B] val =>
// match force()
// | let cons: SCons[A] => cons.map[B](f)
// else
// SNil[A].map[B](f)
// end
// fun val flat_map[B: Any val](f: {(A): Stream[B]} val):
// SFlatMap[A, B] val
// =>
// force().flat_map[B](f)
fun val filter(pred: {(A): Bool} val): Stream[A] =>
SFilter[A](pred, this)
fun val merge(s2: Stream[A]): Stream[A] =>
SMerge[A](this, s2)
fun val delay(): Stream[A] =>
force().delay()
//TODO: Determine why compiler thinks self is not of type Stream[A] and
// replace the force() line with these:
// let self = this
// SDelay[A](recover {()(self): Stream[A] => self} end)
fun val reverse(): Stream[A] =>
"""
Builds a new stream by reversing the elements in the stream.
"""
Streams[A].reverse(this)
fun val prepend(a: A): SCons[A] =>
"""
Builds a new stream with an element added to the front of this stream.
"""
SCons[A](a, this)
fun val take(n: USize): Stream[A] =>
"""
Builds a stream of the first n elements.
"""
Streams[A].take(n, this)
fun string(): String =>
try
let name = match head()?
| let str: Printable =>
str.string()
end
try
return "Stream(" + name.string() + tail()?._string()
else
return "Stream(" + name.string() + ")"
end
else
"Stream()"
end
fun _string(): String =>
try
let name = match head()?
| let str: Printable =>
str.string()
end
try
", " + name.string() + tail()?._string()
else
", " + name.string() + ")"
end
else
")"
end
class val SCons[A: Any val]
"""
A stream with a head and a tail, where the tail can be empty.
"""
let _head: A
let _tail: Stream[A]
new val create(h: A, t: Stream[A]) =>
_head = h
_tail = t
fun val step(): Stream[A] =>
this
fun mature(): (A, Stream[A]) =>
(_head, _tail)
fun val force(): Stream[A] =>
this
fun size(): USize =>
"""
It's unwise to call size() on an infinite stream
"""
1 + _tail.size()
fun empty(): Bool =>
"""
Returns a Bool indicating if the stream is empty.
"""
false
fun is_non_empty(): Bool =>
"""
Returns a Bool indicating if the stream is non-empty.
"""
true
fun head(): A =>
"""
Returns the head of the stream.
"""
_head
fun tail(): Stream[A] =>
"""
Returns the tail of the stream.
"""
_tail
fun val map[B: Any val](f: {(A): B} val): Stream[B] =>
SMap[A, B](f, this)
fun val flat_map[B: Any val](f: {(A): Stream[B]} val): Stream[B] =>
SFlatMap[A, B](f, this)
fun val filter(pred: {(A): Bool} val): Stream[A] =>
SFilter[A](pred, this)
fun val merge(s2: Stream[A]): Stream[A] =>
SMerge[A](this, s2)
fun val delay(): Stream[A] =>
let self = this
SDelay[A](recover {()(self): Stream[A] => self} end)
fun val reverse(): Stream[A] =>
"""
Builds a new stream by reversing the elements in the stream.
"""
Streams[A].reverse(this)
fun val prepend(a: A): SCons[A] =>
"""
Builds a new stream with an element added to the front of this stream.
"""
SCons[A](a, this)
fun val take(n: USize): Stream[A] =>
"""
Builds a stream of the first n elements.
"""
Streams[A].take(n, this)
fun string(): String =>
match head()
| let str: Printable =>
"Stream(" + str.string() + tail()._string()
else
"Stream(" + "?" + tail()._string()
end
fun _string(): String =>
match head()
| let str: Printable =>
", " + str.string() + tail()._string()
else
", " + "?" + tail()._string()
end
primitive Streams[A: Any val]
fun val reverse(l: Stream[A]): Stream[A] =>
_reverse(l, SNil[A])
fun val _reverse(l: Stream[A], acc: Stream[A]): Stream[A] =>
"""
Private helper for reverse, recursively working on elements.
"""
match l.force()
| let cons: SCons[A] => _reverse(cons.tail(), acc.prepend(cons.head()))
else
acc
end
fun val take(n: USize, s: Stream[A]): Stream[A] =>
"""
Builds a stream of the first n elements.
"""
var cur: Stream[A] = s
var count = n
var res: Stream[A] = SNil[A]
while(count > 0) do
match cur.force()
| let cons: SCons[A] =>
res = res.prepend(cons.head())
cur = cons.tail()
else
return res.reverse()
end
count = count - 1
end
res.reverse()
// TODO: Remove this once compiler type issues are resolved above for SNext
fun val map[B: Any val](f: {(A): B} val, s: Stream[A]): Stream[B] =>
SMap[A, B](f, s)
// TODO: Remove this once compiler type issues are resolved above for SNext
fun val flat_map[B: Any val](f: {(A): Stream[B]} val, s: Stream[A]):
Stream[B]
=>
SFlatMap[A, B](f, s)
//////////////////////////
// SNext implementations
//////////////////////////
class val SMerge[A: Any val] is SNext[A]
"""
Interleave two streams
"""
let _l: Stream[A]
let _r: Stream[A]
new val create(l: Stream[A], r: Stream[A]) =>
_l = l
_r = r
fun mature(): (A, Stream[A]) ? =>
match (_l, _r)
| (let l: SNil[A], _) => _r.mature()?
| (_, let r: SNil[A]) => _l.mature()?
else
(let h: A, let t: Stream[A]) = _l.mature()?
(h, SMerge[A](_r, t))
end
class val SDelay[A: Any val] is SNext[A]
let _s: {(): Stream[A]} val
new val create(s: {(): Stream[A]} val) =>
_s = s
fun mature(): (A, Stream[A]) ? =>
let next = _s().force()
(next.head()?, SDelay[A](recover {()(next): Stream[A] =>
try next.tail()? else SNil[A] end}
end))
class val SMap[A: Any val, B: Any val] is SNext[B]
let _f: {(A): B} val
let _s: Stream[A]
new val create(f: {(A): B} val, s: Stream[A]) =>
_f = f
_s = s
fun mature(): (B, Stream[B]) ? =>
match _s.force()
| let cons: SCons[A] =>
(_f(cons.head()), SMap[A, B](_f, cons.tail()))
else
error
end
class val SFlatMap[A: Any val, B: Any val] is SNext[B]
let _f: {(A): Stream[B]} val
let _s: Stream[A]
new val create(f: {(A): Stream[B]} val, s: Stream[A]) =>
_f = f
_s = s
fun mature(): (B, Stream[B]) ? =>
match _s.force()
| let cons: SCons[A] =>
let next = _f(cons.head()).force()
(next.head()?, SFlatMap[A, B](_f, cons.tail()).merge(next.tail()?))
else
error
end
class val SFilter[A: Any val] is SNext[A]
let _pred: {(A): Bool} val
let _s: Stream[A]
new val create(pred: {(A): Bool} val, s: Stream[A]) =>
_pred = pred
_s = s
fun mature(): (A, Stream[A]) ? =>
let s = _s.force()
if _pred(s.head()?) then
(s.head()?, SFilter[A](_pred, s.tail()?))
else
SFilter[A](_pred, s.tail()?).mature()?
end