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.''
14 wiredInTyCons, genericTyCons,
34 falseDataCon, falseDataConId,
56 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
57 unboxedSingletonTyCon, unboxedSingletonDataCon,
58 unboxedPairTyCon, unboxedPairDataCon,
61 genUnitTyCon, genUnitDataCon,
62 plusTyCon, inrDataCon, inlDataCon,
63 crossTyCon, crossDataCon,
67 trueDataCon, trueDataConId,
76 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon
79 #include "HsVersions.h"
81 import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
82 import {-# SOURCE #-} Generics( mkTyConGenInfo )
89 import Constants ( mAX_TUPLE_SIZE )
90 import Module ( mkBasePkgModule )
91 import Name ( Name, nameUnique, nameOccName,
92 nameModule, mkWiredInName )
93 import OccName ( mkOccFS, tcName, dataName, mkWorkerOcc, mkGenOcc1, mkGenOcc2 )
94 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
95 import Var ( TyVar, tyVarKind )
96 import TyCon ( TyCon, AlgTyConFlavour(..), DataConDetails(..), tyConDataCons,
97 mkTupleTyCon, mkAlgTyCon, tyConName
100 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed, StrictnessMark(..) )
102 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
103 mkArrowKinds, liftedTypeKind, unliftedTypeKind,
105 import Unique ( incrUnique, mkTupleTyConUnique,
106 mkTupleDataConUnique, mkPArrDataConUnique )
111 alpha_tyvar = [alphaTyVar]
113 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
117 %************************************************************************
119 \subsection{Wired in type constructors}
121 %************************************************************************
123 If you change which things are wired in, make sure you change their
124 names in PrelNames, so they use wTcQual, wDataQual, etc
127 wiredInTyCons :: [TyCon]
128 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
130 data_tycons = genericTyCons ++
145 genericTyCons :: [TyCon]
146 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
148 tuple_tycons = unitTyCon : [tupleTyCon Boxed i | i <- [2..mAX_TUPLE_SIZE] ]
149 unboxed_tuple_tycons = [tupleTyCon Unboxed i | i <- [1..mAX_TUPLE_SIZE] ]
153 %************************************************************************
155 \subsection{mkWiredInTyCon}
157 %************************************************************************
160 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
161 pcRecDataTyCon = pcTyCon DataTyCon Recursive
163 pcTyCon new_or_data is_rec name tyvars argvrcs cons
166 tycon = mkAlgTyCon name kind
171 [] -- No record selectors
176 mod = nameModule name
177 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
178 gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
180 -- We generate names for the generic to/from Ids by incrementing
181 -- the TyCon unique. So each Prelude tycon needs 3 slots, one
182 -- for itself and two more for the generic Ids.
183 mk_tc_gen_info mod tc_uniq tc_name tycon
184 = mkTyConGenInfo tycon [name1, name2]
186 tc_occ_name = nameOccName tc_name
187 occ_name1 = mkGenOcc1 tc_occ_name
188 occ_name2 = mkGenOcc2 tc_occ_name
189 fn1_key = incrUnique tc_uniq
190 fn2_key = incrUnique fn1_key
191 name1 = mkWiredInName mod occ_name1 fn1_key
192 name2 = mkWiredInName mod occ_name2 fn2_key
194 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
195 -- The unique is the first of two free uniques;
196 -- the first is used for the datacon itself and the worker;
197 -- the second is used for the wrapper.
199 pcDataCon name tyvars context arg_tys tycon
202 data_con = mkDataCon name
203 [ NotMarkedStrict | a <- arg_tys ]
204 [ {- no labelled fields -} ]
205 tyvars context [] [] arg_tys tycon work_id wrap_id
207 wrap_occ = nameOccName name
209 mod = nameModule name
210 wrap_id = mkDataConWrapId data_con
212 work_occ = mkWorkerOcc wrap_occ
213 work_key = incrUnique (nameUnique name)
214 work_name = mkWiredInName mod work_occ work_key
215 work_id = mkDataConId work_name data_con
219 %************************************************************************
221 \subsection[TysWiredIn-tuples]{The tuple types}
223 %************************************************************************
226 tupleTyCon :: Boxity -> Arity -> TyCon
227 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
228 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
229 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
231 tupleCon :: Boxity -> Arity -> DataCon
232 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
233 tupleCon Boxed i = snd (boxedTupleArr ! i)
234 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
236 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
237 boxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i) | i <- [0..mAX_TUPLE_SIZE]]
238 unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
240 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
241 mk_tuple boxity arity = (tycon, tuple_con)
243 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
244 tc_name = mkWiredInName mod (mkOccFS tcName name_str) tc_uniq
245 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
246 res_kind | isBoxed boxity = liftedTypeKind
247 | otherwise = unliftedTypeKind
249 tyvars | isBoxed boxity = take arity alphaTyVars
250 | otherwise = take arity openAlphaTyVars
252 tuple_con = pcDataCon name tyvars [] tyvar_tys tycon
253 tyvar_tys = mkTyVarTys tyvars
254 (mod_name, name_str) = mkTupNameStr boxity arity
255 name = mkWiredInName mod (mkOccFS dataName name_str) dc_uniq
256 tc_uniq = mkTupleTyConUnique boxity arity
257 dc_uniq = mkTupleDataConUnique boxity arity
258 mod = mkBasePkgModule mod_name
259 gen_info = mk_tc_gen_info mod tc_uniq tc_name tycon
261 unitTyCon = tupleTyCon Boxed 0
262 unitDataCon = head (tyConDataCons unitTyCon)
263 unitDataConId = dataConWorkId unitDataCon
265 pairTyCon = tupleTyCon Boxed 2
267 unboxedSingletonTyCon = tupleTyCon Unboxed 1
268 unboxedSingletonDataCon = tupleCon Unboxed 1
270 unboxedPairTyCon = tupleTyCon Unboxed 2
271 unboxedPairDataCon = tupleCon Unboxed 2
274 %************************************************************************
276 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
278 %************************************************************************
281 -- The Void type is represented as a data type with no constructors
282 -- It's a built in type (i.e. there's no way to define it in Haskell;
283 -- the nearest would be
285 -- data Void = -- No constructors!
287 -- ) It's lifted; there is only one value of this
288 -- type, namely "void", whose semantics is just bottom.
290 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
297 charTy = mkTyConTy charTyCon
299 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
300 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
302 stringTy = mkListTy charTy -- convenience only
306 intTy = mkTyConTy intTyCon
308 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
309 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
313 wordTy = mkTyConTy wordTyCon
315 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
316 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
320 addrTy = mkTyConTy addrTyCon
322 addrTyCon = pcNonRecDataTyCon addrTyConName [] [] [addrDataCon]
323 addrDataCon = pcDataCon addrDataConName [] [] [addrPrimTy] addrTyCon
327 ptrTy = mkTyConTy ptrTyCon
329 ptrTyCon = pcNonRecDataTyCon ptrTyConName alpha_tyvar [(True,False)] [ptrDataCon]
330 ptrDataCon = pcDataCon ptrDataConName alpha_tyvar [] [addrPrimTy] ptrTyCon
334 funPtrTy = mkTyConTy funPtrTyCon
336 funPtrTyCon = pcNonRecDataTyCon funPtrTyConName alpha_tyvar [(True,False)] [funPtrDataCon]
337 funPtrDataCon = pcDataCon funPtrDataConName alpha_tyvar [] [addrPrimTy] funPtrTyCon
341 floatTy = mkTyConTy floatTyCon
343 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
344 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
348 doubleTy = mkTyConTy doubleTyCon
350 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
351 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
356 = pcNonRecDataTyCon stablePtrTyConName
357 alpha_tyvar [(True,False)] [stablePtrDataCon]
360 = pcDataCon stablePtrDataConName
361 alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
364 %************************************************************************
366 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
368 %************************************************************************
370 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
373 integerTy = mkTyConTy integerTyCon
375 integerTyCon = pcNonRecDataTyCon integerTyConName
376 [] [] [smallIntegerDataCon, largeIntegerDataCon]
378 smallIntegerDataCon = pcDataCon smallIntegerDataConName
379 [] [] [intPrimTy] integerTyCon
380 largeIntegerDataCon = pcDataCon largeIntegerDataConName
381 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
385 %************************************************************************
387 \subsection[TysWiredIn-Bool]{The @Bool@ type}
389 %************************************************************************
391 An ordinary enumeration type, but deeply wired in. There are no
392 magical operations on @Bool@ (just the regular Prelude code).
394 {\em BEGIN IDLE SPECULATION BY SIMON}
396 This is not the only way to encode @Bool@. A more obvious coding makes
397 @Bool@ just a boxed up version of @Bool#@, like this:
400 data Bool = MkBool Bool#
403 Unfortunately, this doesn't correspond to what the Report says @Bool@
404 looks like! Furthermore, we get slightly less efficient code (I
405 think) with this coding. @gtInt@ would look like this:
408 gtInt :: Int -> Int -> Bool
409 gtInt x y = case x of I# x# ->
411 case (gtIntPrim x# y#) of
415 Notice that the result of the @gtIntPrim@ comparison has to be turned
416 into an integer (here called @b#@), and returned in a @MkBool@ box.
418 The @if@ expression would compile to this:
421 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
424 I think this code is a little less efficient than the previous code,
425 but I'm not certain. At all events, corresponding with the Report is
426 important. The interesting thing is that the language is expressive
427 enough to describe more than one alternative; and that a type doesn't
428 necessarily need to be a straightforwardly boxed version of its
429 primitive counterpart.
431 {\em END IDLE SPECULATION BY SIMON}
434 boolTy = mkTyConTy boolTyCon
436 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
437 [] [] [falseDataCon, trueDataCon]
439 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
440 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
442 falseDataConId = dataConWorkId falseDataCon
443 trueDataConId = dataConWorkId trueDataCon
446 %************************************************************************
448 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
450 %************************************************************************
452 Special syntax, deeply wired in, but otherwise an ordinary algebraic
455 data [] a = [] | a : (List a)
457 data (,) a b = (,,) a b
462 mkListTy :: Type -> Type
463 mkListTy ty = mkTyConApp listTyCon [ty]
465 listTyCon = pcRecDataTyCon listTyConName
466 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
468 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
469 consDataCon = pcDataCon consDataConName
470 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
471 -- Interesting: polymorphic recursion would help here.
472 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
473 -- gets the over-specific type (Type -> Type)
476 %************************************************************************
478 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
480 %************************************************************************
482 The tuple types are definitely magic, because they form an infinite
487 They have a special family of type constructors, of type @TyCon@
488 These contain the tycon arity, but don't require a Unique.
491 They have a special family of constructors, of type
492 @Id@. Again these contain their arity but don't need a Unique.
495 There should be a magic way of generating the info tables and
496 entry code for all tuples.
498 But at the moment we just compile a Haskell source
499 file\srcloc{lib/prelude/...} containing declarations like:
502 data Tuple2 a b = Tup2 a b
503 data Tuple3 a b c = Tup3 a b c
504 data Tuple4 a b c d = Tup4 a b c d
507 The print-names associated with the magic @Id@s for tuple constructors
508 ``just happen'' to be the same as those generated by these
512 The instance environment should have a magic way to know
513 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
514 so on. \ToDo{Not implemented yet.}
517 There should also be a way to generate the appropriate code for each
518 of these instances, but (like the info tables and entry code) it is
519 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
523 mkTupleTy :: Boxity -> Int -> [Type] -> Type
524 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
526 unitTy = mkTupleTy Boxed 0 []
529 %************************************************************************
531 \subsection[TysWiredIn-PArr]{The @[::]@ type}
533 %************************************************************************
535 Special syntax for parallel arrays needs some wired in definitions.
538 -- construct a type representing the application of the parallel array
541 mkPArrTy :: Type -> Type
542 mkPArrTy ty = mkTyConApp parrTyCon [ty]
544 -- represents the type constructor of parallel arrays
546 -- * this must match the definition in `PrelPArr'
548 -- NB: Although the constructor is given here, it will not be accessible in
549 -- user code as it is not in the environment of any compiled module except
561 (DataCons [parrDataCon]) -- The constructor defined in `PrelPArr'
562 [] -- No record selectors
567 mod = nameModule parrTyConName
568 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
569 genInfo = mk_tc_gen_info mod (nameUnique parrTyConName) parrTyConName tycon
571 parrDataCon :: DataCon
572 parrDataCon = pcDataCon
574 alpha_tyvar -- forall'ed type variables
576 [intPrimTy, -- 1st argument: Int#
577 mkTyConApp -- 2nd argument: Array# a
582 -- check whether a type constructor is the constructor for parallel arrays
584 isPArrTyCon :: TyCon -> Bool
585 isPArrTyCon tc = tyConName tc == parrTyConName
587 -- fake array constructors
589 -- * these constructors are never really used to represent array values;
590 -- however, they are very convenient during desugaring (and, in particular,
591 -- in the pattern matching compiler) to treat array pattern just like
592 -- yet another constructor pattern
594 parrFakeCon :: Arity -> DataCon
595 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
596 parrFakeCon i = parrFakeConArr!i
598 -- pre-defined set of constructors
600 parrFakeConArr :: Array Int DataCon
601 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
602 | i <- [0..mAX_TUPLE_SIZE]]
604 -- build a fake parallel array constructor for the given arity
606 mkPArrFakeCon :: Int -> DataCon
607 mkPArrFakeCon arity = pcDataCon name [tyvar] [] tyvarTys parrTyCon
609 tyvar = head alphaTyVars
610 tyvarTys = replicate arity $ mkTyVarTy tyvar
611 nameStr = mkFastString ("MkPArr" ++ show arity)
612 name = mkWiredInName mod (mkOccFS dataName nameStr) uniq
613 uniq = mkPArrDataConUnique arity
614 mod = mkBasePkgModule pREL_PARR_Name
616 -- checks whether a data constructor is a fake constructor for parallel arrays
618 isPArrFakeCon :: DataCon -> Bool
619 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
622 %************************************************************************
624 \subsection{Wired In Type Constructors for Representation Types}
626 %************************************************************************
628 The following code defines the wired in datatypes cross, plus, unit
629 and c_of needed for the generic methods.
631 Ok, so the basic story is that for each type constructor I need to
632 create 2 things - a TyCon and a DataCon and then we are basically
633 ok. There are going to be no arguments passed to these functions
634 because -well- there is nothing to pass to these functions.
638 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
640 crossDataCon :: DataCon
641 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
644 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
646 inlDataCon, inrDataCon :: DataCon
647 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
648 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy] plusTyCon
650 genUnitTyCon :: TyCon -- The "1" type constructor for generics
651 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
653 genUnitDataCon :: DataCon
654 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon