Don't expose (~#), (~R#), (~P#) from GHC.Prim
[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, TyVarBndr(TvBndr), 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 = [ TvBndr runtimeRep1TyVar (NamedTCB Inferred)
355 , TvBndr 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 Data.Type.Equality
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 * We can't actually declare it that way because we don't have syntax for ~#.
646 And ~# isn't a constraint, so even if we could write it, it wouldn't kind
647 check.
648
649 * Users cannot write instances of it.
650
651 * It is "naturally coherent". This means that the solver won't hesitate to
652 solve a goal of type (a ~~ b) even if there is, say (Int ~~ c) in the
653 context. (Normally, it waits to learn more, just in case the given
654 influences what happens next.) See Note [Naturally coherent classes]
655 in TcInteract.
656
657 * It always terminates. That is, in the UndecidableInstances checks, we
658 don't worry if a (~~) constraint is too big, as we know that solving
659 equality terminates.
660
661 On the other hand, this behaves just like any class w.r.t. eager superclass
662 unpacking in the solver. So a lifted equality given quickly becomes an unlifted
663 equality given. This is good, because the solver knows all about unlifted
664 equalities. There is some special-casing in TcInteract.matchClassInst to
665 pretend that there is an instance of this class, as we can't write the instance
666 in Haskell.
667
668 Within GHC, ~~ is called heqTyCon, and it is defined in TysWiredIn.
669
670
671 --------------------------
672 (~) :: forall k. k -> k -> Constraint
673 --------------------------
674 This is defined in Data.Type.Equality:
675 class a ~~ b => (a :: k) ~ (b :: k)
676 instance a ~~ b => a ~ b
677 This is even more so an ordinary class than (~~), with the following exceptions:
678 * Users cannot write instances of it.
679
680 * It is "naturally coherent". (See (~~).)
681
682 * (~) is magical syntax, as ~ is a reserved symbol.
683 It cannot be exported or imported.
684
685 * It always terminates.
686
687 Within GHC, ~ is called eqTyCon, and it is defined in PrelNames. Note that
688 it is *not* wired in.
689
690
691 --------------------------
692 (:~:) :: forall k. k -> k -> *
693 (:~~:) :: forall k1 k2. k1 -> k2 -> *
694 --------------------------
695 These are perfectly ordinary GADTs, wrapping (~) and (~~) resp.
696 They are not defined within GHC at all.
697
698
699 --------------------------
700 (~R#) :: forall k1 k2. k1 -> k2 -> #
701 --------------------------
702 The is the representational analogue of ~#. This is the type of representational
703 equalities that the solver works on. All wanted constraints of this type are
704 built with coercion holes.
705
706 Within GHC, ~R# is called eqReprPrimTyCon, and it is defined in TysPrim.
707
708
709 --------------------------
710 Coercible :: forall k. k -> k -> Constraint
711 --------------------------
712 This is quite like (~~) in the way it's defined and treated within GHC, but
713 it's homogeneous. Homogeneity helps with type inference (as GHC can solve one
714 kind from the other) and, in my (Richard's) estimation, will be more intuitive
715 for users.
716
717 An alternative design included HCoercible (like (~~)) and Coercible (like (~)).
718 One annoyance was that we want `coerce :: Coercible a b => a -> b`, and
719 we need the type of coerce to be fully wired-in. So the HCoercible/Coercible
720 split required that both types be fully wired-in. Instead of doing this,
721 I just got rid of HCoercible, as I'm not sure who would use it, anyway.
722
723 Within GHC, Coercible is called coercibleTyCon, and it is defined in
724 TysWiredIn.
725
726
727 --------------------------
728 Coercion :: forall k. k -> k -> *
729 --------------------------
730 This is a perfectly ordinary GADT, wrapping Coercible. It is not defined
731 within GHC at all.
732
733
734 --------------------------
735 (~P#) :: forall k1 k2. k1 -> k2 -> #
736 --------------------------
737 This is the phantom analogue of ~# and it is barely used at all.
738 (The solver has no idea about this one.) Here is the motivation:
739
740 data Phant a = MkPhant
741 type role Phant phantom
742
743 Phant <Int, Bool>_P :: Phant Int ~P# Phant Bool
744
745 We just need to have something to put on that last line. You probably
746 don't need to worry about it.
747
748
749
750 Note [The State# TyCon]
751 ~~~~~~~~~~~~~~~~~~~~~~~
752 State# is the primitive, unlifted type of states. It has one type parameter,
753 thus
754 State# RealWorld
755 or
756 State# s
757
758 where s is a type variable. The only purpose of the type parameter is to
759 keep different state threads separate. It is represented by nothing at all.
760
761 The type parameter to State# is intended to keep separate threads separate.
762 Even though this parameter is not used in the definition of State#, it is
763 given role Nominal to enforce its intended use.
764 -}
765
766 mkStatePrimTy :: Type -> Type
767 mkStatePrimTy ty = TyConApp statePrimTyCon [ty]
768
769 statePrimTyCon :: TyCon -- See Note [The State# TyCon]
770 statePrimTyCon = pcPrimTyCon statePrimTyConName [Nominal] VoidRep
771
772 {-
773 RealWorld is deeply magical. It is *primitive*, but it is not
774 *unlifted* (hence ptrArg). We never manipulate values of type
775 RealWorld; it's only used in the type system, to parameterise State#.
776 -}
777
778 realWorldTyCon :: TyCon
779 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName [] liftedTypeKind []
780 realWorldTy :: Type
781 realWorldTy = mkTyConTy realWorldTyCon
782 realWorldStatePrimTy :: Type
783 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
784
785 -- Note: the ``state-pairing'' types are not truly primitive,
786 -- so they are defined in \tr{TysWiredIn.hs}, not here.
787
788
789 voidPrimTy :: Type
790 voidPrimTy = TyConApp voidPrimTyCon []
791
792 voidPrimTyCon :: TyCon
793 voidPrimTyCon = pcPrimTyCon voidPrimTyConName [] VoidRep
794
795 mkProxyPrimTy :: Type -> Type -> Type
796 mkProxyPrimTy k ty = TyConApp proxyPrimTyCon [k, ty]
797
798 proxyPrimTyCon :: TyCon
799 proxyPrimTyCon = mkPrimTyCon proxyPrimTyConName binders res_kind [Nominal,Nominal]
800 where
801 -- Kind: forall k. k -> Void#
802 binders = mkTemplateTyConBinders [liftedTypeKind] (\ks-> ks)
803 res_kind = unboxedTupleKind []
804
805
806 {- *********************************************************************
807 * *
808 Primitive equality constraints
809 See Note [The equality types story]
810 * *
811 ********************************************************************* -}
812
813 eqPrimTyCon :: TyCon -- The representation type for equality predicates
814 -- See Note [The equality types story]
815 eqPrimTyCon = mkPrimTyCon eqPrimTyConName binders res_kind roles
816 where
817 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
818 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
819 res_kind = unboxedTupleKind []
820 roles = [Nominal, Nominal, Nominal, Nominal]
821
822 -- like eqPrimTyCon, but the type for *Representational* coercions
823 -- this should only ever appear as the type of a covar. Its role is
824 -- interpreted in coercionRole
825 eqReprPrimTyCon :: TyCon -- See Note [The equality types story]
826 eqReprPrimTyCon = mkPrimTyCon eqReprPrimTyConName binders res_kind roles
827 where
828 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
829 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
830 res_kind = unboxedTupleKind []
831 roles = [Nominal, Nominal, Representational, Representational]
832
833 -- like eqPrimTyCon, but the type for *Phantom* coercions.
834 -- This is only used to make higher-order equalities. Nothing
835 -- should ever actually have this type!
836 eqPhantPrimTyCon :: TyCon
837 eqPhantPrimTyCon = mkPrimTyCon eqPhantPrimTyConName binders res_kind roles
838 where
839 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
840 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
841 res_kind = unboxedTupleKind []
842 roles = [Nominal, Nominal, Phantom, Phantom]
843
844 {- *********************************************************************
845 * *
846 The primitive array types
847 * *
848 ********************************************************************* -}
849
850 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
851 byteArrayPrimTyCon, arrayArrayPrimTyCon, mutableArrayArrayPrimTyCon,
852 smallArrayPrimTyCon, smallMutableArrayPrimTyCon :: TyCon
853 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName [Representational] UnliftedRep
854 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName [Nominal, Representational] UnliftedRep
855 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName [Nominal] UnliftedRep
856 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName UnliftedRep
857 arrayArrayPrimTyCon = pcPrimTyCon0 arrayArrayPrimTyConName UnliftedRep
858 mutableArrayArrayPrimTyCon = pcPrimTyCon mutableArrayArrayPrimTyConName [Nominal] UnliftedRep
859 smallArrayPrimTyCon = pcPrimTyCon smallArrayPrimTyConName [Representational] UnliftedRep
860 smallMutableArrayPrimTyCon = pcPrimTyCon smallMutableArrayPrimTyConName [Nominal, Representational] UnliftedRep
861
862 mkArrayPrimTy :: Type -> Type
863 mkArrayPrimTy elt = TyConApp arrayPrimTyCon [elt]
864 byteArrayPrimTy :: Type
865 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
866 mkArrayArrayPrimTy :: Type
867 mkArrayArrayPrimTy = mkTyConTy arrayArrayPrimTyCon
868 mkSmallArrayPrimTy :: Type -> Type
869 mkSmallArrayPrimTy elt = TyConApp smallArrayPrimTyCon [elt]
870 mkMutableArrayPrimTy :: Type -> Type -> Type
871 mkMutableArrayPrimTy s elt = TyConApp mutableArrayPrimTyCon [s, elt]
872 mkMutableByteArrayPrimTy :: Type -> Type
873 mkMutableByteArrayPrimTy s = TyConApp mutableByteArrayPrimTyCon [s]
874 mkMutableArrayArrayPrimTy :: Type -> Type
875 mkMutableArrayArrayPrimTy s = TyConApp mutableArrayArrayPrimTyCon [s]
876 mkSmallMutableArrayPrimTy :: Type -> Type -> Type
877 mkSmallMutableArrayPrimTy s elt = TyConApp smallMutableArrayPrimTyCon [s, elt]
878
879
880 {- *********************************************************************
881 * *
882 The mutable variable type
883 * *
884 ********************************************************************* -}
885
886 mutVarPrimTyCon :: TyCon
887 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName [Nominal, Representational] UnliftedRep
888
889 mkMutVarPrimTy :: Type -> Type -> Type
890 mkMutVarPrimTy s elt = TyConApp mutVarPrimTyCon [s, elt]
891
892 {-
893 ************************************************************************
894 * *
895 \subsection[TysPrim-synch-var]{The synchronizing variable type}
896 * *
897 ************************************************************************
898 -}
899
900 mVarPrimTyCon :: TyCon
901 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName [Nominal, Representational] UnliftedRep
902
903 mkMVarPrimTy :: Type -> Type -> Type
904 mkMVarPrimTy s elt = TyConApp mVarPrimTyCon [s, elt]
905
906 {-
907 ************************************************************************
908 * *
909 \subsection[TysPrim-stm-var]{The transactional variable type}
910 * *
911 ************************************************************************
912 -}
913
914 tVarPrimTyCon :: TyCon
915 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName [Nominal, Representational] UnliftedRep
916
917 mkTVarPrimTy :: Type -> Type -> Type
918 mkTVarPrimTy s elt = TyConApp tVarPrimTyCon [s, elt]
919
920 {-
921 ************************************************************************
922 * *
923 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
924 * *
925 ************************************************************************
926 -}
927
928 stablePtrPrimTyCon :: TyCon
929 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName [Representational] AddrRep
930
931 mkStablePtrPrimTy :: Type -> Type
932 mkStablePtrPrimTy ty = TyConApp stablePtrPrimTyCon [ty]
933
934 {-
935 ************************************************************************
936 * *
937 \subsection[TysPrim-stable-names]{The stable-name type}
938 * *
939 ************************************************************************
940 -}
941
942 stableNamePrimTyCon :: TyCon
943 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName [Representational] UnliftedRep
944
945 mkStableNamePrimTy :: Type -> Type
946 mkStableNamePrimTy ty = TyConApp stableNamePrimTyCon [ty]
947
948 {-
949 ************************************************************************
950 * *
951 \subsection[TysPrim-compact-nfdata]{The Compact NFData (CNF) type}
952 * *
953 ************************************************************************
954 -}
955
956 compactPrimTyCon :: TyCon
957 compactPrimTyCon = pcPrimTyCon0 compactPrimTyConName UnliftedRep
958
959 compactPrimTy :: Type
960 compactPrimTy = mkTyConTy compactPrimTyCon
961
962 {-
963 ************************************************************************
964 * *
965 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
966 * *
967 ************************************************************************
968 -}
969
970 bcoPrimTy :: Type
971 bcoPrimTy = mkTyConTy bcoPrimTyCon
972 bcoPrimTyCon :: TyCon
973 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName UnliftedRep
974
975 {-
976 ************************************************************************
977 * *
978 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
979 * *
980 ************************************************************************
981 -}
982
983 weakPrimTyCon :: TyCon
984 weakPrimTyCon = pcPrimTyCon weakPrimTyConName [Representational] UnliftedRep
985
986 mkWeakPrimTy :: Type -> Type
987 mkWeakPrimTy v = TyConApp weakPrimTyCon [v]
988
989 {-
990 ************************************************************************
991 * *
992 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
993 * *
994 ************************************************************************
995
996 A thread id is represented by a pointer to the TSO itself, to ensure
997 that they are always unique and we can always find the TSO for a given
998 thread id. However, this has the unfortunate consequence that a
999 ThreadId# for a given thread is treated as a root by the garbage
1000 collector and can keep TSOs around for too long.
1001
1002 Hence the programmer API for thread manipulation uses a weak pointer
1003 to the thread id internally.
1004 -}
1005
1006 threadIdPrimTy :: Type
1007 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
1008 threadIdPrimTyCon :: TyCon
1009 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName UnliftedRep
1010
1011 {-
1012 ************************************************************************
1013 * *
1014 \subsection{SIMD vector types}
1015 * *
1016 ************************************************************************
1017 -}
1018
1019 #include "primop-vector-tys.hs-incl"