alicorn 0.1.2

Rust embedding of the Alicorn compiler
Documentation
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
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
-- SPDX-License-Identifier: Apache-2.0
-- SPDX-FileCopyrightText: 2025 Fundament Software SPC <https://fundament.software>
-- provide ways to construct all terms
-- checker untyped term and typechecking context -> typed term
-- evaluator takes typed term and runtime context -> value

-- type checker monad
-- error handling and metavariable unification facilities
--
-- typechecker is allowed to fail, typechecker monad carries failures upwards
--   for now fail fast, but design should vaguely support multiple failures

local fibbuf = require "fibonacci-buffer"
local pretty_printer = require "pretty-printer"
local s = pretty_printer.s

local gen = require "terms-generators"
local derivers = require "derivers"
local traits = require "traits"
local U = require "alicorn-utils"

local format = require "format"

local map = gen.declare_map
local array = gen.declare_array
local set = gen.declare_set

---@module "types.checkable"
local checkable_term = gen.declare_type()
---@module "types.unanchored_inferrable"
local unanchored_inferrable_term = gen.declare_type()
---@module "types.anchored_inferrable"
local anchored_inferrable_term = gen.declare_type()
---@module "types.typed"
local typed_term = gen.declare_type()
---@module "types.free"
local free = gen.declare_type()
---@module "types.strict_value"
local strict_value = gen.declare_type()
---@module "types.stuck_value"
local stuck_value = gen.declare_type()
---@module "types.flex_value"
local flex_value = gen.declare_type()
---@module "types.flex_runtime_context_type"
local flex_runtime_context_type = gen.declare_type()
---@module "types.binding"
local binding = gen.declare_type()
---@module "types.expression_goal"
local expression_goal = gen.declare_type()

---@class Metavariable
--- a unique key that denotes this metavariable in the graph
---@field value integer
--- a unique key that denotes this metavariable in the graph
---@field usage integer
--- indicates if this metavariable should be solved with trait search or biunification
---@field trait boolean
--- this probably shouldn't be inside the metavariable
---@field block_level integer
local Metavariable = {}

---@return stuck_value
function Metavariable:as_stuck()
	return U.notail(stuck_value.free(free.metavariable(self)))
end

---@return flex_value
function Metavariable:as_flex()
	return U.notail(flex_value.stuck(self:as_stuck()))
end

local metavariable_mt = { __index = Metavariable }
local metavariable_type = gen.declare_foreign(gen.metatable_equality(metavariable_mt), "Metavariable")

local anchor_type = gen.declare_foreign(gen.metatable_equality(format.anchor_mt), "Anchor")
local span_type = gen.declare_foreign(gen.metatable_equality(format.span_mt), "Span")

traits.diff:implement_on(metavariable_type, {
	---@param left Metavariable
	---@param right Metavariable
	diff = function(left, right)
		print("diffing metavariables:")
		if left.value ~= right.value then
			print("left value ~= right value: " .. left.value .. " ~= " .. right.value)
		end
		if left.usage ~= right.usage then
			print("left usage ~= right usage: " .. left.usage .. " ~= " .. right.usage)
		end
		if left.block_level ~= right.block_level then
			print("left block_level ~= right block_level: " .. left.block_level .. " ~= " .. right.block_level)
		end
		if left.trait ~= right.trait then
			if left.trait then
				print("left metavariable is a trait, but right isn't!")
			else
				print("right metavariable is a trait, but left isn't!")
			end
		end
	end,
})

---@module "types.spanned_name"
local spanned_name = gen.declare_record("spanned_name", {
	"name",
	gen.builtin_string,
	"name_span",
	span_type,
})

---@class (exact) FlexRuntimeContext
---@field bindings FibonacciBuffer
---@field stuck_count integer
local FlexRuntimeContext = {}

-- without this, some flex_value.closure comparisons fail erroneously
---@class RuntimeContextBinding<T>: { name: string, val: T, debuginfo: debuginfo }
local RuntimeContextBinding = {
	__eq = function(l, r)
		return l.name == r.name and l.val == r.val
	end,
}

function FlexRuntimeContext:dump_names()
	for i = 1, self.bindings:len() do
		print(i, self.bindings:get(i).name)
	end
end

---@return string
function FlexRuntimeContext:format_names()
	local msg = ""
	for i = 1, self.bindings:len() do
		msg = msg .. tostring(i) .. "\t" .. self.bindings:get(i).name .. "\n"
	end
	return msg
end

---@param index integer
---@return flex_value?
---@return spanned_name?
function FlexRuntimeContext:get(index)
	local binding = self.bindings:get(index)
	if binding == nil then
		return nil
	end
	return binding.val, binding.debuginfo
end

---@param index integer
---@return string?
function FlexRuntimeContext:get_name(index)
	local binding = self.bindings:get(index)
	if binding == nil then
		return nil
	end
	return binding.name
end

---@param index integer
---@return spanned_name?
function FlexRuntimeContext:get_spanned_name(index)
	local binding = self.bindings:get(index)
	if binding == nil then
		return nil
	end
	return binding.debuginfo
end

---@param v flex_value
---@param name string?
---@param debuginfo spanned_name
---@return FlexRuntimeContext
function FlexRuntimeContext:append(v, name, debuginfo)
	if debuginfo == nil then
		error("null debuginfo appended to FlexRuntimeContext")
	end
	name = name or debuginfo.name -- ("#r_ctx%d"):format(self.bindings:len() + 1) -- once switchover to debug is complete, no binding should ever enter the environment without debug info and so this name fallback can be removed
	if name == nil then
		error("All variables MUST have debug information!")
	end
	local copy = {
		provenance = self,
		stuck_count = self.stuck_count,
		bindings = self.bindings:append(
			setmetatable({ name = name, val = v, debuginfo = debuginfo }, RuntimeContextBinding)
		),
	}
	if v:is_stuck() then
		copy.stuck_count = copy.stuck_count + 1
	end
	return setmetatable(copy, getmetatable(self))
end

---@param index integer
---@param v flex_value
---@return FlexRuntimeContext
function FlexRuntimeContext:set(index, v)
	local old = self.bindings:get(index)
	local new = setmetatable({ name = old.name, val = v }, RuntimeContextBinding)
	local copy = { provenance = self, stuck_count = self.stuck_count, bindings = self.bindings:set(index, new) }

	if old.val:is_stuck() then
		copy.stuck_count = copy.stuck_count - 1
	end
	if v:is_stuck() then
		copy.stuck_count = copy.stuck_count + 1
	end
	return setmetatable(copy, getmetatable(self))
end

---@param other FlexRuntimeContext
---@return boolean
function FlexRuntimeContext:eq(other)
	local omt = getmetatable(other)
	if omt ~= getmetatable(self) then
		return false
	end
	return self.bindings == other.bindings
end

---@class StrictRuntimeContext : FlexRuntimeContext
local StrictRuntimeContext = U.shallow_copy(FlexRuntimeContext)

---@param index integer
---@return strict_value
---@return spanned_name?
function StrictRuntimeContext:get(index)
	return U.notail(FlexRuntimeContext.get(self, index):unwrap_strict())
end

---@param index integer
---@return string?
function StrictRuntimeContext:get_name(index)
	local binding = self:get(index)
	if binding == nil then
		return nil
	end
	return binding.name
end

---@param index integer
---@return spanned_name?
function StrictRuntimeContext:get_spanned_name(index)
	local binding = self:get(index)
	if binding == nil then
		return nil
	end
	return binding.debuginfo
end

---@param v strict_value
---@param name string?
---@param debuginfo spanned_name
---@return StrictRuntimeContext
function StrictRuntimeContext:append(v, name, debuginfo)
	if strict_value.value_check(v) ~= true then
		error("StrictRuntimeContext:append v must be a strict_value")
	end
	---@type StrictRuntimeContext
	return U.notail(FlexRuntimeContext.append(self, flex_value.strict(v), name, debuginfo))
end

---@param index integer
---@param v strict_value
---@return StrictRuntimeContext
function StrictRuntimeContext:set(index, v)
	if strict_value.value_check(v) ~= true then
		error("StrictRuntimeContext:set v must be a strict_value")
	end
	---@type StrictRuntimeContext
	return U.notail(FlexRuntimeContext.set(self, index, flex_value.strict(v)))
end

local strict_runtime_context_mt = {
	__index = StrictRuntimeContext,
	__eq = StrictRuntimeContext.eq,
	__tostring = function(t)
		return "StrictRuntimeContext with " .. t.bindings:len() .. " bindings."
	end,
}

---@return StrictRuntimeContext
local function strict_runtime_context()
	return setmetatable({ stuck_count = 0, bindings = fibbuf() }, strict_runtime_context_mt)
end

local flex_runtime_context_mt = {
	__index = FlexRuntimeContext,
	__eq = FlexRuntimeContext.eq,
	__tostring = function(t)
		return "FlexRuntimeContext with " .. t.bindings:len() .. " bindings."
	end,
}

---@return FlexRuntimeContext
local function flex_runtime_context()
	return setmetatable({ stuck_count = 0, bindings = fibbuf() }, flex_runtime_context_mt)
end

---@return StrictRuntimeContext
function FlexRuntimeContext:as_strict()
	if self.stuck_count > 0 then
		error("Cannot convert runtime context to strict, found " .. tostring(self.stuck_count) .. " stuck bindings!")
	end
	return setmetatable(
		{ provenance = self, stuck_count = self.stuck_count, bindings = self.bindings },
		strict_runtime_context_mt
	)
end

---@return FlexRuntimeContext
function StrictRuntimeContext:as_flex()
	return setmetatable(
		{ provenance = self, stuck_count = self.stuck_count, bindings = self.bindings },
		flex_runtime_context_mt
	)
