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