Generate Typeable info at definition sites
[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
9 -- | This module is about types that can be defined in Haskell, but which
10 -- must be wired into the compiler nonetheless. C.f module TysPrim
11 module TysWiredIn (
12 -- * All wired in things
13 wiredInTyCons, isBuiltInOcc_maybe,
14
15 -- * Bool
16 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
17 trueDataCon, trueDataConId, true_RDR,
18 falseDataCon, falseDataConId, false_RDR,
19 promotedBoolTyCon, promotedFalseDataCon, promotedTrueDataCon,
20
21 -- * Ordering
22 ltDataCon, ltDataConId,
23 eqDataCon, eqDataConId,
24 gtDataCon, gtDataConId,
25 promotedOrderingTyCon,
26 promotedLTDataCon, promotedEQDataCon, promotedGTDataCon,
27
28 -- * Char
29 charTyCon, charDataCon, charTyCon_RDR,
30 charTy, stringTy, charTyConName,
31
32 -- * Double
33 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
34
35 -- * Float
36 floatTyCon, floatDataCon, floatTy, floatTyConName,
37
38 -- * Int
39 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
40 intTy,
41
42 -- * Word
43 wordTyCon, wordDataCon, wordTyConName, wordTy,
44
45 -- * Word8
46 word8TyCon, word8DataCon, word8TyConName, word8Ty,
47
48 -- * List
49 listTyCon, listTyCon_RDR, listTyConName, listTyConKey,
50 nilDataCon, nilDataConName, nilDataConKey,
51 consDataCon_RDR, consDataCon, consDataConName,
52
53 mkListTy, mkPromotedListTy,
54
55 -- * Maybe
56 maybeTyCon, maybeTyConName,
57 nothingDataCon, nothingDataConName, justDataCon, justDataConName,
58
59 -- * Tuples
60 mkTupleTy, mkBoxedTupleTy,
61 tupleTyCon, tupleDataCon, tupleTyConName,
62 promotedTupleTyCon, promotedTupleDataCon,
63 unitTyCon, unitDataCon, unitDataConId, unitTy, unitTyConKey,
64 pairTyCon,
65 unboxedUnitTyCon, unboxedUnitDataCon,
66 unboxedSingletonTyCon, unboxedSingletonDataCon,
67 unboxedPairTyCon, unboxedPairDataCon,
68 cTupleTyConName, cTupleTyConNames, isCTupleTyConName,
69
70 -- * Kinds
71 typeNatKindCon, typeNatKind, typeSymbolKindCon, typeSymbolKind,
72
73 -- * Parallel arrays
74 mkPArrTy,
75 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
76 parrTyCon_RDR, parrTyConName,
77
78 -- * Equality predicates
79 eqTyCon_RDR, eqTyCon, eqTyConName, eqBoxDataCon,
80 coercibleTyCon, coercibleDataCon, coercibleClass,
81
82 -- * Implicit Parameters
83 ipTyCon, ipDataCon, ipClass,
84
85 callStackTyCon,
86
87 mkWiredInTyConName -- This is used in TcTypeNats to define the
88 -- built-in functions for evaluation.
89 ) where
90
91 #include "HsVersions.h"
92
93 import {-# SOURCE #-} MkId( mkDataConWorkId )
94
95 -- friends:
96 import PrelNames
97 import TysPrim
98
99 -- others:
100 import CoAxiom
101 import Coercion
102 import Id
103 import Constants ( mAX_TUPLE_SIZE, mAX_CTUPLE_SIZE )
104 import Module ( Module )
105 import Type ( mkTyConApp )
106 import DataCon
107 import {-# SOURCE #-} ConLike
108 import Var
109 import TyCon
110 import Class ( Class, mkClass )
111 import TypeRep
112 import RdrName
113 import Name
114 import NameSet ( NameSet, mkNameSet, elemNameSet )
115 import BasicTypes ( Arity, RecFlag(..), Boxity(..),
116 TupleSort(..) )
117 import ForeignCall
118 import SrcLoc ( noSrcSpan )
119 import Unique
120 import Data.Array
121 import FastString
122 import Outputable
123 import Util
124 import BooleanFormula ( mkAnd )
125
126 alpha_tyvar :: [TyVar]
127 alpha_tyvar = [alphaTyVar]
128
129 alpha_ty :: [Type]
130 alpha_ty = [alphaTy]
131
132 {-
133 ************************************************************************
134 * *
135 \subsection{Wired in type constructors}
136 * *
137 ************************************************************************
138
139 If you change which things are wired in, make sure you change their
140 names in PrelNames, so they use wTcQual, wDataQual, etc
141 -}
142
143 -- This list is used only to define PrelInfo.wiredInThings. That in turn
144 -- is used to initialise the name environment carried around by the renamer.
145 -- This means that if we look up the name of a TyCon (or its implicit binders)
146 -- that occurs in this list that name will be assigned the wired-in key we
147 -- define here.
148 --
149 -- Because of their infinite nature, this list excludes tuples, Any and implicit
150 -- parameter TyCons. Instead, we have a hack in lookupOrigNameCache to deal with
151 -- these names.
152 --
153 -- See also Note [Known-key names]
154 wiredInTyCons :: [TyCon]
155
156 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
157 -- it's defined in GHC.Base, and there's only
158 -- one of it. We put it in wiredInTyCons so
159 -- that it'll pre-populate the name cache, so
160 -- the special case in lookupOrigNameCache
161 -- doesn't need to look out for it
162 , boolTyCon
163 , charTyCon
164 , doubleTyCon
165 , floatTyCon
166 , intTyCon
167 , wordTyCon
168 , word8TyCon
169 , listTyCon
170 , maybeTyCon
171 , parrTyCon
172 , eqTyCon
173 , coercibleTyCon
174 , typeNatKindCon
175 , typeSymbolKindCon
176 , ipTyCon
177 ]
178
179 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
180 mkWiredInTyConName built_in modu fs unique tycon
181 = mkWiredInName modu (mkTcOccFS fs) unique
182 (ATyCon tycon) -- Relevant TyCon
183 built_in
184
185 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
186 mkWiredInDataConName built_in modu fs unique datacon
187 = mkWiredInName modu (mkDataOccFS fs) unique
188 (AConLike (RealDataCon datacon)) -- Relevant DataCon
189 built_in
190
191 mkWiredInCoAxiomName :: BuiltInSyntax -> Module -> FastString -> Unique
192 -> CoAxiom Branched -> Name
193 mkWiredInCoAxiomName built_in modu fs unique ax
194 = mkWiredInName modu (mkTcOccFS fs) unique
195 (ACoAxiom ax) -- Relevant CoAxiom
196 built_in
197
198 -- See Note [Kind-changing of (~) and Coercible]
199 eqTyConName, eqBoxDataConName :: Name
200 eqTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "~") eqTyConKey eqTyCon
201 eqBoxDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "Eq#") eqBoxDataConKey eqBoxDataCon
202
203 -- See Note [Kind-changing of (~) and Coercible]
204 coercibleTyConName, coercibleDataConName :: Name
205 coercibleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Coercible") coercibleTyConKey coercibleTyCon
206 coercibleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "MkCoercible") coercibleDataConKey coercibleDataCon
207
208 charTyConName, charDataConName, intTyConName, intDataConName :: Name
209 charTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
210 charDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
211 intTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Int") intTyConKey intTyCon
212 intDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey intDataCon
213
214 boolTyConName, falseDataConName, trueDataConName :: Name
215 boolTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Bool") boolTyConKey boolTyCon
216 falseDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "False") falseDataConKey falseDataCon
217 trueDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "True") trueDataConKey trueDataCon
218
219 listTyConName, nilDataConName, consDataConName :: Name
220 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
221 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
222 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
223
224 maybeTyConName, nothingDataConName, justDataConName :: Name
225 maybeTyConName = mkWiredInTyConName UserSyntax gHC_BASE (fsLit "Maybe")
226 maybeTyConKey maybeTyCon
227 nothingDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "Nothing")
228 nothingDataConKey nothingDataCon
229 justDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "Just")
230 justDataConKey justDataCon
231
232 wordTyConName, wordDataConName, word8TyConName, word8DataConName :: Name
233 wordTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Word") wordTyConKey wordTyCon
234 wordDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "W#") wordDataConKey wordDataCon
235 word8TyConName = mkWiredInTyConName UserSyntax gHC_WORD (fsLit "Word8") word8TyConKey word8TyCon
236 word8DataConName = mkWiredInDataConName UserSyntax gHC_WORD (fsLit "W8#") word8DataConKey word8DataCon
237
238 floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
239 floatTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Float") floatTyConKey floatTyCon
240 floatDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#") floatDataConKey floatDataCon
241 doubleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey doubleTyCon
242 doubleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#") doubleDataConKey doubleDataCon
243
244 -- Kinds
245 typeNatKindConName, typeSymbolKindConName :: Name
246 typeNatKindConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Nat") typeNatKindConNameKey typeNatKindCon
247 typeSymbolKindConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Symbol") typeSymbolKindConNameKey typeSymbolKindCon
248
249 parrTyConName, parrDataConName :: Name
250 parrTyConName = mkWiredInTyConName BuiltInSyntax
251 gHC_PARR' (fsLit "[::]") parrTyConKey parrTyCon
252 parrDataConName = mkWiredInDataConName UserSyntax
253 gHC_PARR' (fsLit "PArr") parrDataConKey parrDataCon
254
255 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
256 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR, eqTyCon_RDR :: RdrName
257 boolTyCon_RDR = nameRdrName boolTyConName
258 false_RDR = nameRdrName falseDataConName
259 true_RDR = nameRdrName trueDataConName
260 intTyCon_RDR = nameRdrName intTyConName
261 charTyCon_RDR = nameRdrName charTyConName
262 intDataCon_RDR = nameRdrName intDataConName
263 listTyCon_RDR = nameRdrName listTyConName
264 consDataCon_RDR = nameRdrName consDataConName
265 parrTyCon_RDR = nameRdrName parrTyConName
266 eqTyCon_RDR = nameRdrName eqTyConName
267
268 {-
269 ************************************************************************
270 * *
271 \subsection{mkWiredInTyCon}
272 * *
273 ************************************************************************
274 -}
275
276 pcNonRecDataTyCon :: Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
277 -- Not an enumeration, not promotable
278 pcNonRecDataTyCon = pcTyCon False NonRecursive False
279
280 -- This function assumes that the types it creates have all parameters at
281 -- Representational role!
282 pcTyCon :: Bool -> RecFlag -> Bool -> Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
283 pcTyCon is_enum is_rec is_prom name cType tyvars cons
284 = buildAlgTyCon name
285 tyvars
286 (map (const Representational) tyvars)
287 cType
288 [] -- No stupid theta
289 (DataTyCon cons is_enum)
290 is_rec
291 is_prom
292 False -- Not in GADT syntax
293 (VanillaAlgTyCon (mkPrelTyConRepName name))
294
295 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
296 pcDataCon = pcDataConWithFixity False
297
298 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
299 pcDataConWithFixity infx n = pcDataConWithFixity' infx n (incrUnique (nameUnique n))
300 -- The Name's unique is the first of two free uniques;
301 -- the first is used for the datacon itself,
302 -- the second is used for the "worker name"
303 --
304 -- To support this the mkPreludeDataConUnique function "allocates"
305 -- one DataCon unique per pair of Ints.
306
307 pcDataConWithFixity' :: Bool -> Name -> Unique -> [TyVar] -> [Type] -> TyCon -> DataCon
308 -- The Name should be in the DataName name space; it's the name
309 -- of the DataCon itself.
310
311 pcDataConWithFixity' declared_infix dc_name wrk_key tyvars arg_tys tycon
312 = data_con
313 where
314 data_con = mkDataCon dc_name declared_infix prom_info
315 (map (const no_bang) arg_tys)
316 [] -- No labelled fields
317 tyvars
318 [] -- No existential type variables
319 [] -- No equality spec
320 [] -- No theta
321 arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
322 tycon
323 [] -- No stupid theta
324 (mkDataConWorkId wrk_name data_con)
325 NoDataConRep -- Wired-in types are too simple to need wrappers
326
327 no_bang = HsSrcBang Nothing NoSrcUnpack NoSrcStrict
328
329 modu = ASSERT( isExternalName dc_name )
330 nameModule dc_name
331 dc_occ = nameOccName dc_name
332 wrk_occ = mkDataConWorkerOcc dc_occ
333 wrk_name = mkWiredInName modu wrk_occ wrk_key
334 (AnId (dataConWorkId data_con)) UserSyntax
335
336 prom_info | Promoted {} <- promotableTyCon_maybe tycon -- Knot-tied
337 = Promoted (mkPrelTyConRepName dc_name)
338 | otherwise
339 = NotPromoted
340
341 {-
342 ************************************************************************
343 * *
344 Kinds
345 * *
346 ************************************************************************
347 -}
348
349 typeNatKindCon, typeSymbolKindCon :: TyCon
350 -- data Nat
351 -- data Symbol
352 typeNatKindCon = pcTyCon False NonRecursive True typeNatKindConName Nothing [] []
353 typeSymbolKindCon = pcTyCon False NonRecursive True typeSymbolKindConName Nothing [] []
354
355 typeNatKind, typeSymbolKind :: Kind
356 typeNatKind = TyConApp (promoteTyCon typeNatKindCon) []
357 typeSymbolKind = TyConApp (promoteTyCon typeSymbolKindCon) []
358
359 {-
360 ************************************************************************
361 * *
362 Stuff for dealing with tuples
363 * *
364 ************************************************************************
365
366 Note [How tuples work] See also Note [Known-key names] in PrelNames
367 ~~~~~~~~~~~~~~~~~~~~~~
368 * There are three families of tuple TyCons and corresponding
369 DataCons, expressed by the type BasicTypes.TupleSort:
370 data TupleSort = BoxedTuple | UnboxedTuple | ConstraintTuple
371
372 * All three families are AlgTyCons, whose AlgTyConRhs is TupleTyCon
373
374 * BoxedTuples
375 - A wired-in type
376 - Data type declarations in GHC.Tuple
377 - The data constructors really have an info table
378
379 * UnboxedTuples
380 - A wired-in type
381 - Have a pretend DataCon, defined in GHC.Prim,
382 but no actual declaration and no info table
383
384 * ConstraintTuples
385 - Are known-key rather than wired-in. Reason: it's awkward to
386 have all the superclass selectors wired-in.
387 - Declared as classes in GHC.Classes, e.g.
388 class (c1,c2) => (c1,c2)
389 - Given constraints: the superclasses automatically become available
390 - Wanted constraints: there is a built-in instance
391 instance (c1,c2) => (c1,c2)
392 - Currently just go up to 16; beyond that
393 you have to use manual nesting
394 - Their OccNames look like (%,,,%), so they can easily be
395 distinguished from term tuples. But (following Haskell) we
396 pretty-print saturated constraint tuples with round parens; see
397 BasicTypes.tupleParens.
398
399 * In quite a lot of places things are restrcted just to
400 BoxedTuple/UnboxedTuple, and then we used BasicTypes.Boxity to distinguish
401 E.g. tupleTyCon has a Boxity argument
402
403 * When looking up an OccName in the original-name cache
404 (IfaceEnv.lookupOrigNameCache), we spot the tuple OccName to make sure
405 we get the right wired-in name. This guy can't tell the difference
406 betweeen BoxedTuple and ConstraintTuple (same OccName!), so tuples
407 are not serialised into interface files using OccNames at all.
408 -}
409
410 isBuiltInOcc_maybe :: OccName -> Maybe Name
411 -- Built in syntax isn't "in scope" so these OccNames
412 -- map to wired-in Names with BuiltInSyntax
413 isBuiltInOcc_maybe occ
414 = case occNameString occ of
415 "[]" -> choose_ns listTyConName nilDataConName
416 ":" -> Just consDataConName
417 "[::]" -> Just parrTyConName
418 "()" -> tup_name Boxed 0
419 "(##)" -> tup_name Unboxed 0
420 '(':',':rest -> parse_tuple Boxed 2 rest
421 '(':'#':',':rest -> parse_tuple Unboxed 2 rest
422 _other -> Nothing
423 where
424 ns = occNameSpace occ
425
426 parse_tuple sort n rest
427 | (',' : rest2) <- rest = parse_tuple sort (n+1) rest2
428 | tail_matches sort rest = tup_name sort n
429 | otherwise = Nothing
430
431 tail_matches Boxed ")" = True
432 tail_matches Unboxed "#)" = True
433 tail_matches _ _ = False
434
435 tup_name boxity arity
436 = choose_ns (getName (tupleTyCon boxity arity))
437 (getName (tupleDataCon boxity arity))
438
439 choose_ns tc dc
440 | isTcClsNameSpace ns = Just tc
441 | isDataConNameSpace ns = Just dc
442 | otherwise = pprPanic "tup_name" (ppr occ)
443
444 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
445 mkTupleOcc ns sort ar = mkOccName ns str
446 where
447 -- No need to cache these, the caching is done in mk_tuple
448 str = case sort of
449 Unboxed -> '(' : '#' : commas ++ "#)"
450 Boxed -> '(' : commas ++ ")"
451
452 commas = take (ar-1) (repeat ',')
453
454 mkCTupleOcc :: NameSpace -> Arity -> OccName
455 mkCTupleOcc ns ar = mkOccName ns str
456 where
457 str = "(%" ++ commas ++ "%)"
458 commas = take (ar-1) (repeat ',')
459
460 cTupleTyConName :: Arity -> Name
461 cTupleTyConName arity
462 = mkExternalName (mkCTupleTyConUnique arity) gHC_CLASSES
463 (mkCTupleOcc tcName arity) noSrcSpan
464 -- The corresponding DataCon does not have a known-key name
465
466 cTupleTyConNames :: [Name]
467 cTupleTyConNames = map cTupleTyConName (0 : [2..mAX_CTUPLE_SIZE])
468
469 cTupleTyConNameSet :: NameSet
470 cTupleTyConNameSet = mkNameSet cTupleTyConNames
471
472 isCTupleTyConName :: Name -> Bool
473 isCTupleTyConName n
474 = ASSERT2( isExternalName n, ppr n )
475 nameModule n == gHC_CLASSES
476 && n `elemNameSet` cTupleTyConNameSet
477
478 tupleTyCon :: Boxity -> Arity -> TyCon
479 tupleTyCon sort i | i > mAX_TUPLE_SIZE = fst (mk_tuple sort i) -- Build one specially
480 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
481 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
482
483 tupleTyConName :: TupleSort -> Arity -> Name
484 tupleTyConName ConstraintTuple a = cTupleTyConName a
485 tupleTyConName BoxedTuple a = tyConName (tupleTyCon Boxed a)
486 tupleTyConName UnboxedTuple a = tyConName (tupleTyCon Unboxed a)
487
488 promotedTupleTyCon :: Boxity -> Arity -> TyCon
489 promotedTupleTyCon boxity i = promoteTyCon (tupleTyCon boxity i)
490
491 promotedTupleDataCon :: Boxity -> Arity -> TyCon
492 promotedTupleDataCon boxity i = promoteDataCon (tupleDataCon boxity i)
493
494 tupleDataCon :: Boxity -> Arity -> DataCon
495 tupleDataCon sort i | i > mAX_TUPLE_SIZE = snd (mk_tuple sort i) -- Build one specially
496 tupleDataCon Boxed i = snd (boxedTupleArr ! i)
497 tupleDataCon Unboxed i = snd (unboxedTupleArr ! i)
498
499 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
500 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
501 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
502
503 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
504 mk_tuple boxity arity = (tycon, tuple_con)
505 where
506 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con
507 tup_sort
508 prom_tc flavour
509
510 flavour = case boxity of
511 Boxed -> VanillaAlgTyCon (mkPrelTyConRepName tc_name)
512 Unboxed -> UnboxedAlgTyCon
513
514 tup_sort = case boxity of
515 Boxed -> BoxedTuple
516 Unboxed -> UnboxedTuple
517
518 prom_tc = case boxity of
519 Boxed -> Promoted (mkPromotedTyCon tycon (promoteKind tc_kind))
520 Unboxed -> NotPromoted
521
522 modu = case boxity of
523 Boxed -> gHC_TUPLE
524 Unboxed -> gHC_PRIM
525
526 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
527 (ATyCon tycon) BuiltInSyntax
528 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
529
530 res_kind = case boxity of
531 Boxed -> liftedTypeKind
532 Unboxed -> unliftedTypeKind
533
534 tyvars = take arity $ case boxity of
535 Boxed -> alphaTyVars
536 Unboxed -> openAlphaTyVars
537
538 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
539 tyvar_tys = mkTyVarTys tyvars
540 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
541 (AConLike (RealDataCon tuple_con)) BuiltInSyntax
542 tc_uniq = mkTupleTyConUnique boxity arity
543 dc_uniq = mkTupleDataConUnique boxity arity
544
545 unitTyCon :: TyCon
546 unitTyCon = tupleTyCon Boxed 0
547
548 unitTyConKey :: Unique
549 unitTyConKey = getUnique unitTyCon
550
551 unitDataCon :: DataCon
552 unitDataCon = head (tyConDataCons unitTyCon)
553
554 unitDataConId :: Id
555 unitDataConId = dataConWorkId unitDataCon
556
557 pairTyCon :: TyCon
558 pairTyCon = tupleTyCon Boxed 2
559
560 unboxedUnitTyCon :: TyCon
561 unboxedUnitTyCon = tupleTyCon Unboxed 0
562
563 unboxedUnitDataCon :: DataCon
564 unboxedUnitDataCon = tupleDataCon Unboxed 0
565
566 unboxedSingletonTyCon :: TyCon
567 unboxedSingletonTyCon = tupleTyCon Unboxed 1
568
569 unboxedSingletonDataCon :: DataCon
570 unboxedSingletonDataCon = tupleDataCon Unboxed 1
571
572 unboxedPairTyCon :: TyCon
573 unboxedPairTyCon = tupleTyCon Unboxed 2
574
575 unboxedPairDataCon :: DataCon
576 unboxedPairDataCon = tupleDataCon Unboxed 2
577
578 {-
579 ************************************************************************
580 * *
581 The ``boxed primitive'' types (@Char@, @Int@, etc)
582 * *
583 ************************************************************************
584 -}
585
586 charTy :: Type
587 charTy = mkTyConTy charTyCon
588
589 charTyCon :: TyCon
590 charTyCon = pcNonRecDataTyCon charTyConName
591 (Just (CType "" Nothing ("HsChar",fsLit "HsChar")))
592 [] [charDataCon]
593 charDataCon :: DataCon
594 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
595
596 stringTy :: Type
597 stringTy = mkListTy charTy -- convenience only
598
599 intTy :: Type
600 intTy = mkTyConTy intTyCon
601
602 intTyCon :: TyCon
603 intTyCon = pcNonRecDataTyCon intTyConName
604 (Just (CType "" Nothing ("HsInt",fsLit "HsInt"))) []
605 [intDataCon]
606 intDataCon :: DataCon
607 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
608
609 wordTy :: Type
610 wordTy = mkTyConTy wordTyCon
611
612 wordTyCon :: TyCon
613 wordTyCon = pcNonRecDataTyCon wordTyConName
614 (Just (CType "" Nothing ("HsWord", fsLit "HsWord"))) []
615 [wordDataCon]
616 wordDataCon :: DataCon
617 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
618
619 word8Ty :: Type
620 word8Ty = mkTyConTy word8TyCon
621
622 word8TyCon :: TyCon
623 word8TyCon = pcNonRecDataTyCon word8TyConName
624 (Just (CType "" Nothing ("HsWord8", fsLit "HsWord8"))) []
625 [word8DataCon]
626 word8DataCon :: DataCon
627 word8DataCon = pcDataCon word8DataConName [] [wordPrimTy] word8TyCon
628
629 floatTy :: Type
630 floatTy = mkTyConTy floatTyCon
631
632 floatTyCon :: TyCon
633 floatTyCon = pcNonRecDataTyCon floatTyConName
634 (Just (CType "" Nothing ("HsFloat", fsLit "HsFloat"))) []
635 [floatDataCon]
636 floatDataCon :: DataCon
637 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
638
639 doubleTy :: Type
640 doubleTy = mkTyConTy doubleTyCon
641
642 doubleTyCon :: TyCon
643 doubleTyCon = pcNonRecDataTyCon doubleTyConName
644 (Just (CType "" Nothing ("HsDouble",fsLit "HsDouble"))) []
645 [doubleDataCon]
646
647 doubleDataCon :: DataCon
648 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
649
650 {-
651 ************************************************************************
652 * *
653 The Bool type
654 * *
655 ************************************************************************
656
657 An ordinary enumeration type, but deeply wired in. There are no
658 magical operations on @Bool@ (just the regular Prelude code).
659
660 {\em BEGIN IDLE SPECULATION BY SIMON}
661
662 This is not the only way to encode @Bool@. A more obvious coding makes
663 @Bool@ just a boxed up version of @Bool#@, like this:
664 \begin{verbatim}
665 type Bool# = Int#
666 data Bool = MkBool Bool#
667 \end{verbatim}
668
669 Unfortunately, this doesn't correspond to what the Report says @Bool@
670 looks like! Furthermore, we get slightly less efficient code (I
671 think) with this coding. @gtInt@ would look like this:
672
673 \begin{verbatim}
674 gtInt :: Int -> Int -> Bool
675 gtInt x y = case x of I# x# ->
676 case y of I# y# ->
677 case (gtIntPrim x# y#) of
678 b# -> MkBool b#
679 \end{verbatim}
680
681 Notice that the result of the @gtIntPrim@ comparison has to be turned
682 into an integer (here called @b#@), and returned in a @MkBool@ box.
683
684 The @if@ expression would compile to this:
685 \begin{verbatim}
686 case (gtInt x y) of
687 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
688 \end{verbatim}
689
690 I think this code is a little less efficient than the previous code,
691 but I'm not certain. At all events, corresponding with the Report is
692 important. The interesting thing is that the language is expressive
693 enough to describe more than one alternative; and that a type doesn't
694 necessarily need to be a straightforwardly boxed version of its
695 primitive counterpart.
696
697 {\em END IDLE SPECULATION BY SIMON}
698 -}
699
700 boolTy :: Type
701 boolTy = mkTyConTy boolTyCon
702
703 boolTyCon :: TyCon
704 boolTyCon = pcTyCon True NonRecursive True boolTyConName
705 (Just (CType "" Nothing ("HsBool", fsLit "HsBool")))
706 [] [falseDataCon, trueDataCon]
707
708 falseDataCon, trueDataCon :: DataCon
709 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
710 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
711
712 falseDataConId, trueDataConId :: Id
713 falseDataConId = dataConWorkId falseDataCon
714 trueDataConId = dataConWorkId trueDataCon
715
716 orderingTyCon :: TyCon
717 orderingTyCon = pcTyCon True NonRecursive True orderingTyConName Nothing
718 [] [ltDataCon, eqDataCon, gtDataCon]
719
720 ltDataCon, eqDataCon, gtDataCon :: DataCon
721 ltDataCon = pcDataCon ltDataConName [] [] orderingTyCon
722 eqDataCon = pcDataCon eqDataConName [] [] orderingTyCon
723 gtDataCon = pcDataCon gtDataConName [] [] orderingTyCon
724
725 ltDataConId, eqDataConId, gtDataConId :: Id
726 ltDataConId = dataConWorkId ltDataCon
727 eqDataConId = dataConWorkId eqDataCon
728 gtDataConId = dataConWorkId gtDataCon
729
730 {-
731 ************************************************************************
732 * *
733 The List type
734 Special syntax, deeply wired in,
735 but otherwise an ordinary algebraic data type
736 * *
737 ************************************************************************
738
739 data [] a = [] | a : (List a)
740 -}
741
742 mkListTy :: Type -> Type
743 mkListTy ty = mkTyConApp listTyCon [ty]
744
745 listTyCon :: TyCon
746 listTyCon = buildAlgTyCon listTyConName alpha_tyvar [Representational]
747 Nothing []
748 (DataTyCon [nilDataCon, consDataCon] False )
749 Recursive True False
750 (VanillaAlgTyCon (mkSpecialTyConRepName (fsLit "tcList") listTyConName))
751
752 mkPromotedListTy :: Type -> Type
753 mkPromotedListTy ty = mkTyConApp promotedListTyCon [ty]
754
755 promotedListTyCon :: TyCon
756 promotedListTyCon = promoteTyCon listTyCon
757
758 nilDataCon :: DataCon
759 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
760
761 consDataCon :: DataCon
762 consDataCon = pcDataConWithFixity True {- Declared infix -}
763 consDataConName
764 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
765 -- Interesting: polymorphic recursion would help here.
766 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
767 -- gets the over-specific type (Type -> Type)
768
769 -- Wired-in type Maybe
770
771 maybeTyCon :: TyCon
772 maybeTyCon = pcTyCon True NonRecursive True maybeTyConName Nothing alpha_tyvar
773 [nothingDataCon, justDataCon]
774
775 nothingDataCon :: DataCon
776 nothingDataCon = pcDataCon nothingDataConName alpha_tyvar [] maybeTyCon
777
778 justDataCon :: DataCon
779 justDataCon = pcDataCon justDataConName alpha_tyvar [alphaTy] maybeTyCon
780
781 {-
782 ** *********************************************************************
783 * *
784 The tuple types
785 * *
786 ************************************************************************
787
788 The tuple types are definitely magic, because they form an infinite
789 family.
790
791 \begin{itemize}
792 \item
793 They have a special family of type constructors, of type @TyCon@
794 These contain the tycon arity, but don't require a Unique.
795
796 \item
797 They have a special family of constructors, of type
798 @Id@. Again these contain their arity but don't need a Unique.
799
800 \item
801 There should be a magic way of generating the info tables and
802 entry code for all tuples.
803
804 But at the moment we just compile a Haskell source
805 file\srcloc{lib/prelude/...} containing declarations like:
806 \begin{verbatim}
807 data Tuple0 = Tup0
808 data Tuple2 a b = Tup2 a b
809 data Tuple3 a b c = Tup3 a b c
810 data Tuple4 a b c d = Tup4 a b c d
811 ...
812 \end{verbatim}
813 The print-names associated with the magic @Id@s for tuple constructors
814 ``just happen'' to be the same as those generated by these
815 declarations.
816
817 \item
818 The instance environment should have a magic way to know
819 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
820 so on. \ToDo{Not implemented yet.}
821
822 \item
823 There should also be a way to generate the appropriate code for each
824 of these instances, but (like the info tables and entry code) it is
825 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
826 \end{itemize}
827 -}
828
829 mkTupleTy :: Boxity -> [Type] -> Type
830 -- Special case for *boxed* 1-tuples, which are represented by the type itself
831 mkTupleTy Boxed [ty] = ty
832 mkTupleTy boxity tys = mkTyConApp (tupleTyCon boxity (length tys)) tys
833
834 -- | Build the type of a small tuple that holds the specified type of thing
835 mkBoxedTupleTy :: [Type] -> Type
836 mkBoxedTupleTy tys = mkTupleTy Boxed tys
837
838 unitTy :: Type
839 unitTy = mkTupleTy Boxed []
840
841
842 {- *********************************************************************
843 * *
844 The parallel-array type, [::]
845 * *
846 ************************************************************************
847
848 Special syntax for parallel arrays needs some wired in definitions.
849 -}
850
851 -- | Construct a type representing the application of the parallel array constructor
852 mkPArrTy :: Type -> Type
853 mkPArrTy ty = mkTyConApp parrTyCon [ty]
854
855 -- | Represents the type constructor of parallel arrays
856 --
857 -- * This must match the definition in @PrelPArr@
858 --
859 -- NB: Although the constructor is given here, it will not be accessible in
860 -- user code as it is not in the environment of any compiled module except
861 -- @PrelPArr@.
862 --
863 parrTyCon :: TyCon
864 parrTyCon = pcNonRecDataTyCon parrTyConName Nothing alpha_tyvar [parrDataCon]
865
866 parrDataCon :: DataCon
867 parrDataCon = pcDataCon
868 parrDataConName
869 alpha_tyvar -- forall'ed type variables
870 [intTy, -- 1st argument: Int
871 mkTyConApp -- 2nd argument: Array# a
872 arrayPrimTyCon
873 alpha_ty]
874 parrTyCon
875
876 -- | Check whether a type constructor is the constructor for parallel arrays
877 isPArrTyCon :: TyCon -> Bool
878 isPArrTyCon tc = tyConName tc == parrTyConName
879
880 -- | Fake array constructors
881 --
882 -- * These constructors are never really used to represent array values;
883 -- however, they are very convenient during desugaring (and, in particular,
884 -- in the pattern matching compiler) to treat array pattern just like
885 -- yet another constructor pattern
886 --
887 parrFakeCon :: Arity -> DataCon
888 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
889 parrFakeCon i = parrFakeConArr!i
890
891 -- pre-defined set of constructors
892 --
893 parrFakeConArr :: Array Int DataCon
894 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
895 | i <- [0..mAX_TUPLE_SIZE]]
896
897 -- build a fake parallel array constructor for the given arity
898 --
899 mkPArrFakeCon :: Int -> DataCon
900 mkPArrFakeCon arity = data_con
901 where
902 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
903 tyvar = head alphaTyVars
904 tyvarTys = replicate arity $ mkTyVarTy tyvar
905 nameStr = mkFastString ("MkPArr" ++ show arity)
906 name = mkWiredInName gHC_PARR' (mkDataOccFS nameStr) unique
907 (AConLike (RealDataCon data_con)) UserSyntax
908 unique = mkPArrDataConUnique arity
909
910 -- | Checks whether a data constructor is a fake constructor for parallel arrays
911 isPArrFakeCon :: DataCon -> Bool
912 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
913
914 -- Promoted Booleans
915
916 promotedBoolTyCon, promotedFalseDataCon, promotedTrueDataCon :: TyCon
917 promotedBoolTyCon = promoteTyCon boolTyCon
918 promotedTrueDataCon = promoteDataCon trueDataCon
919 promotedFalseDataCon = promoteDataCon falseDataCon
920
921 -- Promoted Ordering
922
923 promotedOrderingTyCon
924 , promotedLTDataCon
925 , promotedEQDataCon
926 , promotedGTDataCon
927 :: TyCon
928 promotedOrderingTyCon = promoteTyCon orderingTyCon
929 promotedLTDataCon = promoteDataCon ltDataCon
930 promotedEQDataCon = promoteDataCon eqDataCon
931 promotedGTDataCon = promoteDataCon gtDataCon
932
933 {- *********************************************************************
934 * *
935 Type equalities
936 * *
937 ********************************************************************* -}
938
939 eqTyCon :: TyCon
940 eqTyCon = mkAlgTyCon eqTyConName
941 (ForAllTy kv $ mkArrowKinds [k, k] constraintKind)
942 [kv, a, b]
943 [Nominal, Nominal, Nominal]
944 Nothing
945 [] -- No stupid theta
946 (DataTyCon [eqBoxDataCon] False)
947 (VanillaAlgTyCon (mkSpecialTyConRepName (fsLit "tcEq") eqTyConName))
948 NonRecursive
949 False
950 NotPromoted
951 where
952 kv = kKiVar
953 k = mkTyVarTy kv
954 [a,b] = mkTemplateTyVars [k,k]
955
956 eqBoxDataCon :: DataCon
957 eqBoxDataCon = pcDataCon eqBoxDataConName args [TyConApp eqPrimTyCon (map mkTyVarTy args)] eqTyCon
958 where
959 kv = kKiVar
960 k = mkTyVarTy kv
961 [a,b] = mkTemplateTyVars [k,k]
962 args = [kv, a, b]
963
964
965 coercibleTyCon :: TyCon
966 coercibleTyCon = mkClassTyCon coercibleTyConName kind tvs
967 [Nominal, Representational, Representational]
968 rhs coercibleClass NonRecursive
969 (mkPrelTyConRepName coercibleTyConName)
970 where
971 kind = (ForAllTy kv $ mkArrowKinds [k, k] constraintKind)
972 kv = kKiVar
973 k = mkTyVarTy kv
974 [a,b] = mkTemplateTyVars [k,k]
975 tvs = [kv, a, b]
976 rhs = DataTyCon [coercibleDataCon] False
977
978 coercibleDataCon :: DataCon
979 coercibleDataCon = pcDataCon coercibleDataConName args [TyConApp eqReprPrimTyCon (map mkTyVarTy args)] coercibleTyCon
980 where
981 kv = kKiVar
982 k = mkTyVarTy kv
983 [a,b] = mkTemplateTyVars [k,k]
984 args = [kv, a, b]
985
986 coercibleClass :: Class
987 coercibleClass = mkClass (tyConTyVars coercibleTyCon) [] [] [] [] [] (mkAnd []) coercibleTyCon
988
989 {-
990 Note [The Implicit Parameter class]
991
992 Implicit parameters `?x :: a` are desugared into dictionaries for the
993 class `IP "x" a`, which is defined (in GHC.Classes) as
994
995 class IP (x :: Symbol) a | x -> a
996
997 This class is wired-in so that `error` and `undefined`, which have
998 wired-in types, can use the implicit-call-stack feature to provide
999 a call-stack alongside the error message.
1000 -}
1001
1002 ipDataConName, ipTyConName, ipCoName :: Name
1003 ipDataConName = mkWiredInDataConName UserSyntax gHC_CLASSES (fsLit "IP")
1004 ipDataConKey ipDataCon
1005 ipTyConName = mkWiredInTyConName UserSyntax gHC_CLASSES (fsLit "IP")
1006 ipTyConKey ipTyCon
1007 ipCoName = mkWiredInCoAxiomName BuiltInSyntax gHC_CLASSES (fsLit "NTCo:IP")
1008 ipCoNameKey (toBranchedAxiom ipCoAxiom)
1009
1010 -- See Note [The Implicit Parameter class]
1011 ipTyCon :: TyCon
1012 ipTyCon = mkClassTyCon ipTyConName kind [ip,a] [] rhs ipClass NonRecursive
1013 (mkPrelTyConRepName ipTyConName)
1014 where
1015 kind = mkArrowKinds [typeSymbolKind, liftedTypeKind] constraintKind
1016 [ip,a] = mkTemplateTyVars [typeSymbolKind, liftedTypeKind]
1017 rhs = NewTyCon ipDataCon (mkTyVarTy a) ([], mkTyVarTy a) ipCoAxiom
1018
1019 ipCoAxiom :: CoAxiom Unbranched
1020 ipCoAxiom = mkNewTypeCo ipCoName ipTyCon [ip,a] [Nominal, Nominal] (mkTyVarTy a)
1021 where
1022 [ip,a] = mkTemplateTyVars [typeSymbolKind, liftedTypeKind]
1023
1024 ipDataCon :: DataCon
1025 ipDataCon = pcDataCon ipDataConName [ip,a] ts ipTyCon
1026 where
1027 [ip,a] = mkTemplateTyVars [typeSymbolKind, liftedTypeKind]
1028 ts = [mkTyVarTy a]
1029
1030 ipClass :: Class
1031 ipClass = mkClass (tyConTyVars ipTyCon) [([ip], [a])] [] [] [] [] (mkAnd [])
1032 ipTyCon
1033 where
1034 [ip, a] = tyConTyVars ipTyCon
1035
1036 -- this is a fake version of the CallStack TyCon so we can refer to it
1037 -- in MkCore.errorTy
1038 callStackTyCon :: TyCon
1039 callStackTyCon = pcNonRecDataTyCon callStackTyConName Nothing [] []