Source file printtyp.ml
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
module M = Misc.Stdlib.String.Map
module S = Misc.Stdlib.String.Set
open Misc
open Ctype
open Longident
open Path
open Asttypes
open Types
open Mode
open Btype
open Outcometree
module String = Misc.Stdlib.String
module Int = Misc.Stdlib.Int
module Sig_component_kind = Shape.Sig_component_kind
module Style = Misc.Style
module Fmt = Format_doc
open Format_doc
let longident = Pprintast.Doc.longident
let () = Env.print_longident := longident; Mode.print_longident := longident
module Out_name = struct
let create x = { printed_name = x }
let print x = x.printed_name
let set out_name x = out_name.printed_name <- x
end
(** Some identifiers may require hiding when printing *)
type bound_ident = { hide:bool; ident:Ident.t }
let printing_env = ref Env.empty
let in_printing_env f = Env.without_cmis f !printing_env
let human_unique n id = Printf.sprintf "%s/%d" (Ident.name id) n
type namespace = Shape.Sig_component_kind.t =
| Value
| Type
| Constructor
| Label
| Unboxed_label
| Module
| Module_type
| Extension_constructor
| Class
| Class_type
| Jkind
module Namespace = struct
let id = function
| Type -> 0
| Module -> 1
| Module_type -> 2
| Class -> 3
| Class_type -> 4
| Extension_constructor | Value | Constructor | Label -> 5
| Unboxed_label -> 6
| Jkind -> 7
let size = 1 + id Jkind
let pp ppf x =
Fmt.pp_print_string ppf (Shape.Sig_component_kind.to_string x)
let lookup =
let to_lookup f lid = fst @@ in_printing_env (f (Lident lid)) in
function
| Some Type -> to_lookup Env.find_type_by_name
| Some Module -> to_lookup Env.find_module_by_name_lazy
| Some Module_type -> to_lookup Env.find_modtype_by_name_lazy
| Some Class -> to_lookup Env.find_class_by_name
| Some Class_type -> to_lookup Env.find_cltype_by_name
| Some Jkind -> to_lookup Env.find_jkind_by_name
| None
| Some(Value|Extension_constructor|Constructor|Label|Unboxed_label) ->
fun _ -> raise Not_found
let location namespace id =
let path = Path.Pident id in
try Some (
match namespace with
| Some Type -> (in_printing_env @@ Env.find_type path).type_loc
| Some Module -> (in_printing_env @@ Env.find_module_lazy path).md_loc
| Some Module_type ->
(in_printing_env @@ Env.find_modtype_lazy path).mtd_loc
| Some Class -> (in_printing_env @@ Env.find_class path).cty_loc
| Some Class_type -> (in_printing_env @@ Env.find_cltype path).clty_loc
| Some Jkind -> (in_printing_env @@ Env.find_jkind path).jkind_loc
| Some (Extension_constructor|Value|Constructor|Label|Unboxed_label)
| None ->
Location.none
) with Not_found -> None
let best_class_namespace = function
| Papply _ | Pdot _ -> Some Module
| Pextra_ty _ -> assert false
| Pident c ->
match location (Some Class) c with
| Some _ -> Some Class
| None -> Some Class_type
end
(** {2 Conflicts printing}
Conflicts arise when multiple items are attributed the same name,
the following module stores the global conflict references and
provides the printing functions for explaining the source of
the conflicts.
*)
module Conflicts = struct
type explanation =
{ kind: namespace; name:string; root_name:string; location:Location.t}
let explanations = ref M.empty
let collect_explanation namespace n id =
let name = human_unique n id in
let root_name = Ident.name id in
if not (M.mem name !explanations) then
match Namespace.location (Some namespace) id with
| None -> ()
| Some location ->
let explanation = { kind = namespace; location; name; root_name } in
explanations := M.add name explanation !explanations
let pp_explanation ppf r=
Fmt.fprintf ppf "@[<v 2>%a:@,Definition of %s %a@]"
(Location.Doc.loc ~capitalize_first:true) r.location
(Shape.Sig_component_kind.to_string r.kind)
Style.inline_code r.name
let print_located_explanations ppf l =
Fmt.fprintf ppf "@[<v>%a@]"
(Fmt.pp_print_list pp_explanation) l
let reset () = explanations := M.empty
let list_explanations () =
let c = !explanations in
reset ();
c |> M.bindings |> List.map snd |> List.sort Stdlib.compare
let print_toplevel_hint ppf l =
let conj ppf () = Fmt.fprintf ppf " and@ " in
let pp_namespace_plural ppf n = Fmt.fprintf ppf "%as" Namespace.pp n in
let root_names = List.map (fun r -> r.kind, r.root_name) l in
let unique_root_names = List.sort_uniq Stdlib.compare root_names in
let submsgs = Array.make Namespace.size [] in
let () = List.iter (fun (n,_ as x) ->
submsgs.(Namespace.id n) <- x :: submsgs.(Namespace.id n)
) unique_root_names in
let pp_submsg ppf names =
match names with
| [] -> ()
| [namespace, a] ->
Fmt.fprintf ppf
"@ \
@[<2>@{<hint>Hint@}: The %a %a has been defined multiple times@ \
in@ this@ toplevel@ session.@ \
Some toplevel values still refer to@ old@ versions@ of@ this@ %a.\
@ Did you try to redefine them?@]"
Namespace.pp namespace
Style.inline_code a Namespace.pp namespace
| (namespace, _) :: _ :: _ ->
Fmt.fprintf ppf
"@ \
@[<2>@{<hint>Hint@}: The %a %a have been defined multiple times@ \
in@ this@ toplevel@ session.@ \
Some toplevel values still refer to@ old@ versions@ of@ those@ %a.\
@ Did you try to redefine them?@]"
pp_namespace_plural namespace
Fmt.(pp_print_list ~pp_sep:conj Style.inline_code)
(List.map snd names)
pp_namespace_plural namespace in
Array.iter (pp_submsg ppf) submsgs
let print_explanations ppf =
let ltop, l =
let from_toplevel a =
a.location.Location.loc_start.Lexing.pos_fname = "//toplevel//" in
List.partition from_toplevel (list_explanations ())
in
begin match l with
| [] -> ()
| l -> Fmt.fprintf ppf "@,%a" print_located_explanations l
end;
print_toplevel_hint ppf ltop
let exists () = M.cardinal !explanations >0
end
module Naming_context = struct
let enabled = ref true
let enable b = enabled := b
(** Name mapping *)
type mapping =
| Need_unique_name of int Ident.Map.t
(** The same name has already been attributed to multiple types.
The [map] argument contains the specific binding time attributed to each
types.
*)
| Uniquely_associated_to of Ident.t * out_name
(** For now, the name [Ident.name id] has been attributed to [id],
[out_name] is used to expand this name if a conflict arises
at a later point
*)
| Associated_to_pervasives of out_name
(** [Associated_to_pervasives out_name] is used when the item
[Stdlib.$name] has been associated to the name [$name].
Upon a conflict, this name will be expanded to ["Stdlib." ^ name ] *)
let hid_start = 0
let add_hid_id id map =
let new_id = 1 + Ident.Map.fold (fun _ -> Int.max) map hid_start in
new_id, Ident.Map.add id new_id map
let find_hid id map =
try Ident.Map.find id map, map with
Not_found -> add_hid_id id map
let pervasives name = "Stdlib." ^ name
let map = Array.make Namespace.size M.empty
let get namespace = map.(Namespace.id namespace)
let set namespace x = map.(Namespace.id namespace) <- x
let protected = ref S.empty
let fuzzy = ref S.empty
let with_arg id f =
protect_refs [ R(fuzzy, S.add (Ident.name id) !fuzzy) ] f
let fuzzy_id namespace id = namespace = Module && S.mem (Ident.name id) !fuzzy
let with_hidden ids f =
let update m id = S.add (Ident.name id.ident) m in
protect_refs [ R(protected, List.fold_left update !protected ids)] f
let pervasives_name namespace name =
match namespace, !enabled with
| None, _ | _, true -> Out_name.create name
| Some namespace, false ->
match M.find name (get namespace) with
| Associated_to_pervasives r -> r
| Need_unique_name _ -> Out_name.create (pervasives name)
| Uniquely_associated_to (id',r) ->
let hid, map = add_hid_id id' Ident.Map.empty in
Out_name.set r (human_unique hid id');
Conflicts.collect_explanation namespace hid id';
set namespace @@ M.add name (Need_unique_name map) (get namespace);
Out_name.create (pervasives name)
| exception Not_found ->
let r = Out_name.create name in
set namespace @@ M.add name (Associated_to_pervasives r) (get namespace);
r
(** Lookup for preexisting named item within the current {!printing_env} *)
let env_ident namespace name =
if S.mem name !protected then None else
match Namespace.lookup namespace name with
| Pident id -> Some id
| _ -> None
| exception Not_found -> None
(** Associate a name to the identifier [id] within [namespace] *)
let ident_name_simple namespace id =
match namespace, !enabled with
| None, _ | _, false -> Out_name.create (Ident.name id)
| Some namespace, true ->
if fuzzy_id namespace id then Out_name.create (Ident.name id)
else
let name = Ident.name id in
match M.find name (get namespace) with
| Uniquely_associated_to (id',r) when Ident.same id id' ->
r
| Need_unique_name map ->
let hid, m = find_hid id map in
Conflicts.collect_explanation namespace hid id;
set namespace @@ M.add name (Need_unique_name m) (get namespace);
Out_name.create (human_unique hid id)
| Uniquely_associated_to (id',r) ->
let hid', m = find_hid id' Ident.Map.empty in
let hid, m = find_hid id m in
Out_name.set r (human_unique hid' id');
List.iter (fun (id,hid) -> Conflicts.collect_explanation namespace hid id)
[id, hid; id', hid' ];
set namespace @@ M.add name (Need_unique_name m) (get namespace);
Out_name.create (human_unique hid id)
| Associated_to_pervasives r ->
Out_name.set r ("Stdlib." ^ Out_name.print r);
let hid, m = find_hid id Ident.Map.empty in
set namespace @@ M.add name (Need_unique_name m) (get namespace);
Out_name.create (human_unique hid id)
| exception Not_found ->
let r = Out_name.create name in
set namespace
@@ M.add name (Uniquely_associated_to (id,r) ) (get namespace);
r
(** Same as {!ident_name_simple} but lookup to existing named identifiers
in the current {!printing_env} *)
let ident_name namespace id =
begin match env_ident namespace (Ident.name id) with
| Some id' -> ignore (ident_name_simple namespace id')
| None -> ()
end;
ident_name_simple namespace id
let reset () =
Array.iteri ( fun i _ -> map.(i) <- M.empty ) map
let with_ctx f =
let old = Array.copy map in
try_finally f
~always:(fun () -> Array.blit old 0 map 0 (Array.length map))
end
let ident_name = Naming_context.ident_name
let reset_naming_context = Naming_context.reset
let ident ppf id = pp_print_string ppf
(Out_name.print (Naming_context.ident_name_simple None id))
let namespaced_ident namespace id =
Out_name.print (Naming_context.ident_name (Some namespace) id)
let instance_name global =
let rec string_of_global global =
let ({ head; args } : Global_module.Name.t) = global in
String.concat "" (head :: List.map string_of_arg args)
and string_of_arg arg =
let ({ param; value } : Global_module.Name.argument) = arg in
Printf.sprintf "(%s)(%s)"
(Global_module.Parameter_name.to_string param) (string_of_global value)
in
let printed_name =
string_of_global global ^ " [@jane.non_erasable.instances]"
in
{ printed_name }
let ident_stdlib = Ident.create_persistent "Stdlib"
let non_shadowed_pervasive = function
| Pdot(Pident id, s) as path ->
Ident.same id ident_stdlib &&
(match in_printing_env (Env.find_type_by_name (Lident s)) with
| (path', _) -> Path.same path path'
| exception Not_found -> true)
| _ -> false
let find_double_underscore s =
let len = String.length s in
let rec loop i =
if i + 1 >= len then
None
else if s.[i] = '_' && s.[i + 1] = '_' then
Some i
else
loop (i + 1)
in
loop 0
let rec module_path_is_an_alias_of env path ~alias_of =
match Env.find_module path env with
| { md_type = Mty_alias path'; _ } ->
Path.same path' alias_of ||
module_path_is_an_alias_of env path' ~alias_of
| _ -> false
| exception Not_found -> false
let expand_longident_head name =
match find_double_underscore name with
| None -> None
| Some i ->
Some
(Ldot
(Lident (String.sub name 0 i),
Unit_info.modulize
(String.sub name (i + 2) (String.length name - i - 2))))
let rec rewrite_double_underscore_paths env p =
match p with
| Pdot (p, s) ->
Pdot (rewrite_double_underscore_paths env p, s)
| Papply (a, b) ->
Papply (rewrite_double_underscore_paths env a,
rewrite_double_underscore_paths env b)
| Pextra_ty (p, ) ->
Pextra_ty (rewrite_double_underscore_paths env p, extra)
| Pident id ->
let name = Ident.name id in
match expand_longident_head name with
| None -> p
| Some better_lid ->
match Env.find_module_by_name_lazy better_lid env with
| exception Not_found -> p
| p', _ ->
if module_path_is_an_alias_of env p' ~alias_of:p then
p'
else
p
let rewrite_double_underscore_paths env p =
if env == Env.empty then
p
else
rewrite_double_underscore_paths env p
let rec rewrite_double_underscore_longidents env (l : Longident.t) =
match l with
| Ldot (l, s) ->
Ldot (rewrite_double_underscore_longidents env l, s)
| Lapply (a, b) ->
Lapply (rewrite_double_underscore_longidents env a,
rewrite_double_underscore_longidents env b)
| Lident name ->
match expand_longident_head name with
| None -> l
| Some l' ->
match
(Env.find_module_by_name_lazy l env,
Env.find_module_by_name_lazy l' env)
with
| exception Not_found -> l
| (p, _), (p', _) ->
if module_path_is_an_alias_of env p' ~alias_of:p then
l'
else
l
let rec tree_of_path namespace = function
| Pident id ->
Oide_ident (ident_name namespace id)
| Pdot(_, s) as path when non_shadowed_pervasive path ->
Oide_ident (Naming_context.pervasives_name namespace s)
| Pdot(p, s) ->
Oide_dot (tree_of_path (Some Module) p, s)
| Papply(p1, p2) ->
Oide_apply (tree_of_path (Some Module) p1, tree_of_path (Some Module) p2)
| Pextra_ty (p, ) -> begin
match extra with
Pcstr_ty s ->
Oide_dot (tree_of_path (Some Type) p, s)
| Pext_ty ->
tree_of_path None p
| Punboxed_ty ->
Oide_hash (tree_of_path namespace p)
end
let tree_of_path namespace = function
| Pident id when Ident.is_instance id ->
Oide_ident (instance_name (Ident.to_global_exn id))
| p -> tree_of_path namespace p
let tree_of_path namespace p =
tree_of_path namespace (rewrite_double_underscore_paths !printing_env p)
let path ppf p = !Oprint.out_ident ppf (tree_of_path None p)
let string_of_path p =
Format.asprintf "%a" (Fmt.compat path) p
let strings_of_paths namespace p =
reset_naming_context ();
let trees = List.map (tree_of_path namespace) p in
List.map (Fmt.asprintf "%a" !Oprint.out_ident) trees
let () = Env.print_path := path
let () = Jkind.set_printtyp_path path
let tree_of_rec = function
| Trec_not -> Orec_not
| Trec_first -> Orec_first
| Trec_next -> Orec_next
let raw_list pr ppf = function
[] -> fprintf ppf "[]"
| a :: l ->
fprintf ppf "@[<1>[%a%t]@]" pr a
(fun ppf -> List.iter (fun x -> fprintf ppf ";@,%a" pr x) l)
let kind_vars = ref []
let kind_count = ref 0
let string_of_field_kind v =
match field_kind_repr v with
| Fpublic -> "Fpublic"
| Fabsent -> "Fabsent"
| Fprivate -> "Fprivate"
let rec safe_repr v t =
match Transient_expr.coerce t with
{desc = Tlink t} when not (List.memq t v) ->
safe_repr (t::v) t
| t' -> t'
let rec list_of_memo = function
Mnil -> []
| Mcons (_priv, p, _t1, _t2, rem) -> p :: list_of_memo rem
| Mlink rem -> list_of_memo !rem
let print_name ppf = function
None -> fprintf ppf "None"
| Some name -> fprintf ppf "\"%s\"" name
let string_of_label : Types.arg_label -> string = function
Nolabel -> ""
| Labelled s | Position s -> s
| Optional s -> "?"^s
let visited = ref []
let rec raw_type ppf ty =
let ty = safe_repr [] ty in
if List.memq ty !visited then fprintf ppf "{id=%d}" ty.id else begin
visited := ty :: !visited;
fprintf ppf "@[<1>{id=%d;level=%d;scope=%d;marks=%x;desc=@,%a}@]"
ty.id ty.level
(Transient_expr.get_scope ty) (Transient_expr.get_marks ty)
raw_type_desc ty.desc
end
and labeled_type ppf (label, ty) =
begin match label with
| Some s -> fprintf ppf "label=\"%s\" " s
| None -> ()
end;
raw_type ppf ty
and raw_type_list tl = raw_list raw_type tl
and labeled_type_list tl = raw_list labeled_type tl
and raw_lid_type_list tl =
raw_list (fun ppf (lid, typ) ->
fprintf ppf "(@,%a,@,%a)" longident lid raw_type typ)
tl
and raw_row_desc ppf row =
let Row {fields; more; name; fixed; closed} = row_repr row in
fprintf ppf
"@[<hov1>{@[%s@,%a;@]@ @[%s@,%a;@]@ %s%B;@ %s%a;@ @[<1>%s%t@]}@]"
"row_fields="
(raw_list (fun ppf (l, f) ->
fprintf ppf "@[%s,@ %a@]" l raw_field f))
fields
"row_more=" raw_type more
"row_closed=" closed
"row_fixed=" raw_row_fixed fixed
"row_name="
(fun ppf ->
match name with None -> fprintf ppf "None"
| Some(p,tl) ->
fprintf ppf "Some(@,%a,@,%a)" path p raw_type_list tl)
and raw_type_desc ppf = function
Tvar { name; jkind } ->
fprintf ppf "Tvar (@,%a,@,%a)"
print_name name (Jkind.format !printing_env) jkind
| Tarrow((l,arg,ret),t1,t2,c) ->
fprintf ppf "@[<hov1>Tarrow((\"%s\",%a,%a),@,%a,@,%a,@,%s)@]"
(string_of_label l)
(Alloc.print ~verbose:true ()) arg
(Alloc.print ~verbose:true ()) ret
raw_type t1 raw_type t2
(if is_commu_ok c then "Cok" else "Cunknown")
| Ttuple tl ->
fprintf ppf "@[<1>Ttuple@,%a@]" labeled_type_list tl
| Tunboxed_tuple tl ->
fprintf ppf "@[<1>Tunboxed_tuple@,%a@]" labeled_type_list tl
| Tconstr (p, tl, abbrev) ->
fprintf ppf "@[<hov1>Tconstr(@,%a,@,%a,@,%a)@]" path p
raw_type_list tl
(raw_list path) (list_of_memo !abbrev)
| Tobject (t, nm) ->
fprintf ppf "@[<hov1>Tobject(@,%a,@,@[<1>ref%t@])@]" raw_type t
(fun ppf ->
match !nm with None -> fprintf ppf " None"
| Some(p,tl) ->
fprintf ppf "(Some(@,%a,@,%a))" path p raw_type_list tl)
| Tquote t ->
fprintf ppf "@[Tquote@ %a@]" raw_type t
| Tsplice t ->
fprintf ppf "@[Tsplice@ %a@]" raw_type t
| Tquote_eval t ->
fprintf ppf "@[Tquote_eval@ %a@]" raw_type t
| Tfield (f, k, t1, t2) ->
fprintf ppf "@[<hov1>Tfield(@,%s,@,%s,@,%a,@;<0 -1>%a)@]" f
(string_of_field_kind k)
raw_type t1 raw_type t2
| Tnil -> fprintf ppf "Tnil"
| Tlink t -> fprintf ppf "@[<1>Tlink@,%a@]" raw_type t
| Tsubst (t, None) -> fprintf ppf "@[<1>Tsubst@,(%a,None)@]" raw_type t
| Tsubst (t, Some t') ->
fprintf ppf "@[<1>Tsubst@,(%a,@ Some%a)@]" raw_type t raw_type t'
| Tunivar { name; jkind } ->
fprintf ppf "Tunivar (@,%a,@,%a)"
print_name name (Jkind.format !printing_env) jkind
| Tpoly (t, tl) ->
fprintf ppf "@[<hov1>Tpoly(@,%a,@,%a)@]"
raw_type t
raw_type_list tl
| Trepr (t, sort_vars) ->
let print_sort_univar ppf uv =
fprintf ppf "%s" (Option.value uv.Jkind_types.Sort.name ~default:"_")
in
fprintf ppf "@[<hov1>Trepr(@,%a,@,[@[%a@]])@]"
raw_type t
(pp_print_list ~pp_sep:(fun ppf () -> fprintf ppf ";@ ")
print_sort_univar) sort_vars
| Tvariant row ->
raw_row_desc ppf row
| Tpackage (p, fl) ->
fprintf ppf "@[<hov1>Tpackage(@,%a,@,%a)@]" path p
raw_lid_type_list fl
| Tof_kind jkind ->
fprintf ppf "Tof_kind@ %a" (Jkind.format !printing_env) jkind
and raw_row_fixed ppf = function
| None -> fprintf ppf "None"
| Some Types.Fixed_private -> fprintf ppf "Some Fixed_private"
| Some Types.Rigid -> fprintf ppf "Some Rigid"
| Some Types.Univar t -> fprintf ppf "Some(Univar(%a))" raw_type t
| Some Types.Reified p -> fprintf ppf "Some(Reified(%a))" path p
| Some Types.Fixed_existential -> fprintf ppf "Some Fixed_existential"
and raw_field ppf rf =
match_row_field
~absent:(fun _ -> fprintf ppf "RFabsent")
~present:(function
| None ->
fprintf ppf "RFpresent None"
| Some t ->
fprintf ppf "@[<1>RFpresent(Some@,%a)@]" raw_type t)
~either:(fun c tl m e ->
fprintf ppf "@[<hov1>RFeither(%B,@,%a,@,%B,@,@[<1>ref%t@])@]" c
raw_type_list tl m
(fun ppf ->
match e with None -> fprintf ppf " RFnone"
| Some f -> fprintf ppf "@,@[<1>(%a)@]" raw_field f))
rf
let raw_type_expr ppf t =
visited := []; kind_vars := []; kind_count := 0;
raw_type ppf t;
visited := []; kind_vars := []
let () = Btype.print_raw := compat raw_type_expr
let set_printing_env env =
printing_env :=
if !Clflags.real_paths then Env.empty
else env
let wrap_mutation f =
let snap = Btype.snapshot () in
try_finally f ~always:(fun () -> Btype.backtrack snap)
let wrap_printing_env ~reset_names env f =
let old_env = !printing_env in
set_printing_env (Env.update_short_paths env);
if reset_names then reset_naming_context ();
try_finally f ~always:(fun () -> set_printing_env old_env)
let wrap_printing_env ?error:_ env f =
Env.without_cmis (wrap_printing_env ~reset_names:true env) f
and wrap_printing_env_unguarded env f =
wrap_printing_env ~reset_names:false env f
type type_result = Short_paths.type_result =
| Nth of int
| Path of int list option * Path.t
type type_resolution = Short_paths.type_resolution =
| Nth of int
| Subst of int list
| Id
let apply_subst ns args =
List.map (List.nth args) ns
let apply_subst_opt nso args =
match nso with
| None -> args
| Some ns -> apply_subst ns args
let apply_nth n args =
List.nth args n
let best_type_path p =
if !Clflags.real_paths || !printing_env == Env.empty
then Path(None, p)
else Short_paths.find_type (Env.short_paths !printing_env) p
let best_type_path_resolution p =
if !Clflags.real_paths || !printing_env == Env.empty
then Id
else Short_paths.find_type_resolution (Env.short_paths !printing_env) p
let best_type_path_simple p =
if !Clflags.real_paths || !printing_env == Env.empty
then p
else Short_paths.find_type_simple (Env.short_paths !printing_env) p
let best_module_type_path p =
if !Clflags.real_paths || !printing_env == Env.empty
then p
else Short_paths.find_module_type (Env.short_paths !printing_env) p
let best_module_path p =
if !Clflags.real_paths || !printing_env == Env.empty
then p
else Short_paths.find_module (Env.short_paths !printing_env) p
let best_class_type_path p =
if !Clflags.real_paths || !printing_env == Env.empty
then None, p
else Short_paths.find_class_type (Env.short_paths !printing_env) p
let best_class_type_path_simple p =
if !Clflags.real_paths || !printing_env == Env.empty
then p
else Short_paths.find_class_type_simple (Env.short_paths !printing_env) p
let proxy ty = Transient_expr.repr (proxy ty)
type type_or_scheme = Type | Type_scheme
let is_non_gen mode ty =
match mode with
| Type_scheme -> is_Tvar ty && get_level ty <> generic_level
| Type -> false
let nameable_row row =
row_name row <> None &&
List.for_all
(fun (_, f) ->
match row_field_repr f with
| Reither(c, l, _) ->
row_closed row && if c then l = [] else List.length l = 1
| _ -> true)
(row_fields row)
let printer_iter_type_expr f ty =
match get_desc ty with
| Tconstr(p, tyl, _) -> begin
match best_type_path_resolution p with
| Nth n ->
f (apply_nth n tyl)
| Subst ns ->
List.iter f (apply_subst ns tyl)
| Id ->
List.iter f tyl
end
| Tvariant row -> begin
match row_name row with
| Some(_p, tyl) when nameable_row row ->
List.iter f tyl
| _ ->
iter_row f row
end
| Tobject (fi, nm) -> begin
match !nm with
| None ->
let fields, _ = flatten_fields fi in
List.iter
(fun (_, kind, ty) ->
if field_kind_repr kind = Fpublic then
f ty)
fields
| Some (_, l) ->
List.iter f (List.tl l)
end
| Tfield(_, kind, ty1, ty2) ->
if field_kind_repr kind = Fpublic then
f ty1;
f ty2
| _ ->
Btype.iter_type_expr f ty
let quoted_ident ppf x =
Style.as_inline_code !Oprint.out_ident ppf x
module Internal_names : sig
val reset : unit -> unit
val add : Path.t -> unit
val print_explanations : Env.t -> Fmt.formatter -> unit
end = struct
let names = ref Ident.Set.empty
let reset () =
names := Ident.Set.empty
let add p =
match p with
| Pident id ->
let name = Ident.name id in
if String.length name > 0 && name.[0] = '$' then begin
names := Ident.Set.add id !names
end
| Pdot _ | Papply _ | Pextra_ty _ -> ()
let print_explanations env ppf =
let constrs =
Ident.Set.fold
(fun id acc ->
let p = Pident id in
match Env.find_type p env with
| exception Not_found -> acc
| decl ->
match type_origin decl with
| Existential constr ->
let prev = String.Map.find_opt constr acc in
let prev = Option.value ~default:[] prev in
String.Map.add constr (tree_of_path None p :: prev) acc
| Definition | Rec_check_regularity -> acc)
!names String.Map.empty
in
String.Map.iter
(fun constr out_idents ->
match out_idents with
| [] -> ()
| [out_ident] ->
fprintf ppf
"@ @[<2>@{<hint>Hint@}:@ %a@ is an existential type@ \
bound by the constructor@ %a.@]"
quoted_ident out_ident
Style.inline_code constr
| out_ident :: out_idents ->
fprintf ppf
"@ @[<2>@{<hint>Hint@}:@ %a@ and %a@ are existential types@ \
bound by the constructor@ %a.@]"
(Fmt.pp_print_list
~pp_sep:(fun ppf () -> fprintf ppf ",@ ")
quoted_ident)
(List.rev out_idents)
quoted_ident out_ident
Style.inline_code constr)
constrs
end
module Names : sig
val reset_names : unit -> unit
val add_named_vars : type_expr -> unit
val add_subst : (type_expr * type_expr) list -> unit
val new_name : unit -> string
val new_var_name : non_gen:bool -> type_expr -> unit -> string
val name_of_type : (unit -> string) -> transient_expr -> string
val check_name_of_type : non_gen:bool -> transient_expr -> unit
val remove_names : transient_expr list -> unit
val with_local_names : (unit -> 'a) -> 'a
val refresh_weak : unit -> unit
end = struct
let names = ref ([] : (transient_expr * string) list)
let name_subst = ref ([] : (transient_expr * transient_expr) list)
let name_counter = ref 0
let named_vars = ref ([] : string list)
let visited_for_named_vars = ref ([] : transient_expr list)
let weak_counter = ref 1
let weak_var_map = ref TypeMap.empty
let named_weak_vars = ref String.Set.empty
let reset_names () =
names := [];
name_subst := [];
name_counter := 0;
named_vars := [];
visited_for_named_vars := []
let add_named_var tty =
match tty.desc with
Tvar { name = Some name } | Tunivar { name = Some name } ->
if List.mem name !named_vars then () else
named_vars := name :: !named_vars
| _ -> ()
let rec add_named_vars ty =
let tty = Transient_expr.repr ty in
let px = proxy ty in
if not (List.memq px !visited_for_named_vars) then begin
visited_for_named_vars := px :: !visited_for_named_vars;
match tty.desc with
| Tvar _ | Tunivar _ ->
add_named_var tty
| _ ->
printer_iter_type_expr add_named_vars ty
end
let substitute ty =
match List.assq ty !name_subst with
| ty' -> ty'
| exception Not_found -> ty
let add_subst subst =
name_subst :=
List.map (fun (t1,t2) -> Transient_expr.repr t1, Transient_expr.repr t2)
subst
@ !name_subst
let name_is_already_used name =
List.mem name !named_vars
|| List.exists (fun (_, name') -> name = name') !names
|| String.Set.mem name !named_weak_vars
let rec new_name () =
let name = Misc.letter_of_int !name_counter in
incr name_counter;
if name_is_already_used name then new_name () else name
let rec new_weak_name ty () =
let name = "weak" ^ Int.to_string !weak_counter in
incr weak_counter;
if name_is_already_used name then new_weak_name ty ()
else begin
named_weak_vars := String.Set.add name !named_weak_vars;
weak_var_map := TypeMap.add ty name !weak_var_map;
name
end
let new_var_name ~non_gen ty () =
if non_gen then new_weak_name ty ()
else new_name ()
let name_of_type name_generator t =
let t = substitute t in
try List.assq t !names with Not_found ->
try TransientTypeMap.find t !weak_var_map with Not_found ->
let name =
match t.desc with
Tvar { name = Some name } | Tunivar { name = Some name } ->
let available name =
List.for_all
(fun (_, name') -> name <> name')
!names
in
if available name then name
else
let suffixed i = name ^ Int.to_string i in
let i = Misc.find_first_mono (fun i -> available (suffixed i)) in
suffixed i
| _ ->
name_generator ()
in
if name <> "_" then names := (t, name) :: !names;
name
let check_name_of_type ~non_gen px =
let name_gen = new_var_name ~non_gen (Transient_expr.type_expr px) in
ignore(name_of_type name_gen px)
let remove_names tyl =
let tyl = List.map substitute tyl in
names := List.filter (fun (ty,_) -> not (List.memq ty tyl)) !names
let with_local_names f =
let old_names = !names in
let old_subst = !name_subst in
names := [];
name_subst := [];
try_finally
~always:(fun () ->
names := old_names;
name_subst := old_subst)
f
let refresh_weak () =
let refresh t name (m,s) =
if is_non_gen Type_scheme t then
begin
TypeMap.add t name m,
String.Set.add name s
end
else m, s in
let m, s =
TypeMap.fold refresh !weak_var_map (TypeMap.empty ,String.Set.empty) in
named_weak_vars := s;
weak_var_map := m
end
let reserve_names ty =
normalize_type ty;
Names.add_named_vars ty
let visited_objects = ref ([] : transient_expr list)
let aliased = ref ([] : transient_expr list)
let delayed = ref ([] : transient_expr list)
let printed_aliases = ref ([] : transient_expr list)
let add_delayed t =
if not (List.memq t !delayed) then delayed := t :: !delayed
let is_aliased_proxy px = List.memq px !aliased
let add_alias_proxy px =
if not (is_aliased_proxy px) then
aliased := px :: !aliased
let add_alias ty = add_alias_proxy (proxy ty)
let add_printed_alias_proxy ~non_gen px =
Names.check_name_of_type ~non_gen px;
printed_aliases := px :: !printed_aliases
let add_printed_alias ty = add_printed_alias_proxy (proxy ty)
let aliasable ty =
match get_desc ty with
Tvar _ | Tunivar _ | Tpoly _ | Trepr _ -> false
| Tconstr (p, _, _) -> begin
match best_type_path_resolution p with
| Nth _ -> false
| Subst _ | Id -> true
end
| _ -> true
let should_visit_object ty =
match get_desc ty with
| Tvariant row -> not (static_row row)
| Tobject _ -> opened_object ty
| _ -> false
let rec mark_loops_rec visited ty =
let px = proxy ty in
if List.memq px visited && aliasable ty then add_alias_proxy px else
let tty = Transient_expr.repr ty in
let visited = px :: visited in
match tty.desc with
| Tvariant _ | Tobject _ ->
if List.memq px !visited_objects then add_alias_proxy px else begin
if should_visit_object ty then
visited_objects := px :: !visited_objects;
printer_iter_type_expr (mark_loops_rec visited) ty
end
| Tpoly(ty, tyl) ->
List.iter add_alias tyl;
mark_loops_rec visited ty
| _ ->
printer_iter_type_expr (mark_loops_rec visited) ty
let mark_loops ty =
mark_loops_rec [] ty;;
let prepare_type ty =
reserve_names ty;
mark_loops ty;;
let reset_loop_marks () =
visited_objects := []; aliased := []; delayed := []; printed_aliases := []
let reset_except_context () =
Names.reset_names (); reset_loop_marks (); Internal_names.reset ()
let reset () =
reset_naming_context (); Conflicts.reset ();
reset_except_context ()
let prepare_for_printing tyl =
reset_except_context ();
List.iter prepare_type tyl
let add_type_to_preparation = prepare_type
let print_labels = ref true
let print_reduced_evals = ref true
let out_jkind_of_const_jkind env jkind =
Ojkind_const (Jkind.Const.to_out_jkind_const env jkind)
let rec out_jkind_of_desc env (desc : 'd Jkind.Desc.t) =
match desc.base with
| Layout (Sort (Var n, sa)) ->
Ojkind_var ("'_representable_layout_" ^
Int.to_string (Jkind.Sort.Var.get_print_number n),
Jkind.Scannable_axes.to_string_list sa)
| Layout (Product lays) ->
Ojkind_product
(List.map
(fun layout ->
out_jkind_of_desc env { desc with base = Layout layout })
lays)
| _ -> match Jkind.Desc.get_const desc with
| Some c -> out_jkind_of_const_jkind env c
| None -> assert false
let out_jkind_option_of_jkind ~ignore_null env jkind =
let desc = Jkind.get jkind in
let elide =
Jkind.is_value_for_printing ~ignore_null env jkind
|| (match desc.base with
| Layout (Sort (Var _, _)) -> true
| _ -> false)
in
if elide then None else Some (out_jkind_of_desc env desc)
let alias_nongen_row mode px ty =
match get_desc ty with
| Tvariant _ | Tobject _ ->
if is_non_gen mode (Transient_expr.type_expr px) then
add_alias_proxy px
| _ -> ()
let outcome_label : Types.arg_label -> Outcometree.arg_label = function
| Nolabel -> Nolabel
| Labelled l -> Labelled l
| Optional l -> Optional l
| Position l -> Position l
(** Un-interpret modalities back to outcome tree. Takes the mutability and
attributes on the field and removes mutable-implied modalities
accordingly. *)
let tree_of_modalities mut t =
t
|> Typemode.least_modalities ~include_implied:false ~mut
|> Typemode.sort_dedup_modalities
|> List.map (fun (Atom (ax, m) : Modality.atom) ->
Fmt.asprintf "%a" (Modality.Per_axis.print ax) m)
let tree_of_modes (modes : Mode.Alloc.Const.t) =
let diff =
let forkable =
match modes.areality, modes.forkable with
| Local, Unforkable | Global, Forkable -> None
| _, _ -> Some modes.forkable
in
let yielding =
match modes.areality, modes.yielding with
| Local, Yielding | Global, Unyielding -> None
| _, _ -> Some modes.yielding
in
let contention =
match modes.visibility, modes.contention with
| Immutable, Contended
| Read, Shared
| Write, Corrupted
| Read_write, Uncontended -> None
| _, _ -> Some modes.contention
in
let portability =
match modes.statefulness, modes.portability with
| Stateless, Portable
| Reading, Shareable
| Writing, Corruptible
| Stateful, Nonportable -> None
| _, _ -> Some modes.portability
in
let diff = Mode.Alloc.Const.diff modes Mode.Alloc.Const.legacy in
{ diff with forkable; yielding; contention; portability }
in
let print_to_string_opt print a = Option.map (Fmt.asprintf "%a" print) a in
let modes =
[ print_to_string_opt Mode.Locality.Const.print diff.areality
; print_to_string_opt Mode.Uniqueness.Const.print diff.uniqueness
; print_to_string_opt Mode.Linearity.Const.print diff.linearity
; print_to_string_opt Mode.Portability.Const.print diff.portability
; print_to_string_opt Mode.Contention.Const.print diff.contention
; print_to_string_opt Mode.Forkable.Const.print diff.forkable
; print_to_string_opt Mode.Yielding.Const.print diff.yielding
; print_to_string_opt Mode.Statefulness.Const.print diff.statefulness
; print_to_string_opt Mode.Visibility.Const.print diff.visibility ]
in
List.filter_map (fun x -> x) modes
(** The modal context on a type when printing it. This is to reproduce the mode
currying logic in [typetexp.ml], so that parsing and printing roundtrip. *)
type modal =
| Arrow_return of
{ acc : Mode.Alloc.Const.t;
mode : Mode.Alloc.lr; }
(** This is the RHS (say [r]) of an arrow type, where [mode] is the real
mode of [r]. and:
- If [r] is also an arrow type, then [acc] is how users would interpret
[r]'s mode, if [r] doesn't have any parens aound it.
- If [r] is not an arrow type, in which case [acc] is meaningless.
The callee is responsible for printing the type with the modes, with parens
if needed.
Note that if [r] is an aliased type (e.g., [(int -> 'r) as 'r]), it will be
treated as NOT an arrow type, to align with the currying logic in
[typetexp.ml].
If [r] is [Tpoly (Tarrow_, [])], it will be treated as NOT an arrow type.
This gives tedious (but still correct) printing. *)
| Other of Mode.Alloc.Const.t
(** In other cases, the caller has already printed the modes (as the
constructor argument) on the type. *)
type typobject_repr = { fields : (string * type_expr) list; open_row : bool }
type typvariant_repr = {
fields : (string * bool * type_expr list) list;
name : (Path.t * type_expr list) option;
closed : bool;
present : (string * row_field) list;
all_present : bool;
tags : string list option
}
let rec tree_of_modal_typexp mode modal ty =
let not_arrow tree =
match modal with
| Arrow_return {mode; _} ->
let mode = Alloc.zap_to_legacy mode in
Otyp_ret (Orm_any (tree_of_modes mode), tree)
| Other _ -> tree
in
let ty =
Ctype.reduce_head ~expand_eval:!print_reduced_evals !printing_env ty
in
let px = proxy ty in
if List.memq px !printed_aliases && not (List.memq px !delayed) then
let non_gen = is_non_gen mode (Transient_expr.type_expr px) in
let name = Names.name_of_type (Names.new_var_name ~non_gen ty) px in
not_arrow (Otyp_var (non_gen, name)) else
let pr_typ alloc_mode =
let tty = Transient_expr.repr ty in
match tty.desc with
| Tvar _ ->
let non_gen = is_non_gen mode ty in
let name_gen = Names.new_var_name ~non_gen ty in
Otyp_var (non_gen, Names.name_of_type name_gen tty)
| Tarrow ((l, marg, mret), ty1, ty2, _) ->
let lab =
if !print_labels || is_omittable l then outcome_label l
else Nolabel
in
let arg_mode = Alloc.zap_to_legacy marg in
let t1 =
if is_optional l then
match
get_desc (Ctype.expand_head !printing_env (tpoly_get_mono ty1))
with
| Tconstr(path, [ty], _)
when Path.same path Predef.path_option ->
tree_of_typexp mode arg_mode ty
| _ -> Otyp_stuff "<hidden>"
else
tree_of_typexp mode arg_mode ty1
in
let acc_mode = curry_mode alloc_mode arg_mode in
let modal = Arrow_return {acc = acc_mode; mode = mret} in
let t2 = tree_of_modal_typexp mode modal ty2 in
Otyp_arrow (lab, tree_of_modes arg_mode, t1, t2)
| Ttuple labeled_tyl ->
Otyp_tuple (tree_of_labeled_typlist mode labeled_tyl)
| Tunboxed_tuple labeled_tyl ->
Otyp_unboxed_tuple (tree_of_labeled_typlist mode labeled_tyl)
| Tconstr(p, tyl, _abbrev) -> begin
match best_type_path p with
| Nth n -> tree_of_typexp mode Alloc.Const.legacy (apply_nth n tyl)
| Path(nso, p') ->
Internal_names.add p';
let tyl' = apply_subst_opt nso tyl in
Otyp_constr (tree_of_path (Some Type) p', tree_of_typlist mode tyl')
end
| Tvariant row ->
let { fields; name; closed; present; all_present; tags } =
tree_of_typvariant_repr row
in
begin match name with
| Some(p, tyl) when nameable_row row ->
let out_variant =
match best_type_path p with
| Nth n -> tree_of_typexp mode Alloc.Const.legacy (apply_nth n tyl)
| Path(s, p) ->
let id = tree_of_path (Some Type) p in
let args = tree_of_typlist mode (apply_subst_opt s tyl) in
Otyp_constr (id, args)
in
if closed && all_present then
out_variant
else
let tags =
if all_present then None else Some (List.map fst present) in
Otyp_variant (Ovar_typ out_variant, closed, tags)
| _ ->
let fields =
List.map
(fun (l, c, tyl) -> (l, c, tree_of_typlist mode tyl)) fields
in
Otyp_variant (Ovar_fields fields, closed, tags)
end
| Tobject (fi, nm) ->
tree_of_typobject mode fi !nm
| Tquote ty ->
wrap_printing_env_unguarded
(Env.enter_quotation !printing_env)
(fun () -> Otyp_quote (tree_of_typexp mode alloc_mode ty))
| Tsplice ty ->
wrap_printing_env_unguarded
(Env.enter_splice ~loc:Location.none !printing_env)
(fun () -> Otyp_splice (tree_of_typexp mode alloc_mode ty))
| Tquote_eval ty ->
(match best_type_path Predef.path_eval with
| Nth _ ->
failwith "printtyp: unexpected Nth as result of best_type_path for eval"
| Path (s, p') ->
let ty = newgenty (Tquote ty) in
let tyl = apply_subst_opt s [ty] in
Internal_names.add p';
let tyl =
wrap_printing_env_unguarded
(Env.enter_quotation !printing_env)
(fun () -> tree_of_typlist mode tyl)
in
Otyp_constr (tree_of_path (Some Type) p', tyl))
| Tnil | Tfield _ ->
tree_of_typobject mode ty None
| Tsubst _ ->
Otyp_stuff "<Tsubst>"
| Tlink _ ->
fatal_error "Printtyp.tree_of_typexp"
| Tpoly (ty, []) | Trepr (ty, []) ->
tree_of_typexp mode alloc_mode ty
| Tpoly (ty, tyl) ->
let tyl = List.map Transient_expr.repr tyl in
let old_delayed = !delayed in
List.iter add_delayed tyl;
let tl = tree_of_qtvs tyl in
let tr = Otyp_poly (tl, tree_of_typexp mode alloc_mode ty) in
Names.remove_names tyl;
delayed := old_delayed; tr
| Trepr (ty, sort_vars) ->
(match get_desc ty with
| Tpoly (inner_ty, (_ :: _ as tyl)) ->
let sorts_match =
match
List.for_all2
(fun sort_var ty ->
match get_desc ty with
| Tunivar { jkind } ->
(match Jkind.get_layout !printing_env jkind with
| Some layout ->
(match Jkind.Layout.Const.get_sort layout with
| Some (Jkind.Sort.Const.Univar uv) ->
uv == sort_var
| _ -> false)
| None -> false)
| _ -> false)
sort_vars tyl
with
| result -> result
| exception Invalid_argument _ -> false
in
if sorts_match then begin
let tyl = List.map Transient_expr.repr tyl in
let old_delayed = !delayed in
List.iter add_delayed tyl;
let sort_names = tree_of_qsvs tyl in
let tr =
Otyp_repr (sort_names, tree_of_typexp mode alloc_mode inner_ty)
in
Names.remove_names tyl;
delayed := old_delayed;
tr
end else
tree_of_typexp mode alloc_mode ty
| _ ->
tree_of_typexp mode alloc_mode ty)
| Tunivar _ ->
Otyp_var (false, Names.name_of_type Names.new_name tty)
| Tpackage (p, fl) ->
let p = best_module_type_path p in
let fl =
List.map
(fun (li, ty) -> (
String.concat "." (Longident.flatten li),
tree_of_typexp mode Alloc.Const.legacy ty
)) fl in
Otyp_module (tree_of_path (Some Module_type) p, fl)
| Tof_kind jkind ->
Otyp_of_kind (out_jkind_of_desc !printing_env (Jkind.get jkind))
in
if List.memq px !delayed then delayed := List.filter ((!=) px) !delayed;
alias_nongen_row mode px ty;
if is_aliased_proxy px && aliasable ty then begin
let non_gen = is_non_gen mode (Transient_expr.type_expr px) in
add_printed_alias_proxy ~non_gen px;
let alias = Names.name_of_type (Names.new_var_name ~non_gen ty) px in
let tree =
Otyp_alias {non_gen; aliased = pr_typ Mode.Alloc.Const.legacy; alias }
in
not_arrow tree end
else
match modal with
| Arrow_return {acc; mode} ->
let rm, alloc_mode = tree_of_ret_typ_mutating acc mode ty in
let ty = pr_typ alloc_mode in
Otyp_ret (rm, ty)
| Other m -> pr_typ m
and tree_of_typexp mode alloc_mode ty =
tree_of_modal_typexp mode (Other alloc_mode) ty
and tree_of_qtvs qtvs =
let tree_of_qtv v : (string * out_jkind option) option =
let tree jkind =
Some (Names.name_of_type Names.new_name v,
out_jkind_option_of_jkind ~ignore_null:true !printing_env jkind)
in
match v.desc with
| Tvar { jkind } when v.level = generic_level -> tree jkind
| Tunivar { jkind } -> tree jkind
| _ -> None
in
List.filter_map tree_of_qtv qtvs
and tree_of_qsvs qtvs =
List.filter_map
(fun v ->
match v.desc with
| Tvar _ when v.level = generic_level ->
Some (Names.name_of_type Names.new_name v)
| Tunivar _ -> Some (Names.name_of_type Names.new_name v)
| _ -> None)
qtvs
and tree_of_row_field (l, f) =
match row_field_repr f with
| Rpresent None | Reither(true, [], _) -> (l, false, [])
| Rpresent(Some ty) -> (l, false, [ty])
| Reither(c, tyl, _) ->
if c
then (l, true, tyl)
else (l, false, tyl)
| Rabsent -> (l, false, [] )
and tree_of_typvariant_repr row =
let Row {fields; name; closed; _} = row_repr row in
let fields =
if closed then
List.filter (fun (_, f) -> row_field_repr f <> Rabsent)
fields
else fields in
let present =
List.filter
(fun (_, f) ->
match row_field_repr f with
| Rpresent _ -> true
| _ -> false)
fields in
let all_present = List.length present = List.length fields in
let fields = List.map tree_of_row_field fields in
let tags =
if all_present then None else Some (List.map fst present) in
{ fields; name; closed; present; all_present; tags }
and tree_of_typlist mode tyl =
List.map (tree_of_typexp mode Alloc.Const.legacy) tyl
and tree_of_labeled_typlist mode tyl =
List.map (fun (label, ty) -> label, tree_of_typexp mode Alloc.Const.legacy ty) tyl
and tree_of_typ_gf {ca_type=ty; ca_modalities=gf; _} =
(tree_of_typexp Type Alloc.Const.legacy ty,
tree_of_modalities Immutable gf)
(** NB: This function might mutate states; the caller is responsible for
reverting them. *)
and tree_of_ret_typ_mutating acc_mode m ty=
match get_desc ty with
| Tarrow _ -> begin
match Alloc.equate (Alloc.of_const acc_mode) m with
| Ok () ->
(Orm_no_parens, acc_mode)
| Error _ ->
let m = Alloc.zap_to_legacy m in
(Orm_parens (tree_of_modes m), m)
end
| _ ->
let m = Alloc.zap_to_legacy m in
(Orm_any (tree_of_modes m), m)
and tree_of_typobject_repr fi =
let (fields, rest) = flatten_fields fi in
let present_fields =
List.fold_right
(fun (n, k, t) l ->
match field_kind_repr k with
| Fpublic -> (n, t) :: l
| _ -> l)
fields [] in
let sorted_fields =
List.sort
(fun (n, _) (n', _) -> String.compare n n') present_fields in
let fields, open_row = tree_of_typfields rest sorted_fields in
{ fields; open_row }
and tree_of_typobject mode fi nm =
begin match nm with
| None ->
let { fields; open_row } = tree_of_typobject_repr fi in
let fields =
List.map
(fun (s, t) -> (s, tree_of_typexp mode Alloc.Const.legacy t))
fields
in
Otyp_object {fields; open_row}
| Some (p, _ty :: tyl) ->
let args = tree_of_typlist mode tyl in
let p' = best_type_path_simple p in
Otyp_class (tree_of_path (Some Type) p', args)
| _ ->
fatal_error "Printtyp.tree_of_typobject"
end
and tree_of_typfields rest = function
| [] ->
let open_row =
match get_desc rest with
| Tvar _ | Tunivar _ | Tconstr _-> true
| Tnil -> false
| _ -> fatal_error "typfields (1)"
in
([], open_row)
| field :: l ->
let (fields, rest) = tree_of_typfields rest l in
(field :: fields, rest)
let tree_of_typexp mode ty =
wrap_mutation (fun () -> tree_of_typexp mode Alloc.Const.legacy ty)
let tree_of_typexp mode ty =
if Ctype.contains_toplevel_splice (Env.stage !printing_env :> int) ty
then
wrap_printing_env_unguarded
(Env.enter_future !printing_env)
(fun () -> tree_of_typexp mode ty)
else
tree_of_typexp mode ty
let typexp mode ppf ty =
!Oprint.out_type ppf (tree_of_typexp mode ty)
let modality ?(id = fun _ppf -> ()) ax ppf modality =
if Mode.Modality.Per_axis.is_id ax modality then id ppf
else
Fmt.asprintf "%a" (Mode.Modality.Per_axis.print ax) modality
|> !Oprint.out_modality ppf
let prepared_type_expr ppf ty = typexp Type ppf ty
let prepared_type_scheme ppf ty = typexp Type_scheme ppf ty
let type_expr ppf ty =
prepare_for_printing [ty];
prepared_type_expr ppf ty
let type_expr_with_reserved_names ppf ty =
reset_loop_marks ();
mark_loops ty;
prepared_type_expr ppf ty
let shared_type_scheme ppf ty =
prepare_type ty;
typexp Type_scheme ppf ty
let type_scheme ppf ty =
prepare_for_printing [ty];
prepared_type_scheme ppf ty
let type_path ppf p =
let p = best_class_type_path_simple p in
let t = tree_of_path (Some Type) p in
!Oprint.out_ident ppf t
let tree_of_type_scheme ty =
prepare_for_printing [ty];
tree_of_typexp Type_scheme ty
let () =
Env.print_type_expr := type_expr;
Env.report_jkind_violation_with_offender :=
Jkind.Violation.report_with_offender;
Jkind.set_outcometrees_of_types (fun tys ->
prepare_for_printing tys;
List.map (tree_of_typexp Type) tys);
Jkind.set_outcometree_of_modalities tree_of_modalities;
Jkind.set_print_type_expr type_expr;
Jkind.set_raw_type_expr (compat raw_type_expr)
let tree_of_constraints params =
List.fold_right
(fun ty list ->
let ty' = unalias ty in
if proxy ty != proxy ty' then
let tr = tree_of_typexp Type_scheme ty in
(tr, tree_of_typexp Type_scheme ty') :: list
else list)
params []
let filter_params tyl =
let params =
List.fold_left
(fun tyl ty ->
if List.exists (eq_type ty) tyl
then newty2 ~level:generic_level (Ttuple [None, ty]) :: tyl
else ty :: tyl)
[] tyl
in List.rev params
let prepare_type_constructor_arguments args =
List.iter prepare_type (tys_of_constr_args args)
let zap_qtvs_if_boring qtvs =
if List.exists (fun (_v, l) -> Option.is_some l) qtvs
then qtvs
else []
let tyl =
let fvs = Ctype.free_non_row_variables_of_list tyl in
let fvs = List.rev fvs in
let tfvs = List.map Transient_expr.repr fvs in
let vars_jkinds = tree_of_qtvs tfvs in
zap_qtvs_if_boring vars_jkinds
let param_jkind ty =
match get_desc ty with
| Tvar { jkind; _ } | Tunivar { jkind; _ } ->
out_jkind_option_of_jkind ~ignore_null:false !printing_env jkind
| _ -> None
let tree_of_label l =
let mut =
match l.ld_mutable with
| Mutable { mode; atomic } ->
let atomic =
match atomic with
| Atomic -> Atomic
| Nonatomic -> Nonatomic
in
let mut =
let open Value.Comonadic in
match equate mode legacy with
| Ok () -> Om_mutable (None, atomic)
| Error _ -> Om_mutable (Some "<non-legacy>", atomic)
in
mut
| Immutable -> Om_immutable
in
let ld_modalities = tree_of_modalities l.ld_mutable l.ld_modalities in
(Ident.name l.ld_id, mut, tree_of_typexp Type l.ld_type, ld_modalities)
let tree_of_constructor_arguments = function
| Cstr_tuple l -> List.map tree_of_typ_gf l
| Cstr_record l -> [ Otyp_record (List.map tree_of_label l), [] ]
let tree_of_constructor_args_and_ret_type args ret_type =
match ret_type with
| None -> (tree_of_constructor_arguments args, None)
| Some res ->
let out_ret = tree_of_typexp Type res in
let out_args = tree_of_constructor_arguments args in
let qtvs = extract_qtvs (res :: tys_of_constr_args args) in
(out_args, Some (qtvs, out_ret))
let tree_of_single_constructor cd =
let name = Ident.name cd.cd_id in
let args, ret = tree_of_constructor_args_and_ret_type cd.cd_args cd.cd_res in
{
ocstr_name = name;
ocstr_args = args;
ocstr_return_type = ret;
}
let tree_of_constructor_in_decl cd =
match cd.cd_res with
| None -> tree_of_single_constructor cd
| Some _ -> Names.with_local_names (fun () -> tree_of_single_constructor cd)
let prepare_decl id decl =
let params = filter_params decl.type_params in
begin match decl.type_manifest with
| Some ty ->
let vars = free_variables ty in
List.iter
(fun ty ->
match get_desc ty with
| Tvar { name = Some "_"; jkind }
when List.exists (eq_type ty) vars ->
set_type_desc ty (Tvar {name = None; jkind})
| _ -> ())
params
| None -> ()
end;
List.iter add_alias params;
List.iter prepare_type params;
List.iter (add_printed_alias ~non_gen:false) params;
let ty_manifest =
match decl.type_manifest with
| None -> None
| Some ty ->
let ty =
match get_desc ty with
Tvariant row ->
begin match row_name row with
Some (Pident id', _) when Ident.same id id' ->
newgenty (Tvariant (set_row_name row None))
| _ -> ty
end
| _ -> ty
in
prepare_type ty;
Some ty
in
begin match decl.type_kind with
| Type_abstract _ -> ()
| Type_variant (cstrs, _rep,_umc) ->
List.iter
(fun c ->
prepare_type_constructor_arguments c.cd_args;
Option.iter prepare_type c.cd_res)
cstrs
| Type_record(l, _rep,_umc) ->
List.iter (fun l -> prepare_type l.ld_type) l
| Type_record_unboxed_product(l, _rep,_umc) ->
List.iter (fun l -> prepare_type l.ld_type) l
| Type_open -> ()
end;
ty_manifest, params
let tree_of_type_decl ?(print_non_value_inferred_jkind = false) id decl =
let ty_manifest, params = prepare_decl id decl in
let type_param ot_variance ot_jkind =
function
| Otyp_var (ot_non_gen, ot_name) ->
{ot_non_gen; ot_name; ot_variance; ot_jkind}
| _ -> {ot_non_gen=false; ot_name="?"; ot_variance; ot_jkind}
in
let type_defined decl =
let abstr =
match decl.type_kind with
Type_abstract _ ->
decl.type_manifest = None || decl.type_private = Private
| Type_record _ ->
decl.type_private = Private
| Type_record_unboxed_product _ ->
decl.type_private = Private
| Type_variant (tll, _rep,_umc) ->
decl.type_private = Private ||
List.exists (fun cd -> cd.cd_res <> None) tll
| Type_open ->
decl.type_manifest = None
in
let vari =
List.map2
(fun ty v ->
let is_var = is_Tvar ty in
if abstr || not is_var then
let inj =
type_kind_is_abstract decl && Variance.mem Inj v &&
match decl.type_manifest with
| None -> true
| Some ty ->
decl.type_private = Private &&
Btype.is_constr_row ~allow_ident:true (Btype.row_of_type ty)
and (co, cn) = Variance.get_upper v in
(if not cn then Covariant else
if not co then Contravariant else NoVariance),
(if inj then Injective else NoInjectivity)
else (NoVariance, NoInjectivity))
decl.type_params decl.type_variance
in
let mk_param ty variance =
let jkind = param_jkind ty in
type_param variance jkind (tree_of_typexp Type ty)
in
(Ident.name id,
List.map2 mk_param params vari)
in
let tree_of_manifest ty1 =
match ty_manifest with
| None -> ty1
| Some ty -> Otyp_manifest (tree_of_typexp Type ty, ty1)
in
let (name, args) = type_defined decl in
let constraints = tree_of_constraints params in
let ty, priv, unboxed, or_null_attribute, unsafe_mode_crossing =
match decl.type_kind with
| Type_abstract _ ->
begin match ty_manifest with
| None -> (Otyp_abstract, Public, false, None, false)
| Some ty ->
tree_of_typexp Type ty, decl.type_private, false, None, false
end
| Type_variant (cstrs, rep, umc) ->
let unboxed =
match rep with
| Variant_unboxed -> true
| Variant_boxed _ | Variant_extensible | Variant_with_null -> false
in
let or_null_attribute =
if Builtin_attributes.has_or_null decl.type_attributes then
Some "or_null"
else if Builtin_attributes.has_or_null_reexport decl.type_attributes
then Some "or_null_reexport"
else None
in
tree_of_manifest (Otyp_sum (List.map tree_of_constructor_in_decl cstrs)),
decl.type_private,
unboxed,
or_null_attribute,
(Option.is_some umc)
| Type_record(lbls, rep, umc) ->
tree_of_manifest (Otyp_record (List.map tree_of_label lbls)),
decl.type_private,
(match rep with Record_unboxed -> true | _ -> false),
None,
(Option.is_some umc)
| Type_record_unboxed_product(lbls, Record_unboxed_product, umc) ->
tree_of_manifest
(Otyp_record_unboxed_product (List.map tree_of_label lbls)),
decl.type_private,
false,
None,
(Option.is_some umc)
| Type_open ->
tree_of_manifest Otyp_open,
decl.type_private,
false,
None,
false
in
let is_value =
Jkind.is_value_for_printing ~ignore_null:false !printing_env decl.type_jkind
in
let otype_jkind =
match ty, is_value, unsafe_mode_crossing, print_non_value_inferred_jkind with
| (Otyp_abstract, false, _, _) | (_, _, true, _)
| (_, false, _, true) ->
Some (out_jkind_of_desc !printing_env (Jkind.get decl.type_jkind))
| _ -> None
in
let attrs =
if unsafe_mode_crossing
then [{ oattr_name = "unsafe_allow_any_mode_crossing" }]
else []
in
{ otype_name = name;
otype_params = args;
otype_type = ty;
otype_private = priv;
otype_jkind;
otype_unboxed = unboxed;
otype_or_null_attribute = or_null_attribute;
otype_cstrs = constraints;
otype_attributes = attrs }
let add_type_decl_to_preparation id decl =
ignore @@ prepare_decl id decl
let tree_of_prepared_type_decl id decl =
tree_of_type_decl id decl
let tree_of_type_decl ?print_non_value_inferred_jkind id decl =
reset_except_context();
tree_of_type_decl ?print_non_value_inferred_jkind id decl
let add_constructor_to_preparation c =
prepare_type_constructor_arguments c.cd_args;
Option.iter prepare_type c.cd_res
let prepared_constructor ppf c =
!Oprint.out_constr ppf (tree_of_single_constructor c)
let constructor ppf c =
reset_except_context ();
add_constructor_to_preparation c;
prepared_constructor ppf c
let label ppf l =
reset_except_context ();
prepare_type l.ld_type;
!Oprint.out_label ppf (tree_of_label l)
let tree_of_type_declaration ?print_non_value_inferred_jkind id decl rs =
Osig_type (tree_of_type_decl ?print_non_value_inferred_jkind id decl, tree_of_rec rs)
let tree_of_prepared_type_declaration id decl rs =
Osig_type (tree_of_prepared_type_decl id decl, tree_of_rec rs)
let type_declaration ~print_non_value_inferred_jkind id ppf decl =
!Oprint.out_sig_item ppf (tree_of_type_declaration ~print_non_value_inferred_jkind id decl Trec_first)
let add_type_declaration_to_preparation id decl =
add_type_decl_to_preparation id decl
let prepared_type_declaration id ppf decl =
!Oprint.out_sig_item ppf
(tree_of_prepared_type_declaration id decl Trec_first)
let constructor_arguments ppf a =
let tys = tree_of_constructor_arguments a in
!Oprint.out_constr_args ppf tys
let add_extension_constructor_to_preparation ext =
let ty_params = filter_params ext.ext_type_params in
List.iter add_alias ty_params;
List.iter prepare_type ty_params;
prepare_type_constructor_arguments ext.ext_args;
Option.iter prepare_type ext.ext_ret_type
let prepared_tree_of_extension_constructor
id ext es
=
let type_path = best_type_path_simple ext.ext_type_path in
let ty_name = Path.name type_path in
let ty_params = filter_params ext.ext_type_params in
let type_param =
function
| Otyp_var (_, id) -> id
| _ -> "?"
in
let param_scope f =
match ext.ext_ret_type with
| None ->
f ()
| Some _ ->
Names.with_local_names f
in
let ty_params =
param_scope
(fun () ->
List.iter (add_printed_alias ~non_gen:false) ty_params;
List.map (fun ty -> type_param (tree_of_typexp Type ty)) ty_params
)
in
let name = Ident.name id in
let args, ret =
tree_of_constructor_args_and_ret_type
ext.ext_args
ext.ext_ret_type
in
let ext =
{ oext_name = name;
oext_type_name = ty_name;
oext_type_params = ty_params;
oext_args = args;
oext_ret_type = ret;
oext_private = ext.ext_private }
in
let es =
match es with
Text_first -> Oext_first
| Text_next -> Oext_next
| Text_exception -> Oext_exception
in
Osig_typext (ext, es)
let tree_of_extension_constructor id ext es =
reset_except_context ();
add_extension_constructor_to_preparation ext;
prepared_tree_of_extension_constructor id ext es
let extension_constructor id ppf ext =
!Oprint.out_sig_item ppf (tree_of_extension_constructor id ext Text_first)
let prepared_extension_constructor id ppf ext =
!Oprint.out_sig_item ppf
(prepared_tree_of_extension_constructor id ext Text_first)
let extension_only_constructor id ppf ext =
reset_except_context ();
prepare_type_constructor_arguments ext.ext_args;
Option.iter prepare_type ext.ext_ret_type;
let name = Ident.name id in
let args, ret =
tree_of_constructor_args_and_ret_type
ext.ext_args
ext.ext_ret_type
in
Fmt.fprintf ppf "@[<hv>%a@]"
!Oprint.out_constr {
ocstr_name = name;
ocstr_args = args;
ocstr_return_type = ret;
}
let tree_of_value_description id decl =
let id = Ident.name id in
let ty = tree_of_type_scheme decl.val_type in
wrap_mutation (fun () ->
let moda =
if Mode.Modality.is_undefined decl.val_modalities then
Mode.Modality.Const.id
else
Ctype.zap_modalities_to_floor_if_modes_enabled_at Alpha
decl.val_modalities
in
let qsvs, qtvs =
Jkind_types.Sort.print_with_genvars (Lpoly.get_exn decl.val_lpoly)
(fun names -> names, extract_qtvs [decl.val_type])
in
let apparent_arity =
let rec count n typ =
match get_desc typ with
| Tarrow (_,_,typ,_) -> count (n+1) typ
| _ -> n
in
count 0 decl.val_type
in
let attrs =
match Zero_alloc.get decl.val_zero_alloc with
| Default_zero_alloc | Ignore_assert_all -> []
| Check { strict; opt; arity; custom_error_msg; loc = _; } ->
[{ oattr_name =
String.concat ""
["zero_alloc";
if strict then " strict" else "";
if opt then " opt" else "";
if arity = apparent_arity then "" else
Printf.sprintf " arity %d" arity;
match custom_error_msg with
| None -> ""
| Some msg -> Printf.sprintf " custom_error_message %S" msg
] }]
| Assume { strict; never_returns_normally; arity; _ } ->
[{ oattr_name =
String.concat ""
["zero_alloc assume";
if strict then " strict" else "";
if never_returns_normally then " never_returns_normally" else "";
if arity = apparent_arity then "" else
Printf.sprintf " arity %d" arity;
]
}]
in
let vd =
{ oval_name = id;
oval_type = Otyp_newlayout(qsvs, Otyp_poly(qtvs, ty));
oval_modalities = tree_of_modalities Immutable moda;
oval_prims = [];
oval_attributes = attrs
}
in
let vd =
match decl.val_kind with
| Val_prim p -> Primitive.print p vd
| _ -> vd
in
Osig_value vd)
let value_description id ppf decl =
!Oprint.out_sig_item ppf (tree_of_value_description id decl)
let method_type priv ty =
match priv, get_desc ty with
| Mpublic, Tpoly(ty, tyl) -> (ty, tyl)
| _ , _ -> (ty, [])
let prepare_method _lab (priv, _virt, ty) =
let ty, _ = method_type priv ty in
prepare_type ty
let tree_of_method mode (lab, priv, virt, ty) =
let (ty, tyl) = method_type priv ty in
let tty = tree_of_typexp mode ty in
let tyl = List.map Transient_expr.repr tyl in
let qtvs = tree_of_qtvs tyl in
let qtvs = zap_qtvs_if_boring qtvs in
Names.remove_names tyl;
let priv = priv <> Mpublic in
let virt = virt = Virtual in
Ocsg_method (lab, priv, virt, Otyp_poly(qtvs, tty))
let rec prepare_class_type params = function
| Cty_constr (_p, tyl, cty) ->
let row = Btype.self_type_row cty in
if List.memq (proxy row) !visited_objects
|| not (List.for_all is_Tvar params)
|| deep_occur_list row tyl
then prepare_class_type params cty
else List.iter prepare_type tyl
| Cty_signature sign ->
let px = proxy sign.csig_self_row in
if List.memq px !visited_objects then add_alias_proxy px
else visited_objects := px :: !visited_objects;
Vars.iter (fun _ (_, _, ty) -> prepare_type ty) sign.csig_vars;
Meths.iter prepare_method sign.csig_meths
| Cty_arrow (_, ty, cty) ->
prepare_type ty;
prepare_class_type params cty
let rec tree_of_class_type mode params =
function
| Cty_constr (p, tyl, cty) ->
let row = Btype.self_type_row cty in
if List.memq (proxy row) !visited_objects
|| not (List.for_all is_Tvar params)
then
tree_of_class_type mode params cty
else begin
let nso, p = best_class_type_path p in
let tyl = apply_subst_opt nso tyl in
let namespace = Namespace.best_class_namespace p in
Octy_constr (tree_of_path namespace p, tree_of_typlist Type_scheme tyl)
end
| Cty_signature sign ->
let px = proxy sign.csig_self_row in
let self_ty =
if is_aliased_proxy px then
Some
(Otyp_var (false, Names.name_of_type Names.new_name px))
else None
in
let csil = [] in
let csil =
List.fold_left
(fun csil (ty1, ty2) -> Ocsg_constraint (ty1, ty2) :: csil)
csil (tree_of_constraints params)
in
let all_vars =
Vars.fold (fun l (m, v, t) all -> (l, m, v, t) :: all) sign.csig_vars []
in
let all_vars = List.rev all_vars in
let csil =
List.fold_left
(fun csil (l, m, v, t) ->
Ocsg_value (l, m = Asttypes.Mutable, v = Virtual, tree_of_typexp mode t)
:: csil)
csil all_vars
in
let all_meths =
Meths.fold
(fun l (p, v, t) all -> (l, p, v, t) :: all)
sign.csig_meths []
in
let all_meths = List.rev all_meths in
let csil =
List.fold_left
(fun csil meth -> tree_of_method mode meth :: csil)
csil all_meths
in
Octy_signature (self_ty, List.rev csil)
| Cty_arrow (l, ty, cty) ->
let lab =
if !print_labels || is_omittable l then outcome_label l
else Nolabel
in
let tr =
if is_optional l then
match get_desc (Ctype.expand_head !printing_env ty) with
| Tconstr(path, [ty], _) when Path.same path Predef.path_option ->
tree_of_typexp mode ty
| _ -> Otyp_stuff "<hidden>"
else tree_of_typexp mode ty in
Octy_arrow (lab, tr, tree_of_class_type mode params cty)
let class_type ppf cty =
reset ();
prepare_class_type [] cty;
!Oprint.out_class_type ppf (tree_of_class_type Type [] cty)
let tree_of_class_param param variance =
let ot_variance =
if is_Tvar param then Asttypes.(NoVariance, NoInjectivity) else variance in
let ot_jkind = param_jkind param in
match tree_of_typexp Type_scheme param with
Otyp_var (ot_non_gen, ot_name) -> {ot_non_gen; ot_name; ot_variance; ot_jkind}
| _ -> {ot_non_gen=false; ot_name="?"; ot_variance; ot_jkind}
let class_variance =
let open Variance in let open Asttypes in
List.map (fun v ->
(if not (mem May_pos v) then Contravariant else
if not (mem May_neg v) then Covariant else NoVariance),
NoInjectivity)
let tree_of_class_declaration id cl rs =
let params = filter_params cl.cty_params in
reset_except_context ();
List.iter add_alias params;
prepare_class_type params cl.cty_type;
let px = proxy (Btype.self_type_row cl.cty_type) in
List.iter prepare_type params;
List.iter (add_printed_alias ~non_gen:false) params;
if is_aliased_proxy px then add_printed_alias_proxy ~non_gen:false px;
let vir_flag = cl.cty_new = None in
Osig_class
(vir_flag, Ident.name id,
List.map2 tree_of_class_param params (class_variance cl.cty_variance),
tree_of_class_type Type_scheme params cl.cty_type,
tree_of_rec rs)
let class_declaration id ppf cl =
!Oprint.out_sig_item ppf (tree_of_class_declaration id cl Trec_first)
let tree_of_cltype_declaration id cl rs =
let params = cl.clty_params in
reset_except_context ();
List.iter add_alias params;
prepare_class_type params cl.clty_type;
let px = proxy (Btype.self_type_row cl.clty_type) in
List.iter prepare_type params;
List.iter (add_printed_alias ~non_gen:false) params;
if is_aliased_proxy px then (add_printed_alias_proxy ~non_gen:false) px;
let sign = Btype.signature_of_class_type cl.clty_type in
let has_virtual_vars =
Vars.fold (fun _ (_,vr,_) b -> vr = Virtual || b)
sign.csig_vars false
in
let has_virtual_meths =
Meths.fold (fun _ (_,vr,_) b -> vr = Virtual || b)
sign.csig_meths false
in
Osig_class_type
(has_virtual_vars || has_virtual_meths, Ident.name id,
List.map2 tree_of_class_param params (class_variance cl.clty_variance),
tree_of_class_type Type_scheme params cl.clty_type,
tree_of_rec rs)
let cltype_declaration id ppf cl =
!Oprint.out_sig_item ppf (tree_of_cltype_declaration id cl Trec_first)
let wrap_env fenv ftree arg =
let env = !printing_env in
let env' = Env.update_short_paths (fenv env) in
set_printing_env env';
let tree = ftree arg in
set_printing_env env;
tree
let dummy =
{
type_params = [];
type_arity = 0;
type_kind = Type_abstract Definition;
type_jkind = Jkind.Builtin.any ~why:Dummy_jkind;
type_ikind = Types.ikinds_todo "print dummy";
type_private = Public;
type_manifest = None;
type_variance = [];
type_separability = [];
type_is_newtype = false;
type_expansion_scope = Btype.lowest_level;
type_loc = Location.none;
type_attributes = [];
type_unboxed_default = false;
type_uid = Uid.internal_not_actually_unique;
type_unboxed_version = None;
}
(** we hide items being defined from short-path to avoid shortening
[type t = Path.To.t] into [type t = t].
*)
let ident_sigitem = function
| Types.Sig_type(ident,_,_,_) -> {hide=true;ident}
| Types.Sig_jkind (ident,_,_)
| Types.Sig_class(ident,_,_,_)
| Types.Sig_class_type (ident,_,_,_)
| Types.Sig_module(ident,_, _,_,_)
| Types.Sig_value (ident,_,_)
| Types.Sig_modtype (ident,_,_)
| Types.Sig_typext (ident,_,_,_) -> {hide=false; ident }
let hide ids env =
let hide_id id env =
if id.hide && not (Ident.is_global_or_predef id.ident) then
Env.add_type ~check:false (Ident.rename_no_exn id.ident) dummy env
else env
in
List.fold_right hide_id ids env
let with_hidden_items ids f =
let with_hidden_in_printing_env ids f =
wrap_env (hide ids) (Naming_context.with_hidden ids) f
in
if not !Clflags.real_paths then
with_hidden_in_printing_env ids f
else
Naming_context.with_hidden ids f
let add_sigitem env x =
Env.add_signature (Signature_group.flatten x) env
let expand_module_type =
ref ((fun _env _mty -> assert false) :
Env.t -> module_type -> module_type)
(** How to abbreviate signatures *)
module Abbrev = struct
type t =
{
mutable depth : int
; mutable width : int
}
(** Standard abbreviation heuristic *)
let abbrev () =
{ depth = 4
; width = 16
}
(** Don't print any signature items *)
let ellipsis () =
{ depth = 0
; width = 0
}
(** Should we print anything in this signature *)
let exhausted = function
| Some {depth; width} -> depth <= 0 || width <= 0
| None -> false
(** Run [f] at one deeper unfolding level *)
let deeper t f =
match t with
| Some t ->
let saved = t.depth in
t.depth <- t.depth - 1;
let x = f () in
t.depth <- saved;
x
| None -> f ()
(** Reduce the remaining width by the number of items in [sg] and return the number of
items to print in [sg] and a flag that inidicates whether [sg] is being trimmed. *)
let items t sg =
match t with
| Some t ->
let n = List.length sg in
let k = min t.width n in
t.width <- t.width - n;
Some k, (k < n)
| None ->
None, false
end
let tree_of_jkind_declaration id decl =
let ojkind =
{ ojkind_name = Ident.name id
; ojkind_jkind =
Option.map
(fun jkind ->
jkind |> Jkind.Desc.of_const |> out_jkind_of_desc !printing_env)
decl.jkind_manifest
}
in
Osig_jkind ojkind
let rec tree_of_modtype ?abbrev = function
| Mty_ident p ->
let p = best_module_type_path p in
Omty_ident (tree_of_path (Some Module_type) p)
| Mty_signature sg ->
Omty_signature (tree_of_signature ?abbrev sg)
| Mty_functor(param, ty_res, m_res) ->
wrap_mutation (fun () ->
let param, env =
tree_of_functor_parameter ?abbrev param
in
let res = wrap_env env (tree_of_modtype ?abbrev) ty_res in
let mres = m_res |> Mode.Alloc.zap_to_legacy |> tree_of_modes in
Omty_functor (param, res, mres))
| Mty_alias p ->
let p = best_module_path p in
Omty_alias (tree_of_path (Some Module) p)
| Mty_for_hole -> Omty_hole
| Mty_strengthen _ as mty ->
begin match !expand_module_type !printing_env mty with
| Mty_strengthen (mty,p,a) ->
let unaliasable =
not (Aliasability.is_aliasable a)
&& not (Env.is_functor_arg p !printing_env)
in
Omty_strengthen
(tree_of_modtype ?abbrev mty, tree_of_path (Some Module) p, unaliasable)
| mty -> tree_of_modtype ?abbrev mty
end
and tree_of_functor_parameter ?abbrev = function
| Unit ->
None, fun k -> k
| Named (param, ty_arg, m_arg) ->
let name, env =
match param with
| None -> None, fun env -> env
| Some id ->
Some (Ident.name id),
fun k -> Env.add_module ~arg:true id Mp_present ty_arg k
in
let marg = m_arg |> Mode.Alloc.zap_to_legacy |> tree_of_modes in
Some (name, tree_of_modtype ?abbrev ty_arg, marg), env
and tree_of_signature ?abbrev = function
| [] -> []
| _ when Abbrev.exhausted abbrev -> [Osig_ellipsis]
| sg ->
Abbrev.deeper abbrev (fun () ->
wrap_env (fun env -> env)(fun sg ->
let max_items, trimmed = Abbrev.items abbrev sg in
let tree_groups = tree_of_signature_rec ?abbrev ?max_items !printing_env sg in
let items = List.concat_map (fun (_env,l) -> List.map snd l) tree_groups in
if trimmed then items @ [Osig_ellipsis] else items
) sg
)
and tree_of_signature_rec ?abbrev ?max_items env' sg =
let structured = List.of_seq (Signature_group.seq sg) in
let collect_trees_of_rec_group max_items group =
match max_items with
| Some n when n <= 0 -> (max_items, (!printing_env, []))
| Some _ | None ->
let env = !printing_env in
let env', group_trees =
Naming_context.with_ctx
(fun () -> trees_of_recursive_sigitem_group ?abbrev env group)
in
set_printing_env env';
let max_items, group_trees = match max_items with
| None -> None, group_trees
| Some n ->
let rec take n acc xs =
match n, xs with
| 0, _ | _, [] -> n, List.rev acc
| n, x :: xs -> take (n-1) (x :: acc) xs
in
let n, group_trees = take n [] group_trees in
Some n, group_trees
in
max_items, (env, group_trees)
in
set_printing_env env';
snd (List.fold_left_map collect_trees_of_rec_group max_items structured)
and trees_of_recursive_sigitem_group ?abbrev env
(syntactic_group: Signature_group.rec_group) =
let display (x:Signature_group.sig_item) = x.src, tree_of_sigitem ?abbrev x.src in
let env = Env.add_signature syntactic_group.pre_ghosts env in
match syntactic_group.group with
| Not_rec x -> add_sigitem env x, [display x]
| Rec_group items ->
let ids = List.map (fun x -> ident_sigitem x.Signature_group.src) items in
List.fold_left add_sigitem env items,
with_hidden_items ids (fun () -> List.map display items)
and tree_of_sigitem ?abbrev = function
| Sig_value(id, decl, _) ->
tree_of_value_description id decl
| Sig_type(id, decl, rs, _) ->
tree_of_type_declaration id decl rs
| Sig_typext(id, ext, es, _) ->
tree_of_extension_constructor id ext es
| Sig_module(id, _, md, rs, _) ->
let abbrev =
if List.exists (function
| Parsetree.{attr_name = {txt="..."}; attr_payload = PStr []} -> true
| _ -> false)
md.md_attributes
then Some (Abbrev.ellipsis ())
else abbrev
in
tree_of_module ?abbrev id md rs
| Sig_modtype(id, decl, _) ->
tree_of_modtype_declaration ?abbrev id decl
| Sig_class(id, decl, rs, _) ->
tree_of_class_declaration id decl rs
| Sig_class_type(id, decl, rs, _) ->
tree_of_cltype_declaration id decl rs
| Sig_jkind(id, decl, _) ->
tree_of_jkind_declaration id decl
and tree_of_modtype_declaration ?abbrev id decl =
let mty =
match decl.mtd_type with
| None -> Omty_abstract
| Some mty -> tree_of_modtype ?abbrev mty
in
Osig_modtype (Ident.name id, mty)
and tree_of_module ?abbrev id md rs = wrap_mutation (fun () ->
let moda =
if Mode.Modality.is_undefined md.md_modalities then
Mode.Modality.Const.id
else
Ctype.zap_modalities_to_floor_if_at_least Alpha md.md_modalities
in
Osig_module (Ident.name id, tree_of_modtype ?abbrev md.md_type,
tree_of_modalities Immutable moda,
tree_of_rec rs)
)
let rec functor_parameters ~sep custom_printer = function
| [] -> ignore
| [id,param] ->
Fmt.dprintf "%t%t"
(custom_printer param)
(functor_param ~sep ~custom_printer id [])
| (id,param) :: q ->
Fmt.dprintf "%t%a%t"
(custom_printer param)
sep ()
(functor_param ~sep ~custom_printer id q)
and functor_param ~sep ~custom_printer id q =
match id with
| None -> functor_parameters ~sep custom_printer q
| Some id ->
Naming_context.with_arg id
(fun () -> functor_parameters ~sep custom_printer q)
let modtype ppf mty = !Oprint.out_module_type ppf (tree_of_modtype mty)
let modtype_declaration id ppf decl =
!Oprint.out_sig_item ppf (tree_of_modtype_declaration id decl)
let print_items showval env x =
Names.refresh_weak();
reset_naming_context ();
Conflicts.reset ();
let extend_val env (sigitem,outcome) = outcome, showval env sigitem in
let post_process (env,l) = List.map (extend_val env) l in
List.concat_map post_process @@ tree_of_signature_rec env x
let print_signature ppf tree =
fprintf ppf "@[<v>%a@]" !Oprint.out_signature tree
let signature ppf sg =
fprintf ppf "%a" print_signature (tree_of_signature sg)
let printed_signature sourcefile ppf sg =
Conflicts.reset ();
reset_naming_context ();
let t = tree_of_signature sg in
if Warnings.(is_active @@ Erroneous_printed_signature "")
&& Conflicts.exists ()
then begin
let conflicts = Format_doc.asprintf "%t" Conflicts.print_explanations in
Location.prerr_warning (Location.in_file sourcefile)
(Warnings.Erroneous_printed_signature conflicts);
Warnings.check_fatal ()
end;
compat print_signature ppf t
type 'variety trace_format =
| Unification : Errortrace.unification trace_format
| Equality : Errortrace.comparison trace_format
| Moregen : Errortrace.comparison trace_format
let incompatibility_phrase (type variety) : variety trace_format -> string =
function
| Unification -> "is not compatible with type"
| Equality -> "is not equal to type"
| Moregen -> "is not compatible with type"
let same_path t t' =
eq_type t t' ||
match get_desc t, get_desc t' with
| Tconstr(p,tl,_), Tconstr(p',tl',_) -> begin
match best_type_path p, best_type_path p' with
| Nth n, Nth n' when n = n' -> true
| Path(nso, p), Path(nso', p') when Path.same p p' ->
let tl = apply_subst_opt nso tl in
let tl' = apply_subst_opt nso' tl' in
List.length tl = List.length tl' &&
List.for_all2 eq_type tl tl'
| _ -> false
end
| _ ->
false
type 'a diff = Same of 'a | Diff of 'a * 'a
let trees_of_type_expansion'
~var_jkinds mode Errortrace.{ty = t; expanded = t'} =
let tree_of_typexp' ty =
let out = tree_of_typexp mode ty in
if var_jkinds then
match get_desc ty with
| Tvar { jkind; _ } | Tunivar { jkind; _ } ->
let okind = out_jkind_of_desc !printing_env (Jkind.get jkind) in
Otyp_jkind_annot (out, okind)
| _ ->
out
else
out
in
reset_loop_marks ();
mark_loops t;
if same_path t t'
then begin add_delayed (proxy t); Same (tree_of_typexp' t) end
else begin
mark_loops t';
let t' = if proxy t == proxy t' then unalias t' else t' in
print_reduced_evals := false;
let first = tree_of_typexp' t in
print_reduced_evals := true;
let second = tree_of_typexp' t' in
if first = second then Same first
else Diff(first,second)
end
let trees_of_type_expansion =
trees_of_type_expansion' ~var_jkinds:false
let pp_type ppf t =
Style.as_inline_code !Oprint.out_type ppf t
let quoted_ident ppf t =
Style.as_inline_code !Oprint.out_ident ppf t
let type_expansion ppf = function
| Same t -> pp_type ppf t
| Diff(t,t') ->
fprintf ppf "@[<2>%a@ =@ %a@]"
pp_type t
pp_type t'
let trees_of_trace mode =
List.map (Errortrace.map_diff (trees_of_type_expansion mode))
let trees_of_type_path_expansion (tp,tp') =
if Path.same tp tp' then Same(tree_of_path (Some Type) tp) else
Diff(tree_of_path (Some Type) tp, tree_of_path (Some Type) tp')
let type_path_expansion ppf = function
| Same p -> quoted_ident ppf p
| Diff(p,p') ->
fprintf ppf "@[<2>%a@ =@ %a@]"
quoted_ident p
quoted_ident p'
let rec trace fst txt ppf = function
| {Errortrace.got; expected} :: rem ->
if not fst then fprintf ppf "@,";
fprintf ppf "@[Type@;<1 2>%a@ %s@;<1 2>%a@]%a"
type_expansion got txt type_expansion expected
(trace false txt) rem
| _ -> ()
type printing_status =
| Discard
| Keep
| Optional_refinement
(** An [Optional_refinement] printing status is attributed to trace
elements that are focusing on a new subpart of a structural type.
Since the whole type should have been printed earlier in the trace,
we only print those elements if they are the last printed element
of a trace, and there is no explicit explanation for the
type error.
*)
let diff_printing_status Errortrace.{ got = {ty = t1; expanded = t1'};
expected = {ty = t2; expanded = t2'} } =
if is_constr_row ~allow_ident:true t1'
|| is_constr_row ~allow_ident:true t2'
then Discard
else if same_path t1 t1' && same_path t2 t2' then Optional_refinement
else Keep
let printing_status = function
| Errortrace.Diff d -> diff_printing_status d
| Errortrace.Escape {kind = Constraint} -> Keep
| _ -> Keep
(** Flatten the trace and remove elements that are always discarded
during printing *)
let prepare_any_trace printing_status tr =
let clean_trace x l = match printing_status x with
| Keep -> x :: l
| Optional_refinement when l = [] -> [x]
| Optional_refinement | Discard -> l
in
match tr with
| [] -> []
| elt :: rem -> elt :: List.fold_right clean_trace rem []
let prepare_trace f tr =
prepare_any_trace printing_status (Errortrace.map f tr)
(** Keep elements that are [Diff _ ] and split the the last element if it is
optionally elidable, require a prepared trace *)
let rec filter_trace = function
| [] -> [], None
| [Errortrace.Diff d as elt]
when printing_status elt = Optional_refinement -> [], Some d
| Errortrace.Diff d :: rem ->
let filtered, last = filter_trace rem in
d :: filtered, last
| _ :: rem -> filter_trace rem
let type_path_list ppf l =
Fmt.pp_print_list ~pp_sep:(fun ppf () -> Fmt.pp_print_break ppf 2 0)
type_path_expansion ppf l
let hide_variant_name t =
match get_desc t with
| Tvariant row ->
let Row {fields; more; name; fixed; closed} = row_repr row in
if name = None then t else
newty2 ~level:(get_level t)
(Tvariant
(create_row ~fields ~fixed ~closed ~name:None
~more:(newvar2 (get_level more)
(Jkind.Builtin.value ~why:Row_variable))))
| _ -> t
let prepare_expansion Errortrace.{ty; expanded} =
let expanded = hide_variant_name expanded in
reserve_names ty;
if not (same_path ty expanded) then reserve_names expanded;
Errortrace.{ty; expanded}
let may_prepare_expansion compact (Errortrace.{ty; expanded} as ty_exp) =
match get_desc expanded with
Tvariant _ | Tobject _ when compact ->
reserve_names ty; Errortrace.{ty; expanded = ty}
| _ -> prepare_expansion ty_exp
let print_path p =
Fmt.dprintf "%a" !Oprint.out_ident (tree_of_path (Some Type) p)
let print_tag ppf s = Style.inline_code ppf ("`" ^ s)
let print_tags ppf tags =
Fmt.(pp_print_list ~pp_sep:comma) print_tag ppf tags
let is_unit_arg env ty =
let ty, vars = tpoly_get_poly ty in
if vars <> [] then false
else begin
let env =
if Ctype.contains_toplevel_splice (Env.stage env :> int) ty
then Env.enter_future env
else env
in
match get_desc (Ctype.expand_head env ty) with
| Tconstr (p, _, _) -> Path.same p Predef.path_unit
| _ -> false
end
let unifiable env ty1 ty2 =
let snap = Btype.snapshot () in
let res =
try Ctype.unify env ty1 ty2; true
with Unify _ -> false
in
Btype.backtrack snap;
res
let explanation_diff env t3 t4 =
match get_desc t3, get_desc t4 with
| Tarrow (_, ty1, ty2, _), _
when is_unit_arg env ty1 && unifiable env ty2 t4 ->
Some (doc_printf
"@,@[@{<hint>Hint@}: Did you forget to provide %a as argument?@]"
Style.inline_code "()"
)
| _, Tarrow (_, ty1, ty2, _)
when is_unit_arg env ty1 && unifiable env t3 ty2 ->
Some (doc_printf
"@,@[@{<hint>Hint@}: Did you forget to wrap the expression using \
%a?@]"
Style.inline_code "fun () ->"
)
| _ ->
None
let explain_fixed_row_case = function
| Errortrace.Cannot_be_closed -> doc_printf "it cannot be closed"
| Errortrace.Cannot_add_tags tags ->
doc_printf "it may not allow the tag(s) %a"
print_tags tags
let explain_fixed_row pos expl = match expl with
| Fixed_private ->
doc_printf "The %a variant type is private" Errortrace.print_pos pos
| Univar x ->
reserve_names x;
doc_printf "The %a variant type is bound to the universal type variable %a"
Errortrace.print_pos pos
(Style.as_inline_code type_expr_with_reserved_names) x
| Reified p ->
doc_printf "The %a variant type is bound to %a"
Errortrace.print_pos pos
(Style.as_inline_code
(fun ppf p ->
Internal_names.add p;
print_path p ppf))
p
| Rigid -> Format_doc.Doc.empty
| Fixed_existential -> Format_doc.Doc.empty
let explain_variant (type variety) : variety Errortrace.variant -> _ = function
| Errortrace.Incompatible_types_for s ->
Some(doc_printf "@,Types for tag %a are incompatible"
print_tag s
)
| Errortrace.No_intersection ->
Some(doc_printf "@,These two variant types have no intersection")
| Errortrace.No_tags(pos,fields) -> Some(
doc_printf
"@,@[The %a variant type does not allow tag(s)@ @[<hov>%a@]@]"
Errortrace.print_pos pos
print_tags (List.map fst fields)
)
| Errortrace.Fixed_row (pos,
k,
(Univar _ | Reified _ | Fixed_private as e)) ->
Some (
doc_printf "@,@[%a,@ %a@]" pp_doc (explain_fixed_row pos e)
pp_doc (explain_fixed_row_case k)
)
| Errortrace.Fixed_row (_,_, (Rigid | Fixed_existential)) ->
None
| Errortrace.Presence_not_guaranteed_for (pos, s) -> Some(
doc_printf
"@,@[The tag %a is guaranteed to be present in the %a variant type,\
@ but not in the %a@]"
print_tag s
Errortrace.print_pos (Errortrace.swap_position pos)
Errortrace.print_pos pos
)
| Errortrace.Openness pos ->
Some(doc_printf "@,The %a variant type is open and the %a is not"
Errortrace.print_pos pos
Errortrace.print_pos (Errortrace.swap_position pos))
let explain_escape pre = function
| Errortrace.Univ u ->
reserve_names u;
Some(
doc_printf "%a@,The universal variable %a would escape its scope"
pp_doc pre
(Style.as_inline_code type_expr_with_reserved_names) u
)
| Errortrace.Constructor p -> Some(
doc_printf
"%a@,@[The type constructor@;<1 2>%a@ would escape its scope@]"
pp_doc pre (Style.as_inline_code path) p
)
| Errortrace.Module_type p -> Some(
doc_printf
"%a@,@[The module type@;<1 2>%a@ would escape its scope@]"
pp_doc pre (Style.as_inline_code path) p
)
| Errortrace.Equation Errortrace.{ty = _; expanded = t} ->
reserve_names t;
Some(
doc_printf "%a@ @[<hov>This instance of %a is ambiguous:@ %s@]"
pp_doc pre
(Style.as_inline_code type_expr_with_reserved_names) t
"it would escape the scope of its equation"
)
| Errortrace.Self ->
Some (doc_printf "%a@,Self type cannot escape its class" pp_doc pre)
| Errortrace.Constraint ->
None
let explain_object (type variety) : variety Errortrace.obj -> _ = function
| Errortrace.Missing_field (pos,f) -> Some(
doc_printf "@,@[The %a object type has no method %a@]"
Errortrace.print_pos pos Style.inline_code f
)
| Errortrace.Abstract_row pos -> Some(
doc_printf
"@,@[The %a object type has an abstract row, it cannot be closed@]"
Errortrace.print_pos pos
)
| Errortrace.Self_cannot_be_closed ->
Some (doc_printf
"@,Self type cannot be unified with a closed object type"
)
let explain_incompatible_fields name (diff: Types.type_expr Errortrace.diff) =
reserve_names diff.got;
reserve_names diff.expected;
doc_printf "@,@[The method %a has type@ %a,@ \
but the expected method type was@ %a@]"
Style.inline_code name
(Style.as_inline_code type_expr_with_reserved_names) diff.got
(Style.as_inline_code type_expr_with_reserved_names) diff.expected
let explanation (type variety) intro prev env
: (Errortrace.expanded_type, variety) Errortrace.elt -> _ = function
| Errortrace.Diff {got; expected} ->
explanation_diff env got.expanded expected.expanded
| Errortrace.Escape {kind; context} ->
let pre =
match context, kind, prev with
| Some ctx, _, _ ->
reserve_names ctx;
doc_printf "@[%a@;<1 2>%a@]" pp_doc intro
(Style.as_inline_code type_expr_with_reserved_names) ctx
| None, Univ _, Some(Errortrace.Incompatible_fields {name; diff}) ->
explain_incompatible_fields name diff
| _ -> Format_doc.Doc.empty
in
explain_escape pre kind
| Errortrace.Incompatible_fields { name; diff} ->
Some(explain_incompatible_fields name diff)
| Errortrace.Variant v ->
explain_variant v
| Errortrace.Obj o ->
explain_object o
| Errortrace.Rec_occur(x,y) ->
reserve_names x;
reserve_names y;
begin match get_desc x with
| Tvar _ | Tunivar _ ->
mark_loops x;
mark_loops y;
Some(
doc_printf "@,@[<hov>The type variable %a occurs inside@ %a@]"
(Style.as_inline_code prepared_type_expr) x
(Style.as_inline_code prepared_type_expr) y
)
| _ ->
Some Format_doc.Doc.empty
end
| Errortrace.Bad_jkind (t,e) ->
Some (doc_printf "@ @[<hov>%a@]"
(Jkind.Violation.report_with_offender
~offender:(fun ppf -> type_expr ppf t)
env) e)
| Errortrace.Bad_jkind_sort (t,e) ->
Some (doc_printf "@ @[<hov>%a@]"
(Jkind.Violation.report_with_offender_sort
~offender:(fun ppf -> type_expr ppf t)
env) e)
| Errortrace.Unequal_var_jkinds (t1,k1,t2,k2) ->
let fmt_history t k ppf =
Jkind.(format_history env ~intro:(
dprintf "The layout of %a is %a" prepared_type_expr t
(format env) k) ppf k)
in
Some (doc_printf "@ because the layouts of their variables are different.\
@ @[<v>%t@;%t@]"
(fmt_history t1 k1) (fmt_history t2 k2))
| Errortrace.Unequal_tof_kind_jkinds (k1, k2) ->
let fmt_history which k ppf =
Jkind.(format_history env ~intro:(
dprintf "The kind of %s is %a" which (format env) k) ppf k)
in
Some (doc_printf "@ because their kinds are different.\
@ @[<v>%t@;%t@]"
(fmt_history "the first" k1) (fmt_history "the second" k2))
let mismatch intro env trace =
Errortrace.explain trace (fun ~prev h -> explanation intro prev env h)
let warn_on_missing_def env ppf t =
match get_desc t with
| Tconstr (p,_,_) ->
begin match Env.find_type p env with
| exception Not_found ->
fprintf ppf
"@,@[<hov>Type %a is abstract because@ no corresponding\
@ cmi file@ was found@ in path.@]" (Style.as_inline_code path) p
| { type_manifest = Some _; _ } -> ()
| { type_manifest = None; _ } as decl ->
match type_origin decl with
| Rec_check_regularity ->
fprintf ppf
"@,@[<hov>Type %a was considered abstract@ when checking\
@ constraints@ in this@ recursive type definition.@]"
(Style.as_inline_code path) p
| Definition | Existential _ -> ()
end
| _ -> ()
let prepare_expansion_head empty_tr = function
| Errortrace.Diff d ->
Some (Errortrace.map_diff (may_prepare_expansion empty_tr) d)
| _ -> None
let head_error_printer ~var_jkinds mode txt_got txt_but = function
| None -> Format_doc.Doc.empty
| Some d ->
let d =
Errortrace.map_diff (trees_of_type_expansion' ~var_jkinds mode) d
in
doc_printf "%a@;<1 2>%a@ %a@;<1 2>%a"
pp_doc txt_got type_expansion d.Errortrace.got
pp_doc txt_but type_expansion d.Errortrace.expected
let warn_on_missing_defs env ppf = function
| None -> ()
| Some Errortrace.{got = {ty=te1; expanded=_};
expected = {ty=te2; expanded=_} } ->
warn_on_missing_def env ppf te1;
warn_on_missing_def env ppf te2
let rec last = function
| [] -> None
| (x :: []) -> Some x
| (_ :: xs) -> last xs
let error trace_format mode subst env tr txt1 ppf txt2 ty_expect_explanation =
reset ();
Names.add_subst (List.map (fun (ty1,ty2) -> ty2,ty1) subst);
let tr =
prepare_trace
(fun ty_exp ->
Errortrace.{ty_exp with expanded = hide_variant_name ty_exp.expanded})
tr
in
let jkind_error = match last tr with
| Some (Bad_jkind _ | Bad_jkind_sort _ | Unequal_var_jkinds _
| Unequal_tof_kind_jkinds _) ->
true
| Some (Diff _ | Escape _ | Variant _ | Obj _ | Incompatible_fields _
| Rec_occur _)
| None ->
false
in
match tr with
| [] -> assert false
| (elt :: tr) as full_trace ->
try
print_labels := not !Clflags.classic;
let tr, last = filter_trace tr in
let head = prepare_expansion_head (tr=[] && last=None) elt in
let tr = List.map (Errortrace.map_diff prepare_expansion) tr in
let last = Option.map (Errortrace.map_diff prepare_expansion) last in
let head_error =
head_error_printer ~var_jkinds:jkind_error mode txt1 txt2 head
in
let tr = trees_of_trace mode tr in
let last =
Option.map (Errortrace.map_diff (trees_of_type_expansion mode)) last in
let mis = mismatch txt1 env full_trace in
let tr = match mis, last with
| None, Some elt -> tr @ [elt]
| Some _, _ | _, None -> tr
in
fprintf ppf
"@[<v>\
@[%a%a@]%a%a\
@]"
pp_doc head_error
pp_doc ty_expect_explanation
(trace false (incompatibility_phrase trace_format)) tr
(pp_print_option pp_doc) mis;
if env <> Env.empty && not jkind_error
then warn_on_missing_defs env ppf head;
Internal_names.print_explanations env ppf;
Conflicts.print_explanations ppf;
print_labels := true
with exn ->
print_labels := true;
print_reduced_evals := true;
raise exn
let report_error trace_format ppf mode env tr
?(subst = [])
?(type_expected_explanation = Fmt.Doc.empty)
txt1 txt2 =
wrap_printing_env ~error:true env (fun () ->
error trace_format mode subst env tr txt1 ppf txt2
type_expected_explanation)
let report_unification_error ?type_expected_explanation
ppf env ({trace} : Errortrace.unification_error) =
report_error ?type_expected_explanation Unification ppf Type env
?subst:None trace
let report_equality_error
ppf mode env ({subst; trace} : Errortrace.equality_error) =
report_error Equality ppf mode env
~subst ?type_expected_explanation:None trace
let report_moregen_error
ppf mode env ({trace} : Errortrace.moregen_error) =
report_error Moregen ppf mode env
?subst:None ?type_expected_explanation:None trace
let report_comparison_error ppf mode env = function
| Errortrace.Equality_error error -> report_equality_error ppf mode env error
| Errortrace.Moregen_error error -> report_moregen_error ppf mode env error
module Subtype = struct
let printing_status = function
| Errortrace.Subtype.Diff d -> diff_printing_status d
let prepare_unification_trace = prepare_trace
let prepare_trace f tr =
prepare_any_trace printing_status (Errortrace.Subtype.map f tr)
let trace filter_trace get_diff fst keep_last txt ppf tr =
print_labels := not !Clflags.classic;
try match tr with
| elt :: tr' ->
let diffed_elt = get_diff elt in
let tr, last = filter_trace tr' in
let tr = match keep_last, last with
| true, Some last -> tr @ [last]
| _ -> tr
in
let tr =
trees_of_trace Type
@@ List.map (Errortrace.map_diff prepare_expansion) tr in
let tr =
match fst, diffed_elt with
| true, Some elt -> elt :: tr
| _, _ -> tr
in
trace fst txt ppf tr;
print_labels := true
| _ -> ()
with exn ->
print_labels := true;
raise exn
let rec filter_subtype_trace = function
| [] -> [], None
| [Errortrace.Subtype.Diff d as elt]
when printing_status elt = Optional_refinement ->
[], Some d
| Errortrace.Subtype.Diff d :: rem ->
let ftr, last = filter_subtype_trace rem in
d :: ftr, last
let unification_get_diff = function
| Errortrace.Diff diff ->
Some (Errortrace.map_diff (trees_of_type_expansion Type) diff)
| _ -> None
let subtype_get_diff = function
| Errortrace.Subtype.Diff diff ->
Some (Errortrace.map_diff (trees_of_type_expansion Type) diff)
let report_error
ppf
env
(Errortrace.Subtype.{trace = tr_sub; unification_trace = tr_unif})
txt1 =
wrap_printing_env ~error:true env (fun () ->
reset ();
let tr_sub = prepare_trace prepare_expansion tr_sub in
let tr_unif = prepare_unification_trace prepare_expansion tr_unif in
let keep_first = match tr_unif with
| [Obj _ | Variant _ | Escape _ ] | [] -> true
| _ -> false in
fprintf ppf "@[<v>%a"
(trace filter_subtype_trace subtype_get_diff true keep_first txt1)
tr_sub;
if tr_unif = [] then fprintf ppf "@]" else
let mis = mismatch (doc_printf "Within this type") env tr_unif in
fprintf ppf "%a%a%t@]"
(trace filter_trace unification_get_diff false
(mis = None) "is not compatible with type") tr_unif
(pp_print_option pp_doc) mis
Conflicts.print_explanations
)
end
let report_ambiguous_type_error ppf env tp0 tpl txt1 txt2 txt3 =
wrap_printing_env ~error:true env (fun () ->
reset ();
let tp0 = trees_of_type_path_expansion tp0 in
match tpl with
[] -> assert false
| [tp] ->
fprintf ppf
"@[%a@;<1 2>%a@ \
%a@;<1 2>%a\
@]"
pp_doc txt1 type_path_expansion (trees_of_type_path_expansion tp)
pp_doc txt3 type_path_expansion tp0
| _ ->
fprintf ppf
"@[%a@;<1 2>@[<hv>%a@]\
@ %a@;<1 2>%a\
@]"
pp_doc txt2 type_path_list (List.map trees_of_type_path_expansion tpl)
pp_doc txt3 type_path_expansion tp0)
let abbreviate ~abbrev f =
f ?abbrev:(if abbrev then Some (Abbrev.abbrev ()) else None)
let tree_of_path = tree_of_path None
let tree_of_module ident ?(ellipsis = false) =
tree_of_module ident ?abbrev:(if ellipsis then Some (Abbrev.ellipsis ()) else None)
let tree_of_signature sg = tree_of_signature sg
let tree_of_modtype ?(abbrev = false) ty =
abbreviate ~abbrev tree_of_modtype ty
let tree_of_modtype_declaration ?(abbrev = false) id md =
abbreviate ~abbrev tree_of_modtype_declaration id md
let type_expansion mode ppf ty_exp =
type_expansion ppf (trees_of_type_expansion mode ty_exp)
let tree_of_type_declaration ident td rs =
with_hidden_items [{hide=true; ident}]
(fun () -> tree_of_type_declaration ident td rs)
(** Compatibility module for Format printers *)
module Compat0 = struct
let longident = Fmt.compat longident
let path = Fmt.compat path
let type_expr = Fmt.compat type_expr
let shared_type_scheme = Fmt.compat shared_type_scheme
let signature = Fmt.compat signature
let class_type = Fmt.compat class_type
let modtype = Fmt.compat modtype
let string_of_label (lbl : Asttypes.arg_label) =
let lbl : Types.arg_label = match lbl with
| Nolabel -> Nolabel
| Labelled s -> Labelled s
| Optional s -> Optional s
in
string_of_label lbl
end
let shorten_type_path env p =
wrap_printing_env env
(fun () -> best_type_path_simple p)
let shorten_module_type_path env p =
wrap_printing_env env
(fun () -> best_module_type_path p)
let shorten_module_path env p =
wrap_printing_env env
(fun () -> best_module_path p)
let shorten_class_type_path env p =
wrap_printing_env env
(fun () -> best_class_type_path_simple p)
let ppf qtvs =
let qtvs =
List.filter_map
(function
| _, None -> None
| name, Some annot -> Some (name, annot))
qtvs
in
match qtvs with
| [] -> ()
| _ :: _ as qtvs ->
let annotated_qtv ppf (name, jkind) =
fprintf ppf "@['%s : %a@]" name !Oprint.out_jkind jkind
in
fprintf ppf " @[(* @[%a@] *)@]"
(Format_doc.pp_print_list annotated_qtv
~pp_sep:(fun ppf () -> fprintf ppf ", "))
qtvs
let type_scheme_for_merlin ~print_non_value_jkind_on_type_variables ppf ty =
type_scheme ppf ty;
if print_non_value_jkind_on_type_variables
then (
let qtvs = extract_qtvs [ ty ] in
print_annotated_qtvs_as_comment ppf qtvs)
let type_declaration_for_merlin = type_declaration
module Compat = struct
include Compat0
let type_declaration_for_merlin ~print_non_value_inferred_jkind id =
Fmt.compat (type_declaration_for_merlin ~print_non_value_inferred_jkind id)
let type_scheme_for_merlin ~print_non_value_jkind_on_type_variables =
Fmt.compat (type_scheme_for_merlin ~print_non_value_jkind_on_type_variables)
end
let type_declaration x y z : unit =
type_declaration x y z ~print_non_value_inferred_jkind:false
let () =
Env.shorten_module_path := shorten_module_path