TysPrim.lhs 26 KB
Newer Older
1
%
2
% (c) The AQUA Project, Glasgow University, 1994-1998
3
%
4 5

     
6 7 8
\section[TysPrim]{Wired-in knowledge about primitive types}

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
9 10 11 12 13 14 15
{-# OPTIONS -fno-warn-tabs #-}
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and
-- detab the module (please do the detabbing in a separate patch). See
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
-- for details

16 17
-- | This module defines TyCons that can't be expressed in Haskell. 
--   They are all, therefore, wired-in TyCons.  C.f module TysWiredIn
18
module TysPrim(
batterseapower's avatar
batterseapower committed
19 20 21
	mkPrimTyConName, -- For implicit parameters in TysWiredIn only

        tyVarList, alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
22
	alphaTy, betaTy, gammaTy, deltaTy,
Simon Marlow's avatar
Simon Marlow committed
23
	openAlphaTy, openBetaTy, openAlphaTyVar, openBetaTyVar, openAlphaTyVars,
dreixel's avatar
dreixel committed
24 25
        argAlphaTy, argAlphaTyVar, argAlphaTyVars, argBetaTy, argBetaTyVar,
        kKiVar,
26

27
        -- Kind constructors...
28
        superKindTyCon, superKind, anyKindTyCon,
29
        liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
batterseapower's avatar
batterseapower committed
30
        argTypeKindTyCon, ubxTupleKindTyCon, constraintKindTyCon,
31

32
        superKindTyConName, anyKindTyConName, liftedTypeKindTyConName,
33 34
        openTypeKindTyConName, unliftedTypeKindTyConName,
        ubxTupleKindTyConName, argTypeKindTyConName,
batterseapower's avatar
batterseapower committed
35
        constraintKindTyConName,
36 37

        -- Kinds
dreixel's avatar
dreixel committed
38
	anyKind, liftedTypeKind, unliftedTypeKind, openTypeKind,
batterseapower's avatar
batterseapower committed
39
        argTypeKind, ubxTupleKind, constraintKind,
40
        mkArrowKind, mkArrowKinds,
41 42 43

        funTyCon, funTyConName,
        primTyCons,
44

45 46 47 48 49 50 51 52 53 54
	charPrimTyCon, 		charPrimTy,
	intPrimTyCon,		intPrimTy,
	wordPrimTyCon,		wordPrimTy,
	addrPrimTyCon,		addrPrimTy,
	floatPrimTyCon,		floatPrimTy,
	doublePrimTyCon,	doublePrimTy,

	statePrimTyCon,		mkStatePrimTy,
	realWorldTyCon,		realWorldTy, realWorldStatePrimTy,

55 56 57 58 59 60 61
	arrayPrimTyCon,	mkArrayPrimTy, 
	byteArrayPrimTyCon,	byteArrayPrimTy,
	arrayArrayPrimTyCon, mkArrayArrayPrimTy, 
	mutableArrayPrimTyCon, mkMutableArrayPrimTy,
	mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
	mutableArrayArrayPrimTyCon, mkMutableArrayArrayPrimTy,
	mutVarPrimTyCon, mkMutVarPrimTy,
62 63

	mVarPrimTyCon,			mkMVarPrimTy,	
64
        tVarPrimTyCon,                  mkTVarPrimTy,
65
	stablePtrPrimTyCon,		mkStablePtrPrimTy,
66
	stableNamePrimTyCon,		mkStableNamePrimTy,
67
	bcoPrimTyCon,			bcoPrimTy,
68 69 70
	weakPrimTyCon,  		mkWeakPrimTy,
	threadIdPrimTyCon,		threadIdPrimTy,
	
apt's avatar
apt committed
71 72 73
	int32PrimTyCon,		int32PrimTy,
	word32PrimTyCon,	word32PrimTy,

74
	int64PrimTyCon,		int64PrimTy,
75 76
        word64PrimTyCon,        word64PrimTy,

batterseapower's avatar
batterseapower committed
77
        eqPrimTyCon,            -- ty1 ~# ty2
78

79
	-- * Any
dreixel's avatar
dreixel committed
80
	anyTy, anyTyCon, anyTypeOfKind
81
  ) where
82

83
#include "HsVersions.h"
84

dreixel's avatar
dreixel committed
85
import Var		( TyVar, KindVar, mkTyVar )
86
import Name		( Name, BuiltInSyntax(..), mkInternalName, mkWiredInName )
dreixel's avatar
dreixel committed
87
import OccName          ( mkTyVarOccFS, mkTcOccFS )
88 89
import TyCon
import TypeRep
90
import SrcLoc
91
import Unique		( mkAlphaTyVarUnique )
92
import PrelNames
93
import FastString
94

Ian Lynagh's avatar
Ian Lynagh committed
95
import Data.Char
96 97
\end{code}

98 99 100 101 102 103 104 105 106 107 108 109
%************************************************************************
%*									*
\subsection{Primitive type constructors}
%*									*
%************************************************************************

\begin{code}
primTyCons :: [TyCon]
primTyCons 
  = [ addrPrimTyCon
    , arrayPrimTyCon
    , byteArrayPrimTyCon
110
    , arrayArrayPrimTyCon
111 112 113 114
    , charPrimTyCon
    , doublePrimTyCon
    , floatPrimTyCon
    , intPrimTyCon
apt's avatar
apt committed
115
    , int32PrimTyCon
116 117 118 119 120
    , int64PrimTyCon
    , bcoPrimTyCon
    , weakPrimTyCon
    , mutableArrayPrimTyCon
    , mutableByteArrayPrimTyCon
121
    , mutableArrayArrayPrimTyCon
122
    , mVarPrimTyCon
123
    , tVarPrimTyCon
124 125 126 127 128 129 130
    , mutVarPrimTyCon
    , realWorldTyCon
    , stablePtrPrimTyCon
    , stableNamePrimTyCon
    , statePrimTyCon
    , threadIdPrimTyCon
    , wordPrimTyCon
apt's avatar
apt committed
131
    , word32PrimTyCon
132
    , word64PrimTyCon
133
    , anyTyCon
dreixel's avatar
dreixel committed
134
    , anyKindTyCon
batterseapower's avatar
batterseapower committed
135
    , eqPrimTyCon
136 137 138 139 140 141 142

    , liftedTypeKindTyCon
    , unliftedTypeKindTyCon
    , openTypeKindTyCon
    , argTypeKindTyCon
    , ubxTupleKindTyCon
    , constraintKindTyCon
143 144
    ]

145
mkPrimTc :: FastString -> Unique -> TyCon -> Name
Ian Lynagh's avatar
Ian Lynagh committed
146
mkPrimTc fs unique tycon
147
  = mkWiredInName gHC_PRIM (mkTcOccFS fs) 
Ian Lynagh's avatar
Ian Lynagh committed
148
		  unique
149
		  (ATyCon tycon)	-- Relevant TyCon
150
		  UserSyntax		-- None are built-in syntax
151

152
charPrimTyConName, 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
Ian Lynagh's avatar
Ian Lynagh committed
153 154 155 156 157 158 159 160 161 162
charPrimTyConName    	      = mkPrimTc (fsLit "Char#") charPrimTyConKey charPrimTyCon
intPrimTyConName     	      = mkPrimTc (fsLit "Int#") intPrimTyConKey  intPrimTyCon
int32PrimTyConName	      = mkPrimTc (fsLit "Int32#") int32PrimTyConKey int32PrimTyCon
int64PrimTyConName   	      = mkPrimTc (fsLit "Int64#") int64PrimTyConKey int64PrimTyCon
wordPrimTyConName    	      = mkPrimTc (fsLit "Word#") wordPrimTyConKey wordPrimTyCon
word32PrimTyConName  	      = mkPrimTc (fsLit "Word32#") word32PrimTyConKey word32PrimTyCon
word64PrimTyConName  	      = mkPrimTc (fsLit "Word64#") word64PrimTyConKey word64PrimTyCon
addrPrimTyConName    	      = mkPrimTc (fsLit "Addr#") addrPrimTyConKey addrPrimTyCon
floatPrimTyConName   	      = mkPrimTc (fsLit "Float#") floatPrimTyConKey floatPrimTyCon
doublePrimTyConName  	      = mkPrimTc (fsLit "Double#") doublePrimTyConKey doublePrimTyCon
163
statePrimTyConName            = mkPrimTc (fsLit "State#") statePrimTyConKey statePrimTyCon
batterseapower's avatar
batterseapower committed
164
eqPrimTyConName               = mkPrimTc (fsLit "~#") eqPrimTyConKey eqPrimTyCon
165
realWorldTyConName            = mkPrimTc (fsLit "RealWorld") realWorldTyConKey realWorldTyCon
Ian Lynagh's avatar
Ian Lynagh committed
166 167
arrayPrimTyConName   	      = mkPrimTc (fsLit "Array#") arrayPrimTyConKey arrayPrimTyCon
byteArrayPrimTyConName	      = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey byteArrayPrimTyCon
168
arrayArrayPrimTyConName   	  = mkPrimTc (fsLit "ArrayArray#") arrayArrayPrimTyConKey arrayArrayPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
169 170
mutableArrayPrimTyConName     = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey mutableArrayPrimTyCon
mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey mutableByteArrayPrimTyCon
171
mutableArrayArrayPrimTyConName= mkPrimTc (fsLit "MutableArrayArray#") mutableArrayArrayPrimTyConKey mutableArrayArrayPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
172 173 174 175 176 177 178 179
mutVarPrimTyConName	      = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey mutVarPrimTyCon
mVarPrimTyConName	      = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey mVarPrimTyCon
tVarPrimTyConName	      = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey tVarPrimTyCon
stablePtrPrimTyConName        = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey stablePtrPrimTyCon
stableNamePrimTyConName       = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey stableNamePrimTyCon
bcoPrimTyConName 	      = mkPrimTc (fsLit "BCO#") bcoPrimTyConKey bcoPrimTyCon
weakPrimTyConName  	      = mkPrimTc (fsLit "Weak#") weakPrimTyConKey weakPrimTyCon
threadIdPrimTyConName  	      = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey threadIdPrimTyCon
180
\end{code}
181 182 183 184 185 186 187

%************************************************************************
%*									*
\subsection{Support code}
%*									*
%************************************************************************

188 189 190
alphaTyVars is a list of type variables for use in templates: 
	["a", "b", ..., "z", "t1", "t2", ... ]

191
\begin{code}
192
tyVarList :: Kind -> [TyVar]
193
tyVarList kind = [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u) 
194
				(mkTyVarOccFS (mkFastString name))
195
			 	noSrcSpan) kind
