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