package odoc

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file paths.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
(*
 * Copyright (c) 2014 Leo White <lpw25@cl.cam.ac.uk>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

open Names

module Reversed = struct
  type elt =
    | Root of UnitName.t
    | Module of ModuleName.t
    | ModuleType of ModuleTypeName.t
    | Argument of int * ArgumentName.t

  type t = elt list

  let rec remove_prefix prefix ~of_ =
    match prefix, of_ with
    | x1 :: xs1, x2 :: xs2 when x1 = x2 ->
      remove_prefix xs1 ~of_:xs2
    | _, _ -> of_
end

module Identifier = struct

  type t = Paths_types.Identifier.any

  let name : [< t] -> string = function
    | `Root(_, name) -> UnitName.to_string name
    | `Page(_, name) -> PageName.to_string name
    | `Module(_, name) -> ModuleName.to_string name
    | `Argument(_, _, name) -> ArgumentName.to_string name
    | `ModuleType(_, name) -> ModuleTypeName.to_string name
    | `Type(_, name) -> TypeName.to_string name
    | `CoreType name -> TypeName.to_string name
    | `Constructor(_, name) -> ConstructorName.to_string name
    | `Field(_, name) -> FieldName.to_string name
    | `Extension(_, name) -> ExtensionName.to_string name
    | `Exception(_, name) -> ExceptionName.to_string name
    | `CoreException name -> ExceptionName.to_string name
    | `Value(_, name) -> ValueName.to_string name
    | `Class(_, name) -> ClassName.to_string name
    | `ClassType(_, name) -> ClassTypeName.to_string name
    | `Method(_, name) -> MethodName.to_string name
    | `InstanceVariable(_, name) -> InstanceVariableName.to_string name
    | `Label(_, name) -> LabelName.to_string name

  let rec equal : t -> t -> bool =
    let open Paths_types.Identifier in
    fun p1 p2 ->
      match p1, p2 with
      | `Root (r1, n1), `Root (r2, n2) ->
        UnitName.equal n1 n2 && Root.equal r1 r2
      | `Module (s1,n1), `Module (s2,n2) ->
        ModuleName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any) 
      | `Argument (s1,i1,n1), `Argument (s2,i2,n2) ->
        i1=i2 && ArgumentName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any)
      | `ModuleType (s1,n1), `ModuleType (s2,n2) ->
        ModuleTypeName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any)
      | `Type (s1, n1), `Type (s2, n2) ->
        TypeName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any)
      | `CoreType n1, `CoreType n2 ->
        TypeName.equal n1 n2
      | `Class (s1, n1), `Class (s2, n2) ->
        ClassName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any)
      | `ClassType (s1, n1), `ClassType (s2, n2) ->
        ClassTypeName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any)
      | `Page (r1, n1), `Page (r2, n2) ->
        PageName.equal n1 n2 && Root.equal r1 r2
      | `Constructor(t1, n1), `Constructor(t2, n2) ->
        ConstructorName.equal n1 n2 && equal (t1 : type_ :> any) (t2 : type_ :> any)
      | `Field(s1, n1), `Field(s2, n2) ->
        FieldName.equal n1 n2 && equal (s1 : parent :> any) (s2 : parent :> any)
      | `Extension(s1, n1), `Extension(s2, n2) ->
        ExtensionName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any)
      | `Exception(s1, n1), `Exception(s2, n2) ->
        ExceptionName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any)
      | `CoreException n1, `CoreException n2 ->
        ExceptionName.equal n1 n2
      | `Value(s1, n1), `Value(s2, n2) ->
        ValueName.equal n1 n2 && equal (s1 : signature :> any) (s2 : signature :> any)
      | `Method(s1, n1), `Method(s2, n2) ->
        MethodName.equal n1 n2 && equal (s1 : class_signature :> any) (s2 : class_signature :> any)
      | `InstanceVariable(s1, n1), `InstanceVariable(s2, n2) ->
        InstanceVariableName.equal n1 n2 && equal (s1 : class_signature :> any) (s2 : class_signature :> any)
      | `Label(s1, n1), `Label(s2, n2) ->
        LabelName.equal n1 n2 && equal (s1 : label_parent :> any) (s2 : label_parent :> any)
      | _, _ -> false

  let rec hash (id : Paths_types.Identifier.any) =
    let open Paths_types.Identifier in
    match id with
    | `Root(r, s) ->
      Hashtbl.hash (1, Root.hash r, s)
    | `Page(r, s) ->
      Hashtbl.hash (2, Root.hash r, s)
    | `Module(id, s) ->
      Hashtbl.hash (3, hash (id : signature :> any), s)
    | `Argument(id, n, s) ->
      Hashtbl.hash (4, hash (id : signature :> any), n, s)
    | `ModuleType(id, s) ->
      Hashtbl.hash (5, hash (id : signature :> any), s)
    | `Type(id, s) ->
      Hashtbl.hash (6, hash (id : signature :> any), s)
    | `CoreType s ->
      Hashtbl.hash (7, s)
    | `Constructor(id, s) ->
      Hashtbl.hash (8, hash (id : type_ :> any), s)
    | `Field(id, s) ->
      Hashtbl.hash (9, hash (id : parent :> any), s)
    | `Extension(id, s) ->
      Hashtbl.hash (10, hash (id : signature :> any), s)
    | `Exception(id, s) ->
      Hashtbl.hash (11, hash (id : signature :> any), s)
    | `CoreException s ->
      Hashtbl.hash (12, s)
    | `Value(id, s) ->
      Hashtbl.hash (13, hash (id : signature :> any), s)
    | `Class(id, s) ->
      Hashtbl.hash (14, hash (id : signature :> any), s)
    | `ClassType(id, s) ->
      Hashtbl.hash (15, hash (id : signature :> any), s)
    | `Method(id, s) ->
      Hashtbl.hash (16, hash (id : class_signature :> any), s)
    | `InstanceVariable(id, s) ->
      Hashtbl.hash (17, hash (id : class_signature :> any), s)
    | `Label(id, s) ->
      Hashtbl.hash (18, hash (id : label_parent :> any ), s)

  module Signature =
  struct
    type t = Paths_types.Identifier.signature 

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)

    let rec root = function
      | `Root(r, _) -> r
      | `Module(id, _)
      | `Argument(id, _, _)
      | `ModuleType(id, _) -> root id
  end

  module ClassSignature =
  struct
    type t = Paths_types.Identifier.class_signature 

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)

    let root = function
      | `Class (s, _) -> Signature.root s
      | `ClassType (s, _) -> Signature.root s
  end

  module DataType =
  struct
    type t = Paths_types.Identifier.datatype

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Parent =
  struct
    type t = Paths_types.Identifier.parent

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module LabelParent =
  struct
    type t = Paths_types.Identifier.label_parent

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)

    let root : t -> Root.t = function
      | `Root(r, _) -> r
      | `Module(id, _)
      | `Argument(id, _, _)
      | `ModuleType(id, _) -> Signature.root id
      | `Type(id,_) -> Signature.root id
      | `CoreType _ -> assert false
      | `Class (s, _) -> Signature.root s
      | `ClassType (s, _) -> Signature.root s
      | `Page (r, _) -> r
  end

  module Module =
  struct
    type t = Paths_types.Identifier.module_

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)

    let root = function
      | `Root(r, _) -> r
      | `Module(id, _)
      | `Argument(id, _, _) -> Signature.root id

  end

  module ModuleType =
  struct
    type t = Paths_types.Identifier.module_type

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)

    let root : t -> Root.t = function
      | `ModuleType(id, _) -> Signature.root id

  end

  module Type =
  struct
    type t = Paths_types.Identifier.type_

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Constructor =
  struct
    type t = Paths_types.Identifier.constructor

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Field =
  struct
    type t = Paths_types.Identifier.field

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Extension =
  struct
    type t = Paths_types.Identifier.extension

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Exception =
  struct
    type t = Paths_types.Identifier.exception_

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Value =
  struct
    type t = Paths_types.Identifier.value

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Class =
  struct
    type t = Paths_types.Identifier.class_

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module ClassType =
  struct
    type t = Paths_types.Identifier.class_type

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Method =
  struct
    type t = Paths_types.Identifier.method_

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module InstanceVariable =
  struct
    type t = Paths_types.Identifier.instance_variable

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Label =
  struct
    type t = Paths_types.Identifier.label

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Page =
  struct
    type t = Paths_types.Identifier.page

    let equal : t -> t -> bool =
      fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

    let hash : t -> int =
      fun t -> hash (t : t :> Paths_types.Identifier.any)
  end

  module Path =
  struct
    module Module =
    struct
      type t = Paths_types.Identifier.path_module

      let equal : t -> t -> bool =
        fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

      let hash : t -> int =
        fun t -> hash (t : t :> Paths_types.Identifier.any)
    end

    module ModuleType =
    struct
      type t = Paths_types.Identifier.path_module_type

      let equal : t -> t -> bool =
        fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

      let hash : t -> int =
        fun t -> hash (t : t :> Paths_types.Identifier.any)
    end

    module Type =
    struct
      type t = Paths_types.Identifier.path_type

      let equal : t -> t -> bool =
        fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

      let hash : t -> int =
        fun t -> hash (t : t :> Paths_types.Identifier.any)
    end

    module ClassType =
    struct
      type t = Paths_types.Identifier.path_class_type

      let equal : t -> t -> bool =
        fun t1 t2 -> equal (t1 : t :> Paths_types.Identifier.any) (t2 : t :> Paths_types.Identifier.any)

      let hash : t -> int =
        fun t -> hash (t : t :> Paths_types.Identifier.any)
    end

    type t = Paths_types.Identifier.path_any
  end

  let to_reversed i =
    let rec loop acc : Signature.t -> Reversed.t = function
      | `Root (_, s) -> Reversed.Root s :: acc
      | `Module (i, s) -> loop (Reversed.Module s :: acc) i
      | `ModuleType (i, s) -> loop (Reversed.ModuleType s :: acc) i
      | `Argument (i, d, s) -> loop (Reversed.Argument (d, s) :: acc) i
    in
    loop [] i

  let signature_of_module m = (m : Module.t :> Signature.t)

  (* It would be much nicer not to have to have these functions *)

  let page_of_t : t -> Page.t = function
    | #Page.t as result -> result
    | _ -> assert false

  let signature_of_t : t -> Signature.t = function
      | #Signature.t as result -> result
      | _ -> assert false

  let class_signature_of_t : t -> ClassSignature.t = function
      | #ClassSignature.t as result -> result
      | _ -> assert false

  let datatype_of_t : t -> DataType.t = function
      | #DataType.t as result -> result
      | _ -> assert false

  let module_of_t : t -> Module.t = function
      | #Module.t as result -> result
      | _ -> assert false

  let module_type_of_t : t -> ModuleType.t = function
      | #ModuleType.t as result -> result
      | _ -> assert false

  let type_of_t : t -> Type.t = function
      | #Type.t as result -> result
      | _ -> assert false

  let constructor_of_t : t -> Constructor.t = function
      | #Constructor.t as result -> result
      | _ -> assert false

  let field_of_t : t -> Field.t = function
      | #Field.t as result -> result
      | _ -> assert false

  let extension_of_t : t -> Extension.t = function
      | #Extension.t as result -> result
      | _ -> assert false

  let exception_of_t : t -> Exception.t = function
      | #Exception.t as result -> result
      | _ -> assert false

  let value_of_t : t -> Value.t = function
      | #Value.t as result -> result
      | _ -> assert false

  let class_of_t : t -> Class.t = function
      | #Class.t as result -> result
      | _ -> assert false

  let class_type_of_t : t -> ClassType.t = function
      | #ClassType.t as result -> result
      | _ -> assert false

  let method_of_t : t -> Method.t = function
      | #Method.t as result -> result
      | _ -> assert false

  let instance_variable_of_t : t -> InstanceVariable.t = function
      | #InstanceVariable.t as result -> result
      | _ -> assert false

  let label_of_t : t -> Label.t = function
      | #Label.t as result -> result
      | _ -> assert false

  let parent_of_t : t -> Parent.t = function
      | #Parent.t as result -> result
      | _ -> assert false

end



module Path = struct

  type t = Paths_types.Path.any

  let rec equal_resolved_path : Paths_types.Resolved_path.any -> Paths_types.Resolved_path.any -> bool =
    let open Paths_types.Resolved_path in
    fun p1 p2 ->
      match p1, p2 with
      | `Identifier id1, `Identifier id2 ->
        Identifier.equal id1 id2
      | `Subst(sub1, p1), `Subst(sub2, p2) ->
        equal_resolved_path (p1 : module_ :> any) (p2 : module_ :> any)
        && equal_resolved_path (sub1 : module_type :> any) (sub2 : module_type :> any)
      | `SubstAlias(sub1, p1), `SubstAlias(sub2, p2) ->
        equal_resolved_path (p1 : module_ :> any) (p2 : module_ :> any)
        && equal_resolved_path (sub1 : module_ :> any) (sub2 : module_ :> any)
      | `Module(p1, s1), `Module(p2, s2) ->
        s1 = s2 && equal_resolved_path (p1 : module_ :> any) (p2 : module_ :> any)
      | `Apply(p1, arg1), `Apply(p2, arg2) ->
        equal_path
          (arg1 : Paths_types.Path.module_ :> Paths_types.Path.any)
          (arg2 : Paths_types.Path.module_ :> Paths_types.Path.any)
        && equal_resolved_path (p1 : module_ :> any) (p2 : module_ :> any)
      | `ModuleType(p1, s1), `ModuleType(p2, s2) ->
        s1 = s2 && equal_resolved_path (p1 : module_ :> any) (p2 : module_ :> any)
      | `Type(p1, s1), `Type(p2, s2) ->
        s1 = s2 && equal_resolved_path (p1 : module_ :> any) (p2 : module_ :> any)
      | `Class(p1, s1), `Class(p2, s2) ->
        s1 = s2 && equal_resolved_path (p1 : module_ :> any) (p2 : module_ :> any)
      | `ClassType(p1, s1), `ClassType(p2, s2) ->
        s1 = s2 && equal_resolved_path (p1 : module_ :> any) (p2 : module_ :> any)
      | _, _ -> false

  and equal_path : Paths_types.Path.any -> Paths_types.Path.any -> bool =
    let open Paths_types.Path in
    fun p1 p2 ->
      match p1, p2 with
      | `Resolved p1, `Resolved p2 ->
        equal_resolved_path p1 p2
      | `Root s1, `Root s2 ->
        s1 = s2
      | `Dot(p1, s1), `Dot(p2, s2) ->
        s1 = s2 && equal_path (p1 : module_ :> any) (p2 : module_ :> any)
      | `Apply(p1, arg1), `Apply(p2, arg2) ->
        equal_path (arg1 : module_ :> any) (arg2 : module_ :> any)
        && equal_path (p1 : module_ :> any) (p2 : module_ :> any)
      | _, _ -> false

  let rec hash_resolved_path : Paths_types.Resolved_path.any -> int =
    fun p ->
    let open Paths_types.Resolved_path in
    match p with
    | `Identifier id ->
      Identifier.hash id
    | `Subst(sub, p) ->
      Hashtbl.hash (19, hash_resolved_path (sub : module_type :> any),
                    hash_resolved_path (p : module_ :> any))
    | `SubstAlias(sub, p) ->
      Hashtbl.hash (20, hash_resolved_path (sub : module_ :> any),
                    hash_resolved_path (p : module_ :> any))
    | `Hidden p -> Hashtbl.hash (21, hash_resolved_path (p : module_ :> any))
    | `Module(p, s) ->
      Hashtbl.hash (22, hash_resolved_path (p : module_ :> any), s)
    | `Canonical(p, canonical) ->
      Hashtbl.hash (23, hash_resolved_path (p : module_ :> any),
                    hash_path (canonical : Paths_types.Path.module_ :> Paths_types.Path.any))
    | `Apply(p, arg) ->
      Hashtbl.hash (24, hash_resolved_path (p : module_ :> any),
                    hash_path (arg : Paths_types.Path.module_ :> Paths_types.Path.any))
    | `ModuleType(p, s) ->
      Hashtbl.hash (25, hash_resolved_path (p : module_ :> any), s)
    | `Type(p, s) ->
      Hashtbl.hash (26, hash_resolved_path (p : module_ :> any), s)
    | `Class(p, s) ->
      Hashtbl.hash (27, hash_resolved_path (p : module_ :> any), s)
    | `ClassType(p, s) ->
      Hashtbl.hash (28, hash_resolved_path (p : module_ :> any), s)

  and hash_path : Paths_types.Path.any -> int =
    fun p ->
    let open Paths_types.Path in
    match p with
    | `Resolved p -> hash_resolved_path p
    | `Root s ->
      Hashtbl.hash (29, s)
    | `Forward s ->
      Hashtbl.hash (30, s)
    | `Dot(p, s) ->
      Hashtbl.hash (31, hash_path (p : module_ :> any), s)
    | `Apply(p, arg) ->
      Hashtbl.hash (32, hash_path (p : module_ :> any), hash_path (arg : module_ :> any))

  let rec is_resolved_hidden : Paths_types.Resolved_path.any -> bool =
    let open Paths_types.Resolved_path in
    function
    | `Identifier _ -> false
    | `Canonical (_, _) -> false
    | `Hidden _ -> true
    | `Subst(p1, p2) -> is_resolved_hidden (p1 : module_type :> any) || is_resolved_hidden (p2 : module_ :> any)
    | `SubstAlias(p1, p2) -> is_resolved_hidden (p1 : module_ :> any) || is_resolved_hidden (p2 : module_ :> any)
    | `Module (p, _) -> is_resolved_hidden (p : module_ :> any)
    | `Apply (p, _) -> is_resolved_hidden (p : module_ :> any)
    | `ModuleType (p, _) -> is_resolved_hidden (p : module_ :> any)
    | `Type (p, _) -> is_resolved_hidden (p : module_ :> any)
    | `Class (p, _) -> is_resolved_hidden (p : module_ :> any)
    | `ClassType (p, _) -> is_resolved_hidden (p : module_ :> any)

  and is_path_hidden : Paths_types.Path.any -> bool =
    let open Paths_types.Path in
    function
    | `Resolved r -> is_resolved_hidden r
    | `Root _ -> false
    | `Forward _ -> false
    | `Dot(p, _) -> is_path_hidden (p : module_ :> any)
    | `Apply(p1, p2) -> is_path_hidden (p1 : module_ :> any) || is_path_hidden (p2 : module_ :> any)

  module Resolved = struct


    type t = Paths_types.Resolved_path.any

    let rec parent_module_type_identifier : Paths_types.Resolved_path.module_type -> Identifier.Signature.t = function
      | `Identifier id -> (id : Identifier.ModuleType.t :> Identifier.Signature.t) 
      | `ModuleType(m, n) -> `ModuleType(parent_module_identifier m, n)

    and parent_module_identifier : Paths_types.Resolved_path.module_ -> Identifier.Signature.t = function
      | `Identifier id -> (id : Identifier.Module.t :> Identifier.Signature.t)
      | `Subst(sub, _) -> parent_module_type_identifier sub
      | `SubstAlias(sub, _) -> parent_module_identifier sub
      | `Hidden p -> parent_module_identifier p
      | `Module(m, n) -> `Module(parent_module_identifier m, n)
      | `Canonical(_, `Resolved p) -> parent_module_identifier p
      | `Canonical(p, _) -> parent_module_identifier p
      | `Apply(m, _) -> parent_module_identifier m

    let equal p1 p2 = equal_resolved_path p1 p2

    let hash p = hash_resolved_path p

    type rebase_result =
      | Stop of Paths_types.Resolved_path.module_
      | Continue of Paths_types.Identifier.path_module * Reversed.t

    let rec rebase_module_path : Reversed.t -> Paths_types.Resolved_path.module_ -> rebase_result =
      fun new_base t ->
      match t with
      | `Identifier id ->
        let rev = Identifier.(to_reversed @@ signature_of_module id) in
        let new_base' = Reversed.remove_prefix rev ~of_:new_base in
        if new_base == new_base' then
          Stop t
        else
          Continue (id, new_base')
      | `Subst (_, p)
      | `SubstAlias (_, p)
      | `Hidden p -> begin
          match rebase_module_path new_base p with
          | Stop p' when p == p' -> Stop t
          | otherwise -> otherwise
        end
      | `Module (m, s) ->
        begin match rebase_module_path new_base m with
          | Stop m' -> if m == m' then Stop t else Stop (`Module (m', s))
          | Continue (id, new_base) ->
            let id = `Module (Identifier.signature_of_module id, s) in
            match new_base with
            | Reversed.Module s' :: rest when s = s' ->
              Continue (id, rest)
            | _ ->
              Stop (`Identifier id)
        end
      | `Canonical (_, `Resolved p) ->
        (* We only care about printing at this point, so let's drop the lhs. *)
        rebase_module_path new_base p
      | `Canonical (rp, p) ->
        begin match rebase_module_path new_base rp with
          | Stop rp' -> Stop (`Canonical (rp', p))
          | _ ->
            (* We might come back at some point with a resolved rhs? So we don't want to
               drop it. *)
            Stop t
        end
      | `Apply _ -> Stop t
    (* TODO: rewrite which side? *)

    let rec equal_identifier :
      Identifier.t -> t -> bool =
      fun id p ->
      match id, p with
      | _, `Identifier id' -> Identifier.equal id id'
      | `Module (id, s1), `Module (p, s2) when s1 = s2 ->
        equal_identifier
          (id : Paths_types.Identifier.signature :> Paths_types.Identifier.any)
          (p : Paths_types.Resolved_path.module_ :> Paths_types.Resolved_path.any)
      | `ModuleType (id, s1), `ModuleType (p, s2) when s1 = s2 ->
        equal_identifier
          (id : Paths_types.Identifier.signature :> Paths_types.Identifier.any)
          (p : Paths_types.Resolved_path.module_ :> Paths_types.Resolved_path.any)
      | _, _ ->
        false

    module Module = struct

      type t = Paths_types.Resolved_path.module_

      let of_ident id = `Identifier id

      let equal m1 m2 = equal_resolved_path (m1 : t :> Paths_types.Resolved_path.any) (m2 : t :> Paths_types.Resolved_path.any)

      let hash m = hash (m : t :> Paths_types.Resolved_path.any)

      let is_hidden m = is_resolved_hidden (m : t :> Paths_types.Resolved_path.any)

        let rec identifier = function
        | `Identifier id -> id
        | `Subst(_, p) -> identifier p
        | `SubstAlias(_, p) -> identifier p
        | `Hidden p -> identifier p
        | `Module(m, n) -> `Module(parent_module_identifier m, n)
        | `Canonical(_, `Resolved p) -> identifier p
        | `Canonical(p, _) -> identifier p
        | `Apply(m, _) -> identifier m

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `Subst _ -> t (* TODO: rewrite which side? *)
        | `SubstAlias _ -> t (* TODO: rewrite which side? *)
        | `Hidden p  -> begin
            match rebase_module_path new_base p with
            | Stop p' ->
              if p == p' then t else p'
            | Continue (id, _) -> `Identifier id
          end
        | `Module (mp, s) ->
          begin match rebase_module_path new_base mp with
            | Continue (id, _) ->
              `Identifier (`Module (Identifier.signature_of_module id, s))
            | Stop mp' -> `Module (mp', s)
          end
        | `Canonical (p, `Resolved rp) ->
          begin match rebase_module_path new_base rp with
            | Continue (id, _) -> `Identifier id
            | Stop rp ->
              (* Easier to reexport a canonical than get the type for rp right... *)
              `Canonical (p, `Resolved rp)
          end
        | `Canonical (rp, p) ->
          begin match rebase_module_path new_base rp with
            | Stop rp' -> `Canonical (rp', p)
            | _ ->
              (* We might come back at some point with a resolved rhs? So we don't want to
                 drop it. *)
              t
          end
        | `Apply (mp, arg) ->
          begin match rebase_module_path new_base mp with
            | Continue (id, _) -> `Apply (`Identifier id, arg)
            | Stop mp' -> `Apply (mp', arg)
          end

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t

      let equal_identifier :
        Identifier.Path.Module.t -> t -> bool =
        fun id t ->
        equal_identifier
          (id : Identifier.Path.Module.t :> Identifier.t)
          (t : t :> Paths_types.Resolved_path.any)
    end

    module ModuleType = struct

      type t = Paths_types.Resolved_path.module_type

      let of_ident id = `Identifier id

      let equal m1 m2 = equal_resolved_path (m1 : t :> Paths_types.Resolved_path.any) (m2 : t :> Paths_types.Resolved_path.any)

      let hash m = hash (m : t :> Paths_types.Resolved_path.any)

      let is_hidden m = is_resolved_hidden (m : t :> Paths_types.Resolved_path.any)

      let identifier = function
        | `Identifier id -> id
        | `ModuleType(m, n) -> `ModuleType(parent_module_identifier m, n)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `ModuleType (mp, s) ->
          begin match rebase_module_path new_base mp with
            | Continue (id, _) ->
              `Identifier (`ModuleType (Identifier.signature_of_module id, s))
            | Stop mp' -> `ModuleType (mp', s)
          end

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t

      let equal_identifier :
        Identifier.Path.ModuleType.t -> t -> bool =
        fun id t ->
        equal_identifier
          (id : Identifier.Path.ModuleType.t :> Identifier.t)
          (t : t :> Paths_types.Resolved_path.any)
    end

    module Type = struct

      type t = Paths_types.Resolved_path.type_

      let of_ident id = `Identifier id

      let equal m1 m2 = equal_resolved_path (m1 : t :> Paths_types.Resolved_path.any) (m2 : t :> Paths_types.Resolved_path.any)

      let hash m = hash (m : t :> Paths_types.Resolved_path.any)

      let is_hidden m = is_resolved_hidden (m : t :> Paths_types.Resolved_path.any)

      let identifier = function
        | `Identifier id -> id
        | `Type(m, n) -> `Type(parent_module_identifier m, n)
        | `Class(m, n) -> `Class(parent_module_identifier m, n)
        | `ClassType(m, n) -> `ClassType(parent_module_identifier m, n)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `Type (mp, s) ->
          begin match rebase_module_path new_base mp with
            | Continue (id, _) ->
              `Identifier (`Type (Identifier.signature_of_module id, s))
            | Stop mp' -> `Type (mp', s)
          end
        | `Class (mp, s) ->
          begin match rebase_module_path new_base mp with
            | Continue (id, _) ->
              `Identifier (`Class (Identifier.signature_of_module id, s))
            | Stop mp' -> `Class (mp', s)
          end
        | `ClassType (mp, s) ->
          begin match rebase_module_path new_base mp with
            | Continue (id, _) ->
              `Identifier (`ClassType (Identifier.signature_of_module id, s))
            | Stop mp' -> `ClassType (mp', s)
          end

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t

      let equal_identifier :
        Identifier.Path.Type.t -> t -> bool =
        fun id t ->
        equal_identifier
          (id : Identifier.Path.Type.t :> Identifier.t)
          (t : t :> Paths_types.Resolved_path.any)

    end

    module ClassType = struct

      type t = Paths_types.Resolved_path.class_type

      let of_ident id = `Identifier id

      let equal m1 m2 = equal_resolved_path (m1 : t :> Paths_types.Resolved_path.any) (m2 : t :> Paths_types.Resolved_path.any)

      let hash m = hash (m : t :> Paths_types.Resolved_path.any)

      let is_hidden m = is_resolved_hidden (m : t :> Paths_types.Resolved_path.any)

      let identifier = function
        | `Identifier id -> id
        | `Class(m, n) -> `Class(parent_module_identifier m, n)
        | `ClassType(m, n) -> `ClassType(parent_module_identifier m, n)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `Class (mp, s) ->
          begin match rebase_module_path new_base mp with
            | Continue (id, _) ->
              `Identifier (`Class (Identifier.signature_of_module id, s))
            | Stop mp' -> `Class (mp', s)
          end
        | `ClassType (mp, s) ->
          begin match rebase_module_path new_base mp with
            | Continue (id, _) ->
              `Identifier (`ClassType (Identifier.signature_of_module id, s))
            | Stop mp' -> `ClassType (mp', s)
          end

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t

      let equal_identifier :
        Identifier.Path.ClassType.t -> t -> bool =
        fun id t ->
        equal_identifier
          (id : Identifier.Path.ClassType.t :> Identifier.t)
          (t : t :> Paths_types.Resolved_path.any)
    end

    let module_of_t : t -> Module.t = function
        | `Identifier (#Identifier.Path.Module.t)
        | #Paths_types.Resolved_path.module_no_id as x -> x
        | _ -> assert false

    let module_type_of_t : t -> ModuleType.t = function
        | `Identifier (#Identifier.Path.ModuleType.t)
        | #Paths_types.Resolved_path.module_type_no_id as x -> x
        | _ -> assert false

    let type_of_t : t -> Type.t = function
        | `Identifier (#Identifier.Path.Type.t)
        | #Paths_types.Resolved_path.type_no_id as x -> x
        | _ -> assert false

    let class_type_of_t : t -> ClassType.t = function
        | `Identifier (#Identifier.Path.ClassType.t)
        | #Paths_types.Resolved_path.class_type_no_id as x -> x
        | _ -> assert false


    let rec identifier : t -> Identifier.t = function
      | `Identifier id -> id
      | `Subst(_, p) -> identifier (p :> t)
      | `SubstAlias(_, p) -> identifier (p :> t)
      | `Hidden p -> identifier (p :> t)
      | `Module(m, n) -> `Module(parent_module_identifier m, n)
      | `Canonical(_, `Resolved p) -> identifier (p :> t)
      | `Canonical(p, _) -> identifier (p :> t)
      | `Apply(m, _) -> identifier (m :> t)
      | `Type(m, n) -> `Type(parent_module_identifier m, n)
      | `ModuleType(m, n) -> `ModuleType(parent_module_identifier m, n)
      | `Class(m, n) -> `Class(parent_module_identifier m, n)
      | `ClassType(m, n) -> `ClassType(parent_module_identifier m, n)

  end

  module Module =
  struct
    type t = Paths_types.Path.module_

    let equal : t -> t -> bool =
      fun p1 p2 ->
      equal_path (p1 : t :> Paths_types.Path.any) (p2 : t :> Paths_types.Path.any)

    let hash : t -> int =
      fun t ->
      hash_path (t : t :> Paths_types.Path.any)

    let is_hidden : t -> bool =
      fun t ->
      is_path_hidden (t : t :> Paths_types.Path.any)
  end

  module ModuleType =
  struct
    type t = Paths_types.Path.module_type

    let equal : t -> t -> bool =
      fun p1 p2 ->
      equal_path (p1 : t :> Paths_types.Path.any) (p2 : t :> Paths_types.Path.any)

    let hash : t -> int =
      fun t ->
      hash_path (t : t :> Paths_types.Path.any)

    let is_hidden : t -> bool =
      fun t ->
      is_path_hidden (t : t :> Paths_types.Path.any)
  end

  module Type =
  struct
    type t = Paths_types.Path.type_

    let equal : t -> t -> bool =
      fun p1 p2 ->
      equal_path (p1 : t :> Paths_types.Path.any) (p2 : t :> Paths_types.Path.any)

    let hash : t -> int =
      fun t ->
      hash_path (t : t :> Paths_types.Path.any)

    let is_hidden : t -> bool =
      fun t ->
      is_path_hidden (t : t :> Paths_types.Path.any)
  end

  module ClassType =
  struct
    type t = Paths_types.Path.class_type

    let equal : t -> t -> bool =
      fun p1 p2 ->
      equal_path (p1 : t :> Paths_types.Path.any) (p2 : t :> Paths_types.Path.any)

    let hash : t -> int =
      fun t ->
      hash_path (t : t :> Paths_types.Path.any)

    let is_hidden : t -> bool =
      fun t ->
      is_path_hidden (t : t :> Paths_types.Path.any)
  end

  let module_of_t : t -> Module.t = function
    | `Resolved (`Identifier (#Paths_types.Identifier.path_module))
    | `Resolved (#Paths_types.Resolved_path.module_no_id)
    | `Root _
    | `Forward _
    | `Dot (_,_)
    | `Apply (_,_) as x -> x
    | _ -> assert false

  let module_type_of_t : t -> ModuleType.t = function
    | `Resolved (`Identifier (#Paths_types.Identifier.path_module_type))
    | `Resolved (#Paths_types.Resolved_path.module_type_no_id)
    | `Dot (_,_) as x -> x
    | _ -> assert false

  let type_of_t : t -> Type.t = function
    | `Resolved (`Identifier (#Paths_types.Identifier.path_type))
    | `Resolved (#Paths_types.Resolved_path.type_no_id)
    | `Dot (_,_) as x -> x
    | _ -> assert false

  let class_type_of_t : t -> ClassType.t = function
    | `Resolved (`Identifier (#Paths_types.Identifier.path_class_type))
    | `Resolved (#Paths_types.Resolved_path.class_type_no_id)
    | `Dot (_,_) as x -> x
    | _ -> assert false

  let module_ : Module.t -> ModuleName.t -> Module.t = fun p name ->
    match p with
    | `Resolved p -> `Resolved (`Module(p, name))
    | p -> `Dot(p, ModuleName.to_string name)

  let apply p arg =
    match p with
    | `Resolved p -> `Resolved (`Apply(p, arg))
    | p -> `Apply(p, arg)

  let module_type p name =
    match p with
    | `Resolved p -> `Resolved (`ModuleType(p, name))
    | p -> `Dot(p, ModuleTypeName.to_string name)

  let is_hidden = is_path_hidden

  let equal = equal_path

  let hash = hash_path
end



module Fragment = struct

  module Resolved = struct

    type t = Paths_types.Resolved_fragment.any

    let t_of_module m = (m : Paths_types.Resolved_fragment.module_ :> t)
    let t_of_signature s = (s : Paths_types.Resolved_fragment.signature :> t)

    let equal p1 p2 =
      let rec loop : t -> t -> bool =
        fun p1 p2 ->
          match p1, p2 with
          | `Root, `Root -> true
          | `Subst(sub1, p1), `Subst(sub2, p2) ->
            Path.Resolved.ModuleType.equal sub1 sub2
            && loop (t_of_module p1) (t_of_module p2)
          | `SubstAlias(sub1, p1), `SubstAlias(sub2, p2) ->
            Path.Resolved.Module.equal sub1 sub2
            && loop (t_of_module p1) (t_of_module p2)
          | `Module(p1, s1), `Module(p2, s2) ->
            s1 = s2 && loop (t_of_signature p1) (t_of_signature p2)
          | `Type(p1, s1), `Type(p2, s2) ->
            s1 = s2 && loop (t_of_signature p1) (t_of_signature p2)
          | `Class(p1, s1), `Class(p2, s2) ->
            s1 = s2 && loop (t_of_signature p1) (t_of_signature p2)
          | `ClassType(p1, s1), `ClassType(p2, s2) ->
            s1 = s2 && loop (t_of_signature p1) (t_of_signature p2)
          | _, _ -> false
      in
      loop p1 p2

    let hash p =
      let rec loop : t -> int =
        fun p ->
          match p with
          | `Root -> Hashtbl.hash 32
          | `Subst(sub, p) ->
            Hashtbl.hash (34, Path.Resolved.ModuleType.hash sub, loop (t_of_module p))
          | `SubstAlias(sub, p) ->
            Hashtbl.hash (35, Path.Resolved.Module.hash sub, loop (t_of_module p))
          | `Module(p, s) ->
            Hashtbl.hash (36, loop (t_of_signature p), s)
          | `Type(p, s) ->
            Hashtbl.hash (37, loop (t_of_signature p), s)
          | `Class(p, s) ->
            Hashtbl.hash (38, loop (t_of_signature p), s)
          | `ClassType(p, s) ->
            Hashtbl.hash (39, loop (t_of_signature p), s)
      in
      loop p

    let sig_of_mod m =
      let open Paths_types.Resolved_fragment in 
      (m : module_ :> signature)

    let rec parent_resolved_path : Path.Resolved.Module.t -> Paths_types.Resolved_fragment.signature -> Path.Resolved.Module.t = fun root -> 
      function
      | `Root -> root
      | `Subst(sub, p) ->
        `Subst(sub, parent_resolved_path root (sig_of_mod p))
      | `SubstAlias(sub, p) ->
        `SubstAlias(sub, parent_resolved_path root (sig_of_mod p))
      | `Module(m, n) ->
        `Module(parent_resolved_path root m, n)

    let rec parent_unresolved_path : Path.Module.t -> Paths_types.Resolved_fragment.signature -> Path.Module.t = fun root -> function
      | `Root -> root
      | `Subst(_, p) -> parent_unresolved_path root (sig_of_mod p)
      | `SubstAlias(_, p) -> parent_unresolved_path root (sig_of_mod p)
      | `Module(m, n) -> `Dot(parent_unresolved_path root m, (ModuleName.to_string n))

    let parent_path root frag =
      match root with
      | `Resolved root -> `Resolved (parent_resolved_path root frag)
      | _ -> parent_unresolved_path root frag

    type base_name =
      | Base
      | Branch of ModuleName.t * Paths_types.Resolved_fragment.signature

    let rec split_parent : Paths_types.Resolved_fragment.signature -> base_name = function
      | `Root -> Base
      | `Subst(_, p) -> split_parent (sig_of_mod p)
      | `SubstAlias(_, p) -> split_parent (sig_of_mod p)
      | `Module(p, name) ->
        match split_parent p with
        | Base -> Branch(name, `Root)
        | Branch(base, m) -> Branch(base, `Module(m, name))


    module Signature =
    struct
      type t = Paths_types.Resolved_fragment.signature

      let equal s1 s2 =
        equal (s1 : t :> Paths_types.Resolved_fragment.any) (s2 : t :> Paths_types.Resolved_fragment.any)

      let hash s =
        hash (s : t :> Paths_types.Resolved_fragment.any)

      let rec resolved_path : Path.Resolved.Module.t -> t -> Path.Resolved.Module.t =
        fun root frag ->
        match frag with
        | `Root -> root
        | `Subst(sub, p) ->
          `Subst(sub, resolved_path root (sig_of_mod p))
        | `SubstAlias(sub, p) ->
          `SubstAlias(sub, resolved_path root (sig_of_mod p))
        | `Module(m, n) ->
          `Module(parent_resolved_path root m, n)

      let rec unresolved_path : Path.Module.t -> t -> Path.Module.t =
        fun root frag ->
        match frag with
        | `Root -> root
        | `Subst(_, p) -> unresolved_path root (sig_of_mod p)
        | `SubstAlias(_, p) -> unresolved_path root (sig_of_mod p)
        | `Module(m, n) -> `Dot(parent_unresolved_path root m, ModuleName.to_string n)

      let path : Path.Module.t -> t -> Path.Module.t =
        fun root frag ->
        match root with
        | `Resolved root -> `Resolved (resolved_path root frag)
        | _ -> unresolved_path root frag

      let rec split : t -> string * t option = function
        | `Root -> "", None
        | `Subst(_,p) -> split (sig_of_mod p)
        | `SubstAlias(_,p) -> split (sig_of_mod p)
        | `Module (m, name) -> begin
            match split_parent m with
            | Base -> (ModuleName.to_string name, None)
            | Branch(base,m) -> ModuleName.to_string base, Some (`Module(m,name))
          end

      let rec identifier : Identifier.Signature.t -> t -> Identifier.Signature.t =
        fun root -> function
          | `Root -> root
          | `Subst(_, p) -> identifier root (sig_of_mod p)
          | `SubstAlias(_, p) -> identifier root (sig_of_mod p)
          | `Module(m, n) -> `Module (identifier root m, n)

    end

    module Module =
    struct
      type t = Paths_types.Resolved_fragment.module_

      let equal s1 s2 =
        equal (s1 : t :> Paths_types.Resolved_fragment.any) (s2 : t :> Paths_types.Resolved_fragment.any)

      let hash s =
        hash (s : t :> Paths_types.Resolved_fragment.any)

      let resolved_path : Path.Resolved.Module.t -> t -> Path.Resolved.Module.t =
        fun root frag ->
        match frag with
        | `Subst(sub, p) ->
          `Subst(sub, Signature.resolved_path root (sig_of_mod p))
        | `SubstAlias(sub, p) ->
          `SubstAlias(sub, Signature.resolved_path root (sig_of_mod p))
        | `Module(m, n) ->
          `Module(parent_resolved_path root m, n)

      let unresolved_path : Path.Module.t -> t -> Path.Module.t =
        fun root frag ->
        match frag with
        | `Subst(_, p) -> Signature.unresolved_path root (sig_of_mod p)
        | `SubstAlias(_, p) -> Signature.unresolved_path root (sig_of_mod p)
        | `Module(m, n) -> `Dot(parent_unresolved_path root m, ModuleName.to_string n)

      let path : Path.Module.t -> t -> Path.Module.t =
        fun root frag ->
        match root with
        | `Resolved root -> `Resolved (resolved_path root frag)
        | _ -> unresolved_path root frag

      let rec split : t -> string * t option = function
        | `Subst(_,p) -> split p
        | `SubstAlias(_,p) -> split p
        | `Module (m, name) -> begin
            match split_parent m with
            | Base -> (ModuleName.to_string name, None)
            | Branch(base,m) -> ModuleName.to_string base, Some (`Module(m,name))
          end

      let rec identifier : Identifier.Signature.t -> t -> Identifier.Path.Module.t =
        fun root -> function
          | `Subst(_, p) -> identifier root p
          | `SubstAlias(_, p) -> identifier root p
          | `Module(m, n) -> `Module (Signature.identifier root m, n)

    end

    module Type =
    struct
      type t = Paths_types.Resolved_fragment.type_

      let equal s1 s2 =
        equal (s1 : t :> Paths_types.Resolved_fragment.any) (s2 : t :> Paths_types.Resolved_fragment.any)

      let hash s =
        hash (s : t :> Paths_types.Resolved_fragment.any)

      let resolved_path : Path.Resolved.Module.t -> t -> Path.Resolved.Type.t = fun root ->
        function
        | `Type(m,n) -> `Type (Signature.resolved_path root m, n)
        | `Class(m,n) -> `Class (Signature.resolved_path root m, n)
        | `ClassType(m,n) -> `ClassType (Signature.resolved_path root m, n)

      let unresolved_path : Path.Module.t -> t -> Path.Type.t = fun root ->
        function
        | `Type(m,n) -> `Dot (Signature.unresolved_path root m, TypeName.to_string n)
        | `Class(m,n) -> `Dot (Signature.unresolved_path root m, ClassName.to_string n)
        | `ClassType(m,n) -> `Dot (Signature.unresolved_path root m, ClassTypeName.to_string n)

      let path :  Path.Module.t -> t -> Path.Type.t = fun root frag ->
        match root with
        | `Resolved root -> `Resolved (resolved_path root frag)
        | _ -> unresolved_path root frag

      let split : t -> string * t option =
        function
        | `Type (m,name) -> begin
            match split_parent m with
            | Base -> TypeName.to_string name, None
            | Branch(base, m) -> ModuleName.to_string base, Some (`Type(m, name))
          end
        | `Class(m, name) -> begin
            match split_parent m with
            | Base -> ClassName.to_string name, None
            | Branch(base, m) -> ModuleName.to_string base, Some (`Class(m, name))
          end
        | `ClassType(m, name) -> begin
            match split_parent m with
            | Base -> ClassTypeName.to_string name, None
            | Branch(base, m) -> ModuleName.to_string base, Some (`ClassType(m, name))
          end

      let identifier : Identifier.Signature.t -> t -> Identifier.Path.Type.t =
        fun root -> function
          | `Type(m, n) -> `Type(Signature.identifier root m, n)
          | `Class(m, n) -> `Class(Signature.identifier root m, n)
          | `ClassType(m, n) -> `ClassType(Signature.identifier root m, n)

    end

    let signature_of_t : t -> Signature.t = function
      | #Signature.t as x -> x
      | _ -> assert false

    let module_of_t : t -> Module.t = function
      | #Module.t as x -> x
      | _ -> assert false

    let type_of_t : t -> Type.t = function
      | #Type.t as x -> x
      | _ -> assert false

    let rec identifier : Identifier.Signature.t -> t -> Identifier.t =
      fun root -> function
        | `Root -> (root :> Identifier.t)
        | `Subst(_, p) -> identifier root (p :> t)
        | `SubstAlias(_, p) -> identifier root (p :> t)
        | `Module(m, n) -> `Module (Signature.identifier root m, n)
        | `Type(m, n) -> `Type(Signature.identifier root m, n)
        | `Class(m, n) -> `Class(Signature.identifier root m, n)
        | `ClassType(m, n) -> `ClassType(Signature.identifier root m, n)


  end

  type t = Paths_types.Fragment.any

  let rec parent_path : Path.Module.t -> Paths_types.Fragment.signature -> Path.Module.t = fun root -> function
    | `Resolved r -> Resolved.parent_path root r
    | `Dot(m, n) -> `Dot(parent_path root m, n)

  type base_name =
    | Base
    | Branch of ModuleName.t * Paths_types.Fragment.signature

  let rec split_parent : Paths_types.Fragment.signature -> base_name =
    function
    | `Resolved r -> begin
        match Resolved.split_parent r with
        | Resolved.Base -> Base
        | Resolved.Branch(base, m) -> Branch(base, `Resolved m)
      end
    | `Dot(m,name) -> begin
        match split_parent m with
        | Base -> Branch(ModuleName.of_string name,`Resolved `Root)
        | Branch(base,m) -> Branch(base, `Dot(m,name))
      end

  let equal p1 p2 =
    let rec loop : t -> t -> bool =
      fun p1 p2 ->
        match p1, p2 with
        | `Resolved p1, `Resolved p2 ->
          Resolved.equal p1 p2
        | `Dot(p1, s1), `Dot(p2, s2) ->
          s1 = s2 && loop (p1 : Paths_types.Fragment.signature :> t) (p2 : Paths_types.Fragment.signature :> t)
        | _, _ -> false
    in
    loop p1 p2

  let hash p =
    let rec loop : t -> int =
      fun p ->
        match p with
        | `Resolved p -> Resolved.hash p
        | `Dot(p, s) ->
          Hashtbl.hash (40, loop (p : Paths_types.Fragment.signature :> t), s)
    in
    loop p

  module Signature = struct
    type t = Paths_types.Fragment.signature

    let equal t1 t2 =
      equal (t1 : t :> Paths_types.Fragment.any) (t2 : t :> Paths_types.Fragment.any)

    let hash t =
      hash (t : t :> Paths_types.Fragment.any)

    let split : t -> string * t option = function
      | `Resolved r ->
        let base, m = Resolved.Signature.split r in
        let m =
          match m with
          | None -> None
          | Some m -> Some (`Resolved m)
        in
        base, m
      | `Dot(m, name) ->
        match split_parent m with
        | Base -> name, None
        | Branch(base, m) -> ModuleName.to_string base, Some(`Dot(m, name))

    let path : Path.Module.t -> t -> Path.Module.t =
      fun root -> function
        | `Resolved r -> Resolved.Signature.path root r
        | `Dot(m, s) -> `Dot(parent_path root m, s)

  end

  module Module = struct
    type t = Paths_types.Fragment.module_

    let equal t1 t2 =
      equal (t1 : t :> Paths_types.Fragment.any) (t2 : t :> Paths_types.Fragment.any)

    let hash t =
      hash (t : t :> Paths_types.Fragment.any)

    let split : t -> string * t option = function
      | `Resolved r ->
        let base, m = Resolved.Module.split r in
        let m =
          match m with
          | None -> None
          | Some m -> Some (`Resolved m)
        in
        base, m
      | `Dot(m, name) ->
        match split_parent m with
        | Base -> name, None
        | Branch(base, m) -> ModuleName.to_string base, Some(`Dot(m, name))

    let path : Path.Module.t -> t -> Path.Module.t =
      fun root -> function
        | `Resolved r -> Resolved.Module.path root r
        | `Dot(m, s) -> `Dot(parent_path root m, s)
  end

  module Type = struct
    type t = Paths_types.Fragment.type_

    let equal t1 t2 =
      equal (t1 : t :> Paths_types.Fragment.any) (t2 : t :> Paths_types.Fragment.any)

    let hash t =
      hash (t : t :> Paths_types.Fragment.any)

    let split : t -> string * t option = function
      | `Resolved r ->
        let base, m = Resolved.Type.split r in
        let m =
          match m with
          | None -> None
          | Some m -> Some (`Resolved m)
        in
        base, m
      | `Dot(m, name) ->
        match split_parent m with
        | Base -> name, None
        | Branch(base, m) -> ModuleName.to_string base, Some(`Dot(m, name))

    let path : Path.Module.t -> t -> Path.Type.t =
      fun root -> function
        | `Resolved r -> Resolved.Type.path root r
        | `Dot(m, s) -> `Dot(parent_path root m, s)

  end

  let signature_of_t : t -> Signature.t = function
    | `Resolved (#Resolved.Signature.t) 
    | `Dot (_,_) as x -> x
    | _ -> assert false

  let module_of_t : t -> Module.t = function
    | `Resolved (#Resolved.Module.t) 
    | `Dot (_,_) as x -> x
    | _ -> assert false

  let type_of_t : t -> Type.t = function
    | `Resolved (#Resolved.Type.t) 
    | `Dot (_,_) as x -> x
    | _ -> assert false

end


module Reference = struct

  let rec hash_resolved : Paths_types.Resolved_reference.any -> int =
    fun p ->
      let open Paths_types.Resolved_reference in
      match p with
      | `Identifier id ->
        Identifier.hash id
      | `SubstAlias (r1, r2) ->
        Hashtbl.hash (41, Path.Resolved.Module.hash r1, hash_resolved (r2 : module_ :> any ))
      | `Module(p, s) ->
        Hashtbl.hash (42, hash_resolved (p : signature :> any), s)
      | `Canonical (rp, p) ->
        Hashtbl.hash (43, hash_resolved (rp : module_ :> any), hash_reference (p : Paths_types.Reference.module_ :> Paths_types.Reference.any))
      | `ModuleType(p, s) ->
        Hashtbl.hash (44, hash_resolved (p : signature :> any), s)
      | `Type(p, s) ->
        Hashtbl.hash (45, hash_resolved (p : signature :> any), s)
      | `Constructor(p, s) ->
        Hashtbl.hash (46, hash_resolved (p : datatype :> any), s)
      | `Field(p, s) ->
        Hashtbl.hash (47, hash_resolved (p : parent :> any), s)
      | `Extension(p, s) ->
        Hashtbl.hash (48, hash_resolved (p : signature :> any), s)
      | `Exception(p, s) ->
        Hashtbl.hash (49, hash_resolved (p : signature :> any), s)
      | `Value(p, s) ->
        Hashtbl.hash (50, hash_resolved (p : signature :> any), s)
      | `Class(p, s) ->
        Hashtbl.hash (51, hash_resolved (p : signature :> any), s)
      | `ClassType(p, s) ->
        Hashtbl.hash (52, hash_resolved (p : signature :> any), s)
      | `Method(p, s) ->
        Hashtbl.hash (53, hash_resolved (p : class_signature :> any), s)
      | `InstanceVariable(p, s) ->
        Hashtbl.hash (54, hash_resolved (p : class_signature :> any), s)
      | `Label(p, s) ->
        Hashtbl.hash (55, hash_resolved (p : label_parent :> any), s)

  and hash_reference : Paths_types.Reference.any -> int =
    fun p ->
      let open Paths_types.Reference in
      match p with
      | `Resolved p -> hash_resolved p
      | `Root (s, k) -> Hashtbl.hash (56, s, k)
      | `Dot (p,s) -> Hashtbl.hash (57, hash_reference (p : label_parent :> any), s)
      | `Module (p,s) -> Hashtbl.hash (58, hash_reference (p : signature :> any), s)
      | `ModuleType (p,s) -> Hashtbl.hash (59, hash_reference (p : signature :> any), s)
      | `Type (p,s) -> Hashtbl.hash (60, hash_reference (p : signature :> any), s)
      | `Constructor (p,s) -> Hashtbl.hash (61, hash_reference (p : datatype :> any), s)
      | `Field (p,s) -> Hashtbl.hash (62, hash_reference (p : parent :> any), s)
      | `Extension (p,s) -> Hashtbl.hash (63, hash_reference (p : signature :> any), s)
      | `Exception (p,s) -> Hashtbl.hash (64, hash_reference (p : signature :> any), s)
      | `Value (p,s) -> Hashtbl.hash (65, hash_reference (p : signature :> any), s)
      | `Class (p,s) -> Hashtbl.hash (66, hash_reference (p : signature :> any), s)
      | `ClassType (p,s) -> Hashtbl.hash (67, hash_reference (p : signature :> any), s)
      | `Method (p,s) -> Hashtbl.hash (68, hash_reference (p : class_signature :> any), s)
      | `InstanceVariable (p,s) -> Hashtbl.hash (69, hash_reference (p : class_signature :> any), s)
      | `Label (p,s) -> Hashtbl.hash (70, hash_reference (p : label_parent :> any), s)

  let rec resolved_equal : Paths_types.Resolved_reference.any -> Paths_types.Resolved_reference.any -> bool = 
    let open Paths_types.Resolved_reference in
    fun id1 id2 ->
      match id1, id2 with
      | `Identifier id1, `Identifier id2 ->
        Identifier.equal id1 id2
      | `SubstAlias (r1, m1), `SubstAlias (r2, m2) ->
        Path.Resolved.equal
          (r1 : Path.Resolved.Module.t :> Path.Resolved.t)
          (r2 : Path.Resolved.Module.t :> Path.Resolved.t)
        && resolved_equal (m1 : module_ :> any) (m2 : module_ :> any)
      | `Module(r1, s1), `Module(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : signature :> any) (r2 : signature :> any)
      | `Canonical(m1, r1), `Canonical(m2, r2) ->
        equal
          (r1 : Paths_types.Reference.module_ :> Paths_types.Reference.any)
          (r2 : Paths_types.Reference.module_ :> Paths_types.Reference.any)
        && resolved_equal (m1 : module_ :> any) (m2 : module_ :> any)
      | `ModuleType(r1, s1), `ModuleType(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : signature :> any) (r2 : signature :> any)
      | `Type(r1, s1), `Type(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : signature :> any) (r2 : signature :> any)
      | `Constructor(r1, s1), `Constructor(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : datatype :> any) (r2 : datatype :> any)
      | `Field(r1, s1), `Field(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : parent :> any) (r2 : parent :> any)
      | `Extension(r1, s1), `Extension(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : signature :> any) (r2 : signature :> any)
      | `Exception(r1, s1), `Exception(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : signature :> any) (r2 : signature :> any)
      | `Value(r1, s1), `Value(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : signature :> any) (r2 : signature :> any)
      | `Class(r1, s1), `Class(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : signature :> any) (r2 : signature :> any)
      | `ClassType(r1, s1), `ClassType(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : signature :> any) (r2 : signature :> any)
      | `Method(r1, s1), `Method(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : class_signature :> any) (r2 : class_signature :> any)
      | `InstanceVariable(r1, s1), `InstanceVariable(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : class_signature :> any) (r2 : class_signature :> any)
      | `Label(r1, s1), `Label(r2, s2) ->
        s1 = s2 && resolved_equal (r1 : label_parent :> any) (r2 : label_parent :> any)
      | _, _ -> false

  and equal : Paths_types.Reference.any -> Paths_types.Reference.any -> bool = 
    let open Paths_types.Reference in
    fun r1 r2 ->
      match r1, r2 with
      | `Resolved r1, `Resolved r2 ->
        resolved_equal r1 r2
      | `Root (s1, k1), `Root (s2, k2) ->
        s1 = s2 && k1 = k2
      | `Dot(r1, s1), `Dot(r2, s2) ->
        s1 = s2 && equal (r1 : label_parent :> any) (r2 : label_parent :> any)
      | `Module(r1, s1), `Module(r2, s2) ->
        s1 = s2 && equal (r1 : signature :> any) (r2 : signature :> any)
      | `ModuleType(r1, s1), `ModuleType(r2, s2) ->
        s1 = s2 && equal (r1 : signature :> any) (r2 : signature :> any)
      | `Type(r1, s1), `Type(r2, s2) ->
        s1 = s2 && equal (r1 : signature :> any) (r2 : signature :> any)
      | `Constructor(r1, s1), `Constructor(r2, s2) ->
        s1 = s2 && equal (r1 : datatype :> any) (r2 : datatype :> any)
      | `Field(r1, s1), `Field(r2, s2) ->
        s1 = s2 && equal (r1 : parent :> any) (r2 : parent :> any)
      | `Extension(r1, s1), `Extension(r2, s2) ->
        s1 = s2 && equal (r1 : signature :> any) (r2 : signature :> any)
      | `Exception(r1, s1), `Exception(r2, s2) ->
        s1 = s2 && equal (r1 : signature :> any) (r2 : signature :> any)
      | `Class(r1, s1), `Class(r2, s2) ->
        s1 = s2 && equal (r1 : signature :> any) (r2 : signature :> any)
      | `ClassType(r1, s1), `ClassType(r2, s2) ->
        s1 = s2 && equal (r1 : signature :> any) (r2 : signature :> any)
      | `Method(r1, s1), `Method(r2, s2) ->
        s1 = s2 && equal (r1 : class_signature :> any) (r2 : class_signature :> any)
      | `InstanceVariable(r1, s1), `InstanceVariable(r2, s2) ->
        s1 = s2 && equal (r1 : class_signature :> any) (r2 : class_signature :> any)
      | `Label(r1, s1), `Label(r2, s2) ->
        s1 = s2 && equal (r1 : label_parent :> any) (r2 : label_parent :> any)
      | _, _ -> false

  module Resolved = struct
    open Paths_types.Resolved_reference

    let rec parent_signature_identifier : Paths_types.Resolved_reference.signature -> Identifier.Signature.t =
      function
      | `Identifier id -> id
      | `SubstAlias(sub, _) -> Path.Resolved.parent_module_identifier sub
      | `Module(m, n) -> `Module(parent_signature_identifier m, n)
      | `Canonical(_, `Resolved r) ->
        parent_signature_identifier (r : module_ :> signature)
      | `Canonical (r, _) -> parent_signature_identifier (r : module_ :> signature)
      | `ModuleType(m, s) -> `ModuleType(parent_signature_identifier m, s)

    let parent_type_identifier : datatype -> Identifier.DataType.t =
      function
      | `Identifier id -> id
      | `Type(sg, s) -> `Type(parent_signature_identifier sg, s)

    let parent_class_signature_identifier :
      class_signature -> Identifier.ClassSignature.t =
      function
      | `Identifier id -> id
      | `Class(sg, s) -> `Class(parent_signature_identifier sg, s)
      | `ClassType(sg, s) -> `ClassType(parent_signature_identifier sg, s)

    let rec parent_identifier : parent -> Identifier.Parent.t =
      function
      | `Identifier id -> id
      | `SubstAlias(sub, _) ->
        let id = Path.Resolved.parent_module_identifier sub in
        (id : Identifier.Signature.t :> Identifier.Parent.t)
      | `Module(m, n) -> `Module(parent_signature_identifier m, n)
      | `Canonical(_, `Resolved r) ->
        parent_identifier (r : module_ :> parent)
      | `Canonical (r, _) -> parent_identifier (r : module_ :> parent)
      | `ModuleType(m, s) -> `ModuleType(parent_signature_identifier m, s)
      | `Type(sg, s) -> `Type(parent_signature_identifier sg, s)
      | `Class(sg, s) -> `Class(parent_signature_identifier sg, s)
      | `ClassType(sg, s) -> `ClassType(parent_signature_identifier sg, s)

    let rec label_parent_identifier : label_parent -> Identifier.LabelParent.t =
      function
      | `Identifier id -> id
      | `SubstAlias(sub, _) ->
        let id = Path.Resolved.parent_module_identifier sub in
        (id : Identifier.Signature.t :> Identifier.LabelParent.t)
      | `Module(m, n) -> `Module(parent_signature_identifier m, n)
      | `Canonical(_, `Resolved r) ->
        label_parent_identifier (r : module_ :> label_parent)
      | `Canonical (r, _) -> label_parent_identifier (r : module_ :> label_parent)
      | `ModuleType(m, s) -> `ModuleType(parent_signature_identifier m, s)
      | `Type(sg, s) -> `Type(parent_signature_identifier sg, s)
      | `Class(sg, s) -> `Class(parent_signature_identifier sg, s)
      | `ClassType(sg, s) -> `ClassType(parent_signature_identifier sg, s)


    type mod_rebase_result =
      | MStop of Paths_types.Resolved_reference.module_
      | MContinue of Identifier.Module.t * Reversed.t

    type sig_rebase_result =
      | SStop of Paths_types.Resolved_reference.signature
      | SContinue of Identifier.Signature.t * Reversed.t

    let rec rebase_module_reference :
      Reversed.t -> Paths_types.Resolved_reference.module_ -> mod_rebase_result =
      fun new_base t ->
      match t with
      | `Identifier id ->
        let rev = Identifier.(to_reversed @@ signature_of_module id) in
        let new_base = Reversed.remove_prefix rev ~of_:new_base in
        MContinue (id, new_base)
      | `SubstAlias _ -> MStop t (* FIXME? *)
      | `Module (m, s) ->
        begin match rebase_signature_reference new_base m with
          | SStop m' -> if m == m' then MStop t else MStop (`Module (m', s))
          | SContinue (id, new_base) ->
            let id = `Module(id, s) in
            match new_base with
            | Reversed.Module s' :: rest when s = s' ->
              MContinue (id, rest)
            | _ ->
              MStop (`Identifier id)
        end
      | `Canonical (_, `Resolved p) ->
        (* We only care about printing at this point, so let's drop the lhs. *)
        rebase_module_reference new_base p
      | `Canonical (rp, p) ->
        begin match rebase_module_reference new_base (rp) with
          | MStop rp' -> MStop (`Canonical (rp', p))
          | _ ->
            (* We might come back at some point with a resolved rhs? So we don't want to
               drop it. *)
            MStop t
        end

    and rebase_signature_reference :
      Reversed.t -> Paths_types.Resolved_reference.signature -> sig_rebase_result =
      fun new_base t ->
      match t with
      | `Identifier id ->
        let rev = Identifier.(to_reversed id) in
        let new_base = Reversed.remove_prefix rev ~of_:new_base in
        SContinue (id, new_base)
      | `ModuleType (m, s) ->
        begin match rebase_signature_reference new_base m with
          | SStop m' -> if m == m' then SStop t else SStop (`ModuleType(m', s))
          | SContinue (id, new_base) ->
            let id = `ModuleType(id, s) in
            match new_base with
            | Reversed.ModuleType s' :: rest when s = s' ->
              SContinue (id, rest)
            | _ ->
              SStop (`Identifier id)
        end
      | `Module _ | `Canonical _ as x ->
        begin match rebase_module_reference new_base x with
          | MStop rp -> SStop (rp : module_ :> signature)
          | MContinue (id, rev) ->
            SContinue (Identifier.signature_of_module id, rev)
        end
      | `SubstAlias _ -> SStop t (* FIXME? *)

    let rebase_single_module = fun
      new_base t ->
      match t with
      | `Module (mp, s) ->
        begin match rebase_signature_reference new_base mp with
          | SContinue (id, _) ->
            `Identifier (`Module(id, s))
          | SStop mp' -> `Module (mp', s)
        end

    type module_id = [  
      | `Identifier of Identifier.Module.t
    ]

    let rebase_single_canonical : Reversed.t -> s_canonical -> [ s_canonical | module_id ] = fun
      new_base t ->
      match t with
      | `Canonical (p, `Resolved rp) ->
        begin match rebase_module_reference new_base rp with
          | MContinue (id, _) -> `Identifier id
          | MStop rp ->
            (* Easier to reexport a canonical than get the type for rp right... *)
            `Canonical (p, `Resolved rp)
        end
      | `Canonical (rp, p) ->
        begin match rebase_module_reference new_base rp with
          | MStop rp' -> `Canonical (rp', p)
          | _ ->
            (* We might come back at some point with a resolved rhs? So we don't want to
               drop it. *)
            (t :> [s_canonical | module_id ])
        end

    let rebase_single_module_type : Reversed.t -> s_module_type -> module_type = fun
      new_base t ->
      match t with
      | `ModuleType (mp, s) ->
        begin match rebase_signature_reference new_base mp with
          | SContinue (id, _) ->
            `Identifier (`ModuleType (id, s))
          | SStop mp' -> `ModuleType (mp', s)
        end

    let rebase_single_module : Reversed.t -> s_module -> [ s_module | `Identifier of Identifier.Module.t ] = fun
      new_base t ->
      match t with
      | `Module (x,y) -> (rebase_single_module new_base (`Module (x,y)))

    let rebase_single_class : Reversed.t -> s_class -> class_ = fun
      new_base t ->
      match t with
      | `Class (mp, s) ->
        begin match rebase_signature_reference new_base mp with
          | SContinue (id, _) -> `Identifier (`Class (id, s))
          | SStop mp' -> `Class (mp', s)
        end

    let rebase_single_value : Reversed.t -> s_value -> [s_value | `Identifier of Identifier.Value.t] = fun
      new_base t ->
      match t with
      | `Value (mp, s) ->
        begin match rebase_signature_reference new_base mp with
          | SContinue (id, _) -> `Identifier (`Value (id, s))
          | SStop mp' -> `Value (mp', s)
        end

    let rebase_single_class_type : Reversed.t -> s_class_type -> class_type = fun
      new_base t ->
      match t with
      | `ClassType (mp, s) ->
        begin match rebase_signature_reference new_base mp with
          | SContinue (id, _) -> `Identifier (`ClassType (id, s))
          | SStop mp' -> `ClassType (mp', s)
        end

    let rebase_single_type : Reversed.t -> s_type -> [s_type | `Identifier of Paths_types.Identifier.type_] = fun
      new_base t ->
      match t with
      | `Type (mp, s) ->
        begin match rebase_signature_reference new_base mp with
          | SContinue (id, _) -> `Identifier (`Type (id, s))
          | SStop mp' -> `Type (mp', s)
        end

    let rebase_single_extension : Reversed.t -> s_extension -> [s_extension | `Identifier of Paths_types.Identifier.reference_extension] = fun
      new_base t ->
      match t with 
      | `Extension (mp, s) ->
        begin match rebase_signature_reference new_base mp with
        | SContinue (id, _) ->
          `Identifier (`Extension (id, s))
        | SStop mp' -> `Extension (mp', s)
        end

    let rebase_single_exception : Reversed.t -> s_exception -> exception_ = fun
      new_base t ->
      match t with
      | `Exception (mp, s) ->
        begin match rebase_signature_reference new_base mp with
        | SContinue (id, _) ->
          `Identifier (`Exception (id, s))
        | SStop mp' -> `Exception (mp', s)
        end


    module Signature =
    struct
      type t = Paths_types.Resolved_reference.signature

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let rec identifier : t -> Identifier.Signature.t = function
        | `Identifier id -> id
        | `SubstAlias(_, p) -> identifier (p : module_ :> signature)
        | `Module(s, n) -> `Module(parent_signature_identifier s, n)
        | `Canonical(_, `Resolved p) -> identifier (p : module_ :> signature)
        | `Canonical(p, _) -> identifier (p : module_ :> signature)
        | `ModuleType(s, n) -> `ModuleType(parent_signature_identifier s, n)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `SubstAlias _ -> t (* TODO: rewrite necessary? *)
        | `Canonical (x, y) -> (rebase_single_canonical new_base (`Canonical (x,y)) :> t)
        | `Module (x,y) -> (rebase_single_module new_base (`Module (x,y)) :> t)
        | `ModuleType (x,y) -> (rebase_single_module_type new_base (`ModuleType (x,y)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module ClassSignature =
    struct
      type t = Paths_types.Resolved_reference.class_signature

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Identifier.ClassSignature.t = function
        | `Identifier id -> id
        | `Class(s, n) -> `Class(parent_signature_identifier s, n)
        | `ClassType(s, n) -> `ClassType(parent_signature_identifier s, n)

      let rebase' : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `Class (mp, s) -> (rebase_single_class new_base (`Class (mp, s)) :> t)
        | `ClassType (x, y) -> (rebase_single_class_type new_base (`ClassType (x, y)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase' rev t

    end


    module DataType =
    struct
      type t = Paths_types.Resolved_reference.datatype

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Identifier.DataType.t = function 
        | `Identifier id -> id
        | `Type(s, n) -> `Type(parent_signature_identifier s, n)

      let rebase' : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `Type (s, n) -> (rebase_single_type new_base (`Type (s, n)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase' rev t
    end

    let rebase_single_constructor : Reversed.t -> s_constructor -> s_constructor = fun
      new_base t ->
      match t with
      | `Constructor (parent, s) ->
          `Constructor(DataType.rebase' new_base parent, s)

    module Parent =
    struct
      type t = Paths_types.Resolved_reference.parent

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let rec identifier : t -> Identifier.Parent.t = function
        | `Identifier id -> id
        | `SubstAlias(_, p) -> identifier (p : module_ :> t)
        | `Module(s, n) -> `Module(parent_signature_identifier s, n)
        | `Canonical(_, `Resolved p) -> identifier (p : module_ :> t)
        | `Canonical(p, _) -> identifier (p : module_ :> t)
        | `ModuleType(s, n) -> `ModuleType(parent_signature_identifier s, n)
        | `Class(s, n) -> `Class(parent_signature_identifier s, n)
        | `ClassType(s, n) -> `ClassType(parent_signature_identifier s, n)
        | `Type(s, n) -> `Type(parent_signature_identifier s, n)

      let rebase' : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `SubstAlias _ -> t (* TODO: rewrite necessary? *)
        | `Canonical (x, y) -> (rebase_single_canonical new_base (`Canonical (x,y)) :> t)
        | `Module (x,y) -> (rebase_single_module new_base (`Module (x,y)) :> t)
        | `ModuleType (x,y) -> (rebase_single_module_type new_base (`ModuleType (x,y)) :> t)
        | `Type (mp, s) -> (rebase_single_type new_base (`Type (mp, s)) :> t)
        | `Class (mp, s) -> (rebase_single_class new_base (`Class (mp, s)) :> t)
        | `ClassType (x, y) -> (rebase_single_class_type new_base (`ClassType (x, y)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase' rev t
    end

    let rebase_single_field : Reversed.t -> s_field -> s_field = fun
      new_base t ->
      match t with
      | `Field (parent, s) ->
          `Field(Parent.rebase' new_base parent, s)

    module LabelParent =
    struct
      type t = Paths_types.Resolved_reference.label_parent

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let rec identifier : t -> Identifier.LabelParent.t = function
        | `Identifier id -> id
        | `SubstAlias(_, p) -> identifier (p : module_ :> t)
        | `Module(s, n) -> `Module(parent_signature_identifier s, n)
        | `Canonical(_, `Resolved p) -> identifier (p : module_ :> t)
        | `Canonical(p, _) -> identifier (p : module_ :> t)
        | `ModuleType(s, n) -> `ModuleType(parent_signature_identifier s, n)
        | `Class(s, n) -> `Class(parent_signature_identifier s, n)
        | `ClassType(s, n) -> `ClassType(parent_signature_identifier s, n)
        | `Type(s, n) -> `Type(parent_signature_identifier s, n)

      let rebase' : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `SubstAlias _ -> t (* TODO: rewrite necessary? *)
        | `Canonical (x, y) -> (rebase_single_canonical new_base (`Canonical (x,y)) :> t)
        | `Module (x,y) -> (rebase_single_module new_base (`Module (x,y)) :> t)
        | `ModuleType (x,y) -> (rebase_single_module_type new_base (`ModuleType (x,y)) :> t)
        | `Type (mp, s) -> (rebase_single_type new_base (`Type (mp, s)) :> t)
        | `Class (mp, s) -> (rebase_single_class new_base (`Class (mp, s)) :> t)
        | `ClassType (x, y) -> (rebase_single_class_type new_base (`ClassType (x, y)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase' rev t
    end

    module Module =
    struct
      type t = Paths_types.Resolved_reference.module_

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let rec identifier : t -> Identifier.Module.t = function
        | `Identifier id -> id
        | `SubstAlias(_, p) -> identifier (p : module_ :> t)
        | `Module(s, n) -> `Module(parent_signature_identifier s, n)
        | `Canonical(_, `Resolved p) -> identifier (p : module_ :> t)
        | `Canonical(p, _) -> identifier (p : module_ :> t)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `SubstAlias _ -> t (* TODO: rewrite necessary? *)
        | `Canonical (x, y) -> (rebase_single_canonical new_base (`Canonical (x,y)) :> t)
        | `Module (x,y) -> (rebase_single_module new_base (`Module (x,y)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

  module ModuleType =
    struct
      type t = Paths_types.Resolved_reference.module_type

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Identifier.ModuleType.t = function
        | `Identifier id -> id
        | `ModuleType(s, n) -> `ModuleType(parent_signature_identifier s, n)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `ModuleType (x,y) -> (rebase_single_module_type new_base (`ModuleType (x,y)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module Type =
    struct
      type t = Paths_types.Resolved_reference.type_

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Identifier.Path.Type.t = function
        | `Identifier id -> id
        | `Type(s, n) -> `Type(parent_signature_identifier s, n)
        | `Class(s,n) -> `Class(parent_signature_identifier s, n)
        | `ClassType(s,n) -> `ClassType(parent_signature_identifier s, n)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `Type (mp, s) -> (rebase_single_type new_base (`Type (mp, s)) :> t)
        | `Class (s, n) -> (rebase_single_class new_base (`Class (s, n)) :> t)
        | `ClassType (s, n) -> (rebase_single_class_type new_base (`ClassType (s, n)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module Constructor =
    struct
      type t = Paths_types.Resolved_reference.constructor

      let equal t1 t2 =
        resolved_equal (t1 : t :> any) (t2 : t :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_constructor = function
        | `Identifier id -> id
        | `Constructor(s, n) -> `Constructor(parent_type_identifier s, n)
        | `Extension(s, n) -> `Extension(parent_signature_identifier s, n)
        | `Exception(s, n) -> `Exception(parent_signature_identifier s, n)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
        match t with
        | `Identifier _ -> t
        | `Constructor (p,q) -> (rebase_single_constructor new_base (`Constructor (p,q)) :> t)
        | `Extension (p,q) -> (rebase_single_extension new_base (`Extension (p,q)) :> t)
        | `Exception (p,q) -> (rebase_single_exception new_base (`Exception (p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module Field =
    struct
      type t = Paths_types.Resolved_reference.field

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Identifier.Field.t = function
        | `Identifier id -> id
        | `Field(p, n) -> `Field(parent_identifier p, n)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `Field (p,q) -> (rebase_single_field new_base (`Field (p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module Extension =
    struct
      type t = Paths_types.Resolved_reference.extension

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_extension  = function
        | `Identifier id -> id
        | `Extension(p,q) -> `Extension(parent_signature_identifier p, q)
        | `Exception(p,q) -> `Exception(parent_signature_identifier p, q)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `Extension(p,q) -> (rebase_single_extension new_base (`Extension (p,q)) :> t)
          | `Exception(p,q) -> (rebase_single_exception new_base (`Exception (p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module Exception =
    struct
      type t = Paths_types.Resolved_reference.exception_

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_exception  = function
        | `Identifier id -> id
        | `Exception(p,q) -> `Exception(parent_signature_identifier p, q)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `Exception(p,q) -> (rebase_single_exception new_base (`Exception (p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module Value =
    struct
      type t = Paths_types.Resolved_reference.value

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_value  = function
        | `Identifier id -> id
        | `Value(p,q) -> `Value(parent_signature_identifier p, q)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `Value(p,q) -> (rebase_single_value new_base (`Value (p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module Class =
    struct
      type t = Paths_types.Resolved_reference.class_

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_class  = function
        | `Identifier id -> id
        | `Class(p,q) -> `Class(parent_signature_identifier p, q)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `Class(p,q) -> (rebase_single_class new_base (`Class (p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module ClassType =
    struct
      type t = Paths_types.Resolved_reference.class_type

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_class_type  = function
        | `Identifier id -> id
        | `Class(p,q) -> `Class(parent_signature_identifier p, q)
        | `ClassType(p,q) -> `ClassType(parent_signature_identifier p, q)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `Class(p,q) -> (rebase_single_class new_base (`Class (p,q)) :> t)
          | `ClassType(p,q) -> (rebase_single_class_type new_base (`ClassType(p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    let rebase_single_method : Reversed.t -> s_method -> s_method = fun
      new_base t ->
      match t with
      | `Method (parent, s) ->
          `Method(ClassSignature.rebase' new_base parent, s)

    let rebase_single_instance_variable : Reversed.t -> s_instance_variable -> s_instance_variable = fun
      new_base t ->
      match t with
      | `InstanceVariable (parent, s) ->
          `InstanceVariable(ClassSignature.rebase' new_base parent, s)

    module Method =
    struct
      type t = Paths_types.Resolved_reference.method_

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_method  = function
        | `Identifier id -> id
        | `Method(p,q) -> `Method(parent_class_signature_identifier p, q)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `Method(p,q) -> (rebase_single_method new_base (`Method (p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module InstanceVariable =
    struct
      type t = Paths_types.Resolved_reference.instance_variable

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_instance_variable  = function
        | `Identifier id -> id
        | `InstanceVariable(p,q) -> `InstanceVariable(parent_class_signature_identifier p, q)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `InstanceVariable(p,q) -> (rebase_single_instance_variable new_base (`InstanceVariable(p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    let rebase_single_label : Reversed.t -> s_label -> s_label = fun
      new_base t ->
      match t with
      | `Label (parent, s) ->
          `Label(LabelParent.rebase' new_base parent, s)

    module Label =
    struct
      type t = Paths_types.Resolved_reference.label

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_label  = function
        | `Identifier id -> id
        | `Label(p,q) -> `Label(label_parent_identifier p, q)

      let rebase : Reversed.t -> t -> t =
        fun new_base t ->
          match t with
          | `Identifier _ -> t
          | `Label(p,q) -> (rebase_single_label new_base (`Label(p,q)) :> t)

      let rebase id t =
        let rev = Identifier.to_reversed id in
        rebase rev t
    end

    module Page =
    struct
      type t = Paths_types.Resolved_reference.page

      let equal t1 t2 =
        resolved_equal (t1 :> any) (t2 :> any)

      let hash t = hash_resolved (t : t :> any)

      let identifier : t -> Paths_types.Identifier.reference_page  = function
        | `Identifier id -> id

      let rebase _id t = t
    end

    type t = Paths_types.Resolved_reference.any

    let rec identifier : t -> Identifier.t = function
      | `Identifier id -> id
      | `SubstAlias(_, p) -> identifier (p :> t)
      | `Module(s, n) -> `Module(parent_signature_identifier s, n)
      | `Canonical(_, `Resolved p) -> identifier (p :> t)
      | `Canonical(p, _) -> identifier (p :> t)
      | `ModuleType(s, n) -> `ModuleType(parent_signature_identifier s, n)
      | `Field(p, n) -> `Field(parent_identifier p, n)
      | `Type(s, n) -> `Type(parent_signature_identifier s, n)
      | `Constructor(s, n) -> `Constructor(parent_type_identifier s, n)
      | `Extension(p,q) -> `Extension(parent_signature_identifier p, q)
      | `Exception(p,q) -> `Exception(parent_signature_identifier p, q)
      | `Value(p,q) -> `Value(parent_signature_identifier p, q)
      | `Class(p,q) -> `Class(parent_signature_identifier p, q)
      | `ClassType(p,q) -> `ClassType(parent_signature_identifier p, q)
      | `Method(p,q) -> `Method(parent_class_signature_identifier p, q)
      | `InstanceVariable(p,q) -> `InstanceVariable(parent_class_signature_identifier p, q)
      | `Label(p,q) -> `Label(label_parent_identifier p, q)

    let module_of_t : t -> Module.t = function
      | `Identifier (#Identifier.Module.t)
      | #Paths_types.Resolved_reference.module_no_id as x -> x
      | _ -> assert false

    let module_type_of_t : t -> ModuleType.t = function
      | `Identifier (#Identifier.ModuleType.t)
      | #Paths_types.Resolved_reference.s_module_type as x -> x
      | _ -> assert false

    let signature_of_t : t -> Signature.t = function
      | `Identifier (#Identifier.Signature.t)
      | #Paths_types.Resolved_reference.signature_no_id as x -> x
      | _ -> assert false

    let class_signature_of_t : t -> ClassSignature.t = function
      | `Identifier (#Identifier.ClassSignature.t)
      | #Paths_types.Resolved_reference.class_signature_no_id as x -> x
      | _ -> assert false

    let parent_of_t : t -> Parent.t = function
      | `Identifier (#Identifier.Parent.t)
      | #Paths_types.Resolved_reference.parent_no_id as x -> x
      | _ -> assert false

    let label_parent_of_t : t -> LabelParent.t = function
      | `Identifier (#Identifier.LabelParent.t)
      | #Paths_types.Resolved_reference.parent_no_id as x -> x
      | _ -> assert false

    let type_of_t : t -> Type.t = function
      | `Identifier (#Identifier.Type.t)
      | #Paths_types.Resolved_reference.s_type as x -> x
      | _ -> assert false

    let datatype_of_t : t -> DataType.t = function
      | `Identifier (#Identifier.DataType.t)
      | #Paths_types.Resolved_reference.s_type as x -> x
      | _ -> assert false

    let constructor_of_t : t -> Constructor.t = function
      | `Identifier (#Identifier.Constructor.t)
      | #Paths_types.Resolved_reference.s_constructor
      | #Paths_types.Resolved_reference.s_extension
      | #Paths_types.Resolved_reference.s_exception as x -> x
      | _ -> assert false

    let field_of_t : t -> Field.t = function
      | `Identifier (#Identifier.Field.t)
      | #Paths_types.Resolved_reference.s_field as x -> x
      | _ -> assert false

    let extension_of_t : t -> Extension.t = function
      | `Identifier (#Paths_types.Identifier.reference_extension)
      | #Paths_types.Resolved_reference.s_extension
      | #Paths_types.Resolved_reference.s_exception as x -> x
      | _ -> assert false

    let exception_of_t : t -> Exception.t = function
      | `Identifier (#Paths_types.Identifier.reference_exception)
      | #Paths_types.Resolved_reference.s_exception as x -> x
      | _ -> assert false

    let value_of_t : t -> Value.t = function
      | `Identifier (#Paths_types.Identifier.reference_value)
      | #Paths_types.Resolved_reference.s_value as x -> x
      | _ -> assert false

    let class_of_t : t -> Class.t = function
      | `Identifier (#Paths_types.Identifier.reference_class)
      | #Paths_types.Resolved_reference.s_class as x -> x
      | _ -> assert false

    let class_type_of_t : t -> ClassType.t = function
      | `Identifier (#Paths_types.Identifier.reference_class_type)
      | #Paths_types.Resolved_reference.s_class_type as x -> x
      | _ -> assert false

    let method_of_t : t -> Method.t = function
      | `Identifier (#Paths_types.Identifier.reference_method)
      | #Paths_types.Resolved_reference.s_method as x -> x
      | _ -> assert false

    let instance_variable_of_t : t -> InstanceVariable.t = function
      | `Identifier (#Paths_types.Identifier.reference_instance_variable)
      | #Paths_types.Resolved_reference.s_instance_variable as x -> x
      | _ -> assert false

    let label_of_t : t -> Label.t = function
      | `Identifier (#Paths_types.Identifier.reference_label)
      | #Paths_types.Resolved_reference.s_label as x -> x
      | _ -> assert false

  end
  type t = Paths_types.Reference.any

  let equal : t -> t -> bool = equal

  let hash p = hash_reference p

  module Signature =
  struct
    type t = Paths_types.Reference.signature
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module ClassSignature =
  struct
    type t = Paths_types.Reference.class_signature
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module DataType =
  struct
    type t = Paths_types.Reference.datatype
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Parent =
  struct
    type t = Paths_types.Reference.parent
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module LabelParent =
  struct
    type t = Paths_types.Reference.label_parent
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Module =
  struct
    type t = Paths_types.Reference.module_
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module ModuleType =
  struct
    type t = Paths_types.Reference.module_type
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Type =
  struct
    type t = Paths_types.Reference.type_
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Constructor =
  struct
    type t = Paths_types.Reference.constructor
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Field =
  struct
    type t = Paths_types.Reference.field
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Extension =
  struct
    type t = Paths_types.Reference.extension
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Exception =
  struct
    type t = Paths_types.Reference.exception_
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Value =
  struct
    type t = Paths_types.Reference.value
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Class =
  struct
    type t = Paths_types.Reference.class_
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module ClassType =
  struct
    type t = Paths_types.Reference.class_type
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Method =
  struct
    type t = Paths_types.Reference.method_
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module InstanceVariable =
  struct
    type t = Paths_types.Reference.instance_variable
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Label =
  struct
    type t = Paths_types.Reference.label
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  module Page =
  struct
    type t = Paths_types.Reference.page
    let equal : t -> t -> bool = fun t1 t2 -> equal (t1 :> Paths_types.Reference.any) (t2 :> Paths_types.Reference.any)
    let hash : t -> int = fun t -> hash (t :> Paths_types.Reference.any)
  end

  let module_of_t : t -> Module.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.module_))
  | `Resolved (#Paths_types.Resolved_reference.module_no_id)
  | `Root (_,#Paths_types.Reference.tag_module)
  | `Dot (_,_)
  | `Module (_, _) as x -> x
  | _ -> assert false

let module_type_of_t : t -> ModuleType.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.module_type))
  | `Resolved (#Paths_types.Resolved_reference.s_module_type)
  | `Root (_,#Paths_types.Reference.tag_module_type)
  | `Dot (_,_)
  | `ModuleType (_, _) as x -> x
  | _ -> assert false

let signature_of_t : t -> Signature.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.signature))
  | `Resolved (#Paths_types.Resolved_reference.signature_no_id)
  | `Root (_,#Paths_types.Reference.tag_signature)
  | `Dot (_,_)
  | `ModuleType (_,_)
  | `Module (_, _) as x -> x
  | _ -> assert false

let class_signature_of_t : t -> ClassSignature.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.class_signature))
  | `Resolved (#Paths_types.Resolved_reference.class_signature_no_id)
  | `Root (_,#Paths_types.Reference.tag_class_signature)
  | `Dot (_,_)
  | `ClassType (_,_)
  | `Class (_, _) as x -> x
  | _ -> assert false

let parent_of_t : t -> Parent.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.parent))
  | `Resolved (#Paths_types.Resolved_reference.parent_no_id)
  | `Root (_,#Paths_types.Reference.tag_parent)
  | `Dot (_,_)
  | `ClassType (_,_)
  | `ModuleType (_,_)
  | `Module (_, _)
  | `Type (_,_)
  | `Class (_, _) as x -> x
  | _ -> assert false

let label_parent_of_t : t -> LabelParent.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.label_parent))
  | `Resolved (#Paths_types.Resolved_reference.parent_no_id) (* Nb, parent_no_id would be equal to label_parent_no_id if it existed! *)
  | `Root (_,#Paths_types.Reference.tag_label_parent)
  | `Dot (_,_)
  | `ClassType (_,_)
  | `ModuleType (_,_)
  | `Module (_, _)
  | `Type (_,_)
  | `Class (_, _) as x -> x
  | _ -> assert false

let type_of_t : t -> Type.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.type_))
  | `Resolved (#Paths_types.Resolved_reference.s_type)
  | `Root (_,#Paths_types.Reference.tag_type)
  | `Dot (_,_)
  | `Type (_,_) as x -> x
  | _ -> assert false

let datatype_of_t : t -> DataType.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.datatype))
  | `Resolved (#Paths_types.Resolved_reference.s_type)
  | `Root (_,#Paths_types.Reference.tag_datatype)
  | `Dot (_,_)
  | `Type (_,_) as x -> x
  | _ -> assert false

let constructor_of_t : t -> Constructor.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_constructor))
  | `Resolved (#Paths_types.Resolved_reference.constructor_no_id)
  | `Root (_,#Paths_types.Reference.tag_constructor)
  | `Dot (_,_)
  | `Constructor (_,_)
  | `Extension (_,_)
  | `Exception (_,_) as x -> x
  | _ -> assert false

let field_of_t : t -> Field.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_field))
  | `Resolved (#Paths_types.Resolved_reference.s_field)
  | `Root (_,#Paths_types.Reference.tag_field)
  | `Dot (_,_)
  | `Field (_,_) as x -> x
  | _ -> assert false

let extension_of_t : t -> Extension.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_extension))
  | `Resolved (#Paths_types.Resolved_reference.extension_no_id)
  | `Root (_,#Paths_types.Reference.tag_extension)
  | `Dot (_,_)
  | `Extension (_,_)
  | `Exception (_,_) as x -> x
  | _ -> assert false

let exception_of_t : t -> Exception.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_exception))
  | `Resolved (#Paths_types.Resolved_reference.s_exception)
  | `Root (_,#Paths_types.Reference.tag_exception)
  | `Dot (_,_)
  | `Exception (_,_) as x -> x
  | _ -> assert false

let value_of_t : t -> Value.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_value))
  | `Resolved (#Paths_types.Resolved_reference.s_value)
  | `Root (_,#Paths_types.Reference.tag_value)
  | `Dot (_,_)
  | `Value (_,_) as x -> x
  | _ -> assert false

let class_of_t : t -> Class.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_class))
  | `Resolved (#Paths_types.Resolved_reference.s_class)
  | `Root (_,#Paths_types.Reference.tag_class)
  | `Dot (_,_)
  | `Class (_,_) as x -> x
  | _ -> assert false

let class_type_of_t : t -> ClassType.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_class_type))
  | `Resolved (#Paths_types.Resolved_reference.class_type_no_id)
  | `Root (_,#Paths_types.Reference.tag_class_type)
  | `Dot (_,_)
  | `Class (_,_) 
  | `ClassType (_,_) as x -> x 
  | _ -> assert false

let method_of_t : t -> Method.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_method))
  | `Resolved (#Paths_types.Resolved_reference.s_method)
  | `Root (_,#Paths_types.Reference.tag_method)
  | `Dot (_,_)
  | `Method (_,_) as x -> x 
  | _ -> assert false

let instance_variable_of_t : t -> InstanceVariable.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_instance_variable))
  | `Resolved (#Paths_types.Resolved_reference.s_instance_variable)
  | `Root (_,#Paths_types.Reference.tag_instance_variable)
  | `Dot (_,_)
  | `InstanceVariable (_,_) as x -> x 
  | _ -> assert false

let label_of_t : t -> Label.t = function
  | `Resolved (`Identifier (#Paths_types.Identifier.reference_label))
  | `Resolved (#Paths_types.Resolved_reference.s_label)
  | `Root (_,#Paths_types.Reference.tag_label)
  | `Dot (_,_)
  | `Label (_,_) as x -> x 
  | _ -> assert false
end
OCaml

Innovation. Community. Security.