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