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