2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[TysPrim]{Wired-in knowledge about primitive types}
6 This module tracks the ``state interface'' document, ``GHC prelude:
7 types and operations.''
11 alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
12 alphaTy, betaTy, gammaTy, deltaTy,
13 openAlphaTy, openAlphaTyVar, openAlphaTyVars,
17 charPrimTyCon, charPrimTy,
18 intPrimTyCon, intPrimTy,
19 wordPrimTyCon, wordPrimTy,
20 addrPrimTyCon, addrPrimTy,
21 floatPrimTyCon, floatPrimTy,
22 doublePrimTyCon, doublePrimTy,
24 statePrimTyCon, mkStatePrimTy,
25 realWorldTyCon, realWorldTy, realWorldStatePrimTy,
27 arrayPrimTyCon, mkArrayPrimTy,
28 byteArrayPrimTyCon, byteArrayPrimTy,
29 mutableArrayPrimTyCon, mkMutableArrayPrimTy,
30 mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
31 mutVarPrimTyCon, mkMutVarPrimTy,
33 mVarPrimTyCon, mkMVarPrimTy,
34 stablePtrPrimTyCon, mkStablePtrPrimTy,
35 stableNamePrimTyCon, mkStableNamePrimTy,
36 bcoPrimTyCon, bcoPrimTy,
37 weakPrimTyCon, mkWeakPrimTy,
38 foreignObjPrimTyCon, foreignObjPrimTy,
39 threadIdPrimTyCon, threadIdPrimTy,
41 int32PrimTyCon, int32PrimTy,
42 word32PrimTyCon, word32PrimTy,
44 int64PrimTyCon, int64PrimTy,
45 word64PrimTyCon, word64PrimTy
48 #include "HsVersions.h"
50 import Var ( TyVar, mkTyVar )
51 import Name ( Name, mkInternalName )
52 import OccName ( mkVarOcc )
53 import PrimRep ( PrimRep(..) )
54 import TyCon ( TyCon, ArgVrcs, mkPrimTyCon, mkLiftedPrimTyCon )
55 import Type ( mkTyConApp, mkTyConTy, mkTyVarTys, mkTyVarTy,
56 unliftedTypeKind, liftedTypeKind, openTypeKind,
59 import SrcLoc ( noSrcLoc )
60 import Unique ( mkAlphaTyVarUnique )
62 import FastString ( mkFastString )
65 import Char ( ord, chr )
68 %************************************************************************
70 \subsection{Primitive type constructors}
72 %************************************************************************
89 , mutableArrayPrimTyCon
90 , mutableByteArrayPrimTyCon
105 %************************************************************************
107 \subsection{Support code}
109 %************************************************************************
111 alphaTyVars is a list of type variables for use in templates:
112 ["a", "b", ..., "z", "t1", "t2", ... ]
115 tyVarList :: Kind -> [TyVar]
116 tyVarList kind = [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u)
117 (mkVarOcc (mkFastString name))
120 let name | c <= 'z' = [c]
121 | otherwise = 't':show u
122 where c = chr (u-2 + ord 'a')
125 alphaTyVars :: [TyVar]
126 alphaTyVars = tyVarList liftedTypeKind
128 betaTyVars = tail alphaTyVars
130 alphaTyVar, betaTyVar, gammaTyVar :: TyVar
131 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
133 alphaTys = mkTyVarTys alphaTyVars
134 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
136 -- openAlphaTyVar is prepared to be instantiated
137 -- to a lifted or unlifted type variable. It's used for the
138 -- result type for "error", so that we can have (error Int# "Help")
139 openAlphaTyVars :: [TyVar]
140 openAlphaTyVars@(openAlphaTyVar:_) = tyVarList openTypeKind
142 openAlphaTy = mkTyVarTy openAlphaTyVar
144 vrcPos,vrcZero :: (Bool,Bool)
145 vrcPos = (True,False)
146 vrcZero = (False,False)
148 vrcsP,vrcsZ,vrcsZP :: ArgVrcs
151 vrcsZP = [vrcZero,vrcPos]
155 %************************************************************************
157 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
159 %************************************************************************
163 pcPrimTyCon :: Name -> ArgVrcs -> PrimRep -> TyCon
164 pcPrimTyCon name arg_vrcs rep
165 = mkPrimTyCon name kind arity arg_vrcs rep
167 arity = length arg_vrcs
168 kind = mkArrowKinds (replicate arity liftedTypeKind) result_kind
169 result_kind = unliftedTypeKind -- all primitive types are unlifted
171 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
172 pcPrimTyCon0 name rep
173 = mkPrimTyCon name result_kind 0 [] rep
175 result_kind = unliftedTypeKind -- all primitive types are unlifted
177 charPrimTy = mkTyConTy charPrimTyCon
178 charPrimTyCon = pcPrimTyCon0 charPrimTyConName CharRep
180 intPrimTy = mkTyConTy intPrimTyCon
181 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
183 int32PrimTy = mkTyConTy int32PrimTyCon
184 int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName Int32Rep
186 int64PrimTy = mkTyConTy int64PrimTyCon
187 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
189 wordPrimTy = mkTyConTy wordPrimTyCon
190 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
192 word32PrimTy = mkTyConTy word32PrimTyCon
193 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName Word32Rep
195 word64PrimTy = mkTyConTy word64PrimTyCon
196 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
198 addrPrimTy = mkTyConTy addrPrimTyCon
199 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
201 floatPrimTy = mkTyConTy floatPrimTyCon
202 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
204 doublePrimTy = mkTyConTy doublePrimTyCon
205 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
209 %************************************************************************
211 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
213 %************************************************************************
215 State# is the primitive, unlifted type of states. It has one type parameter,
221 where s is a type variable. The only purpose of the type parameter is to
222 keep different state threads separate. It is represented by nothing at all.
225 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
226 statePrimTyCon = pcPrimTyCon statePrimTyConName vrcsZ VoidRep
229 RealWorld is deeply magical. It is *primitive*, but it is not
230 *unlifted* (hence PrimPtrRep). We never manipulate values of type
231 RealWorld; it's only used in the type system, to parameterise State#.
234 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 [] PrimPtrRep
235 realWorldTy = mkTyConTy realWorldTyCon
236 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
239 Note: the ``state-pairing'' types are not truly primitive, so they are
240 defined in \tr{TysWiredIn.lhs}, not here.
243 %************************************************************************
245 \subsection[TysPrim-arrays]{The primitive array types}
247 %************************************************************************
250 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName vrcsP ArrayRep
251 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName vrcsZP ArrayRep
252 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName vrcsZ ByteArrayRep
253 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName ByteArrayRep
255 mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
256 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
257 mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
258 mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
261 %************************************************************************
263 \subsection[TysPrim-mut-var]{The mutable variable type}
265 %************************************************************************
268 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName vrcsZP PrimPtrRep
270 mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
273 %************************************************************************
275 \subsection[TysPrim-synch-var]{The synchronizing variable type}
277 %************************************************************************
280 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName vrcsZP PrimPtrRep
282 mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
285 %************************************************************************
287 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
289 %************************************************************************
292 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName vrcsP StablePtrRep
294 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
297 %************************************************************************
299 \subsection[TysPrim-stable-names]{The stable-name type}
301 %************************************************************************
304 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName vrcsP StableNameRep
306 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
309 %************************************************************************
311 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
313 %************************************************************************
315 A Foreign Object is just a boxed, unlifted, Addr#. They're needed
316 because finalisers (weak pointers) can't watch Addr#s, they can only
317 watch heap-resident objects.
319 We can't use a lifted Addr# (such as Addr) because race conditions
320 could bite us. For example, if the program deconstructed the Addr
321 before passing its contents to a ccall, and a weak pointer was
322 watching the Addr, the weak pointer might deduce that the Addr was
323 dead before it really was.
326 foreignObjPrimTy = mkTyConTy foreignObjPrimTyCon
327 foreignObjPrimTyCon = pcPrimTyCon0 foreignObjPrimTyConName ForeignObjRep
330 %************************************************************************
332 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
334 %************************************************************************
337 bcoPrimTy = mkTyConTy bcoPrimTyCon
338 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName BCORep
341 %************************************************************************
343 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
345 %************************************************************************
348 weakPrimTyCon = pcPrimTyCon weakPrimTyConName vrcsP WeakPtrRep
350 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
353 %************************************************************************
355 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
357 %************************************************************************
359 A thread id is represented by a pointer to the TSO itself, to ensure
360 that they are always unique and we can always find the TSO for a given
361 thread id. However, this has the unfortunate consequence that a
362 ThreadId# for a given thread is treated as a root by the garbage
363 collector and can keep TSOs around for too long.
365 Hence the programmer API for thread manipulation uses a weak pointer
366 to the thread id internally.
369 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
370 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName ThreadIdRep