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