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