File: type_struct.ml

package info (click to toggle)
typerep 111.17.00-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,076 kB
  • ctags: 3,093
  • sloc: ml: 18,181; makefile: 55
file content (2001 lines) | stat: -rw-r--r-- 62,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
open Typerep_lib.Std
open Pre_core.Std



module Name = struct
  include Int
  let typerep_of_t = typerep_of_int
  let make_fresh () =
    let index = ref (-1) in
    (fun () -> incr index; !index)
  let incompatible = (-1)
end

module Variant = struct
  module Kind = struct
    type t =
    | Polymorphic
    | Usual
    with sexp, typerep, bin_io

    let is_polymorphic = function
      | Polymorphic -> true
      | Usual -> false
    let equal a b =
      match a with
      | Polymorphic -> b = Polymorphic
      | Usual -> b = Usual
  end
  module V1 = struct
    type t = {
      name : string;
      repr : int;
    } with sexp, bin_io, typerep
  end
  module V2 = struct
    type t = {
      label : string;
      index : int;
      ocaml_repr : int;
    } with sexp, bin_io, typerep
  end
  include V2
  let label t = t.label
  let index t = t.index
  let ocaml_repr t = t.ocaml_repr
  let to_v1 kind t =
    let name = t.label in
    let repr = if Kind.is_polymorphic kind then t.ocaml_repr else t.index in
    { V1.name ; repr }
  let of_v1 kind index this all v1 =
    if Kind.is_polymorphic kind
    then
      { label = v1.V1.name;
        index;
        ocaml_repr = v1.V1.repr; }
    else
      let ocaml_repr =
        let no_arg = Farray.is_empty this in
        let rec count pos acc =
          if pos = index then acc
          else
            let _, args = Farray.get all pos in
            let acc = if no_arg = Farray.is_empty args then succ acc else acc in
            count (succ pos) acc
        in
        count 0 0
      in
      { label = v1.V1.name;
        index = (if index <> v1.V1.repr then assert false; index);
        ocaml_repr;
      }

  module Option = struct
    let none = {
      index = 0;
      ocaml_repr = 0;
      label = "None";
    }
    let some = {
      index = 1;
      ocaml_repr = 0;
      label = "Some";
    }
  end
end

module Variant_infos = struct
  module V1 = struct
    type t = {
      kind : Variant.Kind.t;
    } with sexp, bin_io, typerep
  end
  include V1
  let equal t t' = Variant.Kind.equal t.kind t'.kind
end

module Field = struct
  module V1 = struct
    type t = string with sexp, bin_io, typerep
  end
  module V2 = struct
    type t = {
      label : string;
      index : int;
    } with sexp, bin_io, typerep
  end
  (* module V3 = struct
   *   type t = {
   *     label : string;
   *     index : int;
   *     is_mutable : bool;
   *     etc...
   *   }
   * end *)
  include V2
  let label t = t.label
  let index t = t.index
  let to_v1 t = t.label
  let of_v1 index label = {
    label;
    index;
  }
end

module Record_infos = struct
  module V1 = struct
    type t = {
      has_double_array_tag : bool;
    } with sexp, bin_io, typerep
  end
  include V1
  let equal t t' = t.has_double_array_tag = t'.has_double_array_tag
end

module T = struct
  type t =
  | Int
  | Int32
  | Int64
  | Nativeint
  | Char
  | Float
  | String
  | Bool
  | Unit
  | Option of t
  | List of t
  | Array of t
  | Lazy of t
  | Ref of t
  | Tuple of t Farray.t
  | Record of Record_infos.t * (Field.t * t) Farray.t
  | Variant of Variant_infos.t * (Variant.t * t Farray.t) Farray.t
  | Named of Name.t * t option
  with sexp, typerep
end
include T

type type_struct = t with sexp

let incompatible () = Named (Name.incompatible, None)

let get_variant_by_repr {Variant_infos.kind} cases repr =
  if Variant.Kind.is_polymorphic kind
  then
    Farray.findi cases ~f:(fun _ ((variant, _) as case) ->
      if Int.equal variant.Variant.ocaml_repr repr then Some case else None)
  else
    let length = Farray.length cases in
    if repr < 0 || repr >= length then None else
      let (variant, _) as case = Farray.get cases repr in
      (if variant.Variant.index <> repr then assert false);
      Some case

let get_variant_by_label _infos cases label =
  Farray.findi cases ~f:(fun _ ((variant, _) as case) ->
    if String.equal variant.Variant.label label then Some case else None)

let rec is_polymorphic_variant = function
  | Named (_, Some content) -> is_polymorphic_variant content
  | Variant ({Variant_infos.kind}, _) -> Variant.Kind.is_polymorphic kind
  | _ -> false

let variant_args_of_type_struct ~arity str =
  match arity with
  | 0 ->
    if str = Unit
    then Farray.empty ()
    else assert false
  | 1 -> Farray.make1 str
  | _ ->
    match str with
    | Tuple args -> args
    | _ -> assert false (* ill formed ast *)

let type_struct_of_variant_args args =
  match Farray.length args with
  | 0 -> Unit
  | 1 -> Farray.get args 0
  | _ -> Tuple args

module Option_as_variant = struct
  open Variant
  let kind = Kind.Usual
  let infos = {
    Variant_infos.
    kind;
  }
  let make some_type =
    infos, Farray.make2
      (Option.none, Farray.empty ())
      (Option.some, Farray.make1 some_type)
end

let option_as_variant ~some = Option_as_variant.make some

class traverse =
object(self)
  method iter t =
    let f t = self#iter t in
    match t with
    | Int
    | Int32
    | Int64
    | Nativeint
    | Char
    | Float
    | String
    | Bool
    | Unit
      -> ()
    | Option t -> f t
    | List t -> f t
    | Array t -> f t
    | Lazy t -> f t
    | Ref t -> f t
    | Tuple args ->
      Farray.iter ~f args
    | Record ((_:Record_infos.t), fields) ->
      Farray.iter ~f:(fun ((_:Field.t), t) -> f t) fields
    | Variant ((_:Variant_infos.t), cases) ->
      Farray.iter ~f:(fun ((_:Variant.t), args) -> Farray.iter ~f args) cases
    | Named (_, link) -> Option.iter link ~f

  method map t =
    let map_stable_snd ~f ((a, b) as t) =
      let b' = f b in
      if phys_equal b b' then t else (a, b')
    in
    let f t = self#map t in
    match t with
    | Int
    | Int32
    | Int64
    | Nativeint
    | Char
    | Float
    | String
    | Bool
    | Unit
        -> t
    | Option arg ->
      let arg' = f arg in
      if phys_equal arg arg' then t else Option arg'
    | List arg ->
      let arg' = f arg in
      if phys_equal arg arg' then t else List arg'
    | Array arg ->
      let arg' = f arg in
      if phys_equal arg arg' then t else Array arg'
    | Lazy arg ->
      let arg' = f arg in
      if phys_equal arg arg' then t else Lazy arg'
    | Ref arg ->
      let arg' = f arg in
      if phys_equal arg arg' then t else Ref arg'
    | Tuple args ->
      let args' = Farray.map_stable ~f args in
      if phys_equal args args' then t else Tuple args'
    | Record (infos, fields) ->
      let fields' = Farray.map_stable ~f:(map_stable_snd ~f) fields in
      if phys_equal fields fields' then t else Record (infos, fields')
    | Variant (infos, cases) ->
      let cases' = Farray.map_stable cases
        ~f:(map_stable_snd ~f:(Farray.map_stable ~f))
      in
      if phys_equal cases cases' then t else Variant (infos, cases')
    | Named (name, link) ->
      let link' = Option.map_stable ~f link in
      if phys_equal link link' then t else Named (name, link')
end

