Source file short_paths_graph.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
module String_map = Misc.String.Map
module Ident = struct
type t = Ident.t
let equal t1 t2 = Ident.equal t1 t2
let compare t1 t2 = Ident.compare t1 t2
let name = Ident.name
let global name =
Ident.create_persistent name
end
module Ident_map = Map.Make(Ident)
module Ident_set = Set.Make(Ident)
module Path = struct
type t = Path.t =
| Pident of Ident.t
| Pdot of t * string
| Papply of t * t
let equal t1 t2 = Path.same t1 t2
let compare t1 t2 = Path.compare t1 t2
end
module Path_map = Map.Make(Path)
module Path_set = Set.Make(Path)
module Desc = struct
type deprecated =
| Deprecated
| Not_deprecated
module Type = struct
type t =
| Fresh
| Nth of int
| Subst of Path.t * int list
| Alias of Path.t
end
module Module_type = struct
type t =
| Fresh
| Alias of Path.t
end
module Class_type = struct
type t =
| Fresh
| Subst of Path.t * int list
| Alias of Path.t
end
module Module = struct
type component =
| Type of string * Type.t * deprecated
| Class_type of string * Class_type.t * deprecated
| Module_type of string * Module_type.t * deprecated
| Module of string * t * deprecated
and components = component list
and kind =
| Signature of components Lazy.t
| Functor of (Path.t -> t)
and t =
| Fresh of kind
| Alias of Path.t
end
type source =
| Local
| Open
type t =
| Type of Ident.t * Type.t * source * deprecated
| Class_type of Ident.t * Class_type.t * source * deprecated
| Module_type of Ident.t * Module_type.t * source * deprecated
| Module of Ident.t * Module.t * source * deprecated
| Declare_type of Ident.t
| Declare_class_type of Ident.t
| Declare_module_type of Ident.t
| Declare_module of Ident.t
end
module Sort = struct
type t =
| Defined
| Declared of Ident_set.t
let application t1 t2 =
match t1, t2 with
| Defined, Defined -> Defined
| Defined, Declared _ -> t2
| Declared _, Defined -> t1
| Declared ids1, Declared ids2 -> Declared (Ident_set.union ids1 ids2)
end
module Age = Natural.Make()
module Dependency = Natural.Make()
module Origin = struct
type t =
| Dependency of Dependency.t
| Dependencies of Dependency.t list
| Environment of Age.t
let rec deps_add dep deps =
match deps with
| [] -> [dep]
| dep' :: rest ->
if Dependency.equal dep dep' then
deps
else if Dependency.less_than dep dep' then
dep :: deps
else
dep' :: deps_add dep rest
let rec deps_union deps1 deps2 =
match deps1, deps2 with
| [], _ -> deps2
| _, [] -> deps1
| dep1 :: rest1, dep2 :: rest2 ->
if Dependency.equal dep1 dep2 then
dep1 :: deps_union rest1 rest2
else if Dependency.less_than dep1 dep2 then
dep1 :: deps_union rest1 deps2
else
dep2 :: deps_union deps1 rest2
let rec deps_equal deps1 deps2 =
match deps1, deps2 with
| [], [] -> true
| [], _ :: _ -> false
| _ :: _, [] -> false
| dep1 :: rest1, dep2 :: rest2 ->
Dependency.equal dep1 dep2
&& deps_equal rest1 rest2
let application t1 t2 =
match t1, t2 with
| Dependency dep1, Dependency dep2 ->
if Dependency.equal dep1 dep2 then t1
else if Dependency.less_than dep1 dep2 then
Dependencies [dep1; dep2]
else
Dependencies [dep2; dep1]
| Dependency dep1, Dependencies deps2 ->
Dependencies (deps_add dep1 deps2)
| Dependency _, Environment _ -> t2
| Dependencies deps1, Dependency dep2 ->
Dependencies (deps_add dep2 deps1)
| Dependencies deps1, Dependencies deps2 ->
Dependencies (deps_union deps1 deps2)
| Dependencies _, Environment _ -> t2
| Environment _, Dependency _ -> t1
| Environment _, Dependencies _ -> t1
| Environment age1, Environment age2 ->
Environment (Age.max age1 age2)
let equal t1 t2 =
match t1, t2 with
| Dependency dep1, Dependency dep2 -> Dependency.equal dep1 dep2
| Dependency _, Dependencies _ -> false
| Dependency _, Environment _ -> false
| Dependencies _, Dependency _ -> false
| Dependencies deps1, Dependencies deps2 -> deps_equal deps1 deps2
| Dependencies _, Environment _ -> false
| Environment _, Dependency _ -> false
| Environment _, Dependencies _ -> false
| Environment env1, Environment env2 -> Age.equal env1 env2
let hash = Hashtbl.hash
end
let hidden_name name =
if name <> "" && name.[0] = '_' then true
else
try
for i = 1 to String.length name - 2 do
if name.[i] = '_' && name.[i + 1] = '_' then
raise Exit
done;
false
with Exit -> true
let hidden_ident id =
hidden_name (Ident.name id)
let hidden_definition deprecated name =
match deprecated with
| Desc.Deprecated -> true
| Desc.Not_deprecated -> hidden_name name
let hidden_base_definition deprecated id =
match deprecated with
| Desc.Deprecated -> true
| Desc.Not_deprecated -> hidden_ident id
module rec Type : sig
type t
val base : Origin.t -> Ident.t -> Desc.Type.t option -> Desc.deprecated -> t
val child :
Module.normalized -> string -> Desc.Type.t option -> Desc.deprecated -> t
val declare : Origin.t -> Ident.t -> t
val declaration : t -> Origin.t option
val origin : Graph.t -> t -> Origin.t
val path : Graph.t -> t -> Path.t
val hidden : t -> bool
val sort : Graph.t -> t -> Sort.t
type resolved =
| Nth of int
| Path of int list option * t
val resolve : Graph.t -> t -> resolved
end = struct
open Desc.Type
type definition =
| Alias of Path.t
| Defined
| Nth of int
| Subst of Path.t * int list
| Unknown
type t =
| Declaration of
{ origin : Origin.t;
id : Ident.t;
hidden : bool; }
| Definition of
{ origin : Origin.t;
path : Path.t;
hidden : bool;
sort : Sort.t;
definition : definition; }
let definition_of_desc (desc : Desc.Type.t option) =
match desc with
| None -> Unknown
| Some Fresh -> Defined
| Some (Nth n) -> Nth n
| Some (Subst(p, ns)) -> Subst(p, ns)
| Some (Alias alias) -> Alias alias
let base origin id desc deprecated =
let path = Path.Pident id in
let hidden = hidden_base_definition deprecated id in
let sort = Sort.Defined in
let definition = definition_of_desc desc in
Definition { origin; path; hidden; sort; definition }
let child md name desc deprecated =
let origin = Module.raw_origin md in
let sort = Module.raw_sort md in
let path = Path.Pdot(Module.raw_path md, name) in
let hidden = hidden_definition deprecated name in
let definition = definition_of_desc desc in
Definition { origin; path; hidden; sort; definition }
let declare origin id =
let hidden = hidden_ident id in
Declaration { origin; id; hidden }
let declaration t =
match t with
| Definition _ -> None
| Declaration { origin; _} -> Some origin
let hidden t =
match t with
| Definition { hidden; _ } -> hidden
| Declaration { hidden; _ } -> hidden
let raw_origin t =
match t with
| Declaration { origin; _ }
| Definition { origin; _ } -> origin
let raw_path t =
match t with
| Declaration { id; _ } -> Path.Pident id
| Definition { path; _ } -> path
let raw_sort t =
match t with
| Declaration { id; _ } -> Sort.Declared (Ident_set.singleton id)
| Definition { sort; _ } -> sort
let rec normalize_loop root t =
match t with
| Declaration _ -> t
| Definition { definition = Defined | Unknown | Nth _ | Subst _ } -> t
| Definition ({ definition = Alias alias } as r) -> begin
match Graph.find_type root alias with
| exception Not_found -> Definition { r with definition = Unknown }
| aliased -> normalize_loop root aliased
end
let normalize root t =
match t with
| Definition { sort = Sort.Defined } -> normalize_loop root t
| Definition { sort = Sort.Declared _ } | Declaration _ ->
match Graph.find_type root (raw_path t) with
| exception Not_found -> normalize_loop root t
| t -> normalize_loop root t
let origin root t =
raw_origin (normalize root t)
let path root t =
raw_path (normalize root t)
let sort root t =
raw_sort (normalize root t)
type resolved =
| Nth of int
| Path of int list option * t
let subst ns = function
| Nth n -> Nth (List.nth ns n)
| Path(None, p) -> Path(Some ns, p)
| Path(Some ms, p) -> Path(Some (List.map (List.nth ns) ms), p)
let rec resolve root t =
match normalize root t with
| Declaration _ -> Path(None, t)
| Definition { definition = Defined | Unknown } -> Path(None, t)
| Definition { definition = Nth n } -> Nth n
| Definition { definition = Subst(p, ns) } -> begin
match Graph.find_type root p with
| exception Not_found -> Path(None, t)
| aliased -> subst ns (resolve root aliased)
end
| Definition { definition = Alias _ } -> assert false
end
and Class_type : sig
type t
val base :
Origin.t -> Ident.t -> Desc.Class_type.t option -> Desc.deprecated -> t
val child :
Module.normalized -> string ->
Desc.Class_type.t option -> Desc.deprecated -> t
val declare : Origin.t -> Ident.t -> t
val declaration : t -> Origin.t option
val origin : Graph.t -> t -> Origin.t
val path : Graph.t -> t -> Path.t
val hidden : t -> bool
val sort : Graph.t -> t -> Sort.t
type resolved = int list option * t
val resolve : Graph.t -> t -> resolved
end = struct
open Desc.Class_type
type definition =
| Alias of Path.t
| Defined
| Subst of Path.t * int list
| Unknown
type t =
| Declaration of
{ origin : Origin.t;
id : Ident.t;
hidden : bool; }
| Definition of
{ origin : Origin.t;
path : Path.t;
hidden : bool;
sort : Sort.t;
definition : definition; }
let definition_of_desc (desc : Desc.Class_type.t option) =
match desc with
| None -> Unknown
| Some Fresh -> Defined
| Some (Subst(p, ns)) -> Subst(p, ns)
| Some (Alias alias) -> Alias alias
let base origin id desc deprecated =
let path = Path.Pident id in
let hidden = hidden_base_definition deprecated id in
let sort = Sort.Defined in
let definition = definition_of_desc desc in
Definition { origin; path; hidden; sort; definition }
let child md name desc deprecated =
let origin = Module.raw_origin md in
let sort = Module.raw_sort md in
let path = Path.Pdot(Module.raw_path md, name) in
let hidden = hidden_definition deprecated name in
let definition = definition_of_desc desc in
Definition { origin; path; hidden; sort; definition }
let declare origin id =
let hidden = hidden_ident id in
Declaration { origin; id; hidden }
let declaration t =
match t with
| Definition _ -> None
| Declaration { origin; _} -> Some origin
let hidden t =
match t with
| Definition { hidden; _ } -> hidden
| Declaration { hidden; _ } -> hidden
let raw_origin t =
match t with
| Declaration { origin; _ }
| Definition { origin; _ } -> origin
let raw_path t =
match t with
| Declaration { id; _ } -> Path.Pident id
| Definition { path; _ } -> path
let raw_sort t =
match t with
| Declaration { id; _ } -> Sort.Declared (Ident_set.singleton id)
| Definition { sort; _ } -> sort
let rec normalize_loop root t =
match t with
| Declaration _ -> t
| Definition { definition = Defined | Unknown | Subst _ } -> t
| Definition ({ definition = Alias alias } as r) -> begin
match Graph.find_class_type root alias with
| exception Not_found -> Definition { r with definition = Unknown }
| aliased -> normalize_loop root aliased
end
let normalize root t =
match t with
| Definition { sort = Sort.Defined } -> normalize_loop root t
| Definition { sort = Sort.Declared _ } | Declaration _ ->
match Graph.find_class_type root (raw_path t) with
| exception Not_found -> normalize_loop root t
| t -> normalize_loop root t
let origin root t =
raw_origin (normalize root t)
let path root t =
raw_path (normalize root t)
let sort root t =
raw_sort (normalize root t)
type resolved = int list option * t
let subst ns = function
| (None, p) -> (Some ns, p)
| (Some ms, p) -> (Some (List.map (List.nth ns) ms), p)
let rec resolve root t =
match normalize root t with
| Declaration _ -> (None, t)
| Definition { definition = Defined | Unknown } -> (None, t)
| Definition { definition = Subst(p, ns) } -> begin
match Graph.find_class_type root p with
| exception Not_found -> (None, t)
| aliased -> subst ns (resolve root aliased)
end
| Definition { definition = Alias _ } -> assert false
end
and Module_type : sig
type t
val base :
Origin.t -> Ident.t -> Desc.Module_type.t option -> Desc.deprecated -> t
val child :
Module.normalized -> string ->
Desc.Module_type.t option -> Desc.deprecated -> t
val declare : Origin.t -> Ident.t -> t
val declaration : t -> Origin.t option
val origin : Graph.t -> t -> Origin.t
val path : Graph.t -> t -> Path.t
val hidden : t -> bool
val sort : Graph.t -> t -> Sort.t
end = struct
open Desc.Module_type
type definition =
| Alias of Path.t
| Defined
| Unknown
type t =
| Declaration of
{ origin : Origin.t;
id : Ident.t;
hidden : bool; }
| Definition of
{ origin : Origin.t;
path : Path.t;
hidden : bool;
sort : Sort.t;
definition : definition; }
let base origin id desc deprecated =
let path = Path.Pident id in
let hidden = hidden_base_definition deprecated id in
let sort = Sort.Defined in
let definition =
match desc with
| None -> Unknown
| Some Fresh -> Defined
| Some (Alias alias) -> Alias alias
in
Definition { origin; path; hidden; sort; definition }
let child md name desc deprecated =
let origin = Module.raw_origin md in
let sort = Module.raw_sort md in
let path = Path.Pdot (Module.raw_path md, name) in
let hidden = hidden_definition deprecated name in
let definition =
match desc with
| None -> Unknown
| Some Fresh -> Defined
| Some (Alias alias) -> Alias alias
in
Definition { origin; path; hidden; sort; definition }
let declare origin id =
let hidden = hidden_ident id in
Declaration { origin; id; hidden }
let declaration t =
match t with
| Definition _ -> None
| Declaration { origin; _} -> Some origin
let hidden t =
match t with
| Definition { hidden; _ } -> hidden
| Declaration { hidden; _ } -> hidden
let raw_origin t =
match t with
| Declaration { origin; _ } | Definition { origin; _ } ->
origin
let raw_path t =
match t with
| Declaration { id; _ } -> Path.Pident id
| Definition { path; _ } -> path
let raw_sort t =
match t with
| Declaration { id; _ } -> Sort.Declared (Ident_set.singleton id)
| Definition { sort; _ } -> sort
let rec normalize_loop root t =
match t with
| Declaration _ -> t
| Definition { definition = Defined | Unknown } -> t
| Definition ({ definition = Alias alias } as r) -> begin
match Graph.find_module_type root alias with
| exception Not_found -> Definition { r with definition = Unknown }
| aliased -> normalize_loop root aliased
end
let normalize root t =
match t with
| Definition { sort = Sort.Defined } -> normalize_loop root t
| Definition { sort = Sort.Declared _ } | Declaration _ ->
match Graph.find_module_type root (raw_path t) with
| exception Not_found -> normalize_loop root t
| t -> normalize_loop root t
let origin root t =
raw_origin (normalize root t)
let path root t =
raw_path (normalize root t)
let sort root t =
raw_sort (normalize root t)
end
and Module : sig
type t
type normalized
val base :
Origin.t -> Ident.t -> Desc.Module.t option -> Desc.deprecated -> t
val child :
normalized -> string -> Desc.Module.t option -> Desc.deprecated -> t
val application : normalized -> t -> Desc.Module.t option -> t
val declare : Origin.t -> Ident.t -> t
val declaration : t -> Origin.t option
val origin : Graph.t -> t -> Origin.t
val path : Graph.t -> t -> Path.t
val hidden : t -> bool
val sort : Graph.t -> t -> Sort.t
val types : Graph.t -> t -> Type.t String_map.t option
val class_types : Graph.t -> t -> Class_type.t String_map.t option
val module_types : Graph.t -> t -> Module_type.t String_map.t option
val modules : Graph.t -> t -> Module.t String_map.t option
val find_type : Graph.t -> t -> string -> Type.t
val find_class_type : Graph.t -> t -> string -> Class_type.t
val find_module_type : Graph.t -> t -> string -> Module_type.t
val find_module : Graph.t -> t -> string -> Module.t
val find_application : Graph.t -> t -> Path.t -> Module.t
val normalize : Graph.t -> t -> normalized
val unnormalize : normalized -> t
val raw_origin : normalized -> Origin.t
val raw_path : normalized -> Path.t
val raw_sort : normalized -> Sort.t
end = struct
open Desc.Module
type components =
| Unforced of Desc.Module.components Lazy.t
| Forced of
{ types : Type.t String_map.t;
class_types : Class_type.t String_map.t;
module_types : Module_type.t String_map.t;
modules : t String_map.t; }
and definition =
| Alias of Path.t
| Signature of
{ mutable components : components }
| Functor of
{ apply : Path.t -> Desc.Module.t;
mutable applications : t Path_map.t; }
| Unknown
and t =
| Declaration of
{ origin : Origin.t;
id : Ident.t;
hidden : bool; }
| Definition of
{ origin : Origin.t;
path : Path.t;
hidden : bool;
sort : Sort.t;
definition : definition; }
let base origin id desc deprecated =
let path = Path.Pident id in
let hidden = hidden_base_definition deprecated id in
let sort = Sort.Defined in
let definition =
match desc with
| None -> Unknown
| Some (Fresh (Signature components)) ->
let components = Unforced components in
Signature { components }
| Some (Fresh (Functor apply)) ->
let applications = Path_map.empty in
Functor { apply; applications }
| Some (Alias alias) ->
Alias alias
in
Definition { origin; path; hidden; sort; definition }
let child md name desc deprecated =
let origin = Module.raw_origin md in
let sort = Module.raw_sort md in
let path = Path.Pdot(Module.raw_path md, name) in
let hidden = hidden_definition deprecated name in
let definition =
match desc with
| None -> Unknown
| Some (Fresh (Signature components)) ->
let components = Unforced components in
Signature { components }
| Some (Fresh (Functor apply)) ->
let applications = Path_map.empty in
Functor { apply; applications }
| Some (Alias alias) ->
Alias alias
in
Definition { origin; path; hidden; sort; definition }
let application func arg desc =
let func_origin = Module.raw_origin func in
let arg_origin = Module.raw_origin arg in
let origin = Origin.application func_origin arg_origin in
let func_sort = Module.raw_sort func in
let arg_sort = Module.raw_sort arg in
let sort = Sort.application func_sort arg_sort in
let func_path = Module.raw_path func in
let arg_path = Module.raw_path arg in
let path = Path.Papply(func_path, arg_path) in
let hidden = false in
let definition =
match desc with
| None -> Unknown
| Some (Fresh (Signature components)) ->
let components = Unforced components in
Signature { components }
| Some (Fresh (Functor apply)) ->
let applications = Path_map.empty in
Functor { apply; applications }
| Some (Alias alias) ->
Alias alias
in
Definition { origin; path; hidden; sort; definition }
let declare origin id =
let hidden = hidden_ident id in
Declaration { origin; id; hidden }
let declaration t =
match t with
| Definition _ -> None
| Declaration { origin; _} -> Some origin
let hidden t =
match t with
| Definition { hidden; _ } -> hidden
| Declaration { hidden; _ } -> hidden
let raw_origin t =
match t with
| Declaration { origin; _ } | Definition { origin; _ } ->
origin
let raw_path t =
match t with
| Declaration { id; _ } -> Path.Pident id
| Definition { path; _ } -> path
let raw_sort t =
match t with
| Declaration { id; _ } -> Sort.Declared (Ident_set.singleton id)
| Definition { sort; _ } -> sort
type normalized = t
let rec normalize_loop root t =
match t with
| Declaration _ -> t
| Definition { definition = Signature _ | Functor _ | Unknown } -> t
| Definition ({ definition = Alias alias } as r) -> begin
match Graph.find_module root alias with
| exception Not_found -> Definition { r with definition = Unknown }
| aliased -> normalize_loop root aliased
end
let normalize root t =
match t with
| Definition { sort = Sort.Defined } -> normalize_loop root t
| Definition { sort = Sort.Declared _ } | Declaration _ ->
match Graph.find_module root (raw_path t) with
| exception Not_found -> normalize_loop root t
| t -> normalize_loop root t
let unnormalize t = t
let origin root t =
raw_origin (normalize root t)
let path root t =
raw_path (normalize root t)
let sort root t =
raw_sort (normalize root t)
let definition t =
match Module.unnormalize t with
| Declaration _ -> Unknown
| Definition { definition; _ } -> definition
let force root t =
let t = Module.normalize root t in
match definition t with
| Alias _ -> assert false
| Unknown
| Functor _
| Signature { components = Forced _ } -> t
| Signature ({ components = Unforced components; _} as r) -> begin
let rec loop types class_types module_types modules = function
| [] -> Forced { types; class_types; module_types; modules }
| Type(name, desc, dpr) :: rest ->
let typ = Type.child t name (Some desc) dpr in
let types = String_map.add name typ types in
loop types class_types module_types modules rest
| Class_type(name, desc, dpr) :: rest ->
let clty = Class_type.child t name (Some desc) dpr in
let class_types = String_map.add name clty class_types in
loop types class_types module_types modules rest
| Module_type(name, desc, dpr) :: rest ->
let mty = Module_type.child t name (Some desc) dpr in
let module_types = String_map.add name mty module_types in
loop types class_types module_types modules rest
| Module(name, desc, dpr) :: rest ->
let md = Module.child t name (Some desc) dpr in
let modules = String_map.add name md modules in
loop types class_types module_types modules rest
in
let empty = String_map.empty in
let components = loop empty empty empty empty (Lazy.force components) in
r.components <- components;
t
end
let types root t =
let t = force root t in
match definition t with
| Alias _ | Signature { components = Unforced _ } ->
assert false
| Unknown | Functor _ ->
None
| Signature { components = Forced { types; _ }; _ } ->
Some types
let class_types root t =
let t = force root t in
match definition t with
| Alias _ | Signature { components = Unforced _ } ->
assert false
| Unknown | Functor _ ->
None
| Signature { components = Forced { class_types; _ } } ->
Some class_types
let module_types root t =
let t = force root t in
match definition t with
| Alias _ | Signature { components = Unforced _ } ->
assert false
| Unknown | Functor _ ->
None
| Signature { components = Forced { module_types; _ } } ->
Some module_types
let modules root t =
let t = force root t in
match definition t with
| Alias _ | Signature { components = Unforced _ } ->
assert false
| Unknown | Functor _ ->
None
| Signature { components = Forced { modules; _ } } ->
Some modules
let find_type root t name =
let t = force root t in
match definition t with
| Alias _
| Signature { components = Unforced _ } ->
assert false
| Unknown ->
Type.child t name None Not_deprecated
| Functor _ ->
raise Not_found
| Signature { components = Forced { types; _ }; _ } ->
String_map.find name types
let find_class_type root t name =
let t = force root t in
match definition t with
| Alias _
| Signature { components = Unforced _ } ->
assert false
| Unknown ->
Class_type.child t name None Not_deprecated
| Functor _ ->
raise Not_found
| Signature { components = Forced { class_types; _ }; _ } ->
String_map.find name class_types
let find_module_type root t name =
let t = force root t in
match definition t with
| Alias _
| Signature { components = Unforced _ } ->
assert false
| Unknown ->
Module_type.child t name None Not_deprecated
| Functor _ ->
raise Not_found
| Signature { components = Forced { module_types; _ }; _ } ->
String_map.find name module_types
let find_module root t name =
let t = force root t in
match definition t with
| Alias _
| Signature { components = Unforced _ } ->
assert false
| Unknown ->
Module.child t name None Not_deprecated
| Functor _ ->
raise Not_found
| Signature { components = Forced { modules; _ }; _ } ->
String_map.find name modules
let find_application root t path =
let t = Module.normalize root t in
match definition t with
| Alias _ -> assert false
| Signature _ -> raise Not_found
| Unknown ->
let arg = Graph.find_module root path in
Module.application t arg None
| Functor ({ apply; applications } as r)->
let arg = Graph.find_module root path in
let arg_path = Module.path root arg in
match Path_map.find arg_path applications with
| md -> md
| exception Not_found ->
let md = Module.application t arg (Some (apply arg_path)) in
r.applications <- Path_map.add arg_path md applications;
md
end
and Diff : sig
module Item : sig
type t =
| Type of Ident.t * Type.t * Origin.t option
| Class_type of Ident.t * Class_type.t * Origin.t option
| Module_type of Ident.t * Module_type.t * Origin.t option
| Module of Ident.t * Module.t * Origin.t option
val origin : Graph.t -> t -> Origin.t
val id : Graph.t -> t -> Ident.t
val previous : Graph.t -> t -> Origin.t option
end
type t = Item.t list
end = struct
module Item = struct
type t =
| Type of Ident.t * Type.t * Origin.t option
| Class_type of Ident.t * Class_type.t * Origin.t option
| Module_type of Ident.t * Module_type.t * Origin.t option
| Module of Ident.t * Module.t * Origin.t option
let origin root = function
| Type(_, typ, _) -> Type.origin root typ
| Class_type(_, clty, _) -> Class_type.origin root clty
| Module_type(_, mty, _) -> Module_type.origin root mty
| Module(_, md, _) -> Module.origin root md
let id _root = function
| Type(id, _, _) -> id
| Class_type(id, _, _) -> id
| Module_type(id, _, _) -> id
| Module(id, _, _) -> id
let previous _root = function
| Type(_, _, prev) -> prev
| Class_type(_, _, prev) -> prev
| Module_type(_, _, prev) -> prev
| Module(_, _, prev) -> prev
end
type t = Item.t list
end
and Component : sig
type source =
| Global
| Local
| Open
type t =
| Type of
Origin.t * Ident.t * Desc.Type.t * source * Desc.deprecated
| Class_type of
Origin.t * Ident.t * Desc.Class_type.t * source * Desc.deprecated
| Module_type of
Origin.t * Ident.t * Desc.Module_type.t * source * Desc.deprecated
| Module of
Origin.t * Ident.t * Desc.Module.t * source * Desc.deprecated
| Declare_type of Origin.t * Ident.t
| Declare_class_type of Origin.t * Ident.t
| Declare_module_type of Origin.t * Ident.t
| Declare_module of Origin.t * Ident.t
end = Component
and Graph : sig
type t
val empty : t
val add : t -> Component.t list -> t * Diff.t
val merge : t -> Diff.t -> t
val find_type : t -> Path.t -> Type.t
val find_class_type : t -> Path.t -> Class_type.t
val find_module_type : t -> Path.t -> Module_type.t
val find_module : t -> Path.t -> Module.t
val is_type_path_visible : t -> Path.t -> bool
val is_class_type_path_visible : t -> Path.t -> bool
val is_module_type_path_visible : t -> Path.t -> bool
val is_module_path_visible : t -> Path.t -> bool
val is_type_ident_visible : t -> Ident.t -> bool
val is_class_type_ident_visible : t -> Ident.t -> bool
val is_module_type_ident_visible : t -> Ident.t -> bool
val is_module_ident_visible : t -> Ident.t -> bool
end = struct
type defs =
| Global of Ident.t
| Local of Ident.t
| Unambiguous of Ident.t
| Ambiguous of Ident.t * Ident.t list
type t =
{ types : Type.t Ident_map.t;
class_types : Class_type.t Ident_map.t;
module_types : Module_type.t Ident_map.t;
modules : Module.t Ident_map.t;
type_names : defs String_map.t;
class_type_names : defs String_map.t;
module_type_names : defs String_map.t;
module_names : defs String_map.t; }
let empty =
{ types = Ident_map.empty;
class_types = Ident_map.empty;
module_types = Ident_map.empty;
modules = Ident_map.empty;
type_names = String_map.empty;
class_type_names = String_map.empty;
module_type_names = String_map.empty;
module_names = String_map.empty; }
let previous_type t id =
match Ident_map.find id t.types with
| exception Not_found -> None
| prev ->
match Type.declaration prev with
| None -> failwith "Graph.add: type already defined"
| Some _ as o -> o
let previous_class_type t id =
match Ident_map.find id t.class_types with
| exception Not_found -> None
| prev ->
match Class_type.declaration prev with
| None -> failwith "Graph.add: class type already defined"
| Some _ as o -> o
let previous_module_type t id =
match Ident_map.find id t.module_types with
| exception Not_found -> None
| prev ->
match Module_type.declaration prev with
| None -> failwith "Graph.add: module type already defined"
| Some _ as o -> o
let previous_module t id =
match Ident_map.find id t.modules with
| exception Not_found -> None
| prev ->
match Module.declaration prev with
| None -> failwith "Graph.add: module already defined"
| Some _ as o -> o
let add_name source id names =
let name = Ident.name id in
let defs =
match source with
| Component.Global -> Global id
| Component.Local -> Local id
| Component.Open -> begin
match String_map.find name names with
| exception Not_found -> Unambiguous id
| Global id' -> Unambiguous id'
| Local id' -> Ambiguous(id, [id'])
| Unambiguous id' -> Ambiguous(id, [id'])
| Ambiguous(id', ids) -> Ambiguous(id, id' :: ids)
end
in
String_map.add name defs names
let merge_name id names =
let name = Ident.name id in
match String_map.find name names with
| exception Not_found ->
String_map.add name (Global id) names
| _ -> names
let add t descs =
let rec loop acc diff declarations = function
| [] -> loop_declarations acc diff declarations
| Component.Type(origin, id, desc, source, dpr) :: rest ->
let prev = previous_type acc id in
let typ = Type.base origin id (Some desc) dpr in
let types = Ident_map.add id typ acc.types in
let type_names = add_name source id acc.type_names in
let item = Diff.Item.Type(id, typ, prev) in
let diff = item :: diff in
let acc = { acc with types; type_names } in
loop acc diff declarations rest
| Component.Class_type(origin,id, desc, source, dpr) :: rest ->
let prev = previous_class_type acc id in
let clty = Class_type.base origin id (Some desc) dpr in
let class_types = Ident_map.add id clty acc.class_types in
let class_type_names = add_name source id acc.class_type_names in
let item = Diff.Item.Class_type(id, clty, prev) in
let diff = item :: diff in
let acc = { acc with class_types; class_type_names } in
loop acc diff declarations rest
| Component.Module_type(origin,id, desc, source, dpr) :: rest ->
let prev = previous_module_type acc id in
let mty = Module_type.base origin id (Some desc) dpr in
let module_types = Ident_map.add id mty acc.module_types in
let module_type_names = add_name source id acc.module_type_names in
let item = Diff.Item.Module_type(id, mty, prev) in
let diff = item :: diff in
let acc = { acc with module_types; module_type_names } in
loop acc diff declarations rest
| Component.Module(origin,id, desc, source, dpr) :: rest ->
let prev = previous_module acc id in
let md = Module.base origin id (Some desc) dpr in
let modules = Ident_map.add id md acc.modules in
let module_names = add_name source id acc.module_names in
let item = Diff.Item.Module(id, md, prev) in
let diff = item :: diff in
let acc = { acc with modules; module_names } in
loop acc diff declarations rest
| Component.Declare_type(_, id) as decl :: rest ->
let declarations = decl :: declarations in
let type_names =
add_name Component.Global id acc.type_names
in
let acc = { acc with type_names } in
loop acc diff declarations rest
| Component.Declare_class_type(_, id) as decl :: rest ->
let declarations = decl :: declarations in
let class_type_names =
add_name Component.Global id acc.class_type_names
in
let acc = { acc with class_type_names } in
loop acc diff declarations rest
| Component.Declare_module_type(_, id) as decl :: rest ->
let declarations = decl :: declarations in
let module_type_names =
add_name Component.Global id acc.module_type_names
in
let acc = { acc with module_type_names } in
loop acc diff declarations rest
| Component.Declare_module(_, id) as decl :: rest ->
let declarations = decl :: declarations in
let module_names =
add_name Component.Global id acc.module_names
in
let acc = { acc with module_names } in
loop acc diff declarations rest
and loop_declarations acc diff = function
| [] -> acc, diff
| Component.Declare_type(origin, id) :: rest ->
if Ident_map.mem id acc.types then begin
loop_declarations acc diff rest
end else begin
let typ = Type.declare origin id in
let types = Ident_map.add id typ acc.types in
let acc = { acc with types } in
loop_declarations acc diff rest
end
| Component.Declare_class_type(origin, id) :: rest ->
if Ident_map.mem id acc.class_types then begin
loop_declarations acc diff rest
end else begin
let clty = Class_type.declare origin id in
let class_types = Ident_map.add id clty acc.class_types in
let acc = { acc with class_types } in
loop_declarations acc diff rest
end
| Component.Declare_module_type(origin, id) :: rest ->
if Ident_map.mem id acc.module_types then begin
loop_declarations acc diff rest
end else begin
let mty = Module_type.declare origin id in
let module_types = Ident_map.add id mty acc.module_types in
let acc = { acc with module_types } in
loop_declarations acc diff rest
end
| Component.Declare_module(origin, id) :: rest ->
if Ident_map.mem id acc.modules then begin
loop_declarations acc diff rest
end else begin
let md = Module.declare origin id in
let modules = Ident_map.add id md acc.modules in
let acc = { acc with modules } in
loop_declarations acc diff rest
end
| ( Component.Type _
| Component.Class_type _
| Component.Module_type _
| Component.Module _) :: _ -> assert false
in
loop t [] [] descs
let merge t diff =
let rec loop acc = function
| [] -> acc
| Diff.Item.Type(id, typ, _) :: rest ->
let types = Ident_map.add id typ acc.types in
let type_names = merge_name id acc.type_names in
let acc = { acc with types; type_names } in
loop acc rest
| Diff.Item.Class_type(id, clty, _) :: rest ->
let class_types = Ident_map.add id clty acc.class_types in
let class_type_names = merge_name id acc.class_type_names in
let acc = { acc with class_types; class_type_names } in
loop acc rest
| Diff.Item.Module_type(id, mty, _) :: rest ->
let module_types = Ident_map.add id mty acc.module_types in
let module_type_names = merge_name id acc.module_type_names in
let acc = { acc with module_types; module_type_names } in
loop acc rest
| Diff.Item.Module(id, md, _) :: rest ->
let modules = Ident_map.add id md acc.modules in
let module_names = merge_name id acc.module_names in
let acc = { acc with modules; module_names } in
loop acc rest
in
loop t diff
let rec find_module t path =
match path with
| Path.Pident id ->
Ident_map.find id t.modules
| Path.Pdot(p, name) ->
let md = find_module t p in
Module.find_module t md name
| Path.Papply(p, arg) ->
let md = find_module t p in
Module.find_application t md arg
| Path.Pextra_ty _ ->
raise Not_found
let find_type t path =
match path with
| Path.Pident id ->
Ident_map.find id t.types
| Path.Pdot(p, name) ->
let md = find_module t p in
Module.find_type t md name
| Path.Papply _ | Path.Pextra_ty _ ->
raise Not_found
let find_class_type t path =
match path with
| Path.Pident id ->
Ident_map.find id t.class_types
| Path.Pdot(p, name) ->
let md = find_module t p in
Module.find_class_type t md name
| Path.Papply _ | Path.Pextra_ty _ ->
raise Not_found
let find_module_type t path =
match path with
| Path.Pident id ->
Ident_map.find id t.module_types
| Path.Pdot(p, name) ->
let md = find_module t p in
Module.find_module_type t md name
| Path.Papply _ | Path.Pextra_ty _ ->
raise Not_found
let canonical_type_path t id =
match Ident_map.find id t.types with
| exception Not_found -> Path.Pident id
| md -> Type.path t md
let canonical_class_type_path t id =
match Ident_map.find id t.class_types with
| exception Not_found -> Path.Pident id
| md -> Class_type.path t md
let canonical_module_type_path t id =
match Ident_map.find id t.module_types with
| exception Not_found -> Path.Pident id
| md -> Module_type.path t md
let canonical_module_path t id =
match Ident_map.find id t.modules with
| exception Not_found -> Path.Pident id
| md -> Module.path t md
let is_module_ident_visible t id =
let name = Ident.name id in
match String_map.find name t.module_names with
| exception Not_found -> false
| Local id' -> Ident.equal id id'
| Global id' -> Ident.equal id id'
| Unambiguous id' -> Ident.equal id id'
| Ambiguous(id', ids) ->
if not (Ident.equal id id') then false
else begin
let paths = List.map (canonical_module_path t) ids in
let path = canonical_module_path t id in
List.for_all (Path.equal path) paths
end
let rec is_module_path_visible t = function
| Path.Pident id -> is_module_ident_visible t id
| Path.Pdot(path, _) | Pextra_ty (path, _) ->
is_module_path_visible t path
| Path.Papply(path1, path2) ->
is_module_path_visible t path1
&& is_module_path_visible t path2
let is_type_ident_visible t id =
let name = Ident.name id in
match String_map.find name t.type_names with
| exception Not_found -> false
| Local id' -> Ident.equal id id'
| Global id' -> Ident.equal id id'
| Unambiguous id' -> Ident.equal id id'
| Ambiguous(id', ids) ->
if not (Ident.equal id id') then false
else begin
let paths = List.map (canonical_type_path t) ids in
let path = canonical_type_path t id in
List.for_all (Path.equal path) paths
end
let is_type_path_visible t = function
| Path.Pident id -> is_type_ident_visible t id
| Path.Pdot(path, _) | Pextra_ty (path, _) -> is_module_path_visible t path
| Path.Papply _ ->
failwith
"Short_paths_graph.Graph.is_type_path_visible: \
invalid type path"
let is_class_type_ident_visible t id =
let name = Ident.name id in
match String_map.find name t.class_type_names with
| exception Not_found -> false
| Local id' -> Ident.equal id id'
| Global id' -> Ident.equal id id'
| Unambiguous id' -> Ident.equal id id'
| Ambiguous(id', ids) ->
if not (Ident.equal id id') then false
else begin
let paths = List.map (canonical_class_type_path t) ids in
let path = canonical_class_type_path t id in
List.for_all (Path.equal path) paths
end
let is_class_type_path_visible t = function
| Path.Pident id -> is_class_type_ident_visible t id
| Path.Pdot(path, _) -> is_module_path_visible t path
| Path.Papply _ | Path.Pextra_ty _ ->
failwith
"Short_paths_graph.Graph.is_class_type_path_visible: \
invalid class type path"
let is_module_type_ident_visible t id =
let name = Ident.name id in
match String_map.find name t.module_type_names with
| exception Not_found -> false
| Local id' -> Ident.equal id id'
| Global id' -> Ident.equal id id'
| Unambiguous id' -> Ident.equal id id'
| Ambiguous(id', ids) ->
if not (Ident.equal id id') then false
else begin
let paths = List.map (canonical_module_type_path t) ids in
let path = canonical_module_type_path t id in
List.for_all (Path.equal path) paths
end
let is_module_type_path_visible t = function
| Path.Pident id -> is_module_type_ident_visible t id
| Path.Pdot(path, _) -> is_module_path_visible t path
| Path.Papply _ | Path.Pextra_ty _ ->
failwith
"Short_paths_graph.Graph.is_module_type_path_visible: \
invalid module type path"
end
type graph = Graph.t