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
(* Representation *)
type lab = string
type var = string

type control =
  | Returns        (* regular local function or one-shot shared function *)
  | Promises       (* shared function producing a future value upon call *)
  | Replies        (* (IR only): responds asynchronously using `reply` *)

type obj_sort =
   Object
 | Actor
 | Module
 | Memory          (* (codegen only): stable memory serialization format *)

type async_sort = Fut | Cmp
type shared_sort = Query | Write | Composite
type 'a shared = Local | Shared of 'a
type func_sort = shared_sort shared
type eff = Triv | Await

type prim =
  | Null
  | Bool
  | Nat
  | Nat8
  | Nat16
  | Nat32
  | Nat64
  | Int
  | Int8
  | Int16
  | Int32
  | Int64
  | Float
  | Char
  | Text
  | Blob (* IR use: Packed representation, vec u8 IDL type *)
  | Error
  | Principal
  | Region

type t = typ
and typ =
  | Var of var * int                          (* variable *)
  | Con of con * typ list                     (* constructor *)
  | Prim of prim                              (* primitive *)
  | Obj of obj_sort * field list              (* object *)
  | Variant of field list                     (* variant *)
  | Array of typ                              (* array *)
  | Opt of typ                                (* option *)
  | Tup of typ list                           (* tuple *)
  | Func of func_sort * control * bind list * typ list * typ list  (* function *)
  | Async of async_sort * scope * typ                      (* future *)
  | Mut of typ                                (* mutable type *)
  | Any                                       (* top *)
  | Non                                       (* bottom *)
  | Typ of con                                (* type (field of module) *)
  | Pre                                       (* pre-type *)

and scope = typ
and bind_sort = Scope | Type

and bind = {var : var; sort: bind_sort; bound : typ}
and src = {depr : string option; region : Source.region}
and field = {lab : lab; typ : typ; src : src}

and con = kind Cons.t
and kind =
  | Def of bind list * typ
  | Abs of bind list * typ

let empty_src = {depr = None; region = Source.no_region}

(* Efficient comparison *)
let tag_prim = function
  | Null -> 0
  | Bool -> 1
  | Nat -> 2
  | Nat8 -> 3
  | Nat16 -> 4
  | Nat32 -> 5
  | Nat64 -> 6
  | Int -> 7
  | Int8 -> 8
  | Int16 -> 9
  | Int32 -> 10
  | Int64 -> 11
  | Float -> 12
  | Char -> 13
  | Text -> 14
  | Blob -> 15
  | Error -> 16
  | Principal -> 17
  | Region -> 18

let tag_func_sort = function
  | Local -> 0
  | Shared Write -> 1
  | Shared Query -> 2
  | Shared Composite -> 3

let tag_obj_sort = function
  | Object -> 0
  | Module -> 1
  | Actor -> 2
  | Memory -> 3

let tag_control = function
  | Returns -> 0
  | Promises -> 1
  | Replies -> 2

let tag = function
  | Prim _ -> 0
  | Var _ -> 1
  | Con _ -> 2
  | Array _ -> 3
  | Tup _ -> 4
  | Func _ -> 5
  | Opt _ -> 6
  | Async _ -> 7
  | Obj _ -> 8
  | Variant _ -> 9
  | Mut _ -> 10
  | Any -> 11
  | Non -> 12
  | Pre -> 13
  | Typ _ -> 14

let compare_prim p1 p2 =
  let d = tag_prim p1 - tag_prim p2 in
  if d > 0 then 1 else if d < 0 then -1 else 0

let compare_control c1 c2 =
  let d = tag_control c1 - tag_control c2 in
  if d > 0 then 1 else if d < 0 then -1 else 0

let compare_async_sort s1 s2 =
  match s1, s2 with
  | Fut, Fut
  | Cmp, Cmp -> 0
  | Fut, Cmp -> -1
  | Cmp, Fut -> 1

let compare_obj_sort s1 s2 =
  let d = tag_obj_sort s1 - tag_obj_sort s2 in
  if d > 0 then 1 else if d < 0 then -1 else 0

let compare_func_sort s1 s2 =
  let d = tag_func_sort s1 - tag_func_sort s2 in
  if d > 0 then 1 else if d < 0 then -1 else 0

let compare_bind_sort s1 s2 =
  match s1, s2 with
  | Type, Type
  | Scope, Scope -> 0
  | Type, Scope -> -1
  | Scope, Type -> 1

let compare_src s1 s2 =
  match (s1.depr, s2.depr) with
  | None, None -> 0
  | Some d1, Some d2 -> String.compare d1 d2
  | None, Some _ -> -1
  | _ -> 1

let rec compare_typ (t1 : typ) (t2 : typ) =
  if t1 == t2 then 0
  else match (t1, t2) with
  | Prim p1, Prim p2 ->
    compare_prim p1 p2
  | Var (s1, i1), Var (s2, i2) ->
    (match Int.compare i1 i2 with
     | 0 -> String.compare s1 s2
     | ord -> ord)
  | Con (c1, ts1), Con (c2, ts2) ->
    (match Cons.compare c1 c2 with
     | 0 -> compare_typs ts1 ts2
     | ord -> ord)
  | Array t1, Array t2 ->
    compare_typ t1 t2
  | Tup ts1, Tup ts2 ->
    compare_typs ts1 ts2
  | Func (s1, c1, tbs1, ts11, ts12),
    Func (s2, c2, tbs2, ts21, ts22) ->
    (match compare_func_sort s1 s2 with
     | 0 ->
       (match compare_control c1 c2 with
        | 0 ->
          (match compare_tbs tbs1 tbs2 with
           | 0 ->
             (match compare_typs ts11 ts21 with
             | 0 -> compare_typs ts12 ts22
             | ord -> ord)
           | ord -> ord)
        | ord -> ord)
     | ord -> ord)
  | Opt t1, Opt t2 -> compare_typ t1 t2
  | Async (s1, t11, t12) , Async (s2, t21, t22) ->
    (match compare_async_sort s1 s2 with
     | 0 -> (match compare_typ t11 t21 with
             | 0 -> compare_typ t12 t22
             | ord -> ord)
     | ord -> ord)
  | Obj (s1, fs1), Obj (s2, fs2) ->
    (match compare_obj_sort s1 s2 with
     | 0 -> compare_flds fs1 fs2
     | ord -> ord)
  | Variant fs1, Variant fs2 ->
    compare_flds fs1 fs2
  | Mut t1, Mut t2 ->
    compare_typ t1 t2
  | Any, Any
  | Non, Non
  | Pre, Pre -> 0
  | Typ c1, Typ c2 -> Cons.compare c1 c2
  | _ -> Int.compare (tag t1) (tag t2)

and compare_tb tb1 tb2 =
  match String.compare tb1.var tb2.var with
  | 0 ->
    (match compare_typ tb1.bound tb2.bound with
     | 0 -> compare_bind_sort tb1.sort tb2.sort
     | ord -> ord)
  | ord ->  ord

and compare_tbs tbs1 tbs2 =
  match (tbs1, tbs2) with
  | [], [] -> 0
  | [], (_::_) -> -1
  | (_::_, []) -> 1
  | (tb1::tbs1, tb2 :: tbs2) ->
    (match compare_tb tb1 tb2 with
     | 0 -> compare_tbs tbs1 tbs2
     | ord -> ord)

and compare_fld fld1 fld2 =
  match String.compare fld1.lab fld2.lab with
  | 0 ->
    (match compare_typ fld1.typ fld2.typ with
     | 0 -> compare_src fld1.src fld2.src
     | ord -> ord)
  | ord -> ord

and compare_flds flds1 flds2 =
  match (flds1, flds2) with
  | [], [] -> 0
  | [], (_::_) -> -1
  | (_::_, []) -> 1
  | (fld1::flds1, fld2 :: flds2) ->
    (match compare_fld fld1 fld2 with
     | 0 -> compare_flds flds1 flds2
     | ord -> ord)

and compare_typs ts1 ts2 =
  match (ts1, ts2) with
  | [], [] -> 0
  | [], (_::_) -> -1
  | (t1::ts1, t2 :: ts2) ->
    (match compare_typ t1 t2 with
     | 0 -> compare_typs ts1 ts2
     | ord -> ord)
  | _ -> 1


let compare_rel (t1, t2) (u1, u2) =
  match compare_typ (t1 : typ) (u1 : typ) with
  | 0 -> compare_typ (t2 : typ) (u2 : typ)
  | ord -> ord

(* Syntactic orderings *)

module Ord = struct
  type t = typ
  let compare = compare_typ
end

module OrdPair = struct
  type t = typ * typ
  let compare = compare_rel
end

(* Function sorts *)

let is_shared_sort sort = sort <> Local

(* Constructors *)

let set_kind c k =
  match Cons.kind c with
  | Abs (_, Pre) -> Cons.unsafe_set_kind c k
  | _ -> raise (Invalid_argument "set_kind")

module ConEnv = Env.Make(struct type t = con let compare = Cons.compare end)

module ConSet = ConEnv.Dom


(* Field ordering *)

let compare_field f1 f2 =
  match f1,f2 with
  | {lab = l1; typ = Typ _; _}, {lab = l2; typ = Typ _; _} -> compare l1 l2
  | {lab = l1; typ = Typ _; _}, {lab = l2; typ = _; _} -> -1
  | {lab = l1; typ = _; _}, {lab = l2; typ = Typ _; _} -> 1
  | {lab = l1; typ = _; _}, {lab = l2; typ = _; _} -> compare l1 l2


(* Short-hands *)

let unit = Tup []
let bool = Prim Bool
let nat = Prim Nat
let nat64 = Prim Nat64
let int = Prim Int
let text = Prim Text
let blob = Prim Blob
let error = Prim Error
let char = Prim Char
let principal = Prim Principal
let region = Prim Region

let fields flds =
  List.sort compare_field
    (List.map (fun (lab, typ) -> {lab; typ; src = empty_src}) flds)

let obj sort flds =
  Obj (sort, fields flds)

let sum flds =
  Variant (fields flds)

let throwErrorCodes = List.sort compare_field [
  { lab = "canister_reject"; typ = unit; src = empty_src}
]

let call_error = Obj(Object,[{ lab = "err_code"; typ = Prim Nat32; src = empty_src}])

let catchErrorCodes = List.sort compare_field (
  throwErrorCodes @ [
    { lab = "system_fatal"; typ = unit; src = empty_src};
    { lab = "system_transient"; typ = unit; src = empty_src};
    { lab = "destination_invalid"; typ = unit; src = empty_src};
    { lab = "canister_error"; typ = unit; src = empty_src};
    { lab = "future"; typ = Prim Nat32; src = empty_src};
    { lab = "call_error"; typ = call_error; src = empty_src};
  ])

let throw = Prim Error
let catch = Prim Error

(* Shared call context *)

let caller = principal
let ctxt = Obj (Object,[{ lab = "caller"; typ = caller; src = empty_src}])

let prim = function
  | "Null" -> Null
  | "Bool" -> Bool
  | "Nat" -> Nat
  | "Nat8" -> Nat8
  | "Nat16" -> Nat16
  | "Nat32" -> Nat32
  | "Nat64" -> Nat64
  | "Int" -> Int
  | "Int8" -> Int8
  | "Int16" -> Int16
  | "Int32" -> Int32
  | "Int64" -> Int64
  | "Float" -> Float
  | "Char" -> Char
  | "Text" -> Text
  | "Blob" -> Blob
  | "Error" -> Error
  | "Principal" -> Principal
  | "Region" -> Region
  | s -> raise (Invalid_argument ("Type.prim: " ^ s))

let seq = function [t] -> t | ts -> Tup ts

let codom c to_scope ts2 =  match c with
  | Promises -> Async (Fut, to_scope(), seq ts2)
  | Returns -> seq ts2
  | Replies -> Tup []

(* Coercions *)

let iter_obj t =
  Obj (Object,
    [{lab = "next"; typ = Func (Local, Returns, [], [], [Opt t]); src = empty_src}])


(* Shifting *)

