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