end

local function runtime_context_diff_fn(left, right)
	print("diffing runtime context...")
	local rt = getmetatable(right)
	if getmetatable(left) ~= rt then
		print("unequal types!")
		print(getmetatable(left))
		print(rt)
		print("stopping diff")
		return
	end
	if left.bindings:len() ~= right.bindings:len() then
		print("unequal lengths!")
		print(left.bindings:len())
		print(right.bindings:len())
		print("stopping diff")
		return
	end
	local n = 0
	local diff_elems = {}
	for i = 1, left.bindings:len() do
		if left:get(i) ~= right:get(i) then
			n = n + 1
			diff_elems[n] = i
		end
	end
	if n == 0 then
		print("no difference")
		print("stopping diff")
		return
	elseif n == 1 then
		local d = diff_elems[1]
		print("difference in element: " .. tostring(d))
		local diff_impl
		if rt == flex_runtime_context_mt then
			diff_impl = traits.diff:get(flex_value)
		elseif rt == strict_runtime_context_mt then
			diff_impl = traits.diff:get(strict_value)
		end
		-- tail call
		return diff_impl.diff(left:get(d), right:get(d))
	else
		print("difference in multiple elements:")
		for i = 1, n do
			print("left " .. tostring(diff_elems[i]) .. ": " .. tostring(left:get(diff_elems[i])))
			print("right " .. tostring(diff_elems[i]) .. ": " .. tostring(right:get(diff_elems[i])))
		end
		print("stopping diff")
		return
	end
end

local typechecking_context_mt

---@class TypecheckingContext
---@field runtime_context FlexRuntimeContext
---@field bindings FibonacciBuffer
local TypecheckingContext = {}

---@param ctx FlexRuntimeContext|TypecheckingContext
---@return FlexRuntimeContext
local function to_runtime_context(ctx)
	if getmetatable(ctx) == typechecking_context_mt then
		return ctx.runtime_context
	end
	return ctx
end

---@param v table
---@param ctx FlexRuntimeContext|TypecheckingContext
---@param values flex_value[]
---@return boolean
local function verify_placeholders(v, ctx, values)
	-- If it's not a table we don't care
	if type(v) ~= "table" then
		return true
	end

	ctx = to_runtime_context(ctx)

	-- Special handling for arrays
	if getmetatable(v) and getmetatable(getmetatable(v)) == gen.array_type_mt then
		for k, val in ipairs(v) do
			if not verify_placeholders(val, ctx, values) then
				return false
			end
		end
		return true
	end
	if not v.kind then
		return true
	end

	if v.kind == "free.placeholder" then
		local i, info = v:unwrap_placeholder()
		if info then
			local source_ctx = ctx

			while source_ctx do
				if source_ctx == info.ctx then
					return true
				end

				source_ctx = source_ctx.provenance
			end

			print(
				debug.traceback(
					"INVALID PROVENANCE: "
						.. tostring(info)
						.. "\nORIGINAL CTX: "
						.. info.ctx:format_names()
						.. "\nASSOCIATED CTX: "
						.. ctx:format_names()
				)
			)
			--error("")
			os.exit(-1, true)

			return false
		end
	elseif v.kind == "free.metavariable" then
		if not values then
			error(debug.traceback("FORGOT values PARAMETER!"))
		end
		---@type Metavariable
		local mv = v:unwrap_metavariable()

		local source_ctx = ctx

		local mv_ctx = to_runtime_context(values[mv.value][3])
		while source_ctx do
			if source_ctx == mv_ctx then
				return true
			end

			source_ctx = source_ctx.provenance
		end

		-- for now we just check to see if the first two parts are valid
		if ctx:get(1) == mv_ctx:get(1) then
			return true
		end
		print("dumping metavariable paths")
		source_ctx = ctx
		while source_ctx do
			print(source_ctx)
			source_ctx = source_ctx.provenance
		end

		print("----")
		source_ctx = mv_ctx
		while source_ctx do
			print(source_ctx)
			source_ctx = source_ctx.provenance
		end
		print(
			debug.traceback(
				"INVALID METAVARIABLE PROVENANCE: "
					.. tostring(v)
					.. "\nORIGINAL CTX: "
					.. tostring(values[mv.value][3])
					.. "\n"
					.. values[mv.value][3]:format_names()
					.. "\nASSOCIATED CTX: "
					.. tostring(ctx)
					.. "\n"
					.. ctx:format_names()
			)
		)
		--error("")
		os.exit(-1, true)
		return false
	end

	for k, val in pairs(v) do
		if k ~= "cause" then
			if not verify_placeholders(val, ctx, values) then
				return false
			end
		end
	end

	return true
end

---get the name of a binding in a TypecheckingContext
---@param index integer
---@return string
function TypecheckingContext:get_name(index)
	local binding = self.bindings:get(index)
	if binding == nil then
		return nil
	end
	return binding.name
end

---get the name of a binding in a TypecheckingContext
---@param index integer
---@return spanned_name?
function TypecheckingContext:get_spanned_name(index)
	local binding = self.bindings:get(index)
	if binding == nil then
		return nil
	end
	return binding.debuginfo
end

function TypecheckingContext:dump_names()
	for i = 1, self:len() do
		print(i, tostring(self:get_name(i)))
	end
end

---@return string
function TypecheckingContext:format_names()
	local msg = {}
	for i = 1, self:len() do
		msg[(i * 3) - 2] = tostring(i)
		msg[(i * 3) - 1] = "\t"
		msg[i * 3] = tostring(self:get_name(i))
	end
	return table.concat(msg, "\n")
end

---@return string
function TypecheckingContext:format_names_and_types()
	local msg = {}
	for i = 1, self:len() do
		msg[(i * 5) - 4] = tostring(i)
		msg[(i * 5) - 3] = "\t"
		msg[(i * 5) - 2] = tostring(self:get_name(i))
		msg[(i * 5) - 1] = "\t:\t"
		msg[i * 5] = self:get_type(i):pretty_print(self)
	end
	return table.concat(msg, "\n")
end

---@param index integer
---@return flex_value?
function TypecheckingContext:get_type(index)
	local binding = self.bindings:get(index)
	if binding == nil then
		return nil
	end
	return binding.type
end

function TypecheckingContext:DEBUG_VERIFY_VALUES(state)
	for i = 1, self:len() do
		verify_placeholders(self:get_type(i), self, state.values)
	end
end

---@return FlexRuntimeContext
function TypecheckingContext:get_runtime_context()
	return self.runtime_context
end

---@param name string
---@param type flex_value
---@param val flex_value?
---@param debuginfo spanned_name
---@return TypecheckingContext
function TypecheckingContext:append(name, type, val, debuginfo)
	if gen.builtin_string.value_check(name) ~= true then
		error("TypecheckingContext:append parameter 'name' must be a string")
	end
	if flex_value.value_check(type) ~= true then
		print("type", type)
		p(type)
		for k, v in pairs(type) do
			print(k, v)
		end
		print(getmetatable(type))
		error("TypecheckingContext:append parameter 'type' must be a flex_value")
	end
	if type:is_closure() then
		error "BUG!!!"
	end
	if val ~= nil and flex_value.value_check(val) ~= true then
		error("TypecheckingContext:append parameter 'val' must be a flex_value (or nil if given start_anchor)")
	end
	if debuginfo ~= nil and spanned_name.value_check(debuginfo) ~= true then
		error("TypecheckingContext:append parameter 'debuginfo' must be a spanned_name (or nil if given val)")
	end
	if not val and not debuginfo then
		error("TypecheckingContext:append expected either val or debuginfo")
	end
	if not val then
		--debuginfo["{TRACE}"] = U.bound_here(2)
		val = flex_value.stuck(stuck_value.free(free.placeholder(self:len() + 1, debuginfo)))
	end

	local copy = {
		bindings = self.bindings:append({ name = name, type = type }),
		runtime_context = self.runtime_context:append(val, name, debuginfo),
	}
	return setmetatable(copy, typechecking_context_mt)
end

---@return integer
function TypecheckingContext:len()
	return U.notail(self.bindings:len())
end

typechecking_context_mt = {
	__index = TypecheckingContext,
	__len = TypecheckingContext.len,
	__tostring = function(t)
		return "TypecheckingContext with " .. t.bindings:len() .. " bindings. " .. tostring(t.runtime_context)
	end,
}

---@return TypecheckingContext
local function typechecking_context()
	return setmetatable({ bindings = fibbuf(), runtime_context = flex_runtime_context() }, typechecking_context_mt)
end

-- empty for now, just used to mark the table
local module_mt = {}

local strict_runtime_context_type =
	gen.declare_foreign(gen.metatable_equality(strict_runtime_context_mt), "StrictRuntimeContext")
local flex_runtime_context_type =
	gen.declare_foreign(gen.metatable_equality(flex_runtime_context_mt), "FlexRuntimeContext")
local typechecking_context_type =
	gen.declare_foreign(gen.metatable_equality(typechecking_context_mt), "TypecheckingContext")
local host_user_defined_id = gen.declare_foreign(function(val)
	return type(val) == "table" and type(val.name) == "string"
end, "{ name: string }")

traits.diff:implement_on(strict_runtime_context_type, { diff = runtime_context_diff_fn })
traits.diff:implement_on(flex_runtime_context_type, { diff = runtime_context_diff_fn })

-- implicit arguments are filled in through unification
-- e.g. fn append(t : star(0), n : nat, xs : Array(t, n), val : t) -> Array(t, n+1)
--      t and n can be implicit, given the explicit argument xs, as they're filled in by unification
---@module "types.visibility"
local visibility = gen.declare_enum("visibility", {
	{ "explicit" },
	{ "implicit" },
})

