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,
18 charPrimTyCon, charPrimTy,
19 intPrimTyCon, intPrimTy,
20 wordPrimTyCon, wordPrimTy,
21 addrPrimTyCon, addrPrimTy,
22 floatPrimTyCon, floatPrimTy,
23 doublePrimTyCon, doublePrimTy,
25 statePrimTyCon, mkStatePrimTy,
26 realWorldTyCon, realWorldTy, realWorldStatePrimTy,
28 arrayPrimTyCon, mkArrayPrimTy,
29 byteArrayPrimTyCon, byteArrayPrimTy,
30 mutableArrayPrimTyCon, mkMutableArrayPrimTy,
31 mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
32 mutVarPrimTyCon, mkMutVarPrimTy,
34 mVarPrimTyCon, mkMVarPrimTy,
35 tVarPrimTyCon, mkTVarPrimTy,
36 stablePtrPrimTyCon, mkStablePtrPrimTy,
37 stableNamePrimTyCon, mkStableNamePrimTy,
38 bcoPrimTyCon, bcoPrimTy,
39 weakPrimTyCon, mkWeakPrimTy,
40 threadIdPrimTyCon, threadIdPrimTy,
42 int32PrimTyCon, int32PrimTy,
43 word32PrimTyCon, word32PrimTy,
45 int64PrimTyCon, int64PrimTy,
46 word64PrimTyCon, word64PrimTy,
49 anyTyCon, anyType, anyTyConOfKind, anyTypeOfKind
52 #include "HsVersions.h"
54 import Var ( TyVar, mkTyVar )
55 import Name ( Name, BuiltInSyntax(..), mkInternalName, mkWiredInName )
56 import OccName ( mkTcOcc )
57 import OccName ( mkTyVarOccFS, mkTcOccFS )
58 import TyCon ( TyCon, mkPrimTyCon, mkLiftedPrimTyCon, mkAnyTyCon )
61 import Unique ( mkAlphaTyVarUnique )
69 %************************************************************************
71 \subsection{Primitive type constructors}
73 %************************************************************************
89 , mutableArrayPrimTyCon
90 , mutableByteArrayPrimTyCon
105 mkPrimTc :: FastString -> Unique -> TyCon -> Name
106 mkPrimTc fs unique tycon
107 = mkWiredInName gHC_PRIM (mkTcOccFS fs)
109 (ATyCon tycon) -- Relevant TyCon
110 UserSyntax -- None are built-in syntax
112 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
113 charPrimTyConName = mkPrimTc (fsLit "Char#") charPrimTyConKey charPrimTyCon
114 intPrimTyConName = mkPrimTc (fsLit "Int#") intPrimTyConKey intPrimTyCon
115 int32PrimTyConName = mkPrimTc (fsLit "Int32#") int32PrimTyConKey int32PrimTyCon
116 int64PrimTyConName = mkPrimTc (fsLit "Int64#") int64PrimTyConKey int64PrimTyCon
117 wordPrimTyConName = mkPrimTc (fsLit "Word#") wordPrimTyConKey wordPrimTyCon
118 word32PrimTyConName = mkPrimTc (fsLit "Word32#") word32PrimTyConKey word32PrimTyCon
119 word64PrimTyConName = mkPrimTc (fsLit "Word64#") word64PrimTyConKey word64PrimTyCon
120 addrPrimTyConName = mkPrimTc (fsLit "Addr#") addrPrimTyConKey addrPrimTyCon
121 floatPrimTyConName = mkPrimTc (fsLit "Float#") floatPrimTyConKey floatPrimTyCon
122 doublePrimTyConName = mkPrimTc (fsLit "Double#") doublePrimTyConKey doublePrimTyCon
123 statePrimTyConName = mkPrimTc (fsLit "State#") statePrimTyConKey statePrimTyCon
124 realWorldTyConName = mkPrimTc (fsLit "RealWorld") realWorldTyConKey realWorldTyCon
125 arrayPrimTyConName = mkPrimTc (fsLit "Array#") arrayPrimTyConKey arrayPrimTyCon
126 byteArrayPrimTyConName = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey byteArrayPrimTyCon
127 mutableArrayPrimTyConName = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey mutableArrayPrimTyCon
128 mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey mutableByteArrayPrimTyCon
129 mutVarPrimTyConName = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey mutVarPrimTyCon
130 mVarPrimTyConName = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey mVarPrimTyCon
131 tVarPrimTyConName = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey tVarPrimTyCon
132 stablePtrPrimTyConName = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey stablePtrPrimTyCon
133 stableNamePrimTyConName = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey stableNamePrimTyCon
134 bcoPrimTyConName = mkPrimTc (fsLit "BCO#") bcoPrimTyConKey bcoPrimTyCon
135 weakPrimTyConName = mkPrimTc (fsLit "Weak#") weakPrimTyConKey weakPrimTyCon
136 threadIdPrimTyConName = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey threadIdPrimTyCon
139 %************************************************************************
141 \subsection{Support code}
143 %************************************************************************
145 alphaTyVars is a list of type variables for use in templates:
146 ["a", "b", ..., "z", "t1", "t2", ... ]
149 tyVarList :: Kind -> [TyVar]
150 tyVarList kind = [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u)
151 (mkTyVarOccFS (mkFastString name))
154 let name | c <= 'z' = [c]
155 | otherwise = 't':show u
156 where c = chr (u-2 + ord 'a')
159 alphaTyVars :: [TyVar]
160 alphaTyVars = tyVarList liftedTypeKind
162 betaTyVars :: [TyVar]
163 betaTyVars = tail alphaTyVars
165 alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: TyVar
166 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
169 alphaTys = mkTyVarTys alphaTyVars
170 alphaTy, betaTy, gammaTy, deltaTy :: Type
171 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
173 -- openAlphaTyVar is prepared to be instantiated
174 -- to a lifted or unlifted type variable. It's used for the
175 -- result type for "error", so that we can have (error Int# "Help")
176 openAlphaTyVars :: [TyVar]
177 openAlphaTyVar, openBetaTyVar :: TyVar
178 openAlphaTyVars@(openAlphaTyVar:openBetaTyVar:_) = tyVarList openTypeKind
180 openAlphaTy, openBetaTy :: Type
181 openAlphaTy = mkTyVarTy openAlphaTyVar
182 openBetaTy = mkTyVarTy openBetaTyVar
186 %************************************************************************
190 %************************************************************************
194 The type constructor Any::* has these properties
196 * It is defined in module GHC.Prim, and exported so that it is
197 available to users. For this reason it's treated like any other
199 - has a fixed unique, anyTyConKey,
200 - lives in the global name cache
201 - built with TyCon.PrimTyCon
203 * It is lifted, and hence represented by a pointer
205 * It is inhabited by at least one value, namely bottom
207 * You can unsafely coerce any lifted type to Ayny, and back.
209 * It does not claim to be a *data* type, and that's important for
210 the code generator, because the code gen may *enter* a data value
211 but never enters a function value.
213 * It is used to instantiate otherwise un-constrained type variables of kind *
214 For example length Any []
215 See Note [Strangely-kinded void TyCons]
217 In addition, we have a potentially-infinite family of types, one for
218 each kind /other than/ *, needed to instantiate otherwise
219 un-constrained type variables of kinds other than *. This is a bit
220 like tuples; there is a potentially-infinite family. They have slightly
221 different characteristics to Any::*:
223 * They are built with TyCon.AnyTyCon
224 * They have non-user-writable names like "Any(*->*)"
225 * They are not exported by GHC.Prim
226 * They are uninhabited (of course; not kind *)
227 * They have a unique derived from their OccName (see Note [Uniques of Any])
228 * Their Names do not live in the global name cache
230 Note [Uniques of Any]
231 ~~~~~~~~~~~~~~~~~~~~~
232 Although Any(*->*), say, doesn't have a binding site, it still needs
233 to have a Unique. Unlike tuples (which are also an infinite family)
234 there is no convenient way to index them, so we use the Unique from
235 their OccName instead. That should be unique! (But in principle we
236 must take care: it does not include the module/package.)
238 Note [Strangely-kinded void TyCons]
239 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
240 See Trac #959 for more examples
242 When the type checker finds a type variable with no binding, which
243 means it can be instantiated with an arbitrary type, it usually
244 instantiates it to Void. Eg.
250 But in really obscure programs, the type variable might have a kind
251 other than *, so we need to invent a suitably-kinded type.
255 Any(*->*) for kind *->*
260 anyTyConName = mkPrimTc (fsLit "Any") anyTyConKey anyTyCon
263 anyTyCon = mkLiftedPrimTyCon anyTyConName liftedTypeKind 0 PtrRep
266 anyType = mkTyConApp anyTyCon []
268 anyTypeOfKind :: Kind -> Type
270 | isLiftedTypeKind kind = anyType
271 | otherwise = mkTyConApp (mk_any_tycon kind) []
273 anyTyConOfKind :: Kind -> TyCon
275 | isLiftedTypeKind kind = anyTyCon
276 | otherwise = mk_any_tycon kind
278 mk_any_tycon :: Kind -> TyCon
279 mk_any_tycon kind -- Kind other than *
282 -- Derive the name from the kind, thus:
283 -- Any(*->*), Any(*->*->*)
284 -- These are names that can't be written by the user,
285 -- and are not allocated in the global name cache
286 str = "Any" ++ showSDoc (pprParendKind kind)
289 uniq = getUnique occ -- See Note [Uniques of Any]
290 name = mkWiredInName gHC_PRIM occ uniq (ATyCon tycon) UserSyntax
291 tycon = mkAnyTyCon name kind
295 %************************************************************************
297 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
299 %************************************************************************
303 pcPrimTyCon :: Name -> Int -> PrimRep -> TyCon
304 pcPrimTyCon name arity rep
305 = mkPrimTyCon name kind arity rep
307 kind = mkArrowKinds (replicate arity liftedTypeKind) result_kind
308 result_kind = unliftedTypeKind
310 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
311 pcPrimTyCon0 name rep
312 = mkPrimTyCon name result_kind 0 rep
314 result_kind = unliftedTypeKind
317 charPrimTy = mkTyConTy charPrimTyCon
318 charPrimTyCon :: TyCon
319 charPrimTyCon = pcPrimTyCon0 charPrimTyConName WordRep
322 intPrimTy = mkTyConTy intPrimTyCon
323 intPrimTyCon :: TyCon
324 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
327 int32PrimTy = mkTyConTy int32PrimTyCon
328 int32PrimTyCon :: TyCon
329 int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName IntRep
332 int64PrimTy = mkTyConTy int64PrimTyCon
333 int64PrimTyCon :: TyCon
334 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
337 wordPrimTy = mkTyConTy wordPrimTyCon
338 wordPrimTyCon :: TyCon
339 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
342 word32PrimTy = mkTyConTy word32PrimTyCon
343 word32PrimTyCon :: TyCon
344 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName WordRep
347 word64PrimTy = mkTyConTy word64PrimTyCon
348 word64PrimTyCon :: TyCon
349 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
352 addrPrimTy = mkTyConTy addrPrimTyCon
353 addrPrimTyCon :: TyCon
354 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
357 floatPrimTy = mkTyConTy floatPrimTyCon
358 floatPrimTyCon :: TyCon
359 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
362 doublePrimTy = mkTyConTy doublePrimTyCon
363 doublePrimTyCon :: TyCon
364 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
368 %************************************************************************
370 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
372 %************************************************************************
374 State# is the primitive, unlifted type of states. It has one type parameter,
380 where s is a type variable. The only purpose of the type parameter is to
381 keep different state threads separate. It is represented by nothing at all.
384 mkStatePrimTy :: Type -> Type
385 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
386 statePrimTyCon :: TyCon
387 statePrimTyCon = pcPrimTyCon statePrimTyConName 1 VoidRep
390 RealWorld is deeply magical. It is *primitive*, but it is not
391 *unlifted* (hence ptrArg). We never manipulate values of type
392 RealWorld; it's only used in the type system, to parameterise State#.
395 realWorldTyCon :: TyCon
396 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 PtrRep
398 realWorldTy = mkTyConTy realWorldTyCon
399 realWorldStatePrimTy :: Type
400 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
403 Note: the ``state-pairing'' types are not truly primitive, so they are
404 defined in \tr{TysWiredIn.lhs}, not here.
407 %************************************************************************
409 \subsection[TysPrim-arrays]{The primitive array types}
411 %************************************************************************
414 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
415 byteArrayPrimTyCon :: TyCon
416 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName 1 PtrRep
417 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName 2 PtrRep
418 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName 1 PtrRep
419 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName PtrRep
421 mkArrayPrimTy :: Type -> Type
422 mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
423 byteArrayPrimTy :: Type
424 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
425 mkMutableArrayPrimTy :: Type -> Type -> Type
426 mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
427 mkMutableByteArrayPrimTy :: Type -> Type
428 mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
431 %************************************************************************
433 \subsection[TysPrim-mut-var]{The mutable variable type}
435 %************************************************************************
438 mutVarPrimTyCon :: TyCon
439 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName 2 PtrRep
441 mkMutVarPrimTy :: Type -> Type -> Type
442 mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
445 %************************************************************************
447 \subsection[TysPrim-synch-var]{The synchronizing variable type}
449 %************************************************************************
452 mVarPrimTyCon :: TyCon
453 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName 2 PtrRep
455 mkMVarPrimTy :: Type -> Type -> Type
456 mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
459 %************************************************************************
461 \subsection[TysPrim-stm-var]{The transactional variable type}
463 %************************************************************************
466 tVarPrimTyCon :: TyCon
467 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName 2 PtrRep
469 mkTVarPrimTy :: Type -> Type -> Type
470 mkTVarPrimTy s elt = mkTyConApp tVarPrimTyCon [s, elt]
473 %************************************************************************
475 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
477 %************************************************************************
480 stablePtrPrimTyCon :: TyCon
481 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName 1 AddrRep
483 mkStablePtrPrimTy :: Type -> Type
484 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
487 %************************************************************************
489 \subsection[TysPrim-stable-names]{The stable-name type}
491 %************************************************************************
494 stableNamePrimTyCon :: TyCon
495 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName 1 PtrRep
497 mkStableNamePrimTy :: Type -> Type
498 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
501 %************************************************************************
503 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
505 %************************************************************************
509 bcoPrimTy = mkTyConTy bcoPrimTyCon
510 bcoPrimTyCon :: TyCon
511 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep
514 %************************************************************************
516 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
518 %************************************************************************
521 weakPrimTyCon :: TyCon
522 weakPrimTyCon = pcPrimTyCon weakPrimTyConName 1 PtrRep
524 mkWeakPrimTy :: Type -> Type
525 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
528 %************************************************************************
530 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
532 %************************************************************************
534 A thread id is represented by a pointer to the TSO itself, to ensure
535 that they are always unique and we can always find the TSO for a given
536 thread id. However, this has the unfortunate consequence that a
537 ThreadId# for a given thread is treated as a root by the garbage
538 collector and can keep TSOs around for too long.
540 Hence the programmer API for thread manipulation uses a weak pointer
541 to the thread id internally.
544 threadIdPrimTy :: Type
545 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
546 threadIdPrimTyCon :: TyCon
547 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep