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,
15 charPrimTyCon, charPrimTy,
16 intPrimTyCon, intPrimTy,
17 wordPrimTyCon, wordPrimTy,
18 addrPrimTyCon, addrPrimTy,
19 floatPrimTyCon, floatPrimTy,
20 doublePrimTyCon, doublePrimTy,
22 statePrimTyCon, mkStatePrimTy,
23 realWorldTyCon, realWorldTy, realWorldStatePrimTy,
25 arrayPrimTyCon, mkArrayPrimTy,
26 byteArrayPrimTyCon, byteArrayPrimTy,
27 mutableArrayPrimTyCon, mkMutableArrayPrimTy,
28 mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
29 mutVarPrimTyCon, mkMutVarPrimTy,
31 mVarPrimTyCon, mkMVarPrimTy,
32 stablePtrPrimTyCon, mkStablePtrPrimTy,
33 stableNamePrimTyCon, mkStableNamePrimTy,
34 bcoPrimTyCon, bcoPrimTy,
35 weakPrimTyCon, mkWeakPrimTy,
36 foreignObjPrimTyCon, foreignObjPrimTy,
37 threadIdPrimTyCon, threadIdPrimTy,
39 int64PrimTyCon, int64PrimTy,
40 word64PrimTyCon, word64PrimTy,
47 #include "HsVersions.h"
49 import Var ( TyVar, mkSysTyVar )
50 import Name ( mkWiredInTyConName )
51 import PrimRep ( PrimRep(..), isFollowableRep )
52 import TyCon ( mkPrimTyCon, TyCon, ArgVrcs )
54 mkTyConApp, mkTyConTy, mkTyVarTys, mkTyVarTy,
55 unboxedTypeKind, boxedTypeKind, openTypeKind, mkArrowKinds
57 import Unique ( mkAlphaTyVarUnique )
63 alphaTyVars :: [TyVar]
64 alphaTyVars = [ mkSysTyVar u boxedTypeKind
65 | u <- map mkAlphaTyVarUnique [2..] ]
67 betaTyVars = tail alphaTyVars
69 alphaTyVar, betaTyVar, gammaTyVar :: TyVar
70 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
72 alphaTys = mkTyVarTys alphaTyVars
73 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
75 -- openAlphaTyVar is prepared to be instantiated
76 -- to a boxed or unboxed type variable. It's used for the
77 -- result type for "error", so that we can have (error Int# "Help")
78 openAlphaTyVar :: TyVar
79 openAlphaTyVar = mkSysTyVar (mkAlphaTyVarUnique 1) openTypeKind
81 openAlphaTyVars :: [TyVar]
82 openAlphaTyVars = [ mkSysTyVar u openTypeKind
83 | u <- map mkAlphaTyVarUnique [2..] ]
85 openAlphaTy = mkTyVarTy openAlphaTyVar
87 vrcPos,vrcZero :: (Bool,Bool)
89 vrcZero = (False,False)
91 vrcsP,vrcsZ,vrcsZP :: ArgVrcs
94 vrcsZP = [vrcZero,vrcPos]
97 %************************************************************************
99 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
101 %************************************************************************
105 pcPrimTyCon :: Unique{-TyConKey-} -> FAST_STRING -> Int -> ArgVrcs -> PrimRep -> TyCon
106 pcPrimTyCon key str arity arg_vrcs rep
109 name = mkWiredInTyConName key pREL_GHC str the_tycon
110 the_tycon = mkPrimTyCon name kind arity arg_vrcs rep
111 kind = mkArrowKinds (take arity (repeat boxedTypeKind)) result_kind
112 result_kind | isFollowableRep rep = boxedTypeKind -- Represented by a GC-ish ptr
113 | otherwise = unboxedTypeKind -- Represented by a non-ptr
115 charPrimTy = mkTyConTy charPrimTyCon
116 charPrimTyCon = pcPrimTyCon charPrimTyConKey SLIT("Char#") 0 [] CharRep
118 intPrimTy = mkTyConTy intPrimTyCon
119 intPrimTyCon = pcPrimTyCon intPrimTyConKey SLIT("Int#") 0 [] IntRep
121 int64PrimTy = mkTyConTy int64PrimTyCon
122 int64PrimTyCon = pcPrimTyCon int64PrimTyConKey SLIT("Int64#") 0 [] Int64Rep
124 wordPrimTy = mkTyConTy wordPrimTyCon
125 wordPrimTyCon = pcPrimTyCon wordPrimTyConKey SLIT("Word#") 0 [] WordRep
127 word64PrimTy = mkTyConTy word64PrimTyCon
128 word64PrimTyCon = pcPrimTyCon word64PrimTyConKey SLIT("Word64#") 0 [] Word64Rep
130 addrPrimTy = mkTyConTy addrPrimTyCon
131 addrPrimTyCon = pcPrimTyCon addrPrimTyConKey SLIT("Addr#") 0 [] AddrRep
133 floatPrimTy = mkTyConTy floatPrimTyCon
134 floatPrimTyCon = pcPrimTyCon floatPrimTyConKey SLIT("Float#") 0 [] FloatRep
136 doublePrimTy = mkTyConTy doublePrimTyCon
137 doublePrimTyCon = pcPrimTyCon doublePrimTyConKey SLIT("Double#") 0 [] DoubleRep
141 %************************************************************************
143 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
145 %************************************************************************
147 State# is the primitive, unboxed type of states. It has one type parameter,
153 where s is a type variable. The only purpose of the type parameter is to
154 keep different state threads separate. It is represented by nothing at all.
157 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
158 statePrimTyCon = pcPrimTyCon statePrimTyConKey SLIT("State#") 1 vrcsZ VoidRep
161 @_RealWorld@ is deeply magical. It {\em is primitive}, but it
162 {\em is not unboxed} (hence PtrRep).
163 We never manipulate values of type RealWorld; it's only used in the type
164 system, to parameterise State#.
167 realWorldTy = mkTyConTy realWorldTyCon
168 realWorldTyCon = pcPrimTyCon realWorldTyConKey SLIT("RealWorld") 0 [] PrimPtrRep
169 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
172 Note: the ``state-pairing'' types are not truly primitive, so they are
173 defined in \tr{TysWiredIn.lhs}, not here.
176 %************************************************************************
178 \subsection[TysPrim-arrays]{The primitive array types}
180 %************************************************************************
183 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConKey SLIT("Array#") 1 vrcsP ArrayRep
185 byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConKey SLIT("ByteArray#") 0 [] ByteArrayRep
187 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConKey SLIT("MutableArray#")
190 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConKey SLIT("MutableByteArray#")
193 mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
194 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
195 mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
196 mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
199 %************************************************************************
201 \subsection[TysPrim-mut-var]{The mutable variable type}
203 %************************************************************************
206 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConKey SLIT("MutVar#")
209 mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
212 %************************************************************************
214 \subsection[TysPrim-synch-var]{The synchronizing variable type}
216 %************************************************************************
219 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConKey SLIT("MVar#")
222 mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
225 %************************************************************************
227 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
229 %************************************************************************
232 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConKey SLIT("StablePtr#")
235 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
238 %************************************************************************
240 \subsection[TysPrim-stable-names]{The stable-name type}
242 %************************************************************************
245 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConKey SLIT("StableName#")
246 1 vrcsP StableNameRep
248 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
251 %************************************************************************
253 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
255 %************************************************************************
257 A Foreign Object is just a boxed, unlifted, Addr#. They're needed
258 because finalisers (weak pointers) can't watch Addr#s, they can only
259 watch heap-resident objects.
261 We can't use a lifted Addr# (such as Addr) because race conditions
262 could bite us. For example, if the program deconstructed the Addr
263 before passing its contents to a ccall, and a weak pointer was
264 watching the Addr, the weak pointer might deduce that the Addr was
265 dead before it really was.
268 foreignObjPrimTy = mkTyConTy foreignObjPrimTyCon
269 foreignObjPrimTyCon = pcPrimTyCon foreignObjPrimTyConKey SLIT("ForeignObj#") 0 [] ForeignObjRep
272 %************************************************************************
274 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
276 %************************************************************************
279 bcoPrimTy = mkTyConTy bcoPrimTyCon
280 bcoPrimTyCon = pcPrimTyCon bcoPrimTyConKey SLIT("BCO#") 0 [] BCORep
283 %************************************************************************
285 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
287 %************************************************************************
290 weakPrimTyCon = pcPrimTyCon weakPrimTyConKey SLIT("Weak#") 1 vrcsP WeakPtrRep
292 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
295 %************************************************************************
297 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
299 %************************************************************************
301 A thread id is represented by a pointer to the TSO itself, to ensure
302 that they are always unique and we can always find the TSO for a given
303 thread id. However, this has the unfortunate consequence that a
304 ThreadId# for a given thread is treated as a root by the garbage
305 collector and can keep TSOs around for too long.
307 Hence the programmer API for thread manipulation uses a weak pointer
308 to the thread id internally.
311 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
312 threadIdPrimTyCon = pcPrimTyCon threadIdPrimTyConKey SLIT("ThreadId#") 0 [] ThreadIdRep
315 %************************************************************************
317 \subsection[TysPrim-PrimRep]{Making types from PrimReps}
319 %************************************************************************
321 Each of the primitive types from this module is equivalent to a
322 PrimRep (see PrimRep.lhs). The following function returns the
323 primitive TyCon for a given PrimRep.
326 primRepTyCon CharRep = charPrimTyCon
327 primRepTyCon Int8Rep = charPrimTyCon
328 primRepTyCon IntRep = intPrimTyCon
329 primRepTyCon WordRep = wordPrimTyCon
330 primRepTyCon Int64Rep = int64PrimTyCon
331 primRepTyCon Word64Rep = word64PrimTyCon
332 primRepTyCon AddrRep = addrPrimTyCon
333 primRepTyCon FloatRep = floatPrimTyCon
334 primRepTyCon DoubleRep = doublePrimTyCon
335 primRepTyCon StablePtrRep = stablePtrPrimTyCon
336 primRepTyCon ForeignObjRep = foreignObjPrimTyCon
337 primRepTyCon WeakPtrRep = weakPrimTyCon
338 primRepTyCon other = pprPanic "primRepTyCon" (ppr other)