4c70bcb7452d30d018b7fe950cf43d10bbac81c8
[ghc-hetmet.git] / compiler / prelude / TysPrim.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1998
3 %
4
5      
6 \section[TysPrim]{Wired-in knowledge about primitive types}
7
8 \begin{code}
9 -- | This module defines TyCons that can't be expressed in Haskell. 
10 --   They are all, therefore, wired-in TyCons.  C.f module TysWiredIn
11 module TysPrim(
12         alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
13         alphaTy, betaTy, gammaTy, deltaTy, ecTyVars,
14         openAlphaTy, openBetaTy, openAlphaTyVar, openBetaTyVar, openAlphaTyVars,
15         argAlphaTy, argAlphaTyVar, argBetaTy, argBetaTyVar,
16
17         -- Kind constructors...
18         tySuperKindTyCon, tySuperKind,
19         liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
20         argTypeKindTyCon, ubxTupleKindTyCon,
21
22         tySuperKindTyConName, liftedTypeKindTyConName,
23         openTypeKindTyConName, unliftedTypeKindTyConName,
24         ubxTupleKindTyConName, argTypeKindTyConName,
25
26         -- Kinds
27         liftedTypeKind, unliftedTypeKind, openTypeKind,
28         argTypeKind, ubxTupleKind,
29         mkArrowKind, mkArrowKinds, isCoercionKind,
30
31         funTyCon, funTyConName,
32         primTyCons,
33
34         charPrimTyCon,          charPrimTy,
35         intPrimTyCon,           intPrimTy,
36         wordPrimTyCon,          wordPrimTy,
37         addrPrimTyCon,          addrPrimTy,
38         floatPrimTyCon,         floatPrimTy,
39         doublePrimTyCon,        doublePrimTy,
40
41         statePrimTyCon,         mkStatePrimTy,
42         realWorldTyCon,         realWorldTy, realWorldStatePrimTy,
43
44         arrayPrimTyCon,                 mkArrayPrimTy, 
45         byteArrayPrimTyCon,             byteArrayPrimTy,
46         mutableArrayPrimTyCon,          mkMutableArrayPrimTy,
47         mutableByteArrayPrimTyCon,      mkMutableByteArrayPrimTy,
48         mutVarPrimTyCon,                mkMutVarPrimTy,
49
50         mVarPrimTyCon,                  mkMVarPrimTy,   
51         tVarPrimTyCon,                  mkTVarPrimTy,
52         stablePtrPrimTyCon,             mkStablePtrPrimTy,
53         stableNamePrimTyCon,            mkStableNamePrimTy,
54         bcoPrimTyCon,                   bcoPrimTy,
55         weakPrimTyCon,                  mkWeakPrimTy,
56         threadIdPrimTyCon,              threadIdPrimTy,
57         
58         int32PrimTyCon,         int32PrimTy,
59         word32PrimTyCon,        word32PrimTy,
60
61         int64PrimTyCon,         int64PrimTy,
62         word64PrimTyCon,        word64PrimTy,
63
64         eqPredPrimTyCon,            -- ty1 ~ ty2
65
66         -- * Any
67         anyTyCon, anyTyConOfKind, anyTypeOfKind
68   ) where
69
70 #include "HsVersions.h"
71
72 import Var              ( TyVar, mkTyVar )
73 import Name             ( Name, BuiltInSyntax(..), mkInternalName, mkWiredInName )
74 import OccName          ( mkTcOcc,mkTyVarOccFS, mkTcOccFS )
75 import TyCon
76 import TypeRep
77 import Type
78 import Coercion
79 import SrcLoc
80 import Unique           ( mkAlphaTyVarUnique )
81 import PrelNames
82 import FastString
83 import Outputable
84
85 import Data.Char
86 \end{code}
87
88 %************************************************************************
89 %*                                                                      *
90 \subsection{Primitive type constructors}
91 %*                                                                      *
92 %************************************************************************
93
94 \begin{code}
95 primTyCons :: [TyCon]
96 primTyCons 
97   = [ addrPrimTyCon
98     , arrayPrimTyCon
99     , byteArrayPrimTyCon
100     , charPrimTyCon
101     , doublePrimTyCon
102     , floatPrimTyCon
103     , intPrimTyCon
104     , int32PrimTyCon
105     , int64PrimTyCon
106     , bcoPrimTyCon
107     , weakPrimTyCon
108     , mutableArrayPrimTyCon
109     , mutableByteArrayPrimTyCon
110     , mVarPrimTyCon
111     , tVarPrimTyCon
112     , mutVarPrimTyCon
113     , realWorldTyCon
114     , stablePtrPrimTyCon
115     , stableNamePrimTyCon
116     , statePrimTyCon
117     , threadIdPrimTyCon
118     , wordPrimTyCon
119     , word32PrimTyCon
120     , word64PrimTyCon
121     , anyTyCon
122     , eqPredPrimTyCon
123     ]
124
125 mkPrimTc :: FastString -> Unique -> TyCon -> Name
126 mkPrimTc fs unique tycon
127   = mkWiredInName gHC_PRIM (mkTcOccFS fs) 
128                   unique
129                   (ATyCon tycon)        -- Relevant TyCon
130                   UserSyntax            -- None are built-in syntax
131
132 charPrimTyConName, intPrimTyConName, int32PrimTyConName, int64PrimTyConName, wordPrimTyConName, word32PrimTyConName, word64PrimTyConName, addrPrimTyConName, floatPrimTyConName, doublePrimTyConName, statePrimTyConName, realWorldTyConName, arrayPrimTyConName, byteArrayPrimTyConName, mutableArrayPrimTyConName, mutableByteArrayPrimTyConName, mutVarPrimTyConName, mVarPrimTyConName, tVarPrimTyConName, stablePtrPrimTyConName, stableNamePrimTyConName, bcoPrimTyConName, weakPrimTyConName, threadIdPrimTyConName, eqPredPrimTyConName :: Name
133 charPrimTyConName             = mkPrimTc (fsLit "Char#") charPrimTyConKey charPrimTyCon
134 intPrimTyConName              = mkPrimTc (fsLit "Int#") intPrimTyConKey  intPrimTyCon
135 int32PrimTyConName            = mkPrimTc (fsLit "Int32#") int32PrimTyConKey int32PrimTyCon
136 int64PrimTyConName            = mkPrimTc (fsLit "Int64#") int64PrimTyConKey int64PrimTyCon
137 wordPrimTyConName             = mkPrimTc (fsLit "Word#") wordPrimTyConKey wordPrimTyCon
138 word32PrimTyConName           = mkPrimTc (fsLit "Word32#") word32PrimTyConKey word32PrimTyCon
139 word64PrimTyConName           = mkPrimTc (fsLit "Word64#") word64PrimTyConKey word64PrimTyCon
140 addrPrimTyConName             = mkPrimTc (fsLit "Addr#") addrPrimTyConKey addrPrimTyCon
141 floatPrimTyConName            = mkPrimTc (fsLit "Float#") floatPrimTyConKey floatPrimTyCon
142 doublePrimTyConName           = mkPrimTc (fsLit "Double#") doublePrimTyConKey doublePrimTyCon
143 statePrimTyConName            = mkPrimTc (fsLit "State#") statePrimTyConKey statePrimTyCon
144 eqPredPrimTyConName           = mkPrimTc (fsLit "~") eqPredPrimTyConKey eqPredPrimTyCon
145 realWorldTyConName            = mkPrimTc (fsLit "RealWorld") realWorldTyConKey realWorldTyCon
146 arrayPrimTyConName            = mkPrimTc (fsLit "Array#") arrayPrimTyConKey arrayPrimTyCon
147 byteArrayPrimTyConName        = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey byteArrayPrimTyCon
148 mutableArrayPrimTyConName     = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey mutableArrayPrimTyCon
149 mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey mutableByteArrayPrimTyCon
150 mutVarPrimTyConName           = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey mutVarPrimTyCon
151 mVarPrimTyConName             = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey mVarPrimTyCon
152 tVarPrimTyConName             = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey tVarPrimTyCon
153 stablePtrPrimTyConName        = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey stablePtrPrimTyCon
154 stableNamePrimTyConName       = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey stableNamePrimTyCon
155 bcoPrimTyConName              = mkPrimTc (fsLit "BCO#") bcoPrimTyConKey bcoPrimTyCon
156 weakPrimTyConName             = mkPrimTc (fsLit "Weak#") weakPrimTyConKey weakPrimTyCon
157 threadIdPrimTyConName         = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey threadIdPrimTyCon
158 \end{code}
159
160 %************************************************************************
161 %*                                                                      *
162 \subsection{Support code}
163 %*                                                                      *
164 %************************************************************************
165
166 alphaTyVars is a list of type variables for use in templates: 
167         ["a", "b", ..., "z", "t1", "t2", ... ]
168
169 \begin{code}
170 tyVarList :: Kind -> [TyVar]
171 tyVarList kind = [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u) 
172                                 (mkTyVarOccFS (mkFastString name))
173                                 noSrcSpan) kind
174                  | u <- [2..],
175                    let name | c <= 'z'  = [c]
176                             | otherwise = 't':show u
177                             where c = chr (u-2 + ord 'a')
178                  ]
179
180 ecTyVars :: [TyVar]
181 ecTyVars = tyVarList ecKind
182
183 alphaTyVars :: [TyVar]
184 alphaTyVars = tyVarList liftedTypeKind
185
186 betaTyVars :: [TyVar]
187 betaTyVars = tail alphaTyVars
188
189 alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: TyVar
190 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
191
192 alphaTys :: [Type]
193 alphaTys = mkTyVarTys alphaTyVars
194 alphaTy, betaTy, gammaTy, deltaTy :: Type
195 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
196
197         -- openAlphaTyVar is prepared to be instantiated
198         -- to a lifted or unlifted type variable.  It's used for the 
199         -- result type for "error", so that we can have (error Int# "Help")
200 openAlphaTyVars :: [TyVar]
201 openAlphaTyVar, openBetaTyVar :: TyVar
202 openAlphaTyVars@(openAlphaTyVar:openBetaTyVar:_) = tyVarList openTypeKind
203
204 openAlphaTy, openBetaTy :: Type
205 openAlphaTy = mkTyVarTy openAlphaTyVar
206 openBetaTy  = mkTyVarTy openBetaTyVar
207
208 argAlphaTyVar, argBetaTyVar :: TyVar
209 (argAlphaTyVar : argBetaTyVar : _) = tyVarList argTypeKind
210 argAlphaTy, argBetaTy :: Type
211 argAlphaTy = mkTyVarTy argAlphaTyVar
212 argBetaTy  = mkTyVarTy argBetaTyVar
213 \end{code}
214
215
216 %************************************************************************
217 %*                                                                      *
218                 FunTyCon
219 %*                                                                      *
220 %************************************************************************
221
222 \begin{code}
223 funTyConName :: Name
224 funTyConName = mkPrimTyConName (fsLit "(->)") funTyConKey funTyCon
225
226 funTyCon :: TyCon
227 funTyCon = mkFunTyCon funTyConName (mkArrowKinds [argTypeKind, openTypeKind] liftedTypeKind)
228         -- You might think that (->) should have type (?? -> ? -> *), and you'd be right
229         -- But if we do that we get kind errors when saying
230         --      instance Control.Arrow (->)
231         -- becuase the expected kind is (*->*->*).  The trouble is that the
232         -- expected/actual stuff in the unifier does not go contra-variant, whereas
233         -- the kind sub-typing does.  Sigh.  It really only matters if you use (->) in
234         -- a prefix way, thus:  (->) Int# Int#.  And this is unusual.
235         -- because they are never in scope in the source
236 \end{code}
237
238
239 %************************************************************************
240 %*                                                                      *
241                 Kinds
242 %*                                                                      *
243 %************************************************************************
244
245 \begin{code}
246 -- | See "Type#kind_subtyping" for details of the distinction between the 'Kind' 'TyCon's
247 tySuperKindTyCon, liftedTypeKindTyCon,
248       openTypeKindTyCon, unliftedTypeKindTyCon,
249       ubxTupleKindTyCon, argTypeKindTyCon
250    :: TyCon
251 tySuperKindTyConName, liftedTypeKindTyConName,
252       openTypeKindTyConName, unliftedTypeKindTyConName,
253       ubxTupleKindTyConName, argTypeKindTyConName
254    :: Name
255
256 tySuperKindTyCon      = mkSuperKindTyCon tySuperKindTyConName
257 liftedTypeKindTyCon   = mkKindTyCon liftedTypeKindTyConName   tySuperKind
258 openTypeKindTyCon     = mkKindTyCon openTypeKindTyConName     tySuperKind
259 unliftedTypeKindTyCon = mkKindTyCon unliftedTypeKindTyConName tySuperKind
260 ubxTupleKindTyCon     = mkKindTyCon ubxTupleKindTyConName     tySuperKind
261 argTypeKindTyCon      = mkKindTyCon argTypeKindTyConName      tySuperKind
262
263 --------------------------
264 -- ... and now their names
265
266 tySuperKindTyConName      = mkPrimTyConName (fsLit "BOX") tySuperKindTyConKey tySuperKindTyCon
267 liftedTypeKindTyConName   = mkPrimTyConName (fsLit "*") liftedTypeKindTyConKey liftedTypeKindTyCon
268 openTypeKindTyConName     = mkPrimTyConName (fsLit "?") openTypeKindTyConKey openTypeKindTyCon
269 unliftedTypeKindTyConName = mkPrimTyConName (fsLit "#") unliftedTypeKindTyConKey unliftedTypeKindTyCon
270 ubxTupleKindTyConName     = mkPrimTyConName (fsLit "(#)") ubxTupleKindTyConKey ubxTupleKindTyCon
271 argTypeKindTyConName      = mkPrimTyConName (fsLit "??") argTypeKindTyConKey argTypeKindTyCon
272
273 mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
274 mkPrimTyConName occ key tycon = mkWiredInName gHC_PRIM (mkTcOccFS occ) 
275                                               key 
276                                               (ATyCon tycon)
277                                               BuiltInSyntax
278         -- All of the super kinds and kinds are defined in Prim and use BuiltInSyntax,
279         -- because they are never in scope in the source
280 \end{code}
281
282
283 \begin{code}
284 kindTyConType :: TyCon -> Type
285 kindTyConType kind = TyConApp kind []
286
287 -- | See "Type#kind_subtyping" for details of the distinction between these 'Kind's
288 liftedTypeKind, unliftedTypeKind, openTypeKind, argTypeKind, ubxTupleKind :: Kind
289
290 liftedTypeKind   = kindTyConType liftedTypeKindTyCon
291 unliftedTypeKind = kindTyConType unliftedTypeKindTyCon
292 openTypeKind     = kindTyConType openTypeKindTyCon
293 argTypeKind      = kindTyConType argTypeKindTyCon
294 ubxTupleKind     = kindTyConType ubxTupleKindTyCon
295
296 -- | Given two kinds @k1@ and @k2@, creates the 'Kind' @k1 -> k2@
297 mkArrowKind :: Kind -> Kind -> Kind
298 mkArrowKind k1 k2 = FunTy k1 k2
299
300 -- | Iterated application of 'mkArrowKind'
301 mkArrowKinds :: [Kind] -> Kind -> Kind
302 mkArrowKinds arg_kinds result_kind = foldr mkArrowKind result_kind arg_kinds
303
304 tySuperKind :: SuperKind
305 tySuperKind = kindTyConType tySuperKindTyCon 
306 \end{code}
307
308 %************************************************************************
309 %*                                                                      *
310 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
311 %*                                                                      *
312 %************************************************************************
313
314 \begin{code}
315 -- only used herein
316 pcPrimTyCon :: Name -> Int -> PrimRep -> TyCon
317 pcPrimTyCon name arity rep
318   = mkPrimTyCon name kind arity rep
319   where
320     kind        = mkArrowKinds (replicate arity liftedTypeKind) result_kind
321     result_kind = unliftedTypeKind
322
323 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
324 pcPrimTyCon0 name rep
325   = mkPrimTyCon name result_kind 0 rep
326   where
327     result_kind = unliftedTypeKind
328
329 charPrimTy :: Type
330 charPrimTy      = mkTyConTy charPrimTyCon
331 charPrimTyCon :: TyCon
332 charPrimTyCon   = pcPrimTyCon0 charPrimTyConName WordRep
333
334 intPrimTy :: Type
335 intPrimTy       = mkTyConTy intPrimTyCon
336 intPrimTyCon :: TyCon
337 intPrimTyCon    = pcPrimTyCon0 intPrimTyConName IntRep
338
339 int32PrimTy :: Type
340 int32PrimTy     = mkTyConTy int32PrimTyCon
341 int32PrimTyCon :: TyCon
342 int32PrimTyCon  = pcPrimTyCon0 int32PrimTyConName IntRep
343
344 int64PrimTy :: Type
345 int64PrimTy     = mkTyConTy int64PrimTyCon
346 int64PrimTyCon :: TyCon
347 int64PrimTyCon  = pcPrimTyCon0 int64PrimTyConName Int64Rep
348
349 wordPrimTy :: Type
350 wordPrimTy      = mkTyConTy wordPrimTyCon
351 wordPrimTyCon :: TyCon
352 wordPrimTyCon   = pcPrimTyCon0 wordPrimTyConName WordRep
353
354 word32PrimTy :: Type
355 word32PrimTy    = mkTyConTy word32PrimTyCon
356 word32PrimTyCon :: TyCon
357 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName WordRep
358
359 word64PrimTy :: Type
360 word64PrimTy    = mkTyConTy word64PrimTyCon
361 word64PrimTyCon :: TyCon
362 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
363
364 addrPrimTy :: Type
365 addrPrimTy      = mkTyConTy addrPrimTyCon
366 addrPrimTyCon :: TyCon
367 addrPrimTyCon   = pcPrimTyCon0 addrPrimTyConName AddrRep
368
369 floatPrimTy     :: Type
370 floatPrimTy     = mkTyConTy floatPrimTyCon
371 floatPrimTyCon :: TyCon
372 floatPrimTyCon  = pcPrimTyCon0 floatPrimTyConName FloatRep
373
374 doublePrimTy :: Type
375 doublePrimTy    = mkTyConTy doublePrimTyCon
376 doublePrimTyCon :: TyCon
377 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
378 \end{code}
379
380
381 %************************************************************************
382 %*                                                                      *
383 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
384 %*                                                                      *
385 %************************************************************************
386
387 Note [The (~) TyCon)
388 ~~~~~~~~~~~~~~~~~~~~
389 There is a perfectly ordinary type constructor (~) that represents the type
390 of coercions (which, remember, are values).  For example
391    Refl Int :: Int ~ Int
392
393 Atcually it is not quite "perfectly ordinary" because it is kind-polymorphic:
394    Refl Maybe :: Maybe ~ Maybe
395
396 So the true kind of (~) :: forall k. k -> k -> #.  But we don't have
397 polymorphic kinds (yet). However, (~) really only appears saturated in
398 which case there is no problem in finding the kind of (ty1 ~ ty2). So
399 we check that in CoreLint (and, in an assertion, in Kind.typeKind).
400
401 Note [The State# TyCon]
402 ~~~~~~~~~~~~~~~~~~~~~~~
403 State# is the primitive, unlifted type of states.  It has one type parameter,
404 thus
405         State# RealWorld
406 or
407         State# s
408
409 where s is a type variable. The only purpose of the type parameter is to
410 keep different state threads separate.  It is represented by nothing at all.
411
412 \begin{code}
413 mkStatePrimTy :: Type -> Type
414 mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
415
416 statePrimTyCon :: TyCon   -- See Note [The State# TyCon]
417 statePrimTyCon   = pcPrimTyCon statePrimTyConName 1 VoidRep
418
419 eqPredPrimTyCon :: TyCon  -- The representation type for equality predicates
420                           -- See Note [The (~) TyCon]
421 eqPredPrimTyCon  = pcPrimTyCon eqPredPrimTyConName 2 VoidRep
422 \end{code}
423
424 RealWorld is deeply magical.  It is *primitive*, but it is not
425 *unlifted* (hence ptrArg).  We never manipulate values of type
426 RealWorld; it's only used in the type system, to parameterise State#.
427
428 \begin{code}
429 realWorldTyCon :: TyCon
430 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 PtrRep
431 realWorldTy :: Type
432 realWorldTy          = mkTyConTy realWorldTyCon
433 realWorldStatePrimTy :: Type
434 realWorldStatePrimTy = mkStatePrimTy realWorldTy        -- State# RealWorld
435 \end{code}
436
437 Note: the ``state-pairing'' types are not truly primitive, so they are
438 defined in \tr{TysWiredIn.lhs}, not here.
439
440 %************************************************************************
441 %*                                                                      *
442 \subsection[TysPrim-arrays]{The primitive array types}
443 %*                                                                      *
444 %************************************************************************
445
446 \begin{code}
447 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
448     byteArrayPrimTyCon :: TyCon
449 arrayPrimTyCon            = pcPrimTyCon  arrayPrimTyConName            1 PtrRep
450 mutableArrayPrimTyCon     = pcPrimTyCon  mutableArrayPrimTyConName     2 PtrRep
451 mutableByteArrayPrimTyCon = pcPrimTyCon  mutableByteArrayPrimTyConName 1 PtrRep
452 byteArrayPrimTyCon        = pcPrimTyCon0 byteArrayPrimTyConName          PtrRep
453
454 mkArrayPrimTy :: Type -> Type
455 mkArrayPrimTy elt           = mkTyConApp arrayPrimTyCon [elt]
456 byteArrayPrimTy :: Type
457 byteArrayPrimTy             = mkTyConTy byteArrayPrimTyCon
458 mkMutableArrayPrimTy :: Type -> Type -> Type
459 mkMutableArrayPrimTy s elt  = mkTyConApp mutableArrayPrimTyCon [s, elt]
460 mkMutableByteArrayPrimTy :: Type -> Type
461 mkMutableByteArrayPrimTy s  = mkTyConApp mutableByteArrayPrimTyCon [s]
462 \end{code}
463
464 %************************************************************************
465 %*                                                                      *
466 \subsection[TysPrim-mut-var]{The mutable variable type}
467 %*                                                                      *
468 %************************************************************************
469
470 \begin{code}
471 mutVarPrimTyCon :: TyCon
472 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName 2 PtrRep
473
474 mkMutVarPrimTy :: Type -> Type -> Type
475 mkMutVarPrimTy s elt        = mkTyConApp mutVarPrimTyCon [s, elt]
476 \end{code}
477
478 %************************************************************************
479 %*                                                                      *
480 \subsection[TysPrim-synch-var]{The synchronizing variable type}
481 %*                                                                      *
482 %************************************************************************
483
484 \begin{code}
485 mVarPrimTyCon :: TyCon
486 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName 2 PtrRep
487
488 mkMVarPrimTy :: Type -> Type -> Type
489 mkMVarPrimTy s elt          = mkTyConApp mVarPrimTyCon [s, elt]
490 \end{code}
491
492 %************************************************************************
493 %*                                                                      *
494 \subsection[TysPrim-stm-var]{The transactional variable type}
495 %*                                                                      *
496 %************************************************************************
497
498 \begin{code}
499 tVarPrimTyCon :: TyCon
500 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName 2 PtrRep
501
502 mkTVarPrimTy :: Type -> Type -> Type
503 mkTVarPrimTy s elt = mkTyConApp tVarPrimTyCon [s, elt]
504 \end{code}
505
506 %************************************************************************
507 %*                                                                      *
508 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
509 %*                                                                      *
510 %************************************************************************
511
512 \begin{code}
513 stablePtrPrimTyCon :: TyCon
514 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName 1 AddrRep
515
516 mkStablePtrPrimTy :: Type -> Type
517 mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
518 \end{code}
519
520 %************************************************************************
521 %*                                                                      *
522 \subsection[TysPrim-stable-names]{The stable-name type}
523 %*                                                                      *
524 %************************************************************************
525
526 \begin{code}
527 stableNamePrimTyCon :: TyCon
528 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName 1 PtrRep
529
530 mkStableNamePrimTy :: Type -> Type
531 mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
532 \end{code}
533
534 %************************************************************************
535 %*                                                                      *
536 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
537 %*                                                                      *
538 %************************************************************************
539
540 \begin{code}
541 bcoPrimTy    :: Type
542 bcoPrimTy    = mkTyConTy bcoPrimTyCon
543 bcoPrimTyCon :: TyCon
544 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep
545 \end{code}
546   
547 %************************************************************************
548 %*                                                                      *
549 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
550 %*                                                                      *
551 %************************************************************************
552
553 \begin{code}
554 weakPrimTyCon :: TyCon
555 weakPrimTyCon = pcPrimTyCon weakPrimTyConName 1 PtrRep
556
557 mkWeakPrimTy :: Type -> Type
558 mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
559 \end{code}
560
561 %************************************************************************
562 %*                                                                      *
563 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
564 %*                                                                      *
565 %************************************************************************
566
567 A thread id is represented by a pointer to the TSO itself, to ensure
568 that they are always unique and we can always find the TSO for a given
569 thread id.  However, this has the unfortunate consequence that a
570 ThreadId# for a given thread is treated as a root by the garbage
571 collector and can keep TSOs around for too long.
572
573 Hence the programmer API for thread manipulation uses a weak pointer
574 to the thread id internally.
575
576 \begin{code}
577 threadIdPrimTy :: Type
578 threadIdPrimTy    = mkTyConTy threadIdPrimTyCon
579 threadIdPrimTyCon :: TyCon
580 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep
581 \end{code}
582
583
584
585 %************************************************************************
586 %*                                                                      *
587                 Any
588 %*                                                                      *
589 %************************************************************************
590
591 Note [Any types]
592 ~~~~~~~~~~~~~~~~
593 The type constructor Any::* has these properties
594
595   * It is defined in module GHC.Prim, and exported so that it is 
596     available to users.  For this reason it's treated like any other 
597     primitive type:
598       - has a fixed unique, anyTyConKey, 
599       - lives in the global name cache
600       - built with TyCon.PrimTyCon
601
602   * It is lifted, and hence represented by a pointer
603
604   * It is inhabited by at least one value, namely bottom
605
606   * You can unsafely coerce any lifted type to Ayny, and back.
607
608   * It does not claim to be a *data* type, and that's important for
609     the code generator, because the code gen may *enter* a data value
610     but never enters a function value. 
611
612   * It is used to instantiate otherwise un-constrained type variables of kind *
613     For example         length Any []
614     See Note [Strangely-kinded void TyCons]
615
616 In addition, we have a potentially-infinite family of types, one for
617 each kind /other than/ *, needed to instantiate otherwise
618 un-constrained type variables of kinds other than *.  This is a bit
619 like tuples; there is a potentially-infinite family.  They have slightly
620 different characteristics to Any::*:
621   
622   * They are built with TyCon.AnyTyCon
623   * They have non-user-writable names like "Any(*->*)" 
624   * They are not exported by GHC.Prim
625   * They are uninhabited (of course; not kind *)
626   * They have a unique derived from their OccName (see Note [Uniques of Any])
627   * Their Names do not live in the global name cache
628
629 Note [Uniques of Any]
630 ~~~~~~~~~~~~~~~~~~~~~
631 Although Any(*->*), say, doesn't have a binding site, it still needs
632 to have a Unique.  Unlike tuples (which are also an infinite family)
633 there is no convenient way to index them, so we use the Unique from
634 their OccName instead.  That should be unique, 
635   - both wrt each other, because their strings differ
636
637   - and wrt any other Name, because Names get uniques with 
638     various 'char' tags, but the OccName of Any will 
639     get a Unique built with mkTcOccUnique, which has a particular 'char' 
640     tag; see Unique.mkTcOccUnique!
641
642 Note [Strangely-kinded void TyCons]
643 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
644 See Trac #959 for more examples
645
646 When the type checker finds a type variable with no binding, which
647 means it can be instantiated with an arbitrary type, it usually
648 instantiates it to Void.  Eg.
649
650         length []
651 ===>
652         length Any (Nil Any)
653
654 But in really obscure programs, the type variable might have a kind
655 other than *, so we need to invent a suitably-kinded type.
656
657 This commit uses
658         Any for kind *
659         Any(*->*) for kind *->*
660         etc
661
662 \begin{code}
663 anyTyConName :: Name
664 anyTyConName = mkPrimTc (fsLit "Any") anyTyConKey anyTyCon
665
666 anyTyCon :: TyCon
667 anyTyCon = mkLiftedPrimTyCon anyTyConName liftedTypeKind 0 PtrRep
668
669 anyTypeOfKind :: Kind -> Type
670 anyTypeOfKind kind = mkTyConApp (anyTyConOfKind kind) []
671
672 anyTyConOfKind :: Kind -> TyCon
673 -- Map all superkinds of liftedTypeKind to liftedTypeKind
674 anyTyConOfKind kind 
675   | isLiftedTypeKind kind = anyTyCon
676   | otherwise             = tycon
677   where
678           -- Derive the name from the kind, thus:
679           --     Any(*->*), Any(*->*->*)
680           -- These are names that can't be written by the user,
681           -- and are not allocated in the global name cache
682     str = "Any" ++ showSDoc (pprParendKind kind)
683
684     occ   = mkTcOcc str
685     uniq  = getUnique occ  -- See Note [Uniques of Any]
686     name  = mkWiredInName gHC_PRIM occ uniq (ATyCon tycon) UserSyntax
687     tycon = mkAnyTyCon name kind 
688 \end{code}