2 % (c) The AQUA Project, Glasgow University, 1994-1998
6 \section[TysPrim]{Wired-in knowledge about primitive types}
9 -- | This module defines TyCons that can't be expressed in Haskell.
10 -- They are all, therefore, wired-in TyCons. C.f module TysWiredIn
12 alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
13 alphaTy, betaTy, gammaTy, deltaTy,
14 openAlphaTy, openBetaTy, openAlphaTyVar, openBetaTyVar, openAlphaTyVars,
15 argAlphaTy, argAlphaTyVar, argBetaTy, argBetaTyVar,
19 charPrimTyCon, charPrimTy,
20 intPrimTyCon, intPrimTy,
21 wordPrimTyCon, wordPrimTy,
22 addrPrimTyCon, addrPrimTy,
23 floatPrimTyCon, floatPrimTy,
24 doublePrimTyCon, doublePrimTy,
26 statePrimTyCon, mkStatePrimTy,
27 realWorldTyCon, realWorldTy, realWorldStatePrimTy,
29 arrayPrimTyCon, mkArrayPrimTy,
30 byteArrayPrimTyCon, byteArrayPrimTy,
31 mutableArrayPrimTyCon, mkMutableArrayPrimTy,
32 mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
33 mutVarPrimTyCon, mkMutVarPrimTy,
35 mVarPrimTyCon, mkMVarPrimTy,
36 tVarPrimTyCon, mkTVarPrimTy,
37 stablePtrPrimTyCon, mkStablePtrPrimTy,
38 stableNamePrimTyCon, mkStableNamePrimTy,
39 bcoPrimTyCon, bcoPrimTy,
40 weakPrimTyCon, mkWeakPrimTy,
41 threadIdPrimTyCon, threadIdPrimTy,
43 int32PrimTyCon, int32PrimTy,
44 word32PrimTyCon, word32PrimTy,
46 int64PrimTyCon, int64PrimTy,
47 word64PrimTyCon, word64PrimTy,
50 anyTyCon, anyTyConOfKind, anyTypeOfKind
53 #include "HsVersions.h"
55 import Var ( TyVar, mkTyVar )
56 import Name ( Name, BuiltInSyntax(..), mkInternalName, mkWiredInName )
57 import OccName ( mkTcOcc )
58 import OccName ( mkTyVarOccFS, mkTcOccFS )
59 import TyCon ( TyCon, mkPrimTyCon, mkLiftedPrimTyCon, mkAnyTyCon )
63 import Unique ( mkAlphaTyVarUnique )
71 %************************************************************************
73 \subsection{Primitive type constructors}
75 %************************************************************************
91 , mutableArrayPrimTyCon
92 , mutableByteArrayPrimTyCon
107 mkPrimTc :: FastString -> Unique -> TyCon -> Name
108 mkPrimTc fs unique tycon
109 = mkWiredInName gHC_PRIM (mkTcOccFS fs)
111 (ATyCon tycon) -- Relevant TyCon
112 UserSyntax -- None are built-in syntax
114 charPrimTyConName, intPrimTyConName, int32PrimTyConName, int64PrimTyConName, wordPrimTyConName, word32PrimTyConName, word64PrimTyConName, addrPrimTyConName, floatPrimTyConName, doublePrimTyConName, statePrimTyConName, realWorldTyConName, arrayPrimTyConName, byteArrayPrimTyConName, mutableArrayPrimTyConName, mutableByteArrayPrimTyConName, mutVarPrimTyConName, mVarPrimTyConName, tVarPrimTyConName, stablePtrPrimTyConName, stableNamePrimTyConName, bcoPrimTyConName, weakPrimTyConName, threadIdPrimTyConName :: Name
115 charPrimTyConName = mkPrimTc (fsLit "Char#") charPrimTyConKey charPrimTyCon
116 intPrimTyConName = mkPrimTc (fsLit "Int#") intPrimTyConKey intPrimTyCon
117 int32PrimTyConName = mkPrimTc (fsLit "Int32#") int32PrimTyConKey int32PrimTyCon
118 int64PrimTyConName = mkPrimTc (fsLit "Int64#") int64PrimTyConKey int64PrimTyCon
119 wordPrimTyConName = mkPrimTc (fsLit "Word#") wordPrimTyConKey wordPrimTyCon
120 word32PrimTyConName = mkPrimTc (fsLit "Word32#") word32PrimTyConKey word32PrimTyCon
121 word64PrimTyConName = mkPrimTc (fsLit "Word64#") word64PrimTyConKey word64PrimTyCon
122 addrPrimTyConName = mkPrimTc (fsLit "Addr#") addrPrimTyConKey addrPrimTyCon
123 floatPrimTyConName = mkPrimTc (fsLit "Float#") floatPrimTyConKey floatPrimTyCon
124 doublePrimTyConName = mkPrimTc (fsLit "Double#") doublePrimTyConKey doublePrimTyCon
125 statePrimTyConName = mkPrimTc (fsLit "State#") statePrimTyConKey statePrimTyCon
126 realWorldTyConName = mkPrimTc (fsLit "RealWorld") realWorldTyConKey realWorldTyCon
127 arrayPrimTyConName = mkPrimTc (fsLit "Array#") arrayPrimTyConKey arrayPrimTyCon
128 byteArrayPrimTyConName = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey byteArrayPrimTyCon
129 mutableArrayPrimTyConName = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey mutableArrayPrimTyCon
130 mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey mutableByteArrayPrimTyCon
131 mutVarPrimTyConName = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey mutVarPrimTyCon
132 mVarPrimTyConName = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey mVarPrimTyCon
133 tVarPrimTyConName = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey tVarPrimTyCon
134 stablePtrPrimTyConName = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey stablePtrPrimTyCon
135 stableNamePrimTyConName = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey stableNamePrimTyCon
136 bcoPrimTyConName = mkPrimTc (fsLit "BCO#") bcoPrimTyConKey bcoPrimTyCon
137 weakPrimTyConName = mkPrimTc (fsLit "Weak#") weakPrimTyConKey weakPrimTyCon
138 threadIdPrimTyConName = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey threadIdPrimTyCon
141 %************************************************************************
143 \subsection{Support code}
145 %************************************************************************
147 alphaTyVars is a list of type variables for use in templates:
148 ["a", "b", ..., "z", "t1", "t2", ... ]
151 tyVarList :: Kind -> [TyVar]
152 tyVarList kind = [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u)
153 (mkTyVarOccFS (mkFastString name))
156 let name | c <= 'z' = [c]
157 | otherwise = 't':show u
158 where c = chr (u-2 + ord 'a')
161 alphaTyVars :: [TyVar]
162 alphaTyVars = tyVarList liftedTypeKind
164 betaTyVars :: [TyVar]
165 betaTyVars = tail alphaTyVars
167 alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: TyVar
168 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
171 alphaTys = mkTyVarTys alphaTyVars
172 alphaTy, betaTy, gammaTy, deltaTy :: Type
173 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
175 -- openAlphaTyVar is prepared to be instantiated
176 -- to a lifted or unlifted type variable. It's used for the
177 -- result type for "error", so that we can have (error Int# "Help")
178 openAlphaTyVars :: [TyVar]
179 openAlphaTyVar, openBetaTyVar :: TyVar
180 openAlphaTyVars@(openAlphaTyVar:openBetaTyVar:_) = tyVarList openTypeKind
182 openAlphaTy, openBetaTy :: Type
183 openAlphaTy = mkTyVarTy openAlphaTyVar
184 openBetaTy = mkTyVarTy openBetaTyVar
186 argAlphaTyVar, argBetaTyVar :: TyVar
187 (argAlphaTyVar : argBetaTyVar : _) = tyVarList argTypeKind
188 argAlphaTy, argBetaTy :: Type
189 argAlphaTy = mkTyVarTy argAlphaTyVar
190 argBetaTy = mkTyVarTy argBetaTyVar
194 %************************************************************************
198 %************************************************************************
202 The type constructor Any::* has these properties
204 * It is defined in module GHC.Prim, and exported so that it is
205 available to users. For this reason it's treated like any other
207 - has a fixed unique, anyTyConKey,
208 - lives in the global name cache
209 - built with TyCon.PrimTyCon
211 * It is lifted, and hence represented by a pointer
213 * It is inhabited by at least one value, namely bottom
215 * You can unsafely coerce any lifted type to Ayny, and back.
217 * It does not claim to be a *data* type, and that's important for
218 the code generator, because the code gen may *enter* a data value
219 but never enters a function value.
221 * It is used to instantiate otherwise un-constrained type variables of kind *
222 For example length Any []
223 See Note [Strangely-kinded void TyCons]
225 In addition, we have a potentially-infinite family of types, one for
226 each kind /other than/ *, needed to instantiate otherwise
227 un-constrained type variables of kinds other than *. This is a bit
228 like tuples; there is a potentially-infinite family. They have slightly
229 different characteristics to Any::*:
231 * They are built with TyCon.AnyTyCon
232 * They have non-user-writable names like "Any(*->*)"
233 * They are not exported by GHC.Prim
234 * They are uninhabited (of course; not kind *)
235 * They have a unique derived from their OccName (see Note [Uniques of Any])
236 * Their Names do not live in the global name cache
238 Note [Uniques of Any]
239 ~~~~~~~~~~~~~~~~~~~~~
240 Although Any(*->*), say, doesn't have a binding site, it still needs
241 to have a Unique. Unlike tuples (which are also an infinite family)
242 there is no convenient way to index them, so we use the Unique from
243 their OccName instead. That should be unique,
244 - both wrt each other, because their strings differ
246 - and wrt any other Name, because Names get uniques with
247 various 'char' tags, but the OccName of Any will
248 get a Unique built with mkTcOccUnique, which has a particular 'char'
249 tag; see Unique.mkTcOccUnique!
251 Note [Strangely-kinded void TyCons]
252 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
253 See Trac #959 for more examples
255 When the type checker finds a type variable with no binding, which
256 means it can be instantiated with an arbitrary type, it usually
257 instantiates it to Void. Eg.
263 But in really obscure programs, the type variable might have a kind
264 other than *, so we need to invent a suitably-kinded type.
268 Any(*->*) for kind *->*
273 anyTyConName = mkPrimTc (fsLit "Any") anyTyConKey anyTyCon
276 anyTyCon = mkLiftedPrimTyCon anyTyConName liftedTypeKind 0 PtrRep
278 anyTypeOfKind :: Kind -> Type
279 anyTypeOfKind kind = mkTyConApp (anyTyConOfKind kind) []
281 anyTyConOfKind :: Kind -> TyCon
282 -- Map all superkinds of liftedTypeKind to liftedTypeKind
284 | liftedTypeKind `isSubKind` kind = anyTyCon
287 -- Derive the name from the kind, thus:
288 -- Any(*->*), Any(*->*->*)
289 -- These are names that can't be written by the user,
290 -- and are not allocated in the global name cache
291 str = "Any" ++ showSDoc (pprParendKind kind)
294 uniq = getUnique occ -- See Note [Uniques of Any]
295 name = mkWiredInName gHC_PRIM occ uniq (ATyCon tycon) UserSyntax
296 tycon = mkAnyTyCon name kind
300 %************************************************************************
302 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
304 %************************************************************************
308 pcPrimTyCon :: Name -> Int -> PrimRep -> TyCon
309 pcPrimTyCon name arity rep
310 = mkPrimTyCon name kind arity rep
312 kind = mkArrowKinds (replicate arity liftedTypeKind) result_kind
313 result_kind = unliftedTypeKind
315 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
316 pcPrimTyCon0 name rep
317 = mkPrimTyCon name result_kind 0 rep
319 result_kind = unliftedTypeKind
322 charPrimTy = mkTyConTy charPrimTyCon
323 charPrimTyCon :: TyCon
324 charPrimTyCon = pcPrimTyCon0 charPrimTyConName WordRep
327 intPrimTy = mkTyConTy intPrimTyCon
328 intPrimTyCon :: TyCon
329 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
332 int32PrimTy = mkTyConTy int32PrimTyCon
333 int32PrimTyCon :: TyCon
334 int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName IntRep
337 int64PrimTy = mkTyConTy int64PrimTyCon
338 int64PrimTyCon :: TyCon
339 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
342 wordPrimTy = mkTyConTy wordPrimTyCon
343 wordPrimTyCon :: TyCon
344 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
347 word32PrimTy = mkTyConTy word32PrimTyCon
348 word32PrimTyCon :: TyCon
349 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName WordRep
352 word64PrimTy = mkTyConTy word64PrimTyCon
353 word64PrimTyCon :: TyCon
354 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
357 addrPrimTy = mkTyConTy addrPrimTyCon
358 addrPrimTyCon :: TyCon
359 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
362 floatPrimTy = mkTyConTy floatPrimTyCon
363 floatPrimTyCon :: TyCon
364 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
367 doublePrimTy = mkTyConTy doublePrimTyCon
368 doublePrimTyCon :: TyCon
369 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
373 %************************************************************************
375 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
377 %************************************************************************
379 State# is the primitive, unlifted type of states. It has one type parameter,
385 where s is a type variable. The only purpose of the type parameter is to
386 keep different state threads separate. It is represented by nothing at all.
389 mkStatePrimTy :: Type -> Type
390 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
391 statePrimTyCon :: TyCon
392 statePrimTyCon = pcPrimTyCon statePrimTyConName 1 VoidRep
395 RealWorld is deeply magical. It is *primitive*, but it is not
396 *unlifted* (hence ptrArg). We never manipulate values of type
397 RealWorld; it's only used in the type system, to parameterise State#.
400 realWorldTyCon :: TyCon
401 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 PtrRep
403 realWorldTy = mkTyConTy realWorldTyCon
404 realWorldStatePrimTy :: Type
405 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
408 Note: the ``state-pairing'' types are not truly primitive, so they are
409 defined in \tr{TysWiredIn.lhs}, not here.
412 %************************************************************************
414 \subsection[TysPrim-arrays]{The primitive array types}
416 %************************************************************************
419 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
420 byteArrayPrimTyCon :: TyCon
421 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName 1 PtrRep
422 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName 2 PtrRep
423 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName 1 PtrRep
424 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName PtrRep
426 mkArrayPrimTy :: Type -> Type
427 mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
428 byteArrayPrimTy :: Type
429 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
430 mkMutableArrayPrimTy :: Type -> Type -> Type
431 mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
432 mkMutableByteArrayPrimTy :: Type -> Type
433 mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
436 %************************************************************************
438 \subsection[TysPrim-mut-var]{The mutable variable type}
440 %************************************************************************
443 mutVarPrimTyCon :: TyCon
444 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName 2 PtrRep
446 mkMutVarPrimTy :: Type -> Type -> Type
447 mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
450 %************************************************************************
452 \subsection[TysPrim-synch-var]{The synchronizing variable type}
454 %************************************************************************
457 mVarPrimTyCon :: TyCon
458 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName 2 PtrRep
460 mkMVarPrimTy :: Type -> Type -> Type
461 mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
464 %************************************************************************
466 \subsection[TysPrim-stm-var]{The transactional variable type}
468 %************************************************************************
471 tVarPrimTyCon :: TyCon
472 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName 2 PtrRep
474 mkTVarPrimTy :: Type -> Type -> Type
475 mkTVarPrimTy s elt = mkTyConApp tVarPrimTyCon [s, elt]
478 %************************************************************************
480 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
482 %************************************************************************
485 stablePtrPrimTyCon :: TyCon
486 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName 1 AddrRep
488 mkStablePtrPrimTy :: Type -> Type
489 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
492 %************************************************************************
494 \subsection[TysPrim-stable-names]{The stable-name type}
496 %************************************************************************
499 stableNamePrimTyCon :: TyCon
500 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName 1 PtrRep
502 mkStableNamePrimTy :: Type -> Type
503 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
506 %************************************************************************
508 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
510 %************************************************************************
514 bcoPrimTy = mkTyConTy bcoPrimTyCon
515 bcoPrimTyCon :: TyCon
516 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep
519 %************************************************************************
521 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
523 %************************************************************************
526 weakPrimTyCon :: TyCon
527 weakPrimTyCon = pcPrimTyCon weakPrimTyConName 1 PtrRep
529 mkWeakPrimTy :: Type -> Type
530 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
533 %************************************************************************
535 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
537 %************************************************************************
539 A thread id is represented by a pointer to the TSO itself, to ensure
540 that they are always unique and we can always find the TSO for a given
541 thread id. However, this has the unfortunate consequence that a
542 ThreadId# for a given thread is treated as a root by the garbage
543 collector and can keep TSOs around for too long.
545 Hence the programmer API for thread manipulation uses a weak pointer
546 to the thread id internally.
549 threadIdPrimTy :: Type
550 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
551 threadIdPrimTyCon :: TyCon
552 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep