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, 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 ( mkPrelModule )
91 import Name ( Name, nameRdrName, nameUnique, nameOccName,
92 nameModule, mkWiredInName )
93 import OccName ( mkOccFS, tcName, dataName, mkWorkerOcc, mkGenOcc1, mkGenOcc2 )
94 import RdrName ( rdrNameOcc )
95 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
96 import Var ( TyVar, tyVarKind )
97 import TyCon ( TyCon, AlgTyConFlavour(..), DataConDetails(..), tyConDataCons,
98 mkTupleTyCon, mkAlgTyCon, tyConName
101 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed, StrictnessMark(..) )
103 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
104 mkArrowKinds, liftedTypeKind, unliftedTypeKind,
106 import Unique ( incrUnique, mkTupleTyConUnique,
107 mkTupleDataConUnique, mkPArrDataConUnique )
112 alpha_tyvar = [alphaTyVar]
114 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
118 %************************************************************************
120 \subsection{Wired in type constructors}
122 %************************************************************************
125 wiredInTyCons :: [TyCon]
126 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
128 data_tycons = genericTyCons ++
143 genericTyCons :: [TyCon]
144 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
147 tuple_tycons = unitTyCon : [tupleTyCon Boxed i | i <- [2..mAX_TUPLE_SIZE] ]
148 unboxed_tuple_tycons = [tupleTyCon Unboxed i | i <- [1..mAX_TUPLE_SIZE] ]
152 %************************************************************************
154 \subsection{mkWiredInTyCon}
156 %************************************************************************
159 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
160 pcRecDataTyCon = pcTyCon DataTyCon Recursive
162 pcTyCon new_or_data is_rec name tyvars argvrcs cons
165 tycon = mkAlgTyCon name kind
170 [] -- No record selectors
175 mod = nameModule name
176 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
177 gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
179 -- We generate names for the generic to/from Ids by incrementing
180 -- the TyCon unique. So each Prelude tycon needs 3 slots, one
181 -- for itself and two more for the generic Ids.
182 mk_tc_gen_info mod tc_uniq tc_name tycon
183 = mkTyConGenInfo tycon [name1, name2]
185 tc_occ_name = nameOccName tc_name
186 occ_name1 = mkGenOcc1 tc_occ_name
187 occ_name2 = mkGenOcc2 tc_occ_name
188 fn1_key = incrUnique tc_uniq
189 fn2_key = incrUnique fn1_key
190 name1 = mkWiredInName mod occ_name1 fn1_key
191 name2 = mkWiredInName mod occ_name2 fn2_key
193 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
194 -- The unique is the first of two free uniques;
195 -- the first is used for the datacon itself and the worker;
196 -- the second is used for the wrapper.
198 pcDataCon name tyvars context arg_tys tycon
201 data_con = mkDataCon name
202 [ NotMarkedStrict | a <- arg_tys ]
203 [ {- no labelled fields -} ]
204 tyvars context [] [] arg_tys tycon work_id wrap_id
206 wrap_rdr = nameRdrName name
207 wrap_occ = rdrNameOcc wrap_rdr
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 = mkPrelModule mod_name
259 gen_info = mk_tc_gen_info mod tc_uniq tc_name tycon
261 unitTyCon = tupleTyCon Boxed 0
262 unitDataConId = dataConWorkId (head (tyConDataCons unitTyCon))
264 pairTyCon = tupleTyCon Boxed 2
266 unboxedSingletonTyCon = tupleTyCon Unboxed 1
267 unboxedSingletonDataCon = tupleCon Unboxed 1
269 unboxedPairTyCon = tupleTyCon Unboxed 2
270 unboxedPairDataCon = tupleCon Unboxed 2
273 %************************************************************************
275 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
277 %************************************************************************
280 -- The Void type is represented as a data type with no constructors
281 -- It's a built in type (i.e. there's no way to define it in Haskell;
282 -- the nearest would be
284 -- data Void = -- No constructors!
286 -- ) It's lifted; there is only one value of this
287 -- type, namely "void", whose semantics is just bottom.
289 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
296 charTy = mkTyConTy charTyCon
298 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
299 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
301 stringTy = mkListTy charTy -- convenience only
305 intTy = mkTyConTy intTyCon
307 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
308 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
312 wordTy = mkTyConTy wordTyCon
314 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
315 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
319 addrTy = mkTyConTy addrTyCon
321 addrTyCon = pcNonRecDataTyCon addrTyConName [] [] [addrDataCon]
322 addrDataCon = pcDataCon addrDataConName [] [] [addrPrimTy] addrTyCon
326 ptrTy = mkTyConTy ptrTyCon
328 ptrTyCon = pcNonRecDataTyCon ptrTyConName alpha_tyvar [(True,False)] [ptrDataCon]
329 ptrDataCon = pcDataCon ptrDataConName alpha_tyvar [] [addrPrimTy] ptrTyCon
333 funPtrTy = mkTyConTy funPtrTyCon
335 funPtrTyCon = pcNonRecDataTyCon funPtrTyConName alpha_tyvar [(True,False)] [funPtrDataCon]
336 funPtrDataCon = pcDataCon funPtrDataConName alpha_tyvar [] [addrPrimTy] funPtrTyCon
340 floatTy = mkTyConTy floatTyCon
342 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
343 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
347 doubleTy = mkTyConTy doubleTyCon
349 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
350 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
355 = pcNonRecDataTyCon stablePtrTyConName
356 alpha_tyvar [(True,False)] [stablePtrDataCon]
359 = pcDataCon stablePtrDataConName
360 alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
363 %************************************************************************
365 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
367 %************************************************************************
369 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
372 integerTy = mkTyConTy integerTyCon
374 integerTyCon = pcNonRecDataTyCon integerTyConName
375 [] [] [smallIntegerDataCon, largeIntegerDataCon]
377 smallIntegerDataCon = pcDataCon smallIntegerDataConName
378 [] [] [intPrimTy] integerTyCon
379 largeIntegerDataCon = pcDataCon largeIntegerDataConName
380 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
384 %************************************************************************
386 \subsection[TysWiredIn-Bool]{The @Bool@ type}
388 %************************************************************************
390 An ordinary enumeration type, but deeply wired in. There are no
391 magical operations on @Bool@ (just the regular Prelude code).
393 {\em BEGIN IDLE SPECULATION BY SIMON}
395 This is not the only way to encode @Bool@. A more obvious coding makes
396 @Bool@ just a boxed up version of @Bool#@, like this:
399 data Bool = MkBool Bool#
402 Unfortunately, this doesn't correspond to what the Report says @Bool@
403 looks like! Furthermore, we get slightly less efficient code (I
404 think) with this coding. @gtInt@ would look like this:
407 gtInt :: Int -> Int -> Bool
408 gtInt x y = case x of I# x# ->
410 case (gtIntPrim x# y#) of
414 Notice that the result of the @gtIntPrim@ comparison has to be turned
415 into an integer (here called @b#@), and returned in a @MkBool@ box.
417 The @if@ expression would compile to this:
420 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
423 I think this code is a little less efficient than the previous code,
424 but I'm not certain. At all events, corresponding with the Report is
425 important. The interesting thing is that the language is expressive
426 enough to describe more than one alternative; and that a type doesn't
427 necessarily need to be a straightforwardly boxed version of its
428 primitive counterpart.
430 {\em END IDLE SPECULATION BY SIMON}
433 boolTy = mkTyConTy boolTyCon
435 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
436 [] [] [falseDataCon, trueDataCon]
438 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
439 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
441 falseDataConId = dataConWorkId falseDataCon
442 trueDataConId = dataConWorkId trueDataCon
445 %************************************************************************
447 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
449 %************************************************************************
451 Special syntax, deeply wired in, but otherwise an ordinary algebraic
454 data [] a = [] | a : (List a)
456 data (,) a b = (,,) a b
461 mkListTy :: Type -> Type
462 mkListTy ty = mkTyConApp listTyCon [ty]
464 listTyCon = pcRecDataTyCon listTyConName
465 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
467 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
468 consDataCon = pcDataCon consDataConName
469 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
470 -- Interesting: polymorphic recursion would help here.
471 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
472 -- gets the over-specific type (Type -> Type)
475 %************************************************************************
477 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
479 %************************************************************************
481 The tuple types are definitely magic, because they form an infinite
486 They have a special family of type constructors, of type @TyCon@
487 These contain the tycon arity, but don't require a Unique.
490 They have a special family of constructors, of type
491 @Id@. Again these contain their arity but don't need a Unique.
494 There should be a magic way of generating the info tables and
495 entry code for all tuples.
497 But at the moment we just compile a Haskell source
498 file\srcloc{lib/prelude/...} containing declarations like:
501 data Tuple2 a b = Tup2 a b
502 data Tuple3 a b c = Tup3 a b c
503 data Tuple4 a b c d = Tup4 a b c d
506 The print-names associated with the magic @Id@s for tuple constructors
507 ``just happen'' to be the same as those generated by these
511 The instance environment should have a magic way to know
512 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
513 so on. \ToDo{Not implemented yet.}
516 There should also be a way to generate the appropriate code for each
517 of these instances, but (like the info tables and entry code) it is
518 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
522 mkTupleTy :: Boxity -> Int -> [Type] -> Type
523 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
525 unitTy = mkTupleTy Boxed 0 []
528 %************************************************************************
530 \subsection[TysWiredIn-PArr]{The @[::]@ type}
532 %************************************************************************
534 Special syntax for parallel arrays needs some wired in definitions.
537 -- construct a type representing the application of the parallel array
540 mkPArrTy :: Type -> Type
541 mkPArrTy ty = mkTyConApp parrTyCon [ty]
543 -- represents the type constructor of parallel arrays
545 -- * this must match the definition in `PrelPArr'
547 -- NB: Although the constructor is given here, it will not be accessible in
548 -- user code as it is not in the environment of any compiled module except
560 (DataCons [parrDataCon]) -- The constructor defined in `PrelPArr'
561 [] -- No record selectors
566 mod = nameModule parrTyConName
567 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
568 genInfo = mk_tc_gen_info mod (nameUnique parrTyConName) parrTyConName tycon
570 parrDataCon :: DataCon
571 parrDataCon = pcDataCon
573 alpha_tyvar -- forall'ed type variables
575 [intPrimTy, -- 1st argument: Int#
576 mkTyConApp -- 2nd argument: Array# a
581 -- check whether a type constructor is the constructor for parallel arrays
583 isPArrTyCon :: TyCon -> Bool
584 isPArrTyCon tc = tyConName tc == parrTyConName
586 -- fake array constructors
588 -- * these constructors are never really used to represent array values;
589 -- however, they are very convenient during desugaring (and, in particular,
590 -- in the pattern matching compiler) to treat array pattern just like
591 -- yet another constructor pattern
593 parrFakeCon :: Arity -> DataCon
594 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
595 parrFakeCon i = parrFakeConArr!i
597 -- pre-defined set of constructors
599 parrFakeConArr :: Array Int DataCon
600 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
601 | i <- [0..mAX_TUPLE_SIZE]]
603 -- build a fake parallel array constructor for the given arity
605 mkPArrFakeCon :: Int -> DataCon
606 mkPArrFakeCon arity = pcDataCon name [tyvar] [] tyvarTys parrTyCon
608 tyvar = head alphaTyVars
609 tyvarTys = replicate arity $ mkTyVarTy tyvar
610 nameStr = mkFastString ("MkPArr" ++ show arity)
611 name = mkWiredInName mod (mkOccFS dataName nameStr) uniq
612 uniq = mkPArrDataConUnique arity
613 mod = mkPrelModule pREL_PARR_Name
615 -- checks whether a data constructor is a fake constructor for parallel arrays
617 isPArrFakeCon :: DataCon -> Bool
618 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
621 %************************************************************************
623 \subsection{Wired In Type Constructors for Representation Types}
625 %************************************************************************
627 The following code defines the wired in datatypes cross, plus, unit
628 and c_of needed for the generic methods.
630 Ok, so the basic story is that for each type constructor I need to
631 create 2 things - a TyCon and a DataCon and then we are basically
632 ok. There are going to be no arguments passed to these functions
633 because -well- there is nothing to pass to these functions.
637 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
639 crossDataCon :: DataCon
640 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
643 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
645 inlDataCon, inrDataCon :: DataCon
646 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
647 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy] plusTyCon
649 genUnitTyCon :: TyCon -- The "1" type constructor for generics
650 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
652 genUnitDataCon :: DataCon
653 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon