Address #11471 by putting RuntimeRep in kinds.
[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 runtimeRep1TyVar, runtimeRep2TyVar, runtimeRep1Ty, runtimeRep2Ty,
19 openAlphaTy, openBetaTy, openAlphaTyVar, openBetaTyVar,
20 kKiVar,
21
22 -- Kind constructors...
23 tYPETyConName, unliftedTypeKindTyConName,
24
25 -- Kinds
26 tYPE,
27
28 funTyCon, funTyConName,
29 primTyCons,
30
31 charPrimTyCon, charPrimTy,
32 intPrimTyCon, intPrimTy,
33 wordPrimTyCon, wordPrimTy,
34 addrPrimTyCon, addrPrimTy,
35 floatPrimTyCon, floatPrimTy,
36 doublePrimTyCon, doublePrimTy,
37
38 voidPrimTyCon, voidPrimTy,
39 statePrimTyCon, mkStatePrimTy,
40 realWorldTyCon, realWorldTy, realWorldStatePrimTy,
41
42 proxyPrimTyCon, mkProxyPrimTy,
43
44 arrayPrimTyCon, mkArrayPrimTy,
45 byteArrayPrimTyCon, byteArrayPrimTy,
46 arrayArrayPrimTyCon, mkArrayArrayPrimTy,
47 smallArrayPrimTyCon, mkSmallArrayPrimTy,
48 mutableArrayPrimTyCon, mkMutableArrayPrimTy,
49 mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
50 mutableArrayArrayPrimTyCon, mkMutableArrayArrayPrimTy,
51 smallMutableArrayPrimTyCon, mkSmallMutableArrayPrimTy,
52 mutVarPrimTyCon, mkMutVarPrimTy,
53
54 mVarPrimTyCon, mkMVarPrimTy,
55 tVarPrimTyCon, mkTVarPrimTy,
56 stablePtrPrimTyCon, mkStablePtrPrimTy,
57 stableNamePrimTyCon, mkStableNamePrimTy,
58 bcoPrimTyCon, bcoPrimTy,
59 weakPrimTyCon, mkWeakPrimTy,
60 threadIdPrimTyCon, threadIdPrimTy,
61
62 int32PrimTyCon, int32PrimTy,
63 word32PrimTyCon, word32PrimTy,
64
65 int64PrimTyCon, int64PrimTy,
66 word64PrimTyCon, word64PrimTy,
67
68 eqPrimTyCon, -- ty1 ~# ty2
69 eqReprPrimTyCon, -- ty1 ~R# ty2 (at role Representational)
70 eqPhantPrimTyCon, -- ty1 ~P# ty2 (at role Phantom)
71
72 -- * Any
73 anyTy, anyTyCon, anyTypeOfKind,
74
75 -- * SIMD
76 #include "primop-vector-tys-exports.hs-incl"
77 ) where
78
79 #include "HsVersions.h"
80
81 import {-# SOURCE #-} TysWiredIn
82 ( runtimeRepTy, liftedTypeKind
83 , vecRepDataConTyCon, ptrRepUnliftedDataConTyCon
84 , voidRepDataConTy, intRepDataConTy
85 , wordRepDataConTy, int64RepDataConTy, word64RepDataConTy, addrRepDataConTy
86 , floatRepDataConTy, doubleRepDataConTy
87 , vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy
88 , vec64DataConTy
89 , int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy
90 , int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy
91 , word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy
92 , doubleElemRepDataConTy )
93
94 import Var ( TyVar, KindVar, mkTyVar )
95 import Name
96 import TyCon
97 import SrcLoc
98 import Unique
99 import PrelNames
100 import FastString
101 import Outputable
102 import TyCoRep -- doesn't need special access, but this is easier to avoid
103 -- import loops
104
105 import Data.Char
106
107 {-
108 ************************************************************************
109 * *
110 \subsection{Primitive type constructors}
111 * *
112 ************************************************************************
113 -}
114
115 primTyCons :: [TyCon]
116 primTyCons
117 = [ addrPrimTyCon
118 , arrayPrimTyCon
119 , byteArrayPrimTyCon
120 , arrayArrayPrimTyCon
121 , smallArrayPrimTyCon
122 , charPrimTyCon
123 , doublePrimTyCon
124 , floatPrimTyCon
125 , intPrimTyCon
126 , int32PrimTyCon
127 , int64PrimTyCon
128 , bcoPrimTyCon
129 , weakPrimTyCon
130 , mutableArrayPrimTyCon
131 , mutableByteArrayPrimTyCon
132 , mutableArrayArrayPrimTyCon
133 , smallMutableArrayPrimTyCon
134 , mVarPrimTyCon
135 , tVarPrimTyCon
136 , mutVarPrimTyCon
137 , realWorldTyCon
138 , stablePtrPrimTyCon
139 , stableNamePrimTyCon
140 , statePrimTyCon
141 , voidPrimTyCon
142 , proxyPrimTyCon
143 , threadIdPrimTyCon
144 , wordPrimTyCon
145 , word32PrimTyCon
146 , word64PrimTyCon
147 , anyTyCon
148 , eqPrimTyCon
149 , eqReprPrimTyCon
150 , eqPhantPrimTyCon
151
152 , unliftedTypeKindTyCon
153 , tYPETyCon
154
155 #include "primop-vector-tycons.hs-incl"
156 ]
157
158 mkPrimTc :: FastString -> Unique -> TyCon -> Name
159 mkPrimTc fs unique tycon
160 = mkWiredInName gHC_PRIM (mkTcOccFS fs)
161 unique
162 (ATyCon tycon) -- Relevant TyCon
163 UserSyntax
164
165 mkBuiltInPrimTc :: FastString -> Unique -> TyCon -> Name
166 mkBuiltInPrimTc fs unique tycon
167 = mkWiredInName gHC_PRIM (mkTcOccFS fs)
168 unique
169 (ATyCon tycon) -- Relevant TyCon
170 BuiltInSyntax
171
172
173 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
174 charPrimTyConName = mkPrimTc (fsLit "Char#") charPrimTyConKey charPrimTyCon
175 intPrimTyConName = mkPrimTc (fsLit "Int#") intPrimTyConKey intPrimTyCon
176 int32PrimTyConName = mkPrimTc (fsLit "Int32#") int32PrimTyConKey int32PrimTyCon
177 int64PrimTyConName = mkPrimTc (fsLit "Int64#") int64PrimTyConKey int64PrimTyCon
178 wordPrimTyConName = mkPrimTc (fsLit "Word#") wordPrimTyConKey wordPrimTyCon
179 word32PrimTyConName = mkPrimTc (fsLit "Word32#") word32PrimTyConKey word32PrimTyCon
180 word64PrimTyConName = mkPrimTc (fsLit "Word64#") word64PrimTyConKey word64PrimTyCon
181 addrPrimTyConName = mkPrimTc (fsLit "Addr#") addrPrimTyConKey addrPrimTyCon
182 floatPrimTyConName = mkPrimTc (fsLit "Float#") floatPrimTyConKey floatPrimTyCon
183 doublePrimTyConName = mkPrimTc (fsLit "Double#") doublePrimTyConKey doublePrimTyCon
184 statePrimTyConName = mkPrimTc (fsLit "State#") statePrimTyConKey statePrimTyCon
185 voidPrimTyConName = mkPrimTc (fsLit "Void#") voidPrimTyConKey voidPrimTyCon
186 proxyPrimTyConName = mkPrimTc (fsLit "Proxy#") proxyPrimTyConKey proxyPrimTyCon
187 eqPrimTyConName = mkPrimTc (fsLit "~#") eqPrimTyConKey eqPrimTyCon
188 eqReprPrimTyConName = mkBuiltInPrimTc (fsLit "~R#") eqReprPrimTyConKey eqReprPrimTyCon
189 eqPhantPrimTyConName = mkBuiltInPrimTc (fsLit "~P#") eqPhantPrimTyConKey eqPhantPrimTyCon
190 realWorldTyConName = mkPrimTc (fsLit "RealWorld") realWorldTyConKey realWorldTyCon
191 arrayPrimTyConName = mkPrimTc (fsLit "Array#") arrayPrimTyConKey arrayPrimTyCon
192 byteArrayPrimTyConName = mkPrimTc (fsLit "ByteArray#") byteArrayPrimTyConKey byteArrayPrimTyCon
193 arrayArrayPrimTyConName = mkPrimTc (fsLit "ArrayArray#") arrayArrayPrimTyConKey arrayArrayPrimTyCon
194 smallArrayPrimTyConName = mkPrimTc (fsLit "SmallArray#") smallArrayPrimTyConKey smallArrayPrimTyCon
195 mutableArrayPrimTyConName = mkPrimTc (fsLit "MutableArray#") mutableArrayPrimTyConKey mutableArrayPrimTyCon
196 mutableByteArrayPrimTyConName = mkPrimTc (fsLit "MutableByteArray#") mutableByteArrayPrimTyConKey mutableByteArrayPrimTyCon
197 mutableArrayArrayPrimTyConName= mkPrimTc (fsLit "MutableArrayArray#") mutableArrayArrayPrimTyConKey mutableArrayArrayPrimTyCon
198 smallMutableArrayPrimTyConName= mkPrimTc (fsLit "SmallMutableArray#") smallMutableArrayPrimTyConKey smallMutableArrayPrimTyCon
199 mutVarPrimTyConName = mkPrimTc (fsLit "MutVar#") mutVarPrimTyConKey mutVarPrimTyCon
200 mVarPrimTyConName = mkPrimTc (fsLit "MVar#") mVarPrimTyConKey mVarPrimTyCon
201 tVarPrimTyConName = mkPrimTc (fsLit "TVar#") tVarPrimTyConKey tVarPrimTyCon
202 stablePtrPrimTyConName = mkPrimTc (fsLit "StablePtr#") stablePtrPrimTyConKey stablePtrPrimTyCon
203 stableNamePrimTyConName = mkPrimTc (fsLit "StableName#") stableNamePrimTyConKey stableNamePrimTyCon
204 bcoPrimTyConName = mkPrimTc (fsLit "BCO#") bcoPrimTyConKey bcoPrimTyCon
205 weakPrimTyConName = mkPrimTc (fsLit "Weak#") weakPrimTyConKey weakPrimTyCon
206 threadIdPrimTyConName = mkPrimTc (fsLit "ThreadId#") threadIdPrimTyConKey threadIdPrimTyCon
207
208 {-
209 ************************************************************************
210 * *
211 \subsection{Support code}
212 * *
213 ************************************************************************
214
215 alphaTyVars is a list of type variables for use in templates:
216 ["a", "b", ..., "z", "t1", "t2", ... ]
217 -}
218
219 mkTemplateTyVars :: [Kind] -> [TyVar]
220 mkTemplateTyVars kinds =
221 [ mkTyVar (mkInternalName (mkAlphaTyVarUnique u)
222 (mkTyVarOccFS (mkFastString name))
223 noSrcSpan) k
224 | (k,u) <- zip kinds [2..],
225 let name | c <= 'z' = [c]
226 | otherwise = 't':show u
227 where c = chr (u-2 + ord 'a')
228 ]
229
230 alphaTyVars :: [TyVar]
231 alphaTyVars = mkTemplateTyVars $ repeat liftedTypeKind
232
233 alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar :: TyVar
234 (alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
235
236 alphaTys :: [Type]
237 alphaTys = mkTyVarTys alphaTyVars
238 alphaTy, betaTy, gammaTy, deltaTy :: Type
239 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
240
241 runtimeRep1TyVar, runtimeRep2TyVar :: TyVar
242 (runtimeRep1TyVar : runtimeRep2TyVar : _)
243 = drop 16 (mkTemplateTyVars (repeat runtimeRepTy)) -- selects 'q','r'
244
245 runtimeRep1Ty, runtimeRep2Ty :: Type
246 runtimeRep1Ty = mkTyVarTy runtimeRep1TyVar
247 runtimeRep2Ty = mkTyVarTy runtimeRep2TyVar
248
249 openAlphaTyVar, openBetaTyVar :: TyVar
250 [openAlphaTyVar,openBetaTyVar]
251 = mkTemplateTyVars [tYPE runtimeRep1Ty, tYPE runtimeRep2Ty]
252
253 openAlphaTy, openBetaTy :: Type
254 openAlphaTy = mkTyVarTy openAlphaTyVar
255 openBetaTy = mkTyVarTy openBetaTyVar
256
257 kKiVar :: KindVar
258 kKiVar = (mkTemplateTyVars $ repeat liftedTypeKind) !! 10
259 -- the 10 selects the 11th letter in the alphabet: 'k'
260
261 {-
262 ************************************************************************
263 * *
264 FunTyCon
265 * *
266 ************************************************************************
267 -}
268
269 funTyConName :: Name
270 funTyConName = mkPrimTyConName (fsLit "(->)") funTyConKey funTyCon
271
272 funTyCon :: TyCon
273 funTyCon = mkFunTyCon funTyConName (map Anon [liftedTypeKind, liftedTypeKind])
274 tc_rep_nm
275 where
276 -- You might think that (->) should have type (?? -> ? -> *), and you'd be right
277 -- But if we do that we get kind errors when saying
278 -- instance Control.Arrow (->)
279 -- because the expected kind is (*->*->*). The trouble is that the
280 -- expected/actual stuff in the unifier does not go contra-variant, whereas
281 -- the kind sub-typing does. Sigh. It really only matters if you use (->) in
282 -- a prefix way, thus: (->) Int# Int#. And this is unusual.
283 -- because they are never in scope in the source
284
285 tc_rep_nm = mkPrelTyConRepName funTyConName
286
287 {-
288 ************************************************************************
289 * *
290 Kinds
291 * *
292 ************************************************************************
293
294 Note [TYPE]
295 ~~~~~~~~~~~
296 There are a few places where we wish to be able to deal interchangeably
297 with kind * and kind #. unsafeCoerce#, error, and (->) are some of these
298 places. The way we do this is to use runtime-representation polymorphism.
299
300 We have
301
302 data RuntimeRep = PtrRepLifted | PtrRepUnlifted | ...
303
304 and a magical constant (tYPETyCon)
305
306 TYPE :: RuntimeRep -> TYPE PtrRepLifted
307
308 We then have synonyms (liftedTypeKindTyCon, unliftedTypeKindTyCon)
309
310 type * = TYPE PtrRepLifted
311 type # = TYPE PtrRepUnlifted
312
313 The (...) in the definition for RuntimeRep includes possibilities for
314 the unboxed, unlifted representations, isomorphic to the PrimRep type
315 in TyCon. RuntimeRep is itself declared in GHC.Types.
316
317 An alternative design would be to have
318
319 data RuntimeRep = PtrRep Levity | ...
320 data Levity = Lifted | Unlifted
321
322 but this slowed down GHC because every time we looked at *, we had to
323 follow a bunch of pointers. When we have unpackable sums, we should
324 go back to the stratified representation. This would allow, for example:
325
326 unsafeCoerce# :: forall (r1 :: RuntimeRep) (v2 :: Levity)
327 (a :: TYPE v1) (b :: TYPE v2). a -> b
328
329 TYPE replaces the old sub-kinding machinery. We call variables `a` and `b`
330 above "runtime-representation polymorphic".
331
332 -}
333
334 tYPETyCon, unliftedTypeKindTyCon :: TyCon
335 tYPETyConName, unliftedTypeKindTyConName :: Name
336
337 tYPETyCon = mkKindTyCon tYPETyConName
338 [Anon runtimeRepTy]
339 liftedTypeKind
340 [Nominal]
341 (mkPrelTyConRepName tYPETyConName)
342
343 -- See Note [TYPE]
344 -- NB: unlifted is wired in because there is no way to parse it in
345 -- Haskell. That's the only reason for wiring it in.
346 unliftedTypeKindTyCon = mkSynonymTyCon unliftedTypeKindTyConName
347 [] liftedTypeKind
348 [] []
349 (tYPE (TyConApp ptrRepUnliftedDataConTyCon []))
350
351 --------------------------
352 -- ... and now their names
353
354 -- If you edit these, you may need to update the GHC formalism
355 -- See Note [GHC Formalism] in coreSyn/CoreLint.hs
356 tYPETyConName = mkPrimTyConName (fsLit "TYPE") tYPETyConKey tYPETyCon
357 unliftedTypeKindTyConName = mkPrimTyConName (fsLit "#") unliftedTypeKindTyConKey unliftedTypeKindTyCon
358
359 mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
360 mkPrimTyConName = mkPrimTcName BuiltInSyntax
361 -- All of the super kinds and kinds are defined in Prim,
362 -- and use BuiltInSyntax, because they are never in scope in the source
363
364 mkPrimTcName :: BuiltInSyntax -> FastString -> Unique -> TyCon -> Name
365 mkPrimTcName built_in_syntax occ key tycon
366 = mkWiredInName gHC_PRIM (mkTcOccFS occ) key (ATyCon tycon) built_in_syntax
367
368 -----------------------------
369 -- | Given a RuntimeRep, applies TYPE to it. See Note [TYPE].
370 tYPE :: Type -> Type
371 tYPE rr = TyConApp tYPETyCon [rr]
372
373 {-
374 ************************************************************************
375 * *
376 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
377 * *
378 ************************************************************************
379 -}
380
381 -- only used herein
382 pcPrimTyCon :: Name -> [Role] -> PrimRep -> TyCon
383 pcPrimTyCon name roles rep
384 = mkPrimTyCon name binders result_kind roles
385 where
386 binders = map (const (Anon liftedTypeKind)) roles
387 result_kind = tYPE rr
388
389 rr = case rep of
390 VoidRep -> voidRepDataConTy
391 PtrRep -> TyConApp ptrRepUnliftedDataConTyCon []
392 IntRep -> intRepDataConTy
393 WordRep -> wordRepDataConTy
394 Int64Rep -> int64RepDataConTy
395 Word64Rep -> word64RepDataConTy
396 AddrRep -> addrRepDataConTy
397 FloatRep -> floatRepDataConTy
398 DoubleRep -> doubleRepDataConTy
399 VecRep n elem -> TyConApp vecRepDataConTyCon [n', elem']
400 where
401 n' = case n of
402 2 -> vec2DataConTy
403 4 -> vec4DataConTy
404 8 -> vec8DataConTy
405 16 -> vec16DataConTy
406 32 -> vec32DataConTy
407 64 -> vec64DataConTy
408 _ -> pprPanic "Disallowed VecCount" (ppr n)
409
410 elem' = case elem of
411 Int8ElemRep -> int8ElemRepDataConTy
412 Int16ElemRep -> int16ElemRepDataConTy
413 Int32ElemRep -> int32ElemRepDataConTy
414 Int64ElemRep -> int64ElemRepDataConTy
415 Word8ElemRep -> word8ElemRepDataConTy
416 Word16ElemRep -> word16ElemRepDataConTy
417 Word32ElemRep -> word32ElemRepDataConTy
418 Word64ElemRep -> word64ElemRepDataConTy
419 FloatElemRep -> floatElemRepDataConTy
420 DoubleElemRep -> doubleElemRepDataConTy
421
422
423 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
424 pcPrimTyCon0 name rep
425 = pcPrimTyCon name [] rep
426
427 charPrimTy :: Type
428 charPrimTy = mkTyConTy charPrimTyCon
429 charPrimTyCon :: TyCon
430 charPrimTyCon = pcPrimTyCon0 charPrimTyConName WordRep
431
432 intPrimTy :: Type
433 intPrimTy = mkTyConTy intPrimTyCon
434 intPrimTyCon :: TyCon
435 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
436
437 int32PrimTy :: Type
438 int32PrimTy = mkTyConTy int32PrimTyCon
439 int32PrimTyCon :: TyCon
440 int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName IntRep
441
442 int64PrimTy :: Type
443 int64PrimTy = mkTyConTy int64PrimTyCon
444 int64PrimTyCon :: TyCon
445 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
446
447 wordPrimTy :: Type
448 wordPrimTy = mkTyConTy wordPrimTyCon
449 wordPrimTyCon :: TyCon
450 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
451
452 word32PrimTy :: Type
453 word32PrimTy = mkTyConTy word32PrimTyCon
454 word32PrimTyCon :: TyCon
455 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName WordRep
456
457 word64PrimTy :: Type
458 word64PrimTy = mkTyConTy word64PrimTyCon
459 word64PrimTyCon :: TyCon
460 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
461
462 addrPrimTy :: Type
463 addrPrimTy = mkTyConTy addrPrimTyCon
464 addrPrimTyCon :: TyCon
465 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
466
467 floatPrimTy :: Type
468 floatPrimTy = mkTyConTy floatPrimTyCon
469 floatPrimTyCon :: TyCon
470 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
471
472 doublePrimTy :: Type
473 doublePrimTy = mkTyConTy doublePrimTyCon
474 doublePrimTyCon :: TyCon
475 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
476
477 {-
478 ************************************************************************
479 * *
480 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
481 * *
482 ************************************************************************
483
484 Note [The equality types story]
485 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
486 GHC sports a veritable menagerie of equality types:
487
488 Hetero? Levity Result Role Defining module
489 ------------------------------------------------------------
490 ~# hetero unlifted # nominal GHC.Prim
491 ~~ hetero lifted Constraint nominal GHC.Types
492 ~ homo lifted Constraint nominal Data.Type.Equality
493 :~: homo lifted * nominal Data.Type.Equality
494
495 ~R# hetero unlifted # repr GHC.Prim
496 Coercible homo lifted Constraint repr GHC.Types
497 Coercion homo lifted * repr Data.Type.Coercion
498
499 ~P# hetero unlifted phantom GHC.Prim
500
501 Recall that "hetero" means the equality can related types of different
502 kinds. Knowing that (t1 ~# t2) or (t1 ~R# t2) or even that (t1 ~P# t2)
503 also means that (k1 ~# k2), where (t1 :: k1) and (t2 :: k2).
504
505 To produce less confusion for end users, when not dumping and without
506 -fprint-equality-relations, each of these groups is printed as the bottommost
507 listed equality. That is, (~#) and (~~) are both rendered as (~) in
508 error messages, and (~R#) is rendered as Coercible.
509
510 Let's take these one at a time:
511
512 --------------------------
513 (~#) :: forall k1 k2. k1 -> k2 -> #
514 --------------------------
515 This is The Type Of Equality in GHC. It classifies nominal coercions.
516 This type is used in the solver for recording equality constraints.
517 It responds "yes" to Type.isEqPred and classifies as an EqPred in
518 Type.classifyPredType.
519
520 All wanted constraints of this type are built with coercion holes.
521 (See Note [Coercion holes] in TyCoRep.) But see also
522 Note [Deferred errors for coercion holes] in TcErrors to see how
523 equality constraints are deferred.
524
525 Within GHC, ~# is called eqPrimTyCon, and it is defined in TysPrim.
526
527
528 --------------------------
529 (~~) :: forall k1 k2. k1 -> k2 -> Constraint
530 --------------------------
531 This is (almost) an ordinary class, defined as if by
532 class a ~# b => a ~~ b
533 instance a ~# b => a ~~ b
534 Here's what's unusual about it:
535 * We can't actually declare it that way because we don't have syntax for ~#.
536 And ~# isn't a constraint, so even if we could write it, it wouldn't kind
537 check.
538
539 * Users cannot write instances of it.
540
541 * It is "naturally coherent". This means that the solver won't hesitate to
542 solve a goal of type (a ~~ b) even if there is, say (Int ~~ c) in the
543 context. (Normally, it waits to learn more, just in case the given
544 influences what happens next.) This is quite like having
545 IncoherentInstances enabled.
546
547 * It always terminates. That is, in the UndecidableInstances checks, we
548 don't worry if a (~~) constraint is too big, as we know that solving
549 equality terminates.
550
551 On the other hand, this behaves just like any class w.r.t. eager superclass
552 unpacking in the solver. So a lifted equality given quickly becomes an unlifted
553 equality given. This is good, because the solver knows all about unlifted
554 equalities. There is some special-casing in TcInteract.matchClassInst to
555 pretend that there is an instance of this class, as we can't write the instance
556 in Haskell.
557
558 Within GHC, ~~ is called heqTyCon, and it is defined in TysWiredIn.
559
560
561 --------------------------
562 (~) :: forall k. k -> k -> Constraint
563 --------------------------
564 This is defined in Data.Type.Equality:
565 class a ~~ b => (a :: k) ~ (b :: k)
566 instance a ~~ b => a ~ b
567 This is even more so an ordinary class than (~~), with the following exceptions:
568 * Users cannot write instances of it.
569
570 * It is "naturally coherent". (See (~~).)
571
572 * (~) is magical syntax, as ~ is a reserved symbol. It cannot be exported
573 or imported.
574
575 * It always terminates.
576
577 Within GHC, ~ is called eqTyCon, and it is defined in PrelNames. Note that
578 it is *not* wired in.
579
580
581 --------------------------
582 (:~:) :: forall k. k -> k -> *
583 --------------------------
584 This is a perfectly ordinary GADT, wrapping (~). It is not defined within
585 GHC at all.
586
587
588 --------------------------
589 (~R#) :: forall k1 k2. k1 -> k2 -> #
590 --------------------------
591 The is the representational analogue of ~#. This is the type of representational
592 equalities that the solver works on. All wanted constraints of this type are
593 built with coercion holes.
594
595 Within GHC, ~R# is called eqReprPrimTyCon, and it is defined in TysPrim.
596
597
598 --------------------------
599 Coercible :: forall k. k -> k -> Constraint
600 --------------------------
601 This is quite like (~~) in the way it's defined and treated within GHC, but
602 it's homogeneous. Homogeneity helps with type inference (as GHC can solve one
603 kind from the other) and, in my (Richard's) estimation, will be more intuitive
604 for users.
605
606 An alternative design included HCoercible (like (~~)) and Coercible (like (~)).
607 One annoyance was that we want `coerce :: Coercible a b => a -> b`, and
608 we need the type of coerce to be fully wired-in. So the HCoercible/Coercible
609 split required that both types be fully wired-in. Instead of doing this,
610 I just got rid of HCoercible, as I'm not sure who would use it, anyway.
611
612 Within GHC, Coercible is called coercibleTyCon, and it is defined in
613 TysWiredIn.
614
615
616 --------------------------
617 Coercion :: forall k. k -> k -> *
618 --------------------------
619 This is a perfectly ordinary GADT, wrapping Coercible. It is not defined
620 within GHC at all.
621
622
623 --------------------------
624 (~P#) :: forall k1 k2. k1 -> k2 -> #
625 --------------------------
626 This is the phantom analogue of ~# and it is barely used at all.
627 (The solver has no idea about this one.) Here is the motivation:
628
629 data Phant a = MkPhant
630 type role Phant phantom
631
632 Phant <Int, Bool>_P :: Phant Int ~P# Phant Bool
633
634 We just need to have something to put on that last line. You probably
635 don't need to worry about it.
636
637
638
639 Note [The State# TyCon]
640 ~~~~~~~~~~~~~~~~~~~~~~~
641 State# is the primitive, unlifted type of states. It has one type parameter,
642 thus
643 State# RealWorld
644 or
645 State# s
646
647 where s is a type variable. The only purpose of the type parameter is to
648 keep different state threads separate. It is represented by nothing at all.
649
650 The type parameter to State# is intended to keep separate threads separate.
651 Even though this parameter is not used in the definition of State#, it is
652 given role Nominal to enforce its intended use.
653 -}
654
655 mkStatePrimTy :: Type -> Type
656 mkStatePrimTy ty = TyConApp statePrimTyCon [ty]
657
658 statePrimTyCon :: TyCon -- See Note [The State# TyCon]
659 statePrimTyCon = pcPrimTyCon statePrimTyConName [Nominal] VoidRep
660
661 {-
662 RealWorld is deeply magical. It is *primitive*, but it is not
663 *unlifted* (hence ptrArg). We never manipulate values of type
664 RealWorld; it's only used in the type system, to parameterise State#.
665 -}
666
667 realWorldTyCon :: TyCon
668 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName [] liftedTypeKind []
669 realWorldTy :: Type
670 realWorldTy = mkTyConTy realWorldTyCon
671 realWorldStatePrimTy :: Type
672 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
673
674 -- Note: the ``state-pairing'' types are not truly primitive,
675 -- so they are defined in \tr{TysWiredIn.hs}, not here.
676
677
678 voidPrimTy :: Type
679 voidPrimTy = TyConApp voidPrimTyCon []
680
681 voidPrimTyCon :: TyCon
682 voidPrimTyCon = pcPrimTyCon voidPrimTyConName [] VoidRep
683
684 mkProxyPrimTy :: Type -> Type -> Type
685 mkProxyPrimTy k ty = TyConApp proxyPrimTyCon [k, ty]
686
687 proxyPrimTyCon :: TyCon
688 proxyPrimTyCon = mkPrimTyCon proxyPrimTyConName binders res_kind [Nominal,Nominal]
689 where binders = [ Named kv Specified
690 , Anon k ]
691 res_kind = tYPE voidRepDataConTy
692 kv = kKiVar
693 k = mkTyVarTy kv
694
695
696 {- *********************************************************************
697 * *
698 Primitive equality constraints
699 See Note [The equality types story]
700 * *
701 ********************************************************************* -}
702
703 eqPrimTyCon :: TyCon -- The representation type for equality predicates
704 -- See Note [The equality types story]
705 eqPrimTyCon = mkPrimTyCon eqPrimTyConName binders res_kind roles
706 where binders = [ Named kv1 Specified
707 , Named kv2 Specified
708 , Anon k1
709 , Anon k2 ]
710 res_kind = tYPE voidRepDataConTy
711 [kv1, kv2] = mkTemplateTyVars [liftedTypeKind, liftedTypeKind]
712 k1 = mkTyVarTy kv1
713 k2 = mkTyVarTy kv2
714 roles = [Nominal, Nominal, Nominal, Nominal]
715
716 -- like eqPrimTyCon, but the type for *Representational* coercions
717 -- this should only ever appear as the type of a covar. Its role is
718 -- interpreted in coercionRole
719 eqReprPrimTyCon :: TyCon -- See Note [The equality types story]
720 eqReprPrimTyCon = mkPrimTyCon eqReprPrimTyConName binders res_kind roles
721 where binders = [ Named kv1 Specified
722 , Named kv2 Specified
723 , Anon k1
724 , Anon k2 ]
725 res_kind = tYPE voidRepDataConTy
726 [kv1, kv2] = mkTemplateTyVars [liftedTypeKind, liftedTypeKind]
727 k1 = mkTyVarTy kv1
728 k2 = mkTyVarTy kv2
729 roles = [Nominal, Nominal, Representational, Representational]
730
731 -- like eqPrimTyCon, but the type for *Phantom* coercions.
732 -- This is only used to make higher-order equalities. Nothing
733 -- should ever actually have this type!
734 eqPhantPrimTyCon :: TyCon
735 eqPhantPrimTyCon = mkPrimTyCon eqPhantPrimTyConName binders res_kind
736 [Nominal, Nominal, Phantom, Phantom]
737 where binders = [ Named kv1 Specified
738 , Named kv2 Specified
739 , Anon k1
740 , Anon k2 ]
741 res_kind = tYPE voidRepDataConTy
742 [kv1, kv2] = mkTemplateTyVars [liftedTypeKind, liftedTypeKind]
743 k1 = mkTyVarTy kv1
744 k2 = mkTyVarTy kv2
745
746
747 {- *********************************************************************
748 * *
749 The primitive array types
750 * *
751 ********************************************************************* -}
752
753 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
754 byteArrayPrimTyCon, arrayArrayPrimTyCon, mutableArrayArrayPrimTyCon,
755 smallArrayPrimTyCon, smallMutableArrayPrimTyCon :: TyCon
756 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName [Representational] PtrRep
757 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName [Nominal, Representational] PtrRep
758 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName [Nominal] PtrRep
759 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName PtrRep
760 arrayArrayPrimTyCon = pcPrimTyCon0 arrayArrayPrimTyConName PtrRep
761 mutableArrayArrayPrimTyCon = pcPrimTyCon mutableArrayArrayPrimTyConName [Nominal] PtrRep
762 smallArrayPrimTyCon = pcPrimTyCon smallArrayPrimTyConName [Representational] PtrRep
763 smallMutableArrayPrimTyCon = pcPrimTyCon smallMutableArrayPrimTyConName [Nominal, Representational] PtrRep
764
765 mkArrayPrimTy :: Type -> Type
766 mkArrayPrimTy elt = TyConApp arrayPrimTyCon [elt]
767 byteArrayPrimTy :: Type
768 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
769 mkArrayArrayPrimTy :: Type
770 mkArrayArrayPrimTy = mkTyConTy arrayArrayPrimTyCon
771 mkSmallArrayPrimTy :: Type -> Type
772 mkSmallArrayPrimTy elt = TyConApp smallArrayPrimTyCon [elt]
773 mkMutableArrayPrimTy :: Type -> Type -> Type
774 mkMutableArrayPrimTy s elt = TyConApp mutableArrayPrimTyCon [s, elt]
775 mkMutableByteArrayPrimTy :: Type -> Type
776 mkMutableByteArrayPrimTy s = TyConApp mutableByteArrayPrimTyCon [s]
777 mkMutableArrayArrayPrimTy :: Type -> Type
778 mkMutableArrayArrayPrimTy s = TyConApp mutableArrayArrayPrimTyCon [s]
779 mkSmallMutableArrayPrimTy :: Type -> Type -> Type
780 mkSmallMutableArrayPrimTy s elt = TyConApp smallMutableArrayPrimTyCon [s, elt]
781
782
783 {- *********************************************************************
784 * *
785 The mutable variable type
786 * *
787 ********************************************************************* -}
788
789 mutVarPrimTyCon :: TyCon
790 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName [Nominal, Representational] PtrRep
791
792 mkMutVarPrimTy :: Type -> Type -> Type
793 mkMutVarPrimTy s elt = TyConApp mutVarPrimTyCon [s, elt]
794
795 {-
796 ************************************************************************
797 * *
798 \subsection[TysPrim-synch-var]{The synchronizing variable type}
799 * *
800 ************************************************************************
801 -}
802
803 mVarPrimTyCon :: TyCon
804 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName [Nominal, Representational] PtrRep
805
806 mkMVarPrimTy :: Type -> Type -> Type
807 mkMVarPrimTy s elt = TyConApp mVarPrimTyCon [s, elt]
808
809 {-
810 ************************************************************************
811 * *
812 \subsection[TysPrim-stm-var]{The transactional variable type}
813 * *
814 ************************************************************************
815 -}
816
817 tVarPrimTyCon :: TyCon
818 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName [Nominal, Representational] PtrRep
819
820 mkTVarPrimTy :: Type -> Type -> Type
821 mkTVarPrimTy s elt = TyConApp tVarPrimTyCon [s, elt]
822
823 {-
824 ************************************************************************
825 * *
826 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
827 * *
828 ************************************************************************
829 -}
830
831 stablePtrPrimTyCon :: TyCon
832 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName [Representational] AddrRep
833
834 mkStablePtrPrimTy :: Type -> Type
835 mkStablePtrPrimTy ty = TyConApp stablePtrPrimTyCon [ty]
836
837 {-
838 ************************************************************************
839 * *
840 \subsection[TysPrim-stable-names]{The stable-name type}
841 * *
842 ************************************************************************
843 -}
844
845 stableNamePrimTyCon :: TyCon
846 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName [Representational] PtrRep
847
848 mkStableNamePrimTy :: Type -> Type
849 mkStableNamePrimTy ty = TyConApp stableNamePrimTyCon [ty]
850
851 {-
852 ************************************************************************
853 * *
854 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
855 * *
856 ************************************************************************
857 -}
858
859 bcoPrimTy :: Type
860 bcoPrimTy = mkTyConTy bcoPrimTyCon
861 bcoPrimTyCon :: TyCon
862 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep
863
864 {-
865 ************************************************************************
866 * *
867 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
868 * *
869 ************************************************************************
870 -}
871
872 weakPrimTyCon :: TyCon
873 weakPrimTyCon = pcPrimTyCon weakPrimTyConName [Representational] PtrRep
874
875 mkWeakPrimTy :: Type -> Type
876 mkWeakPrimTy v = TyConApp weakPrimTyCon [v]
877
878 {-
879 ************************************************************************
880 * *
881 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
882 * *
883 ************************************************************************
884
885 A thread id is represented by a pointer to the TSO itself, to ensure
886 that they are always unique and we can always find the TSO for a given
887 thread id. However, this has the unfortunate consequence that a
888 ThreadId# for a given thread is treated as a root by the garbage
889 collector and can keep TSOs around for too long.
890
891 Hence the programmer API for thread manipulation uses a weak pointer
892 to the thread id internally.
893 -}
894
895 threadIdPrimTy :: Type
896 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
897 threadIdPrimTyCon :: TyCon
898 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep
899
900 {-
901 ************************************************************************
902 * *
903 Any
904 * *
905 ************************************************************************
906
907 Note [Any types]
908 ~~~~~~~~~~~~~~~~
909 The type constructor Any of kind forall k. k has these properties:
910
911 * It is defined in module GHC.Prim, and exported so that it is
912 available to users. For this reason it's treated like any other
913 primitive type:
914 - has a fixed unique, anyTyConKey,
915 - lives in the global name cache
916
917 * It is a *closed* type family, with no instances. This means that
918 if ty :: '(k1, k2) we add a given coercion
919 g :: ty ~ (Fst ty, Snd ty)
920 If Any was a *data* type, then we'd get inconsistency because 'ty'
921 could be (Any '(k1,k2)) and then we'd have an equality with Any on
922 one side and '(,) on the other. See also #9097.
923
924 * It is lifted, and hence represented by a pointer
925
926 * It is inhabited by at least one value, namely bottom
927
928 * You can unsafely coerce any lifted type to Any, and back.
929
930 * It does not claim to be a *data* type, and that's important for
931 the code generator, because the code gen may *enter* a data value
932 but never enters a function value.
933
934 * It is used to instantiate otherwise un-constrained type variables
935 For example length Any []
936 See Note [Strangely-kinded void TyCons]
937
938 Note [Strangely-kinded void TyCons]
939 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
940 See Trac #959 for more examples
941
942 When the type checker finds a type variable with no binding, which
943 means it can be instantiated with an arbitrary type, it usually
944 instantiates it to Void. Eg.
945
946 length []
947 ===>
948 length Any (Nil Any)
949
950 But in really obscure programs, the type variable might have a kind
951 other than *, so we need to invent a suitably-kinded type.
952
953 This commit uses
954 Any for kind *
955 Any(*->*) for kind *->*
956 etc
957 -}
958
959 anyTyConName :: Name
960 anyTyConName = mkPrimTc (fsLit "Any") anyTyConKey anyTyCon
961
962 anyTy :: Type
963 anyTy = mkTyConTy anyTyCon
964
965 anyTyCon :: TyCon
966 anyTyCon = mkFamilyTyCon anyTyConName binders res_kind [kKiVar] Nothing
967 (ClosedSynFamilyTyCon Nothing)
968 Nothing
969 NotInjective
970 where
971 binders = [Named kKiVar Specified]
972 res_kind = mkTyVarTy kKiVar
973
974 anyTypeOfKind :: Kind -> Type
975 anyTypeOfKind kind = TyConApp anyTyCon [kind]
976
977 {-
978 ************************************************************************
979 * *
980 \subsection{SIMD vector types}
981 * *
982 ************************************************************************
983 -}
984
985 #include "primop-vector-tys.hs-incl"