let rec shift i n t =
  match t with
  | Prim _ -> t
  | Var (s, j) -> Var (s, if j < i then j else j + n)
  | Con (c, ts) -> Con (c, List.map (shift i n) ts)
  | Array t -> Array (shift i n t)
  | Tup ts -> Tup (List.map (shift i n) ts)
  | Func (s, c, tbs, ts1, ts2) ->
    let i' = i + List.length tbs in
    Func (s, c, List.map (shift_bind i' n) tbs, List.map (shift i' n) ts1, List.map (shift i' n) ts2)
  | Opt t -> Opt (shift i n t)
  | Async (s, t1, t2) -> Async (s, shift i n t1, shift i n t2)
  | Obj (s, fs) -> Obj (s, List.map (shift_field n i) fs)
  | Variant fs -> Variant (List.map (shift_field n i) fs)
  | Mut t -> Mut (shift i n t)
  | Any -> Any
  | Non -> Non
  | Pre -> Pre
  | Typ c -> Typ c

and shift_bind i n tb =
  {tb with bound = shift i n tb.bound}

and shift_field i n {lab; typ; src} =
  {lab; typ = shift i n typ; src}

(*
and shift_kind i n k =
  match k with
  | Def (tbs, t) ->
    let i' = i + List.length tbs in
    Def (List.map (shift_bind i' n) tbs, shift i' n t)
  | Abs (tbs, t) ->
    let i' = i + List.length tbs in
    Abs (List.map (shift_bind i' n) tbs, shift i' n t)
 *)


(* First-order substitution *)

let rec subst sigma t =
  if sigma = ConEnv.empty then t else
  match t with
  | Prim _
  | Var _ -> t
  | Con (c, ts) ->
    (match ConEnv.find_opt c sigma with
    | Some t -> assert (List.length ts = 0); t
    | None -> Con (c, List.map (subst sigma) ts)
    )
  | Array t -> Array (subst sigma t)
  | Tup ts -> Tup (List.map (subst sigma) ts)
  | Func (s, c, tbs, ts1, ts2) ->
    let sigma' = ConEnv.map (shift 0 (List.length tbs)) sigma in
    Func (s, c, List.map (subst_bind sigma') tbs,
          List.map (subst sigma') ts1, List.map (subst sigma') ts2)
  | Opt t -> Opt (subst sigma t)
  | Async (s, t1, t2) -> Async (s, subst sigma t1, subst sigma t2)
  | Obj (s, fs) -> Obj (s, List.map (subst_field sigma) fs)
  | Variant fs -> Variant (List.map (subst_field sigma) fs)
  | Mut t -> Mut (subst sigma t)
  | Any -> Any
  | Non -> Non
  | Pre -> Pre
  | Typ c -> Typ c (* NB: this is incorrect unless we ensure dom(sigma) \cap FV(c.kind) = {}
                      For now, we could do this by ensuring all type definitions are closed,
                      in particular, type components defined within the scope of an enclosing
                      type parameter cannot mention that parameter
                      (but can mention other (closed) type constructors).
                    *)

and subst_bind sigma tb =
  { tb with bound = subst sigma tb.bound}

and subst_field sigma {lab; typ; src} =
  {lab; typ = subst sigma typ; src}

(*
and subst_kind sigma k =
  match k with
  | Def (tbs, t) ->
    let sigma' = ConEnv.map (shift 0 (List.length tbs)) sigma in
    Def (List.map (subst_bind sigma') tbs, subst sigma' t)
  | Abs (tbs, t) ->
    let sigma' = ConEnv.map (shift 0 (List.length tbs)) sigma in
    Abs (List.map (subst_bind sigma') tbs, subst sigma' t)
 *)

(* Handling binders *)

let close cs t =
  if cs = [] then t else
  let ts = List.mapi (fun i c -> Var (Cons.name c, i)) cs in
  let sigma = List.fold_right2 ConEnv.add cs ts ConEnv.empty in
  subst sigma t

let close_binds cs tbs =
  if cs = [] then tbs else
  List.map (fun tb -> { tb with bound = close cs tb.bound })  tbs


let rec open' i ts t =
  match t with
  | Prim _ -> t
  | Var (_, j) -> if j < i then t else List.nth ts (j - i)
  | Con (c, ts') -> Con (c, List.map (open' i ts) ts')
  | Array t -> Array (open' i ts t)
  | Tup ts' -> Tup (List.map (open' i ts) ts')
  | Func (s, c, tbs, ts1, ts2) ->
    let i' = i + List.length tbs in
    Func (s, c, List.map (open_bind i' ts) tbs, List.map (open' i' ts) ts1, List.map (open' i' ts) ts2)
  | Opt t -> Opt (open' i ts t)
  | Async (s, t1, t2) -> Async (s, open' i ts t1, open' i ts t2)
  | Obj (s, fs) -> Obj (s, List.map (open_field i ts) fs)
  | Variant fs -> Variant (List.map (open_field i ts) fs)
  | Mut t -> Mut (open' i ts t)
  | Any -> Any
  | Non -> Non
  | Pre -> Pre
  | Typ c -> Typ c

and open_bind i ts tb  =
  {tb with bound = open' i ts tb.bound}

and open_field i ts {lab; typ; src} =
  {lab; typ = open' i ts typ; src}

(*
and open_kind i ts k =
  match k with
  | Def (tbs, t) ->
    let i' = i + List.length tbs in
    Def (List.map (open_bind i' ts) tbs, open' i' ts t)
  | Abs (tbs, t) ->
    let i' = i + List.length tbs in
    Abs (List.map (open_bind i' ts) tbs, open' i' ts t)
*)

let open_ ts t =
  if ts = [] then t else
  open' 0 ts t

let open_binds tbs =
  if tbs = [] then [] else
  let cs = List.map (fun {var; _} -> Cons.fresh var (Abs ([], Pre))) tbs in
  let ts = List.map (fun c -> Con (c, [])) cs in
  let ks = List.map (fun {bound; _} -> Abs ([], open_ ts bound)) tbs in
  List.iter2 set_kind cs ks;
  ts


(* Normalization and Classification *)

let reduce tbs t ts =
  assert (List.length ts = List.length tbs);
  open_ ts t

let rec normalize = function
  | Con (con, ts) as t ->
    (match Cons.kind con with
    | Def (tbs, t) -> normalize (reduce tbs t ts)
    | _ -> t
    )
  | Mut t -> Mut (normalize t)
  | t -> t

let rec promote = function
  | Con (con, ts) ->
    let Def (tbs, t) | Abs (tbs, t) = Cons.kind con
    in promote (reduce tbs t ts)
  | t -> t


(* Projections *)

let is_non = function Non -> true | _ -> false
let is_prim p = function Prim p' -> p = p' | _ -> false
let is_obj = function Obj _ -> true | _ -> false
let is_module = function Obj (Module, _) -> true | _ -> false
let is_variant = function Variant _ -> true | _ -> false
let is_array = function Array _ -> true | _ -> false
let is_opt = function Opt _ -> true | _ -> false
let is_tup = function Tup _ -> true | _ -> false
let is_unit = function Tup [] -> true | _ -> false
let is_pair = function Tup [_; _] -> true | _ -> false
let is_func = function Func _ -> true | _ -> false
let is_async = function Async _ -> true | _ -> false
let is_mut = function Mut _ -> true | _ -> false
let is_typ = function Typ _ -> true | _ -> false
let is_con = function Con _ -> true | _ -> false

let invalid s = raise (Invalid_argument ("Type." ^ s))

let as_prim p = function Prim p' when p = p' -> () | _ -> invalid "as_prim"
let as_obj = function Obj (s, tfs) -> s, tfs | _ -> invalid "as_obj"
let as_array = function Array t -> t | _ -> invalid "as_array"
let as_opt = function Opt t -> t | _ -> invalid "as_opt"
let as_variant = function Variant fs -> fs | _ -> invalid "as_variant"
let as_tup = function Tup ts -> ts | _ -> invalid "as_tup"
let as_unit = function Tup [] -> () | _ -> invalid "as_unit"
let as_pair = function Tup [t1; t2] -> t1, t2 | _ -> invalid "as_pair"
let as_func = function Func (s, c, tbs, ts1, ts2) -> s, c, tbs, ts1, ts2 | _ -> invalid "as_func"
let as_async = function Async (s, t1, t2) -> (s, t1, t2) | _ -> invalid "as_async"
let as_mut = function Mut t -> t | _ -> invalid "as_mut"
let as_immut = function Mut t -> t | t -> t
let as_typ = function Typ c -> c | _ -> invalid "as_typ"
let as_con = function Con (c, ts) -> c, ts | _ -> invalid "as_con"

let as_seq t =
  match normalize t with
  | Tup ts -> ts
  | t -> [t]

let seq_of_tup t =
  match normalize t with
  | Tup ts -> ts
  | t -> invalid "seq_of_tup"

let arity t =
  match normalize t with
  | Tup ts -> List.length ts
  | t -> 1

let as_prim_sub p t = match promote t with
  | Prim p' when p = p' -> ()
  | Non -> ()
  | _ -> invalid "as_prim_sub"
let as_obj_sub ls t = match promote t with
  | Obj (s, tfs) -> s, tfs
  | Non -> Object, List.map (fun l -> {lab = l; typ = Non; src = empty_src}) ls
  | _ -> invalid "as_obj_sub"
let as_variant_sub l t = match promote t with
  | Variant tfs -> tfs
  | Non -> [{lab = l; typ = Non; src = empty_src}]
  | _ -> invalid "as_variant_sub"
let as_array_sub t = match promote t with
  | Array t -> t
  | Non -> Non
  | _ -> invalid "as_array_sub"
let as_opt_sub t = match promote t with
  | Opt t -> t
  | Prim Null -> Non
  | Non -> Non
  | _ -> invalid "as_opt_sub"
let as_tup_sub n t = match promote t with
  | Tup ts -> ts
  | Non -> Lib.List.make n Non
  | _ -> invalid "as_tup_sub"
let as_unit_sub t = match promote t with
  | Tup []
  | Non -> ()
  | _ -> invalid "as_unit_sub"
let as_pair_sub t = match promote t with
  | Tup [t1; t2] -> t1, t2
  | Non -> Non, Non
  | _ -> invalid "as_pair_sub"
let as_func_sub default_s default_arity t = match promote t with
  | Func (s, c, tbs, ts1, ts2) ->
    s, tbs, seq ts1, codom c (fun () -> Var((List.hd tbs).var, 0)) ts2
  | Non -> default_s, Lib.List.make default_arity {var = "X"; sort = Type; bound = Any}, Any, Non
  | _ -> invalid "as_func_sub"
let as_mono_func_sub t = match promote t with
  | Func (_, _, [], ts1, ts2) -> seq ts1, seq ts2
  | Non -> Any, Non
  | _ -> invalid "as_mono_func_sub"
let as_async_sub s default_scope t = match promote t with
  | Async (s0, t1, t2) when s = s0 -> (t1, t2)
  | Non -> default_scope, Non (* TBR *)
  | _ -> invalid "as_async_sub"

let is_immutable_obj obj_type =
  let _, fields = as_obj_sub [] obj_type in
  List.for_all (fun f -> not (is_mut f.typ)) fields


let lookup_val_field_opt l tfs =
  let is_lab = function {typ = Typ _; _} -> false | {lab; _} -> lab = l in
  match List.find_opt is_lab tfs with
  | Some tf -> Some tf.typ
  | None -> None

let lookup_typ_field_opt l tfs =
  let is_lab = function {typ = Typ _; lab; _} -> lab = l | _ -> false in
  match List.find_opt is_lab tfs with
  | Some {typ = Typ c; _} -> Some c
  | _ -> None

let lookup_val_field l tfs =
  match lookup_val_field_opt l tfs with
  | Some t -> t
  | None -> invalid "lookup_val_field"

let lookup_typ_field l tfs =
  match lookup_typ_field_opt l tfs with
  | Some c -> c
  | _ -> invalid "lookup_typ_field"


let lookup_val_deprecation l tfs =
  let is_lab = function {typ = Typ _; _} -> false | {lab; _} -> lab = l in
  match List.find_opt is_lab tfs with
  | Some {src = {depr; _}; _} -> depr
  | None -> invalid "lookup_val_deprecation"

let lookup_typ_deprecation l tfs =
  let is_lab = function {typ = Typ _; lab; _} -> lab = l | _ -> false in
  match List.find_opt is_lab tfs with
  | Some {src = {depr; _}; _} -> depr
  | _ -> invalid "lookup_typ_deprecation"


(* Span *)

let rec span = function
  | Var _ | Pre -> assert false
  | Con _ as t -> span (promote t)
  | Prim Null -> Some 1
  | Prim Bool -> Some 2
  | Prim (Nat | Int | Float | Text | Blob | Error | Principal | Region) -> None
  | Prim (Nat8 | Int8) -> Some 0x100
  | Prim (Nat16 | Int16) -> Some 0x10000
  | Prim (Nat32 | Int32 | Nat64 | Int64 | Char) -> None  (* for all practical purposes *)
  | Obj _ | Tup _ | Async _ -> Some 1
  | Variant fs -> Some (List.length fs)
  | Array _ | Func _ | Any -> None
  | Opt _ -> Some 2
  | Mut t -> span t
  | Non -> Some 0
  | Typ _ -> Some 1


(* Collecting type constructors *)

(* Parameter `inTyp` controls whether to count a constructor, `c`,  that only occurs as an argument of
   `Typ` field, `Typ c`, but not in its own unfolding.
   Set to false to avoid emitting redundant bindings in stable signature.
*)

let rec cons' inTyp t cs =
  match t with
  | Var _ | Prim _ | Any | Non | Pre -> cs
  | Con (c, ts) ->
    List.fold_right (cons' inTyp) ts (cons_con inTyp c cs)
  | Opt t | Mut t | Array t ->
    cons' inTyp t cs
  | Async (_, t1, t2) ->
    cons' inTyp t2 (cons' inTyp t1 cs)
  | Tup ts -> List.fold_right (cons' inTyp) ts cs
  | Func (s, c, tbs, ts1, ts2) ->
    let cs = List.fold_right (cons_bind inTyp) tbs cs in
    let cs = List.fold_right (cons' inTyp) ts1 cs in
    List.fold_right (cons' inTyp) ts2 cs
  | Obj (_, fs) | Variant fs ->
    List.fold_right (cons_field inTyp) fs cs
  | Typ c ->
    if inTyp then
      cons_con inTyp c cs
    else
      (* don't add c unless mentioned in Cons.kind c *)
      cons_kind' inTyp (Cons.kind c) cs

and cons_con inTyp c cs =
  if ConSet.mem c cs
  then cs
  else cons_kind' inTyp (Cons.kind c) (ConSet.add c cs)

and cons_bind inTyp tb cs =
  cons' inTyp tb.bound cs

and cons_field inTyp {lab; typ; src} cs =
  cons' inTyp typ cs

and cons_kind' inTyp k cs =
  match k with
  | Def (tbs, t)
  | Abs (tbs, t) ->
    cons' inTyp t (List.fold_right (cons_bind inTyp) tbs cs)

let cons t = cons' true t ConSet.empty
let cons_kind k = cons_kind' true k ConSet.empty

(* Checking for concrete types *)

module S = Set.Make (Ord)

(*
This check is a stop-gap measure until we have an IDL strategy that
allows polymorphic types, see #250. It is not what we desire for Motoko.
*)

let concrete t =
  let seen = ref S.empty in
  let rec go t =
    S.mem t !seen ||
    begin
      seen := S.add t !seen;
      match t with
      | Var _ | Pre -> assert false
      | Prim _ | Any | Non -> true
      | Con (c, ts) ->
        (match Cons.kind c with
        | Abs _ -> false
        | Def (_, t) -> go (open_ ts t) (* TBR this may fail to terminate *)
        )
      | Array t | Opt t | Mut t -> go t
      | Async (s, t1, t2) -> go t2 (* t1 is a phantom type *)
      | Tup ts -> List.for_all go ts
      | Obj (_, fs) | Variant fs -> List.for_all (fun f -> go f.typ) fs
      | Func (s, c, tbs, ts1, ts2) ->
        let ts = open_binds tbs in
        List.for_all go (List.map (open_ ts) ts1) &&
        List.for_all go (List.map (open_ ts) ts2)
      | Typ c -> (* assumes type defs are closed *)
        true (* so we can transmit actors with typ fields *)
    end
  in go t

(* stable or shared *)
let serializable allow_mut t =
  let seen = ref S.empty in
  let rec go t =
    S.mem t !seen ||
    begin
      seen := S.add t !seen;
      match t with
      | Var _ | Pre -> assert false
      | Prim Error -> false
      | Prim Region -> allow_mut (* stable, but not shared *)
      | Any | Non | Prim _ | Typ _ -> true
      | Async _ -> false
      | Mut t -> allow_mut && go t
      | Con (c, ts) ->
        (match Cons.kind c with
        | Abs _ -> false
        | Def (_, t) -> go (open_ ts t) (* TBR this may fail to terminate *)
        )
      | Array t | Opt t -> go t
      | Tup ts -> List.for_all go ts
      | Obj (s, fs) ->
        (match s with
         | Actor -> true
         | Module -> false (* TODO(1452) make modules sharable *)
         | Object | Memory -> List.for_all (fun f -> go f.typ) fs)
      | Variant fs -> List.for_all (fun f -> go f.typ) fs
      | Func (s, c, tbs, ts1, ts2) -> is_shared_sort s
    end
  in go t

(* Find the first unshared subexpression in a type *)
let find_unshared t =
  let seen = ref S.empty in
  let rec go t =
    if S.mem t !seen then None else
    begin
      seen := S.add t !seen;
      match t with
      | Var _ | Pre -> assert false
      | Prim Error -> Some t
      | Any | Non | Prim _ | Typ _ -> None
      | Async _ | Mut _ -> Some t
      | Con (c, ts) ->
        (match Cons.kind c with
        | Abs _ -> None
        | Def (_, t) -> go (open_ ts t) (* TBR this may fail to terminate *)
        )
      | Array t | Opt t -> go t
      | Tup ts -> List.find_map go ts
      | Obj (s, fs) ->
        (match s with
         | Actor -> None
         | Module -> Some t (* TODO(1452) make modules sharable *)
         | Object ->
           List.find_map (fun f -> go f.typ) fs
         | Memory -> assert false)
      | Variant fs -> List.find_map (fun f -> go f.typ) fs
      | Func (s, c, tbs, ts1, ts2) ->
        if is_shared_sort s
        then None
        else Some t
    end
  in go t

let is_shared_func typ =
  match promote typ with
  | Func (Shared _, _, _, _, _) -> true
  | _ -> false

let is_local_async_func typ =
  match promote typ with
  | Func
      (Local, Returns,
       { sort = Scope; _ }::_,
       _,
       [Async (Fut, Var (_ ,0), _)]) ->
    true
  | _ ->
    false

let shared t = serializable false t
let stable t = serializable true t


(* Forward declare
   TODO: haul string_of_typ before the lub/glb business, if possible *)
let str = ref (fun _ -> failwith "")


(* Equivalence & Subtyping *)

exception PreEncountered

exception Undecided

module SS = Set.Make (OrdPair)

let max_depth = 10_000

let rel_list d p rel eq xs1 xs2 =
  try List.for_all2 (p d rel eq) xs1 xs2 with Invalid_argument _ -> false

let rec rel_typ d rel eq t1 t2 =
  let d = d + 1 in
  if d > max_depth then raise Undecided else
  t1 == t2 || SS.mem (t1, t2) !rel || begin
  rel := SS.add (t1, t2) !rel;
  match t1, t2 with
  (* Second-class types first, since they mustn't relate to Any/Non *)
  | Pre, _ | _, Pre ->
    raise PreEncountered
  | Mut t1', Mut t2' ->
    eq_typ d rel eq t1' t2'
  | Typ c1, Typ c2 ->
    eq_con d eq c1 c2
  | Mut _, _ | _, Mut _
  | Typ _, _ | _, Typ _ ->
    false
  | Any, Any ->
    true
  | _, Any when rel != eq ->
    true
  | Non, Non ->
    true
  | Non, _ when rel != eq ->
    true
  | Con (con1, ts1), Con (con2, ts2) ->
    (match Cons.kind con1, Cons.kind con2 with
    | Def (tbs, t), _ -> (* TBR this may fail to terminate *)
      rel_typ d rel eq (open_ ts1 t) t2
    | _, Def (tbs, t) -> (* TBR this may fail to terminate *)
      rel_typ d rel eq t1 (open_ ts2 t)
    | _ when Cons.eq con1 con2 ->
      rel_list d eq_typ rel eq ts1 ts2
    | Abs (tbs, t), _ when rel != eq ->
      rel_typ d rel eq (open_ ts1 t) t2
    | _ ->
      false
    )
  | Con (con1, ts1), t2 ->
    (match Cons.kind con1, t2 with
    | Def (tbs, t), _ -> (* TBR this may fail to terminate *)
      rel_typ d rel eq (open_ ts1 t) t2
    | Abs (tbs, t), _ when rel != eq ->
      rel_typ d rel eq (open_ ts1 t) t2
    | _ -> false
    )
  | t1, Con (con2, ts2) ->
    (match Cons.kind con2 with
    | Def (tbs, t) -> (* TBR this may fail to terminate *)
      rel_typ d rel eq t1 (open_ ts2 t)
    | _ -> false
    )
  | Prim p1, Prim p2 when p1 = p2 ->
    true
  | Prim p1, Prim p2 when rel != eq ->
    p1 = Nat && p2 = Int
  | Obj (s1, tfs1), Obj (s2, tfs2) ->
    s1 = s2 &&
    rel_fields d rel eq tfs1 tfs2
  | Array t1', Array t2' ->
    rel_typ d rel eq t1' t2'
  | Opt t1', Opt t2' ->
    rel_typ d rel eq t1' t2'
  | Prim Null, Opt t2' when rel != eq ->
    true
  | Variant fs1, Variant fs2 ->
    rel_tags d rel eq fs1 fs2
  | Tup ts1, Tup ts2 ->
    rel_list d rel_typ rel eq ts1 ts2
  | Func (s1, c1, tbs1, t11, t12), Func (s2, c2, tbs2, t21, t22) ->
    s1 = s2 && c1 = c2 &&
    (match rel_binds d eq eq tbs1 tbs2 with
    | Some ts ->
      rel_list d rel_typ rel eq (List.map (open_ ts) t21) (List.map (open_ ts) t11) &&
      rel_list d rel_typ rel eq (List.map (open_ ts) t12) (List.map (open_ ts) t22)
    | None -> false
    )
  | Async (s1, t11, t12), Async (s2, t21, t22) ->
    s1 = s2 &&
    eq_typ d rel eq t11 t21 &&
    rel_typ d rel eq t12 t22
  | _, _ -> false
  end

and rel_fields d rel eq tfs1 tfs2 =
  (* Assume that tfs1 and tfs2 are sorted. *)
  match tfs1, tfs2 with
  | [], [] ->
    true
  | _, [] when rel != eq ->
    true
  | tf1::tfs1', tf2::tfs2' ->
    (match compare_field tf1 tf2 with
    | 0 ->
      rel_typ d rel eq tf1.typ tf2.typ &&
      rel_fields d rel eq tfs1' tfs2'
    | -1 when rel != eq ->
      rel_fields d rel eq tfs1' tfs2
    | _ -> false
    )
  | _, _ -> false

and rel_tags d rel eq tfs1 tfs2 =
  (* Assume that tfs1 and tfs2 are sorted. *)
  match tfs1, tfs2 with
  | [], [] ->
    true
  | [], _ when rel != eq ->
    true
  | tf1::tfs1', tf2::tfs2' ->
    (match compare_field tf1 tf2 with
    | 0 ->
      rel_typ d rel eq tf1.typ tf2.typ &&
      rel_tags d rel eq tfs1' tfs2'
    | +1 when rel != eq ->
      rel_tags d rel eq tfs1 tfs2'
    | _ -> false
    )
  | _, _ -> false

and rel_binds d rel eq tbs1 tbs2 =
  let ts = open_binds tbs2 in
  if rel_list d (rel_bind ts) rel eq tbs2 tbs1
  then Some ts
  else None

and rel_bind ts d rel eq tb1 tb2 =
  tb1.sort == tb2.sort &&
  rel_typ d rel eq (open_ ts tb1.bound) (open_ ts tb2.bound)

and eq_typ d rel eq t1 t2 = rel_typ d eq eq t1 t2

and eq t1 t2 : bool =
  let eq = ref SS.empty in eq_typ 0 eq eq t1 t2

and sub t1 t2 : bool =
  rel_typ 0 (ref SS.empty) (ref SS.empty) t1 t2

and eq_binds tbs1 tbs2 =
  let eq = ref SS.empty in rel_binds 0 eq eq tbs1 tbs2 <> None

and eq_kind' eq k1 k2 : bool =
  match k1, k2 with
  | Def (tbs1, t1), Def (tbs2, t2)
  | Abs (tbs1, t1), Abs (tbs2, t2) ->
    (match rel_binds 0 eq eq tbs1 tbs2 with
    | Some ts -> eq_typ 0 eq eq (open_ ts t1) (open_ ts t2)
    | None -> false
    )
  | _ -> false

and eq_con d eq c1 c2 =
  match Cons.kind c1, Cons.kind c2 with
  | (Def (tbs1, t1)) as k1, (Def (tbs2, t2) as k2) ->
    eq_kind' eq k1 k2
  | Abs _, Abs _ ->
    Cons.eq c1 c2
  | Def (tbs1, t1), Abs (tbs2, t2)
  | Abs (tbs2, t2), Def (tbs1, t1) ->
    (match rel_binds d eq eq tbs1 tbs2 with
    | Some ts -> eq_typ d eq eq (open_ ts t1) (Con (c2, ts))
    | None -> false
    )

let eq_kind k1 k2 : bool = eq_kind' (ref SS.empty) k1 k2


(* Compatibility *)

let compatible_list p co xs1 xs2 =
  try List.for_all2 (p co) xs1 xs2 with Invalid_argument _ -> false

let rec compatible_typ co t1 t2 =
  t1 == t2 || SS.mem (t1, t2) !co || begin
  co := SS.add (t1, t2) !co;
  match promote t1, promote t2 with
  | Pre, _ | _, Pre ->
    assert false
  | Mut t1', Mut t2' ->
    compatible_typ co t1' t2'
  | Typ _, Typ _ ->
    true
  | Mut _, _ | _, Mut _
  | Typ _, _ | _, Typ _ ->
    false
  | Any, Any ->
    true
  | Any, _ | _, Any ->
    false
  | Non, _ | _, Non ->
    true
  | Prim p1, Prim p2 when p1 = p2 ->
    true
  | Prim (Nat | Int), Prim (Nat | Int) ->
    true
  | Array t1', Array t2' ->
    compatible_typ co t1' t2'
  | Tup ts1, Tup ts2 ->
    compatible_list compatible_typ co ts1 ts2
  | Obj (s1, tfs1), Obj (s2, tfs2) ->
    s1 = s2 &&
    compatible_fields co tfs1 tfs2
  | Opt t1', Opt t2' ->
    compatible_typ co t1' t2'
  | Prim Null, Opt _ | Opt _, Prim Null  ->
    true
  | Variant tfs1, Variant tfs2 ->
    compatible_tags co tfs1 tfs2
  | Async (s1, t11, t12), Async (s2, t21, t22) ->
    s1 = s2 &&
    compatible_typ co t11 t21 && (* TBR *)
    compatible_typ co t12 t22
  | Func _, Func _ ->
    true
  | _, _ ->
    false
  end

and compatible_fields co tfs1 tfs2 =
  (* Assume that tfs1 and tfs2 are sorted. *)
  match tfs1, tfs2 with
  | [], [] -> true
  | tf1::tfs1', tf2::tfs2' ->
    tf1.lab = tf2.lab && compatible_typ co tf1.typ tf2.typ &&
    compatible_fields co tfs1' tfs2'
  | _, _ -> false

and compatible_tags co tfs1 tfs2 =
  (* Assume that tfs1 and tfs2 are sorted. *)
  match tfs1, tfs2 with
  | [], _ | _, [] -> true
  | tf1::tfs1', tf2::tfs2' ->
    match compare_field tf1 tf2 with
    | -1 -> compatible_tags co tfs1' tfs2
    | +1 -> compatible_tags co tfs1 tfs2'
    | _ -> compatible_typ co tf1.typ tf2.typ && compatible_tags co tfs1' tfs2'

and compatible t1 t2 : bool =
  compatible_typ (ref SS.empty) t1 t2


let opaque t = compatible t Any


(* Inhabitance *)

let rec inhabited_typ co t =
  S.mem t !co || begin
  co := S.add t !co;
  match promote t with
  | Pre -> assert false
  | Non -> false
  | Any | Prim _ | Array _ | Opt _ | Async _ | Func _ | Typ _ -> true
  | Mut t' -> inhabited_typ co t'
  | Tup ts -> List.for_all (inhabited_typ co) ts
  | Obj (_, tfs) -> List.for_all (inhabited_field co) tfs
  | Variant tfs -> List.exists (inhabited_field co) tfs
  | Var _ -> true  (* TODO(rossberg): consider bound *)
  | Con (c, ts) ->
    match Cons.kind c with
    | Def (tbs, t') -> (* TBR this may fail to terminate *)
      inhabited_typ co (open_ ts t')
    | Abs (tbs, t') ->
      inhabited_typ co t'
  end

and inhabited_field co tf = inhabited_typ co tf.typ

and inhabited t : bool = inhabited_typ (ref S.empty) t

let rec singleton_typ co t =
  S.mem t !co || begin
  co := S.add t !co;
  match normalize t with
  | Pre -> assert false
  | Prim Null | Any -> true
  | Tup ts -> List.for_all (singleton_typ co) ts
  | Obj ((Object|Memory|Module), fs) -> List.for_all (singleton_field co) fs
  | Variant [f] -> singleton_field co f

  | Non -> false
  | Prim _ | Array _ | Opt _ | Async _ | Func _ | Typ _ -> false
  | Mut t' -> false
  | Obj (_, _) -> false
  | Variant _ -> false
  | Var _ -> false
  | Con _ -> false
  end

and singleton_field co tf = singleton_typ co tf.typ

and singleton t : bool = singleton_typ (ref S.empty) t


(* Least upper bound and greatest lower bound *)

module M = Map.Make (OrdPair)

exception Mismatch

let rec combine rel lubs glbs t1 t2 =
  assert (rel == lubs || rel == glbs);
  if t1 == t2 then t1 else
  match M.find_opt (t1, t2) !rel with
  | Some t -> t
  | _ when eq t1 t2 ->
    let t = if is_con t2 then t2 else t1 in
    rel := M.add (t2, t1) t (M.add (t1, t2) t !rel);
    t
  | _ ->
    match t1, t2 with
    | Pre, _ | _, Pre ->
      raise PreEncountered
    | Mut _, _ | _, Mut _
    | Typ _, _ | _, Typ _ ->
      raise Mismatch
    | Any, t | t, Any ->
      if rel == lubs then Any else t
    | Non, t | t, Non ->
      if rel == lubs then t else Non
    | Prim Nat, Prim Int
    | Prim Int, Prim Nat ->
      Prim (if rel == lubs then Int else Nat)
    | Opt t1', Opt t2' ->
      Opt (combine rel lubs glbs t1' t2')
    | (Opt _ as t), (Prim Null as t')
    | (Prim Null as t'), (Opt _ as t) ->
      if rel == lubs then t else t'
    | Array t1', Array t2' ->
      (try Array (combine rel lubs glbs t1' t2')
      with Mismatch -> if rel == lubs then Any else Non)
    | Variant t1', Variant t2' ->
      Variant (combine_tags rel lubs glbs t1' t2')
    | Tup ts1, Tup ts2 when List.(length ts1 = length ts2) ->
      Tup (List.map2 (combine rel lubs glbs) ts1 ts2)
    | Obj (s1, tf1), Obj (s2, tf2) when s1 = s2 ->
      (try Obj (s1, combine_fields rel lubs glbs tf1 tf2)
      with Mismatch -> assert (rel == glbs); Non)
    | Func (s1, c1, bs1, ts11, ts12), Func (s2, c2, bs2, ts21, ts22) when
        s1 = s2 && c1 = c2 && eq_binds bs1 bs2 &&
        List.(length ts11 = length ts21 && length ts12 = length ts22) ->
      let ts = open_binds bs1 in
      let cs = List.map (fun t -> fst (as_con t)) ts in
      let opened = List.map (open_ ts) in
      let closed = List.map (close cs) in
      let rel' = if rel == lubs then glbs else lubs in
      Func (
        s1, c1, bs1,
        closed (List.map2 (combine rel' lubs glbs) (opened ts11) (opened ts21)),
        closed (List.map2 (combine rel lubs glbs) (opened ts12) (opened ts22))
      )
    | Async (s1, t11, t12), Async (s2, t21, t22) when s1 == s2 && eq t11 t21 ->
      Async (s1, t11, combine rel lubs glbs t12 t22)
    | Con _, _
    | _, Con _ ->
      if sub t1 t2 then
        let t = if rel == glbs then t1 else t2 in
        rel := M.add (t2, t1) t (M.add (t1, t2) t !rel);
        t
      else if sub t2 t1 then
        let t = if rel == lubs then t1 else t2 in
        rel := M.add (t2, t1) t (M.add (t1, t2) t !rel);
        t
      else
        let op, expand =
          if rel == lubs then "lub", promote else "glb", normalize in
        let name = op ^ "<" ^ !str t1 ^ ", " ^ !str t2 ^ ">" in
        let c = Cons.fresh name (Abs ([], Pre)) in
        let t = Con (c, []) in
        rel := M.add (t2, t1) t (M.add (t1, t2) t !rel);
        let t' =
          (* When taking the glb of an abstract con and an incompatible type,
           * normalisation will no further simplify t1 nor t2, so that t itself
           * is returned via the extended relation. In that case, bottom is
           * the correct result.
           *)
          match combine rel lubs glbs (expand t1) (expand t2) with
          | t' when t' == t -> assert (rel == glbs); Non
          | t' -> t'
        in
        set_kind c (Def ([], t'));
        t'
    | _, _ ->
      if rel == lubs then Any else Non

and cons_if b x xs = if b then x::xs else xs

and combine_fields rel lubs glbs fs1 fs2 =
  match fs1, fs2 with
  | _, [] -> if rel == lubs then [] else fs1
  | [], _ -> if rel == lubs then [] else fs2
  | f1::fs1', f2::fs2' ->
    match compare_field f1 f2 with
    | -1 -> cons_if (rel == glbs) f1 (combine_fields rel lubs glbs fs1' fs2)
    | +1 -> cons_if (rel == glbs) f2 (combine_fields rel lubs glbs fs1 fs2')
    | _ ->
      match combine rel lubs glbs f1.typ f2.typ with
      | typ ->
       {lab = f1.lab; typ; src = empty_src} :: combine_fields rel lubs glbs fs1' fs2'
      | exception Mismatch when rel == lubs ->
        combine_fields rel lubs glbs fs1' fs2'

and combine_tags rel lubs glbs fs1 fs2 =
  match fs1, fs2 with
  | _, [] -> if rel == lubs then fs1 else []
  | [], _ -> if rel == lubs then fs2 else []
  | f1::fs1', f2::fs2' ->
    match compare_field f1 f2 with
    | -1 -> cons_if (rel == lubs) f1 (combine_tags rel lubs glbs fs1' fs2)
    | +1 -> cons_if (rel == lubs) f2 (combine_tags rel lubs glbs fs1 fs2')
    | _ ->
      let typ = combine rel lubs glbs f1.typ f2.typ in
      {lab = f1.lab; typ; src = empty_src} :: combine_tags rel lubs glbs fs1' fs2'

let lub t1 t2 = let lubs = ref M.empty in combine lubs lubs (ref M.empty) t1 t2
let glb t1 t2 = let glbs = ref M.empty in combine glbs (ref M.empty) glbs t1 t2


(* Environments *)

module Env = Env.Make(String)

(* Scopes *)

let scope_var var = "$" ^ var
let default_scope_var = scope_var ""
let scope_bound = Any
let scope_bind = { var = default_scope_var; sort = Scope; bound = scope_bound }

(* Well-known fields *)

let motoko_async_helper_fld =
  { lab = "__motoko_async_helper";
    typ = Func(Shared Write, Promises, [scope_bind], [Prim Nat32], []);
    src = empty_src;
  }

let motoko_stable_var_info_fld =
  { lab = "__motoko_stable_var_info";
    typ =
      Func(Shared Query, Promises, [scope_bind], [],
        [ Obj(Object, [{lab = "size"; typ = nat64; src = empty_src}]) ]);
    src = empty_src;
  }

let motoko_gc_trigger_fld =
  { lab = "__motoko_gc_trigger";
    typ = Func(Shared Write, Promises, [scope_bind], [], []);
    src = empty_src;
  }

let motoko_runtime_information_type =
  Obj(Object, [
    (* Fields must be sorted by label *)
    {lab = "callbackTableCount"; typ = nat; src = empty_src};
    {lab = "callbackTableSize"; typ = nat; src = empty_src};
    {lab = "compilerVersion"; typ = text; src = empty_src};
    {lab = "garbageCollector"; typ = text; src = empty_src};
    {lab = "heapSize"; typ = nat; src = empty_src};
    {lab = "logicalStableMemorySize"; typ = nat; src = empty_src};
    {lab = "maxLiveSize"; typ = nat; src = empty_src};
    {lab = "maxStackSize"; typ = nat; src = empty_src};
    {lab = "memorySize"; typ = nat; src = empty_src};
    {lab = "reclaimed"; typ = nat; src = empty_src};
    {lab = "rtsVersion"; typ = text; src = empty_src};
    {lab = "sanityChecks"; typ = bool; src = empty_src};
    {lab = "stableMemorySize"; typ = nat; src = empty_src};
    {lab = "totalAllocation"; typ = nat; src = empty_src};
  ])

let motoko_runtime_information_fld =
  { lab = "__motoko_runtime_information";
    typ = Func(Shared Query, Promises, [scope_bind], [],
      [ motoko_runtime_information_type ]);
    src = empty_src;
  }

let well_known_actor_fields = [
    motoko_async_helper_fld;
    motoko_stable_var_info_fld;
    motoko_gc_trigger_fld;
  ]

let decode_msg_typ tfs =
  Variant
    (List.sort compare_field (List.filter_map (fun tf ->
       match normalize tf.typ with
       | Func(Shared (Write | Query), _, tbs, ts1, ts2) ->
         Some { tf with
           typ =
             Func(Local, Returns, [], [],
               List.map (open_ (List.map (fun _ -> Non) tbs)) ts1);
           src = empty_src }
       | _ -> None)
     tfs))

let canister_settings_typ =
  obj Object [
    "settings",
    Opt (
      obj Object [
      ("controllers", Opt (Array principal));
      ("compute_allocation", Opt nat);
      ("memory_allocation", Opt nat);
      ("freezing_threshold", Opt nat)])]

let wasm_memory_persistence_typ =
  sum [
    ("Keep", unit);
    ("Replace", unit);
  ]

let upgrade_with_persistence_option_typ =
  obj Object [
    ("wasm_memory_persistence", wasm_memory_persistence_typ);
    ("canister", obj Actor []);
  ]

let install_arg_typ =
  sum [
    ("new", canister_settings_typ);
    ("install", principal);
    ("reinstall", obj Actor []);
    ("upgrade", obj Actor []);
    ("upgrade_with_persistence", upgrade_with_persistence_option_typ );
  ]

let install_typ ts actor_typ =
  Func(Local, Returns, [],
    [ install_arg_typ ],
    [ Func(Local, Returns, [scope_bind], ts, [Async (Fut, Var (default_scope_var, 0), actor_typ)]) ])


(* Pretty printing *)

let string_of_async_sort = function
  | Fut -> ""
  | Cmp -> "*"

let string_of_prim = function
  | Null -> "Null"
  | Bool -> "Bool"
  | Nat -> "Nat"
  | Nat8 -> "Nat8"
  | Nat16 -> "Nat16"
  | Nat32 -> "Nat32"
  | Nat64 -> "Nat64"
  | Int -> "Int"
  | Int8 -> "Int8"
  | Int16 -> "Int16"
  | Int32 -> "Int32"
  | Int64 -> "Int64"
  | Float -> "Float"
  | Char -> "Char"
  | Text -> "Text"
  | Blob -> "Blob"
  | Error -> "Error"
  | Principal -> "Principal"
  | Region -> "Region"

let string_of_obj_sort = function
  | Object -> ""
  | Module -> "module "
  | Actor -> "actor "
  | Memory -> "memory "

let string_of_func_sort = function
  | Local -> ""
  | Shared Write -> "shared "
  | Shared Query -> "shared query "
  | Shared Composite -> "shared composite query " (* TBR *)

(* PrettyPrinter configurations *)

module type PrettyConfig = sig
  val show_stamps : bool
  val con_sep : string
  val par_sep : string
end

module ShowStamps = struct
  let show_stamps = true
  let con_sep = "__" (* TODO: revert to "/" *)
  let par_sep = "_"
end

module ElideStamps = struct
  let show_stamps = false
  let con_sep = ShowStamps.con_sep
  let par_sep = ShowStamps.par_sep
end

module ParseableStamps = struct
  let show_stamps = true
  let con_sep = "__"
  let par_sep = "_"
end

module MakePretty(Cfg : PrettyConfig) = struct

open Format

let pr = pp_print_string

let comma ppf () = fprintf ppf ",@ "

let semi ppf () = fprintf ppf ";@ "

module StringSet = Set.Make(String)

let vs_of_cs cs =
  let names = ConSet.fold (fun c ns -> StringSet.add (Cons.name c) ns) cs StringSet.empty in
  StringSet.fold (fun n vs -> (n, 0)::vs) names []

let string_of_var (x, i) =
  if i = 0 then sprintf "%s" x else sprintf "%s%s%d" x Cfg.par_sep i

let string_of_con c = Cons.to_string Cfg.show_stamps Cfg.con_sep c

let rec can_sugar = function
  | Func(s, Promises, tbs, ts1, ts2)
  | Func((Shared _ as s), Returns, tbs, ts1, ([] as ts2))
  | Func(s, Returns, (_::_ as tbs), ts1, ([Async (_, Var(_, 0),_)] as ts2)) ->
    List.for_all (fun tb -> can_omit 0 tb.bound) tbs &&
    List.for_all (can_omit 0) ts1 &&
    List.for_all (can_omit 0) ts2
  | _ -> false

and can_omit n t =
  let rec go i = function
    | Var (_, j) -> i <> j
    | Pre -> assert false
    | Prim _ | Any | Non -> true
    | Con (c, ts) -> List.for_all (go i ) ts
    | Array t | Opt t | Mut t -> go i t
    | Async (s, Var (_, j), t2) when j = i && i <= n -> go i t2 (* t1 is a phantom type *)
    | Async (s, t1, t2) -> go i t1 && go i t2
    | Tup ts -> List.for_all (go i ) ts
    | Obj (_, fs) | Variant fs -> List.for_all (fun f -> go i f.typ) fs
    | Func (s, c, tbs, ts1, ts2) ->
      let i' = i+List.length tbs in
      List.for_all (fun tb -> go i' tb.bound) tbs &&
      List.for_all (go i') ts1 &&
      List.for_all (go i') ts2
    | Typ c -> true (* assumes type defs are closed *)
  in go n t

let rec pp_typ_obj vs ppf o =
  match o with
  | (Object, fs) ->
    fprintf ppf "@[<hv 2>{@;<0 0>%a@;<0 -2>}@]"
      (pp_print_list ~pp_sep:semi (pp_field vs)) fs
  | (s, fs) ->
    fprintf ppf "@[<hv 2>%s{@;<0 0>%a@;<0 -2>}@]"
      (string_of_obj_sort s)
      (pp_print_list ~pp_sep:semi (pp_field vs)) fs

and pp_typ_variant vs ppf fs =
  match fs with
  | [] -> pr ppf "{#}"
  | fs ->
    fprintf ppf "@[<hv 2>{@;<0 0>%a@;<0 -2>}@]"
      (pp_print_list ~pp_sep:semi (pp_tag vs)) fs

and pp_typ_nullary vs ppf t =
  match t with
  | Tup ts ->
    fprintf ppf "@[<1>(%a%s)@]"
      (pp_print_list ~pp_sep:comma (pp_typ' vs)) ts
      (if List.length ts = 1 then "," else "")
  | Pre -> pr ppf "???"
  | Any -> pr ppf "Any"
  | Non -> pr ppf "None"
  | Prim p -> pr ppf (string_of_prim p)
  | Var (s, i) ->
    pr ppf (try string_of_var (List.nth vs i) with _ -> Printf.sprintf "??? %s %i" s i)
  | Con (c, []) -> pr ppf (string_of_con c)
  | Con (c, ts) ->
    fprintf ppf "@[%s<@[<1>%a@]>@]" (string_of_con c)
      (pp_print_list ~pp_sep:comma (pp_typ' vs)) ts
  | Array (Mut t) ->
    fprintf ppf "@[<1>[var %a]@]" (pp_typ' vs) t
  | Array t ->
    fprintf ppf "@[<1>[%a]@]" (pp_typ' vs) t
  | Obj (Object, fs) ->
    pp_typ_obj vs ppf (Object, fs)
  | Variant fs ->
    pp_typ_variant vs ppf fs
  | t ->
    (* In the parser, this case is subsumed by the grammar production for `LPAR .. RPAR` *)
    fprintf ppf "@[<1>(%a)@]" (pp_typ' vs) t

and pp_typ_un vs ppf t =
  match t with
  | Opt t ->
    fprintf ppf "@[<1>?%a@]"  (pp_typ_un vs) t
  | t ->
    pp_typ_nullary vs ppf t

and pp_typ_pre vs ppf t =
  match t with
  (* No case for grammar production `PRIM s` *)
  | Async (s, t1, t2) ->
    if Cfg.show_stamps then
      match t1 with
      | Var(_, n) when fst (List.nth vs n) = "" ->
        fprintf ppf "@[<2>async%s@ %a@]" (string_of_async_sort s) (pp_typ_pre vs) t2
      | _ ->
        fprintf ppf "@[<2>async%s<%a>@ %a@]"
          (string_of_async_sort s)
          (pp_typ' vs) t1
          (pp_typ_pre vs) t2
    else fprintf ppf "@[<2>async%s@ %a@]" (string_of_async_sort s) (pp_typ_pre vs) t2
  | Obj ((Module | Actor | Memory) as os, fs) ->
    pp_typ_obj vs ppf (os, fs)
  | t ->
    pp_typ_un vs ppf t

and sequence pp ppf ts =
  match ts with
  | [Tup _] ->
    fprintf ppf "@[<1>(%a)@]" pp (seq ts)
  | ts ->
    pp ppf (seq ts)

and pp_typ_nobin vs ppf t =
  match t with
  | Func (s, c, tbs, ts1, ts2) ->
    let sugar = can_sugar t in
    let vs' = vars_of_binds vs tbs in
    let vs'', tbs' =
      if sugar then
        List.tl vs', List.tl tbs
      else
        match tbs with
        | { sort = Scope; _ } :: _ -> ("system", List.hd vs' |> snd) :: List.tl vs', tbs
        | _ -> vs', tbs
    in
    let vs'vs = vs' @ vs in
    fprintf ppf "@[<2>%s%a%a ->@ %a@]"
      (string_of_func_sort s)
      (pp_binds vs'vs vs'') tbs'
      (sequence (pp_typ_un vs'vs)) ts1
      (pp_control_cod sugar c vs'vs) ts2
  | t ->
     pp_typ_pre vs ppf t

and pp_control_cod sugar c vs ppf ts =
  match c, ts with
  (* sugar *)
  | Returns, [Async (s, _, t)] when sugar ->
    fprintf ppf "@[<2>async%s@ %a@]" (string_of_async_sort s) (pp_typ_pre vs) t
  | Promises, ts ->
    fprintf ppf "@[<2>async@ %a@]" (sequence (pp_typ_pre vs)) ts
  | Returns, _ ->
    sequence (pp_typ_nobin vs) ppf ts
  | Replies, _ ->
    fprintf ppf "@[<2>replies@ %a@]" (sequence (pp_typ_nobin vs)) ts

and pp_typ' vs ppf t =
  match t with
  (* special, additional cases for printing second-class types *)
  | Typ c ->
    fprintf ppf "@[<1>=@ @[(type@ %a)@]@]" (pp_kind' vs) (Cons.kind c)
  | Mut t ->
    fprintf ppf "@[<1>var@ %a@]" (pp_typ_un vs) t
  (* No cases for syntactic _ And _ & _ Or _ (already desugared) *)
  | t -> pp_typ_nobin vs ppf t

and pp_field vs ppf {lab; typ; src} =
  match typ with
  | Typ c ->
    let op, sbs, st = pps_of_kind' vs (Cons.kind c) in
    fprintf ppf "@[<2>type %s%a %s@ %a@]" lab sbs () op st ()
  | Mut t' ->
    fprintf ppf "@[<2>var %s :@ %a@]" lab (pp_typ' vs) t'
  | _ ->
    fprintf ppf "@[<2>%s :@ %a@]" lab (pp_typ' vs) typ

and pp_stab_field vs ppf {lab; typ; src} =
  match typ with
  | Mut t' ->
    fprintf ppf "@[<2>stable var %s :@ %a@]" lab (pp_typ' vs) t'
  | _ ->
    fprintf ppf "@[<2>stable %s :@ %a@]" lab (pp_typ' vs) typ

and pp_tag vs ppf {lab; typ; src} =
  match typ with
  | Tup [] -> fprintf ppf "#%s" lab
  | _ ->
    fprintf ppf "@[<2>#%s :@ %a@]" lab
      (pp_typ' vs) typ

and vars_of_binds vs bs =
  List.map (fun b -> name_of_var vs (b.var, 0)) bs

and name_of_var vs v =
  match vs with
  | [] -> v
  | v'::vs' -> name_of_var vs' (if fst v = fst v' then (fst v, snd v + 1) else v)

and pp_bind vs ppf (v, {bound; _}) =
  if bound = Any then
    pr ppf (string_of_var v)
  else
    fprintf ppf "%s <: %a"
      (string_of_var v)
      (pp_typ' vs) bound

and pp_binds vs vs' ppf = function
  | [] -> ()
  | tbs ->
    fprintf ppf "@[<1><%a>@]"
      (pp_print_list ~pp_sep:comma (pp_bind vs)) (List.combine vs' tbs)

and pps_of_kind' vs k =
  let op, tbs, t =
    match k with
    | Def (tbs, t) -> "=", tbs, t
    | Abs (tbs, t) -> "<:", tbs, t
  in
  let vs' = vars_of_binds vs tbs in
  let vs'vs = vs'@vs in
  op,
  (fun ppf () -> pp_binds vs'vs vs' ppf tbs),
  (fun ppf () -> pp_typ' vs'vs ppf t)

and pps_of_kind k =
  let cs = cons_kind k in
  let vs = vs_of_cs cs in
  pps_of_kind' vs k

and pp_kind' vs ppf k =
  let op, sbs, st = pps_of_kind' vs k in
  fprintf ppf "%s %a%a" op sbs () st ()

and pp_kind ppf k =
  let cs = cons_kind k in
  let vs = vs_of_cs cs in
  pp_kind' vs ppf k

and pp_stab_sig ppf sig_ =
  let cs = List.fold_right
    (cons_field false)
    (* false here ^ means ignore unreferenced Typ c components
       that would produce unreferenced bindings when unfolded *)
    sig_ ConSet.empty in
  let vs = vs_of_cs cs in
  let ds =
    let cs' = ConSet.filter (fun c ->
      match Cons.kind c with
      | Def ([], Prim p) when string_of_con c = string_of_prim p -> false
      | Def ([], Any) when string_of_con c = "Any" -> false
      | Def ([], Non) when string_of_con c = "None" -> false
      | Def _ -> true
      | Abs _ -> false) cs in
    ConSet.elements cs' in
  let fs =
    List.sort compare_field
      (List.map (fun c ->
        { lab = string_of_con c;
          typ = Typ c;
          src = empty_src }) ds)
  in
  let pp_stab_fields ppf sig_ =
    fprintf ppf "@[<v 2>%s{@;<0 0>%a@;<0 -2>}@]"
      (string_of_obj_sort Actor)
      (pp_print_list ~pp_sep:semi (pp_stab_field vs)) sig_
  in
  fprintf ppf "@[<v 0>%a%a%a;@]"
   (pp_print_list ~pp_sep:semi (pp_field vs)) fs
   (if fs = [] then fun ppf () -> () else semi) ()
   pp_stab_fields sig_

let rec pp_typ_expand' vs ppf t =
  match t with
  | Con (c, ts) ->
    (match Cons.kind c with
    | Abs _ -> pp_typ' vs ppf t
    | Def _ ->
      match normalize t with
      | Prim _ | Any | Non -> pp_typ' vs ppf t
      | t' -> fprintf ppf "%a = %a"
        (pp_typ' vs) t
        (pp_typ_expand' vs) t'
    )
  | _ -> pp_typ' vs ppf t

let pp_lab = pr

let pp_typ ppf t =
  let vs = vs_of_cs (cons t) in
  pp_typ' vs ppf t

let pp_typ_expand ppf t =
  let vs = vs_of_cs (cons t) in
  pp_typ_expand' vs ppf t

let string_of_typ typ : string =
  Lib.Format.with_str_formatter (fun ppf ->
    pp_typ ppf) typ

let string_of_kind k : string =
  Lib.Format.with_str_formatter (fun ppf ->
    pp_kind ppf) k

let strings_of_kind k : string * string * string =
  let op, sbs, st = pps_of_kind k in
  op, Lib.Format.with_str_formatter sbs (), Lib.Format.with_str_formatter st ()

let string_of_typ_expand typ : string =
  Lib.Format.with_str_formatter (fun ppf ->
    pp_typ_expand ppf) typ

end

module type Pretty = sig
  val pp_lab : Format.formatter -> lab -> unit
  val pp_typ : Format.formatter -> typ -> unit
  val pp_typ_expand : Format.formatter -> typ -> unit
  val pps_of_kind : kind ->
    string *
    (Format.formatter -> unit -> unit) *
      (Format.formatter -> unit -> unit)

  val string_of_con : con -> string
  val string_of_typ : typ -> string
  val string_of_kind : kind -> string
  val strings_of_kind : kind -> string * string * string
  val string_of_typ_expand : typ -> string
end

include MakePretty(ShowStamps)

let _ = str := string_of_typ

(* Stable signatures *)

let rec match_stab_sig tfs1 tfs2 =
  (* Assume that tfs1 and tfs2 are sorted. *)
  match tfs1, tfs2 with
  | [], _ | _, [] ->
    (* same amount of fields, new fields, or dropped fields ok *)
    true
  | tf1::tfs1', tf2::tfs2' ->
    (match compare_field tf1 tf2 with
     | 0 ->
       sub (as_immut tf1.typ) (as_immut tf2.typ) &&
       match_stab_sig tfs1' tfs2'
     | -1 ->
       (* dropped field ok *)
       match_stab_sig tfs1' tfs2
     | _ ->
       (* new field ok *)
       match_stab_sig tfs1 tfs2'
    )

let string_of_stab_sig fields : string =
  let module Pretty = MakePretty(ParseableStamps) in
  "// Version: 1.0.0\n" ^
  Format.asprintf "@[<v 0>%a@]@\n" (fun ppf -> Pretty.pp_stab_sig ppf) fields