Omit Typeable from the "naturally coherent" list
[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 Built-in tc Hetero? Levity Result Role Defining module
586 -----------------------------------------------------------------------------------------
587 ~# eqPrimTyCon hetero unlifted # nominal GHC.Prim
588 ~~ hEqTyCon hetero lifted Constraint nominal GHC.Types
589 ~ eqTyCon homo lifted Constraint nominal Data.Type.Equality
590 :~: - homo lifted * nominal Data.Type.Equality
591
592 ~R# eqReprPrimTy hetero unlifted # repr GHC.Prim
593 Coercible coercibleTyCon homo lifted Constraint repr GHC.Types
594 Coercion - homo lifted * repr Data.Type.Coercion
595 ~P# eqPhantPrimTyCon hetero unlifted phantom GHC.Prim
596
597 Recall that "hetero" means the equality can related types of different
598 kinds. Knowing that (t1 ~# t2) or (t1 ~R# t2) or even that (t1 ~P# t2)
599 also means that (k1 ~# k2), where (t1 :: k1) and (t2 :: k2).
600
601 To produce less confusion for end users, when not dumping and without
602 -fprint-equality-relations, each of these groups is printed as the bottommost
603 listed equality. That is, (~#) and (~~) are both rendered as (~) in
604 error messages, and (~R#) is rendered as Coercible.
605
606 Let's take these one at a time:
607
608 --------------------------
609 (~#) :: forall k1 k2. k1 -> k2 -> #
610 --------------------------
611 This is The Type Of Equality in GHC. It classifies nominal coercions.
612 This type is used in the solver for recording equality constraints.
613 It responds "yes" to Type.isEqPred and classifies as an EqPred in
614 Type.classifyPredType.
615
616 All wanted constraints of this type are built with coercion holes.
617 (See Note [Coercion holes] in TyCoRep.) But see also
618 Note [Deferred errors for coercion holes] in TcErrors to see how
619 equality constraints are deferred.
620
621 Within GHC, ~# is called eqPrimTyCon, and it is defined in TysPrim.
622
623
624 --------------------------
625 (~~) :: forall k1 k2. k1 -> k2 -> Constraint
626 --------------------------
627 This is (almost) an ordinary class, defined as if by
628 class a ~# b => a ~~ b
629 instance a ~# b => a ~~ b
630 Here's what's unusual about it:
631 * We can't actually declare it that way because we don't have syntax for ~#.
632 And ~# isn't a constraint, so even if we could write it, it wouldn't kind
633 check.
634
635 * Users cannot write instances of it.
636
637 * It is "naturally coherent". This means that the solver won't hesitate to
638 solve a goal of type (a ~~ b) even if there is, say (Int ~~ c) in the
639 context. (Normally, it waits to learn more, just in case the given
640 influences what happens next.) See Note [Naturally coherent classes]
641 in TcInteract.
642
643 * It always terminates. That is, in the UndecidableInstances checks, we
644 don't worry if a (~~) constraint is too big, as we know that solving
645 equality terminates.
646
647 On the other hand, this behaves just like any class w.r.t. eager superclass
648 unpacking in the solver. So a lifted equality given quickly becomes an unlifted
649 equality given. This is good, because the solver knows all about unlifted
650 equalities. There is some special-casing in TcInteract.matchClassInst to
651 pretend that there is an instance of this class, as we can't write the instance
652 in Haskell.
653
654 Within GHC, ~~ is called heqTyCon, and it is defined in TysWiredIn.
655
656
657 --------------------------
658 (~) :: forall k. k -> k -> Constraint
659 --------------------------
660 This is defined in Data.Type.Equality:
661 class a ~~ b => (a :: k) ~ (b :: k)
662 instance a ~~ b => a ~ b
663 This is even more so an ordinary class than (~~), with the following exceptions:
664 * Users cannot write instances of it.
665
666 * It is "naturally coherent". (See (~~).)
667
668 * (~) is magical syntax, as ~ is a reserved symbol.
669 It cannot be exported or imported.
670
671 * It always terminates.
672
673 Within GHC, ~ is called eqTyCon, and it is defined in PrelNames. Note that
674 it is *not* wired in.
675
676
677 --------------------------
678 (:~:) :: forall k. k -> k -> *
679 --------------------------
680 This is a perfectly ordinary GADT, wrapping (~). It is not defined within
681 GHC at all.
682
683
684 --------------------------
685 (~R#) :: forall k1 k2. k1 -> k2 -> #
686 --------------------------
687 The is the representational analogue of ~#. This is the type of representational
688 equalities that the solver works on. All wanted constraints of this type are
689 built with coercion holes.
690
691 Within GHC, ~R# is called eqReprPrimTyCon, and it is defined in TysPrim.
692
693
694 --------------------------
695 Coercible :: forall k. k -> k -> Constraint
696 --------------------------
697 This is quite like (~~) in the way it's defined and treated within GHC, but
698 it's homogeneous. Homogeneity helps with type inference (as GHC can solve one
699 kind from the other) and, in my (Richard's) estimation, will be more intuitive
700 for users.
701
702 An alternative design included HCoercible (like (~~)) and Coercible (like (~)).
703 One annoyance was that we want `coerce :: Coercible a b => a -> b`, and
704 we need the type of coerce to be fully wired-in. So the HCoercible/Coercible
705 split required that both types be fully wired-in. Instead of doing this,
706 I just got rid of HCoercible, as I'm not sure who would use it, anyway.
707
708 Within GHC, Coercible is called coercibleTyCon, and it is defined in
709 TysWiredIn.
710
711
712 --------------------------
713 Coercion :: forall k. k -> k -> *
714 --------------------------
715 This is a perfectly ordinary GADT, wrapping Coercible. It is not defined
716 within GHC at all.
717
718
719 --------------------------
720 (~P#) :: forall k1 k2. k1 -> k2 -> #
721 --------------------------
722 This is the phantom analogue of ~# and it is barely used at all.
723 (The solver has no idea about this one.) Here is the motivation:
724
725 data Phant a = MkPhant
726 type role Phant phantom
727
728 Phant <Int, Bool>_P :: Phant Int ~P# Phant Bool
729
730 We just need to have something to put on that last line. You probably
731 don't need to worry about it.
732
733
734
735 Note [The State# TyCon]
736 ~~~~~~~~~~~~~~~~~~~~~~~
737 State# is the primitive, unlifted type of states. It has one type parameter,
738 thus
739 State# RealWorld
740 or
741 State# s
742
743 where s is a type variable. The only purpose of the type parameter is to
744 keep different state threads separate. It is represented by nothing at all.
745
746 The type parameter to State# is intended to keep separate threads separate.
747 Even though this parameter is not used in the definition of State#, it is
748 given role Nominal to enforce its intended use.
749 -}
750
751 mkStatePrimTy :: Type -> Type
752 mkStatePrimTy ty = TyConApp statePrimTyCon [ty]
753
754 statePrimTyCon :: TyCon -- See Note [The State# TyCon]
755 statePrimTyCon = pcPrimTyCon statePrimTyConName [Nominal] VoidRep
756
757 {-
758 RealWorld is deeply magical. It is *primitive*, but it is not
759 *unlifted* (hence ptrArg). We never manipulate values of type
760 RealWorld; it's only used in the type system, to parameterise State#.
761 -}
762
763 realWorldTyCon :: TyCon
764 realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName [] liftedTypeKind []
765 realWorldTy :: Type
766 realWorldTy = mkTyConTy realWorldTyCon
767 realWorldStatePrimTy :: Type
768 realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
769
770 -- Note: the ``state-pairing'' types are not truly primitive,
771 -- so they are defined in \tr{TysWiredIn.hs}, not here.
772
773
774 voidPrimTy :: Type
775 voidPrimTy = TyConApp voidPrimTyCon []
776
777 voidPrimTyCon :: TyCon
778 voidPrimTyCon = pcPrimTyCon voidPrimTyConName [] VoidRep
779
780 mkProxyPrimTy :: Type -> Type -> Type
781 mkProxyPrimTy k ty = TyConApp proxyPrimTyCon [k, ty]
782
783 proxyPrimTyCon :: TyCon
784 proxyPrimTyCon = mkPrimTyCon proxyPrimTyConName binders res_kind [Nominal,Nominal]
785 where
786 -- Kind: forall k. k -> Void#
787 binders = mkTemplateTyConBinders [liftedTypeKind] (\ks-> ks)
788 res_kind = unboxedTupleKind []
789
790
791 {- *********************************************************************
792 * *
793 Primitive equality constraints
794 See Note [The equality types story]
795 * *
796 ********************************************************************* -}
797
798 eqPrimTyCon :: TyCon -- The representation type for equality predicates
799 -- See Note [The equality types story]
800 eqPrimTyCon = mkPrimTyCon eqPrimTyConName binders res_kind roles
801 where
802 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
803 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
804 res_kind = unboxedTupleKind []
805 roles = [Nominal, Nominal, Nominal, Nominal]
806
807 -- like eqPrimTyCon, but the type for *Representational* coercions
808 -- this should only ever appear as the type of a covar. Its role is
809 -- interpreted in coercionRole
810 eqReprPrimTyCon :: TyCon -- See Note [The equality types story]
811 eqReprPrimTyCon = mkPrimTyCon eqReprPrimTyConName binders res_kind roles
812 where
813 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
814 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
815 res_kind = unboxedTupleKind []
816 roles = [Nominal, Nominal, Representational, Representational]
817
818 -- like eqPrimTyCon, but the type for *Phantom* coercions.
819 -- This is only used to make higher-order equalities. Nothing
820 -- should ever actually have this type!
821 eqPhantPrimTyCon :: TyCon
822 eqPhantPrimTyCon = mkPrimTyCon eqPhantPrimTyConName binders res_kind roles
823 where
824 -- Kind :: forall k1 k2. k1 -> k2 -> Void#
825 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
826 res_kind = unboxedTupleKind []
827 roles = [Nominal, Nominal, Phantom, Phantom]
828
829 {- *********************************************************************
830 * *
831 The primitive array types
832 * *
833 ********************************************************************* -}
834
835 arrayPrimTyCon, mutableArrayPrimTyCon, mutableByteArrayPrimTyCon,
836 byteArrayPrimTyCon, arrayArrayPrimTyCon, mutableArrayArrayPrimTyCon,
837 smallArrayPrimTyCon, smallMutableArrayPrimTyCon :: TyCon
838 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName [Representational] UnliftedRep
839 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName [Nominal, Representational] UnliftedRep
840 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName [Nominal] UnliftedRep
841 byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName UnliftedRep
842 arrayArrayPrimTyCon = pcPrimTyCon0 arrayArrayPrimTyConName UnliftedRep
843 mutableArrayArrayPrimTyCon = pcPrimTyCon mutableArrayArrayPrimTyConName [Nominal] UnliftedRep
844 smallArrayPrimTyCon = pcPrimTyCon smallArrayPrimTyConName [Representational] UnliftedRep
845 smallMutableArrayPrimTyCon = pcPrimTyCon smallMutableArrayPrimTyConName [Nominal, Representational] UnliftedRep
846
847 mkArrayPrimTy :: Type -> Type
848 mkArrayPrimTy elt = TyConApp arrayPrimTyCon [elt]
849 byteArrayPrimTy :: Type
850 byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
851 mkArrayArrayPrimTy :: Type
852 mkArrayArrayPrimTy = mkTyConTy arrayArrayPrimTyCon
853 mkSmallArrayPrimTy :: Type -> Type
854 mkSmallArrayPrimTy elt = TyConApp smallArrayPrimTyCon [elt]
855 mkMutableArrayPrimTy :: Type -> Type -> Type
856 mkMutableArrayPrimTy s elt = TyConApp mutableArrayPrimTyCon [s, elt]
857 mkMutableByteArrayPrimTy :: Type -> Type
858 mkMutableByteArrayPrimTy s = TyConApp mutableByteArrayPrimTyCon [s]
859 mkMutableArrayArrayPrimTy :: Type -> Type
860 mkMutableArrayArrayPrimTy s = TyConApp mutableArrayArrayPrimTyCon [s]
861 mkSmallMutableArrayPrimTy :: Type -> Type -> Type
862 mkSmallMutableArrayPrimTy s elt = TyConApp smallMutableArrayPrimTyCon [s, elt]
863
864
865 {- *********************************************************************
866 * *
867 The mutable variable type
868 * *
869 ********************************************************************* -}
870
871 mutVarPrimTyCon :: TyCon
872 mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName [Nominal, Representational] UnliftedRep
873
874 mkMutVarPrimTy :: Type -> Type -> Type
875 mkMutVarPrimTy s elt = TyConApp mutVarPrimTyCon [s, elt]
876
877 {-
878 ************************************************************************
879 * *
880 \subsection[TysPrim-synch-var]{The synchronizing variable type}
881 * *
882 ************************************************************************
883 -}
884
885 mVarPrimTyCon :: TyCon
886 mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName [Nominal, Representational] UnliftedRep
887
888 mkMVarPrimTy :: Type -> Type -> Type
889 mkMVarPrimTy s elt = TyConApp mVarPrimTyCon [s, elt]
890
891 {-
892 ************************************************************************
893 * *
894 \subsection[TysPrim-stm-var]{The transactional variable type}
895 * *
896 ************************************************************************
897 -}
898
899 tVarPrimTyCon :: TyCon
900 tVarPrimTyCon = pcPrimTyCon tVarPrimTyConName [Nominal, Representational] UnliftedRep
901
902 mkTVarPrimTy :: Type -> Type -> Type
903 mkTVarPrimTy s elt = TyConApp tVarPrimTyCon [s, elt]
904
905 {-
906 ************************************************************************
907 * *
908 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
909 * *
910 ************************************************************************
911 -}
912
913 stablePtrPrimTyCon :: TyCon
914 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName [Representational] AddrRep
915
916 mkStablePtrPrimTy :: Type -> Type
917 mkStablePtrPrimTy ty = TyConApp stablePtrPrimTyCon [ty]
918
919 {-
920 ************************************************************************
921 * *
922 \subsection[TysPrim-stable-names]{The stable-name type}
923 * *
924 ************************************************************************
925 -}
926
927 stableNamePrimTyCon :: TyCon
928 stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName [Representational] UnliftedRep
929
930 mkStableNamePrimTy :: Type -> Type
931 mkStableNamePrimTy ty = TyConApp stableNamePrimTyCon [ty]
932
933 {-
934 ************************************************************************
935 * *
936 \subsection[TysPrim-compact-nfdata]{The Compact NFData (CNF) type}
937 * *
938 ************************************************************************
939 -}
940
941 compactPrimTyCon :: TyCon
942 compactPrimTyCon = pcPrimTyCon0 compactPrimTyConName UnliftedRep
943
944 compactPrimTy :: Type
945 compactPrimTy = mkTyConTy compactPrimTyCon
946
947 {-
948 ************************************************************************
949 * *
950 \subsection[TysPrim-BCOs]{The ``bytecode object'' type}
951 * *
952 ************************************************************************
953 -}
954
955 bcoPrimTy :: Type
956 bcoPrimTy = mkTyConTy bcoPrimTyCon
957 bcoPrimTyCon :: TyCon
958 bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName UnliftedRep
959
960 {-
961 ************************************************************************
962 * *
963 \subsection[TysPrim-Weak]{The ``weak pointer'' type}
964 * *
965 ************************************************************************
966 -}
967
968 weakPrimTyCon :: TyCon
969 weakPrimTyCon = pcPrimTyCon weakPrimTyConName [Representational] UnliftedRep
970
971 mkWeakPrimTy :: Type -> Type
972 mkWeakPrimTy v = TyConApp weakPrimTyCon [v]
973
974 {-
975 ************************************************************************
976 * *
977 \subsection[TysPrim-thread-ids]{The ``thread id'' type}
978 * *
979 ************************************************************************
980
981 A thread id is represented by a pointer to the TSO itself, to ensure
982 that they are always unique and we can always find the TSO for a given
983 thread id. However, this has the unfortunate consequence that a
984 ThreadId# for a given thread is treated as a root by the garbage
985 collector and can keep TSOs around for too long.
986
987 Hence the programmer API for thread manipulation uses a weak pointer
988 to the thread id internally.
989 -}
990
991 threadIdPrimTy :: Type
992 threadIdPrimTy = mkTyConTy threadIdPrimTyCon
993 threadIdPrimTyCon :: TyCon
994 threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName UnliftedRep
995
996 {-
997 ************************************************************************
998 * *
999 \subsection{SIMD vector types}
1000 * *
1001 ************************************************************************
1002 -}
1003
1004 #include "primop-vector-tys.hs-incl"