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