praxis-runtime 0.2.0

GC ABI types, type descriptors, and runtime context for Praxis.
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
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
//! Runtime ABI versioning (§11.6) and the `praxis_*` extern wrappers (§10.2,
//! §10.4, §11.1) that JIT-generated code calls.
//!
//! The runtime ABI is private to one Praxis executable build: there is no
//! cross-version compatibility promise, and no externally linkable surface for
//! user programs. Even so, the compiler and runtime are built from the same
//! workspace, and a single constant — checked at startup — catches accidental
//! internal drift between the code that *generates* calls and the code that
//! *implements* them.
//!
//! The `praxis_*` wrappers are the **only** functions generated code may call.
//! Every argument and return value that represents a language value is a
//! [`GcRef`]; scalars (`i64` payloads) cross the ABI only as transient values
//! tied to a single non-safepointed computation (§10.3). Per §10.4, **no wrapper
//! ever lets a Rust panic unwind across the ABI**: on overflow or division by
//! zero the wrapper writes the fault into the context's fault slot and returns a
//! defined sentinel.

use crate::context::{RaisedFault, RuntimeContext};
use crate::dynamic_key::DynamicKey;
use crate::gc::GcRef;
use crate::graph::GraphOracle;
use crate::heap::{Heap, Safepoint};
use crate::roots::{NativeScope, Rooted};
use crate::scalars;
use crate::{
    collections::VecPayload,
    descriptor::{Payload, TypeDescriptor},
    repr_c_vec::ReprCVec,
};
pub use praxis_stdlib::abi::{AbiKind, AbiRet, AbiSig, Effect, RuntimeSymbol};

/// The runtime ABI version for this build. Bump it whenever a program compiled
/// against one version could be misled by a runtime of another.
///
/// Three classes of change owe a bump:
///
/// * **Layout, calling convention, or signature.** Any move of a field
///   generated code reads; any change to a `praxis_*` wrapper's parameters or
///   return type; and any change to the *size* of
///   [`RuntimeContext`](crate::RuntimeContext), because a host that built a
///   context of the previous size would have this runtime read past its end.
/// * **Meaning, with the layout unchanged.** A field or wrapper whose bits stay
///   where they were but stand for something else: a slot whose "absent" value
///   moves from a sentinel to all-zero `None`; a counter that counted calls up
///   to a limit now counting a native-stack budget down; a wrapper whose
///   manifest [`Effect`] row changes, and with it whether the caller must emit a
///   `CheckFault` after it.
/// * **A new dependency of generated code.** Nothing moves, but the compiler
///   starts reading a field it never read, so repacking that field becomes a
///   generated-code change from then on.
///
/// All three are about what generated code or a host can observe, so a field
/// with no reader outside `praxis-runtime` whose displacement and width do not
/// move owes nothing, however much the thing it points at changes.
/// `RuntimeContext.native_roots` is the one such field: its writers are
/// `Runtime::context` and `NativeScope`, its only reader is
/// `RuntimeRoots::from_context`, and no `offset_of!` in
/// `crates/praxis-codegen-cranelift/src/lower.rs` names it (ADR-114).
///
/// What generated code reads today — and therefore what the third class now
/// covers — is: [`RuntimeContext`](crate::RuntimeContext)'s own `shadow`,
/// `stack_left`, `pending_fault`, `unit_ref`, `true_ref`, `false_ref`,
/// `small_ints`, `small_chars`, `descriptors`, `debug_frames`, `debug_values`
/// and `heap`; [`Fault::KIND_OFFSET`](crate::Fault::KIND_OFFSET) and
/// [`GcHeader::DESCRIPTOR_OFFSET`](crate::GcHeader::DESCRIPTOR_OFFSET);
/// `EnumPayload::tag`; the two pacing words at
/// [`Heap::BYTES_SINCE_COLLECT_OFFSET`](crate::Heap::BYTES_SINCE_COLLECT_OFFSET)
/// and [`Heap::COLLECT_THRESHOLD_OFFSET`](crate::Heap::COLLECT_THRESHOLD_OFFSET),
/// plus `Heap::live_count`; `PageHeader`'s and `GcHeader`'s fields, which the
/// inline claim path both reads and writes; and `VecPayload`'s and
/// `BitSetPayload`'s leading words, reached through
/// [`ReprCVec`](crate::ReprCVec). A debug value slot's word is an
/// `Option<GcRef>` only when the `DebugLocalMeta` beside it says so: a temp
/// whose box was elided stores its payload raw (ADR-120).
///
/// One numeral per build, not one per change — a version is a statement about a
/// build, so several packages landing in the same round share a single bump,
/// owned by one of them, rather than taking four bumps in four worktrees that a
/// merge would silently reduce to the last.
pub const RUNTIME_ABI_VERSION: u32 = 20;

/// Assert that the compiler's expected ABI version matches this build's.
///
/// Called once at CLI / LSP startup. Today the compiler and runtime are the
/// same binary, so the assertion is trivially satisfied; the point is to have
/// the check in place before the runtime is split across build artifacts.
///
/// # Panics
/// Panics if the versions disagree. A disagreement is always a build bug, never
/// a user-facing condition.
pub fn assert_abi_version() {
    assert_eq!(
        COMPILER_EXPECTED_ABI_VERSION, RUNTIME_ABI_VERSION,
        "compiler/runtime ABI version mismatch: compiler expected \
         {COMPILER_EXPECTED_ABI_VERSION}, runtime reports {RUNTIME_ABI_VERSION}. \
         This is a build inconsistency; rebuild the workspace."
    );
}

/// The ABI version the compiler front end assumes when generating code. Kept in
/// lockstep with [`RUNTIME_ABI_VERSION`] within a single build.
const COMPILER_EXPECTED_ABI_VERSION: u32 = 20;

// ---------------------------------------------------------------------------
// The runtime symbol table.
// ---------------------------------------------------------------------------

/// The address of a runtime wrapper, for the JIT to resolve an import to.
///
/// This match is the **only** symbol→address table in the workspace, and it is
/// exhaustive over [`RuntimeSymbol`]: adding a row to the manifest without
/// giving it an address here is a compile error. There is no fallback — the JIT
/// never reaches `dlsym`, so a symbol the compiler failed to register cannot
/// accidentally "work" because it happens to be linked in.
#[must_use]
pub fn address(symbol: RuntimeSymbol) -> *const u8 {
    let ptr: *const () = match symbol {
        RuntimeSymbol::AllocBool => praxis_alloc_bool as *const (),
        RuntimeSymbol::AllocChar => praxis_alloc_char as *const (),
        RuntimeSymbol::AllocClosure => praxis_alloc_closure as *const (),
        RuntimeSymbol::AllocEnum => praxis_alloc_enum as *const (),
        RuntimeSymbol::AllocFloat => praxis_alloc_float as *const (),
        RuntimeSymbol::AllocInt => praxis_alloc_int as *const (),
        RuntimeSymbol::AllocRecord => praxis_alloc_record as *const (),
        RuntimeSymbol::AllocText => praxis_alloc_text as *const (),
        RuntimeSymbol::AllocTuple => praxis_alloc_tuple as *const (),
        RuntimeSymbol::AllocUnit => praxis_alloc_unit as *const (),
        RuntimeSymbol::AllocVarCell => praxis_alloc_var_cell as *const (),
        RuntimeSymbol::Assert => praxis_assert as *const (),
        RuntimeSymbol::AStarDistance => praxis_a_star_distance as *const (),
        RuntimeSymbol::AStarPath => praxis_a_star_path as *const (),
        RuntimeSymbol::Bfs => praxis_bfs as *const (),
        RuntimeSymbol::BfsDistance => praxis_bfs_distance as *const (),
        RuntimeSymbol::BfsPath => praxis_bfs_path as *const (),
        RuntimeSymbol::BitsetContains => praxis_bitset_contains as *const (),
        RuntimeSymbol::BitsetInsert => praxis_bitset_insert as *const (),
        RuntimeSymbol::BitsetIsEmpty => praxis_bitset_is_empty as *const (),
        RuntimeSymbol::BitsetItems => praxis_bitset_items as *const (),
        RuntimeSymbol::BitsetLen => praxis_bitset_len as *const (),
        RuntimeSymbol::BitsetNew => praxis_bitset_new as *const (),
        RuntimeSymbol::Breakpoint => praxis_breakpoint as *const (),
        RuntimeSymbol::BitsetRemove => praxis_bitset_remove as *const (),
        RuntimeSymbol::BoolLoad => praxis_bool_load as *const (),
        RuntimeSymbol::CharLoad => praxis_char_load as *const (),
        RuntimeSymbol::CharToInt => praxis_char_to_int as *const (),
        RuntimeSymbol::CharToText => praxis_char_to_text as *const (),
        RuntimeSymbol::CheckFault => praxis_check_fault as *const (),
        RuntimeSymbol::ClosureCapture => praxis_closure_capture as *const (),
        RuntimeSymbol::ClosureFnPtr => praxis_closure_fn_ptr as *const (),
        RuntimeSymbol::ClosureSetCapture => praxis_closure_set_capture as *const (),
        RuntimeSymbol::CounterGet => praxis_counter_get as *const (),
        RuntimeSymbol::CounterInc => praxis_counter_inc as *const (),
        RuntimeSymbol::CounterIsEmpty => praxis_counter_is_empty as *const (),
        RuntimeSymbol::CounterLen => praxis_counter_len as *const (),
        RuntimeSymbol::CounterKeys => praxis_counter_keys as *const (),
        RuntimeSymbol::CounterNew => praxis_counter_new as *const (),
        RuntimeSymbol::CounterSet => praxis_counter_set as *const (),
        RuntimeSymbol::CounterValues => praxis_counter_values as *const (),
        RuntimeSymbol::DequeGet => praxis_deque_get as *const (),
        RuntimeSymbol::DequeSet => praxis_deque_set as *const (),
        RuntimeSymbol::DequeIsEmpty => praxis_deque_is_empty as *const (),
        RuntimeSymbol::DequeLen => praxis_deque_len as *const (),
        RuntimeSymbol::DequeNew => praxis_deque_new as *const (),
        RuntimeSymbol::DequePopBack => praxis_deque_pop_back as *const (),
        RuntimeSymbol::DequePopFront => praxis_deque_pop_front as *const (),
        RuntimeSymbol::DequePushBack => praxis_deque_push_back as *const (),
        RuntimeSymbol::DequePushFront => praxis_deque_push_front as *const (),
        RuntimeSymbol::Dbg => praxis_dbg as *const (),
        RuntimeSymbol::Dfs => praxis_dfs as *const (),
        RuntimeSymbol::DfsDistance => praxis_dfs_distance as *const (),
        RuntimeSymbol::DfsPath => praxis_dfs_path as *const (),
        RuntimeSymbol::Dijkstra => praxis_dijkstra as *const (),
        RuntimeSymbol::DijkstraDistance => praxis_dijkstra_distance as *const (),
        RuntimeSymbol::DijkstraPath => praxis_dijkstra_path as *const (),
        RuntimeSymbol::EnumPayload => praxis_enum_payload as *const (),
        RuntimeSymbol::EnumSetPayload => praxis_enum_set_payload as *const (),
        RuntimeSymbol::EnumTag => praxis_enum_tag as *const (),
        RuntimeSymbol::FloatAbs => praxis_float_abs as *const (),
        RuntimeSymbol::FloatCeil => praxis_float_ceil as *const (),
        RuntimeSymbol::FloatE => praxis_float_e as *const (),
        RuntimeSymbol::FloatFloor => praxis_float_floor as *const (),
        RuntimeSymbol::FloatIsInfinite => praxis_float_is_infinite as *const (),
        RuntimeSymbol::FloatIsNan => praxis_float_is_nan as *const (),
        RuntimeSymbol::FloatLoad => praxis_float_load as *const (),
        RuntimeSymbol::FloatMax => praxis_float_max as *const (),
        RuntimeSymbol::FloatMin => praxis_float_min as *const (),
        RuntimeSymbol::FloatPi => praxis_float_pi as *const (),
        RuntimeSymbol::FloatRound => praxis_float_round as *const (),
        RuntimeSymbol::FloatSign => praxis_float_sign as *const (),
        RuntimeSymbol::FloatSqrt => praxis_float_sqrt as *const (),
        RuntimeSymbol::FloatToInt => praxis_float_to_int as *const (),
        RuntimeSymbol::FloatToText => praxis_float_to_text as *const (),
        RuntimeSymbol::FloodFill => praxis_flood_fill as *const (),
        RuntimeSymbol::GetInput => praxis_get_input as *const (),
        RuntimeSymbol::GridAround4 => praxis_grid_around4 as *const (),
        RuntimeSymbol::GridAround8 => praxis_grid_around8 as *const (),
        RuntimeSymbol::GridCells => praxis_grid_cells as *const (),
        RuntimeSymbol::GridColumn => praxis_grid_column as *const (),
        RuntimeSymbol::GridContains => praxis_grid_contains as *const (),
        RuntimeSymbol::GridCount4 => praxis_grid_count4 as *const (),
        RuntimeSymbol::GridCount4Where => praxis_grid_count4_where as *const (),
        RuntimeSymbol::GridCount8 => praxis_grid_count8 as *const (),
        RuntimeSymbol::GridCount8Where => praxis_grid_count8_where as *const (),
        RuntimeSymbol::GridFind => praxis_grid_find as *const (),
        RuntimeSymbol::GridFindAll => praxis_grid_find_all as *const (),
        RuntimeSymbol::GridGet => praxis_grid_get as *const (),
        RuntimeSymbol::GridHeight => praxis_grid_height as *const (),
        RuntimeSymbol::GridNeighbors4 => praxis_grid_neighbors4 as *const (),
        RuntimeSymbol::GridNeighbors8 => praxis_grid_neighbors8 as *const (),
        RuntimeSymbol::GridFilled => praxis_grid_filled as *const (),
        RuntimeSymbol::GridNew => praxis_grid_new as *const (),
        RuntimeSymbol::GridPositions => praxis_grid_positions as *const (),
        RuntimeSymbol::GridRotateLeft => praxis_grid_rotate_left as *const (),
        RuntimeSymbol::GridRotateRight => praxis_grid_rotate_right as *const (),
        RuntimeSymbol::GridRow => praxis_grid_row as *const (),
        RuntimeSymbol::GridSet => praxis_grid_set as *const (),
        RuntimeSymbol::GridTranspose => praxis_grid_transpose as *const (),
        RuntimeSymbol::GridWidth => praxis_grid_width as *const (),
        RuntimeSymbol::IntAbs => praxis_int_abs as *const (),
        RuntimeSymbol::IntAdd => praxis_int_add as *const (),
        RuntimeSymbol::IntCheckedAdd => praxis_int_checked_add as *const (),
        RuntimeSymbol::IntCheckedMul => praxis_int_checked_mul as *const (),
        RuntimeSymbol::IntCheckedSub => praxis_int_checked_sub as *const (),
        RuntimeSymbol::IntClamp => praxis_int_clamp as *const (),
        RuntimeSymbol::IntDiv => praxis_int_div as *const (),
        RuntimeSymbol::IntEq => praxis_int_eq as *const (),
        RuntimeSymbol::IntGcd => praxis_int_gcd as *const (),
        RuntimeSymbol::IntGe => praxis_int_ge as *const (),
        RuntimeSymbol::IntGt => praxis_int_gt as *const (),
        RuntimeSymbol::IntLcm => praxis_int_lcm as *const (),
        RuntimeSymbol::IntLe => praxis_int_le as *const (),
        RuntimeSymbol::IntLoad => praxis_int_load as *const (),
        RuntimeSymbol::IntLt => praxis_int_lt as *const (),
        RuntimeSymbol::IntMax => praxis_int_max as *const (),
        RuntimeSymbol::IntMin => praxis_int_min as *const (),
        RuntimeSymbol::IntMul => praxis_int_mul as *const (),
        RuntimeSymbol::IntNe => praxis_int_ne as *const (),
        RuntimeSymbol::IntNeg => praxis_int_neg as *const (),
        RuntimeSymbol::IntRem => praxis_int_rem as *const (),
        RuntimeSymbol::IntSaturatingAdd => praxis_int_saturating_add as *const (),
        RuntimeSymbol::IntSaturatingMul => praxis_int_saturating_mul as *const (),
        RuntimeSymbol::IntSaturatingSub => praxis_int_saturating_sub as *const (),
        RuntimeSymbol::IntSign => praxis_int_sign as *const (),
        RuntimeSymbol::IntSub => praxis_int_sub as *const (),
        RuntimeSymbol::IntToChar => praxis_int_to_char as *const (),
        RuntimeSymbol::IntToFloat => praxis_int_to_float as *const (),
        RuntimeSymbol::IntToText => praxis_int_to_text as *const (),
        RuntimeSymbol::IntWrappingAdd => praxis_int_wrapping_add as *const (),
        RuntimeSymbol::IntWrappingMul => praxis_int_wrapping_mul as *const (),
        RuntimeSymbol::IntWrappingSub => praxis_int_wrapping_sub as *const (),
        RuntimeSymbol::MapContains => praxis_map_contains as *const (),
        RuntimeSymbol::RangeGet => praxis_range_get as *const (),
        RuntimeSymbol::RangeLen => praxis_range_len as *const (),
        RuntimeSymbol::RangeNew => praxis_range_new as *const (),
        RuntimeSymbol::RangeNewInclusive => praxis_range_new_inclusive as *const (),
        RuntimeSymbol::MapGet => praxis_map_get as *const (),
        RuntimeSymbol::MapIndex => praxis_map_index as *const (),
        RuntimeSymbol::MapInsert => praxis_map_insert as *const (),
        RuntimeSymbol::MapIsEmpty => praxis_map_is_empty as *const (),
        RuntimeSymbol::MapKeys => praxis_map_keys as *const (),
        RuntimeSymbol::MapLen => praxis_map_len as *const (),
        RuntimeSymbol::MapNew => praxis_map_new as *const (),
        RuntimeSymbol::MapRemove => praxis_map_remove as *const (),
        RuntimeSymbol::MapUpdateMax => praxis_map_update_max as *const (),
        RuntimeSymbol::MapUpdateMin => praxis_map_update_min as *const (),
        RuntimeSymbol::MapValues => praxis_map_values as *const (),
        RuntimeSymbol::MaxHeapIsEmpty => praxis_max_heap_is_empty as *const (),
        RuntimeSymbol::MaxHeapItems => praxis_max_heap_items as *const (),
        RuntimeSymbol::MaxHeapLen => praxis_max_heap_len as *const (),
        RuntimeSymbol::MaxHeapNew => praxis_max_heap_new as *const (),
        RuntimeSymbol::MaxHeapPeek => praxis_max_heap_peek as *const (),
        RuntimeSymbol::MaxHeapPop => praxis_max_heap_pop as *const (),
        RuntimeSymbol::MaxHeapPush => praxis_max_heap_push as *const (),
        RuntimeSymbol::MinHeapIsEmpty => praxis_min_heap_is_empty as *const (),
        RuntimeSymbol::MinHeapItems => praxis_min_heap_items as *const (),
        RuntimeSymbol::MinHeapLen => praxis_min_heap_len as *const (),
        RuntimeSymbol::MinHeapNew => praxis_min_heap_new as *const (),
        RuntimeSymbol::MinHeapPeek => praxis_min_heap_peek as *const (),
        RuntimeSymbol::MinHeapPop => praxis_min_heap_pop as *const (),
        RuntimeSymbol::MinHeapPush => praxis_min_heap_push as *const (),
        RuntimeSymbol::Panic => praxis_panic as *const (),
        RuntimeSymbol::RaiseDivByZeroIf => praxis_raise_div_by_zero_if as *const (),
        RuntimeSymbol::RaiseEmptyCollection => praxis_raise_empty_collection as *const (),
        RuntimeSymbol::RaiseIntOverflowIf => praxis_raise_int_overflow_if as *const (),
        RuntimeSymbol::RaiseStackOverflow => praxis_raise_stack_overflow as *const (),
        RuntimeSymbol::RecordField => praxis_record_field as *const (),
        RuntimeSymbol::RecordSetField => praxis_record_set_field as *const (),
        RuntimeSymbol::RunParser => praxis_run_parser as *const (),
        RuntimeSymbol::SetContains => praxis_set_contains as *const (),
        RuntimeSymbol::SetInsert => praxis_set_insert as *const (),
        RuntimeSymbol::SetIsEmpty => praxis_set_is_empty as *const (),
        RuntimeSymbol::SetItems => praxis_set_items as *const (),
        RuntimeSymbol::SetLen => praxis_set_len as *const (),
        RuntimeSymbol::SetNew => praxis_set_new as *const (),
        RuntimeSymbol::SetRemove => praxis_set_remove as *const (),
        RuntimeSymbol::SnapshotDebugChain => {
            crate::crash_snapshot::praxis_snapshot_debug_chain as *const ()
        }
        RuntimeSymbol::StructEq => praxis_struct_eq as *const (),
        RuntimeSymbol::TextConcat => praxis_text_concat as *const (),
        RuntimeSymbol::TextGet => praxis_text_get as *const (),
        RuntimeSymbol::TextFloat => praxis_text_float as *const (),
        RuntimeSymbol::TextInt => praxis_text_int as *const (),
        RuntimeSymbol::TextIsEmpty => praxis_text_is_empty as *const (),
        RuntimeSymbol::TextLen => praxis_text_len as *const (),
        RuntimeSymbol::TupleGet => praxis_tuple_get as *const (),
        RuntimeSymbol::TupleSet => praxis_tuple_set as *const (),
        RuntimeSymbol::ValueCmp => praxis_value_cmp as *const (),
        RuntimeSymbol::ValueToText => praxis_value_to_text as *const (),
        RuntimeSymbol::VarCellGet => praxis_var_cell_get as *const (),
        RuntimeSymbol::VarCellSet => praxis_var_cell_set as *const (),
        RuntimeSymbol::VecFrequencies => praxis_vec_frequencies as *const (),
        RuntimeSymbol::VecGet => praxis_vec_get as *const (),
        RuntimeSymbol::VecSet => praxis_vec_set as *const (),
        RuntimeSymbol::VecIsEmpty => praxis_vec_is_empty as *const (),
        RuntimeSymbol::VecJoin => praxis_vec_join as *const (),
        RuntimeSymbol::VecLen => praxis_vec_len as *const (),
        RuntimeSymbol::VecChunks => praxis_vec_chunks as *const (),
        RuntimeSymbol::VecFilled => praxis_vec_filled as *const (),
        RuntimeSymbol::VecNew => praxis_vec_new as *const (),
        RuntimeSymbol::VecPush => praxis_vec_push as *const (),
        RuntimeSymbol::VecReversed => praxis_vec_reversed as *const (),
        RuntimeSymbol::VecSorted => praxis_vec_sorted as *const (),
        RuntimeSymbol::VecSortedByKey => praxis_vec_sorted_by_key as *const (),
        RuntimeSymbol::VecToText => praxis_vec_to_text as *const (),
        RuntimeSymbol::VecUnique => praxis_vec_unique as *const (),
        RuntimeSymbol::VecWindows => praxis_vec_windows as *const (),
        RuntimeSymbol::WriteStdout => praxis_write_stdout as *const (),
    };
    ptr as *const u8
}

// ---------------------------------------------------------------------------
// The panic backstop (§9.2, §10.4)
// ---------------------------------------------------------------------------

/// The defined dummy a wrapper returns when it has raised a fault and has no
/// real answer (§10.4).
///
/// A wrapper's return type is part of the ABI, so "return nothing" is not
/// available; every type generated code can receive needs a value that is safe
/// to hold and never read. `GcRef` is `NonNull`, so its dummy is the context's
/// `Unit` — the same sentinel the fault epilogue returns — and integer zero
/// would be an invalid reference, not a dummy.
pub(crate) trait AbiSentinel {
    /// # Safety
    /// `ctx` must be null or point at a live, wired `RuntimeContext`.
    unsafe fn sentinel(ctx: *mut RuntimeContext) -> Self;
}

impl AbiSentinel for () {
    unsafe fn sentinel(_ctx: *mut RuntimeContext) {}
}

impl AbiSentinel for i64 {
    unsafe fn sentinel(_ctx: *mut RuntimeContext) -> i64 {
        0
    }
}

impl AbiSentinel for GcRef {
    unsafe fn sentinel(ctx: *mut RuntimeContext) -> GcRef {
        // SAFETY: the caller guarantees a live, wired context. A null one
        // cannot produce a `GcRef` at all, and `abi_panic_escaped` refuses to
        // reach here with one.
        unsafe { unit_sentinel(ctx) }
    }
}

impl<T> AbiSentinel for *mut T {
    unsafe fn sentinel(_ctx: *mut RuntimeContext) -> *mut T {
        std::ptr::null_mut()
    }
}

impl<T> AbiSentinel for *const T {
    unsafe fn sentinel(_ctx: *mut RuntimeContext) -> *const T {
        std::ptr::null()
    }
}

/// Translate a panic that reached an `extern "C"` boundary into a fault, and
/// return the boundary's defined dummy.
///
/// **This must never fire.** Totality is the contract — a wrapper validates its
/// arguments and reports a bad one as a fault — and this exists because a
/// contract that cannot be checked is a hope. A Rust panic unwinding out of
/// `extern "C"` into Cranelift frames is undefined behaviour; the guard turns
/// the one outcome nobody can reason about into the one §10.4 already
/// specifies, and does it uniformly so no future wrapper has to remember.
///
/// The kind is [`FaultKind::Panic`](crate::FaultKind::Panic), with a message
/// naming the wrapper. It is deliberately not a new `FaultKind::Internal`: a
/// new kind is a `#[repr(C)]` layout change that costs an ABI bump (ADR-075),
/// and `Panic` plus a message that names the function carries strictly more
/// information for the crash report (§9.4) than a bare kind would.
///
/// # Safety
/// `ctx` must be null or point at a live, wired `RuntimeContext`.
#[cold]
#[inline(never)]
pub(crate) unsafe fn abi_panic_escaped<T: AbiSentinel>(
    ctx: *mut RuntimeContext,
    wrapper: &'static str,
) -> T {
    if ctx.is_null() {
        // There is no fault slot to write and no `Unit` to return. Aborting is
        // the only defined answer left, and it is still better than unwinding
        // into generated frames.
        std::process::abort();
    }
    // SAFETY: the caller guarantees a live, wired context.
    unsafe { set_fault(ctx, RaisedFault::PANIC) };
    let message = format!("internal error: a panic escaped the runtime wrapper `{wrapper}`");
    // SAFETY: as above.
    unsafe { set_fault_message(ctx, message.clone()) };
    if !panic_fault_is_observable(wrapper) {
        // **The dummy has to be unreachable where nobody will look at the
        // fault.** Generated code tests the fault slot only where MIR emitted
        // a `CheckFault`, and MIR emits one only after a call it classifies as
        // faultable — so for a wrapper the manifest declares non-faulting there
        // is *no* check by construction, and returning `unit_sentinel` would
        // hand a `Unit` into a slot generated code believes holds a Record, a
        // Tuple or a closure. Aborting with the message is the only answer that
        // does not introduce a descriptor/payload confusion.
        eprintln!("{message}");
        std::process::abort();
    }
    // SAFETY: as above.
    unsafe { T::sentinel(ctx) }
}

/// Whether generated code can be expected to observe a `Panic` fault raised by
/// `wrapper` — i.e. whether the wrapper's defined dummy is ever consumed under
/// a fault check rather than as a value.
///
/// The manifest is the authority: a symbol declared [`Effect::Pure`] or
/// [`Effect::Allocates`] cannot be followed by a `CheckFault`. **That is
/// `praxis_mir::verify`'s rule, not a claim restated here** — its
/// `RedundantFaultCheck` variant rejects a check after an instruction that
/// cannot fault, so this function's premise is enforced rather than assumed
/// (ADR-088). A wrapper the manifest does not name at all is in the same
/// position and is treated the same way.
///
/// The converse is *not* claimed here: a declared-faulting wrapper's call sites
/// are MIR's business. What this rules out is the class where the check is
/// impossible.
fn panic_fault_is_observable(wrapper: &str) -> bool {
    praxis_stdlib::abi::RuntimeSymbol::from_name(wrapper).is_some_and(|s| s.faults())
}

/// Wrap an `extern "C"` wrapper's body so a panic becomes a fault.
///
/// Every `#[unsafe(no_mangle)] extern "C" fn` in this crate has its body inside
/// these, and `every_no_mangle_wrapper_is_behind_the_panic_guard` is the test
/// that keeps it that way — a new wrapper that forgets is a failing test rather
/// than a latent abort.
macro_rules! abi_guard {
    ($wrapper:expr_2021, $ctx:expr_2021, $body:block) => {{
        // `AssertUnwindSafe`: the body's captures are the wrapper's own
        // arguments, which are `Copy` C types, and the fault protocol is how
        // the runtime already communicates a half-finished operation.
        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || $body)) {
            Ok(value) => value,
            // SAFETY: `ctx` is the wrapper's own context argument, whose
            // validity every wrapper's `# Safety` section already requires.
            Err(_) => unsafe { crate::abi::abi_panic_escaped($ctx, $wrapper) },
        }
    }};
}

pub(crate) use abi_guard;

// ---------------------------------------------------------------------------
// Internals the wrappers share.
// ---------------------------------------------------------------------------

/// Raise `fault` on `ctx`'s fault slot (§10.4). Does nothing if the context's
/// fault pointer is null (a misuse, but never panics across the ABI).
///
/// Takes a [`RaisedFault`], not a `FaultKind`: every raise names a kind that
/// describes it, and "no fault" is not spellable here.
unsafe fn set_fault(ctx: *mut RuntimeContext, fault: RaisedFault) {
    if let Some(slot) = unsafe { (*ctx).pending_fault.as_mut() } {
        slot.set(fault);
    }
}

/// Record `text` as the message the fault about to be raised carries (§9.1).
/// Does nothing if the context's message slot is null (a host that wired no
/// runtime), so a `panic` still faults even where nothing can render its words.
unsafe fn set_fault_message(ctx: *mut RuntimeContext, text: String) {
    if let Some(slot) = unsafe { (*ctx).fault_message.as_mut() } {
        slot.set(text);
    }
}

/// The heap pointer out of a context, or null.
#[inline]
unsafe fn heap<'a>(ctx: *mut RuntimeContext) -> &'a Heap {
    // SAFETY: the caller guarantees `ctx` points at a live, wired context whose
    // `heap` field references a valid `Heap` for the duration of the call.
    unsafe { &*(*ctx).heap }
}

#[inline]
/// Charge the pacer for a collection's buffer growing (ADR-121).
///
/// `before` and `after` are the same payload's `owned_bytes()`, read either
/// side of a mutation that may reallocate. Nothing is charged when the buffer
/// did not grow, which is the overwhelmingly common case: amortized doubling
/// means a `push` reallocates once every *n* pushes, so this is a compare and a
/// not-taken branch on the hot path.
///
/// # Why every growing wrapper has to call this
///
/// `Heap::alloc_raw` charges `stride + owned_bytes_of(payload)` once, at
/// construction. Leaving later growth uncharged relies on the elements
/// themselves being paced allocations, so that the residual under-count is only
/// the spine — and scalar promotion deletes exactly those element allocations,
/// so an allocation-light program that grows a large buffer paces nothing.
/// Uncharged, `bfs` runs 6 collections instead of 41 and reaches a peak
/// resident set of 224 MiB against 61 (ADR-121).
///
/// So the rule is: **a wrapper that can grow a buffer charges the growth.** The
/// `growth_charging_tests` module below has one case per such wrapper, because
/// the failure mode is silent — a program that simply stops collecting, which
/// reads as a leak nobody connects to the wrapper that was added.
fn charge_growth(ctx: *mut RuntimeContext, before: usize, after: usize) {
    let Some(grown) = after.checked_sub(before).filter(|g| *g != 0) else {
        return;
    };
    if ctx.is_null() {
        return;
    }
    // SAFETY: every caller is inside `abi_guard!`, which established that `ctx`
    // is live and wired; the null check above covers the guard's own edge.
    unsafe { heap(ctx).charge_owned_growth(grown) };
}

/// Trigger a collection on allocation pressure, rooting from the context
/// (§12.4, ADR-019, ADR-101). Called by every allocating `praxis_*` wrapper.
/// Safe to call with a null/unwired context (no-op).
///
/// The roots are every **strong** arm of
/// [`RuntimeRoots`](crate::roots::RuntimeRoots) — the shadow stack, the ambient
/// input buffer, a parse failure's partial value, a runtime-owned crash
/// snapshot, and the native root store. All five, not the shadow stack alone:
/// host-driven allocation and the parser interpreter push no shadow frame, and
/// the other four owners are reachable regardless. The debug arm is
/// deliberately *weak* (ADR-106): it names storage without keeping it alive, so
/// it is cleared after the sweep rather than traced here.
unsafe fn maybe_collect(ctx: *mut RuntimeContext) {
    if ctx.is_null() {
        return;
    }
    // SAFETY: ctx is live and wired.
    let roots = unsafe { crate::roots::RuntimeRoots::from_context(ctx) };
    unsafe { heap(ctx).maybe_collect(&roots) };
}

/// Pace the collector and mint the token one allocation needs.
///
/// Every `praxis_*` wrapper reaches the heap through [`gc_alloc`] or
/// [`gc_alloc_owned`], which call this — and even a wrapper that reached
/// `Heap::alloc` directly would have to come through here, because the token
/// has no other producer. That is the whole point: an allocation that skipped
/// the pacer would let a program whose pressure comes from `Text`, `.len()` or
/// checked arithmetic run arbitrarily long without the collector ever being
/// offered a turn.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`. Every allocating
/// wrapper already requires this to reach the heap at all.
#[inline]
unsafe fn safepoint<'a>(ctx: *mut RuntimeContext) -> (&'a Heap, Safepoint<'a>) {
    // SAFETY: caller upholds ctx validity.
    let h = unsafe { heap(ctx) };
    // SAFETY: as above; the roots are read out of the same live context.
    let roots = unsafe { crate::roots::RuntimeRoots::from_context(ctx) };
    let sp = h.pace(&roots);
    (h, sp)
}

/// Pace the collector, then allocate a `Copy` payload (§12.4).
///
/// The descriptor arrives as a [`Payload<T>`] — `scalars::INT_PAYLOAD`, not
/// `&scalars::INT` — so `value`'s Rust type is checked against it here, at the
/// call: a value of the wrong type is an `E0308`, and an *untyped* literal
/// infers as the payload type instead of defaulting to `i32`. A bare descriptor
/// reference with `T` free would let a width mismatch reach the heap and abort
/// the process from inside `extern "C"`, which §10.4 forbids.
///
/// # Safety
/// `ctx` must be live and wired.
#[inline]
unsafe fn gc_alloc<T: Copy>(ctx: *mut RuntimeContext, payload: Payload<T>, value: T) -> GcRef {
    // SAFETY: caller upholds ctx validity.
    let (h, sp) = unsafe { safepoint(ctx) };
    h.alloc(sp, payload, value)
}

/// Pace the collector, then allocate a payload that owns Rust resources.
///
/// [`gc_alloc`]'s counterpart for the payloads no [`Payload<T>`] can describe.
/// The type is named **once**, as `P`: [`Heap::alloc_payload`] derives the size,
/// the alignment and the write from it, so no wrapper restates all three and
/// keeps them in agreement by hand.
///
/// The payload arrives as a producer rather than a value, and that is
/// load-bearing: `init` runs *after* [`safepoint`] has given the collector its
/// turn, so a payload built out of bare `GcRef`s — `vec![fill; n]` in
/// [`praxis_vec_filled`] and [`praxis_grid_filled`] — is never a `Vec<GcRef>`
/// live across a collection with no root set able to see it.
///
/// # Safety
/// `ctx` must be live and wired, and `descriptor` must be `P`'s own descriptor
/// ([`Heap::alloc_payload`]'s contract).
#[inline]
unsafe fn gc_alloc_owned<P>(
    ctx: *mut RuntimeContext,
    descriptor: &'static TypeDescriptor,
    init: impl FnOnce() -> P,
) -> GcRef {
    // SAFETY: caller upholds ctx validity.
    let (h, sp) = unsafe { safepoint(ctx) };
    // `init()` is evaluated here, downstream of the safepoint above — see the
    // ordering note in this function's doc.
    // SAFETY: forwarded from this function's contract.
    unsafe { h.alloc_payload(sp, descriptor, init()) }
}

/// The immortal `Bool` for `value`, off the context's cached singletons.
///
/// Never an allocation: there are exactly two `Bool` values and the runtime
/// minted both at startup, so every comparison, `contains` and `is_empty` a
/// program evaluates answers with a singleton rather than consuming arena
/// storage permanently. It is also what makes those manifest rows honestly
/// `Effect::Pure`: nothing here can collect, so the call site is not a
/// safepoint.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[inline]
unsafe fn bool_ref(ctx: *mut RuntimeContext, value: bool) -> GcRef {
    // SAFETY: caller upholds ctx validity.
    let c = unsafe { &*ctx };
    if value { c.true_ref } else { c.false_ref }
}

/// The `Int` for `value`: the interned immortal when it is small
/// ([`crate::small_int`]), a fresh allocation otherwise.
///
/// [`bool_ref`]'s shape, one step less absolute: `Bool` has two values so it is
/// always the singleton, while `Int` has a *range* that is interned and an
/// unbounded remainder that is not. Every wrapper that answers an `Int` reaches
/// the heap through here, so the interning covers not just literals but
/// `Vec.len()`, a `Counter` bump, an enum tag, a comparison's index and the
/// result of arithmetic — which is where most of a real program's small `Int`s
/// come from.
///
/// # It paces even when it does not allocate, and that is deliberate
///
/// The manifest declares `VecLen`, `MapLen`, `EnumTag`, `TextLen`, `CounterGet`
/// and two dozen more `Effect::Allocates`, which is generated code's contract
/// that the call site is a GC safepoint. If this returned before [`safepoint`],
/// a loop whose only allocations were small `Int`s would never offer the
/// collector a turn — the collector's *only* trigger is the pacing counter, and
/// nothing else in such a loop touches it. So the token is minted and then
/// dropped: [`Safepoint`] is `#[must_use]`, so `drop(sp)` is the honest spelling
/// of "the collector got its turn and we allocated nothing", and it is a
/// compile error to forget which of the two happened.
///
/// The interned path therefore costs a threshold compare and a range test rather
/// than an allocation. `Inst::ConstGc` is what removes even that, but only for a
/// *literal*, where the compiler knows the value and no manifest row applies.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[inline]
unsafe fn int_ref(ctx: *mut RuntimeContext, value: i64) -> GcRef {
    // SAFETY: caller upholds ctx validity.
    let (h, sp) = unsafe { safepoint(ctx) };
    match crate::small_int::index_of(value) {
        Some(i) => {
            drop(sp);
            // SAFETY: `index_of` bounds `i` by `SMALL_INT_COUNT`, and
            // `Runtime::context` points `small_ints` at a table of exactly that
            // length whose slot `i` holds `SMALL_INT_MIN + i`.
            unsafe { *(*ctx).small_ints.add(i) }
        }
        None => h.alloc(sp, scalars::INT_PAYLOAD, value),
    }
}

/// The `Char` for `code`: the interned immortal when it is ASCII
/// ([`crate::small_char`]), a fresh allocation otherwise.
///
/// [`int_ref`]'s shape and its argument (ADR-107). Every wrapper that answers a
/// `Char` reaches the heap through here — [`checked_alloc_char`], which is both
/// `praxis_alloc_char` and `praxis_int_to_char`; [`praxis_text_get`], which is
/// both `t[i]` and every step of `for c in t`; and [`default_cell`], which is a
/// `Grid[Char]`'s fill. `praxis_text_get` is the one that matters for real code:
/// an AoC-shaped program that walks a line of text would otherwise box a fresh
/// object per character read, and every character of such a line is ASCII.
///
/// # It paces even when it does not allocate
///
/// The manifest declares `TextGet`, `AllocChar` and `IntToChar`
/// `Effect::AllocatesAndFaults`, which is generated code's contract that the call
/// site is a GC safepoint. If this returned before [`safepoint`], `for c in text`
/// over an ASCII line would never offer the collector a turn — the collector's
/// *only* trigger is the pacing counter, and a loop that reads characters and
/// compares them touches nothing else that would bump it. So the token is minted
/// and then dropped: [`Safepoint`] is `#[must_use]`, so `drop(sp)` is the honest
/// spelling of "the collector got its turn and we allocated nothing", and it is a
/// compile error to forget which of the two happened. Pinned by
/// `char_ref_paces_the_collector_even_when_it_answers_from_the_table`.
///
/// Unlike `int_ref` there is no `Inst::ConstGc` that removes even the pacing
/// check: that instruction exists for a *literal*, and the language has no
/// character literal (ADR-107 Decision 2).
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`, and `code` must be a valid
/// Unicode scalar value — every caller has already established this, either by
/// [`checked_alloc_char`]'s range check or by starting from a Rust `char`.
#[inline]
unsafe fn char_ref(ctx: *mut RuntimeContext, code: u32) -> GcRef {
    debug_assert!(
        crate::scalars::is_valid_char(code),
        "char_ref's callers validate first"
    );
    // SAFETY: caller upholds ctx validity.
    let (h, sp) = unsafe { safepoint(ctx) };
    match crate::small_char::index_of(code) {
        Some(i) => {
            drop(sp);
            // SAFETY: `index_of` bounds `i` by `SMALL_CHAR_COUNT`, and
            // `Runtime::context` points `small_chars` at a table of exactly that
            // length whose slot `i` holds code point `i`.
            unsafe { *(*ctx).small_chars.add(i) }
        }
        None => h.alloc(sp, scalars::CHAR_PAYLOAD, code),
    }
}

/// A fresh owned `Text` holding `s`.
///
/// [`bool_ref`]/[`int_ref`]/[`char_ref`]'s place in the file but not their
/// shape: there is nothing interned to answer from — every `Text` is a distinct
/// object — so this always allocates, and always paces.
///
/// `impl Into<Box<str>>` is what lets every text-producing wrapper share this
/// one allocation without a defensive `.clone()`: a `String` from a renderer, a
/// `Box<str>` the caller already built ([`praxis_alloc_text`]), a `&str`.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[inline]
unsafe fn text_ref(ctx: *mut RuntimeContext, s: impl Into<Box<str>>) -> GcRef {
    // SAFETY: caller upholds ctx validity; `TextPayload` is `TEXT`'s payload type.
    unsafe {
        gc_alloc_owned(ctx, &crate::text::TEXT, || {
            crate::text::TextPayload::owned(s)
        })
    }
}

/// Read `r`'s payload through a [`Payload`] handle, first checking that `r`
/// really is that handle's type.
///
/// This is the reader to reach for whenever a wrapper receives a `GcRef` it did
/// not itself allocate — a value handed back by a program's closure, most of
/// all. Two mistakes are impossible through it: reading a value of the wrong
/// *type* (the identity check answers `None`, and the caller decides whether
/// that is a `TypeMismatch` fault), and reading the right type at the wrong
/// *width* (the width is `size_of::<T>()`, which [`Payload::new`] proved is the
/// descriptor's width when the handle was declared). Reading a one-byte `Bool`
/// payload with an eight-byte `int_payload` is the class it rules out.
///
/// # Safety
/// `r` must be a valid `GcRef` into a live heap.
#[inline]
unsafe fn read_scalar<T: Copy>(r: GcRef, handle: crate::descriptor::Payload<T>) -> Option<T> {
    if !std::ptr::eq(r.descriptor(), handle.descriptor()) {
        return None;
    }
    // SAFETY: the identity check proves `r`'s payload is this handle's type, and
    // the handle's own construction proved `T` is that type's layout.
    Some(unsafe { handle.read(r.payload::<u8>()) })
}

/// Read the `i64` payload of an `Int` `GcRef`. Used by every arithmetic wrapper.
///
/// Prefer [`read_scalar`] for any value whose type is not already established:
/// this reads eight bytes, and the descriptor check is all that stands between
/// it and a narrower payload.
///
/// # The width check is a branch, not a `debug_assert`
///
/// A `debug_assert` is not a bound — it compiles out of a release build, leaving
/// an eight-byte read against a descriptor that may be narrower or zero bytes
/// wide, so the two profiles would answer differently and the wrong one is the
/// one users get. As an ordinary branch it holds in every profile: the read
/// cannot happen. What happens instead is ADR-080's defined panic path —
/// `abi_guard` catches it, raises `RaisedFault::PANIC` with a message naming
/// the wrapper, and either faults into the crash debugger (a wrapper the
/// manifest declares faultable) or prints that message and aborts (one it does
/// not, which is `praxis_int_load`'s case). The guard is a memory-safety check
/// on a raw read, not a stand-in for a type system.
///
/// This stays `-> i64` rather than becoming fallible: sixty-odd wrappers read
/// through it, and a `ctx`-threading signature change is a larger edit than the
/// one memory safety needs.
#[inline]
unsafe fn int_payload(r: GcRef) -> i64 {
    // SAFETY: `read_scalar` proves `r`'s descriptor *is* `INT` before reading,
    // so the eight bytes are in bounds and are an `i64`. The compiler only emits
    // these calls with Int-typed operands, and a fault that would feed a
    // non-`Int` (the Unit sentinel, say) into an arithmetic wrapper is diverted
    // by `Inst::CheckFault` before it gets here (§10.4).
    unsafe { read_scalar(r, scalars::INT_PAYLOAD) }
        .unwrap_or_else(|| scalar_type_mismatch("int_payload", "Int", r.descriptor().name))
}

/// The refusal every scalar reader shares, out of line so the check costs a
/// never-taken branch on the hot path.
///
/// `#[cold]` and `#[inline(never)]` are what let the check be unconditional, and
/// it must be unconditional: a `debug_assert` compiles out, so a release build
/// would do an out-of-bounds heap read where a debug build aborted.
///
/// A panic here is ADR-080's defined path: `abi_guard!` catches it, raises
/// `RaisedFault::PANIC` naming the wrapper, and either faults into the crash
/// debugger or prints the message and aborts. That is the backstop, not the
/// primary defence — a raw scalar read must prove its own width whatever the
/// type system believes.
#[cold]
#[inline(never)]
fn scalar_type_mismatch(what: &'static str, want: &'static str, found: &'static str) -> ! {
    panic!("{what} wants a `{want}` payload; this value is a `{found}` (REP-56)");
}

/// [`praxis_alloc_text`]'s refusal when its buffer is not UTF-8 — a violated
/// precondition, not a runtime condition (ADR-111).
///
/// **Why this is a panic and not a fault.** The precondition is the one that is
/// actually true: the compiler's bytes are a Rust `&str` unbroken from
/// `Lit::Text(String)` through `AllocKind::Text { value: String }` to
/// `Generation::alloc_str`, and the one caller in this crate that holds raw
/// *host* bytes — [`praxis_get_input`] — validates them itself and raises
/// `InvalidText` there, where the `read` can observe it. Spelling it as a fault
/// instead would cost a `CheckFault` after every text literal for a fault no
/// generated call site can raise.
///
/// **Why the `from_utf8` call above stays, in every profile.** This is
/// [`scalar_type_mismatch`]'s argument verbatim and it is why that function is
/// the neighbour: a `debug_assert` is not a bound, because it compiles out of a
/// release build. What would be left in release is a `Box<str>` built from bytes
/// that are not UTF-8, which [`crate::text::text_str`] later hands out as a
/// `&str` — so the two profiles would answer differently and the wrong one is
/// the one users get. `from_utf8_unchecked` is the same hole with the check
/// deleted rather than compiled out. The unconditional branch costs a
/// never-taken jump to this cold callee, which is the price ADR-102 §1 already
/// established for the inline scalar loads.
///
/// **It must not reach `set_fault`, and that is enforced.**
/// `a_wrapper_that_can_raise_a_fault_declares_that_it_faults` computes a textual
/// fixed point over this file: a body that can reach `set_fault`, directly or
/// through a helper defined here, must belong to a symbol whose manifest row
/// says it faults. `AllocText`'s row is `Effect::Allocates`, so a refusal
/// spelled as a fault would fail that test.
///
/// The end-to-end path on a violation is ADR-080's: panic → `abi_guard!`
/// catches → `panic_fault_is_observable("praxis_alloc_text")` reads the
/// `Allocates` row and answers `false` → the message is printed and the process
/// aborts. That is the same outcome `praxis_int_load` gives a wrong descriptor,
/// and it falls out of the row change with no code of its own.
#[cold]
#[inline(never)]
fn text_bytes_are_not_utf8(len: usize) -> ! {
    panic!(
        "praxis_alloc_text was handed {len} bytes that are not valid UTF-8; its \
         `# Safety` contract requires them to be (ADR-111). A host with untrusted \
         bytes must validate them first, as `praxis_get_input` does."
    );
}

/// The Unit `GcRef` returned on fault paths as the "defined dummy" (§10.4).
/// Reads the cached immortal `unit_ref` from the context, which is stable for
/// the program's lifetime.
#[inline]
unsafe fn unit_sentinel(ctx: *mut RuntimeContext) -> GcRef {
    unsafe { (*ctx).unit_ref }
}

/// The slot a source index `i` names in a container of `len` elements, or
/// `None` when it is outside `0..len` (§9.2, §11.1).
///
/// **The one place a source index becomes a `usize`.** The `< 0` test has to
/// run before the cast: a negative `i64` casts to a value near `usize::MAX` and
/// would sail straight past a bare length comparison. Every accessor shares this
/// one copy, so no single site can drop the guard invisibly. The construction
/// side guards the same hazard with a named
/// [`GridExtent`](crate::collections::GridExtent).
///
/// `Vec` and `Deque` indexing is one language rule applied to two containers,
/// so they share this rather than each spelling it out.
fn linear_index(i: i64, len: usize) -> Option<usize> {
    if i < 0 {
        return None;
    }
    // Non-negative above, so the cast is exact.
    let i = i as usize;
    (i < len).then_some(i)
}

/// The row-major slot `(x, y)` names in a `width × height` grid, or `None` when
/// either axis falls outside it.
///
/// Both axes go through [`linear_index`], so the 2-D rule is the 1-D rule twice
/// and the signed-to-`usize` cast still exists in exactly one place. The
/// product cannot overflow: `y < height` and `x < width` with
/// `height = items.len() / width`, so the result is below `items.len()`.
fn cell_index(x: i64, y: i64, width: usize, height: usize) -> Option<usize> {
    let x = linear_index(x, width)?;
    let y = linear_index(y, height)?;
    Some(y * width + x)
}

/// [`linear_index`], raising `IndexOutOfBounds` when the index is out of range.
/// `None` means the fault is already set and the caller owes only its sentinel
/// return.
///
/// The raising wrapper is deliberately separate from the pure predicate,
/// because not every bounds question may fault. [`praxis_grid_contains`] is
/// declared `Pure` in the ABI manifest while `GridGet` is `Faults`, and MIR's
/// `RedundantFaultCheck` emits no `CheckFault` after a `Pure` call — so a fault
/// raised on every legitimate `false` would sit pending until some later check
/// mistook it for its own. Bounds-testing sites take [`cell_index`] /
/// [`linear_index`]; only sites the manifest says can fault take these.
///
/// # Safety
/// `ctx` must be live and wired.
unsafe fn checked_index(ctx: *mut RuntimeContext, i: i64, len: usize) -> Option<usize> {
    let idx = linear_index(i, len);
    if idx.is_none() {
        unsafe { set_fault(ctx, RaisedFault::INDEX_OUT_OF_BOUNDS) };
    }
    idx
}

/// [`cell_index`], raising `IndexOutOfBounds` when `(x, y)` is off the grid.
/// See [`checked_index`] for why the raising and the predicate are two
/// functions.
///
/// # Safety
/// `ctx` must be live and wired.
unsafe fn checked_cell(
    ctx: *mut RuntimeContext,
    x: i64,
    y: i64,
    width: usize,
    height: usize,
) -> Option<usize> {
    let idx = cell_index(x, y, width, height);
    if idx.is_none() {
        unsafe { set_fault(ctx, RaisedFault::INDEX_OUT_OF_BOUNDS) };
    }
    idx
}

// ---------------------------------------------------------------------------
// Allocation wrappers.
// ---------------------------------------------------------------------------

/// The `Int` for `value` (§4.3, §11.1) — the interned immortal when it is small
/// ([`crate::small_int`]), a fresh box otherwise.
///
/// The row stays `Effect::Allocates`, not `Pure` as `AllocBool`'s is: this
/// wrapper still allocates for an out-of-range value, so the call site is still
/// a GC safepoint and generated code must still spill its roots across it. The
/// interning is invisible to the caller by design.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext` whose `heap` is valid.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_int(ctx: *mut RuntimeContext, value: i64) -> GcRef {
    abi_guard!("praxis_alloc_int", ctx, {
        // `int_ref` paces first, rooted at the whole `RuntimeRoots`. The new object
        // is not yet a root, but it is returned by value to the caller, which spills
        // it — so it is safe across this collection (the *previous* allocation's
        // result was already spilled by the backend before this wrapper was called).
        // SAFETY: caller upholds the ctx/heap validity.
        unsafe { int_ref(ctx, value) }
    })
}

/// Allocate a boxed `Bool` from a 0/1 value (§4.3). Returns the immortal
/// singleton, never a fresh allocation.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_bool(ctx: *mut RuntimeContext, value: i64) -> GcRef {
    abi_guard!("praxis_alloc_bool", ctx, {
        // There are two `Bool` values, and the runtime allocated both at startup.
        // `value != 0` is true; `0` is false.
        // SAFETY: caller upholds ctx validity.
        let c = unsafe { &*ctx };
        if value != 0 { c.true_ref } else { c.false_ref }
    })
}

/// Allocate the `Unit` singleton (§4.3).
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_unit(ctx: *mut RuntimeContext) -> GcRef {
    abi_guard!("praxis_alloc_unit", ctx, {
        // The one `Unit` value, cached on the context for the fault path.
        // SAFETY: caller upholds ctx validity.
        unsafe { (*ctx).unit_ref }
    })
}

/// Allocate a boxed `Char` from a Unicode scalar value (§4.3). The `value`
/// is the `u32` code point carried as `i64` (the uniform scalar ABI width). If
/// the code point is not a valid scalar, the fault is set and the Unit sentinel
/// is returned (no panic crosses the ABI).
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_char(ctx: *mut RuntimeContext, value: i64) -> GcRef {
    abi_guard!("praxis_alloc_char", ctx, {
        // SAFETY: caller upholds ctx/heap validity.
        unsafe { checked_alloc_char(ctx, value) }
    })
}

/// Box an `i64` as a `Char`, or raise `InvalidChar` and answer the Unit sentinel.
///
/// The one place the `i64`-to-code-point rule is enforced, because there are two
/// doors into it — `praxis_alloc_char` (the parser and codegen's `AllocKind::Char`)
/// and `praxis_int_to_char` (`Int.to_char()`, ADR-086) — and a rule stated at both
/// goes stale at one.
///
/// **Range-check before narrowing.** `value as u32` truncates, so
/// `0x1_0000_0041` would silently become `'A'`. The scalar ABI is 64 bits wide;
/// a code point is not, and the conversion has to say so rather than wrap. The
/// surrogate range is rejected for the same reason: `char::from_u32` is what
/// decides, not a width.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
unsafe fn checked_alloc_char(ctx: *mut RuntimeContext, value: i64) -> GcRef {
    let Ok(code) = u32::try_from(value) else {
        unsafe { set_fault(ctx, RaisedFault::INVALID_CHAR) };
        return unsafe { unit_sentinel(ctx) };
    };
    if !crate::scalars::is_valid_char(code) {
        unsafe { set_fault(ctx, RaisedFault::INVALID_CHAR) };
        return unsafe { unit_sentinel(ctx) };
    }
    // SAFETY: caller upholds ctx/heap validity; code is a validated scalar.
    unsafe { char_ref(ctx, code) }
}

/// Allocate an owned `Text` from a UTF-8 byte buffer (§4.3, ADR-013).
///
/// **UTF-8 is the caller's precondition, and this wrapper cannot fault**
/// (ADR-111). Its row is `Effect::Allocates`, so `Inst::Alloc { AllocKind::Text }`
/// is followed by no `CheckFault` — `praxis_mir::verify` rejects one — and a
/// `Text` literal in a loop is hoisted into the preheader like a `Float` one
/// (ADR-108 §3). Handing this bytes that are not UTF-8 is a violated contract,
/// not a runtime condition, and it aborts through `text_bytes_are_not_utf8`
/// (whose doc carries the argument) rather than raising `InvalidText`.
///
/// A host that holds *untrusted* bytes validates them before calling. There is
/// exactly one such caller in this crate — [`praxis_get_input`], whose row is
/// `AllocatesAndFaults` — and it raises `InvalidText` itself, so the fault a
/// `read` can observe still lands at the `read` (§4.3, §7.10).
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`; `bytes` must point at
/// `len` valid UTF-8 bytes that remain valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_text(
    ctx: *mut RuntimeContext,
    bytes: *const u8,
    len: usize,
) -> GcRef {
    abi_guard!("praxis_alloc_text", ctx, {
        let slice = if bytes.is_null() || len == 0 {
            &[]
        } else {
            // SAFETY: caller guarantees `bytes..bytes+len` is a valid, UTF-8 buffer.
            unsafe { std::slice::from_raw_parts(bytes, len) }
        };
        // The check is unconditional in every profile: it is the backstop on a
        // raw read, the same standing `read_scalar` has, and its argument is
        // written out at `text_bytes_are_not_utf8`. A violation refuses rather
        // than recovering lossily behind a fault nobody at a generated call site
        // could observe (ADR-111).
        let owned: Box<str> = match std::str::from_utf8(slice) {
            Ok(s) => s.into(),
            Err(_) => text_bytes_are_not_utf8(len),
        };
        // SAFETY: ctx/heap valid.
        unsafe { text_ref(ctx, owned) }
    })
}

// ---------------------------------------------------------------------------
// Scalar extraction / materialization.
// ---------------------------------------------------------------------------

/// Read the `i64` payload of an `Int` `GcRef` (§10.3 transient scalar).
///
/// # Safety
/// `r` must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_load(_ctx: *mut RuntimeContext, r: GcRef) -> i64 {
    abi_guard!("praxis_int_load", _ctx, {
        // SAFETY: caller guarantees `r` is an Int.
        unsafe { int_payload(r) }
    })
}

/// Read a `Bool` payload as 0/1 (§10.3 transient scalar).
///
/// # Safety
/// `r` must be a valid `Bool` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bool_load(_ctx: *mut RuntimeContext, r: GcRef) -> i64 {
    abi_guard!("praxis_bool_load", _ctx, {
        // Read the byte, then decide — never `*r.payload::<bool>()`. A Rust
        // `bool` whose byte is not 0 or 1 is an *invalid value*, and
        // materializing one is undefined behaviour whatever the read's bounds
        // are; `BoolPayload` is a `u8` precisely so the runtime never has to.
        // SAFETY: `read_scalar` bounds the read against `r`'s own descriptor.
        let byte = unsafe { read_scalar(r, scalars::BOOL_PAYLOAD) }.unwrap_or_else(|| {
            scalar_type_mismatch("praxis_bool_load", "Bool", r.descriptor().name)
        });
        i64::from(byte != 0)
    })
}

/// Read a `Char` payload as its `u32` code point widened to `i64` (§4.3).
///
/// # Safety
/// `r` must be a valid `Char` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_char_load(_ctx: *mut RuntimeContext, r: GcRef) -> i64 {
    abi_guard!("praxis_char_load", _ctx, {
        // SAFETY: `read_scalar` bounds the read against `r`'s own descriptor.
        let code = unsafe { read_scalar(r, scalars::CHAR_PAYLOAD) }.unwrap_or_else(|| {
            scalar_type_mismatch("praxis_char_load", "Char", r.descriptor().name)
        });
        i64::from(code)
    })
}

/// Allocate a boxed `Float` from an `i64` carrying the IEEE-754 binary64 bit
/// pattern (§4.3, §4.12). The uniform scalar ABI carries every payload as
/// `i64`; a float is transported as `f64::to_bits()` and reassembled here.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_float(ctx: *mut RuntimeContext, value: i64) -> GcRef {
    abi_guard!("praxis_alloc_float", ctx, {
        let f = f64::from_bits(value as u64);
        // SAFETY: caller upholds ctx/heap validity; all f64 values are valid Floats.
        unsafe { gc_alloc(ctx, scalars::FLOAT_PAYLOAD, f) }
    })
}

/// Read a `Float` payload as its IEEE-754 bit pattern widened to `i64`
/// (§10.3 transient scalar). Generated code keeps floats in the uniform `i64`
/// scalar channel; the bit pattern is reassembled into an `f64` only at the
/// point of an arithmetic/comparison instruction.
///
/// # Safety
/// `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_load(_ctx: *mut RuntimeContext, r: GcRef) -> i64 {
    abi_guard!("praxis_float_load", _ctx, {
        // Through `float_payload`, which goes through `read_scalar`: the read
        // proves its own width rather than taking the caller's word for it.
        //
        // It matters more since ADR-102: generated code reads a `Float`
        // payload inline behind a descriptor check, and this wrapper is the
        // cold path that check branches to. If it read unchecked, the two
        // would disagree about what a wrong descriptor means — the inline
        // path would refuse and the fallback would read anyway.
        //
        // SAFETY: caller guarantees `r` is a valid `GcRef`; `float_payload`
        // proves it is a `Float` before reading.
        unsafe { float_payload(r) }.to_bits() as i64
    })
}

// ---------------------------------------------------------------------------
// Float conversion & methods (§4.12). Float arithmetic never faults (IEEE-754
// produces inf/nan); only the narrowing `to_int` conversion does.
// ---------------------------------------------------------------------------

/// Read a `Float` payload as an `f64` (private helper).
///
/// # Safety
/// `r` must be a valid `Float` `GcRef`.
unsafe fn float_payload(r: GcRef) -> f64 {
    // SAFETY: `read_scalar` proves `r`'s descriptor is `FLOAT` before reading.
    unsafe { read_scalar(r, scalars::FLOAT_PAYLOAD) }
        .unwrap_or_else(|| scalar_type_mismatch("float_payload", "Float", r.descriptor().name))
}

/// Widen an `Int` to a `Float` (§4.12). Never faults — every `i64` is exactly
/// representable as an `f64`? No: integers above 2^53 lose precision, but the
/// conversion is still total and well-defined (rounds to nearest). This is the
/// explicit widening method `Int.to_float()`.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_to_float(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_int_to_float", ctx, {
        let i = unsafe { int_payload(r) };
        // SAFETY: ctx/heap valid; every widened int is a valid Float payload.
        unsafe { gc_alloc(ctx, scalars::FLOAT_PAYLOAD, i as f64) }
    })
}

/// `Char.to_int()` — the Unicode scalar value, as an `Int` (ADR-086). Never
/// faults: every valid scalar fits an `i64`.
///
/// This reads through [`read_scalar`] with the `Char` handle rather than
/// `int_payload`, because a `Char` payload is **four** bytes and an `i64` read
/// would take eight of them.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Char` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_char_to_int(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_char_to_int", ctx, {
        // SAFETY: caller guarantees `r` is a valid `GcRef`; `read_scalar` proves
        // the descriptor is `CHAR` before reading its four bytes.
        let code = unsafe { read_scalar(r, scalars::CHAR_PAYLOAD) }.unwrap_or_else(|| {
            scalar_type_mismatch("praxis_char_to_int", "Char", r.descriptor().name)
        });
        // SAFETY: ctx/heap valid; every scalar value is a valid Int payload.
        unsafe { int_ref(ctx, i64::from(code)) }
    })
}

/// `Int.to_char()` — the `Char` with this Unicode scalar value (ADR-086).
/// Faults (`InvalidChar`) on a negative value, one above `0x10FFFF`, or one in
/// the surrogate range: those are not scalar values and have no `Char`.
///
/// It is `Char.to_int()`'s partial half exactly as `Float.to_int()` is
/// `Int.to_float()`'s — the narrowing direction is the one that can fail. The
/// check lives in [`checked_alloc_char`] and is not restated here.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_to_char(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_int_to_char", ctx, {
        let value = unsafe { int_payload(r) };
        // SAFETY: caller upholds ctx/heap validity.
        unsafe { checked_alloc_char(ctx, value) }
    })
}

/// Narrow a `Float` to an `Int` by truncating toward zero (§4.12). Faults
/// (`FloatToInt`) on NaN, ±infinity, or a finite value outside the signed
/// 64-bit range — these have no exact `Int` representation. On fault, sets
/// `pending_fault` and returns the Unit sentinel (no panic crosses the ABI).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_to_int(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_to_int", ctx, {
        let f = unsafe { float_payload(r) };
        // NaN, infinities, and out-of-range finite values are not exactly
        // representable as i64. Rust's `as i64` saturates (inf→i64::MAX,
        // -inf→i64::MIN, nan→0), which would silently produce a plausible-but-wrong
        // value; per §4.12 these cases fault instead.
        if f.is_nan() || f.is_infinite() || f < i64::MIN as f64 || f >= i64::MAX as f64 {
            unsafe { set_fault(ctx, RaisedFault::FLOAT_TO_INT) };
            return unsafe { unit_sentinel(ctx) };
        }
        // The range check above bounds f to (-2^63, 2^63); truncation toward zero is
        // then exact for every representable integer and inexact-but-safe for the
        // fractional part (which is discarded).
        // SAFETY: ctx/heap valid; the value is in i64 range.
        unsafe { int_ref(ctx, f as i64) }
    })
}

/// Re-box a `Float` after a pure transform (no fault possible). Used by
/// `abs`/`sqrt`/`floor`/`ceil`/`round`/`sign`.
unsafe fn rebox_float(ctx: *mut RuntimeContext, out: f64) -> GcRef {
    // SAFETY: ctx/heap valid; every f64 is a valid Float payload.
    unsafe { gc_alloc(ctx, scalars::FLOAT_PAYLOAD, out) }
}

/// `Float.abs()` — absolute value (§4.12).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_abs(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_abs", ctx, {
        let f = unsafe { float_payload(r) };
        unsafe { rebox_float(ctx, f.abs()) }
    })
}

/// `Float.sqrt()` — square root (§4.12). Negative inputs yield NaN (IEEE-754);
/// this never faults.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_sqrt(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_sqrt", ctx, {
        let f = unsafe { float_payload(r) };
        unsafe { rebox_float(ctx, f.sqrt()) }
    })
}

/// `Float.floor()` — round toward negative infinity (§4.12).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_floor(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_floor", ctx, {
        let f = unsafe { float_payload(r) };
        unsafe { rebox_float(ctx, f.floor()) }
    })
}

/// `Float.ceil()` — round toward positive infinity (§4.12).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_ceil(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_ceil", ctx, {
        let f = unsafe { float_payload(r) };
        unsafe { rebox_float(ctx, f.ceil()) }
    })
}

/// `Float.round()` — round half away from zero (§4.12, matches Rust's `f64::round`).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_round(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_round", ctx, {
        let f = unsafe { float_payload(r) };
        unsafe { rebox_float(ctx, f.round()) }
    })
}

/// `Float.sign()` — sign as -1.0 / 0.0 / 1.0 (§4.12). NaN yields NaN.
///
/// Not `f64::signum`: that returns `1.0` for `+0.0` and `-1.0` for `-0.0`,
/// because it reports the IEEE *sign bit*, not the sign of the value. Zero has
/// no sign in the sense `sign()` documents, so both zeros yield `0.0`.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_sign(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_sign", ctx, {
        let f = unsafe { float_payload(r) };
        let sign = if f.is_nan() || f == 0.0 {
            // `f == 0.0` is true for both `+0.0` and `-0.0`; NaN falls through as
            // itself, which is what §4.12 specifies.
            f
        } else if f > 0.0 {
            1.0
        } else {
            -1.0
        };
        unsafe { rebox_float(ctx, sign) }
    })
}

/// `Float.is_nan()` — true iff NaN (§4.12).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_is_nan(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_is_nan", ctx, {
        let result = unsafe { float_payload(r) }.is_nan();
        // SAFETY: ctx valid; Bool immortal path.
        unsafe { bool_ref(ctx, result) }
    })
}

/// `Float.is_infinite()` — true iff ±infinity (§4.12).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_is_infinite(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_is_infinite", ctx, {
        let result = unsafe { float_payload(r) }.is_infinite();
        // SAFETY: ctx valid; Bool immortal path.
        unsafe { bool_ref(ctx, result) }
    })
}

/// `Float.min(other)` — the smaller of two floats (§4.12). Per IEEE-754 /
/// Rust's `f64::min`: if either operand is NaN, returns the other (NaN only
/// propagates when both are NaN). `-0.0` is less than `+0.0`.
///
/// # Safety
/// `ctx` must be live and wired; both operands must be valid `Float` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_min(
    ctx: *mut RuntimeContext,
    lhs: GcRef,
    rhs: GcRef,
) -> GcRef {
    abi_guard!("praxis_float_min", ctx, {
        let a = unsafe { float_payload(lhs) };
        let b = unsafe { float_payload(rhs) };
        unsafe { rebox_float(ctx, a.min(b)) }
    })
}

/// `Float.max(other)` — the larger of two floats (§4.12). See `praxis_float_min`
/// for NaN handling.
///
/// # Safety
/// `ctx` must be live and wired; both operands must be valid `Float` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_max(
    ctx: *mut RuntimeContext,
    lhs: GcRef,
    rhs: GcRef,
) -> GcRef {
    abi_guard!("praxis_float_max", ctx, {
        let a = unsafe { float_payload(lhs) };
        let b = unsafe { float_payload(rhs) };
        unsafe { rebox_float(ctx, a.max(b)) }
    })
}

/// `Float.to_text()` — the same text `out()` writes, which is the shortest form
/// that reads back as the same Praxis `Float` (§4.12, ADR-083).
///
/// It goes through `scalars::write_float` rather than restating the rule,
/// because `to_text()` and `out()` disagreeing is a defect in itself: a program
/// that prints a value and a program that builds a string from it must produce
/// the same characters.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Float` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_to_text(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_float_to_text", ctx, {
        let f = unsafe { float_payload(r) };
        let mut s = String::new();
        scalars::write_float(&mut s, f);
        // SAFETY: `s` is valid UTF-8 for the duration of the call; ctx/heap valid.
        unsafe { text_ref(ctx, s) }
    })
}

/// `Int.to_text()` — the same digits `out()` writes (ADR-143).
///
/// It goes through `scalars::write_int` rather than restating the rendering,
/// because `to_text()` and `out()` disagreeing is a defect in itself: a program
/// that prints a value and a program that builds a string from it must produce
/// the same characters. That is the guarantee, and the shared writer is what
/// makes it structural rather than a thing a test happens to check.
///
/// Never faults: every `i64` renders, `i64::MIN` included.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_to_text(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_int_to_text", ctx, {
        let v = unsafe { int_payload(r) };
        let mut s = String::new();
        scalars::write_int(&mut s, v);
        // SAFETY: `s` is valid UTF-8; ctx/heap valid.
        unsafe { text_ref(ctx, s) }
    })
}

/// `Char.to_text()` — the one-character `Text` holding this scalar, which is the
/// same character `out()` writes (ADR-143).
///
/// Shares `scalars::write_char` with the descriptor's `format` callback for
/// [`praxis_int_to_text`]'s reason. Never faults: a `CharPayload` is a validated
/// Unicode scalar value by construction (ADR-086).
///
/// Reads through [`read_scalar`] with the `Char` handle rather than
/// `int_payload`, because a `Char` payload is **four** bytes and an `i64` read
/// would take eight of them — the same care [`praxis_char_to_int`] takes.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Char` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_char_to_text(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_char_to_text", ctx, {
        // SAFETY: caller guarantees `r` is a valid `GcRef`; `read_scalar` proves
        // the descriptor is `CHAR` before reading its four bytes.
        let code = unsafe { read_scalar(r, scalars::CHAR_PAYLOAD) }.unwrap_or_else(|| {
            scalar_type_mismatch("praxis_char_to_text", "Char", r.descriptor().name)
        });
        let mut s = String::new();
        scalars::write_char(&mut s, code);
        // SAFETY: `s` is valid UTF-8; ctx/heap valid.
        unsafe { text_ref(ctx, s) }
    })
}

/// `pi()` — the constant π as a `Float` (§4.12 prelude free function).
///
/// # Safety
/// `ctx` must be live and wired.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_pi(ctx: *mut RuntimeContext) -> GcRef {
    abi_guard!("praxis_float_pi", ctx, {
        // SAFETY: ctx/heap valid.
        unsafe { gc_alloc(ctx, scalars::FLOAT_PAYLOAD, core::f64::consts::PI) }
    })
}

/// `e()` — Euler's number as a `Float` (§4.12 prelude free function).
///
/// # Safety
/// `ctx` must be live and wired.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_float_e(ctx: *mut RuntimeContext) -> GcRef {
    abi_guard!("praxis_float_e", ctx, {
        // SAFETY: ctx/heap valid.
        unsafe { gc_alloc(ctx, scalars::FLOAT_PAYLOAD, core::f64::consts::E) }
    })
}

// ---------------------------------------------------------------------------
// Checked arithmetic (§4.12). All fault rather than panic (§10.4).
// ---------------------------------------------------------------------------

macro_rules! checked_int_binop {
    ($name:ident, $op:tt, $fault:expr_2021) => {
        #[doc = concat!("Checked `Int ", stringify!($op), "` (§4.12). On fault sets `pending_fault` and returns Unit.")]
        ///
        /// # Safety
        /// `ctx` must be live and wired; both operands must be valid `Int` `GcRef`s.
        #[unsafe(no_mangle)]
        pub unsafe extern "C" fn $name(
            ctx: *mut RuntimeContext,
            lhs: GcRef,
            rhs: GcRef,
        ) -> GcRef {
            abi_guard!(stringify!($name), ctx, {
                let a = unsafe { int_payload(lhs) };
                let b = unsafe { int_payload(rhs) };
                match a.$op(b) {
                    Some(result) => unsafe { int_ref(ctx, result) },
                    None => {
                        unsafe { set_fault(ctx, $fault) };
                        unsafe { unit_sentinel(ctx) }
                    }
                }
            })
        }
    };
}

checked_int_binop!(praxis_int_add, checked_add, RaisedFault::INT_OVERFLOW);
checked_int_binop!(praxis_int_sub, checked_sub, RaisedFault::INT_OVERFLOW);
checked_int_binop!(praxis_int_mul, checked_mul, RaisedFault::INT_OVERFLOW);

/// Checked `Int` division (§4.12). Faults on division by zero, and on overflow
/// (`Int::MIN / -1`, the one signed-division case that overflows §4.12).
///
/// # Safety
/// `ctx` must be live and wired; both operands must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_div(ctx: *mut RuntimeContext, lhs: GcRef, rhs: GcRef) -> GcRef {
    abi_guard!("praxis_int_div", ctx, {
        let a = unsafe { int_payload(lhs) };
        let b = unsafe { int_payload(rhs) };
        if b == 0 {
            unsafe { set_fault(ctx, RaisedFault::DIV_BY_ZERO) };
            return unsafe { unit_sentinel(ctx) };
        }
        // `i64::MIN / -1` is the sole overflowing signed division: the mathematical
        // result (+2^63) is not representable, and the raw `/` panics on overflow in
        // debug builds (violating the no-panic-across-the-ABI rule, §10.4). Treat it
        // as checked-arithmetic overflow per §4.12.
        if a == i64::MIN && b == -1 {
            unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
            return unsafe { unit_sentinel(ctx) };
        }
        // Division truncates toward zero (Rust's `i64::div_euclid` rounds differently;
        // Praxis follows C/Rust integer division semantics toward zero).
        unsafe { int_ref(ctx, a / b) }
    })
}

/// Checked `Int` remainder (§4.12). Faults on division by zero, and on overflow
/// (`Int::MIN % -1`, whose result is not representable under the §4.12 rule).
///
/// # Safety
/// `ctx` must be live and wired; both operands must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_rem(ctx: *mut RuntimeContext, lhs: GcRef, rhs: GcRef) -> GcRef {
    abi_guard!("praxis_int_rem", ctx, {
        let a = unsafe { int_payload(lhs) };
        let b = unsafe { int_payload(rhs) };
        if b == 0 {
            unsafe { set_fault(ctx, RaisedFault::DIV_BY_ZERO) };
            return unsafe { unit_sentinel(ctx) };
        }
        // `i64::MIN % -1`: the remainder is 0 mathematically, but the raw `%` traps
        // on this exact case in debug builds because the corresponding quotient
        // overflows. Guard it for the same no-panic reason as `praxis_int_div`.
        if a == i64::MIN && b == -1 {
            unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
            return unsafe { unit_sentinel(ctx) };
        }
        unsafe { int_ref(ctx, a % b) }
    })
}

/// Negate an `Int` (§4.12). Faults on overflow (`Int::MIN`).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_neg(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_int_neg", ctx, {
        let a = unsafe { int_payload(r) };
        match a.checked_neg() {
            Some(result) => unsafe { int_ref(ctx, result) },
            None => {
                unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

// ---------------------------------------------------------------------------
// §4.12's explicit overflow alternatives: three modes —
// `wrapping_`, `saturating_`, `checked_` — over `add`, `sub` and `mul`.
//
// §4.12 states the family and its two closures (no `_div`/`_rem`, no
// `_neg`/`_abs`) and is the only place that rule is written; the catalog test
// `the_overflow_alternative_family_is_three_modes_over_three_operators` is what
// enforces it. Do not restate it here.
//
// **None of the nine can fault, and that is the whole point of them** — their
// manifest rows are `Allocates`, so ADR-088's verifier rule means no
// `CheckFault` follows the call. They allocate, like every other wrapper that
// answers a fresh number.
// ---------------------------------------------------------------------------

/// `a.wrapping_add(b)` (§4.12): two's-complement wraparound instead of a fault.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_wrapping_add(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_wrapping_add", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        unsafe { int_ref(ctx, x.wrapping_add(y)) }
    })
}

/// `a.saturating_add(b)` (§4.12): clamp to `Int`'s ends instead of faulting.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_saturating_add(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_saturating_add", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        unsafe { int_ref(ctx, x.saturating_add(y)) }
    })
}

/// `a.checked_add(b)` (§4.12): `Option[Int]` — `None` where the checked `+`
/// would fault.
///
/// It answers a real `Option` (ADR-076): the absence is the *answer* here, not
/// an error channel, which is exactly §4.7's distinction.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_checked_add(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_checked_add", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        match x.checked_add(y) {
            Some(sum) => unsafe {
                let scope = NativeScope::new(ctx);
                let boxed = int_ref(ctx, sum);
                let rooted = scope.root(boxed);
                option_some(ctx, rooted.get())
            },
            None => unsafe { option_none(ctx) },
        }
    })
}

/// `a.wrapping_sub(b)` (§4.12): two's-complement wraparound instead of a fault.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_wrapping_sub(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_wrapping_sub", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        unsafe { int_ref(ctx, x.wrapping_sub(y)) }
    })
}

/// `a.saturating_sub(b)` (§4.12): clamp to `Int`'s ends instead of faulting.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_saturating_sub(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_saturating_sub", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        unsafe { int_ref(ctx, x.saturating_sub(y)) }
    })
}

/// `a.checked_sub(b)` (§4.12): `Option[Int]` — `None` where the checked `-`
/// would fault.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_checked_sub(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_checked_sub", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        match x.checked_sub(y) {
            Some(difference) => unsafe {
                let scope = NativeScope::new(ctx);
                let boxed = int_ref(ctx, difference);
                let rooted = scope.root(boxed);
                option_some(ctx, rooted.get())
            },
            None => unsafe { option_none(ctx) },
        }
    })
}

/// `a.wrapping_mul(b)` (§4.12): two's-complement wraparound instead of a fault.
///
/// This is the one of the nine a program could not write for itself: with every
/// arithmetic operator checked and no bitwise operators in the language, there
/// is no in-language spelling of modular multiplication (§4.12).
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_wrapping_mul(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_wrapping_mul", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        unsafe { int_ref(ctx, x.wrapping_mul(y)) }
    })
}

/// `a.saturating_mul(b)` (§4.12): clamp to `Int`'s ends instead of faulting.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_saturating_mul(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_saturating_mul", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        unsafe { int_ref(ctx, x.saturating_mul(y)) }
    })
}

/// `a.checked_mul(b)` (§4.12): `Option[Int]` — `None` where the checked `*`
/// would fault.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_checked_mul(
    ctx: *mut RuntimeContext,
    a: GcRef,
    b: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_checked_mul", ctx, {
        let (x, y) = unsafe { (int_payload(a), int_payload(b)) };
        match x.checked_mul(y) {
            Some(product) => unsafe {
                let scope = NativeScope::new(ctx);
                let boxed = int_ref(ctx, product);
                let rooted = scope.root(boxed);
                option_some(ctx, rooted.get())
            },
            None => unsafe { option_none(ctx) },
        }
    })
}

// ---------------------------------------------------------------------------
// The §16.1 numeric prelude helpers: `abs`, `sign`, `min`, `max`, `clamp`,
// `gcd`, `lcm`.
//
// All seven are monomorphic on `Int` (ADR-058), so every payload read here is
// an `Int` payload and no descriptor check is needed. `min`/`max`/`clamp` hand
// back one of the references they were given rather than allocating a copy:
// an `Int` object is immutable, so sharing it is what "the smaller of the two"
// means. The four that compute a *new* number allocate one, and the three that
// can leave the `Int` range fault rather than wrapping — `abs(Int::MIN)` has no
// positive counterpart, and `gcd`/`lcm` reach the same edge through it.
// ---------------------------------------------------------------------------

/// `abs(n)` (§16.1). Faults on overflow: `Int::MIN` has no positive
/// counterpart, exactly as `praxis_int_neg` faults on it.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_abs(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_int_abs", ctx, {
        let a = unsafe { int_payload(r) };
        match a.checked_abs() {
            Some(result) => unsafe { int_ref(ctx, result) },
            None => {
                unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// `sign(n)` (§16.1): `-1`, `0` or `1`. Total — every `Int`, `Int::MIN`
/// included, has a sign in range.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_sign(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_int_sign", ctx, {
        let a = unsafe { int_payload(r) };
        unsafe { int_ref(ctx, a.signum()) }
    })
}

/// `min(a, b)` (§16.1): the smaller operand, returned as **the reference that
/// was passed in**. Allocates nothing.
///
/// # Safety
/// `ctx` must be live and wired; both operands must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_min(
    _ctx: *mut RuntimeContext,
    lhs: GcRef,
    rhs: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_min", _ctx, {
        let a = unsafe { int_payload(lhs) };
        let b = unsafe { int_payload(rhs) };
        if b < a { rhs } else { lhs }
    })
}

/// `max(a, b)` (§16.1): the larger operand, returned as **the reference that
/// was passed in**. Allocates nothing.
///
/// # Safety
/// `ctx` must be live and wired; both operands must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_max(
    _ctx: *mut RuntimeContext,
    lhs: GcRef,
    rhs: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_max", _ctx, {
        let a = unsafe { int_payload(lhs) };
        let b = unsafe { int_payload(rhs) };
        if b > a { rhs } else { lhs }
    })
}

/// `clamp(value, low, high)` (§16.1): `value` confined to the inclusive range
/// `low..=high`, returned as one of the three references passed in.
///
/// **Faults when `low > high`.** The range is empty, so there is no value to
/// return and no answer that is not a guess — clamping to an empty range is a
/// mistake in the program, not in the data, and a mistake is reported rather
/// than answered with an invented number. (Rust's `Ord::clamp` panics on the
/// same input; a panic across `extern "C"` is what §10.4 forbids, so it is a
/// fault.) The kind is `EmptyRange` (ADR-058).
///
/// # Safety
/// `ctx` must be live and wired; all three operands must be valid `Int`
/// `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_clamp(
    ctx: *mut RuntimeContext,
    value: GcRef,
    low: GcRef,
    high: GcRef,
) -> GcRef {
    abi_guard!("praxis_int_clamp", ctx, {
        let v = unsafe { int_payload(value) };
        let lo = unsafe { int_payload(low) };
        let hi = unsafe { int_payload(high) };
        if lo > hi {
            unsafe { set_fault(ctx, RaisedFault::EMPTY_RANGE) };
            return unsafe { unit_sentinel(ctx) };
        }
        if v < lo {
            low
        } else if v > hi {
            high
        } else {
            value
        }
    })
}

/// The non-negative greatest common divisor of two `i64`s, computed by
/// Euclid's algorithm **in `i128`** so that `Int::MIN`'s absolute value needs no
/// special case. Returns `None` only when the mathematical result is outside the
/// `Int` range, which happens for exactly one input pair:
/// `gcd(Int::MIN, Int::MIN)` is `2^63`.
///
/// `gcd(0, 0)` is `0` — the conventional answer, and the identity `gcd(n, 0) ==
/// abs(n)` extended to `n == 0`.
fn checked_gcd(a: i64, b: i64) -> Option<i64> {
    let mut x = (a as i128).abs();
    let mut y = (b as i128).abs();
    while y != 0 {
        let t = x % y;
        x = y;
        y = t;
    }
    i64::try_from(x).ok()
}

/// `gcd(a, b)` (§16.1): the non-negative greatest common divisor. Faults on the
/// one pair whose result is out of range (`gcd(Int::MIN, Int::MIN)`).
///
/// # Safety
/// `ctx` must be live and wired; both operands must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_gcd(ctx: *mut RuntimeContext, lhs: GcRef, rhs: GcRef) -> GcRef {
    abi_guard!("praxis_int_gcd", ctx, {
        let a = unsafe { int_payload(lhs) };
        let b = unsafe { int_payload(rhs) };
        match checked_gcd(a, b) {
            Some(result) => unsafe { int_ref(ctx, result) },
            None => {
                unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// `lcm(a, b)` (§16.1): the non-negative least common multiple, `0` when either
/// operand is `0`. Faults when the result does not fit an `Int` — which it
/// often does not, since the product of two large operands overflows long
/// before their multiple does.
///
/// # Safety
/// `ctx` must be live and wired; both operands must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_int_lcm(ctx: *mut RuntimeContext, lhs: GcRef, rhs: GcRef) -> GcRef {
    abi_guard!("praxis_int_lcm", ctx, {
        let a = unsafe { int_payload(lhs) };
        let b = unsafe { int_payload(rhs) };
        // `lcm(n, 0)` is 0 for every n: 0 is a multiple of everything, and dividing
        // by the gcd below would divide by zero when both are 0.
        if a == 0 || b == 0 {
            return unsafe { int_ref(ctx, 0i64) };
        }
        // |a / gcd * b| in i128, which cannot overflow: both operands fit i64, so
        // the product fits i128 with room to spare. The range check is the only
        // thing that can refuse.
        let result = checked_gcd(a, b)
            .map(|g| ((a as i128) / (g as i128) * (b as i128)).abs())
            .and_then(|m| i64::try_from(m).ok());
        match result {
            Some(result) => unsafe { int_ref(ctx, result) },
            None => {
                unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

// ---------------------------------------------------------------------------
// Comparisons (yield a Bool GcRef).
// ---------------------------------------------------------------------------

macro_rules! int_cmp {
    ($name:ident, $op:tt) => {
        #[doc = concat!(" `Int ", stringify!($op), "` comparison; returns a Bool GcRef (§4.12).")]
        ///
        /// # Safety
        /// `ctx` must be live and wired; both operands must be valid `Int` `GcRef`s.
        #[unsafe(no_mangle)]
        pub unsafe extern "C" fn $name(
            ctx: *mut RuntimeContext,
            lhs: GcRef,
            rhs: GcRef,
        ) -> GcRef {
            abi_guard!(stringify!($name), ctx, {
                let a = unsafe { int_payload(lhs) };
                let b = unsafe { int_payload(rhs) };
                let result = a $op b;
                // SAFETY: ctx/heap valid; Bool immortal path.
                unsafe { bool_ref(ctx, result) }
            })
        }
    };
}

int_cmp!(praxis_int_eq, ==);
int_cmp!(praxis_int_ne, !=);
int_cmp!(praxis_int_lt, <);
int_cmp!(praxis_int_gt, >);
int_cmp!(praxis_int_le, <=);
int_cmp!(praxis_int_ge, >=);

// ---------------------------------------------------------------------------
// Fault check.
// ---------------------------------------------------------------------------

/// Return 1 if a fault is pending on `ctx`, else 0 (§10.4).
///
/// **Generated code does not call this.** An `Inst::CheckFault` is a load of
/// `ctx.pending_fault`, a load of the kind at
/// [`Fault::KIND_OFFSET`](crate::Fault::KIND_OFFSET) and a `brif` (ADR-102) —
/// the same question, without the call, the `catch_unwind` region and the
/// `Result` discriminant, on a path that runs once per faultable instruction.
///
/// The wrapper stays: it is the named ABI entry point for a host asking the
/// question from Rust (the JIT test harness does), it keeps its manifest row and
/// its address-table arm so `RuntimeSymbol` stays a bijection onto real
/// addresses, and deleting it would churn ADR-080's source-reading test for no
/// gain. Its two null tests are the difference between it and the inline form,
/// and they are why *this* is what a host with a possibly-unwired context calls.
///
/// # Safety
/// `ctx` must point at a live `RuntimeContext` (a null/unwired context reports
/// no fault rather than panicking).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_check_fault(ctx: *mut RuntimeContext) -> i64 {
    abi_guard!("praxis_check_fault", ctx, {
        if ctx.is_null() {
            return 0;
        }
        if let Some(fault) = unsafe { (*ctx).pending_fault.as_ref() } {
            return fault.is_pending().into();
        }
        0
    })
}

/// Stop the program at a `:bp` marker and show the host its frame chain (§9.8).
///
/// `span_start`/`span_end` are the marker's own source span, passed as
/// immediates: this is a call with no operands from the program, and boxing a
/// span so it could ride a `GcRef` argument would put an allocation at the one
/// site whose cost has to stay a single call.
///
/// Everything that makes a stop *not* a fault lives in
/// [`crate::breakpoint::stop`]: the host handler is given a snapshot and no
/// context, so it cannot allocate, cannot collect and cannot raise. That is what
/// lets this be declared [`Effect::Pure`](praxis_stdlib::abi::Effect::Pure), and
/// therefore what lets generated code emit no root spill before it and no fault
/// check after.
///
/// A program with no handler installed — every JIT test, every embedder that
/// wants none — finds nothing to call and returns.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext` whose claimed debug frames
/// satisfy `copy_stack`'s contract, which every generated prologue establishes.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_breakpoint(
    ctx: *mut RuntimeContext,
    span_start: u32,
    span_end: u32,
) {
    abi_guard!("praxis_breakpoint", ctx, {
        if ctx.is_null() {
            return;
        }
        // SAFETY: `ctx` is non-null and the caller guarantees it is live and
        // wired; the debug frames are the ones its prologue chain claimed.
        unsafe { crate::breakpoint::stop(ctx, (span_start, span_end)) };
    })
}

/// Raise a [`FaultKind::StackOverflow`] fault on `ctx` (§9.2, §17.4). Called by
/// the generated prologue guard when `ctx.stack_left` is less than this frame's
/// [`frame_cost`](crate::frame_cost), so the host survives deep recursion
/// instead of overflowing the native stack. The prologue then unwinds to its
/// fault epilogue (pop frame + return Unit) — same path as any other fault.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_raise_stack_overflow(ctx: *mut RuntimeContext) {
    abi_guard!("praxis_raise_stack_overflow", ctx, {
        unsafe { set_fault(ctx, RaisedFault::STACK_OVERFLOW) };
    })
}

/// Raise a [`FaultKind::EmptyCollection`] fault on `ctx` (§9.2).
///
/// `reduce`, `min_by` and `max_by` have no answer for an empty sequence: they
/// seed their accumulator from the first element, and there is no first
/// element. Handing back an unwritten accumulator slot would materialize
/// whatever the register held as a `GcRef` that is `NonNull` by type and
/// arbitrary in fact; this is the defined failure instead, and a fault is what
/// the other empty-collection accessors (`Deque.pop_front`, heap `pop`/`peek`)
/// already raise for the same reason.
///
/// Unconditional, unlike the two arithmetic raise wrappers: the emptiness test
/// is a branch generated code has to make anyway (the seen-flag gates the whole
/// sink), so there is no predicate worth passing. It returns the Unit sentinel
/// rather than nothing, so the MIR `Call` that emits it has an ordinary `Gc`
/// destination — a `Void` row would put the context pointer in a rootable slot.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_raise_empty_collection(ctx: *mut RuntimeContext) -> GcRef {
    abi_guard!("praxis_raise_empty_collection", ctx, {
        unsafe { set_fault(ctx, RaisedFault::EMPTY_COLLECTION) };
        unsafe { unit_sentinel(ctx) }
    })
}

/// Raise a [`FaultKind::IntOverflow`] fault on `ctx` iff `condition` is
/// non-zero (§4.12).
///
/// Generated code lowers `Int` arithmetic natively — `iadd`/`isub`/`imul` on
/// the raw scalar channel — and computes the overflow predicate inline. This is
/// how it reports one. It allocates nothing, so an arithmetic site is not a GC
/// safepoint and spills no roots.
///
/// **The call site branches; this is the cold path.** Calling unconditionally
/// and letting `condition` decide would keep arithmetic to a single basic
/// block, but a branch does not clobber registers and a call does, so it would
/// force a spill and reload of every live value around an arithmetic op that
/// never faults. The site is a `brif` to a cold block (ADR-102);
/// `raise_on_cold_path` in the backend carries the full argument.
///
/// The cold block passes a constant `1` — honest, since it is reached only when
/// the predicate held, and it keeps the test below a true statement rather than
/// dead code.
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_raise_int_overflow_if(ctx: *mut RuntimeContext, condition: i64) {
    abi_guard!("praxis_raise_int_overflow_if", ctx, {
        if condition != 0 {
            unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
        }
    })
}

/// Raise a [`FaultKind::DivByZero`] fault on `ctx` iff `condition` is non-zero
/// (§4.12). The division counterpart of [`praxis_raise_int_overflow_if`].
///
/// # Safety
/// `ctx` must point at a live, wired `RuntimeContext`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_raise_div_by_zero_if(ctx: *mut RuntimeContext, condition: i64) {
    abi_guard!("praxis_raise_div_by_zero_if", ctx, {
        if condition != 0 {
            unsafe { set_fault(ctx, RaisedFault::DIV_BY_ZERO) };
        }
    })
}

// ---------------------------------------------------------------------------
// Collection payload accessors (§11.1, §11.4).
//
// Every collection below reaches its payload through a named accessor —
// `vec_payload`, `map_payload_mut`, … — and the name is the descriptor
// assertion the wrapper is making, which is why the nine kinds keep nine
// (shared, mut) pairs rather than calling `payload::<P>()` inline. The two
// casts underneath live here once, and each named accessor is the one line
// that spells its payload type.
// ---------------------------------------------------------------------------

/// Read a `P` payload out of a `GcRef` as a shared ref.
///
/// # Safety
/// `r` must be a valid `GcRef` whose descriptor's payload type is `P`.
unsafe fn payload_ref<P>(r: GcRef) -> &'static P {
    // SAFETY: caller guarantees `r`'s payload is a `P`; the non-moving GC
    // (ADR-011) keeps the payload address stable for the object's lifetime. The
    // `'static` is unbounded because the raw FFI boundary has no lifetime to
    // carry; the caller (a wrapper that holds `ctx`) ensures the object outlives
    // the use.
    unsafe { &*r.payload::<P>() }
}

/// Read a `P` payload out of a rooted `GcRef` as a mutable ref — the accessor
/// the wrappers that mutate in place go through (§11.1).
///
/// # Safety
/// `r` must be a valid `GcRef` whose descriptor's payload type is `P`, rooted
/// for `'s`.
unsafe fn payload_mut<'s, P>(r: Rooted<'s>) -> &'s mut P {
    // SAFETY: caller guarantees `r`'s payload is a `P`; the non-moving GC
    // (ADR-011) keeps the payload address stable for the object's lifetime, and
    // `Rooted` proves the object is in the collector's root set for `'s`, so a
    // collection triggered while this reference is held cannot reclaim what it
    // points at.
    unsafe { &mut *r.get().payload::<P>() }
}

// ---------------------------------------------------------------------------
// Vec[T] collection methods (§11.1, §11.2, §11.5).
//
// `VecPayload` stores a growable [`ReprCVec<GcRef>`](crate::ReprCVec), so
// `push` mutates the existing payload in place and the receiver's `GcRef` stays
// valid across it. Per §11.5 reallocation safety, no interior pointer into that
// buffer is retained across a capacity-mutating op.
// ---------------------------------------------------------------------------

/// Read the `VecPayload` out of a `GcRef` as a shared ref, asserting it is a Vec.
///
/// # Safety
/// `r` must be a valid `Vec` `GcRef`.
unsafe fn vec_payload(r: GcRef) -> &'static VecPayload {
    // SAFETY: caller guarantees `r` is a Vec; see `payload_ref`.
    unsafe { payload_ref::<VecPayload>(r) }
}

/// Read the `VecPayload` out of a `GcRef` as a mutable ref, asserting it is a
/// Vec. Used by `push` to mutate the vector in place (§11.1).
///
/// # Safety
/// `r` must be a valid `Vec` `GcRef`, rooted for `'s`.
unsafe fn vec_payload_mut<'s>(r: Rooted<'s>) -> &'s mut VecPayload {
    // SAFETY: caller guarantees `r` is a Vec; see `payload_mut`.
    unsafe { payload_mut::<VecPayload>(r) }
}

/// Build a `Vec[T]` holding `items`, with `element_descriptor` as its element
/// type — the shape every wrapper that answers with a collection needs.
///
/// The `Vec` is rooted across the pushes, which is the part worth having in one
/// place: `praxis_vec_new` allocates, and so may the caller's own iteration, so a
/// collection between the allocation and the last push would reclaim it.
///
/// `element_descriptor` may be **null**: the source collection's label is what
/// its own construction site knew, and that may have been nothing. A
/// `Vec`'s null means "empty" — `vec_format` reads it that way — so a null label
/// with members present would answer `[]`. The first member's own descriptor is
/// what the `Vec` adopts instead, which is exactly what `praxis_vec_push` does.
///
/// # Safety
/// `ctx` must be live and wired; `element_descriptor` must be a valid
/// `'static TypeDescriptor` or null; every item must be a valid `GcRef` whose
/// payload matches its own header.
unsafe fn vec_of(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
    items: impl Iterator<Item = GcRef>,
) -> GcRef {
    let items: Vec<GcRef> = items.collect();
    let element_descriptor = if element_descriptor.is_null() {
        items
            .first()
            .map_or(std::ptr::null(), |first| first.descriptor() as *const _)
    } else {
        element_descriptor
    };
    let result = unsafe { praxis_vec_new(ctx, element_descriptor) };
    let scope = unsafe { NativeScope::new(ctx) };
    let rp = unsafe { vec_payload_mut(scope.root(result)) };
    rp.items.extend(items);
    result
}

/// Allocate a new empty `Vec[T]` with the given element descriptor (§11.2).
/// Returns a `GcRef` to a zero-length vector.
///
/// # Safety
/// `ctx` must be live and wired. `element_descriptor` must be a valid pointer to
/// a `'static TypeDescriptor`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_new(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
) -> GcRef {
    abi_guard!("praxis_vec_new", ctx, {
        // A null descriptor is kept null: it means "the caller has no static
        // element type", which is a thing this payload can hold. Spelling it
        // `INT` instead would make an empty `Vec[Float]` claim to hold `Int`s.
        // SAFETY: VecPayload is VEC's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::collections::VEC, || VecPayload {
                element_descriptor,
                items: ReprCVec::new(),
            })
        }
    })
}

/// Allocate a `Vec[T]` of `count` slots, every one holding `fill` (ADR-146's
/// `Vec(n, fill)`).
///
/// Faults `InvalidSize` if `count` is negative or exceeds
/// [`VecExtent::MAX_ITEMS`](crate::collections::VecExtent::MAX_ITEMS): the count
/// arrives from source and would otherwise become a `usize` cast, where a
/// negative value lands near `usize::MAX` (ADR-041 decision 1).
///
/// Faults `TypeMismatch` if the caller declared an element type that the fill is
/// not, through the same [`adopt_or_reject`] `push` uses — a `Vec[Int]` filled
/// with a `Float` is a mislabelled element descriptor, and every later
/// `equals`/`hash`/`format` would read the payloads as the wrong type. A null
/// static descriptor adopts the fill's, which is what "the caller has no static
/// element type" already means here.
///
/// **`fill` is stored `count` times, not copied `count` times.** Every slot is
/// the same `GcRef`, so `Vec(3, Vec())` is three names for one inner `Vec`.
/// That is the language's existing reference semantics — `outer.push(a)` twice
/// aliases too — stated at a new site rather than a new rule (ADR-146 decision
/// 4).
///
/// `count` arrives boxed rather than as a `RawI64` like [`praxis_grid_new`]'s
/// extents: MIR lowers an argument expression to a `Gc` local, and unboxing it
/// there would cost an `ExtractScalar` and a second shape in the codegen's
/// allocation arm. `praxis_grid_new`'s two are `iconst` immediates with no local
/// to unbox, which is why the two wrappers differ.
///
/// # Safety
/// `ctx` must be live and wired; `element_descriptor` must be a valid pointer to
/// a `'static TypeDescriptor` (or null); `count` must be a valid `Int` `GcRef`;
/// `fill` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_filled(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
    count: GcRef,
    fill: GcRef,
) -> GcRef {
    abi_guard!("praxis_vec_filled", ctx, {
        // SAFETY: caller guarantees `count` is a valid Int.
        let n = unsafe { int_payload(count) };
        let Some(extent) = crate::collections::VecExtent::new(n) else {
            unsafe { set_fault(ctx, RaisedFault::INVALID_SIZE) };
            return unsafe { unit_sentinel(ctx) };
        };
        let mut descriptor = element_descriptor;
        if !unsafe { adopt_or_reject(ctx, &mut descriptor, fill) } {
            return unsafe { unit_sentinel(ctx) };
        }
        // `fill` is a bare `GcRef` argument, and `gc_alloc_owned` may collect.
        // Rooting it in a native scope is what keeps it addressable across the
        // allocation — the caller's shadow frame roots it up to the call, and
        // this roots it through it.
        let scope = unsafe { NativeScope::new(ctx) };
        let fill = scope.root(fill).get();
        // The items are built inside the initializer, which `gc_alloc_owned`
        // runs *after* the safepoint: no untraced `Vec<GcRef>` is ever live
        // across a collection.
        // SAFETY: VecPayload is VEC's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::collections::VEC, || VecPayload {
                element_descriptor: descriptor,
                items: ReprCVec::from_vec(vec![fill; extent.len()]),
            })
        }
    })
}

/// Allocate a nominal record (§4.5) with all fields initialized to Unit.
/// The `schema_ptr` points at a `'static RecordSchema` (built and leaked by the
/// codegen from the record def). Fields are filled in declaration order via
/// [`praxis_record_set_field`] after allocation. Returns the record `GcRef`.
///
/// # Safety
/// `ctx` must be live and wired; `schema_ptr` must be a valid `'static` pointer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_record(
    ctx: *mut RuntimeContext,
    schema_ptr: *const crate::records::RecordSchema,
) -> GcRef {
    abi_guard!("praxis_alloc_record", ctx, {
        if schema_ptr.is_null() {
            // SAFETY: `ctx` is the wrapper's own context argument, whose validity the
            // caller already guarantees.
            return unsafe { unit_sentinel(ctx) };
        }
        // SAFETY: caller guarantees schema_ptr is a valid 'static pointer.
        let schema = unsafe { &*schema_ptr };
        let arity = schema.fields.len();
        // SAFETY: `ctx` is the wrapper's own context argument, whose validity the
        // caller already guarantees.
        let unit = unsafe { unit_sentinel(ctx) };
        // SAFETY: RecordPayload is RECORD's payload type.
        // Every field slot starts as Unit (a valid GcRef), keeping the GC sound
        // before the caller fills them in via praxis_record_set_field.
        unsafe {
            gc_alloc_owned(ctx, &crate::records::RECORD, || {
                crate::records::RecordPayload {
                    schema: schema_ptr,
                    items: vec![unit; arity],
                }
            })
        }
    })
}

/// Set field `idx` of `record` to `value` (§4.5). Used by the codegen to
/// fill in fields after [`praxis_alloc_record`]. Returns the record (the
/// receiver is mutated in place).
///
/// # Safety
/// `ctx` must be live; `record` must be a valid record `GcRef`; `idx` must be
/// in bounds.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_record_set_field(
    ctx: *mut RuntimeContext,
    record: GcRef,
    idx: u32,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_record_set_field", ctx, {
        let _ = ctx;
        // SAFETY: caller guarantees record is a valid record GcRef.
        let payload = record.payload::<u8>() as *mut crate::records::RecordPayload;
        // SAFETY: the payload is a RecordPayload for any RECORD-descriptor object.
        let rp = unsafe { &mut *payload };
        if let Some(slot) = rp.items.get_mut(idx as usize) {
            *slot = value;
        }
        record
    })
}

/// Read field `idx` out of a record `GcRef` (§4.5). Returns the field's
/// `GcRef` value. Returns Unit if the record is malformed or the index is out
/// of bounds (defensive; the type checker prevents this in well-typed code).
///
/// # Safety
/// `ctx` must be live; `record` must be a valid record `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_record_field(
    ctx: *mut RuntimeContext,
    record: GcRef,
    idx: u32,
) -> GcRef {
    abi_guard!("praxis_record_field", ctx, {
        // SAFETY: caller guarantees record is a valid record GcRef; the payload is
        // a RecordPayload for any RECORD-descriptor object.
        let payload = record.payload::<u8>() as *const crate::records::RecordPayload;
        let rp = unsafe { &*payload };
        rp.items
            .get(idx as usize)
            .copied()
            .unwrap_or_else(|| unsafe { unit_sentinel(ctx) })
    })
}

/// Allocate an enum value (§4.6) of the type `schema_ptr` describes, with
/// variant `tag` and every payload slot initialized to Unit. Payload values are
/// filled via [`praxis_enum_set_payload`] after allocation. Returns the enum
/// `GcRef`.
///
/// The arity is **read from the schema** rather than passed alongside it, as
/// [`praxis_alloc_tuple`] already does: a schema and an arity that disagree is
/// a state no caller can now reach. A null schema, or a tag the schema has no
/// variant for, allocates nothing and answers the Unit sentinel — the same
/// answer `praxis_alloc_tuple` gives a null schema.
///
/// # Safety
/// `ctx` must be live and wired; `schema_ptr` must be null or a valid
/// `'static` pointer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_enum(
    ctx: *mut RuntimeContext,
    schema_ptr: *const crate::enums::EnumSchema,
    tag: i64,
) -> GcRef {
    abi_guard!("praxis_alloc_enum", ctx, {
        if schema_ptr.is_null() || tag < 0 {
            return unsafe { unit_sentinel(ctx) };
        }
        // SAFETY: caller guarantees schema_ptr is a valid 'static pointer.
        let schema = unsafe { &*schema_ptr };
        if schema.variant_at(tag as usize).is_none() {
            return unsafe { unit_sentinel(ctx) };
        }
        let arity = schema.arity_of(tag as usize);
        let unit = unsafe { unit_sentinel(ctx) };
        let items = vec![unit; arity];
        // SAFETY: EnumPayload is ENUM's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::enums::ENUM, || crate::enums::EnumPayload {
                schema: schema_ptr,
                tag: tag as u32,
                items,
            })
        }
    })
}

/// Allocate `Some(value)` under the runtime's own [`option_schema`].
///
/// `value` is rooted across the enum allocation: the allocation is a safepoint,
/// and a bare `GcRef` argument is not in anyone's root set.
///
/// [`option_schema`]: crate::enums::option_schema
///
/// # Safety
/// `ctx` must be live and wired; `value` must be a valid `GcRef`.
pub(crate) unsafe fn option_some(ctx: *mut RuntimeContext, value: GcRef) -> GcRef {
    // SAFETY: the caller upholds ctx/value validity.
    unsafe {
        let scope = NativeScope::new(ctx);
        let rooted = scope.root(value);
        let some = praxis_alloc_enum(
            ctx,
            crate::enums::option_schema(),
            crate::enums::OPTION_SOME_TAG,
        );
        praxis_enum_set_payload(ctx, some, 0, rooted.get());
        some
    }
}

/// Allocate `None` under the runtime's own `option_schema`.
///
/// # Safety
/// `ctx` must be live and wired.
pub(crate) unsafe fn option_none(ctx: *mut RuntimeContext) -> GcRef {
    // SAFETY: the caller upholds ctx validity.
    unsafe {
        praxis_alloc_enum(
            ctx,
            crate::enums::option_schema(),
            crate::enums::OPTION_NONE_TAG,
        )
    }
}

/// Set payload slot `idx` of `enum_value` to `value` (§4.6). Returns the
/// enum value (mutated in place).
///
/// # Safety
/// `ctx` must be live; `enum_value` must be a valid enum `GcRef`; `idx` in bounds.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_enum_set_payload(
    ctx: *mut RuntimeContext,
    enum_value: GcRef,
    idx: i64,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_enum_set_payload", ctx, {
        let _ = ctx;
        // SAFETY: caller guarantees enum_value is a valid enum GcRef.
        let payload = enum_value.payload::<u8>() as *mut crate::enums::EnumPayload;
        let ep = unsafe { &mut *payload };
        if let Some(slot) = ep.items.get_mut(idx as usize) {
            *slot = value;
        }
        enum_value
    })
}

/// Read the variant tag (discriminant) of an enum value (§4.6). Returns the
/// tag as a boxed `Int` `GcRef` (the uniform ABI convention), so the `match`
/// lowering can extract the scalar and compare. Used by `match` to branch.
///
/// # Safety
/// `ctx` must be live; `enum_value` must be a valid enum `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_enum_tag(ctx: *mut RuntimeContext, enum_value: GcRef) -> GcRef {
    abi_guard!("praxis_enum_tag", ctx, {
        // SAFETY: caller guarantees enum_value is a valid enum GcRef.
        // Read the tag BEFORE allocating — the alloc below may trigger GC, and
        // enum_value is not explicitly rooted (it's only in a Cranelift local).
        let payload = enum_value.payload::<u8>() as *const crate::enums::EnumPayload;
        let tag = unsafe { (*payload).tag as i64 };
        // SAFETY: alloc boxes the i64 into a fresh Int object. The tag value is
        // already in a register, so GC collecting enum_value here is safe.
        unsafe { int_ref(ctx, tag) }
    })
}

/// Read payload slot `idx` of an enum value (§4.6). Returns the slot's
/// `GcRef`. Used by `match` to bind variant payload variables.
///
/// # Safety
/// `ctx` must be live; `enum_value` must be a valid enum `GcRef`; `idx` in bounds.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_enum_payload(
    ctx: *mut RuntimeContext,
    enum_value: GcRef,
    idx: i64,
) -> GcRef {
    abi_guard!("praxis_enum_payload", ctx, {
        // SAFETY: caller guarantees enum_value is a valid enum GcRef.
        let payload = enum_value.payload::<u8>() as *const crate::enums::EnumPayload;
        let ep = unsafe { &*payload };
        ep.items
            .get(idx as usize)
            .copied()
            .unwrap_or_else(|| unsafe { unit_sentinel(ctx) })
    })
}

/// Allocate a tuple (§4.5 structural tuples) with all element slots
/// initialized to Unit. The `schema_ptr` points at a `'static TupleSchema`
/// (built and leaked by the codegen from the tuple's element-type sequence).
/// Elements are filled in positional order via [`praxis_tuple_set`] after
/// allocation. Returns the tuple `GcRef`.
///
/// # Safety
/// `ctx` must be live and wired; `schema_ptr` must be a valid `'static` pointer.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_tuple(
    ctx: *mut RuntimeContext,
    schema_ptr: *const crate::tuples::TupleSchema,
) -> GcRef {
    abi_guard!("praxis_alloc_tuple", ctx, {
        if schema_ptr.is_null() {
            // SAFETY: `ctx` is the wrapper's own context argument, whose validity the
            // caller already guarantees.
            return unsafe { unit_sentinel(ctx) };
        }
        // SAFETY: caller guarantees schema_ptr is a valid 'static pointer.
        let schema = unsafe { &*schema_ptr };
        let arity = schema.descriptors.len();
        // SAFETY: `ctx` is the wrapper's own context argument, whose validity the
        // caller already guarantees.
        let unit = unsafe { unit_sentinel(ctx) };
        // SAFETY: TuplePayload is TUPLE's payload type.
        // Every element slot starts as Unit (a valid GcRef), keeping the GC sound
        // before the caller fills them in via praxis_tuple_set.
        unsafe {
            gc_alloc_owned(ctx, &crate::tuples::TUPLE, || crate::tuples::TuplePayload {
                schema: schema_ptr,
                items: vec![unit; arity],
            })
        }
    })
}

/// Set element `idx` of `tuple` to `value` (§4.5). Used by the codegen to
/// fill in elements after [`praxis_alloc_tuple`]. Returns the tuple (the
/// receiver is mutated in place).
///
/// # Safety
/// `ctx` must be live; `tuple` must be a valid tuple `GcRef`; `idx` in bounds.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_tuple_set(
    ctx: *mut RuntimeContext,
    tuple: GcRef,
    idx: i64,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_tuple_set", ctx, {
        let _ = ctx;
        // SAFETY: caller guarantees tuple is a valid tuple GcRef.
        let payload = tuple.payload::<u8>() as *mut crate::tuples::TuplePayload;
        // SAFETY: the payload is a TuplePayload for any TUPLE-descriptor object.
        let tp = unsafe { &mut *payload };
        if let Some(slot) = tp.items.get_mut(idx as usize) {
            *slot = value;
        }
        tuple
    })
}

/// Read element `idx` out of a tuple `GcRef` (§4.5). Returns the element's
/// `GcRef` value. Returns Unit if the tuple is malformed or the index is out of
/// bounds (defensive; the type checker prevents this in well-typed code).
///
/// # Safety
/// `ctx` must be live; `tuple` must be a valid tuple `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_tuple_get(
    ctx: *mut RuntimeContext,
    tuple: GcRef,
    idx: i64,
) -> GcRef {
    abi_guard!("praxis_tuple_get", ctx, {
        // SAFETY: caller guarantees tuple is a valid tuple GcRef; the payload is a
        // TuplePayload for any TUPLE-descriptor object.
        let payload = tuple.payload::<u8>() as *const crate::tuples::TuplePayload;
        let tp = unsafe { &*payload };
        tp.items
            .get(idx as usize)
            .copied()
            .unwrap_or_else(|| unsafe { unit_sentinel(ctx) })
    })
}

/// Structural equality between two GC values (§5.5). Reads the descriptor
/// from `a` and dispatches to its `equals` callback, which recurses element/field
/// wise for composite types (records, tuples, enums, collections). Returns 1 for
/// equal, 0 for not equal. Returns 0 if `a`'s type is not equatable (functions
/// are never equatable, §5.5) — the type checker rejects this in well-typed code,
/// so this is defensive.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `GcRef`s of the same
/// type (the caller has already unified their types at compile time).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_struct_eq(ctx: *mut RuntimeContext, a: GcRef, b: GcRef) -> i64 {
    abi_guard!("praxis_struct_eq", ctx, {
        let _ = ctx;
        // SAFETY: caller guarantees a is a valid GcRef; the descriptor header is
        // always present and its `equals` (if Some) is safe to call with a/b.
        let desc = a.descriptor();
        // Both operands must be the same runtime type before any callback runs
        // (ADR-045 decision 3). Well-typed code has unified them, so this is the
        // miscompile case — and a callback dispatched on a foreign layout is how a
        // type confusion becomes a wild read rather than a wrong answer.
        if !std::ptr::eq(desc, b.descriptor()) {
            return 0;
        }
        match desc.equals {
            // SAFETY: both a and b are values of desc's type (caller has type-checked
            // them equal); the equals callback is safe under that invariant.
            Some(eq) => {
                let pa = a.payload::<u8>() as *const u8;
                let pb = b.payload::<u8>() as *const u8;
                if unsafe { eq(pa, pb) } { 1 } else { 0 }
            }
            // Not equatable: treat as not-equal. The type checker rejects this in
            // well-typed code; the defensive default keeps runtime sound.
            None => 0,
        }
    })
}

/// Order two GC values through their descriptor's `compare` callback (ADR-045).
/// Returns `-1`, `0` or `1` — the caller turns that into the `<`/`<=`/`>`/`>=`
/// it wanted by comparing against zero.
///
/// This is the ordering counterpart of [`praxis_struct_eq`], and it exists for
/// the same reason: a `Text` is a pointer-and-length structure, so ordering one
/// by loading its first eight payload bytes would compare *addresses*.
///
/// Raises `FaultKind::TypeMismatch` and answers `0` when the two operands are
/// not the same runtime type, or when the type has no `compare`. The type
/// checker rejects both in well-typed code (`Y006`), so reaching either is a
/// compiler bug — reported as a fault rather than a callback dispatched on a
/// foreign layout.
///
/// The second guard is a weak backstop, and deliberately named as one: ADR-138
/// populated `compare` on every type a `Map` key can be, including tuples and
/// records, so a *miscompile* that lowered `(1, 2) < (1, 3)` to this wrapper
/// would be answered rather than faulted. `capability::supports_ord` refuses it
/// at `praxis check`, so no well-typed program reaches here either way.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_value_cmp(ctx: *mut RuntimeContext, a: GcRef, b: GcRef) -> i64 {
    abi_guard!("praxis_value_cmp", ctx, {
        // SAFETY: caller guarantees both are valid GcRefs; every object carries a
        // descriptor in its header.
        let desc = a.descriptor();
        if !std::ptr::eq(desc, b.descriptor()) {
            unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
            return 0;
        }
        let Some(compare) = desc.compare else {
            unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
            return 0;
        };
        // SAFETY: both values carry `desc` (checked above), so both payloads are
        // values of its type.
        let ordering = unsafe {
            compare(
                a.payload::<u8>() as *const u8,
                b.payload::<u8>() as *const u8,
            )
        };
        match ordering {
            std::cmp::Ordering::Less => -1,
            std::cmp::Ordering::Equal => 0,
            std::cmp::Ordering::Greater => 1,
        }
    })
}

/// Allocate a closure value (§4.10) with `fn_ptr` as its entry point and
/// `n_captures` environment slots initialized to Unit. Captures are filled via
/// [`praxis_closure_set_capture`] after allocation. Returns the closure `GcRef`.
///
/// # Safety
/// `ctx` must be live and wired; `fn_ptr` must be a valid JIT'd function pointer
/// whose calling convention matches `fn(ctx, params..., env...) -> i64`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_closure(
    ctx: *mut RuntimeContext,
    fn_ptr: *const u8,
    n_captures: i64,
) -> GcRef {
    abi_guard!("praxis_alloc_closure", ctx, {
        // SAFETY: `ctx` is the wrapper's own context argument, whose validity the
        // caller already guarantees.
        let unit = unsafe { unit_sentinel(ctx) };
        let env = vec![unit; n_captures as usize];
        // SAFETY: ClosurePayload is CLOSURE's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::closures::CLOSURE, || {
                crate::closures::ClosurePayload { fn_ptr, env }
            })
        }
    })
}

/// Set capture slot `idx` of `closure` to `value` (§4.10). Returns the
/// closure (mutated in place).
///
/// # Safety
/// `ctx` must be live; `closure` must be a valid closure `GcRef`; `idx` in bounds.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_closure_set_capture(
    ctx: *mut RuntimeContext,
    closure: GcRef,
    idx: i64,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_closure_set_capture", ctx, {
        let _ = ctx;
        // SAFETY: caller guarantees closure is a valid closure GcRef.
        let payload = closure.payload::<u8>() as *mut crate::closures::ClosurePayload;
        let cp = unsafe { &mut *payload };
        if let Some(slot) = cp.env.get_mut(idx as usize) {
            *slot = value;
        }
        closure
    })
}

/// Read the function pointer out of a closure `GcRef` (§4.10). Used by the
/// indirect-call lowering to obtain the entry point before a native call.
///
/// `ctx` is accepted (and unused) for ABI uniformity with every other `praxis_*`
/// wrapper — generated code calls all wrappers as `fn(ctx, args...)`, so this
/// keeps the calling convention consistent. The returned `*const u8` is carried
/// as an `i64` (pointer-sized) back into the JIT'd code.
///
/// # Safety
/// `ctx` must be live; `closure` must be a valid closure `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_closure_fn_ptr(
    ctx: *mut RuntimeContext,
    closure: GcRef,
) -> *const u8 {
    abi_guard!("praxis_closure_fn_ptr", ctx, {
        let _ = ctx;
        // SAFETY: caller guarantees closure is a valid closure GcRef.
        let payload = closure.payload::<u8>() as *const crate::closures::ClosurePayload;
        unsafe { (*payload).fn_ptr }
    })
}

/// Read capture slot `idx` out of a closure `GcRef` (§4.10). Used by the
/// closure's synthetic function to load its captured values from the env.
///
/// # Safety
/// `ctx` must be live; `closure` must be a valid closure `GcRef`; `idx` in bounds.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_closure_capture(
    ctx: *mut RuntimeContext,
    closure: GcRef,
    idx: i64,
) -> GcRef {
    abi_guard!("praxis_closure_capture", ctx, {
        // SAFETY: caller guarantees closure is a valid closure GcRef.
        let payload = closure.payload::<u8>() as *const crate::closures::ClosurePayload;
        let cp = unsafe { &*payload };
        cp.env
            .get(idx as usize)
            .copied()
            .unwrap_or_else(|| unsafe { unit_sentinel(ctx) })
    })
}

/// Allocate a `VarCell` holding `value` (§4.10). The cell is the shared
/// mutable storage for a captured `var` binding: the binding site and every
/// closure that captures the `var` refer to the same cell. Returns the cell
/// `GcRef`.
///
/// # Safety
/// `ctx` must be live and wired; `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_alloc_var_cell(ctx: *mut RuntimeContext, value: GcRef) -> GcRef {
    abi_guard!("praxis_alloc_var_cell", ctx, {
        // SAFETY: VarCellPayload is VAR_CELL's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::var_cell::VAR_CELL, || {
                crate::var_cell::VarCellPayload { value }
            })
        }
    })
}

/// Read the current value out of a `VarCell` (§4.10). Used by `Path`
/// reads of a captured `var` (the local holds the cell; this derefs it).
///
/// # Safety
/// `ctx` must be live; `cell` must be a valid `VarCell` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_var_cell_get(ctx: *mut RuntimeContext, cell: GcRef) -> GcRef {
    abi_guard!("praxis_var_cell_get", ctx, {
        let _ = ctx;
        // SAFETY: caller guarantees cell is a valid VarCell GcRef.
        let payload = cell.payload::<u8>() as *const crate::var_cell::VarCellPayload;
        unsafe { (*payload).value }
    })
}

/// Store `value` into a `VarCell` (§4.10). Used by `Assign` to a
/// captured `var`. Returns the cell (mutated in place).
///
/// # Safety
/// `ctx` must be live; `cell` must be a valid `VarCell` `GcRef`; `value` valid.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_var_cell_set(
    ctx: *mut RuntimeContext,
    cell: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_var_cell_set", ctx, {
        let _ = ctx;
        // SAFETY: caller guarantees cell is a valid VarCell GcRef.
        let payload = cell.payload::<u8>() as *mut crate::var_cell::VarCellPayload;
        unsafe {
            (*payload).value = value;
        }
        cell
    })
}

/// Append `value` to `vec` in place (§11.1). Returns the Unit sentinel — the
/// receiver is mutated directly, so the caller's `GcRef` remains valid (the
/// `VecPayload` object does not move; only its internal buffer may grow).
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`; `value`
/// must be a valid `GcRef` whose type matches the vector's element descriptor.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_push(
    ctx: *mut RuntimeContext,
    vec: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_vec_push", ctx, {
        // `push` may grow the Vec's backing buffer, which allocates Rust heap memory
        // (not GC memory). A GC collection during this would be safe (the vec
        // object is rooted by the caller's spilled `vec` local), but we trigger it
        // *before* the mutation to keep the rooting story simple: `value` is passed
        // by value and is not yet in the vec, so it must survive across this
        // collection via the caller's shadow frame.
        unsafe { maybe_collect(ctx) };
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { vec_payload_mut(scope.root(vec)) };
        // A vector that was never told its element type adopts the first pushed
        // value's — the `forall T. () -> Vec[T]` builtin leaves `T` generalized
        // until first use, so construction genuinely has nothing to record. A
        // vector that *was* told rejects a mismatch instead of retagging itself:
        // retagging would turn an explicitly typed `Vec[Int]` into a `Vec[Float]`
        // on one bad push, and every later `equals`/`hash`/`format` would then
        // read the remaining `Int` payloads as `f64`.
        if !unsafe { adopt_or_reject(ctx, &mut p.element_descriptor, value) } {
            return unsafe { unit_sentinel(ctx) };
        }
        // Charge the spine when it grows (ADR-121; see
        // `Heap::charge_owned_growth`). Measured either side of the mutation
        // through the payload's own `owned_bytes`, so the growth policy stays
        // `RawVec`'s and the size formula stays the descriptor's.
        let before = p.owned_bytes();
        p.items.push(value);
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// Reconcile a collection's element descriptor with a value about to be stored
/// in it: adopt the value's descriptor if the collection has none, accept if
/// they agree, and raise `TypeMismatch` if they do not.
///
/// Returns whether the store may proceed. Descriptors are `static`, so pointer
/// identity is the authoritative test (ADR-038).
///
/// # Safety
/// `ctx` must be live and wired; `value` must be a valid `GcRef`.
unsafe fn adopt_or_reject(
    ctx: *mut RuntimeContext,
    element_descriptor: &mut *const TypeDescriptor,
    value: GcRef,
) -> bool {
    let pushed = value.descriptor();
    if element_descriptor.is_null() {
        *element_descriptor = pushed;
        return true;
    }
    if std::ptr::eq(*element_descriptor, pushed) {
        return true;
    }
    unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
    false
}

/// The number of elements in `vec`, as a boxed `Int` (§11.1).
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_len(ctx: *mut RuntimeContext, vec: GcRef) -> GcRef {
    abi_guard!("praxis_vec_len", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        let len = p.items.len() as i64;
        // len allocates the returned Int, but the input vec is still live via `vec`.
        unsafe { int_ref(ctx, len) }
    })
}

/// The element at `index`, or an `IndexOutOfBounds` fault if out of range
/// (§9.2, §11.1). Returns the Unit sentinel on fault.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`; `index`
/// must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_get(
    ctx: *mut RuntimeContext,
    vec: GcRef,
    index: GcRef,
) -> GcRef {
    abi_guard!("praxis_vec_get", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        // SAFETY: caller guarantees `index` is a valid Int.
        let idx = unsafe { int_payload(index) };
        // SAFETY: `abi_guard!` established that `ctx` is live and wired.
        let Some(idx) = (unsafe { checked_index(ctx, idx, p.items.len()) }) else {
            return unsafe { unit_sentinel(ctx) };
        };
        // Return the element by value (a copy of the GcRef). No allocation, so no
        // collection is needed; the vec stays live via `vec`.
        p.items[idx]
    })
}

/// Replace the element at `index`; faults `IndexOutOfBounds` if out of range
/// (§9.2, §11.1). Returns the Unit sentinel.
///
/// **Replaces, and never appends.** `v[v.len()] = x` is out of range rather than
/// a push, which is `praxis_vec_push`'s job: a store whose index decides between
/// the two operations makes an off-by-one grow the vector instead of reporting.
///
/// The element descriptor goes through the same [`adopt_or_reject`] every push
/// does, so a store into a vector that was never told its element type adopts
/// the first value's, and one into a `Vec[Int]` raises `TypeMismatch` rather
/// than retagging the collection — `push`'s rule, at the second door.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`; `index`
/// must be a valid `Int` `GcRef`; `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_set(
    ctx: *mut RuntimeContext,
    vec: GcRef,
    index: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_vec_set", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { vec_payload_mut(scope.root(vec)) };
        // SAFETY: caller guarantees `index` is a valid Int.
        let idx = unsafe { int_payload(index) };
        // SAFETY: `abi_guard!` established that `ctx` is live and wired.
        let Some(idx) = (unsafe { checked_index(ctx, idx, p.items.len()) }) else {
            return unsafe { unit_sentinel(ctx) };
        };
        if !unsafe { adopt_or_reject(ctx, &mut p.element_descriptor, value) } {
            return unsafe { unit_sentinel(ctx) };
        }
        // No allocation: the slot takes a `GcRef` the caller already holds.
        p.items[idx] = value;
        unsafe { unit_sentinel(ctx) }
    })
}

/// True iff `vec` has no elements, as a boxed `Bool` (§11.1).
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_is_empty(ctx: *mut RuntimeContext, vec: GcRef) -> GcRef {
    abi_guard!("praxis_vec_is_empty", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        let empty = p.items.is_empty();
        // SAFETY: ctx/heap valid; Bool immortal path.
        unsafe { bool_ref(ctx, empty) }
    })
}

// --- the §6.3 barrier combinators ------------------------------------------
//
// A *barrier* is a pipeline stage that cannot be fused into the loop feeding it
// because it needs the whole sequence before it can answer its first element:
// `sorted` has to see the largest element before it knows the smallest is first.
// So each is a real runtime call over a materialized `Vec` rather than an
// intrinsic the MIR fuser expands, and the fuser's own recognizer already knows
// to end a chain at one and start a fresh chain from its result.
//
// All three rebuild through [`vec_of`] rather than mutating the receiver, which
// is the shape `praxis_set_items` and `praxis_counter_keys` already use.
// `v.sorted()` is an expression, not a statement: §6.3 lists it beside `map` and
// `filter`, and a caller that also holds `v` must still see `v`'s own order.

/// `v.sorted()` — the elements of `vec` in ascending order, as a **new** `Vec`
/// (§6.3). The receiver is not touched.
///
/// Ordering goes through the element descriptor's `compare` callback — the same
/// callback [`praxis_value_cmp`] uses, and for the same reason: a `Text` is a
/// pointer-and-length structure, so ordering one by its first eight payload
/// bytes compares *addresses*. That sorts `Vec[Int]` correctly and `Vec[Text]`
/// into allocation order, which is the failure that looks like it works.
///
/// The sort is **stable**, so equal elements keep their input order and the
/// answer is a function of the input alone.
///
/// Raises `FaultKind::TypeMismatch` and answers Unit when the elements are not
/// all one type, or when that type has no `compare`. The catalog row's `Ord`
/// bound (ADR-093, `Bound::Kind`) rejects both at `praxis check`, so reaching
/// either is a compiler bug — reported as a fault rather than as a callback
/// dispatched on a foreign layout. As [`praxis_value_cmp`], the second guard
/// covers fewer types since ADR-138 populated `compare` on the composites.
///
/// It is the callback a `Set` and a `Map` order their keys through too
/// (ADR-138), which is what makes `out(s)` and `out(s.sorted())` print one
/// sequence rather than one numeric and one lexicographic.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_sorted(ctx: *mut RuntimeContext, vec: GcRef) -> GcRef {
    abi_guard!("praxis_vec_sorted", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        let mut items: Vec<GcRef> = p.items.to_vec();
        // Nothing to order, and nothing to check: a zero- or one-element Vec has
        // no pair to compare, so an empty `Vec[fn(Int) -> Int]` sorts rather than
        // faulting on a `compare` it would never have called.
        if items.len() > 1 {
            // The elements' *own* descriptors decide, not the Vec's label: the
            // label may be null (the construction site knew no element type)
            // while every member is a perfectly good `Text`.
            let desc = items[0].descriptor();
            if !items.iter().all(|i| std::ptr::eq(i.descriptor(), desc)) {
                unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
                return unsafe { unit_sentinel(ctx) };
            }
            let Some(compare) = desc.compare else {
                unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
                return unsafe { unit_sentinel(ctx) };
            };
            items.sort_by(|a, b| {
                // SAFETY: every element carries `desc` (checked above), so both
                // payloads are values of its type; the non-moving GC keeps them
                // stable, and `sort_by` allocates nothing that could collect.
                unsafe {
                    compare(
                        a.payload::<u8>() as *const u8,
                        b.payload::<u8>() as *const u8,
                    )
                }
            });
        }
        unsafe { vec_of(ctx, p.element_descriptor, items.into_iter()) }
    })
}

/// `v.sorted_by_key(f)` — the elements of `vec` ordered by the key `f` extracts,
/// as a **new** `Vec` (§6.3, ADR-127 decision 5). The receiver is not touched.
///
/// # Why this row exists, and why it is not `sorted_by`
///
/// ADR-045 decided that no composite is orderable, so the moment a pipeline's
/// item is a pair — which is the moment its source is a `Map` or a `Counter` —
/// `sorted` is unavailable and "the five most common values" has no spelling.
/// The closure extracts an orderable key from an item that is not.
///
/// Not a `(T, T) -> Bool` comparator: `min_by`/`max_by` already own the
/// less-than-predicate shape, and a comparator is O(n log n) calls back into
/// JIT'd code where a key extractor is n.
///
/// **Decorate–sort–undecorate.** Every key is extracted once, up front, and the
/// sort orders the (key, element) pairs — which is what makes it n calls. The
/// keys are held in a `Vec<GcRef>` the collector cannot see, so they are rooted
/// in a native scope: extraction allocates, and a collection triggered by the
/// *next* call would otherwise free the key the previous one produced.
///
/// Ordering goes through the same `compare` callback [`praxis_value_cmp`] uses,
/// so the `Ord` bound the catalog row puts on the *key* is the rule this
/// enforces. The sort is **stable**, so items with equal keys keep their input
/// order and the answer is a function of the input alone.
///
/// Raises `FaultKind::TypeMismatch` and answers Unit when the keys are not all
/// one type, or when that type has no ordering; a fault the closure itself
/// raised stops the sort and is left for the call site's own check.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef` and `key`
/// a valid closure `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_sorted_by_key(
    ctx: *mut RuntimeContext,
    vec: GcRef,
    key: GcRef,
) -> GcRef {
    abi_guard!("praxis_vec_sorted_by_key", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        // The receiver is rooted explicitly: its items are read into a Rust
        // `Vec` and held across one closure call *per element*, which is a far
        // longer window than a single-allocation wrapper's.
        let _receiver = scope.root(vec);
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        let element_descriptor = p.element_descriptor;
        let items: Vec<GcRef> = p.items.to_vec();
        for item in &items {
            scope.root(*item);
        }
        // Decorate: one call per element, keys rooted as they arrive.
        let mut decorated: Vec<(GcRef, GcRef)> = Vec::with_capacity(items.len());
        for item in items {
            let Some(k) = (unsafe { call_unary_closure(ctx, key, item) }) else {
                // The closure faulted (or is not a closure, which the type
                // checker already refused). Its answer is the Unit sentinel, so
                // sorting on it would order garbage; stop and leave the fault
                // for the call site.
                return unsafe { unit_sentinel(ctx) };
            };
            decorated.push((scope.root(k).get(), item));
        }
        if decorated.len() > 1 {
            // The keys' *own* descriptors decide, as `praxis_vec_sorted`'s
            // elements' do: the source Vec's label says nothing about them.
            let desc = decorated[0].0.descriptor();
            if !decorated
                .iter()
                .all(|(k, _)| std::ptr::eq(k.descriptor(), desc))
            {
                unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
                return unsafe { unit_sentinel(ctx) };
            }
            let Some(compare) = desc.compare else {
                unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
                return unsafe { unit_sentinel(ctx) };
            };
            decorated.sort_by(|(a, _), (b, _)| {
                // SAFETY: every key carries `desc` (checked above), so both
                // payloads are values of its type; the non-moving GC keeps them
                // stable, and `sort_by` allocates nothing that could collect.
                unsafe {
                    compare(
                        a.payload::<u8>() as *const u8,
                        b.payload::<u8>() as *const u8,
                    )
                }
            });
        }
        // Undecorate.
        unsafe {
            vec_of(
                ctx,
                element_descriptor,
                decorated.into_iter().map(|(_, item)| item),
            )
        }
    })
}

/// Call a `(T) -> U` Praxis closure with one argument, or `None` if it faulted —
/// or if it is not a closure at all.
///
/// The descriptor is checked rather than assumed: the type checker says the
/// operand is a function and the only runtime representation of one is a closure
/// object, but the alternative to a `TypeMismatch` fault is transmuting whatever
/// the payload's first word happens to be into a function pointer and jumping to
/// it.
///
/// # Safety
/// `ctx` must be live and wired; `closure` and `arg` must be valid `GcRef`s.
unsafe fn call_unary_closure(
    ctx: *mut RuntimeContext,
    closure: GcRef,
    arg: GcRef,
) -> Option<GcRef> {
    if !std::ptr::eq(closure.descriptor(), &crate::closures::CLOSURE) {
        unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
        return None;
    }
    // SAFETY: the descriptor check proves the payload is a `ClosurePayload`, so
    // `fn_ptr` is the entry point the codegen wrote there.
    let fn_ptr = unsafe { (*closure.payload::<crate::closures::ClosurePayload>()).fn_ptr };
    // A closure's entry point is `fn(ctx, closure_self, params...) -> GcRef`
    // (§4.10, Approach B): the closure value itself is a hidden first argument,
    // and the prologue loads its captures from it.
    //
    // SAFETY: `fn_ptr` is a finalized JIT entry whose parameter count is the one
    // the type checker enforced for this operand; every value crossing is a
    // `GcRef`, which is the ABI's only value kind.
    let result = unsafe {
        let f: unsafe extern "C" fn(*mut RuntimeContext, GcRef, GcRef) -> GcRef =
            std::mem::transmute(fn_ptr);
        f(ctx, closure, arg)
    };
    // The closure ran arbitrary Praxis code and may have faulted; its result on
    // that path is the Unit sentinel.
    if unsafe { praxis_check_fault(ctx) } != 0 {
        return None;
    }
    Some(result)
}

/// `v.unique()` — the elements of `vec` with later duplicates dropped, as a
/// **new** `Vec`, in first-occurrence order (§6.3). The receiver is not touched.
///
/// First-occurrence order rather than sorted-and-deduped: `unique` is listed
/// separately from `sorted` in §6.3, so composing them has to be the user's
/// choice, and an order that depends on a hash map's iteration would make the
/// same program answer differently on two runs — and here the order is the
/// program's *answer*, not only its printing.
///
/// Sameness is [`DynamicKey`]'s — the descriptor's `hash` and `equals`
/// callbacks, which is what "the same value" means everywhere else in this
/// runtime (§5.5, §11.3). The catalog row's `HashStable` bound is what keeps a
/// mutable element out; a key that can change after it is stored cannot be found
/// again.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_unique(ctx: *mut RuntimeContext, vec: GcRef) -> GcRef {
    abi_guard!("praxis_vec_unique", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        let mut seen: std::collections::HashSet<DynamicKey> = std::collections::HashSet::new();
        let mut kept: Vec<GcRef> = Vec::new();
        for item in &p.items {
            if seen.insert(DynamicKey::new(*item)) {
                kept.push(*item);
            }
        }
        unsafe { vec_of(ctx, p.element_descriptor, kept.into_iter()) }
    })
}

/// `v.reversed()` — the elements of `vec` in the opposite order, as a **new**
/// `Vec` (ADR-145). The receiver is not touched.
///
/// A barrier for `praxis_vec_sorted`'s reason and not a fused stage: reversal
/// cannot answer its first element until it has seen the last one.
///
/// It reads **no descriptor callback** — not `compare`, not `equals`, not
/// `hash` — so unlike `sorted` and `unique` there is no element it can be handed
/// that it cannot reverse, and its catalog row carries no capability bound. That
/// is why the manifest row is `Allocates` and there is no `TypeMismatch` path
/// here to read.
///
/// The element label is copied through unchanged, the null a construction site
/// that knew no element type leaves included.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_reversed(ctx: *mut RuntimeContext, vec: GcRef) -> GcRef {
    abi_guard!("praxis_vec_reversed", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        let items: Vec<GcRef> = p.items.iter().rev().copied().collect();
        unsafe { vec_of(ctx, p.element_descriptor, items.into_iter()) }
    })
}

/// The group size `chunks(n)` and `windows(n)` share, or `None` when `n` names no
/// group at all (ADR-149).
///
/// **The only thing either wrapper refuses.** A run of zero elements is not a
/// short run — chunking a non-empty sequence into them has no finite answer, and
/// sliding one along it has no useful one — and a negative run names nothing.
/// Every other `n` has an answer, including one larger than the receiver: a
/// `chunks` wider than the sequence is one short chunk, a `windows` wider than it
/// is no windows. So this returns an `Option` of a size rather than clamping to
/// one, and the two callers spell those two answers themselves.
///
/// There is no upper bound here and none is missing. Both results are *shorter*
/// than the receiver — one group per start position at most — so neither can
/// ask for an extent [`VecExtent`](crate::collections::VecExtent) would refuse,
/// which is the bound `praxis_vec_filled` needs and these do not.
fn group_size(n: i64) -> Option<usize> {
    if n <= 0 {
        return None;
    }
    usize::try_from(n).ok()
}

/// The `Vec[Vec[T]]` both groupings answer, built from the half-open source
/// ranges `groups` names (ADR-149).
///
/// **The outer label is `collections::VEC` at every length, and it is *passed*
/// rather than inferred** (ADR-149 decision 1). Which label belongs there is not
/// this wrapper's choice — `outer.push(inner)` builds a `Vec[Vec[T]]` today and
/// `adopt_or_reject` labels it `VEC`, so anything else would disagree with
/// `push`. What is chosen here is only that it is written down: letting
/// [`vec_of`] infer it from the first group would answer `VEC` for
/// `[1].chunks(2)` and *null* for `[].chunks(2)` — one type with two labels, and
/// the null is the one `vec_format` renders as `[]`.
///
/// That is [`praxis_grid_positions`]'s argument, not a new one: it passes
/// `&tuples::TUPLE` for the same reason, and `Grid(0, 0, 1).positions()` is the
/// same empty case. Naming the label is what a wrapper does whenever its result's
/// element kind is not its receiver's.
///
/// The inner labels *are* the receiver's own, passed through unchanged the way
/// `praxis_vec_reversed` passes its one through, null included.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`; every
/// range `groups` yields must lie within its length.
unsafe fn vec_of_groups(
    ctx: *mut RuntimeContext,
    vec: GcRef,
    groups: impl Iterator<Item = (usize, usize)>,
) -> GcRef {
    // SAFETY: caller guarantees `vec` is a valid Vec.
    let element_descriptor = unsafe { vec_payload(vec) }.element_descriptor;
    let outer = unsafe { praxis_vec_new(ctx, &crate::collections::VEC as *const _) };
    let scope = unsafe { NativeScope::new(ctx) };
    let op = unsafe { vec_payload_mut(scope.root(outer)) };
    for (start, end) in groups {
        // The elements are read out of the receiver, which the caller's shadow
        // frame roots across this call, so the untraced `Vec<GcRef>` below holds
        // nothing a collection inside `vec_of` could reclaim — the receiver
        // holds every one of them too. That is `praxis_vec_unique`'s argument at
        // a second site, and it is why the *groups* are what need rooting and
        // the items are not: an inner `Vec` is reachable from nothing until it
        // is pushed, which is why it is pushed before the next one is built.
        //
        // SAFETY: caller guarantees `vec` is a valid Vec and that `start..end`
        // lies within its length.
        let items: Vec<GcRef> = unsafe { vec_payload(vec) }.items[start..end].to_vec();
        let inner = unsafe { vec_of(ctx, element_descriptor, items.into_iter()) };
        op.items.push(inner);
    }
    outer
}

/// `seq.chunks(n)` — these elements in consecutive non-overlapping runs of `n`,
/// the last short if the length does not divide (ADR-149). The receiver is not
/// touched.
///
/// `[1, 2, 3, 4, 5].chunks(2)` is `[[1, 2], [3, 4], [5]]`. An empty receiver
/// answers `[]` at any size, and an `n` at or above the length answers one chunk
/// holding everything.
///
/// Raises `FaultKind::InvalidSize` and answers Unit when `n` is not positive —
/// [`group_size`] has the reason, and it is the wrapper's whole faulting
/// surface. It reads **no descriptor callback**, so unlike `sorted` there is no
/// element it can be handed that it cannot group.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef` and `n` a
/// valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_chunks(
    ctx: *mut RuntimeContext,
    vec: GcRef,
    n: GcRef,
) -> GcRef {
    abi_guard!("praxis_vec_chunks", ctx, {
        // SAFETY: caller guarantees `n` is a valid Int.
        let Some(size) = group_size(unsafe { int_payload(n) }) else {
            unsafe { set_fault(ctx, RaisedFault::INVALID_SIZE) };
            return unsafe { unit_sentinel(ctx) };
        };
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let len = unsafe { vec_payload(vec) }.items.len();
        // Every `size`th position starts a chunk; the last one stops at the end
        // rather than past it, which is the short tail.
        let groups = (0..len)
            .step_by(size)
            .map(move |s| (s, (s + size).min(len)));
        unsafe { vec_of_groups(ctx, vec, groups) }
    })
}

/// `seq.windows(n)` — every consecutive run of exactly `n`, each starting one
/// element after the last (ADR-149). The receiver is not touched.
///
/// `[1, 2, 3, 4].windows(2)` is `[[1, 2], [2, 3], [3, 4]]`. Elements are shared,
/// not copied: the `2` in the first window and the `2` in the second are one
/// object, which is the language's reference semantics rather than a rule of
/// this wrapper.
///
/// **A window that does not fit is dropped rather than shortened**, which is the
/// one place this and [`praxis_vec_chunks`] answer differently: `[1, 2].windows(5)`
/// is `[]`, because a run of five is a run of five. It is not the fault below
/// arriving late — "which runs of five are there" has an answer for a sequence
/// of two, and that answer is none.
///
/// Raises `FaultKind::InvalidSize` and answers Unit when `n` is not positive,
/// for [`praxis_vec_chunks`]'s reason.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef` and `n` a
/// valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_windows(
    ctx: *mut RuntimeContext,
    vec: GcRef,
    n: GcRef,
) -> GcRef {
    abi_guard!("praxis_vec_windows", ctx, {
        // SAFETY: caller guarantees `n` is a valid Int.
        let Some(size) = group_size(unsafe { int_payload(n) }) else {
            unsafe { set_fault(ctx, RaisedFault::INVALID_SIZE) };
            return unsafe { unit_sentinel(ctx) };
        };
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let len = unsafe { vec_payload(vec) }.items.len();
        // Written as a subtraction guarded by its own comparison rather than a
        // `saturating_sub`: `len - size` saturating to zero would answer *one*
        // window for a receiver too short to hold any, and the empty answer is
        // the whole point of the branch.
        let starts = if size <= len { len - size + 1 } else { 0 };
        let groups = (0..starts).map(move |s| (s, s + size));
        unsafe { vec_of_groups(ctx, vec, groups) }
    })
}

/// `seq.join(sep)` — these `Text` elements concatenated with `sep` between them
/// (ADR-144). An empty sequence answers `""`; a one-element sequence answers
/// that element's characters and no separator.
///
/// Raises `FaultKind::TypeMismatch` and answers Unit when an element is not a
/// `Text`. The catalog row bounds the item to `Text`, so reaching that is a
/// compiler bug — reported the way `praxis_vec_sorted` reports its own, rather
/// than reading a foreign payload as a pointer-and-length pair.
///
/// This does **not** render: a `Vec[Int]` is refused at `praxis check` rather
/// than stringified here, which is what keeps `join` from being a back door
/// around ADR-143's decision about which types have a `to_text`.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef` and `sep` a
/// valid `Text` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_join(
    ctx: *mut RuntimeContext,
    vec: GcRef,
    sep: GcRef,
) -> GcRef {
    abi_guard!("praxis_vec_join", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec and `sep` a valid Text.
        let p = unsafe { vec_payload(vec) };
        if !p
            .items
            .iter()
            .all(|item| std::ptr::eq(item.descriptor(), &crate::text::TEXT))
        {
            unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
            return unsafe { unit_sentinel(ctx) };
        }
        let separator = unsafe { text_str(sep) };
        let mut joined = String::new();
        for (i, item) in p.items.iter().enumerate() {
            if i > 0 {
                joined.push_str(separator);
            }
            // SAFETY: the loop above proved every element's descriptor is TEXT.
            joined.push_str(unsafe { text_str(*item) });
        }
        // SAFETY: `joined` is valid UTF-8; ctx/heap valid.
        unsafe { text_ref(ctx, joined) }
    })
}

/// `chars.to_text()` — these `Char`s as one `Text`, with nothing between them
/// (ADR-144). The inverse of walking a `Text`, and what renders a `Grid` row
/// back as the line it was read from.
///
/// Each code point is read through [`read_scalar`] with the `Char` handle, never
/// a bare payload read: the payload is **four** bytes and an `i64` read would
/// take eight of them. A foreign element is `TypeMismatch` and the Unit
/// sentinel, for [`praxis_vec_join`]'s reason.
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_to_text(ctx: *mut RuntimeContext, vec: GcRef) -> GcRef {
    abi_guard!("praxis_vec_to_text", ctx, {
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        let mut rendered = String::new();
        for item in &p.items {
            // SAFETY: `read_scalar` proves the descriptor is `CHAR` before
            // reading its four bytes, and answers `None` otherwise.
            let Some(code) = (unsafe { read_scalar(*item, scalars::CHAR_PAYLOAD) }) else {
                unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
                return unsafe { unit_sentinel(ctx) };
            };
            // The descriptor's own writer, so a line rebuilt from a `Grid` row
            // holds the characters `out` would have written one at a time.
            scalars::write_char(&mut rendered, code);
        }
        // SAFETY: `rendered` is valid UTF-8; ctx/heap valid.
        unsafe { text_ref(ctx, rendered) }
    })
}

/// `v.frequencies()` — a `Counter[T]` holding how many times each element of
/// `vec` occurs (§6.3, §6.2).
///
/// The first combinator whose result is a **keyed** collection, which is why the
/// catalog row carries a `HashStable` bound of its own:
/// `require_collection_invariants` is applied to a method's *receiver*, and the
/// receiver here is an ordinary `Vec` that may legitimately hold anything. It is
/// the result that has a key rule.
///
/// The counter's key descriptor is the source `Vec`'s element label, which may
/// be null when the construction site knew no element type — the same null
/// [`praxis_counter_new`] already accepts and means "not told yet".
///
/// # Safety
/// `ctx` must be live and wired; `vec` must be a valid `Vec` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_vec_frequencies(ctx: *mut RuntimeContext, vec: GcRef) -> GcRef {
    abi_guard!("praxis_vec_frequencies", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        // The receiver is rooted **explicitly**, unlike the one-allocation
        // wrappers around it. The keys below are `GcRef`s into this `Vec`'s
        // items and they are held across one allocation *per distinct element*,
        // which is a much longer window than `praxis_set_items`' single
        // `vec_of`; relying on the caller's shadow frame alone for that long is
        // an assumption worth not making.
        let _receiver = scope.root(vec);
        // SAFETY: caller guarantees `vec` is a valid Vec.
        let p = unsafe { vec_payload(vec) };
        // Count first, with no allocation at all, so the tally cannot be
        // disturbed by a collection mid-loop. The tally is a `Vec` with a side
        // index rather than a bare map so the counts come out in
        // first-occurrence order, which makes the *allocation* order a function
        // of the input; the `Counter` itself is unordered either way.
        let mut counts: Vec<(DynamicKey, i64)> = Vec::new();
        let mut index: std::collections::HashMap<DynamicKey, usize> =
            std::collections::HashMap::new();
        for item in &p.items {
            let key = DynamicKey::new(*item);
            match index.get(&key) {
                Some(at) => counts[*at].1 += 1,
                None => {
                    index.insert(key, counts.len());
                    counts.push((key, 1));
                }
            }
        }
        let counter = unsafe { praxis_counter_new(ctx, p.element_descriptor) };
        let rooted = scope.root(counter);
        for (key, count) in counts {
            // Allocate first, then take the payload borrow — the boxed `Int`
            // allocation can collect, and the counter has to be reachable
            // through the native root store rather than through a `&mut` this
            // frame is holding across it.
            let boxed = unsafe { int_ref(ctx, count) };
            unsafe { counter_payload_mut(rooted) }
                .entries
                .insert(key, boxed);
        }
        counter
    })
}

// ---------------------------------------------------------------------------
// Deque[T] methods (§6.1). Mirrors the Vec surface but adds the
// front/back distinction: `push_front`/`push_back`/`pop_front`/`pop_back`.
// `pop_*` fault on an empty deque (§9.1 `EmptyCollection`).
// ---------------------------------------------------------------------------

use crate::collections::DequePayload;

/// Read the `DequePayload` out of a `GcRef` as a shared ref, asserting Deque.
///
/// # Safety
/// `r` must be a valid `Deque` `GcRef`.
unsafe fn deque_payload(r: GcRef) -> &'static DequePayload {
    // SAFETY: caller guarantees `r` is a Deque; see `payload_ref`.
    unsafe { payload_ref::<DequePayload>(r) }
}

/// Read the `DequePayload` out of a `GcRef` as a mutable ref, asserting Deque.
///
/// # Safety
/// `r` must be a valid `Deque` `GcRef`, rooted for `'s`.
unsafe fn deque_payload_mut<'s>(r: Rooted<'s>) -> &'s mut DequePayload {
    // SAFETY: caller guarantees `r` is a Deque; see `payload_mut`.
    unsafe { payload_mut::<DequePayload>(r) }
}

/// Allocate a new empty `Deque[T]` with the given element descriptor (§11.2).
/// A null descriptor stays null — "not told yet" — exactly as `praxis_vec_new`.
///
/// # Safety
/// `ctx` must be live and wired. `element_descriptor` must be a valid pointer to
/// a `'static TypeDescriptor` (or null).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_new(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
) -> GcRef {
    abi_guard!("praxis_deque_new", ctx, {
        // SAFETY: DequePayload is DEQUE's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::collections::DEQUE, || DequePayload {
                element_descriptor,
                items: std::collections::VecDeque::new(),
            })
        }
    })
}

/// Prepend `value` to the front of `deque`; returns Unit (§6.1).
///
/// # Safety
/// `ctx` must be live and wired; `deque` must be a valid `Deque` `GcRef`;
/// `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_push_front(
    ctx: *mut RuntimeContext,
    deque: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_deque_push_front", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { deque_payload_mut(scope.root(deque)) };
        if !unsafe { adopt_or_reject(ctx, &mut p.element_descriptor, value) } {
            return unsafe { unit_sentinel(ctx) };
        }
        let before = p.owned_bytes();
        p.items.push_front(value);
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// Append `value` to the back of `deque`; returns Unit (§6.1).
///
/// # Safety
/// `ctx` must be live and wired; `deque` must be a valid `Deque` `GcRef`;
/// `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_push_back(
    ctx: *mut RuntimeContext,
    deque: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_deque_push_back", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { deque_payload_mut(scope.root(deque)) };
        if !unsafe { adopt_or_reject(ctx, &mut p.element_descriptor, value) } {
            return unsafe { unit_sentinel(ctx) };
        }
        let before = p.owned_bytes();
        p.items.push_back(value);
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// Remove and return the front element; faults `EmptyCollection` if empty.
///
/// # Safety
/// `ctx` must be live and wired; `deque` must be a valid `Deque` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_pop_front(ctx: *mut RuntimeContext, deque: GcRef) -> GcRef {
    abi_guard!("praxis_deque_pop_front", ctx, {
        // No allocation in the common case, but `pop_front` on a VecDeque does not
        // allocate Rust heap, so no collection is needed; `deque` stays live.
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { deque_payload_mut(scope.root(deque)) };
        match p.items.pop_front() {
            Some(v) => v,
            None => {
                unsafe { set_fault(ctx, RaisedFault::EMPTY_COLLECTION) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// Remove and return the back element; faults `EmptyCollection` if empty.
///
/// # Safety
/// `ctx` must be live and wired; `deque` must be a valid `Deque` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_pop_back(ctx: *mut RuntimeContext, deque: GcRef) -> GcRef {
    abi_guard!("praxis_deque_pop_back", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { deque_payload_mut(scope.root(deque)) };
        match p.items.pop_back() {
            Some(v) => v,
            None => {
                unsafe { set_fault(ctx, RaisedFault::EMPTY_COLLECTION) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// The number of elements in `deque`, as a boxed `Int` (§6.1).
///
/// # Safety
/// `ctx` must be live and wired; `deque` must be a valid `Deque` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_len(ctx: *mut RuntimeContext, deque: GcRef) -> GcRef {
    abi_guard!("praxis_deque_len", ctx, {
        let p = unsafe { deque_payload(deque) };
        let len = p.items.len() as i64;
        unsafe { int_ref(ctx, len) }
    })
}

/// The element at `index` (0-based from the front); faults `IndexOutOfBounds`.
///
/// # Safety
/// `ctx` must be live and wired; `deque` must be a valid `Deque` `GcRef`;
/// `index` must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_get(
    ctx: *mut RuntimeContext,
    deque: GcRef,
    index: GcRef,
) -> GcRef {
    abi_guard!("praxis_deque_get", ctx, {
        let p = unsafe { deque_payload(deque) };
        let idx = unsafe { int_payload(index) };
        let Some(idx) = (unsafe { checked_index(ctx, idx, p.items.len()) }) else {
            return unsafe { unit_sentinel(ctx) };
        };
        p.items[idx]
    })
}

/// Replace the element at `index` (0-based from the front); faults
/// `IndexOutOfBounds` if out of range. Returns the Unit sentinel.
///
/// A replacement and never an insertion, and the element descriptor is
/// reconciled the same way, for [`praxis_vec_set`]'s reasons.
///
/// # Safety
/// `ctx` must be live and wired; `deque` must be a valid `Deque` `GcRef`;
/// `index` must be a valid `Int` `GcRef`; `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_set(
    ctx: *mut RuntimeContext,
    deque: GcRef,
    index: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_deque_set", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { deque_payload_mut(scope.root(deque)) };
        let idx = unsafe { int_payload(index) };
        let Some(idx) = (unsafe { checked_index(ctx, idx, p.items.len()) }) else {
            return unsafe { unit_sentinel(ctx) };
        };
        if !unsafe { adopt_or_reject(ctx, &mut p.element_descriptor, value) } {
            return unsafe { unit_sentinel(ctx) };
        }
        p.items[idx] = value;
        unsafe { unit_sentinel(ctx) }
    })
}

/// True iff `deque` has no elements, as a boxed `Bool` (§6.1).
///
/// # Safety
/// `ctx` must be live and wired; `deque` must be a valid `Deque` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_deque_is_empty(ctx: *mut RuntimeContext, deque: GcRef) -> GcRef {
    abi_guard!("praxis_deque_is_empty", ctx, {
        let p = unsafe { deque_payload(deque) };
        let empty = p.items.is_empty();
        unsafe { bool_ref(ctx, empty) }
    })
}

// ---------------------------------------------------------------------------
// Map[K, V] / Set[T] / Counter[T] (§6.1, §11.3).
//
// All three reuse Rust hash collections behind opaque GC objects. Keys are
// wrapped in `DynamicKey`, which delegates Rust `Hash`/`Eq` to the descriptor's
// structural callbacks — this is what makes tuples/records/enums/nested
// collections work as keys (§19.7 criterion). Counter's absent keys read as
// zero (§6.2); `min=`/`max=` update a map entry in place (§6.2).
// ---------------------------------------------------------------------------

use crate::maps::{CounterPayload, MapPayload, SetPayload};

/// Read a `MapPayload` as a shared ref. See `payload_ref` for the safety model.
unsafe fn map_payload(r: GcRef) -> &'static MapPayload {
    unsafe { payload_ref::<MapPayload>(r) }
}

unsafe fn map_payload_mut<'s>(r: Rooted<'s>) -> &'s mut MapPayload {
    unsafe { payload_mut::<MapPayload>(r) }
}

unsafe fn set_payload(r: GcRef) -> &'static SetPayload {
    unsafe { payload_ref::<SetPayload>(r) }
}

unsafe fn set_payload_mut<'s>(r: Rooted<'s>) -> &'s mut SetPayload {
    unsafe { payload_mut::<SetPayload>(r) }
}

unsafe fn counter_payload(r: GcRef) -> &'static CounterPayload {
    unsafe { payload_ref::<CounterPayload>(r) }
}

unsafe fn counter_payload_mut<'s>(r: Rooted<'s>) -> &'s mut CounterPayload {
    unsafe { payload_mut::<CounterPayload>(r) }
}

/// Allocate an empty `Map[K, V]`. `key_descriptor` is the key type the
/// construction site knew, or **null** when it knew none — which is kept null,
/// the way `praxis_vec_new` keeps it. Spelling an unknown type `INT` is a claim,
/// and every reader that believed it would read the wrong type.
///
/// # Safety
/// `ctx` must be live and wired. `key_descriptor` must be a valid pointer to a
/// `'static TypeDescriptor` (or null).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_new(
    ctx: *mut RuntimeContext,
    key_descriptor: *const TypeDescriptor,
) -> GcRef {
    abi_guard!("praxis_map_new", ctx, {
        // The `Map` row carries one type argument, so the value type never reaches
        // this wrapper at all — it is unknown here by construction, and says so.
        // `praxis_map_insert` adopts the first inserted value's own descriptor,
        // which is how a `Vec` learns its element type.
        let value_descriptor: *const TypeDescriptor = std::ptr::null();
        // SAFETY: MapPayload is MAP's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::maps::MAP, || MapPayload {
                key_descriptor,
                value_descriptor,
                entries: std::collections::HashMap::new(),
            })
        }
    })
}

/// Insert `(key, value)` into `map`, replacing any prior value; returns Unit.
///
/// # Safety
/// `ctx` must be live and wired; `map` must be a valid `Map` `GcRef`; `key` and
/// `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_insert(
    ctx: *mut RuntimeContext,
    map: GcRef,
    key: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_map_insert", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { map_payload_mut(scope.root(map)) };
        // Learn the value type from the first value inserted, the way a `Vec`
        // learns its element type from the first `push`. Null is the encoding of
        // "never been told", so it is distinguishable from a `Map` that really
        // holds `Int`s.
        //
        // A later value of a different type un-learns it rather than faulting: the
        // type checker makes a `Map` homogeneous, so this is unreachable for a
        // well-typed program, and `praxis_map_insert` is a non-faulting row (its
        // caller emits no fault check). Null is now representable and means "the
        // value's own descriptor answers", so forgetting is the safe direction.
        let val_desc = value.descriptor();
        match p.value() {
            None => p.value_descriptor = val_desc,
            Some(known) if !std::ptr::eq(known, val_desc) => {
                p.value_descriptor = std::ptr::null();
            }
            Some(_) => {}
        }
        let before = p.owned_bytes();
        p.entries.insert(DynamicKey::new(key), value);
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// `Some(value)` for `key`, or `None` if absent (§4.7, §5.7).
///
/// §5.7 writes the signature `Map[K,V].get(K) -> Option[V]` and §4.7 opens
/// "Option[T] represents normal domain-level absence. It is not an error
/// channel." Answering the Unit sentinel under a `V` static type instead would
/// hand the program a value it could not distinguish from a real one without
/// `contains`, while the type system insisted it was a `V`.
///
/// The `Option` is built through the runtime's own `option_schema`, whose
/// `Some` slot is unknown — `V` is learned from the value found, never from a
/// static type — and which `EnumSchema::same_type` therefore recognizes as the
/// same type as the codegen's `Option[Int]`. That is what lets the result match
/// against arms the program wrote.
///
/// # Safety
/// `ctx` must be live and wired; `map` must be a valid `Map` `GcRef`; `key`
/// must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_get(ctx: *mut RuntimeContext, map: GcRef, key: GcRef) -> GcRef {
    abi_guard!("praxis_map_get", ctx, {
        let found = {
            let p = unsafe { map_payload(map) };
            p.entries.get(&DynamicKey::new(key)).copied()
        };
        match found {
            Some(v) => unsafe { option_some(ctx, v) },
            None => unsafe { option_none(ctx) },
        }
    })
}

/// `map[key]` (§4.7): the value for `key`, **faulting** if it is absent.
///
/// A different wrapper from [`praxis_map_get`] because the two answers are the
/// language's own choice, not an implementation detail: §4.7 says "indexing a
/// missing map key faults instead of returning an option… the user chooses
/// between explicit absence with `.get` and assertion-like access with
/// indexing". Sharing one wrapper would take that choice away from the user.
///
/// The fault is [`FaultKind::IndexOutOfBounds`](crate::FaultKind::IndexOutOfBounds)
/// — an index the collection does not hold, which is what its doc already
/// describes. A dedicated `MissingKey` kind would read better, and adding one is
/// a `#[repr(C)]` change that costs an ABI bump (ADR-075).
///
/// # Safety
/// `ctx` must be live and wired; `map` must be a valid `Map` `GcRef`; `key`
/// must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_index(
    ctx: *mut RuntimeContext,
    map: GcRef,
    key: GcRef,
) -> GcRef {
    abi_guard!("praxis_map_index", ctx, {
        let p = unsafe { map_payload(map) };
        match p.entries.get(&DynamicKey::new(key)) {
            Some(v) => *v,
            None => {
                unsafe { set_fault(ctx, RaisedFault::INDEX_OUT_OF_BOUNDS) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// True iff `key` is present, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `map` and `key` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_contains(
    ctx: *mut RuntimeContext,
    map: GcRef,
    key: GcRef,
) -> GcRef {
    abi_guard!("praxis_map_contains", ctx, {
        let p = unsafe { map_payload(map) };
        let present = p.entries.contains_key(&DynamicKey::new(key));
        unsafe { bool_ref(ctx, present) }
    })
}

/// Remove `key`; returns Unit (the removed value, if any, is dropped).
///
/// # Safety
/// `ctx` must be live and wired; `map` and `key` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_remove(
    ctx: *mut RuntimeContext,
    map: GcRef,
    key: GcRef,
) -> GcRef {
    abi_guard!("praxis_map_remove", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { map_payload_mut(scope.root(map)) };
        p.entries.remove(&DynamicKey::new(key));
        unsafe { unit_sentinel(ctx) }
    })
}

/// The number of entries, as a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `map` must be a valid `Map` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_len(ctx: *mut RuntimeContext, map: GcRef) -> GcRef {
    abi_guard!("praxis_map_len", ctx, {
        let p = unsafe { map_payload(map) };
        unsafe { int_ref(ctx, p.entries.len() as i64) }
    })
}

/// `m.keys()` — every key, as a `Vec[K]`. Ordered like
/// [`praxis_counter_keys`], and index-aligned with [`praxis_map_values`].
///
/// This and `values()` are the only way to enumerate a `Map`: `for kv in m` has
/// no lowering.
///
/// # Safety
/// `ctx` must be live and wired; `map` must be a valid `Map` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_keys(ctx: *mut RuntimeContext, map: GcRef) -> GcRef {
    abi_guard!("praxis_map_keys", ctx, {
        let key_desc = unsafe { map_payload(map) }.key_descriptor;
        let rows = unsafe { crate::maps::ordered_entries(&map_payload(map).entries) };
        unsafe { vec_of(ctx, key_desc, rows.into_iter().map(|(k, _)| k)) }
    })
}

/// `m.values()` — every value, as a `Vec[V]`. See [`praxis_map_keys`].
///
/// # Safety
/// `ctx` must be live and wired; `map` must be a valid `Map` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_values(ctx: *mut RuntimeContext, map: GcRef) -> GcRef {
    abi_guard!("praxis_map_values", ctx, {
        let val_desc = unsafe { map_payload(map) }.value_descriptor;
        let rows = unsafe { crate::maps::ordered_entries(&map_payload(map).entries) };
        unsafe { vec_of(ctx, val_desc, rows.into_iter().map(|(_, v)| v)) }
    })
}

/// True iff the map is empty, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `map` must be a valid `Map` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_is_empty(ctx: *mut RuntimeContext, map: GcRef) -> GcRef {
    abi_guard!("praxis_map_is_empty", ctx, {
        let p = unsafe { map_payload(map) };
        unsafe { bool_ref(ctx, p.entries.is_empty()) }
    })
}

/// `distance[key] min= candidate` (§6.2): keep the smaller value, or insert if
/// absent (an absent entry accepts the first value). The value must support
/// ordering (Int); returns Unit.
///
/// # Safety
/// `ctx` must be live and wired; `map`, `key`, `value` must be valid `GcRef`s
/// and `value` must be an `Int`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_update_min(
    ctx: *mut RuntimeContext,
    map: GcRef,
    key: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_map_update_min", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { map_payload_mut(scope.root(map)) };
        let cand = unsafe { int_payload(value) };
        match p.entries.get_mut(&DynamicKey::new(key)) {
            Some(existing) => {
                let cur = unsafe { int_payload(*existing) };
                if cand < cur {
                    *existing = value;
                }
            }
            None => {
                p.entries.insert(DynamicKey::new(key), value);
            }
        }
        unsafe { unit_sentinel(ctx) }
    })
}

/// `best[key] max= score` (§6.2): keep the larger value, or insert if absent.
///
/// # Safety
/// `ctx` must be live and wired; `map`, `key`, `value` must be valid `GcRef`s
/// and `value` must be an `Int`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_map_update_max(
    ctx: *mut RuntimeContext,
    map: GcRef,
    key: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_map_update_max", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { map_payload_mut(scope.root(map)) };
        let cand = unsafe { int_payload(value) };
        match p.entries.get_mut(&DynamicKey::new(key)) {
            Some(existing) => {
                let cur = unsafe { int_payload(*existing) };
                if cand > cur {
                    *existing = value;
                }
            }
            None => {
                p.entries.insert(DynamicKey::new(key), value);
            }
        }
        unsafe { unit_sentinel(ctx) }
    })
}

// --- Set[T] -----------------------------------------------------------------

/// Allocate an empty `Set[T]`. `element_descriptor` is the element type the
/// construction site knew, or **null** when it knew none — kept null.
///
/// # Safety
/// `ctx` must be live and wired; `element_descriptor` must be a valid pointer to
/// a `'static TypeDescriptor` (or null).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_set_new(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
) -> GcRef {
    abi_guard!("praxis_set_new", ctx, {
        // SAFETY: SetPayload is SET's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::maps::SET, || SetPayload {
                element_descriptor,
                entries: std::collections::HashSet::new(),
            })
        }
    })
}

/// Insert `value` into `set`; returns Unit.
///
/// # Safety
/// `ctx` must be live and wired; `set` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_set_insert(
    ctx: *mut RuntimeContext,
    set: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_set_insert", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { set_payload_mut(scope.root(set)) };
        let before = p.owned_bytes();
        p.entries.insert(DynamicKey::new(value));
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// Remove `value` from `set`; returns Unit.
///
/// # Safety
/// `ctx` must be live and wired; `set` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_set_remove(
    ctx: *mut RuntimeContext,
    set: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_set_remove", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { set_payload_mut(scope.root(set)) };
        p.entries.remove(&DynamicKey::new(value));
        unsafe { unit_sentinel(ctx) }
    })
}

/// True iff `value` is in the set, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `set` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_set_contains(
    ctx: *mut RuntimeContext,
    set: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_set_contains", ctx, {
        let p = unsafe { set_payload(set) };
        let present = p.entries.contains(&DynamicKey::new(value));
        unsafe { bool_ref(ctx, present) }
    })
}

/// The number of elements, as a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `set` must be a valid `Set` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_set_len(ctx: *mut RuntimeContext, set: GcRef) -> GcRef {
    abi_guard!("praxis_set_len", ctx, {
        let p = unsafe { set_payload(set) };
        unsafe { int_ref(ctx, p.entries.len() as i64) }
    })
}

/// True iff the set is empty, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `set` must be a valid `Set` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_set_is_empty(ctx: *mut RuntimeContext, set: GcRef) -> GcRef {
    abi_guard!("praxis_set_is_empty", ctx, {
        let p = unsafe { set_payload(set) };
        unsafe { bool_ref(ctx, p.entries.is_empty()) }
    })
}

/// Every member, as a `Vec[T]` in [`crate::maps::ordered_members`] order — the
/// snapshot `for x in s` iterates (ADR-066).
///
/// There is no `praxis_set_get`, and this is why: a `HashSet` has no nth member,
/// so an indexed accessor would be a linear scan per step and the loop would be
/// quadratic. The snapshot is one pass, and it is what makes the order
/// deterministic — which for `for` is the program's *answer* and not only its
/// printing.
///
/// # Safety
/// `ctx` must be live and wired; `set` must be a valid `Set` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_set_items(ctx: *mut RuntimeContext, set: GcRef) -> GcRef {
    abi_guard!("praxis_set_items", ctx, {
        let elem_desc = unsafe { set_payload(set) }.element_descriptor;
        let members = unsafe { crate::maps::ordered_members(&set_payload(set).entries) };
        unsafe { vec_of(ctx, elem_desc, members.into_iter()) }
    })
}

// --- Counter[T] -------------------------------------------------------------

/// Allocate an empty `Counter[T]`. `key_descriptor` is the key type the
/// construction site knew, or **null** when it knew none — kept null.
///
/// # Safety
/// `ctx` must be live and wired; `key_descriptor` must be a valid pointer to a
/// `'static TypeDescriptor` (or null).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_counter_new(
    ctx: *mut RuntimeContext,
    key_descriptor: *const TypeDescriptor,
) -> GcRef {
    abi_guard!("praxis_counter_new", ctx, {
        // SAFETY: CounterPayload is COUNTER's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::maps::COUNTER, || CounterPayload {
                key_descriptor,
                entries: std::collections::HashMap::new(),
            })
        }
    })
}

/// The count for `key`, or zero if absent (§6.2: "absent values read as zero").
/// Never faults. Returns a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `counter` and `key` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_counter_get(
    ctx: *mut RuntimeContext,
    counter: GcRef,
    key: GcRef,
) -> GcRef {
    abi_guard!("praxis_counter_get", ctx, {
        let p = unsafe { counter_payload(counter) };
        let count = match p.entries.get(&DynamicKey::new(key)) {
            Some(v) => unsafe { int_payload(*v) },
            None => 0, // §6.2: absent reads as zero.
        };
        unsafe { int_ref(ctx, count) }
    })
}

/// Increment the count for `key` by one (inserting 1 if absent); returns Unit.
///
/// # Safety
/// `ctx` must be live and wired; `counter` and `key` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_counter_inc(
    ctx: *mut RuntimeContext,
    counter: GcRef,
    key: GcRef,
) -> GcRef {
    abi_guard!("praxis_counter_inc", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { counter_payload_mut(scope.root(counter)) };
        let dk = DynamicKey::new(key);
        match p.entries.get_mut(&dk) {
            Some(v) => {
                let cur = unsafe { int_payload(*v) };
                // Checked, like every other integer computation in this file
                // (§4.12): a raw `cur + 1` panics across `extern "C"` in debug — the
                // non-unwinding panic §10.4 forbids — and wraps to `i64::MIN` in
                // release, which is a silently wrong count. A `Counter`'s values are
                // set to arbitrary `Int`s by `c[k] = n`, so this is reachable from
                // source and not only from `i64::MAX` increments.
                let Some(next) = cur.checked_add(1) else {
                    unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
                    return unsafe { unit_sentinel(ctx) };
                };
                // SAFETY: ctx is wired; alloc a fresh Int for the incremented value.
                *v = unsafe { int_ref(ctx, next) };
            }
            None => {
                let one = unsafe { int_ref(ctx, 1_i64) };
                p.entries.insert(dk, one);
            }
        }
        unsafe { unit_sentinel(ctx) }
    })
}

/// `counts[key] = value` (§6.2): set the count for `key`, replacing any prior
/// one; returns Unit.
///
/// [`praxis_counter_inc`] adds exactly one, so it cannot express
/// `counts[key] += n` or `counts[key] = n`. A subscript assignment is a
/// read-modify-write over the pair (`praxis_counter_get`, this), which is what
/// makes every assignment operator work on a `Counter` rather than only `+= 1`.
///
/// # Safety
/// `ctx` must be live and wired; `counter` and `key` must be valid `GcRef`s and
/// `value` must be an `Int`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_counter_set(
    ctx: *mut RuntimeContext,
    counter: GcRef,
    key: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_counter_set", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { counter_payload_mut(scope.root(counter)) };
        let before = p.owned_bytes();
        p.entries.insert(DynamicKey::new(key), value);
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// `c.keys()` — every key, as a `Vec[T]`.
///
/// Ordered by the key's own `compare` (ADR-138), so it is the *same* order
/// [`praxis_counter_values`] uses and the two are index-aligned. A `HashMap`'s
/// own order is randomized per process, so returning it would make the same
/// program answer differently on two runs — and here the order is the program's
/// *answer*, not only its printing.
///
/// # Safety
/// `ctx` must be live and wired; `counter` must be a valid `Counter` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_counter_keys(ctx: *mut RuntimeContext, counter: GcRef) -> GcRef {
    abi_guard!("praxis_counter_keys", ctx, {
        let key_desc = unsafe { counter_payload(counter) }.key_descriptor;
        let rows = unsafe { crate::maps::ordered_entries(&counter_payload(counter).entries) };
        unsafe { vec_of(ctx, key_desc, rows.into_iter().map(|(k, _)| k)) }
    })
}

/// `c.values()` — every count, as a `Vec[Int]`.
///
/// §3.3's representative program is `counts.values().count(|n| n >= 2)`. Ordered
/// like [`praxis_counter_keys`]; see it for why the order is fixed.
///
/// # Safety
/// `ctx` must be live and wired; `counter` must be a valid `Counter` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_counter_values(ctx: *mut RuntimeContext, counter: GcRef) -> GcRef {
    abi_guard!("praxis_counter_values", ctx, {
        let rows = unsafe { crate::maps::ordered_entries(&counter_payload(counter).entries) };
        unsafe { vec_of(ctx, &scalars::INT, rows.into_iter().map(|(_, v)| v)) }
    })
}

/// The number of distinct keys, as a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `counter` must be a valid `Counter` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_counter_len(ctx: *mut RuntimeContext, counter: GcRef) -> GcRef {
    abi_guard!("praxis_counter_len", ctx, {
        let p = unsafe { counter_payload(counter) };
        unsafe { int_ref(ctx, p.entries.len() as i64) }
    })
}

/// True iff the counter has no keys, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `counter` must be a valid `Counter` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_counter_is_empty(
    ctx: *mut RuntimeContext,
    counter: GcRef,
) -> GcRef {
    abi_guard!("praxis_counter_is_empty", ctx, {
        let p = unsafe { counter_payload(counter) };
        unsafe { bool_ref(ctx, p.entries.is_empty()) }
    })
}

// ---------------------------------------------------------------------------
// MinHeap[T] / MaxHeap[T] (§6.1, §11.2).
//
// `MaxHeap` maps directly to Rust's max-`BinaryHeap`; `MinHeap` wraps entries in
// `Reverse` so the smallest surfaces first. `pop`/`peek` fault `EmptyCollection`
// on an empty heap.
// ---------------------------------------------------------------------------

use crate::heaps::{HeapEntry, MaxHeapPayload, MinHeapPayload};
use std::collections::BinaryHeap;

unsafe fn max_heap_payload_mut<'s>(r: Rooted<'s>) -> &'s mut MaxHeapPayload {
    unsafe { payload_mut::<MaxHeapPayload>(r) }
}

unsafe fn max_heap_payload(r: GcRef) -> &'static MaxHeapPayload {
    unsafe { payload_ref::<MaxHeapPayload>(r) }
}

unsafe fn min_heap_payload_mut<'s>(r: Rooted<'s>) -> &'s mut MinHeapPayload {
    unsafe { payload_mut::<MinHeapPayload>(r) }
}

unsafe fn min_heap_payload(r: GcRef) -> &'static MinHeapPayload {
    unsafe { payload_ref::<MinHeapPayload>(r) }
}

/// Allocate an empty `MaxHeap[T]`. A null `element_descriptor` — the codegen's
/// "no static element type" — is kept null.
///
/// # Safety
/// `ctx` must be live and wired; `element_descriptor` must be a valid pointer to
/// a `'static TypeDescriptor` (or null).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_max_heap_new(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
) -> GcRef {
    abi_guard!("praxis_max_heap_new", ctx, {
        // SAFETY: MaxHeapPayload is MAX_HEAP's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::heaps::MAX_HEAP, || MaxHeapPayload {
                element_descriptor,
                items: BinaryHeap::new(),
            })
        }
    })
}

/// Push `value` onto the max-heap; returns Unit.
///
/// # Safety
/// `ctx` must be live and wired; `heap` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_max_heap_push(
    ctx: *mut RuntimeContext,
    heap_ref: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_max_heap_push", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { max_heap_payload_mut(scope.root(heap_ref)) };
        let before = p.owned_bytes();
        p.items.push(HeapEntry {
            value,
            descriptor: value.descriptor(),
        });
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// Remove and return the largest element; faults `EmptyCollection` if empty.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MaxHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_max_heap_pop(ctx: *mut RuntimeContext, heap_ref: GcRef) -> GcRef {
    abi_guard!("praxis_max_heap_pop", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { max_heap_payload_mut(scope.root(heap_ref)) };
        match p.items.pop() {
            Some(e) => e.value,
            None => {
                unsafe { set_fault(ctx, RaisedFault::EMPTY_COLLECTION) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// The largest element without removing it; faults `EmptyCollection` if empty.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MaxHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_max_heap_peek(ctx: *mut RuntimeContext, heap_ref: GcRef) -> GcRef {
    abi_guard!("praxis_max_heap_peek", ctx, {
        let p = unsafe { max_heap_payload(heap_ref) };
        match p.items.peek() {
            Some(e) => e.value,
            None => {
                unsafe { set_fault(ctx, RaisedFault::EMPTY_COLLECTION) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// The number of elements, as a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MaxHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_max_heap_len(ctx: *mut RuntimeContext, heap_ref: GcRef) -> GcRef {
    abi_guard!("praxis_max_heap_len", ctx, {
        let p = unsafe { max_heap_payload(heap_ref) };
        unsafe { int_ref(ctx, p.items.len() as i64) }
    })
}

/// Every element, as a `Vec[T]` in [`crate::heaps::in_pop_order`] — the snapshot
/// `for x in h` iterates (ADR-066). The heap is **not** drained.
///
/// A heap's backing array is heap-ordered only at its root, so an indexed
/// accessor over it would answer in insertion-history order — reading the array
/// as if it were a `Vec`'s.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MaxHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_max_heap_items(ctx: *mut RuntimeContext, heap_ref: GcRef) -> GcRef {
    abi_guard!("praxis_max_heap_items", ctx, {
        let p = unsafe { max_heap_payload(heap_ref) };
        let items = crate::heaps::in_pop_order(&p.items, |e| e.value);
        unsafe { vec_of(ctx, p.element_descriptor, items.into_iter()) }
    })
}

/// True iff the heap is empty, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MaxHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_max_heap_is_empty(
    ctx: *mut RuntimeContext,
    heap_ref: GcRef,
) -> GcRef {
    abi_guard!("praxis_max_heap_is_empty", ctx, {
        let p = unsafe { max_heap_payload(heap_ref) };
        unsafe { bool_ref(ctx, p.items.is_empty()) }
    })
}

// --- MinHeap (mirrors MaxHeap with Reverse wrapping) -----------------------

/// Allocate an empty `MinHeap[T]`. A null `element_descriptor` — the codegen's
/// "no static element type" — is kept null.
///
/// # Safety
/// `ctx` must be live and wired; `element_descriptor` must be a valid pointer to
/// a `'static TypeDescriptor` (or null).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_min_heap_new(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
) -> GcRef {
    abi_guard!("praxis_min_heap_new", ctx, {
        // SAFETY: MinHeapPayload is MIN_HEAP's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::heaps::MIN_HEAP, || MinHeapPayload {
                element_descriptor,
                items: BinaryHeap::new(),
            })
        }
    })
}

/// Push `value` onto the min-heap; returns Unit.
///
/// # Safety
/// `ctx` must be live and wired; `heap` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_min_heap_push(
    ctx: *mut RuntimeContext,
    heap_ref: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_min_heap_push", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { min_heap_payload_mut(scope.root(heap_ref)) };
        let before = p.owned_bytes();
        p.items.push(std::cmp::Reverse(HeapEntry {
            value,
            descriptor: value.descriptor(),
        }));
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// Remove and return the smallest element; faults `EmptyCollection` if empty.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MinHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_min_heap_pop(ctx: *mut RuntimeContext, heap_ref: GcRef) -> GcRef {
    abi_guard!("praxis_min_heap_pop", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { min_heap_payload_mut(scope.root(heap_ref)) };
        match p.items.pop() {
            Some(e) => e.0.value,
            None => {
                unsafe { set_fault(ctx, RaisedFault::EMPTY_COLLECTION) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// The smallest element without removing it; faults `EmptyCollection` if empty.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MinHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_min_heap_peek(ctx: *mut RuntimeContext, heap_ref: GcRef) -> GcRef {
    abi_guard!("praxis_min_heap_peek", ctx, {
        let p = unsafe { min_heap_payload(heap_ref) };
        match p.items.peek() {
            Some(e) => e.0.value,
            None => {
                unsafe { set_fault(ctx, RaisedFault::EMPTY_COLLECTION) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// The number of elements, as a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MinHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_min_heap_len(ctx: *mut RuntimeContext, heap_ref: GcRef) -> GcRef {
    abi_guard!("praxis_min_heap_len", ctx, {
        let p = unsafe { min_heap_payload(heap_ref) };
        unsafe { int_ref(ctx, p.items.len() as i64) }
    })
}

/// Every element, as a `Vec[T]` in [`crate::heaps::in_pop_order`] — ascending,
/// because the stored entry is a `Reverse<HeapEntry>`. See
/// [`praxis_max_heap_items`].
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MinHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_min_heap_items(ctx: *mut RuntimeContext, heap_ref: GcRef) -> GcRef {
    abi_guard!("praxis_min_heap_items", ctx, {
        let p = unsafe { min_heap_payload(heap_ref) };
        let items = crate::heaps::in_pop_order(&p.items, |e| e.0.value);
        unsafe { vec_of(ctx, p.element_descriptor, items.into_iter()) }
    })
}

/// True iff the heap is empty, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `heap_ref` must be a valid `MinHeap` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_min_heap_is_empty(
    ctx: *mut RuntimeContext,
    heap_ref: GcRef,
) -> GcRef {
    abi_guard!("praxis_min_heap_is_empty", ctx, {
        let p = unsafe { min_heap_payload(heap_ref) };
        unsafe { bool_ref(ctx, p.items.is_empty()) }
    })
}

// ---------------------------------------------------------------------------
// BitSet (§6.1). A compact set of non-negative integers.
// ---------------------------------------------------------------------------

use crate::bitset::{BitIndex, BitSetPayload};

unsafe fn bitset_payload(r: GcRef) -> &'static BitSetPayload {
    unsafe { payload_ref::<BitSetPayload>(r) }
}

unsafe fn bitset_payload_mut<'s>(r: Rooted<'s>) -> &'s mut BitSetPayload {
    unsafe { payload_mut::<BitSetPayload>(r) }
}

/// Allocate an empty `BitSet` (§6.1). Nullary — no element descriptor.
///
/// # Safety
/// `ctx` must be live and wired.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bitset_new(ctx: *mut RuntimeContext) -> GcRef {
    abi_guard!("praxis_bitset_new", ctx, {
        // SAFETY: BitSetPayload is BITSET's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::bitset::BITSET, || BitSetPayload {
                words: ReprCVec::new(),
            })
        }
    })
}

/// Set bit `value`; returns Unit. Faults `InvalidSize` if `value` is negative
/// or above [`BitIndex::MAX`] — a member this set cannot hold.
///
/// # Safety
/// `ctx` must be live and wired; `bs` must be a valid `BitSet` `GcRef`; `value`
/// must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bitset_insert(
    ctx: *mut RuntimeContext,
    bs: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_bitset_insert", ctx, {
        unsafe { maybe_collect(ctx) };
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { bitset_payload_mut(scope.root(bs)) };
        let i = unsafe { int_payload(value) };
        // An insert that cannot be honoured is a fault, not a silent no-op: the
        // caller asked the set to contain something, and it will not.
        let Some(index) = BitIndex::new(i) else {
            unsafe { set_fault(ctx, RaisedFault::INVALID_SIZE) };
            return unsafe { unit_sentinel(ctx) };
        };
        // A `BitSet` grows its word vector to reach the index, so an insert far
        // past the current high-water is a large uncharged allocation — the
        // shape `bfs` has, one visited-set per search.
        let before = p.owned_bytes();
        p.insert(index);
        charge_growth(ctx, before, p.owned_bytes());
        unsafe { unit_sentinel(ctx) }
    })
}

/// Clear bit `value`; returns Unit. A value the set cannot hold is a value it
/// does not contain, so removing one is a no-op rather than a fault.
///
/// # Safety
/// `ctx` must be live and wired; `bs` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bitset_remove(
    ctx: *mut RuntimeContext,
    bs: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_bitset_remove", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { bitset_payload_mut(scope.root(bs)) };
        let i = unsafe { int_payload(value) };
        if let Some(index) = BitIndex::new(i) {
            p.remove(index);
        }
        unsafe { unit_sentinel(ctx) }
    })
}

/// True iff bit `value` is set, as a raw `0`/`1` in the scalar channel. A value
/// the set cannot hold is simply absent — the query is total.
///
/// **It answers an `i64` and not a boxed `Bool` (ADR-118 decision 6.)** A boxed
/// answer would be unboxed again on the next instruction — `if bs.contains(x)`
/// as a `Materialize{Bool}`, an `ExtractScalar{Bool}` and then the branch that
/// wanted the predicate. `praxis_struct_eq` and `praxis_value_cmp` answer the
/// scalar channel for the same reason, and MIR carries this one as
/// [`Inst::BitsetContains`](praxis_mir::Inst::BitsetContains) — a
/// `Scalar(Bool)` result, and, because it neither allocates nor faults, not a
/// GC safepoint.
///
/// `0` and `1` and nothing else, which is what the `Bool` payload byte holds
/// and what `emit_inline_bool` re-boxes with a `!= 0` test.
///
/// # Safety
/// `ctx` must be live and wired; `bs` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bitset_contains(
    ctx: *mut RuntimeContext,
    bs: GcRef,
    value: GcRef,
) -> i64 {
    abi_guard!("praxis_bitset_contains", ctx, {
        let p = unsafe { bitset_payload(bs) };
        let i = unsafe { int_payload(value) };
        let present = BitIndex::new(i).is_some_and(|index| p.contains(index));
        i64::from(present)
    })
}

/// The number of set bits, as a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `bs` must be a valid `BitSet` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bitset_len(ctx: *mut RuntimeContext, bs: GcRef) -> GcRef {
    abi_guard!("praxis_bitset_len", ctx, {
        let p = unsafe { bitset_payload(bs) };
        unsafe { int_ref(ctx, p.count() as i64) }
    })
}

/// Every member, as a `Vec[Int]` **ascending** — the snapshot `for i in b`
/// iterates (ADR-066).
///
/// This is the one iterable whose members are not objects: they are bit
/// positions, so each one is boxed here rather than copied from the payload.
///
/// # Safety
/// `ctx` must be live and wired; `bs` must be a valid `BitSet` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bitset_items(ctx: *mut RuntimeContext, bs: GcRef) -> GcRef {
    abi_guard!("praxis_bitset_items", ctx, {
        // The members are read out before the first allocation: `vec_of` allocates
        // per element, and a collection during the walk would move nothing here
        // (the bits are not objects) but would leave the borrow of the payload
        // spanning a safepoint, which is not allowed.
        let members: Vec<i64> = unsafe { bitset_payload(bs) }.members().collect();
        let result = unsafe { praxis_vec_new(ctx, &scalars::INT as *const _) };
        let scope = unsafe { NativeScope::new(ctx) };
        let rooted = scope.root(result);
        for value in members {
            let boxed = unsafe { int_ref(ctx, value) };
            unsafe { vec_payload_mut(rooted) }.items.push(boxed);
        }
        result
    })
}

/// True iff the bitset is empty, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `bs` must be a valid `BitSet` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bitset_is_empty(ctx: *mut RuntimeContext, bs: GcRef) -> GcRef {
    abi_guard!("praxis_bitset_is_empty", ctx, {
        let p = unsafe { bitset_payload(bs) };
        unsafe { bool_ref(ctx, p.count() == 0) }
    })
}

// ---------------------------------------------------------------------------
// Grid[T] methods (§6.4). `GridPayload` is a row-major `Vec<GcRef>` plus a
// width. Coordinates are (x, y) with x rightward, y downward (§6.4). Indexing
// stays behind runtime wrappers (§11.5 realloc safety).
// ---------------------------------------------------------------------------

use crate::collections::{GridExtent, GridPayload};

unsafe fn grid_payload(r: GcRef) -> &'static GridPayload {
    unsafe { payload_ref::<GridPayload>(r) }
}

unsafe fn grid_payload_mut<'s>(r: Rooted<'s>) -> &'s mut GridPayload {
    unsafe { payload_mut::<GridPayload>(r) }
}

/// Allocate a `(x, y)` point tuple from two `i64` coordinates. The schema is
/// the cached `(Int, Int)` point schema; elements are filled via
/// `praxis_tuple_set`. Returns the point `GcRef`.
///
/// Three allocations, and each one may collect: the tuple must survive the two
/// coordinate allocations, and the x coordinate must survive the y's. Nothing
/// generated is on the stack here — the caller is a runtime helper — so the
/// only thing that can root them is a native scope.
unsafe fn alloc_point(ctx: *mut RuntimeContext, x: i64, y: i64) -> GcRef {
    let scope = unsafe { NativeScope::new(ctx) };
    let schema = crate::tuples::point_schema();
    let schema_ptr = schema as *const crate::tuples::TupleSchema;
    let tup = scope.root(unsafe { praxis_alloc_tuple(ctx, schema_ptr) });
    let x_ref = scope.root(unsafe { int_ref(ctx, x) });
    unsafe { praxis_tuple_set(ctx, tup.get(), 0, x_ref.get()) };
    let y_ref = unsafe { int_ref(ctx, y) };
    unsafe { praxis_tuple_set(ctx, tup.get(), 1, y_ref) };
    tup.get()
}

/// The (x, y) coordinates of a flat `idx` in a grid of `width`.
fn grid_xy(idx: usize, width: usize) -> (i64, i64) {
    ((idx % width) as i64, (idx / width) as i64)
}

/// Read the two coordinates out of a `(Int, Int)` point tuple.
///
/// The inverse of [`alloc_point`], and the one place the grid wrappers unpack a
/// point: every `Grid` method taking a position takes it as this tuple, so
/// there is one shape to read and no reason for two readings of it.
///
/// # Safety
/// `point` must be a valid `(Int, Int)` tuple `GcRef` — which the type checker
/// guarantees for every catalog row whose parameter is `Tuple[Int, Int]`.
unsafe fn point_xy(point: GcRef) -> (i64, i64) {
    let tp = point.payload::<crate::tuples::TuplePayload>() as *const crate::tuples::TuplePayload;
    // SAFETY: caller guarantees `point` is a tuple, so its payload is a
    // `TuplePayload`, and a `(Int, Int)` shape has both slots filled with `Int`s.
    let pt = unsafe { &*tp };
    unsafe { (int_payload(pt.items[0]), int_payload(pt.items[1])) }
}

/// The height (row count) of a grid: `items.len() / width`, or 0 if width is 0
/// (avoids division by zero on a degenerate empty grid).
fn grid_height(items_len: usize, width: usize) -> usize {
    items_len.checked_div(width).unwrap_or(0)
}

/// The in-bounds neighbour at `(px + dx, py + dy)`, or `None` if it falls
/// outside a `width × height` grid.
///
/// The offsets are `checked_add` because `px`/`py` come out of a user-supplied
/// point tuple, so `(i64::MAX, 0).neighbors4()` would otherwise overflow the
/// addition and panic *inside* `extern "C"`. A coordinate that overflows is
/// outside every grid — `GridExtent` bounds the extents far below `i64::MAX` —
/// so "outside" is the whole answer, not a special case.
fn grid_neighbor(
    px: i64,
    py: i64,
    dx: i64,
    dy: i64,
    width: usize,
    height: usize,
) -> Option<(i64, i64)> {
    let nx = px.checked_add(dx)?;
    let ny = py.checked_add(dy)?;
    // Both non-negative below, so the casts are exact.
    (nx >= 0 && ny >= 0 && (nx as usize) < width && (ny as usize) < height).then_some((nx, ny))
}

/// The zero value of the type `descriptor` names, or `None` if that type has no
/// natural default.
///
/// Only the scalars and `Unit` have one. A `Grid[Vec[Int]](3, 3)` would need
/// nine distinct empty vectors and, worse, no way to know their element type —
/// so it is refused rather than filled with something of the wrong type. A null
/// descriptor means the caller never said what the cells are, which is likewise
/// nothing this can invent.
///
/// # Safety
/// `ctx` must be live and wired.
unsafe fn default_cell(
    ctx: *mut RuntimeContext,
    descriptor: *const TypeDescriptor,
) -> Option<GcRef> {
    use crate::descriptor::BuiltinTypeId as B;
    // SAFETY: a non-null descriptor is a valid `&'static`.
    let builtin = unsafe { descriptor.as_ref() }?.as_builtin()?;
    unsafe {
        match builtin {
            B::Unit => Some(unit_sentinel(ctx)),
            B::Bool => Some(bool_ref(ctx, false)),
            B::Int => Some(int_ref(ctx, 0_i64)),
            B::Byte => Some(gc_alloc(ctx, scalars::BYTE_PAYLOAD, 0_u8)),
            // `0_u32`, not `'\0'`: a `Char`'s payload is the scalar *value*,
            // and a Rust `char` only fits because it shares `u32`'s layout. NUL
            // is inside the interned range, so this is the immortal, like the
            // `Int` arm above.
            B::Char => Some(char_ref(ctx, 0_u32)),
            B::Float => Some(gc_alloc(ctx, scalars::FLOAT_PAYLOAD, 0.0_f64)),
            // `(null, 0)` meets `praxis_alloc_text`'s UTF-8 precondition
            // trivially: the wrapper's own `bytes.is_null() || len == 0` branch
            // turns it into the empty slice, and the empty slice is UTF-8.
            // The precondition is load-bearing (ADR-111): a violation here
            // would abort, not fault. `alloc_text_empty_string_round_trips`
            // pins the branch this depends on.
            B::Text => Some(praxis_alloc_text(ctx, std::ptr::null(), 0)),
            // A composite has no zero value the runtime can invent: a
            // `Grid[Vec[Int]]` must be filled by the program that knows what its
            // cells are.
            B::Vec
            | B::Deque
            | B::Grid
            | B::Map
            | B::Set
            | B::Counter
            | B::MinHeap
            | B::MaxHeap
            | B::BitSet
            // A `Range`'s zero value would be a pair of bounds nobody chose;
            // `0..0` is *a* range but it is not "the empty one" in any sense a
            // `Grid[Range]` cell wants.
            | B::Range
            | B::Tuple
            | B::Record
            | B::Enum
            | B::Closure
            | B::VarCell => None,
        }
    }
}

/// Allocate an empty `Grid[T]` with the given element descriptor, width, and
/// height, all cells initialized to the cell type's zero value. (The input parser also constructs
/// grids directly; this wrapper is for source `Grid[T]()` + a follow-up fill.)
///
/// Faults `InvalidSize` if either extent is negative or the grid would exceed
/// [`GridExtent::MAX_CELLS`] — the sizes arrive from source, where a negative
/// value would otherwise land near `usize::MAX` on the cast.
///
/// # Safety
/// `ctx` must be live and wired; `element_descriptor` must be a valid pointer to
/// a `'static TypeDescriptor` (or null).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_new(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
    width: i64,
    height: i64,
) -> GcRef {
    abi_guard!("praxis_grid_new", ctx, {
        let Some(extent) = GridExtent::new(width, height) else {
            unsafe { set_fault(ctx, RaisedFault::INVALID_SIZE) };
            return unsafe { unit_sentinel(ctx) };
        };
        // Every cell of a `Grid[T]` must *be* a `T`. Filling with the Unit sentinel
        // under a `T` element descriptor is the same lie as a mislabelled element
        // descriptor, one level down: `get`, `format`, `equals` and `hash` all
        // dispatch `T`'s callbacks against a zero-sized Unit payload.
        let cells = if extent.cells() == 0 {
            Vec::new()
        } else {
            let Some(fill) = (unsafe { default_cell(ctx, element_descriptor) }) else {
                unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
                return unsafe { unit_sentinel(ctx) };
            };
            vec![fill; extent.cells()]
        };
        // SAFETY: GridPayload is GRID's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::collections::GRID, || GridPayload {
                element_descriptor,
                items: cells,
                width: extent.width(),
            })
        }
    })
}

/// Allocate a `Grid[T]` of `width` × `height` cells, every one holding `fill`
/// (ADR-146's `Grid(w, h, fill)`) — the working grid an algorithm allocates for
/// itself: an occupancy board, a visited mask, a distance table.
///
/// Faults `InvalidSize` on the extents [`praxis_grid_new`] refuses, through the
/// same [`GridExtent::new`], since this is the very allocation ADR-041 was
/// written about and a fill changes nothing about the arithmetic.
///
/// **It does not call [`default_cell`], and that is the whole difference.**
/// `praxis_grid_new` has to invent a zero value for the cell type and has none
/// for a composite, so it raises `TypeMismatch` for a `Grid[Vec[Int]]` rather
/// than filling it with Unit sentinels under a `Vec` descriptor. An explicit
/// fill removes the question — the caller supplied a value of the cell type —
/// so a grid of collections is constructible here and not there. The descriptor
/// is still reconciled through [`adopt_or_reject`], so a *declared* cell type
/// the fill does not match is `TypeMismatch` rather than a silent retag.
///
/// Every cell is the same `GcRef`, exactly as [`praxis_vec_filled`]'s are; see
/// its comment for why that is the language's existing rule rather than a new
/// one. The extents arrive boxed for the reason stated there too.
///
/// # Safety
/// `ctx` must be live and wired; `element_descriptor` must be a valid pointer to
/// a `'static TypeDescriptor` (or null); `width` and `height` must be valid
/// `Int` `GcRef`s; `fill` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_filled(
    ctx: *mut RuntimeContext,
    element_descriptor: *const TypeDescriptor,
    width: GcRef,
    height: GcRef,
    fill: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_filled", ctx, {
        // SAFETY: caller guarantees `width` and `height` are valid Ints.
        let (w, h) = unsafe { (int_payload(width), int_payload(height)) };
        let Some(extent) = GridExtent::new(w, h) else {
            unsafe { set_fault(ctx, RaisedFault::INVALID_SIZE) };
            return unsafe { unit_sentinel(ctx) };
        };
        let mut descriptor = element_descriptor;
        if !unsafe { adopt_or_reject(ctx, &mut descriptor, fill) } {
            return unsafe { unit_sentinel(ctx) };
        }
        let scope = unsafe { NativeScope::new(ctx) };
        let fill = scope.root(fill).get();
        // The cells are built inside the initializer, which `gc_alloc_owned` runs
        // *after* the safepoint — `praxis_vec_filled`'s rule, for the same
        // untraced `Vec<GcRef>`.
        // SAFETY: GridPayload is GRID's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::collections::GRID, || GridPayload {
                element_descriptor: descriptor,
                items: vec![fill; extent.cells()],
                width: extent.width(),
            })
        }
    })
}

/// The grid width (number of columns), as a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_width(ctx: *mut RuntimeContext, grid: GcRef) -> GcRef {
    abi_guard!("praxis_grid_width", ctx, {
        let p = unsafe { grid_payload(grid) };
        unsafe { int_ref(ctx, p.width as i64) }
    })
}

/// The grid height (number of rows), as a boxed Int.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_height(ctx: *mut RuntimeContext, grid: GcRef) -> GcRef {
    abi_guard!("praxis_grid_height", ctx, {
        let p = unsafe { grid_payload(grid) };
        // height = items.len() / width.
        let height = grid_height(p.items.len(), p.width);
        unsafe { int_ref(ctx, height as i64) }
    })
}

/// The cell at `(x, y)`; faults `IndexOutOfBounds` if out of range.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`; `x`/`y`
/// must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_get(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    x: GcRef,
    y: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_get", ctx, {
        let p = unsafe { grid_payload(grid) };
        let (xi, yi) = (unsafe { int_payload(x) }, unsafe { int_payload(y) });
        let height = grid_height(p.items.len(), p.width);
        let Some(idx) = (unsafe { checked_cell(ctx, xi, yi, p.width, height) }) else {
            return unsafe { unit_sentinel(ctx) };
        };
        p.items[idx]
    })
}

/// Set the cell at `(x, y)`; faults `IndexOutOfBounds` if out of range.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`; `x`/`y`
/// must be valid `Int` `GcRef`s; `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_set(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    x: GcRef,
    y: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_set", ctx, {
        let scope = unsafe { NativeScope::new(ctx) };
        let p = unsafe { grid_payload_mut(scope.root(grid)) };
        let (xi, yi) = (unsafe { int_payload(x) }, unsafe { int_payload(y) });
        let height = grid_height(p.items.len(), p.width);
        let Some(idx) = (unsafe { checked_cell(ctx, xi, yi, p.width, height) }) else {
            return unsafe { unit_sentinel(ctx) };
        };
        if !unsafe { adopt_or_reject(ctx, &mut p.element_descriptor, value) } {
            return unsafe { unit_sentinel(ctx) };
        }
        p.items[idx] = value;
        unsafe { unit_sentinel(ctx) }
    })
}

/// True iff `(x, y)` is within the grid, as a boxed Bool.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`; `x`/`y`
/// must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_contains(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    x: GcRef,
    y: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_contains", ctx, {
        let p = unsafe { grid_payload(grid) };
        let (xi, yi) = (unsafe { int_payload(x) }, unsafe { int_payload(y) });
        let height = grid_height(p.items.len(), p.width);
        // The **pure** [`cell_index`], never `checked_cell`: this wrapper's
        // manifest row is `Pure`, so generated code emits no `CheckFault` after
        // it and a fault raised on every legitimate `false` would sit pending
        // until an unrelated check picked it up.
        let inside = cell_index(xi, yi, p.width, height).is_some();
        unsafe { bool_ref(ctx, inside) }
    })
}

/// The 4 orthogonal neighbors of `point` that lie inside the grid, as a `Vec`.
///
/// # Safety
/// `ctx` must be live and wired; `grid` and `point` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_neighbors4(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_neighbors4", ctx, {
        let p = unsafe { grid_payload(grid) };
        let (px, py) = unsafe { point_xy(point) };
        let height = grid_height(p.items.len(), p.width);
        let result = unsafe { praxis_vec_new(ctx, &crate::tuples::TUPLE as *const _) };
        let scope = unsafe { NativeScope::new(ctx) };
        let rp = unsafe { vec_payload_mut(scope.root(result)) };
        for (dx, dy) in [(0i64, -1), (0, 1), (-1, 0), (1, 0)] {
            if let Some((nx, ny)) = grid_neighbor(px, py, dx, dy, p.width, height) {
                let pt_ref = unsafe { alloc_point(ctx, nx, ny) };
                rp.items.push(pt_ref);
            }
        }
        result
    })
}

/// The 8 neighbors of `point` that lie inside the grid, as a `Vec`.
///
/// # Safety
/// `ctx` must be live and wired; `grid` and `point` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_neighbors8(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_neighbors8", ctx, {
        let p = unsafe { grid_payload(grid) };
        let (px, py) = unsafe { point_xy(point) };
        let height = grid_height(p.items.len(), p.width);
        let result = unsafe { praxis_vec_new(ctx, &crate::tuples::TUPLE as *const _) };
        let scope = unsafe { NativeScope::new(ctx) };
        let rp = unsafe { vec_payload_mut(scope.root(result)) };
        for dy in -1i64..=1 {
            for dx in -1i64..=1 {
                if dx == 0 && dy == 0 {
                    continue;
                }
                if let Some((nx, ny)) = grid_neighbor(px, py, dx, dy, p.width, height) {
                    let pt_ref = unsafe { alloc_point(ctx, nx, ny) };
                    rp.items.push(pt_ref);
                }
            }
        }
        result
    })
}

/// Build one neighbourhood record: a field per direction, in `directions`
/// order, each `Some((x, y))` or `None`.
///
/// # What the field order means, and what it costs to get wrong
///
/// Slot *i* of the record is `directions[i]`, and the reader's slot index comes
/// from the *static* type — the catalog's `Around4`/`Around8` row. The two
/// orders are required to agree and nothing derives one from the other, so
/// `around_schemas_match_the_catalog` asserts it: a mismatch is a field read
/// that quietly answers the wrong direction.
///
/// # Rooting
///
/// Every field costs up to four allocations (two `Int`s, a point tuple, a
/// `Some`), each a safepoint, so the record is rooted for the whole loop. Each
/// field's own value is stored the moment it exists — `praxis_record_set_field`
/// allocates nothing — so no second reference is ever live across a safepoint.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef` and
/// `point` a valid `(Int, Int)` tuple.
unsafe fn grid_around(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
    schema: &'static crate::records::RecordSchema,
    directions: &'static [crate::records::Direction],
) -> GcRef {
    // SAFETY: the caller upholds every argument's validity.
    let (width, height) = unsafe {
        let p = grid_payload(grid);
        (p.width, grid_height(p.items.len(), p.width))
    };
    let (px, py) = unsafe { point_xy(point) };
    let scope = unsafe { NativeScope::new(ctx) };
    let record = scope.root(unsafe { praxis_alloc_record(ctx, schema) });
    for (i, d) in directions.iter().enumerate() {
        let field = match grid_neighbor(px, py, d.dx, d.dy, width, height) {
            // `option_some` roots the point across the enum allocation.
            Some((nx, ny)) => unsafe { option_some(ctx, alloc_point(ctx, nx, ny)) },
            None => unsafe { option_none(ctx) },
        };
        unsafe { praxis_record_set_field(ctx, record.get(), i as u32, field) };
    }
    record.get()
}

/// The four orthogonal neighbours of `point` as an `Around4` record — `up`,
/// `left`, `right`, `down`, each `Some((x, y))` or `None` (§6.4).
///
/// **Not a shorter `neighbors4`.** That wrapper answers a `Vec` clipped to what
/// is in bounds, which is what a graph walk wants and what
/// `bfs(start, |p| g.neighbors4(p))` passes; it throws away *which* direction
/// each neighbour was, and off the edge of the grid it throws away that there
/// was a direction at all. This answers exactly that, and the `None` is the
/// half a `Vec` cannot carry.
///
/// # Safety
/// `ctx` must be live and wired; `grid` and `point` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_around4(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_around4", ctx, {
        unsafe {
            grid_around(
                ctx,
                grid,
                point,
                crate::records::around4_schema(),
                crate::records::AROUND4_DIRECTIONS,
            )
        }
    })
}

/// All eight neighbours of `point` as an `Around8` record, in reading order —
/// `up_left`, `up`, `up_right`, `left`, `right`, `down_left`, `down`,
/// `down_right` (§6.4). See [`praxis_grid_around4`].
///
/// # Safety
/// `ctx` must be live and wired; `grid` and `point` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_around8(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_around8", ctx, {
        unsafe {
            grid_around(
                ctx,
                grid,
                point,
                crate::records::around8_schema(),
                crate::records::AROUND8_DIRECTIONS,
            )
        }
    })
}

/// How many of `point`'s in-bounds neighbours in `directions` hold a cell equal
/// to `value`.
///
/// Equality is the value's own descriptor callback — the same path
/// `praxis_grid_find` and `praxis_grid_find_all` take, so "equals" means one
/// thing across every `Grid` row (§5.5).
///
/// A direction that leaves the grid has no cell, so it is not counted. Nothing
/// allocates until the answer is boxed, so `grid` needs no root.
///
/// # Safety
/// `ctx` must be live and wired; `grid`, `point` and `value` must be valid
/// `GcRef`s.
unsafe fn grid_count_equal(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
    value: GcRef,
    directions: &'static [crate::records::Direction],
) -> GcRef {
    // SAFETY: the caller upholds every argument's validity.
    let p = unsafe { grid_payload(grid) };
    let height = grid_height(p.items.len(), p.width);
    let (px, py) = unsafe { point_xy(point) };
    let eq = value.descriptor().equals;
    let mut n = 0_i64;
    for d in directions {
        let Some((nx, ny)) = grid_neighbor(px, py, d.dx, d.dy, p.width, height) else {
            continue;
        };
        let cell = p.items[ny as usize * p.width + nx as usize];
        let matches = match eq {
            // SAFETY: `equals` came off `value`'s descriptor, and the grid's
            // cells are values of the element type the catalog row unified
            // `value` with.
            Some(equals) => unsafe {
                equals(
                    cell.payload::<u8>() as *const u8,
                    value.payload::<u8>() as *const u8,
                )
            },
            None => cell == value,
        };
        n += i64::from(matches);
    }
    unsafe { int_ref(ctx, n) }
}

/// `g.count4(p, v)` — how many of the four orthogonal in-bounds neighbours hold
/// `v` (§6.4).
///
/// # Safety
/// `ctx` must be live and wired; `grid`, `point` and `value` must be valid
/// `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_count4(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_count4", ctx, {
        unsafe { grid_count_equal(ctx, grid, point, value, crate::records::AROUND4_DIRECTIONS) }
    })
}

/// `g.count8(p, v)` — how many of the eight in-bounds neighbours hold `v`
/// (§6.4).
///
/// # Safety
/// `ctx` must be live and wired; `grid`, `point` and `value` must be valid
/// `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_count8(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_count8", ctx, {
        unsafe { grid_count_equal(ctx, grid, point, value, crate::records::AROUND8_DIRECTIONS) }
    })
}

/// How many of `point`'s in-bounds neighbours in `directions` hold a cell the
/// closure accepts.
///
/// A direction that leaves the grid has no cell, so the closure is not called
/// for it — the predicate never sees a position that is not on the grid.
///
/// # Rooting, and what a faulting closure does
///
/// The closure runs arbitrary Praxis code between iterations, so it allocates
/// and it collects. Every cell the loop will hand it is read out of the grid
/// and rooted **before the first call**: `grid_payload` hands back a borrow of
/// a heap object, and a collection triggered by call *i* would otherwise be
/// free to reclaim the cell call *i + 1* is about to receive.
///
/// A fault stops the count and answers the Unit sentinel, exactly as
/// `praxis_vec_sorted_by_key` does: the call site's own fault check is what
/// reports, and a half-finished count is not an answer.
///
/// # Safety
/// `ctx` must be live and wired; `grid` and `point` must be valid `GcRef`s and
/// `pred` a valid closure `GcRef`.
unsafe fn grid_count_where(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
    pred: GcRef,
    directions: &'static [crate::records::Direction],
) -> GcRef {
    let scope = unsafe { NativeScope::new(ctx) };
    // SAFETY: the caller upholds every argument's validity.
    let cells: Vec<GcRef> = unsafe {
        let p = grid_payload(grid);
        let height = grid_height(p.items.len(), p.width);
        let (px, py) = point_xy(point);
        directions
            .iter()
            .filter_map(|d| grid_neighbor(px, py, d.dx, d.dy, p.width, height))
            .map(|(nx, ny)| {
                scope
                    .root(p.items[ny as usize * p.width + nx as usize])
                    .get()
            })
            .collect()
    };
    let mut n = 0_i64;
    for cell in cells {
        let Some(answer) = (unsafe { call_unary_closure(ctx, pred, cell) }) else {
            // The closure faulted (or is not a closure, which the type checker
            // already refused). Leave the fault for the call site's check.
            return unsafe { unit_sentinel(ctx) };
        };
        // A `Bool`'s payload is **one byte**, and `read_scalar` takes the width
        // from `BOOL_PAYLOAD`'s own type after checking the descriptor — so a
        // closure that answered something else is a `TypeMismatch` rather than
        // seven bytes of uninitialized alignment padding.
        // SAFETY: `answer` is the `GcRef` the call just produced.
        let Some(byte) = (unsafe { read_scalar(answer, scalars::BOOL_PAYLOAD) }) else {
            unsafe { set_fault(ctx, RaisedFault::TYPE_MISMATCH) };
            return unsafe { unit_sentinel(ctx) };
        };
        n += i64::from(byte != 0);
    }
    unsafe { int_ref(ctx, n) }
}

/// `g.count4_where(p, f)` — how many of the four orthogonal in-bounds
/// neighbours hold a cell `f` accepts (§6.4).
///
/// # Safety
/// `ctx` must be live and wired; `grid`, `point` and `pred` must be valid
/// `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_count4_where(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
    pred: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_count4_where", ctx, {
        unsafe { grid_count_where(ctx, grid, point, pred, crate::records::AROUND4_DIRECTIONS) }
    })
}

/// `g.count8_where(p, f)` — how many of the eight in-bounds neighbours hold a
/// cell `f` accepts (§6.4).
///
/// # Safety
/// `ctx` must be live and wired; `grid`, `point` and `pred` must be valid
/// `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_count8_where(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    point: GcRef,
    pred: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_count8_where", ctx, {
        unsafe { grid_count_where(ctx, grid, point, pred, crate::records::AROUND8_DIRECTIONS) }
    })
}

/// All `(x, y)` positions in row-major order, as a `Vec`.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_positions(ctx: *mut RuntimeContext, grid: GcRef) -> GcRef {
    abi_guard!("praxis_grid_positions", ctx, {
        unsafe { maybe_collect(ctx) };
        let p = unsafe { grid_payload(grid) };
        let result = unsafe { praxis_vec_new(ctx, &crate::tuples::TUPLE as *const _) };
        let scope = unsafe { NativeScope::new(ctx) };
        let rp = unsafe { vec_payload_mut(scope.root(result)) };
        for i in 0..p.items.len() {
            let (x, y) = grid_xy(i, p.width);
            rp.items.push(unsafe { alloc_point(ctx, x, y) });
        }
        result
    })
}

/// All cells in row-major order, as a `Vec`.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_cells(ctx: *mut RuntimeContext, grid: GcRef) -> GcRef {
    abi_guard!("praxis_grid_cells", ctx, {
        let p = unsafe { grid_payload(grid) };
        let result = unsafe { praxis_vec_new(ctx, p.element_descriptor) };
        let scope = unsafe { NativeScope::new(ctx) };
        let rp = unsafe { vec_payload_mut(scope.root(result)) };
        for cell in p.items.iter() {
            rp.items.push(*cell);
        }
        result
    })
}

/// Row `y` as a `Vec`; faults `IndexOutOfBounds` if out of range.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`; `y`
/// must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_row(ctx: *mut RuntimeContext, grid: GcRef, y: GcRef) -> GcRef {
    abi_guard!("praxis_grid_row", ctx, {
        let p = unsafe { grid_payload(grid) };
        let yi = unsafe { int_payload(y) };
        let height = grid_height(p.items.len(), p.width);
        // One axis of [`cell_index`]'s rule: a row is bounded by the height
        // alone, and every `x` in it is in range by construction.
        let Some(row) = (unsafe { checked_index(ctx, yi, height) }) else {
            return unsafe { unit_sentinel(ctx) };
        };
        let start = row * p.width;
        let result = unsafe { praxis_vec_new(ctx, p.element_descriptor) };
        let scope = unsafe { NativeScope::new(ctx) };
        let rp = unsafe { vec_payload_mut(scope.root(result)) };
        for x in 0..p.width {
            rp.items.push(p.items[start + x]);
        }
        result
    })
}

/// Column `x` as a `Vec`; faults `IndexOutOfBounds` if out of range.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`; `x`
/// must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_column(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    x: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_column", ctx, {
        let p = unsafe { grid_payload(grid) };
        let xi = unsafe { int_payload(x) };
        // The other axis: a column is bounded by the width alone, and the
        // stride below walks only the rows that exist.
        let Some(col) = (unsafe { checked_index(ctx, xi, p.width) }) else {
            return unsafe { unit_sentinel(ctx) };
        };
        let result = unsafe { praxis_vec_new(ctx, p.element_descriptor) };
        let scope = unsafe { NativeScope::new(ctx) };
        let rp = unsafe { vec_payload_mut(scope.root(result)) };
        let mut idx = col;
        while idx < p.items.len() {
            rp.items.push(p.items[idx]);
            idx += p.width;
        }
        result
    })
}

/// `Some((x, y))` for the first position whose cell equals `value`, or `None`
/// (§4.7).
///
/// An `Option` rather than a sentinel: the Unit sentinel under a `(Int, Int)`
/// static type is indistinguishable from a real answer. `find_all` needs no
/// equivalent — a `Vec` already encodes "nothing matched" as emptiness.
///
/// # Safety
/// `ctx` must be live and wired; `grid` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_find(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_find", ctx, {
        let p = unsafe { grid_payload(grid) };
        let val_desc = value.descriptor();
        let eq = val_desc.equals;
        for (i, cell) in p.items.iter().enumerate() {
            let matches = match eq {
                Some(equals) => {
                    let a = cell.payload::<u8>() as *const u8;
                    let b = value.payload::<u8>() as *const u8;
                    unsafe { equals(a, b) }
                }
                None => *cell == value,
            };
            if matches {
                let (x, y) = grid_xy(i, p.width);
                // `option_some` roots the point across the enum allocation.
                return unsafe { option_some(ctx, alloc_point(ctx, x, y)) };
            }
        }
        unsafe { option_none(ctx) }
    })
}

/// All `(x, y)` positions whose cell equals `value`, as a `Vec`.
///
/// # Safety
/// `ctx` must be live and wired; `grid` and `value` must be valid `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_find_all(
    ctx: *mut RuntimeContext,
    grid: GcRef,
    value: GcRef,
) -> GcRef {
    abi_guard!("praxis_grid_find_all", ctx, {
        unsafe { maybe_collect(ctx) };
        let p = unsafe { grid_payload(grid) };
        let val_desc = value.descriptor();
        let eq = val_desc.equals;
        let result = unsafe { praxis_vec_new(ctx, &crate::tuples::TUPLE as *const _) };
        let scope = unsafe { NativeScope::new(ctx) };
        let rp = unsafe { vec_payload_mut(scope.root(result)) };
        for (i, cell) in p.items.iter().enumerate() {
            let matches = match eq {
                Some(equals) => {
                    let a = cell.payload::<u8>() as *const u8;
                    let b = value.payload::<u8>() as *const u8;
                    unsafe { equals(a, b) }
                }
                None => *cell == value,
            };
            if matches {
                let (x, y) = grid_xy(i, p.width);
                rp.items.push(unsafe { alloc_point(ctx, x, y) });
            }
        }
        result
    })
}

/// A transposed copy of the grid (rows ↔ columns), as a new `Grid`.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_transpose(ctx: *mut RuntimeContext, grid: GcRef) -> GcRef {
    abi_guard!("praxis_grid_transpose", ctx, {
        let p = unsafe { grid_payload(grid) };
        let height = grid_height(p.items.len(), p.width);
        let new_width = height;
        let new_height = p.width;
        let mut cells = Vec::with_capacity(p.items.len());
        for y in 0..new_height {
            for x in 0..new_width {
                // new[x,y] = old[y,x]
                cells.push(p.items[x * p.width + y]);
            }
        }
        let _ = ctx;
        // SAFETY: GridPayload is GRID's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::collections::GRID, || GridPayload {
                element_descriptor: p.element_descriptor,
                items: cells,
                width: new_width,
            })
        }
    })
}

/// A copy of the grid rotated 90° left (counter-clockwise), as a new `Grid`.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_rotate_left(ctx: *mut RuntimeContext, grid: GcRef) -> GcRef {
    abi_guard!("praxis_grid_rotate_left", ctx, {
        let p = unsafe { grid_payload(grid) };
        let height = grid_height(p.items.len(), p.width);
        // Rotate left (90° CCW): result is H×W (width=height, height=width).
        // With x rightward and y downward, turning counter-clockwise carries the
        // *rightmost* column to the top row, top-to-bottom:
        // result[x, y] = original[width-1-y, x], for x in 0..height, y in 0..width.
        let new_width = height;
        let new_height = p.width;
        let mut cells = Vec::with_capacity(p.items.len());
        for y in 0..new_height {
            for x in 0..new_width {
                let ox = p.width - 1 - y;
                let oy = x;
                cells.push(p.items[oy * p.width + ox]);
            }
        }
        // SAFETY: GridPayload is GRID's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::collections::GRID, || GridPayload {
                element_descriptor: p.element_descriptor,
                items: cells,
                width: new_width,
            })
        }
    })
}

/// A copy of the grid rotated 90° right (clockwise), as a new `Grid`.
///
/// # Safety
/// `ctx` must be live and wired; `grid` must be a valid `Grid` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_grid_rotate_right(ctx: *mut RuntimeContext, grid: GcRef) -> GcRef {
    abi_guard!("praxis_grid_rotate_right", ctx, {
        let p = unsafe { grid_payload(grid) };
        let height = grid_height(p.items.len(), p.width);
        // Rotate right (90° CW): result is H×W (width=height, height=width).
        // With x rightward and y downward, turning clockwise carries the *leftmost*
        // column to the top row, bottom-to-top:
        // result[x, y] = original[y, height-1-x], for x in 0..height, y in 0..width.
        let new_width = height;
        let new_height = p.width;
        let mut cells = Vec::with_capacity(p.items.len());
        for y in 0..new_height {
            for x in 0..new_width {
                let ox = y;
                let oy = height - 1 - x;
                cells.push(p.items[oy * p.width + ox]);
            }
        }
        // SAFETY: GridPayload is GRID's payload type.
        unsafe {
            gc_alloc_owned(ctx, &crate::collections::GRID, || GridPayload {
                element_descriptor: p.element_descriptor,
                items: cells,
                width: new_width,
            })
        }
    })
}

// ---------------------------------------------------------------------------
// Text methods (§4.3).
//
// `Text` is an immutable UTF-8 payload (`Box<str>`). The methods are pure
// (no allocation beyond the result object) and never fault.
// ---------------------------------------------------------------------------

/// Read the `Text` payload of a `GcRef` as a `&str`, following slice owners.
///
/// # Safety
/// `r` must be a valid `Text` `GcRef`. Non-moving GC keeps it stable.
unsafe fn text_str(r: GcRef) -> &'static str {
    // SAFETY: caller guarantees `r` is Text; payload is a TextPayload.
    let payload = r.payload::<crate::text::TextPayload>() as *const crate::text::TextPayload;
    unsafe { crate::text::text_str(payload) }
}

/// The `Text` payload behind a `GcRef`.
///
/// # Safety
/// `r` must be a valid `Text` `GcRef`. Non-moving GC keeps it stable.
#[inline]
unsafe fn text_payload(r: GcRef) -> *const crate::text::TextPayload {
    r.payload::<crate::text::TextPayload>() as *const crate::text::TextPayload
}

/// The number of Unicode scalar values (chars) in `text`, as a boxed `Int`.
///
/// **O(1) after the text or its owner has been counted once** (ADR-115). The
/// count is cached rather than recomputed as `text_str(text).chars().count()`,
/// which is two passes over every byte — `text_str` re-validates the UTF-8 the
/// payload is already known to hold, and `chars().count()` then decodes it —
/// and this is called *once per iteration* of `for c in t`, because `lower_for`
/// puts the plan's `len` call in the loop **header**
/// (`praxis-mir/src/build.rs`, `lower_for`).
///
/// # Safety
/// `ctx` must be live and wired; `text` must be a valid `Text` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_text_len(ctx: *mut RuntimeContext, text: GcRef) -> GcRef {
    abi_guard!("praxis_text_len", ctx, {
        // SAFETY: caller guarantees `text` is Text.
        let len = unsafe { crate::text::text_char_count(text_payload(text)) } as i64;
        unsafe { int_ref(ctx, len) }
    })
}

/// The whole of `text`, trimmed, if `run` accepts all of it — the shared half of
/// [`praxis_text_int`] and [`praxis_text_float`] (ADR-136).
///
/// **`run` is the input parser's own scanner** (`parser::take_int_run`,
/// `parser::take_float_run`), and that is the point rather than a convenience.
/// `parse(t, int)` and `t.int()` are two spellings of "read a number out of
/// text", and a program that gets different answers from them has found a defect
/// in one of them. Sharing the scanner makes the disagreement unrepresentable.
///
/// The difference between the method and the atomic is *how much* must match,
/// not what: an atomic stops where its run stops and hands the rest of the line
/// to the template, and a method has no rest to hand anywhere — so a run that
/// covers less than the whole trimmed text is `None`. That is what makes
/// `"1 2"`, `"12abc"` and `"1."` rejections rather than partial answers.
///
/// Trimming is the one liberty taken, and it is what makes a line read off input
/// usable without a second call.
fn whole_trimmed(s: &str, run: fn(&[u8]) -> (&str, usize)) -> Option<&str> {
    let trimmed = s.trim();
    let (text, len) = run(trimmed.as_bytes());
    (!text.is_empty() && len == trimmed.len()).then_some(trimmed)
}

/// The `Int` `text` spells, as `Some(n)`, or `None` when it spells no `Int`
/// (ADR-136).
///
/// `Y001`'s help on `var count: Int = raw` names `.int()`, so this is the method
/// that help sends the reader to.
///
/// `Option[Int]` and not `Int`, for §4.7's reason: a text that is not a number
/// is *absence*, not a fault. Input arrives as text and is routinely not what
/// the program hoped, so a panicking conversion would make `"abc".int()` a crash
/// the program has no way to prevent — where `read lines(int)`, the other half
/// of that help, reports at the parser and never produces the value at all.
///
/// The accepted spelling is **§7.4's `int` atomic** over the whole trimmed text:
/// an optional `-` and then digits (see [`whole_trimmed`]). `"1 2"`, `"0x10"`,
/// `"1_000"`, `"+5"` and `""` are all `None`, and so is a value outside `Int`'s
/// range — for the reason `Y013` exists: a saturated answer is a number nobody
/// wrote.
///
/// # Safety
/// `ctx` must be live and wired; `text` must be a valid `Text` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_text_int(ctx: *mut RuntimeContext, text: GcRef) -> GcRef {
    abi_guard!("praxis_text_int", ctx, {
        // SAFETY: caller guarantees `text` is Text.
        let s = unsafe { text_str(text) };
        match whole_trimmed(s, crate::parser::take_int_run).and_then(|t| t.parse::<i64>().ok()) {
            // SAFETY: `ctx` is live and wired; `int_ref` allocates the payload
            // and `option_some` roots it across the enum allocation.
            Some(n) => unsafe {
                let boxed = int_ref(ctx, n);
                option_some(ctx, boxed)
            },
            // SAFETY: `ctx` is live and wired.
            None => unsafe { option_none(ctx) },
        }
    })
}

/// The `Float` `text` spells, as `Some(x)`, or `None` when it spells no `Float`
/// (ADR-136).
///
/// [`praxis_text_int`]'s twin, over §7.4's `float` atomic: an optional sign,
/// digits, an optional `.` **with** a fraction, and an optional complete
/// exponent. `"1.5"`, `"-2"`, `"+5.0"` and `"1e10"` are values; `"1."`, `"1e"`,
/// `"inf"`, `"nan"` and `""` are `None`, because none of them is a token the
/// input parser reads either.
///
/// `inf` and `nan` are the answer worth stating: Rust's `f64::from_str` accepts
/// both, §7.4's `float` accepts neither, and a method that took them would be a
/// second opinion about what a number is. `Float` still *has* those values —
/// `1.0 / 0.0` is one — and `Float.to_text()` prints them; what has no spelling
/// is reading one back out of arbitrary text.
///
/// The leading `+` this accepts and [`praxis_text_int`] does not is §7.4's own
/// asymmetry, carried over rather than papered over: changing an atomic's
/// accepted set is a change to the input language.
///
/// # Safety
/// `ctx` must be live and wired; `text` must be a valid `Text` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_text_float(ctx: *mut RuntimeContext, text: GcRef) -> GcRef {
    abi_guard!("praxis_text_float", ctx, {
        // SAFETY: caller guarantees `text` is Text.
        let s = unsafe { text_str(text) };
        match whole_trimmed(s, crate::parser::take_float_run).and_then(|t| t.parse::<f64>().ok()) {
            // SAFETY: `ctx` is live and wired. `praxis_alloc_float` takes the
            // bit pattern the uniform scalar ABI carries (§4.3), and
            // `option_some` roots the box across the enum allocation.
            Some(x) => unsafe {
                let boxed = praxis_alloc_float(ctx, x.to_bits() as i64);
                option_some(ctx, boxed)
            },
            // SAFETY: `ctx` is live and wired.
            None => unsafe { option_none(ctx) },
        }
    })
}

/// True iff `text` has no chars, as a boxed `Bool`.
///
/// Asks the bytes rather than a `&str`: `text_str` validates the whole payload
/// to hand back a `&str`, which would make an O(1) question O(n) (ADR-115). A
/// text is empty iff it has no bytes — no scalar encodes to zero of them.
///
/// # Safety
/// `ctx` must be live and wired; `text` must be a valid `Text` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_text_is_empty(ctx: *mut RuntimeContext, text: GcRef) -> GcRef {
    abi_guard!("praxis_text_is_empty", ctx, {
        // SAFETY: caller guarantees `text` is Text.
        let empty = unsafe { crate::text::text_bytes(text_payload(text)) }.is_empty();
        // SAFETY: ctx/heap valid; Bool immortal path.
        unsafe { bool_ref(ctx, empty) }
    })
}

/// `a + b` on two `Text`s — a new owned `Text` holding their concatenation
/// (ADR-085).
///
/// Declared `Allocates` rather than `AllocatesAndFaults`, which is
/// `praxis_float_to_text`'s row and for the same reason: both payloads are
/// UTF-8 by construction, so their concatenation is too, and there is nothing
/// for the `InvalidText` fault to check. Since ADR-111 `praxis_alloc_text` is
/// `Allocates` on the same footing — every wrapper here trusts its caller about
/// encoding, and the one place that cannot (`praxis_get_input`, which holds the
/// host's raw bytes) validates and faults there.
///
/// The result is `Owned` and never a `Slice`: a concatenation has no single
/// owner to point into, and a slice of one would be a lie about its extent.
///
/// # Safety
/// `ctx` must be live and wired; `a` and `b` must be valid `Text` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_text_concat(ctx: *mut RuntimeContext, a: GcRef, b: GcRef) -> GcRef {
    abi_guard!("praxis_text_concat", ctx, {
        // SAFETY: caller guarantees both are Text.
        let left = unsafe { text_str(a) };
        let right = unsafe { text_str(b) };
        let mut joined = String::with_capacity(left.len() + right.len());
        joined.push_str(left);
        joined.push_str(right);
        // SAFETY: TextPayload matches TEXT's size/align and is fully initialized.
        unsafe { text_ref(ctx, joined) }
    })
}

/// Render `value` into a fresh `Text`, **exactly as `out` renders it** (§8.1,
/// ADR-147).
///
/// This is the whole of an interpolation hole. `"{v}"` on a `Vec[Int]` is
/// `[1, 2, 3]` because this function and [`praxis_write_stdout`] are the same
/// two lines with a different destination: both call [`GcRef::format`], which
/// dispatches through the value's type descriptor. There is no second renderer
/// here and there must never be one — writing a `write!` inline instead of
/// calling `format` is the mistake this wrapper exists to make unnecessary, and
/// it is the mistake ADR-143 decision 2 records for the three scalar rows.
///
/// That is also why a hole may hold **any** type (ADR-147 decision 2). Every
/// `GcRef` has a descriptor and every descriptor has a `format` callback, so
/// there is no value this can be handed that it cannot render — which is what
/// lets inference impose no requirement on a hole at all.
///
/// Declared `Allocates`, never `AllocatesAndFaults`: nothing above can fail, and
/// a `String` built by `format` is valid UTF-8 by construction, so there is
/// nothing for an `InvalidText` fault to check. That is `praxis_text_concat`'s
/// row exactly.
///
/// # Safety
/// `ctx` must be live and wired; `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_value_to_text(ctx: *mut RuntimeContext, value: GcRef) -> GcRef {
    abi_guard!("praxis_value_to_text", ctx, {
        let mut s = String::new();
        value.format(&mut s);
        // SAFETY: `s` is valid UTF-8; ctx/heap valid.
        unsafe { text_ref(ctx, s) }
    })
}

/// The `Char` at `index`, or an `IndexOutOfBounds` fault if out of range
/// (ADR-086). `index` counts Unicode scalar values, not bytes.
///
/// # Safety
/// `ctx` must be live and wired; `text` must be a valid `Text` `GcRef`; `index`
/// must be a valid `Int` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_text_get(
    ctx: *mut RuntimeContext,
    text: GcRef,
    index: GcRef,
) -> GcRef {
    abi_guard!("praxis_text_get", ctx, {
        // SAFETY: caller guarantees `text` is Text.
        let payload = unsafe { text_payload(text) };
        // SAFETY: caller guarantees `index` is a valid Int.
        let idx = unsafe { int_payload(index) };
        if idx < 0 {
            unsafe { set_fault(ctx, RaisedFault::INDEX_OUT_OF_BOUNDS) };
            return unsafe { unit_sentinel(ctx) };
        }
        // **The byte index is the character index exactly when every scalar is
        // one byte, and `text_ascii_bytes` answers that in O(1)** (ADR-115).
        // The fallback is `chars().nth(i)`, which is O(i): a multi-byte text
        // has no random access without either a wider representation or a
        // cursor, and ADR-115 declines the cursor with its arithmetic. `idx` is
        // non-negative above, so the `as usize` cannot wrap.
        // SAFETY: caller guarantees `text` is Text.
        if let Some(bytes) = unsafe { crate::text::text_ascii_bytes(payload) } {
            return match bytes.get(idx as usize) {
                // One-byte scalars are exactly the ASCII range, so the byte
                // *is* the code point (§4.3, ADR-086).
                Some(&b) => unsafe { char_ref(ctx, u32::from(b)) },
                None => {
                    unsafe { set_fault(ctx, RaisedFault::INDEX_OUT_OF_BOUNDS) };
                    unsafe { unit_sentinel(ctx) }
                }
            };
        }
        // SAFETY: caller guarantees `text` is Text.
        let s = unsafe { text_str(text) };
        match s.chars().nth(idx as usize) {
            Some(ch) => {
                // No validity check, and none belongs here: `ch` is a Rust `char`,
                // so `ch as u32` is a valid Unicode scalar by construction. The
                // check `praxis_int_to_char` needs is for the values that did not
                // come from one — which is why this goes to `char_ref` directly
                // rather than through `checked_alloc_char`.
                //
                // This is the interning's largest site (ADR-107): the same call
                // is `t[i]` and every step of `for c in t` (the `iter_plan`
                // lowering), so a program that walks a line of ASCII text would
                // otherwise box one object per character.
                unsafe { char_ref(ctx, ch as u32) }
            }
            None => {
                unsafe { set_fault(ctx, RaisedFault::INDEX_OUT_OF_BOUNDS) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

// ---------------------------------------------------------------------------
// `out(...)` — write a value to stdout followed by a newline (§16.1).
// ---------------------------------------------------------------------------

/// Format `value` through its descriptor and write it to stdout followed by a
/// newline. Returns the Unit sentinel (§4.3), matching `out`'s `(T) -> Unit`
/// type. Never faults.
///
/// # Safety
/// `ctx` must be live and wired; `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_write_stdout(ctx: *mut RuntimeContext, value: GcRef) -> GcRef {
    abi_guard!("praxis_write_stdout", ctx, {
        use std::io::Write;
        let mut out = String::new();
        value.format(&mut out);
        let _ = std::io::stdout().write_all(out.as_bytes());
        let _ = std::io::stdout().write_all(b"\n");
        // `out` is `(T) -> Unit`: return the Unit sentinel so a Unit-typed value
        // flows out, not the printed argument (which would otherwise leak as the
        // function's result and be printed a second time by the host).
        unsafe { unit_sentinel(ctx) }
    })
}

// ---------------------------------------------------------------------------
// `dbg(...)`, `panic(...)`, `assert(...)` — the rest of §16.1's control names.
// ---------------------------------------------------------------------------

/// Format `value` through its descriptor, write it to stderr followed by a
/// newline, and hand **the same reference back** (§8.1). `dbg` is `forall T.
/// (T) -> T`, so it can be wrapped around any subexpression without changing
/// what the program computes. Never faults, never allocates.
///
/// # Safety
/// `ctx` must be live and wired; `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_dbg(_ctx: *mut RuntimeContext, value: GcRef) -> GcRef {
    abi_guard!("praxis_dbg", _ctx, {
        use std::io::Write;
        let mut rendered = String::new();
        value.format(&mut rendered);
        let _ = std::io::stderr().write_all(rendered.as_bytes());
        let _ = std::io::stderr().write_all(b"\n");
        value
    })
}

/// Record `value` as the fault message and raise [`FaultKind::Panic`] (§9.1).
///
/// The message is rendered **here**, through the value's descriptor, exactly as
/// `out` renders its argument. It has to be: the host reads the message after
/// the heap the `GcRef` points into has been torn down, so a stored reference
/// would outlive what it names.
///
/// Returns the Unit sentinel. `panic` is `forall T. (T) -> Never`, so no caller
/// can use the result — but the ABI returns a `GcRef` on every path, and a
/// fault epilogue needs a defined value to carry out (§10.4).
///
/// # Safety
/// `ctx` must be live and wired; `value` must be a valid `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_panic(ctx: *mut RuntimeContext, value: GcRef) -> GcRef {
    abi_guard!("praxis_panic", ctx, {
        let mut message = String::new();
        value.format(&mut message);
        unsafe { set_fault_message(ctx, message) };
        unsafe { set_fault(ctx, RaisedFault::PANIC) };
        unsafe { unit_sentinel(ctx) }
    })
}

/// Raise [`FaultKind::AssertFailed`] when `condition` is false (§9.1), and do
/// nothing at all when it is true.
///
/// `assert` is `(Bool) -> Unit`, so the argument is one of the two `Bool`
/// immortals and reading its payload needs no descriptor check.
///
/// It sets **no** message: `assert` takes a condition and nothing else, so the
/// only text available would restate the fault kind. `panic` is the name that
/// carries words.
///
/// # Safety
/// `ctx` must be live and wired; `condition` must be a valid `Bool` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_assert(ctx: *mut RuntimeContext, condition: GcRef) -> GcRef {
    abi_guard!("praxis_assert", ctx, {
        // SAFETY: `assert`'s scheme is `(Bool) -> Unit`, so the argument is a Bool.
        if !unsafe { crate::immortal::read_bool(condition) } {
            unsafe { set_fault(ctx, RaisedFault::ASSERT_FAILED) };
        }
        unsafe { unit_sentinel(ctx) }
    })
}

// ---------------------------------------------------------------------------
// `Range` (§4.11, ADR-059).
//
// `a..b` and `a..=b` are two symbols rather than one symbol with a flag: the
// choice is already a syntactic fact the MIR builder holds, and a boolean
// smuggled through an `i64` parameter would have 2^64 spellings for two states.
// Both bounds arrive as `Int` `GcRef`s, because a bound is an arbitrary
// expression and every other wrapper takes its operands boxed.
// ---------------------------------------------------------------------------

/// Build the half-open range `start..end` (§4.11). A descending range is
/// **empty** — [`RangeVal::new`](crate::range::RangeVal::new) normalizes it, so
/// no range with a negative length exists.
///
/// # Safety
/// `ctx` must be live and wired; both bounds must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_range_new(
    ctx: *mut RuntimeContext,
    start: GcRef,
    end: GcRef,
) -> GcRef {
    abi_guard!("praxis_range_new", ctx, {
        let a = unsafe { int_payload(start) };
        let b = unsafe { int_payload(end) };
        unsafe {
            gc_alloc(
                ctx,
                crate::range::RANGE_PAYLOAD,
                crate::range::RangeVal::new(a, b),
            )
        }
    })
}

/// Build the inclusive range `start..=end` (§4.11).
///
/// # Safety
/// `ctx` must be live and wired; both bounds must be valid `Int` `GcRef`s.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_range_new_inclusive(
    ctx: *mut RuntimeContext,
    start: GcRef,
    end: GcRef,
) -> GcRef {
    abi_guard!("praxis_range_new_inclusive", ctx, {
        let a = unsafe { int_payload(start) };
        let b = unsafe { int_payload(end) };
        unsafe {
            gc_alloc(
                ctx,
                crate::range::RANGE_PAYLOAD,
                crate::range::RangeVal::new_inclusive(a, b),
            )
        }
    })
}

/// The number of integers in a range (§4.11) — what a `for` loop reads to
/// bound itself.
///
/// **Faults when the count does not fit an `Int`.** Only the very widest ranges
/// reach it (`Int::MIN..Int::MAX` holds `2^64 - 1` integers), and reporting a
/// wrapped negative length instead would be a `for` loop that ran zero times
/// over every integer there is.
///
/// The kind is `IntOverflow`, which is what `gcd`, `lcm` and A\*'s path cost
/// already answer for a result with no `Int`. It is deliberately not
/// `EmptyRange`: the range this fires on is the *fullest* one there is, so that
/// message would lie about it (ADR-059, ADR-075).
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Range` `GcRef`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_range_len(ctx: *mut RuntimeContext, r: GcRef) -> GcRef {
    abi_guard!("praxis_range_len", ctx, {
        // SAFETY: the compiler only emits this with a Range-typed operand.
        let range = unsafe { &*r.payload::<crate::range::RangeVal>() };
        match i64::try_from(range.len()) {
            Ok(len) => unsafe { int_ref(ctx, len) },
            Err(_) => {
                unsafe { set_fault(ctx, RaisedFault::INT_OVERFLOW) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

/// The `index`-th integer of a range (§4.11). Faults when `index` is outside
/// it, exactly as `Vec.get` does.
///
/// # Safety
/// `ctx` must be live and wired; `r` must be a valid `Range` `GcRef` and
/// `index` a valid `Int` one.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_range_get(
    ctx: *mut RuntimeContext,
    r: GcRef,
    index: GcRef,
) -> GcRef {
    abi_guard!("praxis_range_get", ctx, {
        // SAFETY: the compiler only emits this with a Range-typed receiver.
        let range = unsafe { &*r.payload::<crate::range::RangeVal>() };
        let i = unsafe { int_payload(index) };
        match range.get(i) {
            Some(value) => unsafe { int_ref(ctx, value) },
            None => {
                unsafe { set_fault(ctx, RaisedFault::INDEX_OUT_OF_BOUNDS) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

// ---------------------------------------------------------------------------
// Input parser (§7).
//
// `read` / `parse` lower to runtime calls that fetch the input buffer and run
// a compiled parser plan against it. The plan is compiled at HIR time and
// registered in a global slab; its index is passed as a boxed Int.
// ---------------------------------------------------------------------------

/// Return the process-input source buffer (§7.10), reading it the **first**
/// time a program asks.
///
/// A `read` lowers to this call and then to `praxis_run_parser`, so this is
/// where §7.10's "the first `read` lazily reads standard input once" happens.
/// The host installs a [`crate::input::InputReader`] rather than a buffer; it
/// is called at most once — [`crate::input::take_input_reader`] removes it, so
/// "once" is structural rather than a flag — and the result is installed as
/// `input_source`, which every later `read` reuses.
///
/// Nothing before a program's first `read` touches the host's input. Reading it
/// up front would make a program with no `read` in it still consume standard
/// input, so `praxis run` against an open pipe would block forever.
///
/// A host that installs no reader — every JIT test, and the crash debugger's
/// re-run path, which installs the buffer directly to keep re-runs identical
/// (§9.7) — reaches the plain `input_source` read below.
///
/// **A reader that answers zero bytes has given empty input, not no input.** Its
/// answer is installed as `input_source` whatever its length, so `read` runs
/// against a zero-length buffer and the parser constructors answer from their own
/// rules — `lines(int)` over it is `[]` by `split_lines`'s rule, and one that
/// requires content faults at `0..0` naming what it expected. That is what §7.11
/// asks a mismatch to carry, and a fault raised before any buffer existed can
/// carry none of it: it has no input span to name. A zero-byte `--input` file is
/// the same decision, made at `praxis-cli/src/run.rs` (ADR-087).
///
/// The one remaining Unit-source state belongs to a host that installs **neither**
/// a buffer nor a reader — every JIT test, every embedder. `praxis_run_parser`'s
/// descriptor guard (§6.3) is what keeps that state survivable; no `praxis run`
/// reaches it.
///
/// **This wrapper owns the UTF-8 judgement, and it is the only producer of
/// [`FaultKind::InvalidText`](crate::FaultKind::InvalidText)** (ADR-111). A
/// reader's bytes are the host's, not the compiler's, so they are checked here
/// and `INVALID_TEXT` is raised here — where `lower_read`'s `CheckFault` makes
/// it divert at the `read`. Raising it inside `praxis_alloc_text` instead would
/// cost a check after every text *literal* for a fault a literal cannot
/// produce; that wrapper trusts its caller, and this is the caller that has to
/// earn the trust.
///
/// `praxis run` cannot reach the fault: `lazy_stdin::read` goes through
/// `std::io::read_to_string` and exits 2 on non-UTF-8 stdin before the runtime
/// sees a byte. An embedder installing its own reader can.
///
/// # Safety
/// `ctx` must be live and wired.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_get_input(ctx: *mut RuntimeContext) -> GcRef {
    abi_guard!("praxis_get_input", ctx, {
        if let Some(read) = crate::input::take_input_reader() {
            let bytes = read();
            // **This is the one place in the runtime that holds raw host bytes,
            // so it is the one place the UTF-8 judgement §4.3 assigns belongs**
            // (ADR-111). Here the fault is real: a host's `InputReader` is
            // infallible about I/O by design (`crate::input`) and says nothing
            // about encoding, so these bytes are exactly as trustworthy as the
            // host. `GetInput`'s row is `AllocatesAndFaults` and `lower_read`
            // emits the check, so `InvalidText` diverts *at the `read`*.
            //
            // The Unit sentinel is the defined dummy (§10.4); `input_source`
            // holds it until a buffer is installed, so answering it below is
            // the same value by a shorter route.
            let Ok(text) = std::str::from_utf8(&bytes) else {
                unsafe { set_fault(ctx, RaisedFault::INVALID_TEXT) };
                return unsafe { (*ctx).input_source };
            };
            // **The validation is strictly before the allocation, and must
            // stay there.** SAFETY: `text` borrows a live, initialized buffer
            // for this call, and `ctx` is the caller's live context. The result
            // is stored into `input_source` — a root (`RuntimeRoots`) — with no
            // allocation in between, so the collection this allocation paces
            // cannot reclaim it. `praxis_alloc_text` takes `&[]` for
            // `len == 0`, so the empty answer needs no special case here and
            // must not get one.
            let text = unsafe { praxis_alloc_text(ctx, text.as_ptr(), text.len()) };
            unsafe { (*ctx).input_source = text };
        }
        unsafe { (*ctx).input_source }
    })
}

/// Run a compiled parser plan against `input`, returning the parsed result as a
/// `GcRef` (§7.1). `plan_index_gc` is a boxed `Int` whose payload is the
/// plan's index in the HIR's global slab.
///
/// On a parse mismatch (or a non-Text `input`), sets `FaultKind::ParseFailed`
/// and returns the Unit sentinel (§7.11). No Rust panic crosses the ABI.
///
/// The non-Text guard is load-bearing (§6.3 host-safety gap): the parser
/// interpreter reinterprets `input`'s payload as a `TextPayload`, so a non-Text
/// `input` (e.g. the default Unit singleton when no input buffer was installed)
/// would be dereferenced as a Text buffer and segfault. Both `read` (whose
/// `input` comes from `praxis_get_input`) and `parse(text, expr)` (whose `input`
/// is an arbitrary expression) funnel through here, so guarding at this ABI
/// boundary closes the gap regardless of how the input was produced.
///
/// The guard **clears** the parse detail and records none of its own. It runs no
/// parse, so it has nothing to report — and fabricating a [`ParseFail`] there
/// would be worse than silence: with no buffer there is no input span, and an
/// invented `expected` would make an embedder's host bug read as a parse failure
/// at an offset that does not exist. Clearing is also what stops it reporting a
/// *previous* parse's offset: this is the one entry into the parser that does
/// not go through `run_plan`'s own clear.
///
/// # Safety
/// `ctx` must be live and wired; `plan_index_gc` must be a valid `Int`; `input`
/// must be a valid `GcRef` (any descriptor — a non-Text descriptor faults cleanly
/// rather than dereferencing garbage).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_run_parser(
    ctx: *mut RuntimeContext,
    plan_index_gc: GcRef,
    input: GcRef,
) -> GcRef {
    abi_guard!("praxis_run_parser", ctx, {
        // Guard the parser interpreter against a non-Text input (§6.3). Reaching
        // `run_plan` with a non-Text payload would reinterpret foreign bytes as a
        // TextPayload and segfault; fault cleanly instead.
        if input.descriptor().id() != crate::text::TEXT.id() {
            unsafe { crate::parser::clear_parse_detail(ctx) };
            unsafe { set_fault(ctx, RaisedFault::PARSE_FAILED) };
            return unsafe { unit_sentinel(ctx) };
        }
        let idx = unsafe { int_payload(plan_index_gc) };
        // Delegate to the parser interpreter. It validates the id, reads the
        // plan from the process-wide arena, runs it against the input bytes, and
        // allocates the result.
        // SAFETY: `ctx` is the wrapper's own context argument, and `input` was
        // checked to carry a Text payload above.
        match unsafe { crate::parser::run_plan_by_id(ctx, idx, input) } {
            Some(result) => result,
            None => {
                // A `None` return means the value named no registered plan (out of
                // range, negative, or zero) or the interpreter was not linked.
                // Treat as a parse fault.
                unsafe { set_fault(ctx, RaisedFault::PARSE_FAILED) };
                unsafe { unit_sentinel(ctx) }
            }
        }
    })
}

// ---------------------------------------------------------------------------
// Graph helpers (§6.5, ADR-060).
//
// Twelve prelude names whose graph is a closure: the caller passes a start
// state and a function from a state to its neighbours, and the wrapper walks
// whatever that function describes. `crate::graph` owns the walks and never
// touches a closure; `ClosureOracle` below is the one thing that does.
//
// The bare name is the whole walk, `_distance` is the number of the cheapest
// route to a goal, and `_path` is the route. The two goal-directed forms of a
// family share one walk and project the `cost` or the `states` out of the one
// `graph::Route` it answers, so they can never disagree about which route was
// found.
// ---------------------------------------------------------------------------

/// A [`GraphOracle`](crate::graph::GraphOracle) backed by the closures a
/// program passed, with every state it is handed rooted in a native frame.
///
/// The scope is what makes the walks safe: a state lives in a Rust visited set
/// or queue, which the collector cannot see, and every closure call may
/// allocate. `retain` roots each state the moment the walk decides to remember
/// it, so a collection triggered inside the *next* call finds it.
struct ClosureOracle<'s, 'c> {
    ctx: *mut RuntimeContext,
    scope: &'s NativeScope<'c>,
    /// `(T) -> Vec[T]`.
    neighbours: GcRef,
    /// `(T, T) -> Int`, or the Unit sentinel for a helper that has no weights.
    weight: GcRef,
    /// `(T) -> Int`, or the Unit sentinel for a helper with no heuristic.
    heuristic: GcRef,
    /// `(T) -> Bool`, or the Unit sentinel for a helper with no goal test.
    goal: GcRef,
}

impl ClosureOracle<'_, '_> {
    /// Call `closure` with `args`, or `Err` if it faulted — or if it is not a
    /// closure at all.
    ///
    /// The type checker says every one of these operands is a function, and the
    /// only runtime representation of a function value is a closure object. The
    /// descriptor is checked anyway: the alternative to a `TypeMismatch` fault
    /// is transmuting whatever the payload's first word happens to be into a
    /// function pointer and jumping to it.
    unsafe fn call(
        &mut self,
        closure: GcRef,
        args: &[GcRef],
    ) -> Result<GcRef, crate::graph::Aborted> {
        if !std::ptr::eq(closure.descriptor(), &crate::closures::CLOSURE) {
            return Err(self.abort(crate::context::FaultKind::TypeMismatch));
        }
        // SAFETY: the descriptor check above proves the payload is a
        // `ClosurePayload`, so `fn_ptr` is the entry point the codegen wrote
        // there (`praxis_alloc_closure`).
        let fn_ptr = unsafe { (*closure.payload::<crate::closures::ClosurePayload>()).fn_ptr };
        // A closure's entry point is `fn(ctx, closure_self, params...) -> GcRef`
        // (§4.10, Approach B): the closure value itself is a hidden first
        // explicit argument, and the prologue loads its captures from it. The
        // arity is fixed by the helper's signature, which inference has already
        // checked, so only the shapes the six helpers use exist here.
        let result = match args {
            // SAFETY: `fn_ptr` is a finalized JIT entry whose parameter count is
            // the one the type checker enforced for this operand; every value
            // crossing is a `GcRef`, which is the ABI's only value kind.
            [a] => unsafe {
                let f: unsafe extern "C" fn(*mut RuntimeContext, GcRef, GcRef) -> GcRef =
                    std::mem::transmute(fn_ptr);
                f(self.ctx, closure, *a)
            },
            // SAFETY: as above, at the two-parameter shape.
            [a, b] => unsafe {
                let f: unsafe extern "C" fn(*mut RuntimeContext, GcRef, GcRef, GcRef) -> GcRef =
                    std::mem::transmute(fn_ptr);
                f(self.ctx, closure, *a, *b)
            },
            // Unreachable: `GraphParam` has no shape with another arity, and the
            // match on it in `seed_builtin_schemes` is exhaustive. Faulting is
            // still the only safe answer, because the alternative is calling
            // with the wrong number of arguments.
            _ => return Err(self.abort(crate::context::FaultKind::TypeMismatch)),
        };
        // The closure ran arbitrary Praxis code and may have faulted. Its result
        // on that path is the Unit sentinel, so continuing would walk a graph of
        // Units; stop instead, leaving the fault for the call site's own check.
        if unsafe { praxis_check_fault(self.ctx) } != 0 {
            return Err(crate::graph::Aborted);
        }
        Ok(self.scope.root(result).get())
    }

    /// The `i64` inside a boxed `Int` a closure returned, or a fault if it is
    /// not one.
    unsafe fn int_result(&mut self, value: GcRef) -> Result<i64, crate::graph::Aborted> {
        if !std::ptr::eq(value.descriptor(), &scalars::INT) {
            return Err(self.abort(crate::context::FaultKind::TypeMismatch));
        }
        Ok(unsafe { int_payload(value) })
    }
}

impl crate::graph::GraphOracle for ClosureOracle<'_, '_> {
    fn neighbours(&mut self, state: GcRef) -> Result<Vec<GcRef>, crate::graph::Aborted> {
        // SAFETY: `ctx` is live for the wrapper's duration and `neighbours` is
        // the operand the type checker typed `(T) -> Vec[T]`.
        let result = unsafe { self.call(self.neighbours, &[state])? };
        if !std::ptr::eq(result.descriptor(), &crate::collections::VEC) {
            return Err(self.abort(crate::context::FaultKind::TypeMismatch));
        }
        // SAFETY: the descriptor check proves the payload is a `VecPayload`, and
        // the result is rooted by `call`, so reading its items cannot race a
        // collection — nothing allocates between here and the copy.
        let items = unsafe { (*result.payload::<VecPayload>()).items.to_vec() };
        for item in &items {
            self.scope.root(*item);
        }
        Ok(items)
    }

    fn weight(&mut self, from: GcRef, to: GcRef) -> Result<i64, crate::graph::Aborted> {
        // SAFETY: as above, at the `(T, T) -> Int` operand.
        let result = unsafe { self.call(self.weight, &[from, to])? };
        // SAFETY: `result` is a live, rooted `GcRef`.
        unsafe { self.int_result(result) }
    }

    fn heuristic(&mut self, state: GcRef) -> Result<i64, crate::graph::Aborted> {
        // SAFETY: as above, at the `(T) -> Int` operand.
        let result = unsafe { self.call(self.heuristic, &[state])? };
        // SAFETY: `result` is a live, rooted `GcRef`.
        unsafe { self.int_result(result) }
    }

    fn is_goal(&mut self, state: GcRef) -> Result<bool, crate::graph::Aborted> {
        // SAFETY: as above, at the `(T) -> Bool` operand.
        let result = unsafe { self.call(self.goal, &[state])? };
        // A `Bool`'s payload is **one byte**. Reading it as an `i64` would take
        // seven further bytes of the block's alignment padding, which the bump
        // allocator never initialized. `read_scalar` checks the descriptor and
        // takes the width from `BOOL_PAYLOAD`'s own type, so neither half is
        // written here.
        //
        // SAFETY: `result` is a `GcRef` the oracle's own call just produced.
        match unsafe { read_scalar(result, scalars::BOOL_PAYLOAD) } {
            Some(b) => Ok(b != 0),
            None => Err(self.abort(crate::context::FaultKind::TypeMismatch)),
        }
    }

    fn retain(&mut self, state: GcRef) {
        self.scope.root(state);
    }

    fn abort(&mut self, kind: crate::context::FaultKind) -> crate::graph::Aborted {
        if let Some(fault) = RaisedFault::new(kind) {
            // SAFETY: `ctx` is live and wired for the wrapper's duration.
            unsafe { set_fault(self.ctx, fault) };
        }
        crate::graph::Aborted
    }
}

/// The descriptor every state in this walk shares: the start state's own.
///
/// The type checker guarantees one state type per call, and a `GcRef` carries
/// its descriptor in its header — so the start state is the authority on what
/// the result collection holds, and no separate type argument has to cross the
/// ABI.
#[inline]
fn state_descriptor(start: GcRef) -> *const TypeDescriptor {
    start.descriptor() as *const TypeDescriptor
}

/// Build a `Vec[T]` holding `states`, in order.
///
/// # Safety
/// `ctx` must be live and wired; every state must be a valid, rooted `GcRef`.
unsafe fn states_as_vec(
    ctx: *mut RuntimeContext,
    element: *const TypeDescriptor,
    states: &[GcRef],
) -> GcRef {
    let result = unsafe { praxis_vec_new(ctx, element) };
    let scope = unsafe { NativeScope::new(ctx) };
    let rooted = scope.root(result);
    // SAFETY: `result` is the `Vec` just allocated, and `rooted` proves it is in
    // the collector's root set for the borrow.
    let payload = unsafe { vec_payload_mut(rooted) };
    payload.items.extend_from_slice(states);
    result
}

/// `bfs(start, neighbours)` — every reachable state, in breadth-first order
/// (§6.5).
///
/// # Safety
/// `ctx` must be live and wired; `start` must be a valid `GcRef` and
/// `neighbours` a closure value of type `(T) -> Vec[T]`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bfs(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
) -> GcRef {
    abi_guard!("praxis_bfs", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight: unit_sentinel(ctx),
                heuristic: unit_sentinel(ctx),
                goal: unit_sentinel(ctx),
            };
            match crate::graph::bfs_order(&mut oracle, start) {
                Ok(states) => states_as_vec(ctx, state_descriptor(start), &states),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `dfs(start, neighbours)` — every reachable state, in depth-first pre-order
/// (§6.5).
///
/// # Safety
/// As [`praxis_bfs`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_dfs(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
) -> GcRef {
    abi_guard!("praxis_dfs", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight: unit_sentinel(ctx),
                heuristic: unit_sentinel(ctx),
                goal: unit_sentinel(ctx),
            };
            match crate::graph::dfs_order(&mut oracle, start) {
                Ok(states) => states_as_vec(ctx, state_descriptor(start), &states),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `flood_fill(start, neighbours)` — every reachable state, as a `Set` (§6.5).
///
/// # Safety
/// As [`praxis_bfs`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_flood_fill(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
) -> GcRef {
    abi_guard!("praxis_flood_fill", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight: unit_sentinel(ctx),
                heuristic: unit_sentinel(ctx),
                goal: unit_sentinel(ctx),
            };
            let states = match crate::graph::reachable(&mut oracle, start) {
                Ok(states) => states,
                Err(_) => return unit_sentinel(ctx),
            };
            let result = praxis_set_new(ctx, state_descriptor(start));
            let rooted = scope.root(result);
            let payload = set_payload_mut(rooted);
            for state in states {
                payload.entries.insert(DynamicKey::new(state));
            }
            result
        }
    })
}

/// `bfs_distance(start, neighbours, is_goal)` — the fewest steps to a goal, or
/// `None` (§6.5).
///
/// # Safety
/// `ctx` must be live and wired; `start` must be a valid `GcRef`, `neighbours` a
/// `(T) -> Vec[T]` closure and `goal` a `(T) -> Bool` closure.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bfs_distance(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    goal: GcRef,
) -> GcRef {
    abi_guard!("praxis_bfs_distance", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight: unit_sentinel(ctx),
                heuristic: unit_sentinel(ctx),
                goal,
            };
            match crate::graph::bfs_route(&mut oracle, start) {
                Ok(route) => alloc_optional_int(ctx, route.map(|r| r.cost)),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `bfs_path(start, neighbours, is_goal)` — a shortest route to a goal, from
/// the start to the goal inclusive, or `None` (§6.5).
///
/// The same walk [`praxis_bfs_distance`] runs; this projects the route out of
/// its answer where that one projects the number.
///
/// # Safety
/// As [`praxis_bfs_distance`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_bfs_path(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    goal: GcRef,
) -> GcRef {
    abi_guard!("praxis_bfs_path", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight: unit_sentinel(ctx),
                heuristic: unit_sentinel(ctx),
                goal,
            };
            match crate::graph::bfs_route(&mut oracle, start) {
                Ok(route) => states_as_optional_vec(
                    ctx,
                    state_descriptor(start),
                    route.as_ref().map(|r| r.states.as_slice()),
                ),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `dfs_distance(start, neighbours, is_goal)` — the number of edges on the
/// route depth-first search found to a goal, or `None` (§6.5).
///
/// Depth-first arrives by the route it descended into first, which need not be
/// a short one, so this and [`praxis_bfs_distance`] answer different numbers on
/// the same graph.
///
/// # Safety
/// As [`praxis_bfs_distance`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_dfs_distance(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    goal: GcRef,
) -> GcRef {
    abi_guard!("praxis_dfs_distance", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight: unit_sentinel(ctx),
                heuristic: unit_sentinel(ctx),
                goal,
            };
            match crate::graph::dfs_route(&mut oracle, start) {
                Ok(route) => alloc_optional_int(ctx, route.map(|r| r.cost)),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `dfs_path(start, neighbours, is_goal)` — the route depth-first search found
/// to a goal, from the start to the goal inclusive, or `None` (§6.5).
///
/// # Safety
/// As [`praxis_bfs_distance`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_dfs_path(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    goal: GcRef,
) -> GcRef {
    abi_guard!("praxis_dfs_path", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight: unit_sentinel(ctx),
                heuristic: unit_sentinel(ctx),
                goal,
            };
            match crate::graph::dfs_route(&mut oracle, start) {
                Ok(route) => states_as_optional_vec(
                    ctx,
                    state_descriptor(start),
                    route.as_ref().map(|r| r.states.as_slice()),
                ),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `dijkstra(start, neighbours, weight)` — the least cost to every reachable
/// state, as a `Map[T, Int]` (§6.5).
///
/// # Safety
/// `ctx` must be live and wired; `start` must be a valid `GcRef`, `neighbours` a
/// `(T) -> Vec[T]` closure and `weight` a `(T, T) -> Int` closure.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_dijkstra(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    weight: GcRef,
) -> GcRef {
    abi_guard!("praxis_dijkstra", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight,
                heuristic: unit_sentinel(ctx),
                goal: unit_sentinel(ctx),
            };
            let costs = match crate::graph::dijkstra_costs(&mut oracle, start) {
                Ok(costs) => costs,
                Err(_) => return unit_sentinel(ctx),
            };
            let result = scope.root(praxis_map_new(ctx, state_descriptor(start)));
            for (state, cost) in costs {
                // Each boxed cost is allocated *before* the payload borrow: an
                // allocation while a `&mut MapPayload` is live is what `Rooted`
                // exists to make impossible, and taking the borrow per entry is
                // what keeps that true.
                let boxed = scope.root(int_ref(ctx, cost));
                map_payload_mut(result)
                    .entries
                    .insert(DynamicKey::new(state), boxed.get());
            }
            result.get()
        }
    })
}

/// `dijkstra_distance(start, neighbours, weight, is_goal)` — the cost of the
/// cheapest route to a goal, or `None` (§6.5).
///
/// The search [`praxis_dijkstra`] runs, stopped at the first goal it settles,
/// so it makes the same two refusals: a negative edge weight and a cost with no
/// `Int`.
///
/// # Safety
/// `ctx` must be live and wired; `start` must be a valid `GcRef`, `neighbours` a
/// `(T) -> Vec[T]` closure, `weight` a `(T, T) -> Int` closure and `goal` a
/// `(T) -> Bool` closure.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_dijkstra_distance(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    weight: GcRef,
    goal: GcRef,
) -> GcRef {
    abi_guard!("praxis_dijkstra_distance", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight,
                heuristic: unit_sentinel(ctx),
                goal,
            };
            match crate::graph::dijkstra_route(&mut oracle, start) {
                Ok(route) => alloc_optional_int(ctx, route.map(|r| r.cost)),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `dijkstra_path(start, neighbours, weight, is_goal)` — the cheapest route to
/// a goal, from the start to the goal inclusive, or `None` (§6.5).
///
/// # Safety
/// As [`praxis_dijkstra_distance`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_dijkstra_path(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    weight: GcRef,
    goal: GcRef,
) -> GcRef {
    abi_guard!("praxis_dijkstra_path", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight,
                heuristic: unit_sentinel(ctx),
                goal,
            };
            match crate::graph::dijkstra_route(&mut oracle, start) {
                Ok(route) => states_as_optional_vec(
                    ctx,
                    state_descriptor(start),
                    route.as_ref().map(|r| r.states.as_slice()),
                ),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `a_star_distance(start, neighbours, weight, heuristic, is_goal)` — the cost
/// of the cheapest route to a goal, or `None` (§6.5).
///
/// # Safety
/// `ctx` must be live and wired; `start` must be a valid `GcRef` and each of
/// `neighbours`, `weight`, `heuristic` and `goal` a closure value of the type
/// the helper's signature declares.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_a_star_distance(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    weight: GcRef,
    heuristic: GcRef,
    goal: GcRef,
) -> GcRef {
    abi_guard!("praxis_a_star_distance", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight,
                heuristic,
                goal,
            };
            match crate::graph::a_star_route(&mut oracle, start) {
                Ok(route) => alloc_optional_int(ctx, route.map(|r| r.cost)),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// `a_star_path(start, neighbours, weight, heuristic, is_goal)` — the cheapest
/// route to a goal, from the start to the goal inclusive, or `None` (§6.5).
///
/// # Safety
/// As [`praxis_a_star_distance`].
#[unsafe(no_mangle)]
pub unsafe extern "C" fn praxis_a_star_path(
    ctx: *mut RuntimeContext,
    start: GcRef,
    neighbours: GcRef,
    weight: GcRef,
    heuristic: GcRef,
    goal: GcRef,
) -> GcRef {
    abi_guard!("praxis_a_star_path", ctx, {
        // SAFETY: the caller upholds ctx/operand validity.
        unsafe {
            let scope = NativeScope::new(ctx);
            let mut oracle = ClosureOracle {
                ctx,
                scope: &scope,
                neighbours,
                weight,
                heuristic,
                goal,
            };
            match crate::graph::a_star_route(&mut oracle, start) {
                Ok(route) => states_as_optional_vec(
                    ctx,
                    state_descriptor(start),
                    route.as_ref().map(|r| r.states.as_slice()),
                ),
                Err(_) => unit_sentinel(ctx),
            }
        }
    })
}

/// Allocate `Some(n)` or `None` for an `Option[Int]` result.
///
/// The tags are `Option`'s own declaration order — `Some` first, `None` second
/// (`TypeDb::new`) — which is the same order the codegen uses for a `Some(x)`
/// the program writes, so a runtime-built `Option` matches against the same
/// arms.
///
/// # Safety
/// `ctx` must be live and wired.
unsafe fn alloc_optional_int(ctx: *mut RuntimeContext, value: Option<i64>) -> GcRef {
    // SAFETY: the caller upholds ctx validity.
    unsafe {
        match value {
            Some(n) => {
                let boxed = int_ref(ctx, n);
                option_some(ctx, boxed)
            }
            None => option_none(ctx),
        }
    }
}

/// Allocate `Some(states)` as an `Option[Vec[T]]`, or `None` when the search
/// found no route.
///
/// The `Vec` is rooted before the `Some` is built: `option_some` allocates an
/// enum, an allocation is a safepoint, and a bare `GcRef` in a local is in
/// nobody's root set. That ordering is the whole of what this adds to
/// [`states_as_vec`] and [`option_some`].
///
/// # Safety
/// `ctx` must be live and wired; every state must be a valid, rooted `GcRef`.
unsafe fn states_as_optional_vec(
    ctx: *mut RuntimeContext,
    element: *const TypeDescriptor,
    states: Option<&[GcRef]>,
) -> GcRef {
    // SAFETY: the caller upholds ctx/state validity.
    unsafe {
        match states {
            Some(states) => {
                let scope = NativeScope::new(ctx);
                let vec = scope.root(states_as_vec(ctx, element, states));
                option_some(ctx, vec.get())
            }
            None => option_none(ctx),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::{Fault, FaultKind, Runtime};
    use crate::parse_detail::ParseFail;
    use crate::shadow_stack::{SlotCount, push_frame};

    /// A wired context backed by a real runtime.
    pub(super) fn wired_ctx(rt: &mut Runtime) -> *mut RuntimeContext {
        let ctx = Box::leak(Box::new(rt.context()));
        ctx as *mut RuntimeContext
    }

    pub(super) unsafe fn drop_ctx(ctx: *mut RuntimeContext) {
        // Reclaim the leaked Box. The runtime outlives this call in tests.
        let _ = unsafe { Box::from_raw(ctx) };
    }

    /// The first `Int` value the runtime does **not** intern.
    ///
    /// Every test below that detects a collection by watching the live registry
    /// *shrink* must allocate above this. An interned `Int` never enters the
    /// registry, so `praxis_alloc_int(ctx, 5)` in such a loop makes
    /// `after < before + 1` true on the first iteration and the test reports
    /// success without a collection ever having run — a false pass, which is
    /// strictly worse than the failure it replaces.
    const UNINTERNED: i64 = crate::small_int::SMALL_INT_MAX + 1;

    /// Allocate through a safepointed ABI wrapper until its pre-allocation
    /// collection causes the live registry to shrink. Returns the live count
    /// immediately after that wrapper allocates its result.
    unsafe fn allocate_until_automatic_collection(rt: &Runtime, ctx: *mut RuntimeContext) -> usize {
        let mut before = rt.heap().stats().live_count;
        for i in 0..10_000_i64 {
            // Above the interned range: see `UNINTERNED`.
            let _ = unsafe { praxis_alloc_int(ctx, UNINTERNED + i) };
            let after = rt.heap().stats().live_count;
            if after < before.saturating_add(1) {
                return after;
            }
            before = after;
        }
        panic!("automatic collection did not run after 10,000 allocations");
    }

    /// The version number this build declares.
    ///
    /// Named for the version rather than for any one change, because a version
    /// is a statement about a build and several changes share one bump. This
    /// pins the numeral so a build cannot ship a layout change without moving
    /// it.
    ///
    /// `gc::tests::the_folded_payload_offset_moved_at_v19_and_is_pinned_here`
    /// asserts the other direction, pinning the payload offset *to* a version
    /// number, so a layout change and the version that declares it cannot drift
    /// apart.
    #[test]
    fn version_is_twenty_for_the_batch_this_build_ships() {
        assert_eq!(RUNTIME_ABI_VERSION, 20);
    }

    #[test]
    fn assert_passes_within_a_single_build() {
        assert_abi_version();
    }

    /// [`int_payload`]'s width check must be a real branch, not a
    /// `debug_assert` — the read has to be bounded in the profile users
    /// actually run.
    ///
    /// `debug_assert_eq!` is compiled out of a release build, leaving
    /// `unsafe { *r.payload::<i64>() }` against a descriptor that may be zero
    /// bytes wide: an 8-byte out-of-bounds heap read, reachable from a program
    /// that passes `praxis check`.
    ///
    /// **This is a source gate on purpose, and it is the only kind that works
    /// here.** The defect is a difference *between profiles*, and `cargo test`
    /// builds exactly one of them — a behavioural test is green under
    /// `debug_assertions` whether the check is conditional or not. The companion
    /// below asserts the branch actually refuses; this asserts it is still
    /// *there* at `-O`.
    ///
    /// It reads the file rather than the function because there is nothing in a
    /// compiled artifact to ask. `every_no_mangle_wrapper_is_behind_the_panic_guard`
    /// is the same technique for the same reason.
    #[test]
    fn every_scalar_payload_read_goes_through_the_bounded_reader() {
        let source = include_str!("abi.rs");

        // 1. The reader itself checks before it reads, and the check is an
        //    ordinary branch — not a `debug_assert`, which compiles out of a
        //    release build. That distinction is the point: with the check
        //    compiled out, a `praxis check`-clean program does an out-of-bounds
        //    read where a debug build aborts cleanly.
        const SIGNATURE: &str = "unsafe fn read_scalar<T: Copy>(r: GcRef, handle: crate::descriptor::Payload<T>) -> Option<T> {";
        let at = source
            .find(SIGNATURE)
            .expect("`read_scalar`'s definition moved; this gate names it by signature");
        let body_start = at + SIGNATURE.len();
        let body_len = source[body_start..]
            .find("\n}")
            .expect("`read_scalar` has no closing brace in the first column");
        let body = &source[body_start..body_start + body_len];

        assert!(
            !body.contains("debug_assert"),
            "`read_scalar`'s type check is a `debug_assert`, which is compiled out of a \
             release build — and what is left is an unchecked read off a payload that may \
             be narrower (REP-56). Make it an ordinary branch.\nbody was:{body}"
        );
        assert!(
            body.contains("std::ptr::eq(r.descriptor(), handle.descriptor())"),
            "`read_scalar` no longer proves the value is the handle's type before reading \
             it (REP-37, REP-56).\nbody was:{body}"
        );

        // 2. Nothing else in this file reads a scalar payload directly. This
        //    is the half that matters: a gate that names one function can only
        //    ever gate that function, and every scalar reader needs bounding.
        //
        //    Scanned over the crate's own code only: `include_str!` hands us this
        //    test too, whose list below would otherwise match itself, and
        //    comments naming the pattern are describing it rather than doing it.
        let code: String = source[..source
            .find("#[cfg(test)]")
            .expect("abi.rs has no test module marker")]
            .lines()
            .filter(|l| !l.trim_start().starts_with("//"))
            .collect::<Vec<_>>()
            .join("\n");
        //
        //    The patterns are the *bare* calls, not the dereferenced ones:
        //    binding `r.payload::<f64>()` to a local and dereferencing it on the
        //    next line breaks the spelling `*r.payload::<f64>()` without
        //    breaking the defect. Forbidding the call means no phrasing of it
        //    passes. `payload::<u8>()` stays legal: it is how
        //    `read_scalar` itself reaches the bytes, and how every *compound*
        //    payload (record, tuple, closure) is reached — those are cast to a
        //    struct the descriptor already vouched for, not read at a width.
        for forbidden in [
            "r.payload::<i64>()",
            "r.payload::<f64>()",
            "r.payload::<u32>()",
            "r.payload::<bool>()",
        ] {
            assert!(
                !code.contains(forbidden),
                "a scalar payload is read directly as `{forbidden}` instead of through \
                 `read_scalar`, so its type is unchecked in release (REP-56). Route it \
                 through `read_scalar(r, scalars::…_PAYLOAD)` instead."
            );
        }

        // 3. And no Rust `bool` is ever materialized from a payload byte: a
        //    `bool` whose byte is not 0 or 1 is an *invalid value*, which is
        //    undefined behaviour independently of whether the read was in
        //    bounds. `BoolPayload` is a `u8` precisely so it never has to be.
        assert!(
            !code.contains("Payload<bool>") && !code.contains("read_scalar::<bool>"),
            "a `bool` is read straight out of a payload; read `scalars::BOOL_PAYLOAD` \
             (a `u8`) and compare it instead (REP-56)."
        );
    }

    /// **ADR-111.** `praxis_alloc_text`'s UTF-8 backstop is unconditional in
    /// every profile, and it never becomes an unchecked read.
    ///
    /// The same source-gate technique as
    /// [`every_scalar_payload_read_goes_through_the_bounded_reader`], for the
    /// same reason and against a sharper temptation. Making the row `Allocates`
    /// says the caller promises UTF-8; the next tidy-up reads that as licence to
    /// delete the check — either into a `debug_assert` (which compiles out of a
    /// release build, so debug aborts and release builds a `Box<str>` of
    /// non-UTF-8 bytes that `text_str` later hands out as a `&str`) or into
    /// `from_utf8_unchecked` (the same hole, with the check deleted rather than
    /// compiled out). Both give two profiles two answers, and `just ci` never
    /// builds the one users get.
    ///
    /// A behavioural test cannot see this: under `cfg(debug_assertions)` a
    /// `debug_assert` version passes every test the branch version passes.
    #[test]
    fn the_text_precondition_backstop_is_unconditional_in_every_profile() {
        let source = include_str!("abi.rs");
        const SIGNATURE: &str = "pub unsafe extern \"C\" fn praxis_alloc_text(";
        let at = source
            .find(SIGNATURE)
            .expect("`praxis_alloc_text`'s definition moved; this gate names it by signature");
        let body_len = source[at..]
            .find("\n}")
            .expect("`praxis_alloc_text` has no closing brace in the first column");
        let body = &source[at..at + body_len];

        assert!(
            body.contains("std::str::from_utf8(slice)"),
            "`praxis_alloc_text` no longer validates its buffer. The check is the \
             backstop on a raw read, not an optimization the `Allocates` row traded \
             away (ADR-111).\nbody was:{body}"
        );
        assert!(
            !body.contains("debug_assert"),
            "`praxis_alloc_text`'s UTF-8 check is a `debug_assert`, which is compiled \
             out of a release build — leaving a `Box<str>` built from bytes that are \
             not UTF-8 (REP-56's shape). Make it an ordinary branch.\nbody was:{body}"
        );
        assert!(
            !body.contains("from_utf8_unchecked"),
            "`praxis_alloc_text` skips the check outright. A precondition is not a \
             licence to read unvalidated bytes as a `str` — the refusal is \
             `text_bytes_are_not_utf8`, which costs a never-taken branch \
             (ADR-111).\nbody was:{body}"
        );
        // And the refusal is not a fault. If it were, the fault sweep would
        // classify the wrapper as faulting and correctly refuse the
        // `Allocates` row — this says so at the site rather than leaving the
        // failure to be diagnosed three tests away.
        assert!(
            !body.contains("set_fault"),
            "`praxis_alloc_text` sets a fault. Its row is `Effect::Allocates`, so no \
             `CheckFault` follows the call and nothing would ever observe it \
             (ADR-088, ADR-111).\nbody was:{body}"
        );
    }

    /// The companion to the source gate above: the branch it insists on is real,
    /// and it refuses rather than reading.
    ///
    /// A `Unit` is zero bytes wide, which is the shape that must be refused.
    /// The refusal is a panic, which is ADR-080's defined path — inside a
    /// wrapper `abi_guard!` turns it into a `Panic` fault (or a message and an
    /// abort where the manifest makes that fault unobservable). What must not
    /// happen, in any profile, is the read.
    #[test]
    fn a_scalar_read_refuses_a_value_that_is_not_its_type() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx is wired to rt; the Unit immortal is a valid GcRef.
        let unit = unsafe { praxis_alloc_unit(ctx) };
        assert_eq!(unit.descriptor().size(), 0, "Unit is a zero-width payload");

        // The panic is the refusal. `catch_unwind` here is the test standing in
        // for `abi_guard!`, which is what catches it in a real wrapper.
        let previous = std::panic::take_hook();
        std::panic::set_hook(Box::new(|_| {}));
        // `AssertUnwindSafe` for the same reason `abi_guard!` uses it: the
        // capture is a `Copy` C type and nothing observes a half-finished read.
        // SAFETY: `unit` is a valid GcRef into rt's live heap.
        let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| unsafe {
            int_payload(unit)
        }));
        std::panic::set_hook(previous);
        unsafe { drop_ctx(ctx) };

        let payload = outcome.expect_err("a zero-width payload must not be read as eight bytes");
        let message = payload
            .downcast_ref::<String>()
            .map(String::as_str)
            .or_else(|| payload.downcast_ref::<&str>().copied())
            .unwrap_or("");
        assert!(
            message.contains("int_payload wants a `Int` payload")
                && message.contains("this value is a `Unit`"),
            "unexpected panic message: {message:?}"
        );
    }

    #[test]
    fn alloc_int_and_load_round_trip() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx is wired to rt.
        let r = unsafe { praxis_alloc_int(ctx, 9001) };
        // SAFETY: r is a valid Int allocated above.
        assert_eq!(unsafe { praxis_int_load(ctx, r) }, 9001);
        unsafe { drop_ctx(ctx) };
    }

    /// The `Int` counterpart of
    /// [`bool_and_unit_abi_allocations_reuse_runtime_singletons`]: a small `Int`
    /// is one object per value, and an out-of-range one is still a fresh box.
    ///
    /// Both halves matter. The first is the optimization; the second is the
    /// branch a regression would silently delete, leaving every large `Int` in
    /// the language reading slot `value - SMALL_INT_MIN` of a table that ends
    /// long before it.
    #[test]
    fn small_ints_are_one_object_per_value_and_large_ones_are_not() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx is wired to rt throughout.
        unsafe {
            // In range: two calls, one object — and it is the runtime's own
            // table entry, not some other cache.
            let a = praxis_alloc_int(ctx, 7);
            let b = praxis_alloc_int(ctx, 7);
            assert_eq!(a.as_ptr(), b.as_ptr());
            assert_eq!(a.as_ptr(), rt.immortals().small_int(7).unwrap().as_ptr());
            assert_eq!(praxis_int_load(ctx, a), 7);

            // The four boundary cases, through the ABI: the exact endpoints are
            // interned and one step outside either is not.
            for v in [
                crate::small_int::SMALL_INT_MIN,
                crate::small_int::SMALL_INT_MAX,
            ] {
                assert_eq!(
                    praxis_alloc_int(ctx, v).as_ptr(),
                    praxis_alloc_int(ctx, v).as_ptr(),
                    "{v} is the edge of the range and must be interned"
                );
            }
            for v in [
                crate::small_int::SMALL_INT_MIN - 1,
                crate::small_int::SMALL_INT_MAX + 1,
            ] {
                let x = praxis_alloc_int(ctx, v);
                let y = praxis_alloc_int(ctx, v);
                assert_ne!(
                    x.as_ptr(),
                    y.as_ptr(),
                    "{v} is outside the range and must still allocate"
                );
                assert_eq!(praxis_int_load(ctx, x), v, "and still hold its value");
                assert_eq!(praxis_int_load(ctx, y), v);
            }

            // Distinct in-range values are distinct objects: interning shares
            // an object across *calls*, never across values.
            assert_ne!(
                praxis_alloc_int(ctx, 7).as_ptr(),
                praxis_alloc_int(ctx, 8).as_ptr()
            );

            // The host helper and the ABI wrapper answer the same object, as
            // `Runtime::alloc_bool` and `praxis_alloc_bool` already do.
            assert_eq!(rt.alloc_int(7).as_ptr(), a.as_ptr());
        }
        unsafe { drop_ctx(ctx) };
    }

    /// The `Char` counterpart of
    /// [`small_ints_are_one_object_per_value_and_large_ones_are_not`] (ADR-107).
    ///
    /// Both halves matter. The first is the optimization; the second is the
    /// branch a regression would silently delete, leaving every non-ASCII `Char`
    /// in the language reading slot `code` of a table that ends at 128.
    #[test]
    fn alloc_char_answers_one_object_per_ascii_code_point_and_a_large_one_still_allocates() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx is wired to rt throughout.
        unsafe {
            // In range: two calls, one object — and it is the runtime's own
            // table entry, not some other cache.
            let a = praxis_alloc_char(ctx, i64::from('a' as u32));
            let b = praxis_alloc_char(ctx, i64::from('a' as u32));
            assert_eq!(a.as_ptr(), b.as_ptr());
            assert_eq!(
                a.as_ptr(),
                rt.immortals().small_char('a' as u32).unwrap().as_ptr()
            );
            assert_eq!(a.as_char(), 'a');

            // The ceiling is interned; one above it is not. There is no floor
            // case — the payload is unsigned and NUL is interned.
            let max = i64::from(crate::small_char::SMALL_CHAR_MAX);
            assert_eq!(
                praxis_alloc_char(ctx, max).as_ptr(),
                praxis_alloc_char(ctx, max).as_ptr(),
                "the last ASCII scalar is the edge of the range and must be interned"
            );
            assert_eq!(
                praxis_alloc_char(ctx, 0).as_ptr(),
                praxis_alloc_char(ctx, 0).as_ptr(),
                "NUL is the floor and must be interned"
            );
            for code in [max + 1, i64::from('é' as u32), 0x10_FFFF] {
                let x = praxis_alloc_char(ctx, code);
                let y = praxis_alloc_char(ctx, code);
                assert_ne!(
                    x.as_ptr(),
                    y.as_ptr(),
                    "{code:#x} is outside the range and must still allocate"
                );
                assert_eq!(
                    u32::from(x.as_char()),
                    code as u32,
                    "and still hold its code point"
                );
            }

            // Distinct in-range code points are distinct objects: interning
            // shares an object across *calls*, never across values.
            assert_ne!(
                praxis_alloc_char(ctx, i64::from('a' as u32)).as_ptr(),
                praxis_alloc_char(ctx, i64::from('b' as u32)).as_ptr()
            );

            // The validity rule is untouched by the table: an interned slot is
            // reached only after `checked_alloc_char` has approved the value, so
            // a code point that is not a scalar still faults.
            for bad in [-1_i64, 0xD800, 0x11_0000, 0x1_0000_0041] {
                let _ = praxis_alloc_char(ctx, bad);
                assert_eq!(
                    rt.take_fault(),
                    Some(FaultKind::InvalidChar),
                    "{bad:#x} is not a scalar value"
                );
            }

            // The host helper and the ABI wrapper answer the same object, as
            // `Runtime::alloc_int`/`praxis_alloc_int` already do. This is what
            // makes "a `Char` is interned" one fact rather than two.
            assert_eq!(rt.alloc_char('a' as u32).as_ptr(), a.as_ptr());
            // …and the out-of-range halves still disagree as objects, which is
            // the same statement from the other side.
            assert_ne!(
                rt.alloc_char('é' as u32).as_ptr(),
                rt.alloc_char('é' as u32).as_ptr()
            );
        }
        unsafe { drop_ctx(ctx) };
    }

    /// `praxis_text_get` is the interning's largest site: it is `t[i]` *and*
    /// every step of `for c in t`. It reaches [`char_ref`] directly rather than
    /// through [`checked_alloc_char`] — a Rust `char` needs no validity check —
    /// so it is its own door and must be pinned as one.
    ///
    /// `text_get_answers_a_char_object` covers the uninterned half (`é`) and the
    /// descriptor; this covers the identity.
    #[test]
    fn text_get_answers_the_interned_char() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let s = "abca";
            let text = praxis_alloc_text(ctx, s.as_ptr(), s.len());
            let zero = praxis_alloc_int(ctx, 0);
            let three = praxis_alloc_int(ctx, 3);
            let first = praxis_text_get(ctx, text, zero);
            let last = praxis_text_get(ctx, text, three);
            assert!(!rt.has_pending_fault());

            // Two reads of the same character are one object, and it is the same
            // object every other door answers.
            assert_eq!(first.as_ptr(), last.as_ptr());
            assert_eq!(
                first.as_ptr(),
                praxis_alloc_char(ctx, i64::from('a' as u32)).as_ptr()
            );
            assert_eq!(
                first.as_ptr(),
                rt.immortals().small_char('a' as u32).unwrap().as_ptr()
            );
            assert_eq!(first.as_char(), 'a');

            // The non-ASCII half still allocates, and still answers the right
            // scalar — the branch a regression would delete.
            let u = "éé";
            let utext = praxis_alloc_text(ctx, u.as_ptr(), u.len());
            let one = praxis_alloc_int(ctx, 1);
            let x = praxis_text_get(ctx, utext, zero);
            let y = praxis_text_get(ctx, utext, one);
            assert_ne!(x.as_ptr(), y.as_ptr(), "`é` is outside the interned range");
            assert_eq!(x.as_char(), 'é');
            assert_eq!(y.as_char(), 'é');
        }
        unsafe { drop_ctx(ctx) };
    }

    /// The two doors into [`checked_alloc_char`] answer the same object, which
    /// is that helper's whole reason for existing — a rule stated at both goes
    /// stale at one, and now the rule includes which object.
    #[test]
    fn int_to_char_answers_the_same_object_as_alloc_char() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let code = praxis_alloc_int(ctx, i64::from('Z' as u32));
            let via_to_char = praxis_int_to_char(ctx, code);
            let via_alloc = praxis_alloc_char(ctx, i64::from('Z' as u32));
            assert!(!rt.has_pending_fault());
            assert_eq!(via_to_char.as_ptr(), via_alloc.as_ptr());
            assert_eq!(via_to_char.as_char(), 'Z');

            // Outside the range both still allocate, and still not each other.
            let big = praxis_alloc_int(ctx, i64::from('é' as u32));
            assert_ne!(
                praxis_int_to_char(ctx, big).as_ptr(),
                praxis_alloc_char(ctx, i64::from('é' as u32)).as_ptr()
            );
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-143.** `Int.to_text()` answers exactly what `out` writes, and the
    /// assertion is against `out`'s own path rather than a literal.
    ///
    /// Comparing to `"1660"` would pass while the two renderers disagreed about
    /// everything else; comparing to `GcRef::format`'s output cannot, because
    /// that is the function `praxis_write_stdout` calls. `i64::MIN` is in the
    /// list because it is the one value whose negation does not fit, and
    /// therefore the first thing a hand-rolled renderer gets wrong.
    #[test]
    fn int_to_text_renders_exactly_what_out_renders() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every receiver is an Int.
        unsafe {
            for v in [0_i64, 1, -1, 1660, i64::MAX, i64::MIN, UNINTERNED] {
                let receiver = praxis_alloc_int(ctx, v);
                let answer = praxis_int_to_text(ctx, receiver);
                assert!(!rt.has_pending_fault(), "{v} faulted");
                let mut printed = String::new();
                receiver.format(&mut printed);
                assert_eq!(answer.as_text(), printed, "to_text and out disagree on {v}");
            }
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-143.** The same claim for `Char.to_text()`, at an interned ASCII
    /// character and an uninterned multi-byte one.
    ///
    /// The multi-byte case is the one that would catch reading the four-byte
    /// payload as an `i64`: `'é'` is `0xE9`, and eight bytes from a four-byte
    /// payload picks up whatever follows it.
    #[test]
    fn char_to_text_renders_exactly_what_out_renders() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every receiver is a Char.
        unsafe {
            for c in ['#', 'a', 'é', '', '\u{10FFFF}'] {
                let receiver = praxis_alloc_char(ctx, i64::from(u32::from(c)));
                let answer = praxis_char_to_text(ctx, receiver);
                assert!(!rt.has_pending_fault(), "{c} faulted");
                let mut printed = String::new();
                receiver.format(&mut printed);
                assert_eq!(answer.as_text(), printed, "to_text and out disagree on {c}");
                assert_eq!(answer.as_text(), c.to_string());
            }
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-147.** An interpolation hole renders exactly what `out` writes, for
    /// **every** type — including the ones with no `to_text()` row.
    ///
    /// This is the wrapper-level half of ADR-147 decision 2, and it is asserted
    /// against `GcRef::format` — `praxis_write_stdout`'s own call — rather than
    /// against a literal, for `int_to_text_renders_exactly_what_out_renders`'s
    /// reason: a literal comparison passes while the two agree by coincidence.
    ///
    /// The receivers deliberately span a scalar, a `Text` (whose rendering is
    /// its own characters and not a quoted form), a collection and a tuple, so a
    /// wrapper that reached for a scalar payload instead of the descriptor fails
    /// on the last two rather than on none of them.
    #[test]
    fn value_to_text_renders_exactly_what_out_renders() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every receiver below is freshly allocated here.
        unsafe {
            let empty = praxis_alloc_text(ctx, std::ptr::null(), 0);
            let hello = "hello";
            let text = praxis_alloc_text(ctx, hello.as_ptr(), hello.len());
            let vec = praxis_vec_new(ctx, &scalars::INT as *const _);
            for n in [1_i64, 2, 3] {
                let _ = praxis_vec_push(ctx, vec, praxis_alloc_int(ctx, n));
            }
            let receivers = [
                praxis_alloc_int(ctx, UNINTERNED),
                praxis_alloc_int(ctx, 0),
                praxis_alloc_bool(ctx, 1),
                praxis_alloc_char(ctx, i64::from(u32::from(''))),
                empty,
                text,
                vec,
            ];
            for receiver in receivers {
                let answer = praxis_value_to_text(ctx, receiver);
                assert!(!rt.has_pending_fault(), "value_to_text faulted");
                let mut printed = String::new();
                receiver.format(&mut printed);
                assert_eq!(
                    answer.as_text(),
                    printed,
                    "a hole and `out` must write the same characters"
                );
            }
            // The `Text` rows pin the shape a caller is most likely to assume
            // wrong: `out("hello")` writes `hello`, not `"hello"`, so `"{s}"`
            // must not add quotes either.
            assert_eq!(praxis_value_to_text(ctx, text).as_text(), "hello");
            assert_eq!(praxis_value_to_text(ctx, empty).as_text(), "");
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-144.** `join` puts the separator *between* elements and nowhere
    /// else, which is the whole of the specification and the whole of what an
    /// off-by-one gets wrong.
    #[test]
    fn vec_join_puts_the_separator_between_and_nowhere_else() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every element and separator is a Text.
        unsafe {
            let cases: [(&[&str], &str, &str); 5] = [
                (&[], ", ", ""),
                (&["only"], ", ", "only"),
                (&["a", "b", "c"], ", ", "a, b, c"),
                (&["a", "b", "c"], "", "abc"),
                (&["é", ""], "", "é — ☃"),
            ];
            for (items, sep, want) in cases {
                let members: Vec<GcRef> = items.iter().map(|s| rt.alloc_text(s)).collect();
                let vec = rt.alloc_vec(&crate::text::TEXT, members);
                let separator = rt.alloc_text(sep);
                let answer = praxis_vec_join(ctx, vec, separator);
                assert!(!rt.has_pending_fault(), "{items:?} faulted");
                assert_eq!(answer.as_text(), want);
            }
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-144.** A non-`Text` element is `TypeMismatch` and the Unit
    /// sentinel, not a `Text` payload read out of an `Int`.
    ///
    /// The catalog row's `Text` bound means only a compiler bug gets here, and
    /// this is what that bug looks like when it does: a fault the program can
    /// see, rather than a pointer-and-length pair read from eight bytes of
    /// integer.
    #[test]
    fn vec_join_refuses_a_non_text_element() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let mixed = rt.alloc_vec(&scalars::INT, vec![rt.alloc_text("a"), rt.alloc_int(1)]);
            let sep = rt.alloc_text(",");
            let answer = praxis_vec_join(ctx, mixed, sep);
            assert!(rt.has_pending_fault());
            assert!(std::ptr::eq(answer.descriptor(), &scalars::UNIT));
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-144.** `Vec[Char].to_text()` is the characters with nothing between
    /// them, and it agrees with `out` on each of them for ADR-143's reason: it
    /// goes through `scalars::write_char` too.
    #[test]
    fn vec_to_text_renders_every_char() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every element is a Char.
        unsafe {
            for want in ["", ".", "..|", "héllo", "☃☃"] {
                let members: Vec<GcRef> = want
                    .chars()
                    .map(|c| praxis_alloc_char(ctx, i64::from(u32::from(c))))
                    .collect();
                let vec = rt.alloc_vec(&scalars::CHAR, members);
                let answer = praxis_vec_to_text(ctx, vec);
                assert!(!rt.has_pending_fault(), "{want:?} faulted");
                assert_eq!(answer.as_text(), want);
            }
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-144.** A non-`Char` element faults rather than being read as four
    /// bytes of something else.
    #[test]
    fn vec_to_text_refuses_a_non_char_element() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let mixed = rt.alloc_vec(&scalars::CHAR, vec![rt.alloc_int(65)]);
            let answer = praxis_vec_to_text(ctx, mixed);
            assert!(rt.has_pending_fault());
            assert!(std::ptr::eq(answer.descriptor(), &scalars::UNIT));
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-145.** `reversed` answers a **new** `Vec` and leaves the receiver
    /// alone — the rule every barrier in this block states, and the one a
    /// wrapper that reversed in place would break invisibly for a caller still
    /// holding `v`.
    ///
    /// The empty case is here because `praxis_vec_sorted` needs a `len() > 1`
    /// guard for the analogous one and this needs none: there is no callback to
    /// avoid calling.
    #[test]
    fn vec_reversed_answers_a_new_vec_and_leaves_the_receiver_alone() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let source = rt.alloc_vec(
                &scalars::INT,
                vec![rt.alloc_int(3), rt.alloc_int(1), rt.alloc_int(2)],
            );
            let answer = praxis_vec_reversed(ctx, source);
            assert!(!rt.has_pending_fault());
            let got: Vec<i64> = answer.as_vec().iter().map(|r| r.as_int()).collect();
            assert_eq!(got, vec![2, 1, 3]);
            let still: Vec<i64> = source.as_vec().iter().map(|r| r.as_int()).collect();
            assert_eq!(still, vec![3, 1, 2], "the receiver is not touched");
            assert_ne!(answer.as_ptr(), source.as_ptr());

            let empty = rt.alloc_vec(&scalars::INT, vec![]);
            assert!(praxis_vec_reversed(ctx, empty).as_vec().is_empty());
            assert!(!rt.has_pending_fault());
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-145.** Reversal reads no descriptor callback, so a `Vec` of a type
    /// with no `compare` reverses where `sorted` faults.
    ///
    /// This is the runtime half of the catalog row carrying no capability bound.
    /// A `Unit` has no `compare` — `praxis_vec_sorted` raises `TypeMismatch` on
    /// one — and it reverses without a word.
    #[test]
    fn vec_reversed_needs_no_callback_where_sorted_needs_compare() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let closures = rt.alloc_vec(
                &crate::closures::CLOSURE,
                vec![
                    praxis_alloc_closure(ctx, std::ptr::null(), 0),
                    praxis_alloc_closure(ctx, std::ptr::null(), 0),
                ],
            );
            assert_eq!(praxis_vec_reversed(ctx, closures).as_vec().len(), 2);
            assert!(!rt.has_pending_fault(), "reversal asks for no callback");

            praxis_vec_sorted(ctx, closures);
            assert!(rt.has_pending_fault(), "ordering still asks for `compare`");
        }
        unsafe { drop_ctx(ctx) };
    }

    /// The shape of both groupings, read back as nested `Int`s.
    ///
    /// # Safety
    /// `answer` must be a valid `Vec[Vec[Int]]` `GcRef`.
    unsafe fn groups_of_int(answer: GcRef) -> Vec<Vec<i64>> {
        answer
            .as_vec()
            .iter()
            .map(|inner| inner.as_vec().iter().map(|r| r.as_int()).collect())
            .collect()
    }

    /// **ADR-149.** `chunks` partitions: every element appears once, in order,
    /// and a length the size does not divide leaves a *short last chunk* rather
    /// than dropping the tail or padding it.
    #[test]
    fn vec_chunks_partitions_and_keeps_a_short_tail() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let ints: Vec<GcRef> = (1..=5).map(|n| rt.alloc_int(n)).collect();
            let source = rt.alloc_vec(&scalars::INT, ints);

            let two = rt.alloc_int(2);
            let answer = praxis_vec_chunks(ctx, source, two);
            assert!(!rt.has_pending_fault());
            assert_eq!(groups_of_int(answer), vec![vec![1, 2], vec![3, 4], vec![5]]);

            // A size that divides leaves no short chunk, which is the same rule
            // and is worth pinning beside the one that does.
            let five = rt.alloc_int(5);
            assert_eq!(
                groups_of_int(praxis_vec_chunks(ctx, source, five)),
                vec![vec![1, 2, 3, 4, 5]],
            );

            // Wider than the receiver is not a fault: it is one short chunk.
            let nine = rt.alloc_int(9);
            assert_eq!(
                groups_of_int(praxis_vec_chunks(ctx, source, nine)),
                vec![vec![1, 2, 3, 4, 5]],
            );

            let still: Vec<i64> = source.as_vec().iter().map(|r| r.as_int()).collect();
            assert_eq!(still, vec![1, 2, 3, 4, 5], "the receiver is not touched");
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-149.** `windows` slides by one and keeps only the runs that fit, so
    /// a receiver shorter than the size answers `[]` rather than one short run —
    /// the one place the two groupings differ.
    #[test]
    fn vec_windows_slide_by_one_and_drop_a_run_that_does_not_fit() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let ints: Vec<GcRef> = (1..=4).map(|n| rt.alloc_int(n)).collect();
            let source = rt.alloc_vec(&scalars::INT, ints);

            let two = rt.alloc_int(2);
            assert_eq!(
                groups_of_int(praxis_vec_windows(ctx, source, two)),
                vec![vec![1, 2], vec![2, 3], vec![3, 4]],
            );
            assert!(!rt.has_pending_fault());

            // Exactly the length is one window; one past it is none. Off by one
            // here is the whole difference between `[]` and a wrong answer.
            let four = rt.alloc_int(4);
            assert_eq!(
                groups_of_int(praxis_vec_windows(ctx, source, four)),
                vec![vec![1, 2, 3, 4]],
            );
            let five = rt.alloc_int(5);
            let none = praxis_vec_windows(ctx, source, five);
            assert!(
                none.as_vec().is_empty(),
                "a run of five does not fit in four"
            );
            assert!(
                !rt.has_pending_fault(),
                "not fitting is an answer, not a fault"
            );

            // Windows share their elements rather than copying them, which is
            // the language's reference semantics and not a rule of this wrapper.
            let answer = praxis_vec_windows(ctx, source, two);
            let first = answer.as_vec()[0].as_vec()[1].as_ptr();
            let second = answer.as_vec()[1].as_vec()[0].as_ptr();
            assert_eq!(first, second, "the overlapping element is one object");
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-149.** The only thing either grouping refuses: a run of `n <= 0` is
    /// not a short run, it is not a run, so there is no sequence of them to
    /// answer with.
    ///
    /// The empty receiver is here beside it because it is the case that looks
    /// like a fault and is not — `[].chunks(2)` is `[]`, the same way `[]`
    /// reverses to `[]`.
    #[test]
    fn a_group_size_of_zero_or_less_is_an_invalid_size_fault() {
        for size in [0i64, -1, i64::MIN] {
            for (name, wrapper) in [
                (
                    "chunks",
                    praxis_vec_chunks as unsafe extern "C" fn(_, _, _) -> _,
                ),
                ("windows", praxis_vec_windows),
            ] {
                let mut rt = Runtime::new();
                let ctx = wired_ctx(&mut rt);
                // SAFETY: ctx wired.
                unsafe {
                    let source = rt.alloc_vec(&scalars::INT, vec![rt.alloc_int(1)]);
                    let n = rt.alloc_int(size);
                    let answer = wrapper(ctx, source, n);
                    assert!(rt.has_pending_fault(), "{name}({size}) must fault");
                    assert_eq!(rt.fault(), crate::FaultKind::InvalidSize, "{name}({size})");
                    assert!(
                        std::ptr::eq(answer.descriptor(), &scalars::UNIT),
                        "{name}({size}) answers the Unit sentinel"
                    );
                }
                unsafe { drop_ctx(ctx) };
            }
        }

        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let empty = rt.alloc_vec(&scalars::INT, vec![]);
            let two = rt.alloc_int(2);
            assert!(praxis_vec_chunks(ctx, empty, two).as_vec().is_empty());
            assert!(praxis_vec_windows(ctx, empty, two).as_vec().is_empty());
            assert!(
                !rt.has_pending_fault(),
                "an empty receiver is an empty answer"
            );
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-149 decision 1.** The outer `Vec` is labelled `VEC` at every length
    /// and the inner ones carry the receiver's element descriptor.
    ///
    /// The **empty** answer is the whole reason this test exists, and it is the
    /// only part that is a choice: `VEC` is what `outer.push(inner)` already
    /// produces, so a non-empty grouping could hardly answer anything else and
    /// asserting it proves little. With the label inferred from the first group
    /// there would be none to read, and `[1, 2].windows(5)` would carry a null
    /// where `[1, 2].windows(2)` carries `VEC` — one type with two labels, and
    /// the null is the one `vec_format` renders as `[]` and `push` treats as
    /// "adopt whatever arrives".
    #[test]
    fn a_grouping_labels_the_outer_vec_even_when_it_is_empty() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let source = rt.alloc_vec(&scalars::INT, vec![rt.alloc_int(1), rt.alloc_int(2)]);
            let two = rt.alloc_int(2);
            let five = rt.alloc_int(5);

            for answer in [
                praxis_vec_chunks(ctx, source, two),
                praxis_vec_windows(ctx, source, two),
                // The two that come out empty, and the reason this test exists.
                praxis_vec_windows(ctx, source, five),
                praxis_vec_chunks(ctx, rt.alloc_vec(&scalars::INT, vec![]), two),
            ] {
                let p = vec_payload(answer);
                assert!(
                    std::ptr::eq(p.element_descriptor, &crate::collections::VEC),
                    "the outer Vec holds Vecs whether or not it holds any"
                );
                for inner in p.items.iter() {
                    assert!(std::ptr::eq(
                        vec_payload(*inner).element_descriptor,
                        &scalars::INT
                    ));
                }
            }
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-149.** A grouping reads no descriptor callback, so a `Vec` of a
    /// type with no `compare` groups where `sorted` faults — `reversed`'s claim,
    /// and the runtime half of these two rows carrying no capability bound.
    #[test]
    fn a_grouping_needs_no_callback_where_sorted_needs_compare() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let closures = rt.alloc_vec(
                &crate::closures::CLOSURE,
                vec![
                    praxis_alloc_closure(ctx, std::ptr::null(), 0),
                    praxis_alloc_closure(ctx, std::ptr::null(), 0),
                    praxis_alloc_closure(ctx, std::ptr::null(), 0),
                ],
            );
            let two = rt.alloc_int(2);
            assert_eq!(praxis_vec_chunks(ctx, closures, two).as_vec().len(), 2);
            assert_eq!(praxis_vec_windows(ctx, closures, two).as_vec().len(), 2);
            assert!(!rt.has_pending_fault(), "grouping asks for no callback");

            praxis_vec_sorted(ctx, closures);
            assert!(rt.has_pending_fault(), "ordering still asks for `compare`");
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-107's pacing half, and ADR-100 §3's analogue.** [`char_ref`] must
    /// give the collector its turn on the path where it allocates *nothing*.
    ///
    /// `TextGet` is `AllocatesAndFaults` in the manifest, which is generated
    /// code's contract that the call site is a GC safepoint. A `for c in line`
    /// loop over ASCII touches nothing else that bumps the pacing counter, and
    /// the counter is the collector's only trigger — so an early return here
    /// would make such a loop run arbitrarily long with no collection at all.
    ///
    /// **The interleaved allocation is load-bearing and must stay unpaced.** The
    /// observable is the live registry *shrinking*, and an interned `Char` never
    /// enters it, so a loop of nothing but `praxis_text_get` could not shrink
    /// anything however well it paced — a guaranteed false pass (see
    /// `UNINTERNED`). `Runtime::alloc_int` is the one helper that grows the heap
    /// **without** pacing, so it supplies the pressure and the population while
    /// leaving `praxis_text_get` as the only safepoint in the loop. Swapping it
    /// for `praxis_alloc_int` would make the test pass with this function's
    /// safepoint deleted.
    #[test]
    fn char_ref_paces_the_collector_even_when_it_answers_from_the_table() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired throughout; `text` and `index` are rooted below.
        unsafe {
            let s = "abcdefgh";
            let text = praxis_alloc_text(ctx, s.as_ptr(), s.len());
            let index = praxis_alloc_int(ctx, 3);
            let mut frame = push_frame(ctx, SlotCount::new(2).unwrap());
            frame.set(0, text);
            frame.set(1, index);

            let mut before = rt.heap().stats().live_count;
            let mut paced = false;
            for i in 0..100_000_i64 {
                // Registered, unrooted, and *unpaced*: pressure the collector can
                // see and reclaim, contributed by something that never offers a
                // turn itself.
                let _ = rt.alloc_int(UNINTERNED + i);
                // The wrapper under test. Every character of `s` is ASCII, so
                // this answers from the table and allocates nothing.
                let c = praxis_text_get(ctx, text, index);
                assert_eq!(c.as_char(), 'd');
                let after = rt.heap().stats().live_count;
                if after < before.saturating_add(1) {
                    paced = true;
                    break;
                }
                before = after;
            }
            drop(frame);
            assert!(
                paced,
                "praxis_text_get never gave the collector a turn on the interned path"
            );
        }
        unsafe { drop_ctx(ctx) };
    }

    /// `default_cell`'s `Char` arm, the fourth boxing site. A `Grid[Char]`'s
    /// fill is NUL, which is inside the range.
    #[test]
    fn a_grid_of_char_fills_with_the_interned_nul() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let grid = praxis_grid_new(ctx, &crate::scalars::CHAR, 3, 2);
            assert!(!rt.has_pending_fault());
            let nul = rt.immortals().small_char(0).expect("NUL is interned");
            for y in 0..2 {
                for x in 0..3 {
                    let xi = praxis_alloc_int(ctx, x);
                    let yi = praxis_alloc_int(ctx, y);
                    let cell = praxis_grid_get(ctx, grid, xi, yi);
                    assert_eq!(
                        cell.as_ptr(),
                        nul.as_ptr(),
                        "every cell of a fresh Grid[Char] is the one interned NUL"
                    );
                }
            }
        }
        unsafe { drop_ctx(ctx) };
    }

    /// The executable form of "nothing in the language can observe `Char`
    /// identity", and the [`crate::dynamic_key::DynamicKey`] leg of ADR-107's
    /// argument.
    ///
    /// `DynamicKey::eq` opens with a pointer comparison, and that is the line
    /// interning could in principle have moved — but it is a fast path *for*
    /// structural equality and `char_equals` is a reflexive `u32 ==`, so sharing
    /// can only make it fire more often. This asserts the consequence rather than
    /// the argument: the same shape is run twice, once with interned keys and
    /// once with keys the runtime does not intern, and the two must agree.
    #[test]
    fn interning_a_char_does_not_change_keyed_collection_behaviour() {
        // (key, a different key, label): once inside the ASCII range and once
        // outside it. `é` and `ü` are two scalars the table does not hold.
        for (a_ch, b_ch, label) in [('a', 'b', "interned"), ('é', 'ü', "allocated")] {
            let mut rt = Runtime::new();
            let ctx = wired_ctx(&mut rt);
            // SAFETY: ctx wired; every ref below comes from the ABI.
            unsafe {
                let a = praxis_alloc_char(ctx, i64::from(a_ch as u32));
                // A *second* reference to the same value, built separately.
                // Interned it is `a`; uninterned it is a different object with
                // the same payload. Both must key the same slot.
                let a_again = praxis_alloc_char(ctx, i64::from(a_ch as u32));
                let b = praxis_alloc_char(ctx, i64::from(b_ch as u32));
                assert_eq!(
                    std::ptr::eq(a.as_ptr(), a_again.as_ptr()),
                    label == "interned",
                    "the fixture must actually be {label}"
                );

                let set = praxis_set_new(ctx, &crate::scalars::CHAR);
                let _ = praxis_set_insert(ctx, set, a);
                assert_eq!(
                    praxis_bool_load(ctx, praxis_set_contains(ctx, set, a_again)),
                    1,
                    "{label}: an equal Char is the same set member"
                );
                assert_eq!(
                    praxis_bool_load(ctx, praxis_set_contains(ctx, set, b)),
                    0,
                    "{label}: a different Char is not"
                );
                // Inserting the equal-but-possibly-distinct object must not add
                // a second member — the property that would break if the pointer
                // fast path and `char_equals` ever disagreed.
                let _ = praxis_set_insert(ctx, set, a_again);
                assert_eq!(praxis_int_load(ctx, praxis_set_len(ctx, set)), 1, "{label}");

                let counter = praxis_counter_new(ctx, &crate::scalars::CHAR);
                let _ = praxis_counter_inc(ctx, counter, a);
                let _ = praxis_counter_inc(ctx, counter, a_again);
                assert_eq!(
                    praxis_int_load(ctx, praxis_counter_get(ctx, counter, a)),
                    2,
                    "{label}: two bumps of an equal key are one key"
                );
                assert_eq!(
                    praxis_int_load(ctx, praxis_counter_len(ctx, counter)),
                    1,
                    "{label}"
                );
            }
            unsafe { drop_ctx(ctx) };
        }
    }

    /// An interned `Char` is never registered, so a collection cannot reclaim it
    /// however unrooted it is — the `Char` half of
    /// [`an_interned_int_survives_collection_unrooted`].
    #[test]
    fn an_interned_char_survives_collection_unrooted() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx is wired to rt throughout.
        unsafe {
            let _ = praxis_alloc_char(ctx, i64::from('q' as u32));
        }
        assert_eq!(
            rt.heap().stats().live_count,
            0,
            "an interned Char must not enter the live registry"
        );
        // Nothing roots `'q'`: no shadow frame, no native scope, no Rust local
        // the collector can see. A registered object here would be swept.
        rt.collect_now();
        // SAFETY: ctx is still wired; the reference must still be readable.
        unsafe {
            let q = praxis_alloc_char(ctx, i64::from('q' as u32));
            assert!(!q.header().is_poisoned(), "an immortal is never swept");
            assert_eq!(q.as_char(), 'q');
        }
        unsafe { drop_ctx(ctx) };
    }

    /// An interned `Int` is never registered, so a collection cannot reclaim it
    /// however unrooted it is. The `Int` analogue of
    /// `runtime_collect_keeps_immortals_alive_unrooted`.
    #[test]
    fn an_interned_int_survives_collection_unrooted() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx is wired to rt throughout.
        unsafe {
            let _ = praxis_alloc_int(ctx, 5);
        }
        assert_eq!(
            rt.heap().stats().live_count,
            0,
            "an interned Int must not enter the live registry"
        );
        // Nothing roots `5`: no shadow frame, no native scope, no Rust local the
        // collector can see. A registered object here would be swept.
        rt.collect_now();
        // SAFETY: ctx is still wired; the reference must still be readable.
        unsafe {
            let five = praxis_alloc_int(ctx, 5);
            assert!(!five.header().is_poisoned(), "an immortal is never swept");
            assert_eq!(praxis_int_load(ctx, five), 5);
        }
        unsafe { drop_ctx(ctx) };
    }

    /// The executable form of "nothing in the language can observe `Int`
    /// identity": the three keyed collections must behave identically whether
    /// their keys are shared objects or distinct ones.
    ///
    /// [`crate::dynamic_key::DynamicKey`]'s `eq` opens with a pointer
    /// comparison, and that is the line interning could in principle have moved
    /// — but it is a fast path *for* structural equality and `int_equals` is
    /// reflexive, so sharing can only make it fire more often. This asserts the
    /// consequence rather than the argument: every operation below is run twice
    /// at the same shape, once with interned keys and once with uninterned ones,
    /// and the two must agree.
    #[test]
    fn interning_does_not_change_keyed_collection_behaviour() {
        // (key_a, key_b) pairs: two distinct keys, once inside the interned
        // range and once outside it.
        for (a_val, b_val, label) in [
            (5_i64, 6_i64, "interned"),
            (UNINTERNED, UNINTERNED + 1, "allocated"),
        ] {
            let mut rt = Runtime::new();
            let ctx = wired_ctx(&mut rt);
            // SAFETY: ctx wired; every ref below comes from the ABI.
            unsafe {
                let a = praxis_alloc_int(ctx, a_val);
                // A *second* reference to the same value, allocated separately.
                // Interned it is `a`; uninterned it is a different object with
                // the same payload. Both must key the same slot.
                let a_again = praxis_alloc_int(ctx, a_val);
                let b = praxis_alloc_int(ctx, b_val);

                let map = praxis_map_new(ctx, &scalars::INT);
                let one = praxis_alloc_int(ctx, 1);
                let _ = praxis_map_insert(ctx, map, a, one);
                // `map_index` rather than `map_get`: the subscript answers the
                // value where `.get` answers an `Option` (ADR-076), and the
                // value is what has to match.
                assert_eq!(
                    praxis_int_load(ctx, praxis_map_index(ctx, map, a_again)),
                    1,
                    "{label}: an equal key must find the entry"
                );
                assert_eq!(
                    rt.fault(),
                    FaultKind::None,
                    "{label}: an equal key is a present key"
                );
                assert_eq!(
                    praxis_bool_load(ctx, praxis_map_contains(ctx, map, b)),
                    0,
                    "{label}: a different key must not"
                );
                assert_eq!(praxis_int_load(ctx, praxis_map_len(ctx, map)), 1);

                let set = praxis_set_new(ctx, &scalars::INT);
                let _ = praxis_set_insert(ctx, set, a);
                let _ = praxis_set_insert(ctx, set, a_again);
                assert_eq!(
                    praxis_int_load(ctx, praxis_set_len(ctx, set)),
                    1,
                    "{label}: re-inserting an equal value must not grow the set"
                );
                assert_eq!(praxis_bool_load(ctx, praxis_set_contains(ctx, set, b)), 0);

                let counter = praxis_counter_new(ctx, &scalars::INT);
                let _ = praxis_counter_inc(ctx, counter, a);
                let _ = praxis_counter_inc(ctx, counter, a_again);
                assert_eq!(
                    praxis_int_load(ctx, praxis_counter_get(ctx, counter, a)),
                    2,
                    "{label}: two bumps of an equal key are two bumps of one key"
                );
                assert_eq!(praxis_int_load(ctx, praxis_counter_len(ctx, counter)), 1);
            }
            unsafe { drop_ctx(ctx) };
        }
    }

    #[test]
    fn bool_and_unit_abi_allocations_reuse_runtime_singletons() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let (true_ref, false_ref, unit_ref) = unsafe {
            (
                praxis_alloc_bool(ctx, 1),
                praxis_alloc_bool(ctx, 0),
                praxis_alloc_unit(ctx),
            )
        };
        let expected = (
            rt.immortals().true_(),
            rt.immortals().false_(),
            rt.immortals().unit(),
        );
        unsafe { drop_ctx(ctx) };

        assert_eq!(true_ref.as_ptr(), expected.0.as_ptr());
        assert_eq!(false_ref.as_ptr(), expected.1.as_ptr());
        assert_eq!(unit_ref.as_ptr(), expected.2.as_ptr());
    }

    #[test]
    fn repeated_bool_allocation_mints_no_new_objects() {
        // A fresh *immortal* per call would be unregistered storage no
        // collection can reclaim, leaking one Bool per loop iteration. There
        // are two Bools; a hundred calls must name two objects.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let mut seen = std::collections::HashSet::new();
        // SAFETY: ctx wired.
        unsafe {
            for i in 0..100_i64 {
                seen.insert(praxis_alloc_bool(ctx, i % 2).as_ptr());
                seen.insert(praxis_alloc_unit(ctx).as_ptr());
            }
        }
        unsafe { drop_ctx(ctx) };
        assert_eq!(seen.len(), 3, "true, false and unit — and nothing else");
    }

    /// Every wrapper that answers a *predicate* hands back one of the two `Bool`
    /// singletons — the comparisons and the `is_empty`/`contains` family, which
    /// are what a real program calls in a loop. It is also what makes their
    /// `Effect::Pure` rows honest: nothing here can collect, so the call site is
    /// not a safepoint.
    #[test]
    fn predicate_wrappers_return_bool_singletons_and_allocate_nothing() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let (immortal_true, immortal_false) = (rt.immortals().true_(), rt.immortals().false_());
        // SAFETY: ctx wired; every argument below is allocated through the ABI.
        unsafe {
            let one = praxis_alloc_int(ctx, 1);
            let two = praxis_alloc_int(ctx, 2);
            let empty_vec = praxis_vec_new(ctx, &scalars::INT);
            let empty_text = praxis_alloc_text(ctx, std::ptr::null(), 0);
            let live_before = rt.heap().stats().live_count;

            let answers = [
                (praxis_int_eq(ctx, one, two), false),
                (praxis_int_ne(ctx, one, two), true),
                (praxis_int_lt(ctx, one, two), true),
                (praxis_int_gt(ctx, one, two), false),
                (praxis_int_le(ctx, one, one), true),
                (praxis_int_ge(ctx, one, two), false),
                (praxis_vec_is_empty(ctx, empty_vec), true),
                (praxis_text_is_empty(ctx, empty_text), true),
            ];

            assert_eq!(
                rt.heap().stats().live_count,
                live_before,
                "a predicate wrapper must not allocate"
            );
            for (answer, expected) in answers {
                let want = if expected {
                    immortal_true
                } else {
                    immortal_false
                };
                assert_eq!(
                    answer.as_ptr(),
                    want.as_ptr(),
                    "predicate answered with a fresh Bool instead of the singleton"
                );
            }
        }
        unsafe { drop_ctx(ctx) };
    }

    /// Every wrapper that boxes a *derived* scalar — `Text` construction, the
    /// `.len()` family, `Grid` extents, checked arithmetic — paces the
    /// collector. Without that, a program whose pressure comes from those (a
    /// text-processing loop, say) could run arbitrarily long with the collector
    /// never offered a turn. Each is driven here until its own pacing collects.
    ///
    /// **Every receiver is sized past the interned range on purpose.** The first
    /// four wrappers answer a *length*, and a length inside
    /// [`crate::small_int`]'s range is an immortal that never enters the live
    /// registry — so with a five-byte `Text` or a 2×2 `Grid` the shrink test
    /// below is true on the first iteration and the test passes without a
    /// collection ever running (see `UNINTERNED`). Interning removes the
    /// allocation, not the pacing, and it is the pacing this test is about; the
    /// oversized receivers are what keep the observable in place.
    #[test]
    fn every_scalar_boxing_wrapper_paces_the_collector() {
        // (name, a closure that performs one allocating call)
        type Call = unsafe extern "C" fn(*mut RuntimeContext, GcRef) -> GcRef;
        let cases: [(&str, Call); 7] = [
            ("praxis_text_len", praxis_text_len),
            ("praxis_vec_len", praxis_vec_len),
            ("praxis_grid_width", praxis_grid_width),
            ("praxis_grid_height", praxis_grid_height),
            ("praxis_float_to_text", praxis_float_to_text),
            ("praxis_int_to_text", praxis_int_to_text),
            ("praxis_char_to_text", praxis_char_to_text),
        ];
        // One past the interned range, in whatever unit the receiver measures.
        let big = UNINTERNED as usize;
        for (name, call) in cases {
            let mut rt = Runtime::new();
            let ctx = wired_ctx(&mut rt);
            // SAFETY: ctx wired; each receiver matches its wrapper.
            unsafe {
                let text = "x".repeat(big);
                let receiver = match name {
                    "praxis_text_len" => praxis_alloc_text(ctx, text.as_ptr(), big),
                    "praxis_vec_len" => {
                        // The elements are all the same interned `0`, so the Vec
                        // costs one allocation regardless of its length — only
                        // its `len()` matters here.
                        rt.alloc_vec(&scalars::INT, vec![rt.alloc_int(0); big])
                    }
                    "praxis_float_to_text" => praxis_alloc_float(ctx, 1.5_f64.to_bits() as i64),
                    // The two `to_text` rows answer a fresh owned `Text` every
                    // call whatever the receiver is, so an interned receiver is
                    // the honest case: the allocation is the *answer*, not the
                    // argument.
                    "praxis_int_to_text" => praxis_alloc_int(ctx, big as i64),
                    "praxis_char_to_text" => praxis_alloc_char(ctx, i64::from(u32::from('e'))),
                    // `width` reads the first dimension and `height` the second,
                    // so each case makes *its own* answer uninterned and leaves
                    // the other dimension at one cell.
                    "praxis_grid_width" => praxis_grid_new(ctx, &scalars::INT, big as i64, 1),
                    _ => praxis_grid_new(ctx, &scalars::INT, 1, big as i64),
                };
                let mut frame = push_frame(ctx, SlotCount::new(1).unwrap());
                frame.set(0, receiver);

                let mut before = rt.heap().stats().live_count;
                let mut paced = false;
                for _ in 0..10_000 {
                    let _ = call(ctx, receiver);
                    let after = rt.heap().stats().live_count;
                    if after < before.saturating_add(1) {
                        paced = true;
                        break;
                    }
                    before = after;
                }
                drop(frame);
                assert!(paced, "{name} never gave the collector a turn");
            }
            unsafe { drop_ctx(ctx) };
        }
    }

    #[test]
    fn checked_add_returns_sum() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; operands allocated as Ints.
        unsafe {
            let a = praxis_alloc_int(ctx, 40);
            let b = praxis_alloc_int(ctx, 2);
            let s = praxis_int_add(ctx, a, b);
            assert_eq!(praxis_int_load(ctx, s), 42);
            assert!(!rt.has_pending_fault());
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn float_sign_of_zero_is_zero() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let signed = unsafe {
            let zero = praxis_alloc_float(ctx, 0.0_f64.to_bits() as i64);
            let result = praxis_float_sign(ctx, zero);
            f64::from_bits(praxis_float_load(ctx, result) as u64)
        };
        unsafe { drop_ctx(ctx) };

        assert_eq!(signed, 0.0);
    }

    /// `-0.0` is still zero: `signum` reports the sign *bit* and would answer
    /// `-1.0` here.
    #[test]
    fn float_sign_of_negative_zero_is_zero() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let signed = unsafe {
            let zero = praxis_alloc_float(ctx, (-0.0_f64).to_bits() as i64);
            let result = praxis_float_sign(ctx, zero);
            f64::from_bits(praxis_float_load(ctx, result) as u64)
        };
        unsafe { drop_ctx(ctx) };

        assert_eq!(signed, 0.0);
    }

    #[test]
    fn float_sign_of_nan_is_nan() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let signed = unsafe {
            let nan = praxis_alloc_float(ctx, f64::NAN.to_bits() as i64);
            let result = praxis_float_sign(ctx, nan);
            f64::from_bits(praxis_float_load(ctx, result) as u64)
        };
        unsafe { drop_ctx(ctx) };

        assert!(signed.is_nan());
    }

    /// `min`/`max`/`clamp` hand back **the reference they were given**, not an
    /// equal copy (ADR-058). That is what makes them `Effect::Pure` — no
    /// allocation, so their call site is not a safepoint — and a version that
    /// allocated would pass every value test while quietly making three of the
    /// seven helpers collect.
    #[test]
    fn the_selecting_helpers_return_an_operand_and_allocate_nothing() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every operand is a valid Int.
        unsafe {
            let lo = praxis_alloc_int(ctx, 3);
            let hi = praxis_alloc_int(ctx, 7);
            assert_eq!(praxis_int_min(ctx, lo, hi).as_ptr(), lo.as_ptr());
            assert_eq!(praxis_int_min(ctx, hi, lo).as_ptr(), lo.as_ptr());
            assert_eq!(praxis_int_max(ctx, lo, hi).as_ptr(), hi.as_ptr());
            assert_eq!(praxis_int_max(ctx, hi, lo).as_ptr(), hi.as_ptr());
            // Equal operands pick the left one — arbitrary but fixed, so the
            // choice is a decision and not a coin flip.
            let three = praxis_alloc_int(ctx, 3);
            assert_eq!(praxis_int_min(ctx, lo, three).as_ptr(), lo.as_ptr());
            assert_eq!(praxis_int_max(ctx, lo, three).as_ptr(), lo.as_ptr());
            // `clamp` returns whichever of its three operands is the answer.
            let v = praxis_alloc_int(ctx, 5);
            assert_eq!(praxis_int_clamp(ctx, v, lo, hi).as_ptr(), v.as_ptr());
            let below = praxis_alloc_int(ctx, 1);
            assert_eq!(praxis_int_clamp(ctx, below, lo, hi).as_ptr(), lo.as_ptr());
            let above = praxis_alloc_int(ctx, 9);
            assert_eq!(praxis_int_clamp(ctx, above, lo, hi).as_ptr(), hi.as_ptr());
            assert!(!rt.has_pending_fault());
        }
        unsafe { drop_ctx(ctx) };
    }

    /// An inverted `clamp` range is empty, so there is no operand to return and
    /// no answer that is not invented. It faults (ADR-058) and returns the Unit
    /// sentinel, like every other faulting wrapper.
    #[test]
    fn an_inverted_clamp_range_faults_rather_than_guessing() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every operand is a valid Int.
        unsafe {
            let v = praxis_alloc_int(ctx, 5);
            let lo = praxis_alloc_int(ctx, 10);
            let hi = praxis_alloc_int(ctx, 0);
            let r = praxis_int_clamp(ctx, v, lo, hi);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::EmptyRange);
            assert_eq!(r.as_ptr(), rt.immortals().unit().as_ptr());
        }
        let _ = rt.take_fault();
        // A degenerate but *legal* range — one value wide — is not inverted.
        // SAFETY: ctx wired; every operand is a valid Int.
        unsafe {
            let v = praxis_alloc_int(ctx, 5);
            let same = praxis_alloc_int(ctx, 4);
            let r = praxis_int_clamp(ctx, v, same, same);
            assert!(!rt.has_pending_fault());
            assert_eq!(r.as_ptr(), same.as_ptr());
        }
        unsafe { drop_ctx(ctx) };
    }

    /// A range whose member count has no `Int` faults with `IntOverflow`,
    /// answering the Unit sentinel like every other faulting wrapper.
    ///
    /// The kind is `IntOverflow` and not `EmptyRange` (ADR-059, ADR-075):
    /// `Int::MIN..Int::MAX` is the *widest* range expressible, so "empty range"
    /// would be a fault message that contradicts the input. `gcd`, `lcm` and
    /// A\*'s path cost answer `IntOverflow` for a result with no `Int` too.
    #[test]
    fn a_range_whose_count_has_no_int_faults_rather_than_wrapping_negative() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; both bounds are valid Ints.
        unsafe {
            let lo = praxis_alloc_int(ctx, i64::MIN);
            let hi = praxis_alloc_int(ctx, i64::MAX);
            let r = praxis_range_new(ctx, lo, hi);
            let len = praxis_range_len(ctx, r);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
            assert_eq!(len.as_ptr(), rt.immortals().unit().as_ptr());
        }
        let _ = rt.take_fault();
        // A range one narrower is countable, so the refusal is the edge and not
        // the rule.
        // SAFETY: ctx wired; both bounds are valid Ints.
        unsafe {
            let lo = praxis_alloc_int(ctx, 0);
            let hi = praxis_alloc_int(ctx, i64::MAX);
            let r = praxis_range_new(ctx, lo, hi);
            let len = praxis_range_len(ctx, r);
            assert!(!rt.has_pending_fault());
            assert_eq!(praxis_int_load(ctx, len), i64::MAX);
        }
        unsafe { drop_ctx(ctx) };
    }

    /// `gcd` and `lcm` at the edges of what an `Int` can hold. Both are computed
    /// in `i128` and range-checked on the way out, so the only refusal is a
    /// result that genuinely has no `Int` — and `gcd`'s is exactly one input
    /// pair, which a naive `i64` implementation would have wrapped instead.
    #[test]
    fn gcd_and_lcm_are_non_negative_and_refuse_only_what_has_no_int() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every operand is a valid Int.
        unsafe {
            let load = |r: GcRef| praxis_int_load(ctx, r);
            // `Int::MIN`'s divisors: |Int::MIN| is out of range, but every gcd
            // *with* it that is not itself is in range.
            let min = praxis_alloc_int(ctx, i64::MIN);
            let two = praxis_alloc_int(ctx, 2);
            assert_eq!(load(praxis_int_gcd(ctx, min, two)), 2);
            assert!(!rt.has_pending_fault());
            // …and the one pair whose answer is 2^63 faults.
            let min2 = praxis_alloc_int(ctx, i64::MIN);
            let _ = praxis_int_gcd(ctx, min, min2);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
        }
        let _ = rt.take_fault();
        // SAFETY: ctx wired; every operand is a valid Int.
        unsafe {
            let load = |r: GcRef| praxis_int_load(ctx, r);
            // Both signs, one answer: the lcm is non-negative.
            let neg = praxis_alloc_int(ctx, -4);
            let six = praxis_alloc_int(ctx, 6);
            assert_eq!(load(praxis_int_lcm(ctx, neg, six)), 12);
            let neg6 = praxis_alloc_int(ctx, -6);
            assert_eq!(load(praxis_int_lcm(ctx, neg, neg6)), 12);
            // `lcm(n, 0)` is 0, and the pair `(0, 0)` does not divide by zero.
            let zero = praxis_alloc_int(ctx, 0);
            assert_eq!(load(praxis_int_lcm(ctx, six, zero)), 0);
            assert_eq!(load(praxis_int_lcm(ctx, zero, zero)), 0);
            assert_eq!(load(praxis_int_gcd(ctx, zero, zero)), 0);
            assert!(!rt.has_pending_fault());
            // An lcm that does not fit: two coprime halves of the range.
            let big = praxis_alloc_int(ctx, i64::MAX);
            let three = praxis_alloc_int(ctx, 3);
            let _ = praxis_int_lcm(ctx, big, three);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    /// `abs` faults on the one input with no positive counterpart, and `sign` is
    /// total on the same input — the distinction the manifest records as
    /// `AllocatesAndFaults` versus `Allocates`.
    #[test]
    fn abs_faults_on_the_value_with_no_positive_and_sign_does_not() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; every operand is a valid Int.
        unsafe {
            let min = praxis_alloc_int(ctx, i64::MIN);
            let r = praxis_int_abs(ctx, min);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
            assert_eq!(r.as_ptr(), rt.immortals().unit().as_ptr());
        }
        let _ = rt.take_fault();
        // SAFETY: ctx wired; every operand is a valid Int.
        unsafe {
            let min = praxis_alloc_int(ctx, i64::MIN);
            assert_eq!(praxis_int_load(ctx, praxis_int_sign(ctx, min)), -1);
            let max = praxis_alloc_int(ctx, i64::MAX);
            assert_eq!(praxis_int_load(ctx, praxis_int_abs(ctx, max)), i64::MAX);
            assert_eq!(praxis_int_load(ctx, praxis_int_sign(ctx, max)), 1);
            assert!(!rt.has_pending_fault());
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn overflow_sets_fault_and_returns_sentinel() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; operands are valid Ints.
        unsafe {
            let a = praxis_alloc_int(ctx, i64::MAX);
            let b = praxis_alloc_int(ctx, 1);
            let s = praxis_int_add(ctx, a, b);
            // The fault is set; the return is the Unit sentinel.
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
            assert_eq!(s.as_ptr(), rt.immortals().unit().as_ptr());
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn division_by_zero_sets_fault() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let a = praxis_alloc_int(ctx, 10);
            let b = praxis_alloc_int(ctx, 0);
            let _ = praxis_int_div(ctx, a, b);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::DivByZero);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn remainder_by_zero_sets_fault() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let a = praxis_alloc_int(ctx, 10);
            let b = praxis_alloc_int(ctx, 0);
            let _ = praxis_int_rem(ctx, a, b);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::DivByZero);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn subtraction_overflow_sets_fault() {
        // The add/sub/mul overflow paths are symmetric. Sub: `Int::MIN - 1`
        // overflows.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; operands are valid Ints.
        unsafe {
            let a = praxis_alloc_int(ctx, i64::MIN);
            let b = praxis_alloc_int(ctx, 1);
            let _ = praxis_int_sub(ctx, a, b);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn multiplication_overflow_sets_fault() {
        // `Int::MIN * -1` is the canonical mul overflow (same magnitude as
        // `Int::MAX + 1`).
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; operands are valid Ints.
        unsafe {
            let a = praxis_alloc_int(ctx, i64::MIN);
            let b = praxis_alloc_int(ctx, -1);
            let _ = praxis_int_mul(ctx, a, b);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn division_truncates_toward_zero() {
        // §4.12 / abi.rs comment: division truncates toward zero, so -7 / 2 == -3
        // (not -4 as floor division would give). Remainder takes the sign of the
        // dividend.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let a = praxis_alloc_int(ctx, -7);
            let b = praxis_alloc_int(ctx, 2);
            let q = praxis_int_div(ctx, a, b);
            assert!(!rt.has_pending_fault());
            assert_eq!(praxis_int_load(ctx, q), -3);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn remainder_truncates_toward_zero() {
        // -7 % 2 == -1 (remainder takes the dividend's sign under truncation).
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let a = praxis_alloc_int(ctx, -7);
            let b = praxis_alloc_int(ctx, 2);
            let r = praxis_int_rem(ctx, a, b);
            assert!(!rt.has_pending_fault());
            assert_eq!(praxis_int_load(ctx, r), -1);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn division_min_div_minus_one_overflows() {
        // Regression for the §10.4 no-panic-across-ABI contract: `Int::MIN / -1`
        // is the sole signed-division case that overflows. The raw `/` panics in
        // debug builds; the wrapper must instead fault `IntOverflow`.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; operands are valid Ints.
        unsafe {
            let a = praxis_alloc_int(ctx, i64::MIN);
            let b = praxis_alloc_int(ctx, -1);
            let _ = praxis_int_div(ctx, a, b);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn remainder_min_div_minus_one_overflows() {
        // Companion to the division regression: `Int::MIN % -1` traps in debug
        // builds even though the mathematical remainder is 0, because the
        // corresponding quotient overflows. The wrapper must fault instead.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; operands are valid Ints.
        unsafe {
            let a = praxis_alloc_int(ctx, i64::MIN);
            let b = praxis_alloc_int(ctx, -1);
            let _ = praxis_int_rem(ctx, a, b);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn comparisons_yield_bools() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let one = praxis_alloc_int(ctx, 1);
            let two = praxis_alloc_int(ctx, 2);
            assert_eq!(praxis_bool_load(ctx, praxis_int_lt(ctx, one, two)), 1);
            assert_eq!(praxis_bool_load(ctx, praxis_int_gt(ctx, one, two)), 0);
            assert_eq!(praxis_bool_load(ctx, praxis_int_eq(ctx, one, one)), 1);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn neg_of_min_overflows() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let min = praxis_alloc_int(ctx, i64::MIN);
            let _ = praxis_int_neg(ctx, min);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IntOverflow);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn check_fault_reports_pending() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            assert_eq!(praxis_check_fault(ctx), 0);
            let a = praxis_alloc_int(ctx, 1);
            let b = praxis_alloc_int(ctx, 0);
            let _ = praxis_int_div(ctx, a, b);
            assert_eq!(praxis_check_fault(ctx), 1);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn alloc_text_round_trips() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let s = "héllo";
        // SAFETY: ctx wired; `bytes` is a valid UTF-8 buffer for the call.
        unsafe {
            let r = praxis_alloc_text(ctx, s.as_ptr(), s.len());
            assert_eq!(r.as_text(), "héllo");
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn alloc_bool_round_trips_value() {
        // This pins the *value*; `bool_and_unit_abi_allocations_reuse_runtime_singletons`
        // pins the identity. Bool equality is structural (§5.5).
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let t = praxis_alloc_bool(ctx, 1);
            let f = praxis_alloc_bool(ctx, 0);
            assert_eq!(praxis_bool_load(ctx, t), 1);
            assert_eq!(praxis_bool_load(ctx, f), 0);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn fault_clear_default_is_none() {
        let f = Fault::clear();
        assert!(!f.is_pending());
        assert_eq!(f.kind(), FaultKind::None);
    }

    // --- Vec[T] collection wrappers ----------------------------------------

    #[test]
    fn vec_new_is_empty() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; INT is a valid static descriptor.
        unsafe {
            let v = praxis_vec_new(ctx, &crate::scalars::INT as *const _);
            assert_eq!(praxis_bool_load(ctx, praxis_vec_is_empty(ctx, v)), 1);
            assert_eq!(praxis_int_load(ctx, praxis_vec_len(ctx, v)), 0);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn vec_push_grows_and_get_reads_back() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; push mutates the vec in place (returns Unit), so we
        // keep using the same `v` GcRef throughout.
        unsafe {
            let v = praxis_vec_new(ctx, &crate::scalars::INT as *const _);
            let a = praxis_alloc_int(ctx, 10);
            let b = praxis_alloc_int(ctx, 20);
            let c = praxis_alloc_int(ctx, 30);
            let _ = praxis_vec_push(ctx, v, a);
            let _ = praxis_vec_push(ctx, v, b);
            let _ = praxis_vec_push(ctx, v, c);
            assert_eq!(praxis_int_load(ctx, praxis_vec_len(ctx, v)), 3);
            let i0 = praxis_alloc_int(ctx, 0);
            let i2 = praxis_alloc_int(ctx, 2);
            assert_eq!(praxis_int_load(ctx, praxis_vec_get(ctx, v, i0)), 10);
            assert_eq!(praxis_int_load(ctx, praxis_vec_get(ctx, v, i2)), 30);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn vec_get_out_of_bounds_faults() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let v = praxis_vec_new(ctx, &crate::scalars::INT as *const _);
            let one = praxis_alloc_int(ctx, 1);
            let _ = praxis_vec_get(ctx, v, one); // empty vec, index 0
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IndexOutOfBounds);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    /// `praxis_vec_set` and `praxis_deque_set` **replace** — the property that
    /// separates them from the push beside them, and the one a store row pointed
    /// at the wrong wrapper would break silently.
    ///
    /// So each assertion is about what appending would get wrong: the length is
    /// unchanged, the neighbours are unchanged, an index one past the end faults
    /// instead of growing the collection, and a value of the wrong type is
    /// refused rather than retagging an explicitly typed collection.
    #[test]
    fn a_sequence_store_replaces_and_never_appends() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; the stores mutate in place, so the same `GcRef`s
        // stay valid throughout.
        unsafe {
            let v = praxis_vec_new(ctx, &crate::scalars::INT as *const _);
            for n in [10, 20, 30] {
                let _ = praxis_vec_push(ctx, v, praxis_alloc_int(ctx, n));
            }
            let d = praxis_deque_new(ctx, &crate::scalars::INT as *const _);
            for n in [10, 20] {
                let _ = praxis_deque_push_back(ctx, d, praxis_alloc_int(ctx, n));
            }

            let one = praxis_alloc_int(ctx, 1);
            let ninety_nine = praxis_alloc_int(ctx, 99);
            let _ = praxis_vec_set(ctx, v, one, ninety_nine);
            let _ = praxis_deque_set(ctx, d, one, ninety_nine);
            assert!(!rt.has_pending_fault());

            assert_eq!(praxis_int_load(ctx, praxis_vec_len(ctx, v)), 3);
            assert_eq!(praxis_int_load(ctx, praxis_deque_len(ctx, d)), 2);
            let zero = praxis_alloc_int(ctx, 0);
            let two = praxis_alloc_int(ctx, 2);
            assert_eq!(praxis_int_load(ctx, praxis_vec_get(ctx, v, zero)), 10);
            assert_eq!(praxis_int_load(ctx, praxis_vec_get(ctx, v, one)), 99);
            assert_eq!(praxis_int_load(ctx, praxis_vec_get(ctx, v, two)), 30);
            assert_eq!(praxis_int_load(ctx, praxis_deque_get(ctx, d, zero)), 10);
            assert_eq!(praxis_int_load(ctx, praxis_deque_get(ctx, d, one)), 99);

            // One past the end, and a negative index, are both out of range —
            // and neither grows the collection, which is what a store that fell
            // through to the appending wrapper would do.
            let three = praxis_alloc_int(ctx, 3);
            let neg = praxis_alloc_int(ctx, -1);
            type Store = unsafe extern "C" fn(*mut RuntimeContext, GcRef, GcRef, GcRef) -> GcRef;
            type Len = unsafe extern "C" fn(*mut RuntimeContext, GcRef) -> GcRef;
            for (recv, idx, store, len_of) in [
                (v, three, praxis_vec_set as Store, praxis_vec_len as Len),
                (v, neg, praxis_vec_set as Store, praxis_vec_len as Len),
                (d, two, praxis_deque_set as Store, praxis_deque_len as Len),
                (d, neg, praxis_deque_set as Store, praxis_deque_len as Len),
            ] {
                let before = praxis_int_load(ctx, len_of(ctx, recv));
                let _ = store(ctx, recv, idx, ninety_nine);
                assert!(rt.has_pending_fault(), "an out-of-range store must fault");
                assert_eq!(rt.take_fault(), Some(FaultKind::IndexOutOfBounds));
                assert_eq!(
                    praxis_int_load(ctx, len_of(ctx, recv)),
                    before,
                    "a faulting store must not have grown the collection"
                );
            }

            // A `Vec[Int]` refuses a `Float` rather than retagging itself.
            let float = praxis_alloc_float(ctx, 1.5_f64.to_bits() as i64);
            let _ = praxis_vec_set(ctx, v, zero, float);
            assert_eq!(rt.take_fault(), Some(FaultKind::TypeMismatch));
            assert_eq!(praxis_int_load(ctx, praxis_vec_get(ctx, v, zero)), 10);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn vec_push_many_survive_collection() {
        // Stress: root the receiver/current element exactly as generated code
        // does, push enough elements to force multiple automatic collections,
        // and leave one unrooted allocation per iteration so collection is
        // observable as a live-registry shrink.
        //
        // Both the pushed element and the deliberately-unrooted allocation are
        // offset past the interned range: an interned `Int` is never registered,
        // so an in-range element would trip the shrink test on iteration zero
        // and neither the collection nor the rooting would be exercised (see
        // `UNINTERNED`). The offset is carried through the spot checks below so
        // the values read back are still the values pushed.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; push mutates in place so `v` stays valid throughout.
        unsafe {
            let v = praxis_vec_new(ctx, &crate::scalars::INT as *const _);
            let mut frame = push_frame(ctx, SlotCount::new(2).unwrap());
            frame.set(0, v);
            let mut observed_reclamation = false;
            for i in 0..5000_i64 {
                let before_alloc = rt.heap().stats().live_count;
                let elem = praxis_alloc_int(ctx, UNINTERNED + i);
                if rt.heap().stats().live_count < before_alloc.saturating_add(1) {
                    observed_reclamation = true;
                }
                frame.set(1, elem);
                let before_push = rt.heap().stats().live_count;
                let _ = praxis_vec_push(ctx, v, elem);
                if rt.heap().stats().live_count < before_push {
                    observed_reclamation = true;
                }
                frame.clear(1);
                let _ = rt.alloc_int(-UNINTERNED - i - 1);
            }
            assert!(
                observed_reclamation,
                "the test must observe an automatic collection, not merely allocation pressure"
            );
            assert_eq!(praxis_int_load(ctx, praxis_vec_len(ctx, v)), 5000);
            // Spot-check first/middle/last. The *indices* stay small (they are
            // interned, which is fine — nothing here watches them); the values
            // carry the offset the elements were pushed with.
            let zero = praxis_alloc_int(ctx, 0);
            assert_eq!(
                praxis_int_load(ctx, praxis_vec_get(ctx, v, zero)),
                UNINTERNED
            );
            let middle = praxis_alloc_int(ctx, 2500);
            assert_eq!(
                praxis_int_load(ctx, praxis_vec_get(ctx, v, middle)),
                UNINTERNED + 2500
            );
            let last = praxis_alloc_int(ctx, 4999);
            assert_eq!(
                praxis_int_load(ctx, praxis_vec_get(ctx, v, last)),
                UNINTERNED + 4999
            );
            drop(frame);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn vec_get_negative_index_faults() {
        // The `idx < 0` guard in `praxis_vec_get`: a negative index is out of
        // bounds, not a wrapped-around large one.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let v = praxis_vec_new(ctx, &crate::scalars::INT as *const _);
            let a = praxis_alloc_int(ctx, 1);
            let _ = praxis_vec_push(ctx, v, a); // non-empty vec, so only the sign can fail
            let neg = praxis_alloc_int(ctx, -1);
            let _ = praxis_vec_get(ctx, v, neg);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IndexOutOfBounds);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn text_get_negative_index_faults() {
        // Companion to `vec_get_negative_index_faults`, for `praxis_text_get`'s
        // own `idx < 0` guard.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let s = "ab";
            let text = praxis_alloc_text(ctx, s.as_ptr(), s.len());
            let neg = praxis_alloc_int(ctx, -1);
            let _ = praxis_text_get(ctx, text, neg);
            assert!(rt.has_pending_fault());
            assert_eq!(rt.fault(), FaultKind::IndexOutOfBounds);
        }
        let _ = rt.take_fault();
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-086, the runtime half.** `praxis_text_get` allocates a `Char`.
    ///
    /// The catalog's twin (`the_two_text_reads_answer_a_char`) is pure data and
    /// cannot see this; this is pure runtime and cannot see that. Both halves
    /// are needed, and they must hold together: with only one, a `Char`-typed
    /// value routes into `praxis_char_load`, whose `read_scalar` answers `None`
    /// against the `INT` descriptor and panics.
    #[test]
    fn text_get_answers_a_char_object() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            // `"sddddd"[4]` must be `'d'` and not `100`: an object carrying the
            // `INT` descriptor would have the right value and the wrong type.
            let s = "sddddd";
            let text = praxis_alloc_text(ctx, s.as_ptr(), s.len());
            let four = praxis_alloc_int(ctx, 4);
            let got = praxis_text_get(ctx, text, four);
            assert!(!rt.has_pending_fault());
            assert!(
                std::ptr::eq(got.descriptor(), &crate::scalars::CHAR),
                "ADR-086: the read answers a Char, not the char's scalar value"
            );
            assert_eq!(got.as_char(), 'd');

            // Scalar-not-byte indexing, pinned at the runtime level too: `é` is
            // one scalar and two UTF-8 bytes, so a byte index would answer 0xC3.
            let u = "héllo";
            let utext = praxis_alloc_text(ctx, u.as_ptr(), u.len());
            let one = praxis_alloc_int(ctx, 1);
            let got = praxis_text_get(ctx, utext, one);
            assert!(!rt.has_pending_fault());
            assert_eq!(got.as_char(), 'é');
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-115 at the ABI, on the shape that decides it.** `t.len()` and
    /// `t[i]` are defined on scalars (§4.3, ADR-086); indexing bytes is an
    /// optimization licensed by the count, and the licence must be refused on
    /// every text where it would be wrong.
    ///
    /// The cases are chosen to break a byte-indexing implementation that only
    /// looked at the text's own leading byte or only at its first scalar: a
    /// multi-byte scalar at the start, in the middle, at the end, a four-byte
    /// one, and a slice whose own bytes are all one-byte but whose owner's are
    /// not.
    #[test]
    fn a_text_reads_by_scalar_wherever_the_multi_byte_scalar_sits() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired for every call in this block.
        unsafe {
            for src in [
                "",
                "abc",
                "\u{0}\u{7f}",
                "éabc",
                "abéc",
                "abcé",
                "a\u{1F600}b",
                "\u{20AC}\u{20AC}",
                "héllo wörld",
            ] {
                let text = praxis_alloc_text(ctx, src.as_ptr(), src.len());
                let expected: Vec<char> = src.chars().collect();

                let len = praxis_text_len(ctx, text);
                assert!(!rt.has_pending_fault(), "{src:?}");
                assert_eq!(len.as_int(), expected.len() as i64, "{src:?}");

                let empty = praxis_text_is_empty(ctx, text);
                assert_eq!(empty.as_bool(), expected.is_empty(), "{src:?}");

                for (i, want) in expected.iter().enumerate() {
                    let idx = praxis_alloc_int(ctx, i as i64);
                    let got = praxis_text_get(ctx, text, idx);
                    assert!(!rt.has_pending_fault(), "{src:?}[{i}]");
                    assert!(
                        std::ptr::eq(got.descriptor(), &crate::scalars::CHAR),
                        "{src:?}[{i}] answers a Char (ADR-086)"
                    );
                    assert_eq!(got.as_char(), *want, "{src:?}[{i}]");
                }

                // One past the end faults, whichever path answered above.
                let past = praxis_alloc_int(ctx, expected.len() as i64);
                let _ = praxis_text_get(ctx, text, past);
                assert!(rt.has_pending_fault(), "{src:?}[{}]", expected.len());
                assert_eq!(rt.fault(), FaultKind::IndexOutOfBounds);
                let _ = rt.take_fault();
            }

            // A view whose own bytes are all one-byte, inside an owner whose
            // are not. The answers are the same as the owner's corresponding
            // scalars; the byte-index path is refused because the licence is
            // the owner's to give.
            let owner_src = "héllo wörld";
            let owner = praxis_alloc_text(ctx, owner_src.as_ptr(), owner_src.len());
            // "llo " — bytes [3, 7) of the owner, all below 0x80.
            let view = rt
                .alloc_text_slice(owner, 3, 4)
                .expect("[3, 7) is on scalar boundaries");
            let len = praxis_text_len(ctx, view);
            assert_eq!(len.as_int(), 4);
            for (i, want) in "llo ".chars().enumerate() {
                let idx = praxis_alloc_int(ctx, i as i64);
                let got = praxis_text_get(ctx, view, idx);
                assert!(!rt.has_pending_fault());
                assert_eq!(got.as_char(), want);
            }
        }
        unsafe { drop_ctx(ctx) };
    }

    /// **ADR-086's narrowing half.** `Int.to_char()` reaches the same
    /// range check `praxis_alloc_char` does, because they share one helper.
    ///
    /// A wrapper that forwarded `value as u32` without the check would answer
    /// `'A'` for `0x1_0000_0041` instead of faulting, which is the case that
    /// proves this door reaches the shared guard rather than restating it.
    #[test]
    fn int_to_char_rejects_what_is_not_a_scalar_value() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            for bad in [-1_i64, 0xD800, 0x11_0000, 0x1_0000_0041] {
                let n = praxis_alloc_int(ctx, bad);
                let got = praxis_int_to_char(ctx, n);
                assert!(rt.has_pending_fault(), "{bad} must not answer a Char");
                assert_eq!(rt.fault(), FaultKind::InvalidChar, "{bad}");
                assert!(std::ptr::eq(got.descriptor(), &crate::scalars::UNIT));
                let _ = rt.take_fault();
            }

            // …and the round trip holds for one that is.
            let n = praxis_alloc_int(ctx, 233);
            let got = praxis_int_to_char(ctx, n);
            assert!(!rt.has_pending_fault());
            assert_eq!(got.as_char(), 'é');
            let back = praxis_char_to_int(ctx, got);
            assert!(!rt.has_pending_fault());
            assert_eq!(back.as_int(), 233);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn alloc_text_empty_string_round_trips() {
        // The `len == 0` branch in `praxis_alloc_text` treats an empty buffer as
        // the empty slice. An empty Text must format as "".
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; null pointer + zero length is the documented empty path.
        unsafe {
            let r = praxis_alloc_text(ctx, std::ptr::null(), 0);
            assert_eq!(r.as_text(), "");
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn vec_new_with_null_descriptor_defaults_to_int() {
        // A null element descriptor is kept null — "the caller has no static
        // element type" — and the vec must still be usable.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired; null descriptor is the handled default case.
        unsafe {
            let v = praxis_vec_new(ctx, std::ptr::null());
            assert_eq!(praxis_bool_load(ctx, praxis_vec_is_empty(ctx, v)), 1);
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn vec_push_rejects_a_value_with_the_wrong_descriptor() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let length_after;
        unsafe {
            let ints = praxis_vec_new(ctx, &crate::scalars::INT as *const _);
            let float = praxis_alloc_float(ctx, 1.5_f64.to_bits() as i64);
            let _ = praxis_vec_push(ctx, ints, float);
            length_after = ints.as_vec().len();
        }
        unsafe { drop_ctx(ctx) };

        assert_eq!(
            length_after, 0,
            "an ABI type mismatch must not silently retag and mutate an explicitly typed Vec[Int]"
        );
    }

    #[test]
    fn alloc_char_rejects_values_that_only_become_valid_after_truncation() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let result = unsafe { praxis_alloc_char(ctx, 0x1_0000_0041) };
        let unit = rt.immortals().unit();
        unsafe { drop_ctx(ctx) };

        assert_eq!(
            result.as_ptr(),
            unit.as_ptr(),
            "the ABI must range-check the i64 code point before converting it to u32"
        );
        // And the fault it raises must name itself: a `FaultKind::None` here
        // would have the host report "no fault" while generated code took its
        // fault path.
        assert_eq!(rt.fault(), FaultKind::InvalidChar);
        assert!(rt.has_pending_fault());
    }

    /// A negative code point is out of range for the same reason a too-large
    /// one is, and `as u32` wraps it into the valid range just as silently.
    #[test]
    fn alloc_char_rejects_a_negative_code_point() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let result = unsafe { praxis_alloc_char(ctx, -1) };
        let unit = rt.immortals().unit();
        unsafe { drop_ctx(ctx) };

        assert_eq!(result.as_ptr(), unit.as_ptr());
        assert_eq!(rt.fault(), FaultKind::InvalidChar);
    }

    /// **ADR-111.** Input that is not UTF-8 faults at the `read`, because that
    /// is where the bytes stop being the compiler's and start being the host's.
    ///
    /// Asserted through `praxis_get_input` and never by feeding
    /// `praxis_alloc_text` bad bytes directly: that is a violated precondition,
    /// so it panics through `abi_guard!`, and `praxis_alloc_text`'s
    /// `Allocates` row makes the resulting `Panic` fault unobservable — the
    /// process aborts instead of failing. The property is that a program
    /// reading non-UTF-8 input gets `InvalidText` at its `read`.
    ///
    /// `praxis run` cannot reach this: `lazy_stdin::read` goes through
    /// `std::io::read_to_string` and exits 2 on non-UTF-8 stdin before the
    /// runtime sees a byte (`praxis-cli/src/run.rs`). The reachable caller is an
    /// embedder that installs its own `InputReader`, which is exactly what this
    /// test is.
    #[test]
    fn input_that_is_not_utf8_faults_at_the_read() {
        fn not_utf8() -> Vec<u8> {
            vec![0xF0, 0x28, 0x8C, 0x28]
        }
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        crate::input::install_input_reader(not_utf8);
        let result = unsafe { praxis_get_input(ctx) };
        let unit = rt.immortals().unit();
        crate::input::clear_input_reader();
        unsafe { drop_ctx(ctx) };

        assert_eq!(rt.fault(), FaultKind::InvalidText);
        assert!(rt.has_pending_fault());
        assert_eq!(
            result.as_ptr(),
            unit.as_ptr(),
            "the fault path answers §10.4's defined dummy, not a half-built Text"
        );
    }

    /// The mutation companion, and it is required: a `praxis_get_input` that
    /// faulted on *every* input would pass the gate above.
    ///
    /// Multi-byte on purpose — a validation that accepted only ASCII would also
    /// pass a test written with `"hi"`.
    #[test]
    fn input_that_is_utf8_still_becomes_the_buffer() {
        fn multibyte() -> Vec<u8> {
            "héllo wörld".as_bytes().to_vec()
        }
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        crate::input::install_input_reader(multibyte);
        let result = unsafe { praxis_get_input(ctx) };
        let contents = result.as_text().to_string();
        let descriptor = result.descriptor().name;
        crate::input::clear_input_reader();
        unsafe { drop_ctx(ctx) };

        assert!(!rt.has_pending_fault(), "fault: {:?}", rt.fault());
        assert_eq!(descriptor, "Text");
        assert_eq!(contents, "héllo wörld");
    }

    #[test]
    fn grid_cell_vectors_preserve_the_grid_element_descriptor() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let cell = rt.alloc_text("x");
        let grid = rt.alloc_grid(&crate::text::TEXT, vec![cell], 1);
        let descriptors;
        unsafe {
            let zero = praxis_alloc_int(ctx, 0);
            let cells = praxis_grid_cells(ctx, grid);
            let row = praxis_grid_row(ctx, grid, zero);
            let column = praxis_grid_column(ctx, grid, zero);
            descriptors = [
                (*vec_payload(cells).element_descriptor).id(),
                (*vec_payload(row).element_descriptor).id(),
                (*vec_payload(column).element_descriptor).id(),
            ];
        }
        unsafe { drop_ctx(ctx) };

        assert!(
            descriptors.iter().all(|id| *id == crate::text::TEXT.id()),
            "cells(), row(), and column() must return Vec values tagged with the Grid cell type"
        );
    }

    #[test]
    fn constructed_grid_cells_satisfy_the_declared_element_descriptor() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let cell_descriptor;
        unsafe {
            let grid = praxis_grid_new(ctx, &crate::scalars::INT as *const _, 1, 1);
            cell_descriptor = grid_payload(grid).items[0].descriptor().id();
        }
        unsafe { drop_ctx(ctx) };

        assert_eq!(
            cell_descriptor,
            crate::scalars::INT.id(),
            "a live Grid[Int] must never contain a Unit placeholder observable through get/format/hash"
        );
    }

    #[test]
    fn grid_position_vectors_use_the_point_tuple_descriptor() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let cell = rt.alloc_int(1);
        let grid = rt.alloc_grid(&crate::scalars::INT, vec![cell], 1);
        let descriptors;
        unsafe {
            let point = alloc_point(ctx, 0, 0);
            let positions = praxis_grid_positions(ctx, grid);
            let neighbors4 = praxis_grid_neighbors4(ctx, grid, point);
            let neighbors8 = praxis_grid_neighbors8(ctx, grid, point);
            let matches = praxis_grid_find_all(ctx, grid, cell);
            descriptors = [
                (*vec_payload(positions).element_descriptor).id(),
                (*vec_payload(neighbors4).element_descriptor).id(),
                (*vec_payload(neighbors8).element_descriptor).id(),
                (*vec_payload(matches).element_descriptor).id(),
            ];
        }
        unsafe { drop_ctx(ctx) };

        assert!(
            descriptors
                .iter()
                .all(|id| *id == crate::tuples::TUPLE.id()),
            "position-producing Grid methods must return Vec[Tuple[Int, Int]] at runtime"
        );
    }

    /// An extent must be validated before it becomes a `usize`. Unchecked,
    /// `vec![unit; (w as usize) * (h as usize)]` turns `-1` into `usize::MAX`,
    /// and the products either overflow (a capacity panic across `extern "C"`)
    /// or ask the host for terabytes (an OOM abort). The wrapper must answer
    /// with a fault, and the heap must be untouched — a partly-built grid is as
    /// bad as a crash.
    #[test]
    fn a_negative_or_absurd_grid_extent_faults_instead_of_allocating() {
        let absurd = GridExtent::MAX_CELLS as i64 + 1;
        for (width, height) in [
            (-1_i64, 4_i64),
            (4, -1),
            (-1, -1),
            (i64::MIN, 1),
            // Overflows the `usize` multiplication outright.
            (i64::MAX, 2),
            (1 << 40, 1 << 40),
            // Multiplies cleanly and is still an allocation no host can serve.
            (absurd, 1),
            (1, absurd),
        ] {
            let mut rt = Runtime::new();
            let ctx = wired_ctx(&mut rt);
            let live_before = rt.heap().stats().live_count;
            let result =
                unsafe { praxis_grid_new(ctx, &crate::scalars::INT as *const _, width, height) };
            let live_after = rt.heap().stats().live_count;
            let unit = rt.immortals().unit();
            unsafe { drop_ctx(ctx) };

            assert_eq!(
                rt.fault(),
                FaultKind::InvalidSize,
                "Grid[Int]({width}, {height}) must fault"
            );
            assert_eq!(
                result.as_ptr(),
                unit.as_ptr(),
                "a faulted Grid[Int]({width}, {height}) returns the Unit sentinel"
            );
            assert_eq!(
                live_after, live_before,
                "a rejected Grid[Int]({width}, {height}) allocates nothing"
            );
        }
    }

    /// The other side of the same gate: an extent the runtime *can* serve still
    /// builds the grid it asked for, including the degenerate zero cases.
    #[test]
    fn an_in_range_grid_extent_still_builds_its_cells() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let shapes: Vec<(i64, i64, usize, usize)> = vec![(0, 0, 0, 0), (0, 5, 0, 0), (3, 2, 6, 3)];
        let mut observed = Vec::new();
        for (width, height, _, _) in &shapes {
            let grid =
                unsafe { praxis_grid_new(ctx, &crate::scalars::INT as *const _, *width, *height) };
            let p = unsafe { grid_payload(grid) };
            observed.push((p.items.len(), p.width));
        }
        unsafe { drop_ctx(ctx) };

        assert_eq!(rt.fault(), FaultKind::None, "no in-range extent faults");
        for ((w, h, cells, width), (got_cells, got_width)) in shapes.iter().zip(observed) {
            assert_eq!(
                (got_cells, got_width),
                (*cells, *width),
                "Grid[Int]({w}, {h}) shape"
            );
        }
    }

    /// **ADR-146.** `Vec(n, fill)` builds `n` slots, all of them the fill, and
    /// the empty case is a `Vec` and not a fault.
    #[test]
    fn vec_filled_builds_n_copies_of_one_value() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let observed: Vec<(usize, bool)> = [0_i64, 1, 7]
            .into_iter()
            .map(|n| unsafe {
                let count = praxis_alloc_int(ctx, n);
                let fill = praxis_alloc_int(ctx, 42);
                let v = praxis_vec_filled(ctx, &crate::scalars::INT as *const _, count, fill);
                let p = vec_payload(v);
                // Every slot is the *same* reference, which is the aliasing
                // ADR-146 decision 4 states rather than n copies of a value.
                let all_same = p.items.iter().all(|item| item.as_ptr() == fill.as_ptr());
                (p.items.len(), all_same)
            })
            .collect();
        unsafe { drop_ctx(ctx) };

        assert_eq!(rt.fault(), FaultKind::None, "no in-range count faults");
        assert_eq!(observed, vec![(0, true), (1, true), (7, true)]);
    }

    /// The other half of ADR-041 decision 1, for the newtype it added: a count
    /// the runtime cannot serve is a fault and not an allocation, and the heap
    /// is untouched — a half-built `Vec` is as bad as a crash.
    #[test]
    fn vec_filled_refuses_a_negative_or_absurd_count() {
        let absurd = crate::collections::VecExtent::MAX_ITEMS as i64 + 1;
        for n in [-1_i64, i64::MIN, absurd, i64::MAX] {
            let mut rt = Runtime::new();
            let ctx = wired_ctx(&mut rt);
            let (result, live_before, live_after, unit) = unsafe {
                let count = praxis_alloc_int(ctx, n);
                let fill = praxis_alloc_int(ctx, 0);
                let before = rt.heap().stats().live_count;
                let r = praxis_vec_filled(ctx, &crate::scalars::INT as *const _, count, fill);
                (
                    r,
                    before,
                    rt.heap().stats().live_count,
                    rt.immortals().unit(),
                )
            };
            unsafe { drop_ctx(ctx) };

            assert_eq!(rt.fault(), FaultKind::InvalidSize, "Vec({n}, 0) must fault");
            assert_eq!(
                result.as_ptr(),
                unit.as_ptr(),
                "a faulted Vec({n}, 0) returns the Unit sentinel"
            );
            assert_eq!(
                live_after, live_before,
                "a rejected Vec({n}, 0) allocates nothing"
            );
        }
    }

    /// A declared element type the fill is not is a `TypeMismatch`, through the
    /// same `adopt_or_reject` a `push` goes through — not a silent retag of the
    /// collection to the fill's type, which is the mislabelling defect one
    /// level down.
    /// A *null* static descriptor adopts instead, which is what "the caller has
    /// no static element type" already means for `praxis_vec_new`.
    #[test]
    fn vec_filled_reconciles_its_element_descriptor() {
        let mut rejecting = Runtime::new();
        let ctx = wired_ctx(&mut rejecting);
        unsafe {
            let count = praxis_alloc_int(ctx, 3);
            let text = praxis_alloc_text(ctx, b"x".as_ptr(), 1);
            praxis_vec_filled(ctx, &crate::scalars::INT as *const _, count, text);
            drop_ctx(ctx);
        }
        assert_eq!(
            rejecting.fault(),
            FaultKind::TypeMismatch,
            "a `Vec[Int]` filled with a `Text` is a mislabelled element descriptor"
        );

        let mut adopting = Runtime::new();
        let ctx = wired_ctx(&mut adopting);
        let adopted = unsafe {
            let count = praxis_alloc_int(ctx, 3);
            let text = praxis_alloc_text(ctx, b"x".as_ptr(), 1);
            let v = praxis_vec_filled(ctx, std::ptr::null(), count, text);
            let matches = std::ptr::eq(vec_payload(v).element_descriptor, text.descriptor());
            drop_ctx(ctx);
            matches
        };
        assert_eq!(adopting.fault(), FaultKind::None);
        assert!(
            adopted,
            "a null static descriptor adopts the fill's, as `praxis_vec_new` already does"
        );
    }

    /// **ADR-146 decision 6.** `Grid(w, h, fill)` accepts a fill
    /// `praxis_grid_new` cannot invent: `default_cell` has no zero value for a
    /// composite and answers `TypeMismatch`, and the explicit fill is exactly
    /// what removes the question. The contrast is the assertion — both calls
    /// are in one test so a later change that reintroduced `default_cell` here
    /// fails rather than passes quietly.
    #[test]
    fn grid_filled_accepts_a_composite_fill_where_grid_new_cannot() {
        let mut inventing = Runtime::new();
        let ctx = wired_ctx(&mut inventing);
        unsafe {
            praxis_grid_new(ctx, &crate::collections::VEC as *const _, 2, 2);
            drop_ctx(ctx);
        }
        assert_eq!(
            inventing.fault(),
            FaultKind::TypeMismatch,
            "`praxis_grid_new` still has no zero value for a `Vec` cell"
        );

        let mut supplied = Runtime::new();
        let ctx = wired_ctx(&mut supplied);
        let (cells, all_same) = unsafe {
            let inner = praxis_vec_new(ctx, &crate::scalars::INT as *const _);
            let (w, h) = (praxis_alloc_int(ctx, 2), praxis_alloc_int(ctx, 2));
            let g = praxis_grid_filled(ctx, &crate::collections::VEC as *const _, w, h, inner);
            let p = grid_payload(g);
            let same = p.items.iter().all(|c| c.as_ptr() == inner.as_ptr());
            let len = p.items.len();
            drop_ctx(ctx);
            (len, same)
        };
        assert_eq!(supplied.fault(), FaultKind::None);
        assert_eq!(cells, 4, "an explicit fill builds all four cells");
        assert!(
            all_same,
            "the four cells are one `Vec`, not four (ADR-146 decision 4)"
        );
    }

    /// `Grid(w, h, fill)` takes the extents `praxis_grid_new` refuses, through
    /// the same `GridExtent::new` — a fill changes nothing about the
    /// arithmetic.
    #[test]
    fn grid_filled_refuses_the_extents_grid_new_refuses() {
        let absurd = GridExtent::MAX_CELLS as i64 + 1;
        for (width, height) in [(-1_i64, 4_i64), (4, -1), (i64::MAX, 2), (absurd, 1)] {
            let mut rt = Runtime::new();
            let ctx = wired_ctx(&mut rt);
            let (result, live_before, live_after, unit) = unsafe {
                let (w, h) = (praxis_alloc_int(ctx, width), praxis_alloc_int(ctx, height));
                let fill = praxis_alloc_int(ctx, 0);
                let before = rt.heap().stats().live_count;
                let r = praxis_grid_filled(ctx, &crate::scalars::INT as *const _, w, h, fill);
                (
                    r,
                    before,
                    rt.heap().stats().live_count,
                    rt.immortals().unit(),
                )
            };
            unsafe { drop_ctx(ctx) };

            assert_eq!(
                rt.fault(),
                FaultKind::InvalidSize,
                "Grid({width}, {height}, 0) must fault"
            );
            assert_eq!(
                result.as_ptr(),
                unit.as_ptr(),
                "a faulted Grid({width}, {height}, 0) returns the Unit sentinel"
            );
            assert_eq!(
                live_after, live_before,
                "a rejected Grid({width}, {height}, 0) allocates nothing"
            );
        }
    }

    /// A member the set cannot hold is a fault, and a negative one does not
    /// vanish silently. Unchecked, `bs.insert(10^18)` would ask `Vec::resize`
    /// for 10^16 words — an OOM abort from inside `extern "C"`.
    #[test]
    fn a_bitset_member_outside_the_representable_range_faults() {
        for member in [-1_i64, i64::MIN, i64::MAX, BitIndex::MAX + 1] {
            let mut rt = Runtime::new();
            let ctx = wired_ctx(&mut rt);
            let words;
            unsafe {
                let bs = praxis_bitset_new(ctx);
                let value = praxis_alloc_int(ctx, member);
                let _ = praxis_bitset_insert(ctx, bs, value);
                words = bitset_payload(bs).words.len();
            }
            unsafe { drop_ctx(ctx) };

            assert_eq!(
                rt.fault(),
                FaultKind::InvalidSize,
                "BitSet.insert({member}) must fault"
            );
            assert_eq!(words, 0, "BitSet.insert({member}) must allocate no words");
        }
    }

    /// The words of a **live, heap-allocated** `BitSet`, reached the way
    /// generated code reaches them: through
    /// [`INLINE_BITSET_SITE`](crate::bitset::INLINE_BITSET_SITE), from the
    /// object base, with no knowledge of the payload beyond what the site
    /// carries (ADR-118 part 2).
    ///
    /// `a_backend_can_read_the_length_and_the_elements_out_of_a_live_payload`
    /// in `collections.rs` is this test for `Vec`; this is the second payload's
    /// copy of the same agreement between the emitted load and the layout.
    ///
    /// Compiled out under `std-vec-payload`: that arm has no site to name, so
    /// naming one fails the *build* rather than miscompiling a load.
    #[cfg(not(feature = "std-vec-payload"))]
    #[test]
    fn the_inline_bitset_site_addresses_a_live_bitsets_words() {
        use crate::bitset::INLINE_BITSET_SITE;

        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let (words, len) = unsafe {
            let bs = praxis_bitset_new(ctx);
            assert!(
                std::ptr::eq(INLINE_BITSET_SITE.type_id().descriptor(), bs.descriptor()),
                "the site names the descriptor the inline proof compares against"
            );
            for member in [0_i64, 63, 64, 200] {
                let value = praxis_alloc_int(ctx, member);
                let _ = praxis_bitset_insert(ctx, bs, value);
            }
            let base = bs.as_ptr().cast::<u8>().cast_const();
            (
                base.add(INLINE_BITSET_SITE.elements_offset())
                    .cast::<*const u64>()
                    .read(),
                base.add(INLINE_BITSET_SITE.len_offset())
                    .cast::<usize>()
                    .read(),
            )
        };

        assert_eq!(len, 4, "bit 200 lives in the fourth word");
        assert_eq!(
            INLINE_BITSET_SITE.element_shift(),
            3,
            "a word is eight bytes"
        );
        for member in [0_u64, 63, 64, 200] {
            // SAFETY: `member >> 6 < len`, and `words` is the live buffer the
            // site's displacement just answered.
            let w = unsafe { *words.add((member >> 6) as usize) };
            assert!(
                (w >> (member & 63)) & 1 == 1,
                "bit {member} read back through the site's displacements"
            );
        }
        unsafe { drop_ctx(ctx) };
    }

    /// Queries stay total: a value the set cannot hold is a value it does not
    /// contain, and removing one is a no-op. Neither may fault, and neither may
    /// grow the word vector.
    #[test]
    fn bitset_queries_outside_the_range_are_absent_rather_than_faults() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let (present, words) = unsafe {
            let bs = praxis_bitset_new(ctx);
            let huge = praxis_alloc_int(ctx, i64::MAX);
            let _ = praxis_bitset_remove(ctx, bs, huge);
            // The answer is the scalar channel's `0`/`1` (ADR-118 decision 6),
            // so there is no box to load it back out of.
            let answer = praxis_bitset_contains(ctx, bs, huge);
            (answer != 0, bitset_payload(bs).words.len())
        };
        unsafe { drop_ctx(ctx) };

        assert!(!present, "an unrepresentable member is absent");
        assert_eq!(words, 0, "a query allocates no words");
        assert_eq!(rt.fault(), FaultKind::None, "a query does not fault");
    }

    /// `(i64::MAX, i64::MAX).neighbors4()` must not overflow the offset addition
    /// and panic across `extern "C"`. Every such neighbour is outside every
    /// grid, so the answer is an empty Vec.
    #[test]
    fn neighbors_of_an_extreme_point_are_empty_rather_than_a_panic() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let cell = rt.alloc_int(1);
        let grid = rt.alloc_grid(&crate::scalars::INT, vec![cell], 1);
        let counts = unsafe {
            let mut counts = Vec::new();
            for (x, y) in [
                (i64::MAX, i64::MAX),
                (i64::MIN, i64::MIN),
                (i64::MAX, 0),
                (0, i64::MIN),
            ] {
                let point = alloc_point(ctx, x, y);
                counts.push((
                    vec_payload(praxis_grid_neighbors4(ctx, grid, point))
                        .items
                        .len(),
                    vec_payload(praxis_grid_neighbors8(ctx, grid, point))
                        .items
                        .len(),
                ));
            }
            counts
        };
        unsafe { drop_ctx(ctx) };

        assert!(
            counts.iter().all(|(n4, n8)| *n4 == 0 && *n8 == 0),
            "an out-of-range point has no in-grid neighbours: {counts:?}"
        );
        assert_eq!(rt.fault(), FaultKind::None);
    }

    /// A 3×3 grid of `Int`s holding `1..=9` in reading order, so a neighbour's
    /// cell names its own position.
    fn nine_grid(rt: &mut Runtime) -> GcRef {
        let cells: Vec<GcRef> = (1..=9).map(|n| rt.alloc_int(n)).collect();
        rt.alloc_grid(&crate::scalars::INT, cells, 3)
    }

    /// Every field of a neighbourhood record as `(name, Some((x, y)) | None)`,
    /// **in slot order** — which is what a field read indexes, so a test that
    /// reads through this is a test of the order and not only of the values.
    ///
    /// # Safety
    /// `record` must be an `Around4`/`Around8` `GcRef`.
    unsafe fn around_fields(record: GcRef) -> Vec<(&'static str, Option<(i64, i64)>)> {
        // SAFETY: the caller guarantees a record built under one of the two
        // neighbourhood schemas, so every field is an `Option[(Int, Int)]`.
        unsafe {
            let rp = &*(record.payload::<u8>() as *const crate::records::RecordPayload);
            let schema = &*rp.schema;
            schema
                .fields
                .iter()
                .zip(&rp.items)
                .map(|(field, value)| {
                    let ep = &*(value.payload::<u8>() as *const crate::enums::EnumPayload);
                    let point = (i64::from(ep.tag) == crate::enums::OPTION_SOME_TAG)
                        .then(|| point_xy(ep.items[0]));
                    (field.name, point)
                })
                .collect()
        }
    }

    /// `around4` names all four directions, and a direction that leaves the
    /// grid is `None` rather than absent.
    ///
    /// That is the whole difference from `neighbors4`, which answers a clipped
    /// `Vec` in which the corner case and the interior case are two lists of
    /// different lengths with no way to tell which entry was which direction.
    #[test]
    fn around4_answers_every_direction_and_the_missing_ones_are_none() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let grid = nine_grid(&mut rt);
        let (middle, corner, far_corner) = unsafe {
            let m = around_fields(praxis_grid_around4(ctx, grid, alloc_point(ctx, 1, 1)));
            let c = around_fields(praxis_grid_around4(ctx, grid, alloc_point(ctx, 0, 0)));
            let f = around_fields(praxis_grid_around4(ctx, grid, alloc_point(ctx, 2, 2)));
            (m, c, f)
        };
        unsafe { drop_ctx(ctx) };

        assert_eq!(
            middle,
            vec![
                ("up", Some((1, 0))),
                ("left", Some((0, 1))),
                ("right", Some((2, 1))),
                ("down", Some((1, 2))),
            ],
            "the plus in reading order, centre skipped"
        );
        assert_eq!(
            corner,
            vec![
                ("up", None),
                ("left", None),
                ("right", Some((1, 0))),
                ("down", Some((0, 1))),
            ]
        );
        assert_eq!(
            far_corner,
            vec![
                ("up", Some((2, 1))),
                ("left", Some((1, 2))),
                ("right", None),
                ("down", None),
            ]
        );
        assert_eq!(rt.fault(), FaultKind::None);
    }

    /// `around8`'s slots are the 3×3 block in reading order, centre skipped —
    /// which is what makes a printed `Around8` look like the block it
    /// describes, and what a diagonal read depends on.
    #[test]
    fn around8_is_a_3x3_block_in_reading_order() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let grid = nine_grid(&mut rt);
        let (middle, edge) = unsafe {
            let m = around_fields(praxis_grid_around8(ctx, grid, alloc_point(ctx, 1, 1)));
            let e = around_fields(praxis_grid_around8(ctx, grid, alloc_point(ctx, 1, 0)));
            (m, e)
        };
        unsafe { drop_ctx(ctx) };

        assert_eq!(
            middle,
            vec![
                ("up_left", Some((0, 0))),
                ("up", Some((1, 0))),
                ("up_right", Some((2, 0))),
                ("left", Some((0, 1))),
                ("right", Some((2, 1))),
                ("down_left", Some((0, 2))),
                ("down", Some((1, 2))),
                ("down_right", Some((2, 2))),
            ]
        );
        // The top edge: the whole first row of the block is off the grid, and
        // says so three times rather than shortening the answer.
        assert_eq!(
            edge,
            vec![
                ("up_left", None),
                ("up", None),
                ("up_right", None),
                ("left", Some((0, 0))),
                ("right", Some((2, 0))),
                ("down_left", Some((0, 1))),
                ("down", Some((1, 1))),
                ("down_right", Some((2, 1))),
            ]
        );
        assert_eq!(rt.fault(), FaultKind::None);
    }

    /// `neighbors_of_an_extreme_point_are_empty_rather_than_a_panic`'s case for
    /// the record and the counts: `grid_neighbor`'s `checked_add` is what keeps
    /// `(i64::MAX, i64::MAX)` from overflowing inside `extern "C"`, and every
    /// wrapper that steps a point has to be behind it.
    #[test]
    fn a_neighbourhood_of_an_extreme_point_is_all_absent_rather_than_a_panic() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let grid = nine_grid(&mut rt);
        let one = rt.alloc_int(1);
        let observed = unsafe {
            let mut observed = Vec::new();
            for (x, y) in [
                (i64::MAX, i64::MAX),
                (i64::MIN, i64::MIN),
                (i64::MAX, 0),
                (0, i64::MIN),
            ] {
                let point = alloc_point(ctx, x, y);
                let four = around_fields(praxis_grid_around4(ctx, grid, point));
                let eight = around_fields(praxis_grid_around8(ctx, grid, point));
                observed.push((
                    four.iter().filter(|(_, p)| p.is_some()).count(),
                    eight.len(),
                    eight.iter().filter(|(_, p)| p.is_some()).count(),
                    int_payload(praxis_grid_count4(ctx, grid, point, one)),
                    int_payload(praxis_grid_count8(ctx, grid, point, one)),
                ));
            }
            observed
        };
        unsafe { drop_ctx(ctx) };

        for row in &observed {
            assert_eq!(
                *row,
                (0, 8, 0, 0, 0),
                "an out-of-range point has no in-grid neighbours, and its \
                 record still has all eight fields: {observed:?}"
            );
        }
        assert_eq!(rt.fault(), FaultKind::None);
    }

    /// `count4`/`count8` count cells, so a direction with no cell is not one of
    /// them — a corner counts over three neighbours, not eight.
    ///
    /// Equality is the descriptor's, which is `praxis_grid_find`'s path: the
    /// grid holds `1..=9`, so each count is the size of a known subset.
    #[test]
    fn a_neighbourhood_count_counts_only_the_cells_that_are_there() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let grid = nine_grid(&mut rt);
        let counts = unsafe {
            let centre = alloc_point(ctx, 1, 1);
            let corner = alloc_point(ctx, 0, 0);
            let two = praxis_alloc_int(ctx, 2);
            let five = praxis_alloc_int(ctx, 5);
            let nine = praxis_alloc_int(ctx, 9);
            [
                // `2` is the cell above the centre: one orthogonal hit.
                int_payload(praxis_grid_count4(ctx, grid, centre, two)),
                // `9` is diagonal from the centre, so only the eight sees it.
                int_payload(praxis_grid_count4(ctx, grid, centre, nine)),
                int_payload(praxis_grid_count8(ctx, grid, centre, nine)),
                // The centre is never its own neighbour.
                int_payload(praxis_grid_count8(ctx, grid, centre, five)),
                // A corner's neighbourhood is two cells of four, three of eight.
                int_payload(praxis_grid_count4(ctx, grid, corner, five)),
                int_payload(praxis_grid_count8(ctx, grid, corner, five)),
            ]
        };
        unsafe { drop_ctx(ctx) };

        assert_eq!(counts, [1, 0, 1, 0, 0, 1]);
        assert_eq!(rt.fault(), FaultKind::None);
    }

    /// `map[key]` faults on an absent key where `.get` answers, and
    /// `praxis_counter_set` replaces a count where `praxis_counter_inc` only
    /// adds one.
    ///
    /// Here as well as in the JIT tests because §4.7's choice is the *runtime's*
    /// to make: the two map wrappers differ in one line, and a compiler that
    /// pointed both rows at `praxis_map_get` would still pass every type test.
    #[test]
    fn a_map_index_faults_where_get_answers_and_a_counter_set_replaces() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let (present, absent_get) = unsafe {
            let map = praxis_map_new(ctx, &crate::scalars::INT as *const _);
            let key = praxis_alloc_int(ctx, 1);
            let val = praxis_alloc_int(ctx, 42);
            praxis_map_insert(ctx, map, key, val);
            let present = int_payload(praxis_map_index(ctx, map, key));
            assert_eq!(rt.fault(), FaultKind::None, "a present key does not fault");
            // `.get` on an absent key is `None` and no fault…
            let other = praxis_alloc_int(ctx, 2);
            let absent_get = praxis_map_get(ctx, map, other);
            assert_eq!(rt.fault(), FaultKind::None, "`.get` does not fault");
            // …and the subscript on the same key faults.
            praxis_map_index(ctx, map, other);
            (present, absent_get)
        };
        assert_eq!(present, 42);
        assert_eq!(
            absent_get.descriptor().id(),
            crate::enums::ENUM.id(),
            "`.get` answers with absence, and absence is an `Option` value"
        );
        assert_eq!(
            rt.fault(),
            FaultKind::IndexOutOfBounds,
            "§4.7: indexing a missing key faults"
        );
        unsafe { drop_ctx(ctx) };

        // The counter half: `set` replaces, where `inc` adds one.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let (after_inc, after_set, len) = unsafe {
            let c = praxis_counter_new(ctx, &crate::scalars::INT as *const _);
            let key = praxis_alloc_int(ctx, 7);
            praxis_counter_inc(ctx, c, key);
            let after_inc = int_payload(praxis_counter_get(ctx, c, key));
            let five = praxis_alloc_int(ctx, 5);
            praxis_counter_set(ctx, c, key, five);
            let after_set = int_payload(praxis_counter_get(ctx, c, key));
            let len = int_payload(praxis_counter_len(ctx, c));
            (after_inc, after_set, len)
        };
        unsafe { drop_ctx(ctx) };
        assert_eq!(after_inc, 1);
        assert_eq!(after_set, 5, "a set replaces rather than adds");
        assert_eq!(len, 1, "and does not add a second entry for the same key");
        assert_eq!(rt.fault(), FaultKind::None);
    }

    /// `Map.get` is statically value-typed, so an absent key cannot answer the
    /// Unit sentinel: a value whose static type is `V` and whose runtime
    /// descriptor is `Unit` is a type confusion the program cannot detect.
    ///
    /// The assertion names the variant, not merely `!= UNIT`: the answer is the
    /// `None` of the runtime's own `option_schema`, which is what makes it match
    /// a program's `None` arm.
    #[test]
    fn absent_map_get_does_not_return_an_untyped_unit_sentinel() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let (missing, found);
        unsafe {
            let map = praxis_map_new(ctx, &crate::scalars::INT as *const _);
            let key = praxis_alloc_int(ctx, 1);
            missing = praxis_map_get(ctx, map, key);
            let value = praxis_alloc_int(ctx, 42);
            praxis_map_insert(ctx, map, key, value);
            found = praxis_map_get(ctx, map, key);
        }

        assert_ne!(
            missing.descriptor().id(),
            crate::scalars::UNIT.id(),
            "Map.get is statically value-typed; absence needs Option or a checked fault, not Unit"
        );
        assert_eq!(missing.descriptor().id(), crate::enums::ENUM.id());
        assert_eq!(
            enum_tag_of(missing),
            crate::enums::OPTION_NONE_TAG as u32,
            "absence is `None`"
        );
        assert_eq!(enum_tag_of(found), crate::enums::OPTION_SOME_TAG as u32);
        // …and the `Some` carries the value, rather than merely not being Unit.
        let payload = unsafe { praxis_enum_payload(ctx, found, 0) };
        assert_eq!(unsafe { praxis_int_load(ctx, payload) }, 42);
        unsafe { drop_ctx(ctx) };
    }

    /// A goal predicate that is `false` at every state, as a closure entry
    /// point. Rust-side `extern "C"` stands in for a JIT'd closure body: the
    /// oracle only cares that the pointer has the closure calling convention.
    ///
    /// # Safety
    /// Called only through [`ClosureOracle::call`], which upholds the ABI.
    unsafe extern "C" fn always_false(
        ctx: *mut RuntimeContext,
        _closure: GcRef,
        _state: GcRef,
    ) -> GcRef {
        // SAFETY: the oracle passes its own wired ctx.
        unsafe { bool_ref(ctx, false) }
    }

    /// A neighbour function with no neighbours: the walk visits the start and
    /// stops, so the only thing that can decide the answer is the goal test.
    ///
    /// # Safety
    /// As [`always_false`].
    unsafe extern "C" fn no_neighbours(
        ctx: *mut RuntimeContext,
        _closure: GcRef,
        _state: GcRef,
    ) -> GcRef {
        // SAFETY: the oracle passes its own wired ctx.
        unsafe { praxis_vec_new(ctx, &crate::scalars::INT as *const _) }
    }

    /// A `Bool` object laid out exactly the way [`Heap::alloc_raw`] lays one
    /// out — a `GcHeader` followed by its payload at
    /// `GcHeader::payload_offset_for(1)` — but in memory *this module* owns,
    /// and with the seven bytes after the one-byte payload set to `0xFF`.
    ///
    /// The heap cannot be asked for this shape. Under the page allocator
    /// (ADR-103) a `Bool` lands on the ladder's bottom rung, so its block is
    /// rounded up to an 8-byte boundary and the seven bytes after the one-byte
    /// payload are slack no object ever writes — and a fresh page is `mmap`ped
    /// zero, so an eight-byte read of a heap `Bool` answers *correctly* by
    /// accident. Owning the storage turns the padding from an accident into a
    /// fixture, which is the only way the read can be measured rather than
    /// sampled.
    ///
    /// The header carries a **freshly minted** `HeapId`, so the collector's
    /// provenance check (`Heap::mark`) skips this object instead of colouring
    /// it: the oracle roots every closure result, and a root the heap did not
    /// allocate is not the heap's to touch.
    #[repr(C)]
    struct DirtyPaddedBool {
        header: crate::gc::GcHeader,
        /// Byte 0 is the `BoolPayload`; bytes 1..8 are the neighbours a
        /// wrong-width or wrong-offset read would consume.
        payload: [u8; 8],
    }

    /// A `false` whose seven following bytes are `0xFF`, leaked once per thread
    /// so a closure entry point can answer it. See [`DirtyPaddedBool`].
    fn dirty_padded_false() -> GcRef {
        thread_local! {
            static CELL: std::cell::Cell<*mut crate::gc::GcHeader> =
                const { std::cell::Cell::new(std::ptr::null_mut()) };
        }
        CELL.with(|cell| {
            if cell.get().is_null() {
                let object = Box::leak(Box::new(DirtyPaddedBool {
                    header: crate::gc::GcHeader::new(
                        &scalars::BOOL,
                        crate::gc::GcHeader::payload_offset_for(scalars::BOOL.align()) as u16,
                        crate::gc::HeapId::mint(),
                    ),
                    payload: [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
                }));
                cell.set(&mut object.header as *mut crate::gc::GcHeader);
            }
            // SAFETY: the pointer heads a leaked, correctly-laid-out `Bool`
            // object that lives for the rest of the process.
            unsafe { GcRef::from_raw(cell.get()) }
        })
    }

    /// As [`always_false`], but answering the [`dirty_padded_false`] fixture.
    ///
    /// # Safety
    /// As [`always_false`].
    unsafe extern "C" fn always_dirty_false(
        _ctx: *mut RuntimeContext,
        _closure: GcRef,
        _state: GcRef,
    ) -> GcRef {
        dirty_padded_false()
    }

    /// `ClosureOracle::is_goal` must read the closure's `Bool` answer at a
    /// `Bool`'s width: the payload is **one** byte (`BoolPayload = u8`, and
    /// `BOOL` is built from it), so an eight-byte read would take seven further
    /// bytes from past the object.
    ///
    /// Every goal predicate of every goal-directed helper goes
    /// through that read, so this walk is the whole class: the goal answers
    /// `false` at the only reachable state, and the answer must be `None`.
    ///
    /// The `false` it answers is [`dirty_padded_false`], whose payload byte is
    /// `0x00` and whose next seven bytes are `0xFF`. That is what makes this a
    /// gate rather than a walk: an eight-byte read answers
    /// `0xFFFF_FFFF_FFFF_FF00`, which is "goal reached at the start state" and
    /// therefore `Some(0)`; a read at *any* offset past byte zero answers
    /// `0xFF`, likewise `Some(0)`. Only one byte at offset zero answers `None`,
    /// so the test fails if either the width or the offset is wrong. Asking the
    /// heap for this shape does not work — see [`DirtyPaddedBool`].
    ///
    /// [`read_scalar`] is what makes both mistakes unspellable at the call
    /// site, and `int_payload`'s width check — an ordinary branch, so it holds
    /// in every profile — is what stops the next site from making them;
    /// `read_scalar_answers_none_for_a_foreign_descriptor` pins the reader
    /// itself.
    #[test]
    fn a_graph_goal_predicate_reads_a_bool_at_a_bool_s_width() {
        // The fixture is only a gate while its padding is dirty: state that
        // here, so a later edit that zeroes it fails loudly rather than
        // silently turning this back into the walk it replaced.
        let fixture = dirty_padded_false();
        assert!(std::ptr::eq(fixture.descriptor(), &scalars::BOOL));
        assert_eq!(
            unsafe { read_scalar(fixture, scalars::BOOL_PAYLOAD) },
            Some(0u8),
            "the fixture is `false` at a Bool's width"
        );
        assert_ne!(
            unsafe { *fixture.payload::<i64>() },
            0,
            "…and non-zero at an Int's, which is what the wrong read consumed"
        );

        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let answer = unsafe {
            let goal = praxis_alloc_closure(ctx, always_dirty_false as *const u8, 0);
            let neighbours = praxis_alloc_closure(ctx, no_neighbours as *const u8, 0);
            let start = praxis_alloc_int(ctx, 0);
            praxis_bfs_distance(ctx, start, neighbours, goal)
        };
        assert!(!rt.has_pending_fault(), "fault: {:?}", rt.fault());
        assert_eq!(answer.descriptor().id(), crate::enums::ENUM.id());
        assert_eq!(
            enum_tag_of(answer),
            crate::enums::OPTION_NONE_TAG as u32,
            "the goal answered `false` at every state, so no distance was found"
        );
        unsafe { drop_ctx(ctx) };
    }

    /// The same walk against the immortal `false` every real program's closure
    /// answers — a companion, not a gate: a wrong-width read passes it, because
    /// the allocator leaves a `Bool`'s slack bytes zero. It rules out a reader
    /// that only handles the fixture correctly.
    #[test]
    fn a_graph_goal_predicate_that_is_false_everywhere_finds_nothing() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let answer = unsafe {
            let goal = praxis_alloc_closure(ctx, always_false as *const u8, 0);
            let neighbours = praxis_alloc_closure(ctx, no_neighbours as *const u8, 0);
            let start = praxis_alloc_int(ctx, 0);
            praxis_bfs_distance(ctx, start, neighbours, goal)
        };
        assert!(!rt.has_pending_fault(), "fault: {:?}", rt.fault());
        assert_eq!(answer.descriptor().id(), crate::enums::ENUM.id());
        assert_eq!(enum_tag_of(answer), crate::enums::OPTION_NONE_TAG as u32);
        unsafe { drop_ctx(ctx) };
    }

    /// The reader itself, both directions. `read_scalar` is what makes a
    /// wrong-type or wrong-width read unspellable at a call site, so its own
    /// contract is pinned here: the right type reads at the right width, and a
    /// foreign type answers `None` instead of reinterpreting the bytes.
    #[test]
    fn read_scalar_answers_none_for_a_foreign_descriptor() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        unsafe {
            let t = bool_ref(ctx, true);
            let f = bool_ref(ctx, false);
            assert_eq!(read_scalar(t, crate::scalars::BOOL_PAYLOAD), Some(1u8));
            assert_eq!(read_scalar(f, crate::scalars::BOOL_PAYLOAD), Some(0u8));
            // An `Int` is not a `Bool`, and the answer is absence rather than
            // the first byte of the `i64`.
            let n = praxis_alloc_int(ctx, 1);
            assert_eq!(read_scalar(n, crate::scalars::BOOL_PAYLOAD), None);
            assert_eq!(read_scalar(n, crate::scalars::INT_PAYLOAD), Some(1i64));
            drop_ctx(ctx);
        }
    }

    /// The variant tag of an enum value, read the way the runtime's own
    /// `enum_format` reads it.
    fn enum_tag_of(value: GcRef) -> u32 {
        // SAFETY: the caller passes an ENUM-descriptor object.
        unsafe { (*(value.payload::<u8>() as *const crate::enums::EnumPayload)).tag }
    }

    /// The same rule under a *tuple* static type:
    /// `Grid.find` answers `(Int, Int)`, so "nothing matched" cannot be the Unit
    /// sentinel wearing that type.
    #[test]
    fn absent_grid_find_does_not_return_an_untyped_unit_sentinel() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let (missing, found);
        unsafe {
            let cell = praxis_alloc_int(ctx, 1);
            let sought = praxis_alloc_int(ctx, 2);
            let grid = rt.alloc_grid(&crate::scalars::INT, vec![cell], 1);
            missing = praxis_grid_find(ctx, grid, sought);
            let present = praxis_alloc_int(ctx, 1);
            found = praxis_grid_find(ctx, grid, present);
        }

        assert_ne!(
            missing.descriptor().id(),
            crate::scalars::UNIT.id(),
            "Grid.find is statically point-typed; absence needs Option or a checked fault, not Unit"
        );
        assert_eq!(missing.descriptor().id(), crate::enums::ENUM.id());
        assert_eq!(enum_tag_of(missing), crate::enums::OPTION_NONE_TAG as u32);
        // …and a hit is `Some((x, y))`, still a real point inside the option.
        assert_eq!(enum_tag_of(found), crate::enums::OPTION_SOME_TAG as u32);
        let point = unsafe { praxis_enum_payload(ctx, found, 0) };
        assert_eq!(point.descriptor().id(), crate::tuples::TUPLE.id());
        unsafe { drop_ctx(ctx) };
    }

    // --- GC pacing (§12.4, ADR-019) ----------------------------------------
    //
    // `maybe_collect` is the load-bearing mechanism for the shadow-stack
    // spill: the alloc wrappers call it so collection happens automatically
    // inside JIT'd code.

    #[test]
    fn maybe_collect_skips_below_threshold() {
        // A fresh heap with a single small allocation is well under the 64 KiB
        // threshold, so `maybe_collect` must report no collection ran.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let _ = praxis_alloc_int(ctx, 1);
            // Nothing live matters here; we only ask whether collection *ran*.
            let roots = crate::roots::RuntimeRoots::from_context(ctx);
            let ran = rt.heap().maybe_collect(&roots);
            assert!(
                !ran,
                "a single small Int must not trip the 64 KiB threshold"
            );
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn maybe_collect_runs_under_pressure() {
        // Allocating past the 64 KiB threshold collects on its own, with no
        // generated frame on the stack and no hand-written `maybe_collect`
        // call. The helper asserts it happens within 10,000 allocations.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: ctx wired.
        unsafe {
            let _ = allocate_until_automatic_collection(&rt, ctx);
            // After a collection the pacing counter resets, so an immediate
            // call (no new allocations) does not collect again.
            let roots = crate::roots::RuntimeRoots::from_context(ctx);
            assert!(
                !rt.heap().maybe_collect(&roots),
                "counter must reset after a collection"
            );
        }
        unsafe { drop_ctx(ctx) };
    }

    #[test]
    fn checked_int_add_is_an_automatic_gc_safepoint() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let collected;
        unsafe {
            // The *sum* has to be uninterned, not just the operands: what is
            // watched below is whether `praxis_int_add`'s result enters the live
            // registry, and an interned sum never does (see `UNINTERNED`).
            let lhs = praxis_alloc_int(ctx, UNINTERNED);
            let rhs = praxis_alloc_int(ctx, 22);
            let mut frame = push_frame(ctx, SlotCount::new(2).unwrap());
            frame.set(0, lhs);
            frame.set(1, rhs);

            let mut before = rt.heap().stats().live_count;
            let mut observed = false;
            for _ in 0..10_000 {
                let _ = praxis_int_add(ctx, lhs, rhs);
                let after = rt.heap().stats().live_count;
                if after < before.saturating_add(1) {
                    observed = true;
                    break;
                }
                before = after;
            }
            collected = observed;
            drop(frame);
        }
        unsafe { drop_ctx(ctx) };

        assert!(
            collected,
            "every allocating ABI wrapper must participate in automatic GC pacing"
        );
    }

    #[test]
    fn automatic_gc_roots_the_ambient_input_buffer() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let live_after_collection;
        unsafe {
            (*ctx).input_source = rt.alloc_text("input that main has not read yet");
            let frame = push_frame(ctx, SlotCount::new(0).unwrap());
            live_after_collection = allocate_until_automatic_collection(&rt, ctx);
            drop(frame);
        }
        unsafe { drop_ctx(ctx) };

        assert!(
            live_after_collection >= 2,
            "the ambient input Text and the allocation returned after collection must both remain live"
        );
    }

    #[test]
    fn automatic_gc_roots_parse_failure_partial_values() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // Uninterned: what this test watches is a *registered* object surviving
        // a collection that roots only through `ParseDetail.partial`, and an
        // interned `Int` is never registered, so it would survive whether the
        // root set included the slot or not (see `UNINTERNED`).
        let partial = rt.alloc_int(UNINTERNED);
        rt.parse_detail_mut()
            .consider(ParseFail::here(0, "test").with_partial(Some(partial)), b"");
        let live_after_collection;
        unsafe {
            let frame = push_frame(ctx, SlotCount::new(0).unwrap());
            live_after_collection = allocate_until_automatic_collection(&rt, ctx);
            drop(frame);
        }
        unsafe { drop_ctx(ctx) };

        assert!(
            live_after_collection >= 2,
            "ParseDetail.partial is runtime-owned and must be included in every automatic root set"
        );
    }

    #[test]
    fn automatic_gc_roots_runtime_owned_crash_snapshots() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // Uninterned, for `automatic_gc_roots_parse_failure_partial_values`'s
        // reason: the observable is a registered object surviving because the
        // snapshot rooted it.
        let captured = rt.alloc_int(UNINTERNED);
        let local_name = b"value";
        let meta = crate::debug::DebugLocalMeta {
            callee_name: std::ptr::null(),
            callee_name_len: 0,
            source_name: local_name.as_ptr(),
            name_len: local_name.len() as u32,
            symbol_id: 1,
            descriptor: &crate::scalars::INT as *const _,
            type_id: 0,
            kind: crate::debug::LOCAL_KIND_USER,
            span_start: 0,
            span_end: 0,
            slot_kind: crate::debug::DebugSlotKind::Reference,
        };
        let metas = [meta];
        let func_name = b"main";
        let func_meta = crate::debug::FunctionDebugMeta {
            func_name: func_name.as_ptr(),
            func_name_len: func_name.len() as u32,
            local_count: 1,
            locals: metas.as_ptr(),
            span_start: 0,
            span_end: 0,
        };
        let live_after_collection;
        // SAFETY: `ctx` is wired to `rt`; `func_meta`/`metas` outlive the guard,
        // and the snapshot is taken while the frame is still claimed — the
        // ordering a generated fault epilogue has (ADR-033 decision 1).
        unsafe {
            let mut debug_frame = crate::debug::push_frame(ctx, &func_meta);
            debug_frame.set(0, captured);
            crate::crash_snapshot::praxis_snapshot_debug_chain(ctx);
            drop(debug_frame);
            assert!(rt.crash_snapshot().is_some());

            let shadow_frame = push_frame(ctx, SlotCount::new(0).unwrap());
            live_after_collection = allocate_until_automatic_collection(&rt, ctx);
            drop(shadow_frame);
        }
        unsafe { drop_ctx(ctx) };

        assert!(
            live_after_collection >= 2,
            "a runtime-owned CrashSnapshot must root its copied local values during automatic GC"
        );
    }

    #[test]
    fn nested_allocating_helpers_root_intermediate_results() {
        // `Grid.positions` builds its result Vec in a Rust local and fills it by
        // calling `alloc_point`, which allocates three times per point. Every
        // one of those is a safepoint, and the shadow stack only sees what
        // generated code spilled — this is all native code, so the result Vec,
        // the points already in it and the tuple `alloc_point` is midway
        // through filling are rooted by the helper's own `NativeScope`.
        //
        // This calls the real helper rather than inlining a sketch of it, and
        // reading the points back afterwards is what proves nothing was
        // reclaimed.
        // The grid is wide enough that the helper's own point allocations cross
        // the pacing threshold partway through the loop — the collection has to
        // happen *inside* the helper for this to test anything.
        const W: usize = 40;
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        let mut coords: Vec<(i64, i64)> = Vec::new();
        let collections_inside_the_helper;
        unsafe {
            let cells: Vec<GcRef> = (0..(W * W) as i64).map(|i| rt.alloc_int(i)).collect();
            let grid = rt.alloc_grid(&scalars::INT, cells, W);
            let mut frame = push_frame(ctx, SlotCount::new(1).unwrap());
            frame.set(0, grid);

            let before = rt.heap().stats().live_count;
            let positions = praxis_grid_positions(ctx, grid);
            // Every point survived, so the live count only grew; a collection
            // that reclaimed the half-built result would show up as a drop.
            collections_inside_the_helper = rt.heap().stats().live_count > before;

            let items = &(*positions.payload::<VecPayload>()).items;
            assert_eq!(items.len(), W * W, "one position per cell");
            for point in items {
                let tuple = &*point.payload::<crate::tuples::TuplePayload>();
                coords.push((int_payload(tuple.items[0]), int_payload(tuple.items[1])));
            }

            drop(frame);
        }
        unsafe { drop_ctx(ctx) };

        assert!(collections_inside_the_helper);
        let expected: Vec<(i64, i64)> = (0..W * W)
            .map(|i| ((i % W) as i64, (i / W) as i64))
            .collect();
        assert_eq!(
            coords, expected,
            "every point and coordinate the helper allocated must survive the \
             collections the helper itself triggers"
        );
    }

    // --- null-context safety (defensive guards, §10.4 spirit) --------------

    #[test]
    fn check_fault_on_null_context_is_zero() {
        // A null/unwired context must report no fault rather than dereferencing
        // the null pointer (the guard at `praxis_check_fault`).
        // SAFETY: passing a null context is the exact case the guard handles.
        assert_eq!(unsafe { praxis_check_fault(std::ptr::null_mut()) }, 0);
    }

    // The inline prologue deliberately does not null-check the context
    // (ADR-101): the check would cost every call in the language, and
    // `Runtime::context` is the only producer of a context generated code is
    // handed. `RuntimeContext::placeholder` carries the obligation in its doc.

    // --- a null element type stays unknown ---------------------------------

    /// A collection built with no static element type must not claim to hold
    /// `Int`s, and what it holds must render as what it is.
    ///
    /// The codegen passes a **null** descriptor for `var c = Counter()` — its
    /// contract above `collection_element_descriptor_for` says so, and says
    /// every `praxis_*_new` wrapper reads it that way. Replacing the null with
    /// `&INT` is not a default but a false claim, and the label is dispatched
    /// through: a `Text` key would hash and print as an `i64`, a `Float`
    /// element as the integer its bits spell.
    ///
    /// Both halves are asserted because either alone passes a wrong fix: the
    /// absent label is the representation, the rendering is the answer.
    #[test]
    fn a_collection_with_no_static_element_type_does_not_claim_int() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: `ctx` is wired; every argument below is a wrapper's own.
        unsafe {
            // Exactly what the codegen passes when the element type is still an
            // inference variable.
            let null = std::ptr::null::<TypeDescriptor>();
            let counter = praxis_counter_new(ctx, null);
            let set = praxis_set_new(ctx, null);
            let map = praxis_map_new(ctx, null);
            let min_heap = praxis_min_heap_new(ctx, null);
            let max_heap = praxis_max_heap_new(ctx, null);

            assert!(
                counter_payload(counter).key().is_none(),
                "a Counter with no static key type must not claim one"
            );
            assert!(set_payload(set).element().is_none());
            assert!(map_payload(map).key().is_none());
            assert!(min_heap_payload(min_heap).element().is_none());
            assert!(max_heap_payload(max_heap).element().is_none());

            // …and the values come back out as themselves. A `Text` key through
            // a `Counter`, a `Float` through a `MinHeap`: neither is an `Int`,
            // and a guessed `Int` label would print both as one.
            let key = praxis_alloc_text(ctx, "ab".as_ptr(), 2);
            praxis_counter_inc(ctx, counter, key);
            let keys = praxis_counter_keys(ctx, counter);
            let mut rendered = String::new();
            keys.format(&mut rendered);
            assert_eq!(rendered, "[ab]", "a Counter's keys are its keys");

            let half = praxis_alloc_float(ctx, 1.5f64.to_bits() as i64);
            praxis_min_heap_push(ctx, min_heap, half);
            let mut rendered = String::new();
            min_heap.format(&mut rendered);
            assert_eq!(rendered, "[1.5]", "a MinHeap prints the elements it holds");

            let member = praxis_alloc_text(ctx, "zz".as_ptr(), 2);
            praxis_set_insert(ctx, set, member);
            let items = praxis_set_items(ctx, set);
            let mut rendered = String::new();
            items.format(&mut rendered);
            assert_eq!(rendered, "[zz]");
        }
        // SAFETY: the context was leaked by `wired_ctx` and is unused after this.
        unsafe { drop_ctx(ctx) };
    }

    /// A `Map` does not claim its values are `Int`s.
    ///
    /// `praxis_map_new` takes one descriptor — the key's — because the `MapNew`
    /// row carries one type argument, so the value slot starts **null**.
    /// Writing `INT` there would be a claim rather than a default, and it would
    /// be the same word as "unknown", so the adoption that follows could not
    /// tell a `Map` that really holds `Int`s from one that had never been told
    /// anything — and a `Map[Text, Text]`'s value would be read as an `i64`.
    #[test]
    fn a_map_does_not_claim_its_values_are_ints() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: `ctx` is wired; every argument below is a wrapper's own.
        unsafe {
            let empty = praxis_map_new(ctx, &crate::text::TEXT);
            assert!(
                map_payload(empty).value().is_none(),
                "an empty Map has been told nothing about its values"
            );
            // …so the `Vec` its `values()` answers is not labelled `Int`
            // either. A guessed `Int` there would make an empty
            // `Map[Text, Text]`'s values unequal to an empty `Vec[Text]`,
            // because `vec_equals` compares element labels.
            let none_yet = praxis_map_values(ctx, empty);
            assert!(vec_payload(none_yet).element().is_none());

            // The first insert is what the map learns from.
            let k = praxis_alloc_text(ctx, "k".as_ptr(), 1);
            let v = praxis_alloc_text(ctx, "vv".as_ptr(), 2);
            praxis_map_insert(ctx, empty, k, v);
            assert!(
                std::ptr::eq(map_payload(empty).value().unwrap(), &crate::text::TEXT),
                "a Map learns its value type from the first value inserted"
            );
            let mut rendered = String::new();
            praxis_map_values(ctx, empty).format(&mut rendered);
            assert_eq!(rendered, "[vv]");

            // A `Map` that really does hold `Int`s says so — an assertion only
            // a null "unknown" makes possible, since a hardcoded `INT` would be
            // the same word as "never been told".
            let ints = praxis_map_new(ctx, &crate::text::TEXT);
            let ik = praxis_alloc_text(ctx, "n".as_ptr(), 1);
            praxis_map_insert(ctx, ints, ik, praxis_alloc_int(ctx, 7));
            assert!(std::ptr::eq(
                map_payload(ints).value().unwrap(),
                &scalars::INT
            ));
        }
        // SAFETY: the context was leaked by `wired_ctx` and is unused after this.
        unsafe { drop_ctx(ctx) };
    }

    /// An unlearned label is not a label, so it cannot make two empty
    /// collections unequal.
    ///
    /// `same_element` is not pointer identity, because a never-inserted `Map`'s
    /// `values()` carries no label and an equally-typed empty `Vec[Int]`
    /// carries `Int`. ADR-066 decision 5 is the rule: a null slot means the
    /// *value's own* descriptor answers, and a collection with no label has no
    /// values, so nothing is left to disagree. Reinstating a guessed descriptor
    /// is the fix this rejects.
    ///
    /// The last case is the limit: two collections that have each been told
    /// their element type must still disagree when the types differ.
    #[test]
    fn an_unlearned_element_label_does_not_make_two_empty_collections_unequal() {
        use crate::collections::same_element;
        let int: *const crate::descriptor::TypeDescriptor = &scalars::INT;
        let text: *const crate::descriptor::TypeDescriptor = &crate::text::TEXT;
        let unlearned: *const crate::descriptor::TypeDescriptor = std::ptr::null();

        assert!(same_element(unlearned, int), "no label agrees with `Int`");
        assert!(same_element(int, unlearned), "and in the other order");
        assert!(same_element(unlearned, unlearned));
        assert!(same_element(int, int));
        // The rule this must not weaken: two *learned* labels that differ are
        // two different collections, empty or not.
        assert!(!same_element(int, text));

        // End to end: a `Map` never inserted into has no value label, and its
        // `values()` is an empty `Vec` that is equal to an empty `Vec` however
        // that one was labelled.
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        // SAFETY: `ctx` is wired; every argument below is a wrapper's own.
        unsafe {
            let never_inserted = praxis_map_new(ctx, &crate::text::TEXT);
            let unlabelled = praxis_map_values(ctx, never_inserted);
            assert!(vec_payload(unlabelled).element().is_none());

            let labelled_ints = praxis_vec_new(ctx, &scalars::INT as *const _);
            assert!(
                praxis_struct_eq(ctx, unlabelled, labelled_ints) != 0,
                "an empty Map's values are an empty Vec[Int]"
            );
            assert!(
                praxis_struct_eq(ctx, labelled_ints, unlabelled) != 0,
                "and equality is symmetric"
            );

            // A non-empty collection is still not an empty one: the length
            // check behind `same_element` is what answers, and it must.
            praxis_vec_push(ctx, labelled_ints, praxis_alloc_int(ctx, 1));
            assert!(praxis_struct_eq(ctx, unlabelled, labelled_ints) == 0);
        }
        // SAFETY: the context was leaked by `wired_ctx` and is unused after this.
        unsafe { drop_ctx(ctx) };
    }

    // --- the manifest's fault column is checked against the code -----------

    /// Every function defined in this file, as `(name, body)`.
    ///
    /// Line-based on purpose: a definition is a line whose first tokens are one
    /// of Rust's `fn` spellings, and its body runs to the line where the brace
    /// depth opened by that definition returns to zero. Anything cleverer would
    /// be a Rust parser, and anything looser — matching `fn` anywhere — reads
    /// the word out of doc comments and glues unrelated bodies together.
    fn functions_in_this_file() -> Vec<(String, String)> {
        functions_in(include_str!("abi.rs"))
    }

    /// The code of one line, with any `//` comment removed.
    ///
    /// The sweep reads what the **compiler** sees, not what a reader wrote
    /// beside it. The fixed point matches `set_fault(` as a plain substring, so
    /// a comment inside a wrapper naming the helper would otherwise classify
    /// that wrapper as faulting. A sweep a comment can fool is a sweep that gets
    /// edited around rather than satisfied, which is the failure mode the whole
    /// invariant exists to prevent.
    ///
    /// A `//` inside a string or `char` literal is **not** a comment — `"//"`
    /// and `'/'` both occur in this file — so the scan tracks which it is in.
    /// It is not a Rust lexer: a raw string's hashes and a block comment are
    /// not modelled, because neither appears in a function body here and a
    /// half-lexer that claimed to be one would be worse than a stated
    /// limitation. It errs toward keeping code, never toward dropping it.
    fn code_only(line: &str) -> &str {
        let bytes = line.as_bytes();
        let (mut in_str, mut in_char, mut escaped) = (false, false, false);
        let mut i = 0;
        while i < bytes.len() {
            let c = bytes[i];
            if escaped {
                escaped = false;
            } else if c == b'\\' && (in_str || in_char) {
                escaped = true;
            } else if in_str {
                in_str = c != b'"';
            } else if in_char {
                in_char = c != b'\'';
            } else if c == b'"' {
                in_str = true;
            } else if c == b'\'' {
                // A lifetime (`'a`) is not a `char` literal; a `char` literal's
                // closing quote is at most three bytes away (`'\\n'`, `'\\''`).
                in_char = bytes[i + 1..].iter().take(4).any(|b| *b == b'\'');
            } else if c == b'/' && bytes.get(i + 1) == Some(&b'/') {
                return &line[..i];
            }
            i += 1;
        }
        line
    }

    /// Every function defined in `src`, as `(name, code)` — the code only, with
    /// comments stripped by [`code_only`]. Braces are counted on the code too,
    /// so a comment holding an unbalanced brace cannot end a body early or run
    /// two bodies together.
    fn functions_in(src: &str) -> Vec<(String, String)> {
        const PREFIXES: [&str; 6] = [
            "fn ",
            "pub fn ",
            "pub(crate) fn ",
            "unsafe fn ",
            "pub unsafe fn ",
            "pub unsafe extern \"C\" fn ",
        ];
        let mut out: Vec<(String, String)> = Vec::new();
        // (name, body so far, brace depth, whether the body has opened at all —
        // a multi-line signature spends several lines at depth zero before its
        // `{`, and closing there would give every such wrapper a one-line body).
        let mut open: Option<(String, String, i32, bool)> = None;
        for raw in src.lines() {
            let line = code_only(raw);
            let depth_change = |l: &str| -> i32 {
                l.chars().filter(|c| *c == '{').count() as i32
                    - l.chars().filter(|c| *c == '}').count() as i32
            };
            if let Some((name, body, depth, opened)) = open.as_mut() {
                body.push_str(line);
                body.push('\n');
                *depth += depth_change(line);
                *opened |= line.contains('{');
                if *opened && *depth <= 0 {
                    out.push((std::mem::take(name), std::mem::take(body)));
                    open = None;
                }
                continue;
            }
            let trimmed = line.trim_start();
            let Some(rest) = PREFIXES
                .iter()
                .find_map(|p| trimmed.strip_prefix(p).filter(|_| trimmed.starts_with(p)))
            else {
                continue;
            };
            let name: String = rest
                .chars()
                .take_while(|c| c.is_alphanumeric() || *c == '_')
                .collect();
            if name.is_empty() {
                continue;
            }
            let depth = depth_change(line);
            let opened = line.contains('{');
            // A one-line definition has already opened and closed its body.
            if opened && depth <= 0 {
                out.push((name, line.to_string()));
            } else {
                open = Some((name, format!("{line}\n"), depth, opened));
            }
        }
        out
    }

    /// A wrapper that can raise a fault says so in the manifest.
    ///
    /// A row declared `Allocates` makes `RuntimeSymbol::faults()` answer
    /// `false`, so no `CheckFault` follows the call. If such a wrapper can reach
    /// `set_fault` the consequence is not cosmetic: the operation is silently
    /// abandoned, the wrapper answers the Unit sentinel, and the fault is
    /// observed by some later unrelated check — at the wrong source location,
    /// after the program has computed and possibly printed an answer.
    ///
    /// A hand-corrected row drifts again, so this is the invariant instead: the
    /// file is read at compile time, each `praxis_*` wrapper's body is walked,
    /// and any body that can reach `set_fault` — directly or through a helper
    /// defined here, transitively — must belong to a symbol whose row says
    /// `faults()`.
    ///
    /// One direction only. A row may declare a fault the reader cannot see: the
    /// arithmetic wrappers are generated by `checked_int_binop!` and have no
    /// textual definition at all, and a future wrapper may fault through a
    /// helper in another module. Those are false negatives — this test is weaker
    /// than the truth, never stricter — and the direction it does check is the
    /// one that produces wrong answers.
    /// The fixed point of "can reach `set_fault`" over `defs`: a function
    /// faults if it calls `set_fault`, or calls something that does.
    fn faulting_functions(defs: &[(String, String)]) -> std::collections::HashSet<String> {
        let mut faulting: std::collections::HashSet<String> =
            ["set_fault".to_string()].into_iter().collect();
        loop {
            let mut grew = false;
            for (name, body) in defs {
                if faulting.contains(name) {
                    continue;
                }
                if faulting.iter().any(|f| body.contains(&format!("{f}("))) {
                    faulting.insert(name.clone());
                    grew = true;
                }
            }
            if !grew {
                break;
            }
        }
        faulting
    }

    #[test]
    fn a_wrapper_that_can_raise_a_fault_declares_that_it_faults() {
        let defs = functions_in_this_file();
        let faulting = faulting_functions(&defs);

        let mut checked = 0usize;
        for (name, _) in &defs {
            let Some(sym) = praxis_stdlib::abi::RuntimeSymbol::from_name(name) else {
                continue;
            };
            if !faulting.contains(name) {
                continue;
            }
            assert!(
                sym.faults(),
                "{name} can reach `set_fault`, but its manifest row says it \
                 cannot fault — so no `CheckFault` follows the call and the \
                 fault is observed somewhere else entirely"
            );
            checked += 1;
        }
        // If the scan stopped finding wrappers, the assertion above would be
        // vacuous and this test would pass while saying nothing.
        assert!(
            checked >= 20,
            "expected the fault-raising wrappers to be found; saw {checked}"
        );
        // These three reach `set_fault` only through a helper, so they are the
        // shape the transitive scan has to be able to see.
        for name in [
            "praxis_vec_push",
            "praxis_deque_push_front",
            "praxis_deque_push_back",
        ] {
            assert!(
                faulting.contains(name),
                "{name} reaches `set_fault` through `adopt_or_reject`; a scan \
                 that cannot see that cannot hold the invariant"
            );
        }
        // **`InvalidText` lives at exactly one site (ADR-111).**
        // `praxis_alloc_text` trusts its caller; the one caller holding raw host
        // bytes raises the fault instead. Asserting the destination is visible
        // to the scan is what distinguishes a relocated fault from a deleted
        // one — without it, deleting the validation outright would leave this
        // test just as green.
        assert!(
            faulting.contains("praxis_get_input"),
            "`praxis_get_input` validates the host's input and raises \
             `InvalidText` itself (ADR-111); a scan that cannot see that cannot \
             tell a relocated fault from a deleted one"
        );
        assert!(
            !faulting.contains("praxis_alloc_text"),
            "`praxis_alloc_text` reaches `set_fault` again. Its row is \
             `Effect::Allocates`, so nothing observes the fault — a violated \
             UTF-8 precondition aborts through `abi_guard!` instead (ADR-111)"
        );
    }
    /// **The sweep above reads code, not prose.** Its classification is a
    /// substring match, so without [`code_only`] a *comment* inside a wrapper
    /// naming the helper would classify that wrapper as faulting and fail the
    /// invariant for a wrapper that cannot fault. A sweep a comment can fool is
    /// a sweep that gets edited around rather than satisfied.
    ///
    /// Synthetic source, because the real file must not contain the shape: the
    /// point is that it may, safely.
    #[test]
    fn the_manifest_sweep_reads_code_and_not_comments() {
        let src = r#"
pub unsafe extern "C" fn praxis_pretend_pure(ctx: *mut RuntimeContext) -> GcRef {
    // It used to call set_fault(ctx, RaisedFault::TYPE_MISMATCH) here, and a
    // later edit removed the only path that could. Prose, not code. }
    let sep = "//";
    let slash = '/';
    let _ = (sep, slash);
    unit_sentinel(ctx)
}

pub unsafe extern "C" fn praxis_pretend_faulting(ctx: *mut RuntimeContext) -> GcRef {
    set_fault(ctx, RaisedFault::TYPE_MISMATCH);
    unit_sentinel(ctx)
}
"#;
        let defs = functions_in(src);
        let names: Vec<&str> = defs.iter().map(|(n, _)| n.as_str()).collect();
        assert_eq!(names, ["praxis_pretend_pure", "praxis_pretend_faulting"]);
        // The unbalanced `}` in that comment must not close the body early:
        // braces are counted on the code too, so the whole function is read.
        assert!(
            defs[0].1.contains("unit_sentinel(ctx)"),
            "a brace inside a comment ended the body early: {:?}",
            defs[0].1
        );

        let faulting = faulting_functions(&defs);
        assert!(
            !faulting.contains("praxis_pretend_pure"),
            "a comment naming `set_fault` is not a call to it"
        );
        assert!(
            faulting.contains("praxis_pretend_faulting"),
            "and a real call still is — stripping comments must not blind the sweep"
        );
    }

    /// [`code_only`]'s own contract, both directions.
    #[test]
    fn code_only_keeps_a_slash_inside_a_literal() {
        assert_eq!(code_only("let x = 1; // two"), "let x = 1; ");
        assert_eq!(code_only(r#"let s = "a//b";"#), r#"let s = "a//b";"#);
        assert_eq!(code_only(r"let c = '/'; // gone"), r"let c = '/'; ");
        assert_eq!(
            code_only(r#"let e = "\"//"; // gone"#),
            r#"let e = "\"//"; "#
        );
        assert_eq!(code_only("    /// a doc comment"), "    ");
        assert_eq!(code_only("no comment here"), "no comment here");
        // A lifetime is not a `char` literal, so the comment after it is still
        // a comment.
        assert_eq!(
            code_only("fn f<'a>(x: &'a str) {} // gone"),
            "fn f<'a>(x: &'a str) {} "
        );
    }

    // --- the panic backstop ------------------------------------------------

    /// Every `#[unsafe(no_mangle)] extern "C"` function in this crate has its body
    /// inside `abi_guard!`.
    ///
    /// Per-wrapper totality is the contract: a wrapper validates its arguments
    /// and reports a bad one as a fault, so the guard never fires. This is the
    /// proof that the contract cannot be violated *silently* — a panic
    /// unwinding out of `extern "C"` into Cranelift frames is undefined
    /// behaviour, and the failure mode of forgetting is a corrupted process at
    /// some unrelated later point rather than a message.
    ///
    /// Read as source text on purpose. The property is "every entry point is
    /// wrapped", which is a property of the *set* of entry points; a test that
    /// called them one by one would be a test of the ones somebody remembered.
    ///
    /// **The file set is discovered, not declared.** A hand-written list of
    /// files would make the guarantee "every entry point in a file somebody
    /// remembered to list", and the `wrappers > 100` floor would still pass on
    /// the files that were listed. So the walk covers **every crate's `src/`**,
    /// not only this one: nothing says a future `#[unsafe(no_mangle)]` has to live
    /// here.
    #[test]
    fn every_no_mangle_wrapper_is_behind_the_panic_guard() {
        /// Every `.rs` file under `dir`, recursively, in a stable order.
        fn rust_sources(dir: &std::path::Path, out: &mut Vec<(String, String)>) {
            let entries =
                std::fs::read_dir(dir).unwrap_or_else(|e| panic!("read {}: {e}", dir.display()));
            let mut entries: Vec<_> = entries.map(|e| e.expect("dir entry").path()).collect();
            entries.sort();
            for path in entries {
                let name = path.file_name().unwrap_or_default().to_string_lossy();
                if name == "target" || name.starts_with('.') {
                    continue;
                }
                if path.is_dir() {
                    rust_sources(&path, out);
                } else if path.extension().is_some_and(|e| e == "rs") {
                    let text = std::fs::read_to_string(&path)
                        .unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
                    out.push((path.display().to_string(), text));
                }
            }
        }

        // `crates/`, from this crate's manifest directory.
        let mut crates_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        crates_dir.pop();
        let mut sources: Vec<(String, String)> = Vec::new();
        rust_sources(&crates_dir, &mut sources);
        // A guard against the walk silently covering nothing — a wrong root
        // would otherwise pass by finding no wrapper attribute at all.
        assert!(
            sources.len() > 50,
            "the walk of {} found only {} Rust files, so it is not reading the workspace",
            crates_dir.display(),
            sources.len()
        );

        let mut wrappers = 0usize;
        let mut unguarded: Vec<String> = Vec::new();
        for (file, source) in &sources {
            let lines: Vec<&str> = source.lines().collect();
            for (n, line) in lines.iter().enumerate() {
                // Both spellings: `#[unsafe(no_mangle)]` is the edition-2024
                // form and the only one this crate uses, but matching the bare
                // attribute too keeps the scan from going blind if a wrapper is
                // ever pasted in from older code.
                if !matches!(line.trim(), "#[unsafe(no_mangle)]" | "#[no_mangle]") {
                    continue;
                }
                wrappers += 1;
                // Walk to the line that opens the body, then require the very
                // next non-blank line to be the guard.
                let mut k = n + 1;
                while k < lines.len() && !lines[k].trim_end().ends_with('{') {
                    k += 1;
                }
                // Inclusive of `k`: a one-line signature puts `fn name(` on the
                // very line that opens the body, so an exclusive range would
                // report `<unnamed>` in the message that tells someone which
                // wrapper they forgot.
                let name = lines[n..=k.min(lines.len() - 1)]
                    .iter()
                    .find_map(|l| l.split("fn ").nth(1))
                    .and_then(|l| l.split('(').next())
                    .unwrap_or("<unnamed>")
                    .trim()
                    .to_string();
                let opens_guard = lines
                    .get(k + 1)
                    .map(|l| l.trim_start().starts_with("abi_guard!("))
                    .unwrap_or(false);
                if !opens_guard {
                    unguarded.push(format!("{file}:{} {name}", n + 1));
                }
            }
        }

        assert!(
            wrappers > 100,
            "the scan found only {wrappers} wrappers, so it is not reading the ABI surface"
        );
        assert!(
            unguarded.is_empty(),
            "these `extern \"C\"` entry points can let a panic unwind into generated frames: {unguarded:#?}"
        );
    }

    /// The guard's own behaviour: a panic inside a wrapper becomes a fault with
    /// a message naming the wrapper, and the wrapper returns its defined dummy.
    ///
    /// `praxis_dbg` is the one wrapper that can be made to panic on demand
    /// without an invalid argument — it formats its value, and a `Text` whose
    /// payload is a live `Unit` is a descriptor/payload pairing no validation
    /// catches. Every *reachable* panic is a bug to fix in the wrapper; this
    /// test is about what happens when one is missed.
    #[test]
    fn a_panic_inside_a_wrapper_becomes_a_fault_and_a_defined_dummy() {
        let value = {
            abi_guard!(
                "praxis_test_panics",
                std::ptr::null_mut::<RuntimeContext>(),
                {
                    #[allow(unreachable_code)]
                    {
                        if std::hint::black_box(false) {
                            panic!("this is the guard under test");
                        }
                        7i64
                    }
                }
            )
        };
        assert_eq!(value, 7, "the guard is transparent when nothing panics");

        // A **faulting** wrapper: its call sites can carry a `CheckFault`, so
        // generated code observes the fault before it looks at the value, and
        // the defined dummy is the right answer. The name has to be a real
        // manifest symbol — see `panic_fault_is_observable`, which is what
        // decides whether the dummy is returned at all.
        let mut runtime = crate::Runtime::new();
        let mut ctx = runtime.context();
        let previous = std::panic::take_hook();
        std::panic::set_hook(Box::new(|_| {}));
        let dummy: GcRef = abi_guard!("praxis_run_parser", &mut ctx as *mut RuntimeContext, {
            panic!("a wrapper that forgot to be total");
        });
        std::panic::set_hook(previous);

        assert_eq!(
            runtime.fault(),
            crate::FaultKind::Panic,
            "an escaped panic is a fault, not an unwind into generated code"
        );
        assert!(
            runtime
                .fault_message()
                .is_some_and(|m| m.contains("praxis_run_parser")),
            "the fault names the wrapper it escaped, which a bare kind could not"
        );
        assert_eq!(
            dummy.descriptor().id(),
            crate::scalars::UNIT.id(),
            "the dummy is the Unit sentinel §10.4 already specifies"
        );
    }

    /// **The dummy is only returned where the fault will be seen.**
    ///
    /// Generated code tests the fault slot only where MIR emitted a
    /// `CheckFault`, and **`praxis_mir::verify` is what makes that true of a
    /// non-faulting wrapper** — its `RedundantFaultCheck` rule rejects a check
    /// after an instruction that cannot fault (ADR-088). So for the wrappers the
    /// manifest declares non-faulting there is no check, and returning
    /// `unit_sentinel` there would hand a `Unit` into a slot generated code
    /// believes holds a Record, a Tuple or a closure — a descriptor/payload
    /// confusion introduced by the backstop meant to prevent worse. Those abort
    /// instead.
    ///
    /// This test states the classification. The abort itself cannot be asserted
    /// in-process, which is exactly why the rule has to be a total function of
    /// the manifest rather than a case-by-case judgement.
    #[test]
    fn a_panic_dummy_is_only_returned_where_a_fault_check_can_follow() {
        use praxis_stdlib::abi::RuntimeSymbol;

        let mut pure = 0usize;
        let mut faulting = 0usize;
        for symbol in RuntimeSymbol::ALL.iter().copied() {
            let observable = panic_fault_is_observable(symbol.name());
            assert_eq!(
                observable,
                symbol.faults(),
                "`{}` is declared {:?}; the panic dummy must be returned iff a \
                 fault check can follow it",
                symbol.name(),
                symbol.sig().effect
            );
            if symbol.faults() {
                faulting += 1;
            } else {
                pure += 1;
            }
        }
        assert!(
            pure > 0 && faulting > 0,
            "the manifest must contain both classes for this rule to mean anything \
             ({pure} non-faulting, {faulting} faulting)"
        );

        // Every `#[unsafe(no_mangle)]` wrapper in this crate is manifested, so the
        // unobservable case left is a name that is not a wrapper at all.
        assert!(
            !panic_fault_is_observable("praxis_not_a_wrapper_at_all"),
            "an unknown name is never treated as observable"
        );
    }

    // ---- Process input (§7.10) ----

    /// A reader that answers nothing. A `fn` and not a closure because
    /// [`crate::input::InputReader`] is a plain `fn` pointer.
    fn no_bytes() -> Vec<u8> {
        Vec::new()
    }

    /// Read the bytes behind a `Text` `GcRef`.
    ///
    /// # Safety
    /// `r` must be a live `Text`.
    unsafe fn text_bytes_of(r: GcRef) -> &'static [u8] {
        // SAFETY: the caller guarantees `r` is a live Text, so its payload is a
        // validly-linked `TextPayload`.
        unsafe { crate::text::text_bytes(r.payload::<crate::text::TextPayload>() as *const _) }
    }

    /// A reader that answers zero bytes has given *empty input*, not no input,
    /// so its answer is installed as `input_source` whatever its length.
    ///
    /// Allocating the buffer only `if !bytes.is_empty()` would leave empty
    /// standard input at the immortal Unit, so `praxis_run_parser`'s §6.3
    /// descriptor guard would fault *before* the parser ran — a `ParseFailed`
    /// with no input span, no `expected` and no `actual`, which is none of the
    /// six fields §7.11 says a mismatch carries. A fault raised before any
    /// buffer exists cannot carry them; the buffer is what makes the diagnostic
    /// possible at all (ADR-087).
    #[test]
    fn a_reader_that_answers_zero_bytes_installs_an_empty_text() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        crate::input::install_input_reader(no_bytes);
        // SAFETY: ctx is wired to rt and live for this call.
        let source = unsafe { praxis_get_input(ctx) };
        assert_eq!(
            source.descriptor().id(),
            crate::text::TEXT.id(),
            "a zero-byte answer is still an input buffer"
        );
        // SAFETY: the assertion above proves `source` is a Text.
        assert!(
            unsafe { text_bytes_of(source) }.is_empty(),
            "and the buffer holds exactly what the reader answered"
        );
        // SAFETY: ctx is wired to rt and live for this call.
        assert_eq!(
            unsafe { (*ctx).input_source }.as_ptr(),
            source.as_ptr(),
            "the buffer is installed, not merely returned — §7.10's later \
             `read`s reuse it"
        );
        // SAFETY: ctx came from `wired_ctx` and is not used again.
        unsafe { drop_ctx(ctx) };
    }

    /// **A mutation companion, not a gate.**
    ///
    /// The cheapest wrong repair is to allocate a `Text` unconditionally in
    /// `praxis_get_input`, which passes the gate above and quietly deletes the
    /// one state the §6.3 descriptor guard exists for. A host that installs
    /// **neither** a buffer nor a reader — every JIT test, every embedder — must
    /// still reach `praxis_run_parser` with the Unit source, because
    /// `adv_read_against_non_text_input_faults_cleanly` in the codegen crate's
    /// `jit.rs` is the probe that a `read` there faults instead of
    /// reinterpreting Unit's payload as a `TextPayload` and segfaulting.
    ///
    /// That is the boundary ADR-087 draws: a reader answering zero bytes is a
    /// program state (empty input); no reader at all is a host state (no input),
    /// and no `praxis run` reaches it.
    #[test]
    fn a_host_that_installs_no_reader_keeps_the_unit_source() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        crate::input::clear_input_reader();
        // SAFETY: ctx is wired to rt and live for these calls.
        let before = unsafe { (*ctx).input_source };
        // SAFETY: as above.
        let source = unsafe { praxis_get_input(ctx) };
        assert_eq!(
            source.as_ptr(),
            before.as_ptr(),
            "with no reader installed there is nothing to call and nothing to \
             install; `input_source` is answered untouched"
        );
        assert_ne!(
            source.descriptor().id(),
            crate::text::TEXT.id(),
            "and it is still the Unit the §6.3 guard is the net under"
        );
        // SAFETY: ctx came from `wired_ctx` and is not used again.
        unsafe { drop_ctx(ctx) };
    }

    /// **The guard must not report a parse that never ran.**
    ///
    /// `praxis_run_parser` returns early for a non-Text `input` (§6.3) —
    /// `run_plan` would otherwise reinterpret the payload as a `TextPayload` —
    /// and that early return must still perform the `clear_parse_detail` every
    /// other entry into the parser performs. Without it, a host reaching the
    /// guard after an earlier mismatch reports *that* mismatch's offset and
    /// expectation for a parse that never started.
    ///
    /// Not reachable end to end: a fault is terminal within one `praxis run`,
    /// so the shape is an embedder calling `main` twice (or the crash debugger's
    /// `restart`). This test pins it at the level where the hazard exists.
    ///
    /// Fabricating a `ParseFail` here instead would be worse than clearing: with
    /// no buffer there is no input span, and an invented `expected` would make
    /// an embedder's host bug read as a parse failure at an offset that does not
    /// exist.
    #[test]
    fn the_non_text_guard_does_not_report_a_previous_parses_failure() {
        let mut rt = Runtime::new();
        let ctx = wired_ctx(&mut rt);
        rt.parse_detail_mut()
            .consider(ParseFail::here(7, "int"), b"0123456789");
        assert!(rt.parse_detail().is_set(), "the seed is in place");
        // SAFETY: ctx is wired to rt; the plan index is never read, because the
        // descriptor guard returns before it.
        unsafe {
            let plan = praxis_alloc_int(ctx, 1);
            let unit = (*ctx).unit_ref;
            let result = praxis_run_parser(ctx, plan, unit);
            assert_eq!(
                result.descriptor().id(),
                crate::scalars::UNIT.id(),
                "the guard answers the sentinel"
            );
        }
        assert!(rt.has_pending_fault());
        assert_eq!(rt.fault(), FaultKind::ParseFailed);
        assert!(
            !rt.parse_detail().is_set(),
            "the §6.3 guard runs no parse, so it has no detail to report — and \
             it must not report the previous parse's"
        );
        // SAFETY: ctx came from `wired_ctx` and is not used again.
        unsafe { drop_ctx(ctx) };
    }
}

#[cfg(test)]
mod growth_charging_tests {
    //! **Every wrapper that can grow a collection's buffer charges the pacer**
    //! (ADR-121). See [`super::charge_growth`] for why.
    //!
    //! The values pushed are all inside `small_int`'s interned range, and that
    //! is the whole design of these tests rather than a convenience: an interned
    //! `Int` is an immortal the allocator never charges for, so the *only* thing
    //! that can move `bytes_since_collect` here is the spine. Push
    //! `UNINTERNED + i` instead and every one of these passes whether or not the
    //! growth is charged, because the elements would be paying for it.

    use super::tests::{drop_ctx, wired_ctx};
    use super::*;
    use crate::Runtime;

    /// Reset the counter, run `body`, and answer what it charged.
    fn charged_by(rt: &Runtime, body: impl FnOnce()) -> usize {
        // A collection zeroes the counter, so take a reading either side and
        // require the run not to have collected; the pushes below are far too
        // few to reach any threshold.
        let before = rt.heap().bytes_since_collect();
        body();
        rt.heap().bytes_since_collect().saturating_sub(before)
    }

    /// Enough pushes that amortized doubling must have reallocated at least
    /// once, whatever the initial capacity is.
    const PUSHES: i64 = 256;

    macro_rules! charges_its_spine {
        ($name:ident, $make:expr_2021, $push:expr_2021) => {
            #[test]
            fn $name() {
                let mut rt = Runtime::new();
                let ctx = wired_ctx(&mut rt);
                // SAFETY: `ctx` is wired to `rt` for the whole test.
                unsafe {
                    let subject = $make(ctx);
                    let charged = charged_by(&rt, || {
                        for i in 0..PUSHES {
                            $push(ctx, subject, i);
                        }
                    });
                    assert!(
                        charged > 0,
                        "growing this collection charged the pacer nothing, so a \
                         program whose memory is this buffer would never collect \
                         (ADR-121); every value pushed is an interned immortal, so \
                         the spine is the only thing that could have charged"
                    );
                    drop_ctx(ctx);
                }
            }
        };
    }

    charges_its_spine!(
        vec_push_charges_its_spine,
        |c| praxis_vec_new(c, &crate::scalars::INT),
        |c, s, i| { praxis_vec_push(c, s, praxis_alloc_int(c, i)) }
    );
    charges_its_spine!(
        deque_push_back_charges_its_spine,
        |c| praxis_deque_new(c, &crate::scalars::INT),
        |c, s, i| praxis_deque_push_back(c, s, praxis_alloc_int(c, i))
    );
    charges_its_spine!(
        deque_push_front_charges_its_spine,
        |c| praxis_deque_new(c, &crate::scalars::INT),
        |c, s, i| praxis_deque_push_front(c, s, praxis_alloc_int(c, i))
    );
    charges_its_spine!(
        map_insert_charges_its_spine,
        |c| praxis_map_new(c, &crate::scalars::INT),
        |c, s, i| praxis_map_insert(c, s, praxis_alloc_int(c, i), praxis_alloc_int(c, i))
    );
    charges_its_spine!(
        set_insert_charges_its_spine,
        |c| praxis_set_new(c, &crate::scalars::INT),
        |c, s, i| praxis_set_insert(c, s, praxis_alloc_int(c, i))
    );
    charges_its_spine!(
        counter_set_charges_its_spine,
        |c| praxis_counter_new(c, &crate::scalars::INT),
        |c, s, i| praxis_counter_set(c, s, praxis_alloc_int(c, i), praxis_alloc_int(c, i))
    );
    charges_its_spine!(
        bitset_insert_charges_its_spine,
        |c| praxis_bitset_new(c),
        |c, s, i| praxis_bitset_insert(c, s, praxis_alloc_int(c, i))
    );
    charges_its_spine!(
        max_heap_push_charges_its_spine,
        |c| praxis_max_heap_new(c, &crate::scalars::INT),
        |c, s, i| praxis_max_heap_push(c, s, praxis_alloc_int(c, i))
    );
    charges_its_spine!(
        min_heap_push_charges_its_spine,
        |c| praxis_min_heap_new(c, &crate::scalars::INT),
        |c, s, i| praxis_min_heap_push(c, s, praxis_alloc_int(c, i))
    );
}