Fix and document Unique generation for sum TyCon and DataCons
[ghc.git] / compiler / prelude / TysWiredIn.hs
1 {-
2 (c) The GRASP Project, Glasgow University, 1994-1998
3
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
5 -}
6
7 {-# LANGUAGE CPP #-}
8 {-# LANGUAGE OverloadedStrings #-}
9
10 -- | This module is about types that can be defined in Haskell, but which
11 -- must be wired into the compiler nonetheless. C.f module TysPrim
12 module TysWiredIn (
13 -- * Helper functions defined here
14 mkWiredInTyConName, -- This is used in TcTypeNats to define the
15 -- built-in functions for evaluation.
16
17 mkWiredInIdName, -- used in MkId
18
19 mkFunKind, mkForAllKind,
20
21 -- * All wired in things
22 wiredInTyCons, isBuiltInOcc_maybe,
23
24 -- * Bool
25 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
26 trueDataCon, trueDataConId, true_RDR,
27 falseDataCon, falseDataConId, false_RDR,
28 promotedFalseDataCon, promotedTrueDataCon,
29
30 -- * Ordering
31 orderingTyCon,
32 ltDataCon, ltDataConId,
33 eqDataCon, eqDataConId,
34 gtDataCon, gtDataConId,
35 promotedLTDataCon, promotedEQDataCon, promotedGTDataCon,
36
37 -- * Char
38 charTyCon, charDataCon, charTyCon_RDR,
39 charTy, stringTy, charTyConName,
40
41 -- * Double
42 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
43
44 -- * Float
45 floatTyCon, floatDataCon, floatTy, floatTyConName,
46
47 -- * Int
48 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
49 intTy,
50
51 -- * Word
52 wordTyCon, wordDataCon, wordTyConName, wordTy,
53
54 -- * Word8
55 word8TyCon, word8DataCon, word8TyConName, word8Ty,
56
57 -- * List
58 listTyCon, listTyCon_RDR, listTyConName, listTyConKey,
59 nilDataCon, nilDataConName, nilDataConKey,
60 consDataCon_RDR, consDataCon, consDataConName,
61 promotedNilDataCon, promotedConsDataCon,
62 mkListTy,
63
64 -- * Maybe
65 maybeTyCon, maybeTyConName,
66 nothingDataCon, nothingDataConName, promotedNothingDataCon,
67 justDataCon, justDataConName, promotedJustDataCon,
68
69 -- * Tuples
70 mkTupleTy, mkBoxedTupleTy,
71 tupleTyCon, tupleDataCon, tupleTyConName,
72 promotedTupleDataCon,
73 unitTyCon, unitDataCon, unitDataConId, unitTy, unitTyConKey,
74 pairTyCon,
75 unboxedUnitTyCon, unboxedUnitDataCon,
76 cTupleTyConName, cTupleTyConNames, isCTupleTyConName,
77
78 -- * Any
79 anyTyCon, anyTy, anyTypeOfKind,
80
81 -- * Sums
82 mkSumTy, sumTyCon, sumDataCon,
83
84 -- * Kinds
85 typeNatKindCon, typeNatKind, typeSymbolKindCon, typeSymbolKind,
86 isLiftedTypeKindTyConName, liftedTypeKind, constraintKind,
87 starKindTyCon, starKindTyConName, unboxedTupleKind,
88 unicodeStarKindTyCon, unicodeStarKindTyConName,
89 liftedTypeKindTyCon, constraintKindTyCon,
90
91 -- * Parallel arrays
92 mkPArrTy,
93 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
94 parrTyCon_RDR, parrTyConName,
95
96 -- * Equality predicates
97 heqTyCon, heqClass, heqDataCon,
98 coercibleTyCon, coercibleDataCon, coercibleClass,
99
100 -- * RuntimeRep and friends
101 runtimeRepTyCon, vecCountTyCon, vecElemTyCon,
102
103 runtimeRepTy, ptrRepLiftedTy,
104
105 vecRepDataConTyCon, ptrRepUnliftedDataConTyCon,
106
107 voidRepDataConTy, intRepDataConTy,
108 wordRepDataConTy, int64RepDataConTy, word64RepDataConTy, addrRepDataConTy,
109 floatRepDataConTy, doubleRepDataConTy, unboxedTupleRepDataConTy,
110 unboxedSumRepDataConTy,
111
112 vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy,
113 vec64DataConTy,
114
115 int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy,
116 int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy,
117 word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy,
118 doubleElemRepDataConTy
119
120 ) where
121
122 #include "HsVersions.h"
123 #include "MachDeps.h"
124
125 import {-# SOURCE #-} MkId( mkDataConWorkId, mkDictSelId )
126
127 -- friends:
128 import PrelNames
129 import TysPrim
130
131 -- others:
132 import CoAxiom
133 import Id
134 import Constants ( mAX_TUPLE_SIZE, mAX_CTUPLE_SIZE, mAX_SUM_SIZE )
135 import Module ( Module )
136 import Type
137 import DataCon
138 import {-# SOURCE #-} ConLike
139 import TyCon
140 import Class ( Class, mkClass )
141 import RdrName
142 import Name
143 import NameSet ( NameSet, mkNameSet, elemNameSet )
144 import BasicTypes ( Arity, Boxity(..), TupleSort(..), ConTagZ )
145 import ForeignCall
146 import SrcLoc ( noSrcSpan )
147 import Unique
148 import Data.Array
149 import FastString
150 import Outputable
151 import Util
152 import BooleanFormula ( mkAnd )
153
154 import qualified Data.ByteString.Char8 as BS
155 #if !MIN_VERSION_bytestring(0,10,8)
156 import qualified Data.ByteString.Internal as BSI
157 import qualified Data.ByteString.Unsafe as BSU
158 #endif
159
160 alpha_tyvar :: [TyVar]
161 alpha_tyvar = [alphaTyVar]
162
163 alpha_ty :: [Type]
164 alpha_ty = [alphaTy]
165
166 {-
167 Note [Wiring in RuntimeRep]
168 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
169 The RuntimeRep type (and friends) in GHC.Types has a bunch of constructors,
170 making it a pain to wire in. To ease the pain somewhat, we use lists of
171 the different bits, like Uniques, Names, DataCons. These lists must be
172 kept in sync with each other. The rule is this: use the order as declared
173 in GHC.Types. All places where such lists exist should contain a reference
174 to this Note, so a search for this Note's name should find all the lists.
175
176 ************************************************************************
177 * *
178 \subsection{Wired in type constructors}
179 * *
180 ************************************************************************
181
182 If you change which things are wired in, make sure you change their
183 names in PrelNames, so they use wTcQual, wDataQual, etc
184 -}
185
186 -- This list is used only to define PrelInfo.wiredInThings. That in turn
187 -- is used to initialise the name environment carried around by the renamer.
188 -- This means that if we look up the name of a TyCon (or its implicit binders)
189 -- that occurs in this list that name will be assigned the wired-in key we
190 -- define here.
191 --
192 -- Because of their infinite nature, this list excludes tuples, Any and implicit
193 -- parameter TyCons (see Note [Built-in syntax and the OrigNameCache]).
194 --
195 -- See also Note [Known-key names]
196 wiredInTyCons :: [TyCon]
197
198 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
199 -- it's defined in GHC.Base, and there's only
200 -- one of it. We put it in wiredInTyCons so
201 -- that it'll pre-populate the name cache, so
202 -- the special case in lookupOrigNameCache
203 -- doesn't need to look out for it
204 , anyTyCon
205 , boolTyCon
206 , charTyCon
207 , doubleTyCon
208 , floatTyCon
209 , intTyCon
210 , wordTyCon
211 , word8TyCon
212 , listTyCon
213 , maybeTyCon
214 , parrTyCon
215 , heqTyCon
216 , coercibleTyCon
217 , typeNatKindCon
218 , typeSymbolKindCon
219 , runtimeRepTyCon
220 , vecCountTyCon
221 , vecElemTyCon
222 , constraintKindTyCon
223 , liftedTypeKindTyCon
224 , starKindTyCon
225 , unicodeStarKindTyCon
226 ]
227
228 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
229 mkWiredInTyConName built_in modu fs unique tycon
230 = mkWiredInName modu (mkTcOccFS fs) unique
231 (ATyCon tycon) -- Relevant TyCon
232 built_in
233
234 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
235 mkWiredInDataConName built_in modu fs unique datacon
236 = mkWiredInName modu (mkDataOccFS fs) unique
237 (AConLike (RealDataCon datacon)) -- Relevant DataCon
238 built_in
239
240 mkWiredInIdName :: Module -> FastString -> Unique -> Id -> Name
241 mkWiredInIdName mod fs uniq id
242 = mkWiredInName mod (mkOccNameFS Name.varName fs) uniq (AnId id) UserSyntax
243
244 -- See Note [Kind-changing of (~) and Coercible]
245 -- in libraries/ghc-prim/GHC/Types.hs
246 heqTyConName, heqDataConName, heqSCSelIdName :: Name
247 heqTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "~~") heqTyConKey heqTyCon
248 heqDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "Eq#") heqDataConKey heqDataCon
249 heqSCSelIdName = mkWiredInIdName gHC_TYPES (fsLit "HEq_sc") heqSCSelIdKey heqSCSelId
250
251 -- See Note [Kind-changing of (~) and Coercible] in libraries/ghc-prim/GHC/Types.hs
252 coercibleTyConName, coercibleDataConName, coercibleSCSelIdName :: Name
253 coercibleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Coercible") coercibleTyConKey coercibleTyCon
254 coercibleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "MkCoercible") coercibleDataConKey coercibleDataCon
255 coercibleSCSelIdName = mkWiredInIdName gHC_TYPES (fsLit "Coercible_sc") coercibleSCSelIdKey coercibleSCSelId
256
257 charTyConName, charDataConName, intTyConName, intDataConName :: Name
258 charTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
259 charDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
260 intTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Int") intTyConKey intTyCon
261 intDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey intDataCon
262
263 boolTyConName, falseDataConName, trueDataConName :: Name
264 boolTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Bool") boolTyConKey boolTyCon
265 falseDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "False") falseDataConKey falseDataCon
266 trueDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "True") trueDataConKey trueDataCon
267
268 listTyConName, nilDataConName, consDataConName :: Name
269 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
270 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
271 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
272
273 maybeTyConName, nothingDataConName, justDataConName :: Name
274 maybeTyConName = mkWiredInTyConName UserSyntax gHC_BASE (fsLit "Maybe")
275 maybeTyConKey maybeTyCon
276 nothingDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "Nothing")
277 nothingDataConKey nothingDataCon
278 justDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "Just")
279 justDataConKey justDataCon
280
281 wordTyConName, wordDataConName, word8TyConName, word8DataConName :: Name
282 wordTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Word") wordTyConKey wordTyCon
283 wordDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "W#") wordDataConKey wordDataCon
284 word8TyConName = mkWiredInTyConName UserSyntax gHC_WORD (fsLit "Word8") word8TyConKey word8TyCon
285 word8DataConName = mkWiredInDataConName UserSyntax gHC_WORD (fsLit "W8#") word8DataConKey word8DataCon
286
287 floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
288 floatTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Float") floatTyConKey floatTyCon
289 floatDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#") floatDataConKey floatDataCon
290 doubleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey doubleTyCon
291 doubleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#") doubleDataConKey doubleDataCon
292
293 -- Any
294
295 {-
296 Note [Any types]
297 ~~~~~~~~~~~~~~~~
298 The type constructor Any,
299
300 type family Any :: k where { }
301
302 It has these properties:
303
304 * Note that 'Any' is kind polymorphic since in some program we may
305 need to use Any to fill in a type variable of some kind other than *
306 (see #959 for examples). Its kind is thus `forall k. k``.
307
308 * It is defined in module GHC.Types, and exported so that it is
309 available to users. For this reason it's treated like any other
310 wired-in type:
311 - has a fixed unique, anyTyConKey,
312 - lives in the global name cache
313
314 * It is a *closed* type family, with no instances. This means that
315 if ty :: '(k1, k2) we add a given coercion
316 g :: ty ~ (Fst ty, Snd ty)
317 If Any was a *data* type, then we'd get inconsistency because 'ty'
318 could be (Any '(k1,k2)) and then we'd have an equality with Any on
319 one side and '(,) on the other. See also #9097 and #9636.
320
321 * When instantiated at a lifted type it is inhabited by at least one value,
322 namely bottom
323
324 * You can safely coerce any /lifted/ type to Any, and back with unsafeCoerce.
325
326 * It does not claim to be a *data* type, and that's important for
327 the code generator, because the code gen may *enter* a data value
328 but never enters a function value.
329
330 * It is wired-in so we can easily refer to it where we don't have a name
331 environment (e.g. see Rules.matchRule for one example)
332
333 * If (Any k) is the type of a value, it must be a /lifted/ value. So
334 if we have (Any @(TYPE rr)) then rr must be 'PtrRepLifted. See
335 Note [TYPE and RuntimeRep] in TysPrim. This is a convenient
336 invariant, and makes isUnliftedTyCon well-defined; otherwise what
337 would (isUnliftedTyCon Any) be?
338
339 It's used to instantiate un-constrained type variables after type checking. For
340 example, 'length' has type
341
342 length :: forall a. [a] -> Int
343
344 and the list datacon for the empty list has type
345
346 [] :: forall a. [a]
347
348 In order to compose these two terms as @length []@ a type
349 application is required, but there is no constraint on the
350 choice. In this situation GHC uses 'Any',
351
352 > length (Any *) ([] (Any *))
353
354 Above, we print kinds explicitly, as if with --fprint-explicit-kinds.
355
356 The Any tycon used to be quite magic, but we have since been able to
357 implement it merely with an empty kind polymorphic type family. See #10886 for a
358 bit of history.
359 -}
360
361
362 anyTyConName :: Name
363 anyTyConName =
364 mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Any") anyTyConKey anyTyCon
365
366 anyTyCon :: TyCon
367 anyTyCon = mkFamilyTyCon anyTyConName binders res_kind Nothing
368 (ClosedSynFamilyTyCon Nothing)
369 Nothing
370 NotInjective
371 where
372 binders@[kv] = mkTemplateKindTyConBinders [liftedTypeKind]
373 res_kind = mkTyVarTy (binderVar kv)
374
375 anyTy :: Type
376 anyTy = mkTyConTy anyTyCon
377
378 anyTypeOfKind :: Kind -> Type
379 anyTypeOfKind kind = mkTyConApp anyTyCon [kind]
380
381 -- Kinds
382 typeNatKindConName, typeSymbolKindConName :: Name
383 typeNatKindConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Nat") typeNatKindConNameKey typeNatKindCon
384 typeSymbolKindConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Symbol") typeSymbolKindConNameKey typeSymbolKindCon
385
386 constraintKindTyConName :: Name
387 constraintKindTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Constraint") constraintKindTyConKey constraintKindTyCon
388
389 liftedTypeKindTyConName, starKindTyConName, unicodeStarKindTyConName
390 :: Name
391 liftedTypeKindTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Type") liftedTypeKindTyConKey liftedTypeKindTyCon
392 starKindTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "*") starKindTyConKey starKindTyCon
393 unicodeStarKindTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "★") unicodeStarKindTyConKey unicodeStarKindTyCon
394
395 runtimeRepTyConName, vecRepDataConName :: Name
396 runtimeRepTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "RuntimeRep") runtimeRepTyConKey runtimeRepTyCon
397 vecRepDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "VecRep") vecRepDataConKey vecRepDataCon
398
399 -- See Note [Wiring in RuntimeRep]
400 runtimeRepSimpleDataConNames :: [Name]
401 runtimeRepSimpleDataConNames
402 = zipWith3Lazy mk_special_dc_name
403 [ fsLit "PtrRepLifted", fsLit "PtrRepUnlifted"
404 , fsLit "VoidRep", fsLit "IntRep"
405 , fsLit "WordRep", fsLit "Int64Rep", fsLit "Word64Rep"
406 , fsLit "AddrRep", fsLit "FloatRep", fsLit "DoubleRep"
407 , fsLit "UnboxedTupleRep", fsLit "UnboxedSumRep" ]
408 runtimeRepSimpleDataConKeys
409 runtimeRepSimpleDataCons
410
411 vecCountTyConName :: Name
412 vecCountTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "VecCount") vecCountTyConKey vecCountTyCon
413
414 -- See Note [Wiring in RuntimeRep]
415 vecCountDataConNames :: [Name]
416 vecCountDataConNames = zipWith3Lazy mk_special_dc_name
417 [ fsLit "Vec2", fsLit "Vec4", fsLit "Vec8"
418 , fsLit "Vec16", fsLit "Vec32", fsLit "Vec64" ]
419 vecCountDataConKeys
420 vecCountDataCons
421
422 vecElemTyConName :: Name
423 vecElemTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "VecElem") vecElemTyConKey vecElemTyCon
424
425 -- See Note [Wiring in RuntimeRep]
426 vecElemDataConNames :: [Name]
427 vecElemDataConNames = zipWith3Lazy mk_special_dc_name
428 [ fsLit "Int8ElemRep", fsLit "Int16ElemRep", fsLit "Int32ElemRep"
429 , fsLit "Int64ElemRep", fsLit "Word8ElemRep", fsLit "Word16ElemRep"
430 , fsLit "Word32ElemRep", fsLit "Word64ElemRep"
431 , fsLit "FloatElemRep", fsLit "DoubleElemRep" ]
432 vecElemDataConKeys
433 vecElemDataCons
434
435 mk_special_dc_name :: FastString -> Unique -> DataCon -> Name
436 mk_special_dc_name fs u dc = mkWiredInDataConName UserSyntax gHC_TYPES fs u dc
437
438 parrTyConName, parrDataConName :: Name
439 parrTyConName = mkWiredInTyConName BuiltInSyntax
440 gHC_PARR' (fsLit "[::]") parrTyConKey parrTyCon
441 parrDataConName = mkWiredInDataConName UserSyntax
442 gHC_PARR' (fsLit "PArr") parrDataConKey parrDataCon
443
444 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
445 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR :: RdrName
446 boolTyCon_RDR = nameRdrName boolTyConName
447 false_RDR = nameRdrName falseDataConName
448 true_RDR = nameRdrName trueDataConName
449 intTyCon_RDR = nameRdrName intTyConName
450 charTyCon_RDR = nameRdrName charTyConName
451 intDataCon_RDR = nameRdrName intDataConName
452 listTyCon_RDR = nameRdrName listTyConName
453 consDataCon_RDR = nameRdrName consDataConName
454 parrTyCon_RDR = nameRdrName parrTyConName
455
456 {-
457 ************************************************************************
458 * *
459 \subsection{mkWiredInTyCon}
460 * *
461 ************************************************************************
462 -}
463
464 pcNonEnumTyCon :: Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
465 -- Not an enumeration
466 pcNonEnumTyCon = pcTyCon False
467
468 -- This function assumes that the types it creates have all parameters at
469 -- Representational role, and that there is no kind polymorphism.
470 pcTyCon :: Bool -> Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
471 pcTyCon is_enum name cType tyvars cons
472 = mkAlgTyCon name
473 (mkAnonTyConBinders tyvars)
474 liftedTypeKind
475 (map (const Representational) tyvars)
476 cType
477 [] -- No stupid theta
478 (DataTyCon cons is_enum)
479 (VanillaAlgTyCon (mkPrelTyConRepName name))
480 False -- Not in GADT syntax
481
482 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
483 pcDataCon n univs = pcDataConWithFixity False n univs [] -- no ex_tvs
484
485 pcDataConWithFixity :: Bool -- ^ declared infix?
486 -> Name -- ^ datacon name
487 -> [TyVar] -- ^ univ tyvars
488 -> [TyVar] -- ^ ex tyvars
489 -> [Type] -- ^ args
490 -> TyCon
491 -> DataCon
492 pcDataConWithFixity infx n = pcDataConWithFixity' infx n (incrUnique (nameUnique n))
493 NoRRI
494 -- The Name's unique is the first of two free uniques;
495 -- the first is used for the datacon itself,
496 -- the second is used for the "worker name"
497 --
498 -- To support this the mkPreludeDataConUnique function "allocates"
499 -- one DataCon unique per pair of Ints.
500
501 pcDataConWithFixity' :: Bool -> Name -> Unique -> RuntimeRepInfo
502 -> [TyVar] -> [TyVar]
503 -> [Type] -> TyCon -> DataCon
504 -- The Name should be in the DataName name space; it's the name
505 -- of the DataCon itself.
506
507 pcDataConWithFixity' declared_infix dc_name wrk_key rri tyvars ex_tyvars arg_tys tycon
508 = data_con
509 where
510 data_con = mkDataCon dc_name declared_infix prom_info
511 (map (const no_bang) arg_tys)
512 [] -- No labelled fields
513 (mkTyVarBinders Specified tyvars)
514 (mkTyVarBinders Specified ex_tyvars)
515 [] -- No equality spec
516 [] -- No theta
517 arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
518 rri
519 tycon
520 [] -- No stupid theta
521 (mkDataConWorkId wrk_name data_con)
522 NoDataConRep -- Wired-in types are too simple to need wrappers
523
524 no_bang = HsSrcBang Nothing NoSrcUnpack NoSrcStrict
525
526 modu = ASSERT( isExternalName dc_name )
527 nameModule dc_name
528 dc_occ = nameOccName dc_name
529 wrk_occ = mkDataConWorkerOcc dc_occ
530 wrk_name = mkWiredInName modu wrk_occ wrk_key
531 (AnId (dataConWorkId data_con)) UserSyntax
532
533 prom_info = mkPrelTyConRepName dc_name
534
535 -- used for RuntimeRep and friends
536 pcSpecialDataCon :: Name -> [Type] -> TyCon -> RuntimeRepInfo -> DataCon
537 pcSpecialDataCon dc_name arg_tys tycon rri
538 = pcDataConWithFixity' False dc_name (incrUnique (nameUnique dc_name)) rri
539 [] [] arg_tys tycon
540
541 {-
542 ************************************************************************
543 * *
544 Kinds
545 * *
546 ************************************************************************
547 -}
548
549 typeNatKindCon, typeSymbolKindCon :: TyCon
550 -- data Nat
551 -- data Symbol
552 typeNatKindCon = pcTyCon False typeNatKindConName Nothing [] []
553 typeSymbolKindCon = pcTyCon False typeSymbolKindConName Nothing [] []
554
555 typeNatKind, typeSymbolKind :: Kind
556 typeNatKind = mkTyConTy typeNatKindCon
557 typeSymbolKind = mkTyConTy typeSymbolKindCon
558
559 constraintKindTyCon :: TyCon
560 constraintKindTyCon = pcTyCon False constraintKindTyConName
561 Nothing [] []
562
563 liftedTypeKind, constraintKind, unboxedTupleKind :: Kind
564 liftedTypeKind = tYPE ptrRepLiftedTy
565 constraintKind = mkTyConApp constraintKindTyCon []
566 unboxedTupleKind = tYPE unboxedTupleRepDataConTy
567
568 -- mkFunKind and mkForAllKind are defined here
569 -- solely so that TyCon can use them via a SOURCE import
570 mkFunKind :: Kind -> Kind -> Kind
571 mkFunKind = mkFunTy
572
573 mkForAllKind :: TyVar -> ArgFlag -> Kind -> Kind
574 mkForAllKind = mkForAllTy
575
576 {-
577 ************************************************************************
578 * *
579 Stuff for dealing with tuples
580 * *
581 ************************************************************************
582
583 Note [How tuples work] See also Note [Known-key names] in PrelNames
584 ~~~~~~~~~~~~~~~~~~~~~~
585 * There are three families of tuple TyCons and corresponding
586 DataCons, expressed by the type BasicTypes.TupleSort:
587 data TupleSort = BoxedTuple | UnboxedTuple | ConstraintTuple
588
589 * All three families are AlgTyCons, whose AlgTyConRhs is TupleTyCon
590
591 * BoxedTuples
592 - A wired-in type
593 - Data type declarations in GHC.Tuple
594 - The data constructors really have an info table
595
596 * UnboxedTuples
597 - A wired-in type
598 - Have a pretend DataCon, defined in GHC.Prim,
599 but no actual declaration and no info table
600
601 * ConstraintTuples
602 - Are known-key rather than wired-in. Reason: it's awkward to
603 have all the superclass selectors wired-in.
604 - Declared as classes in GHC.Classes, e.g.
605 class (c1,c2) => (c1,c2)
606 - Given constraints: the superclasses automatically become available
607 - Wanted constraints: there is a built-in instance
608 instance (c1,c2) => (c1,c2)
609 - Currently just go up to 16; beyond that
610 you have to use manual nesting
611 - Their OccNames look like (%,,,%), so they can easily be
612 distinguished from term tuples. But (following Haskell) we
613 pretty-print saturated constraint tuples with round parens; see
614 BasicTypes.tupleParens.
615
616 * In quite a lot of places things are restrcted just to
617 BoxedTuple/UnboxedTuple, and then we used BasicTypes.Boxity to distinguish
618 E.g. tupleTyCon has a Boxity argument
619
620 * When looking up an OccName in the original-name cache
621 (IfaceEnv.lookupOrigNameCache), we spot the tuple OccName to make sure
622 we get the right wired-in name. This guy can't tell the difference
623 between BoxedTuple and ConstraintTuple (same OccName!), so tuples
624 are not serialised into interface files using OccNames at all.
625
626 Note [One-tuples]
627 ~~~~~~~~~~~~~~~~~
628 GHC supports both boxed and unboxed one-tuples:
629 - Unboxed one-tuples are sometimes useful when returning a
630 single value after CPR analysis
631 - A boxed one-tuple is used by DsUtils.mkSelectorBinds, when
632 there is just one binder
633 Basically it keeps everythig uniform.
634
635 However the /naming/ of the type/data constructors for one-tuples is a
636 bit odd:
637 3-tuples: (,,) (,,)#
638 2-tuples: (,) (,)#
639 1-tuples: ??
640 0-tuples: () ()#
641
642 Zero-tuples have used up the logical name. So we use 'Unit' and 'Unit#'
643 for one-tuples. So in ghc-prim:GHC.Tuple we see the declarations:
644 data () = ()
645 data Unit a = Unit a
646 data (a,b) = (a,b)
647
648 NB (Feb 16): for /constraint/ one-tuples I have 'Unit%' but no class
649 decl in GHC.Classes, so I think this part may not work properly. But
650 it's unused I think.
651 -}
652
653 -- | Built in syntax isn't "in scope" so these OccNames map to wired-in Names
654 -- with BuiltInSyntax. However, this should only be necessary while resolving
655 -- names produced by Template Haskell splices since we take care to encode
656 -- built-in syntax names specially in interface files. See
657 -- Note [Symbol table representation of names].
658 isBuiltInOcc_maybe :: OccName -> Maybe Name
659 isBuiltInOcc_maybe occ =
660 case name of
661 "[]" -> Just $ choose_ns listTyConName nilDataConName
662 ":" -> Just consDataConName
663 "[::]" -> Just parrTyConName
664 "()" -> Just $ tup_name Boxed 0
665 "(##)" -> Just $ tup_name Unboxed 0
666 _ | Just rest <- "(" `stripPrefix` name
667 , (commas, rest') <- BS.span (==',') rest
668 , ")" <- rest'
669 -> Just $ tup_name Boxed (1+BS.length commas)
670 _ | Just rest <- "(#" `stripPrefix` name
671 , (commas, rest') <- BS.span (==',') rest
672 , "#)" <- rest'
673 -> Just $ tup_name Unboxed (1+BS.length commas)
674 _ -> Nothing
675 where
676 -- TODO: Drop when bytestring 0.10.8 can be assumed
677 #if MIN_VERSION_bytestring(0,10,8)
678 stripPrefix = BS.stripPrefix
679 #else
680 stripPrefix bs1@(BSI.PS _ _ l1) bs2
681 | bs1 `BS.isPrefixOf` bs2 = Just (BSU.unsafeDrop l1 bs2)
682 | otherwise = Nothing
683 #endif
684
685 name = fastStringToByteString $ occNameFS occ
686
687 choose_ns :: Name -> Name -> Name
688 choose_ns tc dc
689 | isTcClsNameSpace ns = tc
690 | isDataConNameSpace ns = dc
691 | otherwise = pprPanic "tup_name" (ppr occ)
692 where ns = occNameSpace occ
693
694 tup_name boxity arity
695 = choose_ns (getName (tupleTyCon boxity arity))
696 (getName (tupleDataCon boxity arity))
697
698 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
699 -- No need to cache these, the caching is done in mk_tuple
700 mkTupleOcc ns Boxed ar = mkOccName ns (mkBoxedTupleStr ar)
701 mkTupleOcc ns Unboxed ar = mkOccName ns (mkUnboxedTupleStr ar)
702
703 mkCTupleOcc :: NameSpace -> Arity -> OccName
704 mkCTupleOcc ns ar = mkOccName ns (mkConstraintTupleStr ar)
705
706 mkBoxedTupleStr :: Arity -> String
707 mkBoxedTupleStr 0 = "()"
708 mkBoxedTupleStr 1 = "Unit" -- See Note [One-tuples]
709 mkBoxedTupleStr ar = '(' : commas ar ++ ")"
710
711 mkUnboxedTupleStr :: Arity -> String
712 mkUnboxedTupleStr 0 = "(##)"
713 mkUnboxedTupleStr 1 = "Unit#" -- See Note [One-tuples]
714 mkUnboxedTupleStr ar = "(#" ++ commas ar ++ "#)"
715
716 mkConstraintTupleStr :: Arity -> String
717 mkConstraintTupleStr 0 = "(%%)"
718 mkConstraintTupleStr 1 = "Unit%" -- See Note [One-tuples]
719 mkConstraintTupleStr ar = "(%" ++ commas ar ++ "%)"
720
721 commas :: Arity -> String
722 commas ar = take (ar-1) (repeat ',')
723
724 cTupleTyConName :: Arity -> Name
725 cTupleTyConName arity
726 = mkExternalName (mkCTupleTyConUnique arity) gHC_CLASSES
727 (mkCTupleOcc tcName arity) noSrcSpan
728 -- The corresponding DataCon does not have a known-key name
729
730 cTupleTyConNames :: [Name]
731 cTupleTyConNames = map cTupleTyConName (0 : [2..mAX_CTUPLE_SIZE])
732
733 cTupleTyConNameSet :: NameSet
734 cTupleTyConNameSet = mkNameSet cTupleTyConNames
735
736 isCTupleTyConName :: Name -> Bool
737 -- Use Type.isCTupleClass where possible
738 isCTupleTyConName n
739 = ASSERT2( isExternalName n, ppr n )
740 nameModule n == gHC_CLASSES
741 && n `elemNameSet` cTupleTyConNameSet
742
743 tupleTyCon :: Boxity -> Arity -> TyCon
744 tupleTyCon sort i | i > mAX_TUPLE_SIZE = fst (mk_tuple sort i) -- Build one specially
745 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
746 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
747
748 tupleTyConName :: TupleSort -> Arity -> Name
749 tupleTyConName ConstraintTuple a = cTupleTyConName a
750 tupleTyConName BoxedTuple a = tyConName (tupleTyCon Boxed a)
751 tupleTyConName UnboxedTuple a = tyConName (tupleTyCon Unboxed a)
752
753 promotedTupleDataCon :: Boxity -> Arity -> TyCon
754 promotedTupleDataCon boxity i = promoteDataCon (tupleDataCon boxity i)
755
756 tupleDataCon :: Boxity -> Arity -> DataCon
757 tupleDataCon sort i | i > mAX_TUPLE_SIZE = snd (mk_tuple sort i) -- Build one specially
758 tupleDataCon Boxed i = snd (boxedTupleArr ! i)
759 tupleDataCon Unboxed i = snd (unboxedTupleArr ! i)
760
761 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
762 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
763 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
764
765 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
766 mk_tuple Boxed arity = (tycon, tuple_con)
767 where
768 tycon = mkTupleTyCon tc_name tc_binders tc_res_kind tc_arity tuple_con
769 BoxedTuple flavour
770
771 tc_binders = mkTemplateAnonTyConBinders (nOfThem arity liftedTypeKind)
772 tc_res_kind = liftedTypeKind
773 tc_arity = arity
774 flavour = VanillaAlgTyCon (mkPrelTyConRepName tc_name)
775
776 dc_tvs = binderVars tc_binders
777 dc_arg_tys = mkTyVarTys dc_tvs
778 tuple_con = pcDataCon dc_name dc_tvs dc_arg_tys tycon
779
780 boxity = Boxed
781 modu = gHC_TUPLE
782 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
783 (ATyCon tycon) BuiltInSyntax
784 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
785 (AConLike (RealDataCon tuple_con)) BuiltInSyntax
786 tc_uniq = mkTupleTyConUnique boxity arity
787 dc_uniq = mkTupleDataConUnique boxity arity
788
789 mk_tuple Unboxed arity = (tycon, tuple_con)
790 where
791 tycon = mkTupleTyCon tc_name tc_binders tc_res_kind tc_arity tuple_con
792 UnboxedTuple flavour
793
794 -- See Note [Unboxed tuple RuntimeRep vars] in TyCon
795 -- Kind: forall (k1:RuntimeRep) (k2:RuntimeRep). TYPE k1 -> TYPE k2 -> #
796 tc_binders = mkTemplateTyConBinders (nOfThem arity runtimeRepTy)
797 (\ks -> map tYPE ks)
798
799 tc_res_kind | arity == 0 = tYPE voidRepDataConTy -- Nullary unboxed tuple
800 | otherwise = unboxedTupleKind
801
802 tc_arity = arity * 2
803 flavour = UnboxedAlgTyCon
804
805 dc_tvs = binderVars tc_binders
806 dc_arg_tys = mkTyVarTys (drop arity dc_tvs)
807 tuple_con = pcDataCon dc_name dc_tvs dc_arg_tys tycon
808
809 boxity = Unboxed
810 modu = gHC_PRIM
811 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
812 (ATyCon tycon) BuiltInSyntax
813 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
814 (AConLike (RealDataCon tuple_con)) BuiltInSyntax
815 tc_uniq = mkTupleTyConUnique boxity arity
816 dc_uniq = mkTupleDataConUnique boxity arity
817
818 unitTyCon :: TyCon
819 unitTyCon = tupleTyCon Boxed 0
820
821 unitTyConKey :: Unique
822 unitTyConKey = getUnique unitTyCon
823
824 unitDataCon :: DataCon
825 unitDataCon = head (tyConDataCons unitTyCon)
826
827 unitDataConId :: Id
828 unitDataConId = dataConWorkId unitDataCon
829
830 pairTyCon :: TyCon
831 pairTyCon = tupleTyCon Boxed 2
832
833 unboxedUnitTyCon :: TyCon
834 unboxedUnitTyCon = tupleTyCon Unboxed 0
835
836 unboxedUnitDataCon :: DataCon
837 unboxedUnitDataCon = tupleDataCon Unboxed 0
838
839
840 {- *********************************************************************
841 * *
842 Unboxed sums
843 * *
844 ********************************************************************* -}
845
846 -- | OccName for n-ary unboxed sum type constructor.
847 mkSumTyConOcc :: Arity -> OccName
848 mkSumTyConOcc n = mkOccName tcName str
849 where
850 -- No need to cache these, the caching is done in mk_sum
851 str = '(' : '#' : bars ++ "#)"
852 bars = replicate (n-1) '|'
853
854 -- | OccName for i-th alternative of n-ary unboxed sum data constructor.
855 mkSumDataConOcc :: ConTag -> Arity -> OccName
856 mkSumDataConOcc alt n = mkOccName dataName str
857 where
858 -- No need to cache these, the caching is done in mk_sum
859 str = '(' : '#' : bars alt ++ '_' : bars (n - alt - 1) ++ "#)"
860 bars i = replicate i '|'
861
862 -- | Type constructor for n-ary unboxed sum.
863 sumTyCon :: Arity -> TyCon
864 sumTyCon arity
865 | arity > mAX_SUM_SIZE
866 = fst (mk_sum arity) -- Build one specially
867
868 | arity < 2
869 = panic ("sumTyCon: Arity starts from 2. (arity: " ++ show arity ++ ")")
870
871 | otherwise
872 = fst (unboxedSumArr ! arity)
873
874 -- | Data constructor for i-th alternative of a n-ary unboxed sum.
875 sumDataCon :: ConTag -- Alternative
876 -> Arity -- Arity
877 -> DataCon
878 sumDataCon alt arity
879 | alt > arity
880 = panic ("sumDataCon: index out of bounds: alt: "
881 ++ show alt ++ " > arity " ++ show arity)
882
883 | alt <= 0
884 = panic ("sumDataCon: Alts start from 1. (alt: " ++ show alt
885 ++ ", arity: " ++ show arity ++ ")")
886
887 | arity < 2
888 = panic ("sumDataCon: Arity starts from 2. (alt: " ++ show alt
889 ++ ", arity: " ++ show arity ++ ")")
890
891 | arity > mAX_SUM_SIZE
892 = snd (mk_sum arity) ! (alt - 1) -- Build one specially
893
894 | otherwise
895 = snd (unboxedSumArr ! arity) ! (alt - 1)
896
897 -- | Cached type and data constructors for sums. The outer array is
898 -- indexed by the arity of the sum and the inner array is indexed by
899 -- the alternative.
900 unboxedSumArr :: Array Int (TyCon, Array Int DataCon)
901 unboxedSumArr = listArray (2,mAX_SUM_SIZE) [mk_sum i | i <- [2..mAX_SUM_SIZE]]
902
903 -- | Create type constructor and data constructors for n-ary unboxed sum.
904 mk_sum :: Arity -> (TyCon, Array ConTagZ DataCon)
905 mk_sum arity = (tycon, sum_cons)
906 where
907 tycon = mkSumTyCon tc_name tc_binders tc_res_kind (arity * 2) tyvars (elems sum_cons)
908 UnboxedAlgTyCon
909
910 tc_binders = mkTemplateTyConBinders (nOfThem arity runtimeRepTy)
911 (\ks -> map tYPE ks)
912
913 tyvars = mkTemplateTyVars (replicate arity runtimeRepTy ++
914 map (tYPE . mkTyVarTy) (take arity tyvars))
915
916 tc_res_kind = tYPE unboxedSumRepDataConTy
917
918 open_tvs = drop arity tyvars
919
920 tc_name = mkWiredInName gHC_PRIM (mkSumTyConOcc arity) tc_uniq
921 (ATyCon tycon) BuiltInSyntax
922
923 sum_cons = listArray (0,arity-1) [sum_con i | i <- [0..arity-1]]
924 sum_con i = let dc = pcDataCon dc_name
925 tyvars -- univ tyvars
926 [tyvar_tys !! i] -- arg types
927 tycon
928
929 dc_name = mkWiredInName gHC_PRIM
930 (mkSumDataConOcc i arity)
931 (dc_uniq i)
932 (AConLike (RealDataCon dc))
933 BuiltInSyntax
934 in dc
935 tyvar_tys = mkTyVarTys open_tvs
936 tc_uniq = mkSumTyConUnique arity
937 dc_uniq i = mkSumDataConUnique i arity
938
939 {-
940 ************************************************************************
941 * *
942 Equality types and classes
943 * *
944 ********************************************************************* -}
945
946 -- See Note [The equality types story] in TysPrim
947 -- (:~~: :: forall k1 k2 (a :: k1) (b :: k2). a -> b -> Constraint)
948 heqTyCon, coercibleTyCon :: TyCon
949 heqClass, coercibleClass :: Class
950 heqDataCon, coercibleDataCon :: DataCon
951 heqSCSelId, coercibleSCSelId :: Id
952
953 (heqTyCon, heqClass, heqDataCon, heqSCSelId)
954 = (tycon, klass, datacon, sc_sel_id)
955 where
956 tycon = mkClassTyCon heqTyConName binders roles
957 rhs klass
958 (mkPrelTyConRepName heqTyConName)
959 klass = mk_class tycon sc_pred sc_sel_id
960 datacon = pcDataCon heqDataConName tvs [sc_pred] tycon
961
962 -- Kind: forall k1 k2. k1 -> k2 -> Constraint
963 binders = mkTemplateTyConBinders [liftedTypeKind, liftedTypeKind] (\ks -> ks)
964 roles = [Nominal, Nominal, Nominal, Nominal]
965 rhs = DataTyCon { data_cons = [datacon], is_enum = False }
966
967 tvs = binderVars binders
968 sc_pred = mkTyConApp eqPrimTyCon (mkTyVarTys tvs)
969 sc_sel_id = mkDictSelId heqSCSelIdName klass
970
971 (coercibleTyCon, coercibleClass, coercibleDataCon, coercibleSCSelId)
972 = (tycon, klass, datacon, sc_sel_id)
973 where
974 tycon = mkClassTyCon coercibleTyConName binders roles
975 rhs klass
976 (mkPrelTyConRepName coercibleTyConName)
977 klass = mk_class tycon sc_pred sc_sel_id
978 datacon = pcDataCon coercibleDataConName tvs [sc_pred] tycon
979
980 -- Kind: forall k. k -> k -> Constraint
981 binders = mkTemplateTyConBinders [liftedTypeKind] (\[k] -> [k,k])
982 roles = [Nominal, Representational, Representational]
983 rhs = DataTyCon { data_cons = [datacon], is_enum = False }
984
985 tvs@[k,a,b] = binderVars binders
986 sc_pred = mkTyConApp eqReprPrimTyCon (mkTyVarTys [k, k, a, b])
987 sc_sel_id = mkDictSelId coercibleSCSelIdName klass
988
989 mk_class :: TyCon -> PredType -> Id -> Class
990 mk_class tycon sc_pred sc_sel_id
991 = mkClass (tyConName tycon) (tyConTyVars tycon) [] [sc_pred] [sc_sel_id]
992 [] [] (mkAnd []) tycon
993
994 {- *********************************************************************
995 * *
996 Kinds and RuntimeRep
997 * *
998 ********************************************************************* -}
999
1000 -- For information about the usage of the following type,
1001 -- see Note [TYPE and RuntimeRep] in module TysPrim
1002 runtimeRepTy :: Type
1003 runtimeRepTy = mkTyConTy runtimeRepTyCon
1004
1005 liftedTypeKindTyCon, starKindTyCon, unicodeStarKindTyCon :: TyCon
1006
1007 -- Type syononyms; see Note [TYPE and RuntimeRep] in TysPrim
1008 -- type Type = tYPE 'PtrRepLifted
1009 -- type * = tYPE 'PtrRepLifted
1010 -- type * = tYPE 'PtrRepLifted -- Unicode variant
1011
1012 liftedTypeKindTyCon = mkSynonymTyCon liftedTypeKindTyConName
1013 [] liftedTypeKind []
1014 (tYPE ptrRepLiftedTy)
1015
1016 starKindTyCon = mkSynonymTyCon starKindTyConName
1017 [] liftedTypeKind []
1018 (tYPE ptrRepLiftedTy)
1019
1020 unicodeStarKindTyCon = mkSynonymTyCon unicodeStarKindTyConName
1021 [] liftedTypeKind []
1022 (tYPE ptrRepLiftedTy)
1023
1024 runtimeRepTyCon :: TyCon
1025 runtimeRepTyCon = pcNonEnumTyCon runtimeRepTyConName Nothing []
1026 (vecRepDataCon : runtimeRepSimpleDataCons)
1027
1028 vecRepDataCon :: DataCon
1029 vecRepDataCon = pcSpecialDataCon vecRepDataConName [ mkTyConTy vecCountTyCon
1030 , mkTyConTy vecElemTyCon ]
1031 runtimeRepTyCon
1032 (RuntimeRep prim_rep_fun)
1033 where
1034 prim_rep_fun [count, elem]
1035 | VecCount n <- tyConRuntimeRepInfo (tyConAppTyCon count)
1036 , VecElem e <- tyConRuntimeRepInfo (tyConAppTyCon elem)
1037 = VecRep n e
1038 prim_rep_fun args
1039 = pprPanic "vecRepDataCon" (ppr args)
1040
1041 vecRepDataConTyCon :: TyCon
1042 vecRepDataConTyCon = promoteDataCon vecRepDataCon
1043
1044 ptrRepUnliftedDataConTyCon :: TyCon
1045 ptrRepUnliftedDataConTyCon = promoteDataCon ptrRepUnliftedDataCon
1046
1047 -- See Note [Wiring in RuntimeRep]
1048 runtimeRepSimpleDataCons :: [DataCon]
1049 ptrRepLiftedDataCon, ptrRepUnliftedDataCon :: DataCon
1050 runtimeRepSimpleDataCons@(ptrRepLiftedDataCon : ptrRepUnliftedDataCon : _)
1051 = zipWithLazy mk_runtime_rep_dc
1052 [ PtrRep, PtrRep, VoidRep, IntRep, WordRep, Int64Rep
1053 , Word64Rep, AddrRep, FloatRep, DoubleRep
1054 , panic "unboxed tuple PrimRep", panic "unboxed sum PrimRep" ]
1055 runtimeRepSimpleDataConNames
1056 where
1057 mk_runtime_rep_dc primrep name
1058 = pcSpecialDataCon name [] runtimeRepTyCon (RuntimeRep (\_ -> primrep))
1059
1060 -- See Note [Wiring in RuntimeRep]
1061 voidRepDataConTy, intRepDataConTy, wordRepDataConTy, int64RepDataConTy,
1062 word64RepDataConTy, addrRepDataConTy, floatRepDataConTy, doubleRepDataConTy,
1063 unboxedTupleRepDataConTy, unboxedSumRepDataConTy :: Type
1064 [_, _, voidRepDataConTy, intRepDataConTy, wordRepDataConTy, int64RepDataConTy,
1065 word64RepDataConTy, addrRepDataConTy, floatRepDataConTy, doubleRepDataConTy,
1066 unboxedTupleRepDataConTy, unboxedSumRepDataConTy] = map (mkTyConTy . promoteDataCon)
1067 runtimeRepSimpleDataCons
1068
1069 vecCountTyCon :: TyCon
1070 vecCountTyCon = pcNonEnumTyCon vecCountTyConName Nothing []
1071 vecCountDataCons
1072
1073 -- See Note [Wiring in RuntimeRep]
1074 vecCountDataCons :: [DataCon]
1075 vecCountDataCons = zipWithLazy mk_vec_count_dc
1076 [ 2, 4, 8, 16, 32, 64 ]
1077 vecCountDataConNames
1078 where
1079 mk_vec_count_dc n name
1080 = pcSpecialDataCon name [] vecCountTyCon (VecCount n)
1081
1082 -- See Note [Wiring in RuntimeRep]
1083 vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy,
1084 vec64DataConTy :: Type
1085 [vec2DataConTy, vec4DataConTy, vec8DataConTy, vec16DataConTy, vec32DataConTy,
1086 vec64DataConTy] = map (mkTyConTy . promoteDataCon) vecCountDataCons
1087
1088 vecElemTyCon :: TyCon
1089 vecElemTyCon = pcNonEnumTyCon vecElemTyConName Nothing [] vecElemDataCons
1090
1091 -- See Note [Wiring in RuntimeRep]
1092 vecElemDataCons :: [DataCon]
1093 vecElemDataCons = zipWithLazy mk_vec_elem_dc
1094 [ Int8ElemRep, Int16ElemRep, Int32ElemRep, Int64ElemRep
1095 , Word8ElemRep, Word16ElemRep, Word32ElemRep, Word64ElemRep
1096 , FloatElemRep, DoubleElemRep ]
1097 vecElemDataConNames
1098 where
1099 mk_vec_elem_dc elem name
1100 = pcSpecialDataCon name [] vecElemTyCon (VecElem elem)
1101
1102 -- See Note [Wiring in RuntimeRep]
1103 int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy,
1104 int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy,
1105 word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy,
1106 doubleElemRepDataConTy :: Type
1107 [int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy,
1108 int64ElemRepDataConTy, word8ElemRepDataConTy, word16ElemRepDataConTy,
1109 word32ElemRepDataConTy, word64ElemRepDataConTy, floatElemRepDataConTy,
1110 doubleElemRepDataConTy] = map (mkTyConTy . promoteDataCon)
1111 vecElemDataCons
1112
1113 -- The type ('PtrRepLifted)
1114 ptrRepLiftedTy :: Type
1115 ptrRepLiftedTy = mkTyConTy $ promoteDataCon ptrRepLiftedDataCon
1116
1117 {- *********************************************************************
1118 * *
1119 The boxed primitive types: Char, Int, etc
1120 * *
1121 ********************************************************************* -}
1122
1123 charTy :: Type
1124 charTy = mkTyConTy charTyCon
1125
1126 charTyCon :: TyCon
1127 charTyCon = pcNonEnumTyCon charTyConName
1128 (Just (CType "" Nothing ("HsChar",fsLit "HsChar")))
1129 [] [charDataCon]
1130 charDataCon :: DataCon
1131 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
1132
1133 stringTy :: Type
1134 stringTy = mkListTy charTy -- convenience only
1135
1136 intTy :: Type
1137 intTy = mkTyConTy intTyCon
1138
1139 intTyCon :: TyCon
1140 intTyCon = pcNonEnumTyCon intTyConName
1141 (Just (CType "" Nothing ("HsInt",fsLit "HsInt"))) []
1142 [intDataCon]
1143 intDataCon :: DataCon
1144 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
1145
1146 wordTy :: Type
1147 wordTy = mkTyConTy wordTyCon
1148
1149 wordTyCon :: TyCon
1150 wordTyCon = pcNonEnumTyCon wordTyConName
1151 (Just (CType "" Nothing ("HsWord", fsLit "HsWord"))) []
1152 [wordDataCon]
1153 wordDataCon :: DataCon
1154 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
1155
1156 word8Ty :: Type
1157 word8Ty = mkTyConTy word8TyCon
1158
1159 word8TyCon :: TyCon
1160 word8TyCon = pcNonEnumTyCon word8TyConName
1161 (Just (CType "" Nothing ("HsWord8", fsLit "HsWord8"))) []
1162 [word8DataCon]
1163 word8DataCon :: DataCon
1164 word8DataCon = pcDataCon word8DataConName [] [wordPrimTy] word8TyCon
1165
1166 floatTy :: Type
1167 floatTy = mkTyConTy floatTyCon
1168
1169 floatTyCon :: TyCon
1170 floatTyCon = pcNonEnumTyCon floatTyConName
1171 (Just (CType "" Nothing ("HsFloat", fsLit "HsFloat"))) []
1172 [floatDataCon]
1173 floatDataCon :: DataCon
1174 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
1175
1176 doubleTy :: Type
1177 doubleTy = mkTyConTy doubleTyCon
1178
1179 doubleTyCon :: TyCon
1180 doubleTyCon = pcNonEnumTyCon doubleTyConName
1181 (Just (CType "" Nothing ("HsDouble",fsLit "HsDouble"))) []
1182 [doubleDataCon]
1183
1184 doubleDataCon :: DataCon
1185 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
1186
1187 {-
1188 ************************************************************************
1189 * *
1190 The Bool type
1191 * *
1192 ************************************************************************
1193
1194 An ordinary enumeration type, but deeply wired in. There are no
1195 magical operations on @Bool@ (just the regular Prelude code).
1196
1197 {\em BEGIN IDLE SPECULATION BY SIMON}
1198
1199 This is not the only way to encode @Bool@. A more obvious coding makes
1200 @Bool@ just a boxed up version of @Bool#@, like this:
1201 \begin{verbatim}
1202 type Bool# = Int#
1203 data Bool = MkBool Bool#
1204 \end{verbatim}
1205
1206 Unfortunately, this doesn't correspond to what the Report says @Bool@
1207 looks like! Furthermore, we get slightly less efficient code (I
1208 think) with this coding. @gtInt@ would look like this:
1209
1210 \begin{verbatim}
1211 gtInt :: Int -> Int -> Bool
1212 gtInt x y = case x of I# x# ->
1213 case y of I# y# ->
1214 case (gtIntPrim x# y#) of
1215 b# -> MkBool b#
1216 \end{verbatim}
1217
1218 Notice that the result of the @gtIntPrim@ comparison has to be turned
1219 into an integer (here called @b#@), and returned in a @MkBool@ box.
1220
1221 The @if@ expression would compile to this:
1222 \begin{verbatim}
1223 case (gtInt x y) of
1224 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
1225 \end{verbatim}
1226
1227 I think this code is a little less efficient than the previous code,
1228 but I'm not certain. At all events, corresponding with the Report is
1229 important. The interesting thing is that the language is expressive
1230 enough to describe more than one alternative; and that a type doesn't
1231 necessarily need to be a straightforwardly boxed version of its
1232 primitive counterpart.
1233
1234 {\em END IDLE SPECULATION BY SIMON}
1235 -}
1236
1237 boolTy :: Type
1238 boolTy = mkTyConTy boolTyCon
1239
1240 boolTyCon :: TyCon
1241 boolTyCon = pcTyCon True boolTyConName
1242 (Just (CType "" Nothing ("HsBool", fsLit "HsBool")))
1243 [] [falseDataCon, trueDataCon]
1244
1245 falseDataCon, trueDataCon :: DataCon
1246 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
1247 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
1248
1249 falseDataConId, trueDataConId :: Id
1250 falseDataConId = dataConWorkId falseDataCon
1251 trueDataConId = dataConWorkId trueDataCon
1252
1253 orderingTyCon :: TyCon
1254 orderingTyCon = pcTyCon True orderingTyConName Nothing
1255 [] [ltDataCon, eqDataCon, gtDataCon]
1256
1257 ltDataCon, eqDataCon, gtDataCon :: DataCon
1258 ltDataCon = pcDataCon ltDataConName [] [] orderingTyCon
1259 eqDataCon = pcDataCon eqDataConName [] [] orderingTyCon
1260 gtDataCon = pcDataCon gtDataConName [] [] orderingTyCon
1261
1262 ltDataConId, eqDataConId, gtDataConId :: Id
1263 ltDataConId = dataConWorkId ltDataCon
1264 eqDataConId = dataConWorkId eqDataCon
1265 gtDataConId = dataConWorkId gtDataCon
1266
1267 {-
1268 ************************************************************************
1269 * *
1270 The List type
1271 Special syntax, deeply wired in,
1272 but otherwise an ordinary algebraic data type
1273 * *
1274 ************************************************************************
1275
1276 data [] a = [] | a : (List a)
1277 -}
1278
1279 mkListTy :: Type -> Type
1280 mkListTy ty = mkTyConApp listTyCon [ty]
1281
1282 listTyCon :: TyCon
1283 listTyCon = buildAlgTyCon listTyConName alpha_tyvar [Representational]
1284 Nothing []
1285 (DataTyCon [nilDataCon, consDataCon] False )
1286 False
1287 (VanillaAlgTyCon $ mkPrelTyConRepName listTyConName)
1288
1289 nilDataCon :: DataCon
1290 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
1291
1292 consDataCon :: DataCon
1293 consDataCon = pcDataConWithFixity True {- Declared infix -}
1294 consDataConName
1295 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
1296 -- Interesting: polymorphic recursion would help here.
1297 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
1298 -- gets the over-specific type (Type -> Type)
1299
1300 -- Wired-in type Maybe
1301
1302 maybeTyCon :: TyCon
1303 maybeTyCon = pcTyCon False maybeTyConName Nothing alpha_tyvar
1304 [nothingDataCon, justDataCon]
1305
1306 nothingDataCon :: DataCon
1307 nothingDataCon = pcDataCon nothingDataConName alpha_tyvar [] maybeTyCon
1308
1309 justDataCon :: DataCon
1310 justDataCon = pcDataCon justDataConName alpha_tyvar [alphaTy] maybeTyCon
1311
1312 {-
1313 ** *********************************************************************
1314 * *
1315 The tuple types
1316 * *
1317 ************************************************************************
1318
1319 The tuple types are definitely magic, because they form an infinite
1320 family.
1321
1322 \begin{itemize}
1323 \item
1324 They have a special family of type constructors, of type @TyCon@
1325 These contain the tycon arity, but don't require a Unique.
1326
1327 \item
1328 They have a special family of constructors, of type
1329 @Id@. Again these contain their arity but don't need a Unique.
1330
1331 \item
1332 There should be a magic way of generating the info tables and
1333 entry code for all tuples.
1334
1335 But at the moment we just compile a Haskell source
1336 file\srcloc{lib/prelude/...} containing declarations like:
1337 \begin{verbatim}
1338 data Tuple0 = Tup0
1339 data Tuple2 a b = Tup2 a b
1340 data Tuple3 a b c = Tup3 a b c
1341 data Tuple4 a b c d = Tup4 a b c d
1342 ...
1343 \end{verbatim}
1344 The print-names associated with the magic @Id@s for tuple constructors
1345 ``just happen'' to be the same as those generated by these
1346 declarations.
1347
1348 \item
1349 The instance environment should have a magic way to know
1350 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
1351 so on. \ToDo{Not implemented yet.}
1352
1353 \item
1354 There should also be a way to generate the appropriate code for each
1355 of these instances, but (like the info tables and entry code) it is
1356 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
1357 \end{itemize}
1358 -}
1359
1360 -- | Make a tuple type. The list of types should /not/ include any
1361 -- RuntimeRep specifications.
1362 mkTupleTy :: Boxity -> [Type] -> Type
1363 -- Special case for *boxed* 1-tuples, which are represented by the type itself
1364 mkTupleTy Boxed [ty] = ty
1365 mkTupleTy Boxed tys = mkTyConApp (tupleTyCon Boxed (length tys)) tys
1366 mkTupleTy Unboxed tys = mkTyConApp (tupleTyCon Unboxed (length tys))
1367 (map (getRuntimeRep "mkTupleTy") tys ++ tys)
1368
1369 -- | Build the type of a small tuple that holds the specified type of thing
1370 mkBoxedTupleTy :: [Type] -> Type
1371 mkBoxedTupleTy tys = mkTupleTy Boxed tys
1372
1373 unitTy :: Type
1374 unitTy = mkTupleTy Boxed []
1375
1376 {- *********************************************************************
1377 * *
1378 The sum types
1379 * *
1380 ************************************************************************
1381 -}
1382
1383 mkSumTy :: [Type] -> Type
1384 mkSumTy tys = mkTyConApp (sumTyCon (length tys))
1385 (map (getRuntimeRep "mkSumTy") tys ++ tys)
1386
1387 {- *********************************************************************
1388 * *
1389 The parallel-array type, [::]
1390 * *
1391 ************************************************************************
1392
1393 Special syntax for parallel arrays needs some wired in definitions.
1394 -}
1395
1396 -- | Construct a type representing the application of the parallel array constructor
1397 mkPArrTy :: Type -> Type
1398 mkPArrTy ty = mkTyConApp parrTyCon [ty]
1399
1400 -- | Represents the type constructor of parallel arrays
1401 --
1402 -- * This must match the definition in @PrelPArr@
1403 --
1404 -- NB: Although the constructor is given here, it will not be accessible in
1405 -- user code as it is not in the environment of any compiled module except
1406 -- @PrelPArr@.
1407 --
1408 parrTyCon :: TyCon
1409 parrTyCon = pcNonEnumTyCon parrTyConName Nothing alpha_tyvar [parrDataCon]
1410
1411 parrDataCon :: DataCon
1412 parrDataCon = pcDataCon
1413 parrDataConName
1414 alpha_tyvar -- forall'ed type variables
1415 [intTy, -- 1st argument: Int
1416 mkTyConApp -- 2nd argument: Array# a
1417 arrayPrimTyCon
1418 alpha_ty]
1419 parrTyCon
1420
1421 -- | Check whether a type constructor is the constructor for parallel arrays
1422 isPArrTyCon :: TyCon -> Bool
1423 isPArrTyCon tc = tyConName tc == parrTyConName
1424
1425 -- | Fake array constructors
1426 --
1427 -- * These constructors are never really used to represent array values;
1428 -- however, they are very convenient during desugaring (and, in particular,
1429 -- in the pattern matching compiler) to treat array pattern just like
1430 -- yet another constructor pattern
1431 --
1432 parrFakeCon :: Arity -> DataCon
1433 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
1434 parrFakeCon i = parrFakeConArr!i
1435
1436 -- pre-defined set of constructors
1437 --
1438 parrFakeConArr :: Array Int DataCon
1439 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
1440 | i <- [0..mAX_TUPLE_SIZE]]
1441
1442 -- build a fake parallel array constructor for the given arity
1443 --
1444 mkPArrFakeCon :: Int -> DataCon
1445 mkPArrFakeCon arity = data_con
1446 where
1447 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
1448 tyvar = head alphaTyVars
1449 tyvarTys = replicate arity $ mkTyVarTy tyvar
1450 nameStr = mkFastString ("MkPArr" ++ show arity)
1451 name = mkWiredInName gHC_PARR' (mkDataOccFS nameStr) unique
1452 (AConLike (RealDataCon data_con)) UserSyntax
1453 unique = mkPArrDataConUnique arity
1454
1455 -- | Checks whether a data constructor is a fake constructor for parallel arrays
1456 isPArrFakeCon :: DataCon -> Bool
1457 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
1458
1459 -- Promoted Booleans
1460
1461 promotedFalseDataCon, promotedTrueDataCon :: TyCon
1462 promotedTrueDataCon = promoteDataCon trueDataCon
1463 promotedFalseDataCon = promoteDataCon falseDataCon
1464
1465 -- Promoted Maybe
1466 promotedNothingDataCon, promotedJustDataCon :: TyCon
1467 promotedNothingDataCon = promoteDataCon nothingDataCon
1468 promotedJustDataCon = promoteDataCon justDataCon
1469
1470 -- Promoted Ordering
1471
1472 promotedLTDataCon
1473 , promotedEQDataCon
1474 , promotedGTDataCon
1475 :: TyCon
1476 promotedLTDataCon = promoteDataCon ltDataCon
1477 promotedEQDataCon = promoteDataCon eqDataCon
1478 promotedGTDataCon = promoteDataCon gtDataCon
1479
1480 -- Promoted List
1481 promotedConsDataCon, promotedNilDataCon :: TyCon
1482 promotedConsDataCon = promoteDataCon consDataCon
1483 promotedNilDataCon = promoteDataCon nilDataCon