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