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