blob: a92175d53b9ce425efc393bf3dcca84aebbb357f (
plain)
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
(.module:
[lux #*
[control
[monoid (#+ Monoid)]
["." functor (#+ Functor)]
[apply (#+ Apply)]
["." monad (#+ do Monad)]
[equivalence (#+ Equivalence)]
[fold (#+ Fold)]
[predicate (#+ Predicate)]]
[data
bit
["." product]]])
## [Types]
## (type: (List a)
## #Nil
## (#Cons a (List a)))
## [Functions]
(structure: #export _ (Fold List)
(def: (fold f init xs)
(case xs
#.Nil
init
(#.Cons [x xs'])
(fold f (f x init) xs'))))
(open: "." Fold<List>)
(def: #export (reverse xs)
(All [a]
(-> (List a) (List a)))
(fold (function (_ head tail) (#.Cons head tail))
#.Nil
xs))
(def: #export (filter predicate xs)
(All [a]
(-> (Predicate a) (List a) (List a)))
(case xs
#.Nil
#.Nil
(#.Cons [x xs'])
(if (predicate x)
(#.Cons x (filter predicate xs'))
(filter predicate xs'))))
(def: #export (partition predicate xs)
{#.doc "Divide the list into all elements that satisfy a predicate, and all elements that do not."}
(All [a] (-> (Predicate a) (List a) [(List a) (List a)]))
[(filter predicate xs)
(filter (complement predicate) xs)])
(def: #export (as-pairs xs)
{#.doc (doc "Cut the list into pairs of 2."
"Caveat emptor: If the list has an uneven number of elements, the last one will be skipped.")}
(All [a] (-> (List a) (List [a a])))
(case xs
(^ (#.Cons [x1 (#.Cons [x2 xs'])]))
(#.Cons [[x1 x2] (as-pairs xs')])
_
#.Nil))
(do-template [<name> <then> <else>]
[(def: #export (<name> n xs)
(All [a]
(-> Nat (List a) (List a)))
(if (n/> 0 n)
(case xs
#.Nil
#.Nil
(#.Cons [x xs'])
<then>)
<else>))]
[take (#.Cons [x (take (dec n) xs')]) #.Nil]
[drop (drop (dec n) xs') xs]
)
(do-template [<name> <then> <else>]
[(def: #export (<name> predicate xs)
(All [a]
(-> (Predicate a) (List a) (List a)))
(case xs
#.Nil
#.Nil
(#.Cons [x xs'])
(if (predicate x)
<then>
<else>)))]
[take-while (#.Cons [x (take-while predicate xs')]) #.Nil]
[drop-while (drop-while predicate xs') xs]
)
(def: #export (split n xs)
(All [a]
(-> Nat (List a) [(List a) (List a)]))
(if (n/> 0 n)
(case xs
#.Nil
[#.Nil #.Nil]
(#.Cons [x xs'])
(let [[tail rest] (split (dec n) xs')]
[(#.Cons [x tail]) rest]))
[#.Nil xs]))
(def: (split-with' predicate ys xs)
(All [a]
(-> (Predicate a) (List a) (List a) [(List a) (List a)]))
(case xs
#.Nil
[ys xs]
(#.Cons [x xs'])
(if (predicate x)
(split-with' predicate (#.Cons [x ys]) xs')
[ys xs])))
(def: #export (split-with predicate xs)
{#.doc "Segment the list by using a predicate to tell when to cut."}
(All [a]
(-> (Predicate a) (List a) [(List a) (List a)]))
(let [[ys' xs'] (split-with' predicate #.Nil xs)]
[(reverse ys') xs']))
(def: #export (split-all n xs)
{#.doc "Segment the list in chunks of size n."}
(All [a] (-> Nat (List a) (List (List a))))
(case xs
#.Nil
(list)
_
(let [[pre post] (split n xs)]
(#.Cons pre (split-all n post)))))
(def: #export (repeat n x)
{#.doc "A list of the value x, repeated n times."}
(All [a]
(-> Nat a (List a)))
(if (n/> 0 n)
(#.Cons [x (repeat (dec n) x)])
#.Nil))
(def: (iterate' f x)
(All [a]
(-> (-> a (Maybe a)) a (List a)))
(case (f x)
(#.Some x')
(list& x (iterate' f x'))
#.None
(list)))
(def: #export (iterate f x)
{#.doc "Generates a list element by element until the function returns #.None."}
(All [a]
(-> (-> a (Maybe a)) a (List a)))
(case (f x)
(#.Some x')
(list& x (iterate' f x'))
#.None
(list x)))
(def: #export (find predicate xs)
{#.doc "Returns the first value in the list for which the predicate is #1."}
(All [a]
(-> (Predicate a) (List a) (Maybe a)))
(case xs
#.Nil
#.None
(#.Cons [x xs'])
(if (predicate x)
(#.Some x)
(find predicate xs'))))
(def: #export (search check xs)
(All [a b]
(-> (-> a (Maybe b)) (List a) (Maybe b)))
(case xs
#.Nil
#.None
(#.Cons [x xs'])
(case (check x)
(#.Some output)
(#.Some output)
#.None
(search check xs'))))
(def: #export (search-all check xs)
(All [a b]
(-> (-> a (Maybe b)) (List a) (List b)))
(case xs
#.Nil
#.None
(#.Cons [x xs'])
(case (check x)
(#.Some output)
(#.Cons output (search-all check xs'))
#.None
(search-all check xs'))))
(def: #export (interpose sep xs)
{#.doc "Puts a value between every two elements in the list."}
(All [a]
(-> a (List a) (List a)))
(case xs
#.Nil
xs
(#.Cons [x #.Nil])
xs
(#.Cons [x xs'])
(#.Cons [x (#.Cons [sep (interpose sep xs')])])))
(def: #export (size list)
(All [a] (-> (List a) Nat))
(fold (function (_ _ acc) (n/+ 1 acc)) 0 list))
(do-template [<name> <init> <op>]
[(def: #export (<name> predicate xs)
(All [a]
(-> (Predicate a) (List a) Bit))
(loop [xs xs]
(case xs
#.Nil
<init>
(#.Cons x xs')
(case (predicate x)
<init>
(recur xs')
output
output))))]
[every? #1 and]
[any? #0 or]
)
(def: #export (nth i xs)
{#.doc "Fetches the element at the specified index."}
(All [a]
(-> Nat (List a) (Maybe a)))
(case xs
#.Nil
#.None
(#.Cons [x xs'])
(if (n/= 0 i)
(#.Some x)
(nth (dec i) xs'))))
## [Structures]
(structure: #export (Equivalence<List> Equivalence<a>)
(All [a] (-> (Equivalence a) (Equivalence (List a))))
(def: (= xs ys)
(case [xs ys]
[#.Nil #.Nil]
#1
[(#.Cons x xs') (#.Cons y ys')]
(and (:: Equivalence<a> = x y)
(= xs' ys'))
[_ _]
#0
)))
(structure: #export Monoid<List> (All [a]
(Monoid (List a)))
(def: identity #.Nil)
(def: (compose xs ys)
(case xs
#.Nil ys
(#.Cons x xs') (#.Cons x (compose xs' ys)))))
(open: "." Monoid<List>)
(structure: #export _ (Functor List)
(def: (map f ma)
(case ma
#.Nil #.Nil
(#.Cons a ma') (#.Cons (f a) (map f ma')))))
(open: "." Functor<List>)
(structure: #export _ (Apply List)
(def: functor Functor<List>)
(def: (apply ff fa)
(case ff
#.Nil
#.Nil
(#.Cons f ff')
(compose (map f fa) (apply ff' fa)))))
(structure: #export _ (Monad List)
(def: functor Functor<List>)
(def: (wrap a)
(#.Cons a #.Nil))
(def: join (|>> reverse (fold compose identity))))
## [Functions]
(def: #export (sort < xs)
(All [a] (-> (-> a a Bit) (List a) (List a)))
(case xs
#.Nil
(list)
(#.Cons x xs')
(let [[pre post] (fold (function (_ x' [pre post])
(if (< x x')
[(#.Cons x' pre) post]
[pre (#.Cons x' post)]))
[(list) (list)]
xs')]
($_ compose (sort < pre) (list x) (sort < post)))))
(do-template [<name> <type> <lt> <gt>]
[(def: #export (<name> from to)
{#.doc "Generates an inclusive interval of values [from, to]."}
(-> <type> <type> (List <type>))
(cond (<lt> to from)
(list& from (<name> (inc from) to))
(<gt> to from)
(list& from (<name> (dec from) to))
## (= to from)
(list from)))]
[i/range Int i/< i/>]
[n/range Nat n/< n/>]
)
(def: #export (empty? xs)
(All [a] (Predicate (List a)))
(case xs
#.Nil #1
_ #0))
(def: #export (member? eq xs x)
(All [a] (-> (Equivalence a) (List a) a Bit))
(case xs
#.Nil #0
(#.Cons x' xs') (or (:: eq = x x')
(member? eq xs' x))))
(do-template [<name> <output> <side> <doc>]
[(def: #export (<name> xs)
{#.doc <doc>}
(All [a] (-> (List a) (Maybe <output>)))
(case xs
#.Nil
#.None
(#.Cons x xs')
(#.Some <side>)))]
[head a x "Returns the first element of a list."]
[tail (List a) xs' "For a list of size N, returns the N-1 elements after the first one."]
)
(def: #export (indices size)
{#.doc "Produces all the valid indices for a given size."}
(All [a] (-> Nat (List Nat)))
(if (n/= 0 size)
(list)
(|> size dec (n/range 0))))
## [Syntax]
(def: (identifier$ name)
(-> Text Code)
[["" 0 0] (#.Identifier "" name)])
(def: (nat/encode value)
(-> Nat Text)
(loop [input value
output ""]
(let [digit (case (n/% 10 input)
0 "0"
1 "1"
2 "2"
3 "3"
4 "4"
5 "5"
6 "6"
7 "7"
8 "8"
9 "9"
_ (undefined))
output' ("lux text concat" digit output)
input' (n// 10 input)]
(if (n/= 0 input')
output'
(recur input' output')))))
(macro: #export (zip tokens state)
{#.doc (doc "Create list zippers with the specified number of input lists."
(def: #export zip2 (zip 2))
(def: #export zip3 (zip 3))
((zip 3) xs ys zs))}
(case tokens
(^ (list [_ (#.Nat num-lists)]))
(if (n/> 0 num-lists)
(let [(^open ".") Functor<List>
indices (..indices num-lists)
type-vars (: (List Code) (map (|>> nat/encode identifier$) indices))
zip-type (` (All [(~+ type-vars)]
(-> (~+ (map (: (-> Code Code) (function (_ var) (` (List (~ var)))))
type-vars))
(List [(~+ type-vars)]))))
vars+lists (|> indices
(map inc)
(map (function (_ idx)
(let [base (nat/encode idx)]
[(identifier$ base)
(identifier$ ("lux text concat" base "'"))]))))
pattern (` [(~+ (map (function (_ [v vs]) (` (#.Cons (~ v) (~ vs))))
vars+lists))])
g!step (identifier$ "0step0")
g!blank (identifier$ "0_0")
list-vars (map product.right vars+lists)
code (` (: (~ zip-type)
(function ((~ g!step) (~+ list-vars))
(case [(~+ list-vars)]
(~ pattern)
(#.Cons [(~+ (map product.left vars+lists))]
((~ g!step) (~+ list-vars)))
(~ g!blank)
#.Nil))))]
(#.Right [state (list code)]))
(#.Left "Cannot zip 0 lists."))
_
(#.Left "Wrong syntax for zip")))
(def: #export zip2 (zip 2))
(def: #export zip3 (zip 3))
(macro: #export (zip-with tokens state)
{#.doc (doc "Create list zippers with the specified number of input lists."
(def: #export zip2-with (zip-with 2))
(def: #export zip3-with (zip-with 3))
((zip-with 2) i/+ xs ys))}
(case tokens
(^ (list [_ (#.Nat num-lists)]))
(if (n/> 0 num-lists)
(let [(^open ".") Functor<List>
indices (..indices num-lists)
g!return-type (identifier$ "0return-type0")
g!func (identifier$ "0func0")
type-vars (: (List Code) (map (|>> nat/encode identifier$) indices))
zip-type (` (All [(~+ type-vars) (~ g!return-type)]
(-> (-> (~+ type-vars) (~ g!return-type))
(~+ (map (: (-> Code Code) (function (_ var) (` (List (~ var)))))
type-vars))
(List (~ g!return-type)))))
vars+lists (|> indices
(map inc)
(map (function (_ idx)
(let [base (nat/encode idx)]
[(identifier$ base)
(identifier$ ("lux text concat" base "'"))]))))
pattern (` [(~+ (map (function (_ [v vs]) (` (#.Cons (~ v) (~ vs))))
vars+lists))])
g!step (identifier$ "0step0")
g!blank (identifier$ "0_0")
list-vars (map product.right vars+lists)
code (` (: (~ zip-type)
(function ((~ g!step) (~ g!func) (~+ list-vars))
(case [(~+ list-vars)]
(~ pattern)
(#.Cons ((~ g!func) (~+ (map product.left vars+lists)))
((~ g!step) (~ g!func) (~+ list-vars)))
(~ g!blank)
#.Nil))))]
(#.Right [state (list code)]))
(#.Left "Cannot zip-with 0 lists."))
_
(#.Left "Wrong syntax for zip-with")))
(def: #export zip2-with (zip-with 2))
(def: #export zip3-with (zip-with 3))
(def: #export (last xs)
(All [a] (-> (List a) (Maybe a)))
(case xs
#.Nil
#.None
(#.Cons x #.Nil)
(#.Some x)
(#.Cons x xs')
(last xs')))
(def: #export (inits xs)
{#.doc (doc "For a list of size N, returns the first N-1 elements."
"Empty lists will result in a #.None value being returned instead.")}
(All [a] (-> (List a) (Maybe (List a))))
(case xs
#.Nil
#.None
(#.Cons x #.Nil)
(#.Some #.Nil)
(#.Cons x xs')
(case (inits xs')
#.None
(undefined)
(#.Some tail)
(#.Some (#.Cons x tail)))
))
(def: #export (concat xss)
(All [a] (-> (List (List a)) (List a)))
(:: Monad<List> join xss))
(structure: #export (ListT Monad<M>)
(All [M] (-> (Monad M) (Monad (All [a] (M (List a))))))
(def: functor (functor.compose (get@ #monad.functor Monad<M>) Functor<List>))
(def: wrap (|>> (:: Monad<List> wrap) (:: Monad<M> wrap)))
(def: (join MlMla)
(do Monad<M>
[lMla MlMla
## TODO: Remove this version ASAP and use one below.
lla (: (($ 0) (List (List ($ 1))))
(monad.seq @ lMla))
## lla (monad.seq @ lMla)
]
(wrap (concat lla)))))
(def: #export (lift Monad<M>)
(All [M a] (-> (Monad M) (-> (M a) (M (List a)))))
(monad.lift Monad<M> (:: Monad<List> wrap)))
(def: (enumerate' idx xs)
(All [a] (-> Nat (List a) (List [Nat a])))
(case xs
#.Nil
#.Nil
(#.Cons x xs')
(#.Cons [idx x] (enumerate' (inc idx) xs'))))
(def: #export (enumerate xs)
{#.doc "Pairs every element in the list with its index, starting at 0."}
(All [a] (-> (List a) (List [Nat a])))
(enumerate' 0 xs))
|