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