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