blob: 03c2a60546ff2e174fbe853a291f542966cc0af2 (
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
|
## Base interfaces & classes
(jvm;interface Function
(: apply (-> [java.lang.Object] java.lang.Object)))
(jvm;class Tuple0 java.lang.Object
[])
(jvm;class Tuple1 java.lang.Object
[[java.lang.Object _0]])
(jvm;class Tuple2 java.lang.Object
[[java.lang.Object _0] [java.lang.Object _1]])
(jvm;class Tuple3 java.lang.Object
[[java.lang.Object _0] [java.lang.Object _1]
[java.lang.Object _2]])
(jvm;class Tuple4 java.lang.Object
[[java.lang.Object _0] [java.lang.Object _1]
[java.lang.Object _2] [java.lang.Object _3]])
(jvm;class Tuple5 java.lang.Object
[[java.lang.Object _0] [java.lang.Object _1]
[java.lang.Object _2] [java.lang.Object _3]
[java.lang.Object _4]])
(jvm;class Tuple6 java.lang.Object
[[java.lang.Object _0] [java.lang.Object _1]
[java.lang.Object _2] [java.lang.Object _3]
[java.lang.Object _4] [java.lang.Object _5]])
(jvm;class Tuple7 java.lang.Object
[[java.lang.Object _0] [java.lang.Object _1]
[java.lang.Object _2] [java.lang.Object _3]
[java.lang.Object _4] [java.lang.Object _5]
[java.lang.Object _6]])
(jvm;class Tuple8 java.lang.Object
[[java.lang.Object _0] [java.lang.Object _1]
[java.lang.Object _2] [java.lang.Object _3]
[java.lang.Object _4] [java.lang.Object _5]
[java.lang.Object _6] [java.lang.Object _7]])
(jvm;class Variant java.lang.Object
[[java.lang.String tag] [java.lang.Object value]])
## Base functions & macros
(def' let'
(lambda' _ tokens
(lambda' _ state
(case' tokens
(#Cons [lhs (#Cons [rhs (#Cons [body #Nil])])])
[(#Cons [(#Form (#Cons [(#Ident "case'") (#Cons [rhs (#Cons [lhs (#Cons [body #Nil])])])]))
#Nil])
state])
)))
(declare-macro let')
(def' lambda
(lambda' _ tokens
(lambda' _ state
(let' output (case' tokens
(#Cons [(#Form (#Cons [self (#Cons [arg args'])])) (#Cons [body #Nil])])
(#Form (#Cons [(#Ident "lambda'")
(#Cons [self
(#Cons [arg
(#Cons [(case' args'
#Nil
body
_
(#Form (#Cons [(#Ident "lux;lambda")
(#Cons [(#Form (#Cons [(#Ident "_") args']))
(#Cons [body #Nil])])])))
#Nil])])])])))
[(#Cons [output #Nil]) state])
)))
(declare-macro lambda)
## (def' lambda
## (lambda' _ tokens
## (lambda' _ state
## (let' output (case' tokens
## (#Cons [(#Form (#Cons [self (#Cons [arg args'])])) (#Cons [body #Nil])])
## (#Form (#Cons [(#Ident "lambda'")
## (#Cons [self
## (#Cons [arg
## (#Cons [(case' args'
## #Nil
## body
## _
## (#Form (#Cons [(#Ident "lux;lambda")
## (#Cons [(#Form (#Cons [(#Ident "_") args']))
## (#Cons [body #Nil])])])))
## #Nil])])])])))
## [(#Cons [output #Nil]) state])
## )))
## (def' lambda
## (lambda' _ tokens
## (lambda' _ state
## (let' output (case' tokens
## (#Cons [(#Form (#Cons [self (#Cons [arg args'])])) (#Cons [body #Nil])])
## (#Form (#Cons [(#Ident "lambda'")
## (#Cons [self
## (#Cons [arg
## (#Cons [(case args'
## #Nil
## body
## _
## (#Form (#Cons [(#Ident "lux;lambda")
## (#Cons [(#Form (#Cons [(#Ident "_") args']))
## (#Cons [body #Nil])])])))
## #Nil])])])])))
## [(#Cons output #Nil) state])
## )))
## (def' def
## (lambda (_ tokens state)
## (let' output (case' tokens
## (#Cons (#Ident name) (#Cons body #Nil))
## (#Form (#Cons (#Ident "def'")
## (#Cons (#Ident name)
## (#Cons body #Nil))))
## (#Cons (#Form (#Cons (#Ident name) args))
## (#Cons body #Nil))
## (#Form (#Cons (#Ident "def'")
## (#Cons (#Ident name)
## (#Cons (#Form (#Cons (#Ident "lux:lambda")
## (#Cons (#Form (#Cons (#Ident name) args))
## (#Cons body #Nil))))
## #Nil)))))
## [(#Cons output #Nil) state])))
## (declare-macro def)
## (def (comment tokens state)
## [#Nil state])
## (declare-macro comment)
## (def (+ x y)
## (jvm;iadd x y))
## (def (id x)
## x)
## (def (print x)
## (jvm;invokevirtual java.io.PrintStream "print" [Object]
## (jvm;getstatic System out) [x]))
## (def (println x)
## (jvm;invokevirtual java.io.PrintStream "println" [Object]
## (jvm;getstatic System out) [x]))
## (def (fold f init xs)
## (do (print "fold ") (print init) (print " ") (println xs)
## (case' xs
## #Nil
## init
## (#Cons x xs')
## (let' init' (f init x)
## (do (print "init': ") (println init')
## (fold f init' xs')))
## #((fold f (f init x) xs'))#
## )))
## (def (reverse list)
## (do (print "reverse ") (println list)
## (let' reversed (fold (lambda [tail head]
## (do (print "reverse/0 ") (print "tail: ") (print tail) (print " head: ") (println head)
## (#Cons head tail)))
## #Nil
## list)
## (do (print "!reversed ") (println reversed)
## reversed))))
## (def (list xs state)
## (let' xs' (reverse xs)
## (let' output (fold (lambda [tail head]
## (do (print "tail: ") (print tail) (print " head: ") (println head)
## (#Form (#Cons (#Tag "Cons")
## (#Cons head
## (#Cons tail #Nil))))))
## (#Tag "Nil")
## (do (print "REVERSED: ") (println xs')
## xs'))
## (do (print "output: ") (println output)
## [(#Cons output #Nil) state]))))
## (declare-macro list)
## (def (list+ xs state)
## (case' (reverse xs)
## #Nil
## [#Nil state]
## (#Cons last init')
## (let' output (fold (lambda [tail head]
## (#Form (#Cons (#Tag "Cons")
## (#Cons head tail))))
## last
## init')
## [(#Cons output #Nil) state])))
## (declare-macro list+)
## (def (->pairs xs)
## (case' xs
## (#Cons x (#Cons y xs'))
## (#Cons [x y] (->pairs xs'))
## _
## #Nil))
## (def (let tokens state)
## (case' tokens
## (#Cons (#Tuple bindings) (#Cons body #Nil))
## (let' output (fold (lambda [body binding]
## (case binding
## [label value]
## (#Form (list (#Ident "let'") label value body))))
## body
## (reverse (->pairs bindings)))
## [(list output) state])))
## (declare-macro let)
## (def (++-list xs ys)
## (case' xs
## #Nil
## ys
## (#Cons x xs*)
## (#Cons x (++-list xs* ys))))
## (def (map-list f xs)
## (case' xs
## #Nil
## #Nil
## (#Cons x xs*)
## (#Cons (f x) (map-list f xs*))))
#(
(def (untemplate-list untemplate tokens)
(case tokens
#Nil
(#Tag "Nil")
(#Cons token tokens')
(#Form (list (#Tag "Cons") (untemplate token) (untemplate-list untemplate tokens')))))
(def (untemplate token)
(case token
(#Bool elem)
(#Form (list (#Tag "Bool") (#Bool elem)))
(#Int elem)
(#Form (list (#Tag "Int") (#Int elem)))
(#Real elem)
(#Form (list (#Tag "Real") (#Real elem)))
(#Char elem)
(#Form (list (#Tag "Char") (#Char elem)))
(#Text elem)
(#Form (list (#Tag "Text") (#Text elem)))
(#Tag elem)
(#Form (list (#Tag "Tag") (#Text elem)))
(#Ident elem)
(#Form (list (#Tag "Ident") (#Text elem)))
(#Form (#Cons (#Ident "~") (#Cons unquoted #Nil)))
unquoted
(#Tuple elems)
(#Form (list (#Tag "Tuple") (untemplate-list untemplate elems)))
(#Form elems)
(#Form (list (#Tag "Form") (untemplate-list untemplate elems)))
))
## I/O
(def (print x)
(jvm;invokevirtual java.io.PrintStream "print" [Object]
(jvm;getstatic System out) [x]))
(def (println x)
(jvm;invokevirtual java.io.PrintStream "println" [Object]
(jvm;getstatic System out) [x]))
(def (' form)
(case form
(#Cons token #Nil)
(untemplate token)))
(declare-macro ')
(def (+ x y)
(jvm;iadd x y))
(def inc (+ 1))
(def length (fold (lambda' l (lambda' x (inc l))) 0))
(def (rem dividend divisor)
(jvm;irem dividend divisor))
(def (= x y)
(jvm;invokevirtual Object "equals" [Object]
x [y]))
(def (pairs list)
(case list
(#Cons x (#Cons y list*))
(#Cons [x y] (pairs list*))
_
#Nil))
(def (show x)
(jvm;invokevirtual Object "toString" []
x []))
(def (concat t1 t2)
(jvm;invokevirtual String "concat" [String]
t1 [t2]))
(def (range from to)
(if (= from to)
#Nil
(#Cons from (range (inc from) to))))
(def (text->list text)
(let' length (jvm;invokevirtual String "length" []
text [])
(map (lambda' idx
(jvm;invokevirtual String "charAt" [int]
text [idx]))
(range 0 length))))
(def (enumerate list)
(case (fold (lambda' state
(lambda' x
(case state
[idx list']
[(inc idx) (#Cons [idx x] list')])))
[0 #Nil]
list)
[_ list']
(reverse list')))
(def list-map #Nil)
(def (put key val map)
(case map
#Nil
(#Cons [key val] map)
(#Cons [?key ?val] map')
(if (= key ?key)
(#Cons [?key val] map')
(#Cons [?key ?val] (put key val map')))))
(def (get key map)
(case map
#Nil
#None
(#Cons [?key ?val] map')
(if (= key ?key)
(#Some ?val)
(get key map'))))
(def (show-kv kv)
(case kv
[?key ?val]
(fold concat "" (list "#" ?key " " (show ?val)))))
(def (interpose elem list)
(case list
(#Cons x (#Cons y list'))
(list+ x elem y (interpose elem list'))
_
list))
(def (show-list xs)
(case xs
#Nil
"#Nil"
(#Cons x xs')
(fold concat "" (list "(#Cons " (show x) " " (show-list xs') ")"))))
(defsig (Equal x)
(: = (-> x x Bool)))
(deftype Equal (All [x r] {#= (-> x x Bool) & r}))
(deftype Equal (All [x] {#= (-> x x Bool)}))
(deftype Equal (All [x] (Exists [r] {#= (-> x x Bool) & r})))
(defimpl (Equal Bool)
(def (= x y)
(case [x y]
[ true true] true
[false false] true
_ false)))
(def Equal_Bool
(struct {#= [(-> Bool Bool Bool)
(lambda [x y] ...)]}))
(: (~ g!Equal) (Equal Bool))
(: (~ g!Equal) {#= (-> Bool Bool Bool)})
(def (~ g!Equal)
{#= (lambda [x y] ...)})
(def Equal_List
(lambda [Equal_x]
(struct {#= })))
(: Equal_List
(All [x] (-> (Equal x) (Equal (List x)))))
(defimpl (All [x]
(=> [(Equal x)]
(Equal (List x))))
(def (= xs1 xs2)
(case [xs1 xs2]
[#Nil #Nil]
true
[(#Cons x1 xs1') (#Cons x2 xs2')]
(and (Equal_x x1 x2) (= xs1' xs2')))))
(All [x]
(-> (Equal x) (Equal (List x))))
(EqualList EqualBool) => {#= ...}
(: ops (List (Exists [a] [(-> Text a) (-> a [])])))
(def ops (list [(lambda [_] 10) (lambda [_] [])]
[(lambda [_] "") (lambda [_] [])]))
(case ops
#Nil
[]
(#Cons [f1 f2] ops')
(f2 (f1 "E")))
(defsig (Add x)
(: + (-> x x x)))
(defimpl AddInt (Add Int)
#defs
(def (+ x y)
(jvm;ladd x y)))
(defimpl (Add Int)
(def (+ x y)
(jvm;ladd x y)))
(: adder (All [x] (=> [(Add x)]
(-> x (-> x x)))))
(def (adder by)
(lambda [x] (+ by x)))
(adder AddInt 1) -> (lambda [x] ((get@ #+ AddInt) 1 x))
adder == (lambda [impl]
(case impl
{#+ +}
(lambda [x] (+ by x))))
(: calc (All [v]
(-> (-> v Int)
(| (#Add Int Int) (#Mul Int Int) & v)
Int)))
(def (calc backup expr)
(case expr
(#Add x y) (+ x y)
(#Mul x y) (* x y)
else (backup else)))
(defsig Yolo
(: lol? (-> Text Bool))
(: foo Int))
(defimpl Meme [Yolo]
(def (lol? _) true)
(def foo 10))
(defimpl Nyan [Yolo]
Meme
(def foo 20))
(list 1 2 3) == (#Cons 1 (#Cons 2 (#Cons 3 #Nil)))
(list+ 1 2 (list 3))
(defsig (Monoid a)
(: empty a)
(: ++ (BinaryOp a)))
(: concat (All [a]
(=> [(Monoid a)]
(-> (List a) a))))
(def (concat mon
xs)
(fold (:: mon #++) (:: mon #empty) xs))
(defstruct (Monoid Text)
(def empty "")
(def (++ x y)
...))
(defstruct (All [a]
(Monoid (List a)))
(def empty (list))
(def (++ xs ys)
(case xs
#Nil
ys
(#Cons x xs')
(#Cons x (++ xs' ys)))))
(: map (All [a b] (-> (-> a b) (List a) (List b))))
(defsig (Collection c)
(: add (All [x] (-> x (c x) (c x))))
(: length (All [x] (-> (c x) Int))))
(defclass (Stack s)
(: push (All [x] (-> x (s x) (s x))))
(: pop (All [x] (-> (s x) (s x))))
(: peek (All [x] (-> (s x) (Maybe x)))))
(deftype (BinaryOp t)
(-> t t t))
(defclass (Number n)
(: + (BinaryOp n))
(: - (BinaryOp n))
(: * (BinaryOp n))
(: / (BinaryOp n)))
(def (flip f)
(lambda [x y]
(f y x)))
(def (concat' xss)
(case (reverse xss)
#Nil
#Nil
(#Cons xs xss')
(fold (flip ++) xs xss')))
n + n*m
(with [AddInt]
(+ 10 20))
(:: AddInt (+ 10 20)) == ((get@ AddInt #=) 10 20)
(defimpl (Stack List)
(def (push x xs)
(#Cons x xs))
(def (pop xs)
(case xs
#Nil #Nil
(#Cons _ xs') xs'))
(def (peek xs)
(case xs
#Nil #None
(#Cons x _) (#Some x))))
(defsig (Functor f)
(: map (All [a b] (-> (-> a b) (f a) (f b)))))
(def (Functor f)
{#map (All [a b] (-> (-> a b) (f a) (f b)))})
(defimpl ListFunctor (Functor List)
(def (map func fa)
(case fa
#Nil
#Nil
(list a fa')
(list (func a) (map func fa')))))
(implicit ListFunctor
(map inc (list 1 2 3)))
(defsig (=> [(Functor m)]
(Monad m))
(: return (All [x] (-> x (m x))))
(: bind (All [a b] (-> (m a) (-> a (m b)) (m b)))))
(#User {#name Text #age Int})
(deftype User {#name Text #age Int})
(deftype User (& (#name Text)
(#age Int)))
(deftype User (All [r]
(& (#name Text)
(#age Int)
++ r)))
(def User (#Record (list ["name" Text] ["age" Int])))
(let [thunk (... (+ 5 6))]
(! thunk))
(Thunk Int)
(deftype Int&Bool [Int Bool])
(deftype (List a)
(| #Nil
(#Cons a (List a))))
(defclass (Equal a)
(: = (-> a a Bool)))
==
(deftype (Equal a)
{#= (-> a a Bool)})
(def Equals
(All [a] {#= (-> a a Bool)}))
(defimpl EqualBool [(Equal Bool)]
(def (= x y)
(if x
y
(not y))))
(def ... {#= (lambda [x y] (if x
y
(not y)))})
(def Class (All [I]
(Exists [S] (& (#state S)
(#methods (I S))))))
(definterface Vector
(: translate (BinaryOp Vector)))
(def Vector (Some [Vector]
(& (#translate (BinaryOp v)))))
(defclass Vector2D
{#x Real, #y Real}
(def (new-Vector2D x y)
{#x x, #y y})
Vector
(def (translate self offset)
(-> self
(update@ #x + (get@ offset #x))
(update@ #y + (get@ offset #y)))))
(def Vector2D
{#translate (: (lambda [self offset]
(-> self
(update@ #x +real (get@ offset #x))
(update@ #y +real (get@ offset #y))))
(BinaryOp {#x Real, #y Real}))})
(: new-Vector2D (-> Real Real [{#x Real, #y Real} (@class Vector2D)]))
(def (new-Vector2D x y)
[{#x x, #y y} Vector2D])
(defsig (Vector v)
(: translate (BinaryOp v))
(: scale (BinaryOp v)))
(def Vector (All Vector [v]
(& (#translate (BinaryOp v))
(#scale (BinaryOp v)))))
(defstruct Vector2D (Vector [Real Real])
(def (translate [x1 y1] [x2 y2])
[(+ x1 x2) (+ y1 y2)])
(def (scale [x1 y1] [x2 y2])
[(* x1 x2) (* y1 y2)]))
(def Vector2D (: {#translate (lambda [[x1 y1] [x2 y2]]
[(+real x1 x2) (+real y1 y2)])
#scale (lambda [[x1 y1] [x2 y2]]
[(*real x1 x2) (*real y1 y2)])}
(Vector [Real Real])))
(deftype (Stream a)
(| (#Cons a (Thunk (Stream a)))))
(: iterate (All [a] (-> (-> a a) a (Stream a))))
(def (iterate f init)
(list init (... (iterate f (f init)))))
(def (take n stream)
(if (<= n 0)
#Nil
(case stream
(#Cons x stream')
(#Cons x (take (dec n) stream')))))
(deftype (Stream a)
(All [b] (-> (-> a (Stream a b) b) b)))
(: iterate (All [a] (-> (-> a a) a (Stream a))))
(def (iterate f init)
(lambda [k]
(k init (iterate f (f init)))))
(def (repeat x)
(lambda [k] (k x (repeat x))))
(def (take n stream)
(if (<= n 0)
#Nil
(stream (lambda [x stream']
(#Cons x (take (dec n) stream'))))))
(defsig (Comonad w)
(: extract (All [a] (-> (w a) a)))
(: extend (All [a b] (-> (w a) (-> (w a) b) (w b)))))
(defstruct Stream (Comonad w)
(def (extract stream)
(stream (lambda [x _] x)))
(def (extend w f)
...))
(: fibonacci (Stream Int))
(def fibonacci ((lambda fibonacci [a b]
(lambda [k] (k a (fibonacci b (+ a b)))))
0 1))
(gen fibonacci [a 0 b 1]
(yield a (fibonacci b (+ a b))))
(defgen fibonacci [a 0 b 1]
(yield a (fibonacci b (+ a b))))
(gen fibonacci [a 0 b 1]
(yield a [b (+ a b)]))
## The dual of do-notation should be be-notation
(deftype (Stream a)
(| (#Cons a (Thunk (Stream a)))))
(defstruct (Functor Stream)
(def (map f s)
(lambda [k]
(stream (lambda [x stream']
(f s))))))
(deftype (Tape a)
(| (#Index (Thunk (Stream a)) a (Thunk (Stream a)))))
(deftype (Area a)
(| (#Cursor (Tape a) (Surreal a))))
(def (ints offset n)
(#Index (... (iterate (lambda [n'] (- n' offset)) n))
n
(... (iterate (+ offset) n))))
(def (reals offset x)
(#Cursor (ints offset x)
(reals (/ offset 10) 0)))
)#
#(
(deftype (Session i o s)
(All [s' r]
(-> (-> i s' r)
(-> o s [i s'])
r))
(All [s' s''] (-> (-> c s' [p s''])
(-> [] s [c s']) (Session c p s)
[p s'']))
(All [] (-> (-> c s p)
(-> p [])))
(All [r] (-> c (Session r p s) p)))
(Session Int [] (Session Int (Session [] Int <END>)))
(bind (session' [])
(lambda [x session']
(bind (session' [])
(lambda [y session'']
(session'' (+ x y))))))
(defstruct SessionMonad
(Monad Session)
(def (return v)
(lambda [k session]
(k v session)))
(def (bind step m-value)
(lambda [k session]
(let [[v session'] (m-value [] session)]
(k (step v) session')))))
## Not really "do"; but oh, well...
(deftype <NIL>
(| #Nil))
(deftype (HList h t)
(| (#Cons h t)))
(deftype (Session c p s)
(All [r] (-> c (-> p s r) r)))
(deftype (Session c p s)
(-> (-> p s c) c))
(deftype (? r s)
(Session r [] s))
(deftype (! w s)
(Session [] w s))
(deftype #rec <END>
(Session [] [] <END>))
(def <<
(lambda [k session]
(k [] session)))
(def (>> val)
(lambda [k session]
(session val k)))
(<$> << (>> 5))
(def (<$> consumer producer)
(producer [] consumer))
(HList Int (HList Int <NIL>))
(<.> (? Int) (? Int) (! Int) <END>)
(def fn-session
(do [x <<
y <<]
(>> (+ x y))))
(<.> (! Int) (! Int) (? Int) <END>)
(def call-session
(do [_ (>> 5)
_ (>> 10)]
<<))
(<$> fn-session call-session)
(def <<
(lambda [chan]
(chan (lambda []))))
(def (>> value)
(lambda [chan]
(chan value)))
)#
## (defsig (Equal a)
## (: = (-> a a Bool)))
## (: not= (All [a] (-> (Equal a) a a Bool)))
## (def (not= &Equal
## x x)
## (not (:: &Equal (= x x))))
## (defstruct Int
## []
## (Equal Int)
## (def (= x y)
## (zero? (- x y))))
## (defsig (Show a)
## (: show (-> a Text)))
## (defstruct (ListShow x)
## [&show (Show a)]
## (Show (List a))
## (def (show xs)
## (<> "(" (interpose ", " (map (:: &show show) xs)) ")")))
## (def ListShow
## (: (lambda [&show]
## {#show (lambda show [xs]
## (<> "(" (interpose ", " (map (:: &show show) xs)) ")"))})
## (-> (Show a) (Show (List a)))))
## (deftype (Identity a) a)
## (deftype (List a)
## (| #Nil
## (#Cons a (List a))))
## (def (ListT m)
## (All [a] (List (m a))))
## (ListT Identity)
## (defsig (Monad m)
## (: return (All [a] (-> a (m a))))
## (: bind (All [a b] (-> (-> a (m b)) (m a) (m b)))))
## (def Monad
## (All [m]
## (sig (: return (All [a] (-> a (m a))))
## (: bind (All [a b] (-> (-> a (m b)) (m a) (m b)))))))
## (defstruct Monad<Identity> (Monad Identity)
## (def (return x)
## x)
## (def (bind f x)
## (f x)))
## (: Monad<Identity> (Monad Identity))
## (def Monad<Identity>
## (struct
## (def (return x)
## x)
## (def (bind f x)
## (f x))))
## (defstruct Monad<List> (All [m] (-> (Monad m)
## (Monad (ListT m))))
## (def (return x)
## (list x))
## (def (bind f xs)
## (case xs
## #Nil #Nil
## (#Cons x xs') (#Cons (f x) (bind f xs')))))
## (deftype #rec Type
## ($data #Any
## #Nothing
## (#Data Text (List Type))
## (#Lambda Type Type)
## (#All (List [Text Type]) Text Text Type)
## (#Exists (List [Text Type]) Text Type)
## (#Lookup Text)
## (#Var Int)))
|