Coercion Quantification
[ghc.git] / compiler / prelude / TysPrim.hs
1 {-
2 (c) The AQUA Project, Glasgow University, 1994-1998
3
4
5 \section[TysPrim]{Wired-in knowledge about primitive types}
6 -}
7
8 {-# LANGUAGE CPP #-}
9
10 -- | This module defines TyCons that can't be expressed in Haskell.
11 -- They are all, therefore, wired-in TyCons. C.f module TysWiredIn
12 module TysPrim(
13 mkPrimTyConName, -- For implicit parameters in TysWiredIn only
14
15 mkTemplateKindVars, mkTemplateTyVars, mkTemplateTyVarsFrom,
16 mkTemplateKiTyVars,
17
18 mkTemplateTyConBinders, mkTemplateKindTyConBinders,
19 mkTemplateAnonTyConBinders,
20
21 alphaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
22 alphaTys, alphaTy, betaTy, gammaTy, deltaTy,
23 runtimeRep1TyVar, runtimeRep2TyVar, runtimeRep1Ty, runtimeRep2Ty,
24 openAlphaTy, openBetaTy, openAlphaTyVar, openBetaTyVar,
25
26 -- Kind constructors...
27 tYPETyCon, tYPETyConName,
28
29 -- Kinds
30 tYPE, primRepToRuntimeRep,
31
32 funTyCon, funTyConName,
33 unexposedPrimTyCons, exposedPrimTyCons, primTyCons,
34
35 charPrimTyCon, charPrimTy, charPrimTyConName,
36 intPrimTyCon, intPrimTy, intPrimTyConName,
37 wordPrimTyCon, wordPrimTy, wordPrimTyConName,
38 addrPrimTyCon, addrPrimTy, addrPrimTyConName,
39 floatPrimTyCon, floatPrimTy, floatPrimTyConName,
40 doublePrimTyCon, doublePrimTy, doublePrimTyConName,
41
42 voidPrimTyCon, voidPrimTy,
43 statePrimTyCon, mkStatePrimTy,
44 realWorldTyCon, realWorldTy, realWorldStatePrimTy,
45
46 proxyPrimTyCon, mkProxyPrimTy,
47
48 arrayPrimTyCon, mkArrayPrimTy,
49 byteArrayPrimTyCon, byteArrayPrimTy,
50 arrayArrayPrimTyCon, mkArrayArrayPrimTy,
51 smallArrayPrimTyCon, mkSmallArrayPrimTy,
52 mutableArrayPrimTyCon, mkMutableArrayPrimTy,
53 mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
54 mutableArrayArrayPrimTyCon, mkMutableArrayArrayPrimTy,
55 smallMutableArrayPrimTyCon, mkSmallMutableArrayPrimTy,
56 mutVarPrimTyCon, mkMutVarPrimTy,
57
58 mVarPrimTyCon, mkMVarPrimTy,
59 tVarPrimTyCon, mkTVarPrimTy,
60 stablePtrPrimTyCon, mkStablePtrPrimTy,
61 stableNamePrimTyCon, mkStableNamePrimTy,
62 compactPrimTyCon, compactPrimTy,
63 bcoPrimTyCon, bcoPrimTy,
64 weakPrimTyCon, mkWeakPrimTy,
65 threadIdPrimTyCon, threadIdPrimTy,
66
67 int32PrimTyCon, int32PrimTy,
68 word32PrimTyCon, word32PrimTy,
69
70 int64PrimTyCon, int64PrimTy,
71 word64PrimTyCon, word64PrimTy,
72
73 eqPrimTyCon, -- ty1 ~# ty2
74 eqReprPrimTyCon, -- ty1 ~R# ty2 (at role Representational)
75 eqPhantPrimTyCon, -- ty1 ~P# ty2 (at role Phantom)
76
77 -- * SIMD
78 #include "primop-vector-tys-exports.hs-incl"
79 ) where
80
81 #include "HsVersions.h"
82
83 import GhcPrelude
84
85 import {-# SOURCE #-} TysWiredIn
86 ( runtimeRepTy, unboxedTupleKind, liftedTypeKind
87 , vecRepDataConTyCon, tupleRepDataConTyCon
88 , liftedRepDataConTy, unliftedRepDataConTy, intRepDataConTy
89 , wordRepDataConTy, int64RepDataConTy, word64RepDataConTy, addrRepDataConTy
90 , floatRepDataConTy, doubleRepDataConTy
91 , vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy
92 , vec64DataConTy
93 , int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy
94 , int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy
95 , word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy
96 , doubleElemRepDataConTy
97 , mkPromotedListTy )
98
99 import Var ( TyVar, VarBndr(Bndr), mkTyVar )
100 import Name
101 import TyCon
102 import SrcLoc
103 import Unique
104 import PrelNames
105 import FastString
106 import Outputable
107 import TyCoRep -- Doesn't need special access, but this is easier to avoid
108 -- import loops which show up if you import Type instead
109
110 import Data.Char
111
112 {-
113 ************************************************************************
114 * *
115 \subsection{Primitive type constructors}
116 * *
117 ************************************************************************
118 -}
119
120 primTyCons :: [TyCon]
121 primTyCons = unexposedPrimTyCons ++ exposedPrimTyCons
122
123 -- | Primitive 'TyCon's that are defined in "GHC.Prim" but not exposed.
124 -- It's important to keep these separate as we don't want users to be able to
125 -- write them (see Trac #15209) or see them in GHCi's @:browse@ output
126 -- (see Trac #12023).
127 unexposedPrimTyCons :: [TyCon]
128 unexposedPrimTyCons
129 = [ eqPrimTyCon
130 , eqReprPrimTyCon
131 , eqPhantPrimTyCon
132 ]
133
134 -- | Primitive 'TyCon's that are defined in, and exported from, "GHC.Prim".
135 exposedPrimTyCons :: [TyCon]
136 exposedPrimTyCons
137 = [ addrPrimTyCon
138 , arrayPrimTyCon
139 , byteArrayPrimTyCon
140 , arrayArrayPrimTyCon
141 , smallArrayPrimTyCon
142 , charPrimTyCon
143 , doublePrimTyCon
144 , floatPrimTyCon
145 , intPrimTyCon
146 , int32PrimTyCon
147 , int64PrimTyCon
148 , bcoPrimTyCon
149 , weakPrimTyCon
150 , mutableArrayPrimTyCon
151 , mutableByteArrayPrimTyCon
152 , mutableArrayArrayPrimTyCon
153 , smallMutableArrayPrimTyCon
154 , mVarPrimTyCon
155 , tVarPrimTyCon
156 , mutVarPrimTyCon
157 , realWorldTyCon
158 , stablePtrPrimTyCon
159 , stableNamePrimTyCon
160 , compactPrimTyCon
161 , statePrimTyCon
162 , voidPrimTyCon
163 , proxyPrimTyCon
164 , threadIdPrimTyCon
165 , wordPrimTyCon
166 , word32PrimTyCon
167 , word64PrimTyCon
168
169 , tYPETyCon
170
171 #include "primop-vector-tycons.hs-incl"
172 ]
173
174 mkPrimTc :: FastString -> Unique -> TyCon -> Name
175 mkPrimTc fs unique tycon
176 = mkWiredInName gHC_PRIM (mkTcOccFS fs)
177 unique
178 (ATyCon tycon) -- Relevant TyCon
179 UserSyntax
180
181 mkBuiltInPrimTc :: FastString -> Unique -> TyCon -> Name
182 mkBuiltInPrimTc fs unique tycon
183 = mkWiredInName gHC_PRIM (mkTcOccFS fs)
184 unique
185 (ATyCon tycon) -- Relevant TyCon
186 BuiltInSyntax
187
188
189 charPrimTyConName, intPrimTyConName, int32PrimTyConName, int64PrimTyConName, wordPrimTyConName, word32PrimTyConName, word64PrimTyConName, addrPrimTyConName, floatPrimTyConName, doublePrimTyConName, statePrimTyConName, proxyPrimTyConName, realWorldTyConName, arrayPrimTyConName, arrayArrayPrimTyConName, smallArrayPrimTyConName, byteArrayPrimTyConName, mutableArrayPrimTyConName, mutableByteArrayPrimTyConName, mutableArrayArrayPrimTyConName, smallMutableArrayPrimTyConName, mutVarPrimTyConName, mVarPrimTyConName, tVarPrimTyConName, stablePtrPrimTyConName, stableNamePrimTyConName, compactPrimTyConName, bcoPrimTyConName, weakPrimTyConName, threadIdPrimTyConName, eqPrimTyConName, eqReprPrimTyConName, eqPhantPrimTyConName, voidPrimTyConName :: Name
190 charPrimTyConName = mkPrimTc (fsLit "Char#") charPrimTyConKey charPrimTyCon
191 intPrimTyConName = mkPrimTc (fsLit "Int#") intPrimTyConKey intPrimTyCon
192 int32PrimTyConName = mkPrimTc (fsLit "Int32#") int32PrimTyConKey int32PrimTyCon
193 int64PrimTyConName = mkPrimTc (fsLit "Int64#") int64PrimTyConKey int64PrimTyCon
194 wordPrimTyConName = mkPrimTc (fsLit "Word#") wordPrimTyConKey wordPrimTyCon
195 word32PrimTyConName = mkPrimTc (fsLit "Word32#") word32PrimTyConKey word32PrimTyCon
196 word64PrimTyConName = mkPrimTc (fsLit "Word64#") word64PrimTyConKey word64PrimTyCon
197 addrPrimTyConName = mkPrimTc (fsLit "Addr#") addrPrimTyConKey addrPrimTyCon
198 floatPrimTyConName = mkPrimTc (fsLit "Float#") floatPrimTyConKey floatPrimTyCon
199 doublePrimTyConName = mkPrimTc (fsLit "Double#") doublePrimTyConKey doublePrimTyCon
200 statePrimTyConName = mkPrimTc (fsLit "State#") statePrimTyConKey statePrimTyCon
201 voidPrimTyConName = mkPrimTc (fsLit "Void#") voidPrimTyConKey voidPrimTyCon
202 proxyPrimTyConName = mkPrimTc (fsLit "Proxy#") proxyPrimTyConKey proxyPrimTyCon
203 eqPrimTyConName = mkPrimTc (fsLit "~#") eqPrimTyConKey eqPrimTyCon
204 eqReprPrimTyConName = mkBuiltInPrimTc (fsLit "~R#") eqReprPrimTyConKey eqReprPrimTyCon
205 eqPhantPrimTyConName = mkBuiltInPrimTc (fsLit "~P#") eqPhantPrimTyConKey eqPhantPrimTyCon
206 realWorldTyConName = mkPrimTc (fsLit "RealWorld") realWorldTyConKey realWorldTyCon
207 arrayPrimTyConName = mkPrimTc (fsLit "Array#") arrayPrimTyConKey arrayPrimTyCon
208 byteArrayPrimTyConName = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey byteArrayPrimTyCon
209 arrayArrayPrimTyConName = mkPrimTc (fsLit "ArrayArray#") arrayArrayPrimTyConKey arrayArrayPrimTyCon
210 smallArrayPrimTyConName = mkPrimTc (fsLit "SmallArray#") smallArrayPrimTyConKey smallArrayPrimTyCon
211 mutableArrayPrimTyConName = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey mutableArrayPrimTyCon
212 mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey mutableByteArrayPrimTyCon
213 mutableArrayArrayPrimTyConName= mkPrimTc (fsLit "MutableArrayArray#") mutableArrayArrayPrimTyConKey mutableArrayArrayPrimTyCon
214 smallMutableArrayPrimTyConName= mkPrimTc (fsLit "SmallMutableArray#") smallMutableArrayPrimTyConKey smallMutableArrayPrimTyCon
215 mutVarPrimTyConName = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey mutVarPrimTyCon
216 mVarPrimTyConName = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey mVarPrimTyCon
217 tVarPrimTyConName = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey tVarPrimTyCon
218 stablePtrPrimTyConName = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey stablePtrPrimTyCon
219 stableNamePrimTyConName = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey stableNamePrimTyCon
220 compactPrimTyConName = mkPrimTc (fsLit "Compact#") compactPrimTyConKey compactPrimTyCon
221 bcoPrimTyConName = mkPrimTc (fsLit "BCO#") bcoPrimTyConKey bcoPrimTyCon
222 weakPrimTyConName = mkPrimTc (fsLit "Weak#") weakPrimTyConKey weakPrimTyCon
223 threadIdPrimTyConName = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey threadIdPrimTyCon
224
225 {-
226 ************************************************************************
227 * *
228 \subsection{Support code}
229 * *
230 ************************************************************************
231
232 alphaTyVars is a list of type variables for use in templates:
233 ["a", "b", ..., "z", "t1", "t2", ... ]
234 -}
235
236 mkTemplateKindVars :: [Kind] -> [TyVar]
237 -- k0 with unique (mkAlphaTyVarUnique 0)
238 -- k1 with unique (mkAlphaTyVarUnique 1)
239 -- ... etc
240 mkTemplateKindVars kinds
241 = [ mkTyVar name kind
242 | (kind, u) <- kinds `zip` [0..]
243 , let occ = mkTyVarOccFS (mkFastString ('k' : show u))
244 name = mkInternalName (mkAlphaTyVarUnique u) occ noSrcSpan
245 ]
246
247 mkTemplateTyVarsFrom :: Int -> [Kind] -> [TyVar]
248 -- a with unique (mkAlphaTyVarUnique n)
249 -- b with unique (mkAlphaTyVarUnique n+1)
250 -- ... etc
251 -- Typically called as
252 -- mkTemplateTyVarsFrom (length kv_bndrs) kinds
253 -- where kv_bndrs are the kind-level binders of a TyCon
254 mkTemplateTyVarsFrom n kinds
255 = [ mkTyVar name kind
256 | (kind, index) <- zip kinds [0..],
257 let ch_ord = index + ord 'a'
258 name_str | ch_ord <= ord 'z' = [chr ch_ord]
259 | otherwise = 't':show index
260 uniq = mkAlphaTyVarUnique (index + n)
261 name = mkInternalName uniq occ noSrcSpan
262 occ = mkTyVarOccFS (mkFastString name_str)
263 ]
264
265 mkTemplateTyVars :: [Kind] -> [TyVar]
266 mkTemplateTyVars = mkTemplateTyVarsFrom 1
267
268 mkTemplateTyConBinders
269 :: [Kind] -- [k1, .., kn] Kinds of kind-forall'd vars
270 -> ([Kind] -> [Kind]) -- Arg is [kv1:k1, ..., kvn:kn]
271 -- same length as first arg
272 -- Result is anon arg kinds
273 -> [TyConBinder]
274 mkTemplateTyConBinders kind_var_kinds mk_anon_arg_kinds
275 = kv_bndrs ++ tv_bndrs
276 where
277 kv_bndrs = mkTemplateKindTyConBinders kind_var_kinds
278 anon_kinds = mk_anon_arg_kinds (mkTyVarTys (binderVars kv_bndrs))
279 tv_bndrs = mkTemplateAnonTyConBindersFrom (length kv_bndrs) anon_kinds
280
281 mkTemplateKiTyVars
282 :: [Kind] -- [k1, .., kn] Kinds of kind-forall'd vars
283 -> ([Kind] -> [Kind]) -- Arg is [kv1:k1, ..., kvn:kn]
284 -- same length as first arg
285 -- Result is anon arg kinds [ak1, .., akm]
286 -> [TyVar] -- [kv1:k1, ..., kvn:kn, av1:ak1, ..., avm:akm]
287 -- Example: if you want the tyvars for
288 -- forall (r:RuntimeRep) (a:TYPE r) (b:*). blah
289 -- call mkTemplateKiTyVars [RuntimeRep] (\[r]. [TYPE r, *)
290 mkTemplateKiTyVars kind_var_kinds mk_arg_kinds
291 = kv_bndrs ++ tv_bndrs
292 where
293 kv_bndrs = mkTemplateKindVars kind_var_kinds
294 anon_kinds = mk_arg_kinds (mkTyVarTys kv_bndrs)
295 tv_bndrs = mkTemplateTyVarsFrom (length kv_bndrs) anon_kinds
296
297 mkTemplateKindTyConBinders :: [Kind] -> [TyConBinder]
298 -- Makes named, Specified binders
299 mkTemplateKindTyConBinders kinds = [mkNamedTyConBinder Specified tv | tv <- mkTemplateKindVars kinds]
300
301 mkTemplateAnonTyConBinders :: [Kind] -> [TyConBinder]
302 mkTemplateAnonTyConBinders kinds = map mkAnonTyConBinder (mkTemplateTyVars kinds)
303
304 mkTemplateAnonTyConBindersFrom :: Int -> [Kind] -> [TyConBinder]
305 mkTemplateAnonTyConBindersFrom n kinds = map mkAnonTyConBinder (mkTemplateTyVarsFrom n kinds)
306
307 alphaTyVars :: [TyVar]
308 alphaTyVars = mkTemplateTyVars $ repeat liftedTypeKind
309
310 alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: TyVar
311 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
312
313 alphaTys :: [Type]
314 alphaTys = mkTyVarTys alphaTyVars
315 alphaTy, betaTy, gammaTy, deltaTy :: Type
316 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
317
318 runtimeRep1TyVar, runtimeRep2TyVar :: TyVar
319 (runtimeRep1TyVar : runtimeRep2TyVar : _)
320 = drop 16 (mkTemplateTyVars (repeat runtimeRepTy)) -- selects 'q','r'
321
322 runtimeRep1Ty, runtimeRep2Ty :: Type
323 runtimeRep1Ty = mkTyVarTy runtimeRep1TyVar
324 runtimeRep2Ty = mkTyVarTy runtimeRep2TyVar
325
326 openAlphaTyVar, openBetaTyVar :: TyVar
327 [openAlphaTyVar,openBetaTyVar]
328 = mkTemplateTyVars [tYPE runtimeRep1Ty, tYPE runtimeRep2Ty]
329
330 openAlphaTy, openBetaTy :: Type
331 openAlphaTy = mkTyVarTy openAlphaTyVar
332 openBetaTy = mkTyVarTy openBetaTyVar
333
334 {-
335 ************************************************************************
336 * *
337 FunTyCon
338 * *
339 ************************************************************************
340 -}
341
342 funTyConName :: Name
343 funTyConName = mkPrimTyConName (fsLit "->") funTyConKey funTyCon
344
345 -- | The @(->)@ type constructor.
346 --
347 -- @
348 -- (->) :: forall (rep1 :: RuntimeRep) (rep2 :: RuntimeRep).
349 -- TYPE rep1 -> TYPE rep2 -> *
350 -- @
351 funTyCon :: TyCon
352 funTyCon = mkFunTyCon funTyConName tc_bndrs tc_rep_nm
353 where
354 tc_bndrs = [ Bndr runtimeRep1TyVar (NamedTCB Inferred)
355 , Bndr runtimeRep2TyVar (NamedTCB Inferred)
356 ]
357 ++ mkTemplateAnonTyConBinders [ tYPE runtimeRep1Ty
358 , tYPE runtimeRep2Ty
359 ]
360 tc_rep_nm = mkPrelTyConRepName funTyConName
361
362 {-
363 ************************************************************************
364 * *
365 Kinds
366 * *
367 ************************************************************************
368
369 Note [TYPE and RuntimeRep]
370 ~~~~~~~~~~~~~~~~~~~~~~~~~~
371 All types that classify values have a kind of the form (TYPE rr), where
372
373 data RuntimeRep -- Defined in ghc-prim:GHC.Types
374 = LiftedRep
375 | UnliftedRep
376 | IntRep
377 | FloatRep
378 .. etc ..
379
380 rr :: RuntimeRep
381
382 TYPE :: RuntimeRep -> TYPE 'LiftedRep -- Built in
383
384 So for example:
385 Int :: TYPE 'LiftedRep
386 Array# Int :: TYPE 'UnliftedRep
387 Int# :: TYPE 'IntRep
388 Float# :: TYPE 'FloatRep
389 Maybe :: TYPE 'LiftedRep -> TYPE 'LiftedRep
390 (# , #) :: TYPE r1 -> TYPE r2 -> TYPE (TupleRep [r1, r2])
391
392 We abbreviate '*' specially:
393 type * = TYPE 'LiftedRep
394
395 The 'rr' parameter tells us how the value is represented at runime.
396
397 Generally speaking, you can't be polymorphic in 'rr'. E.g
398 f :: forall (rr:RuntimeRep) (a:TYPE rr). a -> [a]
399 f = /\(rr:RuntimeRep) (a:rr) \(a:rr). ...
400 This is no good: we could not generate code code for 'f', because the
401 calling convention for 'f' varies depending on whether the argument is
402 a a Int, Int#, or Float#. (You could imagine generating specialised
403 code, one for each instantiation of 'rr', but we don't do that.)
404
405 Certain functions CAN be runtime-rep-polymorphic, because the code
406 generator never has to manipulate a value of type 'a :: TYPE rr'.
407
408 * error :: forall (rr:RuntimeRep) (a:TYPE rr). String -> a
409 Code generator never has to manipulate the return value.
410
411 * unsafeCoerce#, defined in MkId.unsafeCoerceId:
412 Always inlined to be a no-op
413 unsafeCoerce# :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
414 (a :: TYPE r1) (b :: TYPE r2).
415 a -> b
416
417 * Unboxed tuples, and unboxed sums, defined in TysWiredIn
418 Always inlined, and hence specialised to the call site
419 (#,#) :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
420 (a :: TYPE r1) (b :: TYPE r2).
421 a -> b -> TYPE ('TupleRep '[r1, r2])
422
423 Note [PrimRep and kindPrimRep]
424 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
425 As part of its source code, in TyCon, GHC has
426 data PrimRep = LiftedRep | UnliftedRep | IntRep | FloatRep | ...etc...
427
428 Notice that
429 * RuntimeRep is part of the syntax tree of the program being compiled
430 (defined in a library: ghc-prim:GHC.Types)
431 * PrimRep is part of GHC's source code.
432 (defined in TyCon)
433
434 We need to get from one to the other; that is what kindPrimRep does.
435 Suppose we have a value
436 (v :: t) where (t :: k)
437 Given this kind
438 k = TyConApp "TYPE" [rep]
439 GHC needs to be able to figure out how 'v' is represented at runtime.
440 It expects 'rep' to be form
441 TyConApp rr_dc args
442 where 'rr_dc' is a promoteed data constructor from RuntimeRep. So
443 now we need to go from 'dc' to the corresponding PrimRep. We store this
444 PrimRep in the promoted data constructor itself: see TyCon.promDcRepInfo.
445
446 -}
447
448 tYPETyCon :: TyCon
449 tYPETyConName :: Name
450
451 tYPETyCon = mkKindTyCon tYPETyConName
452 (mkTemplateAnonTyConBinders [runtimeRepTy])
453 liftedTypeKind
454 [Nominal]
455 (mkPrelTyConRepName tYPETyConName)
456
457 --------------------------
458 -- ... and now their names
459
460 -- If you edit these, you may need to update the GHC formalism
461 -- See Note [GHC Formalism] in coreSyn/CoreLint.hs
462 tYPETyConName = mkPrimTyConName (fsLit "TYPE") tYPETyConKey tYPETyCon
463
464 mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
465 mkPrimTyConName = mkPrimTcName BuiltInSyntax
466 -- All of the super kinds and kinds are defined in Prim,
467 -- and use BuiltInSyntax, because they are never in scope in the source
468
469 mkPrimTcName :: BuiltInSyntax -> FastString -> Unique -> TyCon -> Name
470 mkPrimTcName built_in_syntax occ key tycon
471 = mkWiredInName gHC_PRIM (mkTcOccFS occ) key (ATyCon tycon) built_in_syntax
472
473 -----------------------------
474 -- | Given a RuntimeRep, applies TYPE to it.
475 -- see Note [TYPE and RuntimeRep]
476 tYPE :: Type -> Type
477 tYPE rr = TyConApp tYPETyCon [rr]
478
479 {-
480 ************************************************************************
481 * *
482 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
483 * *
484 ************************************************************************
485 -}
486
487 -- only used herein
488 pcPrimTyCon :: Name -> [Role] -> PrimRep -> TyCon
489 pcPrimTyCon name roles rep
490 = mkPrimTyCon name binders result_kind roles
491 where
492 binders = mkTemplateAnonTyConBinders (map (const liftedTypeKind) roles)
493 result_kind = tYPE (primRepToRuntimeRep rep)
494
495 -- | Convert a 'PrimRep' to a 'Type' of kind RuntimeRep
496 -- Defined here to avoid (more) module loops
497 primRepToRuntimeRep :: PrimRep -> Type
498 primRepToRuntimeRep rep = case rep of
499 VoidRep -> TyConApp tupleRepDataConTyCon [mkPromotedListTy runtimeRepTy []]
500 LiftedRep -> liftedRepDataConTy
501 UnliftedRep -> unliftedRepDataConTy
502 IntRep -> intRepDataConTy
503 WordRep -> wordRepDataConTy
504 Int64Rep -> int64RepDataConTy
505 Word64Rep -> word64RepDataConTy
506 AddrRep -> addrRepDataConTy
507 FloatRep -> floatRepDataConTy
508 DoubleRep -> doubleRepDataConTy
509 VecRep n elem -> TyConApp vecRepDataConTyCon [n', elem']
510 where
511 n' = case n of
512 2 -> vec2DataConTy
513 4 -> vec4DataConTy
514 8 -> vec8DataConTy
515 16 -> vec16DataConTy
516 32 -> vec32DataConTy
517 64 -> vec64DataConTy
518 _ -> pprPanic "Disallowed VecCount" (ppr n)
519
520 elem' = case elem of
521 Int8ElemRep -> int8ElemRepDataConTy
522 Int16ElemRep -> int16ElemRepDataConTy
523 Int32ElemRep -> int32ElemRepDataConTy
524 Int64ElemRep -> int64ElemRepDataConTy
525 Word8ElemRep -> word8ElemRepDataConTy
526 Word16ElemRep -> word16ElemRepDataConTy
527 Word32ElemRep -> word32ElemRepDataConTy
528 Word64ElemRep -> word64ElemRepDataConTy
529 FloatElemRep -> floatElemRepDataConTy
530 DoubleElemRep -> doubleElemRepDataConTy
531
532 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
533 pcPrimTyCon0 name rep
534 = pcPrimTyCon name [] rep
535
536 charPrimTy :: Type
537 charPrimTy = mkTyConTy charPrimTyCon
538 charPrimTyCon :: TyCon
539 charPrimTyCon = pcPrimTyCon0 charPrimTyConName WordRep
540
541 intPrimTy :: Type
542 intPrimTy = mkTyConTy intPrimTyCon
543 intPrimTyCon :: TyCon
544 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
545
546 int32PrimTy :: Type
547 int32PrimTy = mkTyConTy int32PrimTyCon
548 int32PrimTyCon :: TyCon
549 int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName IntRep
550
551 int64PrimTy :: Type
552 int64PrimTy = mkTyConTy int64PrimTyCon
553 int64PrimTyCon :: TyCon
554 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
555
556 wordPrimTy :: Type
557 wordPrimTy = mkTyConTy wordPrimTyCon
558 wordPrimTyCon :: TyCon
559 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
560
561 word32PrimTy :: Type
562 word32PrimTy = mkTyConTy word32PrimTyCon
563 word32PrimTyCon :: TyCon
564 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName WordRep
565
566 word64PrimTy :: Type
567 word64PrimTy = mkTyConTy word64PrimTyCon
568 word64PrimTyCon :: TyCon
569 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
570
571 addrPrimTy :: Type
572 addrPrimTy = mkTyConTy addrPrimTyCon
573 addrPrimTyCon :: TyCon
574 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
575
576 floatPrimTy :: Type
577 floatPrimTy = mkTyConTy floatPrimTyCon
578 floatPrimTyCon :: TyCon
579 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
580
581 doublePrimTy :: Type
582 doublePrimTy = mkTyConTy doublePrimTyCon
583 doublePrimTyCon :: TyCon
584 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
585
586 {-
587 ************************************************************************
588 * *
589 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
590 * *
591 ************************************************************************
592
593 Note [The equality types story]
594 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
595 GHC sports a veritable menagerie of equality types:
596
597 Type or Lifted? Hetero? Role Built in Defining module
598 class? L/U TyCon
599 -----------------------------------------------------------------------------------------
600 ~# T U hetero nominal eqPrimTyCon GHC.Prim
601 ~~ C L hetero nominal heqTyCon GHC.Types
602 ~ C L homo nominal eqTyCon GHC.Types
603 :~: T L homo nominal (not built-in) Data.Type.Equality
604 :~~: T L hetero nominal (not built-in) Data.Type.Equality
605
606 ~R# T U hetero repr eqReprPrimTy GHC.Prim
607 Coercible C L homo repr coercibleTyCon GHC.Types
608 Coercion T L homo repr (not built-in) Data.Type.Coercion
609 ~P# T U hetero phantom eqPhantPrimTyCon GHC.Prim
610
611 Recall that "hetero" means the equality can related types of different
612 kinds. Knowing that (t1 ~# t2) or (t1 ~R# t2) or even that (t1 ~P# t2)
613 also means that (k1 ~# k2), where (t1 :: k1) and (t2 :: k2).
614
615 To produce less confusion for end users, when not dumping and without
616 -fprint-equality-relations, each of these groups is printed as the bottommost
617 listed equality. That is, (~#) and (~~) are both rendered as (~) in
618 error messages, and (~R#) is rendered as Coercible.
619
620 Let's take these one at a time:
621
622 --------------------------
623 (~#) :: forall k1 k2. k1 -> k2 -> #
624 --------------------------
625 This is The Type Of Equality in GHC. It classifies nominal coercions.
626 This type is used in the solver for recording equality constraints.
627 It responds "yes" to Type.isEqPred and classifies as an EqPred in
628 Type.classifyPredType.
629
630 All wanted constraints of this type are built with coercion holes.
631 (See Note [Coercion holes] in TyCoRep.) But see also
632 Note [Deferred errors for coercion holes] in TcErrors to see how
633 equality constraints are deferred.
634
635 Within GHC, ~# is called eqPrimTyCon, and it is defined in TysPrim.
636
637
638 --------------------------
639 (~~) :: forall k1 k2. k1 -> k2 -> Constraint
640 --------------------------
641 This is (almost) an ordinary class, defined as if by
642 class a ~# b => a ~~ b
643 instance a ~# b => a ~~ b
644 Here's what's unusual about it:
645
646 * We can't actually declare it that way because we don't have syntax for ~#.
647 And ~# isn't a constraint, so even if we could write it, it wouldn't kind
648 check.
649
650 * Users cannot write instances of it.
651
652 * It is "naturally coherent". This means that the solver won't hesitate to
653 solve a goal of type (a ~~ b) even if there is, say (Int ~~ c) in the
654 context. (Normally, it waits to learn more, just in case the given
655 influences what happens next.) See Note [Naturally coherent classes]
656 in TcInteract.
657
658 * It always terminates. That is, in the UndecidableInstances checks, we
659 don't worry if a (~~) constraint is too big, as we know that solving
660 equality terminates.
661
662 On the other hand, this behaves just like any class w.r.t. eager superclass
663 unpacking in the solver. So a lifted equality given quickly becomes an unlifted
664 equality given. This is good, because the solver knows all about unlifted
665 equalities. There is some special-casing in TcInteract.matchClassInst to
666 pretend that there is an instance of this class, as we can't write the instance
667 in Haskell.
668
669 Within GHC, ~~ is called heqTyCon, and it is defined in TysWiredIn.
670
671
672 --------------------------
673 (~) :: forall k. k -> k -> Constraint
674 --------------------------
675 This is /exactly/ like (~~), except with a homogeneous kind.
676 It is an almost-ordinary class defined as if by
677 class a ~# b => (a :: k) ~ (b :: k)
678 instance a ~# b => a ~ b
679
680 * All the bullets for (~~) apply
681
682 * In addition (~) is magical syntax, as ~ is a reserved symbol.
683 It cannot be exported or imported.
684
685 Within GHC, ~ is called eqTyCon, and it is defined in TysWiredIn.
686
687 Historical note: prior to July 18 (~) was defined as a
688 more-ordinary class with (~~) as a superclass. But that made it
689 special in different ways; and the extra superclass selections to
690 get from (~) to (~#) via (~~) were tiresome. Now it's defined
691 uniformly with (~~) and Coercible; much nicer.)
692
693
694 --------------------------
695 (:~:) :: forall k. k -> k -> *
696 (:~~:) :: forall k1 k2. k1 -> k2 -> *
697 --------------------------
698 These are perfectly ordinary GADTs, wrapping (~) and (~~) resp.
699 They are not defined within GHC at all.
700
701
702 --------------------------
703 (~R#) :: forall k1 k2. k1 -> k2 -> #
704 --------------------------
705 The is the representational analogue of ~#. This is the type of representational
706 equalities that the solver works on. All wanted constraints of this type are
707 built with coercion holes.
708
709 Within GHC, ~R# is called eqReprPrimTyCon, and it is defined in TysPrim.
710
711
712 --------------------------
713 Coercible :: forall k. k -> k -> Constraint
714 --------------------------
715 This is quite like (~~) in the way it's defined and treated within GHC, but
716 it's homogeneous. Homogeneity helps with type inference (as GHC can solve one
717 kind from the other) and, in my (Richard's) estimation, will be more intuitive
718 for users.
719
720 An alternative design included HCoercible (like (~~)) and Coercible (like (~)).
721 One annoyance was that we want `coerce :: Coercible a b => a -> b`, and
722 we need the type of coerce to be fully wired-in. So the HCoercible/Coercible
723 split required that both types be fully wired-in. Instead of doing this,
724 I just got rid of HCoercible, as I'm not sure who would use it, anyway.
725
726 Within GHC, Coercible is called coercibleTyCon, and it is defined in
727 TysWiredIn.
728
729
730 --------------------------
731 Coercion :: forall k. k -> k -> *
732 --------------------------
733 This is a perfectly ordinary GADT, wrapping Coercible. It is not defined
734 within GHC at all.
735
736
737 --------------------------
738 (~P#) :: forall k1 k2. k1 -> k2 -> #
739 --------------------------
740 This is the phantom analogue of ~# and it is barely used at all.
741 (The solver has no idea about this one.) Here is the motivation:
742
743 data Phant a = MkPhant
744 type role Phant phantom
745
746 Phant <Int, Bool>_P :: Phant Int ~P# Phant Bool
747
748 We just need to have something to put on that last line. You probably
749 don't need to worry about it.
750
751
752
753 Note [The State# TyCon]
754 ~~~~~~~~~~~~~~~~~~~~~~~
755 State# is the primitive, unlifted type of states. It has one type parameter,
756 thus
757 State# RealWorld
758 or
759 State# s
760
761 where s is a type variable. The only purpose of the type parameter is to
762 keep different state threads separate. It is represented by nothing at all.
763
764 The type parameter to State# is intended to keep separate threads separate.
765 Even though this parameter is not used in the definition of State#, it is
766 given role Nominal to enforce its intended use.
767 -}
768
769 mkStatePrimTy :: Type -> Type
770 mkStatePrimTy ty = TyConApp statePrimTyCon [ty]
771
772 statePrimTyCon :: TyCon -- See Note [The State# TyCon]
773 statePrimTyCon = pcPrimTyCon statePrimTyConName [Nominal] VoidRep
774
775 {-
776 RealWorld is deeply magical. It is *primitive*, but it is not
777 *unlifted* (hence ptrArg). We never manipulate values of type
778 RealWorld; it's only used in the type system, to parameterise State#.
779 -}
780
781 realWorldTyCon :: TyCon
782 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName [] liftedTypeKind []
783 realWorldTy :: Type
784 realWorldTy = mkTyConTy realWorldTyCon
785 realWorldStatePrimTy :: Type
786 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
787
788 -- Note: the ``state-pairing'' types are not truly primitive,
789 -- so they are defined in \tr{TysWiredIn.hs}, not here.
790
791
792 voidPrimTy :: Type
793 voidPrimTy = TyConApp voidPrimTyCon []
794
795 voidPrimTyCon :: TyCon
796 voidPrimTyCon = pcPrimTyCon voidPrimTyConName [] VoidRep
797
798 mkProxyPrimTy :: Type -> Type -> Type
799 mkProxyPrimTy k ty = TyConApp proxyPrimTyCon [k, ty]
800
801 proxyPrimTyCon :: TyCon
802 proxyPrimTyCon = mkPrimTyCon proxyPrimTyConName binders res_kind [Nominal,Nominal]
803 where
804 -- Kind: forall k. k -> Void#
805 binders = mkTemplateTyConBinders [liftedTypeKind] (\ks-> ks)
806 res_kind = unboxedTupleKind []
807
808
809 {- *********************************************************************
810 * *
811 Primitive equality constraints
812 See Note [The equality types story]
813 * *
814 ********************************************************************* -}
815
816 eqPrimTyCon :: TyCon -- The representation type for equality predicates
817 -- See Note [The equality types story]
818 eqPrimTyCon = mkPrimTyCon eqPrimTyConName binders res_kind roles
819 where
820 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
821 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
822 res_kind = unboxedTupleKind []
823 roles = [Nominal, Nominal, Nominal, Nominal]
824
825 -- like eqPrimTyCon, but the type for *Representational* coercions
826 -- this should only ever appear as the type of a covar. Its role is
827 -- interpreted in coercionRole
828 eqReprPrimTyCon :: TyCon -- See Note [The equality types story]
829 eqReprPrimTyCon = mkPrimTyCon eqReprPrimTyConName binders res_kind roles
830 where
831 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
832 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
833 res_kind = unboxedTupleKind []
834 roles = [Nominal, Nominal, Representational, Representational]
835
836 -- like eqPrimTyCon, but the type for *Phantom* coercions.
837 -- This is only used to make higher-order equalities. Nothing
838 -- should ever actually have this type!
839 eqPhantPrimTyCon :: TyCon
840 eqPhantPrimTyCon = mkPrimTyCon eqPhantPrimTyConName binders res_kind roles
841 where
842 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
843 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
844 res_kind = unboxedTupleKind []
845 roles = [Nominal, Nominal, Phantom, Phantom]
846
847 {- *********************************************************************
848 * *
849 The primitive array types
850 * *
851 ********************************************************************* -}
852
853 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
854 byteArrayPrimTyCon, arrayArrayPrimTyCon, mutableArrayArrayPrimTyCon,
855 smallArrayPrimTyCon, smallMutableArrayPrimTyCon :: TyCon
856 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName [Representational] UnliftedRep
857 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName [Nominal, Representational] UnliftedRep
858 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName [Nominal] UnliftedRep
859 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName UnliftedRep
860 arrayArrayPrimTyCon = pcPrimTyCon0 arrayArrayPrimTyConName UnliftedRep
861 mutableArrayArrayPrimTyCon = pcPrimTyCon mutableArrayArrayPrimTyConName [Nominal] UnliftedRep
862 smallArrayPrimTyCon = pcPrimTyCon smallArrayPrimTyConName [Representational] UnliftedRep
863 smallMutableArrayPrimTyCon = pcPrimTyCon smallMutableArrayPrimTyConName [Nominal, Representational] UnliftedRep
864
865 mkArrayPrimTy :: Type -> Type
866 mkArrayPrimTy elt = TyConApp arrayPrimTyCon [elt]
867 byteArrayPrimTy :: Type
868 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
869 mkArrayArrayPrimTy :: Type
870 mkArrayArrayPrimTy = mkTyConTy arrayArrayPrimTyCon
871 mkSmallArrayPrimTy :: Type -> Type
872 mkSmallArrayPrimTy elt = TyConApp smallArrayPrimTyCon [elt]
873 mkMutableArrayPrimTy :: Type -> Type -> Type
874 mkMutableArrayPrimTy s elt = TyConApp mutableArrayPrimTyCon [s, elt]
875 mkMutableByteArrayPrimTy :: Type -> Type
876 mkMutableByteArrayPrimTy s = TyConApp mutableByteArrayPrimTyCon [s]
877 mkMutableArrayArrayPrimTy :: Type -> Type
878 mkMutableArrayArrayPrimTy s = TyConApp mutableArrayArrayPrimTyCon [s]
879 mkSmallMutableArrayPrimTy :: Type -> Type -> Type
880 mkSmallMutableArrayPrimTy s elt = TyConApp smallMutableArrayPrimTyCon [s, elt]
881
882
883 {- *********************************************************************
884 * *
885 The mutable variable type
886 * *
887 ********************************************************************* -}
888
889 mutVarPrimTyCon :: TyCon
890 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName [Nominal, Representational] UnliftedRep
891
892 mkMutVarPrimTy :: Type -> Type -> Type
893 mkMutVarPrimTy s elt = TyConApp mutVarPrimTyCon [s, elt]
894
895 {-
896 ************************************************************************
897 * *
898 \subsection[TysPrim-synch-var]{The synchronizing variable type}
899 * *
900 ************************************************************************
901 -}
902
903 mVarPrimTyCon :: TyCon
904 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName [Nominal, Representational] UnliftedRep
905
906 mkMVarPrimTy :: Type -> Type -> Type
907 mkMVarPrimTy s elt = TyConApp mVarPrimTyCon [s, elt]
908
909 {-
910 ************************************************************************
911 * *
912 \subsection[TysPrim-stm-var]{The transactional variable type}
913 * *
914 ************************************************************************
915 -}
916
917 tVarPrimTyCon :: TyCon
918 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName [Nominal, Representational] UnliftedRep
919
920 mkTVarPrimTy :: Type -> Type -> Type
921 mkTVarPrimTy s elt = TyConApp tVarPrimTyCon [s, elt]
922
923 {-
924 ************************************************************************
925 * *
926 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
927 * *
928 ************************************************************************
929 -}
930
931 stablePtrPrimTyCon :: TyCon
932 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName [Representational] AddrRep
933
934 mkStablePtrPrimTy :: Type -> Type
935 mkStablePtrPrimTy ty = TyConApp stablePtrPrimTyCon [ty]
936
937 {-
938 ************************************************************************
939 * *
940 \subsection[TysPrim-stable-names]{The stable-name type}
941 * *
942 ************************************************************************
943 -}
944
945 stableNamePrimTyCon :: TyCon
946 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName [Representational] UnliftedRep
947
948 mkStableNamePrimTy :: Type -> Type
949 mkStableNamePrimTy ty = TyConApp stableNamePrimTyCon [ty]
950
951 {-
952 ************************************************************************
953 * *
954 \subsection[TysPrim-compact-nfdata]{The Compact NFData (CNF) type}
955 * *
956 ************************************************************************
957 -}
958
959 compactPrimTyCon :: TyCon
960 compactPrimTyCon = pcPrimTyCon0 compactPrimTyConName UnliftedRep
961
962 compactPrimTy :: Type
963 compactPrimTy = mkTyConTy compactPrimTyCon
964
965 {-
966 ************************************************************************
967 * *
968 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
969 * *
970 ************************************************************************
971 -}
972
973 bcoPrimTy :: Type
974 bcoPrimTy = mkTyConTy bcoPrimTyCon
975 bcoPrimTyCon :: TyCon
976 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName UnliftedRep
977
978 {-
979 ************************************************************************
980 * *
981 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
982 * *
983 ************************************************************************
984 -}
985
986 weakPrimTyCon :: TyCon
987 weakPrimTyCon = pcPrimTyCon weakPrimTyConName [Representational] UnliftedRep
988
989 mkWeakPrimTy :: Type -> Type
990 mkWeakPrimTy v = TyConApp weakPrimTyCon [v]
991
992 {-
993 ************************************************************************
994 * *
995 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
996 * *
997 ************************************************************************
998
999 A thread id is represented by a pointer to the TSO itself, to ensure
1000 that they are always unique and we can always find the TSO for a given
1001 thread id. However, this has the unfortunate consequence that a
1002 ThreadId# for a given thread is treated as a root by the garbage
1003 collector and can keep TSOs around for too long.
1004
1005 Hence the programmer API for thread manipulation uses a weak pointer
1006 to the thread id internally.
1007 -}
1008
1009 threadIdPrimTy :: Type
1010 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
1011 threadIdPrimTyCon :: TyCon
1012 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName UnliftedRep
1013
1014 {-
1015 ************************************************************************
1016 * *
1017 \subsection{SIMD vector types}
1018 * *
1019 ************************************************************************
1020 -}
1021
1022 #include "primop-vector-tys.hs-incl"