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 int64PrimTyCon, int64PrimTy,
42 word64PrimTyCon, word64PrimTy,
47 #include "HsVersions.h"
49 import Var ( TyVar, mkSysTyVar )
51 import PrimRep ( PrimRep(..) )
52 import TyCon ( TyCon, ArgVrcs, mkPrimTyCon )
53 import Type ( mkTyConApp, mkTyConTy, mkTyVarTys, mkTyVarTy,
54 unliftedTypeKind, liftedTypeKind, openTypeKind, mkArrowKinds
56 import Unique ( mkAlphaTyVarUnique )
61 %************************************************************************
63 \subsection{Primitive type constructors}
65 %************************************************************************
81 , mutableArrayPrimTyCon
82 , mutableByteArrayPrimTyCon
96 %************************************************************************
98 \subsection{Support code}
100 %************************************************************************
103 alphaTyVars :: [TyVar]
104 alphaTyVars = [ mkSysTyVar u liftedTypeKind
105 | u <- map mkAlphaTyVarUnique [2..] ]
107 betaTyVars = tail alphaTyVars
109 alphaTyVar, betaTyVar, gammaTyVar :: TyVar
110 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
112 alphaTys = mkTyVarTys alphaTyVars
113 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
115 -- openAlphaTyVar is prepared to be instantiated
116 -- to a lifted or unlifted type variable. It's used for the
117 -- result type for "error", so that we can have (error Int# "Help")
118 openAlphaTyVar :: TyVar
119 openAlphaTyVar = mkSysTyVar (mkAlphaTyVarUnique 1) openTypeKind
121 openAlphaTyVars :: [TyVar]
122 openAlphaTyVars = [ mkSysTyVar u openTypeKind
123 | u <- map mkAlphaTyVarUnique [2..] ]
125 openAlphaTy = mkTyVarTy openAlphaTyVar
127 vrcPos,vrcZero :: (Bool,Bool)
128 vrcPos = (True,False)
129 vrcZero = (False,False)
131 vrcsP,vrcsZ,vrcsZP :: ArgVrcs
134 vrcsZP = [vrcZero,vrcPos]
138 %************************************************************************
140 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
142 %************************************************************************
146 pcPrimTyCon :: Name -> ArgVrcs -> PrimRep -> TyCon
147 pcPrimTyCon name arg_vrcs rep
148 = mkPrimTyCon name kind arity arg_vrcs rep
150 arity = length arg_vrcs
151 kind = mkArrowKinds (take arity (repeat liftedTypeKind)) result_kind
152 result_kind = unliftedTypeKind -- all primitive types are unlifted
154 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
155 pcPrimTyCon0 name rep
156 = mkPrimTyCon name result_kind 0 [] rep
158 result_kind = unliftedTypeKind -- all primitive types are unlifted
160 charPrimTy = mkTyConTy charPrimTyCon
161 charPrimTyCon = pcPrimTyCon0 charPrimTyConName CharRep
163 intPrimTy = mkTyConTy intPrimTyCon
164 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
166 int64PrimTy = mkTyConTy int64PrimTyCon
167 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
169 wordPrimTy = mkTyConTy wordPrimTyCon
170 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
172 word64PrimTy = mkTyConTy word64PrimTyCon
173 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
175 addrPrimTy = mkTyConTy addrPrimTyCon
176 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
178 floatPrimTy = mkTyConTy floatPrimTyCon
179 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
181 doublePrimTy = mkTyConTy doublePrimTyCon
182 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
186 %************************************************************************
188 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
190 %************************************************************************
192 State# is the primitive, unlifted type of states. It has one type parameter,
198 where s is a type variable. The only purpose of the type parameter is to
199 keep different state threads separate. It is represented by nothing at all.
202 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
203 statePrimTyCon = pcPrimTyCon statePrimTyConName vrcsZ VoidRep
206 RealWorld is deeply magical. It is *primitive*, but it is not
207 *unlifted* (hence PrimPtrRep). We never manipulate values of type
208 RealWorld; it's only used in the type system, to parameterise State#.
211 realWorldTyCon = mkPrimTyCon realWorldTyConName liftedTypeKind 0 [] PrimPtrRep
212 realWorldTy = mkTyConTy realWorldTyCon
213 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
216 Note: the ``state-pairing'' types are not truly primitive, so they are
217 defined in \tr{TysWiredIn.lhs}, not here.
220 %************************************************************************
222 \subsection[TysPrim-arrays]{The primitive array types}
224 %************************************************************************
227 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName vrcsP ArrayRep
228 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName vrcsZP ArrayRep
229 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName vrcsZ ByteArrayRep
230 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName ByteArrayRep
232 mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
233 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
234 mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
235 mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
238 %************************************************************************
240 \subsection[TysPrim-mut-var]{The mutable variable type}
242 %************************************************************************
245 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName vrcsZP PrimPtrRep
247 mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
250 %************************************************************************
252 \subsection[TysPrim-synch-var]{The synchronizing variable type}
254 %************************************************************************
257 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName vrcsZP PrimPtrRep
259 mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
262 %************************************************************************
264 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
266 %************************************************************************
269 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName vrcsP StablePtrRep
271 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
274 %************************************************************************
276 \subsection[TysPrim-stable-names]{The stable-name type}
278 %************************************************************************
281 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName vrcsP StableNameRep
283 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
286 %************************************************************************
288 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
290 %************************************************************************
292 A Foreign Object is just a boxed, unlifted, Addr#. They're needed
293 because finalisers (weak pointers) can't watch Addr#s, they can only
294 watch heap-resident objects.
296 We can't use a lifted Addr# (such as Addr) because race conditions
297 could bite us. For example, if the program deconstructed the Addr
298 before passing its contents to a ccall, and a weak pointer was
299 watching the Addr, the weak pointer might deduce that the Addr was
300 dead before it really was.
303 foreignObjPrimTy = mkTyConTy foreignObjPrimTyCon
304 foreignObjPrimTyCon = pcPrimTyCon0 foreignObjPrimTyConName ForeignObjRep
307 %************************************************************************
309 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
311 %************************************************************************
314 bcoPrimTy = mkTyConTy bcoPrimTyCon
315 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName BCORep
318 %************************************************************************
320 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
322 %************************************************************************
325 weakPrimTyCon = pcPrimTyCon weakPrimTyConName vrcsP WeakPtrRep
327 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
330 %************************************************************************
332 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
334 %************************************************************************
336 A thread id is represented by a pointer to the TSO itself, to ensure
337 that they are always unique and we can always find the TSO for a given
338 thread id. However, this has the unfortunate consequence that a
339 ThreadId# for a given thread is treated as a root by the garbage
340 collector and can keep TSOs around for too long.
342 Hence the programmer API for thread manipulation uses a weak pointer
343 to the thread id internally.
346 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
347 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName ThreadIdRep
350 %************************************************************************
352 \subsection[TysPrim-PrimRep]{Making types from PrimReps}
354 %************************************************************************
356 Each of the primitive types from this module is equivalent to a
357 PrimRep (see PrimRep.lhs). The following function returns the
358 primitive TyCon for a given PrimRep.
361 primRepTyCon CharRep = charPrimTyCon
362 primRepTyCon Int8Rep = charPrimTyCon
363 primRepTyCon IntRep = intPrimTyCon
364 primRepTyCon WordRep = wordPrimTyCon
365 primRepTyCon Int64Rep = int64PrimTyCon
366 primRepTyCon Word64Rep = word64PrimTyCon
367 primRepTyCon AddrRep = addrPrimTyCon
368 primRepTyCon FloatRep = floatPrimTyCon
369 primRepTyCon DoubleRep = doublePrimTyCon
370 primRepTyCon StablePtrRep = stablePtrPrimTyCon
371 primRepTyCon ForeignObjRep = foreignObjPrimTyCon
372 primRepTyCon WeakPtrRep = weakPrimTyCon
373 primRepTyCon other = pprPanic "primRepTyCon" (ppr other)