-- whether a function is effectful or pure
-- an effectful function must return a monad
-- calling an effectful function implicitly inserts a monad bind between the
-- function return and getting the result of the call
---@module "types.purity"
local purity = gen.declare_enum("purity", {
	{ "effectful" },
	{ "pure" },
})

---@module 'types.block_purity'
local block_purity = gen.declare_enum("block_purity", {
	{ "effectful" },
	{ "pure" },
	{ "dependent", { "val", flex_value } },
	{ "inherit" },
})

expression_goal:define_enum("expression_goal", {
	-- infer
	{ "infer" },
	-- check to a goal type
	{ "check", { "goal_type", flex_value } },
	-- TODO
	{ "mechanism", { "TODO", flex_value } },
})

-- terms that don't have a body yet
-- stylua: ignore
binding:define_enum("binding", {
	{ "let", {
		"name", gen.builtin_string,
		"debug", spanned_name,
		"expr", anchored_inferrable_term,
	} },
	{ "tuple_elim", {
		"names",   array(gen.builtin_string),
		"debug",   array(spanned_name),
		"subject", anchored_inferrable_term,
	} },
	{ "record_elim", {
		"binding_anchor",   anchor_type,
		"subject",          anchored_inferrable_term,
		"field_names",      array(gen.builtin_string),
		"field_var_debugs", array(spanned_name),
	} },
	{ "annotated_lambda", {
		"param_name",       gen.builtin_string,
		"param_annotation", anchored_inferrable_term,
		"start_anchor",     anchor_type,
		"visible",          visibility,
		"pure",             checkable_term,
	} },
	{ "program_sequence", {
		"first",        anchored_inferrable_term,
		"start_anchor", anchor_type,
	} },
})

-- checkable terms need a goal type to typecheck against
-- stylua: ignore
checkable_term:define_enum("checkable", {
	{ "inferrable", { "inferrable_term", anchored_inferrable_term } },
	{ "tuple_cons", {
		"elements", array(checkable_term),
		"debug", array(spanned_name),
	} },
	{ "host_tuple_cons", {
		"elements", array(checkable_term),
		"debug", array(spanned_name),
	} },
	{ "lambda", {
		"param_name", gen.builtin_string,
		"body",       checkable_term,
	} },
})

-- inferrable terms can have their type inferred / don't need a goal type
-- stylua: ignore
unanchored_inferrable_term:define_enum("unanchored_inferrable", {
	{ "bound_variable", { "index", gen.builtin_integer, "debug", spanned_name } },
	{ "typed", {
		"type",         typed_term,
		"usage_counts", array(gen.builtin_integer),
		"typed_term",   typed_term,
	} },
	{ "annotated_lambda", {
		"param_name",       gen.builtin_string,
		"param_annotation", anchored_inferrable_term,
		"body",             anchored_inferrable_term,
		"start_anchor",     anchor_type,
		"visible",          visibility,
		"pure",             checkable_term,
	} },
	{ "pi", {
		"param_type",  anchored_inferrable_term,
		"param_info",  checkable_term,
		"result_type", anchored_inferrable_term,
		"result_info", checkable_term,
	} },
	{ "application", {
		"f",   anchored_inferrable_term,
		"arg", checkable_term,
	} },
	{ "tuple_cons", {
		"elements", array(anchored_inferrable_term),
		"debug", array(spanned_name),
	} },
	{ "tuple_elim", {
		"names",   array(gen.builtin_string),
		"debug", array(spanned_name),
		"subject", anchored_inferrable_term,
		"body",    anchored_inferrable_term,
	} },
	{ "tuple_type", { "desc", anchored_inferrable_term } },
	{ "record_desc_cons", { "fields", map(gen.builtin_string, anchored_inferrable_term) } },
	{ "record_desc_extend_single", { "base", anchored_inferrable_term, "name", anchored_inferrable_term, "val", anchored_inferrable_term } },
	{ "record_cons", { "fields", map(gen.builtin_string, anchored_inferrable_term) } },
	{ "record_elim", {
		"subject",          anchored_inferrable_term,
		"field_names",      array(gen.builtin_string),
		"field_var_debugs", array(spanned_name),
		"body",             anchored_inferrable_term,
	} },
	{ "record_type", { "desc", anchored_inferrable_term } },
	{ "enum_cons", {
		"constructor", gen.builtin_string,
		"arg",         anchored_inferrable_term,
	} },
	{ "enum_desc_cons", {
		"variants", map(gen.builtin_string, anchored_inferrable_term),
		"rest",     anchored_inferrable_term,
	} },
	{ "enum_elim", {
		"subject",   anchored_inferrable_term,
		"mechanism", anchored_inferrable_term,
	} },
	{ "enum_type", { "desc", anchored_inferrable_term } },
	{ "enum_case", {
		"target",   anchored_inferrable_term,
		"variants", map(gen.builtin_string, anchored_inferrable_term),
		"variant_debug", map(gen.builtin_string, spanned_name), -- would be better to make this a single map with a pair value
		--"default",  inferrable_term,
	} },
	{ "enum_absurd", {
		"target", anchored_inferrable_term,
		"debug",  gen.builtin_string,
	} },

	{ "object_cons", { "methods", map(gen.builtin_string, anchored_inferrable_term) } },
	{ "object_elim", {
		"subject",   anchored_inferrable_term,
		"mechanism", anchored_inferrable_term,
	} },
	{ "let", {
		"name", gen.builtin_string,
		"debug", spanned_name,
		"expr", anchored_inferrable_term,
		"body", anchored_inferrable_term,
	} },
	{ "operative_cons", {
		"operative_type", anchored_inferrable_term,
		"userdata",       anchored_inferrable_term,
	} },
	{ "operative_type_cons", {
		"userdata_type", anchored_inferrable_term,
		"handler",       checkable_term,
	} },
	{ "level_type" },
	{ "level0" },
	{ "level_suc", { "previous_level", anchored_inferrable_term } },
	{ "level_max", {
		"level_a", anchored_inferrable_term,
		"level_b", anchored_inferrable_term,
	} },
	--{"star"},
	--{"prop"},
	--{"prim"},
	{ "annotated", {
		"annotated_term", checkable_term,
		"annotated_type", anchored_inferrable_term,
	} },
	{ "host_tuple_cons", {
		"elements", array(anchored_inferrable_term),
		"debug", array(spanned_name),
	} }, -- host_value
	{ "host_user_defined_type_cons", {
		"id",          host_user_defined_id, -- host_user_defined_type
		"family_args", array(anchored_inferrable_term), -- host_value
	} },
	{ "host_tuple_type", { "desc", anchored_inferrable_term } }, -- just like an ordinary tuple type but can only hold host_values
	{ "host_function_type", {
		"param_type",  anchored_inferrable_term, -- must be a host_tuple_type
		-- host functions can only have explicit arguments
		"result_type", anchored_inferrable_term, -- must be a host_tuple_type
		"result_info", checkable_term,
	} },
	{ "host_wrapped_type", { "type", anchored_inferrable_term } },
	{ "host_unstrict_wrapped_type", { "type", anchored_inferrable_term } },
	{ "host_wrap", { "content", anchored_inferrable_term } },
	{ "host_unstrict_wrap", { "content", anchored_inferrable_term } },
	{ "host_unwrap", { "container", anchored_inferrable_term } },
	{ "host_unstrict_unwrap", { "container", anchored_inferrable_term } },
	{ "host_if", {
		"subject",    checkable_term, -- checkable because we always know must be of host_bool_type
		"consequent", anchored_inferrable_term,
		"alternate",  anchored_inferrable_term,
	} },
	{ "host_intrinsic", {
		"source",       checkable_term,
		"type",         anchored_inferrable_term, --checkable_term,
		"start_anchor", anchor_type,
	} },
	{ "program_sequence", {
		"first",        anchored_inferrable_term,
		"start_anchor", anchor_type,
		"continue",     anchored_inferrable_term,
		"debug_info",	spanned_name,
	} },
	{ "program_end", { "result", anchored_inferrable_term } },
	{ "program_type", {
		"effect_type", anchored_inferrable_term,
		"result_type", anchored_inferrable_term,
	} },
})

