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