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