2 % (c) The GRASP Project, Glasgow University, 1994-1998
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
6 This module tracks the ``state interface'' document, ``GHC prelude:
7 types and operations.''
10 -- | This module is about types that can be defined in Haskell, but which
11 -- must be wired into the compiler nonetheless.
13 -- * All wired in things
17 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
18 trueDataCon, trueDataConId, true_RDR,
19 falseDataCon, falseDataConId, false_RDR,
22 charTyCon, charDataCon, charTyCon_RDR,
23 charTy, stringTy, charTyConName,
26 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
29 floatTyCon, floatDataCon, floatTy, floatTyConName,
32 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
36 wordTyCon, wordDataCon, wordTyConName, wordTy,
39 listTyCon, nilDataCon, consDataCon,
40 listTyCon_RDR, consDataCon_RDR, listTyConName,
46 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
47 unboxedSingletonTyCon, unboxedSingletonDataCon,
48 unboxedPairTyCon, unboxedPairDataCon,
55 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
56 parrTyCon_RDR, parrTyConName
59 #include "HsVersions.h"
61 import {-# SOURCE #-} MkId( mkDataConIds )
68 import Constants ( mAX_TUPLE_SIZE )
69 import Module ( Module )
72 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
74 import TyCon ( TyCon, AlgTyConRhs(DataTyCon), tyConDataCons,
75 mkTupleTyCon, mkAlgTyCon, tyConName,
76 TyConParent(NoParentTyCon) )
78 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed,
81 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
83 import Coercion ( unsafeCoercionTyCon, symCoercionTyCon,
84 transCoercionTyCon, leftCoercionTyCon,
85 rightCoercionTyCon, instCoercionTyCon )
86 import TypeRep ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
87 import Unique ( incrUnique, mkTupleTyConUnique,
88 mkTupleDataConUnique, mkPArrDataConUnique )
93 alpha_tyvar :: [TyVar]
94 alpha_tyvar = [alphaTyVar]
101 %************************************************************************
103 \subsection{Wired in type constructors}
105 %************************************************************************
107 If you change which things are wired in, make sure you change their
108 names in PrelNames, so they use wTcQual, wDataQual, etc
111 wiredInTyCons :: [TyCon] -- Excludes tuples
112 -- This list is used only to define PrelInfo.wiredInThings
114 -- It does not need to include kind constructors, because
115 -- all that wiredInThings does is to initialise the Name table,
116 -- and kind constructors don't appear in source code.
118 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
119 -- it's defined in GHC.Base, and there's only
120 -- one of it. We put it in wiredInTyCons so
121 -- that it'll pre-populate the name cache, so
122 -- the special case in lookupOrigNameCache
123 -- doesn't need to look out for it
131 , unsafeCoercionTyCon
141 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
142 mkWiredInTyConName built_in modu fs unique tycon
143 = mkWiredInName modu (mkTcOccFS fs) unique
144 (ATyCon tycon) -- Relevant TyCon
147 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
148 mkWiredInDataConName built_in modu fs unique datacon
149 = mkWiredInName modu (mkDataOccFS fs) unique
150 (ADataCon datacon) -- Relevant DataCon
153 charTyConName, charDataConName, intTyConName, intDataConName :: Name
154 charTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
155 charDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
156 intTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Int") intTyConKey intTyCon
157 intDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey intDataCon
159 boolTyConName, falseDataConName, trueDataConName :: Name
160 boolTyConName = mkWiredInTyConName UserSyntax gHC_BOOL (fsLit "Bool") boolTyConKey boolTyCon
161 falseDataConName = mkWiredInDataConName UserSyntax gHC_BOOL (fsLit "False") falseDataConKey falseDataCon
162 trueDataConName = mkWiredInDataConName UserSyntax gHC_BOOL (fsLit "True") trueDataConKey trueDataCon
164 listTyConName, nilDataConName, consDataConName :: Name
165 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
166 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
167 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
169 floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
170 floatTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Float") floatTyConKey floatTyCon
171 floatDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#") floatDataConKey floatDataCon
172 doubleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey doubleTyCon
173 doubleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#") doubleDataConKey doubleDataCon
175 parrTyConName, parrDataConName :: Name
176 parrTyConName = mkWiredInTyConName BuiltInSyntax gHC_PARR (fsLit "[::]") parrTyConKey parrTyCon
177 parrDataConName = mkWiredInDataConName UserSyntax gHC_PARR (fsLit "PArr") parrDataConKey parrDataCon
179 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
180 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR:: RdrName
181 boolTyCon_RDR = nameRdrName boolTyConName
182 false_RDR = nameRdrName falseDataConName
183 true_RDR = nameRdrName trueDataConName
184 intTyCon_RDR = nameRdrName intTyConName
185 charTyCon_RDR = nameRdrName charTyConName
186 intDataCon_RDR = nameRdrName intDataConName
187 listTyCon_RDR = nameRdrName listTyConName
188 consDataCon_RDR = nameRdrName consDataConName
189 parrTyCon_RDR = nameRdrName parrTyConName
191 tySuperKindTyCon_RDR = nameRdrName tySuperKindTyConName
192 coSuperKindTyCon_RDR = nameRdrName coSuperKindTyConName
193 liftedTypeKindTyCon_RDR = nameRdrName liftedTypeKindTyConName
194 openTypeKindTyCon_RDR = nameRdrName openTypeKindTyConName
195 unliftedTypeKindTyCon_RDR = nameRdrName unliftedTypeKindTyConName
196 ubxTupleKindTyCon_RDR = nameRdrName ubxTupleKindTyConName
197 argTypeKindTyCon_RDR = nameRdrName argTypeKindTyConName
198 funKindTyCon_RDR = nameRdrName funKindTyConName
203 %************************************************************************
205 \subsection{mkWiredInTyCon}
207 %************************************************************************
210 pcNonRecDataTyCon :: Name -> [TyVar] -> [DataCon] -> TyCon
211 pcNonRecDataTyCon = pcTyCon False NonRecursive
212 pcRecDataTyCon :: Name -> [TyVar] -> [DataCon] -> TyCon
213 pcRecDataTyCon = pcTyCon False Recursive
215 pcTyCon :: Bool -> RecFlag -> Name -> [TyVar] -> [DataCon] -> TyCon
216 pcTyCon is_enum is_rec name tyvars cons
219 tycon = mkAlgTyCon name
220 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
222 [] -- No stupid theta
223 (DataTyCon cons is_enum)
226 True -- All the wired-in tycons have generics
227 False -- Not in GADT syntax
229 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
230 pcDataCon = pcDataConWithFixity False
232 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
233 -- The Name should be in the DataName name space; it's the name
234 -- of the DataCon itself.
236 -- The unique is the first of two free uniques;
237 -- the first is used for the datacon itself,
238 -- the second is used for the "worker name"
240 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
243 data_con = mkDataCon dc_name declared_infix
244 (map (const NotMarkedStrict) arg_tys)
245 [] -- No labelled fields
247 [] -- No existential type variables
248 [] -- No equality spec
250 arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
252 [] -- No stupid theta
253 (mkDataConIds bogus_wrap_name wrk_name data_con)
256 modu = ASSERT( isExternalName dc_name )
258 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
259 wrk_key = incrUnique (nameUnique dc_name)
260 wrk_name = mkWiredInName modu wrk_occ wrk_key
261 (AnId (dataConWorkId data_con)) UserSyntax
262 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
263 -- Wired-in types are too simple to need wrappers
267 %************************************************************************
269 \subsection[TysWiredIn-tuples]{The tuple types}
271 %************************************************************************
274 tupleTyCon :: Boxity -> Arity -> TyCon
275 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
276 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
277 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
279 tupleCon :: Boxity -> Arity -> DataCon
280 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
281 tupleCon Boxed i = snd (boxedTupleArr ! i)
282 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
284 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
285 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
286 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
288 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
289 mk_tuple boxity arity = (tycon, tuple_con)
291 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
292 modu = mkTupleModule boxity arity
293 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
294 (ATyCon tycon) BuiltInSyntax
295 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
296 res_kind | isBoxed boxity = liftedTypeKind
297 | otherwise = ubxTupleKind
299 tyvars | isBoxed boxity = take arity alphaTyVars
300 | otherwise = take arity openAlphaTyVars
302 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
303 tyvar_tys = mkTyVarTys tyvars
304 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
305 (ADataCon tuple_con) BuiltInSyntax
306 tc_uniq = mkTupleTyConUnique boxity arity
307 dc_uniq = mkTupleDataConUnique boxity arity
308 gen_info = True -- Tuples all have generics..
309 -- hmm: that's a *lot* of code
312 unitTyCon = tupleTyCon Boxed 0
313 unitDataCon :: DataCon
314 unitDataCon = head (tyConDataCons unitTyCon)
316 unitDataConId = dataConWorkId unitDataCon
319 pairTyCon = tupleTyCon Boxed 2
321 unboxedSingletonTyCon :: TyCon
322 unboxedSingletonTyCon = tupleTyCon Unboxed 1
323 unboxedSingletonDataCon :: DataCon
324 unboxedSingletonDataCon = tupleCon Unboxed 1
326 unboxedPairTyCon :: TyCon
327 unboxedPairTyCon = tupleTyCon Unboxed 2
328 unboxedPairDataCon :: DataCon
329 unboxedPairDataCon = tupleCon Unboxed 2
332 %************************************************************************
334 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
336 %************************************************************************
340 charTy = mkTyConTy charTyCon
343 charTyCon = pcNonRecDataTyCon charTyConName [] [charDataCon]
344 charDataCon :: DataCon
345 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
348 stringTy = mkListTy charTy -- convenience only
353 intTy = mkTyConTy intTyCon
356 intTyCon = pcNonRecDataTyCon intTyConName [] [intDataCon]
357 intDataCon :: DataCon
358 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
363 wordTy = mkTyConTy wordTyCon
366 wordTyCon = pcNonRecDataTyCon wordTyConName [] [wordDataCon]
367 wordDataCon :: DataCon
368 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
373 floatTy = mkTyConTy floatTyCon
376 floatTyCon = pcNonRecDataTyCon floatTyConName [] [floatDataCon]
377 floatDataCon :: DataCon
378 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
383 doubleTy = mkTyConTy doubleTyCon
386 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [doubleDataCon]
388 doubleDataCon :: DataCon
389 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
393 %************************************************************************
395 \subsection[TysWiredIn-Bool]{The @Bool@ type}
397 %************************************************************************
399 An ordinary enumeration type, but deeply wired in. There are no
400 magical operations on @Bool@ (just the regular Prelude code).
402 {\em BEGIN IDLE SPECULATION BY SIMON}
404 This is not the only way to encode @Bool@. A more obvious coding makes
405 @Bool@ just a boxed up version of @Bool#@, like this:
408 data Bool = MkBool Bool#
411 Unfortunately, this doesn't correspond to what the Report says @Bool@
412 looks like! Furthermore, we get slightly less efficient code (I
413 think) with this coding. @gtInt@ would look like this:
416 gtInt :: Int -> Int -> Bool
417 gtInt x y = case x of I# x# ->
419 case (gtIntPrim x# y#) of
423 Notice that the result of the @gtIntPrim@ comparison has to be turned
424 into an integer (here called @b#@), and returned in a @MkBool@ box.
426 The @if@ expression would compile to this:
429 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
432 I think this code is a little less efficient than the previous code,
433 but I'm not certain. At all events, corresponding with the Report is
434 important. The interesting thing is that the language is expressive
435 enough to describe more than one alternative; and that a type doesn't
436 necessarily need to be a straightforwardly boxed version of its
437 primitive counterpart.
439 {\em END IDLE SPECULATION BY SIMON}
443 boolTy = mkTyConTy boolTyCon
446 boolTyCon = pcTyCon True NonRecursive boolTyConName
447 [] [falseDataCon, trueDataCon]
449 falseDataCon, trueDataCon :: DataCon
450 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
451 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
453 falseDataConId, trueDataConId :: Id
454 falseDataConId = dataConWorkId falseDataCon
455 trueDataConId = dataConWorkId trueDataCon
458 %************************************************************************
460 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
462 %************************************************************************
464 Special syntax, deeply wired in, but otherwise an ordinary algebraic
467 data [] a = [] | a : (List a)
469 data (,) a b = (,,) a b
474 mkListTy :: Type -> Type
475 mkListTy ty = mkTyConApp listTyCon [ty]
478 listTyCon = pcRecDataTyCon listTyConName alpha_tyvar [nilDataCon, consDataCon]
480 nilDataCon :: DataCon
481 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
483 consDataCon :: DataCon
484 consDataCon = pcDataConWithFixity True {- Declared infix -}
486 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
487 -- Interesting: polymorphic recursion would help here.
488 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
489 -- gets the over-specific type (Type -> Type)
492 %************************************************************************
494 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
496 %************************************************************************
498 The tuple types are definitely magic, because they form an infinite
503 They have a special family of type constructors, of type @TyCon@
504 These contain the tycon arity, but don't require a Unique.
507 They have a special family of constructors, of type
508 @Id@. Again these contain their arity but don't need a Unique.
511 There should be a magic way of generating the info tables and
512 entry code for all tuples.
514 But at the moment we just compile a Haskell source
515 file\srcloc{lib/prelude/...} containing declarations like:
518 data Tuple2 a b = Tup2 a b
519 data Tuple3 a b c = Tup3 a b c
520 data Tuple4 a b c d = Tup4 a b c d
523 The print-names associated with the magic @Id@s for tuple constructors
524 ``just happen'' to be the same as those generated by these
528 The instance environment should have a magic way to know
529 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
530 so on. \ToDo{Not implemented yet.}
533 There should also be a way to generate the appropriate code for each
534 of these instances, but (like the info tables and entry code) it is
535 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
539 mkTupleTy :: Boxity -> Int -> [Type] -> Type
540 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
543 unitTy = mkTupleTy Boxed 0 []
546 %************************************************************************
548 \subsection[TysWiredIn-PArr]{The @[::]@ type}
550 %************************************************************************
552 Special syntax for parallel arrays needs some wired in definitions.
555 -- | Construct a type representing the application of the parallel array constructor
556 mkPArrTy :: Type -> Type
557 mkPArrTy ty = mkTyConApp parrTyCon [ty]
559 -- | Represents the type constructor of parallel arrays
561 -- * This must match the definition in @PrelPArr@
563 -- NB: Although the constructor is given here, it will not be accessible in
564 -- user code as it is not in the environment of any compiled module except
568 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [parrDataCon]
570 parrDataCon :: DataCon
571 parrDataCon = pcDataCon
573 alpha_tyvar -- forall'ed type variables
574 [intPrimTy, -- 1st argument: Int#
575 mkTyConApp -- 2nd argument: Array# a
580 -- | Check whether a type constructor is the constructor for parallel arrays
581 isPArrTyCon :: TyCon -> Bool
582 isPArrTyCon tc = tyConName tc == parrTyConName
584 -- | Fake array constructors
586 -- * These constructors are never really used to represent array values;
587 -- however, they are very convenient during desugaring (and, in particular,
588 -- in the pattern matching compiler) to treat array pattern just like
589 -- yet another constructor pattern
591 parrFakeCon :: Arity -> DataCon
592 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
593 parrFakeCon i = parrFakeConArr!i
595 -- pre-defined set of constructors
597 parrFakeConArr :: Array Int DataCon
598 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
599 | i <- [0..mAX_TUPLE_SIZE]]
601 -- build a fake parallel array constructor for the given arity
603 mkPArrFakeCon :: Int -> DataCon
604 mkPArrFakeCon arity = data_con
606 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
607 tyvar = head alphaTyVars
608 tyvarTys = replicate arity $ mkTyVarTy tyvar
609 nameStr = mkFastString ("MkPArr" ++ show arity)
610 name = mkWiredInName gHC_PARR (mkDataOccFS nameStr) unique
611 (ADataCon data_con) UserSyntax
612 unique = mkPArrDataConUnique arity
614 -- | Checks whether a data constructor is a fake constructor for parallel arrays
615 isPArrFakeCon :: DataCon -> Bool
616 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)