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,
32 falseDataCon, falseDataConId,
57 unitTyCon, unitDataConId, pairTyCon,
58 unboxedSingletonTyCon, unboxedSingletonDataCon,
59 unboxedPairTyCon, unboxedPairDataCon,
62 genUnitTyCon, genUnitDataCon,
63 plusTyCon, inrDataCon, inlDataCon,
64 crossTyCon, crossDataCon,
68 trueDataCon, trueDataConId,
75 isFFIArgumentTy, -- :: DynFlags -> Bool -> Type -> Bool
76 isFFIImportResultTy, -- :: DynFlags -> Type -> Bool
77 isFFIExportResultTy, -- :: Type -> Bool
78 isFFIExternalTy, -- :: Type -> Bool
79 isFFIDynArgumentTy, -- :: Type -> Bool
80 isFFIDynResultTy, -- :: Type -> Bool
81 isFFILabelTy, -- :: Type -> Bool
82 isAddrTy, -- :: Type -> Bool
83 isForeignPtrTy -- :: Type -> Bool
87 #include "HsVersions.h"
89 import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
90 import {-# SOURCE #-} Generics( mkTyConGenInfo )
97 import Constants ( mAX_TUPLE_SIZE )
98 import Module ( mkPrelModule )
99 import Name ( Name, nameRdrName, nameUnique, nameOccName,
100 nameModule, mkWiredInName )
101 import OccName ( mkOccFS, tcName, dataName, mkWorkerOcc, mkGenOcc1, mkGenOcc2 )
102 import RdrName ( rdrNameOcc )
103 import DataCon ( DataCon, StrictnessMark(..), mkDataCon, dataConId )
104 import Var ( TyVar, tyVarKind )
105 import TyCon ( TyCon, AlgTyConFlavour(..), tyConDataCons,
106 mkTupleTyCon, isUnLiftedTyCon, mkAlgTyCon
109 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
111 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTys,
112 mkArrowKinds, liftedTypeKind, unliftedTypeKind,
113 splitTyConApp_maybe, repType,
114 TauType, ClassContext )
115 import Unique ( incrUnique, mkTupleTyConUnique, mkTupleDataConUnique )
120 alpha_tyvar = [alphaTyVar]
122 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
126 %************************************************************************
128 \subsection{Wired in type constructors}
130 %************************************************************************
133 wiredInTyCons :: [TyCon]
134 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
136 data_tycons = genericTyCons ++
149 genericTyCons :: [TyCon]
150 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
153 tuple_tycons = unitTyCon : [tupleTyCon Boxed i | i <- [2..37] ]
154 unboxed_tuple_tycons = [tupleTyCon Unboxed i | i <- [1..37] ]
158 %************************************************************************
160 \subsection{mkWiredInTyCon}
162 %************************************************************************
165 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
166 pcRecDataTyCon = pcTyCon DataTyCon Recursive
168 pcTyCon new_or_data is_rec name tyvars argvrcs cons
171 tycon = mkAlgTyCon name kind
177 [] -- No record selectors
182 mod = nameModule name
183 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
184 gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
186 -- We generate names for the generic to/from Ids by incrementing
187 -- the TyCon unique. So each Prelude tycon needs 3 slots, one
188 -- for itself and two more for the generic Ids.
189 mk_tc_gen_info mod tc_uniq tc_name tycon
190 = mkTyConGenInfo tycon [name1, name2]
192 tc_occ_name = nameOccName tc_name
193 occ_name1 = mkGenOcc1 tc_occ_name
194 occ_name2 = mkGenOcc2 tc_occ_name
195 fn1_key = incrUnique tc_uniq
196 fn2_key = incrUnique fn1_key
197 name1 = mkWiredInName mod occ_name1 fn1_key
198 name2 = mkWiredInName mod occ_name2 fn2_key
200 pcDataCon :: Name -> [TyVar] -> ClassContext -> [TauType] -> TyCon -> DataCon
201 -- The unique is the first of two free uniques;
202 -- the first is used for the datacon itself and the worker;
203 -- the second is used for the wrapper.
205 pcDataCon name tyvars context arg_tys tycon
208 data_con = mkDataCon name
209 [ NotMarkedStrict | a <- arg_tys ]
210 [ {- no labelled fields -} ]
211 tyvars context [] [] arg_tys tycon work_id wrap_id
213 wrap_rdr = nameRdrName name
214 wrap_occ = rdrNameOcc wrap_rdr
216 mod = nameModule name
217 wrap_id = mkDataConWrapId data_con
219 work_occ = mkWorkerOcc wrap_occ
220 work_key = incrUnique (nameUnique name)
221 work_name = mkWiredInName mod work_occ work_key
222 work_id = mkDataConId work_name data_con
226 %************************************************************************
228 \subsection[TysWiredIn-tuples]{The tuple types}
230 %************************************************************************
233 tupleTyCon :: Boxity -> Arity -> TyCon
234 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
235 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
236 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
238 tupleCon :: Boxity -> Arity -> DataCon
239 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
240 tupleCon Boxed i = snd (boxedTupleArr ! i)
241 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
243 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
244 boxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i) | i <- [0..mAX_TUPLE_SIZE]]
245 unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
247 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
248 mk_tuple boxity arity = (tycon, tuple_con)
250 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
251 tc_name = mkWiredInName mod (mkOccFS tcName name_str) tc_uniq
252 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
253 res_kind | isBoxed boxity = liftedTypeKind
254 | otherwise = unliftedTypeKind
256 tyvars | isBoxed boxity = take arity alphaTyVars
257 | otherwise = take arity openAlphaTyVars
259 tuple_con = pcDataCon name tyvars [] tyvar_tys tycon
260 tyvar_tys = mkTyVarTys tyvars
261 (mod_name, name_str) = mkTupNameStr boxity arity
262 name = mkWiredInName mod (mkOccFS dataName name_str) dc_uniq
263 tc_uniq = mkTupleTyConUnique boxity arity
264 dc_uniq = mkTupleDataConUnique boxity arity
265 mod = mkPrelModule mod_name
266 gen_info = mk_tc_gen_info mod tc_uniq tc_name tycon
268 unitTyCon = tupleTyCon Boxed 0
269 unitDataConId = dataConId (head (tyConDataCons unitTyCon))
271 pairTyCon = tupleTyCon Boxed 2
273 unboxedSingletonTyCon = tupleTyCon Unboxed 1
274 unboxedSingletonDataCon = tupleCon Unboxed 1
276 unboxedPairTyCon = tupleTyCon Unboxed 2
277 unboxedPairDataCon = tupleCon Unboxed 2
280 %************************************************************************
282 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
284 %************************************************************************
287 -- The Void type is represented as a data type with no constructors
288 -- It's a built in type (i.e. there's no way to define it in Haskell;
289 -- the nearest would be
291 -- data Void = -- No constructors!
293 -- ) It's lifted; there is only one value of this
294 -- type, namely "void", whose semantics is just bottom.
296 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
303 charTy = mkTyConTy charTyCon
305 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
306 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
308 stringTy = mkListTy charTy -- convenience only
312 intTy = mkTyConTy intTyCon
314 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
315 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
317 isIntTy :: Type -> Bool
318 isIntTy = isTyCon intTyConKey
323 wordTy = mkTyConTy wordTyCon
325 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
326 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
330 addrTy = mkTyConTy addrTyCon
332 addrTyCon = pcNonRecDataTyCon addrTyConName [] [] [addrDataCon]
333 addrDataCon = pcDataCon addrDataConName [] [] [addrPrimTy] addrTyCon
335 isAddrTy :: Type -> Bool
336 isAddrTy = isTyCon addrTyConKey
340 ptrTy = mkTyConTy ptrTyCon
342 ptrTyCon = pcNonRecDataTyCon ptrTyConName alpha_tyvar [(True,False)] [ptrDataCon]
343 ptrDataCon = pcDataCon ptrDataConName alpha_tyvar [] [addrPrimTy] ptrTyCon
347 floatTy = mkTyConTy floatTyCon
349 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
350 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
352 isFloatTy :: Type -> Bool
353 isFloatTy = isTyCon floatTyConKey
357 doubleTy = mkTyConTy doubleTyCon
359 isDoubleTy :: Type -> Bool
360 isDoubleTy = isTyCon doubleTyConKey
362 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
363 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
368 = pcNonRecDataTyCon stablePtrTyConName
369 alpha_tyvar [(True,False)] [stablePtrDataCon]
372 = pcDataCon stablePtrDataConName
373 alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
378 = pcNonRecDataTyCon foreignObjTyConName
379 [] [] [foreignObjDataCon]
382 = pcDataCon foreignObjDataConName
383 [] [] [foreignObjPrimTy] foreignObjTyCon
385 isForeignObjTy :: Type -> Bool
386 isForeignObjTy = isTyCon foreignObjTyConKey
391 = pcNonRecDataTyCon foreignPtrTyConName
392 alpha_tyvar [(True,False)] [foreignPtrDataCon]
395 = pcDataCon foreignPtrDataConName
396 alpha_tyvar [] [foreignObjPrimTy] foreignPtrTyCon
398 isForeignPtrTy :: Type -> Bool
399 isForeignPtrTy = isTyCon foreignPtrTyConKey
402 %************************************************************************
404 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
406 %************************************************************************
408 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
411 integerTy = mkTyConTy integerTyCon
413 integerTyCon = pcNonRecDataTyCon integerTyConName
414 [] [] [smallIntegerDataCon, largeIntegerDataCon]
416 smallIntegerDataCon = pcDataCon smallIntegerDataConName
417 [] [] [intPrimTy] integerTyCon
418 largeIntegerDataCon = pcDataCon largeIntegerDataConName
419 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
422 isIntegerTy :: Type -> Bool
423 isIntegerTy = isTyCon integerTyConKey
427 %************************************************************************
429 \subsection[TysWiredIn-ext-type]{External types}
431 %************************************************************************
433 The compiler's foreign function interface supports the passing of a
434 restricted set of types as arguments and results (the restricting factor
438 isFFIArgumentTy :: DynFlags -> Bool -> Type -> Bool
439 -- Checks for valid argument type for a 'foreign import'
440 isFFIArgumentTy dflags is_safe ty
441 = checkRepTyCon (legalOutgoingTyCon dflags is_safe) ty
443 isFFIExternalTy :: Type -> Bool
444 -- Types that are allowed as arguments of a 'foreign export'
445 isFFIExternalTy ty = checkRepTyCon legalFEArgTyCon ty
447 isFFIImportResultTy :: DynFlags -> Type -> Bool
448 isFFIImportResultTy dflags ty
449 = checkRepTyCon (legalFIResultTyCon dflags) ty
451 isFFIExportResultTy :: Type -> Bool
452 isFFIExportResultTy ty = checkRepTyCon legalFEResultTyCon ty
454 isFFIDynArgumentTy :: Type -> Bool
455 -- The argument type of a foreign import dynamic must be Ptr, Addr,
456 -- or a newtype of either.
457 isFFIDynArgumentTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == addrTyCon)
459 isFFIDynResultTy :: Type -> Bool
460 -- The result type of a foreign export dynamic must be Ptr, Addr,
461 -- or a newtype of either.
462 isFFIDynResultTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == addrTyCon)
464 isFFILabelTy :: Type -> Bool
465 -- The type of a foreign label must be Ptr, Addr,
466 -- or a newtype of either.
467 isFFILabelTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == addrTyCon)
469 checkRepTyCon :: (TyCon -> Bool) -> Type -> Bool
470 -- look through newtypes
471 checkRepTyCon check_tc ty = checkTyCon check_tc (repType ty)
473 checkTyCon :: (TyCon -> Bool) -> Type -> Bool
474 checkTyCon check_tc ty = case splitTyConApp_maybe ty of
475 Just (tycon, _) -> check_tc tycon
478 isTyCon :: Unique -> Type -> Bool
479 isTyCon uniq ty = checkTyCon (\tc -> uniq == getUnique tc) ty
482 ----------------------------------------------
483 These chaps do the work; they are not exported
484 ----------------------------------------------
487 legalFEArgTyCon :: TyCon -> Bool
488 -- It's illegal to return foreign objects and (mutable)
489 -- bytearrays from a _ccall_ / foreign declaration
490 -- (or be passed them as arguments in foreign exported functions).
492 | getUnique tc `elem` [ foreignObjTyConKey, foreignPtrTyConKey,
493 byteArrayTyConKey, mutableByteArrayTyConKey ]
495 -- It's also illegal to make foreign exports that take unboxed
496 -- arguments. The RTS API currently can't invoke such things. --SDM 7/2000
498 = boxedMarshalableTyCon tc
500 legalFIResultTyCon :: DynFlags -> TyCon -> Bool
501 legalFIResultTyCon dflags tc
502 | getUnique tc `elem`
503 [ foreignObjTyConKey, foreignPtrTyConKey,
504 byteArrayTyConKey, mutableByteArrayTyConKey ] = False
505 | tc == unitTyCon = True
506 | otherwise = marshalableTyCon dflags tc
508 legalFEResultTyCon :: TyCon -> Bool
509 legalFEResultTyCon tc
510 | getUnique tc `elem`
511 [ foreignObjTyConKey, foreignPtrTyConKey,
512 byteArrayTyConKey, mutableByteArrayTyConKey ] = False
513 | tc == unitTyCon = True
514 | otherwise = boxedMarshalableTyCon tc
516 legalOutgoingTyCon :: DynFlags -> Bool -> TyCon -> Bool
517 -- Checks validity of types going from Haskell -> external world
518 -- The boolean is true for a 'safe' call (when we don't want to
519 -- pass Haskell pointers to the world)
520 legalOutgoingTyCon dflags be_safe tc
521 | be_safe && getUnique tc `elem` [byteArrayTyConKey, mutableByteArrayTyConKey]
524 = marshalableTyCon dflags tc
526 marshalableTyCon dflags tc
527 = (dopt Opt_GlasgowExts dflags && isUnLiftedTyCon tc)
528 || boxedMarshalableTyCon tc
530 boxedMarshalableTyCon tc
531 = getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey
532 , int32TyConKey, int64TyConKey
533 , wordTyConKey, word8TyConKey, word16TyConKey
534 , word32TyConKey, word64TyConKey
535 , floatTyConKey, doubleTyConKey
536 , addrTyConKey, ptrTyConKey
537 , charTyConKey, foreignObjTyConKey
540 , byteArrayTyConKey, mutableByteArrayTyConKey
546 %************************************************************************
548 \subsection[TysWiredIn-Bool]{The @Bool@ type}
550 %************************************************************************
552 An ordinary enumeration type, but deeply wired in. There are no
553 magical operations on @Bool@ (just the regular Prelude code).
555 {\em BEGIN IDLE SPECULATION BY SIMON}
557 This is not the only way to encode @Bool@. A more obvious coding makes
558 @Bool@ just a boxed up version of @Bool#@, like this:
561 data Bool = MkBool Bool#
564 Unfortunately, this doesn't correspond to what the Report says @Bool@
565 looks like! Furthermore, we get slightly less efficient code (I
566 think) with this coding. @gtInt@ would look like this:
569 gtInt :: Int -> Int -> Bool
570 gtInt x y = case x of I# x# ->
572 case (gtIntPrim x# y#) of
576 Notice that the result of the @gtIntPrim@ comparison has to be turned
577 into an integer (here called @b#@), and returned in a @MkBool@ box.
579 The @if@ expression would compile to this:
582 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
585 I think this code is a little less efficient than the previous code,
586 but I'm not certain. At all events, corresponding with the Report is
587 important. The interesting thing is that the language is expressive
588 enough to describe more than one alternative; and that a type doesn't
589 necessarily need to be a straightforwardly boxed version of its
590 primitive counterpart.
592 {\em END IDLE SPECULATION BY SIMON}
595 boolTy = mkTyConTy boolTyCon
597 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
598 [] [] [falseDataCon, trueDataCon]
600 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
601 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
603 falseDataConId = dataConId falseDataCon
604 trueDataConId = dataConId trueDataCon
607 %************************************************************************
609 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
611 %************************************************************************
613 Special syntax, deeply wired in, but otherwise an ordinary algebraic
616 data [] a = [] | a : (List a)
618 data (,) a b = (,,) a b
623 mkListTy :: Type -> Type
624 mkListTy ty = mkTyConApp listTyCon [ty]
626 listTyCon = pcRecDataTyCon listTyConName
627 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
629 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
630 consDataCon = pcDataCon consDataConName
631 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
632 -- Interesting: polymorphic recursion would help here.
633 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
634 -- gets the over-specific type (Type -> Type)
637 %************************************************************************
639 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
641 %************************************************************************
643 The tuple types are definitely magic, because they form an infinite
648 They have a special family of type constructors, of type @TyCon@
649 These contain the tycon arity, but don't require a Unique.
652 They have a special family of constructors, of type
653 @Id@. Again these contain their arity but don't need a Unique.
656 There should be a magic way of generating the info tables and
657 entry code for all tuples.
659 But at the moment we just compile a Haskell source
660 file\srcloc{lib/prelude/...} containing declarations like:
663 data Tuple2 a b = Tup2 a b
664 data Tuple3 a b c = Tup3 a b c
665 data Tuple4 a b c d = Tup4 a b c d
668 The print-names associated with the magic @Id@s for tuple constructors
669 ``just happen'' to be the same as those generated by these
673 The instance environment should have a magic way to know
674 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
675 so on. \ToDo{Not implemented yet.}
678 There should also be a way to generate the appropriate code for each
679 of these instances, but (like the info tables and entry code) it is
680 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
684 mkTupleTy :: Boxity -> Int -> [Type] -> Type
685 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
687 unitTy = mkTupleTy Boxed 0 []
690 %************************************************************************
692 \subsection{Wired In Type Constructors for Representation Types}
694 %************************************************************************
696 The following code defines the wired in datatypes cross, plus, unit
697 and c_of needed for the generic methods.
699 Ok, so the basic story is that for each type constructor I need to
700 create 2 things - a TyCon and a DataCon and then we are basically
701 ok. There are going to be no arguments passed to these functions
702 because -well- there is nothing to pass to these functions.
706 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
708 crossDataCon :: DataCon
709 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
712 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
714 inlDataCon, inrDataCon :: DataCon
715 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
716 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy] plusTyCon
718 genUnitTyCon :: TyCon -- The "1" type constructor for generics
719 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
721 genUnitDataCon :: DataCon
722 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon