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