package odoc

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

Source file components.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
(*
 * 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 Odoc_model
open Paths
open Names

(* Sets and maps for components *)

module SSet = Set.Make(String)

module SMap = struct

  include Map.Make(String)

  let filter_item name pred map =
    try
      let v = find name map in
        if pred v then map
        else remove name map
    with Not_found -> map

  let map_item name f map =
    try
      let v = find name map in
        add name (f v) map
    with Not_found -> map

end

module LMap = struct

  type 'a t = 'a list SMap.t

  let empty = SMap.empty

  let add name item map =
    try
      let items = SMap.find name map in
        SMap.add name (item :: items) map
    with Not_found ->
      SMap.add name [item] map

  (*
  let find name pred map =
    let items = SMap.find name map in
      List.find pred items
  *)

  let find_name name map =
    let items = SMap.find name map in
      match items with
      | [] -> raise Not_found
      | x :: _ -> x

  let map_find name pred map =
    let rec loop pred = function
      | [] -> raise Not_found
      | x :: l ->
          match pred x with
          | Some x -> x
          | None -> loop pred l
    in
    let items = SMap.find name map in
      loop pred items

  let fold f map acc =
    SMap.fold
      (fun name -> List.fold_right (f name))
      map acc

  let filter_item name pred map =
    try
      let items = SMap.find name map in
      let items = List.filter pred items in
        match items with
        | [] -> SMap.remove name map
        | _ -> SMap.add name items map
    with Not_found -> map

  let map_item name f map =
    try
      let items = SMap.find name map in
      let items = List.map f items in
        SMap.add name items map
    with Not_found -> map

end

(* Tables for caches *)
type ('a, 'b) tbl =
  { fresh: int -> ('a, 'b) tbl;
    find: 'a -> 'b;
    add: 'a -> 'b  -> unit; }

let make_tbl (type a) (equal : (a -> a -> bool) option)
           (hash : (a -> int) option) size =
  let make create find add =
    let rec fresh size =
      let t = create size in
      let find x = find t x in
      let add x y = add t x y in
        {fresh; find; add}
    in
      fresh size
  in
    match equal, hash with
    | None, None ->
        make (Hashtbl.create ?random:None) Hashtbl.find Hashtbl.add
    | _ ->
        let equal =
          match equal with
          | None -> (=)
          | Some eq -> eq
        in
        let hash =
          match hash with
          | None -> Hashtbl.hash
          | Some h -> h
        in
        let module Hash = struct
          type t = a
          let equal = equal
          let hash = hash
        end in
        let module Tbl = Hashtbl.Make(Hash) in
          make Tbl.create Tbl.find Tbl.add

(* Read labels from documentation *)

let documentation_labels acc doc =
  List.fold_left (fun acc element ->
    match element.Odoc_model.Location_.value with
    | `Heading (_, label, nested_text) ->
      let name = Identifier.name label in
      (name, nested_text)::acc
    | _ -> acc)
  acc doc

let comment_labels acc comment =
  match comment with
  | `Stop -> acc
  | `Docs doc -> documentation_labels acc doc

module rec Sig : sig

  type t

  val set_canonical : t -> (Path.Module.t * Reference.Module.t) option -> t

  val get_canonical : t -> (Path.Module.t * Reference.Module.t) option

  val set_hidden : t -> bool -> t

  val get_hidden : t -> bool

  val find_parent_module : string -> t -> Parent.module_

  val find_parent_apply : (Path.Module.t -> t) -> Path.Module.t ->
        t -> Parent.module_

  val find_parent_module_type : string -> t -> Parent.module_type

  val find_parent_signature : string -> t -> Parent.signature

  val find_parent_class_signature : string -> t -> Parent.class_signature

  val find_parent_datatype : string -> t -> Parent.datatype

  val find_parent_sig_or_type : string -> t -> Parent.sig_or_type

  val find_parent_subst : t -> Parent.subst

  val find_parent : string -> t -> Parent.any

  val find_module_element : string -> t -> Element.signature_module

  val find_apply_element : t -> Element.signature_module

  val find_module_type_element : string -> t -> Element.signature_module_type

  val find_type_element : string -> t -> Element.signature_type

  val find_constructor_element : string -> t -> Element.signature_constructor

  val find_field_element : string -> t -> Element.signature_field

  val find_extension_element : string -> t -> Element.signature_extension

  val find_exception_element : string -> t -> Element.signature_exception

  val find_value_element : string -> t -> Element.signature_value

  val find_class_element : string -> t -> Element.signature_class

  val find_class_type_element : string -> t -> Element.signature_class_type

  val find_label_element : string -> t -> Element.signature_label

  val find_element : string -> t -> Element.signature

  val find_section_title : string -> t -> Odoc_model.Comment.link_content

  val lookup_module : string -> t -> t

  val lookup_argument : int -> t -> t

  val lookup_apply : (Path.Module.t -> t) -> Path.Module.t ->
        t -> t

  val lookup_module_type  : string -> t -> t

  val lookup_class_type : string -> t -> ClassSig.t

  val lookup_datatype : string -> t -> Datatype.t

  type signature

  val empty : signature

  val add_module : string -> t -> signature -> signature

  val add_module_type : string -> t -> signature -> signature

  val add_datatype : string -> Datatype.t -> signature -> signature

  val add_class : string -> ClassSig.t -> signature -> signature

  val add_class_type : string -> ClassSig.t -> signature -> signature

  val add_element : string -> Element.signature -> signature -> signature

  val add_documentation : Odoc_model.Comment.docs -> signature -> signature

  val add_comment : Odoc_model.Comment.docs_or_stop -> signature -> signature

  val include_ : t -> signature -> signature

  val modules : t -> (ModuleName.t * t) list

  val module_types : t -> (ModuleTypeName.t * t) list

  val path : (Path.ModuleType.t -> t) -> Path.ModuleType.t -> t

  val alias : (Path.Module.t -> t) -> Path.Module.t -> t

  val signature : ('b -> signature) -> 'b -> t

  val functor_ : (Root.t -> Root.t -> bool) option -> (Root.t -> int) option ->
                 Identifier.Module.t -> t -> t -> t

  val generative : t -> t

  val abstract : t

  val unresolved : t

  val with_module : Fragment.Module.t -> t -> t -> t

  val with_module_subst : Fragment.Module.t -> t -> t

  val with_type_subst : Fragment.Type.t -> t -> t

end = struct

  type term =
    | Path of Path.ModuleType.t * bool
    | Alias of Path.Module.t * bool
    | WithModule of expr * Fragment.Module.t * t
    | WithModuleSubst of expr * Fragment.Module.t
    | WithTypeSubst of expr * Fragment.Type.t

  and expr =
    { term : term;
      expansion : t Lazy.t; }

  and functor_ =
    { id : Identifier.Module.t;
      arg : t;
      res : t;
      cache : (Path.Module.t, t) tbl; }

  and signature =
    { modules: t SMap.t;
      module_types: t SMap.t;
      class_signatures: ClassSig.t SMap.t;
      types: Element.signature_type SMap.t;
      parents: Parent.any LMap.t;
      elements: Element.signature LMap.t;
      section_titles: Odoc_model.Comment.link_content SMap.t; }

  and body =
    | Expr of expr
    | Sig of signature Lazy.t
    | Functor of functor_
    | Generative of t
    | Abstract
    | Unresolved

  and t =
    { canonical : (Path.Module.t * Reference.Module.t) option;
      hidden : bool;
      body : body }

  let set_canonical t canonical = { t with canonical }

  let set_hidden t hidden = { t with hidden }

  let get_canonical t = t.canonical

  let get_hidden t = t.hidden

  let mkExpr ex = { canonical = None; body = Expr ex; hidden = false }

  let mkSig sg = { canonical = None; body = Sig sg; hidden = false }

  let mkFunctor fn = { canonical = None; body = Functor fn; hidden = false }

  let generative t = { canonical = None; body = Generative t; hidden = false }

  let abstract = { canonical = None; body = Abstract; hidden = false }

  let unresolved = { canonical = None; body = Unresolved; hidden = false }

  let rec lift_find f x t =
    match t.body with
    | Expr expr -> begin
        match expr.term with
        | Path(_, true)
        | Alias(_, true) -> raise Not_found
        | Alias(_, false) -> begin
            let t = Lazy.force expr.expansion in
            match t.hidden with
            | false -> raise Not_found
            | true -> lift_find f x t
          end
        | _ -> lift_find f x (Lazy.force expr.expansion)
      end
    | Sig sg -> f x (Lazy.force sg)
    | Functor fn -> lift_find f x fn.res
    | Generative t -> lift_find f x t
    | Abstract -> raise Not_found
    | Unresolved -> raise Not_found

  let find_parent_module name t =
    let find name sg =
      `Module (SMap.find name sg.modules)
    in
      lift_find find name t

  let find_parent_module_type name t =
    let find name sg =
      `ModuleType (SMap.find name sg.module_types)
    in
      lift_find find name t

  let find_parent_signature name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Module _ as x -> Some x
          | `ModuleType _ as x -> Some x
          | _ -> None)
        sg.parents
    in
      lift_find find name t

  let find_parent_class_signature name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Class _ as x -> Some x
          | `ClassType _ as x -> Some x
          | _ -> None)
        sg.parents
    in
      lift_find find name t

  let find_parent_datatype name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Datatype _ as x -> Some x
          | _ -> None)
        sg.parents
    in
      lift_find find name t

  let find_parent_sig_or_type name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Module _ as x -> Some x
          | `ModuleType _ as x -> Some x
          | `Datatype _ as x -> Some x
          | _ -> None)
        sg.parents
    in
      lift_find find name t

  let rec find_parent_subst : t -> Parent.subst = fun t ->
    match t.body with
    | Expr expr -> begin
        match expr.term with
        | Path(p, true) -> Subst p
        | Alias(p, true) -> SubstAlias p
        | Alias(p, false) -> begin
            let t' = Lazy.force expr.expansion in
            match t'.hidden with
            | false -> SubstAlias p
            | true -> find_parent_subst t'
          end
        | _ -> find_parent_subst (Lazy.force expr.expansion)
      end
    | Sig _ -> raise Not_found
    | Functor fn -> find_parent_subst fn.res
    | Generative t -> find_parent_subst t
    | Abstract -> raise Not_found
    | Unresolved -> raise Not_found

  let find_parent name t =
    let find name sg = LMap.find_name name sg.parents in
      lift_find find name t

  let find_module_element name t =
    let find name sg =
      let t = SMap.find name sg.modules in
      `Module {
        Element.canonical = t.canonical;
        hidden = t.hidden;
      }
    in
      lift_find find name t

  let rec find_apply_element t =
    match t.body with
    | Expr expr -> begin
        match expr.term with
        | Path(_, true) | Alias(_, true) -> raise Not_found
        | Alias(_, false) -> begin
            let t = Lazy.force expr.expansion in
            match t.hidden with
            | false -> raise Not_found
            | true -> find_apply_element t
          end
        | _ -> find_apply_element (Lazy.force expr.expansion)
      end
    | Sig _ -> raise Not_found
    | Functor _ -> `Module { Element.canonical = None; hidden = false }
    | Generative _ -> raise Not_found
    | Abstract -> raise Not_found
    | Unresolved -> raise Not_found

  let find_module_type_element name t =
    let find name sg =
      if SMap.mem name sg.module_types then `ModuleType
      else raise Not_found
    in
      lift_find find name t

  let find_type_element name t =
    let find name sg = SMap.find name sg.types in
      lift_find find name t

  let find_constructor_element name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Constructor _ as x -> Some x
          | `Extension as x -> Some x
          | `Exception as x -> Some x
          | _ -> None)
        sg.elements
    in
      lift_find find name t

  let find_field_element name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Field _ as x -> Some x
          | _ -> None)
        sg.elements
    in
      lift_find find name t

  let find_extension_element name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Extension as x -> Some x
          | `Exception as x -> Some x
          | _ -> None)
        sg.elements
    in
      lift_find find name t

  let find_exception_element name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Exception as x -> Some x
          | _ -> None)
        sg.elements
    in
      lift_find find name t

  let find_value_element name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Value as x -> Some x
          | _ -> None)
        sg.elements
    in
      lift_find find name t

  let find_class_element name t =
    let find name sg =
      match SMap.find name sg.types with
      | `Class as x -> x
      | _ -> raise Not_found
    in
      lift_find find name t

  let find_class_type_element name t =
    let find name sg =
      match SMap.find name sg.types with
      | `Class as x -> x
      | `ClassType as x -> x
      | _ -> raise Not_found
    in
      lift_find find name t

  let find_label_element name t =
    let find name sg =
      LMap.map_find name
        (function
          | `Label _ as x -> Some x
          | _ -> None)
        sg.elements
    in
      lift_find find name t

  let find_section_title name t =
    let find name sg = SMap.find name sg.section_titles in
      lift_find find name t

  let find_element name t =
    let find name sg = LMap.find_name name sg.elements in
      lift_find find name t

  let rec lookup_module name t =
    match t.body with
    | Expr expr -> lookup_module name (Lazy.force expr.expansion)
    | Sig sg -> begin
        try
          SMap.find name (Lazy.force sg).modules
        with Not_found -> unresolved
      end
    | Functor fn -> lookup_module name fn.res
    | Generative t -> lookup_module name t
    | Abstract -> unresolved
    | Unresolved -> unresolved

  let rec lookup_argument pos t =
    match t.body with
    | Expr expr -> lookup_argument pos (Lazy.force expr.expansion)
    | Sig _ -> unresolved
    | Functor fn ->
        if pos = 1 then fn.arg
        else lookup_argument (pos - 1) fn.res
    | Generative t ->
        if pos = 1 then unresolved
        else lookup_argument (pos - 1) t
    | Abstract -> unresolved
    | Unresolved -> unresolved

  let rec lookup_module_type name t =
    match t.body with
    | Expr expr -> lookup_module_type name (Lazy.force expr.expansion)
    | Sig sg -> begin
        try
          SMap.find name (Lazy.force sg).module_types
        with Not_found -> unresolved
      end
    | Functor fn -> lookup_module_type name fn.res
    | Generative t -> lookup_module_type name t
    | Abstract -> unresolved
    | Unresolved -> unresolved

  let rec lookup_class_type name t =
    match t.body with
    | Expr expr -> lookup_class_type name (Lazy.force expr.expansion)
    | Sig sg -> begin
        try
          SMap.find name (Lazy.force sg).class_signatures
        with Not_found -> ClassSig.unresolved
      end
    | Functor fn -> lookup_class_type name fn.res
    | Generative t -> lookup_class_type name t
    | Abstract -> ClassSig.unresolved
    | Unresolved -> ClassSig.unresolved

  let rec lookup_datatype name t =
    match t.body with
    | Expr expr -> lookup_datatype name (Lazy.force expr.expansion)
    | Sig sg -> begin
          try
            LMap.map_find name
              (function
                | `Datatype t -> Some t
                | _ -> None)
              (Lazy.force sg).parents
          with Not_found -> Datatype.unresolved
      end
    | Functor fn -> lookup_datatype name fn.res
    | Generative t -> lookup_datatype name t
    | Abstract -> Datatype.unresolved
    | Unresolved -> Datatype.unresolved

  let empty =
    { modules = SMap.empty;
      module_types = SMap.empty;
      class_signatures = SMap.empty;
      types = SMap.empty;
      parents = SMap.empty;
      elements = SMap.empty;
      section_titles = SMap.empty; }

  let add_module name md sg =
    let modules = SMap.add name md sg.modules in
    let parents = LMap.add name (`Module md) sg.parents in
    let elements =
      let md = `Module Element.{ canonical=md.canonical; hidden=md.hidden } in
      LMap.add name md sg.elements
    in
      {sg with modules; parents; elements}

  let add_module_type name mty sg =
    let module_types = SMap.add name mty sg.module_types in
    let parents = LMap.add name (`ModuleType mty) sg.parents in
    let elements = LMap.add name `ModuleType sg.elements in
      {sg with module_types; parents; elements}

  let add_datatype name decl sg =
    let types = SMap.add name `Type sg.types in
    let parents = LMap.add name (`Datatype decl) sg.parents in
    let elements =
      let add_element name (elem : Element.datatype) acc =
        let (`Constructor _ | `Field _ | `Label _ as elem) = elem in
          LMap.add name elem acc
      in
        LMap.fold add_element (Datatype.elements decl) sg.elements
    in
    let elements = LMap.add name `Type elements in
      {sg with types; parents; elements}

  let add_class name cl sg =
    let types = SMap.add name `Class sg.types in
    let class_signatures = SMap.add name cl sg.class_signatures in
    let parents = LMap.add name (`Class cl) sg.parents in
    let elements = LMap.add name `Class sg.elements in
      {sg with types; class_signatures; parents; elements}

  let add_class_type name clty sg =
    let types = SMap.add name `ClassType sg.types in
    let class_signatures = SMap.add name clty sg.class_signatures in
    let parents = LMap.add name (`ClassType clty) sg.parents in
    let elements = LMap.add name `ClassType sg.elements in
      {sg with types; class_signatures; parents; elements}

  let add_element name element sg =
    let elements = LMap.add name element sg.elements in
      {sg with elements}

  let add_documentation doc sg =
    let labels = documentation_labels [] doc in
    let add_label sg (label, txt) =
      let sg = add_element label (`Label None) sg in
      let section_titles = SMap.add label txt sg.section_titles in
      {sg with section_titles}
    in
      List.fold_left add_label sg labels

  let add_comment comment sg =
    let labels = comment_labels [] comment in
    let add_label sg (label, txt) =
      let sg = add_element label (`Label None) sg in
      let section_titles = SMap.add label txt sg.section_titles in
      {sg with section_titles}
    in
      List.fold_left add_label sg labels

  let strengthen_submodule path expansion name t =
    match t.body with
    | Unresolved -> t
    | Expr { term = Alias(p, b); _ } when b || not (Path.is_hidden (p :> Path.t)) -> t
    | _ ->
        let path = Path.module_ path name in
        let term = Alias(path, false) in
        let expansion = lazy (lookup_module (ModuleName.to_string name) (Lazy.force expansion)) in
        { t with body = Expr {term; expansion} }

  let rec strengthen_module path expansion t =
    if Path.is_hidden (path : Path.Module.t :> Path.t) then t else
    match t.body with
    | Expr { term; expansion = ex } -> begin
        let ex' = lazy (strengthen_module path expansion (Lazy.force ex)) in
        { t with body = Expr { term; expansion = ex' } }
      end
    | Sig sg ->
      let sg =
        lazy (
          let sg = Lazy.force sg in
          let modules = SMap.mapi (fun n -> strengthen_submodule path expansion (ModuleName.of_string n)) sg.modules in
          { sg with modules }
        )
      in
      { t with body = Sig sg }
    | Functor _ | Generative _ | Abstract | Unresolved -> t

  let rec include_ t sg =
    match t.body with
    | Expr expr -> include_ (Lazy.force expr.expansion) sg
    | Sig incl ->
        let incl = Lazy.force incl in
        let modules =
          SMap.fold SMap.add incl.modules sg.modules
        in
        let module_types =
          SMap.fold SMap.add incl.module_types sg.module_types
        in
        let class_signatures =
          SMap.fold SMap.add incl.class_signatures sg.class_signatures
        in
        let types =
          SMap.fold SMap.add incl.types sg.types
        in
        let parents =
          LMap.fold LMap.add incl.parents sg.parents
        in
        let elements =
          LMap.fold LMap.add incl.elements sg.elements
        in
        let section_titles =
          LMap.fold LMap.add incl.section_titles sg.section_titles
        in
          {modules; module_types; class_signatures;
           types; parents; elements; section_titles}
    | Functor _ | Generative _ | Abstract | Unresolved -> sg

  let rec modules t =
    match t.body with
    | Expr expr -> modules (Lazy.force expr.expansion)
    | Sig sg ->
        let sg = Lazy.force sg in
          SMap.bindings sg.modules |>
          List.map (fun (x,y) -> ModuleName.of_string x, y)
    | Functor _ | Generative _ | Abstract | Unresolved -> []

  let rec module_types t =
    match t.body with
    | Expr expr -> module_types (Lazy.force expr.expansion)
    | Sig sg ->
        let sg = Lazy.force sg in
          SMap.bindings sg.module_types |>
          List.map (fun (x,y) -> ModuleTypeName.of_string x, y)
    | Functor _ | Generative _ | Abstract | Unresolved -> []

  let path lookup p =
    let term = Path(p, false) in
    let expansion = lazy (lookup p) in
      mkExpr {term; expansion}

  let alias lookup p =
    let term = Alias(p, false) in
    let expansion =
      lazy (
        let ex = lookup p in
        strengthen_module p (Lazy.from_val ex) ex
      )
    in
      mkExpr {term; expansion}

  let signature f x = mkSig (lazy (f x))

  let functor_ equal hash id arg res =
    let equal =
      match equal with
      | None -> None
      | Some _equal -> Some Path.Module.equal
    in
    let hash =
      match hash with
      | None -> None
      | Some _hash -> Some Path.Module.hash
    in
    let cache = make_tbl equal hash 3 in
      mkFunctor {id; arg; res; cache}

  let replace_module name t sg =
    let modules = SMap.map_item name (fun _ -> t) sg.modules in
    let parents =
      LMap.map_item name
        (function
          | `Module _ -> `Module t
          | item -> item)
        sg.parents
    in
      {sg with modules; parents}

  let map_module name f sg =
    let modules = SMap.map_item name f sg.modules in
    let parents =
      LMap.map_item name
        (function
          | `Module t -> `Module (f t)
          | item -> item)
        sg.parents
    in
      {sg with modules; parents}

  let remove_module name sg =
    let modules = SMap.remove name sg.modules in
    let parents =
      LMap.filter_item name
        (function
          | `Module _ -> true
          | _ -> false)
        sg.parents
    in
    let elements =
      LMap.filter_item name
        (function `Module _ -> false | _ -> true) sg.elements
    in
      {sg with modules; parents; elements}

  let remove_datatype name sg =
    let types = SMap.filter_item name ((<>) `Type) sg.types in
    let parents =
      LMap.filter_item name
        (function
          | `Datatype _ -> true
          | _ -> false)
        sg.parents
    in
    let elements = LMap.filter_item name ((<>) `Type) sg.elements in
      {sg with types; parents; elements}

  let rec with_module frag eq t =
    match t.body with
    | Expr expr ->
        let term = WithModule(expr, frag, eq) in
        let expansion =
          lazy (with_module frag eq (Lazy.force expr.expansion))
        in
          mkExpr {term; expansion}
    | Sig sg ->
        let sg =
          lazy
            ( let sg = Lazy.force sg in
              let name, frag = Fragment.Module.split frag in
                match frag with
                | None -> replace_module name eq sg
                | Some frag -> map_module name (with_module frag eq) sg )
        in
          mkSig sg
    | Functor _ | Generative _ | Abstract | Unresolved -> t

  let rec with_module_subst frag t =
    match t.body with
    | Expr expr ->
        let term = WithModuleSubst(expr, frag) in
        let expansion =
          lazy (with_module_subst frag (Lazy.force expr.expansion))
        in
          mkExpr {term; expansion}
    | Sig sg ->
        let sg =
          lazy
            ( let sg = Lazy.force sg in
              let name, frag = Fragment.Module.split frag in
                match frag with
                | None -> remove_module name sg
                | Some frag -> map_module name (with_module_subst frag) sg )
        in
          mkSig sg
    | Functor _ | Generative _ | Abstract | Unresolved -> t

  let rec with_type_subst frag t =
    match t.body with
    | Expr expr ->
        let term = WithTypeSubst(expr, frag) in
        let expansion =
          lazy (with_type_subst frag (Lazy.force expr.expansion))
        in
          mkExpr {term; expansion}
    | Sig sg ->
        let sg =
          lazy
            ( let sg = Lazy.force sg in
              let name, frag = Fragment.Type.split frag in
                match frag with
                | None -> remove_datatype name sg
                | Some frag -> map_module name (with_type_subst frag) sg )
        in
          mkSig sg
    | Functor _ | Generative _ | Abstract | Unresolved -> t

  let module_type_substitution path expansion t =
    match t.body with
    | Abstract ->
        let term = Path(path, true) in
          mkExpr {term; expansion}
    | _ -> t

  let rec module_substitution path expansion t =
    match t.body with
    | Expr _ -> t
    | Sig sg ->
        let sg =
          lazy
            ( let sg = Lazy.force sg in
              let modules =
                SMap.mapi
                  (fun name body ->
                     let path = Path.module_ path (ModuleName.of_string name) in
                     let expansion =
                       lazy (lookup_module name (Lazy.force expansion))
                     in
                       module_substitution path expansion body)
               sg.modules
              in
              let module_types =
                SMap.mapi
                  (fun name body ->
                     let path = Path.module_type path (ModuleTypeName.of_string name) in
                     let expansion =
                       lazy (lookup_module_type name (Lazy.force expansion))
                     in
                       module_type_substitution path expansion body)
                  sg.module_types
              in
                {sg with modules; module_types} )
        in
          mkSig sg
    | Functor fn ->
        let res = module_substitution path expansion fn.res in
        let cache = fn.cache.fresh 3 in
          mkFunctor {fn with res; cache}
    | Generative body ->
        let body = module_substitution path expansion body in
          generative body
    | Abstract ->
        let term = Alias(path, true) in
          mkExpr {term; expansion}
    | Unresolved -> t

  let rec reduce_signature_ident id path = function
      | `Root _ -> None
      | `Module(p, name) -> begin
          match reduce_signature_ident id path p with
          | Some p -> Some (Path.module_ p name)
          | None -> None
        end
      | `Argument _ as id' -> if id = id' then Some path else None
      | `ModuleType _ -> None

  and reduce_module_ident id path (m : Identifier.Module.t) =
    match m with
      | `Root _ -> None
      | `Module(p, name) -> begin
          match reduce_signature_ident id path p with
          | Some p -> Some (Path.module_ p name)
          | None -> None
        end
      | `Argument _ as id' -> if id = id' then Some path else None

  and reduce_resolved_module_path in_arg id path (p : Path.Resolved.Module.t) =
    match p with
    | `Identifier id' ->
        if in_arg then reduce_module_ident id path id' else None
    | `Subst(_, p) ->
        reduce_resolved_module_path in_arg id path p
    | `SubstAlias(_, p) ->
        reduce_resolved_module_path in_arg id path p
    | `Hidden p ->
        reduce_resolved_module_path in_arg id path p
    | `Module(p, name) -> begin
        match reduce_resolved_module_path in_arg id path p with
        | Some p -> Some (Path.module_ p name)
        | None -> None
      end
    | `Canonical (p, _) ->
        reduce_resolved_module_path in_arg id path p
    | `Apply(p, arg) -> begin
        let rp = reduce_resolved_module_path in_arg id path p in
        let rarg = reduce_module_path true id path arg in
          match rp, rarg with
          | None, None -> None
          | None, Some arg -> Some(`Resolved(`Apply(p, arg)))
          | Some p, None -> Some(Path.apply p arg)
          | Some p, Some arg -> Some(Path.apply p arg)
      end

  and reduce_resolved_module_type_path id path (p : Path.Resolved.ModuleType.t) =
    match p with
    | `Identifier _ -> None
    | `ModuleType(p, name) -> begin
        match reduce_resolved_module_path false id path p with
        | Some p -> Some (Path.module_type p name)
        | None -> None
      end

  and reduce_module_path in_arg id path (p : Path.Module.t) =
    match p with
    | `Resolved r -> reduce_resolved_module_path in_arg id path r
    | `Root _ -> None
    | `Forward _ -> None
    | `Dot(p, name) -> begin
        match reduce_module_path in_arg id path p with
        | Some p -> Some (`Dot(p, name))
        | None -> None
      end
    | `Apply(p, arg) -> begin
        let rp = reduce_module_path in_arg id path p in
        let rarg = reduce_module_path true id path arg in
          match rp, rarg with
          | None, None -> None
          | None, Some arg -> Some(`Apply(p, arg))
          | Some p, None -> Some(`Apply(p, arg))
          | Some p, Some arg -> Some(`Apply(p, arg))
      end

  and reduce_module_type_path id path (m : Path.ModuleType.t) =
    match m with
    | `Resolved r -> reduce_resolved_module_type_path id path r
    | `Dot(p, name) -> begin
        match reduce_module_path false id path p with
        | Some p -> Some (`Dot(p, name))
        | None -> None
      end

  let rec subst_signature_ident id lookup path (s : Identifier.Signature.t) =
    match s with
      | `Root _ -> None
      | `Module(p, name) -> begin
          match subst_signature_ident id lookup path p with
          | Some (p, t) ->
              let p = Path.module_ p name in
              let t = lazy (lookup_module (ModuleName.to_string name) (Lazy.force t)) in
                Some (p, t)
          | None -> None
        end
      | `Argument _ as id' ->
          if id = id' then Some (path, lazy (lookup path))
          else None
      | `ModuleType _ -> None

  and subst_module_ident id lookup path (id' : Identifier.Module.t) =
    if id = id' then Some (path, lazy (lookup path))
    else match id' with
      | (`Root _ : Identifier.Module.t) -> None
      | `Module(p, name) -> begin
          match subst_signature_ident id lookup path p with
          | Some (p, t) ->
              let p = Path.module_ p name in
              let t = lazy (lookup_module (ModuleName.to_string name) (Lazy.force t)) in
                Some (p, t)
          | None -> None
        end
      | `Argument _ -> None

  and subst_module_type_ident id lookup (path : Path.Module.t) (id' : Identifier.ModuleType.t) =
      match id' with
      | `ModuleType(p, name) -> begin
          match subst_signature_ident id lookup path p with
          | Some (p, t) ->
              let p = Path.module_type p name in
              let t = lazy (lookup_module_type (ModuleTypeName.to_string name) (Lazy.force t)) in
                Some (p, t)
          | None -> None
        end

  and subst_resolved_module_path id lookup path (p : Path.Resolved.Module.t) =
    match p with
    | `Identifier id' -> subst_module_ident id lookup path id'
    | `Subst(_, p) -> subst_resolved_module_path id lookup path p
    | `SubstAlias(sub, _) -> subst_resolved_module_path id lookup path sub
    | `Hidden p -> subst_resolved_module_path id lookup path p
    | `Module(p, name) -> begin
        match subst_resolved_module_path id lookup path p with
        | Some (p, t) ->
            let p = Path.module_ p name in
            let t = lazy (lookup_module (ModuleName.to_string name) (Lazy.force t)) in
              Some (p, t)
        | None -> None
      end
    | `Canonical (p, _) -> subst_resolved_module_path id lookup path p
    | `Apply(p, arg) -> begin
        match subst_resolved_module_path id lookup path p with
        | Some (p, t) ->
            let p = Path.apply p arg in
            let t = lazy (lookup_apply lookup arg (Lazy.force t)) in
              Some (p, t)
        | None -> None
      end

  and subst_resolved_module_type_path id lookup (path : Path.Module.t) (p : Path.Resolved.ModuleType.t) =
    match p with
    | `Identifier id' -> subst_module_type_ident id lookup path id'
    | `ModuleType(p, name) -> begin
        match subst_resolved_module_path id lookup path p with
        | Some (p, t) ->
            let p = Path.module_type p name in
            let t = lazy (lookup_module_type (ModuleTypeName.to_string name) (Lazy.force t)) in
              Some (p, t)
        | None -> None
      end

  and subst_module_path id lookup path (p : Path.Module.t) =
    match p with
    | `Resolved r -> subst_resolved_module_path id lookup path r
    | `Root _ -> None
    | `Forward _ -> None
    | `Dot(p, name) -> begin
        match subst_module_path id lookup path p with
        | Some (p, t) ->
            let p = `Dot(p, name) in
            let t = lazy (lookup_module name (Lazy.force t)) in
              Some (p, t)
        | None -> None
      end
    | `Apply(p, arg) -> begin
        match subst_module_path id lookup path p with
        | Some (p, t) ->
            let p = `Apply(p, arg) in
            let t = lazy (lookup_apply lookup arg (Lazy.force t)) in
              Some (p, t)
        | None -> None
      end

  and subst_module_type_path id lookup (path : Path.Module.t) (p : Path.ModuleType.t) =
    match p with
    | `Resolved r -> subst_resolved_module_type_path id lookup path r
    | `Dot(p, name) -> begin
        match subst_module_path id lookup path p with
        | Some (p, t) ->
            let p = `Dot(p, name) in
            let t = lazy (lookup_module_type name (Lazy.force t)) in
              Some (p, t)
        | None -> None
      end

  and subst_expr id lookup path expr =
    match expr.term with
    | Path(p, sub) -> begin
        let p =
          match reduce_module_type_path id path p with
          | None -> p
          | Some p -> p
        in
          match subst_module_type_path id lookup path p with
          | None ->
              let term = Path(p, sub) in
              let expansion =
                lazy (subst id lookup path (Lazy.force expr.expansion))
              in
                {term; expansion}
          | Some (p, t) ->
              let term = Path(p, sub) in
              let expansion =
                lazy (module_type_substitution p t
                        (subst id lookup path (Lazy.force expr.expansion)) )
              in
                {term; expansion}
      end
    | Alias(p, sub) -> begin
        let p =
          match reduce_module_path false id path p with
          | None -> p
          | Some p -> p
        in
          match subst_module_path id lookup path p with
          | None ->
              let term = Alias(p, sub) in
              let expansion =
                lazy (subst id lookup path (Lazy.force expr.expansion))
              in
                {term; expansion}
          | Some (p, t) ->
              let term = Alias(p, sub) in
              let expansion =
                lazy (module_substitution p t
                        (subst id lookup path (Lazy.force expr.expansion)) )
              in
                {term; expansion}
      end
    | WithModule(body, frag, eq) ->
        let body = subst_expr id lookup path body in
        let eq = subst id lookup path eq in
        let term = WithModule(body, frag, eq) in
        let expansion =
          lazy (with_module frag eq (Lazy.force body.expansion))
        in
          {term; expansion}
    | WithModuleSubst(body, frag) ->
        let body = subst_expr id lookup path body in
        let term = WithModuleSubst(body, frag) in
        let expansion =
          lazy (with_module_subst frag (Lazy.force body.expansion))
        in
          {term; expansion}
    | WithTypeSubst(body, frag) ->
        let body = subst_expr id lookup path body in
        let term = WithTypeSubst(body, frag) in
        let expansion =
          lazy (with_type_subst frag (Lazy.force body.expansion))
        in
          {term; expansion}

  and subst id lookup path t =
    match t.body with
    | Expr expr -> mkExpr (subst_expr id lookup path expr)
    | Sig sg ->
        let sg =
          lazy
            ( let sg = Lazy.force sg in
              let modules =
                SMap.map (subst id lookup path) sg.modules
              in
              let module_types =
                SMap.map (subst id lookup path) sg.module_types
              in
                {sg with modules; module_types} )
        in
          mkSig sg
    | Functor fn ->
        let arg = subst id lookup path fn.arg in
        let res = subst id lookup path fn.res in
        let cache = fn.cache.fresh 3 in
          mkFunctor {id = fn.id; arg; res; cache}
    | Generative t -> generative (subst id lookup path t)
    | Abstract -> abstract
    | Unresolved -> unresolved

  and lookup_apply : (Paths_types.Path.module_ -> t) ->
           Paths_types.Path.module_ -> t -> t = fun lookup arg t ->
    match t.body with
    | Expr expr -> lookup_apply lookup arg (Lazy.force expr.expansion)
    | Sig _ -> unresolved
    | Functor fn -> begin
        try
          fn.cache.find arg
        with Not_found ->
          let res = subst fn.id lookup arg fn.res in
            fn.cache.add arg res;
            res
      end
    | Generative _ -> unresolved
    | Abstract -> unresolved
    | Unresolved -> unresolved

  let rec find_parent_apply : (Paths_types.Path.module_ -> t) ->
           Paths_types.Path.module_ -> t -> Parent.module_ = fun lookup arg t ->
    match t.body with
    | Expr expr -> begin
        match expr.term with
        | Path(_, true) | Alias(_, true) -> raise Not_found
        | Alias(_, false) -> begin
            let t = Lazy.force expr.expansion in
            match t.hidden with
            | false -> raise Not_found
            | true -> find_parent_apply lookup arg t
          end
        | _ -> find_parent_apply lookup arg (Lazy.force expr.expansion)
      end
    | Sig _ -> raise Not_found
    | Functor fn -> begin
        try
          `Module (fn.cache.find arg)
        with Not_found ->
          let res = subst fn.id lookup arg fn.res in
            fn.cache.add arg res;
            `Module res
      end
    | Generative _ -> raise Not_found
    | Abstract -> raise Not_found
    | Unresolved -> raise Not_found

end

and Datatype : sig

  type t

  val find_constructor_element : string -> t -> Element.datatype_constructor

  val find_field_element : string -> t -> Element.datatype_field

  val find_label_element : string -> t -> Element.datatype_label

  val find_element : string -> t -> Element.datatype

  val add_documentation : Odoc_model.Comment.docs -> t -> t

  val abstract : t

  val variant : string -> string list -> t

  val record : string -> string list -> t

  val extensible : t

  val unresolved : t

  val elements : t -> Element.datatype LMap.t

end = struct

  type variant =
    { type_name: string;
      constructors: SSet.t;
      labels: SSet.t; }

  type record =
    { type_name: string;
      fields: SSet.t;
      labels: SSet.t; }

  type t =
    | Variant of variant
    | Record of record
    | Unresolved

  let find_constructor_element name = function
    | Variant v ->
        if SSet.mem name v.constructors then `Constructor v.type_name
        else raise Not_found
    | _ -> raise Not_found

  let find_field_element name = function
    | Record r ->
        if SSet.mem name r.fields then `Field r.type_name
        else raise Not_found
    | _ -> raise Not_found

  let find_label_element name = function
    | Variant v ->
        if SSet.mem name v.labels then `Label (Some v.type_name)
        else raise Not_found
    | Record r ->
        if SSet.mem name r.labels then `Label (Some r.type_name)
        else raise Not_found
    | _ -> raise Not_found

  let find_element name = function
    | Variant v ->
        if SSet.mem name v.constructors then `Constructor v.type_name
        else if SSet.mem name v.labels then `Label (Some v.type_name)
        else raise Not_found
    | Record r ->
        if SSet.mem name r.fields then `Field r.type_name
        else if SSet.mem name r.labels then `Label (Some r.type_name)
        else raise Not_found
    | _ -> raise Not_found

  let add_documentation doc = function
    | Variant v ->
        let lbls = documentation_labels [] doc in
        let labels =
          List.fold_right (fun (lbl, _) map -> SSet.add lbl map) lbls v.labels
        in
          Variant {v with labels}
    | Record r ->
        let lbls = documentation_labels [] doc in
        let labels =
          List.fold_right (fun (lbl, _) map -> SSet.add lbl map) lbls r.labels
        in
          Record {r with labels}
    | Unresolved -> Unresolved

  let abstract = Unresolved

  let variant type_name constructors =
    let constructors =
        List.fold_right SSet.add constructors SSet.empty
    in
    let labels = SSet.empty in
    let variant = {type_name; constructors; labels} in
      Variant variant

  let record type_name fields =
    let fields =
        List.fold_right SSet.add fields SSet.empty
    in
    let labels = SSet.empty in
    let record = {type_name; fields; labels} in
      Record record

  let extensible = Unresolved

  let unresolved = Unresolved

  let elements = function
    | Variant v ->
        let elements =
          SSet.fold
            (fun name acc ->
               LMap.add name (`Constructor v.type_name) acc)
              v.constructors LMap.empty
        in
        let elements =
          SSet.fold
            (fun name acc ->
               LMap.add name (`Label (Some v.type_name)) acc)
            v.labels elements
        in
          elements
      | Record r ->
          let elements =
            SSet.fold
              (fun name acc ->
                 LMap.add name (`Field r.type_name) acc)
              r.fields LMap.empty
          in
          let elements =
            SSet.fold
              (fun name acc ->
                 LMap.add name (`Label (Some r.type_name)) acc)
              r.labels elements
          in
            elements
      | Unresolved -> LMap.empty

end

and ClassSig : sig

  type t

  val find_method_element : string -> t -> Element.class_signature_method

  val find_instance_variable_element : string -> t ->
        Element.class_signature_instance_variable

  val find_label_element : string -> t -> Element.class_signature_label

  val find_element : string -> t -> Element.class_signature

  type signature

  val empty : signature

  val add_element : string -> Element.class_signature ->
    signature -> signature

  val add_documentation : Odoc_model.Comment.docs -> signature -> signature

  val add_comment : Odoc_model.Comment.docs_or_stop -> signature -> signature

  val inherit_ : t -> signature -> signature

  val constr : (Path.ClassType.t -> t) -> Path.ClassType.t -> t

  val signature : ('b -> signature) -> 'b -> t

  val unresolved : t

end = struct

  type signature = Element.class_signature LMap.t

  type desc =
    | Sig of signature
    | Unresolved

  type t = desc Lazy.t

  let find_method_element name t =
    let desc = Lazy.force t in
      match desc with
      | Sig csig ->
          LMap.map_find name
            (function
              | `Method as x -> Some x
              | _ -> None)
            csig
      | Unresolved -> raise Not_found

  let find_instance_variable_element name t =
    let desc = Lazy.force t in
      match desc with
      | Sig csig ->
          LMap.map_find name
            (function
              | `InstanceVariable as x -> Some x
              | _ -> None)
            csig
      | Unresolved -> raise Not_found

  let find_label_element name t =
    let desc = Lazy.force t in
      match desc with
      | Sig csig ->
          LMap.map_find name
            (function
              | `Label _ as x -> Some x
              | _ -> None)
            csig
      | Unresolved -> raise Not_found

  let find_element name t =
    let desc = Lazy.force t in
      match desc with
      | Sig csig ->
          LMap.find_name name csig
      | Unresolved -> raise Not_found

  let empty = LMap.empty

  let add_element name element csig = LMap.add name element csig

  let add_documentation doc csig =
    let labels = documentation_labels [] doc in
    let add_label csig (label, _) = add_element label (`Label None) csig in
      List.fold_left add_label csig labels

  let add_comment comment sg =
    let labels = comment_labels [] comment in
    let add_label sg (label, _) = add_element label (`Label None) sg in
      List.fold_left add_label sg labels

  let inherit_ t csig =
    let desc = Lazy.force t in
      match desc with
      | Sig inhr -> LMap.fold LMap.add inhr csig
      | Unresolved -> csig

  let constr f x = lazy (Lazy.force (f x))

  let signature f x = lazy (Sig (f x))

  let unresolved = lazy Unresolved

end

and Page : sig

  type t

  val find_label_element : string -> t -> Element.page_label

  val find_section_title : string -> t -> Odoc_model.Comment.link_content

  val of_doc : Odoc_model.Comment.docs -> t

end = struct

  type t = {
    labels : Element.page_label LMap.t;
    section_titles : Odoc_model.Comment.link_content SMap.t;
  }

  let find_label_element name t =
    LMap.find_name name t.labels

  let find_section_title name t =
    SMap.find name t.section_titles

  let of_doc doc =
    let labels = documentation_labels [] doc in
    let add_label t (label, txt) =
      let labels = LMap.add label (`Label None) t.labels in
      let section_titles = SMap.add label txt t.section_titles in
      {labels; section_titles}
    in
    List.fold_left add_label
      { labels = LMap.empty; section_titles = SMap.empty } labels

end

and Parent : sig

  type t = [
    | `Module of Sig.t
    | `ModuleType of Sig.t
    | `Datatype of Datatype.t
    | `Class of ClassSig.t
    | `ClassType of ClassSig.t
  ]

  type signature = [
    | `Module of Sig.t
    | `ModuleType of Sig.t
  ]

  type class_signature = [
    | `Class of ClassSig.t
    | `ClassType of ClassSig.t
  ]


  type datatype = [
    | `Datatype of Datatype.t
  ]

  type module_ = [
    | `Module of Sig.t
  ]

  type module_type = [
    | `ModuleType of Sig.t
  ]

  type sig_or_type = [
    | `Module of Sig.t
    | `ModuleType of Sig.t
    | `Datatype of Datatype.t
  ]

  type any = t

  type subst =
    | Subst of Path.ModuleType.t
    | SubstAlias of Path.Module.t

end = Parent

and Element : sig

  type mod_t = { canonical : (Path.Module.t * Reference.Module.t) option
        ; hidden : bool }

  type s_module = [
    | `Module of mod_t
  ]

  type s_module_type = [
    | `ModuleType
  ]

  type s_type = [
    | `Type
  ]

  type s_constructor = [
    | `Constructor of string
  ]

  type s_field = [
    | `Field of string
  ]

  type s_extension = [
    | `Extension
  ]

  type s_exception = [
    | `Exception
  ]

  type s_value = [
    | `Value
  ]

  type s_class = [
    | `Class
  ]

  type s_class_type = [
    | `ClassType
  ]

  type s_method = [
    | `Method
  ]

  type s_instance_variable = [
    | `InstanceVariable
  ]

  type s_label = [
    | `Label of string option
  ]

  type t = [
    | s_module | s_module_type | s_type | s_constructor
    | s_field | s_extension | s_exception | s_value | s_class
    | s_class_type | s_method | s_instance_variable | s_label
  ]

  type signature_module = s_module

  type signature_module_type = s_module_type

  type signature_type = [ s_type | s_class | s_class_type ]

  type signature_constructor = [s_constructor | s_extension | s_exception]

  type signature_field = s_field

  type signature_extension = [s_extension | s_exception ]

  type signature_exception = s_exception

  type signature_value = s_value
  type signature_class = s_class

  type signature_class_type = [ s_class | s_class_type ]

  type signature_label = s_label

  type signature =
    [ s_module | s_module_type | s_type
         | s_constructor | s_field | s_extension
         | s_exception | s_value | s_class | s_class_type | s_label ]

  type datatype_constructor = s_constructor

  type datatype_field = s_field

  type datatype_label = s_label

  type datatype = [ s_constructor | s_field | s_label]

  type class_signature_method = s_method

  type class_signature_instance_variable = s_instance_variable

  type class_signature_label = s_label

  type class_signature = [ s_method | s_instance_variable | s_label ]

  type page_label = s_label

end = Element
OCaml

Innovation. Community. Security.