| 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 |
|---|
| 18 | module 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 | |
|---|
| 86 | import Var ( TyVar, KindVar, mkTyVar ) |
|---|
| 87 | import Name ( Name, BuiltInSyntax(..), mkInternalName, mkWiredInName ) |
|---|
| 88 | import OccName ( mkTyVarOccFS, mkTcOccFS ) |
|---|
| 89 | import TyCon |
|---|
| 90 | import TypeRep |
|---|
| 91 | import SrcLoc |
|---|
| 92 | import Unique ( mkAlphaTyVarUnique ) |
|---|
| 93 | import PrelNames |
|---|
| 94 | import FastString |
|---|
| 95 | |
|---|
| 96 | import Data.Char |
|---|
| 97 | \end{code} |
|---|
| 98 | |
|---|
| 99 | %************************************************************************ |
|---|
| 100 | %* * |
|---|
| 101 | \subsection{Primitive type constructors} |
|---|
| 102 | %* * |
|---|
| 103 | %************************************************************************ |
|---|
| 104 | |
|---|
| 105 | \begin{code} |
|---|
| 106 | primTyCons :: [TyCon] |
|---|
| 107 | primTyCons |
|---|
| 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 | |
|---|
| 147 | mkPrimTc :: FastString -> Unique -> TyCon -> Name |
|---|
| 148 | mkPrimTc fs unique tycon |
|---|
| 149 | = mkWiredInName gHC_PRIM (mkTcOccFS fs) |
|---|
| 150 | unique |
|---|
| 151 | (ATyCon tycon) -- Relevant TyCon |
|---|
| 152 | UserSyntax -- None are built-in syntax |
|---|
| 153 | |
|---|
| 154 | 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 |
|---|
| 155 | charPrimTyConName = mkPrimTc (fsLit "Char#") charPrimTyConKey charPrimTyCon |
|---|
| 156 | intPrimTyConName = mkPrimTc (fsLit "Int#") intPrimTyConKey intPrimTyCon |
|---|
| 157 | int32PrimTyConName = mkPrimTc (fsLit "Int32#") int32PrimTyConKey int32PrimTyCon |
|---|
| 158 | int64PrimTyConName = mkPrimTc (fsLit "Int64#") int64PrimTyConKey int64PrimTyCon |
|---|
| 159 | wordPrimTyConName = mkPrimTc (fsLit "Word#") wordPrimTyConKey wordPrimTyCon |
|---|
| 160 | word32PrimTyConName = mkPrimTc (fsLit "Word32#") word32PrimTyConKey word32PrimTyCon |
|---|
| 161 | word64PrimTyConName = mkPrimTc (fsLit "Word64#") word64PrimTyConKey word64PrimTyCon |
|---|
| 162 | addrPrimTyConName = mkPrimTc (fsLit "Addr#") addrPrimTyConKey addrPrimTyCon |
|---|
| 163 | floatPrimTyConName = mkPrimTc (fsLit "Float#") floatPrimTyConKey floatPrimTyCon |
|---|
| 164 | doublePrimTyConName = mkPrimTc (fsLit "Double#") doublePrimTyConKey doublePrimTyCon |
|---|
| 165 | statePrimTyConName = mkPrimTc (fsLit "State#") statePrimTyConKey statePrimTyCon |
|---|
| 166 | eqPrimTyConName = mkPrimTc (fsLit "~#") eqPrimTyConKey eqPrimTyCon |
|---|
| 167 | realWorldTyConName = mkPrimTc (fsLit "RealWorld") realWorldTyConKey realWorldTyCon |
|---|
| 168 | arrayPrimTyConName = mkPrimTc (fsLit "Array#") arrayPrimTyConKey arrayPrimTyCon |
|---|
| 169 | byteArrayPrimTyConName = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey byteArrayPrimTyCon |
|---|
| 170 | arrayArrayPrimTyConName = mkPrimTc (fsLit "ArrayArray#") arrayArrayPrimTyConKey arrayArrayPrimTyCon |
|---|
| 171 | mutableArrayPrimTyConName = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey mutableArrayPrimTyCon |
|---|
| 172 | mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey mutableByteArrayPrimTyCon |
|---|
| 173 | mutableArrayArrayPrimTyConName= mkPrimTc (fsLit "MutableArrayArray#") mutableArrayArrayPrimTyConKey mutableArrayArrayPrimTyCon |
|---|
| 174 | mutVarPrimTyConName = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey mutVarPrimTyCon |
|---|
| 175 | mVarPrimTyConName = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey mVarPrimTyCon |
|---|
| 176 | tVarPrimTyConName = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey tVarPrimTyCon |
|---|
| 177 | stablePtrPrimTyConName = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey stablePtrPrimTyCon |
|---|
| 178 | stableNamePrimTyConName = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey stableNamePrimTyCon |
|---|
| 179 | bcoPrimTyConName = mkPrimTc (fsLit "BCO#") bcoPrimTyConKey bcoPrimTyCon |
|---|
| 180 | weakPrimTyConName = mkPrimTc (fsLit "Weak#") weakPrimTyConKey weakPrimTyCon |
|---|
| 181 | threadIdPrimTyConName = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey threadIdPrimTyCon |
|---|
| 182 | \end{code} |
|---|
| 183 | |
|---|
| 184 | %************************************************************************ |
|---|
| 185 | %* * |
|---|
| 186 | \subsection{Support code} |
|---|
| 187 | %* * |
|---|
| 188 | %************************************************************************ |
|---|
| 189 | |
|---|
| 190 | alphaTyVars is a list of type variables for use in templates: |
|---|
| 191 | ["a", "b", ..., "z", "t1", "t2", ... ] |
|---|
| 192 | |
|---|
| 193 | \begin{code} |
|---|
| 194 | tyVarList :: Kind -> [TyVar] |
|---|
| 195 | tyVarList 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 | |
|---|
| 204 | alphaTyVars :: [TyVar] |
|---|
| 205 | alphaTyVars = tyVarList liftedTypeKind |
|---|
| 206 | |
|---|
| 207 | betaTyVars :: [TyVar] |
|---|
| 208 | betaTyVars = tail alphaTyVars |
|---|
| 209 | |
|---|
| 210 | alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: TyVar |
|---|
| 211 | (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars |
|---|
| 212 | |
|---|
| 213 | alphaTys :: [Type] |
|---|
| 214 | alphaTys = mkTyVarTys alphaTyVars |
|---|
| 215 | alphaTy, 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") |
|---|
| 221 | openAlphaTyVars :: [TyVar] |
|---|
| 222 | openAlphaTyVar, openBetaTyVar :: TyVar |
|---|
| 223 | openAlphaTyVars@(openAlphaTyVar:openBetaTyVar:_) = tyVarList openTypeKind |
|---|
| 224 | |
|---|
| 225 | openAlphaTy, openBetaTy :: Type |
|---|
| 226 | openAlphaTy = mkTyVarTy openAlphaTyVar |
|---|
| 227 | openBetaTy = mkTyVarTy openBetaTyVar |
|---|
| 228 | |
|---|
| 229 | argAlphaTyVars :: [TyVar] |
|---|
| 230 | argAlphaTyVar, argBetaTyVar :: TyVar |
|---|
| 231 | argAlphaTyVars@(argAlphaTyVar : argBetaTyVar : _) = tyVarList argTypeKind |
|---|
| 232 | argAlphaTy, argBetaTy :: Type |
|---|
| 233 | argAlphaTy = mkTyVarTy argAlphaTyVar |
|---|
| 234 | argBetaTy = mkTyVarTy argBetaTyVar |
|---|
| 235 | |
|---|
| 236 | kKiVar :: KindVar |
|---|
| 237 | kKiVar = (tyVarList superKind) !! 10 |
|---|
| 238 | |
|---|
| 239 | \end{code} |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | %************************************************************************ |
|---|
| 243 | %* * |
|---|
| 244 | FunTyCon |
|---|
| 245 | %* * |
|---|
| 246 | %************************************************************************ |
|---|
| 247 | |
|---|
| 248 | \begin{code} |
|---|
| 249 | funTyConName :: Name |
|---|
| 250 | funTyConName = mkPrimTyConName (fsLit "(->)") funTyConKey funTyCon |
|---|
| 251 | |
|---|
| 252 | funTyCon :: TyCon |
|---|
| 253 | funTyCon = 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 | |
|---|
| 286 | Note [SuperKind (BOX)] |
|---|
| 287 | ~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 288 | Kinds are classified by "super-kinds". There is only one super-kind, namely BOX. |
|---|
| 289 | |
|---|
| 290 | Perhaps surprisingly we give BOX the kind BOX, thus BOX :: BOX |
|---|
| 291 | Reason: we want to have kind equalities, thus (without the kind applications) |
|---|
| 292 | keq :: * ~ * = Eq# <refl *> |
|---|
| 293 | Remember 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 | |
|---|
| 298 | So the full defn of keq is |
|---|
| 299 | keq :: (~) BOX * * = Eq# BOX * * <refl *> |
|---|
| 300 | |
|---|
| 301 | So 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 |
|---|
| 306 | superKindTyCon, anyKindTyCon, liftedTypeKindTyCon, |
|---|
| 307 | openTypeKindTyCon, unliftedTypeKindTyCon, |
|---|
| 308 | ubxTupleKindTyCon, argTypeKindTyCon, |
|---|
| 309 | constraintKindTyCon |
|---|
| 310 | :: TyCon |
|---|
| 311 | superKindTyConName, anyKindTyConName, liftedTypeKindTyConName, |
|---|
| 312 | openTypeKindTyConName, unliftedTypeKindTyConName, |
|---|
| 313 | ubxTupleKindTyConName, argTypeKindTyConName, |
|---|
| 314 | constraintKindTyConName |
|---|
| 315 | :: Name |
|---|
| 316 | |
|---|
| 317 | superKindTyCon = mkKindTyCon superKindTyConName superKind |
|---|
| 318 | -- See Note [SuperKind (BOX)] |
|---|
| 319 | |
|---|
| 320 | anyKindTyCon = mkKindTyCon anyKindTyConName superKind |
|---|
| 321 | liftedTypeKindTyCon = mkKindTyCon liftedTypeKindTyConName superKind |
|---|
| 322 | openTypeKindTyCon = mkKindTyCon openTypeKindTyConName superKind |
|---|
| 323 | unliftedTypeKindTyCon = mkKindTyCon unliftedTypeKindTyConName superKind |
|---|
| 324 | ubxTupleKindTyCon = mkKindTyCon ubxTupleKindTyConName superKind |
|---|
| 325 | argTypeKindTyCon = mkKindTyCon argTypeKindTyConName superKind |
|---|
| 326 | constraintKindTyCon = mkKindTyCon constraintKindTyConName superKind |
|---|
| 327 | |
|---|
| 328 | -------------------------- |
|---|
| 329 | -- ... and now their names |
|---|
| 330 | |
|---|
| 331 | superKindTyConName = mkPrimTyConName (fsLit "BOX") superKindTyConKey superKindTyCon |
|---|
| 332 | anyKindTyConName = mkPrimTyConName (fsLit "AnyK") anyKindTyConKey anyKindTyCon |
|---|
| 333 | liftedTypeKindTyConName = mkPrimTyConName (fsLit "*") liftedTypeKindTyConKey liftedTypeKindTyCon |
|---|
| 334 | openTypeKindTyConName = mkPrimTyConName (fsLit "OpenKind") openTypeKindTyConKey openTypeKindTyCon |
|---|
| 335 | unliftedTypeKindTyConName = mkPrimTyConName (fsLit "#") unliftedTypeKindTyConKey unliftedTypeKindTyCon |
|---|
| 336 | ubxTupleKindTyConName = mkPrimTyConName (fsLit "(#)") ubxTupleKindTyConKey ubxTupleKindTyCon |
|---|
| 337 | argTypeKindTyConName = mkPrimTyConName (fsLit "ArgKind") argTypeKindTyConKey argTypeKindTyCon |
|---|
| 338 | constraintKindTyConName = mkPrimTyConName (fsLit "Constraint") constraintKindTyConKey constraintKindTyCon |
|---|
| 339 | |
|---|
| 340 | mkPrimTyConName :: FastString -> Unique -> TyCon -> Name |
|---|
| 341 | mkPrimTyConName 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} |
|---|
| 351 | kindTyConType :: TyCon -> Type |
|---|
| 352 | kindTyConType kind = TyConApp kind [] |
|---|
| 353 | |
|---|
| 354 | -- | See "Type#kind_subtyping" for details of the distinction between these 'Kind's |
|---|
| 355 | anyKind, liftedTypeKind, unliftedTypeKind, openTypeKind, |
|---|
| 356 | argTypeKind, ubxTupleKind, constraintKind, |
|---|
| 357 | superKind :: Kind |
|---|
| 358 | |
|---|
| 359 | superKind = kindTyConType superKindTyCon |
|---|
| 360 | anyKind = kindTyConType anyKindTyCon -- See Note [Any kinds] |
|---|
| 361 | liftedTypeKind = kindTyConType liftedTypeKindTyCon |
|---|
| 362 | unliftedTypeKind = kindTyConType unliftedTypeKindTyCon |
|---|
| 363 | openTypeKind = kindTyConType openTypeKindTyCon |
|---|
| 364 | argTypeKind = kindTyConType argTypeKindTyCon |
|---|
| 365 | ubxTupleKind = kindTyConType ubxTupleKindTyCon |
|---|
| 366 | constraintKind = kindTyConType constraintKindTyCon |
|---|
| 367 | |
|---|
| 368 | typeNatKind :: Kind |
|---|
| 369 | typeNatKind = kindTyConType (mkKindTyCon typeNatKindConName superKind) |
|---|
| 370 | |
|---|
| 371 | typeStringKind :: Kind |
|---|
| 372 | typeStringKind = kindTyConType (mkKindTyCon typeStringKindConName superKind) |
|---|
| 373 | |
|---|
| 374 | -- | Given two kinds @k1@ and @k2@, creates the 'Kind' @k1 -> k2@ |
|---|
| 375 | mkArrowKind :: Kind -> Kind -> Kind |
|---|
| 376 | mkArrowKind k1 k2 = FunTy k1 k2 |
|---|
| 377 | |
|---|
| 378 | -- | Iterated application of 'mkArrowKind' |
|---|
| 379 | mkArrowKinds :: [Kind] -> Kind -> Kind |
|---|
| 380 | mkArrowKinds 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 |
|---|
| 391 | pcPrimTyCon :: Name -> Int -> PrimRep -> TyCon |
|---|
| 392 | pcPrimTyCon name arity rep |
|---|
| 393 | = mkPrimTyCon name kind arity rep |
|---|
| 394 | where |
|---|
| 395 | kind = mkArrowKinds (replicate arity liftedTypeKind) result_kind |
|---|
| 396 | result_kind = unliftedTypeKind |
|---|
| 397 | |
|---|
| 398 | pcPrimTyCon0 :: Name -> PrimRep -> TyCon |
|---|
| 399 | pcPrimTyCon0 name rep |
|---|
| 400 | = mkPrimTyCon name result_kind 0 rep |
|---|
| 401 | where |
|---|
| 402 | result_kind = unliftedTypeKind |
|---|
| 403 | |
|---|
| 404 | charPrimTy :: Type |
|---|
| 405 | charPrimTy = mkTyConTy charPrimTyCon |
|---|
| 406 | charPrimTyCon :: TyCon |
|---|
| 407 | charPrimTyCon = pcPrimTyCon0 charPrimTyConName WordRep |
|---|
| 408 | |
|---|
| 409 | intPrimTy :: Type |
|---|
| 410 | intPrimTy = mkTyConTy intPrimTyCon |
|---|
| 411 | intPrimTyCon :: TyCon |
|---|
| 412 | intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep |
|---|
| 413 | |
|---|
| 414 | int32PrimTy :: Type |
|---|
| 415 | int32PrimTy = mkTyConTy int32PrimTyCon |
|---|
| 416 | int32PrimTyCon :: TyCon |
|---|
| 417 | int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName IntRep |
|---|
| 418 | |
|---|
| 419 | int64PrimTy :: Type |
|---|
| 420 | int64PrimTy = mkTyConTy int64PrimTyCon |
|---|
| 421 | int64PrimTyCon :: TyCon |
|---|
| 422 | int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep |
|---|
| 423 | |
|---|
| 424 | wordPrimTy :: Type |
|---|
| 425 | wordPrimTy = mkTyConTy wordPrimTyCon |
|---|
| 426 | wordPrimTyCon :: TyCon |
|---|
| 427 | wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep |
|---|
| 428 | |
|---|
| 429 | word32PrimTy :: Type |
|---|
| 430 | word32PrimTy = mkTyConTy word32PrimTyCon |
|---|
| 431 | word32PrimTyCon :: TyCon |
|---|
| 432 | word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName WordRep |
|---|
| 433 | |
|---|
| 434 | word64PrimTy :: Type |
|---|
| 435 | word64PrimTy = mkTyConTy word64PrimTyCon |
|---|
| 436 | word64PrimTyCon :: TyCon |
|---|
| 437 | word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep |
|---|
| 438 | |
|---|
| 439 | addrPrimTy :: Type |
|---|
| 440 | addrPrimTy = mkTyConTy addrPrimTyCon |
|---|
| 441 | addrPrimTyCon :: TyCon |
|---|
| 442 | addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep |
|---|
| 443 | |
|---|
| 444 | floatPrimTy :: Type |
|---|
| 445 | floatPrimTy = mkTyConTy floatPrimTyCon |
|---|
| 446 | floatPrimTyCon :: TyCon |
|---|
| 447 | floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep |
|---|
| 448 | |
|---|
| 449 | doublePrimTy :: Type |
|---|
| 450 | doublePrimTy = mkTyConTy doublePrimTyCon |
|---|
| 451 | doublePrimTyCon :: TyCon |
|---|
| 452 | doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep |
|---|
| 453 | \end{code} |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | %************************************************************************ |
|---|
| 457 | %* * |
|---|
| 458 | \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)} |
|---|
| 459 | %* * |
|---|
| 460 | %************************************************************************ |
|---|
| 461 | |
|---|
| 462 | Note [The ~# TyCon) |
|---|
| 463 | ~~~~~~~~~~~~~~~~~~~~ |
|---|
| 464 | There is a perfectly ordinary type constructor ~# that represents the type |
|---|
| 465 | of coercions (which, remember, are values). For example |
|---|
| 466 | Refl Int :: ~# * Int Int |
|---|
| 467 | |
|---|
| 468 | It 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 |
|---|
| 472 | assertion, in Kind.typeKind). |
|---|
| 473 | |
|---|
| 474 | Note [The State# TyCon] |
|---|
| 475 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 476 | State# is the primitive, unlifted type of states. It has one type parameter, |
|---|
| 477 | thus |
|---|
| 478 | State# RealWorld |
|---|
| 479 | or |
|---|
| 480 | State# s |
|---|
| 481 | |
|---|
| 482 | where s is a type variable. The only purpose of the type parameter is to |
|---|
| 483 | keep different state threads separate. It is represented by nothing at all. |
|---|
| 484 | |
|---|
| 485 | \begin{code} |
|---|
| 486 | mkStatePrimTy :: Type -> Type |
|---|
| 487 | mkStatePrimTy ty = mkNakedTyConApp statePrimTyCon [ty] |
|---|
| 488 | |
|---|
| 489 | statePrimTyCon :: TyCon -- See Note [The State# TyCon] |
|---|
| 490 | statePrimTyCon = pcPrimTyCon statePrimTyConName 1 VoidRep |
|---|
| 491 | |
|---|
| 492 | eqPrimTyCon :: TyCon -- The representation type for equality predicates |
|---|
| 493 | -- See Note [The ~# TyCon] |
|---|
| 494 | eqPrimTyCon = 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 | |
|---|
| 500 | RealWorld is deeply magical. It is *primitive*, but it is not |
|---|
| 501 | *unlifted* (hence ptrArg). We never manipulate values of type |
|---|
| 502 | RealWorld; it's only used in the type system, to parameterise State#. |
|---|
| 503 | |
|---|
| 504 | \begin{code} |
|---|
| 505 | realWorldTyCon :: TyCon |
|---|
| 506 | realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 PtrRep |
|---|
| 507 | realWorldTy :: Type |
|---|
| 508 | realWorldTy = mkTyConTy realWorldTyCon |
|---|
| 509 | realWorldStatePrimTy :: Type |
|---|
| 510 | realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld |
|---|
| 511 | \end{code} |
|---|
| 512 | |
|---|
| 513 | Note: the ``state-pairing'' types are not truly primitive, so they are |
|---|
| 514 | defined in \tr{TysWiredIn.lhs}, not here. |
|---|
| 515 | |
|---|
| 516 | %************************************************************************ |
|---|
| 517 | %* * |
|---|
| 518 | \subsection[TysPrim-arrays]{The primitive array types} |
|---|
| 519 | %* * |
|---|
| 520 | %************************************************************************ |
|---|
| 521 | |
|---|
| 522 | \begin{code} |
|---|
| 523 | arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon, |
|---|
| 524 | byteArrayPrimTyCon, arrayArrayPrimTyCon, mutableArrayArrayPrimTyCon :: TyCon |
|---|
| 525 | arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName 1 PtrRep |
|---|
| 526 | mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName 2 PtrRep |
|---|
| 527 | mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName 1 PtrRep |
|---|
| 528 | byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName PtrRep |
|---|
| 529 | arrayArrayPrimTyCon = pcPrimTyCon0 arrayArrayPrimTyConName PtrRep |
|---|
| 530 | mutableArrayArrayPrimTyCon = pcPrimTyCon mutableArrayArrayPrimTyConName 1 PtrRep |
|---|
| 531 | |
|---|
| 532 | mkArrayPrimTy :: Type -> Type |
|---|
| 533 | mkArrayPrimTy elt = mkNakedTyConApp arrayPrimTyCon [elt] |
|---|
| 534 | byteArrayPrimTy :: Type |
|---|
| 535 | byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon |
|---|
| 536 | mkArrayArrayPrimTy :: Type |
|---|
| 537 | mkArrayArrayPrimTy = mkTyConTy arrayArrayPrimTyCon |
|---|
| 538 | mkMutableArrayPrimTy :: Type -> Type -> Type |
|---|
| 539 | mkMutableArrayPrimTy s elt = mkNakedTyConApp mutableArrayPrimTyCon [s, elt] |
|---|
| 540 | mkMutableByteArrayPrimTy :: Type -> Type |
|---|
| 541 | mkMutableByteArrayPrimTy s = mkNakedTyConApp mutableByteArrayPrimTyCon [s] |
|---|
| 542 | mkMutableArrayArrayPrimTy :: Type -> Type |
|---|
| 543 | mkMutableArrayArrayPrimTy 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} |
|---|
| 553 | mutVarPrimTyCon :: TyCon |
|---|
| 554 | mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName 2 PtrRep |
|---|
| 555 | |
|---|
| 556 | mkMutVarPrimTy :: Type -> Type -> Type |
|---|
| 557 | mkMutVarPrimTy 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} |
|---|
| 567 | mVarPrimTyCon :: TyCon |
|---|
| 568 | mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName 2 PtrRep |
|---|
| 569 | |
|---|
| 570 | mkMVarPrimTy :: Type -> Type -> Type |
|---|
| 571 | mkMVarPrimTy 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} |
|---|
| 581 | tVarPrimTyCon :: TyCon |
|---|
| 582 | tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName 2 PtrRep |
|---|
| 583 | |
|---|
| 584 | mkTVarPrimTy :: Type -> Type -> Type |
|---|
| 585 | mkTVarPrimTy 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} |
|---|
| 595 | stablePtrPrimTyCon :: TyCon |
|---|
| 596 | stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName 1 AddrRep |
|---|
| 597 | |
|---|
| 598 | mkStablePtrPrimTy :: Type -> Type |
|---|
| 599 | mkStablePtrPrimTy 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} |
|---|
| 609 | stableNamePrimTyCon :: TyCon |
|---|
| 610 | stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName 1 PtrRep |
|---|
| 611 | |
|---|
| 612 | mkStableNamePrimTy :: Type -> Type |
|---|
| 613 | mkStableNamePrimTy ty = mkNakedTyConApp stableNamePrimTyCon [ty] |
|---|
| 614 | \end{code} |
|---|
| 615 | |
|---|
| 616 | %************************************************************************ |
|---|
| 617 | %* * |
|---|
| 618 | \subsection[TysPrim-BCOs]{The ``bytecode object'' type} |
|---|
| 619 | %* * |
|---|
| 620 | %************************************************************************ |
|---|
| 621 | |
|---|
| 622 | \begin{code} |
|---|
| 623 | bcoPrimTy :: Type |
|---|
| 624 | bcoPrimTy = mkTyConTy bcoPrimTyCon |
|---|
| 625 | bcoPrimTyCon :: TyCon |
|---|
| 626 | bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep |
|---|
| 627 | \end{code} |
|---|
| 628 | |
|---|
| 629 | %************************************************************************ |
|---|
| 630 | %* * |
|---|
| 631 | \subsection[TysPrim-Weak]{The ``weak pointer'' type} |
|---|
| 632 | %* * |
|---|
| 633 | %************************************************************************ |
|---|
| 634 | |
|---|
| 635 | \begin{code} |
|---|
| 636 | weakPrimTyCon :: TyCon |
|---|
| 637 | weakPrimTyCon = pcPrimTyCon weakPrimTyConName 1 PtrRep |
|---|
| 638 | |
|---|
| 639 | mkWeakPrimTy :: Type -> Type |
|---|
| 640 | mkWeakPrimTy v = mkNakedTyConApp weakPrimTyCon [v] |
|---|
| 641 | \end{code} |
|---|
| 642 | |
|---|
| 643 | %************************************************************************ |
|---|
| 644 | %* * |
|---|
| 645 | \subsection[TysPrim-thread-ids]{The ``thread id'' type} |
|---|
| 646 | %* * |
|---|
| 647 | %************************************************************************ |
|---|
| 648 | |
|---|
| 649 | A thread id is represented by a pointer to the TSO itself, to ensure |
|---|
| 650 | that they are always unique and we can always find the TSO for a given |
|---|
| 651 | thread id. However, this has the unfortunate consequence that a |
|---|
| 652 | ThreadId# for a given thread is treated as a root by the garbage |
|---|
| 653 | collector and can keep TSOs around for too long. |
|---|
| 654 | |
|---|
| 655 | Hence the programmer API for thread manipulation uses a weak pointer |
|---|
| 656 | to the thread id internally. |
|---|
| 657 | |
|---|
| 658 | \begin{code} |
|---|
| 659 | threadIdPrimTy :: Type |
|---|
| 660 | threadIdPrimTy = mkTyConTy threadIdPrimTyCon |
|---|
| 661 | threadIdPrimTyCon :: TyCon |
|---|
| 662 | threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep |
|---|
| 663 | \end{code} |
|---|
| 664 | |
|---|
| 665 | %************************************************************************ |
|---|
| 666 | %* * |
|---|
| 667 | Any |
|---|
| 668 | %* * |
|---|
| 669 | %************************************************************************ |
|---|
| 670 | |
|---|
| 671 | Note [Any types] |
|---|
| 672 | ~~~~~~~~~~~~~~~~ |
|---|
| 673 | The 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 | |
|---|
| 696 | Note [Any kinds] |
|---|
| 697 | ~~~~~~~~~~~~~~~~ |
|---|
| 698 | |
|---|
| 699 | The type constructor AnyK (of sort BOX) is used internally only to zonk kind |
|---|
| 700 | variables with no constraints on them. It appears in similar circumstances to |
|---|
| 701 | Any, but at the kind level. For example: |
|---|
| 702 | |
|---|
| 703 | type family Length (l :: [k]) :: Nat |
|---|
| 704 | type instance Length [] = Zero |
|---|
| 705 | |
|---|
| 706 | Length is kind-polymorphic, and when applied to the empty (promoted) list it |
|---|
| 707 | will have the kind Length AnyK []. |
|---|
| 708 | |
|---|
| 709 | Note [Strangely-kinded void TyCons] |
|---|
| 710 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 711 | See Trac #959 for more examples |
|---|
| 712 | |
|---|
| 713 | When the type checker finds a type variable with no binding, which |
|---|
| 714 | means it can be instantiated with an arbitrary type, it usually |
|---|
| 715 | instantiates it to Void. Eg. |
|---|
| 716 | |
|---|
| 717 | length [] |
|---|
| 718 | ===> |
|---|
| 719 | length Any (Nil Any) |
|---|
| 720 | |
|---|
| 721 | But in really obscure programs, the type variable might have a kind |
|---|
| 722 | other than *, so we need to invent a suitably-kinded type. |
|---|
| 723 | |
|---|
| 724 | This commit uses |
|---|
| 725 | Any for kind * |
|---|
| 726 | Any(*->*) for kind *->* |
|---|
| 727 | etc |
|---|
| 728 | |
|---|
| 729 | \begin{code} |
|---|
| 730 | anyTyConName :: Name |
|---|
| 731 | anyTyConName = mkPrimTc (fsLit "Any") anyTyConKey anyTyCon |
|---|
| 732 | |
|---|
| 733 | anyTy :: Type |
|---|
| 734 | anyTy = mkTyConTy anyTyCon |
|---|
| 735 | |
|---|
| 736 | anyTyCon :: TyCon |
|---|
| 737 | anyTyCon = mkLiftedPrimTyCon anyTyConName kind 1 PtrRep |
|---|
| 738 | where kind = ForAllTy kKiVar (mkTyVarTy kKiVar) |
|---|
| 739 | |
|---|
| 740 | anyTypeOfKind :: Kind -> Type |
|---|
| 741 | anyTypeOfKind kind = mkNakedTyConApp anyTyCon [kind] |
|---|
| 742 | \end{code} |
|---|