-- function Alice wants to assign the value True (of type SingletonTrue) to variable Foo,
-- which means that SingletonTrue must be a subtype of FooT (the type metavariable for Foo's type).
-- function Bob wants to consume the value Foo as always the value False (of type SingletonFalse),
-- which means that Bob wants FooT to be a subtype of SingletonFalse.
--
-- on behalf of Bob, Alicorn will, _very_ early,
-- call `TypeCheckerState:flow(`[`flex_value` for FooT]`, `[`TypecheckingContext` for FooT]`, `[`flex_value` for SingletonFalse]`, `[`TypecheckingContext` for SingletonFalse]`, cause)`.
-- that'll call out to `TypeCheckerState:constrain(`[`flex_value` for FooT]`, `[`TypecheckingContext` for FooT]`, `[`flex_value` for SingletonFalse]`, `[`TypecheckingContext` for SingletonFalse]`, UniverseOmegaRelation, cause)`.
-- that'll queue an `EdgeNotif.constrain(`[`flex_value` for FooT]`, UniverseOmegaRelation, `[`flex_value` for SingletonFalse]`, self` (as `TypeCheckerState`)`.block_level, cause)`, to be processed within that last `TypeCheckerState:constrain` call.

-- stylua: ignore
anchored_inferrable_term:define_record("anchored_inferrable", {
	"anchor",
	anchor_type,
	"term",
	unanchored_inferrable_term,
})

---@class SubtypeRelation
---@field debug_name string
--- : (val:T, use:T) -> Prop__\
--- Construct a subtyping relation (val :> use), that type val is a supertype of type use, i.e. that type use is a subtype of type val, i.e. that type val flows into type use.
--- Lua value is currently used only for reference equality?
---@field Rel strict_value
--- : (a:T) -> Rel(a,a)\
--- Construct a reflexive subtyping relation—that a type flows into itself.
--- Lua value is currently unused?
---@field refl strict_value
--- : (a:T, b:T, Rel(a,b), Rel(b,a)) -> a == b\
--- Lua value is currently unused?
---@field antisym strict_value
--- : (val:Node(T), use:Node(T)) -> [TCState] (Error)\
--- Work with the ambient typechecker state to constrain that type val flows into type use.
---@field constrain strict_value
local subtype_relation_mt = {}

local SubtypeRelation = gen.declare_foreign(gen.metatable_equality(subtype_relation_mt), "SubtypeRelation")

subtype_relation_mt.__tostring = function(self)
	return "«" .. self.debug_name .. "»"
end

---@module 'types.constraintcause'
local constraintcause = gen.declare_type()

-- stylua: ignore
constraintcause:define_enum("constraintcause", {
	{ "primitive", {
		"description", gen.builtin_string,
		"position",    anchor_type,
		"track", gen.any_lua_type,
	} },
	{ "composition", {
		"left",     gen.builtin_integer,
		"right",    gen.builtin_integer,
		"position", anchor_type,
	} },
	{ "nested", {
		"description", gen.builtin_string,
		"inner",     constraintcause,
	} },
	{ "leftcall_discharge", {
		"call",       gen.builtin_integer,
		"constraint", gen.builtin_integer,
		"position",   anchor_type,
	} },
	{ "rightcall_discharge", {
		"constraint", gen.builtin_integer,
		"call",       gen.builtin_integer,
		"position",   anchor_type,
	} },
	{ "lost", { --Information has been lost, please generate any information you can to help someone debug the lost information in the future
		"unique_string", gen.builtin_string,
		"stacktrace",    gen.builtin_string,
		"auxiliary",     gen.any_lua_type,
	} },
})

---@module 'types.constraintelem'
-- stylua: ignore
local constraintelem = gen.declare_enum("constraintelem", {
	{ "sliced_constrain", {
		"rel",      SubtypeRelation,
		"right",    typed_term,
		"rightctx", typechecking_context_type,
		"cause",    constraintcause,
	} },
	{ "constrain_sliced", {
		"left",    typed_term,
		"leftctx", typechecking_context_type,
		"rel",     SubtypeRelation,
		"cause",   constraintcause,
	} },
	{ "sliced_leftcall", {
		"arg",      typed_term,
		"rel",      SubtypeRelation,
		"right",    typed_term,
		"rightctx", typechecking_context_type,
		"cause",    constraintcause,
	} },
	{ "leftcall_sliced", {
		"left",    typed_term,
		"leftctx", typechecking_context_type,
		"arg",     typed_term,
		"rel",     SubtypeRelation,
		"cause",   constraintcause,
	} },
	{ "sliced_rightcall", {
		"rel",      SubtypeRelation,
		"right",    typed_term,
		"rightctx", typechecking_context_type,
		"arg",      typed_term,
		"cause",    constraintcause,
	} },
	{ "rightcall_sliced", {
		"left",    typed_term,
		"leftctx", typechecking_context_type,
		"rel",     SubtypeRelation,
		"arg",     typed_term,
		"cause",   constraintcause,
	} },
})

local unique_id = gen.builtin_table

-- typed terms have been typechecked but do not store their type internally
-- stylua: ignore
typed_term:define_enum("typed", {
	{ "bound_variable", { "index", gen.builtin_integer, "debug", spanned_name } },
	{ "literal", { "literal_value", strict_value } },
	{ "metavariable", { "metavariable", metavariable_type } },
	{ "unique", { "id", unique_id } },
	{ "lambda", {
		"param_name", gen.builtin_string,
		"param_debug", spanned_name,
		"body",       typed_term,
		"capture",    typed_term,
		"capture_dbg", spanned_name,
		"start_anchor",     anchor_type,
	} },
	{ "pi", {
		"param_type",  typed_term,
		"param_info",  typed_term,
		"result_type", typed_term,
		"result_info", typed_term,
	} },
	{ "application", {
		"f",   typed_term,
		"arg", typed_term,
	} },
	{ "let", {
		"name", gen.builtin_string,
		"debug", spanned_name,
		"expr", typed_term,
		"body", typed_term,
	} },
	{ "level_type" },
	{ "level0" },
	{ "level_suc", { "previous_level", typed_term } },
	{ "level_max", {
		"level_a", typed_term,
		"level_b", typed_term,
	} },
	{ "star", { "level", gen.builtin_integer, "depth", gen.builtin_integer } },
	{ "prop", { "level", gen.builtin_integer } },
	{ "tuple_cons", { "elements", array(typed_term) } },
	--{"tuple_extend", {"base", typed_term, "fields", array(typed_term)}}, -- maybe?
	{ "tuple_elim", {
		"names",   array(gen.builtin_string),
		"debug", array(spanned_name), -- can probably replace the names array entirely
		"subject", typed_term,
		"length",  gen.builtin_integer,
		"body",    typed_term,
	} },
	{ "tuple_element_access", {
		"subject", typed_term,
		"index",   gen.builtin_integer,
	} },
	{ "tuple_type", { "desc", typed_term } },
	{ "tuple_desc_type", { "universe", typed_term } },
	{ "tuple_desc_concat_indep", { "prefix", typed_term, "suffix", typed_term }},
	{ "record_cons", { "fields", map(gen.builtin_string, typed_term) } },
	{ "record_type", { "desc", typed_term } },
	{ "record_desc_cons", {"field_typefns", map(gen.builtin_string, typed_term)}},
	{ "record_desc_extend_single", {"base", typed_term, "name", typed_term, "type", typed_term}},
	{ "record_desc_extend", {"base", typed_term, "extension", map(gen.builtin_string, typed_term)}},
	{ "name_set_of_record_desc", {"desc", typed_term}},
	{ "noncolliding_name_type_cons", {"set", typed_term}},
	{ "record_extend_single", {"base", typed_term, "name", typed_term, "val", typed_term}},
	{ "record_extend", {
		"base",   typed_term,
		"fields", map(gen.builtin_string, typed_term),
	} },
	{ "record_elim", {
		"subject",          typed_term,
		"field_names",      array(gen.builtin_string),
		"field_var_debugs", array(spanned_name),
		"body",             typed_term,
	} },
	{ "record_field_access", {"subject", typed_term, "name", gen.builtin_string}},
	--TODO record elim
	{ "enum_cons", {
		"constructor", gen.builtin_string,
		"arg",         typed_term,
	} },
	{ "enum_elim", {
		"subject",   typed_term,
		"mechanism", typed_term,
	} },
	{ "enum_rec_elim", {
		"subject",   typed_term,
		"mechanism", typed_term,
	} },
	{ "enum_desc_cons", {
		"variants", map(gen.builtin_string, typed_term),
		"rest",     typed_term,
	} },
	{ "enum_desc_type", {
		"univ", typed_term
	} },
	{ "enum_type", { "desc", typed_term } },
	{ "enum_case", {
		"target",   typed_term,
		"variants", map(gen.builtin_string, typed_term),
		"variant_debug", map(gen.builtin_string, spanned_name), -- would be better to make this a single map with a pair value
		"default",  typed_term,
		"default_debug", spanned_name,
	} },
	{ "enum_absurd", {
		"target", typed_term,
		"debug",  gen.builtin_string,
	} },
	{ "object_cons", { "methods", map(gen.builtin_string, typed_term) } },
	{ "object_corec_cons", { "methods", map(gen.builtin_string, typed_term) } },
	{ "object_elim", {
		"subject",   typed_term,
		"mechanism", typed_term,
	} },
	{ "operative_cons", { "userdata", typed_term } },
	{ "operative_type_cons", {
		"userdata_type", typed_term,
		"handler",       typed_term,
	} },
	{ "host_tuple_cons", { "elements", array(typed_term) } }, -- host_value
	{ "host_user_defined_type_cons", {
		"id",          host_user_defined_id,
		"family_args", array(typed_term), -- host_value
	} },
	{ "host_tuple_type", { "desc", typed_term } }, -- just like an ordinary tuple type but can only hold host_values
	{ "host_function_type", {
		"param_type",  typed_term, -- must be a host_tuple_type
		-- host functions can only have explicit arguments
		"result_type", typed_term, -- must be a host_tuple_type
		"result_info", typed_term,
	} },
	{ "host_wrapped_type", { "type", typed_term } },
	{ "host_unstrict_wrapped_type", { "type", typed_term } },
	{ "host_wrap", { "content", typed_term } },
	{ "host_unwrap", { "container", typed_term } },
	{ "host_unstrict_wrap", { "content", typed_term } },
	{ "host_unstrict_unwrap", { "container", typed_term } },
	{ "host_int_fold", {"n", typed_term, "f", typed_term, "acc", typed_term}},
	{ "host_user_defined_type", {
		"id",          host_user_defined_id,
		"family_args", array(typed_term),
	} },
	{ "host_if", {
		"subject",    typed_term,
		"consequent", typed_term,
		"alternate",  typed_term,
	} },
	{ "host_intrinsic", {
		"source",       typed_term,
		"start_anchor", anchor_type,
	} },

	-- a list of upper and lower bounds, and a relation being bound with respect to
	{ "range", {
		"lower_bounds", array(typed_term),
		"upper_bounds", array(typed_term),
		"relation",     typed_term, -- a subtyping relation. not currently represented.
	} },

	{ "singleton", {
		"supertype", typed_term,
		"value",     typed_term,
	} },

	{ "program_sequence", {
		"first",    typed_term,
		"continue", typed_term,
		"debug_info", spanned_name,
	} },
	{ "program_end", { "result", typed_term } },
	{ "program_invoke", {
		"effect_tag", typed_term,
		"effect_arg", typed_term,
	} },
	{ "effect_type", {
		"components", array(typed_term),
		"base",       typed_term,
	} },
	{ "effect_row", {
		"elems",      array(typed_term),
		"rest",       typed_term,
	} },
	{ "effect_row_resolve", {
		"elems",      set(unique_id),
		"rest",       typed_term,
	} },
	{ "program_type", {
		"effect_type", typed_term,
		"result_type", typed_term,
	} },
	{ "srel_type", { "target_type", typed_term } },
	{ "variance_type", { "target_type", typed_term } },
	{ "variance_cons", {
		"positive", typed_term,
		"srel",     typed_term,
	} },
	{ "intersection_type", {
		"left",  typed_term,
		"right", typed_term,
	} },
	{ "union_type", {
		"left",  typed_term,
		"right", typed_term,
	} },
	{ "constrained_type", {
		"constraints", array(constraintelem),
		"ctx", typechecking_context_type,
	} },
})

---@param v table
---@param ctx TypecheckingContext
---@param nested boolean
---@return boolean
local function verify_placeholder_lite(v, ctx, nested)
	-- If it's not a table we don't care
	if type(v) ~= "table" then
		return true
	end

	-- Special handling for arrays
	local v_mt = getmetatable(v)
	if v_mt and getmetatable(v_mt) == gen.array_type_mt then
		for k, val in ipairs(v) do
			local ok, i, info, info_mismatch = verify_placeholder_lite(val, ctx, true)
			if not ok then
				if not nested then
					print(v)
					if info_mismatch ~= nil then
						print("EXPECTED INFO: " .. info_mismatch)
					end
					error("AAAAAAAAAAAAAA found " .. tostring(i))
				end
				return false, i, info
			end
		end
		return true
	end
	if not v.kind then
		return true
	end

	if v.kind == "free.placeholder" then
		local i, info = v:unwrap_placeholder()
		if i > ctx:len() or i > ctx.runtime_context.bindings:len() then
			--os.exit(-1, true)
			--error("AAAAAAAAAAAAAA found " .. tostring(i) .. " " .. tostring(info))
			return false, i, info
		end
		local info_target = ctx.runtime_context.bindings:get(i).debuginfo
		if info ~= info_target then
			return false, i, info, info_target
		end
	end

	for k, val in pairs(v) do
		if k ~= "cause" and k ~= "bindings" and k ~= "provenance" then
			local ok, i, info, info_mismatch = verify_placeholder_lite(val, ctx, true)
			if not ok then
				if not nested then
					print(v)
					if info_mismatch ~= nil then
						print("EXPECTED INFO: " .. info_mismatch)
					end
					error("AAAAAAAAAAAAAA found " .. tostring(i) .. " " .. tostring(info))
				end
				return false, i, info
			end
		end
	end

	return true
end

local orig_literal_constructor = typed_term.literal
local function literal_constructor_check(val)
	-- FIXME: make sure no placeholders in val
	verify_placeholder_lite(val, typechecking_context())
	return U.notail(orig_literal_constructor(val))
end
typed_term.literal = literal_constructor_check

-- stylua: ignore
free:define_enum("free", {
	{ "metavariable", { "metavariable", metavariable_type } },
	{ "placeholder", {
		"index", gen.builtin_integer,
		"debug", spanned_name,
	} },
	{ "unique", { "id", unique_id } },
	-- TODO: axiom
})

---@module "types.result_info"
local result_info = gen.declare_record("result_info", { "purity", purity })

---@class Registry
---@field idx integer
---@field name string
local Registry = {}

---add an entry to the registry, retrieving a unique identifier for it.
---@param name string
---@param debuginfo any
---@return table
function Registry:register(name, debuginfo)
	return {
		name = name,
		debuginfo = debuginfo,
		registry = self,
	}
end

local registry_mt = { __index = Registry }
---construct a registry for a specific kind of things
---@param name string
---@return Registry
local function new_registry(name)
	return setmetatable({ name = name }, registry_mt)
end

---@module 'types.effect_id'
local effect_id = gen.declare_type()
-- stylua: ignore
effect_id:define_record("effect_id", {
	"primary",   unique_id,
	"extension", set(unique_id), --TODO: switch to a set
})

local semantic_id = gen.declare_type()
-- stylua: ignore
semantic_id:define_record("semantic_id", {
	"primary",   unique_id,
	"extension", set(unique_id), --TODO: switch to a set
})

--TODO: consider switching to a nicer coterm representation
---@module 'types.flex_continuation'
local flex_continuation = gen.declare_type()
---@module 'types.strict_continuation'
local strict_continuation = gen.declare_type()
---@module 'types.stuck_continuation'
local stuck_continuation = gen.declare_type()

---@param tag ("strict"|"stuck")
---@param t Type
---@return Type t
local function replace_flex_type(tag, t)
	if type(t) == "string" then
		error(debug.traceback("wrong type passed to replace_flex_type"))
	end
	if t == flex_value then
		if tag == "strict" then
			return strict_value
		elseif tag == "stuck" then
			return flex_value
		end
		error("Unknown tag: " .. tag)
	elseif getmetatable(t) == gen.array_type_mt then
		---@cast t ArrayType
		return U.notail(array(replace_flex_type(tag, t.value_type)))
	elseif getmetatable(t) == gen.map_type_mt then
		---@cast t MapType
		return U.notail(map(replace_flex_type(tag, t.key_type), replace_flex_type(tag, t.value_type)))
	elseif t == flex_runtime_context_type then
		if tag == "strict" then
			return strict_runtime_context_type
		elseif tag == "stuck" then
			return flex_runtime_context_type
		end
		error("Unknown tag: " .. tag)
	elseif t == flex_continuation then
		if tag == "strict" then
			return strict_continuation
		elseif tag == "stuck" then
			return flex_continuation
		end
		error("Unknown tag: " .. tag)
	end

	return t
end
replace_flex_type = U.memoize(replace_flex_type, false)

---@param arg (Value | StrictRuntimeContext | FlexRuntimeContext)
---@param t Type
---@return ("strict"|"stuck") tag
---@return (Value | StrictRuntimeContext | FlexRuntimeContext) arg
local function specify_flex_value(arg, t)
	if t == flex_value then
		---@cast arg flex_value
		if arg:is_stuck() then
			return "stuck", arg
		end
		return "strict", U.notail(arg:unwrap_strict())
	elseif getmetatable(t) == gen.array_type_mt then
		---@cast arg ArrayValue
		---@cast t ArrayType
		local arg_value_t = t.value_type
		local arg_values, arg_strict_values, arg_values_length = arg.array, {}, arg.n
		for i = 1, arg_values_length do
			local arg_value_tag
			arg_value_tag, arg_strict_values[i] = specify_flex_value(arg_values[i], arg_value_t)
			if arg_value_tag == "stuck" then
				return "stuck", arg
			end
		end
		local arg_strict_value_t = replace_flex_type("strict", arg_value_t)
		return "strict", array(arg_strict_value_t):unchecked_new(arg_strict_values, arg_values_length)
	elseif getmetatable(t) == gen.map_type_mt then
		---@cast arg MapValue
		---@cast t MapType
		local arg_key_t, arg_value_t = t.key_type, t.value_type
		local arg_values, arg_strict_values = arg._map, {}
		for arg_key, arg_value in pairs(arg_values) do
			local arg_key_tag, arg_strict_key = specify_flex_value(arg_key, arg_key_t)
			if arg_key_tag == "stuck" then
				return "stuck", arg
			end
			local arg_value_tag
			arg_value_tag, arg_strict_values[arg_strict_key] = specify_flex_value(arg_value, arg_value_t)
			if arg_value_tag == "stuck" then
				return "stuck", arg
			end
		end
		local arg_strict_key_t = replace_flex_type("strict", arg_key_t)
		local arg_strict_value_t = replace_flex_type("strict", arg_value_t)
		return "strict", map(arg_strict_key_t, arg_strict_value_t):unchecked_new(arg_strict_values)
	elseif t == flex_continuation then
		---@cast arg flex_continuation
		if arg:is_stuck() then
			return "stuck", arg
		end
		return "strict", U.notail(arg:unwrap_strict())
	elseif t == flex_runtime_context_type then
		---@cast arg FlexRuntimeContext
		if arg.stuck_count > 0 then
			return "stuck", arg
		end
		return "strict", U.notail(arg:as_strict())
	end
	return "strict", arg
end

---@param args (Value | StrictRuntimeContext | FlexRuntimeContext)[]
---@param types Type[]
---@return ("strict" | "stuck") tag
---@return (Value | StrictRuntimeContext | FlexRuntimeContext)[] arg
local function specify_flex_values(args, types)
	local strict_args = {}
	for i, t in ipairs(types) do
		local tag, arg = specify_flex_value(args[i], t)
		if tag == "stuck" then
			return tag, args
		end
		table.insert(strict_args, arg)
	end
	return "strict", strict_args
end

---@generic T
---@param key T
---@return T key
local function unify_already_flex(val)
	return val
end

---@param t Type
---@return Type t
---@return (fun(val: (Value | StrictRuntimeContext | FlexRuntimeContext)): (flex_val: (Value | FlexRuntimeContext)))? unify
local function unify_flex_type(t)
	if t == strict_value then
		return flex_value, flex_value.strict
	elseif t == stuck_value then
		return flex_value, flex_value.stuck
	elseif t == strict_continuation then
		return flex_continuation, flex_continuation.strict
	elseif t == stuck_continuation then
		return flex_continuation, flex_continuation.stuck
	elseif t == strict_runtime_context_type then
		return flex_runtime_context_type, StrictRuntimeContext.as_flex
	end
	local t_mt = getmetatable(t)
	if t_mt == gen.array_type_mt then
		---@cast t ArrayType
		local value_t = t.value_type
		local flex_value_t, unify_value = unify_flex_type(value_t)
		if unify_value == nil then
			return t, nil
		end
		---@cast unify_value -nil
		local flex_t = array(flex_value_t)
		---@param values ArrayValue
		---@return ArrayValue flex_value
		local function unify(values)
			return U.notail(values:map(flex_t, unify_value))
		end
		return flex_t, unify
	elseif t_mt == gen.map_type_mt then
		---@cast t MapType
		local key_t, value_t = t.key_type, t.value_type
		local flex_key_t, unify_key = unify_flex_type(key_t)
		local flex_value_t, unify_value = unify_flex_type(value_t)
		if unify_key == nil and unify_value == nil then
			return t, nil
		end
		if unify_key == nil then
			unify_key = unify_already_flex
		end
		if unify_value == nil then
			unify_value = unify_already_flex
		end
		local flex_t = map(flex_key_t, flex_value_t)
		---@param vals MapValue
		---@return MapValue flex_vals
		local function unify(vals)
			---@type (Value | FlexRuntimeContext)[]
			local flex_vals = {}
			for key, val in pairs(vals._map) do
				local flex_key, flex_val = unify_key(key), unify_value(val)
				flex_vals[flex_key] = flex_val
			end
			return U.notail(flex_t:unchecked_new(flex_vals))
		end
		return flex_t, unify
	end
	return t, nil
end
unify_flex_type = U.memoize(unify_flex_type, false)

---@param arg (Value | StrictRuntimeContext | FlexRuntimeContext)
---@return (Value | FlexRuntimeContext) arg
local function unify_flex_value(arg)
	local arg_mt = getmetatable(arg)
	local _strict_t, unify = unify_flex_type(arg_mt)
	if unify ~= nil then
		return U.notail(unify(arg))
	end
	return arg
end

---@param args (Value | StrictRuntimeContext | FlexRuntimeContext)[]
---@return (Value | FlexRuntimeContext)[] arg
local function unify_flex_values(args)
	---@type (Value | FlexRuntimeContext)[]
	local flex_args = {}
	for i, v in ipairs(args) do
		flex_args[i] = unify_flex_value(v)
	end
	return flex_args
end

-- stylua: ignore
gen.define_multi_enum(flex_continuation, "flex_continuation", replace_flex_type, specify_flex_values, unify_flex_values,
{ strict = strict_continuation, stuck = stuck_continuation },
{ strict = "strict_continuation", stuck = "stuck_continuation" },
{
	{ "empty$strict" },
	{ "frame$flex", {
		"context", flex_runtime_context_type,
		"code",    typed_term,
	} },
	{ "sequence$flex", {
		"first",  flex_continuation,
		"second", flex_continuation,
	} },
})

-- values must always be constructed in their simplest form, that cannot be reduced further.
-- their format must enforce this invariant.
-- e.g. it must be impossible to construct "2 + 2"; it should be constructed in reduced form "4".
-- values can contain neutral values, which represent free variables and stuck operations.
-- stuck operations are those that are blocked from further evaluation by a neutral value.
-- therefore neutral values must always contain another neutral value or a free variable.
-- their format must enforce this invariant.
-- e.g. it's possible to construct the neutral value "x + 2"; "2" is not neutral, but "x" is.
-- values must all be finite in size and must not have loops.
-- i.e. destructuring values always (eventually) terminates.

-- stylua: ignore
gen.define_multi_enum(
	flex_value,
	"flex_value",
	replace_flex_type,
	specify_flex_values,
	unify_flex_values,
	{ strict = strict_value, stuck = stuck_value },
	{ strict = "strict_value", stuck = "stuck_value" },
	{
		-- explicit, implicit,
		{ "visibility_type$strict" },
		{ "visibility$strict", { "visibility", visibility } },
		-- info about the parameter (is it implicit / what are the usage restrictions?)
		-- quantity/visibility should be restricted to free or (quantity/visibility) rather than any value
		{ "param_info_type$strict" },
		{ "param_info$flex", { "visibility", flex_value } },
		-- whether or not a function is effectful /
		-- for a function returning a monad do i have to be called in an effectful context or am i pure
		{ "result_info_type$strict" },
		{ "result_info$strict", { "result_info", result_info } },
		{ "pi$flex", {
			"param_type",  flex_value,
			"param_info",  flex_value, -- param_info
			"result_type", flex_value, -- closure from input -> result
			"result_info", flex_value, -- result_info
		}, },
		-- closure is a type that contains a typed term corresponding to the body
		-- and a runtime context representing the bound context where the closure was created
		{ "closure$flex", {
			"param_name", gen.builtin_string,
			"code",       typed_term,
			"capture",    flex_value,
			"capture_dbg", spanned_name,
			"param_debug",      spanned_name,
		}, },
		-- a list of upper and lower bounds, and a relation being bound with respect to
		{ "range$flex", {
			"lower_bounds", array(flex_value),
			"upper_bounds", array(flex_value),
			"relation",     strict_value, -- a subtyping relation. not currently represented.
		}, },
		{ "name_type$strict" },
		{ "name$strict", { "name", gen.builtin_string } },
		{ "name_set$strict", { "names", gen.builtin_string }},
		{ "name_set_type$strict"},
		{ "name_set_of_record_desc$stuck", { "desc", stuck_value}},
		{ "noncolliding_name_type$flex", {"set", flex_value}},
		{ "operative_value$flex", { "userdata", flex_value } },
		{ "operative_type$flex", {
			"handler",       flex_value,
			"userdata_type", flex_value,
		} },
		-- ordinary data
		{ "tuple_value$flex", { "elements", array(flex_value) } },
		{ "tuple_type$flex", { "desc", flex_value } },
		{ "tuple_desc_type$flex", { "universe", flex_value } },
		{ "tuple_desc_concat_indep$stuck", { "prefix", flex_value, "suffix", flex_value}},
		{ "enum_value$flex", {
			"constructor", gen.builtin_string,
			"arg",         flex_value,
		} },
		{ "enum_type$flex", { "desc", flex_value } },
		{ "enum_desc_type$flex", { "universe", flex_value } },
		{ "enum_desc_value$flex", { "variants", gen.declare_map(gen.builtin_string, flex_value) } },
		{ "record_value$flex", { "fields", map(gen.builtin_string, flex_value) } },
		{ "record_type$flex", { "desc", flex_value } },
		{ "record_desc_type$flex", { "universe", flex_value } },
		{ "record_desc_value$flex", { "fields", map(gen.builtin_string, flex_value)}},
		{ "record_desc_extend_single$stuck", { "base", flex_value, "name", stuck_value, "typefn", flex_value}},
		{ "record_desc_extend$stuck", {"base", flex_value, "extension", map(gen.builtin_string, flex_value)}},
		{ "record_extend_single$stuck", {"base", flex_value, "name", stuck_value, "val", flex_value}},
		{ "record_extend$stuck", {
			"base",      stuck_value,
			"extension", map(gen.builtin_string, flex_value),
		}, },
		-- Not used yet
		{ "object_value$flex", {
			"methods", map(gen.builtin_string, typed_term),
			"capture", flex_runtime_context_type,
		}, },
		{ "object_type$flex", { "desc", flex_value } },

		{ "star$strict", { "level", gen.builtin_integer, "depth", gen.builtin_integer } },
		{ "prop$strict", { "level", gen.builtin_integer } },

		{ "host_value$strict", { "host_value", gen.any_lua_type } },
		-- foreign data
		{ "host_type_type$strict" },
		{ "host_number_type$strict" },
		{ "host_int_fold$stuck", { "num", stuck_value, "f", flex_value, "acc", flex_value}},
		{ "host_bool_type$strict" },
		{ "host_string_type$strict" },
		{ "host_function_type$flex", {
			"param_type",  flex_value, -- must be a host_tuple_type
			-- host functions can only have explicit arguments
			"result_type", flex_value, -- must be a host_tuple_type
			"result_info", flex_value,
		}, },
		{ "host_wrapped_type$flex", { "type", flex_value } },
		{ "host_unstrict_wrapped_type$flex", { "type", flex_value } },
		{ "host_user_defined_type$flex", {
			"id",          host_user_defined_id,
			"family_args", array(flex_value),
		}, },
		{ "host_nil_type$strict" },
		--NOTE: host_tuple is not considered a host type because it's not a first class value in lua.
		{ "host_tuple_value$strict", { "elements", array(gen.any_lua_type) } },
		{ "host_tuple_type$flex", { "desc", flex_value } }, -- just like an ordinary tuple type but can only hold host_values

		-- a type family, that takes a type and a value, and produces a new type
		-- inhabited only by that single value and is a subtype of the type.
		-- example: singleton(integer, 5) is the type that is inhabited only by the
		-- number 5. values of this type can be, for example, passed to a function
		-- that takes any integer.
		-- alternative names include:
		-- - Most Specific Type (from discussion with open),
		-- - Val (from julia)
		{ "singleton$flex", {
			"supertype", flex_value,
			"value",     flex_value,
		} },
		{ "program_end$flex", { "result", flex_value } },
		{ "program_cont$flex", {
			"action",       unique_id,
			"argument",     flex_value,
			"continuation", flex_continuation,
		}, },

		{ "effect_elem$strict", { "tag", effect_id } },
		{ "effect_type$strict" },
		{ "effect_row$strict", {
			"components", set(unique_id),
		} },
		{ "effect_row_extend$stuck", {
			"base", flex_value,
			"rest", flex_value,
		} },
		{ "effect_row_type$strict" },

		{ "program_type$flex", {
			"effect_sig", flex_value,
			"base_type",  flex_value,
		} },
		{ "srel_type$flex", { "target_type", flex_value } },
		{ "variance_type$flex", { "target_type", flex_value } },
		{ "intersection_type$flex", {
			"left",  flex_value,
			"right", flex_value,
		} },
		{ "union_type$flex", {
			"left",  flex_value,
			"right", flex_value,
		} },

		{ "free$stuck", { "free", free } },
		{ "application$stuck", {
			"f",   stuck_value,
			"arg", flex_value,
		} },
		-- { "enum_elim_stuck", {
		-- 	"mechanism", value,
		-- 	"subject",   stuck_value,
		-- } },
		-- { "enum_rec_elim_stuck", {
		-- 	"handler", value,
		-- 	"subject", stuck_value,
		-- } },
		-- { "object_elim_stuck", {
		-- 	"mechanism", value,
		-- 	"subject",   stuck_value,
		-- } },
		{ "tuple_element_access$stuck", {
			"subject", stuck_value,
			"index",   gen.builtin_integer,
		} },
		{ "record_field_access$stuck", {
			"subject",    stuck_value,
			"field_name", gen.builtin_string,
		}, },
		{ "host_application$stuck", {
			"function", gen.any_lua_type,
			"arg",      stuck_value,
		} },
		{ "host_tuple$stuck", {
			"leading",       array(gen.any_lua_type),
			"stuck_element", stuck_value,
			"trailing",      array(flex_value), -- either host or neutral
		}, },
		{ "host_if$stuck", {
			"subject",    stuck_value,
			"consequent", flex_value,
			"alternate",  flex_value,
		}, },
		{ "host_intrinsic$stuck", {
			"source",       stuck_value,
			"start_anchor", anchor_type,
		} },
		{ "host_wrap$stuck", { "content", stuck_value } },
		{ "host_unwrap$stuck", { "container", stuck_value } },
	},
	function(_)
		local orig_host_value_constructor = strict_value.host_value
		local function host_value_constructor_check(val)
			-- Absolutely do not ever put a flex_value or stuck_value into here
			if stuck_value.value_check(val) or flex_value.value_check(val) then
				error("Tried to put flex or stuck value into strict_value.host_value!" .. tostring(val))
			end
			return U.notail(orig_host_value_constructor(val))
		end
		strict_value.host_value = host_value_constructor_check

		local orig_host_tuple_value_constructor = strict_value.host_tuple_value
		local function host_tuple_value_constructor_check(val)
			-- Absolutely do not ever put a flex_value or stuck_value into here
			for _, v in ipairs(val) do
				if stuck_value.value_check(v) or flex_value.value_check(v) then
					error("Tried to put flex or stuck value into strict_value.host_tuple_value!" .. tostring(v))
				end
			end

			return U.notail(orig_host_tuple_value_constructor(val))
		end
		strict_value.host_tuple_value = host_tuple_value_constructor_check
	end
)

-- metaprogramming stuff
-- TODO: add types of terms, and type indices
-- NOTE: we're doing this through host_values instead
--{"syntax_value", {"syntax", metalang.constructed_syntax_type}},
--{"syntax_type"},
--{"matcher_value", {"matcher", metalang.matcher_type}},
--{"matcher_type", {"result_type", value}},
--{"reducer_value", {"reducer", metalang.reducer_type}},
--{"environment_value", {"environment", environment_type}},
--{"environment_type"},
--{"checkable_term", {"checkable_term", checkable_term}},
--{"inferrable_term", {"inferrable_term", inferrable_term}},
--{"inferrable_term_type"},
--{"typed_term", {"typed_term", typed_term}},
--{"typechecker_monad_value", }, -- TODO
--{"typechecker_monad_type", {"wrapped_type", value}},

-- metavariables are unique (typechecker state increments after each mv constructed)
-- anchors are unique (their constructor is already memoized)
-- runtime and typechecking contexts are immutable (or at least not intended to be mutated)
-- host user defined ids are unique (identified by identity, not by name)
-- subtype relations are unique (all instances are either individual
-- or constructed from FunctionRelation, which is already memoized)
for _, t in ipairs {
	metavariable_type,
	anchor_type,
	span_type,
	flex_runtime_context_type,
	strict_runtime_context_type,
	typechecking_context_type,
	host_user_defined_id,
	SubtypeRelation,
} do
	traits.freeze:implement_on(t, {
		freeze = function(_, val)
			return val
		end,
	})
end

local host_syntax_type = strict_value.host_user_defined_type({ name = "syntax" }, array(strict_value)())
local host_environment_type = strict_value.host_user_defined_type({ name = "environment" }, array(strict_value)())
local host_typed_term_type = strict_value.host_user_defined_type({ name = "typed_term" }, array(strict_value)())
local host_goal_type = strict_value.host_user_defined_type({ name = "goal" }, array(strict_value)())
local host_inferrable_term_type =
	strict_value.host_user_defined_type({ name = "inferrable_term" }, array(strict_value)())
local host_checkable_term_type = strict_value.host_user_defined_type({ name = "checkable_term" }, array(strict_value)())
local host_purity_type = strict_value.host_user_defined_type({ name = "purity" }, array(strict_value)())
local host_block_purity_type = strict_value.host_user_defined_type({ name = "block_purity" }, array(strict_value)())
-- return ok, err
local host_lua_error_type = strict_value.host_user_defined_type({ name = "lua_error_type" }, array(strict_value)())

---@class DescConsContainer
local DescCons = --[[@enum DescCons]]
	{
		cons = "cons",
		empty = "empty",
	}

local typed_term_array = array(typed_term)
local anchored_inferrable_term_array = array(anchored_inferrable_term)
local unanchored_inferrable_term_array = array(unanchored_inferrable_term)
local flex_value_array = array(flex_value)
local strict_value_array = array(strict_value)
local stuck_value_array = array(stuck_value)
local spanned_name_array = array(spanned_name)

---@param ... flex_value
---@return flex_value
local function tup_val(...)
	return U.notail(flex_value.tuple_value(flex_value_array(...)))
end

---@param prefix flex_value
---@param next_elem flex_value
---@return flex_value
---@diagnostic disable-next-line: incomplete-signature-doc
local function cons(prefix, next_elem, ...)
	if select("#", ...) > 0 then
		error(("%d extra arguments passed to terms.cons"):format(select("#", ...)))
	end
	return U.notail(flex_value.enum_value(DescCons.cons, tup_val(prefix, next_elem)))
end

local empty = flex_value.enum_value(DescCons.empty, tup_val())

---@param desc flex_value `flex_value.enum_value(DescCons.cons, …))`
---@return flex_value prefix
---@return flex_value next_elem
local function uncons(desc)
	local constructor, arg = desc:unwrap_enum_value()
	if constructor ~= DescCons.cons then
		error(string.format("expected constructor DescCons.cons, got %s: %s", s(constructor), s(desc)))
	end
	local elements = arg:unwrap_tuple_value()
	if elements:len() ~= 2 then
		error(
			string.format("enum_value with constructor DescCons.cons should have 2 args, but has %s", s(elements:len()))
		)
	end
	return elements[1], elements[2]
end

---@param desc flex_value `flex_value.enum_value(DescCons.empty, …))`
local function unempty(desc)
	local constructor = desc:unwrap_enum_value()
	if constructor ~= DescCons.empty then
		error(string.format("expected constructor DescCons.empty, got %s: %s", s(constructor), s(desc)))
	end
end

---@param ... flex_value
---@return flex_value
local function tuple_desc(...)
	local a = empty
	for i = 1, select("#", ...) do
		local e = select(i, ...)
		if e ~= nil then
			a = cons(a, e)
		end
	end
	return a
end

---@param ... strict_value
---@return strict_value
local function strict_tup_val(...)
	return U.notail(strict_value.tuple_value(strict_value_array(...)))
end

---@param prefix strict_value
---@param next_elem strict_value
---@return strict_value
---@diagnostic disable-next-line: incomplete-signature-doc
local function strict_cons(prefix, next_elem, ...)
	if select("#", ...) > 0 then
		error(("%d extra arguments passed to terms.strict_cons"):format(select("#", ...)))
	end
	return U.notail(strict_value.enum_value(DescCons.cons, strict_tup_val(prefix, next_elem, ...)))
end

local strict_empty = empty:unwrap_strict()

---@param ... strict_value
---@return strict_value
local function strict_tuple_desc(...)
	local a = strict_empty
	for i = 1, select("#", ...) do
		local e = select(i, ...)
		if e ~= nil then
			a = strict_cons(a, e)
		end
	end
	return a
end

---@param start_anchor Anchor
---@param prefix anchored_inferrable
---@param debug_prefix spanned_name
---@param next_elem anchored_inferrable
---@param debug_next_elem spanned_name
---@return anchored_inferrable `anchored_inferrable_term(unanchored_inferrable_term.enum_cons(DescCons.cons, anchored_inferrable_term(unanchored_inferrable_term.tuple_cons(…))))`
---@diagnostic disable-next-line: incomplete-signature-doc
local function inferrable_cons(start_anchor, prefix, debug_prefix, next_elem, debug_next_elem, ...)
	if select("#", ...) > 0 then
		error(("%d extra arguments passed to terms.inferrable_cons"):format(select("#", ...)))
	end
	return U.notail(
		anchored_inferrable_term(
			start_anchor,
			unanchored_inferrable_term.enum_cons(
				DescCons.cons,
				anchored_inferrable_term(
					start_anchor,
					unanchored_inferrable_term.tuple_cons(
						anchored_inferrable_term_array(prefix, next_elem),
						spanned_name_array(debug_prefix, debug_next_elem)
					)
				)
			)
		)
	)
end

local inferrable_empty = anchored_inferrable_term(
	format.anchor_here(),
	unanchored_inferrable_term.enum_cons(
		DescCons.empty,
		anchored_inferrable_term(
			format.anchor_here(),
			unanchored_inferrable_term.tuple_cons(anchored_inferrable_term_array(), spanned_name_array())
		)
	)
)
local debug_inferrable_empty = spanned_name("terms.inferrable_empty", format.span_here())

---@param start_anchor Anchor
---@param ... (anchored_inferrable | spanned_name) (`anchored_inferrable`, `spanned_name`)\*
---@return anchored_inferrable
local function inferrable_tuple_desc(start_anchor, ...)
	local a = inferrable_empty
	local debug_a = debug_inferrable_empty
	local span = format.span_here(2)
	for i = 1, select("#", ...), 2 do
		local e, debug_e = select(i, ...), select(i + 1, ...)
		if e ~= nil then
			if debug_e == nil then
				error(("inferrable_tuple_desc: missing spanned_name at argument %d"):format(i + 1))
			end
			a, debug_a =
				inferrable_cons(start_anchor, a, debug_a, e, debug_e),
				spanned_name(("terms.inferrable_tuple_desc.varargs[%d]"):format(i), span)
		end
	end
	return a
end

---@param prefix typed
---@param next_elem typed
---@return typed `typed_term.enum_cons(DescCons.cons, typed_term.tuple_cons(…))`
---@diagnostic disable-next-line: incomplete-signature-doc
local function typed_cons(prefix, next_elem, ...)
	if select("#", ...) > 0 then
		error(("%d extra arguments passed to terms.typed_cons"):format(select("#", ...)))
	end
	return U.notail(typed_term.enum_cons(DescCons.cons, typed_term.tuple_cons(typed_term_array(prefix, next_elem))))
end

local typed_empty = typed_term.enum_cons(DescCons.empty, typed_term.tuple_cons(typed_term_array()))

---@param ... typed
---@return typed
local function typed_tuple_desc(...)
	local a = typed_empty
	for i = 1, select("#", ...) do
		local e = select(i, ...)
		if e ~= nil then
			a = typed_cons(a, e)
		end
	end
	return a
end

---@class RecordDescConsContainer
local RecordDescCons = --[[@enum RecordDescCons]]
	{
		cons = "cons",
		empty = "empty",
	}

---@param desc flex_value `flex_value.enum_value(RecordDescCons.cons, …))`
---@return flex_value field_descs
---@return flex_value name_something
---@return flex_value f
local function record_uncons(desc)
	local constructor, arg = desc:unwrap_enum_value()
	if constructor ~= RecordDescCons.cons then
		error(string.format("expected constructor RecordDescCons.cons, got %s: %s", s(constructor), s(desc)))
	end
	local elements = arg:unwrap_tuple_value()
	if elements:len() ~= 3 then
		error(
			string.format(
				"enum_value with constructor RecordDescCons.cons should have 3 args, but has %s",
				s(elements:len())
			)
		)
	end
	return elements[1], elements[2], elements[3]
end

---@param desc flex_value `flex_value.enum_value(RecordDescCons.empty, …))`
local function record_unempty(desc)
	local constructor = desc:unwrap_enum_value()
	if constructor ~= DescCons.empty then
		error(string.format("expected constructor RecordDescCons.empty, got %s: %s", s(constructor), s(desc)))
	end
end

---@module "types.tristate"
local tristate = gen.declare_enum("tristate", {
	{ "success" },
	{ "continue" },
	{ "failure" },
})

local unique_id_set = set(unique_id)

local unit_type = strict_value.tuple_type(empty:unwrap_strict())
local unit_val = strict_tup_val()
local effect_registry = new_registry("effect")
local TCState =
	effect_id(effect_registry:register("TCState", "effects that manipulate the typechecker state"), unique_id_set())
local lua_prog = effect_id(effect_registry:register("lua_prog", "running effectful lua code"), unique_id_set())

local terms = {
	metavariable_mt = metavariable_mt,
	checkable_term = checkable_term, -- {}
	anchored_inferrable_term = anchored_inferrable_term, -- {}
	anchored_inferrable_term_array = anchored_inferrable_term_array, -- {}
	unanchored_inferrable_term = unanchored_inferrable_term, -- {}
	typed_term = typed_term, -- {}
	typed_term_array = typed_term_array,
	free = free,
	visibility = visibility,
	purity = purity,
	block_purity = block_purity,
	result_info = result_info,
	flex_value = flex_value,
	flex_value_array = flex_value_array,
	strict_value = strict_value,
	strict_value_array = strict_value_array,
	stuck_value = stuck_value,
	stuck_value_array = stuck_value_array,
	binding = binding,
	expression_goal = expression_goal,
	host_syntax_type = host_syntax_type,
	host_environment_type = host_environment_type,
	host_typed_term_type = host_typed_term_type,
	host_goal_type = host_goal_type,
	host_inferrable_term_type = host_inferrable_term_type,
	host_checkable_term_type = host_checkable_term_type,
	host_purity_type = host_purity_type,
	host_block_purity_type = host_block_purity_type,
	host_lua_error_type = host_lua_error_type,
	unique_id = unique_id,
	unique_id_set = unique_id_set,
	spanned_name = spanned_name,
	spanned_name_array = spanned_name_array,

	flex_runtime_context = flex_runtime_context,
	strict_runtime_context = strict_runtime_context,
	typechecking_context = typechecking_context,
	module_mt = module_mt,
	strict_runtime_context_type = strict_runtime_context_type,
	flex_runtime_context_type = flex_runtime_context_type,
	typechecking_context_type = typechecking_context_type,
	subtype_relation_mt = subtype_relation_mt,
	SubtypeRelation = SubtypeRelation,
	constraintelem = constraintelem,
	constraintcause = constraintcause,

	DescCons = DescCons,
	tup_val = tup_val,
	cons = cons,
	empty = empty,
	uncons = uncons,
	unempty = unempty,
	tuple_desc = tuple_desc,
	strict_tup_val = strict_tup_val,
	strict_cons = strict_cons,
	strict_empty = strict_empty,
	strict_tuple_desc = strict_tuple_desc,
	typed_cons = typed_cons,
	typed_empty = typed_empty,
	typed_tuple_desc = typed_tuple_desc,
	inferrable_cons = inferrable_cons,
	inferrable_empty = inferrable_empty,
	inferrable_tuple_desc = inferrable_tuple_desc,
	RecordDescCons = RecordDescCons,
	record_empty = record_empty,
	record_uncons = record_uncons,
	unit_type = unit_type,
	unit_val = unit_val,
	effect_id = effect_id,
	flex_continuation = flex_continuation,
	strict_continuation = strict_continuation,
	stuck_continuation = stuck_continuation,

	effect_registry = effect_registry,
	TCState = TCState,
	lua_prog = lua_prog,
	verify_placeholders = verify_placeholders,
	verify_placeholder_lite = verify_placeholder_lite,

	tristate = tristate,
}

local override_prettys = require "terms-pretty"(terms)
local checkable_term_override_pretty = override_prettys.checkable_term_override_pretty
local unanchored_inferrable_term_override_pretty = override_prettys.unanchored_inferrable_term_override_pretty
local typed_term_override_pretty = override_prettys.typed_term_override_pretty
local flex_value_override_pretty = override_prettys.flex_value_override_pretty
local stuck_value_override_pretty = override_prettys.stuck_value_override_pretty
local binding_override_pretty = override_prettys.binding_override_pretty
local spanned_name_override_pretty = override_prettys.spanned_name_override_pretty

checkable_term:derive(derivers.pretty_print, checkable_term_override_pretty)
anchored_inferrable_term:derive(derivers.pretty_print)
unanchored_inferrable_term:derive(derivers.pretty_print, unanchored_inferrable_term_override_pretty)
typed_term:derive(derivers.pretty_print, typed_term_override_pretty)
visibility:derive(derivers.pretty_print)
free:derive(derivers.pretty_print)
flex_value:derive(derivers.pretty_print, flex_value_override_pretty)
strict_value:derive(derivers.pretty_print, flex_value_override_pretty)
stuck_value:derive(derivers.pretty_print, flex_value_override_pretty)
binding:derive(derivers.pretty_print, binding_override_pretty)
expression_goal:derive(derivers.pretty_print)
spanned_name:derive(derivers.pretty_print, spanned_name_override_pretty)
purity:derive(derivers.pretty_print)
result_info:derive(derivers.pretty_print)
constraintcause:derive(derivers.pretty_print)
flex_continuation:derive(derivers.pretty_print)
strict_continuation:derive(derivers.pretty_print)
stuck_continuation:derive(derivers.pretty_print)

local glsl_print = require "glsl-print"
typed_term:derive(glsl_print.glsl_print_deriver, glsl_print.typed_term_glsl)
local pretty_printer = require "pretty-printer"
typed_term.methods.glsl_print = pretty_printer.glsl_print

local internals_interface = require "internals-interface"
internals_interface.terms = terms
return setmetatable(terms, {
	__index = function(_, k)
		error(debug.traceback("'" .. k .. "' doesn't exist in terms!"))
	end,
})