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