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,
49 #include "HsVersions.h"
51 import Var ( TyVar, mkSysTyVar )
53 import PrimRep ( PrimRep(..) )
54 import TyCon ( TyCon, ArgVrcs, mkPrimTyCon )
55 import Type ( mkTyConApp, mkTyConTy, mkTyVarTys, mkTyVarTy,
56 unliftedTypeKind, liftedTypeKind, openTypeKind, mkArrowKinds
58 import Unique ( mkAlphaTyVarUnique )
63 %************************************************************************
65 \subsection{Primitive type constructors}
67 %************************************************************************
83 , mutableArrayPrimTyCon
84 , mutableByteArrayPrimTyCon
98 %************************************************************************
100 \subsection{Support code}
102 %************************************************************************
105 alphaTyVars :: [TyVar]
106 alphaTyVars = [ mkSysTyVar u liftedTypeKind
107 | u <- map mkAlphaTyVarUnique [2..] ]
109 betaTyVars = tail alphaTyVars
111 alphaTyVar, betaTyVar, gammaTyVar :: TyVar
112 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
114 alphaTys = mkTyVarTys alphaTyVars
115 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
117 -- openAlphaTyVar is prepared to be instantiated
118 -- to a lifted or unlifted type variable. It's used for the
119 -- result type for "error", so that we can have (error Int# "Help")
120 openAlphaTyVar :: TyVar
121 openAlphaTyVar = mkSysTyVar (mkAlphaTyVarUnique 1) openTypeKind
123 openAlphaTyVars :: [TyVar]
124 openAlphaTyVars = [ mkSysTyVar u openTypeKind
125 | u <- map mkAlphaTyVarUnique [2..] ]
127 openAlphaTy = mkTyVarTy openAlphaTyVar
129 vrcPos,vrcZero :: (Bool,Bool)
130 vrcPos = (True,False)
131 vrcZero = (False,False)
133 vrcsP,vrcsZ,vrcsZP :: ArgVrcs
136 vrcsZP = [vrcZero,vrcPos]
140 %************************************************************************
142 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
144 %************************************************************************
148 pcPrimTyCon :: Name -> Int -> ArgVrcs -> PrimRep -> TyCon
149 pcPrimTyCon name arity arg_vrcs rep
152 the_tycon = mkPrimTyCon name kind arity arg_vrcs rep
153 kind = mkArrowKinds (take arity (repeat liftedTypeKind)) result_kind
154 result_kind = unliftedTypeKind -- all primitive types are unlifted
156 charPrimTy = mkTyConTy charPrimTyCon
157 charPrimTyCon = pcPrimTyCon charPrimTyConName 0 [] CharRep
159 intPrimTy = mkTyConTy intPrimTyCon
160 intPrimTyCon = pcPrimTyCon intPrimTyConName 0 [] IntRep
162 int64PrimTy = mkTyConTy int64PrimTyCon
163 int64PrimTyCon = pcPrimTyCon int64PrimTyConName 0 [] Int64Rep
165 wordPrimTy = mkTyConTy wordPrimTyCon
166 wordPrimTyCon = pcPrimTyCon wordPrimTyConName 0 [] WordRep
168 word64PrimTy = mkTyConTy word64PrimTyCon
169 word64PrimTyCon = pcPrimTyCon word64PrimTyConName 0 [] Word64Rep
171 addrPrimTy = mkTyConTy addrPrimTyCon
172 addrPrimTyCon = pcPrimTyCon addrPrimTyConName 0 [] AddrRep
174 floatPrimTy = mkTyConTy floatPrimTyCon
175 floatPrimTyCon = pcPrimTyCon floatPrimTyConName 0 [] FloatRep
177 doublePrimTy = mkTyConTy doublePrimTyCon
178 doublePrimTyCon = pcPrimTyCon doublePrimTyConName 0 [] DoubleRep
182 %************************************************************************
184 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
186 %************************************************************************
188 State# is the primitive, unlifted type of states. It has one type parameter,
194 where s is a type variable. The only purpose of the type parameter is to
195 keep different state threads separate. It is represented by nothing at all.
198 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
199 statePrimTyCon = pcPrimTyCon statePrimTyConName 1 vrcsZ VoidRep
202 RealWorld is deeply magical. It is *primitive*, but it is not
203 *unlifted* (hence PrimPtrRep). We never manipulate values of type
204 RealWorld; it's only used in the type system, to parameterise State#.
207 realWorldTyCon = mkPrimTyCon realWorldTyConName liftedTypeKind 0 [] PrimPtrRep
208 realWorldTy = mkTyConTy realWorldTyCon
209 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
212 Note: the ``state-pairing'' types are not truly primitive, so they are
213 defined in \tr{TysWiredIn.lhs}, not here.
216 %************************************************************************
218 \subsection[TysPrim-arrays]{The primitive array types}
220 %************************************************************************
223 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName 1 vrcsP ArrayRep
224 byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConName 0 [] ByteArrayRep
225 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName 2 vrcsZP ArrayRep
226 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName 1 vrcsZ ByteArrayRep
228 mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
229 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
230 mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
231 mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
234 %************************************************************************
236 \subsection[TysPrim-mut-var]{The mutable variable type}
238 %************************************************************************
241 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName 2 vrcsZP PrimPtrRep
243 mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
246 %************************************************************************
248 \subsection[TysPrim-synch-var]{The synchronizing variable type}
250 %************************************************************************
253 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName 2 vrcsZP PrimPtrRep
255 mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
258 %************************************************************************
260 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
262 %************************************************************************
265 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName 1 vrcsP StablePtrRep
267 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
270 %************************************************************************
272 \subsection[TysPrim-stable-names]{The stable-name type}
274 %************************************************************************
277 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName 1 vrcsP StableNameRep
279 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
282 %************************************************************************
284 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
286 %************************************************************************
288 A Foreign Object is just a boxed, unlifted, Addr#. They're needed
289 because finalisers (weak pointers) can't watch Addr#s, they can only
290 watch heap-resident objects.
292 We can't use a lifted Addr# (such as Addr) because race conditions
293 could bite us. For example, if the program deconstructed the Addr
294 before passing its contents to a ccall, and a weak pointer was
295 watching the Addr, the weak pointer might deduce that the Addr was
296 dead before it really was.
299 foreignObjPrimTy = mkTyConTy foreignObjPrimTyCon
300 foreignObjPrimTyCon = pcPrimTyCon foreignObjPrimTyConName 0 [] ForeignObjRep
303 %************************************************************************
305 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
307 %************************************************************************
310 bcoPrimTy = mkTyConTy bcoPrimTyCon
311 bcoPrimTyCon = pcPrimTyCon bcoPrimTyConName 0 [] BCORep
314 %************************************************************************
316 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
318 %************************************************************************
321 weakPrimTyCon = pcPrimTyCon weakPrimTyConName 1 vrcsP WeakPtrRep
323 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
326 %************************************************************************
328 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
330 %************************************************************************
332 A thread id is represented by a pointer to the TSO itself, to ensure
333 that they are always unique and we can always find the TSO for a given
334 thread id. However, this has the unfortunate consequence that a
335 ThreadId# for a given thread is treated as a root by the garbage
336 collector and can keep TSOs around for too long.
338 Hence the programmer API for thread manipulation uses a weak pointer
339 to the thread id internally.
342 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
343 threadIdPrimTyCon = pcPrimTyCon threadIdPrimTyConName 0 [] ThreadIdRep
346 %************************************************************************
348 \subsection[TysPrim-PrimRep]{Making types from PrimReps}
350 %************************************************************************
352 Each of the primitive types from this module is equivalent to a
353 PrimRep (see PrimRep.lhs). The following function returns the
354 primitive TyCon for a given PrimRep.
357 primRepTyCon CharRep = charPrimTyCon
358 primRepTyCon Int8Rep = charPrimTyCon
359 primRepTyCon IntRep = intPrimTyCon
360 primRepTyCon WordRep = wordPrimTyCon
361 primRepTyCon Int64Rep = int64PrimTyCon
362 primRepTyCon Word64Rep = word64PrimTyCon
363 primRepTyCon AddrRep = addrPrimTyCon
364 primRepTyCon FloatRep = floatPrimTyCon
365 primRepTyCon DoubleRep = doublePrimTyCon
366 primRepTyCon StablePtrRep = stablePtrPrimTyCon
367 primRepTyCon ForeignObjRep = foreignObjPrimTyCon
368 primRepTyCon WeakPtrRep = weakPrimTyCon
369 primRepTyCon other = pprPanic "primRepTyCon" (ppr other)