summaryrefslogtreecommitdiff
path: root/compiler/GHC/Builtin/Types/Prim.hs
blob: f1e2906d3dfd3b32a944f589f034a6fbff38d9c8 (plain)
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
{-
(c) The AQUA Project, Glasgow University, 1994-1998


Wired-in knowledge about primitive types
-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}

-- | This module defines TyCons that can't be expressed in Haskell.
--   They are all, therefore, wired-in TyCons.  C.f module "GHC.Builtin.Types"
module GHC.Builtin.Types.Prim(
        mkTemplateKindVar, mkTemplateKindVars,
        mkTemplateTyVars, mkTemplateTyVarsFrom,
        mkTemplateKiTyVars, mkTemplateKiTyVar,

        mkTemplateTyConBinders, mkTemplateKindTyConBinders,
        mkTemplateAnonTyConBinders,

        alphaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
        alphaTyVarSpec, betaTyVarSpec, gammaTyVarSpec, deltaTyVarSpec,
        alphaTys, alphaTy, betaTy, gammaTy, deltaTy,
        alphaTyVarsUnliftedRep, alphaTyVarUnliftedRep,
        alphaTysUnliftedRep, alphaTyUnliftedRep,
        runtimeRep1TyVar, runtimeRep2TyVar, runtimeRep3TyVar,
        runtimeRep1TyVarInf, runtimeRep2TyVarInf,
        runtimeRep1Ty, runtimeRep2Ty, runtimeRep3Ty,
        levity1TyVar, levity2TyVar,
        levity1TyVarInf, levity2TyVarInf,
        levity1Ty, levity2Ty,

        alphaConstraintTyVar, alphaConstraintTy,

        openAlphaTyVar, openBetaTyVar, openGammaTyVar,
        openAlphaTyVarSpec, openBetaTyVarSpec, openGammaTyVarSpec,
        openAlphaTy, openBetaTy, openGammaTy,

        levPolyAlphaTyVar, levPolyBetaTyVar,
        levPolyAlphaTyVarSpec, levPolyBetaTyVarSpec,
        levPolyAlphaTy, levPolyBetaTy,

        multiplicityTyVar1, multiplicityTyVar2,

        -- Kind constructors...
        tYPETyCon, tYPETyConName, tYPEKind,
        cONSTRAINTTyCon, cONSTRAINTTyConName, cONSTRAINTKind,

        -- Arrows
        funTyFlagTyCon, isArrowTyCon,
        fUNTyCon,       fUNTyConName,
        ctArrowTyCon, ctArrowTyConName,
        ccArrowTyCon, ccArrowTyConName,
        tcArrowTyCon, tcArrowTyConName,

        unexposedPrimTyCons, exposedPrimTyCons, primTyCons,

        charPrimTyCon,          charPrimTy, charPrimTyConName,
        intPrimTyCon,           intPrimTy, intPrimTyConName,
        wordPrimTyCon,          wordPrimTy, wordPrimTyConName,
        addrPrimTyCon,          addrPrimTy, addrPrimTyConName,
        floatPrimTyCon,         floatPrimTy, floatPrimTyConName,
        doublePrimTyCon,        doublePrimTy, doublePrimTyConName,

        statePrimTyCon,         mkStatePrimTy,
        realWorldTyCon,         realWorldTy, realWorldStatePrimTy,

        proxyPrimTyCon,         mkProxyPrimTy,

        arrayPrimTyCon, mkArrayPrimTy,
        byteArrayPrimTyCon,     byteArrayPrimTy,
        smallArrayPrimTyCon, mkSmallArrayPrimTy,
        mutableArrayPrimTyCon, mkMutableArrayPrimTy,
        mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
        smallMutableArrayPrimTyCon, mkSmallMutableArrayPrimTy,
        mutVarPrimTyCon, mkMutVarPrimTy,

        mVarPrimTyCon,                  mkMVarPrimTy,
        ioPortPrimTyCon,                mkIOPortPrimTy,
        tVarPrimTyCon,                  mkTVarPrimTy,
        stablePtrPrimTyCon,             mkStablePtrPrimTy,
        stableNamePrimTyCon,            mkStableNamePrimTy,
        compactPrimTyCon,               compactPrimTy,
        bcoPrimTyCon,                   bcoPrimTy,
        weakPrimTyCon,                  mkWeakPrimTy,
        threadIdPrimTyCon,              threadIdPrimTy,
        stackSnapshotPrimTyCon,         stackSnapshotPrimTy,
        promptTagPrimTyCon,             mkPromptTagPrimTy,

        int8PrimTyCon,          int8PrimTy, int8PrimTyConName,
        word8PrimTyCon,         word8PrimTy, word8PrimTyConName,

        int16PrimTyCon,         int16PrimTy, int16PrimTyConName,
        word16PrimTyCon,        word16PrimTy, word16PrimTyConName,

        int32PrimTyCon,         int32PrimTy, int32PrimTyConName,
        word32PrimTyCon,        word32PrimTy, word32PrimTyConName,

        int64PrimTyCon,         int64PrimTy, int64PrimTyConName,
        word64PrimTyCon,        word64PrimTy, word64PrimTyConName,

        eqPrimTyCon,            -- ty1 ~# ty2
        eqReprPrimTyCon,        -- ty1 ~R# ty2  (at role Representational)
        eqPhantPrimTyCon,       -- ty1 ~P# ty2  (at role Phantom)
        equalityTyCon,

        -- * SIMD
#include "primop-vector-tys-exports.hs-incl"
  ) where

import GHC.Prelude

