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