Comments only
[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 equality types story]
447 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
448 GHC sports a veritable menagerie of equality types:
449
450 Hetero? Levity Result Role Defining module
451 ------------------------------------------------------------
452 ~# hetero unlifted # nominal GHC.Prim
453 ~~ hetero lifted Constraint nominal GHC.Types
454 ~ homo lifted Constraint nominal Data.Type.Equality
455 :~: homo lifted * nominal Data.Type.Equality
456
457 ~R# hetero unlifted # repr GHC.Prim
458 Coercible homo lifted Constraint repr GHC.Types
459 Coercion homo lifted * repr Data.Type.Coercion
460
461 ~P# hetero unlifted phantom GHC.Prim
462
463 Recall that "hetero" means the equality can related types of different
464 kinds. Knowing that (t1 ~# t2) or (t1 ~R# t2) or even that (t1 ~P# t2)
465 also means that (k1 ~# k2), where (t1 :: k1) and (t2 :: k2).
466
467 To produce less confusion for end users, when not dumping and without
468 -fprint-equality-relations, each of these groups is printed as the bottommost
469 listed equality. That is, (~#) and (~~) are both rendered as (~) in
470 error messages, and (~R#) is rendered as Coercible.
471
472 Let's take these one at a time:
473
474 --------------------------
475 (~#) :: forall k1 k2. k1 -> k2 -> #
476 --------------------------
477 This is The Type Of Equality in GHC. It classifies nominal coercions.
478 This type is used in the solver for recording equality constraints.
479 It responds "yes" to Type.isEqPred and classifies as an EqPred in
480 Type.classifyPredType.
481
482 All wanted constraints of this type are built with coercion holes.
483 (See Note [Coercion holes] in TyCoRep.) But see also
484 Note [Deferred errors for coercion holes] in TcErrors to see how
485 equality constraints are deferred.
486
487 Within GHC, ~# is called eqPrimTyCon, and it is defined in TysPrim.
488
489
490 --------------------------
491 (~~) :: forall k1 k2. k1 -> k2 -> Constraint
492 --------------------------
493 This is (almost) an ordinary class, defined as if by
494 class a ~# b => a ~~ b
495 instance a ~# b => a ~~ b
496 Here's what's unusual about it:
497 * We can't actually declare it that way because we don't have syntax for ~#.
498 And ~# isn't a constraint, so even if we could write it, it wouldn't kind
499 check.
500
501 * Users cannot write instances of it.
502
503 * It is "naturally coherent". This means that the solver won't hesitate to
504 solve a goal of type (a ~~ b) even if there is, say (Int ~~ c) in the
505 context. (Normally, it waits to learn more, just in case the given
506 influences what happens next.) This is quite like having
507 IncoherentInstances enabled.
508
509 * It always terminates. That is, in the UndecidableInstances checks, we
510 don't worry if a (~~) constraint is too big, as we know that solving
511 equality terminates.
512
513 On the other hand, this behaves just like any class w.r.t. eager superclass
514 unpacking in the solver. So a lifted equality given quickly becomes an unlifted
515 equality given. This is good, because the solver knows all about unlifted
516 equalities. There is some special-casing in TcInteract.matchClassInst to
517 pretend that there is an instance of this class, as we can't write the instance
518 in Haskell.
519
520 Within GHC, ~~ is called heqTyCon, and it is defined in TysWiredIn.
521
522
523 --------------------------
524 (~) :: forall k. k -> k -> Constraint
525 --------------------------
526 This is defined in Data.Type.Equality:
527 class a ~~ b => (a :: k) ~ (b :: k)
528 instance a ~~ b => a ~ b
529 This is even more so an ordinary class than (~~), with the following exceptions:
530 * Users cannot write instances of it.
531
532 * It is "naturally coherent". (See (~~).)
533
534 * (~) is magical syntax, as ~ is a reserved symbol. It cannot be exported
535 or imported.
536
537 * It always terminates.
538
539 Within GHC, ~ is called eqTyCon, and it is defined in PrelNames. Note that
540 it is *not* wired in.
541
542
543 --------------------------
544 (:~:) :: forall k. k -> k -> *
545 --------------------------
546 This is a perfectly ordinary GADT, wrapping (~). It is not defined within
547 GHC at all.
548
549
550 --------------------------
551 (~R#) :: forall k1 k2. k1 -> k2 -> #
552 --------------------------
553 The is the representational analogue of ~#. This is the type of representational
554 equalities that the solver works on. All wanted constraints of this type are
555 built with coercion holes.
556
557 Within GHC, ~R# is called eqReprPrimTyCon, and it is defined in TysPrim.
558
559
560 --------------------------
561 Coercible :: forall k. k -> k -> Constraint
562 --------------------------
563 This is quite like (~~) in the way it's defined and treated within GHC, but
564 it's homogeneous. Homogeneity helps with type inference (as GHC can solve one
565 kind from the other) and, in my (Richard's) estimation, will be more intuitive
566 for users.
567
568 An alternative design included HCoercible (like (~~)) and Coercible (like (~)).
569 One annoyance was that we want `coerce :: Coercible a b => a -> b`, and
570 we need the type of coerce to be fully wired-in. So the HCoercible/Coercible
571 split required that both types be fully wired-in. Instead of doing this,
572 I just got rid of HCoercible, as I'm not sure who would use it, anyway.
573
574 Within GHC, Coercible is called coercibleTyCon, and it is defined in
575 TysWiredIn.
576
577
578 --------------------------
579 Coercion :: forall k. k -> k -> *
580 --------------------------
581 This is a perfectly ordinary GADT, wrapping Coercible. It is not defined
582 within GHC at all.
583
584
585 --------------------------
586 (~P#) :: forall k1 k2. k1 -> k2 -> #
587 --------------------------
588 This is the phantom analogue of ~# and it is barely used at all.
589 (The solver has no idea about this one.) Here is the motivation:
590
591 data Phant a = MkPhant
592 type role Phant phantom
593
594 Phant <Int, Bool>_P :: Phant Int ~P# Phant Bool
595
596 We just need to have something to put on that last line. You probably
597 don't need to worry about it.
598
599
600
601 Note [The State# TyCon]
602 ~~~~~~~~~~~~~~~~~~~~~~~
603 State# is the primitive, unlifted type of states. It has one type parameter,
604 thus
605 State# RealWorld
606 or
607 State# s
608
609 where s is a type variable. The only purpose of the type parameter is to
610 keep different state threads separate. It is represented by nothing at all.
611
612 The type parameter to State# is intended to keep separate threads separate.
613 Even though this parameter is not used in the definition of State#, it is
614 given role Nominal to enforce its intended use.
615 -}
616
617 mkStatePrimTy :: Type -> Type
618 mkStatePrimTy ty = TyConApp statePrimTyCon [ty]
619
620 statePrimTyCon :: TyCon -- See Note [The State# TyCon]
621 statePrimTyCon = pcPrimTyCon statePrimTyConName [Nominal] VoidRep
622
623 {-
624 RealWorld is deeply magical. It is *primitive*, but it is not
625 *unlifted* (hence ptrArg). We never manipulate values of type
626 RealWorld; it's only used in the type system, to parameterise State#.
627 -}
628
629 realWorldTyCon :: TyCon
630 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind [] PtrRep
631 realWorldTy :: Type
632 realWorldTy = mkTyConTy realWorldTyCon
633 realWorldStatePrimTy :: Type
634 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
635
636 -- Note: the ``state-pairing'' types are not truly primitive,
637 -- so they are defined in \tr{TysWiredIn.hs}, not here.
638
639
640 voidPrimTy :: Type
641 voidPrimTy = TyConApp voidPrimTyCon []
642
643 voidPrimTyCon :: TyCon
644 voidPrimTyCon = pcPrimTyCon voidPrimTyConName [] VoidRep
645
646 mkProxyPrimTy :: Type -> Type -> Type
647 mkProxyPrimTy k ty = TyConApp proxyPrimTyCon [k, ty]
648
649 proxyPrimTyCon :: TyCon
650 proxyPrimTyCon = mkPrimTyCon proxyPrimTyConName kind [Nominal,Nominal] VoidRep
651 where kind = ForAllTy (Named kv Invisible) $
652 mkFunTy k unliftedTypeKind
653 kv = kKiVar
654 k = mkTyVarTy kv
655
656
657 {- *********************************************************************
658 * *
659 Primitive equality constraints
660 See Note [The equality types story]
661 * *
662 ********************************************************************* -}
663
664 eqPrimTyCon :: TyCon -- The representation type for equality predicates
665 -- See Note [The equality types story]
666 eqPrimTyCon = mkPrimTyCon eqPrimTyConName kind roles VoidRep
667 where kind = ForAllTy (Named kv1 Invisible) $
668 ForAllTy (Named kv2 Invisible) $
669 mkFunTys [k1, k2] unliftedTypeKind
670 [kv1, kv2] = mkTemplateTyVars [liftedTypeKind, liftedTypeKind]
671 k1 = mkTyVarTy kv1
672 k2 = mkTyVarTy kv2
673 roles = [Nominal, Nominal, Nominal, Nominal]
674
675 -- like eqPrimTyCon, but the type for *Representational* coercions
676 -- this should only ever appear as the type of a covar. Its role is
677 -- interpreted in coercionRole
678 eqReprPrimTyCon :: TyCon -- See Note [The equality types story]
679 eqReprPrimTyCon = mkPrimTyCon eqReprPrimTyConName kind
680 roles VoidRep
681 where kind = ForAllTy (Named kv1 Invisible) $
682 ForAllTy (Named kv2 Invisible) $
683 mkFunTys [k1, k2] unliftedTypeKind
684 [kv1, kv2] = mkTemplateTyVars [liftedTypeKind, liftedTypeKind]
685 k1 = mkTyVarTy kv1
686 k2 = mkTyVarTy kv2
687 roles = [Nominal, Nominal, Representational, Representational]
688
689 -- like eqPrimTyCon, but the type for *Phantom* coercions.
690 -- This is only used to make higher-order equalities. Nothing
691 -- should ever actually have this type!
692 eqPhantPrimTyCon :: TyCon
693 eqPhantPrimTyCon = mkPrimTyCon eqPhantPrimTyConName kind
694 [Nominal, Nominal, Phantom, Phantom]
695 VoidRep
696 where kind = ForAllTy (Named kv1 Invisible) $
697 ForAllTy (Named kv2 Invisible) $
698 mkFunTys [k1, k2] unliftedTypeKind
699 [kv1, kv2] = mkTemplateTyVars [liftedTypeKind, liftedTypeKind]
700 k1 = mkTyVarTy kv1
701 k2 = mkTyVarTy kv2
702
703
704 {- *********************************************************************
705 * *
706 The primitive array types
707 * *
708 ********************************************************************* -}
709
710 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
711 byteArrayPrimTyCon, arrayArrayPrimTyCon, mutableArrayArrayPrimTyCon,
712 smallArrayPrimTyCon, smallMutableArrayPrimTyCon :: TyCon
713 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName [Representational] PtrRep
714 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName [Nominal, Representational] PtrRep
715 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName [Nominal] PtrRep
716 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName PtrRep
717 arrayArrayPrimTyCon = pcPrimTyCon0 arrayArrayPrimTyConName PtrRep
718 mutableArrayArrayPrimTyCon = pcPrimTyCon mutableArrayArrayPrimTyConName [Nominal] PtrRep
719 smallArrayPrimTyCon = pcPrimTyCon smallArrayPrimTyConName [Representational] PtrRep
720 smallMutableArrayPrimTyCon = pcPrimTyCon smallMutableArrayPrimTyConName [Nominal, Representational] PtrRep
721
722 mkArrayPrimTy :: Type -> Type
723 mkArrayPrimTy elt = TyConApp arrayPrimTyCon [elt]
724 byteArrayPrimTy :: Type
725 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
726 mkArrayArrayPrimTy :: Type
727 mkArrayArrayPrimTy = mkTyConTy arrayArrayPrimTyCon
728 mkSmallArrayPrimTy :: Type -> Type
729 mkSmallArrayPrimTy elt = TyConApp smallArrayPrimTyCon [elt]
730 mkMutableArrayPrimTy :: Type -> Type -> Type
731 mkMutableArrayPrimTy s elt = TyConApp mutableArrayPrimTyCon [s, elt]
732 mkMutableByteArrayPrimTy :: Type -> Type
733 mkMutableByteArrayPrimTy s = TyConApp mutableByteArrayPrimTyCon [s]
734 mkMutableArrayArrayPrimTy :: Type -> Type
735 mkMutableArrayArrayPrimTy s = TyConApp mutableArrayArrayPrimTyCon [s]
736 mkSmallMutableArrayPrimTy :: Type -> Type -> Type
737 mkSmallMutableArrayPrimTy s elt = TyConApp smallMutableArrayPrimTyCon [s, elt]
738
739
740 {- *********************************************************************
741 * *
742 The mutable variable type
743 * *
744 ********************************************************************* -}
745
746 mutVarPrimTyCon :: TyCon
747 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName [Nominal, Representational] PtrRep
748
749 mkMutVarPrimTy :: Type -> Type -> Type
750 mkMutVarPrimTy s elt = TyConApp mutVarPrimTyCon [s, elt]
751
752 {-
753 ************************************************************************
754 * *
755 \subsection[TysPrim-synch-var]{The synchronizing variable type}
756 * *
757 ************************************************************************
758 -}
759
760 mVarPrimTyCon :: TyCon
761 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName [Nominal, Representational] PtrRep
762
763 mkMVarPrimTy :: Type -> Type -> Type
764 mkMVarPrimTy s elt = TyConApp mVarPrimTyCon [s, elt]
765
766 {-
767 ************************************************************************
768 * *
769 \subsection[TysPrim-stm-var]{The transactional variable type}
770 * *
771 ************************************************************************
772 -}
773
774 tVarPrimTyCon :: TyCon
775 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName [Nominal, Representational] PtrRep
776
777 mkTVarPrimTy :: Type -> Type -> Type
778 mkTVarPrimTy s elt = TyConApp tVarPrimTyCon [s, elt]
779
780 {-
781 ************************************************************************
782 * *
783 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
784 * *
785 ************************************************************************
786 -}
787
788 stablePtrPrimTyCon :: TyCon
789 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName [Representational] AddrRep
790
791 mkStablePtrPrimTy :: Type -> Type
792 mkStablePtrPrimTy ty = TyConApp stablePtrPrimTyCon [ty]
793
794 {-
795 ************************************************************************
796 * *
797 \subsection[TysPrim-stable-names]{The stable-name type}
798 * *
799 ************************************************************************
800 -}
801
802 stableNamePrimTyCon :: TyCon
803 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName [Representational] PtrRep
804
805 mkStableNamePrimTy :: Type -> Type
806 mkStableNamePrimTy ty = TyConApp stableNamePrimTyCon [ty]
807
808 {-
809 ************************************************************************
810 * *
811 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
812 * *
813 ************************************************************************
814 -}
815
816 bcoPrimTy :: Type
817 bcoPrimTy = mkTyConTy bcoPrimTyCon
818 bcoPrimTyCon :: TyCon
819 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep
820
821 {-
822 ************************************************************************
823 * *
824 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
825 * *
826 ************************************************************************
827 -}
828
829 weakPrimTyCon :: TyCon
830 weakPrimTyCon = pcPrimTyCon weakPrimTyConName [Representational] PtrRep
831
832 mkWeakPrimTy :: Type -> Type
833 mkWeakPrimTy v = TyConApp weakPrimTyCon [v]
834
835 {-
836 ************************************************************************
837 * *
838 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
839 * *
840 ************************************************************************
841
842 A thread id is represented by a pointer to the TSO itself, to ensure
843 that they are always unique and we can always find the TSO for a given
844 thread id. However, this has the unfortunate consequence that a
845 ThreadId# for a given thread is treated as a root by the garbage
846 collector and can keep TSOs around for too long.
847
848 Hence the programmer API for thread manipulation uses a weak pointer
849 to the thread id internally.
850 -}
851
852 threadIdPrimTy :: Type
853 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
854 threadIdPrimTyCon :: TyCon
855 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep
856
857 {-
858 ************************************************************************
859 * *
860 Any
861 * *
862 ************************************************************************
863
864 Note [Any types]
865 ~~~~~~~~~~~~~~~~
866 The type constructor Any of kind forall k. k has these properties:
867
868 * It is defined in module GHC.Prim, and exported so that it is
869 available to users. For this reason it's treated like any other
870 primitive type:
871 - has a fixed unique, anyTyConKey,
872 - lives in the global name cache
873
874 * It is a *closed* type family, with no instances. This means that
875 if ty :: '(k1, k2) we add a given coercion
876 g :: ty ~ (Fst ty, Snd ty)
877 If Any was a *data* type, then we'd get inconsistency because 'ty'
878 could be (Any '(k1,k2)) and then we'd have an equality with Any on
879 one side and '(,) on the other. See also #9097.
880
881 * It is lifted, and hence represented by a pointer
882
883 * It is inhabited by at least one value, namely bottom
884
885 * You can unsafely coerce any lifted type to Any, and back.
886
887 * It does not claim to be a *data* type, and that's important for
888 the code generator, because the code gen may *enter* a data value
889 but never enters a function value.
890
891 * It is used to instantiate otherwise un-constrained type variables
892 For example length Any []
893 See Note [Strangely-kinded void TyCons]
894
895 Note [Strangely-kinded void TyCons]
896 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
897 See Trac #959 for more examples
898
899 When the type checker finds a type variable with no binding, which
900 means it can be instantiated with an arbitrary type, it usually
901 instantiates it to Void. Eg.
902
903 length []
904 ===>
905 length Any (Nil Any)
906
907 But in really obscure programs, the type variable might have a kind
908 other than *, so we need to invent a suitably-kinded type.
909
910 This commit uses
911 Any for kind *
912 Any(*->*) for kind *->*
913 etc
914 -}
915
916 anyTyConName :: Name
917 anyTyConName = mkPrimTc (fsLit "Any") anyTyConKey anyTyCon
918
919 anyTy :: Type
920 anyTy = mkTyConTy anyTyCon
921
922 anyTyCon :: TyCon
923 anyTyCon = mkFamilyTyCon anyTyConName kind [kKiVar] Nothing
924 (ClosedSynFamilyTyCon Nothing)
925 Nothing
926 NotInjective
927 where
928 kind = ForAllTy (Named kKiVar Invisible) (mkTyVarTy kKiVar)
929
930 anyTypeOfKind :: Kind -> Type
931 anyTypeOfKind kind = TyConApp anyTyCon [kind]
932
933 {-
934 ************************************************************************
935 * *
936 \subsection{SIMD vector types}
937 * *
938 ************************************************************************
939 -}
940
941 #include "primop-vector-tys.hs-incl"