import {-# SOURCE #-} GHC.Builtin.Types
  ( runtimeRepTy, levityTy, unboxedTupleKind, liftedTypeKind, unliftedTypeKind
  , boxedRepDataConTyCon, vecRepDataConTyCon
  , liftedRepTy, unliftedRepTy, zeroBitRepTy
  , intRepDataConTy
  , int8RepDataConTy, int16RepDataConTy, int32RepDataConTy, int64RepDataConTy
  , wordRepDataConTy
  , word16RepDataConTy, word8RepDataConTy, word32RepDataConTy, word64RepDataConTy
  , addrRepDataConTy
  , floatRepDataConTy, doubleRepDataConTy
  , vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy
  , vec64DataConTy
  , int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy
  , int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy
  , word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy
  , doubleElemRepDataConTy
  , multiplicityTy
  , constraintKind )

import {-# SOURCE #-} GHC.Types.TyThing( mkATyCon )
import {-# SOURCE #-} GHC.Core.Type ( mkTyConApp, getLevity )

import GHC.Core.TyCon
import GHC.Core.TyCo.Rep -- Doesn't need special access, but this is easier to avoid
                         -- import loops which show up if you import Type instead

import GHC.Types.Var    ( TyVarBinder, TyVar,binderVar, binderVars
                        , mkTyVar, mkTyVarBinder, mkTyVarBinders )
import GHC.Types.Name
import GHC.Types.SrcLoc
import GHC.Types.Unique

import GHC.Builtin.Uniques
import GHC.Builtin.Names
import GHC.Utils.Misc ( changeLast )
import GHC.Utils.Panic ( assertPpr )
import GHC.Utils.Outputable

import GHC.Data.FastString
import Data.Char

import GHC.Unit.Types (WiredIn)

{- *********************************************************************
*                                                                      *
             Building blocks
*                                                                      *
********************************************************************* -}

mk_TYPE_app :: Type -> WiredIn Type
mk_TYPE_app rep = mkTyConApp <$> tYPETyCon <*> pure [rep]

mk_CONSTRAINT_app :: Type -> WiredIn Type
mk_CONSTRAINT_app rep = mkTyConApp <$> cONSTRAINTTyCon <*> pure [rep]

mkPrimTc :: FastString -> Unique -> TyCon -> WiredIn Name
mkPrimTc = mkGenPrimTc UserSyntax

mkBuiltInPrimTc :: FastString -> Unique -> TyCon -> WiredIn Name
mkBuiltInPrimTc = mkGenPrimTc BuiltInSyntax

mkGenPrimTc :: BuiltInSyntax -> FastString -> Unique -> TyCon -> WiredIn Name
mkGenPrimTc built_in_syntax occ key tycon
  = mkWiredInName gHC_PRIM (mkTcOccFS occ)
                  key
                  (mkATyCon tycon)
                  built_in_syntax

-- | Create a primitive 'TyCon' with the given 'Name',
-- arguments of kind 'Type` with the given 'Role's,
-- and the given result kind representation.
--
-- Only use this in "GHC.Builtin.Types.Prim".
pcPrimTyCon :: WiredIn Name
            -> [Role] -> WiredIn RuntimeRepType -> WiredIn TyCon
pcPrimTyCon name roles res_rep
  = mkPrimTyCon name binders result_kind roles
  where
    bndr_kis    = sequence $ liftedTypeKind <$ roles
    binders     = mkTemplateAnonTyConBinders <$> bndr_kis
    result_kind = mk_TYPE_app =<< res_rep

-- | Create a primitive nullary 'TyCon' with the given 'Name'
-- and result kind representation.
--
-- Only use this in "GHC.Builtin.Types.Prim".
pcPrimTyCon0 :: WiredIn Name -> WiredIn RuntimeRepType -> WiredIn TyCon
pcPrimTyCon0 name res_rep
  = pcPrimTyCon name [] res_rep

-- | Create a primitive 'TyCon' like 'pcPrimTyCon', except the last
-- argument is levity-polymorphic, where the levity argument is
-- implicit and comes before other arguments
--
-- Only use this in "GHC.Builtin.Types.Prim".
pcPrimTyCon_LevPolyLastArg :: WiredIn Name
                           -> [Role] -- ^ roles of the arguments (must be non-empty),
                                     -- not including the implicit argument of kind 'Levity',
                                     -- which always has 'Nominal' role
                           -> WiredIn RuntimeRepType  -- ^ representation of the fully-applied type
                           -> WiredIn TyCon
pcPrimTyCon_LevPolyLastArg name roles res_rep
  = do
    mkPrimTyCon name binders result_kind (Nominal : roles)
    where
      result_kind = mk_TYPE_app =<< res_rep
      lev_bndr = mkNamedTyConBinder Inferred <$> levity1TyVar
      lev_tv   = mkTyVarTy . binderVar <$> lev_bndr
      binders = liftA2 (:) lev_bndr (mkTemplateAnonTyConBinders <$> sequence anon_bndr_kis)

      -- [ Type, ..., Type, TYPE (BoxedRep l) ]
      anon_bndr_kis = changeLast (liftedTypeKind <$ roles) $ mk_TYPE_app =<< (mkTyConApp <$> boxedRepDataConTyCon <*> sequence [lev_tv])


{- *********************************************************************
*                                                                      *
           Primitive type constructors
*                                                                      *
********************************************************************* -}

{- Note Note [Unexposed TyCons]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A few primitive TyCons are "unexposed", meaning:
* We don't want users to be able to write them (see #15209);
  i.e. they aren't in scope, ever.  In particular they do not
  appear in the exports of GHC.Prim: see GHC.Builtin.Utils.ghcPrimExports

* We don't want users to see them in GHCi's @:browse@ output (see #12023).
-}

primTyCons :: [WiredIn TyCon]
primTyCons = unexposedPrimTyCons ++ exposedPrimTyCons

-- | Primitive 'TyCon's that are defined in GHC.Prim but not "exposed".
-- See Note [Unexposed TyCons]
unexposedPrimTyCons :: [WiredIn TyCon]
unexposedPrimTyCons
  = [ eqPrimTyCon      -- (~#)
    , eqReprPrimTyCon  -- (~R#)
    , eqPhantPrimTyCon -- (~P#)

    -- These arrows are un-exposed for now
    , ctArrowTyCon  -- (=>)
    , ccArrowTyCon  -- (==>)
    , tcArrowTyCon  -- (-=>)
    ]

-- | Primitive 'TyCon's that are defined in, and exported from, GHC.Prim.
exposedPrimTyCons :: [WiredIn TyCon]
exposedPrimTyCons
  = [ addrPrimTyCon
    , arrayPrimTyCon
    , byteArrayPrimTyCon
    , smallArrayPrimTyCon
    , charPrimTyCon
    , doublePrimTyCon
    , floatPrimTyCon
    , intPrimTyCon
    , int8PrimTyCon
    , int16PrimTyCon
    , int32PrimTyCon
    , int64PrimTyCon
    , bcoPrimTyCon
    , weakPrimTyCon
    , mutableArrayPrimTyCon
    , mutableByteArrayPrimTyCon
    , smallMutableArrayPrimTyCon
    , mVarPrimTyCon
    , ioPortPrimTyCon
    , tVarPrimTyCon
    , mutVarPrimTyCon
    , realWorldTyCon
    , stablePtrPrimTyCon
    , stableNamePrimTyCon
    , compactPrimTyCon
    , statePrimTyCon
    , proxyPrimTyCon
    , threadIdPrimTyCon
    , wordPrimTyCon
    , word8PrimTyCon
    , word16PrimTyCon
    , word32PrimTyCon
    , word64PrimTyCon
    , stackSnapshotPrimTyCon
    , promptTagPrimTyCon

    , fUNTyCon
    , tYPETyCon
    , cONSTRAINTTyCon

#include "primop-vector-tycons.hs-incl"
    ]

charPrimTyConName, intPrimTyConName, int8PrimTyConName, int16PrimTyConName, int32PrimTyConName, int64PrimTyConName,
  wordPrimTyConName, word32PrimTyConName, word8PrimTyConName, word16PrimTyConName, word64PrimTyConName,
  addrPrimTyConName, floatPrimTyConName, doublePrimTyConName,
  statePrimTyConName, proxyPrimTyConName, realWorldTyConName,
  arrayPrimTyConName, smallArrayPrimTyConName, byteArrayPrimTyConName,
  mutableArrayPrimTyConName, mutableByteArrayPrimTyConName,
  smallMutableArrayPrimTyConName, mutVarPrimTyConName, mVarPrimTyConName,
  ioPortPrimTyConName, tVarPrimTyConName, stablePtrPrimTyConName,
  stableNamePrimTyConName, compactPrimTyConName, bcoPrimTyConName,
  weakPrimTyConName, threadIdPrimTyConName,
  eqPrimTyConName, eqReprPrimTyConName, eqPhantPrimTyConName,
  stackSnapshotPrimTyConName, promptTagPrimTyConName :: WiredIn Name
charPrimTyConName             = mkPrimTc (fsLit "Char#") charPrimTyConKey   =<< charPrimTyCon
intPrimTyConName              = mkPrimTc (fsLit "Int#") intPrimTyConKey     =<< intPrimTyCon
int8PrimTyConName             = mkPrimTc (fsLit "Int8#") int8PrimTyConKey   =<< int8PrimTyCon
int16PrimTyConName            = mkPrimTc (fsLit "Int16#") int16PrimTyConKey =<< int16PrimTyCon
int32PrimTyConName            = mkPrimTc (fsLit "Int32#") int32PrimTyConKey =<< int32PrimTyCon
int64PrimTyConName            = mkPrimTc (fsLit "Int64#") int64PrimTyConKey =<< int64PrimTyCon
wordPrimTyConName             = mkPrimTc (fsLit "Word#") wordPrimTyConKey   =<< wordPrimTyCon
word8PrimTyConName            = mkPrimTc (fsLit "Word8#") word8PrimTyConKey =<< word8PrimTyCon
word16PrimTyConName           = mkPrimTc (fsLit "Word16#") word16PrimTyConKey =<< word16PrimTyCon
word32PrimTyConName           = mkPrimTc (fsLit "Word32#") word32PrimTyConKey =<< word32PrimTyCon
word64PrimTyConName           = mkPrimTc (fsLit "Word64#") word64PrimTyConKey =<< word64PrimTyCon
addrPrimTyConName             = mkPrimTc (fsLit "Addr#") addrPrimTyConKey   =<< addrPrimTyCon
floatPrimTyConName            = mkPrimTc (fsLit "Float#") floatPrimTyConKey =<< floatPrimTyCon
doublePrimTyConName           = mkPrimTc (fsLit "Double#") doublePrimTyConKey =<< doublePrimTyCon
statePrimTyConName            = mkPrimTc (fsLit "State#") statePrimTyConKey =<< statePrimTyCon
proxyPrimTyConName            = mkPrimTc (fsLit "Proxy#") proxyPrimTyConKey =<< proxyPrimTyCon
eqPrimTyConName               = mkPrimTc (fsLit "~#") eqPrimTyConKey =<< eqPrimTyCon
eqReprPrimTyConName           = mkBuiltInPrimTc (fsLit "~R#") eqReprPrimTyConKey  =<< eqReprPrimTyCon
eqPhantPrimTyConName          = mkBuiltInPrimTc (fsLit "~P#") eqPhantPrimTyConKey =<< eqPhantPrimTyCon
realWorldTyConName            = mkPrimTc (fsLit "RealWorld") realWorldTyConKey =<< realWorldTyCon
arrayPrimTyConName            = mkPrimTc (fsLit "Array#") arrayPrimTyConKey =<< arrayPrimTyCon
byteArrayPrimTyConName        = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey   =<< byteArrayPrimTyCon
smallArrayPrimTyConName       = mkPrimTc (fsLit "SmallArray#") smallArrayPrimTyConKey =<< smallArrayPrimTyCon
mutableArrayPrimTyConName     = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey =<< mutableArrayPrimTyCon
mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey =<< mutableByteArrayPrimTyCon
smallMutableArrayPrimTyConName= mkPrimTc (fsLit "SmallMutableArray#") smallMutableArrayPrimTyConKey =<< smallMutableArrayPrimTyCon
mutVarPrimTyConName           = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey =<< mutVarPrimTyCon
ioPortPrimTyConName           = mkPrimTc (fsLit "IOPort#") ioPortPrimTyConKey =<< ioPortPrimTyCon
mVarPrimTyConName             = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey =<< mVarPrimTyCon
tVarPrimTyConName             = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey =<< tVarPrimTyCon
stablePtrPrimTyConName        = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey =<< stablePtrPrimTyCon
stableNamePrimTyConName       = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey =<< stableNamePrimTyCon
compactPrimTyConName          = mkPrimTc (fsLit "Compact#") compactPrimTyConKey =<< compactPrimTyCon
stackSnapshotPrimTyConName    = mkPrimTc (fsLit "StackSnapshot#") stackSnapshotPrimTyConKey =<< stackSnapshotPrimTyCon
bcoPrimTyConName              = mkPrimTc (fsLit "BCO") bcoPrimTyConKey =<< bcoPrimTyCon
weakPrimTyConName             = mkPrimTc (fsLit "Weak#") weakPrimTyConKey =<< weakPrimTyCon
threadIdPrimTyConName         = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey =<< threadIdPrimTyCon
promptTagPrimTyConName        = mkPrimTc (fsLit "PromptTag#") promptTagPrimTyConKey =<< promptTagPrimTyCon

{- *********************************************************************
*                                                                      *
                Type variables
*                                                                      *
********************************************************************* -}

{-
alphaTyVars is a list of type variables for use in templates:
        ["a", "b", ..., "z", "t1", "t2", ... ]
-}

mkTemplateKindVar :: Kind -> TyVar
mkTemplateKindVar = mkTyVar (mk_tv_name 0 "k")

mkTemplateKindVars :: [Kind] -> [TyVar]
-- k0  with unique (mkAlphaTyVarUnique 0)
-- k1  with unique (mkAlphaTyVarUnique 1)
-- ... etc
mkTemplateKindVars [kind] = [mkTemplateKindVar kind]
  -- Special case for one kind: just "k"
mkTemplateKindVars kinds
  = [ mkTyVar (mk_tv_name u ('k' : show u)) kind
    | (kind, u) <- kinds `zip` [0..] ]
mk_tv_name :: Int -> String -> Name
mk_tv_name u s = mkInternalName (mkAlphaTyVarUnique u)
                                (mkTyVarOccFS (mkFastString s))
                                noSrcSpan

mkTemplateTyVarsFrom :: Int -> [Kind] -> [TyVar]
-- a  with unique (mkAlphaTyVarUnique n)
-- b  with unique (mkAlphaTyVarUnique n+1)
-- ... etc
-- Typically called as
--   mkTemplateTyVarsFrom (length kv_bndrs) kinds
-- where kv_bndrs are the kind-level binders of a TyCon
mkTemplateTyVarsFrom n kinds
  = [ mkTyVar name kind
    | (kind, index) <- zip kinds [0..],
      let ch_ord = index + ord 'a'
          name_str | ch_ord <= ord 'z' = [chr ch_ord]
                   | otherwise         = 't':show index
          name = mk_tv_name (index + n) name_str
    ]

mkTemplateTyVars :: [Kind] -> [TyVar]
mkTemplateTyVars = mkTemplateTyVarsFrom 1

mkTemplateTyConBinders
    :: [Kind]                -- [k1, .., kn]   Kinds of kind-forall'd vars
    -> ([Kind] -> [Kind])    -- Arg is [kv1:k1, ..., kvn:kn]
                             --     same length as first arg
                             -- Result is anon arg kinds
    -> [TyConBinder]
mkTemplateTyConBinders kind_var_kinds mk_anon_arg_kinds
  = kv_bndrs ++ tv_bndrs
  where
    kv_bndrs   = mkTemplateKindTyConBinders kind_var_kinds
    anon_kinds = mk_anon_arg_kinds (mkTyVarTys (binderVars kv_bndrs))
    tv_bndrs   = mkTemplateAnonTyConBindersFrom (length kv_bndrs) anon_kinds

mkTemplateKiTyVars
    :: [Kind]                -- [k1, .., kn]   Kinds of kind-forall'd vars
    -> ([Kind] -> [Kind])    -- Arg is [kv1:k1, ..., kvn:kn]
                             --     same length as first arg
                             -- Result is anon arg kinds [ak1, .., akm]
    -> [TyVar]   -- [kv1:k1, ..., kvn:kn, av1:ak1, ..., avm:akm]
-- Example: if you want the tyvars for
--   forall (r:RuntimeRep) (a:TYPE r) (b:*). blah
-- call mkTemplateKiTyVars [RuntimeRep] (\[r] -> [TYPE r, *])
mkTemplateKiTyVars kind_var_kinds mk_arg_kinds
  = kv_bndrs ++ tv_bndrs
  where
    kv_bndrs   = mkTemplateKindVars kind_var_kinds
    anon_kinds = mk_arg_kinds (mkTyVarTys kv_bndrs)
    tv_bndrs   = mkTemplateTyVarsFrom (length kv_bndrs) anon_kinds

mkTemplateKiTyVar
    :: Kind                  -- [k1, .., kn]   Kind of kind-forall'd var
    -> (Kind -> [Kind])      -- Arg is kv1:k1
                             -- Result is anon arg kinds [ak1, .., akm]
    -> [TyVar]   -- [kv1:k1, ..., kvn:kn, av1:ak1, ..., avm:akm]
-- Example: if you want the tyvars for
--   forall (r:RuntimeRep) (a:TYPE r) (b:*). blah
-- call mkTemplateKiTyVar RuntimeRep (\r -> [TYPE r, *])
mkTemplateKiTyVar kind mk_arg_kinds
  = kv_bndr : tv_bndrs
  where
    kv_bndr    = mkTemplateKindVar kind
    anon_kinds = mk_arg_kinds (mkTyVarTy kv_bndr)
    tv_bndrs   = mkTemplateTyVarsFrom 1 anon_kinds

mkTemplateKindTyConBinders :: [Kind] -> [TyConBinder]
-- Makes named, Specified binders
mkTemplateKindTyConBinders kinds
  = [mkNamedTyConBinder Specified tv | tv <- mkTemplateKindVars kinds]

mkTemplateAnonTyConBinders :: [Kind] -> [TyConBinder]
mkTemplateAnonTyConBinders kinds
  = mkAnonTyConBinders (mkTemplateTyVars kinds)

mkTemplateAnonTyConBindersFrom :: Int -> [Kind] -> [TyConBinder]
mkTemplateAnonTyConBindersFrom n kinds
  = mkAnonTyConBinders (mkTemplateTyVarsFrom n kinds)

alphaTyVars :: WiredIn [TyVar]
alphaTyVars = mkTemplateTyVars <$> sequence (repeat liftedTypeKind)

alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: WiredIn TyVar
alphaTyVar = (\case (alphaTyVar:_betaTyVar:_gammaTyVar:_deltaTyVar:_) -> alphaTyVar) <$> alphaTyVars
betaTyVar  = (\case (_alphaTyVar:betaTyVar:_gammaTyVar:_deltaTyVar:_) -> betaTyVar)  <$> alphaTyVars
gammaTyVar = (\case (_alphaTyVar:_betaTyVar:gammaTyVar:_deltaTyVar:_) -> gammaTyVar) <$> alphaTyVars
deltaTyVar = (\case (_alphaTyVar:_betaTyVar:_gammaTyVar:deltaTyVar:_) -> deltaTyVar) <$> alphaTyVars

alphaTyVarSpec, betaTyVarSpec, gammaTyVarSpec, deltaTyVarSpec :: WiredIn TyVarBinder
alphaTyVarSpec = (\case (alphaTyVarSpec:_betaTyVarSpec:_gammaTyVarSpec:_deltaTyVarSpec:_) -> alphaTyVarSpec) . mkTyVarBinders Specified <$> alphaTyVars
betaTyVarSpec  = (\case (_alphaTyVarSpec:betaTyVarSpec:_gammaTyVarSpec:_deltaTyVarSpec:_) -> betaTyVarSpec) . mkTyVarBinders Specified <$> alphaTyVars
gammaTyVarSpec = (\case (_alphaTyVarSpec:_betaTyVarSpec:gammaTyVarSpec:_deltaTyVarSpec:_) -> gammaTyVarSpec) . mkTyVarBinders Specified <$> alphaTyVars
deltaTyVarSpec = (\case (_alphaTyVarSpec:_betaTyVarSpec:_gammaTyVarSpec:deltaTyVarSpec:_) -> deltaTyVarSpec) . mkTyVarBinders Specified <$> alphaTyVars
 
alphaConstraintTyVars :: WiredIn [TyVar]
alphaConstraintTyVars = mkTemplateTyVars <$> sequence (repeat constraintKind)

alphaConstraintTyVar :: WiredIn TyVar
alphaConstraintTyVar = (\case (alphaConstraintTyVar:_) -> alphaConstraintTyVar) <$> alphaConstraintTyVars

alphaConstraintTy :: WiredIn Type
alphaConstraintTy = mkTyVarTy <$> alphaConstraintTyVar

alphaTys :: WiredIn [Type]
alphaTys = mkTyVarTys <$> alphaTyVars
alphaTy, betaTy, gammaTy, deltaTy :: WiredIn Type
alphaTy = (\case (alphaTy:_betaTy:_gammaTy:_deltaTy:_) -> alphaTy) <$> alphaTys
betaTy  = (\case (_alphaTy:betaTy:_gammaTy:_deltaTy:_) -> betaTy)  <$> alphaTys
gammaTy = (\case (_alphaTy:_betaTy:gammaTy:_deltaTy:_) -> gammaTy) <$> alphaTys
deltaTy = (\case (_alphaTy:_betaTy:_gammaTy:deltaTy:_) -> deltaTy) <$> alphaTys

alphaTyVarsUnliftedRep :: WiredIn [TyVar]
alphaTyVarsUnliftedRep = mkTemplateTyVars <$> sequence (repeat unliftedTypeKind)

alphaTyVarUnliftedRep :: WiredIn TyVar
alphaTyVarUnliftedRep = (\case (alphaTyVarUnliftedRep:_) -> alphaTyVarUnliftedRep) <$> alphaTyVarsUnliftedRep

alphaTysUnliftedRep :: WiredIn [Type]
alphaTysUnliftedRep = mkTyVarTys <$> alphaTyVarsUnliftedRep
alphaTyUnliftedRep :: WiredIn Type
alphaTyUnliftedRep = (\case (alphaTyUnliftedRep:_) -> alphaTyUnliftedRep) <$> alphaTysUnliftedRep

runtimeRep1TyVar, runtimeRep2TyVar, runtimeRep3TyVar :: WiredIn TyVar
(runtimeRep1TyVar : runtimeRep2TyVar : runtimeRep3TyVar : _)
  = drop 16 . mkTemplateTyVars <$> sequence (repeat runtimeRepTy)  -- selects 'q','r'

runtimeRep1TyVarInf, runtimeRep2TyVarInf :: WiredIn TyVarBinder
runtimeRep1TyVarInf = mkTyVarBinder Inferred <$> runtimeRep1TyVar
runtimeRep2TyVarInf = mkTyVarBinder Inferred <$> runtimeRep2TyVar

runtimeRep1Ty, runtimeRep2Ty, runtimeRep3Ty :: WiredIn RuntimeRepType
runtimeRep1Ty = mkTyVarTy <$> runtimeRep1TyVar
runtimeRep2Ty = mkTyVarTy <$> runtimeRep2TyVar
runtimeRep3Ty = mkTyVarTy <$> runtimeRep3TyVar
openAlphaTyVar, openBetaTyVar, openGammaTyVar :: WiredIn TyVar
-- alpha :: TYPE r1
-- beta  :: TYPE r2
-- gamma :: TYPE r3
openAlphaTyVar = (\case [openAlphaTyVar,_openBetaTyVar,_openGammaTyVar] -> openAlphaTyVar) <$> openTyVars 
openBetaTyVar  = (\case [_openAlphaTyVar,openBetaTyVar,_openGammaTyVar] -> openBetaTyVar)  <$> openTyVars
openGammaTyVar = (\case [_openAlphaTyVar,_openBetaTyVar,openGammaTyVar] -> openGammaTyVar) <$> openTyVars
openTyVars :: WiredIn [TyVar]
openTyVars
  =  mkTemplateTyVars <$> sequence [ mk_TYPE_app =<< runtimeRep1Ty
                                   , mk_TYPE_app =<< runtimeRep2Ty
                                   , mk_TYPE_app =<< runtimeRep3Ty]

openAlphaTyVarSpec, openBetaTyVarSpec, openGammaTyVarSpec :: WiredIn TyVarBinder
openAlphaTyVarSpec = mkTyVarBinder Specified <$> openAlphaTyVar
openBetaTyVarSpec  = mkTyVarBinder Specified <$> openBetaTyVar
openGammaTyVarSpec = mkTyVarBinder Specified <$> openGammaTyVar

openAlphaTy, openBetaTy, openGammaTy :: WiredIn Type
openAlphaTy = mkTyVarTy <$> openAlphaTyVar
openBetaTy  = mkTyVarTy <$> openBetaTyVar
openGammaTy = mkTyVarTy <$> openGammaTyVar

levityTyVars :: WiredIn [TyVar]
levityTyVars = drop 10 . mkTemplateTyVars <$> sequence (repeat levityTy) -- selects 'k', 'l'
levity1TyVar, levity2TyVar :: WiredIn TyVar
-- NB: levity2TyVar before levity1TyVar
levity2TyVar = (\case (levity2TyVar : _levity1TyVar : _) -> levity2TyVar) <$> levityTyVars
levity1TyVar = (\case (_levity2TyVar : levity1TyVar : _) -> levity1TyVar) <$> levityTyVars
-- The ordering of levity2TyVar before levity1TyVar is chosen so that
-- the more common levity1TyVar uses the levity variable 'l'.

levity1TyVarInf, levity2TyVarInf :: WiredIn TyVarBinder
levity1TyVarInf = mkTyVarBinder Inferred <$> levity1TyVar
levity2TyVarInf = mkTyVarBinder Inferred <$> levity2TyVar

levity1Ty, levity2Ty :: WiredIn Type
levity1Ty = mkTyVarTy <$> levity1TyVar
levity2Ty = mkTyVarTy <$> levity2TyVar

levPolyAlphaTyVar, levPolyBetaTyVar :: WiredIn TyVar
levPolyAlphaTyVar = (\case [levPolyAlphaTy,_levPolyBetaTyVar] -> levPolyAlphaTy) <$> levPolyTyVars
levPolyBetaTyVar  = (\case [_levPolyAlphaTy,levPolyBetaTyVar] -> levPolyBetaTyVar) <$> levPolyTyVars
levPolyTyVars :: WiredIn [TyVar]
levPolyTyVars =
  mkTemplateTyVars <$> sequence
    [ mk_TYPE_app =<< (mkTyConApp <$> boxedRepDataConTyCon <*> sequence [levity1Ty])
    , mk_TYPE_app =<< (mkTyConApp <$> boxedRepDataConTyCon <*> sequence [levity2Ty])]
-- alpha :: TYPE ('BoxedRep l)
-- beta  :: TYPE ('BoxedRep k)

levPolyAlphaTyVarSpec, levPolyBetaTyVarSpec :: WiredIn TyVarBinder
levPolyAlphaTyVarSpec = mkTyVarBinder Specified <$> levPolyAlphaTyVar
levPolyBetaTyVarSpec  = mkTyVarBinder Specified <$> levPolyBetaTyVar

levPolyAlphaTy, levPolyBetaTy :: WiredIn Type
levPolyAlphaTy = mkTyVarTy <$> levPolyAlphaTyVar
levPolyBetaTy  = mkTyVarTy <$> levPolyBetaTyVar

multiplicityTyVars :: WiredIn [TyVar]
multiplicityTyVars
   = drop 13 . mkTemplateTyVars <$> sequence (repeat multiplicityTy)  -- selects 'n', 'm'
multiplicityTyVar1, multiplicityTyVar2  :: WiredIn TyVar
multiplicityTyVar1 = (\case (multiplicityTyVar1 : _multiplicityTyVar2 : _) -> multiplicityTyVar1) <$> multiplicityTyVars
multiplicityTyVar2 = (\case (_multiplicityTyVar1 : multiplicityTyVar2 : _) -> multiplicityTyVar2) <$> multiplicityTyVars


{-
************************************************************************
*                                                                      *
                FunTyCon
*                                                                      *
************************************************************************
-}

{- Note [Function type constructors and FunTy]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We have four distinct function type constructors, and a type synonym

 FUN :: forall (m :: Multiplicity) ->
        forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
        TYPE rep1 -> TYPE rep2 -> Type

 (=>)  :: forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
          CONSTRAINT rep1 -> TYPE rep2 -> Type

 (==>) :: forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
          CONSTRAINT rep1 -> CONSTRAINT rep2 -> Constraint

 (-=>) :: forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
          TYPE rep1 -> CONSTRAINT rep2 -> Constraint

 type (->) = FUN Many

For efficiency, all four are always represented by
  FunTy { ft_af :: FunTyFlag, ft_mult :: Mult
        , ft_arg :: Type, ft_res :: Type }
rather than by using a TyConApp.

* The four TyCons FUN, (=>), (==>), (-=>) are all wired in.
  But (->) is just a regular synonym, with no special treatment;
  in particular it is not wired-in.

* The ft_af :: FunTyFlag distinguishes the four cases.
  See Note [FunTyFlag] in GHC.Types.Var.

* The ft_af field is redundant: it can always be gleaned from
  the kinds of ft_arg and ft_res.  See Note [FunTyFlag] in GHC.Types.Var.

* The ft_mult :: Mult field gives the first argument for FUN
  For the other three cases ft_mult is redundant; it is always Many.
  Note that of the four type constructors, only `FUN` takes a Multiplicity.

* Functions in GHC.Core.Type help to build and decompose `FunTy`.
  * funTyConAppTy_maybe
  * funTyFlagTyCon
  * tyConAppFun_maybe
  * splitFunTy_maybe
  Use them!
-}

funTyFlagTyCon :: FunTyFlag -> WiredIn TyCon
-- `anonArgTyCon af` gets the TyCon that corresponds to the `FunTyFlag`
-- But be careful: fUNTyCon has a different kind to the others!
-- See Note [Function type constructors and FunTy]
funTyFlagTyCon FTF_T_T = fUNTyCon
funTyFlagTyCon FTF_T_C = tcArrowTyCon
funTyFlagTyCon FTF_C_T = ctArrowTyCon
funTyFlagTyCon FTF_C_C = ccArrowTyCon

isArrowTyCon :: TyCon -> Bool
-- We don't bother to look for plain (->), because this function
-- should only be used after unwrapping synonyms
isArrowTyCon tc
  = assertPpr (not (isTypeSynonymTyCon tc)) (ppr tc)
    getUnique tc `elem`
    [fUNTyConKey, ctArrowTyConKey, ccArrowTyConKey, tcArrowTyConKey]

fUNTyConName, ctArrowTyConName, ccArrowTyConName, tcArrowTyConName :: WiredIn Name
fUNTyConName     = mkPrimTc        (fsLit "FUN") fUNTyConKey     =<< fUNTyCon
ctArrowTyConName = mkBuiltInPrimTc (fsLit "=>")  ctArrowTyConKey =<< ctArrowTyCon
ccArrowTyConName = mkBuiltInPrimTc (fsLit "==>") ccArrowTyConKey =<< ccArrowTyCon
tcArrowTyConName = mkBuiltInPrimTc (fsLit "-=>") tcArrowTyConKey =<< tcArrowTyCon

-- | The @FUN@ type constructor.
--
-- @
-- FUN :: forall (m :: Multiplicity) ->
--        forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
--        TYPE rep1 -> TYPE rep2 -> Type
-- @
--
-- The runtime representations quantification is left inferred. This
-- means they cannot be specified with @-XTypeApplications@.
--
-- This is a deliberate choice to allow future extensions to the
-- function arrow.
fUNTyCon :: WiredIn TyCon
fUNTyCon = do
  mkPrimTyCon fUNTyConName tc_bndrs liftedTypeKind tc_roles
  where
    -- See also unrestrictedFunTyCon
    tc_bndrs = liftA2 (++)
                (sequence
                  [ mkNamedTyConBinder Required <$> multiplicityTyVar1
                  , mkNamedTyConBinder Inferred <$> runtimeRep1TyVar
                  , mkNamedTyConBinder Inferred <$> runtimeRep2TyVar
                  ])
                (mkTemplateAnonTyConBinders <$> sequence [ mk_TYPE_app =<< runtimeRep1Ty
                                                         , mk_TYPE_app =<< runtimeRep2Ty ])
    tc_roles = [Nominal, Nominal, Nominal, Representational, Representational]

-- (=>) :: forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
--         CONSTRAINT rep1 -> TYPE rep2 -> Type
ctArrowTyCon :: WiredIn TyCon
ctArrowTyCon = mkPrimTyCon ctArrowTyConName tc_bndrs liftedTypeKind tc_roles
  where
    -- See also unrestrictedFunTyCon
    tc_bndrs = liftA2 (++)
                (sequence
                  [ mkNamedTyConBinder Inferred <$> runtimeRep1TyVar
                  , mkNamedTyConBinder Inferred <$> runtimeRep2TyVar ])
               (mkTemplateAnonTyConBinders <$> sequence [ mk_CONSTRAINT_app =<< runtimeRep1Ty
                                                        , mk_TYPE_app       =<< runtimeRep2Ty ])
    tc_roles = [Nominal, Nominal, Representational, Representational]

-- (==>) :: forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
--          CONSTRAINT rep1 -> CONSTRAINT rep2 -> Constraint
ccArrowTyCon :: WiredIn TyCon
ccArrowTyCon = mkPrimTyCon ccArrowTyConName tc_bndrs constraintKind tc_roles
  where
    -- See also unrestrictedFunTyCon
    tc_bndrs = liftA2 (++)
                (sequence [ mkNamedTyConBinder Inferred <$> runtimeRep1TyVar
                          , mkNamedTyConBinder Inferred <$> runtimeRep2TyVar ])
               (mkTemplateAnonTyConBinders <$> sequence [ mk_CONSTRAINT_app =<< runtimeRep1Ty
                                                        , mk_CONSTRAINT_app =<< runtimeRep2Ty ])
    tc_roles = [Nominal, Nominal, Representational, Representational]

-- (-=>) :: forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
--          TYPE rep1 -> CONSTRAINT rep2 -> Constraint
tcArrowTyCon :: WiredIn TyCon
tcArrowTyCon = mkPrimTyCon tcArrowTyConName tc_bndrs constraintKind tc_roles
  where
    -- See also unrestrictedFunTyCon
    tc_bndrs = liftA2 (++)
                (sequence [ mkNamedTyConBinder Inferred <$> runtimeRep1TyVar
                          , mkNamedTyConBinder Inferred <$> runtimeRep2TyVar ])
                (mkTemplateAnonTyConBinders <$> sequence [ mk_TYPE_app       =<< runtimeRep1Ty
                                                         , mk_CONSTRAINT_app =<< runtimeRep2Ty ])
    tc_roles = [Nominal, Nominal, Representational, Representational]

{-
************************************************************************
*                                                                      *
                Type and Constraint
*                                                                      *
************************************************************************

Note [TYPE and CONSTRAINT]  aka Note [Type vs Constraint]
~~~~~~~~~~~~~~~~~~~~~~~~~~
GHC distinguishes Type from Constraint throughout the compiler.
See GHC Proposal #518, and tickets #21623 and #11715.

All types that classify values have a kind of the form
  (TYPE rr) or (CONSTRAINT rr)
where the `RuntimeRep` parameter, rr, tells us how the value is represented
at runtime.  TYPE and CONSTRAINT are primitive type constructors.

See Note [RuntimeRep polymorphism] about the `rr` parameter.

There are a bunch of type synonyms and data types defined in the
library ghc-prim:GHC.Types.  All of them are also wired in to GHC, in
GHC.Builtin.Types

  type Constraint   = CONSTRAINT LiftedRep  :: Type

  type Type         = TYPE LiftedRep   :: Type
  type UnliftedType = TYPE UnliftedRep :: Type

  type LiftedRep    = BoxedRep Lifted   :: RuntimeRep
  type UnliftedRep  = BoxedRep Unlifted :: RuntimeRep

  data RuntimeRep     -- Defined in ghc-prim:GHC.Types
      = BoxedRep Levity
      | IntRep
      | FloatRep
      .. etc ..

  data Levity = Lifted | Unlifted

We abbreviate '*' specially (with -XStarIsType), as if we had this:
    type * = Type

So for example:
    Int        :: TYPE (BoxedRep Lifted)
    Array# Int :: TYPE (BoxedRep Unlifted)
    Int#       :: TYPE IntRep
    Float#     :: TYPE FloatRep
    Maybe      :: TYPE (BoxedRep Lifted) -> TYPE (BoxedRep Lifted)
    (# , #)    :: TYPE r1 -> TYPE r2 -> TYPE (TupleRep [r1, r2])

    Eq Int       :: CONSTRAINT (BoxedRep Lifted)
    IP "foo" Int :: CONSTRAINT (BoxedRep Lifted)
    a ~ b        :: CONSTRAINT (BoxedRep Lifted)
    a ~# b       :: CONSTRAINT (TupleRep [])

Constraints are mostly lifted, but unlifted ones are useful too.
Specifically  (a ~# b) :: CONSTRAINT (TupleRep [])

Wrinkles

(W1) Type and Constraint are considered distinct throughout GHC. But they
     are not /apart/: see Note [Type and Constraint are not apart]

(W2) We need two absent-error Ids, aBSENT_ERROR_ID for types of kind Type, and
     aBSENT_CONSTRAINT_ERROR_ID for vaues of kind Constraint.  Ditto noInlineId
     vs noInlieConstraintId in GHC.Types.Id.Make; see Note [inlineId magic].

(W3) We need a TypeOrConstraint flag in LitRubbish.

Note [Type and Constraint are not apart]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Type and Constraint are not equal (eqType) but they are not /apart/
either. Reason (c.f. #7451):

* We want to allow newtype classes, where
    class C a where { op :: a -> a }

* The axiom for such a class will look like
    axiom axC a :: (C a :: Constraint) ~# (a->a :: Type)

* This axiom connects a type of kind Type with one of kind Constraint
  That is dangerous: kindCo (axC Int) :: Type ~N Constraint
  And /that/ is bad because we could have
     type family F a where
        F Type       = Int
        F Constraint = Bool
  So now we can prove Int ~N Bool, and all is lost.  We prevent this
  by saying that Type and Constraint are not Apart, which makes the
  above type family instances illegal.

So we ensure that Type and Constraint are not apart; or, more
precisely, that TYPE and CONSTRAINT are not apart.  This
non-apart-ness check is implemented in GHC.Core.Unify.unify_ty: look
for `maybeApart MARTypeVsConstraint`.

Note that, as before, nothing prevents writing instances like:

  instance C (Proxy @Type a) where ...

In particular, TYPE and CONSTRAINT (and the synonyms Type, Constraint
etc) are all allowed in instance heads. It's just that TYPE is not
apart from CONSTRAINT, which means that the above instance would
irretrievably overlap with:

  instance C (Proxy @Constraint a) where ...

Wrinkles

(W1) In GHC.Core.RoughMap.roughMtchTyConName we are careful to map
     TYPE and CONSTRAINT to the same rough-map key.  Reason:
     If we insert (F @Constraint tys) into a FamInstEnv, and look
     up (F @Type tys'), we /must/ ensure that the (C @Constraint tys)
     appears among the unifiables when we do the lookupRM' in
     GHC.Core.FamInstEnv.lookup_fam_inst_env'.  So for the RoughMap we
     simply pretend that they are the same type constructor.  If we
     don't, we'll treat them as fully apart, which is unsound.

(W2) We must extend this treatment to the different arrow types (see
     Note [Function type constructors and FunTy]): if we have
       FunCo (axC Int) <Int> :: (C Int => Int) ~ ((Int -> Int) -> Int),
     then we could extract an equality between (=>) and (->). We thus
     must ensure that (=>) and (->) (among the other arrow combinations)
     are not Apart. See the FunTy/FunTy case in GHC.Core.Unify.unify_ty.

(W3) Are (TYPE IntRep) and (CONSTRAINT WordRep) apart?  In truth yes,
     they are.  But it's easier to say that htey are not apart, by
     reporting "maybeApart" (which is always safe), rather than
     recurse into the arguments (whose kinds may be utterly different)
     to look for apartness inside them.  Again this is in
     GHC.Core.Unify.unify_ty.

(W4) We give a different Typeable instance for Type than for Constraint.
     For type classes instances (unlike type family instances) it is not
     /unsound/ for Type and Constraint to treated as fully distinct; and
     for Typeable is desirable to give them different TypeReps.
     Certainly,
       - both Type and Constraint must /have/ a TypeRep, and
       - they had better not be the same (else eqTypeRep would give us
         a proof Type ~N Constraint, which we do not want
     So in GHC.Tc.Instance.Class.matchTypeable, Type and Constraint are
     treated as separate TyCons; i.e. given no special treatment.

Note [RuntimeRep polymorphism]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Generally speaking, you can't be polymorphic in `RuntimeRep`.  E.g
   f :: forall (rr:RuntimeRep) (a:TYPE rr). a -> [a]
   f = /\(rr:RuntimeRep) (a:rr) \(a:rr). ...
This is no good: we could not generate code for 'f', because the
calling convention for 'f' varies depending on whether the argument is
a a Int, Int#, or Float#.  (You could imagine generating specialised
code, one for each instantiation of 'rr', but we don't do that.)

Certain functions CAN be runtime-rep-polymorphic, because the code
generator never has to manipulate a value of type 'a :: TYPE rr'.

* error :: forall (rr:RuntimeRep) (a:TYPE rr). String -> a
  Code generator never has to manipulate the return value.

* unsafeCoerce#, defined in Desugar.mkUnsafeCoercePair:
  Always inlined to be a no-op
     unsafeCoerce# :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
                             (a :: TYPE r1) (b :: TYPE r2).
                             a -> b

* Unboxed tuples, and unboxed sums, defined in GHC.Builtin.Types
  Always inlined, and hence specialised to the call site
     (#,#) :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
                     (a :: TYPE r1) (b :: TYPE r2).
                     a -> b -> TYPE ('TupleRep '[r1, r2])
-}

----------------------
tYPETyCon :: WiredIn TyCon
tYPETyCon = mkPrimTyCon tYPETyConName
                        (mkTemplateAnonTyConBinders <$> sequence [runtimeRepTy])
                        liftedTypeKind
                        [Nominal]

tYPETyConName :: WiredIn Name
tYPETyConName = mkPrimTc (fsLit "TYPE") tYPETyConKey =<< tYPETyCon

tYPEKind :: WiredIn Type
tYPEKind = mkTyConTy <$> tYPETyCon

----------------------
cONSTRAINTTyCon :: WiredIn TyCon
cONSTRAINTTyCon = mkPrimTyCon cONSTRAINTTyConName
                              (mkTemplateAnonTyConBinders <$> sequence [runtimeRepTy])
                              liftedTypeKind
                              [Nominal]

cONSTRAINTTyConName :: WiredIn Name
cONSTRAINTTyConName = mkPrimTc (fsLit "CONSTRAINT") cONSTRAINTTyConKey =<< cONSTRAINTTyCon

cONSTRAINTKind :: WiredIn Type
cONSTRAINTKind = mkTyConTy <$> cONSTRAINTTyCon


{- *********************************************************************
*                                                                      *
       Basic primitive types (Char#, Int#, etc.)
*                                                                      *
********************************************************************* -}

charPrimTy :: WiredIn Type
charPrimTy      = mkTyConTy <$> charPrimTyCon
charPrimTyCon :: WiredIn TyCon
charPrimTyCon   = pcPrimTyCon0 charPrimTyConName wordRepDataConTy

intPrimTy :: WiredIn Type
intPrimTy       = mkTyConTy <$> intPrimTyCon
intPrimTyCon :: WiredIn TyCon
intPrimTyCon    = pcPrimTyCon0 intPrimTyConName intRepDataConTy

int8PrimTy :: WiredIn Type
int8PrimTy     = mkTyConTy <$> int8PrimTyCon
int8PrimTyCon :: WiredIn TyCon
int8PrimTyCon  = pcPrimTyCon0 int8PrimTyConName int8RepDataConTy

int16PrimTy :: WiredIn Type
int16PrimTy    = mkTyConTy <$> int16PrimTyCon
int16PrimTyCon :: WiredIn TyCon
int16PrimTyCon = pcPrimTyCon0 int16PrimTyConName int16RepDataConTy

int32PrimTy :: WiredIn Type
int32PrimTy     = mkTyConTy <$> int32PrimTyCon
int32PrimTyCon :: WiredIn TyCon
int32PrimTyCon  = pcPrimTyCon0 int32PrimTyConName int32RepDataConTy

int64PrimTy :: WiredIn Type
int64PrimTy     = mkTyConTy <$> int64PrimTyCon
int64PrimTyCon :: WiredIn TyCon
int64PrimTyCon  = pcPrimTyCon0 int64PrimTyConName int64RepDataConTy

wordPrimTy :: WiredIn Type
wordPrimTy      = mkTyConTy <$> wordPrimTyCon
wordPrimTyCon :: WiredIn TyCon
wordPrimTyCon   = pcPrimTyCon0 wordPrimTyConName wordRepDataConTy

word8PrimTy :: WiredIn Type
word8PrimTy     = mkTyConTy <$> word8PrimTyCon
word8PrimTyCon :: WiredIn TyCon
word8PrimTyCon  = pcPrimTyCon0 word8PrimTyConName word8RepDataConTy

word16PrimTy :: WiredIn Type
word16PrimTy    = mkTyConTy <$> word16PrimTyCon
word16PrimTyCon :: WiredIn TyCon
word16PrimTyCon = pcPrimTyCon0 word16PrimTyConName word16RepDataConTy

word32PrimTy :: WiredIn Type
word32PrimTy    = mkTyConTy <$> word32PrimTyCon
word32PrimTyCon :: WiredIn TyCon
word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName word32RepDataConTy

word64PrimTy :: WiredIn Type
word64PrimTy    = mkTyConTy <$> word64PrimTyCon
word64PrimTyCon :: WiredIn TyCon
word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName word64RepDataConTy

addrPrimTy :: WiredIn Type
addrPrimTy      = mkTyConTy <$> addrPrimTyCon
addrPrimTyCon :: WiredIn TyCon
addrPrimTyCon   = pcPrimTyCon0 addrPrimTyConName addrRepDataConTy

floatPrimTy     :: WiredIn Type
floatPrimTy     = mkTyConTy <$> floatPrimTyCon
floatPrimTyCon :: WiredIn TyCon
floatPrimTyCon  = pcPrimTyCon0 floatPrimTyConName floatRepDataConTy

doublePrimTy :: WiredIn Type
doublePrimTy    = mkTyConTy <$> doublePrimTyCon
doublePrimTyCon :: WiredIn TyCon
doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName doubleRepDataConTy


{-
************************************************************************
*                                                                      *
   The @State#@ type (and @_RealWorld@ types)
*                                                                      *
************************************************************************

Note [The equality types story]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GHC sports a veritable menagerie of equality types:

         Type or  Lifted?  Hetero?  Role      Built in         Defining module
         class?    L/U                        TyCon
-----------------------------------------------------------------------------------------
~#         T        U      hetero   nominal   eqPrimTyCon      GHC.Prim
~~         C        L      hetero   nominal   heqTyCon         GHC.Types
~          C        L      homo     nominal   eqTyCon          GHC.Types
:~:        T        L      homo     nominal   (not built-in)   Data.Type.Equality
:~~:       T        L      hetero   nominal   (not built-in)   Data.Type.Equality

~R#        T        U      hetero   repr      eqReprPrimTy     GHC.Prim
Coercible  C        L      homo     repr      coercibleTyCon   GHC.Types
Coercion   T        L      homo     repr      (not built-in)   Data.Type.Coercion
~P#        T        U      hetero   phantom   eqPhantPrimTyCon GHC.Prim

Recall that "hetero" means the equality can related types of different
kinds. Knowing that (t1 ~# t2) or (t1 ~R# t2) or even that (t1 ~P# t2)
also means that (k1 ~# k2), where (t1 :: k1) and (t2 :: k2).

To produce less confusion for end users, when not dumping and without
-fprint-equality-relations, each of these groups is printed as the bottommost
listed equality. That is, (~#) and (~~) are both rendered as (~) in
error messages, and (~R#) is rendered as Coercible.

Let's take these one at a time:

    --------------------------
    (~#) :: forall k1 k2. k1 -> k2 -> TYPE (TupleRep '[])
    --------------------------
This is The Type Of Equality in GHC. It classifies nominal coercions.
This type is used in the solver for recording equality constraints.
It responds "yes" to Type.isEqPrimPred and classifies as an EqPred in
Type.classifyPredType.

All wanted constraints of this type are built with coercion holes.
(See Note [Coercion holes] in GHC.Core.TyCo.Rep.) But see also
Note [Deferred errors for coercion holes] in GHC.Tc.Errors to see how
equality constraints are deferred.

Within GHC, ~# is called eqPrimTyCon, and it is defined in GHC.Builtin.Types.Prim.


    --------------------------
    (~~) :: forall k1 k2. k1 -> k2 -> Constraint
    --------------------------
This is (almost) an ordinary class, defined as if by
  class a ~# b => a ~~ b
  instance a ~# b => a ~~ b
Here's what's unusual about it:

 * We can't actually declare it that way because we don't have syntax for ~#.
   And ~# isn't a constraint, so even if we could write it, it wouldn't kind
   check.

 * Users cannot write instances of it.

 * It is "naturally coherent". This means that the solver won't hesitate to
   solve a goal of type (a ~~ b) even if there is, say (Int ~~ c) in the
   context. (Normally, it waits to learn more, just in case the given
   influences what happens next.) See Note [Naturally coherent classes]
   in GHC.Tc.Solver.Interact.

 * It always terminates. That is, in the UndecidableInstances checks, we
   don't worry if a (~~) constraint is too big, as we know that solving
   equality terminates.

On the other hand, this behaves just like any class w.r.t. eager superclass
unpacking in the solver. So a lifted equality given quickly becomes an unlifted
equality given. This is good, because the solver knows all about unlifted
equalities. There is some special-casing in GHC.Tc.Solver.Interact.matchClassInst to
pretend that there is an instance of this class, as we can't write the instance
in Haskell.

Within GHC, ~~ is called heqTyCon, and it is defined in GHC.Builtin.Types.


    --------------------------
    (~) :: forall k. k -> k -> Constraint
    --------------------------
This is /exactly/ like (~~), except with a homogeneous kind.
It is an almost-ordinary class defined as if by
  class a ~# b => (a :: k) ~ (b :: k)
  instance a ~# b => a ~ b

 * All the bullets for (~~) apply

 * In addition (~) is magical syntax, as ~ is a reserved symbol.
   It cannot be exported or imported.

 * The data constructor of the class is "Eq#", not ":C~"

Within GHC, ~ is called eqTyCon, and it is defined in GHC.Builtin.Types.

Historical note: prior to July 18 (~) was defined as a
  more-ordinary class with (~~) as a superclass.  But that made it
  special in different ways; and the extra superclass selections to
  get from (~) to (~#) via (~~) were tiresome.  Now it's defined
  uniformly with (~~) and Coercible; much nicer.)


    --------------------------
    (:~:) :: forall k. k -> k -> *
    (:~~:) :: forall k1 k2. k1 -> k2 -> *
    --------------------------
These are perfectly ordinary GADTs, wrapping (~) and (~~) resp.
They are not defined within GHC at all.


    --------------------------
    (~R#) :: forall k1 k2. k1 -> k2 -> TYPE (TupleRep '[])
    --------------------------
The is the representational analogue of ~#. This is the type of representational
equalities that the solver works on. All wanted constraints of this type are
built with coercion holes.

Within GHC, ~R# is called eqReprPrimTyCon, and it is defined in GHC.Builtin.Types.Prim.


    --------------------------
    Coercible :: forall k. k -> k -> Constraint
    --------------------------
This is quite like (~~) in the way it's defined and treated within GHC, but
it's homogeneous. Homogeneity helps with type inference (as GHC can solve one
kind from the other) and, in my (Richard's) estimation, will be more intuitive
for users.

An alternative design included HCoercible (like (~~)) and Coercible (like (~)).
One annoyance was that we want `coerce :: Coercible a b => a -> b`, and
we need the type of coerce to be fully wired-in. So the HCoercible/Coercible
split required that both types be fully wired-in. Instead of doing this,
I just got rid of HCoercible, as I'm not sure who would use it, anyway.

Within GHC, Coercible is called coercibleTyCon, and it is defined in
GHC.Builtin.Types.


    --------------------------
    Coercion :: forall k. k -> k -> *
    --------------------------
This is a perfectly ordinary GADT, wrapping Coercible. It is not defined
within GHC at all.


    --------------------------
    (~P#) :: forall k1 k2. k1 -> k2 -> TYPE (TupleRep '[])
    --------------------------
This is the phantom analogue of ~# and it is barely used at all.
(The solver has no idea about this one.) Here is the motivation:

    data Phant a = MkPhant
    type role Phant phantom

    Phant <Int, Bool>_P :: Phant Int ~P# Phant Bool

We just need to have something to put on that last line. You probably
don't need to worry about it.



Note [The State# TyCon]
~~~~~~~~~~~~~~~~~~~~~~~
State# is the primitive, unlifted type of states.  It has one type parameter,
thus
        State# RealWorld
or
        State# s

where s is a type variable. The only purpose of the type parameter is to
keep different state threads separate.  It is represented by nothing at all.

The type parameter to State# is intended to keep separate threads separate.
Even though this parameter is not used in the definition of State#, it is
given role Nominal to enforce its intended use.
-}

mkStatePrimTy :: Type -> WiredIn Type
mkStatePrimTy ty = TyConApp <$> statePrimTyCon <*> pure [ty]

statePrimTyCon :: WiredIn TyCon   -- See Note [The State# TyCon]
statePrimTyCon   = pcPrimTyCon statePrimTyConName [Nominal] zeroBitRepTy

{-
RealWorld is deeply magical.  It is *primitive*, but it is not
*unlifted* (hence ptrArg).  We never manipulate values of type
RealWorld; it's only used in the type system, to parameterise State#.
-}

realWorldTyCon :: WiredIn TyCon
realWorldTyCon = mkPrimTyCon realWorldTyConName (pure []) liftedTypeKind []
realWorldTy :: WiredIn Type
realWorldTy          = mkTyConTy <$> realWorldTyCon
realWorldStatePrimTy :: WiredIn Type
realWorldStatePrimTy = mkStatePrimTy =<< realWorldTy        -- State# RealWorld


mkProxyPrimTy :: Type -> Type -> WiredIn Type
mkProxyPrimTy k ty = TyConApp <$> proxyPrimTyCon <*> pure [k, ty]

proxyPrimTyCon :: WiredIn TyCon
proxyPrimTyCon = mkPrimTyCon proxyPrimTyConName binders res_kind [Nominal,Phantom]
  where
     -- Kind: forall k. k -> TYPE (TupleRep '[])
     binders = mkTemplateTyConBinders <$> sequence [liftedTypeKind] <*> pure id
     res_kind = unboxedTupleKind []


{- *********************************************************************
*                                                                      *
                Primitive equality constraints
    See Note [The equality types story]
*                                                                      *
********************************************************************* -}

eqPrimTyCon :: WiredIn TyCon  -- The representation type for equality predicates
                      -- See Note [The equality types story]
eqPrimTyCon  = mkPrimTyCon eqPrimTyConName binders res_kind roles
  where
    -- Kind :: forall k1 k2. k1 -> k2 -> CONSTRAINT ZeroBitRep
    binders  = mkTemplateTyConBinders <$> sequence [liftedTypeKind, liftedTypeKind] <*> pure id
    res_kind = TyConApp <$> cONSTRAINTTyCon <*> sequence [zeroBitRepTy]
    roles    = [Nominal, Nominal, Nominal, Nominal]

-- like eqPrimTyCon, but the type for *Representational* coercions
-- this should only ever appear as the type of a covar. Its role is
-- interpreted in coercionRole
eqReprPrimTyCon :: WiredIn TyCon   -- See Note [The equality types story]
eqReprPrimTyCon = mkPrimTyCon eqReprPrimTyConName binders res_kind roles
  where
    -- Kind :: forall k1 k2. k1 -> k2 -> CONSTRAINT ZeroBitRep
    binders  = mkTemplateTyConBinders <$> sequence [liftedTypeKind, liftedTypeKind] <*> pure id
    res_kind = TyConApp <$> cONSTRAINTTyCon <*> sequence [zeroBitRepTy]
    roles    = [Nominal, Nominal, Representational, Representational]

-- like eqPrimTyCon, but the type for *Phantom* coercions.
-- This is only used to make higher-order equalities. Nothing
-- should ever actually have this type!
eqPhantPrimTyCon :: WiredIn TyCon
eqPhantPrimTyCon = mkPrimTyCon eqPhantPrimTyConName binders res_kind roles
  where
    -- Kind :: forall k1 k2. k1 -> k2 -> CONSTRAINT ZeroBitRep
    binders  = mkTemplateTyConBinders <$> sequence [liftedTypeKind, liftedTypeKind] <*> pure id
    res_kind = TyConApp <$> cONSTRAINTTyCon <*> sequence [zeroBitRepTy]
    roles    = [Nominal, Nominal, Phantom, Phantom]

-- | Given a Role, what TyCon is the type of equality predicates at that role?
equalityTyCon :: Role -> WiredIn TyCon
equalityTyCon Nominal          = eqPrimTyCon
equalityTyCon Representational = eqReprPrimTyCon
equalityTyCon Phantom          = eqPhantPrimTyCon

{- *********************************************************************
*                                                                      *
             The primitive array types
*                                                                      *
********************************************************************* -}

arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
    byteArrayPrimTyCon,
    smallArrayPrimTyCon, smallMutableArrayPrimTyCon :: WiredIn TyCon
arrayPrimTyCon             = pcPrimTyCon_LevPolyLastArg arrayPrimTyConName        [Representational]          unliftedRepTy
mutableArrayPrimTyCon      = pcPrimTyCon_LevPolyLastArg mutableArrayPrimTyConName [Nominal, Representational] unliftedRepTy
mutableByteArrayPrimTyCon  = pcPrimTyCon mutableByteArrayPrimTyConName  [Nominal] unliftedRepTy
byteArrayPrimTyCon         = pcPrimTyCon0 byteArrayPrimTyConName        unliftedRepTy
smallArrayPrimTyCon        = pcPrimTyCon_LevPolyLastArg smallArrayPrimTyConName        [Representational]          unliftedRepTy
smallMutableArrayPrimTyCon = pcPrimTyCon_LevPolyLastArg smallMutableArrayPrimTyConName [Nominal, Representational] unliftedRepTy

mkArrayPrimTy :: Type -> WiredIn Type
mkArrayPrimTy elt           = TyConApp <$> arrayPrimTyCon <*> pure [getLevity elt, elt]
byteArrayPrimTy :: WiredIn Type
byteArrayPrimTy             = mkTyConTy <$> byteArrayPrimTyCon
mkSmallArrayPrimTy :: Type -> WiredIn Type
mkSmallArrayPrimTy elt = TyConApp <$> smallArrayPrimTyCon <*> pure [getLevity elt, elt]
mkMutableArrayPrimTy :: Type -> Type -> WiredIn Type
mkMutableArrayPrimTy s elt  = TyConApp <$> mutableArrayPrimTyCon <*> pure [getLevity elt, s, elt]
mkMutableByteArrayPrimTy :: Type -> WiredIn Type
mkMutableByteArrayPrimTy s  = TyConApp <$> mutableByteArrayPrimTyCon <*> pure [s]
mkSmallMutableArrayPrimTy :: Type -> Type -> WiredIn Type
mkSmallMutableArrayPrimTy s elt = TyConApp <$> smallMutableArrayPrimTyCon <*> pure [getLevity elt, s, elt]


{- *********************************************************************
*                                                                      *
                The mutable variable type
*                                                                      *
********************************************************************* -}

mutVarPrimTyCon :: WiredIn TyCon
mutVarPrimTyCon = pcPrimTyCon_LevPolyLastArg mutVarPrimTyConName [Nominal, Representational] unliftedRepTy

mkMutVarPrimTy :: Type -> Type -> WiredIn Type
mkMutVarPrimTy s elt        = TyConApp <$> mutVarPrimTyCon <*> pure [getLevity elt, s, elt]

{-
************************************************************************
*                                                                      *
\subsection[TysPrim-io-port-var]{The synchronizing I/O Port type}
*                                                                      *
************************************************************************
-}

ioPortPrimTyCon :: WiredIn TyCon
ioPortPrimTyCon = pcPrimTyCon_LevPolyLastArg ioPortPrimTyConName [Nominal, Representational] unliftedRepTy

mkIOPortPrimTy :: Type -> Type -> WiredIn Type
mkIOPortPrimTy s elt          = TyConApp <$> ioPortPrimTyCon <*> pure [getLevity elt, s, elt]

{-
************************************************************************
*                                                                      *
   The synchronizing variable type
\subsection[TysPrim-synch-var]{The synchronizing variable type}
*                                                                      *
************************************************************************
-}

mVarPrimTyCon :: WiredIn TyCon
mVarPrimTyCon = pcPrimTyCon_LevPolyLastArg mVarPrimTyConName [Nominal, Representational] unliftedRepTy

mkMVarPrimTy :: Type -> Type -> WiredIn Type
mkMVarPrimTy s elt          = TyConApp <$> mVarPrimTyCon <*> pure [getLevity elt, s, elt]

{-
************************************************************************
*                                                                      *
   The transactional variable type
*                                                                      *
************************************************************************
-}

tVarPrimTyCon :: WiredIn TyCon
tVarPrimTyCon = pcPrimTyCon_LevPolyLastArg tVarPrimTyConName [Nominal, Representational] unliftedRepTy

mkTVarPrimTy :: Type -> Type -> WiredIn Type
mkTVarPrimTy s elt = TyConApp <$> tVarPrimTyCon <*> pure [getLevity elt, s, elt]

{-
************************************************************************
*                                                                      *
   The stable-pointer type
*                                                                      *
************************************************************************
-}

stablePtrPrimTyCon :: WiredIn TyCon
stablePtrPrimTyCon = pcPrimTyCon_LevPolyLastArg stablePtrPrimTyConName [Representational] addrRepDataConTy

mkStablePtrPrimTy :: Type -> WiredIn Type
mkStablePtrPrimTy ty = TyConApp <$> stablePtrPrimTyCon <*> pure [getLevity ty, ty]

{-
************************************************************************
*                                                                      *
   The stable-name type
*                                                                      *
************************************************************************
-}

stableNamePrimTyCon :: WiredIn TyCon
stableNamePrimTyCon = pcPrimTyCon_LevPolyLastArg stableNamePrimTyConName [Phantom] unliftedRepTy

mkStableNamePrimTy :: Type -> WiredIn Type
mkStableNamePrimTy ty = TyConApp <$> stableNamePrimTyCon <*> pure [getLevity ty, ty]

{-
************************************************************************
*                                                                      *
   The Compact NFData (CNF) type
*                                                                      *
************************************************************************
-}

compactPrimTyCon :: WiredIn TyCon
compactPrimTyCon = pcPrimTyCon0 compactPrimTyConName unliftedRepTy

compactPrimTy :: WiredIn Type
compactPrimTy = mkTyConTy <$> compactPrimTyCon

{-
************************************************************************
*                                                                      *
   The @StackSnapshot#@ type
*                                                                      *
************************************************************************
-}

stackSnapshotPrimTyCon :: WiredIn TyCon
stackSnapshotPrimTyCon = pcPrimTyCon0 stackSnapshotPrimTyConName unliftedRepTy

stackSnapshotPrimTy :: WiredIn Type
stackSnapshotPrimTy = mkTyConTy <$> stackSnapshotPrimTyCon


{-
************************************************************************
*                                                                      *
   The ``bytecode object'' type
*                                                                      *
************************************************************************
-}

-- Unlike most other primitive types, BCO is lifted. This is because in
-- general a BCO may be a thunk for the reasons given in Note [Updatable CAF
-- BCOs] in GHCi.CreateBCO.
bcoPrimTy    :: WiredIn Type
bcoPrimTy    = mkTyConTy <$> bcoPrimTyCon
bcoPrimTyCon :: WiredIn TyCon
bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName liftedRepTy

{-
************************************************************************
*                                                                      *
   The ``weak pointer'' type
*                                                                      *
************************************************************************
-}

weakPrimTyCon :: WiredIn TyCon
weakPrimTyCon = pcPrimTyCon_LevPolyLastArg weakPrimTyConName [Representational] unliftedRepTy

mkWeakPrimTy :: Type -> WiredIn Type
mkWeakPrimTy v = TyConApp <$> weakPrimTyCon <*> pure [getLevity v, v]

{-
************************************************************************
*                                                                      *
   The ``thread id'' type
*                                                                      *
************************************************************************

A thread id is represented by a pointer to the TSO itself, to ensure
that they are always unique and we can always find the TSO for a given
thread id.  However, this has the unfortunate consequence that a
ThreadId# for a given thread is treated as a root by the garbage
collector and can keep TSOs around for too long.

Hence the programmer API for thread manipulation uses a weak pointer
to the thread id internally.
-}

threadIdPrimTy :: WiredIn Type
threadIdPrimTy    = mkTyConTy <$> threadIdPrimTyCon
threadIdPrimTyCon :: WiredIn TyCon
threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName unliftedRepTy

{-
************************************************************************
*                                                                      *
   The ``prompt tag'' type
*                                                                      *
************************************************************************
-}

promptTagPrimTyCon :: WiredIn TyCon
promptTagPrimTyCon = pcPrimTyCon promptTagPrimTyConName [Representational] unliftedRepTy

mkPromptTagPrimTy :: Type -> WiredIn Type
mkPromptTagPrimTy v = TyConApp <$> promptTagPrimTyCon <*> pure [v]

{-
************************************************************************
*                                                                      *
\subsection{SIMD vector types}
*                                                                      *
************************************************************************
-}

#include "primop-vector-tys.hs-incl"