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,
35 falseDataCon, falseDataConId,
60 unitTyCon, unitDataConId, pairTyCon,
61 unboxedSingletonTyCon, unboxedSingletonDataCon,
62 unboxedPairTyCon, unboxedPairDataCon,
65 genUnitTyCon, genUnitDataCon,
66 plusTyCon, inrDataCon, inlDataCon,
67 crossTyCon, crossDataCon,
71 trueDataCon, trueDataConId,
78 isFFIArgumentTy, -- :: DynFlags -> Bool -> Type -> Bool
79 isFFIImportResultTy, -- :: DynFlags -> Type -> Bool
80 isFFIExportResultTy, -- :: Type -> Bool
81 isFFIExternalTy, -- :: Type -> Bool
82 isFFIDynArgumentTy, -- :: Type -> Bool
83 isFFIDynResultTy, -- :: Type -> Bool
84 isFFILabelTy, -- :: Type -> Bool
85 isAddrTy, -- :: Type -> Bool
86 isForeignPtrTy -- :: Type -> Bool
90 #include "HsVersions.h"
92 import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
93 import {-# SOURCE #-} Generics( mkTyConGenInfo )
100 import Constants ( mAX_TUPLE_SIZE )
101 import Module ( mkPrelModule )
102 import Name ( Name, nameRdrName, nameUnique, nameOccName,
103 nameModule, mkWiredInName )
104 import OccName ( mkOccFS, tcName, dataName, mkWorkerOcc, mkGenOcc1, mkGenOcc2 )
105 import RdrName ( rdrNameOcc )
106 import DataCon ( DataCon, mkDataCon, dataConId )
107 import Demand ( StrictnessMark(..) )
108 import Var ( TyVar, tyVarKind )
109 import TyCon ( TyCon, AlgTyConFlavour(..), tyConDataCons,
110 mkTupleTyCon, isUnLiftedTyCon, mkAlgTyCon
113 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
115 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTys,
116 mkArrowKinds, liftedTypeKind, unliftedTypeKind,
117 splitTyConApp_maybe, repType,
119 import Unique ( incrUnique, mkTupleTyConUnique, mkTupleDataConUnique )
124 alpha_tyvar = [alphaTyVar]
126 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
130 %************************************************************************
132 \subsection{Wired in type constructors}
134 %************************************************************************
137 wiredInTyCons :: [TyCon]
138 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
140 data_tycons = genericTyCons ++
154 genericTyCons :: [TyCon]
155 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
158 tuple_tycons = unitTyCon : [tupleTyCon Boxed i | i <- [2..mAX_TUPLE_SIZE] ]
159 unboxed_tuple_tycons = [tupleTyCon Unboxed i | i <- [1..mAX_TUPLE_SIZE] ]
163 %************************************************************************
165 \subsection{mkWiredInTyCon}
167 %************************************************************************
170 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
171 pcRecDataTyCon = pcTyCon DataTyCon Recursive
173 pcTyCon new_or_data is_rec name tyvars argvrcs cons
176 tycon = mkAlgTyCon name kind
182 [] -- No record selectors
187 mod = nameModule name
188 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
189 gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
191 -- We generate names for the generic to/from Ids by incrementing
192 -- the TyCon unique. So each Prelude tycon needs 3 slots, one
193 -- for itself and two more for the generic Ids.
194 mk_tc_gen_info mod tc_uniq tc_name tycon
195 = mkTyConGenInfo tycon [name1, name2]
197 tc_occ_name = nameOccName tc_name
198 occ_name1 = mkGenOcc1 tc_occ_name
199 occ_name2 = mkGenOcc2 tc_occ_name
200 fn1_key = incrUnique tc_uniq
201 fn2_key = incrUnique fn1_key
202 name1 = mkWiredInName mod occ_name1 fn1_key
203 name2 = mkWiredInName mod occ_name2 fn2_key
205 pcDataCon :: Name -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> DataCon
206 -- The unique is the first of two free uniques;
207 -- the first is used for the datacon itself and the worker;
208 -- the second is used for the wrapper.
210 pcDataCon name tyvars context arg_tys tycon
213 data_con = mkDataCon name
214 [ NotMarkedStrict | a <- arg_tys ]
215 [ {- no labelled fields -} ]
216 tyvars context [] [] arg_tys tycon work_id wrap_id
218 wrap_rdr = nameRdrName name
219 wrap_occ = rdrNameOcc wrap_rdr
221 mod = nameModule name
222 wrap_id = mkDataConWrapId data_con
224 work_occ = mkWorkerOcc wrap_occ
225 work_key = incrUnique (nameUnique name)
226 work_name = mkWiredInName mod work_occ work_key
227 work_id = mkDataConId work_name data_con
231 %************************************************************************
233 \subsection[TysWiredIn-tuples]{The tuple types}
235 %************************************************************************
238 tupleTyCon :: Boxity -> Arity -> TyCon
239 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
240 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
241 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
243 tupleCon :: Boxity -> Arity -> DataCon
244 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
245 tupleCon Boxed i = snd (boxedTupleArr ! i)
246 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
248 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
249 boxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i) | i <- [0..mAX_TUPLE_SIZE]]
250 unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
252 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
253 mk_tuple boxity arity = (tycon, tuple_con)
255 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
256 tc_name = mkWiredInName mod (mkOccFS tcName name_str) tc_uniq
257 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
258 res_kind | isBoxed boxity = liftedTypeKind
259 | otherwise = unliftedTypeKind
261 tyvars | isBoxed boxity = take arity alphaTyVars
262 | otherwise = take arity openAlphaTyVars
264 tuple_con = pcDataCon name tyvars [] tyvar_tys tycon
265 tyvar_tys = mkTyVarTys tyvars
266 (mod_name, name_str) = mkTupNameStr boxity arity
267 name = mkWiredInName mod (mkOccFS dataName name_str) dc_uniq
268 tc_uniq = mkTupleTyConUnique boxity arity
269 dc_uniq = mkTupleDataConUnique boxity arity
270 mod = mkPrelModule mod_name
271 gen_info = mk_tc_gen_info mod tc_uniq tc_name tycon
273 unitTyCon = tupleTyCon Boxed 0
274 unitDataConId = dataConId (head (tyConDataCons unitTyCon))
276 pairTyCon = tupleTyCon Boxed 2
278 unboxedSingletonTyCon = tupleTyCon Unboxed 1
279 unboxedSingletonDataCon = tupleCon Unboxed 1
281 unboxedPairTyCon = tupleTyCon Unboxed 2
282 unboxedPairDataCon = tupleCon Unboxed 2
285 %************************************************************************
287 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
289 %************************************************************************
292 -- The Void type is represented as a data type with no constructors
293 -- It's a built in type (i.e. there's no way to define it in Haskell;
294 -- the nearest would be
296 -- data Void = -- No constructors!
298 -- ) It's lifted; there is only one value of this
299 -- type, namely "void", whose semantics is just bottom.
301 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
308 charTy = mkTyConTy charTyCon
310 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
311 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
313 stringTy = mkListTy charTy -- convenience only
317 intTy = mkTyConTy intTyCon
319 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
320 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
322 isIntTy :: Type -> Bool
323 isIntTy = isTyCon intTyConKey
328 wordTy = mkTyConTy wordTyCon
330 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
331 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
335 addrTy = mkTyConTy addrTyCon
337 addrTyCon = pcNonRecDataTyCon addrTyConName [] [] [addrDataCon]
338 addrDataCon = pcDataCon addrDataConName [] [] [addrPrimTy] addrTyCon
340 isAddrTy :: Type -> Bool
341 isAddrTy = isTyCon addrTyConKey
345 ptrTy = mkTyConTy ptrTyCon
347 ptrTyCon = pcNonRecDataTyCon ptrTyConName alpha_tyvar [(True,False)] [ptrDataCon]
348 ptrDataCon = pcDataCon ptrDataConName alpha_tyvar [] [addrPrimTy] ptrTyCon
352 funPtrTy = mkTyConTy funPtrTyCon
354 funPtrTyCon = pcNonRecDataTyCon funPtrTyConName alpha_tyvar [(True,False)] [funPtrDataCon]
355 funPtrDataCon = pcDataCon funPtrDataConName alpha_tyvar [] [addrPrimTy] funPtrTyCon
359 floatTy = mkTyConTy floatTyCon
361 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
362 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
364 isFloatTy :: Type -> Bool
365 isFloatTy = isTyCon floatTyConKey
369 doubleTy = mkTyConTy doubleTyCon
371 isDoubleTy :: Type -> Bool
372 isDoubleTy = isTyCon doubleTyConKey
374 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
375 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
380 = pcNonRecDataTyCon stablePtrTyConName
381 alpha_tyvar [(True,False)] [stablePtrDataCon]
384 = pcDataCon stablePtrDataConName
385 alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
390 = pcNonRecDataTyCon foreignObjTyConName
391 [] [] [foreignObjDataCon]
394 = pcDataCon foreignObjDataConName
395 [] [] [foreignObjPrimTy] foreignObjTyCon
397 isForeignObjTy :: Type -> Bool
398 isForeignObjTy = isTyCon foreignObjTyConKey
403 = pcNonRecDataTyCon foreignPtrTyConName
404 alpha_tyvar [(True,False)] [foreignPtrDataCon]
407 = pcDataCon foreignPtrDataConName
408 alpha_tyvar [] [foreignObjPrimTy] foreignPtrTyCon
410 isForeignPtrTy :: Type -> Bool
411 isForeignPtrTy = isTyCon foreignPtrTyConKey
414 %************************************************************************
416 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
418 %************************************************************************
420 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
423 integerTy = mkTyConTy integerTyCon
425 integerTyCon = pcNonRecDataTyCon integerTyConName
426 [] [] [smallIntegerDataCon, largeIntegerDataCon]
428 smallIntegerDataCon = pcDataCon smallIntegerDataConName
429 [] [] [intPrimTy] integerTyCon
430 largeIntegerDataCon = pcDataCon largeIntegerDataConName
431 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
434 isIntegerTy :: Type -> Bool
435 isIntegerTy = isTyCon integerTyConKey
439 %************************************************************************
441 \subsection[TysWiredIn-ext-type]{External types}
443 %************************************************************************
445 The compiler's foreign function interface supports the passing of a
446 restricted set of types as arguments and results (the restricting factor
450 isFFIArgumentTy :: DynFlags -> Bool -> Type -> Bool
451 -- Checks for valid argument type for a 'foreign import'
452 isFFIArgumentTy dflags is_safe ty
453 = checkRepTyCon (legalOutgoingTyCon dflags is_safe) ty
455 isFFIExternalTy :: Type -> Bool
456 -- Types that are allowed as arguments of a 'foreign export'
457 isFFIExternalTy ty = checkRepTyCon legalFEArgTyCon ty
459 isFFIImportResultTy :: DynFlags -> Type -> Bool
460 isFFIImportResultTy dflags ty
461 = checkRepTyCon (legalFIResultTyCon dflags) ty
463 isFFIExportResultTy :: Type -> Bool
464 isFFIExportResultTy ty = checkRepTyCon legalFEResultTyCon ty
466 isFFIDynArgumentTy :: Type -> Bool
467 -- The argument type of a foreign import dynamic must be Ptr, FunPtr, Addr,
468 -- or a newtype of either.
469 isFFIDynArgumentTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
471 isFFIDynResultTy :: Type -> Bool
472 -- The result type of a foreign export dynamic must be Ptr, FunPtr, Addr,
473 -- or a newtype of either.
474 isFFIDynResultTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
476 isFFILabelTy :: Type -> Bool
477 -- The type of a foreign label must be Ptr, FunPtr, Addr,
478 -- or a newtype of either.
479 isFFILabelTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
481 checkRepTyCon :: (TyCon -> Bool) -> Type -> Bool
482 -- look through newtypes
483 checkRepTyCon check_tc ty = checkTyCon check_tc (repType ty)
485 checkTyCon :: (TyCon -> Bool) -> Type -> Bool
486 checkTyCon check_tc ty = case splitTyConApp_maybe ty of
487 Just (tycon, _) -> check_tc tycon
490 isTyCon :: Unique -> Type -> Bool
491 isTyCon uniq ty = checkTyCon (\tc -> uniq == getUnique tc) ty
494 ----------------------------------------------
495 These chaps do the work; they are not exported
496 ----------------------------------------------
499 legalFEArgTyCon :: TyCon -> Bool
500 -- It's illegal to return foreign objects and (mutable)
501 -- bytearrays from a _ccall_ / foreign declaration
502 -- (or be passed them as arguments in foreign exported functions).
504 | getUnique tc `elem` [ foreignObjTyConKey, foreignPtrTyConKey,
505 byteArrayTyConKey, mutableByteArrayTyConKey ]
507 -- It's also illegal to make foreign exports that take unboxed
508 -- arguments. The RTS API currently can't invoke such things. --SDM 7/2000
510 = boxedMarshalableTyCon tc
512 legalFIResultTyCon :: DynFlags -> TyCon -> Bool
513 legalFIResultTyCon dflags tc
514 | getUnique tc `elem`
515 [ foreignObjTyConKey, foreignPtrTyConKey,
516 byteArrayTyConKey, mutableByteArrayTyConKey ] = False
517 | tc == unitTyCon = True
518 | otherwise = marshalableTyCon dflags tc
520 legalFEResultTyCon :: TyCon -> Bool
521 legalFEResultTyCon tc
522 | getUnique tc `elem`
523 [ foreignObjTyConKey, foreignPtrTyConKey,
524 byteArrayTyConKey, mutableByteArrayTyConKey ] = False
525 | tc == unitTyCon = True
526 | otherwise = boxedMarshalableTyCon tc
528 legalOutgoingTyCon :: DynFlags -> Bool -> TyCon -> Bool
529 -- Checks validity of types going from Haskell -> external world
530 -- The boolean is true for a 'safe' call (when we don't want to
531 -- pass Haskell pointers to the world)
532 legalOutgoingTyCon dflags be_safe tc
533 | be_safe && getUnique tc `elem` [byteArrayTyConKey, mutableByteArrayTyConKey]
536 = marshalableTyCon dflags tc
538 marshalableTyCon dflags tc
539 = (dopt Opt_GlasgowExts dflags && isUnLiftedTyCon tc)
540 || boxedMarshalableTyCon tc
542 boxedMarshalableTyCon tc
543 = getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey
544 , int32TyConKey, int64TyConKey
545 , wordTyConKey, word8TyConKey, word16TyConKey
546 , word32TyConKey, word64TyConKey
547 , floatTyConKey, doubleTyConKey
548 , addrTyConKey, ptrTyConKey, funPtrTyConKey
549 , charTyConKey, foreignObjTyConKey
552 , byteArrayTyConKey, mutableByteArrayTyConKey
558 %************************************************************************
560 \subsection[TysWiredIn-Bool]{The @Bool@ type}
562 %************************************************************************
564 An ordinary enumeration type, but deeply wired in. There are no
565 magical operations on @Bool@ (just the regular Prelude code).
567 {\em BEGIN IDLE SPECULATION BY SIMON}
569 This is not the only way to encode @Bool@. A more obvious coding makes
570 @Bool@ just a boxed up version of @Bool#@, like this:
573 data Bool = MkBool Bool#
576 Unfortunately, this doesn't correspond to what the Report says @Bool@
577 looks like! Furthermore, we get slightly less efficient code (I
578 think) with this coding. @gtInt@ would look like this:
581 gtInt :: Int -> Int -> Bool
582 gtInt x y = case x of I# x# ->
584 case (gtIntPrim x# y#) of
588 Notice that the result of the @gtIntPrim@ comparison has to be turned
589 into an integer (here called @b#@), and returned in a @MkBool@ box.
591 The @if@ expression would compile to this:
594 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
597 I think this code is a little less efficient than the previous code,
598 but I'm not certain. At all events, corresponding with the Report is
599 important. The interesting thing is that the language is expressive
600 enough to describe more than one alternative; and that a type doesn't
601 necessarily need to be a straightforwardly boxed version of its
602 primitive counterpart.
604 {\em END IDLE SPECULATION BY SIMON}
607 boolTy = mkTyConTy boolTyCon
609 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
610 [] [] [falseDataCon, trueDataCon]
612 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
613 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
615 falseDataConId = dataConId falseDataCon
616 trueDataConId = dataConId trueDataCon
619 %************************************************************************
621 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
623 %************************************************************************
625 Special syntax, deeply wired in, but otherwise an ordinary algebraic
628 data [] a = [] | a : (List a)
630 data (,) a b = (,,) a b
635 mkListTy :: Type -> Type
636 mkListTy ty = mkTyConApp listTyCon [ty]
638 listTyCon = pcRecDataTyCon listTyConName
639 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
641 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
642 consDataCon = pcDataCon consDataConName
643 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
644 -- Interesting: polymorphic recursion would help here.
645 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
646 -- gets the over-specific type (Type -> Type)
649 %************************************************************************
651 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
653 %************************************************************************
655 The tuple types are definitely magic, because they form an infinite
660 They have a special family of type constructors, of type @TyCon@
661 These contain the tycon arity, but don't require a Unique.
664 They have a special family of constructors, of type
665 @Id@. Again these contain their arity but don't need a Unique.
668 There should be a magic way of generating the info tables and
669 entry code for all tuples.
671 But at the moment we just compile a Haskell source
672 file\srcloc{lib/prelude/...} containing declarations like:
675 data Tuple2 a b = Tup2 a b
676 data Tuple3 a b c = Tup3 a b c
677 data Tuple4 a b c d = Tup4 a b c d
680 The print-names associated with the magic @Id@s for tuple constructors
681 ``just happen'' to be the same as those generated by these
685 The instance environment should have a magic way to know
686 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
687 so on. \ToDo{Not implemented yet.}
690 There should also be a way to generate the appropriate code for each
691 of these instances, but (like the info tables and entry code) it is
692 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
696 mkTupleTy :: Boxity -> Int -> [Type] -> Type
697 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
699 unitTy = mkTupleTy Boxed 0 []
702 %************************************************************************
704 \subsection{Wired In Type Constructors for Representation Types}
706 %************************************************************************
708 The following code defines the wired in datatypes cross, plus, unit
709 and c_of needed for the generic methods.
711 Ok, so the basic story is that for each type constructor I need to
712 create 2 things - a TyCon and a DataCon and then we are basically
713 ok. There are going to be no arguments passed to these functions
714 because -well- there is nothing to pass to these functions.
718 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
720 crossDataCon :: DataCon
721 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
724 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
726 inlDataCon, inrDataCon :: DataCon
727 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
728 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy] plusTyCon
730 genUnitTyCon :: TyCon -- The "1" type constructor for generics
731 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
733 genUnitDataCon :: DataCon
734 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon