Prim.hs 45.5 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1 2 3
{-
(c) The AQUA Project, Glasgow University, 1994-1998

4

Sylvain Henry's avatar
Sylvain Henry committed
5
Wired-in knowledge about primitive types
Austin Seipp's avatar
Austin Seipp committed
6
-}
7

8
{-# LANGUAGE CPP #-}
9
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
10 11

-- | This module defines TyCons that can't be expressed in Haskell.
12
--   They are all, therefore, wired-in TyCons.  C.f module "GHC.Builtin.Types"
Sylvain Henry's avatar
Sylvain Henry committed
13 14
module GHC.Builtin.Types.Prim(
        mkPrimTyConName, -- For implicit parameters in GHC.Builtin.Types only
15

16
        mkTemplateKindVars, mkTemplateTyVars, mkTemplateTyVarsFrom,
17
        mkTemplateKiTyVars, mkTemplateKiTyVar,
18 19 20 21

        mkTemplateTyConBinders, mkTemplateKindTyConBinders,
        mkTemplateAnonTyConBinders,

22 23
        alphaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
        alphaTys, alphaTy, betaTy, gammaTy, deltaTy,
24 25
        alphaTyVarsUnliftedRep, alphaTyVarUnliftedRep,
        alphaTysUnliftedRep, alphaTyUnliftedRep,
26
        runtimeRep1TyVar, runtimeRep2TyVar, runtimeRep1Ty, runtimeRep2Ty,
27
        openAlphaTy, openBetaTy, openAlphaTyVar, openBetaTyVar,
28

Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
29 30
        multiplicityTyVar,

31
        -- Kind constructors...
Ben Gamari's avatar
Ben Gamari committed
32
        tYPETyCon, tYPETyConName,
33 34

        -- Kinds
Richard Eisenberg's avatar
Richard Eisenberg committed
35
        tYPE, primRepToRuntimeRep,
36

Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
37
        functionWithMultiplicity,
38
        funTyCon, funTyConName,
39
        unexposedPrimTyCons, exposedPrimTyCons, primTyCons,
40

41 42 43 44 45 46
        charPrimTyCon,          charPrimTy, charPrimTyConName,
        intPrimTyCon,           intPrimTy, intPrimTyConName,
        wordPrimTyCon,          wordPrimTy, wordPrimTyConName,
        addrPrimTyCon,          addrPrimTy, addrPrimTyConName,
        floatPrimTyCon,         floatPrimTy, floatPrimTyConName,
        doublePrimTyCon,        doublePrimTy, doublePrimTyConName,
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

        statePrimTyCon,         mkStatePrimTy,
        realWorldTyCon,         realWorldTy, realWorldStatePrimTy,

        proxyPrimTyCon,         mkProxyPrimTy,

        arrayPrimTyCon, mkArrayPrimTy,
        byteArrayPrimTyCon,     byteArrayPrimTy,
        arrayArrayPrimTyCon, mkArrayArrayPrimTy,
        smallArrayPrimTyCon, mkSmallArrayPrimTy,
        mutableArrayPrimTyCon, mkMutableArrayPrimTy,
        mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
        mutableArrayArrayPrimTyCon, mkMutableArrayArrayPrimTy,
        smallMutableArrayPrimTyCon, mkSmallMutableArrayPrimTy,
        mutVarPrimTyCon, mkMutVarPrimTy,

        mVarPrimTyCon,                  mkMVarPrimTy,
64
        ioPortPrimTyCon,                mkIOPortPrimTy,
65
        tVarPrimTyCon,                  mkTVarPrimTy,
66 67
        stablePtrPrimTyCon,             mkStablePtrPrimTy,
        stableNamePrimTyCon,            mkStableNamePrimTy,
gcampax's avatar
gcampax committed
68
        compactPrimTyCon,               compactPrimTy,
69 70 71 72
        bcoPrimTyCon,                   bcoPrimTy,
        weakPrimTyCon,                  mkWeakPrimTy,
        threadIdPrimTyCon,              threadIdPrimTy,

73 74
        int8PrimTyCon,          int8PrimTy, int8PrimTyConName,
        word8PrimTyCon,         word8PrimTy, word8PrimTyConName,
Michal Terepeta's avatar
Michal Terepeta committed
75

76 77
        int16PrimTyCon,         int16PrimTy, int16PrimTyConName,
        word16PrimTyCon,        word16PrimTy, word16PrimTyConName,
Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
78

79 80
        int32PrimTyCon,         int32PrimTy, int32PrimTyConName,
        word32PrimTyCon,        word32PrimTy, word32PrimTyConName,
81

82 83
        int64PrimTyCon,         int64PrimTy, int64PrimTyConName,
        word64PrimTyCon,        word64PrimTy, word64PrimTyConName,
84

batterseapower's avatar
batterseapower committed
85
        eqPrimTyCon,            -- ty1 ~# ty2
86
        eqReprPrimTyCon,        -- ty1 ~R# ty2  (at role Representational)
87
        eqPhantPrimTyCon,       -- ty1 ~P# ty2  (at role Phantom)
88
        equalityTyCon,
89

90
        -- * SIMD
91
#include "primop-vector-tys-exports.hs-incl"
92
  ) where
93

94
#include "HsVersions.h"
95

96
import GHC.Prelude
97

Sylvain Henry's avatar
Sylvain Henry committed
98
import {-# SOURCE #-} GHC.Builtin.Types
Richard Eisenberg's avatar
Richard Eisenberg committed
99 100
  ( runtimeRepTy, unboxedTupleKind, liftedTypeKind
  , vecRepDataConTyCon, tupleRepDataConTyCon
101
  , liftedRepDataConTy, unliftedRepDataConTy
John Ericson's avatar
John Ericson committed
102 103 104 105
  , intRepDataConTy
  , int8RepDataConTy, int16RepDataConTy, int32RepDataConTy, int64RepDataConTy
  , wordRepDataConTy
  , word16RepDataConTy, word8RepDataConTy, word32RepDataConTy, word64RepDataConTy
Michal Terepeta's avatar
Michal Terepeta committed
106
  , addrRepDataConTy
107 108 109 110 111 112
  , floatRepDataConTy, doubleRepDataConTy
  , vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy
  , vec64DataConTy
  , int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy
  , int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy
  , word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy
Richard Eisenberg's avatar
Richard Eisenberg committed
113
  , doubleElemRepDataConTy
Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
114
  , mkPromotedListTy, multiplicityTy )
115

Sylvain Henry's avatar
Sylvain Henry committed
116 117
import GHC.Types.Var    ( TyVar, mkTyVar )
import GHC.Types.Name
Sylvain Henry's avatar
Sylvain Henry committed
118
import {-# SOURCE #-} GHC.Types.TyThing
Sylvain Henry's avatar
Sylvain Henry committed
119
import GHC.Core.TyCon
Sylvain Henry's avatar
Sylvain Henry committed
120 121
import GHC.Types.SrcLoc
import GHC.Types.Unique
Aditya Gupta's avatar
Aditya Gupta committed
122
import GHC.Builtin.Uniques
Sylvain Henry's avatar
Sylvain Henry committed
123
import GHC.Builtin.Names
124 125
import GHC.Data.FastString
import GHC.Utils.Outputable
126
import GHC.Utils.Panic
Sylvain Henry's avatar
Sylvain Henry committed
127 128
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
129
import {-# SOURCE #-} GHC.Core.Type ( mkTyConTy )
130

Ian Lynagh's avatar
Ian Lynagh committed
131
import Data.Char
132

Austin Seipp's avatar
Austin Seipp committed
133 134 135
{-
************************************************************************
*                                                                      *
136
\subsection{Primitive type constructors}
Austin Seipp's avatar
Austin Seipp committed
137 138 139
*                                                                      *
************************************************************************
-}
140 141

primTyCons :: [TyCon]
142 143
primTyCons = unexposedPrimTyCons ++ exposedPrimTyCons

144
-- | Primitive 'TyCon's that are defined in GHC.Prim but not exposed.
145
-- It's important to keep these separate as we don't want users to be able to
146 147
-- write them (see #15209) or see them in GHCi's @:browse@ output
-- (see #12023).
148 149 150 151 152 153 154
unexposedPrimTyCons :: [TyCon]
unexposedPrimTyCons
  = [ eqPrimTyCon
    , eqReprPrimTyCon
    , eqPhantPrimTyCon
    ]

155
-- | Primitive 'TyCon's that are defined in, and exported from, GHC.Prim.
156 157
exposedPrimTyCons :: [TyCon]
exposedPrimTyCons
158 159 160
  = [ addrPrimTyCon
    , arrayPrimTyCon
    , byteArrayPrimTyCon
161
    , arrayArrayPrimTyCon
162
    , smallArrayPrimTyCon
163 164 165 166
    , charPrimTyCon
    , doublePrimTyCon
    , floatPrimTyCon
    , intPrimTyCon
Michal Terepeta's avatar
Michal Terepeta committed
167
    , int8PrimTyCon
Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
168
    , int16PrimTyCon
apt's avatar
apt committed
169
    , int32PrimTyCon
170 171 172 173 174
    , int64PrimTyCon
    , bcoPrimTyCon
    , weakPrimTyCon
    , mutableArrayPrimTyCon
    , mutableByteArrayPrimTyCon
175
    , mutableArrayArrayPrimTyCon
176
    , smallMutableArrayPrimTyCon
177
    , mVarPrimTyCon
178
    , ioPortPrimTyCon
179
    , tVarPrimTyCon
180 181 182 183
    , mutVarPrimTyCon
    , realWorldTyCon
    , stablePtrPrimTyCon
    , stableNamePrimTyCon
gcampax's avatar
gcampax committed
184
    , compactPrimTyCon
185
    , statePrimTyCon
186
    , proxyPrimTyCon
187 188
    , threadIdPrimTyCon
    , wordPrimTyCon
Michal Terepeta's avatar
Michal Terepeta committed
189
    , word8PrimTyCon
Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
190
    , word16PrimTyCon
apt's avatar
apt committed
191
    , word32PrimTyCon
192
    , word64PrimTyCon
193

194
    , tYPETyCon
Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
195
    , funTyCon
196

197
#include "primop-vector-tycons.hs-incl"
198 199
    ]

200
mkPrimTc :: FastString -> Unique -> TyCon -> Name
Ian Lynagh's avatar
Ian Lynagh committed
201
mkPrimTc fs unique tycon
202 203
  = mkWiredInName gHC_PRIM (mkTcOccFS fs)
                  unique
Sylvain Henry's avatar
Sylvain Henry committed
204
                  (mkATyCon tycon)        -- Relevant TyCon
205
                  UserSyntax
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
206 207 208

mkBuiltInPrimTc :: FastString -> Unique -> TyCon -> Name
mkBuiltInPrimTc fs unique tycon
209 210
  = mkWiredInName gHC_PRIM (mkTcOccFS fs)
                  unique
Sylvain Henry's avatar
Sylvain Henry committed
211
                  (mkATyCon tycon)        -- Relevant TyCon
212
                  BuiltInSyntax
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
213

214

215
charPrimTyConName, intPrimTyConName, int8PrimTyConName, int16PrimTyConName, int32PrimTyConName, int64PrimTyConName, wordPrimTyConName, word32PrimTyConName, word8PrimTyConName, word16PrimTyConName, word64PrimTyConName, addrPrimTyConName, floatPrimTyConName, doublePrimTyConName, statePrimTyConName, proxyPrimTyConName, realWorldTyConName, arrayPrimTyConName, arrayArrayPrimTyConName, smallArrayPrimTyConName, byteArrayPrimTyConName, mutableArrayPrimTyConName, mutableByteArrayPrimTyConName, mutableArrayArrayPrimTyConName, smallMutableArrayPrimTyConName, mutVarPrimTyConName, mVarPrimTyConName, ioPortPrimTyConName, tVarPrimTyConName, stablePtrPrimTyConName, stableNamePrimTyConName, compactPrimTyConName, bcoPrimTyConName, weakPrimTyConName, threadIdPrimTyConName, eqPrimTyConName, eqReprPrimTyConName, eqPhantPrimTyConName :: Name
216 217
charPrimTyConName             = mkPrimTc (fsLit "Char#") charPrimTyConKey charPrimTyCon
intPrimTyConName              = mkPrimTc (fsLit "Int#") intPrimTyConKey  intPrimTyCon
Michal Terepeta's avatar
Michal Terepeta committed
218
int8PrimTyConName             = mkPrimTc (fsLit "Int8#") int8PrimTyConKey int8PrimTyCon
Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
219
int16PrimTyConName            = mkPrimTc (fsLit "Int16#") int16PrimTyConKey int16PrimTyCon
220 221 222
int32PrimTyConName            = mkPrimTc (fsLit "Int32#") int32PrimTyConKey int32PrimTyCon
int64PrimTyConName            = mkPrimTc (fsLit "Int64#") int64PrimTyConKey int64PrimTyCon
wordPrimTyConName             = mkPrimTc (fsLit "Word#") wordPrimTyConKey wordPrimTyCon
Michal Terepeta's avatar
Michal Terepeta committed
223
word8PrimTyConName            = mkPrimTc (fsLit "Word8#") word8PrimTyConKey word8PrimTyCon
Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
224
word16PrimTyConName           = mkPrimTc (fsLit "Word16#") word16PrimTyConKey word16PrimTyCon
225 226 227 228 229
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
230
statePrimTyConName            = mkPrimTc (fsLit "State#") statePrimTyConKey statePrimTyCon
231
proxyPrimTyConName            = mkPrimTc (fsLit "Proxy#") proxyPrimTyConKey proxyPrimTyCon
batterseapower's avatar
batterseapower committed
232
eqPrimTyConName               = mkPrimTc (fsLit "~#") eqPrimTyConKey eqPrimTyCon
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
233
eqReprPrimTyConName           = mkBuiltInPrimTc (fsLit "~R#") eqReprPrimTyConKey eqReprPrimTyCon
234
eqPhantPrimTyConName          = mkBuiltInPrimTc (fsLit "~P#") eqPhantPrimTyConKey eqPhantPrimTyCon
235
realWorldTyConName            = mkPrimTc (fsLit "RealWorld") realWorldTyConKey realWorldTyCon
236 237
arrayPrimTyConName            = mkPrimTc (fsLit "Array#") arrayPrimTyConKey arrayPrimTyCon
byteArrayPrimTyConName        = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey byteArrayPrimTyCon
Sylvain Henry's avatar
Sylvain Henry committed
238
arrayArrayPrimTyConName       = mkPrimTc (fsLit "ArrayArray#") arrayArrayPrimTyConKey arrayArrayPrimTyCon
239
smallArrayPrimTyConName       = mkPrimTc (fsLit "SmallArray#") smallArrayPrimTyConKey smallArrayPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
240 241
mutableArrayPrimTyConName     = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey mutableArrayPrimTyCon
mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey mutableByteArrayPrimTyCon
242
mutableArrayArrayPrimTyConName= mkPrimTc (fsLit "MutableArrayArray#") mutableArrayArrayPrimTyConKey mutableArrayArrayPrimTyCon
243
smallMutableArrayPrimTyConName= mkPrimTc (fsLit "SmallMutableArray#") smallMutableArrayPrimTyConKey smallMutableArrayPrimTyCon
244
mutVarPrimTyConName           = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey mutVarPrimTyCon
245
ioPortPrimTyConName           = mkPrimTc (fsLit "IOPort#") ioPortPrimTyConKey ioPortPrimTyCon
246 247
mVarPrimTyConName             = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey mVarPrimTyCon
tVarPrimTyConName             = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey tVarPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
248 249
stablePtrPrimTyConName        = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey stablePtrPrimTyCon
stableNamePrimTyConName       = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey stableNamePrimTyCon
gcampax's avatar
gcampax committed
250
compactPrimTyConName          = mkPrimTc (fsLit "Compact#") compactPrimTyConKey compactPrimTyCon
Ben Gamari's avatar
Ben Gamari committed
251
bcoPrimTyConName              = mkPrimTc (fsLit "BCO") bcoPrimTyConKey bcoPrimTyCon
252 253
weakPrimTyConName             = mkPrimTc (fsLit "Weak#") weakPrimTyConKey weakPrimTyCon
threadIdPrimTyConName         = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey threadIdPrimTyCon
254

Austin Seipp's avatar
Austin Seipp committed
255 256 257
{-
************************************************************************
*                                                                      *
258
\subsection{Support code}
Austin Seipp's avatar
Austin Seipp committed
259 260
*                                                                      *
************************************************************************
261

262 263
alphaTyVars is a list of type variables for use in templates:
        ["a", "b", ..., "z", "t1", "t2", ... ]
Austin Seipp's avatar
Austin Seipp committed
264
-}
265

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

269 270 271 272
mkTemplateKindVars :: [Kind] -> [TyVar]
-- k0  with unique (mkAlphaTyVarUnique 0)
-- k1  with unique (mkAlphaTyVarUnique 1)
-- ... etc
273 274
mkTemplateKindVars [kind] = [mkTemplateKindVar kind]
  -- Special case for one kind: just "k"
275
mkTemplateKindVars kinds
276 277 278 279 280 281
  = [ 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
282 283 284 285 286 287

mkTemplateTyVarsFrom :: Int -> [Kind] -> [TyVar]
-- a  with unique (mkAlphaTyVarUnique n)
-- b  with unique (mkAlphaTyVarUnique n+1)
-- ... etc
-- Typically called as
288
--   mkTemplateTyVarsFrom (length kv_bndrs) kinds
289 290 291 292 293 294 295
-- 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
296
          name = mk_tv_name (index + n) name_str
297 298
    ]

299
mkTemplateTyVars :: [Kind] -> [TyVar]
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
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
323
-- call mkTemplateKiTyVars [RuntimeRep] (\[r] -> [TYPE r, *])
324 325 326 327 328 329 330
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

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
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

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

mkTemplateAnonTyConBinders :: [Kind] -> [TyConBinder]
Simon Peyton Jones's avatar
Simon Peyton Jones committed
351
mkTemplateAnonTyConBinders kinds = mkAnonTyConBinders VisArg (mkTemplateTyVars kinds)
352 353

mkTemplateAnonTyConBindersFrom :: Int -> [Kind] -> [TyConBinder]
Simon Peyton Jones's avatar
Simon Peyton Jones committed
354
mkTemplateAnonTyConBindersFrom n kinds = mkAnonTyConBinders VisArg (mkTemplateTyVarsFrom n kinds)
355

356
alphaTyVars :: [TyVar]
357
alphaTyVars = mkTemplateTyVars $ repeat liftedTypeKind
358

Ian Lynagh's avatar
Ian Lynagh committed
359
alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: TyVar
360 361
(alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars

Ian Lynagh's avatar
Ian Lynagh committed
362
alphaTys :: [Type]
363
alphaTys = mkTyVarTys alphaTyVars
Ian Lynagh's avatar
Ian Lynagh committed
364
alphaTy, betaTy, gammaTy, deltaTy :: Type
365
(alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
366

367
alphaTyVarsUnliftedRep :: [TyVar]
368
alphaTyVarsUnliftedRep = mkTemplateTyVars $ repeat (tYPE unliftedRepDataConTy)
369 370 371 372 373 374 375 376 377

alphaTyVarUnliftedRep :: TyVar
(alphaTyVarUnliftedRep:_) = alphaTyVarsUnliftedRep

alphaTysUnliftedRep :: [Type]
alphaTysUnliftedRep = mkTyVarTys alphaTyVarsUnliftedRep
alphaTyUnliftedRep :: Type
(alphaTyUnliftedRep:_) = alphaTysUnliftedRep

378 379 380
runtimeRep1TyVar, runtimeRep2TyVar :: TyVar
(runtimeRep1TyVar : runtimeRep2TyVar : _)
  = drop 16 (mkTemplateTyVars (repeat runtimeRepTy))  -- selects 'q','r'
381

382 383 384
runtimeRep1Ty, runtimeRep2Ty :: Type
runtimeRep1Ty = mkTyVarTy runtimeRep1TyVar
runtimeRep2Ty = mkTyVarTy runtimeRep2TyVar
385

Ian Lynagh's avatar
Ian Lynagh committed
386
openAlphaTyVar, openBetaTyVar :: TyVar
387 388
-- alpha :: TYPE r1
-- beta  :: TYPE r2
389
[openAlphaTyVar,openBetaTyVar]
390
  = mkTemplateTyVars [tYPE runtimeRep1Ty, tYPE runtimeRep2Ty]
391

Ian Lynagh's avatar
Ian Lynagh committed
392
openAlphaTy, openBetaTy :: Type
393
openAlphaTy = mkTyVarTy openAlphaTyVar
394 395
openBetaTy  = mkTyVarTy openBetaTyVar

Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
396 397 398
multiplicityTyVar :: TyVar
multiplicityTyVar = mkTemplateTyVars (repeat multiplicityTy) !! 13  -- selects 'n'

Austin Seipp's avatar
Austin Seipp committed
399 400 401
{-
************************************************************************
*                                                                      *
402
                FunTyCon
Austin Seipp's avatar
Austin Seipp committed
403 404 405
*                                                                      *
************************************************************************
-}
406

407
funTyConName :: Name
Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
408
funTyConName = mkPrimTyConName (fsLit "FUN") funTyConKey funTyCon
409

Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
410
-- | The @FUN@ type constructor.
Ben Gamari's avatar
Ben Gamari committed
411 412
--
-- @
413 414 415
-- FUN :: forall (m :: Multiplicity) ->
--        forall {rep1 :: RuntimeRep} {rep2 :: RuntimeRep}.
--        TYPE rep1 -> TYPE rep2 -> *
Ben Gamari's avatar
Ben Gamari committed
416
-- @
417 418 419 420 421 422 423 424 425 426 427
--
-- 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. To allow visible application a type synonym can be
-- defined:
--
-- @
-- type Arr :: forall (rep1 :: RuntimeRep) (rep2 :: RuntimeRep).
--             TYPE rep1 -> TYPE rep2 -> Type
428
-- type Arr = FUN 'Many
429 430
-- @
--
431
funTyCon :: TyCon
432
funTyCon = mkFunTyCon funTyConName tc_bndrs tc_rep_nm
433
  where
Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
434 435 436
    -- See also unrestrictedFunTyCon
    tc_bndrs = [ mkNamedTyConBinder Required multiplicityTyVar
               , mkNamedTyConBinder Inferred runtimeRep1TyVar
Simon Peyton Jones's avatar
Simon Peyton Jones committed
437
               , mkNamedTyConBinder Inferred runtimeRep2TyVar ]
Ben Gamari's avatar
Ben Gamari committed
438 439 440
               ++ mkTemplateAnonTyConBinders [ tYPE runtimeRep1Ty
                                             , tYPE runtimeRep2Ty
                                             ]
441
    tc_rep_nm = mkPrelTyConRepName funTyConName
442

Austin Seipp's avatar
Austin Seipp committed
443 444 445
{-
************************************************************************
*                                                                      *
446
                Kinds
Austin Seipp's avatar
Austin Seipp committed
447 448
*                                                                      *
************************************************************************
449

Simon Peyton Jones's avatar
Simon Peyton Jones committed
450 451 452 453 454
Note [TYPE and RuntimeRep]
~~~~~~~~~~~~~~~~~~~~~~~~~~
All types that classify values have a kind of the form (TYPE rr), where

    data RuntimeRep     -- Defined in ghc-prim:GHC.Types
455 456
      = LiftedRep
      | UnliftedRep
Simon Peyton Jones's avatar
Simon Peyton Jones committed
457 458 459 460 461 462
      | IntRep
      | FloatRep
      .. etc ..

    rr :: RuntimeRep

Richard Eisenberg's avatar
Richard Eisenberg committed
463
    TYPE :: RuntimeRep -> TYPE 'LiftedRep  -- Built in
Simon Peyton Jones's avatar
Simon Peyton Jones committed
464 465

So for example:
466 467
    Int        :: TYPE 'LiftedRep
    Array# Int :: TYPE 'UnliftedRep
Simon Peyton Jones's avatar
Simon Peyton Jones committed
468 469
    Int#       :: TYPE 'IntRep
    Float#     :: TYPE 'FloatRep
470
    Maybe      :: TYPE 'LiftedRep -> TYPE 'LiftedRep
Richard Eisenberg's avatar
Richard Eisenberg committed
471
    (# , #)    :: TYPE r1 -> TYPE r2 -> TYPE (TupleRep [r1, r2])
Simon Peyton Jones's avatar
Simon Peyton Jones committed
472 473

We abbreviate '*' specially:
474
    type * = TYPE 'LiftedRep
Simon Peyton Jones's avatar
Simon Peyton Jones committed
475

476
The 'rr' parameter tells us how the value is represented at runtime.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
477 478 479 480

Generally speaking, you can't be polymorphic in 'rr'.  E.g
   f :: forall (rr:RuntimeRep) (a:TYPE rr). a -> [a]
   f = /\(rr:RuntimeRep) (a:rr) \(a:rr). ...
481
This is no good: we could not generate code for 'f', because the
Simon Peyton Jones's avatar
Simon Peyton Jones committed
482 483 484 485 486 487 488 489 490 491
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.

492
* unsafeCoerce#, defined in Desugar.mkUnsafeCoercePair:
Simon Peyton Jones's avatar
Simon Peyton Jones committed
493 494 495 496 497
  Always inlined to be a no-op
     unsafeCoerce# :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
                             (a :: TYPE r1) (b :: TYPE r2).
                             a -> b

Sylvain Henry's avatar
Sylvain Henry committed
498
* Unboxed tuples, and unboxed sums, defined in GHC.Builtin.Types
Simon Peyton Jones's avatar
Simon Peyton Jones committed
499 500 501
  Always inlined, and hence specialised to the call site
     (#,#) :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
                     (a :: TYPE r1) (b :: TYPE r2).
Richard Eisenberg's avatar
Richard Eisenberg committed
502
                     a -> b -> TYPE ('TupleRep '[r1, r2])
Simon Peyton Jones's avatar
Simon Peyton Jones committed
503 504 505

Note [PrimRep and kindPrimRep]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sylvain Henry's avatar
Sylvain Henry committed
506
As part of its source code, in GHC.Core.TyCon, GHC has
Richard Eisenberg's avatar
Richard Eisenberg committed
507
  data PrimRep = LiftedRep | UnliftedRep | IntRep | FloatRep | ...etc...
Simon Peyton Jones's avatar
Simon Peyton Jones committed
508 509 510 511 512

Notice that
 * RuntimeRep is part of the syntax tree of the program being compiled
     (defined in a library: ghc-prim:GHC.Types)
 * PrimRep is part of GHC's source code.
Sylvain Henry's avatar
Sylvain Henry committed
513
     (defined in GHC.Core.TyCon)
Simon Peyton Jones's avatar
Simon Peyton Jones committed
514 515 516 517 518 519 520 521 522 523

We need to get from one to the other; that is what kindPrimRep does.
Suppose we have a value
   (v :: t) where (t :: k)
Given this kind
    k = TyConApp "TYPE" [rep]
GHC needs to be able to figure out how 'v' is represented at runtime.
It expects 'rep' to be form
    TyConApp rr_dc args
where 'rr_dc' is a promoteed data constructor from RuntimeRep. So
524
now we need to go from 'dc' to the corresponding PrimRep.  We store this
Simon Peyton Jones's avatar
Simon Peyton Jones committed
525
PrimRep in the promoted data constructor itself: see TyCon.promDcRepInfo.
526

Austin Seipp's avatar
Austin Seipp committed
527
-}
528

Richard Eisenberg's avatar
Richard Eisenberg committed
529 530
tYPETyCon :: TyCon
tYPETyConName :: Name
531 532

tYPETyCon = mkKindTyCon tYPETyConName
533
                        (mkTemplateAnonTyConBinders [runtimeRepTy])
534
                        liftedTypeKind
535
                        [Nominal]
536
                        (mkPrelTyConRepName tYPETyConName)
537

538 539 540
--------------------------
-- ... and now their names

541
-- If you edit these, you may need to update the GHC formalism
Sylvain Henry's avatar
Sylvain Henry committed
542
-- See Note [GHC Formalism] in GHC.Core.Lint
543 544
tYPETyConName             = mkPrimTyConName (fsLit "TYPE") tYPETyConKey tYPETyCon

545
mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
546 547 548 549 550 551
mkPrimTyConName = mkPrimTcName BuiltInSyntax
  -- All of the super kinds and kinds are defined in Prim,
  -- and use BuiltInSyntax, because they are never in scope in the source

mkPrimTcName :: BuiltInSyntax -> FastString -> Unique -> TyCon -> Name
mkPrimTcName built_in_syntax occ key tycon
Sylvain Henry's avatar
Sylvain Henry committed
552
  = mkWiredInName gHC_PRIM (mkTcOccFS occ) key (mkATyCon tycon) built_in_syntax
553

554
-----------------------------
555

Krzysztof Gogolewski's avatar
Krzysztof Gogolewski committed
556 557 558 559
-- Given a Multiplicity, applies FUN to it.
functionWithMultiplicity :: Type -> Type
functionWithMultiplicity mul = TyConApp funTyCon [mul]

Austin Seipp's avatar
Austin Seipp committed
560 561 562
{-
************************************************************************
*                                                                      *
Sylvain Henry's avatar
Sylvain Henry committed
563
   Basic primitive types (@Char#@, @Int#@, etc.)
Austin Seipp's avatar
Austin Seipp committed
564 565 566
*                                                                      *
************************************************************************
-}
567

568
-- only used herein
569 570
pcPrimTyCon :: Name -> [Role] -> PrimRep -> TyCon
pcPrimTyCon name roles rep
571
  = mkPrimTyCon name binders result_kind roles
572
  where
573
    binders     = mkTemplateAnonTyConBinders (map (const liftedTypeKind) roles)
Richard Eisenberg's avatar
Richard Eisenberg committed
574 575 576 577 578 579 580
    result_kind = tYPE (primRepToRuntimeRep rep)

-- | Convert a 'PrimRep' to a 'Type' of kind RuntimeRep
-- Defined here to avoid (more) module loops
primRepToRuntimeRep :: PrimRep -> Type
primRepToRuntimeRep rep = case rep of
  VoidRep       -> TyConApp tupleRepDataConTyCon [mkPromotedListTy runtimeRepTy []]
581 582
  LiftedRep     -> liftedRepDataConTy
  UnliftedRep   -> unliftedRepDataConTy
Richard Eisenberg's avatar
Richard Eisenberg committed
583
  IntRep        -> intRepDataConTy
Michal Terepeta's avatar
Michal Terepeta committed
584
  Int8Rep       -> int8RepDataConTy
Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
585
  Int16Rep      -> int16RepDataConTy
John Ericson's avatar
John Ericson committed
586
  Int32Rep      -> int32RepDataConTy
Richard Eisenberg's avatar
Richard Eisenberg committed
587
  Int64Rep      -> int64RepDataConTy
John Ericson's avatar
John Ericson committed
588
  WordRep       -> wordRepDataConTy
Michal Terepeta's avatar
Michal Terepeta committed
589
  Word8Rep      -> word8RepDataConTy
Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
590
  Word16Rep     -> word16RepDataConTy
John Ericson's avatar
John Ericson committed
591
  Word32Rep     -> word32RepDataConTy
Richard Eisenberg's avatar
Richard Eisenberg committed
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
  Word64Rep     -> word64RepDataConTy
  AddrRep       -> addrRepDataConTy
  FloatRep      -> floatRepDataConTy
  DoubleRep     -> doubleRepDataConTy
  VecRep n elem -> TyConApp vecRepDataConTyCon [n', elem']
    where
      n' = case n of
        2  -> vec2DataConTy
        4  -> vec4DataConTy
        8  -> vec8DataConTy
        16 -> vec16DataConTy
        32 -> vec32DataConTy
        64 -> vec64DataConTy
        _  -> pprPanic "Disallowed VecCount" (ppr n)

      elem' = case elem of
        Int8ElemRep   -> int8ElemRepDataConTy
        Int16ElemRep  -> int16ElemRepDataConTy
        Int32ElemRep  -> int32ElemRepDataConTy
        Int64ElemRep  -> int64ElemRepDataConTy
        Word8ElemRep  -> word8ElemRepDataConTy
        Word16ElemRep -> word16ElemRepDataConTy
        Word32ElemRep -> word32ElemRepDataConTy
        Word64ElemRep -> word64ElemRepDataConTy
        FloatElemRep  -> floatElemRepDataConTy
        DoubleElemRep -> doubleElemRepDataConTy
618

619 620
pcPrimTyCon0 :: Name -> PrimRep -> TyCon
pcPrimTyCon0 name rep
621
  = pcPrimTyCon name [] rep
622

Ian Lynagh's avatar
Ian Lynagh committed
623
charPrimTy :: Type
624
charPrimTy      = mkTyConTy charPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
625
charPrimTyCon :: TyCon
626
charPrimTyCon   = pcPrimTyCon0 charPrimTyConName WordRep
627

Ian Lynagh's avatar
Ian Lynagh committed
628
intPrimTy :: Type
629
intPrimTy       = mkTyConTy intPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
630
intPrimTyCon :: TyCon
631
intPrimTyCon    = pcPrimTyCon0 intPrimTyConName IntRep
632

Michal Terepeta's avatar
Michal Terepeta committed
633 634 635 636 637
int8PrimTy :: Type
int8PrimTy     = mkTyConTy int8PrimTyCon
int8PrimTyCon :: TyCon
int8PrimTyCon  = pcPrimTyCon0 int8PrimTyConName Int8Rep

Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
638 639 640 641 642
int16PrimTy :: Type
int16PrimTy    = mkTyConTy int16PrimTyCon
int16PrimTyCon :: TyCon
int16PrimTyCon = pcPrimTyCon0 int16PrimTyConName Int16Rep

Ian Lynagh's avatar
Ian Lynagh committed
643
int32PrimTy :: Type
644
int32PrimTy     = mkTyConTy int32PrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
645
int32PrimTyCon :: TyCon
John Ericson's avatar
John Ericson committed
646
int32PrimTyCon  = pcPrimTyCon0 int32PrimTyConName Int32Rep
apt's avatar
apt committed
647

Ian Lynagh's avatar
Ian Lynagh committed
648
int64PrimTy :: Type
649
int64PrimTy     = mkTyConTy int64PrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
650
int64PrimTyCon :: TyCon
651
int64PrimTyCon  = pcPrimTyCon0 int64PrimTyConName Int64Rep
sof's avatar
sof committed
652

Ian Lynagh's avatar
Ian Lynagh committed
653
wordPrimTy :: Type
654
wordPrimTy      = mkTyConTy wordPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
655
wordPrimTyCon :: TyCon
656
wordPrimTyCon   = pcPrimTyCon0 wordPrimTyConName WordRep
657

Michal Terepeta's avatar
Michal Terepeta committed
658 659 660 661 662
word8PrimTy :: Type
word8PrimTy     = mkTyConTy word8PrimTyCon
word8PrimTyCon :: TyCon
word8PrimTyCon  = pcPrimTyCon0 word8PrimTyConName Word8Rep

Abhiroop Sarkar's avatar
Abhiroop Sarkar committed
663 664 665 666 667
word16PrimTy :: Type
word16PrimTy    = mkTyConTy word16PrimTyCon
word16PrimTyCon :: TyCon
word16PrimTyCon = pcPrimTyCon0 word16PrimTyConName Word16Rep

Ian Lynagh's avatar
Ian Lynagh committed
668
word32PrimTy :: Type
669
word32PrimTy    = mkTyConTy word32PrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
670
word32PrimTyCon :: TyCon
John Ericson's avatar
John Ericson committed
671
word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName Word32Rep
apt's avatar
apt committed
672

Ian Lynagh's avatar
Ian Lynagh committed
673
word64PrimTy :: Type
674
word64PrimTy    = mkTyConTy word64PrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
675
word64PrimTyCon :: TyCon
676
word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
sof's avatar
sof committed
677

Ian Lynagh's avatar
Ian Lynagh committed
678
addrPrimTy :: Type
679
addrPrimTy      = mkTyConTy addrPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
680
addrPrimTyCon :: TyCon
681
addrPrimTyCon   = pcPrimTyCon0 addrPrimTyConName AddrRep
682

683 684
floatPrimTy     :: Type
floatPrimTy     = mkTyConTy floatPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
685
floatPrimTyCon :: TyCon
686
floatPrimTyCon  = pcPrimTyCon0 floatPrimTyConName FloatRep
687

Ian Lynagh's avatar
Ian Lynagh committed
688
doublePrimTy :: Type
689 690 691
doublePrimTy    = mkTyConTy doublePrimTyCon
doublePrimTyCon :: TyCon
doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
692

Austin Seipp's avatar
Austin Seipp committed
693 694 695
{-
************************************************************************
*                                                                      *
Sylvain Henry's avatar
Sylvain Henry committed
696
   The @State#@ type (and @_RealWorld@ types)
Austin Seipp's avatar
Austin Seipp committed
697 698
*                                                                      *
************************************************************************
699

700 701 702 703
Note [The equality types story]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GHC sports a veritable menagerie of equality types:

704 705
         Type or  Lifted?  Hetero?  Role      Built in         Defining module
         class?    L/U                        TyCon
706
-----------------------------------------------------------------------------------------
707
~#         T        U      hetero   nominal   eqPrimTyCon      GHC.Prim
Ningning Xie's avatar
Ningning Xie committed
708
~~         C        L      hetero   nominal   heqTyCon         GHC.Types
709
~          C        L      homo     nominal   eqTyCon          GHC.Types
710 711
:~:        T        L      homo     nominal   (not built-in)   Data.Type.Equality
:~~:       T        L      hetero   nominal   (not built-in)   Data.Type.Equality
712

713 714 715 716
~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
717 718 719 720 721 722 723 724 725 726 727 728

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:

Simon Peyton Jones's avatar
Simon Peyton Jones committed
729 730 731
    --------------------------
    (~#) :: forall k1 k2. k1 -> k2 -> #
    --------------------------
732 733
This is The Type Of Equality in GHC. It classifies nominal coercions.
This type is used in the solver for recording equality constraints.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
734
It responds "yes" to Type.isEqPrimPred and classifies as an EqPred in
735 736 737
Type.classifyPredType.

All wanted constraints of this type are built with coercion holes.
Sylvain Henry's avatar
Sylvain Henry committed
738
(See Note [Coercion holes] in GHC.Core.TyCo.Rep.) But see also
Sylvain Henry's avatar
Sylvain Henry committed
739
Note [Deferred errors for coercion holes] in GHC.Tc.Errors to see how
740 741
equality constraints are deferred.

Sylvain Henry's avatar
Sylvain Henry committed
742
Within GHC, ~# is called eqPrimTyCon, and it is defined in GHC.Builtin.Types.Prim.
743 744


Simon Peyton Jones's avatar
Simon Peyton Jones committed
745 746 747
    --------------------------
    (~~) :: forall k1 k2. k1 -> k2 -> Constraint
    --------------------------
748 749 750 751
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:
752

753 754 755 756 757 758 759 760 761
 * 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
762
   influences what happens next.) See Note [Naturally coherent classes]
Sylvain Henry's avatar
Sylvain Henry committed
763
   in GHC.Tc.Solver.Interact.
764 765 766 767 768 769 770 771

 * 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
Sylvain Henry's avatar
Sylvain Henry committed
772
equalities. There is some special-casing in GHC.Tc.Solver.Interact.matchClassInst to
773 774 775
pretend that there is an instance of this class, as we can't write the instance
in Haskell.

Sylvain Henry's avatar
Sylvain Henry committed
776
Within GHC, ~~ is called heqTyCon, and it is defined in GHC.Builtin.Types.
777 778


Simon Peyton Jones's avatar
Simon Peyton Jones committed
779 780 781
    --------------------------
    (~) :: forall k. k -> k -> Constraint
    --------------------------
782 783 784 785
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
786

787
 * All the bullets for (~~) apply
788

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

Sylvain Henry's avatar
Sylvain Henry committed
792
Within GHC, ~ is called eqTyCon, and it is defined in GHC.Builtin.Types.
793

794 795 796 797 798
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.)
799 800


Simon Peyton Jones's avatar
Simon Peyton Jones committed
801 802
    --------------------------
    (:~:) :: forall k. k -> k -> *
803
    (:~~:) :: forall k1 k2. k1 -> k2 -> *
Simon Peyton Jones's avatar
Simon Peyton Jones committed
804
    --------------------------
805 806
These are perfectly ordinary GADTs, wrapping (~) and (~~) resp.
They are not defined within GHC at all.
807 808


Simon Peyton Jones's avatar
Simon Peyton Jones committed
809 810 811
    --------------------------
    (~R#) :: forall k1 k2. k1 -> k2 -> #
    --------------------------
812 813 814 815