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