[project @ 2001-08-14 16:29:56 by simonmar]
[ghc-hetmet.git] / ghc / compiler / prelude / TysPrim.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1998
3 %
4 \section[TysPrim]{Wired-in knowledge about primitive types}
5
6 This module tracks the ``state interface'' document, ``GHC prelude:
7 types and operations.''
8
9 \begin{code}
10 module TysPrim(
11         alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
12         alphaTy, betaTy, gammaTy, deltaTy,
13         openAlphaTy, openAlphaTyVar, openAlphaTyVars,
14
15         primTyCons,
16
17         charPrimTyCon,          charPrimTy,
18         intPrimTyCon,           intPrimTy,
19         wordPrimTyCon,          wordPrimTy,
20         addrPrimTyCon,          addrPrimTy,
21         floatPrimTyCon,         floatPrimTy,
22         doublePrimTyCon,        doublePrimTy,
23
24         statePrimTyCon,         mkStatePrimTy,
25         realWorldTyCon,         realWorldTy, realWorldStatePrimTy,
26
27         arrayPrimTyCon,                 mkArrayPrimTy, 
28         byteArrayPrimTyCon,             byteArrayPrimTy,
29         mutableArrayPrimTyCon,          mkMutableArrayPrimTy,
30         mutableByteArrayPrimTyCon,      mkMutableByteArrayPrimTy,
31         mutVarPrimTyCon,                mkMutVarPrimTy,
32
33         mVarPrimTyCon,                  mkMVarPrimTy,   
34         stablePtrPrimTyCon,             mkStablePtrPrimTy,
35         stableNamePrimTyCon,            mkStableNamePrimTy,
36         bcoPrimTyCon,                   bcoPrimTy,
37         weakPrimTyCon,                  mkWeakPrimTy,
38         foreignObjPrimTyCon,            foreignObjPrimTy,
39         threadIdPrimTyCon,              threadIdPrimTy,
40         
41         int64PrimTyCon,         int64PrimTy,
42         word64PrimTyCon,        word64PrimTy,
43
44         primRepTyCon
45   ) where
46
47 #include "HsVersions.h"
48
49 import Var              ( TyVar, mkSysTyVar )
50 import Name             ( Name )
51 import PrimRep          ( PrimRep(..) )
52 import TyCon            ( TyCon, ArgVrcs, mkPrimTyCon, mkLiftedPrimTyCon )
53 import Type             ( mkTyConApp, mkTyConTy, mkTyVarTys, mkTyVarTy,
54                           unliftedTypeKind, liftedTypeKind, openTypeKind, mkArrowKinds
55                         )
56 import Unique           ( mkAlphaTyVarUnique )
57 import PrelNames
58 import Outputable
59 \end{code}
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection{Primitive type constructors}
64 %*                                                                      *
65 %************************************************************************
66
67 \begin{code}
68 primTyCons :: [TyCon]
69 primTyCons 
70   = [ addrPrimTyCon
71     , arrayPrimTyCon
72     , byteArrayPrimTyCon
73     , charPrimTyCon
74     , doublePrimTyCon
75     , floatPrimTyCon
76     , intPrimTyCon
77     , int64PrimTyCon
78     , foreignObjPrimTyCon
79     , bcoPrimTyCon
80     , weakPrimTyCon
81     , mutableArrayPrimTyCon
82     , mutableByteArrayPrimTyCon
83     , mVarPrimTyCon
84     , mutVarPrimTyCon
85     , realWorldTyCon
86     , stablePtrPrimTyCon
87     , stableNamePrimTyCon
88     , statePrimTyCon
89     , threadIdPrimTyCon
90     , wordPrimTyCon
91     , word64PrimTyCon
92     ]
93 \end{code}
94
95
96 %************************************************************************
97 %*                                                                      *
98 \subsection{Support code}
99 %*                                                                      *
100 %************************************************************************
101
102 \begin{code}
103 alphaTyVars :: [TyVar]
104 alphaTyVars = [ mkSysTyVar u liftedTypeKind
105               | u <- map mkAlphaTyVarUnique [2..] ]
106
107 betaTyVars = tail alphaTyVars
108
109 alphaTyVar, betaTyVar, gammaTyVar :: TyVar
110 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
111
112 alphaTys = mkTyVarTys alphaTyVars
113 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
114
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
120
121 openAlphaTyVars :: [TyVar]
122 openAlphaTyVars = [ mkSysTyVar u openTypeKind
123                   | u <- map mkAlphaTyVarUnique [2..] ]
124
125 openAlphaTy = mkTyVarTy openAlphaTyVar
126
127 vrcPos,vrcZero :: (Bool,Bool)
128 vrcPos  = (True,False)
129 vrcZero = (False,False)
130
131 vrcsP,vrcsZ,vrcsZP :: ArgVrcs
132 vrcsP  = [vrcPos]
133 vrcsZ  = [vrcZero]
134 vrcsZP = [vrcZero,vrcPos]
135 \end{code}
136
137
138 %************************************************************************
139 %*                                                                      *
140 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
141 %*                                                                      *
142 %************************************************************************
143
144 \begin{code}
145 -- only used herein
146 pcPrimTyCon :: Name -> ArgVrcs -> PrimRep -> TyCon
147 pcPrimTyCon name arg_vrcs rep
148   = mkPrimTyCon name kind arity arg_vrcs rep
149   where
150     arity       = length arg_vrcs
151     kind        = mkArrowKinds (take arity (repeat liftedTypeKind)) result_kind
152     result_kind = unliftedTypeKind -- all primitive types are unlifted
153
154 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
155 pcPrimTyCon0 name rep
156   = mkPrimTyCon name result_kind 0 [] rep
157   where
158     result_kind = unliftedTypeKind -- all primitive types are unlifted
159
160 charPrimTy      = mkTyConTy charPrimTyCon
161 charPrimTyCon   = pcPrimTyCon0 charPrimTyConName CharRep
162
163 intPrimTy       = mkTyConTy intPrimTyCon
164 intPrimTyCon    = pcPrimTyCon0 intPrimTyConName IntRep
165
166 int64PrimTy     = mkTyConTy int64PrimTyCon
167 int64PrimTyCon  = pcPrimTyCon0 int64PrimTyConName Int64Rep
168
169 wordPrimTy      = mkTyConTy wordPrimTyCon
170 wordPrimTyCon   = pcPrimTyCon0 wordPrimTyConName WordRep
171
172 word64PrimTy    = mkTyConTy word64PrimTyCon
173 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
174
175 addrPrimTy      = mkTyConTy addrPrimTyCon
176 addrPrimTyCon   = pcPrimTyCon0 addrPrimTyConName AddrRep
177
178 floatPrimTy     = mkTyConTy floatPrimTyCon
179 floatPrimTyCon  = pcPrimTyCon0 floatPrimTyConName FloatRep
180
181 doublePrimTy    = mkTyConTy doublePrimTyCon
182 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
183 \end{code}
184
185
186 %************************************************************************
187 %*                                                                      *
188 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
189 %*                                                                      *
190 %************************************************************************
191
192 State# is the primitive, unlifted type of states.  It has one type parameter,
193 thus
194         State# RealWorld
195 or
196         State# s
197
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.
200
201 \begin{code}
202 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
203 statePrimTyCon   = pcPrimTyCon statePrimTyConName vrcsZ VoidRep
204 \end{code}
205
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#.
209
210 \begin{code}
211 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 [] PrimPtrRep
212 realWorldTy          = mkTyConTy realWorldTyCon
213 realWorldStatePrimTy = mkStatePrimTy realWorldTy        -- State# RealWorld
214 \end{code}
215
216 Note: the ``state-pairing'' types are not truly primitive, so they are
217 defined in \tr{TysWiredIn.lhs}, not here.
218
219
220 %************************************************************************
221 %*                                                                      *
222 \subsection[TysPrim-arrays]{The primitive array types}
223 %*                                                                      *
224 %************************************************************************
225
226 \begin{code}
227 arrayPrimTyCon            = pcPrimTyCon  arrayPrimTyConName            vrcsP  ArrayRep
228 mutableArrayPrimTyCon     = pcPrimTyCon  mutableArrayPrimTyConName     vrcsZP ArrayRep
229 mutableByteArrayPrimTyCon = pcPrimTyCon  mutableByteArrayPrimTyConName vrcsZ  ByteArrayRep
230 byteArrayPrimTyCon        = pcPrimTyCon0 byteArrayPrimTyConName               ByteArrayRep
231
232 mkArrayPrimTy elt           = mkTyConApp arrayPrimTyCon [elt]
233 byteArrayPrimTy             = mkTyConTy byteArrayPrimTyCon
234 mkMutableArrayPrimTy s elt  = mkTyConApp mutableArrayPrimTyCon [s, elt]
235 mkMutableByteArrayPrimTy s  = mkTyConApp mutableByteArrayPrimTyCon [s]
236 \end{code}
237
238 %************************************************************************
239 %*                                                                      *
240 \subsection[TysPrim-mut-var]{The mutable variable type}
241 %*                                                                      *
242 %************************************************************************
243
244 \begin{code}
245 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName vrcsZP PrimPtrRep
246
247 mkMutVarPrimTy s elt        = mkTyConApp mutVarPrimTyCon [s, elt]
248 \end{code}
249
250 %************************************************************************
251 %*                                                                      *
252 \subsection[TysPrim-synch-var]{The synchronizing variable type}
253 %*                                                                      *
254 %************************************************************************
255
256 \begin{code}
257 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName vrcsZP PrimPtrRep
258
259 mkMVarPrimTy s elt          = mkTyConApp mVarPrimTyCon [s, elt]
260 \end{code}
261
262 %************************************************************************
263 %*                                                                      *
264 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
265 %*                                                                      *
266 %************************************************************************
267
268 \begin{code}
269 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName vrcsP StablePtrRep
270
271 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
272 \end{code}
273
274 %************************************************************************
275 %*                                                                      *
276 \subsection[TysPrim-stable-names]{The stable-name type}
277 %*                                                                      *
278 %************************************************************************
279
280 \begin{code}
281 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName vrcsP StableNameRep
282
283 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
284 \end{code}
285
286 %************************************************************************
287 %*                                                                      *
288 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
289 %*                                                                      *
290 %************************************************************************
291
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.  
295
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.
301
302 \begin{code}
303 foreignObjPrimTy    = mkTyConTy foreignObjPrimTyCon
304 foreignObjPrimTyCon = pcPrimTyCon0 foreignObjPrimTyConName ForeignObjRep
305 \end{code}
306   
307 %************************************************************************
308 %*                                                                      *
309 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
310 %*                                                                      *
311 %************************************************************************
312
313 \begin{code}
314 bcoPrimTy    = mkTyConTy bcoPrimTyCon
315 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName BCORep
316 \end{code}
317   
318 %************************************************************************
319 %*                                                                      *
320 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
321 %*                                                                      *
322 %************************************************************************
323
324 \begin{code}
325 weakPrimTyCon = pcPrimTyCon weakPrimTyConName vrcsP WeakPtrRep
326
327 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
328 \end{code}
329
330 %************************************************************************
331 %*                                                                      *
332 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
333 %*                                                                      *
334 %************************************************************************
335
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.
341
342 Hence the programmer API for thread manipulation uses a weak pointer
343 to the thread id internally.
344
345 \begin{code}
346 threadIdPrimTy    = mkTyConTy threadIdPrimTyCon
347 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName ThreadIdRep
348 \end{code}
349
350 %************************************************************************
351 %*                                                                      *
352 \subsection[TysPrim-PrimRep]{Making types from PrimReps}
353 %*                                                                      *
354 %************************************************************************
355
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.
359
360 \begin{code}
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)
374 \end{code}