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