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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
|
## Copyright (c) Eduardo Julian. All rights reserved.
## The use and distribution terms for this software are covered by the
## Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
## which can be found in the file epl-v10.html at the root of this distribution.
## By using this software in any fashion, you are agreeing to be bound by
## the terms of this license.
## You must not remove this notice, or any other, from this software.
## First things first, must define functions
(_jvm_interface "lux.Function" []
(apply ["java.lang.Object"] "java.lang.Object" ["public" "abstract"]))
## (_jvm_class "lux.MyFunction" "java.lang.Object" ["lux.Function"]
## [(foo "java.lang.Object" ["public" "static"])]
## (<init> [] "void"
## ["public"]
## (_jvm_invokespecial java.lang.Object <init> [] this []))
## (apply [(arg "java.lang.Object")] "java.lang.Object"
## ["public"]
## "YOLO"))
## Basic types
(_lux_def Bool (#DataT "java.lang.Boolean"))
(_lux_export Bool)
(_lux_def Int (#DataT "java.lang.Long"))
(_lux_export Int)
(_lux_def Real (#DataT "java.lang.Double"))
(_lux_export Real)
(_lux_def Char (#DataT "java.lang.Character"))
(_lux_export Char)
(_lux_def Text (#DataT "java.lang.String"))
(_lux_export Text)
(_lux_def Void (#VariantT #Nil))
(_lux_export Void)
(_lux_def Ident (#TupleT (#Cons [Text (#Cons [Text #Nil])])))
(_lux_export Ident)
## (deftype (List a)
## (| #Nil
## (#Cons (, a (List a)))))
(_lux_def List
(#AllT [(#Some #Nil) "lux;List" "a"
(#VariantT (#Cons [["lux;Nil" (#TupleT #Nil)]
(#Cons [["lux;Cons" (#TupleT (#Cons [(#BoundT "a")
(#Cons [(#AppT [(#BoundT "lux;List") (#BoundT "a")])
#Nil])]))]
#Nil])]))]))
(_lux_export List)
## (deftype (Maybe a)
## (| #None
## (#Some a)))
(_lux_def Maybe
(#AllT [(#Some #Nil) "lux;Maybe" "a"
(#VariantT (#Cons [["lux;None" (#TupleT #Nil)]
(#Cons [["lux;Some" (#BoundT "a")]
#Nil])]))]))
(_lux_export Maybe)
## (deftype #rec Type
## (| (#DataT Text)
## (#TupleT (List Type))
## (#VariantT (List (, Text Type)))
## (#RecordT (List (, Text Type)))
## (#LambdaT (, Type Type))
## (#BoundT Text)
## (#VarT Int)
## (#AllT (, (Maybe (List (, Text Type))) Text Text Type))
## (#AppT (, Type Type))))
(_lux_def Type
(_lux_case (#AppT [(#BoundT "Type") (#BoundT "_")])
Type
(_lux_case (#AppT [List (#TupleT (#Cons [Text (#Cons [Type #Nil])]))])
TypeEnv
(#AppT [(#AllT [(#Some #Nil) "Type" "_"
(#VariantT (#Cons [["lux;DataT" Text]
(#Cons [["lux;TupleT" (#AppT [List Type])]
(#Cons [["lux;VariantT" TypeEnv]
(#Cons [["lux;RecordT" TypeEnv]
(#Cons [["lux;LambdaT" (#TupleT (#Cons [Type (#Cons [Type #Nil])]))]
(#Cons [["lux;BoundT" Text]
(#Cons [["lux;VarT" Int]
(#Cons [["lux;AllT" (#TupleT (#Cons [(#AppT [Maybe TypeEnv]) (#Cons [Text (#Cons [Text (#Cons [Type #Nil])])])]))]
(#Cons [["lux;AppT" (#TupleT (#Cons [Type (#Cons [Type #Nil])]))]
(#Cons [["lux;ExT" Int]
#Nil])])])])])])])])])]))])
Void]))))
(_lux_export Type)
## (deftype (Bindings k v)
## (& #counter Int
## #mappings (List (, k v))))
(_lux_def Bindings
(#AllT [(#Some #Nil) "lux;Bindings" "k"
(#AllT [#None "" "v"
(#RecordT (#Cons [["lux;counter" Int]
(#Cons [["lux;mappings" (#AppT [List
(#TupleT (#Cons [(#BoundT "k")
(#Cons [(#BoundT "v")
#Nil])]))])]
#Nil])]))])]))
## (deftype (Env k v)
## (& #name Text
## #inner-closures Int
## #locals (Bindings k v)
## #closure (Bindings k v)))
(_lux_def Env
(#AllT [(#Some #Nil) "lux;Env" "k"
(#AllT [#None "" "v"
(#RecordT (#Cons [["lux;name" Text]
(#Cons [["lux;inner-closures" Int]
(#Cons [["lux;locals" (#AppT [(#AppT [Bindings (#BoundT "k")])
(#BoundT "v")])]
(#Cons [["lux;closure" (#AppT [(#AppT [Bindings (#BoundT "k")])
(#BoundT "v")])]
#Nil])])])]))])]))
## (deftype Cursor
## (, Text Int Int))
(_lux_def Cursor
(#TupleT (#Cons [Text (#Cons [Int (#Cons [Int #Nil])])])))
## (deftype (Meta m v)
## (| (#Meta (, m v))))
(_lux_def Meta
(#AllT [(#Some #Nil) "lux;Meta" "m"
(#AllT [#None "" "v"
(#VariantT (#Cons [["lux;Meta" (#TupleT (#Cons [(#BoundT "m")
(#Cons [(#BoundT "v")
#Nil])]))]
#Nil]))])]))
(_lux_export Meta)
## (deftype (Syntax' w)
## (| (#BoolS Bool)
## (#IntS Int)
## (#RealS Real)
## (#CharS Char)
## (#TextS Text)
## (#SymbolS (, Text Text))
## (#TagS (, Text Text))
## (#FormS (List (w (Syntax' w))))
## (#TupleS (List (w (Syntax' w))))
## (#RecordS (List (, (w (Syntax' w)) (w (Syntax' w)))))))
(_lux_def Syntax'
(_lux_case (#AppT [(#BoundT "w")
(#AppT [(#BoundT "lux;Syntax'")
(#BoundT "w")])])
Syntax
(_lux_case (#AppT [List Syntax])
SyntaxList
(#AllT [(#Some #Nil) "lux;Syntax'" "w"
(#VariantT (#Cons [["lux;BoolS" Bool]
(#Cons [["lux;IntS" Int]
(#Cons [["lux;RealS" Real]
(#Cons [["lux;CharS" Char]
(#Cons [["lux;TextS" Text]
(#Cons [["lux;SymbolS" Ident]
(#Cons [["lux;TagS" Ident]
(#Cons [["lux;FormS" SyntaxList]
(#Cons [["lux;TupleS" SyntaxList]
(#Cons [["lux;RecordS" (#AppT [List (#TupleT (#Cons [Syntax (#Cons [Syntax #Nil])]))])]
#Nil])
])])])])])])])])])
)]))))
(_lux_export Syntax')
## (deftype Syntax
## (Meta Cursor (Syntax' (Meta Cursor))))
(_lux_def Syntax
(_lux_case (#AppT [Meta Cursor])
w
(#AppT [w (#AppT [Syntax' w])])))
(_lux_export Syntax)
(_lux_def SyntaxList (#AppT [List Syntax]))
## (deftype (Either l r)
## (| (#Left l)
## (#Right r)))
(_lux_def Either
(#AllT [(#Some #Nil) "lux;Either" "l"
(#AllT [#None "" "r"
(#VariantT (#Cons [["lux;Left" (#BoundT "l")]
(#Cons [["lux;Right" (#BoundT "r")]
#Nil])]))])]))
(_lux_export Either)
## (deftype (StateE s a)
## (-> s (Either Text (, s a))))
(_lux_def StateE
(#AllT [(#Some #Nil) "lux;StateE" "s"
(#AllT [#None "" "a"
(#LambdaT [(#BoundT "s")
(#AppT [(#AppT [Either Text])
(#TupleT (#Cons [(#BoundT "s")
(#Cons [(#BoundT "a")
#Nil])]))])])])]))
## (deftype Reader
## (List (Meta Cursor Text)))
(_lux_def Reader
(#AppT [List
(#AppT [(#AppT [Meta Cursor])
Text])]))
(_lux_export Reader)
## (deftype HostState
## (& #writer (^ org.objectweb.asm.ClassWriter)
## #loader (^ java.net.URLClassLoader)
## #classes (^ clojure.lang.Atom)))
(_lux_def HostState
(#RecordT (#Cons [["lux;writer" (#DataT "org.objectweb.asm.ClassWriter")]
(#Cons [["lux;loader" (#DataT "java.lang.ClassLoader")]
(#Cons [["lux;classes" (#DataT "clojure.lang.Atom")]
#Nil])])])))
## (deftype (DefData' m)
## (| #TypeD
## (#ValueD Type)
## (#MacroD m)
## (#AliasD Ident)))
(_lux_def DefData'
(#AllT [(#Some #Nil) "lux;DefData'" ""
(#VariantT (#Cons [["lux;TypeD" (#TupleT #Nil)]
(#Cons [["lux;ValueD" Type]
(#Cons [["lux;MacroD" (#BoundT "")]
(#Cons [["lux;AliasD" Ident]
#Nil])])])]))]))
## (deftype LuxVar
## (| (#Local Int)
## (#Global Ident)))
(_lux_def LuxVar
(#VariantT (#Cons [["lux;Local" Int]
(#Cons [["lux;Global" Ident]
#Nil])])))
(_lux_export LuxVar)
## (deftype #rec Compiler
## (& #source Reader
## #modules (List (, Text (List (, Text (, Bool (DefData' (-> (List Syntax) (StateE Compiler (List Syntax)))))))))
## #module-aliases (List Void)
## #envs (List (Env Text (, LuxVar Type)))
## #types (Bindings Int Type)
## #host HostState))
(_lux_def Compiler
(#AppT [(#AllT [(#Some #Nil) "lux;Compiler" ""
(#RecordT (#Cons [["lux;source" Reader]
(#Cons [["lux;modules" (#AppT [List (#TupleT (#Cons [Text
(#Cons [(#AppT [List (#TupleT (#Cons [Text
(#Cons [(#TupleT (#Cons [Bool (#Cons [(#AppT [DefData' (#LambdaT [SyntaxList
(#AppT [(#AppT [StateE (#AppT [(#BoundT "lux;Compiler")
(#BoundT "")])])
SyntaxList])])])
#Nil])]))
#Nil])]))])
#Nil])]))])]
(#Cons [["lux;module-aliases" (#AppT [List Void])]
(#Cons [["lux;envs" (#AppT [List (#AppT [(#AppT [Env Text])
(#TupleT (#Cons [LuxVar (#Cons [Type #Nil])]))])])]
(#Cons [["lux;types" (#AppT [(#AppT [Bindings Int]) Type])]
(#Cons [["lux;host" HostState]
(#Cons [["lux;seed" Int]
#Nil])])])])])])]))])
Void]))
(_lux_export Compiler)
## (deftype Macro
## (-> (List Syntax) (StateE Compiler (List Syntax))))
(_lux_def Macro
(#LambdaT [SyntaxList
(#AppT [(#AppT [StateE Compiler])
SyntaxList])]))
(_lux_export Macro)
## Base functions & macros
## (def (_meta data)
## (-> (Syntax' (Meta Cursor)) Syntax)
## (#Meta [["" -1 -1] data]))
(_lux_def _meta
(_lux_: (#LambdaT [(#AppT [Syntax'
(#AppT [Meta Cursor])])
Syntax])
(_lux_lambda _ data
(#Meta [["" -1 -1] data]))))
## (def (return x)
## (All [a]
## (-> a Compiler
## (Either Text (, Compiler a))))
## ...)
(_lux_def return
(_lux_: (#AllT [(#Some #Nil) "" "a"
(#LambdaT [(#BoundT "a")
(#LambdaT [Compiler
(#AppT [(#AppT [Either Text])
(#TupleT (#Cons [Compiler
(#Cons [(#BoundT "a")
#Nil])]))])])])])
(_lux_lambda _ val
(_lux_lambda _ state
(#Right [state val])))))
## (def (fail msg)
## (All [a]
## (-> Text Compiler
## (Either Text (, Compiler a))))
## ...)
(_lux_def fail
(_lux_: (#AllT [(#Some #Nil) "" "a"
(#LambdaT [Text
(#LambdaT [Compiler
(#AppT [(#AppT [Either Text])
(#TupleT (#Cons [Compiler
(#Cons [(#BoundT "a")
#Nil])]))])])])])
(_lux_lambda _ msg
(_lux_lambda _ state
(#Left msg)))))
(_lux_def $text
(_lux_: (#LambdaT [Text Syntax])
(_lux_lambda _ text
(_meta (#TextS text)))))
(_lux_def $symbol
(_lux_: (#LambdaT [Ident Syntax])
(_lux_lambda _ ident
(_meta (#SymbolS ident)))))
(_lux_def $tag
(_lux_: (#LambdaT [Ident Syntax])
(_lux_lambda _ ident
(_meta (#TagS ident)))))
(_lux_def $form
(_lux_: (#LambdaT [(#AppT [List Syntax]) Syntax])
(_lux_lambda _ tokens
(_meta (#FormS tokens)))))
(_lux_def $tuple
(_lux_: (#LambdaT [(#AppT [List Syntax]) Syntax])
(_lux_lambda _ tokens
(_meta (#TupleS tokens)))))
(_lux_def $record
(_lux_: (#LambdaT [(#AppT [List (#TupleT (#Cons [Syntax (#Cons [Syntax #Nil])]))]) Syntax])
(_lux_lambda _ tokens
(_meta (#RecordS tokens)))))
(_lux_def let'
(_lux_: Macro
(_lux_lambda _ tokens
(_lux_case tokens
(#Cons [lhs (#Cons [rhs (#Cons [body #Nil])])])
(return (_lux_: SyntaxList
(#Cons [($form (#Cons [($symbol ["" "_lux_case"])
(#Cons [rhs (#Cons [lhs (#Cons [body #Nil])])])]))
#Nil])))
_
(fail "Wrong syntax for let'")))))
(_lux_declare-macro let')
(_lux_def lambda'
(_lux_: Macro
(_lux_lambda _ tokens
(_lux_case tokens
(#Cons [(#Meta [_ (#TupleS (#Cons [arg args']))]) (#Cons [body #Nil])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_lambda"]))
(#Cons [(_meta (#SymbolS ["" ""]))
(#Cons [arg
(#Cons [(_lux_case args'
#Nil
body
_
(_meta (#FormS (#Cons [(_meta (#SymbolS ["lux" "lambda'"]))
(#Cons [(_meta (#TupleS args'))
(#Cons [body #Nil])])]))))
#Nil])])])])))
#Nil])))
(#Cons [(#Meta [_ (#SymbolS self)]) (#Cons [(#Meta [_ (#TupleS (#Cons [arg args']))]) (#Cons [body #Nil])])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_lambda"]))
(#Cons [(_meta (#SymbolS self))
(#Cons [arg
(#Cons [(_lux_case args'
#Nil
body
_
(_meta (#FormS (#Cons [(_meta (#SymbolS ["lux" "lambda'"]))
(#Cons [(_meta (#TupleS args'))
(#Cons [body #Nil])])]))))
#Nil])])])])))
#Nil])))
_
(fail "Wrong syntax for lambda")))))
(_lux_declare-macro lambda')
(_lux_def def'
(_lux_: Macro
(lambda' [tokens]
(_lux_case tokens
(#Cons [(#Meta [_ (#TagS ["" "export"])])
(#Cons [(#Meta [_ (#FormS (#Cons [name args]))])
(#Cons [type (#Cons [body #Nil])])])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_def"]))
(#Cons [name
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_:"]))
(#Cons [type
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["lux" "lambda'"]))
(#Cons [name
(#Cons [(_meta (#TupleS args))
(#Cons [body #Nil])])])])))
#Nil])])])))
#Nil])])])))
(#Cons [(_meta (#FormS (#Cons [($symbol ["" "_lux_export"]) (#Cons [name #Nil])])))
#Nil])])))
(#Cons [(#Meta [_ (#TagS ["" "export"])]) (#Cons [name (#Cons [type (#Cons [body #Nil])])])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_def"]))
(#Cons [name
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_:"]))
(#Cons [type
(#Cons [body
#Nil])])])))
#Nil])])])))
(#Cons [(_meta (#FormS (#Cons [($symbol ["" "_lux_export"]) (#Cons [name #Nil])])))
#Nil])])))
(#Cons [(#Meta [_ (#FormS (#Cons [name args]))])
(#Cons [type (#Cons [body #Nil])])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_def"]))
(#Cons [name
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_:"]))
(#Cons [type
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["lux" "lambda'"]))
(#Cons [name
(#Cons [(_meta (#TupleS args))
(#Cons [body #Nil])])])])))
#Nil])])])))
#Nil])])])))
#Nil])))
(#Cons [name (#Cons [type (#Cons [body #Nil])])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_def"]))
(#Cons [name
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["" "_lux_:"]))
(#Cons [type
(#Cons [body
#Nil])])])))
#Nil])])])))
#Nil])))
_
(fail "Wrong syntax for def")
))))
(_lux_declare-macro def')
(def' #export (defmacro tokens)
Macro
(_lux_case tokens
(#Cons [(#Meta [_ (#FormS (#Cons [name args]))]) (#Cons [body #Nil])])
(return (_lux_: SyntaxList
(#Cons [($form (#Cons [($symbol ["lux" "def'"])
(#Cons [($form (#Cons [name args]))
(#Cons [($symbol ["lux" "Macro"])
(#Cons [body
#Nil])])
])]))
(#Cons [($form (#Cons [($symbol ["" "_lux_declare-macro"]) (#Cons [name #Nil])]))
#Nil])])))
(#Cons [(#Meta [_ (#TagS ["" "export"])]) (#Cons [(#Meta [_ (#FormS (#Cons [name args]))]) (#Cons [body #Nil])])])
(return (_lux_: SyntaxList
(#Cons [($form (#Cons [($symbol ["lux" "def'"])
(#Cons [($tag ["" "export"])
(#Cons [($form (#Cons [name args]))
(#Cons [($symbol ["lux" "Macro"])
(#Cons [body
#Nil])])
])])]))
(#Cons [($form (#Cons [($symbol ["" "_lux_declare-macro"]) (#Cons [name #Nil])]))
#Nil])])))
_
(fail "Wrong syntax for defmacro")))
(_lux_declare-macro defmacro)
(defmacro #export (comment tokens)
(return (_lux_: SyntaxList #Nil)))
(defmacro (->' tokens)
(_lux_case tokens
(#Cons [input (#Cons [output #Nil])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#TagS ["lux" "LambdaT"]))
(#Cons [(_meta (#TupleS (#Cons [input (#Cons [output #Nil])])))
#Nil])])))
#Nil])))
(#Cons [input (#Cons [output others])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#TagS ["lux" "LambdaT"]))
(#Cons [(_meta (#TupleS (#Cons [input
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["lux" "->'"]))
(#Cons [output others])])))
#Nil])])))
#Nil])])))
#Nil])))
_
(fail "Wrong syntax for ->'")))
(defmacro (All' tokens)
(_lux_case tokens
(#Cons [(#Meta [_ (#TupleS #Nil)])
(#Cons [body #Nil])])
(return (_lux_: SyntaxList
(#Cons [body
#Nil])))
(#Cons [(#Meta [_ (#TupleS (#Cons [(#Meta [_ (#SymbolS ["" arg-name])]) other-args]))])
(#Cons [body #Nil])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#TagS ["lux" "AllT"]))
(#Cons [(_meta (#TupleS (#Cons [(_meta (#TagS ["lux" "None"]))
(#Cons [(_meta (#TextS ""))
(#Cons [(_meta (#TextS arg-name))
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["lux" "All'"]))
(#Cons [(_meta (#TupleS other-args))
(#Cons [body
#Nil])])])))
#Nil])])])])))
#Nil])])))
#Nil])))
_
(fail "Wrong syntax for All'")))
(defmacro (B' tokens)
(_lux_case tokens
(#Cons [(#Meta [_ (#SymbolS ["" bound-name])])
#Nil])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#TagS ["lux" "BoundT"]))
(#Cons [(_meta (#TextS bound-name))
#Nil])])))
#Nil])))
_
(fail "Wrong syntax for B'")))
(defmacro ($' tokens)
(_lux_case tokens
(#Cons [x #Nil])
(return tokens)
(#Cons [x (#Cons [y xs])])
(return (_lux_: SyntaxList
(#Cons [(_meta (#FormS (#Cons [(_meta (#SymbolS ["lux" "$'"]))
(#Cons [(_meta (#FormS (#Cons [(_meta (#TagS ["lux" "AppT"]))
(#Cons [(_meta (#TupleS (#Cons [x (#Cons [y #Nil])])))
#Nil])])))
xs])])))
#Nil])))
_
(fail "Wrong syntax for $'")))
(def' #export (foldL f init xs)
(All' [a b]
(->' (->' (B' a) (B' b) (B' a))
(B' a)
($' List (B' b))
(B' a)))
(_lux_case xs
#Nil
init
(#Cons [x xs'])
(foldL f (f init x) xs')))
(def' #export (foldR f init xs)
(All' [a b]
(->' (->' (B' b) (B' a) (B' a))
(B' a)
($' List (B' b))
(B' a)))
(_lux_case xs
#Nil
init
(#Cons [x xs'])
(f x (foldR f init xs'))))
(def' #export (reverse list)
(All' [a]
(->' ($' List (B' a)) ($' List (B' a))))
(foldL (_lux_: (All' [a]
(->' ($' List (B' a)) (B' a) ($' List (B' a))))
(lambda' [tail head]
(#Cons [head tail])))
#Nil
list))
(defmacro #export (list xs)
(return (_lux_: SyntaxList
(#Cons [(foldL (lambda' [tail head]
(_meta (#FormS (#Cons [(_meta (#TagS ["lux" "Cons"]))
(#Cons [(_meta (#TupleS (#Cons [head (#Cons [tail #Nil])])))
#Nil])]))))
(_meta (#TagS ["lux" "Nil"]))
(reverse xs))
#Nil]))))
(defmacro #export (list& xs)
(_lux_case (reverse xs)
(#Cons [last init])
(return (_lux_: SyntaxList
(list (foldL (lambda' [tail head]
(_meta (#FormS (list (_meta (#TagS ["lux" "Cons"]))
(_meta (#TupleS (list head tail)))))))
last
init))))
_
(fail "Wrong syntax for list&")))
(defmacro #export (lambda tokens)
(let' [name tokens'] (_lux_: (#TupleT (list Ident ($' List Syntax)))
(_lux_case tokens
(#Cons [(#Meta [_ (#SymbolS name)]) tokens'])
[name tokens']
_
[["" ""] tokens]))
(_lux_case tokens'
(#Cons [(#Meta [_ (#TupleS args)]) (#Cons [body #Nil])])
(_lux_case args
#Nil
(fail "lambda requires a non-empty arguments tuple.")
(#Cons [harg targs])
(return (_lux_: SyntaxList
(list ($form (list ($symbol ["" "_lux_lambda"])
($symbol name)
harg
(foldL (lambda' [body' arg]
($form (list ($symbol ["" "_lux_lambda"])
($symbol ["" ""])
arg
body')))
body
(reverse targs))))))))
_
(fail "Wrong syntax for lambda"))))
(defmacro (def'' tokens)
(_lux_case tokens
(#Cons [(#Meta [_ (#TagS ["" "export"])])
(#Cons [(#Meta [_ (#FormS (#Cons [name args]))])
(#Cons [type (#Cons [body #Nil])])])])
(return (_lux_: SyntaxList
(list ($form (list ($symbol ["" "_lux_def"])
name
($form (list ($symbol ["" "_lux_:"])
type
($form (list ($symbol ["lux" "lambda"])
name
($tuple args)
body))))))
($form (list ($symbol ["" "_lux_export"]) name)))))
(#Cons [(#Meta [_ (#TagS ["" "export"])]) (#Cons [name (#Cons [type (#Cons [body #Nil])])])])
(return (_lux_: SyntaxList
(list ($form (list ($symbol ["" "_lux_def"])
name
($form (list ($symbol ["" "_lux_:"])
type
body))))
($form (list ($symbol ["" "_lux_export"]) name)))))
(#Cons [(#Meta [_ (#FormS (#Cons [name args]))])
(#Cons [type (#Cons [body #Nil])])])
(return (_lux_: SyntaxList
(list ($form (list ($symbol ["" "_lux_def"])
name
($form (list ($symbol ["" "_lux_:"])
type
($form (list ($symbol ["lux" "lambda"])
name
($tuple args)
body)))))))))
(#Cons [name (#Cons [type (#Cons [body #Nil])])])
(return (_lux_: SyntaxList
(list ($form (list ($symbol ["" "_lux_def"])
name
($form (list ($symbol ["" "_lux_:"]) type body)))))))
_
(fail "Wrong syntax for def")
))
(def'' (as-pairs xs)
(All' [a]
(->' ($' List (B' a)) ($' List (#TupleT (list (B' a) (B' a))))))
(_lux_case xs
(#Cons [x (#Cons [y xs'])])
(#Cons [[x y] (as-pairs xs')])
_
#Nil))
(defmacro #export (let tokens)
(_lux_case tokens
(#Cons [(#Meta [_ (#TupleS bindings)]) (#Cons [body #Nil])])
(return (_lux_: SyntaxList
(list (foldL (_lux_: (->' Syntax (#TupleT (list Syntax Syntax))
Syntax)
(lambda [body binding]
(_lux_case binding
[label value]
(_meta (#FormS (list (_meta (#SymbolS ["lux" "let'"])) label value body))))))
body
(foldL (_lux_: (All' [a]
(->' ($' List (B' a)) (B' a) ($' List (B' a))))
(lambda [tail head] (#Cons [head tail])))
#Nil
(as-pairs bindings))))))
_
(fail "Wrong syntax for let")))
(def'' #export (map f xs)
(All' [a b]
(->' (->' (B' a) (B' b)) ($' List (B' a)) ($' List (B' b))))
(_lux_case xs
#Nil
#Nil
(#Cons [x xs'])
(#Cons [(f x) (map f xs')])))
(def'' #export (any? p xs)
(All' [a]
(->' (->' (B' a) Bool) ($' List (B' a)) Bool))
(_lux_case xs
#Nil
false
(#Cons [x xs'])
(_lux_case (p x)
true true
false (any? p xs'))))
(def'' (spliced? token)
(->' Syntax Bool)
(_lux_case token
(#Meta [_ (#FormS (#Cons [(#Meta [_ (#SymbolS ["" "~@"])]) (#Cons [_ #Nil])]))])
true
_
false))
(def'' (wrap-meta content)
(->' Syntax Syntax)
(_meta (#FormS (list (_meta (#TagS ["lux" "Meta"]))
(_meta (#TupleS (list (_meta (#TupleS (list (_meta (#TextS "")) (_meta (#IntS -1)) (_meta (#IntS -1)))))
content)))))))
(def'' (untemplate-list tokens)
(->' ($' List Syntax) Syntax)
(_lux_case tokens
#Nil
(_meta (#TagS ["lux" "Nil"]))
(#Cons [token tokens'])
(_meta (#FormS (list (_meta (#TagS ["lux" "Cons"]))
(_meta (#TupleS (list token (untemplate-list tokens')))))))))
(def'' (list:++ xs ys)
(All' [a] (->' ($' List (B' a)) ($' List (B' a)) ($' List (B' a))))
(_lux_case xs
(#Cons [x xs'])
(#Cons [x (list:++ xs' ys)])
#Nil
ys))
(defmacro #export ($ tokens)
(_lux_case tokens
(#Cons [op (#Cons [init args])])
(return (_lux_: SyntaxList
(list (foldL (lambda [a1 a2] ($form (list op a1 a2)))
init
args))))
_
(fail "Wrong syntax for $")))
(def'' (splice untemplate tag elems)
(->' (->' Syntax Syntax) Syntax ($' List Syntax) Syntax)
(_lux_case (any? spliced? elems)
true
(let [elems' (map (_lux_: (->' Syntax Syntax)
(lambda [elem]
(_lux_case elem
(#Meta [_ (#FormS (#Cons [(#Meta [_ (#SymbolS ["" "~@"])]) (#Cons [spliced #Nil])]))])
spliced
_
($form (list ($symbol ["" "_lux_:"])
($symbol ["lux" "SyntaxList"])
($form (list ($symbol ["lux" "list"]) (untemplate elem))))))))
elems)]
(wrap-meta ($form (list tag
($form (list& ($symbol ["lux" "$"])
($symbol ["lux" "list:++"])
elems'))))))
false
(wrap-meta ($form (list tag (untemplate-list (map untemplate elems)))))))
(def'' (untemplate subst token)
(->' Text Syntax Syntax)
(_lux_case token
(#Meta [_ (#BoolS value)])
(wrap-meta ($form (list ($tag ["lux" "BoolS"]) (_meta (#BoolS value)))))
(#Meta [_ (#IntS value)])
(wrap-meta ($form (list ($tag ["lux" "IntS"]) (_meta (#IntS value)))))
(#Meta [_ (#RealS value)])
(wrap-meta ($form (list ($tag ["lux" "RealS"]) (_meta (#RealS value)))))
(#Meta [_ (#CharS value)])
(wrap-meta ($form (list ($tag ["lux" "CharS"]) (_meta (#CharS value)))))
(#Meta [_ (#TextS value)])
(wrap-meta ($form (list ($tag ["lux" "TextS"]) (_meta (#TextS value)))))
(#Meta [_ (#TagS [module name])])
(let [module' (_lux_case module
""
subst
_
module)]
(wrap-meta ($form (list ($tag ["lux" "TagS"]) ($tuple (list ($text module') ($text name)))))))
(#Meta [_ (#SymbolS [module name])])
(let [module' (_lux_case module
""
subst
_
module)]
(wrap-meta ($form (list ($tag ["lux" "SymbolS"]) ($tuple (list ($text module') ($text name)))))))
(#Meta [_ (#TupleS elems)])
(splice (untemplate subst) ($tag ["lux" "TupleS"]) elems)
(#Meta [_ (#FormS (#Cons [(#Meta [_ (#SymbolS ["" "~"])]) (#Cons [unquoted #Nil])]))])
unquoted
(#Meta [_ (#FormS elems)])
(splice (untemplate subst) ($tag ["lux" "FormS"]) elems)
(#Meta [_ (#RecordS fields)])
(wrap-meta ($form (list ($tag ["lux" "RecordS"])
(untemplate-list (map (_lux_: (->' (#TupleT (list Syntax Syntax)) Syntax)
(lambda [kv]
(let [[k v] kv]
($tuple (list (untemplate subst k) (untemplate subst v))))))
fields)))))
))
(defmacro (`' tokens)
(_lux_case tokens
(#Cons [template #Nil])
(return (_lux_: SyntaxList
(list (untemplate "" template))))
_
(fail "Wrong syntax for `'")))
(defmacro #export (|> tokens)
(_lux_case tokens
(#Cons [init apps])
(return (_lux_: SyntaxList
(list (foldL (_lux_: (->' Syntax Syntax Syntax)
(lambda [acc app]
(_lux_case app
(#Meta [_ (#FormS parts)])
($form (list:++ parts (list acc)))
_
(`' ((~ app) (~ acc))))))
init
apps))))
_
(fail "Wrong syntax for |>")))
(defmacro #export (if tokens)
(_lux_case tokens
(#Cons [test (#Cons [then (#Cons [else #Nil])])])
(return (_lux_: SyntaxList
(list (`' (_lux_case (~ test)
true (~ then)
false (~ else))))))
_
(fail "Wrong syntax for if")))
## (deftype (Lux a)
## (-> Compiler (Either Text (, Compiler a))))
(def'' #export Lux
Type
(All' [a]
(->' Compiler ($' Either Text (#TupleT (list Compiler (B' a)))))))
## (defsig (Monad m)
## (: (All [a] (-> a (m a)))
## return)
## (: (All [a b] (-> (-> a (m b)) (m a) (m b)))
## bind))
(def'' Monad
Type
(All' [m]
(#RecordT (list ["lux;return" (All' [a] (->' (B' a) ($' (B' m) (B' a))))]
["lux;bind" (All' [a b] (->' (->' (B' a) ($' (B' m) (B' b)))
($' (B' m) (B' a))
($' (B' m) (B' b))))]))))
(def'' Maybe:Monad
($' Monad Maybe)
{#lux;return
(lambda return [x]
(#Some x))
#lux;bind
(lambda [f ma]
(_lux_case ma
#None #None
(#Some a) (f a)))})
(def'' Lux:Monad
($' Monad Lux)
{#lux;return
(lambda [x]
(lambda [state]
(#Right [state x])))
#lux;bind
(lambda [f ma]
(lambda [state]
(_lux_case (ma state)
(#Left msg)
(#Left msg)
(#Right [state' a])
(f a state'))))})
(defmacro #export (^ tokens)
(_lux_case tokens
(#Cons [(#Meta [_ (#SymbolS ["" class-name])]) #Nil])
(return (_lux_: SyntaxList
(list (`' (#;DataT (~ (_meta (#TextS class-name))))))))
_
(fail "Wrong syntax for ^")))
(defmacro #export (-> tokens)
(_lux_case (reverse tokens)
(#Cons [output inputs])
(return (_lux_: SyntaxList
(list (foldL (_lux_: (->' Syntax Syntax Syntax)
(lambda [o i] (`' (#;LambdaT [(~ i) (~ o)]))))
output
inputs))))
_
(fail "Wrong syntax for ->")))
(defmacro #export (, tokens)
(return (_lux_: SyntaxList
(list (`' (#;TupleT (;list (~@ tokens))))))))
(defmacro (do tokens)
(_lux_case tokens
(#Cons [monad (#Cons [(#Meta [_ (#TupleS bindings)]) (#Cons [body #Nil])])])
(let [body' (foldL (_lux_: (-> Syntax (, Syntax Syntax) Syntax)
(lambda [body' binding]
(let [[var value] binding]
(_lux_case var
(#Meta [_ (#TagS ["" "let"])])
(`' (;let (~ value) (~ body')))
_
(`' (;bind (_lux_lambda (~ ($symbol ["" ""]))
(~ var)
(~ body'))
(~ value)))))))
body
(reverse (as-pairs bindings)))]
(return (_lux_: SyntaxList
(list (`' (_lux_case (~ monad)
{#;return ;return #;bind ;bind}
(~ body')))))))
_
(fail "Wrong syntax for do")))
(def'' (map% m f xs)
## (All [m a b]
## (-> (Monad m) (-> a (m b)) (List a) (m (List b))))
(All' [m a b]
(-> ($' Monad (B' m))
(-> (B' a) ($' (B' m) (B' b)))
($' List (B' a))
($' (B' m) ($' List (B' b)))))
(let [{#;return ;return #;bind _} m]
(_lux_case xs
#Nil
(;return (_lux_: List #Nil))
(#Cons [x xs'])
(do m
[y (f x)
ys (map% m f xs')]
(;return (_lux_: List (#Cons [y ys]))))
)))
(def'' #export (. f g)
(All' [a b c]
(-> (-> (B' b) (B' c)) (-> (B' a) (B' b)) (-> (B' a) (B' c))))
(lambda [x]
(f (g x))))
(def'' (get-ident x)
(-> Syntax ($' Maybe Text))
(_lux_case x
(#Meta [_ (#SymbolS ["" sname])])
(#Some sname)
_
#None))
(def'' (tuple->list tuple)
(-> Syntax ($' Maybe ($' List Syntax)))
(_lux_case tuple
(#Meta [_ (#TupleS members)])
(#Some members)
_
#None))
(def'' RepEnv
Type
($' List (, Text Syntax)))
(def'' (make-env xs ys)
(-> ($' List Text) ($' List Syntax) RepEnv)
(_lux_case (_lux_: (, ($' List Text) ($' List Syntax))
[xs ys])
[(#Cons [x xs']) (#Cons [y ys'])]
(#Cons [[x y] (make-env xs' ys')])
_
#Nil))
(def'' (text:= x y)
(-> Text Text Bool)
(_jvm_invokevirtual java.lang.Object equals [java.lang.Object]
x [y]))
(def'' (get-rep key env)
(-> Text RepEnv ($' Maybe Syntax))
(_lux_case env
#Nil
#None
(#Cons [[k v] env'])
(if (text:= k key)
(#Some v)
(get-rep key env'))))
(def'' (apply-template env template)
(-> RepEnv Syntax Syntax)
(_lux_case template
(#Meta [_ (#SymbolS ["" sname])])
(_lux_case (get-rep sname env)
(#Some subst)
subst
_
template)
(#Meta [_ (#TupleS elems)])
($tuple (map (apply-template env) elems))
(#Meta [_ (#FormS elems)])
($form (map (apply-template env) elems))
(#Meta [_ (#RecordS members)])
($record (map (_lux_: (-> (, Syntax Syntax) (, Syntax Syntax))
(lambda [kv]
(let [[slot value] kv]
[(apply-template env slot) (apply-template env value)])))
members))
_
template))
(def'' (join-map f xs)
(All' [a b]
(-> (-> (B' a) ($' List (B' b))) ($' List (B' a)) ($' List (B' b))))
(_lux_case xs
#Nil
#Nil
(#Cons [x xs'])
(list:++ (f x) (join-map f xs'))))
(defmacro #export (do-template tokens)
(_lux_case tokens
(#Cons [(#Meta [_ (#TupleS bindings)]) (#Cons [(#Meta [_ (#TupleS templates)]) data])])
(_lux_case (_lux_: (, ($' Maybe ($' List Text)) ($' Maybe ($' List ($' List Syntax))))
[(map% Maybe:Monad get-ident bindings)
(map% Maybe:Monad tuple->list data)])
[(#Some bindings') (#Some data')]
(let [apply (_lux_: (-> RepEnv ($' List Syntax))
(lambda [env] (map (apply-template env) templates)))]
(|> data'
(join-map (. apply (make-env bindings')))
return))
_
(fail "All the do-template bindigns must be symbols."))
_
(fail "Wrong syntax for do-template")))
(do-template [<name> <cmp> <type>]
[(def'' #export (<name> x y)
(-> <type> <type> Bool)
(<cmp> x y))]
[int:= _jvm_leq Int]
[int:> _jvm_lgt Int]
[int:< _jvm_llt Int]
[real:= _jvm_deq Real]
[real:> _jvm_dgt Real]
[real:< _jvm_dlt Real]
)
(do-template [<name> <cmp> <type>]
[(def'' #export (<name> x y)
(-> <type> <type> <type>)
(<cmp> x y))]
[int:+ _jvm_ladd Int]
[int:- _jvm_lsub Int]
[int:* _jvm_lmul Int]
[int:/ _jvm_ldiv Int]
[int:% _jvm_lrem Int]
[real:+ _jvm_dadd Real]
[real:- _jvm_dsub Real]
[real:* _jvm_dmul Real]
[real:/ _jvm_ddiv Real]
[real:% _jvm_drem Real]
)
(def'' (multiple? div n)
(-> Int Int Bool)
(int:= 0 (int:% n div)))
(def'' #export (length list)
(-> List Int)
(foldL (lambda [acc _] (int:+ 1 acc)) 0 list))
(def'' #export (not x)
(-> Bool Bool)
(if x false true))
(def'' #export (text:++ x y)
(-> Text Text Text)
(_jvm_invokevirtual java.lang.String concat [java.lang.String]
x [y]))
(def'' (ident->text ident)
(-> Ident Text)
(let [[module name] ident]
($ text:++ module ";" name)))
(def'' (replace-syntax reps syntax)
(-> RepEnv Syntax Syntax)
(_lux_case syntax
(#Meta [_ (#SymbolS ["" name])])
(_lux_case (get-rep name reps)
(#Some replacement)
replacement
#None
syntax)
(#Meta [_ (#FormS parts)])
(#Meta [_ (#FormS (map (replace-syntax reps) parts))])
(#Meta [_ (#TupleS members)])
(#Meta [_ (#TupleS (map (replace-syntax reps) members))])
(#Meta [_ (#RecordS slots)])
(#Meta [_ (#RecordS (map (_lux_: (-> (, Syntax Syntax) (, Syntax Syntax))
(lambda [slot]
(let [[k v] slot]
[(replace-syntax reps k) (replace-syntax reps v)])))
slots))])
_
syntax)
)
(defmacro #export (All tokens)
(let [[self-ident tokens'] (_lux_: (, Text SyntaxList)
(_lux_case tokens
(#Cons [(#Meta [_ (#SymbolS ["" self-ident])]) tokens'])
[self-ident tokens']
_
["" tokens]))]
(_lux_case tokens'
(#Cons [(#Meta [_ (#TupleS args)]) (#Cons [body #Nil])])
(_lux_case (map% Maybe:Monad get-ident args)
(#Some idents)
(_lux_case idents
#Nil
(return (_lux_: SyntaxList
(list body)))
(#Cons [harg targs])
(let [replacements (map (_lux_: (-> Text (, Text Syntax))
(lambda [ident] [ident (`' (#;BoundT (~ ($text ident))))]))
(list& self-ident idents))
body' (foldL (_lux_: (-> Syntax Text Syntax)
(lambda [body' arg']
(`' (#;AllT [#;None "" (~ ($text arg')) (~ body')]))))
(replace-syntax replacements body)
(reverse targs))]
(return (_lux_: SyntaxList
(list (`' (#;AllT [(#;Some #;Nil) (~ ($text self-ident)) (~ ($text harg)) (~ body')])))))))
#None
(fail "'All' arguments must be symbols."))
_
(fail "Wrong syntax for All"))
))
(def'' (get k plist)
(All [a]
(-> Text ($' List (, Text a)) ($' Maybe a)))
(_lux_case plist
(#Cons [[k' v] plist'])
(if (text:= k k')
(#Some v)
(get k plist'))
#Nil
#None))
(def'' #export (get-module-name state)
($' Lux Text)
(_lux_case state
{#source source #modules modules #module-aliases module-aliases
#envs envs #types types #host host
#seed seed}
(_lux_case (reverse envs)
#Nil
(#Left "Can't get the module name without a module!")
(#Cons [{#name module-name #inner-closures _ #locals _ #closure _} _])
(#Right [state module-name]))))
(def'' (find-macro' modules current-module module name)
(-> ($' List (, Text ($' List (, Text (, Bool ($' DefData' (-> ($' List Syntax) ($' StateE Compiler ($' List Syntax)))))))))
Text Text Text
($' Maybe Macro))
(do Maybe:Monad
[bindings (get module modules)
gdef (get name bindings)]
(_lux_case (_lux_: (, Bool ($' DefData' Macro)) gdef)
[exported? (#MacroD macro')]
(if exported?
(#Some macro')
(if (text:= module current-module)
(#Some macro')
#None))
[_ (#AliasD [r-module r-name])]
(find-macro' modules current-module r-module r-name)
_
#None)))
(def'' #export (find-macro ident)
(-> Ident ($' Lux ($' Maybe Macro)))
(do Lux:Monad
[current-module get-module-name]
(let [[module name] ident]
(lambda [state]
(_lux_case state
{#source source #modules modules #module-aliases module-aliases
#envs envs #types types #host host
#seed seed}
(#Right [state (find-macro' modules current-module module name)]))))))
(def'' (list:join xs)
(All [a]
(-> ($' List ($' List a)) ($' List a)))
(foldL list:++ #Nil xs))
## (def'' #export (normalize ident)
## (-> Ident ($' Lux Ident))
## (_lux_case ident
## ["" name]
## (do Lux:Monad
## [module-name get-module-name]
## (;return (: Ident [module-name name])))
## _
## (return ident)))
(def'' #export (normalize ident state)
(-> Ident ($' Lux Ident))
(_lux_case ident
["" name]
(_lux_case state
{#source source #modules modules #module-aliases module-aliases
#envs envs #types types #host host
#seed seed}
(_lux_case (reverse envs)
#Nil
(#Left "Can't normalize Ident without a global environment.")
(#Cons [{#name prefix #inner-closures _ #locals _ #closure _} _])
(#Right [state [prefix name]])))
_
(#Right [state ident])))
(defmacro #export (| tokens)
(do Lux:Monad
[pairs (map% Lux:Monad
(_lux_: (-> Syntax ($' Lux Syntax))
(lambda [token]
(_lux_case token
(#Meta [_ (#TagS ident)])
(do Lux:Monad
[ident (normalize ident)]
(;return (_lux_: Syntax (`' [(~ ($text (ident->text ident))) (;,)]))))
(#Meta [_ (#FormS (#Cons [(#Meta [_ (#TagS ident)]) (#Cons [value #Nil])]))])
(do Lux:Monad
[ident (normalize ident)]
(;return (_lux_: Syntax (`' [(~ ($text (ident->text ident))) (~ value)]))))
_
(fail "Wrong syntax for |"))))
tokens)]
(;return (_lux_: SyntaxList
(list (`' (#;VariantT (;list (~@ pairs)))))))))
(defmacro #export (& tokens)
(if (not (multiple? 2 (length tokens)))
(fail "& expects an even number of arguments.")
(do Lux:Monad
[pairs (map% Lux:Monad
(_lux_: (-> (, Syntax Syntax) ($' Lux Syntax))
(lambda [pair]
(_lux_case pair
[(#Meta [_ (#TagS ident)]) value]
(do Lux:Monad
[ident (normalize ident)]
(;return (_lux_: Syntax (`' [(~ ($text (ident->text ident))) (~ value)]))))
_
(fail "Wrong syntax for &"))))
(as-pairs tokens))]
(;return (_lux_: SyntaxList
(list (`' (#;RecordT (;list (~@ pairs))))))))))
(def'' #export (->text x)
(-> (^ java.lang.Object) Text)
(_jvm_invokevirtual java.lang.Object toString [] x []))
(def'' #export (interpose sep xs)
(All [a]
(-> a ($' List a) ($' List a)))
(_lux_case xs
#Nil
xs
(#Cons [x #Nil])
xs
(#Cons [x xs'])
(list& x sep (interpose sep xs'))))
(def'' #export (syntax:show syntax)
(-> Syntax Text)
(_lux_case syntax
(#Meta [_ (#BoolS value)])
(->text value)
(#Meta [_ (#IntS value)])
(->text value)
(#Meta [_ (#RealS value)])
(->text value)
(#Meta [_ (#CharS value)])
($ text:++ "#\"" (->text value) "\"")
(#Meta [_ (#TextS value)])
value
(#Meta [_ (#SymbolS ident)])
(ident->text ident)
(#Meta [_ (#TagS ident)])
(text:++ "#" (ident->text ident))
(#Meta [_ (#TupleS members)])
($ text:++ "[" (|> members (map syntax:show) (interpose " ") (foldL text:++ "")) "]")
(#Meta [_ (#FormS members)])
($ text:++ "(" (|> members (map syntax:show) (interpose " ") (foldL text:++ "")) ")")
(#Meta [_ (#RecordS slots)])
($ text:++ "{"
(|> slots
(map (_lux_: (-> (, Syntax Syntax) Text)
(lambda [slot]
(let [[k v] slot]
($ text:++ (syntax:show k) " " (syntax:show v))))))
(interpose " ")
(foldL text:++ ""))
"}")
))
(def'' #export (macro-expand syntax)
(-> Syntax ($' Lux ($' List Syntax)))
(_lux_case syntax
(#Meta [_ (#FormS (#Cons [(#Meta [_ (#SymbolS macro-name)]) args]))])
(do Lux:Monad
[macro-name' (normalize macro-name)
?macro (find-macro (_lux_: Ident macro-name'))]
(_lux_case (_lux_: ($' Maybe Macro) ?macro)
(#Some macro)
(do Lux:Monad
[expansion (macro args)
expansion' (map% Lux:Monad macro-expand (_lux_: SyntaxList expansion))]
(;return (_lux_: SyntaxList (list:join (_lux_: ($' List SyntaxList) expansion')))))
#None
(do Lux:Monad
[parts' (map% Lux:Monad macro-expand (list& ($symbol macro-name) args))]
(;return (_lux_: SyntaxList (list ($form (list:join (_lux_: ($' List SyntaxList) parts')))))))))
(#Meta [_ (#FormS (#Cons [harg targs]))])
(do Lux:Monad
[harg+ (macro-expand harg)
targs+ (map% Lux:Monad macro-expand (_lux_: SyntaxList targs))]
(;return (_lux_: SyntaxList (list ($form (list:++ harg+ (list:join (_lux_: ($' List SyntaxList) targs+))))))))
(#Meta [_ (#TupleS members)])
(do Lux:Monad
[members' (map% Lux:Monad macro-expand members)]
(;return (_lux_: SyntaxList (list ($tuple (list:join (_lux_: ($' List SyntaxList) members')))))))
_
(return (_lux_: SyntaxList (list syntax)))))
(def'' (walk-type type)
(-> Syntax Syntax)
(_lux_case type
(#Meta [_ (#FormS (#Cons [(#Meta [_ (#TagS tag)]) parts]))])
($form (#Cons [($tag tag) (map walk-type parts)]))
(#Meta [_ (#TupleS members)])
($tuple (map walk-type members))
(#Meta [_ (#FormS (#Cons [type-fn args]))])
(foldL (_lux_: (-> Syntax Syntax Syntax)
(lambda [type-fn arg] (`' (#;AppT [(~ type-fn) (~ arg)]))))
(walk-type type-fn)
(map walk-type args))
_
type))
(defmacro #export (type` tokens)
(_lux_case tokens
(#Cons [type #Nil])
(do Lux:Monad
[type+ (macro-expand type)]
(_lux_case (_lux_: SyntaxList type+)
(#Cons [type' #Nil])
(;return (_lux_: SyntaxList
(list (walk-type type'))))
_
(fail "type`: The expansion of the type-syntax had to yield a single element.")))
_
(fail "Wrong syntax for type`")))
(defmacro #export (: tokens)
(_lux_case tokens
(#Cons [type (#Cons [value #Nil])])
(return (_lux_: SyntaxList
(list (`' (_lux_: (;type` (~ type)) (~ value))))))
_
(fail "Wrong syntax for :")))
(defmacro #export (:! tokens)
(_lux_case tokens
(#Cons [type (#Cons [value #Nil])])
(return (: (List Syntax)
(list (`' (_lux_:! (;type` (~ type)) (~ value))))))
_
(fail "Wrong syntax for :!")))
(defmacro #export (deftype tokens)
(let [[export? tokens'] (: (, Bool (List Syntax))
(_lux_case tokens
(#Cons [(#Meta [_ (#TagS ["" "export"])]) tokens'])
[true tokens']
_
[false tokens]))
parts (: (Maybe (, Syntax (List Syntax) Syntax))
(_lux_case tokens'
(#Cons [(#Meta [_ (#SymbolS name)]) (#Cons [type #Nil])])
(#Some [($symbol name) #Nil type])
(#Cons [(#Meta [_ (#FormS (#Cons [(#Meta [_ (#SymbolS name)]) args]))]) (#Cons [type #Nil])])
(#Some [($symbol name) args type])
_
#None))]
(_lux_case parts
(#Some [name args type])
(let [with-export (: (List Syntax)
(if export?
(list (`' (_lux_export (~ name))))
#Nil))
type' (: Syntax
(_lux_case args
#Nil
type
_
(`' (;All (~ name) [(~@ args)] (~ type)))))]
(return (: (List Syntax)
(list& (`' (_lux_def (~ name) (;type` (~ type'))))
with-export))))
#None
(fail "Wrong syntax for deftype"))
))
(deftype #export (IO a)
(-> (,) a))
(defmacro #export (io tokens)
(_lux_case tokens
(#Cons [value #Nil])
(let [blank ($symbol ["" ""])]
(return (_lux_: SyntaxList
(list (`' (_lux_lambda (~ blank) (~ blank) (~ value)))))))
_
(fail "Wrong syntax for io")))
(defmacro #export (exec tokens)
(_lux_case (reverse tokens)
(#Cons [value actions])
(let [dummy ($symbol ["" ""])]
(return (_lux_: SyntaxList
(list (foldL (: (-> Syntax Syntax Syntax)
(lambda [post pre] (`' (_lux_case (~ pre) (~ dummy) (~ post)))))
value
actions)))))
_
(fail "Wrong syntax for exec")))
(defmacro #export (def tokens)
(let [[export? tokens'] (: (, Bool (List Syntax))
(_lux_case tokens
(#Cons [(#Meta [_ (#TagS ["" "export"])]) tokens'])
[true tokens']
_
[false tokens]))
parts (: (Maybe (, Syntax (List Syntax) (Maybe Syntax) Syntax))
(_lux_case tokens'
(#Cons [(#Meta [_ (#FormS (#Cons [name args]))]) (#Cons [type (#Cons [body #Nil])])])
(#Some [name args (#Some type) body])
(#Cons [name (#Cons [type (#Cons [body #Nil])])])
(#Some [name #Nil (#Some type) body])
(#Cons [(#Meta [_ (#FormS (#Cons [name args]))]) (#Cons [body #Nil])])
(#Some [name args #None body])
(#Cons [name (#Cons [body #Nil])])
(#Some [name #Nil #None body])
_
#None))]
(_lux_case parts
(#Some [name args ?type body])
(let [body' (: Syntax
(_lux_case args
#Nil
body
_
(`' (;lambda (~ name) [(~@ args)] (~ body)))))
body'' (: Syntax
(_lux_case ?type
(#Some type)
(`' (: (~ type) (~ body')))
#None
body'))]
(return (: (List Syntax)
(list& (`' (_lux_def (~ name) (~ body'')))
(if export?
(list (`' (_lux_export (~ name))))
#Nil)))))
#None
(fail "Wrong syntax for def"))))
(def (rejoin-pair pair)
(-> (, Syntax Syntax) (List Syntax))
(let [[left right] pair]
(list left right)))
(defmacro #export (case tokens)
(_lux_case tokens
(#Cons [value branches])
(do Lux:Monad
[expansions (map% Lux:Monad
(: (-> (, Syntax Syntax) (Lux (List (, Syntax Syntax))))
(lambda expander [branch]
(let [[pattern body] branch]
(_lux_case pattern
(#Meta [_ (#FormS (#Cons [(#Meta [_ (#SymbolS macro-name)]) macro-args]))])
(do Lux:Monad
[expansion (macro-expand ($form (list& ($symbol macro-name) body macro-args)))
expansions (map% Lux:Monad expander (as-pairs (: (List Syntax) expansion)))]
(;return (: (List (, Syntax Syntax)) (list:join (: (List (List (, Syntax Syntax))) expansions)))))
_
(;return (: (List (, Syntax Syntax)) (list branch)))))))
(as-pairs branches))]
(;return (_lux_: SyntaxList
(list (`' (_lux_case (~ value)
(~@ (|> (: (List (List (, Syntax Syntax))) expansions)
list:join (map rejoin-pair) list:join))))))))
_
(fail "Wrong syntax for case")))
(defmacro #export (\ tokens)
(case tokens
(#Cons [body (#Cons [pattern #Nil])])
(do Lux:Monad
[pattern+ (macro-expand pattern)]
(case (: (List Syntax) pattern+)
(#Cons [pattern' #Nil])
(;return (: (List Syntax)
(list pattern' body)))
_
(fail "\\ can only expand to 1 pattern.")))
_
(fail "Wrong syntax for \\")))
(defmacro #export (\or tokens)
(case tokens
(#Cons [body patterns])
(case patterns
#Nil
(fail "\\or can't have 0 patterns")
_
(do Lux:Monad
[patterns' (map% Lux:Monad macro-expand patterns)]
(;return (: (List Syntax)
(list:join (map (: (-> Syntax (List Syntax))
(lambda [pattern] (list pattern body)))
(list:join patterns')))))))
_
(fail "Wrong syntax for \\or")))
(do-template [<name> <offset>]
[(def #export <name> (int:+ <offset>))]
[inc 1]
[dec -1])
(def (int:show int)
(-> Int Text)
(_jvm_invokevirtual java.lang.Object toString []
int []))
(defmacro #export (` tokens)
(do Lux:Monad
[module-name get-module-name]
(case tokens
(\ (list template))
(;return (_lux_: SyntaxList
(list (untemplate module-name template))))
_
(fail "Wrong syntax for `"))))
(def #export (gensym prefix state)
(-> Text (Lux Syntax))
(case state
{#source source #modules modules #module-aliases module-aliases
#envs envs #types types #host host
#seed seed}
(#Right [{#source source #modules modules #module-aliases module-aliases
#envs envs #types types #host host
#seed (inc seed)}
($symbol ["__gensym__" (int:show seed)])])))
(def #export (macro-expand-1 token)
(-> Syntax (Lux Syntax))
(do Lux:Monad
[token+ (macro-expand token)]
(case (: (List Syntax) token+)
(\ (list token'))
(;return token')
_
(fail "Macro expanded to more than 1 element."))))
(defmacro #export (sig tokens)
(do Lux:Monad
[tokens' (map% Lux:Monad macro-expand-1 tokens)
members (map% Lux:Monad
(: (-> Syntax (Lux (, Ident Syntax)))
(lambda [token]
(case token
(\ (#Meta [_ (#FormS (list (#Meta [_ (#SymbolS ["" "_lux_:"])]) type (#Meta [_ (#SymbolS name)])))]))
(do Lux:Monad
[name' (normalize name)]
(;return (: (, Ident Syntax) [name' type])))
_
(fail "Signatures require typed members!"))))
tokens')]
(;return (: (List Syntax)
(list (`' (#;RecordT (list (~@ (map (: (-> (, Ident Syntax) Syntax)
(lambda [pair]
(let [[name type] pair]
(`' [(~ (|> name ident->text $text))
(~ type)]))))
members))))))))))
(defmacro #export (defsig tokens)
(let [[export? tokens'] (: (, Bool (List Syntax))
(case tokens
(\ (list& (#Meta [_ (#TagS ["" "export"])]) tokens'))
[true tokens']
_
[false tokens]))
?parts (: (Maybe (, Syntax (List Syntax) (List Syntax)))
(case tokens'
(\ (list& (#Meta [_ (#FormS (list& name args))]) sigs))
(#Some [name args sigs])
(\ (list& name sigs))
(#Some [name #Nil sigs])
_
#None))]
(case ?parts
(#Some [name args sigs])
(let [sigs' (: Syntax
(case args
#Nil
(`' (;sig (~@ sigs)))
_
(`' (;All (~ name) [(~@ args)] (;sig (~@ sigs))))))]
(return (: (List Syntax)
(list& (`' (_lux_def (~ name) (~ sigs')))
(if export?
(list (`' (_lux_export (~ name))))
#Nil)))))
#None
(fail "Wrong syntax for defsig"))))
(defmacro #export (struct tokens)
(do Lux:Monad
[tokens' (map% Lux:Monad macro-expand-1 tokens)
members (map% Lux:Monad
(: (-> Syntax (Lux (, Syntax Syntax)))
(lambda [token]
(case token
(\ (#Meta [_ (#FormS (list (#Meta [_ (#SymbolS ["" "_lux_def"])]) (#Meta [_ (#SymbolS name)]) value))]))
(do Lux:Monad
[name' (normalize name)]
(;return (: (, Syntax Syntax) [($tag name') value])))
_
(fail "Structures require defined members!"))))
tokens')]
(;return (: (List Syntax)
(list ($record members))))))
(defmacro #export (defstruct tokens)
(let [[export? tokens'] (: (, Bool (List Syntax))
(case tokens
(\ (list& (#Meta [_ (#TagS ["" "export"])]) tokens'))
[true tokens']
_
[false tokens]))
?parts (: (Maybe (, Syntax (List Syntax) Syntax (List Syntax)))
(case tokens'
(\ (list& (#Meta [_ (#FormS (list& name args))]) type defs))
(#Some [name args type defs])
(\ (list& name type defs))
(#Some [name #Nil type defs])
_
#None))]
(case ?parts
(#Some [name args type defs])
(let [defs' (: Syntax
(case args
#Nil
(`' (;struct (~@ defs)))
_
(`' (;lambda (~ name) [(~@ args)] (;struct (~@ defs))))))]
(return (: (List Syntax)
(list& (`' (def (~ name) (~ type) (~ defs')))
(if export?
(list (`' (_lux_export (~ name))))
#Nil)))))
#None
(fail "Wrong syntax for defsig"))))
(defsig #export (Eq a)
(: (-> a a Bool)
=))
(do-template [<name> <type> <test>]
[(defstruct #export <name> (Eq <type>)
(def (= x y)
(<test> x y)))]
[Int:Eq Int _jvm_leq]
[Real:Eq Real _jvm_deq])
(def #export (id x)
(All [a] (-> a a))
x)
(defsig #export (Show a)
(: (-> a Text)
show))
(do-template [<name> <type> <body>]
[(defstruct #export <name> (Show <type>)
(def (show x)
<body>))]
[Bool:Show Bool (->text x)]
[Int:Show Int (->text x)]
[Real:Show Real (->text x)]
[Char:Show Char ($ text:++ "#\"" (->text x) "\"")])
(defsig #export (Ord a)
(: (-> a a Bool)
<)
(: (-> a a Bool)
<=)
(: (-> a a Bool)
>)
(: (-> a a Bool)
>=))
(do-template [<name> <form> <message>]
[(defmacro #export (<name> tokens)
(case (reverse tokens)
(\ (list& last init))
(return (: (List Syntax)
(list (foldL (: (-> Syntax Syntax Syntax)
(lambda [post pre] (` <form>)))
last
init))))
_
(fail <message>)))]
[and (if (~ pre) true (~ post)) "and requires >=1 clauses."]
[or (if (~ pre) (~ post) false) "or requires >=1 clauses."])
(do-template [<name> <type> <lt> <gt> <eq>]
[(defstruct #export <name> (Ord <type>)
(def (< x y)
(<lt> x y))
(def (<= x y)
(or (<lt> x y)
(<eq> x y)))
(def (> x y)
(<gt> x y))
(def (>= x y)
(or (<gt> x y)
(<eq> x y))))]
[Int:Ord Int _jvm_llt _jvm_lgt _jvm_leq]
[Real:Ord Real _jvm_dlt _jvm_dgt _jvm_deq])
(defmacro #export (lux tokens state)
(case state
{#source source #modules modules #module-aliases module-aliases
#envs envs #types types #host host
#seed seed}
(case (get "lux" modules)
(#Some lux)
(let [to-alias (map (: (-> (, Text (, Bool (DefData' (-> (List Syntax) (StateE Compiler (List Syntax))))))
(List Text))
(lambda [gdef]
(let [[name [export? _]] gdef]
(if export?
(list name)
(list)))))
lux)]
(#Right [state (: (List Syntax)
(map (: (-> Text Syntax)
(lambda [name]
(` ((~ ($symbol ["" "_lux_def"])) (~ ($symbol ["" name])) (~ ($symbol ["lux" name]))))))
(list:join to-alias)))]))
#None
(#Left "Uh, oh... The universe is not working properly..."))
))
(def #export (print x)
(-> Text (IO (,)))
(lambda [_]
(exec (_jvm_invokevirtual java.io.PrintStream print [java.lang.Object]
(_jvm_getstatic java.lang.System out) [x])
[])))
(def #export (println x)
(-> Text (IO (,)))
(print (text:++ x "\n")))
(def #export (some f xs)
(All [a b]
(-> (-> a (Maybe b)) (List a) (Maybe b)))
(case xs
#Nil
#None
(#Cons [x xs'])
(case (f x)
#None
(some f xs')
(#Some y)
(#Some y))))
(def (index-of part text)
(-> Text Text Int)
(_jvm_i2l (_jvm_invokevirtual java.lang.String indexOf [java.lang.String]
text [part])))
(def (substring1 idx text)
(-> Int Text Text)
(_jvm_invokevirtual java.lang.String substring [int]
text [(_jvm_l2i idx)]))
(def (substring2 idx1 idx2 text)
(-> Int Int Text Text)
(_jvm_invokevirtual java.lang.String substring [int int]
text [(_jvm_l2i idx1) (_jvm_l2i idx2)]))
(def (split-slot slot)
(-> Text (, Text Text))
(let [idx (index-of ";" slot)
module (substring2 0 idx slot)
name (substring1 (inc idx) slot)]
[module name]))
(def (resolve-struct-type type)
(-> Type (Maybe Type))
(case type
(#RecordT slots)
(#Some type)
(#AppT [fun arg])
(resolve-struct-type fun)
(#AllT [_ _ _ body])
(resolve-struct-type body)
_
#None))
(defmacro #export (using tokens state)
(case tokens
(\ (list struct body))
(case struct
(#Meta [_ (#SymbolS vname)])
(let [vname' (ident->text vname)]
(case state
{#source source #modules modules #module-aliases module-aliases
#envs envs #types types #host host
#seed seed}
(let [?struct-type (some (: (-> (Env Text (, LuxVar Type)) (Maybe Type))
(lambda [env]
(case env
{#name _ #inner-closures _ #locals {#counter _ #mappings mappings} #closure _}
(some (: (-> (, Text (, LuxVar Type)) (Maybe Type))
(lambda [binding]
(let [[bname [_ type]] binding]
(if (text:= vname' bname)
(#Some type)
#None))))
mappings))))
envs)]
(case ?struct-type
#None
(#Left ($ text:++ "Unknown structure: " vname'))
(#Some struct-type)
(case (resolve-struct-type struct-type)
(#Some (#RecordT slots))
(let [pattern ($record (map (: (-> (, Text Type) (, Syntax Syntax))
(lambda [slot]
(let [[sname stype] slot
[module name] (split-slot sname)]
[($tag [module name]) ($symbol ["" name])])))
slots))]
(#Right [state (: (List Syntax)
(list (` (_lux_case (~ struct) (~ pattern) (~ body)))))]))
_
(#Left "Can only \"use\" records."))))))
_
(let [dummy ($symbol ["" ""])]
(#Right [state (: (List Syntax)
(list (` (_lux_case (~ struct)
(~ dummy)
(using (~ dummy)
(~ body))))))])))
_
(#Left "Wrong syntax for defsig")))
(def #export (flip f)
(All [a b c]
(-> (-> a b c) (-> b a c)))
(lambda [y x]
(f x y)))
## (def #export (curry f)
## (All [a b c]
## (-> (-> (, a b) c)
## (-> a b c)))
## (lambda [x y]
## (f [x y])))
## (def #export (uncurry f)
## (All [a b c]
## (-> (-> a b c)
## (-> (, a b) c)))
## (lambda [[x y]]
## (f x y)))
## (defmacro (loop tokens)
## (_lux_case tokens
## (#Cons [bindings (#Cons [body #Nil])])
## (let [pairs (as-pairs bindings)]
## (return (list (#FormS (#Cons [(` (lambda (~ (#SymbolS ["" "recur"])) (~ (#TupleS (map first pairs)))
## (~ body)))
## (map second pairs)])))))))
## (defmacro (get@ tokens)
## (let [output (_lux_case tokens
## (#Cons [tag (#Cons [record #Nil])])
## (` (get@' (~ tag) (~ record)))
## (#Cons [tag #Nil])
## (` (lambda [record] (get@' (~ tag) record))))]
## (return (list output))))
## (defmacro (set@ tokens)
## (let [output (_lux_case tokens
## (#Cons [tag (#Cons [value (#Cons [record #Nil])])])
## (` (set@' (~ tag) (~ value) (~ record)))
## (#Cons [tag (#Cons [value #Nil])])
## (` (lambda [record] (set@' (~ tag) (~ value) record)))
## (#Cons [tag #Nil])
## (` (lambda [value record] (set@' (~ tag) value record))))]
## (return (list output))))
## (defmacro (update@ tokens)
## (let [output (_lux_case tokens
## (#Cons [tag (#Cons [func (#Cons [record #Nil])])])
## (` (let [_record_ (~ record)]
## (set@' (~ tag) ((~ func) (get@' (~ tag) _record_)) _record_)))
## (#Cons [tag (#Cons [func #Nil])])
## (` (lambda [record]
## (` (set@' (~ tag) ((~ func) (get@' (~ tag) record)) record))))
## (#Cons [tag #Nil])
## (` (lambda [func record]
## (set@' (~ tag) (func (get@' (~ tag) record)) record))))]
## (return (list output))))
|