module Raw = struct
  module T = struct
    type nonrec t = t with sexp
    let compare = Pervasives.compare
    let hash = Hashtbl.hash
  end
  include Hashable.Make(T)
end

module Named_utils(X:sig
  type t with sexp_of
  class traverse : object
    method iter : t -> unit
    method map : t -> t
  end
  val match_named : t -> [ `Named of Name.t * t option | `Other of t ]
  val cons_named : Name.t -> t option -> t
end) = struct
  let has_named t =
    let module M = struct
      exception Exists
    end in
    let exists = object
      inherit X.traverse as super
      method! iter t =
        match X.match_named t with
        | `Named _ -> raise M.Exists
        | `Other t -> super#iter t
    end in
    try exists#iter t; false with M.Exists -> true

  let remove_dead_links t =
    let used = Name.Hash_set.create () in
    let has_named = ref false in
    let collect = object
      inherit X.traverse as super
      method! iter t =
        super#iter t;
        match X.match_named t with
        | `Named (name, link) ->
          has_named := true;
          if Option.is_none link then Name.Hash_set.add used name
        | `Other _ -> ()
    end in
    collect#iter t;
    if not !has_named then t else begin
      let map = object
        inherit X.traverse as super
        method! map t =
          let t = super#map t in
          match X.match_named t with
          | `Named (name, Some arg) ->
            if Name.Hash_set.mem used name then t else arg
          | `Named (_, None) | `Other _ -> t
      end in
      map#map t
    end

  let alpha_conversion t =
    if not (has_named t) then t else (* allocation optimization *)
    let fresh_name = Name.make_fresh () in
    let table = Name.Table.create () in
    let rename i =
      match Name.Table.find table i with
      | Some i -> i
      | None ->
        let name = fresh_name () in
        Name.Table.set table ~key:i ~data:name;
        name
    in
    let rename = object
      inherit X.traverse as super
      method! map t =
        match X.match_named t with
        | `Named (name, arg) ->
          let name' = rename name in
          let t =
            if Name.equal name name' then t else X.cons_named name' arg
          in
          super#map t
        | `Other t -> super#map t
    end in
    rename#map t

  exception Invalid_recursive_name of Name.t * X.t with sexp

  let standalone_exn ~readonly (t:X.t) =
    if not (has_named t) then t else (* allocation optimization *)
    let local_table = Name.Table.create () in
    let or_lookup name content =
      match content with
      | Some data ->
        Name.Table.set local_table ~key:name ~data;
        content
      | None ->
        match Name.Table.find local_table name with
        | Some _ as content -> content
        | None ->
          match Name.Table.find readonly name with
          | (Some data) as content ->
            Name.Table.set local_table ~key:name ~data;
            content
          | None -> raise (Invalid_recursive_name (name, t))
    in
    let seen = Name.Hash_set.create () in
    let enrich = object
      inherit X.traverse as super
      method! map t =
        let t =
          match X.match_named t with
          | `Named (name, content) ->
            if Name.Hash_set.mem seen name then begin
              if Option.is_none content then t else
                X.cons_named name None
            end else begin
              Name.Hash_set.add seen name;
              let content' = or_lookup name content in
              if phys_equal content content'
              then t
              else X.cons_named name content'
            end
          | `Other t -> t
        in
        super#map t
    end in
    enrich#map t
end

include Named_utils(struct
  type t = type_struct with sexp_of
  class traverse_non_rec = traverse
  class traverse = traverse_non_rec
  let match_named = function
    | Named (name, link) -> `Named (name, link)
    | t -> `Other t
  let cons_named name contents = Named (name, contents)
end)

let reduce t =
  let fresh_name = Name.make_fresh () in
  let alias = Name.Table.create () in
  let consing = Raw.Table.create () in
  let share t =
    match t with
    | Named (name, None) -> begin
      match Name.Table.find alias name with
      | Some shared_name ->
        if Name.equal name shared_name then t else Named (shared_name, None)
      | None -> t
    end
    | Named (name, Some key) -> begin
      match Raw.Table.find consing key with
      | Some (shared_name, shared) ->
        if not (Name.equal name shared_name) then
          Name.Table.set alias ~key:name ~data:shared_name;
        shared
      | None ->
        let shared = Named (name, None) in
        let data = name, shared in
        Raw.Table.set consing ~key ~data;
        t
    end
    | (Record _ | Variant _) as key -> begin
      match Raw.Table.find consing key with
      | Some (_, shared) ->
        shared
      | None -> begin
        let shared_name = fresh_name () in
        let shared = Named (shared_name, None) in
        let data = shared_name, shared in
        Raw.Table.set consing ~key ~data;
        Named (shared_name, Some key)
      end
    end
    | Int
    | Int32
    | Int64
    | Nativeint
    | Char
    | Float
    | String
    | Bool
    | Unit
    | Option _
    | List _
    | Array _
    | Lazy _
    | Ref _
    | Tuple _
      -> t
  in
  let share = object
    inherit traverse as super
    method! map t =
      let t = super#map t in (* deep first *)
      share t
  end in
  let shared = share#map t in
  let reduced = remove_dead_links shared in
  reduced

exception Invalid_recursive_typestruct of Name.t * t with sexp

let sort_variant_cases cases =
  let cmp (variant, _) (variant', _) =
    String.compare variant.Variant.label variant'.Variant.label
  in
  Farray.sort ~cmp cases

module Pairs = struct
  module T = struct
    type t = Name.t * Name.t with sexp
    let compare (a, b) (a', b') =
      let cmp = Name.compare a a' in
      if cmp <> 0 then cmp else Name.compare b b'
    let hash = Hashtbl.hash
  end
  include Hashable.Make(T)
end

let equivalent_array f a b =
  let len_a = Farray.length a in
  let len_b = Farray.length b in
  if len_a <> len_b then false else
    let rec aux index = if index = len_a then true else
        f (Farray.get a index) (Farray.get b index) && aux (succ index)
    in
    aux 0

let are_equivalent a b =
  let wip = Pairs.Table.create () in
  let start_pair name name' =
    Pairs.Table.set wip ~key:(name, name') ~data:`started
  in
  let finish_pair name name' result =
    Pairs.Table.set wip ~key:(name, name') ~data:(`finished result)
  in
  let status name name' =
    match Pairs.Table.find wip (name, name') with
    | Some ((`started | `finished _) as status) -> status
    | None -> `unknown
  in
  let table_a = Name.Table.create () in
  let table_b = Name.Table.create () in
  let or_lookup table name = function
    | (Some data) as str -> Name.Table.set table ~key:name ~data ; str
    | None -> Name.Table.find table name
  in
  let rec aux a b =
    match a, b with
    | Named (name, struct_a), Named (name', struct_b) -> begin
      match status name name' with
      | `unknown -> begin
        let struct_a = or_lookup table_a name struct_a in
        let struct_b = or_lookup table_b name struct_b in
        match struct_a, struct_b with
        | Some struct_a, Some struct_b ->
          start_pair name name';
          let res = aux struct_a struct_b in
          finish_pair name name' res;
          res
        | Some _, None
        | None, Some _
        | None, None
          -> false
      end
      | `started -> true
      | `finished res -> res
    end

    | Named (name, struct_a), struct_b -> begin
      let struct_a = or_lookup table_a name struct_a in
      match struct_a with
      | None -> false
      | Some struct_a ->
        aux struct_a struct_b
    end

    | struct_a, Named (name, struct_b) -> begin
      let struct_b = or_lookup table_b name struct_b in
      match struct_b with
      | None -> false
      | Some struct_b ->
        aux struct_a struct_b
    end

    | Int, Int
    | Int32, Int32
    | Int64, Int64
    | Nativeint, Nativeint
    | Char, Char
    | Float, Float
    | String, String
    | Bool, Bool
    | Unit, Unit
      -> true

    | Option t, Option t'
    | List t, List t'
    | Array t, Array t'
    | Lazy t, Lazy t'
    | Ref t, Ref t'
      -> aux t t'

    | Tuple tys, Tuple tys' ->
      equivalent_array aux tys tys'

    | Record (infos, fields), Record (infos', fields') ->
      Record_infos.equal infos infos' &&
        let eq (field, t) (field', t') =
          String.equal field.Field.label field'.Field.label
          && Int.equal field.Field.index field'.Field.index
          && aux t t'
        in
        equivalent_array eq fields fields'

    | Variant (infos, cases), Variant (infos', cases') ->
      Variant_infos.equal infos infos' &&
        let is_polymorphic = Variant.Kind.is_polymorphic infos.Variant_infos.kind in
        let cases = if is_polymorphic then sort_variant_cases cases else cases in
        let cases' = if is_polymorphic then sort_variant_cases cases' else cases' in
        let eq (variant, args) (variant', args') =
          Int.equal variant.Variant.ocaml_repr variant'.Variant.ocaml_repr
          && String.equal variant.Variant.label variant'.Variant.label
          && equivalent_array aux args args'
        in
        equivalent_array eq cases cases'

    | Int, _
    | Int32, _
    | Int64, _
    | Nativeint, _
    | Char, _
    | Float, _
    | String, _
    | Bool, _
    | Unit, _
    | Option _, _
    | List _, _
    | Array _, _
    | Lazy _, _
    | Ref _, _
    | Record _, _
    | Variant _, _
    | Tuple _, _
      -> false
  in
  aux a b

let combine_array ~fail combine a b =
  let len = Farray.length a in
  if Farray.length b <> len then fail () else
  Farray.init len ~f:(fun index ->
    combine
      (Farray.get a index)
      (Farray.get b index)
  )

module Incompatible_types = struct
  (* these exception are used to create human readable error messages for
     least_upper_bound. we use exception with sexp rather than an error monad because
     this library does not depend on core, and dealing with exception is the easiest way
     to plunge this library in a world using a error monad, as soon as there exists a
     [try_with] function *)
  exception Invalid_recursive_structure of t * t with sexp
  exception Field_conflict of t * t * (Field.t * Field.t) with sexp
  exception Types_conflict of t * t with sexp
end

let least_upper_bound_exn a b =
  let open Incompatible_types in
  let wip = Pairs.Table.create () in
  let fresh_name = Name.make_fresh () in
  let merge_named_pair name name' =
    let merged_name = fresh_name () in
    Pairs.Table.set wip ~key:(name, name') ~data:(`name merged_name);
    merged_name
  in
  let status name name' =
    match Pairs.Table.find wip (name, name') with
    | Some ((`name _) as status) -> status
    | None -> `unknown
  in
  let table_a = Name.Table.create () in
  let table_b = Name.Table.create () in
  let or_lookup table name = function
    | (Some data) as str -> Name.Table.set table ~key:name ~data ; str
    | None -> Name.Table.find table name
  in
  let rec aux a b =
    let fail () = raise (Types_conflict (a, b)) in
    match a, b with
    | Named (name, struct_a), Named (name', struct_b) -> begin
      match status name name' with
      | `unknown -> begin
        let struct_a = or_lookup table_a name struct_a in
        let struct_b = or_lookup table_b name struct_b in
        match struct_a, struct_b with
        | Some struct_a, Some struct_b -> begin
          let name = merge_named_pair name name' in
          let content = aux struct_a struct_b in
          Named (name, Some content)
        end
        | Some _, None
        | None, Some _
        | None, None
          -> raise (Invalid_recursive_structure (a, b))
      end
      | `name name -> Named (name, None)
    end

    | Named (name, struct_a), struct_b -> begin
      let struct_a = or_lookup table_a name struct_a in
      match struct_a with
      | None -> raise (Invalid_recursive_structure (a, b))
      | Some struct_a ->
        aux struct_a struct_b
    end

    | struct_a, Named (name, struct_b) -> begin
      let struct_b = or_lookup table_b name struct_b in
      match struct_b with
      | None -> raise (Invalid_recursive_structure (a, b))
      | Some struct_b ->
        aux struct_a struct_b
    end

    | Int, Int
    | Int32, Int32
    | Int64, Int64
    | Nativeint, Nativeint
    | Char, Char
    | Float, Float
    | String, String
    | Bool, Bool
    | Unit, Unit
      -> a

    | Option t, Option t' -> Option (aux t t')
    | List t, List t'     -> List (aux t t')
    | Array t, Array t'   -> Array (aux t t')
    | Lazy t, Lazy t'     -> Lazy (aux t t')
    | Ref t, Ref t'       -> Ref (aux t t')

    | Tuple tys, Tuple tys' ->
      let args = combine_array ~fail aux tys tys' in
      Tuple args

    | Record (infos, fields), Record (infos', fields') ->
      if Record_infos.equal infos infos'
      then
        let combine (field, t) (field', t') =
          if
            String.equal field.Field.label field'.Field.label
            && Int.equal field.Field.index field'.Field.index
          then (field, (aux t t'))
          else raise (Field_conflict (a, b, (field, field')))
        in
        let fields = combine_array ~fail combine fields fields' in
        Record (infos, fields)
      else fail ()

    | Variant (infos_a, cases_a), Variant (infos_b, cases_b) ->
      if Variant_infos.equal infos_a infos_b
      then
        match infos_a.Variant_infos.kind with
        | Variant.Kind.Polymorphic -> begin
          (* polymorphic variant may be merged if there is no conflict in hashing and
             arguments are compatible *)
          let repr_table = Int.Table.create () in
          let iter_variants variants =
            let len = Farray.length variants in
            let rec iter index =
              if index >= len then true
              else begin
                let (({Variant.label=name; ocaml_repr; index=_}, args) as init) =
                  (Farray.get variants index)
                in
                match Int.Table.find repr_table ocaml_repr with
                | None ->
                  Int.Table.set repr_table ~key:ocaml_repr ~data:init;
                  iter (succ index)
                | Some (({Variant.label=name'; ocaml_repr=_ ; index=_ } as variant)
                           , args') ->
                  if name <> name' then false else begin
                    let args = combine_array ~fail aux args args' in
                    let data = variant, args in
                    Int.Table.set repr_table ~key:ocaml_repr ~data;
                    iter (succ index)
                  end
              end
            in iter 0
          in
          if iter_variants cases_a && iter_variants cases_b then begin
            let cases_merged =
              Int.Table.to_init Farray.init repr_table ~f:(fun _ case -> case)
            in
            Variant (infos_a, cases_merged)
          end else fail ()
        end
        | Variant.Kind.Usual -> begin
          (* usual variant may be merged only if one is a prefix of the other and
             arguments are compatible *)
          let len_a = Farray.length cases_a in
          let len_b = Farray.length cases_b in
          let cases_merged = Farray.init (max len_a len_b) ~f:(fun index ->
            let var_a =
              if index < len_a then Some (Farray.get cases_a index) else None
            in
            let var_b =
              if index < len_b then Some (Farray.get cases_b index) else None
            in
            match var_a, var_b with
            | Some (variant_a, args_a), Some (variant_b, args_b) ->
              if
                Int.equal variant_a.Variant.ocaml_repr variant_b.Variant.ocaml_repr
                && String.equal variant_a.Variant.label variant_b.Variant.label
              then
                let args = combine_array ~fail aux args_a args_b in
                variant_a, args
              else
                fail ()
            | Some (variant, args), None ->
              variant, args
            | None, Some (variant, args) ->
              variant, args
            | None, None -> assert false
          ) in
          Variant (infos_a, cases_merged)
        end
      else fail ()

    | Int, _
    | Int32, _
    | Int64, _
    | Nativeint, _
    | Char, _
    | Float, _
    | String, _
    | Bool, _
    | Unit, _
    | Option _, _
    | List _, _
    | Array _, _
    | Lazy _, _
    | Ref _, _
    | Record _, _
    | Variant _, _
    | Tuple _, _
      -> fail ()
  in
  aux a b

module type Typestructable = sig
  type t
  val typestruct_of_t : type_struct
end

module Internal_generic = Type_generic.Make(struct
  type 'a t = type_struct
  module Type_struct_variant = Variant
  module Type_struct_field = Field
  include Type_generic.Variant_and_record_intf.M(struct type nonrec 'a t = 'a t end)

  let name = "typestruct"
  let required = []

  let int                 = Int
  let int32               = Int32
  let int64               = Int64
  let nativeint           = Nativeint
  let char                = Char
  let float               = Float
  let string              = String
  let bool                = Bool
  let unit                = Unit
  let option  str         = Option str
  let list    str         = List str
  let array   str         = Array str
  let lazy_t  str         = Lazy str
  let ref_    str         = Ref str
  let tuple2  a b         = Tuple (Farray.make2 a b)
  let tuple3  a b c       = Tuple (Farray.make3 a b c)
  let tuple4  a b c d     = Tuple (Farray.make4 a b c d)
  let tuple5  a b c d e   = Tuple (Farray.make5 a b c d e)

  let function_ _  = assert false

  let record record =
    let infos =
      let has_double_array_tag = Record.has_double_array_tag record in
      { Record_infos.
        has_double_array_tag;
      }
    in
    let fields = Farray.init (Record.length record) ~f:(fun index ->
      match Record.field record index with
      | Record.Field field ->
        let label = Field.label field in
        let type_struct = (Field.traverse field : type_struct) in
        { Type_struct_field.label ; index }, type_struct
    )
    in
    Record (infos, fields)

  let variant variant =
    let infos =
      let polymorphic = Variant.is_polymorphic variant in
      let kind =
        Type_struct_variant.Kind.(if polymorphic then Polymorphic else Usual)
      in
      { Variant_infos.
        kind;
      }
    in
    let tags = Farray.init (Variant.length variant) ~f:(fun index ->
      match Variant.tag variant index with
      | Variant.Tag tag ->
        let label = Tag.label tag in
        let index = Tag.index tag in
        let ocaml_repr = Tag.ocaml_repr tag in
        let arity = Tag.arity tag in
        let variant = {
          Type_struct_variant.
          label;
          ocaml_repr;
          index;
        } in
        let str = Tag.traverse tag in
        let args = variant_args_of_type_struct ~arity str in
        variant, args
    )
    in
    Variant (infos, tags)

  module Named = struct
    module Context = struct
      type t = {
        fresh_name : unit -> Name.t;
      }
      let create () = {
        fresh_name = Name.make_fresh ();
      }
    end

    type 'a t = Name.t

    let init context _name =
      context.Context.fresh_name ()

    let get_wip_computation shared_name = Named (shared_name, None)

    let set_final_computation shared_name str = Named (shared_name, Some str)

    let share : type a. a Typerep.t -> bool = function
      | Typerep.Int        -> false
      | Typerep.Int32      -> false
      | Typerep.Int64      -> false
      | Typerep.Nativeint  -> false
      | Typerep.Char       -> false
      | Typerep.Float      -> false
      | Typerep.String     -> false
      | Typerep.Bool       -> false
      | Typerep.Unit       -> false
      | Typerep.Option _   -> false
      | Typerep.List _     -> false
      | Typerep.Array _    -> false
      | Typerep.Lazy _     -> false
      | Typerep.Ref _      -> false

      | Typerep.Function _ -> true
      | Typerep.Tuple _    -> true
      | Typerep.Record _   -> true
      | Typerep.Variant _  -> true

      | Typerep.Named _    -> false
  end
end)

module Generic = struct
  include Internal_generic
  let of_typerep_fct rep =
    let `generic str = Internal_generic.of_typerep rep in
    remove_dead_links str
  let of_typerep rep = `generic (of_typerep_fct rep)
end
let of_typerep = Generic.of_typerep_fct

let sexp_of_typerep rep = sexp_of_t (of_typerep rep)

module Diff = struct

  module Path = struct
    (* the path leading to the variant. succession of field names, variant names,
       or string representation of base types. for tuple with use 'f0', 'f1', etc. *)
    type t = string list with sexp
  end

  module Compatibility = struct
    type t = [
    | `Backward_compatible
    | `Break
    ] with sexp
  end

  module Atom = struct
    (* [str * str] means [old * new] *)
    type t =
    | Update of type_struct * type_struct
    | Add_field of (Field.t * type_struct)
    | Remove_field of (Field.t * type_struct)
    | Update_field of (Field.t * type_struct) * (Field.t * type_struct)
    | Add_variant of (Compatibility.t * Variant.t * type_struct Farray.t)
    | Remove_variant of (Variant.t * type_struct Farray.t)
    | Update_variant of
        (Variant.t * type_struct Farray.t)
      * (Variant.t * type_struct Farray.t)
    with sexp
  end

  type t = (Path.t * Atom.t) list with sexp

  let is_empty = List.is_empty

  (*
    The diff is done such as the length of the path associated with atoms is maximal
  *)
  let compute a b =
    let wip = Pairs.Table.create () in
    let start_pair name name' =
      Pairs.Table.set wip ~key:(name, name') ~data:`started
    in
    let status name name' =
      match Pairs.Table.find wip (name, name') with
      | Some (`started as status) -> status
      | None -> `unknown
    in
    let table_a = Name.Table.create () in
    let table_b = Name.Table.create () in
    let or_lookup table name = function
      | (Some data) as str -> Name.Table.set table ~key:name ~data ; str
      | None -> Name.Table.find table name
    in
    let diffs = Queue.create () in
    let enqueue path diff = Queue.push (path, diff) diffs in
    let rec aux_list :
        'a. add:('a -> unit)
        -> remove:('a -> unit)
        -> compute:(('a  * 'a list) -> ('a * 'a list) -> 'a list * 'a list)
        -> 'a list -> 'a list -> unit
      = fun ~add ~remove ~compute a b ->
      match a, b with
      | [], [] -> ()
      | [], _::_ -> List.iter ~f:add b
      | _::_, [] -> List.iter ~f:remove a
      | hd::tl, hd'::tl' ->
        let tl, tl' = compute (hd, tl) (hd', tl') in
        aux_list ~add ~remove ~compute tl tl'
    in
    let aux_farray :
        'a. add:('a -> unit)
        -> remove:('a -> unit)
        -> compute:(('a  * 'a list) -> ('a * 'a list) -> 'a list * 'a list)
        -> 'a Farray.t -> 'a Farray.t -> unit
      = fun ~add ~remove ~compute a b ->
      aux_list ~add ~remove ~compute (Farray.to_list a) (Farray.to_list b)
    in
    let rec aux path a b =
      match a, b with
      | Named (name_a, struct_a), Named (name_b, struct_b) -> begin
        match status name_a name_b with
        | `unknown -> begin
          let struct_a = or_lookup table_a name_a struct_a in
          let struct_b = or_lookup table_b name_b struct_b in
          match struct_a, struct_b with
          | Some struct_a, Some struct_b ->
            start_pair name_a name_b;
            aux path struct_a struct_b
          | Some _, None
          | None, Some _
          | None, None -> enqueue path (Atom.Update (a, b))
        end
        | `started -> ()
      end

      | Named (name, struct_a), struct_b -> begin
        let struct_a = or_lookup table_a name struct_a in
        match struct_a with
        | None -> enqueue path (Atom.Update (a, b))
        | Some struct_a ->
          aux path struct_a struct_b
      end

      | struct_a, Named (name, struct_b) -> begin
        let struct_b = or_lookup table_b name struct_b in
        match struct_b with
        | None -> enqueue path (Atom.Update (a, b))
        | Some struct_b ->
          aux path struct_a struct_b
      end

      | Int, Int
      | Int32, Int32
      | Int64, Int64
      | Nativeint, Nativeint
      | Char, Char
      | Float, Float
      | String, String
      | Bool, Bool
      | Unit, Unit
        -> ()

      | Option a, Option b -> aux ("option"::path) a b
      | List a, List b -> aux ("list"::path) a b
      | Array a, Array b -> aux ("array"::path) a b
      | Lazy a, Lazy b -> aux ("lazy"::path) a b
      | Ref a, Ref b -> aux ("ref"::path) a b

      | Tuple tys, Tuple tys' ->
        let arity = Farray.length tys in
        let arity' = Farray.length tys' in
        if arity <> arity'
        then enqueue path (Atom.Update (a, b))
        else
          (* since arity = arity', add and remove are never called *)
          let add _ = assert false in
          let remove _ =  assert false in
          let index = ref 0 in
          let compute (ty, tl) (ty', tl') =
            let path = ("f"^(string_of_int !index)) :: path in
            aux path ty ty';
            incr index;
            tl, tl'
          in
          aux_farray ~add ~remove ~compute tys tys'

      | Record (infos, fields), Record (infos', fields') ->
        if not (Record_infos.equal infos infos')
        then enqueue path (Atom.Update (a, b)) else begin
          let add hd = enqueue path (Atom.Add_field hd) in
          let remove hd = enqueue path (Atom.Remove_field hd) in
          let update a b = enqueue path (Atom.Update_field (a, b)) in
          let labels fields =
            let set = String.Hash_set.create () in
            let iter (field, _) = String.Hash_set.add set field.Field.label in
            Farray.iter ~f:iter fields;
            set
          in
          let labels_a = labels fields in
          let labels_b = labels fields' in
          let compute ((field, str) as hd, tl) ((field', str') as hd', tl') =
            let field = field.Field.label in
            let field' = field'.Field.label in
            if String.equal field field'
            then begin
              aux (field::path) str str';
              tl, tl'
            end else begin
              match
                String.Hash_set.mem labels_b field,
                String.Hash_set.mem labels_a field' with
                | false, false
                | true, true -> update hd hd'; tl, tl'
                | true, false -> add hd'; (hd::tl), tl'
                | false, true -> remove hd; tl, (hd'::tl')
            end
          in
          aux_farray ~add ~remove ~compute fields fields'
        end

      | Variant ({Variant_infos.kind} as infos, cases), Variant (infos', cases') ->
        if not (Variant_infos.equal infos infos')
        then enqueue path (Atom.Update (a, b)) else begin
          let is_polymorphic = Variant.Kind.is_polymorphic kind in
          let cases = if is_polymorphic then sort_variant_cases cases else cases in
          let cases' = if is_polymorphic then sort_variant_cases cases' else cases' in
          let add compatible (a,b) = enqueue path (Atom.Add_variant (compatible, a, b)) in
          let remove hd = enqueue path (Atom.Remove_variant hd) in
          let update a b = enqueue path (Atom.Update_variant (a, b)) in
          let labels cases =
            let set = String.Hash_set.create () in
            let iter (variant, _) = String.Hash_set.add set variant.Variant.label in
            Farray.iter ~f:iter cases;
            set
          in
          let labels_a = labels cases in
          let labels_b = labels cases' in
          let compute ((variant, args) as hd, tl) ((variant', args') as hd', tl') =
            let label = variant.Variant.label in
            let label' = variant'.Variant.label in
            let arity = Farray.length args in
            let arity' = Farray.length args' in
            match String.equal label label', arity = arity' with
            | true, true -> begin
              Farray.iter2_exn args args'
                ~f:(let path = label::path in fun str str' -> aux path str str');
              tl, tl'
            end
            | true, false -> begin
              update hd hd';
              tl, tl'
            end
            | false, (true | false) -> begin
              match
                String.Hash_set.mem labels_b label,
                String.Hash_set.mem labels_a label' with
              | false, false
              | true, true -> update hd hd'; tl, tl'
              | true, false ->
                let compatible =
                  if is_polymorphic || List.is_empty tl'
                  then `Backward_compatible
                  else `Break
                in
                add compatible hd'; (hd::tl), tl'
              | false, true -> remove hd; tl, (hd'::tl')
            end
          in
          aux_farray ~add:(add `Backward_compatible) ~remove ~compute cases cases'
        end

      | Int, _
      | Int32, _
      | Int64, _
      | Nativeint, _
      | Char, _
      | Float, _
      | String, _
      | Bool, _
      | Unit, _
      | Option _, _
      | List _, _
      | Array _, _
      | Lazy _, _
      | Ref _, _
      | Tuple _, _
      | Record _, _
      | Variant _, _
        -> enqueue path (Atom.Update (a, b))
    in
    aux [] a b;
    let diffs = Queue.fold (fun acc x -> x::acc) [] diffs in
    let f (path, diff) = List.rev path, diff in
    List.rev_map ~f diffs

  let is_bin_prot_subtype ~subtype:a ~supertype:b =
    let diffs = compute a b in
    let for_all = function
      | _, Atom.Add_variant (compatible, _, _) -> compatible = `Backward_compatible
      | _ -> false
    in
    List.for_all ~f:for_all diffs

  let incompatible_changes t =
    let filter (_, atom) =
      let open Atom in
      match atom with
      | Add_variant (compatible, _, _) -> compatible = `Break
      | Update _
      | Add_field _
      | Remove_field _
      | Update_field _
      | Remove_variant _
      | Update_variant _
        -> true
    in
    List.rev_filter ~f:filter t
end

module To_typerep = struct
  exception Unbound_name of t * Name.t with sexp
  exception Unsupported_tuple of t with sexp

  let to_typerep : t -> Typerep.packed = fun type_struct ->
    let table = Name.Table.create () in
    let rec aux = function
      | Int -> Typerep.T typerep_of_int
      | Int32 -> Typerep.T typerep_of_int32
      | Int64 -> Typerep.T typerep_of_int64
      | Nativeint -> Typerep.T typerep_of_nativeint
      | Char -> Typerep.T typerep_of_char
      | Float -> Typerep.T typerep_of_float
      | String -> Typerep.T typerep_of_string
      | Bool -> Typerep.T typerep_of_bool
      | Unit -> Typerep.T typerep_of_unit
      | Option t ->
        let Typerep.T rep = aux t in
        Typerep.T (typerep_of_option rep)
      | List t ->
        let Typerep.T rep = aux t in
        Typerep.T (typerep_of_list rep)
      | Array t ->
        let Typerep.T rep = aux t in
        Typerep.T (typerep_of_array rep)
      | Lazy t ->
        let Typerep.T rep = aux t in
        Typerep.T (typerep_of_lazy_t rep)
      | Ref t ->
        let Typerep.T rep = aux t in
        Typerep.T (typerep_of_ref rep)
      | (Tuple args) as type_struct -> begin
        match Farray.to_array ~f:(fun _ x -> x) args with
        | [| a ; b |] ->
          let Typerep.T a = aux a in
          let Typerep.T b = aux b in
          Typerep.T (typerep_of_tuple2 a b)
        | [| a ; b ; c |] ->
          let Typerep.T a = aux a in
          let Typerep.T b = aux b in
          let Typerep.T c = aux c in
          Typerep.T (typerep_of_tuple3 a b c)
        | [| a ; b ; c ; d |] ->
          let Typerep.T a = aux a in
          let Typerep.T b = aux b in
          let Typerep.T c = aux c in
          let Typerep.T d = aux d in
          Typerep.T (typerep_of_tuple4 a b c d)
        | [| a ; b ; c ; d ; e |] ->
          let Typerep.T a = aux a in
          let Typerep.T b = aux b in
          let Typerep.T c = aux c in
          let Typerep.T d = aux d in
          let Typerep.T e = aux e in
          Typerep.T (typerep_of_tuple5 a b c d e)
        | _ -> raise (Unsupported_tuple type_struct)
      end
      | Record (infos, fields) ->
        let len = Farray.length fields in
        let typed_fields = Farray.to_array fields ~f:(fun index (field, str) ->
          if index <> field.Field.index then assert false;
          let label = field.Field.label in
          let Typerep.T typerep_of_field = aux str in
          let get obj =
            let cond = Obj.is_block obj && Obj.size obj = len in
            if not cond then assert false;
            (* [Obj.field] works on float array *)
            Obj.obj (Obj.field obj index)
          in
          let field = {
            Typerep.Field_internal.
            label;
            rep = typerep_of_field;
            index;
            tyid = Typename.create ();
            get;
          } in
          Typerep.Record_internal.Field (Typerep.Field.internal_use_only field)
        )
        in
        let module Typename_of_t = Make_typename.Make0(struct
          type t = Obj.t
          let name = "dynamic record"
        end)
        in
        let typename = Typerep.Named.typename_of_t Typename_of_t.named in
        let has_double_array_tag = infos.Record_infos.has_double_array_tag in
        let create { Typerep.Record_internal.get } =
          let t =
            let tag =
              if has_double_array_tag
              then Obj.double_array_tag
              else 0
            in
            Obj.new_block tag len
          in
          let iter = function
            | Typerep.Record_internal.Field field ->
              let index = Typerep.Field.index field in
              let value = get field in
              Obj.set_field t index (Obj.repr value)
          in
          Array.iter ~f:iter typed_fields;
          t
        in
        let record = Typerep.Record.internal_use_only {
          Typerep.Record_internal.
          typename;
          fields = typed_fields;
          has_double_array_tag;
          create;
        } in
        let typerep_of_t =
          Typerep.Named ((Typename_of_t.named,
                           (Some (lazy (Typerep.Record record)))))
        in
        Typerep.T typerep_of_t
      | Variant ({Variant_infos.kind}, tags_str) ->
        let polymorphic = Variant.Kind.is_polymorphic kind in
        let typed_tags = Farray.to_array tags_str ~f:(fun index (variant, args) ->
          let type_struct = type_struct_of_variant_args args in
          let Typerep.T typerep_of_tag = aux type_struct in
          let index = (if index <> variant.Variant.index then assert false); index in
          let ocaml_repr = variant.Variant.ocaml_repr in
          let arity = Farray.length args in
          let label = variant.Variant.label in
          let create_polymorphic =
            if arity = 0
            then Typerep.Tag_internal.Const (Obj.repr ocaml_repr)
            else Typerep.Tag_internal.Args (fun value ->
              let block = Obj.new_block 0 2 in
              Obj.set_field block 0 (Obj.repr ocaml_repr);
              Obj.set_field block 1 (Obj.repr value);
              block
            )
          in
          let create_usual =
            match arity with
            | 0 -> Typerep.Tag_internal.Const (Obj.repr ocaml_repr)
            | 1 -> Typerep.Tag_internal.Args (fun value ->
              let block = Obj.new_block ocaml_repr 1 in
              Obj.set_field block 0 (Obj.repr value);
              block)
            | n -> Typerep.Tag_internal.Args (fun value ->
              let args = Obj.repr value in
              if not (Obj.size args = n) then assert false;
              let block = Obj.dup args in
              Obj.set_tag block ocaml_repr;
              block)
          in
          let create = if polymorphic then create_polymorphic else create_usual in
          let tyid =
            (fun (type exist) (typerep_of_tag:exist Typerep.t) ->
              if arity = 0
              then
                let Type_equal.T =
                  Typerep.same_witness_exn typerep_of_tuple0 typerep_of_tag
                in
                (typename_of_tuple0 : exist Typename.t)
              else
                (Typename.create () : exist Typename.t)
            ) typerep_of_tag
          in
          let tag = {
            Typerep.Tag_internal.
            label;
            rep = typerep_of_tag;
            arity;
            index;
            ocaml_repr;
            tyid;
            create;
          } in
          Typerep.Variant_internal.Tag (Typerep.Tag.internal_use_only tag)
        ) in
        let module Typename_of_t = Make_typename.Make0(struct
          type t = Obj.t
          let name = "dynamic variant"
        end) in
        let typename = Typerep.Named.typename_of_t Typename_of_t.named in
        let value_polymorphic =
          let map =
            let map exists =
              match exists with
              | Typerep.Variant_internal.Tag tag ->
                let ocaml_repr = Typerep.Tag.ocaml_repr tag in
                ocaml_repr, exists
            in
            Flat_map.Flat_int_map.of_array_map ~f:map typed_tags
          in
          (fun obj ->
            let repr = Typerep_obj.repr_of_poly_variant (Obj.obj obj) in
            let no_arg = Obj.is_int obj in
            match Flat_map.Flat_int_map.find map repr with
            | None -> assert false
            | Some (Typerep.Variant_internal.Tag tag) ->
              let arity = Typerep.Tag.arity tag in
              let value =
                match no_arg with
                | true ->
                  if arity = 0
                  then Obj.repr value_tuple0
                  else assert false
                | false ->
                  let size = Obj.size obj in
                  if size <> 2 then assert false;
                  let value = Obj.field obj 1 in
                  if arity = 1
                  then value
                  else begin
                    if Obj.is_int value then assert false;
                    if Obj.size value <> arity then assert false;
                    value
                  end
              in
              Typerep.Variant_internal.Value (tag, Obj.obj value)
          )
        in
        let value_usual =
          let bound = Array.length typed_tags in
          let collect pred_args =
            let rec aux acc index =
              if index >= bound then Farray.of_list (List.rev acc)
              else
                match typed_tags.(index) with
                | (Typerep.Variant_internal.Tag tag) as exists ->
                  let arity = Typerep.Tag.arity tag in
                  let acc =
                    if pred_args arity then exists::acc else acc
                  in
                  aux acc (succ index)
            in
            aux [] 0
          in
          let without_args = collect (fun i -> i = 0) in
          let with_args = collect (fun i -> i > 0) in
          let find_tag ~no_arg idx =
            let table = if no_arg then without_args else with_args in
            if idx < 0 || idx >= Farray.length table then assert false;
            Farray.get table idx
          in
          (fun obj ->
            match Obj.is_int obj with
            | true -> begin
              match find_tag ~no_arg:true (Obj.obj obj) with
              | Typerep.Variant_internal.Tag tag ->
                let Type_equal.T =
                  Typename.same_witness_exn
                    (Typerep.Tag.tyid tag)
                    typename_of_tuple0
                in
                let arity = Typerep.Tag.arity tag in
                if arity <> 0 then assert false;
                Typerep.Variant_internal.Value (tag, value_tuple0)
            end
            | false ->
              let idx = Obj.tag obj in
              match find_tag ~no_arg:false idx with
              | Typerep.Variant_internal.Tag tag ->
                let arity = Typerep.Tag.arity tag in
                if arity <> Obj.size obj then assert false;
                let value =
                  if arity = 1
                  then Obj.field obj 0
                  else
                    let block = Obj.dup obj in
                    Obj.set_tag block 0; (* tuple *)
                    block
                in
                Typerep.Variant_internal.Value (tag, Obj.obj value)
          )
        in
        let value = if polymorphic then value_polymorphic else value_usual in
        let variant = Typerep.Variant.internal_use_only {
          Typerep.Variant_internal.
          typename;
          tags = typed_tags;
          polymorphic;
          value;
        } in
        let typerep_of_t =
          Typerep.Named ((Typename_of_t.named,
                           (Some (lazy (Typerep.Variant variant)))))
        in
        Typerep.T typerep_of_t
      | Named (name, content) ->
        match Name.Table.find table name with
        | Some content -> content
        | None ->
          let module T = struct
            (*
              let [t] be the type represented by this named type_struct
              this type will be equal to the existential type returned by the call to
              aux performed on the content.
            *)
            type t
            let name = string_of_int name
          end in
          let module Named = Make_typename.Make0(T) in
          let content =
            match content with
            | Some content -> content
            | None ->
              raise (Unbound_name (type_struct, name))
          in
          let release_content_ref = ref (`aux_content content) in
          let typerep_of_t = Typerep.Named (Named.named, Some (lazy (
            match !release_content_ref with
            | `aux_content content ->
              let Typerep.T typerep_of_content = aux content in
              let rep = (fun (type content) (rep:content Typerep.t) ->
                (Obj.magic (rep : content Typerep.t) : T.t Typerep.t)
              ) typerep_of_content
              in
              release_content_ref := `typerep rep;
              rep
            | `typerep rep -> rep
          ))) in
          let data = Typerep.T typerep_of_t in
          Name.Table.set table ~key:name ~data;
          data
    in
    aux type_struct
end
let to_typerep = To_typerep.to_typerep

module Versioned = struct
  module Version = struct
    type t = [
    | `v0
    | `v1
    | `v2
    | `v3
    | `v4
    ] with bin_io, sexp, typerep
    let v1 = `v1
    let v2 = `v2
    let v3 = `v3
    let v4 = `v4
  end
  exception Not_downgradable of Sexp.t with sexp
  module type V_sig = sig
    type t
    with sexp, bin_io, typerep
    val serialize : type_struct -> t
    val unserialize : t -> type_struct
  end
  module V0 : V_sig = struct
    type t =
    | Unit
    with sexp, bin_io, typerep

    let serialize = function
      | T.Unit -> Unit
      | str -> raise (Not_downgradable (T.sexp_of_t str))

    let unserialize = function
      | Unit -> T.Unit
  end
  module V1 : V_sig = struct
    type t =
    | Int
    | Int32
    | Int64
    | Nativeint
    | Char
    | Float
    | String
    | Bool
    | Unit
    | Option of t
    | List of t
    | Array of t
    | Lazy of t
    | Ref of t
    | Record of (Field.V1.t * t) Farray.t
    | Tuple of t Farray.t
    | Variant of Variant.Kind.t * (Variant.V1.t * t Farray.t) Farray.t
    with sexp, bin_io, typerep

    let rec serialize = function
      | T.Int       -> Int
      | T.Int32     -> Int32
      | T.Int64     -> Int64
      | T.Nativeint -> Nativeint
      | T.Char      -> Char
      | T.Float     -> Float
      | T.String    -> String
      | T.Bool      -> Bool
      | T.Unit      -> Unit
      | T.Option t  -> Option (serialize t)
      | T.List t    -> List (serialize t)
      | T.Array t   -> Array (serialize t)
      | T.Lazy t    -> Lazy (serialize t)
      | T.Ref t     -> Ref (serialize t)
      | T.Record (infos, fields) as str ->
        if infos.Record_infos.has_double_array_tag
        then raise (Not_downgradable (T.sexp_of_t str));
        let map (field, t) =
          let field = Field.to_v1 field in
          field, serialize t in
        let fields = Farray.map ~f:map fields in
        Record fields
      | T.Tuple args -> Tuple (Farray.map ~f:serialize args)
      | T.Variant (info, tags) ->
        let kind = info.Variant_infos.kind in
        let map (variant, t) =
          let variant_v1 = Variant.to_v1 kind variant in
          variant_v1, Farray.map ~f:serialize t
        in
        Variant (kind, Farray.map ~f:map tags)
      | (T.Named _) as str -> raise (Not_downgradable (T.sexp_of_t str))

    let rec unserialize = function
      | Int       -> T.Int
      | Int32     -> T.Int32
      | Int64     -> T.Int64
      | Nativeint -> T.Nativeint
      | Char      -> T.Char
      | Float     -> T.Float
      | String    -> T.String
      | Bool      -> T.Bool
      | Unit      -> T.Unit
      | Option t  -> T.Option (unserialize t)
      | List t    -> T.List (unserialize t)
      | Array t   -> T.Array (unserialize t)
      | Lazy t    -> T.Lazy (unserialize t)
      | Ref t     -> T.Ref (unserialize t)
      | Record fields ->
        let infos = { Record_infos.
          (* this is wrong is some cases, if so the exec should upgrade to >= v3 *)
          has_double_array_tag = false;
        } in
        let mapi index (field, t) =
          let field = Field.of_v1 index field in
          field, unserialize t
        in
        let fields = Farray.mapi ~f:mapi fields in
        T.Record (infos, fields)
      | Tuple args -> T.Tuple (Farray.map ~f:unserialize args)
      | Variant (kind, tags) ->
        let infos = { Variant_infos.kind } in
        let mapi index (variant_v1, t) =
          let variant = Variant.of_v1 kind index t tags variant_v1 in
          variant, Farray.map ~f:unserialize t
        in
        T.Variant (infos, Farray.mapi ~f:mapi tags)
  end
  (* Adding Named *)
  module V2 : V_sig = struct
    type t =
    | Int
    | Int32
    | Int64
    | Nativeint
    | Char
    | Float
    | String
    | Bool
    | Unit
    | Option of t
    | List of t
    | Array of t
    | Lazy of t
    | Ref of t
    | Record of (Field.V1.t * t) Farray.t
    | Tuple of t Farray.t
    | Variant of Variant.Kind.t * (Variant.V1.t * t Farray.t) Farray.t
    | Named of Name.t * t option
    with sexp, bin_io, typerep

    let rec serialize = function
      | T.Int       -> Int
      | T.Int32     -> Int32
      | T.Int64     -> Int64
      | T.Nativeint -> Nativeint
      | T.Char      -> Char
      | T.Float     -> Float
      | T.String    -> String
      | T.Bool      -> Bool
      | T.Unit      -> Unit
      | T.Option t  -> Option (serialize t)
      | T.List t    -> List (serialize t)
      | T.Array t   -> Array (serialize t)
      | T.Lazy t    -> Lazy (serialize t)
      | T.Ref t     -> Ref (serialize t)
      | T.Record (infos, fields) as str ->
        if infos.Record_infos.has_double_array_tag
        then raise (Not_downgradable (T.sexp_of_t str));
        let map (field, t) =
          let field = Field.to_v1 field in
          field, serialize t
        in
        let fields = Farray.map ~f:map fields in
        Record fields
      | T.Tuple args -> Tuple (Farray.map ~f:serialize args)
      | T.Variant (info, tags) ->
        let kind = info.Variant_infos.kind in
        let map (variant, t) =
          let variant_v1 = Variant.to_v1 kind variant in
          variant_v1, Farray.map ~f:serialize t
        in
        Variant (kind, Farray.map ~f:map tags)
      | T.Named (name, content) ->
        let content = Option.map ~f:serialize content in
        Named (name, content)

    let rec unserialize = function
      | Int       -> T.Int
      | Int32     -> T.Int32
      | Int64     -> T.Int64
      | Nativeint -> T.Nativeint
      | Char      -> T.Char
      | Float     -> T.Float
      | String    -> T.String
      | Bool      -> T.Bool
      | Unit      -> T.Unit
      | Option t  -> T.Option (unserialize t)
      | List t    -> T.List (unserialize t)
      | Array t   -> T.Array (unserialize t)
      | Lazy t    -> T.Lazy (unserialize t)
      | Ref t     -> T.Ref (unserialize t)
      | Record fields ->
        let infos = { Record_infos.
          (* this is wrong is some cases, if so the exec should upgrade to >= v3 *)
          has_double_array_tag = false;
        } in
        let mapi index (field, t) =
          let field = Field.of_v1 index field in
          field, unserialize t
        in
        let fields = Farray.mapi ~f:mapi fields in
        T.Record (infos, fields)
      | Tuple args -> T.Tuple (Farray.map ~f:unserialize args)
      | Variant (kind, tags) ->
        let infos = { Variant_infos.kind } in
        let mapi index (variant_v1, t) =
          let variant = Variant.of_v1 kind index t tags variant_v1 in
          variant, Farray.map ~f:unserialize t
        in
        T.Variant (infos, Farray.mapi ~f:mapi tags)
      | Named (name, content) ->
        let content = Option.map ~f:unserialize content in
        T.Named (name, content)
  end
  (* Adding meta-info to the records and variants *)
  module V3 : V_sig = struct
    type t =
    | Int
    | Int32
    | Int64
    | Nativeint
    | Char
    | Float
    | String
    | Bool
    | Unit
    | Option of t
    | List of t
    | Array of t
    | Lazy of t
    | Ref of t
    | Record of Record_infos.V1.t * (Field.V1.t * t) Farray.t
    | Tuple of t Farray.t
    | Variant of Variant_infos.V1.t * (Variant.V1.t * t Farray.t) Farray.t
    | Named of Name.t * t option
    with sexp, bin_io, typerep

    let rec serialize = function
      | T.Int       -> Int
      | T.Int32     -> Int32
      | T.Int64     -> Int64
      | T.Nativeint -> Nativeint
      | T.Char      -> Char
      | T.Float     -> Float
      | T.String    -> String
      | T.Bool      -> Bool
      | T.Unit      -> Unit
      | T.Option t  -> Option (serialize t)
      | T.List t    -> List (serialize t)
      | T.Array t   -> Array (serialize t)
      | T.Lazy t    -> Lazy (serialize t)
      | T.Ref t     -> Ref (serialize t)
      | T.Record (infos, fields) ->
        let map (field, t) =
          let field = Field.to_v1 field in
          field, serialize t
        in
        let fields = Farray.map ~f:map fields in
        Record (infos, fields)
      | T.Tuple args -> Tuple (Farray.map ~f:serialize args)
      | T.Variant (infos, tags) ->
        let kind = infos.Variant_infos.kind in
        let map (variant, t) =
          let variant_v1 = Variant.to_v1 kind variant in
          variant_v1, Farray.map ~f:serialize t
        in
        Variant (infos, Farray.map ~f:map tags)
      | T.Named (name, content) ->
        let content = Option.map ~f:serialize content in
        Named (name, content)

    let rec unserialize = function
      | Int       -> T.Int
      | Int32     -> T.Int32
      | Int64     -> T.Int64
      | Nativeint -> T.Nativeint
      | Char      -> T.Char
      | Float     -> T.Float
      | String    -> T.String
      | Bool      -> T.Bool
      | Unit      -> T.Unit
      | Option t  -> T.Option (unserialize t)
      | List t    -> T.List (unserialize t)
      | Array t   -> T.Array (unserialize t)
      | Lazy t    -> T.Lazy (unserialize t)
      | Ref t     -> T.Ref (unserialize t)
      | Record (infos, fields) ->
        let mapi index (field, t) =
          let field = Field.of_v1 index field in
          field, unserialize t
        in
        let fields = Farray.mapi ~f:mapi fields in
        T.Record (infos, fields)
      | Tuple args -> T.Tuple (Farray.map ~f:unserialize args)
      | Variant (infos, tags) ->
        let kind = infos.Variant_infos.kind in
        let mapi index (variant_v1, t) =
          let variant = Variant.of_v1 kind index t tags variant_v1 in
          variant, Farray.map ~f:unserialize t
        in
        T.Variant (infos, Farray.mapi ~f:mapi tags)
      | Named (name, content) ->
        let content = Option.map ~f:unserialize content in
        T.Named (name, content)
  end

  (* Switching to Variant.V2 and Field.V2.t *)
  module V4 : V_sig with type t = T.t = struct
    type t = T.t =
    | Int
    | Int32
    | Int64
    | Nativeint
    | Char
    | Float
    | String
    | Bool
    | Unit
    | Option of t
    | List of t
    | Array of t
    | Lazy of t
    | Ref of t
    | Tuple of t Farray.t
    | Record of Record_infos.V1.t * (Field.V2.t * t) Farray.t
    | Variant of Variant_infos.V1.t * (Variant.V2.t * t Farray.t) Farray.t
    | Named of Name.t * t option
    with sexp, bin_io
    let typerep_of_t = T.typerep_of_t
    let typename_of_t = T.typename_of_t
    (* *)

    let serialize t = t
    let unserialize t = t
  end

  type t = [
  | `V0 of V0.t
  | `V1 of V1.t
  | `V2 of V2.t
  | `V3 of V3.t
  | `V4 of V4.t
  ] with bin_io, sexp, typerep

  let aux_unserialize = function
    | `V0 v0 -> V0.unserialize v0
    | `V1 v1 -> V1.unserialize v1
    | `V2 v2 -> V2.unserialize v2
    | `V3 v3 -> V3.unserialize v3
    | `V4 v4 -> V4.unserialize v4

  let serialize ~version v4 =
    match version with
    | `v0 -> `V0 (V0.serialize v4)
    | `v1 -> `V1 (V1.serialize v4)
    | `v2 -> `V2 (V2.serialize v4)
    | `v3 -> `V3 (V3.serialize v4)
    | `v4 -> `V4 (V4.serialize v4)

  let version = function
    | `V0 _ -> `v0
    | `V1 _ -> `v1
    | `V2 _ -> `v2
    | `V3 _ -> `v3
    | `V4 _ -> `v4

  let change_version ~version:requested t =
    if version t = requested
    then t
    else serialize ~version:requested (aux_unserialize t)

  module Diff = struct
    let compute a b = Diff.compute (aux_unserialize a) (aux_unserialize b)
    let is_bin_prot_subtype ~subtype ~supertype =
      let subtype = aux_unserialize subtype in
      let supertype = aux_unserialize supertype in
      Diff.is_bin_prot_subtype ~subtype ~supertype
  end

  let is_polymorphic_variant t = is_polymorphic_variant (aux_unserialize t)

  let least_upper_bound_exn t1 t2 =
    let supremum = least_upper_bound_exn (aux_unserialize t1) (aux_unserialize t2) in
    serialize ~version:(version t1) supremum

  let unserialize = aux_unserialize

  let to_typerep t =
    To_typerep.to_typerep (aux_unserialize t)

  let of_typerep ~version rep =
    let type_struct = of_typerep rep in
    serialize ~version type_struct
end

type 'a typed_t = t

let recreate_dynamically_typerep_for_test (type a) (rep:a Typerep.t) =
  let Typerep.T typerep = to_typerep (of_typerep rep) in
  (fun (type b) (typerep:b Typerep.t) ->
    (* this Obj.magic is used to be able to add more testing, basically we use the ocaml
       runtime to deal with value create with the Obj module during the execution of
       [Type_struct.to_typerep] and allow ocaml to treat them as value of the right type
       as they had been generated by some functions whose code had been known at compile
       time *)
    (Obj.magic (typerep:b Typerep.t) : a Typerep.t)
  ) typerep