Float unboxed expressions by boxing
[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, charPrimTyConName,
36 intPrimTyCon, intPrimTy, intPrimTyConName,
37 wordPrimTyCon, wordPrimTy, wordPrimTyConName,
38 addrPrimTyCon, addrPrimTy, addrPrimTyConName,
39 floatPrimTyCon, floatPrimTy, floatPrimTyConName,
40 doublePrimTyCon, doublePrimTy, doublePrimTyConName,
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 and RuntimeRep]
355 ~~~~~~~~~~~~~~~~~~~~~~~~~~
356 All types that classify values have a kind of the form (TYPE rr), where
357
358 data RuntimeRep -- Defined in ghc-prim:GHC.Types
359 = PtrRepLifted
360 | PtrRepUnlifted
361 | IntRep
362 | FloatRep
363 .. etc ..
364
365 rr :: RuntimeRep
366
367 TYPE :: RuntimeRep -> TYPE 'PtrRepLifted -- Built in
368
369 So for example:
370 Int :: TYPE 'PtrRepLifted
371 Array# Int :: TYPE 'PtrRepUnlifted
372 Int# :: TYPE 'IntRep
373 Float# :: TYPE 'FloatRep
374 Maybe :: TYPE 'PtrRepLifted -> TYPE 'PtrRepLifted
375
376 We abbreviate '*' specially:
377 type * = TYPE 'PtrRepLifted
378
379 The 'rr' parameter tells us how the value is represented at runime.
380
381 Generally speaking, you can't be polymorphic in 'rr'. E.g
382 f :: forall (rr:RuntimeRep) (a:TYPE rr). a -> [a]
383 f = /\(rr:RuntimeRep) (a:rr) \(a:rr). ...
384 This is no good: we could not generate code code for 'f', because the
385 calling convention for 'f' varies depending on whether the argument is
386 a a Int, Int#, or Float#. (You could imagine generating specialised
387 code, one for each instantiation of 'rr', but we don't do that.)
388
389 Certain functions CAN be runtime-rep-polymorphic, because the code
390 generator never has to manipulate a value of type 'a :: TYPE rr'.
391
392 * error :: forall (rr:RuntimeRep) (a:TYPE rr). String -> a
393 Code generator never has to manipulate the return value.
394
395 * unsafeCoerce#, defined in MkId.unsafeCoerceId:
396 Always inlined to be a no-op
397 unsafeCoerce# :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
398 (a :: TYPE r1) (b :: TYPE r2).
399 a -> b
400
401 * Unboxed tuples, and unboxed sums, defined in TysWiredIn
402 Always inlined, and hence specialised to the call site
403 (#,#) :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
404 (a :: TYPE r1) (b :: TYPE r2).
405 a -> b -> TYPE 'UnboxedTupleRep
406 See Note [Unboxed tuple kinds]
407
408 Note [Unboxed tuple kinds]
409 ~~~~~~~~~~~~~~~~~~~~~~~~~~
410 What kind does (# Int, Float# #) have?
411 The "right" answer would be
412 TYPE ('UnboxedTupleRep [PtrRepLifted, FloatRep])
413 Currently we do not do this. We just have
414 (# Int, Float# #) :: TYPE 'UnboxedTupleRep
415 which does not tell us exactly how is is represented.
416
417 Note [PrimRep and kindPrimRep]
418 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
419 As part of its source code, in TyCon, GHC has
420 data PrimRep = PtrRep | IntRep | FloatRep | ...etc...
421
422 Notice that
423 * RuntimeRep is part of the syntax tree of the program being compiled
424 (defined in a library: ghc-prim:GHC.Types)
425 * PrimRep is part of GHC's source code.
426 (defined in TyCon)
427
428 We need to get from one to the other; that is what kindPrimRep does.
429 Suppose we have a value
430 (v :: t) where (t :: k)
431 Given this kind
432 k = TyConApp "TYPE" [rep]
433 GHC needs to be able to figure out how 'v' is represented at runtime.
434 It expects 'rep' to be form
435 TyConApp rr_dc args
436 where 'rr_dc' is a promoteed data constructor from RuntimeRep. So
437 now we need to go from 'dc' to the correponding PrimRep. We store this
438 PrimRep in the promoted data constructor itself: see TyCon.promDcRepInfo.
439
440 -}
441
442 tYPETyCon, unliftedTypeKindTyCon :: TyCon
443 tYPETyConName, unliftedTypeKindTyConName :: Name
444
445 tYPETyCon = mkKindTyCon tYPETyConName
446 (mkTemplateAnonTyConBinders [runtimeRepTy])
447 liftedTypeKind
448 [Nominal]
449 (mkPrelTyConRepName tYPETyConName)
450
451 -- See Note [TYPE and RuntimeRep]
452 -- NB: unlifted is wired in because there is no way to parse it in
453 -- Haskell. That's the only reason for wiring it in.
454 unliftedTypeKindTyCon = mkSynonymTyCon unliftedTypeKindTyConName
455 [] liftedTypeKind []
456 (tYPE (TyConApp ptrRepUnliftedDataConTyCon []))
457 True -- no foralls
458 True -- family free
459
460 --------------------------
461 -- ... and now their names
462
463 -- If you edit these, you may need to update the GHC formalism
464 -- See Note [GHC Formalism] in coreSyn/CoreLint.hs
465 tYPETyConName = mkPrimTyConName (fsLit "TYPE") tYPETyConKey tYPETyCon
466 unliftedTypeKindTyConName = mkPrimTyConName (fsLit "#") unliftedTypeKindTyConKey unliftedTypeKindTyCon
467
468 mkPrimTyConName :: FastString -> Unique -> TyCon -> Name
469 mkPrimTyConName = mkPrimTcName BuiltInSyntax
470 -- All of the super kinds and kinds are defined in Prim,
471 -- and use BuiltInSyntax, because they are never in scope in the source
472
473 mkPrimTcName :: BuiltInSyntax -> FastString -> Unique -> TyCon -> Name
474 mkPrimTcName built_in_syntax occ key tycon
475 = mkWiredInName gHC_PRIM (mkTcOccFS occ) key (ATyCon tycon) built_in_syntax
476
477 -----------------------------
478 -- | Given a RuntimeRep, applies TYPE to it.
479 -- see Note [TYPE and RuntimeRep]
480 tYPE :: Type -> Type
481 tYPE rr = TyConApp tYPETyCon [rr]
482
483 {-
484 ************************************************************************
485 * *
486 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
487 * *
488 ************************************************************************
489 -}
490
491 -- only used herein
492 pcPrimTyCon :: Name -> [Role] -> PrimRep -> TyCon
493 pcPrimTyCon name roles rep
494 = mkPrimTyCon name binders result_kind roles
495 where
496 binders = mkTemplateAnonTyConBinders (map (const liftedTypeKind) roles)
497 result_kind = tYPE rr
498
499 rr = case rep of
500 VoidRep -> voidRepDataConTy
501 PtrRep -> TyConApp ptrRepUnliftedDataConTyCon []
502 IntRep -> intRepDataConTy
503 WordRep -> wordRepDataConTy
504 Int64Rep -> int64RepDataConTy
505 Word64Rep -> word64RepDataConTy
506 AddrRep -> addrRepDataConTy
507 FloatRep -> floatRepDataConTy
508 DoubleRep -> doubleRepDataConTy
509 VecRep n elem -> TyConApp vecRepDataConTyCon [n', elem']
510 where
511 n' = case n of
512 2 -> vec2DataConTy
513 4 -> vec4DataConTy
514 8 -> vec8DataConTy
515 16 -> vec16DataConTy
516 32 -> vec32DataConTy
517 64 -> vec64DataConTy
518 _ -> pprPanic "Disallowed VecCount" (ppr n)
519
520 elem' = case elem of
521 Int8ElemRep -> int8ElemRepDataConTy
522 Int16ElemRep -> int16ElemRepDataConTy
523 Int32ElemRep -> int32ElemRepDataConTy
524 Int64ElemRep -> int64ElemRepDataConTy
525 Word8ElemRep -> word8ElemRepDataConTy
526 Word16ElemRep -> word16ElemRepDataConTy
527 Word32ElemRep -> word32ElemRepDataConTy
528 Word64ElemRep -> word64ElemRepDataConTy
529 FloatElemRep -> floatElemRepDataConTy
530 DoubleElemRep -> doubleElemRepDataConTy
531
532
533 pcPrimTyCon0 :: Name -> PrimRep -> TyCon
534 pcPrimTyCon0 name rep
535 = pcPrimTyCon name [] rep
536
537 charPrimTy :: Type
538 charPrimTy = mkTyConTy charPrimTyCon
539 charPrimTyCon :: TyCon
540 charPrimTyCon = pcPrimTyCon0 charPrimTyConName WordRep
541
542 intPrimTy :: Type
543 intPrimTy = mkTyConTy intPrimTyCon
544 intPrimTyCon :: TyCon
545 intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
546
547 int32PrimTy :: Type
548 int32PrimTy = mkTyConTy int32PrimTyCon
549 int32PrimTyCon :: TyCon
550 int32PrimTyCon = pcPrimTyCon0 int32PrimTyConName IntRep
551
552 int64PrimTy :: Type
553 int64PrimTy = mkTyConTy int64PrimTyCon
554 int64PrimTyCon :: TyCon
555 int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
556
557 wordPrimTy :: Type
558 wordPrimTy = mkTyConTy wordPrimTyCon
559 wordPrimTyCon :: TyCon
560 wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
561
562 word32PrimTy :: Type
563 word32PrimTy = mkTyConTy word32PrimTyCon
564 word32PrimTyCon :: TyCon
565 word32PrimTyCon = pcPrimTyCon0 word32PrimTyConName WordRep
566
567 word64PrimTy :: Type
568 word64PrimTy = mkTyConTy word64PrimTyCon
569 word64PrimTyCon :: TyCon
570 word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
571
572 addrPrimTy :: Type
573 addrPrimTy = mkTyConTy addrPrimTyCon
574 addrPrimTyCon :: TyCon
575 addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
576
577 floatPrimTy :: Type
578 floatPrimTy = mkTyConTy floatPrimTyCon
579 floatPrimTyCon :: TyCon
580 floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
581
582 doublePrimTy :: Type
583 doublePrimTy = mkTyConTy doublePrimTyCon
584 doublePrimTyCon :: TyCon
585 doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
586
587 {-
588 ************************************************************************
589 * *
590 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
591 * *
592 ************************************************************************
593
594 Note [The equality types story]
595 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
596 GHC sports a veritable menagerie of equality types:
597
598 Hetero? Levity Result Role Defining module
599 ------------------------------------------------------------
600 ~# hetero unlifted # nominal GHC.Prim
601 ~~ hetero lifted Constraint nominal GHC.Types
602 ~ homo lifted Constraint nominal Data.Type.Equality
603 :~: homo lifted * nominal Data.Type.Equality
604
605 ~R# hetero unlifted # repr GHC.Prim
606 Coercible homo lifted Constraint repr GHC.Types
607 Coercion homo lifted * repr Data.Type.Coercion
608
609 ~P# hetero unlifted phantom GHC.Prim
610
611 Recall that "hetero" means the equality can related types of different
612 kinds. Knowing that (t1 ~# t2) or (t1 ~R# t2) or even that (t1 ~P# t2)
613 also means that (k1 ~# k2), where (t1 :: k1) and (t2 :: k2).
614
615 To produce less confusion for end users, when not dumping and without
616 -fprint-equality-relations, each of these groups is printed as the bottommost
617 listed equality. That is, (~#) and (~~) are both rendered as (~) in
618 error messages, and (~R#) is rendered as Coercible.
619
620 Let's take these one at a time:
621
622 --------------------------
623 (~#) :: forall k1 k2. k1 -> k2 -> #
624 --------------------------
625 This is The Type Of Equality in GHC. It classifies nominal coercions.
626 This type is used in the solver for recording equality constraints.
627 It responds "yes" to Type.isEqPred and classifies as an EqPred in
628 Type.classifyPredType.
629
630 All wanted constraints of this type are built with coercion holes.
631 (See Note [Coercion holes] in TyCoRep.) But see also
632 Note [Deferred errors for coercion holes] in TcErrors to see how
633 equality constraints are deferred.
634
635 Within GHC, ~# is called eqPrimTyCon, and it is defined in TysPrim.
636
637
638 --------------------------
639 (~~) :: forall k1 k2. k1 -> k2 -> Constraint
640 --------------------------
641 This is (almost) an ordinary class, defined as if by
642 class a ~# b => a ~~ b
643 instance a ~# b => a ~~ b
644 Here's what's unusual about it:
645 * We can't actually declare it that way because we don't have syntax for ~#.
646 And ~# isn't a constraint, so even if we could write it, it wouldn't kind
647 check.
648
649 * Users cannot write instances of it.
650
651 * It is "naturally coherent". This means that the solver won't hesitate to
652 solve a goal of type (a ~~ b) even if there is, say (Int ~~ c) in the
653 context. (Normally, it waits to learn more, just in case the given
654 influences what happens next.) This is quite like having
655 IncoherentInstances enabled.
656
657 * It always terminates. That is, in the UndecidableInstances checks, we
658 don't worry if a (~~) constraint is too big, as we know that solving
659 equality terminates.
660
661 On the other hand, this behaves just like any class w.r.t. eager superclass
662 unpacking in the solver. So a lifted equality given quickly becomes an unlifted
663 equality given. This is good, because the solver knows all about unlifted
664 equalities. There is some special-casing in TcInteract.matchClassInst to
665 pretend that there is an instance of this class, as we can't write the instance
666 in Haskell.
667
668 Within GHC, ~~ is called heqTyCon, and it is defined in TysWiredIn.
669
670
671 --------------------------
672 (~) :: forall k. k -> k -> Constraint
673 --------------------------
674 This is defined in Data.Type.Equality:
675 class a ~~ b => (a :: k) ~ (b :: k)
676 instance a ~~ b => a ~ b
677 This is even more so an ordinary class than (~~), with the following exceptions:
678 * Users cannot write instances of it.
679
680 * It is "naturally coherent". (See (~~).)
681
682 * (~) is magical syntax, as ~ is a reserved symbol. It cannot be exported
683 or imported.
684
685 * It always terminates.
686
687 Within GHC, ~ is called eqTyCon, and it is defined in PrelNames. Note that
688 it is *not* wired in.
689
690
691 --------------------------
692 (:~:) :: forall k. k -> k -> *
693 --------------------------
694 This is a perfectly ordinary GADT, wrapping (~). It is not defined within
695 GHC at all.
696
697
698 --------------------------
699 (~R#) :: forall k1 k2. k1 -> k2 -> #
700 --------------------------
701 The is the representational analogue of ~#. This is the type of representational
702 equalities that the solver works on. All wanted constraints of this type are
703 built with coercion holes.
704
705 Within GHC, ~R# is called eqReprPrimTyCon, and it is defined in TysPrim.
706
707
708 --------------------------
709 Coercible :: forall k. k -> k -> Constraint
710 --------------------------
711 This is quite like (~~) in the way it's defined and treated within GHC, but
712 it's homogeneous. Homogeneity helps with type inference (as GHC can solve one
713 kind from the other) and, in my (Richard's) estimation, will be more intuitive
714 for users.
715
716 An alternative design included HCoercible (like (~~)) and Coercible (like (~)).
717 One annoyance was that we want `coerce :: Coercible a b => a -> b`, and
718 we need the type of coerce to be fully wired-in. So the HCoercible/Coercible
719 split required that both types be fully wired-in. Instead of doing this,
720 I just got rid of HCoercible, as I'm not sure who would use it, anyway.
721
722 Within GHC, Coercible is called coercibleTyCon, and it is defined in
723 TysWiredIn.
724
725
726 --------------------------
727 Coercion :: forall k. k -> k -> *
728 --------------------------
729 This is a perfectly ordinary GADT, wrapping Coercible. It is not defined
730 within GHC at all.
731
732
733 --------------------------
734 (~P#) :: forall k1 k2. k1 -> k2 -> #
735 --------------------------
736 This is the phantom analogue of ~# and it is barely used at all.
737 (The solver has no idea about this one.) Here is the motivation:
738
739 data Phant a = MkPhant
740 type role Phant phantom
741
742 Phant <Int, Bool>_P :: Phant Int ~P# Phant Bool
743
744 We just need to have something to put on that last line. You probably
745 don't need to worry about it.
746
747
748
749 Note [The State# TyCon]
750 ~~~~~~~~~~~~~~~~~~~~~~~
751 State# is the primitive, unlifted type of states. It has one type parameter,
752 thus
753 State# RealWorld
754 or
755 State# s
756
757 where s is a type variable. The only purpose of the type parameter is to
758 keep different state threads separate. It is represented by nothing at all.
759
760 The type parameter to State# is intended to keep separate threads separate.
761 Even though this parameter is not used in the definition of State#, it is
762 given role Nominal to enforce its intended use.
763 -}
764
765 mkStatePrimTy :: Type -> Type
766 mkStatePrimTy ty = TyConApp statePrimTyCon [ty]
767
768 statePrimTyCon :: TyCon -- See Note [The State# TyCon]
769 statePrimTyCon = pcPrimTyCon statePrimTyConName [Nominal] VoidRep
770
771 {-
772 RealWorld is deeply magical. It is *primitive*, but it is not
773 *unlifted* (hence ptrArg). We never manipulate values of type
774 RealWorld; it's only used in the type system, to parameterise State#.
775 -}
776
777 realWorldTyCon :: TyCon
778 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName [] liftedTypeKind []
779 realWorldTy :: Type
780 realWorldTy = mkTyConTy realWorldTyCon
781 realWorldStatePrimTy :: Type
782 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
783
784 -- Note: the ``state-pairing'' types are not truly primitive,
785 -- so they are defined in \tr{TysWiredIn.hs}, not here.
786
787
788 voidPrimTy :: Type
789 voidPrimTy = TyConApp voidPrimTyCon []
790
791 voidPrimTyCon :: TyCon
792 voidPrimTyCon = pcPrimTyCon voidPrimTyConName [] VoidRep
793
794 mkProxyPrimTy :: Type -> Type -> Type
795 mkProxyPrimTy k ty = TyConApp proxyPrimTyCon [k, ty]
796
797 proxyPrimTyCon :: TyCon
798 proxyPrimTyCon = mkPrimTyCon proxyPrimTyConName binders res_kind [Nominal,Nominal]
799 where
800 -- Kind: forall k. k -> Void#
801 binders = mkTemplateTyConBinders [liftedTypeKind] (\ks-> ks)
802 res_kind = tYPE voidRepDataConTy
803
804
805 {- *********************************************************************
806 * *
807 Primitive equality constraints
808 See Note [The equality types story]
809 * *
810 ********************************************************************* -}
811
812 eqPrimTyCon :: TyCon -- The representation type for equality predicates
813 -- See Note [The equality types story]
814 eqPrimTyCon = mkPrimTyCon eqPrimTyConName binders res_kind roles
815 where
816 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
817 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
818 res_kind = tYPE voidRepDataConTy
819 roles = [Nominal, Nominal, Nominal, Nominal]
820
821 -- like eqPrimTyCon, but the type for *Representational* coercions
822 -- this should only ever appear as the type of a covar. Its role is
823 -- interpreted in coercionRole
824 eqReprPrimTyCon :: TyCon -- See Note [The equality types story]
825 eqReprPrimTyCon = mkPrimTyCon eqReprPrimTyConName binders res_kind roles
826 where
827 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
828 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
829 res_kind = tYPE voidRepDataConTy
830 roles = [Nominal, Nominal, Representational, Representational]
831
832 -- like eqPrimTyCon, but the type for *Phantom* coercions.
833 -- This is only used to make higher-order equalities. Nothing
834 -- should ever actually have this type!
835 eqPhantPrimTyCon :: TyCon
836 eqPhantPrimTyCon = mkPrimTyCon eqPhantPrimTyConName binders res_kind roles
837 where
838 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
839 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
840 res_kind = tYPE voidRepDataConTy
841 roles = [Nominal, Nominal, Phantom, Phantom]
842
843 {- *********************************************************************
844 * *
845 The primitive array types
846 * *
847 ********************************************************************* -}
848
849 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
850 byteArrayPrimTyCon, arrayArrayPrimTyCon, mutableArrayArrayPrimTyCon,
851 smallArrayPrimTyCon, smallMutableArrayPrimTyCon :: TyCon
852 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName [Representational] PtrRep
853 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName [Nominal, Representational] PtrRep
854 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName [Nominal] PtrRep
855 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName PtrRep
856 arrayArrayPrimTyCon = pcPrimTyCon0 arrayArrayPrimTyConName PtrRep
857 mutableArrayArrayPrimTyCon = pcPrimTyCon mutableArrayArrayPrimTyConName [Nominal] PtrRep
858 smallArrayPrimTyCon = pcPrimTyCon smallArrayPrimTyConName [Representational] PtrRep
859 smallMutableArrayPrimTyCon = pcPrimTyCon smallMutableArrayPrimTyConName [Nominal, Representational] PtrRep
860
861 mkArrayPrimTy :: Type -> Type
862 mkArrayPrimTy elt = TyConApp arrayPrimTyCon [elt]
863 byteArrayPrimTy :: Type
864 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
865 mkArrayArrayPrimTy :: Type
866 mkArrayArrayPrimTy = mkTyConTy arrayArrayPrimTyCon
867 mkSmallArrayPrimTy :: Type -> Type
868 mkSmallArrayPrimTy elt = TyConApp smallArrayPrimTyCon [elt]
869 mkMutableArrayPrimTy :: Type -> Type -> Type
870 mkMutableArrayPrimTy s elt = TyConApp mutableArrayPrimTyCon [s, elt]
871 mkMutableByteArrayPrimTy :: Type -> Type
872 mkMutableByteArrayPrimTy s = TyConApp mutableByteArrayPrimTyCon [s]
873 mkMutableArrayArrayPrimTy :: Type -> Type
874 mkMutableArrayArrayPrimTy s = TyConApp mutableArrayArrayPrimTyCon [s]
875 mkSmallMutableArrayPrimTy :: Type -> Type -> Type
876 mkSmallMutableArrayPrimTy s elt = TyConApp smallMutableArrayPrimTyCon [s, elt]
877
878
879 {- *********************************************************************
880 * *
881 The mutable variable type
882 * *
883 ********************************************************************* -}
884
885 mutVarPrimTyCon :: TyCon
886 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName [Nominal, Representational] PtrRep
887
888 mkMutVarPrimTy :: Type -> Type -> Type
889 mkMutVarPrimTy s elt = TyConApp mutVarPrimTyCon [s, elt]
890
891 {-
892 ************************************************************************
893 * *
894 \subsection[TysPrim-synch-var]{The synchronizing variable type}
895 * *
896 ************************************************************************
897 -}
898
899 mVarPrimTyCon :: TyCon
900 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName [Nominal, Representational] PtrRep
901
902 mkMVarPrimTy :: Type -> Type -> Type
903 mkMVarPrimTy s elt = TyConApp mVarPrimTyCon [s, elt]
904
905 {-
906 ************************************************************************
907 * *
908 \subsection[TysPrim-stm-var]{The transactional variable type}
909 * *
910 ************************************************************************
911 -}
912
913 tVarPrimTyCon :: TyCon
914 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName [Nominal, Representational] PtrRep
915
916 mkTVarPrimTy :: Type -> Type -> Type
917 mkTVarPrimTy s elt = TyConApp tVarPrimTyCon [s, elt]
918
919 {-
920 ************************************************************************
921 * *
922 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
923 * *
924 ************************************************************************
925 -}
926
927 stablePtrPrimTyCon :: TyCon
928 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName [Representational] AddrRep
929
930 mkStablePtrPrimTy :: Type -> Type
931 mkStablePtrPrimTy ty = TyConApp stablePtrPrimTyCon [ty]
932
933 {-
934 ************************************************************************
935 * *
936 \subsection[TysPrim-stable-names]{The stable-name type}
937 * *
938 ************************************************************************
939 -}
940
941 stableNamePrimTyCon :: TyCon
942 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName [Representational] PtrRep
943
944 mkStableNamePrimTy :: Type -> Type
945 mkStableNamePrimTy ty = TyConApp stableNamePrimTyCon [ty]
946
947 {-
948 ************************************************************************
949 * *
950 \subsection[TysPrim-compact-nfdata]{The Compact NFData (CNF) type}
951 * *
952 ************************************************************************
953 -}
954
955 compactPrimTyCon :: TyCon
956 compactPrimTyCon = pcPrimTyCon0 compactPrimTyConName PtrRep
957
958 compactPrimTy :: Type
959 compactPrimTy = mkTyConTy compactPrimTyCon
960
961 {-
962 ************************************************************************
963 * *
964 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
965 * *
966 ************************************************************************
967 -}
968
969 bcoPrimTy :: Type
970 bcoPrimTy = mkTyConTy bcoPrimTyCon
971 bcoPrimTyCon :: TyCon
972 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName PtrRep
973
974 {-
975 ************************************************************************
976 * *
977 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
978 * *
979 ************************************************************************
980 -}
981
982 weakPrimTyCon :: TyCon
983 weakPrimTyCon = pcPrimTyCon weakPrimTyConName [Representational] PtrRep
984
985 mkWeakPrimTy :: Type -> Type
986 mkWeakPrimTy v = TyConApp weakPrimTyCon [v]
987
988 {-
989 ************************************************************************
990 * *
991 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
992 * *
993 ************************************************************************
994
995 A thread id is represented by a pointer to the TSO itself, to ensure
996 that they are always unique and we can always find the TSO for a given
997 thread id. However, this has the unfortunate consequence that a
998 ThreadId# for a given thread is treated as a root by the garbage
999 collector and can keep TSOs around for too long.
1000
1001 Hence the programmer API for thread manipulation uses a weak pointer
1002 to the thread id internally.
1003 -}
1004
1005 threadIdPrimTy :: Type
1006 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
1007 threadIdPrimTyCon :: TyCon
1008 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName PtrRep
1009
1010 {-
1011 ************************************************************************
1012 * *
1013 \subsection{SIMD vector types}
1014 * *
1015 ************************************************************************
1016 -}
1017
1018 #include "primop-vector-tys.hs-incl"