196 197 198 199 200 201
	         | u <- [2..],
		   let name | c <= 'z'  = [c]
		            | otherwise = 't':show u
			    where c = chr (u-2 + ord 'a')
	         ]

202
alphaTyVars :: [TyVar]
203
alphaTyVars = tyVarList liftedTypeKind
204

Ian Lynagh's avatar
Ian Lynagh committed
205
betaTyVars :: [TyVar]
206 207
betaTyVars = tail alphaTyVars

Ian Lynagh's avatar
Ian Lynagh committed
208
alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: TyVar
209 210
(alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars

Ian Lynagh's avatar
Ian Lynagh committed
211
alphaTys :: [Type]
212
alphaTys = mkTyVarTys alphaTyVars
Ian Lynagh's avatar
Ian Lynagh committed
213
alphaTy, betaTy, gammaTy, deltaTy :: Type
214
(alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
215 216

	-- openAlphaTyVar is prepared to be instantiated
217
	-- to a lifted or unlifted type variable.  It's used for the 
218 219
	-- result type for "error", so that we can have (error Int# "Help")
openAlphaTyVars :: [TyVar]
Ian Lynagh's avatar
Ian Lynagh committed
220
openAlphaTyVar, openBetaTyVar :: TyVar
Simon Marlow's avatar
Simon Marlow committed
221
openAlphaTyVars@(openAlphaTyVar:openBetaTyVar:_) = tyVarList openTypeKind
222

Ian Lynagh's avatar
Ian Lynagh committed
223
openAlphaTy, openBetaTy :: Type
224
openAlphaTy = mkTyVarTy openAlphaTyVar
225 226
openBetaTy  = mkTyVarTy openBetaTyVar

227
argAlphaTyVars :: [TyVar]
228
argAlphaTyVar, argBetaTyVar :: TyVar
229
argAlphaTyVars@(argAlphaTyVar : argBetaTyVar : _) = tyVarList argTypeKind
230
argAlphaTy, argBetaTy :: Type
231
argAlphaTy = mkTyVarTy argAlphaTyVar
232
argBetaTy  = mkTyVarTy argBetaTyVar
dreixel's avatar
dreixel committed
233 234

kKiVar :: KindVar
235
kKiVar = (tyVarList superKind) !! 10
dreixel's avatar
dreixel committed
236

237 238
\end{code}

239

240 241
%************************************************************************
%*									*
242
                FunTyCon
243 244 245
%*									*
%************************************************************************

246 247 248 249 250
\begin{code}
funTyConName :: Name
funTyConName = mkPrimTyConName (fsLit "(->)") funTyConKey funTyCon

funTyCon :: TyCon
251 252 253 254 255 256 257 258 259 260 261
funTyCon = mkFunTyCon funTyConName $ 
           mkArrowKinds [liftedTypeKind, liftedTypeKind] liftedTypeKind
        -- You might think that (->) should have type (?? -> ? -> *), and you'd be right
	-- But if we do that we get kind errors when saying
	--	instance Control.Arrow (->)
	-- becuase the expected kind is (*->*->*).  The trouble is that the
	-- expected/actual stuff in the unifier does not go contra-variant, whereas
	-- the kind sub-typing does.  Sigh.  It really only matters if you use (->) in
	-- a prefix way, thus:  (->) Int# Int#.  And this is unusual.
        -- because they are never in scope in the source

dreixel's avatar
dreixel committed
262 263 264 265 266 267 268 269 270 271 272 273 274
-- One step to remove subkinding.
-- (->) :: * -> * -> *
-- but we should have (and want) the following typing rule for fully applied arrows
--      Gamma |- tau   :: k1    k1 in {*, #}
--      Gamma |- sigma :: k2    k2 in {*, #, (#)}
--      -----------------------------------------
--      Gamma |- tau -> sigma :: *
-- Currently we have the following rule which achieves more or less the same effect
--      Gamma |- tau   :: ??
--      Gamma |- sigma :: ?
--      --------------------------
--      Gamma |- tau -> sigma :: *
-- In the end we don't want subkinding at all.
275
\end{code}
276 277


278 279 280 281 282
%************************************************************************
%*									*
                Kinds
%*									*
%************************************************************************
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
Note [SuperKind (BOX)]
~~~~~~~~~~~~~~~~~~~~~~
Kinds are classified by "super-kinds".  There is only one super-kind, namely BOX.

Perhaps surprisingly we give BOX the kind BOX, thus   BOX :: BOX
Reason: we want to have kind equalities, thus (without the kind applications)
            keq :: * ~ * = Eq# <refl *>
Remember that
   (~)  :: forall (k:BOX). k -> k -> Constraint
   (~#) :: forall (k:BOX). k -> k -> #
   Eq#  :: forall (k:BOX). forall (a:k) (b:k). (~#) k a b -> (~) k a b

So the full defn of keq is
   keq :: (~) BOX * * = Eq# BOX * * <refl *>

So you can see it's convenient to have BOX:BOX


302 303
\begin{code}
-- | See "Type#kind_subtyping" for details of the distinction between the 'Kind' 'TyCon's
304
superKindTyCon, anyKindTyCon, liftedTypeKindTyCon,
305
      openTypeKindTyCon, unliftedTypeKindTyCon,
batterseapower's avatar
batterseapower committed
306 307
      ubxTupleKindTyCon, argTypeKindTyCon,
      constraintKindTyCon
308
   :: TyCon
309
superKindTyConName, anyKindTyConName, liftedTypeKindTyConName,
310
      openTypeKindTyConName, unliftedTypeKindTyConName,
batterseapower's avatar
batterseapower committed
311 312
      ubxTupleKindTyConName, argTypeKindTyConName,
      constraintKindTyConName
313 314
   :: Name

315 316 317 318 319 320 321 322 323 324
superKindTyCon        = mkKindTyCon superKindTyConName        superKind
   -- See Note [SuperKind (BOX)]

anyKindTyCon          = mkKindTyCon anyKindTyConName          superKind
liftedTypeKindTyCon   = mkKindTyCon liftedTypeKindTyConName   superKind
openTypeKindTyCon     = mkKindTyCon openTypeKindTyConName     superKind
unliftedTypeKindTyCon = mkKindTyCon unliftedTypeKindTyConName superKind
ubxTupleKindTyCon     = mkKindTyCon ubxTupleKindTyConName     superKind
argTypeKindTyCon      = mkKindTyCon argTypeKindTyConName      superKind
constraintKindTyCon   = mkKindTyCon constraintKindTyConName   superKind
325 326 327 328

--------------------------
-- ... and now their names

329 330
superKindTyConName      = mkPrimTyConName (fsLit "BOX") superKindTyConKey superKindTyCon
anyKindTyConName          = mkPrimTyConName (fsLit "AnyK") anyKindTyConKey anyKindTyCon
331
liftedTypeKindTyConName   = mkPrimTyConName (fsLit "*") liftedTypeKindTyConKey liftedTypeKindTyCon
332
openTypeKindTyConName     = mkPrimTyConName (fsLit "OpenKind") openTypeKindTyConKey openTypeKindTyCon
333 334
unliftedTypeKindTyConName = mkPrimTyConName (fsLit "#") unliftedTypeKindTyConKey unliftedTypeKindTyCon
ubxTupleKindTyConName     = mkPrimTyConName (fsLit "(#)") ubxTupleKindTyConKey ubxTupleKindTyCon
335
argTypeKindTyConName      = mkPrimTyConName (fsLit "ArgKind") argTypeKindTyConKey argTypeKindTyCon
batterseapower's avatar
batterseapower committed
336
constraintKindTyConName   = mkPrimTyConName (fsLit "Constraint") constraintKindTyConKey constraintKindTyCon
337 338 339 340 341 342 343 344 345

mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
mkPrimTyConName occ key tycon = mkWiredInName gHC_PRIM (mkTcOccFS occ) 
					      key 
					      (ATyCon tycon)
					      BuiltInSyntax
	-- All of the super kinds and kinds are defined in Prim and use BuiltInSyntax,
	-- because they are never in scope in the source
\end{code}
346 347 348


\begin{code}
349 350
kindTyConType :: TyCon -> Type
kindTyConType kind = TyConApp kind []
351

352
-- | See "Type#kind_subtyping" for details of the distinction between these 'Kind's
353 354 355
anyKind, liftedTypeKind, unliftedTypeKind, openTypeKind, 
  argTypeKind, ubxTupleKind, constraintKind,
  superKind :: Kind
356

357 358
superKind        = kindTyConType superKindTyCon 
anyKind          = kindTyConType anyKindTyCon  -- See Note [Any kinds]
359 360 361 362
liftedTypeKind   = kindTyConType liftedTypeKindTyCon
unliftedTypeKind = kindTyConType unliftedTypeKindTyCon
openTypeKind     = kindTyConType openTypeKindTyCon
argTypeKind      = kindTyConType argTypeKindTyCon
dreixel's avatar
dreixel committed
363
ubxTupleKind     = kindTyConType ubxTupleKindTyCon
batterseapower's avatar
batterseapower committed
364
constraintKind   = kindTyConType constraintKindTyCon
365

366 367 368
-- | Given two kinds @k1@ and @k2@, creates the 'Kind' @k1 -> k2@
mkArrowKind :: Kind -> Kind -> Kind
mkArrowKind k1 k2 = FunTy k1 k2
369

370 371 372 373
-- | Iterated application of 'mkArrowKind'
mkArrowKinds :: [Kind] -> Kind -> Kind
mkArrowKinds arg_kinds result_kind = foldr mkArrowKind result_kind arg_kinds
\end{code}
374

375 376 377 378 379 380 381
%************************************************************************
%*									*
\subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
%*									*
%************************************************************************

\begin{code}
382
-- only used herein
383 384 385
pcPrimTyCon :: Name -> Int -> PrimRep -> TyCon
pcPrimTyCon name arity rep
  = mkPrimTyCon name kind arity rep
386
  where
sof's avatar
sof committed
387
    kind        = mkArrowKinds (replicate arity liftedTypeKind) result_kind
388
    result_kind = unliftedTypeKind
389

390 391
pcPrimTyCon0 :: Name -> PrimRep -> TyCon
pcPrimTyCon0 name rep
392
  = mkPrimTyCon name result_kind 0 rep
393
  where
394
    result_kind = unliftedTypeKind
395

Ian Lynagh's avatar
Ian Lynagh committed
396
charPrimTy :: Type
397
charPrimTy	= mkTyConTy charPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
398
charPrimTyCon :: TyCon
399
charPrimTyCon	= pcPrimTyCon0 charPrimTyConName WordRep
400

Ian Lynagh's avatar
Ian Lynagh committed
401
intPrimTy :: Type
402
intPrimTy	= mkTyConTy intPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
403
intPrimTyCon :: TyCon
404
intPrimTyCon	= pcPrimTyCon0 intPrimTyConName IntRep
405

Ian Lynagh's avatar
Ian Lynagh committed
406
int32PrimTy :: Type
apt's avatar
apt committed
407
int32PrimTy	= mkTyConTy int32PrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
408
int32PrimTyCon :: TyCon
409
int32PrimTyCon	= pcPrimTyCon0 int32PrimTyConName IntRep
apt's avatar
apt committed
410

Ian Lynagh's avatar
Ian Lynagh committed
411
int64PrimTy :: Type
sof's avatar
sof committed
412
int64PrimTy	= mkTyConTy int64PrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
413
int64PrimTyCon :: TyCon
414
int64PrimTyCon	= pcPrimTyCon0 int64PrimTyConName Int64Rep
sof's avatar
sof committed
415

Ian Lynagh's avatar
Ian Lynagh committed
416
wordPrimTy :: Type
417
wordPrimTy	= mkTyConTy wordPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
418
wordPrimTyCon :: TyCon
419
wordPrimTyCon	= pcPrimTyCon0 wordPrimTyConName WordRep
420

Ian Lynagh's avatar
Ian Lynagh committed
421
word32PrimTy :: Type
apt's avatar
apt committed
422
word32PrimTy	= mkTyConTy word32PrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
423
word32PrimTyCon :: TyCon
424
word32PrimTyCon	= pcPrimTyCon0 word32PrimTyConName WordRep
apt's avatar
apt committed
425

Ian Lynagh's avatar
Ian Lynagh committed
426
word64PrimTy :: Type
sof's avatar
sof committed
427
word64PrimTy	= mkTyConTy word64PrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
428
word64PrimTyCon :: TyCon
429
word64PrimTyCon	= pcPrimTyCon0 word64PrimTyConName Word64Rep
sof's avatar
sof committed
430

Ian Lynagh's avatar
Ian Lynagh committed
431
addrPrimTy :: Type
432
addrPrimTy	= mkTyConTy addrPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
433
addrPrimTyCon :: TyCon
434
addrPrimTyCon	= pcPrimTyCon0 addrPrimTyConName AddrRep
435

Ian Lynagh's avatar
Ian Lynagh committed
436
floatPrimTy	:: Type
437
floatPrimTy	= mkTyConTy floatPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
438
floatPrimTyCon :: TyCon
439
floatPrimTyCon	= pcPrimTyCon0 floatPrimTyConName FloatRep
440

Ian Lynagh's avatar
Ian Lynagh committed
441
doublePrimTy :: Type
442
doublePrimTy	= mkTyConTy doublePrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
443
doublePrimTyCon	:: TyCon
444
doublePrimTyCon	= pcPrimTyCon0 doublePrimTyConName DoubleRep
445 446
\end{code}

447 448 449

%************************************************************************
%*									*
450
\subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
451 452 453
%*									*
%************************************************************************

batterseapower's avatar
batterseapower committed
454
Note [The ~# TyCon)
455
~~~~~~~~~~~~~~~~~~~~
batterseapower's avatar
batterseapower committed
456
There is a perfectly ordinary type constructor ~# that represents the type
457
of coercions (which, remember, are values).  For example
dreixel's avatar
dreixel committed
458
   Refl Int :: ~# * Int Int
459

dreixel's avatar
dreixel committed
460 461
It is a kind-polymorphic type constructor like Any:
   Refl Maybe :: ~# (* -> *) Maybe Maybe
462

dreixel's avatar
dreixel committed
463 464
(~) only appears saturated. So we check that in CoreLint (and, in an
assertion, in Kind.typeKind).
465 466 467

Note [The State# TyCon]
~~~~~~~~~~~~~~~~~~~~~~~
468
State# is the primitive, unlifted type of states.  It has one type parameter,
469 470 471 472
thus
	State# RealWorld
or
	State# s
473

474 475
where s is a type variable. The only purpose of the type parameter is to
keep different state threads separate.  It is represented by nothing at all.
476 477

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
478
mkStatePrimTy :: Type -> Type
479
mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
480

481
statePrimTyCon :: TyCon   -- See Note [The State# TyCon]
482
statePrimTyCon	 = pcPrimTyCon statePrimTyConName 1 VoidRep
483

batterseapower's avatar
batterseapower committed
484 485
eqPrimTyCon :: TyCon  -- The representation type for equality predicates
		      -- See Note [The ~# TyCon]
dreixel's avatar
dreixel committed
486 487 488 489
eqPrimTyCon  = mkPrimTyCon eqPrimTyConName kind 3 VoidRep
  where kind = ForAllTy kv $ mkArrowKinds [k, k] unliftedTypeKind
        kv = kKiVar
        k = mkTyVarTy kv
490 491
\end{code}

492
RealWorld is deeply magical.  It is *primitive*, but it is not
493
*unlifted* (hence ptrArg).  We never manipulate values of type
494
RealWorld; it's only used in the type system, to parameterise State#.
495

496
\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
497
realWorldTyCon :: TyCon
498
realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 PtrRep
Ian Lynagh's avatar
Ian Lynagh committed
499
realWorldTy :: Type
500
realWorldTy	     = mkTyConTy realWorldTyCon
Ian Lynagh's avatar
Ian Lynagh committed
501
realWorldStatePrimTy :: Type
502
realWorldStatePrimTy = mkStatePrimTy realWorldTy	-- State# RealWorld
503 504 505 506 507 508 509 510 511 512 513 514
\end{code}

Note: the ``state-pairing'' types are not truly primitive, so they are
defined in \tr{TysWiredIn.lhs}, not here.

%************************************************************************
%*									*
\subsection[TysPrim-arrays]{The primitive array types}
%*									*
%************************************************************************

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
515
arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
516 517 518 519 520 521 522
    byteArrayPrimTyCon, arrayArrayPrimTyCon, mutableArrayArrayPrimTyCon :: TyCon
arrayPrimTyCon             = pcPrimTyCon  arrayPrimTyConName             1 PtrRep
mutableArrayPrimTyCon      = pcPrimTyCon  mutableArrayPrimTyConName      2 PtrRep
mutableByteArrayPrimTyCon  = pcPrimTyCon  mutableByteArrayPrimTyConName  1 PtrRep
byteArrayPrimTyCon         = pcPrimTyCon0 byteArrayPrimTyConName           PtrRep
arrayArrayPrimTyCon        = pcPrimTyCon0 arrayArrayPrimTyConName          PtrRep
mutableArrayArrayPrimTyCon = pcPrimTyCon  mutableArrayArrayPrimTyConName 1 PtrRep
523

Ian Lynagh's avatar
Ian Lynagh committed
524
mkArrayPrimTy :: Type -> Type
525
mkArrayPrimTy elt    	    = mkTyConApp arrayPrimTyCon [elt]
Ian Lynagh's avatar
Ian Lynagh committed
526
byteArrayPrimTy :: Type
527
byteArrayPrimTy	    	    = mkTyConTy byteArrayPrimTyCon
528 529
mkArrayArrayPrimTy :: Type
mkArrayArrayPrimTy = mkTyConTy arrayArrayPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
530
mkMutableArrayPrimTy :: Type -> Type -> Type
531
mkMutableArrayPrimTy s elt  = mkTyConApp mutableArrayPrimTyCon [s, elt]
Ian Lynagh's avatar
Ian Lynagh committed
532
mkMutableByteArrayPrimTy :: Type -> Type
533
mkMutableByteArrayPrimTy s  = mkTyConApp mutableByteArrayPrimTyCon [s]
534 535
mkMutableArrayArrayPrimTy :: Type -> Type
mkMutableArrayArrayPrimTy s = mkTyConApp mutableArrayArrayPrimTyCon [s]
536 537
\end{code}

538 539 540 541 542 543 544
%************************************************************************
%*									*
\subsection[TysPrim-mut-var]{The mutable variable type}
%*									*
%************************************************************************

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
545
mutVarPrimTyCon :: TyCon
546
mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName 2 PtrRep
547

Ian Lynagh's avatar
Ian Lynagh committed
548
mkMutVarPrimTy :: Type -> Type -> Type
549 550 551
mkMutVarPrimTy s elt 	    = mkTyConApp mutVarPrimTyCon [s, elt]
\end{code}

552 553 554 555 556 557 558
%************************************************************************
%*									*
\subsection[TysPrim-synch-var]{The synchronizing variable type}
%*									*
%************************************************************************

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
559
mVarPrimTyCon :: TyCon
560
mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName 2 PtrRep
561

Ian Lynagh's avatar
Ian Lynagh committed
562
mkMVarPrimTy :: Type -> Type -> Type
563
mkMVarPrimTy s elt 	    = mkTyConApp mVarPrimTyCon [s, elt]
564 565 566
\end{code}

%************************************************************************
567 568 569 570 571 572
%*									*
\subsection[TysPrim-stm-var]{The transactional variable type}
%*									*
%************************************************************************

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
573
tVarPrimTyCon :: TyCon
574
tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName 2 PtrRep
575

Ian Lynagh's avatar
Ian Lynagh committed
576 577
mkTVarPrimTy :: Type -> Type -> Type
mkTVarPrimTy s elt = mkTyConApp tVarPrimTyCon [s, elt]
578 579 580
\end{code}

%************************************************************************
581 582 583 584 585 586
%*									*
\subsection[TysPrim-stable-ptrs]{The stable-pointer type}
%*									*
%************************************************************************

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
587
stablePtrPrimTyCon :: TyCon
588
stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName 1 AddrRep
589

Ian Lynagh's avatar
Ian Lynagh committed
590
mkStablePtrPrimTy :: Type -> Type
591
mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
592 593
\end{code}

594 595 596 597 598 599 600
%************************************************************************
%*									*
\subsection[TysPrim-stable-names]{The stable-name type}
%*									*
%************************************************************************

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
601
stableNamePrimTyCon :: TyCon
602
stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName 1 PtrRep
603

Ian Lynagh's avatar
Ian Lynagh committed
604
mkStableNamePrimTy :: Type -> Type
605 606 607
mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
\end{code}

608 609
%************************************************************************
%*									*
610 611 612 613 614
\subsection[TysPrim-BCOs]{The ``bytecode object'' type}
%*									*
%************************************************************************

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
615
bcoPrimTy    :: Type
616
bcoPrimTy    = mkTyConTy bcoPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
617
bcoPrimTyCon :: TyCon
618
bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep
619 620 621 622
\end{code}
  
%************************************************************************
%*									*
623 624 625 626 627
\subsection[TysPrim-Weak]{The ``weak pointer'' type}
%*									*
%************************************************************************

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
628
weakPrimTyCon :: TyCon
629
weakPrimTyCon = pcPrimTyCon weakPrimTyConName 1 PtrRep
630

Ian Lynagh's avatar
Ian Lynagh committed
631
mkWeakPrimTy :: Type -> Type
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
\end{code}

%************************************************************************
%*									*
\subsection[TysPrim-thread-ids]{The ``thread id'' type}
%*									*
%************************************************************************

A thread id is represented by a pointer to the TSO itself, to ensure
that they are always unique and we can always find the TSO for a given
thread id.  However, this has the unfortunate consequence that a
ThreadId# for a given thread is treated as a root by the garbage
collector and can keep TSOs around for too long.

Hence the programmer API for thread manipulation uses a weak pointer
to the thread id internally.

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
651
threadIdPrimTy :: Type
652
threadIdPrimTy    = mkTyConTy threadIdPrimTyCon
Ian Lynagh's avatar
Ian Lynagh committed
653
threadIdPrimTyCon :: TyCon
654
threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep
655
\end{code}
656 657 658 659 660 661 662 663 664

%************************************************************************
%*									*
		Any
%*									*
%************************************************************************

Note [Any types]
~~~~~~~~~~~~~~~~
dreixel's avatar
dreixel committed
665
The type constructor Any of kind forall k. k -> k has these properties:
666 667 668 669 670 671 672 673 674 675 676 677

  * It is defined in module GHC.Prim, and exported so that it is 
    available to users.  For this reason it's treated like any other 
    primitive type:
      - has a fixed unique, anyTyConKey, 
      - lives in the global name cache
      - built with TyCon.PrimTyCon

  * It is lifted, and hence represented by a pointer

  * It is inhabited by at least one value, namely bottom

Ian Lynagh's avatar
Ian Lynagh committed
678
  * You can unsafely coerce any lifted type to Any, and back.
679 680 681 682 683

  * It does not claim to be a *data* type, and that's important for
    the code generator, because the code gen may *enter* a data value
    but never enters a function value. 

dreixel's avatar
dreixel committed
684
  * It is used to instantiate otherwise un-constrained type variables
685 686 687
    For example   	length Any []
    See Note [Strangely-kinded void TyCons]

dreixel's avatar
dreixel committed
688 689 690 691 692 693 694 695 696 697 698
Note [Any kinds]
~~~~~~~~~~~~~~~~

The type constructor AnyK (of sort BOX) is used internally only to zonk kind
variables with no constraints on them. It appears in similar circumstances to
Any, but at the kind level. For example:

  type family Length (l :: [k]) :: Nat
  type instance Length [] = Zero

Length is kind-polymorphic, and when applied to the empty (promoted) list it
dreixel's avatar
dreixel committed
699
will have the kind Length AnyK [].
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

Note [Strangely-kinded void TyCons]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
See Trac #959 for more examples

When the type checker finds a type variable with no binding, which
means it can be instantiated with an arbitrary type, it usually
instantiates it to Void.  Eg.

	length []
===>
	length Any (Nil Any)

But in really obscure programs, the type variable might have a kind
other than *, so we need to invent a suitably-kinded type.

This commit uses
	Any for kind *
	Any(*->*) for kind *->*
	etc

\begin{code}
anyTyConName :: Name
anyTyConName = mkPrimTc (fsLit "Any") anyTyConKey anyTyCon

Ian Lynagh's avatar
Ian Lynagh committed
725 726 727
anyTy :: Type
anyTy = mkTyConTy anyTyCon

728
anyTyCon :: TyCon
dreixel's avatar
dreixel committed
729 730
anyTyCon = mkLiftedPrimTyCon anyTyConName kind 1 PtrRep
  where kind = ForAllTy kKiVar (mkTyVarTy kKiVar)
731 732

anyTypeOfKind :: Kind -> Type
dreixel's avatar
dreixel committed
733
anyTypeOfKind kind = mkTyConApp anyTyCon [kind]
734
\end{code}