never executed always true always false
1 {-
2 (c) The GRASP Project, Glasgow University, 1994-1998
3
4 Wired-in knowledge about {\em non-primitive} types
5 -}
6
7 {-# LANGUAGE OverloadedStrings #-}
8
9 {-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
10
11 -- | This module is about types that can be defined in Haskell, but which
12 -- must be wired into the compiler nonetheless. C.f module "GHC.Builtin.Types.Prim"
13 module GHC.Builtin.Types (
14 -- * Helper functions defined here
15 mkWiredInTyConName, -- This is used in GHC.Builtin.Types.Literals to define the
16 -- built-in functions for evaluation.
17
18 mkWiredInIdName, -- used in GHC.Types.Id.Make
19
20 -- * All wired in things
21 wiredInTyCons, isBuiltInOcc_maybe,
22
23 -- * Bool
24 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
25 trueDataCon, trueDataConId, true_RDR,
26 falseDataCon, falseDataConId, false_RDR,
27 promotedFalseDataCon, promotedTrueDataCon,
28
29 -- * Ordering
30 orderingTyCon,
31 ordLTDataCon, ordLTDataConId,
32 ordEQDataCon, ordEQDataConId,
33 ordGTDataCon, ordGTDataConId,
34 promotedLTDataCon, promotedEQDataCon, promotedGTDataCon,
35
36 -- * Boxing primitive types
37 boxingDataCon_maybe,
38
39 -- * Char
40 charTyCon, charDataCon, charTyCon_RDR,
41 charTy, stringTy, charTyConName, stringTyCon_RDR,
42
43 -- * Double
44 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
45
46 -- * Float
47 floatTyCon, floatDataCon, floatTy, floatTyConName,
48
49 -- * Int
50 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
51 intTy,
52
53 -- * Word
54 wordTyCon, wordDataCon, wordTyConName, wordTy,
55
56 -- * Word8
57 word8TyCon, word8DataCon, word8Ty,
58
59 -- * List
60 listTyCon, listTyCon_RDR, listTyConName, listTyConKey,
61 nilDataCon, nilDataConName, nilDataConKey,
62 consDataCon_RDR, consDataCon, consDataConName,
63 promotedNilDataCon, promotedConsDataCon,
64 mkListTy, mkPromotedListTy,
65
66 -- * Maybe
67 maybeTyCon, maybeTyConName,
68 nothingDataCon, nothingDataConName, promotedNothingDataCon,
69 justDataCon, justDataConName, promotedJustDataCon,
70 mkPromotedMaybeTy, mkMaybeTy, isPromotedMaybeTy,
71
72 -- * Tuples
73 mkTupleTy, mkTupleTy1, mkBoxedTupleTy, mkTupleStr,
74 tupleTyCon, tupleDataCon, tupleTyConName, tupleDataConName,
75 promotedTupleDataCon,
76 unitTyCon, unitDataCon, unitDataConId, unitTy, unitTyConKey,
77 pairTyCon, mkPromotedPairTy, isPromotedPairType,
78 unboxedUnitTy,
79 unboxedUnitTyCon, unboxedUnitDataCon,
80 unboxedTupleKind, unboxedSumKind,
81 filterCTuple,
82
83 -- ** Constraint tuples
84 cTupleTyCon, cTupleTyConName, cTupleTyConNames, isCTupleTyConName,
85 cTupleTyConNameArity_maybe,
86 cTupleDataCon, cTupleDataConName, cTupleDataConNames,
87 cTupleSelId, cTupleSelIdName,
88
89 -- * Any
90 anyTyCon, anyTy, anyTypeOfKind,
91
92 -- * Recovery TyCon
93 makeRecoveryTyCon,
94
95 -- * Sums
96 mkSumTy, sumTyCon, sumDataCon,
97
98 -- * Kinds
99 typeSymbolKindCon, typeSymbolKind,
100 isLiftedTypeKindTyConName,
101 typeToTypeKind,
102 liftedRepTyCon, unliftedRepTyCon,
103 constraintKind, liftedTypeKind, unliftedTypeKind,
104 constraintKindTyCon, liftedTypeKindTyCon, unliftedTypeKindTyCon,
105 constraintKindTyConName, liftedTypeKindTyConName, unliftedTypeKindTyConName,
106 liftedRepTyConName, unliftedRepTyConName,
107
108 -- * Equality predicates
109 heqTyCon, heqTyConName, heqClass, heqDataCon,
110 eqTyCon, eqTyConName, eqClass, eqDataCon, eqTyCon_RDR,
111 coercibleTyCon, coercibleTyConName, coercibleDataCon, coercibleClass,
112
113 -- * RuntimeRep and friends
114 runtimeRepTyCon, levityTyCon, vecCountTyCon, vecElemTyCon,
115
116 boxedRepDataConTyCon,
117 runtimeRepTy, levityTy, liftedRepTy, unliftedRepTy,
118
119 vecRepDataConTyCon, tupleRepDataConTyCon, sumRepDataConTyCon,
120
121 liftedDataConTyCon, unliftedDataConTyCon,
122 liftedDataConTy, unliftedDataConTy,
123
124 intRepDataConTy,
125 int8RepDataConTy, int16RepDataConTy, int32RepDataConTy, int64RepDataConTy,
126 wordRepDataConTy,
127 word8RepDataConTy, word16RepDataConTy, word32RepDataConTy, word64RepDataConTy,
128 addrRepDataConTy,
129 floatRepDataConTy, doubleRepDataConTy,
130
131 vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy,
132 vec64DataConTy,
133
134 int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy,
135 int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy,
136 word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy,
137
138 doubleElemRepDataConTy,
139
140 -- * Multiplicity and friends
141 multiplicityTyConName, oneDataConName, manyDataConName, multiplicityTy,
142 multiplicityTyCon, oneDataCon, manyDataCon, oneDataConTy, manyDataConTy,
143 oneDataConTyCon, manyDataConTyCon,
144 multMulTyCon,
145
146 unrestrictedFunTyCon, unrestrictedFunTyConName,
147
148 -- * Bignum
149 integerTy, integerTyCon, integerTyConName,
150 integerISDataCon, integerISDataConName,
151 integerIPDataCon, integerIPDataConName,
152 integerINDataCon, integerINDataConName,
153 naturalTy, naturalTyCon, naturalTyConName,
154 naturalNSDataCon, naturalNSDataConName,
155 naturalNBDataCon, naturalNBDataConName
156
157 ) where
158
159 import GHC.Prelude
160
161 import {-# SOURCE #-} GHC.Types.Id.Make ( mkDataConWorkId, mkDictSelId )
162
163 -- friends:
164 import GHC.Builtin.Names
165 import GHC.Builtin.Types.Prim
166 import GHC.Builtin.Uniques
167
168 -- others:
169 import GHC.Core.Coercion.Axiom
170 import GHC.Types.Id
171 import GHC.Types.TyThing
172 import GHC.Types.SourceText
173 import GHC.Types.Var (VarBndr (Bndr))
174 import GHC.Settings.Constants ( mAX_TUPLE_SIZE, mAX_CTUPLE_SIZE, mAX_SUM_SIZE )
175 import GHC.Unit.Module ( Module )
176 import GHC.Core.Type
177 import qualified GHC.Core.TyCo.Rep as TyCoRep (Type(TyConApp))
178 import GHC.Types.RepType
179 import GHC.Core.DataCon
180 import GHC.Core.ConLike
181 import GHC.Core.TyCon
182 import GHC.Core.Class ( Class, mkClass )
183 import GHC.Types.Name.Reader
184 import GHC.Types.Name as Name
185 import GHC.Types.Name.Env ( NameEnv, mkNameEnv, lookupNameEnv, lookupNameEnv_NF )
186 import GHC.Types.Basic
187 import GHC.Types.ForeignCall
188 import GHC.Types.Unique.Set
189 import Data.Array
190 import GHC.Data.FastString
191 import GHC.Data.BooleanFormula ( mkAnd )
192
193 import GHC.Utils.Outputable
194 import GHC.Utils.Misc
195 import GHC.Utils.Panic
196 import GHC.Utils.Panic.Plain
197
198 import qualified Data.ByteString.Char8 as BS
199
200 import Data.List ( elemIndex )
201
202 alpha_tyvar :: [TyVar]
203 alpha_tyvar = [alphaTyVar]
204
205 alpha_ty :: [Type]
206 alpha_ty = [alphaTy]
207
208 {-
209 Note [Wiring in RuntimeRep]
210 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
211 The RuntimeRep type (and friends) in GHC.Types has a bunch of constructors,
212 making it a pain to wire in. To ease the pain somewhat, we use lists of
213 the different bits, like Uniques, Names, DataCons. These lists must be
214 kept in sync with each other. The rule is this: use the order as declared
215 in GHC.Types. All places where such lists exist should contain a reference
216 to this Note, so a search for this Note's name should find all the lists.
217
218 See also Note [Getting from RuntimeRep to PrimRep] in GHC.Types.RepType.
219
220
221 Note [Wired-in Types and Type Constructors]
222 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
223
224 This module include a lot of wired-in types and type constructors. Here,
225 these are presented in a tabular format to make it easier to find the
226 wired-in type identifier corresponding to a known Haskell type. Data
227 constructors are nested under their corresponding types with two spaces
228 of indentation.
229
230 Identifier Type Haskell name Notes
231 ----------------------------------------------------------------------------
232 liftedTypeKindTyCon TyCon GHC.Types.Type Synonym for: TYPE LiftedRep
233 unliftedTypeKindTyCon TyCon GHC.Types.Type Synonym for: TYPE UnliftedRep
234 liftedRepTyCon TyCon GHC.Types.LiftedRep Synonym for: 'BoxedRep 'Lifted
235 unliftedRepTyCon TyCon GHC.Types.LiftedRep Synonym for: 'BoxedRep 'Unlifted
236 levityTyCon TyCon GHC.Types.Levity Data type
237 liftedDataConTyCon TyCon GHC.Types.Lifted Data constructor
238 unliftedDataConTyCon TyCon GHC.Types.Unlifted Data constructor
239 vecCountTyCon TyCon GHC.Types.VecCount Data type
240 vec2DataConTy Type GHC.Types.Vec2 Data constructor
241 vec4DataConTy Type GHC.Types.Vec4 Data constructor
242 vec8DataConTy Type GHC.Types.Vec8 Data constructor
243 vec16DataConTy Type GHC.Types.Vec16 Data constructor
244 vec32DataConTy Type GHC.Types.Vec32 Data constructor
245 vec64DataConTy Type GHC.Types.Vec64 Data constructor
246 runtimeRepTyCon TyCon GHC.Types.RuntimeRep Data type
247 boxedRepDataConTyCon TyCon GHC.Types.BoxedRep Data constructor
248 intRepDataConTy Type GHC.Types.IntRep Data constructor
249 doubleRepDataConTy Type GHC.Types.DoubleRep Data constructor
250 floatRepDataConTy Type GHC.Types.FloatRep Data constructor
251 boolTyCon TyCon GHC.Types.Bool Data type
252 trueDataCon DataCon GHC.Types.True Data constructor
253 falseDataCon DataCon GHC.Types.False Data constructor
254 promotedTrueDataCon TyCon GHC.Types.True Data constructor
255 promotedFalseDataCon TyCon GHC.Types.False Data constructor
256
257 ************************************************************************
258 * *
259 \subsection{Wired in type constructors}
260 * *
261 ************************************************************************
262
263 If you change which things are wired in, make sure you change their
264 names in GHC.Builtin.Names, so they use wTcQual, wDataQual, etc
265
266 -}
267
268
269 -- This list is used only to define GHC.Builtin.Utils.wiredInThings. That in turn
270 -- is used to initialise the name environment carried around by the renamer.
271 -- This means that if we look up the name of a TyCon (or its implicit binders)
272 -- that occurs in this list that name will be assigned the wired-in key we
273 -- define here.
274 --
275 -- Because of their infinite nature, this list excludes
276 -- * tuples, including boxed, unboxed and constraint tuples
277 --- (mkTupleTyCon, unitTyCon, pairTyCon)
278 -- * unboxed sums (sumTyCon)
279 -- See Note [Infinite families of known-key names] in GHC.Builtin.Names
280 --
281 -- See also Note [Known-key names]
282 wiredInTyCons :: [TyCon]
283
284 wiredInTyCons = [ -- Units are not treated like other tuples, because they
285 -- are defined in GHC.Base, and there's only a few of them. We
286 -- put them in wiredInTyCons so that they will pre-populate
287 -- the name cache, so the parser in isBuiltInOcc_maybe doesn't
288 -- need to look out for them.
289 unitTyCon
290 , unboxedUnitTyCon
291 , anyTyCon
292 , boolTyCon
293 , charTyCon
294 , stringTyCon
295 , doubleTyCon
296 , floatTyCon
297 , intTyCon
298 , wordTyCon
299 , listTyCon
300 , orderingTyCon
301 , maybeTyCon
302 , heqTyCon
303 , eqTyCon
304 , coercibleTyCon
305 , typeSymbolKindCon
306 , runtimeRepTyCon
307 , levityTyCon
308 , vecCountTyCon
309 , vecElemTyCon
310 , constraintKindTyCon
311 , liftedTypeKindTyCon
312 , unliftedTypeKindTyCon
313 , multiplicityTyCon
314 , naturalTyCon
315 , integerTyCon
316 , liftedRepTyCon
317 , unliftedRepTyCon
318 ]
319
320 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
321 mkWiredInTyConName built_in modu fs unique tycon
322 = mkWiredInName modu (mkTcOccFS fs) unique
323 (ATyCon tycon) -- Relevant TyCon
324 built_in
325
326 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
327 mkWiredInDataConName built_in modu fs unique datacon
328 = mkWiredInName modu (mkDataOccFS fs) unique
329 (AConLike (RealDataCon datacon)) -- Relevant DataCon
330 built_in
331
332 mkWiredInIdName :: Module -> FastString -> Unique -> Id -> Name
333 mkWiredInIdName mod fs uniq id
334 = mkWiredInName mod (mkOccNameFS Name.varName fs) uniq (AnId id) UserSyntax
335
336 -- See Note [Kind-changing of (~) and Coercible]
337 -- in libraries/ghc-prim/GHC/Types.hs
338 eqTyConName, eqDataConName, eqSCSelIdName :: Name
339 eqTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "~") eqTyConKey eqTyCon
340 eqDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "Eq#") eqDataConKey eqDataCon
341 eqSCSelIdName = mkWiredInIdName gHC_TYPES (fsLit "eq_sel") eqSCSelIdKey eqSCSelId
342
343 {- Note [eqTyCon (~) is built-in syntax]
344 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
345 The (~) type operator used in equality constraints (a~b) is considered built-in
346 syntax. This has a few consequences:
347
348 * The user is not allowed to define their own type constructors with this name:
349
350 ghci> class a ~ b
351 <interactive>:1:1: error: Illegal binding of built-in syntax: ~
352
353 * Writing (a ~ b) does not require enabling -XTypeOperators. It does, however,
354 require -XGADTs or -XTypeFamilies.
355
356 * The (~) type operator is always in scope. It doesn't need to be imported,
357 and it cannot be hidden.
358
359 * We have a bunch of special cases in the compiler to arrange all of the above.
360
361 There's no particular reason for (~) to be special, but fixing this would be a
362 breaking change.
363 -}
364 eqTyCon_RDR :: RdrName
365 eqTyCon_RDR = nameRdrName eqTyConName
366
367 -- See Note [Kind-changing of (~) and Coercible]
368 -- in libraries/ghc-prim/GHC/Types.hs
369 heqTyConName, heqDataConName, heqSCSelIdName :: Name
370 heqTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "~~") heqTyConKey heqTyCon
371 heqDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "HEq#") heqDataConKey heqDataCon
372 heqSCSelIdName = mkWiredInIdName gHC_TYPES (fsLit "heq_sel") heqSCSelIdKey heqSCSelId
373
374 -- See Note [Kind-changing of (~) and Coercible] in libraries/ghc-prim/GHC/Types.hs
375 coercibleTyConName, coercibleDataConName, coercibleSCSelIdName :: Name
376 coercibleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Coercible") coercibleTyConKey coercibleTyCon
377 coercibleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "MkCoercible") coercibleDataConKey coercibleDataCon
378 coercibleSCSelIdName = mkWiredInIdName gHC_TYPES (fsLit "coercible_sel") coercibleSCSelIdKey coercibleSCSelId
379
380 charTyConName, charDataConName, intTyConName, intDataConName, stringTyConName :: Name
381 charTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
382 charDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
383 stringTyConName = mkWiredInTyConName UserSyntax gHC_BASE (fsLit "String") stringTyConKey stringTyCon
384 intTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Int") intTyConKey intTyCon
385 intDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey intDataCon
386
387 boolTyConName, falseDataConName, trueDataConName :: Name
388 boolTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Bool") boolTyConKey boolTyCon
389 falseDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "False") falseDataConKey falseDataCon
390 trueDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "True") trueDataConKey trueDataCon
391
392 listTyConName, nilDataConName, consDataConName :: Name
393 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
394 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
395 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
396
397 maybeTyConName, nothingDataConName, justDataConName :: Name
398 maybeTyConName = mkWiredInTyConName UserSyntax gHC_MAYBE (fsLit "Maybe")
399 maybeTyConKey maybeTyCon
400 nothingDataConName = mkWiredInDataConName UserSyntax gHC_MAYBE (fsLit "Nothing")
401 nothingDataConKey nothingDataCon
402 justDataConName = mkWiredInDataConName UserSyntax gHC_MAYBE (fsLit "Just")
403 justDataConKey justDataCon
404
405 wordTyConName, wordDataConName, word8DataConName :: Name
406 wordTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Word") wordTyConKey wordTyCon
407 wordDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "W#") wordDataConKey wordDataCon
408 word8DataConName = mkWiredInDataConName UserSyntax gHC_WORD (fsLit "W8#") word8DataConKey word8DataCon
409
410 floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
411 floatTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Float") floatTyConKey floatTyCon
412 floatDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#") floatDataConKey floatDataCon
413 doubleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey doubleTyCon
414 doubleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#") doubleDataConKey doubleDataCon
415
416 -- Any
417
418 {-
419 Note [Any types]
420 ~~~~~~~~~~~~~~~~
421 The type constructor Any,
422
423 type family Any :: k where { }
424
425 It has these properties:
426
427 * Note that 'Any' is kind polymorphic since in some program we may
428 need to use Any to fill in a type variable of some kind other than *
429 (see #959 for examples). Its kind is thus `forall k. k``.
430
431 * It is defined in module GHC.Types, and exported so that it is
432 available to users. For this reason it's treated like any other
433 wired-in type:
434 - has a fixed unique, anyTyConKey,
435 - lives in the global name cache
436
437 * It is a *closed* type family, with no instances. This means that
438 if ty :: '(k1, k2) we add a given coercion
439 g :: ty ~ (Fst ty, Snd ty)
440 If Any was a *data* type, then we'd get inconsistency because 'ty'
441 could be (Any '(k1,k2)) and then we'd have an equality with Any on
442 one side and '(,) on the other. See also #9097 and #9636.
443
444 * When instantiated at a lifted type it is inhabited by at least one value,
445 namely bottom
446
447 * You can safely coerce any /lifted/ type to Any, and back with unsafeCoerce.
448
449 * It does not claim to be a *data* type, and that's important for
450 the code generator, because the code gen may *enter* a data value
451 but never enters a function value.
452
453 * It is wired-in so we can easily refer to it where we don't have a name
454 environment (e.g. see Rules.matchRule for one example)
455
456 * If (Any k) is the type of a value, it must be a /lifted/ value. So
457 if we have (Any @(TYPE rr)) then rr must be 'LiftedRep. See
458 Note [TYPE and RuntimeRep] in GHC.Builtin.Types.Prim. This is a convenient
459 invariant, and makes isUnliftedTyCon well-defined; otherwise what
460 would (isUnliftedTyCon Any) be?
461
462 It's used to instantiate un-constrained type variables after type checking. For
463 example, 'length' has type
464
465 length :: forall a. [a] -> Int
466
467 and the list datacon for the empty list has type
468
469 [] :: forall a. [a]
470
471 In order to compose these two terms as @length []@ a type
472 application is required, but there is no constraint on the
473 choice. In this situation GHC uses 'Any',
474
475 > length (Any *) ([] (Any *))
476
477 Above, we print kinds explicitly, as if with --fprint-explicit-kinds.
478
479 The Any tycon used to be quite magic, but we have since been able to
480 implement it merely with an empty kind polymorphic type family. See #10886 for a
481 bit of history.
482 -}
483
484
485 anyTyConName :: Name
486 anyTyConName =
487 mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Any") anyTyConKey anyTyCon
488
489 anyTyCon :: TyCon
490 anyTyCon = mkFamilyTyCon anyTyConName binders res_kind Nothing
491 (ClosedSynFamilyTyCon Nothing)
492 Nothing
493 NotInjective
494 where
495 binders@[kv] = mkTemplateKindTyConBinders [liftedTypeKind]
496 res_kind = mkTyVarTy (binderVar kv)
497
498 anyTy :: Type
499 anyTy = mkTyConTy anyTyCon
500
501 anyTypeOfKind :: Kind -> Type
502 anyTypeOfKind kind = mkTyConApp anyTyCon [kind]
503
504 -- | Make a fake, recovery 'TyCon' from an existing one.
505 -- Used when recovering from errors in type declarations
506 makeRecoveryTyCon :: TyCon -> TyCon
507 makeRecoveryTyCon tc
508 = mkTcTyCon (tyConName tc)
509 bndrs res_kind
510 noTcTyConScopedTyVars
511 True -- Fully generalised
512 flavour -- Keep old flavour
513 where
514 flavour = tyConFlavour tc
515 [kv] = mkTemplateKindVars [liftedTypeKind]
516 (bndrs, res_kind)
517 = case flavour of
518 PromotedDataConFlavour -> ([mkNamedTyConBinder Inferred kv], mkTyVarTy kv)
519 _ -> (tyConBinders tc, tyConResKind tc)
520 -- For data types we have already validated their kind, so it
521 -- makes sense to keep it. For promoted data constructors we haven't,
522 -- so we recover with kind (forall k. k). Otherwise consider
523 -- data T a where { MkT :: Show a => T a }
524 -- If T is for some reason invalid, we don't want to fall over
525 -- at (promoted) use-sites of MkT.
526
527 -- Kinds
528 typeSymbolKindConName :: Name
529 typeSymbolKindConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Symbol") typeSymbolKindConNameKey typeSymbolKindCon
530
531 constraintKindTyConName :: Name
532 constraintKindTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Constraint") constraintKindTyConKey constraintKindTyCon
533
534 liftedTypeKindTyConName, unliftedTypeKindTyConName :: Name
535 liftedTypeKindTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Type") liftedTypeKindTyConKey liftedTypeKindTyCon
536 unliftedTypeKindTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "UnliftedType") unliftedTypeKindTyConKey unliftedTypeKindTyCon
537
538 liftedRepTyConName, unliftedRepTyConName :: Name
539 liftedRepTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "LiftedRep") liftedRepTyConKey liftedRepTyCon
540 unliftedRepTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "UnliftedRep") unliftedRepTyConKey unliftedRepTyCon
541
542 multiplicityTyConName :: Name
543 multiplicityTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Multiplicity")
544 multiplicityTyConKey multiplicityTyCon
545
546 oneDataConName, manyDataConName :: Name
547 oneDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "One") oneDataConKey oneDataCon
548 manyDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "Many") manyDataConKey manyDataCon
549 -- It feels wrong to have One and Many be BuiltInSyntax. But otherwise,
550 -- `Many`, in particular, is considered out of scope unless an appropriate
551 -- file is open. The problem with this is that `Many` appears implicitly in
552 -- types every time there is an `(->)`, hence out-of-scope errors get
553 -- reported. Making them built-in make it so that they are always considered in
554 -- scope.
555
556 runtimeRepTyConName, vecRepDataConName, tupleRepDataConName, sumRepDataConName, boxedRepDataConName :: Name
557 runtimeRepTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "RuntimeRep") runtimeRepTyConKey runtimeRepTyCon
558 vecRepDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "VecRep") vecRepDataConKey vecRepDataCon
559 tupleRepDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "TupleRep") tupleRepDataConKey tupleRepDataCon
560 sumRepDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "SumRep") sumRepDataConKey sumRepDataCon
561 boxedRepDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "BoxedRep") boxedRepDataConKey boxedRepDataCon
562
563 levityTyConName, liftedDataConName, unliftedDataConName :: Name
564 levityTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Levity") levityTyConKey levityTyCon
565 liftedDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "Lifted") liftedDataConKey liftedDataCon
566 unliftedDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "Unlifted") unliftedDataConKey unliftedDataCon
567
568
569 -- See Note [Wiring in RuntimeRep]
570 runtimeRepSimpleDataConNames :: [Name]
571 runtimeRepSimpleDataConNames
572 = zipWith3Lazy mk_special_dc_name
573 [ fsLit "IntRep"
574 , fsLit "Int8Rep", fsLit "Int16Rep", fsLit "Int32Rep", fsLit "Int64Rep"
575 , fsLit "WordRep"
576 , fsLit "Word8Rep", fsLit "Word16Rep", fsLit "Word32Rep", fsLit "Word64Rep"
577 , fsLit "AddrRep"
578 , fsLit "FloatRep", fsLit "DoubleRep"
579 ]
580 runtimeRepSimpleDataConKeys
581 runtimeRepSimpleDataCons
582
583 vecCountTyConName :: Name
584 vecCountTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "VecCount") vecCountTyConKey vecCountTyCon
585
586 -- See Note [Wiring in RuntimeRep]
587 vecCountDataConNames :: [Name]
588 vecCountDataConNames = zipWith3Lazy mk_special_dc_name
589 [ fsLit "Vec2", fsLit "Vec4", fsLit "Vec8"
590 , fsLit "Vec16", fsLit "Vec32", fsLit "Vec64" ]
591 vecCountDataConKeys
592 vecCountDataCons
593
594 vecElemTyConName :: Name
595 vecElemTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "VecElem") vecElemTyConKey vecElemTyCon
596
597 -- See Note [Wiring in RuntimeRep]
598 vecElemDataConNames :: [Name]
599 vecElemDataConNames = zipWith3Lazy mk_special_dc_name
600 [ fsLit "Int8ElemRep", fsLit "Int16ElemRep", fsLit "Int32ElemRep"
601 , fsLit "Int64ElemRep", fsLit "Word8ElemRep", fsLit "Word16ElemRep"
602 , fsLit "Word32ElemRep", fsLit "Word64ElemRep"
603 , fsLit "FloatElemRep", fsLit "DoubleElemRep" ]
604 vecElemDataConKeys
605 vecElemDataCons
606
607 mk_special_dc_name :: FastString -> Unique -> DataCon -> Name
608 mk_special_dc_name fs u dc = mkWiredInDataConName UserSyntax gHC_TYPES fs u dc
609
610 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR, stringTyCon_RDR,
611 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR :: RdrName
612 boolTyCon_RDR = nameRdrName boolTyConName
613 false_RDR = nameRdrName falseDataConName
614 true_RDR = nameRdrName trueDataConName
615 intTyCon_RDR = nameRdrName intTyConName
616 charTyCon_RDR = nameRdrName charTyConName
617 stringTyCon_RDR = nameRdrName stringTyConName
618 intDataCon_RDR = nameRdrName intDataConName
619 listTyCon_RDR = nameRdrName listTyConName
620 consDataCon_RDR = nameRdrName consDataConName
621
622 {-
623 ************************************************************************
624 * *
625 \subsection{mkWiredInTyCon}
626 * *
627 ************************************************************************
628 -}
629
630 -- This function assumes that the types it creates have all parameters at
631 -- Representational role, and that there is no kind polymorphism.
632 pcTyCon :: Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
633 pcTyCon name cType tyvars cons
634 = mkAlgTyCon name
635 (mkAnonTyConBinders VisArg tyvars)
636 liftedTypeKind
637 (map (const Representational) tyvars)
638 cType
639 [] -- No stupid theta
640 (mkDataTyConRhs cons)
641 (VanillaAlgTyCon (mkPrelTyConRepName name))
642 False -- Not in GADT syntax
643
644 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
645 pcDataCon n univs tys = pcDataConW n univs (map linear tys)
646
647 pcDataConW :: Name -> [TyVar] -> [Scaled Type] -> TyCon -> DataCon
648 pcDataConW n univs tys = pcDataConWithFixity False n univs
649 [] -- no ex_tvs
650 univs -- the univs are precisely the user-written tyvars
651 tys
652
653 pcDataConWithFixity :: Bool -- ^ declared infix?
654 -> Name -- ^ datacon name
655 -> [TyVar] -- ^ univ tyvars
656 -> [TyCoVar] -- ^ ex tycovars
657 -> [TyCoVar] -- ^ user-written tycovars
658 -> [Scaled Type] -- ^ args
659 -> TyCon
660 -> DataCon
661 pcDataConWithFixity infx n = pcDataConWithFixity' infx n (dataConWorkerUnique (nameUnique n))
662 NoRRI
663 -- The Name's unique is the first of two free uniques;
664 -- the first is used for the datacon itself,
665 -- the second is used for the "worker name"
666 --
667 -- To support this the mkPreludeDataConUnique function "allocates"
668 -- one DataCon unique per pair of Ints.
669
670 pcDataConWithFixity' :: Bool -> Name -> Unique -> RuntimeRepInfo
671 -> [TyVar] -> [TyCoVar] -> [TyCoVar]
672 -> [Scaled Type] -> TyCon -> DataCon
673 -- The Name should be in the DataName name space; it's the name
674 -- of the DataCon itself.
675 --
676 -- IMPORTANT NOTE:
677 -- if you try to wire-in a /GADT/ data constructor you will
678 -- find it hard (we did). You will need wrapper and worker
679 -- Names, a DataConBoxer, DataConRep, EqSpec, etc.
680 -- Try hard not to wire-in GADT data types. You will live
681 -- to regret doing so (we do).
682
683 pcDataConWithFixity' declared_infix dc_name wrk_key rri
684 tyvars ex_tyvars user_tyvars arg_tys tycon
685 = data_con
686 where
687 tag_map = mkTyConTagMap tycon
688 -- This constructs the constructor Name to ConTag map once per
689 -- constructor, which is quadratic. It's OK here, because it's
690 -- only called for wired in data types that don't have a lot of
691 -- constructors. It's also likely that GHC will lift tag_map, since
692 -- we call pcDataConWithFixity' with static TyCons in the same module.
693 -- See Note [Constructor tag allocation] and #14657
694 data_con = mkDataCon dc_name declared_infix prom_info
695 (map (const no_bang) arg_tys)
696 [] -- No labelled fields
697 tyvars ex_tyvars
698 (mkTyVarBinders SpecifiedSpec user_tyvars)
699 [] -- No equality spec
700 [] -- No theta
701 arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
702 rri
703 tycon
704 (lookupNameEnv_NF tag_map dc_name)
705 [] -- No stupid theta
706 (mkDataConWorkId wrk_name data_con)
707 NoDataConRep -- Wired-in types are too simple to need wrappers
708
709 no_bang = HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict
710
711 wrk_name = mkDataConWorkerName data_con wrk_key
712
713 prom_info = mkPrelTyConRepName dc_name
714
715 mkDataConWorkerName :: DataCon -> Unique -> Name
716 mkDataConWorkerName data_con wrk_key =
717 mkWiredInName modu wrk_occ wrk_key
718 (AnId (dataConWorkId data_con)) UserSyntax
719 where
720 modu = assert (isExternalName dc_name) $
721 nameModule dc_name
722 dc_name = dataConName data_con
723 dc_occ = nameOccName dc_name
724 wrk_occ = mkDataConWorkerOcc dc_occ
725
726 -- used for RuntimeRep and friends
727 pcSpecialDataCon :: Name -> [Type] -> TyCon -> RuntimeRepInfo -> DataCon
728 pcSpecialDataCon dc_name arg_tys tycon rri
729 = pcDataConWithFixity' False dc_name (dataConWorkerUnique (nameUnique dc_name)) rri
730 [] [] [] (map linear arg_tys) tycon
731
732 {-
733 ************************************************************************
734 * *
735 Kinds
736 * *
737 ************************************************************************
738 -}
739
740 typeSymbolKindCon :: TyCon
741 -- data Symbol
742 typeSymbolKindCon = pcTyCon typeSymbolKindConName Nothing [] []
743
744 typeSymbolKind :: Kind
745 typeSymbolKind = mkTyConTy typeSymbolKindCon
746
747 constraintKindTyCon :: TyCon
748 -- 'TyCon.isConstraintKindCon' assumes that this is an AlgTyCon!
749 constraintKindTyCon = pcTyCon constraintKindTyConName Nothing [] []
750
751 -- See Note [Prefer Type over TYPE 'LiftedRep] in GHC.Core.TyCo.Rep.
752 liftedTypeKind, unliftedTypeKind, typeToTypeKind, constraintKind :: Kind
753 liftedTypeKind = mkTyConTy liftedTypeKindTyCon
754 unliftedTypeKind = mkTyConTy unliftedTypeKindTyCon
755 typeToTypeKind = liftedTypeKind `mkVisFunTyMany` liftedTypeKind
756 constraintKind = mkTyConTy constraintKindTyCon
757
758 {-
759 ************************************************************************
760 * *
761 Stuff for dealing with tuples
762 * *
763 ************************************************************************
764
765 Note [How tuples work] See also Note [Known-key names] in GHC.Builtin.Names
766 ~~~~~~~~~~~~~~~~~~~~~~
767 * There are three families of tuple TyCons and corresponding
768 DataCons, expressed by the type BasicTypes.TupleSort:
769 data TupleSort = BoxedTuple | UnboxedTuple | ConstraintTuple
770
771 * All three families are AlgTyCons, whose AlgTyConRhs is TupleTyCon
772
773 * BoxedTuples
774 - A wired-in type
775 - Data type declarations in GHC.Tuple
776 - The data constructors really have an info table
777
778 * UnboxedTuples
779 - A wired-in type
780 - Have a pretend DataCon, defined in GHC.Prim,
781 but no actual declaration and no info table
782
783 * ConstraintTuples
784 - A wired-in type.
785 - Declared as classes in GHC.Classes, e.g.
786 class (c1,c2) => (c1,c2)
787 - Given constraints: the superclasses automatically become available
788 - Wanted constraints: there is a built-in instance
789 instance (c1,c2) => (c1,c2)
790 See GHC.Tc.Instance.Class.matchCTuple
791 - Currently just go up to 64; beyond that
792 you have to use manual nesting
793 - Their OccNames look like (%,,,%), so they can easily be
794 distinguished from term tuples. But (following Haskell) we
795 pretty-print saturated constraint tuples with round parens;
796 see BasicTypes.tupleParens.
797 - Unlike BoxedTuples and UnboxedTuples, which only wire
798 in type constructors and data constructors, ConstraintTuples also wire in
799 superclass selector functions. For instance, $p1(%,%) and $p2(%,%) are
800 the selectors for the binary constraint tuple.
801
802 * In quite a lot of places things are restricted just to
803 BoxedTuple/UnboxedTuple, and then we used BasicTypes.Boxity to distinguish
804 E.g. tupleTyCon has a Boxity argument
805
806 * When looking up an OccName in the original-name cache
807 (GHC.Iface.Env.lookupOrigNameCache), we spot the tuple OccName to make sure
808 we get the right wired-in name. This guy can't tell the difference
809 between BoxedTuple and ConstraintTuple (same OccName!), so tuples
810 are not serialised into interface files using OccNames at all.
811
812 * Serialization to interface files works via the usual mechanism for known-key
813 things: instead of serializing the OccName we just serialize the key. During
814 deserialization we lookup the Name associated with the unique with the logic
815 in GHC.Builtin.Uniques. See Note [Symbol table representation of names] for details.
816
817 Note [One-tuples]
818 ~~~~~~~~~~~~~~~~~
819 GHC supports both boxed and unboxed one-tuples:
820 - Unboxed one-tuples are sometimes useful when returning a
821 single value after CPR analysis
822 - A boxed one-tuple is used by GHC.HsToCore.Utils.mkSelectorBinds, when
823 there is just one binder
824 Basically it keeps everything uniform.
825
826 However the /naming/ of the type/data constructors for one-tuples is a
827 bit odd:
828 3-tuples: (,,) (,,)#
829 2-tuples: (,) (,)#
830 1-tuples: ??
831 0-tuples: () ()#
832
833 Zero-tuples have used up the logical name. So we use 'Solo' and 'Solo#'
834 for one-tuples. So in ghc-prim:GHC.Tuple we see the declarations:
835 data () = ()
836 data Solo a = Solo a
837 data (a,b) = (a,b)
838
839 There is no way to write a boxed one-tuple in Haskell using tuple syntax.
840 They can, however, be written using other methods:
841
842 1. They can be written directly by importing them from GHC.Tuple.
843 2. They can be generated by way of Template Haskell or in `deriving` code.
844
845 There is nothing special about one-tuples in Core; in particular, they have no
846 custom pretty-printing, just using `Solo`.
847
848 Note that there is *not* a unary constraint tuple, unlike for other forms of
849 tuples. See [Ignore unary constraint tuples] in GHC.Tc.Gen.HsType for more
850 details.
851
852 See also Note [Flattening one-tuples] in GHC.Core.Make and
853 Note [Don't flatten tuples from HsSyn] in GHC.Core.Make.
854
855 -----
856 -- Wrinkle: Make boxed one-tuple names have known keys
857 -----
858
859 We make boxed one-tuple names have known keys so that `data Solo a = Solo a`,
860 defined in GHC.Tuple, will be used when one-tuples are spliced in through
861 Template Haskell. This program (from #18097) crucially relies on this:
862
863 case $( tupE [ [| "ok" |] ] ) of Solo x -> putStrLn x
864
865 Unless Solo has a known key, the type of `$( tupE [ [| "ok" |] ] )` (an
866 ExplicitTuple of length 1) will not match the type of Solo (an ordinary
867 data constructor used in a pattern). Making Solo known-key allows GHC to make
868 this connection.
869
870 Unlike Solo, every other tuple is /not/ known-key
871 (see Note [Infinite families of known-key names] in GHC.Builtin.Names). The
872 main reason for this exception is that other tuples are written with special
873 syntax, and as a result, they are renamed using a special `isBuiltInOcc_maybe`
874 function (see Note [Built-in syntax and the OrigNameCache] in GHC.Types.Name.Cache).
875 In contrast, Solo is just an ordinary data type with no special syntax, so it
876 doesn't really make sense to handle it in `isBuiltInOcc_maybe`. Making Solo
877 known-key is the next-best way to teach the internals of the compiler about it.
878 -}
879
880 -- | Built-in syntax isn't "in scope" so these OccNames map to wired-in Names
881 -- with BuiltInSyntax. However, this should only be necessary while resolving
882 -- names produced by Template Haskell splices since we take care to encode
883 -- built-in syntax names specially in interface files. See
884 -- Note [Symbol table representation of names].
885 --
886 -- Moreover, there is no need to include names of things that the user can't
887 -- write (e.g. type representation bindings like $tc(,,,)).
888 isBuiltInOcc_maybe :: OccName -> Maybe Name
889 isBuiltInOcc_maybe occ =
890 case name of
891 "[]" -> Just $ choose_ns listTyConName nilDataConName
892 ":" -> Just consDataConName
893
894 -- equality tycon
895 "~" -> Just eqTyConName
896
897 -- function tycon
898 "FUN" -> Just funTyConName
899 "->" -> Just unrestrictedFunTyConName
900
901 -- boxed tuple data/tycon
902 -- We deliberately exclude Solo (the boxed 1-tuple).
903 -- See Note [One-tuples] (Wrinkle: Make boxed one-tuple names have known keys)
904 "()" -> Just $ tup_name Boxed 0
905 _ | Just rest <- "(" `BS.stripPrefix` name
906 , (commas, rest') <- BS.span (==',') rest
907 , ")" <- rest'
908 -> Just $ tup_name Boxed (1+BS.length commas)
909
910 -- unboxed tuple data/tycon
911 "(##)" -> Just $ tup_name Unboxed 0
912 "Solo#" -> Just $ tup_name Unboxed 1
913 _ | Just rest <- "(#" `BS.stripPrefix` name
914 , (commas, rest') <- BS.span (==',') rest
915 , "#)" <- rest'
916 -> Just $ tup_name Unboxed (1+BS.length commas)
917
918 -- unboxed sum tycon
919 _ | Just rest <- "(#" `BS.stripPrefix` name
920 , (pipes, rest') <- BS.span (=='|') rest
921 , "#)" <- rest'
922 -> Just $ tyConName $ sumTyCon (1+BS.length pipes)
923
924 -- unboxed sum datacon
925 _ | Just rest <- "(#" `BS.stripPrefix` name
926 , (pipes1, rest') <- BS.span (=='|') rest
927 , Just rest'' <- "_" `BS.stripPrefix` rest'
928 , (pipes2, rest''') <- BS.span (=='|') rest''
929 , "#)" <- rest'''
930 -> let arity = BS.length pipes1 + BS.length pipes2 + 1
931 alt = BS.length pipes1 + 1
932 in Just $ dataConName $ sumDataCon alt arity
933 _ -> Nothing
934 where
935 name = bytesFS $ occNameFS occ
936
937 choose_ns :: Name -> Name -> Name
938 choose_ns tc dc
939 | isTcClsNameSpace ns = tc
940 | isDataConNameSpace ns = dc
941 | otherwise = pprPanic "tup_name" (ppr occ)
942 where ns = occNameSpace occ
943
944 tup_name boxity arity
945 = choose_ns (getName (tupleTyCon boxity arity))
946 (getName (tupleDataCon boxity arity))
947
948 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
949 -- No need to cache these, the caching is done in mk_tuple
950 mkTupleOcc ns Boxed ar = mkOccName ns (mkBoxedTupleStr ar)
951 mkTupleOcc ns Unboxed ar = mkOccName ns (mkUnboxedTupleStr ar)
952
953 mkCTupleOcc :: NameSpace -> Arity -> OccName
954 mkCTupleOcc ns ar = mkOccName ns (mkConstraintTupleStr ar)
955
956 mkTupleStr :: Boxity -> Arity -> String
957 mkTupleStr Boxed = mkBoxedTupleStr
958 mkTupleStr Unboxed = mkUnboxedTupleStr
959
960 mkBoxedTupleStr :: Arity -> String
961 mkBoxedTupleStr 0 = "()"
962 mkBoxedTupleStr 1 = "Solo" -- See Note [One-tuples]
963 mkBoxedTupleStr ar = '(' : commas ar ++ ")"
964
965 mkUnboxedTupleStr :: Arity -> String
966 mkUnboxedTupleStr 0 = "(##)"
967 mkUnboxedTupleStr 1 = "Solo#" -- See Note [One-tuples]
968 mkUnboxedTupleStr ar = "(#" ++ commas ar ++ "#)"
969
970 mkConstraintTupleStr :: Arity -> String
971 mkConstraintTupleStr 0 = "(%%)"
972 mkConstraintTupleStr 1 = "Solo%" -- See Note [One-tuples]
973 mkConstraintTupleStr ar = "(%" ++ commas ar ++ "%)"
974
975 commas :: Arity -> String
976 commas ar = take (ar-1) (repeat ',')
977
978 cTupleTyCon :: Arity -> TyCon
979 cTupleTyCon i
980 | i > mAX_CTUPLE_SIZE = fstOf3 (mk_ctuple i) -- Build one specially
981 | otherwise = fstOf3 (cTupleArr ! i)
982
983 cTupleTyConName :: Arity -> Name
984 cTupleTyConName a = tyConName (cTupleTyCon a)
985
986 cTupleTyConNames :: [Name]
987 cTupleTyConNames = map cTupleTyConName (0 : [2..mAX_CTUPLE_SIZE])
988
989 cTupleTyConKeys :: UniqSet Unique
990 cTupleTyConKeys = mkUniqSet $ map getUnique cTupleTyConNames
991
992 isCTupleTyConName :: Name -> Bool
993 isCTupleTyConName n
994 = assertPpr (isExternalName n) (ppr n) $
995 getUnique n `elementOfUniqSet` cTupleTyConKeys
996
997 -- | If the given name is that of a constraint tuple, return its arity.
998 cTupleTyConNameArity_maybe :: Name -> Maybe Arity
999 cTupleTyConNameArity_maybe n
1000 | not (isCTupleTyConName n) = Nothing
1001 | otherwise = fmap adjustArity (n `elemIndex` cTupleTyConNames)
1002 where
1003 -- Since `cTupleTyConNames` jumps straight from the `0` to the `2`
1004 -- case, we have to adjust accordingly our calculated arity.
1005 adjustArity a = if a > 0 then a + 1 else a
1006
1007 cTupleDataCon :: Arity -> DataCon
1008 cTupleDataCon i
1009 | i > mAX_CTUPLE_SIZE = sndOf3 (mk_ctuple i) -- Build one specially
1010 | otherwise = sndOf3 (cTupleArr ! i)
1011
1012 cTupleDataConName :: Arity -> Name
1013 cTupleDataConName i = dataConName (cTupleDataCon i)
1014
1015 cTupleDataConNames :: [Name]
1016 cTupleDataConNames = map cTupleDataConName (0 : [2..mAX_CTUPLE_SIZE])
1017
1018 cTupleSelId :: ConTag -- Superclass position
1019 -> Arity -- Arity
1020 -> Id
1021 cTupleSelId sc_pos arity
1022 | sc_pos > arity
1023 = panic ("cTupleSelId: index out of bounds: superclass position: "
1024 ++ show sc_pos ++ " > arity " ++ show arity)
1025
1026 | sc_pos <= 0
1027 = panic ("cTupleSelId: Superclass positions start from 1. "
1028 ++ "(superclass position: " ++ show sc_pos
1029 ++ ", arity: " ++ show arity ++ ")")
1030
1031 | arity < 2
1032 = panic ("cTupleSelId: Arity starts from 2. "
1033 ++ "(superclass position: " ++ show sc_pos
1034 ++ ", arity: " ++ show arity ++ ")")
1035
1036 | arity > mAX_CTUPLE_SIZE
1037 = thdOf3 (mk_ctuple arity) ! (sc_pos - 1) -- Build one specially
1038
1039 | otherwise
1040 = thdOf3 (cTupleArr ! arity) ! (sc_pos - 1)
1041
1042 cTupleSelIdName :: ConTag -- Superclass position
1043 -> Arity -- Arity
1044 -> Name
1045 cTupleSelIdName sc_pos arity = idName (cTupleSelId sc_pos arity)
1046
1047 tupleTyCon :: Boxity -> Arity -> TyCon
1048 tupleTyCon sort i | i > mAX_TUPLE_SIZE = fst (mk_tuple sort i) -- Build one specially
1049 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
1050 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
1051
1052 tupleTyConName :: TupleSort -> Arity -> Name
1053 tupleTyConName ConstraintTuple a = cTupleTyConName a
1054 tupleTyConName BoxedTuple a = tyConName (tupleTyCon Boxed a)
1055 tupleTyConName UnboxedTuple a = tyConName (tupleTyCon Unboxed a)
1056
1057 promotedTupleDataCon :: Boxity -> Arity -> TyCon
1058 promotedTupleDataCon boxity i = promoteDataCon (tupleDataCon boxity i)
1059
1060 tupleDataCon :: Boxity -> Arity -> DataCon
1061 tupleDataCon sort i | i > mAX_TUPLE_SIZE = snd (mk_tuple sort i) -- Build one specially
1062 tupleDataCon Boxed i = snd (boxedTupleArr ! i)
1063 tupleDataCon Unboxed i = snd (unboxedTupleArr ! i)
1064
1065 tupleDataConName :: Boxity -> Arity -> Name
1066 tupleDataConName sort i = dataConName (tupleDataCon sort i)
1067
1068 mkPromotedPairTy :: Kind -> Kind -> Type -> Type -> Type
1069 mkPromotedPairTy k1 k2 t1 t2 = mkTyConApp (promotedTupleDataCon Boxed 2) [k1,k2,t1,t2]
1070
1071 isPromotedPairType :: Type -> Maybe (Type, Type)
1072 isPromotedPairType t
1073 | Just (tc, [_,_,x,y]) <- splitTyConApp_maybe t
1074 , tc == promotedTupleDataCon Boxed 2
1075 = Just (x, y)
1076 | otherwise = Nothing
1077
1078 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
1079 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
1080 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
1081
1082 -- | Cached type constructors, data constructors, and superclass selectors for
1083 -- constraint tuples. The outer array is indexed by the arity of the constraint
1084 -- tuple and the inner array is indexed by the superclass position.
1085 cTupleArr :: Array Int (TyCon, DataCon, Array Int Id)
1086 cTupleArr = listArray (0,mAX_CTUPLE_SIZE) [mk_ctuple i | i <- [0..mAX_CTUPLE_SIZE]]
1087 -- Although GHC does not make use of unary constraint tuples
1088 -- (see Note [Ignore unary constraint tuples] in GHC.Tc.Gen.HsType),
1089 -- this array creates one anyway. This is primarily motivated by the fact
1090 -- that (1) the indices of an Array must be contiguous, and (2) we would like
1091 -- the index of a constraint tuple in this Array to correspond to its Arity.
1092 -- We could envision skipping over the unary constraint tuple and having index
1093 -- 1 correspond to a 2-constraint tuple (and so on), but that's more
1094 -- complicated than it's worth.
1095
1096 -- | Given the TupleRep/SumRep tycon and list of RuntimeReps of the unboxed
1097 -- tuple/sum arguments, produces the return kind of an unboxed tuple/sum type
1098 -- constructor. @unboxedTupleSumKind [IntRep, LiftedRep] --> TYPE (TupleRep/SumRep
1099 -- [IntRep, LiftedRep])@
1100 unboxedTupleSumKind :: TyCon -> [Type] -> Kind
1101 unboxedTupleSumKind tc rr_tys
1102 = tYPE (mkTyConApp tc [mkPromotedListTy runtimeRepTy rr_tys])
1103
1104 -- | Specialization of 'unboxedTupleSumKind' for tuples
1105 unboxedTupleKind :: [Type] -> Kind
1106 unboxedTupleKind = unboxedTupleSumKind tupleRepDataConTyCon
1107
1108 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
1109 mk_tuple Boxed arity = (tycon, tuple_con)
1110 where
1111 tycon = mkTupleTyCon tc_name tc_binders tc_res_kind tc_arity tuple_con
1112 BoxedTuple flavour
1113
1114 tc_binders = mkTemplateAnonTyConBinders (replicate arity liftedTypeKind)
1115 tc_res_kind = liftedTypeKind
1116 tc_arity = arity
1117 flavour = VanillaAlgTyCon (mkPrelTyConRepName tc_name)
1118
1119 dc_tvs = binderVars tc_binders
1120 dc_arg_tys = mkTyVarTys dc_tvs
1121 tuple_con = pcDataCon dc_name dc_tvs dc_arg_tys tycon
1122
1123 boxity = Boxed
1124 modu = gHC_TUPLE
1125 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
1126 (ATyCon tycon) BuiltInSyntax
1127 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
1128 (AConLike (RealDataCon tuple_con)) BuiltInSyntax
1129 tc_uniq = mkTupleTyConUnique boxity arity
1130 dc_uniq = mkTupleDataConUnique boxity arity
1131
1132 mk_tuple Unboxed arity = (tycon, tuple_con)
1133 where
1134 tycon = mkTupleTyCon tc_name tc_binders tc_res_kind tc_arity tuple_con
1135 UnboxedTuple flavour
1136
1137 -- See Note [Unboxed tuple RuntimeRep vars] in GHC.Core.TyCon
1138 -- Kind: forall (k1:RuntimeRep) (k2:RuntimeRep). TYPE k1 -> TYPE k2 -> TYPE (TupleRep [k1, k2])
1139 tc_binders = mkTemplateTyConBinders (replicate arity runtimeRepTy)
1140 (\ks -> map tYPE ks)
1141
1142 tc_res_kind = unboxedTupleKind rr_tys
1143
1144 tc_arity = arity * 2
1145 flavour = UnboxedAlgTyCon $ Just (mkPrelTyConRepName tc_name)
1146
1147 dc_tvs = binderVars tc_binders
1148 (rr_tys, dc_arg_tys) = splitAt arity (mkTyVarTys dc_tvs)
1149 tuple_con = pcDataCon dc_name dc_tvs dc_arg_tys tycon
1150
1151 boxity = Unboxed
1152 modu = gHC_PRIM
1153 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
1154 (ATyCon tycon) BuiltInSyntax
1155 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
1156 (AConLike (RealDataCon tuple_con)) BuiltInSyntax
1157 tc_uniq = mkTupleTyConUnique boxity arity
1158 dc_uniq = mkTupleDataConUnique boxity arity
1159
1160 mk_ctuple :: Arity -> (TyCon, DataCon, Array ConTagZ Id)
1161 mk_ctuple arity = (tycon, tuple_con, sc_sel_ids_arr)
1162 where
1163 tycon = mkClassTyCon tc_name binders roles
1164 rhs klass
1165 (mkPrelTyConRepName tc_name)
1166
1167 klass = mk_ctuple_class tycon sc_theta sc_sel_ids
1168 tuple_con = pcDataConW dc_name tvs (map unrestricted sc_theta) tycon
1169
1170 binders = mkTemplateAnonTyConBinders (replicate arity constraintKind)
1171 roles = replicate arity Nominal
1172 rhs = TupleTyCon{data_con = tuple_con, tup_sort = ConstraintTuple}
1173
1174 modu = gHC_CLASSES
1175 tc_name = mkWiredInName modu (mkCTupleOcc tcName arity) tc_uniq
1176 (ATyCon tycon) BuiltInSyntax
1177 dc_name = mkWiredInName modu (mkCTupleOcc dataName arity) dc_uniq
1178 (AConLike (RealDataCon tuple_con)) BuiltInSyntax
1179 tc_uniq = mkCTupleTyConUnique arity
1180 dc_uniq = mkCTupleDataConUnique arity
1181
1182 tvs = binderVars binders
1183 sc_theta = map mkTyVarTy tvs
1184 sc_sel_ids = [mk_sc_sel_id sc_pos | sc_pos <- [0..arity-1]]
1185 sc_sel_ids_arr = listArray (0,arity-1) sc_sel_ids
1186
1187 mk_sc_sel_id sc_pos =
1188 let sc_sel_id_uniq = mkCTupleSelIdUnique sc_pos arity
1189 sc_sel_id_occ = mkCTupleOcc tcName arity
1190 sc_sel_id_name = mkWiredInIdName
1191 gHC_CLASSES
1192 (occNameFS (mkSuperDictSelOcc sc_pos sc_sel_id_occ))
1193 sc_sel_id_uniq
1194 sc_sel_id
1195 sc_sel_id = mkDictSelId sc_sel_id_name klass
1196
1197 in sc_sel_id
1198
1199 unitTyCon :: TyCon
1200 unitTyCon = tupleTyCon Boxed 0
1201
1202 unitTyConKey :: Unique
1203 unitTyConKey = getUnique unitTyCon
1204
1205 unitDataCon :: DataCon
1206 unitDataCon = head (tyConDataCons unitTyCon)
1207
1208 unitDataConId :: Id
1209 unitDataConId = dataConWorkId unitDataCon
1210
1211 pairTyCon :: TyCon
1212 pairTyCon = tupleTyCon Boxed 2
1213
1214 unboxedUnitTy :: Type
1215 unboxedUnitTy = mkTyConApp unboxedUnitTyCon []
1216
1217 unboxedUnitTyCon :: TyCon
1218 unboxedUnitTyCon = tupleTyCon Unboxed 0
1219
1220 unboxedUnitDataCon :: DataCon
1221 unboxedUnitDataCon = tupleDataCon Unboxed 0
1222
1223
1224 {- *********************************************************************
1225 * *
1226 Unboxed sums
1227 * *
1228 ********************************************************************* -}
1229
1230 -- | OccName for n-ary unboxed sum type constructor.
1231 mkSumTyConOcc :: Arity -> OccName
1232 mkSumTyConOcc n = mkOccName tcName str
1233 where
1234 -- No need to cache these, the caching is done in mk_sum
1235 str = '(' : '#' : bars ++ "#)"
1236 bars = replicate (n-1) '|'
1237
1238 -- | OccName for i-th alternative of n-ary unboxed sum data constructor.
1239 mkSumDataConOcc :: ConTag -> Arity -> OccName
1240 mkSumDataConOcc alt n = mkOccName dataName str
1241 where
1242 -- No need to cache these, the caching is done in mk_sum
1243 str = '(' : '#' : bars alt ++ '_' : bars (n - alt - 1) ++ "#)"
1244 bars i = replicate i '|'
1245
1246 -- | Type constructor for n-ary unboxed sum.
1247 sumTyCon :: Arity -> TyCon
1248 sumTyCon arity
1249 | arity > mAX_SUM_SIZE
1250 = fst (mk_sum arity) -- Build one specially
1251
1252 | arity < 2
1253 = panic ("sumTyCon: Arity starts from 2. (arity: " ++ show arity ++ ")")
1254
1255 | otherwise
1256 = fst (unboxedSumArr ! arity)
1257
1258 -- | Data constructor for i-th alternative of a n-ary unboxed sum.
1259 sumDataCon :: ConTag -- Alternative
1260 -> Arity -- Arity
1261 -> DataCon
1262 sumDataCon alt arity
1263 | alt > arity
1264 = panic ("sumDataCon: index out of bounds: alt: "
1265 ++ show alt ++ " > arity " ++ show arity)
1266
1267 | alt <= 0
1268 = panic ("sumDataCon: Alts start from 1. (alt: " ++ show alt
1269 ++ ", arity: " ++ show arity ++ ")")
1270
1271 | arity < 2
1272 = panic ("sumDataCon: Arity starts from 2. (alt: " ++ show alt
1273 ++ ", arity: " ++ show arity ++ ")")
1274
1275 | arity > mAX_SUM_SIZE
1276 = snd (mk_sum arity) ! (alt - 1) -- Build one specially
1277
1278 | otherwise
1279 = snd (unboxedSumArr ! arity) ! (alt - 1)
1280
1281 -- | Cached type and data constructors for sums. The outer array is
1282 -- indexed by the arity of the sum and the inner array is indexed by
1283 -- the alternative.
1284 unboxedSumArr :: Array Int (TyCon, Array Int DataCon)
1285 unboxedSumArr = listArray (2,mAX_SUM_SIZE) [mk_sum i | i <- [2..mAX_SUM_SIZE]]
1286
1287 -- | Specialization of 'unboxedTupleSumKind' for sums
1288 unboxedSumKind :: [Type] -> Kind
1289 unboxedSumKind = unboxedTupleSumKind sumRepDataConTyCon
1290
1291 -- | Create type constructor and data constructors for n-ary unboxed sum.
1292 mk_sum :: Arity -> (TyCon, Array ConTagZ DataCon)
1293 mk_sum arity = (tycon, sum_cons)
1294 where
1295 tycon = mkSumTyCon tc_name tc_binders tc_res_kind (arity * 2) tyvars (elems sum_cons)
1296 (UnboxedAlgTyCon rep_name)
1297
1298 -- Unboxed sums are currently not Typeable due to efficiency concerns. See #13276.
1299 rep_name = Nothing -- Just $ mkPrelTyConRepName tc_name
1300
1301 tc_binders = mkTemplateTyConBinders (replicate arity runtimeRepTy)
1302 (\ks -> map tYPE ks)
1303
1304 tyvars = binderVars tc_binders
1305
1306 tc_res_kind = unboxedSumKind rr_tys
1307
1308 (rr_tys, tyvar_tys) = splitAt arity (mkTyVarTys tyvars)
1309
1310 tc_name = mkWiredInName gHC_PRIM (mkSumTyConOcc arity) tc_uniq
1311 (ATyCon tycon) BuiltInSyntax
1312
1313 sum_cons = listArray (0,arity-1) [sum_con i | i <- [0..arity-1]]
1314 sum_con i = let dc = pcDataCon dc_name
1315 tyvars -- univ tyvars
1316 [tyvar_tys !! i] -- arg types
1317 tycon
1318
1319 dc_name = mkWiredInName gHC_PRIM
1320 (mkSumDataConOcc i arity)
1321 (dc_uniq i)
1322 (AConLike (RealDataCon dc))
1323 BuiltInSyntax
1324 in dc
1325
1326 tc_uniq = mkSumTyConUnique arity
1327 dc_uniq i = mkSumDataConUnique i arity
1328
1329 {-
1330 ************************************************************************
1331 * *
1332 Equality types and classes
1333 * *
1334 ********************************************************************* -}
1335
1336 -- See Note [The equality types story] in GHC.Builtin.Types.Prim
1337 -- ((~~) :: forall k1 k2 (a :: k1) (b :: k2). a -> b -> Constraint)
1338 --
1339 -- It's tempting to put functional dependencies on (~~), but it's not
1340 -- necessary because the functional-dependency coverage check looks
1341 -- through superclasses, and (~#) is handled in that check.
1342
1343 eqTyCon, heqTyCon, coercibleTyCon :: TyCon
1344 eqClass, heqClass, coercibleClass :: Class
1345 eqDataCon, heqDataCon, coercibleDataCon :: DataCon
1346 eqSCSelId, heqSCSelId, coercibleSCSelId :: Id
1347
1348 (eqTyCon, eqClass, eqDataCon, eqSCSelId)
1349 = (tycon, klass, datacon, sc_sel_id)
1350 where
1351 tycon = mkClassTyCon eqTyConName binders roles
1352 rhs klass
1353 (mkPrelTyConRepName eqTyConName)
1354 klass = mk_class tycon sc_pred sc_sel_id
1355 datacon = pcDataConW eqDataConName tvs [unrestricted sc_pred] tycon
1356
1357 -- Kind: forall k. k -> k -> Constraint
1358 binders = mkTemplateTyConBinders [liftedTypeKind] (\[k] -> [k,k])
1359 roles = [Nominal, Nominal, Nominal]
1360 rhs = mkDataTyConRhs [datacon]
1361
1362 tvs@[k,a,b] = binderVars binders
1363 sc_pred = mkTyConApp eqPrimTyCon (mkTyVarTys [k,k,a,b])
1364 sc_sel_id = mkDictSelId eqSCSelIdName klass
1365
1366 (heqTyCon, heqClass, heqDataCon, heqSCSelId)
1367 = (tycon, klass, datacon, sc_sel_id)
1368 where
1369 tycon = mkClassTyCon heqTyConName binders roles
1370 rhs klass
1371 (mkPrelTyConRepName heqTyConName)
1372 klass = mk_class tycon sc_pred sc_sel_id
1373 datacon = pcDataConW heqDataConName tvs [unrestricted sc_pred] tycon
1374
1375 -- Kind: forall k1 k2. k1 -> k2 -> Constraint
1376 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] id
1377 roles = [Nominal, Nominal, Nominal, Nominal]
1378 rhs = mkDataTyConRhs [datacon]
1379
1380 tvs = binderVars binders
1381 sc_pred = mkTyConApp eqPrimTyCon (mkTyVarTys tvs)
1382 sc_sel_id = mkDictSelId heqSCSelIdName klass
1383
1384 (coercibleTyCon, coercibleClass, coercibleDataCon, coercibleSCSelId)
1385 = (tycon, klass, datacon, sc_sel_id)
1386 where
1387 tycon = mkClassTyCon coercibleTyConName binders roles
1388 rhs klass
1389 (mkPrelTyConRepName coercibleTyConName)
1390 klass = mk_class tycon sc_pred sc_sel_id
1391 datacon = pcDataConW coercibleDataConName tvs [unrestricted sc_pred] tycon
1392
1393 -- Kind: forall k. k -> k -> Constraint
1394 binders = mkTemplateTyConBinders [liftedTypeKind] (\[k] -> [k,k])
1395 roles = [Nominal, Representational, Representational]
1396 rhs = mkDataTyConRhs [datacon]
1397
1398 tvs@[k,a,b] = binderVars binders
1399 sc_pred = mkTyConApp eqReprPrimTyCon (mkTyVarTys [k, k, a, b])
1400 sc_sel_id = mkDictSelId coercibleSCSelIdName klass
1401
1402 mk_class :: TyCon -> PredType -> Id -> Class
1403 mk_class tycon sc_pred sc_sel_id
1404 = mkClass (tyConName tycon) (tyConTyVars tycon) [] [sc_pred] [sc_sel_id]
1405 [] [] (mkAnd []) tycon
1406
1407 mk_ctuple_class :: TyCon -> ThetaType -> [Id] -> Class
1408 mk_ctuple_class tycon sc_theta sc_sel_ids
1409 = mkClass (tyConName tycon) (tyConTyVars tycon) [] sc_theta sc_sel_ids
1410 [] [] (mkAnd []) tycon
1411
1412 {- *********************************************************************
1413 * *
1414 Multiplicity Polymorphism
1415 * *
1416 ********************************************************************* -}
1417
1418 {- Multiplicity polymorphism is implemented very similarly to representation
1419 polymorphism. We write in the multiplicity kind and the One and Many
1420 types which can appear in user programs. These are defined properly in GHC.Types.
1421
1422 data Multiplicity = One | Many
1423 -}
1424
1425 multiplicityTy :: Type
1426 multiplicityTy = mkTyConTy multiplicityTyCon
1427
1428 multiplicityTyCon :: TyCon
1429 multiplicityTyCon = pcTyCon multiplicityTyConName Nothing []
1430 [oneDataCon, manyDataCon]
1431
1432 oneDataCon, manyDataCon :: DataCon
1433 oneDataCon = pcDataCon oneDataConName [] [] multiplicityTyCon
1434 manyDataCon = pcDataCon manyDataConName [] [] multiplicityTyCon
1435
1436 oneDataConTy, manyDataConTy :: Type
1437 oneDataConTy = mkTyConTy oneDataConTyCon
1438 manyDataConTy = mkTyConTy manyDataConTyCon
1439
1440 oneDataConTyCon, manyDataConTyCon :: TyCon
1441 oneDataConTyCon = promoteDataCon oneDataCon
1442 manyDataConTyCon = promoteDataCon manyDataCon
1443
1444 multMulTyConName :: Name
1445 multMulTyConName =
1446 mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "MultMul") multMulTyConKey multMulTyCon
1447
1448 multMulTyCon :: TyCon
1449 multMulTyCon = mkFamilyTyCon multMulTyConName binders multiplicityTy Nothing
1450 (BuiltInSynFamTyCon trivialBuiltInFamily)
1451 Nothing
1452 NotInjective
1453 where
1454 binders = mkTemplateAnonTyConBinders [multiplicityTy, multiplicityTy]
1455
1456 unrestrictedFunTy :: Type
1457 unrestrictedFunTy = functionWithMultiplicity manyDataConTy
1458
1459 unrestrictedFunTyCon :: TyCon
1460 unrestrictedFunTyCon = buildSynTyCon unrestrictedFunTyConName [] arrowKind [] unrestrictedFunTy
1461 where arrowKind = mkTyConKind binders liftedTypeKind
1462 -- See also funTyCon
1463 binders = [ Bndr runtimeRep1TyVar (NamedTCB Inferred)
1464 , Bndr runtimeRep2TyVar (NamedTCB Inferred)
1465 ]
1466 ++ mkTemplateAnonTyConBinders [ tYPE runtimeRep1Ty
1467 , tYPE runtimeRep2Ty
1468 ]
1469
1470 unrestrictedFunTyConName :: Name
1471 unrestrictedFunTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "->") unrestrictedFunTyConKey unrestrictedFunTyCon
1472
1473 {- *********************************************************************
1474 * *
1475 Kinds and RuntimeRep
1476 * *
1477 ********************************************************************* -}
1478
1479 -- For information about the usage of the following type,
1480 -- see Note [TYPE and RuntimeRep] in module GHC.Builtin.Types.Prim
1481 runtimeRepTy, levityTy :: Type
1482 runtimeRepTy = mkTyConTy runtimeRepTyCon
1483 levityTy = mkTyConTy levityTyCon
1484
1485 -- Type synonyms; see Note [TYPE and RuntimeRep] in GHC.Builtin.Types.Prim
1486 -- and Note [Prefer Type over TYPE 'LiftedRep] in GHC.Core.TyCo.Rep.
1487 --
1488 -- @type Type = TYPE ('BoxedRep 'Lifted)@
1489 liftedTypeKindTyCon :: TyCon
1490 liftedTypeKindTyCon =
1491 buildSynTyCon liftedTypeKindTyConName [] liftedTypeKind [] rhs
1492 where rhs = TyCoRep.TyConApp tYPETyCon [mkTyConApp liftedRepTyCon []]
1493
1494 -- | @type UnliftedType = TYPE ('BoxedRep 'Unlifted)@
1495 unliftedTypeKindTyCon :: TyCon
1496 unliftedTypeKindTyCon =
1497 buildSynTyCon unliftedTypeKindTyConName [] liftedTypeKind [] rhs
1498 where rhs = TyCoRep.TyConApp tYPETyCon [mkTyConApp unliftedRepTyCon []]
1499
1500 -- | @type LiftedRep = 'BoxedRep 'Lifted@
1501 liftedRepTyCon :: TyCon
1502 liftedRepTyCon = buildSynTyCon
1503 liftedRepTyConName [] runtimeRepTy [] liftedRepTy
1504
1505 -- | @type UnliftedRep = 'BoxedRep 'Unlifted@
1506 unliftedRepTyCon :: TyCon
1507 unliftedRepTyCon = buildSynTyCon
1508 unliftedRepTyConName [] runtimeRepTy [] unliftedRepTy
1509
1510 runtimeRepTyCon :: TyCon
1511 runtimeRepTyCon = pcTyCon runtimeRepTyConName Nothing []
1512 (vecRepDataCon : tupleRepDataCon :
1513 sumRepDataCon : boxedRepDataCon : runtimeRepSimpleDataCons)
1514
1515 levityTyCon :: TyCon
1516 levityTyCon = pcTyCon levityTyConName Nothing [] [liftedDataCon,unliftedDataCon]
1517
1518 liftedDataCon, unliftedDataCon :: DataCon
1519 liftedDataCon = pcSpecialDataCon liftedDataConName
1520 [] levityTyCon LiftedInfo
1521 unliftedDataCon = pcSpecialDataCon unliftedDataConName
1522 [] levityTyCon UnliftedInfo
1523
1524 boxedRepDataCon :: DataCon
1525 boxedRepDataCon = pcSpecialDataCon boxedRepDataConName
1526 [ mkTyConTy levityTyCon ] runtimeRepTyCon (RuntimeRep prim_rep_fun)
1527 where
1528 -- See Note [Getting from RuntimeRep to PrimRep] in RepType
1529 prim_rep_fun [lev]
1530 = case tyConRuntimeRepInfo (tyConAppTyCon lev) of
1531 LiftedInfo -> [LiftedRep]
1532 UnliftedInfo -> [UnliftedRep]
1533 _ -> pprPanic "boxedRepDataCon" (ppr lev)
1534 prim_rep_fun args
1535 = pprPanic "boxedRepDataCon" (ppr args)
1536
1537 vecRepDataCon :: DataCon
1538 vecRepDataCon = pcSpecialDataCon vecRepDataConName [ mkTyConTy vecCountTyCon
1539 , mkTyConTy vecElemTyCon ]
1540 runtimeRepTyCon
1541 (RuntimeRep prim_rep_fun)
1542 where
1543 -- See Note [Getting from RuntimeRep to PrimRep] in GHC.Types.RepType
1544 prim_rep_fun [count, elem]
1545 | VecCount n <- tyConRuntimeRepInfo (tyConAppTyCon count)
1546 , VecElem e <- tyConRuntimeRepInfo (tyConAppTyCon elem)
1547 = [VecRep n e]
1548 prim_rep_fun args
1549 = pprPanic "vecRepDataCon" (ppr args)
1550
1551 vecRepDataConTyCon :: TyCon
1552 vecRepDataConTyCon = promoteDataCon vecRepDataCon
1553
1554 tupleRepDataCon :: DataCon
1555 tupleRepDataCon = pcSpecialDataCon tupleRepDataConName [ mkListTy runtimeRepTy ]
1556 runtimeRepTyCon (RuntimeRep prim_rep_fun)
1557 where
1558 -- See Note [Getting from RuntimeRep to PrimRep] in GHC.Types.RepType
1559 prim_rep_fun [rr_ty_list]
1560 = concatMap (runtimeRepPrimRep doc) rr_tys
1561 where
1562 rr_tys = extractPromotedList rr_ty_list
1563 doc = text "tupleRepDataCon" <+> ppr rr_tys
1564 prim_rep_fun args
1565 = pprPanic "tupleRepDataCon" (ppr args)
1566
1567 tupleRepDataConTyCon :: TyCon
1568 tupleRepDataConTyCon = promoteDataCon tupleRepDataCon
1569
1570 sumRepDataCon :: DataCon
1571 sumRepDataCon = pcSpecialDataCon sumRepDataConName [ mkListTy runtimeRepTy ]
1572 runtimeRepTyCon (RuntimeRep prim_rep_fun)
1573 where
1574 -- See Note [Getting from RuntimeRep to PrimRep] in GHC.Types.RepType
1575 prim_rep_fun [rr_ty_list]
1576 = map slotPrimRep (ubxSumRepType prim_repss)
1577 where
1578 rr_tys = extractPromotedList rr_ty_list
1579 doc = text "sumRepDataCon" <+> ppr rr_tys
1580 prim_repss = map (runtimeRepPrimRep doc) rr_tys
1581 prim_rep_fun args
1582 = pprPanic "sumRepDataCon" (ppr args)
1583
1584 sumRepDataConTyCon :: TyCon
1585 sumRepDataConTyCon = promoteDataCon sumRepDataCon
1586
1587 -- See Note [Wiring in RuntimeRep]
1588 -- See Note [Getting from RuntimeRep to PrimRep] in GHC.Types.RepType
1589 runtimeRepSimpleDataCons :: [DataCon]
1590 runtimeRepSimpleDataCons
1591 = zipWithLazy mk_runtime_rep_dc
1592 [ IntRep
1593 , Int8Rep, Int16Rep, Int32Rep, Int64Rep
1594 , WordRep
1595 , Word8Rep, Word16Rep, Word32Rep, Word64Rep
1596 , AddrRep
1597 , FloatRep, DoubleRep
1598 ]
1599 runtimeRepSimpleDataConNames
1600 where
1601 mk_runtime_rep_dc primrep name
1602 = pcSpecialDataCon name [] runtimeRepTyCon (RuntimeRep (\_ -> [primrep]))
1603
1604 -- See Note [Wiring in RuntimeRep]
1605 intRepDataConTy,
1606 int8RepDataConTy, int16RepDataConTy, int32RepDataConTy, int64RepDataConTy,
1607 wordRepDataConTy,
1608 word8RepDataConTy, word16RepDataConTy, word32RepDataConTy, word64RepDataConTy,
1609 addrRepDataConTy,
1610 floatRepDataConTy, doubleRepDataConTy :: Type
1611 [intRepDataConTy,
1612 int8RepDataConTy, int16RepDataConTy, int32RepDataConTy, int64RepDataConTy,
1613 wordRepDataConTy,
1614 word8RepDataConTy, word16RepDataConTy, word32RepDataConTy, word64RepDataConTy,
1615 addrRepDataConTy,
1616 floatRepDataConTy, doubleRepDataConTy
1617 ]
1618 = map (mkTyConTy . promoteDataCon) runtimeRepSimpleDataCons
1619
1620 vecCountTyCon :: TyCon
1621 vecCountTyCon = pcTyCon vecCountTyConName Nothing [] vecCountDataCons
1622
1623 -- See Note [Wiring in RuntimeRep]
1624 vecCountDataCons :: [DataCon]
1625 vecCountDataCons = zipWithLazy mk_vec_count_dc
1626 [ 2, 4, 8, 16, 32, 64 ]
1627 vecCountDataConNames
1628 where
1629 mk_vec_count_dc n name
1630 = pcSpecialDataCon name [] vecCountTyCon (VecCount n)
1631
1632 -- See Note [Wiring in RuntimeRep]
1633 vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy,
1634 vec64DataConTy :: Type
1635 [vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy,
1636 vec64DataConTy] = map (mkTyConTy . promoteDataCon) vecCountDataCons
1637
1638 vecElemTyCon :: TyCon
1639 vecElemTyCon = pcTyCon vecElemTyConName Nothing [] vecElemDataCons
1640
1641 -- See Note [Wiring in RuntimeRep]
1642 vecElemDataCons :: [DataCon]
1643 vecElemDataCons = zipWithLazy mk_vec_elem_dc
1644 [ Int8ElemRep, Int16ElemRep, Int32ElemRep, Int64ElemRep
1645 , Word8ElemRep, Word16ElemRep, Word32ElemRep, Word64ElemRep
1646 , FloatElemRep, DoubleElemRep ]
1647 vecElemDataConNames
1648 where
1649 mk_vec_elem_dc elem name
1650 = pcSpecialDataCon name [] vecElemTyCon (VecElem elem)
1651
1652 -- See Note [Wiring in RuntimeRep]
1653 int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy,
1654 int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy,
1655 word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy,
1656 doubleElemRepDataConTy :: Type
1657 [int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy,
1658 int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy,
1659 word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy,
1660 doubleElemRepDataConTy] = map (mkTyConTy . promoteDataCon)
1661 vecElemDataCons
1662
1663
1664 liftedDataConTyCon :: TyCon
1665 liftedDataConTyCon = promoteDataCon liftedDataCon
1666
1667 unliftedDataConTyCon :: TyCon
1668 unliftedDataConTyCon = promoteDataCon unliftedDataCon
1669
1670 liftedDataConTy :: Type
1671 liftedDataConTy = mkTyConTy liftedDataConTyCon
1672
1673 unliftedDataConTy :: Type
1674 unliftedDataConTy = mkTyConTy unliftedDataConTyCon
1675
1676 boxedRepDataConTyCon :: TyCon
1677 boxedRepDataConTyCon = promoteDataCon boxedRepDataCon
1678
1679 -- The type ('BoxedRep 'Lifted)
1680 liftedRepTy :: Type
1681 liftedRepTy = mkTyConApp boxedRepDataConTyCon [liftedDataConTy]
1682
1683 -- The type ('BoxedRep 'Unlifted)
1684 unliftedRepTy :: Type
1685 unliftedRepTy = mkTyConApp boxedRepDataConTyCon [unliftedDataConTy]
1686
1687 {- *********************************************************************
1688 * *
1689 The boxed primitive types: Char, Int, etc
1690 * *
1691 ********************************************************************* -}
1692
1693 boxingDataCon_maybe :: TyCon -> Maybe DataCon
1694 -- boxingDataCon_maybe Char# = C#
1695 -- boxingDataCon_maybe Int# = I#
1696 -- ... etc ...
1697 -- See Note [Boxing primitive types]
1698 boxingDataCon_maybe tc
1699 = lookupNameEnv boxing_constr_env (tyConName tc)
1700
1701 boxing_constr_env :: NameEnv DataCon
1702 boxing_constr_env
1703 = mkNameEnv [(charPrimTyConName , charDataCon )
1704 ,(intPrimTyConName , intDataCon )
1705 ,(wordPrimTyConName , wordDataCon )
1706 ,(floatPrimTyConName , floatDataCon )
1707 ,(doublePrimTyConName, doubleDataCon) ]
1708
1709 {- Note [Boxing primitive types]
1710 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1711 For a handful of primitive types (Int, Char, Word, Float, Double),
1712 we can readily box and an unboxed version (Int#, Char# etc) using
1713 the corresponding data constructor. This is useful in a couple
1714 of places, notably let-floating -}
1715
1716
1717 charTy :: Type
1718 charTy = mkTyConTy charTyCon
1719
1720 charTyCon :: TyCon
1721 charTyCon = pcTyCon charTyConName
1722 (Just (CType NoSourceText Nothing
1723 (NoSourceText,fsLit "HsChar")))
1724 [] [charDataCon]
1725 charDataCon :: DataCon
1726 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
1727
1728 stringTy :: Type
1729 stringTy = mkTyConApp stringTyCon []
1730
1731 stringTyCon :: TyCon
1732 -- We have this wired-in so that Haskell literal strings
1733 -- get type String (in hsLitType), which in turn influences
1734 -- inferred types and error messages
1735 stringTyCon = buildSynTyCon stringTyConName
1736 [] liftedTypeKind []
1737 (mkListTy charTy)
1738
1739 intTy :: Type
1740 intTy = mkTyConTy intTyCon
1741
1742 intTyCon :: TyCon
1743 intTyCon = pcTyCon intTyConName
1744 (Just (CType NoSourceText Nothing (NoSourceText,fsLit "HsInt")))
1745 [] [intDataCon]
1746 intDataCon :: DataCon
1747 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
1748
1749 wordTy :: Type
1750 wordTy = mkTyConTy wordTyCon
1751
1752 wordTyCon :: TyCon
1753 wordTyCon = pcTyCon wordTyConName
1754 (Just (CType NoSourceText Nothing (NoSourceText, fsLit "HsWord")))
1755 [] [wordDataCon]
1756 wordDataCon :: DataCon
1757 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
1758
1759 word8Ty :: Type
1760 word8Ty = mkTyConTy word8TyCon
1761
1762 word8TyCon :: TyCon
1763 word8TyCon = pcTyCon word8TyConName
1764 (Just (CType NoSourceText Nothing
1765 (NoSourceText, fsLit "HsWord8"))) []
1766 [word8DataCon]
1767 word8DataCon :: DataCon
1768 word8DataCon = pcDataCon word8DataConName [] [word8PrimTy] word8TyCon
1769
1770 floatTy :: Type
1771 floatTy = mkTyConTy floatTyCon
1772
1773 floatTyCon :: TyCon
1774 floatTyCon = pcTyCon floatTyConName
1775 (Just (CType NoSourceText Nothing
1776 (NoSourceText, fsLit "HsFloat"))) []
1777 [floatDataCon]
1778 floatDataCon :: DataCon
1779 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
1780
1781 doubleTy :: Type
1782 doubleTy = mkTyConTy doubleTyCon
1783
1784 doubleTyCon :: TyCon
1785 doubleTyCon = pcTyCon doubleTyConName
1786 (Just (CType NoSourceText Nothing
1787 (NoSourceText,fsLit "HsDouble"))) []
1788 [doubleDataCon]
1789
1790 doubleDataCon :: DataCon
1791 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
1792
1793 {-
1794 ************************************************************************
1795 * *
1796 The Bool type
1797 * *
1798 ************************************************************************
1799
1800 An ordinary enumeration type, but deeply wired in. There are no
1801 magical operations on @Bool@ (just the regular Prelude code).
1802
1803 {\em BEGIN IDLE SPECULATION BY SIMON}
1804
1805 This is not the only way to encode @Bool@. A more obvious coding makes
1806 @Bool@ just a boxed up version of @Bool#@, like this:
1807 \begin{verbatim}
1808 type Bool# = Int#
1809 data Bool = MkBool Bool#
1810 \end{verbatim}
1811
1812 Unfortunately, this doesn't correspond to what the Report says @Bool@
1813 looks like! Furthermore, we get slightly less efficient code (I
1814 think) with this coding. @gtInt@ would look like this:
1815
1816 \begin{verbatim}
1817 gtInt :: Int -> Int -> Bool
1818 gtInt x y = case x of I# x# ->
1819 case y of I# y# ->
1820 case (gtIntPrim x# y#) of
1821 b# -> MkBool b#
1822 \end{verbatim}
1823
1824 Notice that the result of the @gtIntPrim@ comparison has to be turned
1825 into an integer (here called @b#@), and returned in a @MkBool@ box.
1826
1827 The @if@ expression would compile to this:
1828 \begin{verbatim}
1829 case (gtInt x y) of
1830 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
1831 \end{verbatim}
1832
1833 I think this code is a little less efficient than the previous code,
1834 but I'm not certain. At all events, corresponding with the Report is
1835 important. The interesting thing is that the language is expressive
1836 enough to describe more than one alternative; and that a type doesn't
1837 necessarily need to be a straightforwardly boxed version of its
1838 primitive counterpart.
1839
1840 {\em END IDLE SPECULATION BY SIMON}
1841 -}
1842
1843 boolTy :: Type
1844 boolTy = mkTyConTy boolTyCon
1845
1846 boolTyCon :: TyCon
1847 boolTyCon = pcTyCon boolTyConName
1848 (Just (CType NoSourceText Nothing
1849 (NoSourceText, fsLit "HsBool")))
1850 [] [falseDataCon, trueDataCon]
1851
1852 falseDataCon, trueDataCon :: DataCon
1853 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
1854 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
1855
1856 falseDataConId, trueDataConId :: Id
1857 falseDataConId = dataConWorkId falseDataCon
1858 trueDataConId = dataConWorkId trueDataCon
1859
1860 orderingTyCon :: TyCon
1861 orderingTyCon = pcTyCon orderingTyConName Nothing
1862 [] [ordLTDataCon, ordEQDataCon, ordGTDataCon]
1863
1864 ordLTDataCon, ordEQDataCon, ordGTDataCon :: DataCon
1865 ordLTDataCon = pcDataCon ordLTDataConName [] [] orderingTyCon
1866 ordEQDataCon = pcDataCon ordEQDataConName [] [] orderingTyCon
1867 ordGTDataCon = pcDataCon ordGTDataConName [] [] orderingTyCon
1868
1869 ordLTDataConId, ordEQDataConId, ordGTDataConId :: Id
1870 ordLTDataConId = dataConWorkId ordLTDataCon
1871 ordEQDataConId = dataConWorkId ordEQDataCon
1872 ordGTDataConId = dataConWorkId ordGTDataCon
1873
1874 {-
1875 ************************************************************************
1876 * *
1877 The List type
1878 Special syntax, deeply wired in,
1879 but otherwise an ordinary algebraic data type
1880 * *
1881 ************************************************************************
1882
1883 data [] a = [] | a : (List a)
1884 -}
1885
1886 mkListTy :: Type -> Type
1887 mkListTy ty = mkTyConApp listTyCon [ty]
1888
1889 listTyCon :: TyCon
1890 listTyCon = pcTyCon listTyConName Nothing [alphaTyVar] [nilDataCon, consDataCon]
1891
1892 -- See also Note [Empty lists] in GHC.Hs.Expr.
1893 nilDataCon :: DataCon
1894 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
1895
1896 consDataCon :: DataCon
1897 consDataCon = pcDataConWithFixity True {- Declared infix -}
1898 consDataConName
1899 alpha_tyvar [] alpha_tyvar
1900 (map linear [alphaTy, mkTyConApp listTyCon alpha_ty]) listTyCon
1901 -- Interesting: polymorphic recursion would help here.
1902 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
1903 -- gets the over-specific type (Type -> Type)
1904
1905 -- Wired-in type Maybe
1906
1907 maybeTyCon :: TyCon
1908 maybeTyCon = pcTyCon maybeTyConName Nothing alpha_tyvar
1909 [nothingDataCon, justDataCon]
1910
1911 nothingDataCon :: DataCon
1912 nothingDataCon = pcDataCon nothingDataConName alpha_tyvar [] maybeTyCon
1913
1914 justDataCon :: DataCon
1915 justDataCon = pcDataCon justDataConName alpha_tyvar [alphaTy] maybeTyCon
1916
1917 mkPromotedMaybeTy :: Kind -> Maybe Type -> Type
1918 mkPromotedMaybeTy k (Just x) = mkTyConApp promotedJustDataCon [k,x]
1919 mkPromotedMaybeTy k Nothing = mkTyConApp promotedNothingDataCon [k]
1920
1921 mkMaybeTy :: Type -> Kind
1922 mkMaybeTy t = mkTyConApp maybeTyCon [t]
1923
1924 isPromotedMaybeTy :: Type -> Maybe (Maybe Type)
1925 isPromotedMaybeTy t
1926 | Just (tc,[_,x]) <- splitTyConApp_maybe t, tc == promotedJustDataCon = return $ Just x
1927 | Just (tc,[_]) <- splitTyConApp_maybe t, tc == promotedNothingDataCon = return $ Nothing
1928 | otherwise = Nothing
1929
1930
1931 {-
1932 ** *********************************************************************
1933 * *
1934 The tuple types
1935 * *
1936 ************************************************************************
1937
1938 The tuple types are definitely magic, because they form an infinite
1939 family.
1940
1941 \begin{itemize}
1942 \item
1943 They have a special family of type constructors, of type @TyCon@
1944 These contain the tycon arity, but don't require a Unique.
1945
1946 \item
1947 They have a special family of constructors, of type
1948 @Id@. Again these contain their arity but don't need a Unique.
1949
1950 \item
1951 There should be a magic way of generating the info tables and
1952 entry code for all tuples.
1953
1954 But at the moment we just compile a Haskell source
1955 file\srcloc{lib/prelude/...} containing declarations like:
1956 \begin{verbatim}
1957 data Tuple0 = Tup0
1958 data Tuple2 a b = Tup2 a b
1959 data Tuple3 a b c = Tup3 a b c
1960 data Tuple4 a b c d = Tup4 a b c d
1961 ...
1962 \end{verbatim}
1963 The print-names associated with the magic @Id@s for tuple constructors
1964 ``just happen'' to be the same as those generated by these
1965 declarations.
1966
1967 \item
1968 The instance environment should have a magic way to know
1969 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
1970 so on. \ToDo{Not implemented yet.}
1971
1972 \item
1973 There should also be a way to generate the appropriate code for each
1974 of these instances, but (like the info tables and entry code) it is
1975 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
1976 \end{itemize}
1977 -}
1978
1979 -- | Make a tuple type. The list of types should /not/ include any
1980 -- RuntimeRep specifications. Boxed 1-tuples are flattened.
1981 -- See Note [One-tuples]
1982 mkTupleTy :: Boxity -> [Type] -> Type
1983 -- Special case for *boxed* 1-tuples, which are represented by the type itself
1984 mkTupleTy Boxed [ty] = ty
1985 mkTupleTy boxity tys = mkTupleTy1 boxity tys
1986
1987 -- | Make a tuple type. The list of types should /not/ include any
1988 -- RuntimeRep specifications. Boxed 1-tuples are *not* flattened.
1989 -- See Note [One-tuples] and Note [Don't flatten tuples from HsSyn]
1990 -- in "GHC.Core.Make"
1991 mkTupleTy1 :: Boxity -> [Type] -> Type
1992 mkTupleTy1 Boxed tys = mkTyConApp (tupleTyCon Boxed (length tys)) tys
1993 mkTupleTy1 Unboxed tys = mkTyConApp (tupleTyCon Unboxed (length tys))
1994 (map getRuntimeRep tys ++ tys)
1995
1996 -- | Build the type of a small tuple that holds the specified type of thing
1997 -- Flattens 1-tuples. See Note [One-tuples].
1998 mkBoxedTupleTy :: [Type] -> Type
1999 mkBoxedTupleTy tys = mkTupleTy Boxed tys
2000
2001 unitTy :: Type
2002 unitTy = mkTupleTy Boxed []
2003
2004 {- *********************************************************************
2005 * *
2006 The sum types
2007 * *
2008 ************************************************************************
2009 -}
2010
2011 mkSumTy :: [Type] -> Type
2012 mkSumTy tys = mkTyConApp (sumTyCon (length tys))
2013 (map getRuntimeRep tys ++ tys)
2014
2015 -- Promoted Booleans
2016
2017 promotedFalseDataCon, promotedTrueDataCon :: TyCon
2018 promotedTrueDataCon = promoteDataCon trueDataCon
2019 promotedFalseDataCon = promoteDataCon falseDataCon
2020
2021 -- Promoted Maybe
2022 promotedNothingDataCon, promotedJustDataCon :: TyCon
2023 promotedNothingDataCon = promoteDataCon nothingDataCon
2024 promotedJustDataCon = promoteDataCon justDataCon
2025
2026 -- Promoted Ordering
2027
2028 promotedLTDataCon
2029 , promotedEQDataCon
2030 , promotedGTDataCon
2031 :: TyCon
2032 promotedLTDataCon = promoteDataCon ordLTDataCon
2033 promotedEQDataCon = promoteDataCon ordEQDataCon
2034 promotedGTDataCon = promoteDataCon ordGTDataCon
2035
2036 -- Promoted List
2037 promotedConsDataCon, promotedNilDataCon :: TyCon
2038 promotedConsDataCon = promoteDataCon consDataCon
2039 promotedNilDataCon = promoteDataCon nilDataCon
2040
2041 -- | Make a *promoted* list.
2042 mkPromotedListTy :: Kind -- ^ of the elements of the list
2043 -> [Type] -- ^ elements
2044 -> Type
2045 mkPromotedListTy k tys
2046 = foldr cons nil tys
2047 where
2048 cons :: Type -- element
2049 -> Type -- list
2050 -> Type
2051 cons elt list = mkTyConApp promotedConsDataCon [k, elt, list]
2052
2053 nil :: Type
2054 nil = mkTyConApp promotedNilDataCon [k]
2055
2056 -- | Extract the elements of a promoted list. Panics if the type is not a
2057 -- promoted list
2058 extractPromotedList :: Type -- ^ The promoted list
2059 -> [Type]
2060 extractPromotedList tys = go tys
2061 where
2062 go list_ty
2063 | Just (tc, [_k, t, ts]) <- splitTyConApp_maybe list_ty
2064 = assert (tc `hasKey` consDataConKey) $
2065 t : go ts
2066
2067 | Just (tc, [_k]) <- splitTyConApp_maybe list_ty
2068 = assert (tc `hasKey` nilDataConKey)
2069 []
2070
2071 | otherwise
2072 = pprPanic "extractPromotedList" (ppr tys)
2073
2074 ---------------------------------------
2075 -- ghc-bignum
2076 ---------------------------------------
2077
2078 integerTyConName
2079 , integerISDataConName
2080 , integerIPDataConName
2081 , integerINDataConName
2082 :: Name
2083 integerTyConName
2084 = mkWiredInTyConName
2085 UserSyntax
2086 gHC_NUM_INTEGER
2087 (fsLit "Integer")
2088 integerTyConKey
2089 integerTyCon
2090 integerISDataConName
2091 = mkWiredInDataConName
2092 UserSyntax
2093 gHC_NUM_INTEGER
2094 (fsLit "IS")
2095 integerISDataConKey
2096 integerISDataCon
2097 integerIPDataConName
2098 = mkWiredInDataConName
2099 UserSyntax
2100 gHC_NUM_INTEGER
2101 (fsLit "IP")
2102 integerIPDataConKey
2103 integerIPDataCon
2104 integerINDataConName
2105 = mkWiredInDataConName
2106 UserSyntax
2107 gHC_NUM_INTEGER
2108 (fsLit "IN")
2109 integerINDataConKey
2110 integerINDataCon
2111
2112 integerTy :: Type
2113 integerTy = mkTyConTy integerTyCon
2114
2115 integerTyCon :: TyCon
2116 integerTyCon = pcTyCon integerTyConName Nothing []
2117 [integerISDataCon, integerIPDataCon, integerINDataCon]
2118
2119 integerISDataCon :: DataCon
2120 integerISDataCon = pcDataCon integerISDataConName [] [intPrimTy] integerTyCon
2121
2122 integerIPDataCon :: DataCon
2123 integerIPDataCon = pcDataCon integerIPDataConName [] [byteArrayPrimTy] integerTyCon
2124
2125 integerINDataCon :: DataCon
2126 integerINDataCon = pcDataCon integerINDataConName [] [byteArrayPrimTy] integerTyCon
2127
2128 naturalTyConName
2129 , naturalNSDataConName
2130 , naturalNBDataConName
2131 :: Name
2132 naturalTyConName
2133 = mkWiredInTyConName
2134 UserSyntax
2135 gHC_NUM_NATURAL
2136 (fsLit "Natural")
2137 naturalTyConKey
2138 naturalTyCon
2139 naturalNSDataConName
2140 = mkWiredInDataConName
2141 UserSyntax
2142 gHC_NUM_NATURAL
2143 (fsLit "NS")
2144 naturalNSDataConKey
2145 naturalNSDataCon
2146 naturalNBDataConName
2147 = mkWiredInDataConName
2148 UserSyntax
2149 gHC_NUM_NATURAL
2150 (fsLit "NB")
2151 naturalNBDataConKey
2152 naturalNBDataCon
2153
2154 naturalTy :: Type
2155 naturalTy = mkTyConTy naturalTyCon
2156
2157 naturalTyCon :: TyCon
2158 naturalTyCon = pcTyCon naturalTyConName Nothing []
2159 [naturalNSDataCon, naturalNBDataCon]
2160
2161 naturalNSDataCon :: DataCon
2162 naturalNSDataCon = pcDataCon naturalNSDataConName [] [wordPrimTy] naturalTyCon
2163
2164 naturalNBDataCon :: DataCon
2165 naturalNBDataCon = pcDataCon naturalNBDataConName [] [byteArrayPrimTy] naturalTyCon
2166
2167
2168 -- | Replaces constraint tuple names with corresponding boxed ones.
2169 filterCTuple :: RdrName -> RdrName
2170 filterCTuple (Exact n)
2171 | Just arity <- cTupleTyConNameArity_maybe n
2172 = Exact $ tupleTyConName BoxedTuple arity
2173 filterCTuple rdr = rdr