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