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