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