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