root/compiler/prelude/TysPrim.lhs

Revision 1dd9b1ae1a5994a78ec1c4ca18c8df6e5b2ca9e0, 26.3 KB (checked in by Iavor S. Diatchki <iavor.diatchki@…>, 3 months ago)

Merge remote-tracking branch 'origin/master' into type-nats

Conflicts:

compiler/coreSyn/CoreLint.lhs
compiler/deSugar/DsBinds.lhs
compiler/hsSyn/HsTypes.lhs
compiler/iface/IfaceType.lhs
compiler/rename/RnHsSyn.lhs
compiler/rename/RnTypes.lhs
compiler/stgSyn/StgLint.lhs
compiler/typecheck/TcHsType.lhs
compiler/utils/ListSetOps.lhs

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