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