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