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