blob: acd48d7304f2d6beed4726385f9ef6756539377d (
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
|
## Copyright (c) Eduardo Julian. All rights reserved.
## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
## If a copy of the MPL was not distributed with this file,
## You can obtain one at http://mozilla.org/MPL/2.0/.
(;module:
lux
(lux (control monoid
functor
applicative
["M" monad #*]
eq
[fold])
(data [number "Int/" Number<Int> Codec<Text,Int>]
bool
[product])
codata/function))
## [Types]
## (type: (List a)
## #Nil
## (#Cons a (List a)))
## [Functions]
(struct: #export _ (fold;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 (lambda [head tail] (#;Cons head tail))
#;Nil
xs))
(def: #export (filter p xs)
(All [a]
(-> (-> a Bool) (List a) (List a)))
(case xs
#;Nil
#;Nil
(#;Cons [x xs'])
(if (p x)
(#;Cons [x (filter p xs')])
(filter p xs'))))
(def: #export (partition p xs)
(All [a] (-> (-> a Bool) (List a) [(List a) (List a)]))
[(filter p xs) (filter (complement p) xs)])
(def: #export (as-pairs xs)
(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 (n.dec n) xs')]) #;Nil]
[drop (drop (n.dec n) xs') xs]
)
(do-template [<name> <then> <else>]
[(def: #export (<name> p xs)
(All [a]
(-> (-> a Bool) (List a) (List a)))
(case xs
#;Nil
#;Nil
(#;Cons [x xs'])
(if (p x)
<then>
<else>)))]
[take-while (#;Cons [x (take-while p xs')]) #;Nil]
[drop-while (drop-while p 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 (n.dec n) xs')]
[(#;Cons [x tail]) rest]))
[#;Nil xs]))
(def: (split-with' p ys xs)
(All [a]
(-> (-> a Bool) (List a) (List a) [(List a) (List a)]))
(case xs
#;Nil
[ys xs]
(#;Cons [x xs'])
(if (p x)
(split-with' p (#;Cons [x ys]) xs')
[ys xs])))
(def: #export (split-with p xs)
(All [a]
(-> (-> a Bool) (List a) [(List a) (List a)]))
(let [[ys' xs'] (split-with' p #;Nil xs)]
[(reverse ys') xs']))
(def: #export (split-all n xs)
(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)
(All [a]
(-> Nat a (List a)))
(if (n.> +0 n)
(#;Cons [x (repeat (n.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)
(All [a]
(-> (-> a (Maybe a)) a (List a)))
(case (f x)
(#;Some x')
(list& x (iterate' f x'))
#;None
(list x)))
(def: #export (find p xs)
(All [a]
(-> (-> a Bool) (List a) (Maybe a)))
(case xs
#;Nil
#;None
(#;Cons [x xs'])
(if (p x)
(#;Some x)
(find p xs'))))
(def: #export (interpose sep xs)
(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 (lambda [_ acc] (n.+ +1 acc)) +0 list))
(do-template [<name> <init> <op>]
[(def: #export (<name> p xs)
(All [a]
(-> (-> a Bool) (List a) Bool))
(fold (lambda [_2 _1] (<op> _1 (p _2))) <init> xs))]
[every? true and]
[any? false or])
(def: #export (at i xs)
(All [a]
(-> Nat (List a) (Maybe a)))
(case xs
#;Nil
#;None
(#;Cons [x xs'])
(if (n.= +0 i)
(#;Some x)
(at (n.dec i) xs'))))
## [Structures]
(struct: #export (Eq<List> (^open "a:"))
(All [a] (-> (Eq a) (Eq (List a))))
(def: (= xs ys)
(case [xs ys]
[#;Nil #;Nil]
true
[(#;Cons x xs') (#;Cons y ys')]
(and (a:= x y)
(= xs' ys'))
[_ _]
false
)))
(struct: #export Monoid<List> (All [a]
(Monoid (List a)))
(def: unit #;Nil)
(def: (append xs ys)
(case xs
#;Nil ys
(#;Cons x xs') (#;Cons x (append xs' ys)))))
(open Monoid<List>)
(struct: #export _ (Functor List)
(def: (map f ma)
(case ma
#;Nil #;Nil
(#;Cons a ma') (#;Cons (f a) (map f ma')))))
(open Functor<List>)
(struct: #export _ (Applicative List)
(def: functor Functor<List>)
(def: (wrap a)
(#;Cons a #;Nil))
(def: (apply ff fa)
(case ff
#;Nil
#;Nil
(#;Cons f ff')
(append (map f fa) (apply ff' fa)))))
(struct: #export _ (Monad List)
(def: applicative Applicative<List>)
(def: join (|>. reverse (fold append unit))))
## [Functions]
(def: #export (sort < xs)
(All [a] (-> (-> a a Bool) (List a) (List a)))
(case xs
#;Nil
(list)
(#;Cons x xs')
(let [[pre post] (fold (lambda [x' [pre post]]
(if (< x x')
[(#;Cons x' pre) post]
[pre (#;Cons x' post)]))
[(list) (list)]
xs')]
($_ append (sort < pre) (list x) (sort < post)))))
(do-template [<name> <type> <comp> <inc>]
[(def: #export (<name> from to)
(-> <type> <type> (List <type>))
(if (<comp> to from)
(list& from (<name> (<inc> from) to))
(list)))]
[i.range Int i.<= i.inc]
[n.range Nat n.<= n.inc]
)
(def: #export (empty? xs)
(All [a] (-> (List a) Bool))
(case xs
#;Nil true
_ false))
(def: #export (member? eq xs x)
(All [a] (-> (Eq a) (List a) a Bool))
(case xs
#;Nil false
(#;Cons x' xs') (or (:: eq = x x')
(member? eq xs' x))))
(do-template [<name> <output> <side>]
[(def: #export (<name> xs)
(All [a] (-> (List a) (Maybe <output>)))
(case xs
#;Nil
#;None
(#;Cons x xs')
(#;Some <side>)))]
[head a x]
[tail (List a) xs']
)
## [Syntax]
(def: (symbol$ name)
(-> Text AST)
[["" -1 -1] (#;SymbolS "" name)])
(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 [_ (#;IntS num-lists)]))
(if (i.> 0 num-lists)
(let [(^open) Functor<List>
indices (i.range 0 (i.dec num-lists))
type-vars (: (List AST) (map (. symbol$ Int/encode) indices))
zip-type (` (All [(~@ type-vars)]
(-> (~@ (map (: (-> AST AST) (lambda [var] (` (List (~ var)))))
type-vars))
(List [(~@ type-vars)]))))
vars+lists (|> indices
(map i.inc)
(map (lambda [idx]
[(symbol$ (Int/encode idx))
(symbol$ (Int/encode (Int/negate idx)))])))
pattern (` [(~@ (map (lambda [[v vs]] (` (#;Cons (~ v) (~ vs))))
vars+lists))])
g!step (symbol$ "\tstep\t")
g!blank (symbol$ "\t_\t")
list-vars (map product;right vars+lists)
code (` (: (~ zip-type)
(lambda (~ 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 "Can't 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 zip-with`s 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 [_ (#;IntS num-lists)]))
(if (i.> 0 num-lists)
(let [(^open) Functor<List>
indices (i.range 0 (i.dec num-lists))
g!return-type (symbol$ "\treturn-type\t")
g!func (symbol$ "\tfunc\t")
type-vars (: (List AST) (map (. symbol$ Int/encode) indices))
zip-type (` (All [(~@ type-vars) (~ g!return-type)]
(-> (-> (~@ type-vars) (~ g!return-type))
(~@ (map (: (-> AST AST) (lambda [var] (` (List (~ var)))))
type-vars))
(List (~ g!return-type)))))
vars+lists (|> indices
(map i.inc)
(map (lambda [idx]
[(symbol$ (Int/encode idx))
(symbol$ (Int/encode (Int/negate idx)))])))
pattern (` [(~@ (map (lambda [[v vs]] (` (#;Cons (~ v) (~ vs))))
vars+lists))])
g!step (symbol$ "\tstep\t")
g!blank (symbol$ "\t_\t")
list-vars (map product;right vars+lists)
code (` (: (~ zip-type)
(lambda (~ 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 "Can't 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)
(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))
(struct: #export (ListT Monad<M>)
(All [M] (-> (Monad M) (Monad (All [a] (M (List a))))))
(def: applicative (compA (get@ #M;applicative Monad<M>) Applicative<List>))
(def: (join MlMla)
(do Monad<M>
[lMla MlMla
lla (: (($ +0) (List (List ($ +1))))
(mapM @ join lMla))]
(wrap (concat lla)))))
(def: #export (lift-list Monad<M>)
(All [M a] (-> (Monad M) (-> (M a) (M (List a)))))
(liftM 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' (n.inc idx) xs'))))
(def: #export (enumerate xs)
(All [a] (-> (List a) (List [Nat a])))
(enumerate' +0 xs))
(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 n.dec (n.range +0))))
|