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