2 % (c) The GRASP Project, Glasgow University, 1994-1998
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
6 This module is about types that can be defined in Haskell, but which
7 must be wired into the compiler nonetheless.
9 This module tracks the ``state interface'' document, ``GHC prelude:
10 types and operations.''
16 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
17 trueDataCon, trueDataConId, true_RDR,
18 falseDataCon, falseDataConId, false_RDR,
20 charTyCon, charDataCon, charTyCon_RDR,
21 charTy, stringTy, charTyConName,
24 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
26 floatTyCon, floatDataCon, floatTy, floatTyConName,
28 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
31 wordTyCon, wordDataCon, wordTyConName, wordTy,
33 listTyCon, nilDataCon, consDataCon,
34 listTyCon_RDR, consDataCon_RDR, listTyConName,
40 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
41 unboxedSingletonTyCon, unboxedSingletonDataCon,
42 unboxedPairTyCon, unboxedPairDataCon,
44 boxedTupleArr, unboxedTupleArr,
50 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
51 parrTyCon_RDR, parrTyConName
54 import {-# SOURCE #-} MkId( mkDataConIds )
61 import Constants ( mAX_TUPLE_SIZE )
62 import Module ( Module )
64 import Name ( Name, BuiltInSyntax(..), nameUnique, nameOccName,
65 nameModule, mkWiredInName )
66 import OccName ( mkOccNameFS, tcName, dataName, mkTupleOcc,
68 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
70 import TyCon ( TyCon, AlgTyConRhs(DataTyCon), tyConDataCons,
71 mkTupleTyCon, mkAlgTyCon, tyConName,
72 TyConParent(NoParentTyCon) )
74 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed,
77 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
79 import Coercion ( unsafeCoercionTyCon, symCoercionTyCon,
80 transCoercionTyCon, leftCoercionTyCon,
81 rightCoercionTyCon, instCoercionTyCon )
82 import TypeRep ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
83 import Unique ( incrUnique, mkTupleTyConUnique,
84 mkTupleDataConUnique, mkPArrDataConUnique )
89 alpha_tyvar :: [TyVar]
90 alpha_tyvar = [alphaTyVar]
97 %************************************************************************
99 \subsection{Wired in type constructors}
101 %************************************************************************
103 If you change which things are wired in, make sure you change their
104 names in PrelNames, so they use wTcQual, wDataQual, etc
107 wiredInTyCons :: [TyCon] -- Excludes tuples
108 -- This list is used only to define PrelInfo.wiredInThings
110 -- It does not need to include kind constructors, because
111 -- all that wiredInThings does is to initialise the Name table,
112 -- and kind constructors don't appear in source code.
114 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
115 -- it's defined in GHC.Base, and there's only
116 -- one of it. We put it in wiredInTyCons so
117 -- that it'll pre-populate the name cache, so
118 -- the special case in lookupOrigNameCache
119 -- doesn't need to look out for it
127 , unsafeCoercionTyCon
137 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
138 mkWiredInTyConName built_in modu fs unique tycon
139 = mkWiredInName modu (mkOccNameFS tcName fs) unique
140 (ATyCon tycon) -- Relevant TyCon
143 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
144 mkWiredInDataConName built_in modu fs unique datacon
145 = mkWiredInName modu (mkOccNameFS dataName fs) unique
146 (ADataCon datacon) -- Relevant DataCon
149 charTyConName, charDataConName, intTyConName, intDataConName :: Name
150 charTyConName = mkWiredInTyConName UserSyntax gHC_BASE (fsLit "Char") charTyConKey charTyCon
151 charDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "C#") charDataConKey charDataCon
152 intTyConName = mkWiredInTyConName UserSyntax gHC_BASE (fsLit "Int") intTyConKey intTyCon
153 intDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "I#") intDataConKey intDataCon
155 boolTyConName, falseDataConName, trueDataConName :: Name
156 boolTyConName = mkWiredInTyConName UserSyntax gHC_BOOL (fsLit "Bool") boolTyConKey boolTyCon
157 falseDataConName = mkWiredInDataConName UserSyntax gHC_BOOL (fsLit "False") falseDataConKey falseDataCon
158 trueDataConName = mkWiredInDataConName UserSyntax gHC_BOOL (fsLit "True") trueDataConKey trueDataCon
160 listTyConName, nilDataConName, consDataConName :: Name
161 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_BASE (fsLit "[]") listTyConKey listTyCon
162 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE (fsLit "[]") nilDataConKey nilDataCon
163 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE (fsLit ":") consDataConKey consDataCon
165 floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
166 floatTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT (fsLit "Float") floatTyConKey floatTyCon
167 floatDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT (fsLit "F#") floatDataConKey floatDataCon
168 doubleTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT (fsLit "Double") doubleTyConKey doubleTyCon
169 doubleDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT (fsLit "D#") doubleDataConKey doubleDataCon
171 parrTyConName, parrDataConName :: Name
172 parrTyConName = mkWiredInTyConName BuiltInSyntax gHC_PARR (fsLit "[::]") parrTyConKey parrTyCon
173 parrDataConName = mkWiredInDataConName UserSyntax gHC_PARR (fsLit "PArr") parrDataConKey parrDataCon
175 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
176 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR:: RdrName
177 boolTyCon_RDR = nameRdrName boolTyConName
178 false_RDR = nameRdrName falseDataConName
179 true_RDR = nameRdrName trueDataConName
180 intTyCon_RDR = nameRdrName intTyConName
181 charTyCon_RDR = nameRdrName charTyConName
182 intDataCon_RDR = nameRdrName intDataConName
183 listTyCon_RDR = nameRdrName listTyConName
184 consDataCon_RDR = nameRdrName consDataConName
185 parrTyCon_RDR = nameRdrName parrTyConName
187 tySuperKindTyCon_RDR = nameRdrName tySuperKindTyConName
188 coSuperKindTyCon_RDR = nameRdrName coSuperKindTyConName
189 liftedTypeKindTyCon_RDR = nameRdrName liftedTypeKindTyConName
190 openTypeKindTyCon_RDR = nameRdrName openTypeKindTyConName
191 unliftedTypeKindTyCon_RDR = nameRdrName unliftedTypeKindTyConName
192 ubxTupleKindTyCon_RDR = nameRdrName ubxTupleKindTyConName
193 argTypeKindTyCon_RDR = nameRdrName argTypeKindTyConName
194 funKindTyCon_RDR = nameRdrName funKindTyConName
199 %************************************************************************
201 \subsection{mkWiredInTyCon}
203 %************************************************************************
206 pcNonRecDataTyCon :: Name -> [TyVar] -> [DataCon] -> TyCon
207 pcNonRecDataTyCon = pcTyCon False NonRecursive
208 pcRecDataTyCon :: Name -> [TyVar] -> [DataCon] -> TyCon
209 pcRecDataTyCon = pcTyCon False Recursive
211 pcTyCon :: Bool -> RecFlag -> Name -> [TyVar] -> [DataCon] -> TyCon
212 pcTyCon is_enum is_rec name tyvars cons
215 tycon = mkAlgTyCon name
216 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
218 [] -- No stupid theta
219 (DataTyCon cons is_enum)
220 [] -- No record selectors
223 True -- All the wired-in tycons have generics
224 False -- Not in GADT syntax
226 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
227 pcDataCon = pcDataConWithFixity False
229 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
230 -- The Name should be in the DataName name space; it's the name
231 -- of the DataCon itself.
233 -- The unique is the first of two free uniques;
234 -- the first is used for the datacon itself,
235 -- the second is used for the "worker name"
237 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
240 data_con = mkDataCon dc_name declared_infix
241 (map (const NotMarkedStrict) arg_tys)
242 [] -- No labelled fields
244 [] -- No existential type variables
245 [] -- No equality spec
248 [] -- No stupid theta
249 (mkDataConIds bogus_wrap_name wrk_name data_con)
252 modu = nameModule dc_name
253 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
254 wrk_key = incrUnique (nameUnique dc_name)
255 wrk_name = mkWiredInName modu wrk_occ wrk_key
256 (AnId (dataConWorkId data_con)) UserSyntax
257 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
258 -- Wired-in types are too simple to need wrappers
262 %************************************************************************
264 \subsection[TysWiredIn-tuples]{The tuple types}
266 %************************************************************************
269 tupleTyCon :: Boxity -> Arity -> TyCon
270 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
271 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
272 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
274 tupleCon :: Boxity -> Arity -> DataCon
275 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
276 tupleCon Boxed i = snd (boxedTupleArr ! i)
277 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
279 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
280 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
281 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
283 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
284 mk_tuple boxity arity = (tycon, tuple_con)
286 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
287 modu = mkTupleModule boxity arity
288 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
289 (ATyCon tycon) BuiltInSyntax
290 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
291 res_kind | isBoxed boxity = liftedTypeKind
292 | otherwise = ubxTupleKind
294 tyvars | isBoxed boxity = take arity alphaTyVars
295 | otherwise = take arity openAlphaTyVars
297 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
298 tyvar_tys = mkTyVarTys tyvars
299 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
300 (ADataCon tuple_con) BuiltInSyntax
301 tc_uniq = mkTupleTyConUnique boxity arity
302 dc_uniq = mkTupleDataConUnique boxity arity
303 gen_info = True -- Tuples all have generics..
304 -- hmm: that's a *lot* of code
307 unitTyCon = tupleTyCon Boxed 0
308 unitDataCon :: DataCon
309 unitDataCon = head (tyConDataCons unitTyCon)
311 unitDataConId = dataConWorkId unitDataCon
314 pairTyCon = tupleTyCon Boxed 2
316 unboxedSingletonTyCon :: TyCon
317 unboxedSingletonTyCon = tupleTyCon Unboxed 1
318 unboxedSingletonDataCon :: DataCon
319 unboxedSingletonDataCon = tupleCon Unboxed 1
321 unboxedPairTyCon :: TyCon
322 unboxedPairTyCon = tupleTyCon Unboxed 2
323 unboxedPairDataCon :: DataCon
324 unboxedPairDataCon = tupleCon Unboxed 2
327 %************************************************************************
329 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
331 %************************************************************************
335 charTy = mkTyConTy charTyCon
338 charTyCon = pcNonRecDataTyCon charTyConName [] [charDataCon]
339 charDataCon :: DataCon
340 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
343 stringTy = mkListTy charTy -- convenience only
348 intTy = mkTyConTy intTyCon
351 intTyCon = pcNonRecDataTyCon intTyConName [] [intDataCon]
352 intDataCon :: DataCon
353 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
358 wordTy = mkTyConTy wordTyCon
361 wordTyCon = pcNonRecDataTyCon wordTyConName [] [wordDataCon]
362 wordDataCon :: DataCon
363 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
368 floatTy = mkTyConTy floatTyCon
371 floatTyCon = pcNonRecDataTyCon floatTyConName [] [floatDataCon]
372 floatDataCon :: DataCon
373 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
378 doubleTy = mkTyConTy doubleTyCon
381 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [doubleDataCon]
383 doubleDataCon :: DataCon
384 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
388 %************************************************************************
390 \subsection[TysWiredIn-Bool]{The @Bool@ type}
392 %************************************************************************
394 An ordinary enumeration type, but deeply wired in. There are no
395 magical operations on @Bool@ (just the regular Prelude code).
397 {\em BEGIN IDLE SPECULATION BY SIMON}
399 This is not the only way to encode @Bool@. A more obvious coding makes
400 @Bool@ just a boxed up version of @Bool#@, like this:
403 data Bool = MkBool Bool#
406 Unfortunately, this doesn't correspond to what the Report says @Bool@
407 looks like! Furthermore, we get slightly less efficient code (I
408 think) with this coding. @gtInt@ would look like this:
411 gtInt :: Int -> Int -> Bool
412 gtInt x y = case x of I# x# ->
414 case (gtIntPrim x# y#) of
418 Notice that the result of the @gtIntPrim@ comparison has to be turned
419 into an integer (here called @b#@), and returned in a @MkBool@ box.
421 The @if@ expression would compile to this:
424 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
427 I think this code is a little less efficient than the previous code,
428 but I'm not certain. At all events, corresponding with the Report is
429 important. The interesting thing is that the language is expressive
430 enough to describe more than one alternative; and that a type doesn't
431 necessarily need to be a straightforwardly boxed version of its
432 primitive counterpart.
434 {\em END IDLE SPECULATION BY SIMON}
438 boolTy = mkTyConTy boolTyCon
441 boolTyCon = pcTyCon True NonRecursive boolTyConName
442 [] [falseDataCon, trueDataCon]
444 falseDataCon, trueDataCon :: DataCon
445 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
446 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
448 falseDataConId, trueDataConId :: Id
449 falseDataConId = dataConWorkId falseDataCon
450 trueDataConId = dataConWorkId trueDataCon
453 %************************************************************************
455 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
457 %************************************************************************
459 Special syntax, deeply wired in, but otherwise an ordinary algebraic
462 data [] a = [] | a : (List a)
464 data (,) a b = (,,) a b
469 mkListTy :: Type -> Type
470 mkListTy ty = mkTyConApp listTyCon [ty]
473 listTyCon = pcRecDataTyCon listTyConName alpha_tyvar [nilDataCon, consDataCon]
475 nilDataCon :: DataCon
476 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
477 consDataCon :: DataCon
478 consDataCon = pcDataConWithFixity True {- Declared infix -}
480 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
481 -- Interesting: polymorphic recursion would help here.
482 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
483 -- gets the over-specific type (Type -> Type)
486 %************************************************************************
488 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
490 %************************************************************************
492 The tuple types are definitely magic, because they form an infinite
497 They have a special family of type constructors, of type @TyCon@
498 These contain the tycon arity, but don't require a Unique.
501 They have a special family of constructors, of type
502 @Id@. Again these contain their arity but don't need a Unique.
505 There should be a magic way of generating the info tables and
506 entry code for all tuples.
508 But at the moment we just compile a Haskell source
509 file\srcloc{lib/prelude/...} containing declarations like:
512 data Tuple2 a b = Tup2 a b
513 data Tuple3 a b c = Tup3 a b c
514 data Tuple4 a b c d = Tup4 a b c d
517 The print-names associated with the magic @Id@s for tuple constructors
518 ``just happen'' to be the same as those generated by these
522 The instance environment should have a magic way to know
523 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
524 so on. \ToDo{Not implemented yet.}
527 There should also be a way to generate the appropriate code for each
528 of these instances, but (like the info tables and entry code) it is
529 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
533 mkTupleTy :: Boxity -> Int -> [Type] -> Type
534 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
537 unitTy = mkTupleTy Boxed 0 []
540 %************************************************************************
542 \subsection[TysWiredIn-PArr]{The @[::]@ type}
544 %************************************************************************
546 Special syntax for parallel arrays needs some wired in definitions.
549 -- construct a type representing the application of the parallel array
552 mkPArrTy :: Type -> Type
553 mkPArrTy ty = mkTyConApp parrTyCon [ty]
555 -- represents the type constructor of parallel arrays
557 -- * this must match the definition in `PrelPArr'
559 -- NB: Although the constructor is given here, it will not be accessible in
560 -- user code as it is not in the environment of any compiled module except
564 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [parrDataCon]
566 parrDataCon :: DataCon
567 parrDataCon = pcDataCon
569 alpha_tyvar -- forall'ed type variables
570 [intPrimTy, -- 1st argument: Int#
571 mkTyConApp -- 2nd argument: Array# a
576 -- check whether a type constructor is the constructor for parallel arrays
578 isPArrTyCon :: TyCon -> Bool
579 isPArrTyCon tc = tyConName tc == parrTyConName
581 -- fake array constructors
583 -- * these constructors are never really used to represent array values;
584 -- however, they are very convenient during desugaring (and, in particular,
585 -- in the pattern matching compiler) to treat array pattern just like
586 -- yet another constructor pattern
588 parrFakeCon :: Arity -> DataCon
589 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
590 parrFakeCon i = parrFakeConArr!i
592 -- pre-defined set of constructors
594 parrFakeConArr :: Array Int DataCon
595 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
596 | i <- [0..mAX_TUPLE_SIZE]]
598 -- build a fake parallel array constructor for the given arity
600 mkPArrFakeCon :: Int -> DataCon
601 mkPArrFakeCon arity = data_con
603 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
604 tyvar = head alphaTyVars
605 tyvarTys = replicate arity $ mkTyVarTy tyvar
606 nameStr = mkFastString ("MkPArr" ++ show arity)
607 name = mkWiredInName gHC_PARR (mkOccNameFS dataName nameStr) unique
608 (ADataCon data_con) UserSyntax
609 unique = mkPArrDataConUnique arity
611 -- checks whether a data constructor is a fake constructor for parallel arrays
613 isPArrFakeCon :: DataCon -> Bool
614 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)