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, StrictnessMark(..), mkDataCon, dataConId )
107 import Var ( TyVar, tyVarKind )
108 import TyCon ( TyCon, AlgTyConFlavour(..), tyConDataCons,
109 mkTupleTyCon, isUnLiftedTyCon, mkAlgTyCon
112 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
114 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTys,
115 mkArrowKinds, liftedTypeKind, unliftedTypeKind,
116 splitTyConApp_maybe, repType,
118 import Unique ( incrUnique, mkTupleTyConUnique, mkTupleDataConUnique )
123 alpha_tyvar = [alphaTyVar]
125 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
129 %************************************************************************
131 \subsection{Wired in type constructors}
133 %************************************************************************
136 wiredInTyCons :: [TyCon]
137 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
139 data_tycons = genericTyCons ++
153 genericTyCons :: [TyCon]
154 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
157 tuple_tycons = unitTyCon : [tupleTyCon Boxed i | i <- [2..37] ]
158 unboxed_tuple_tycons = [tupleTyCon Unboxed i | i <- [1..37] ]
162 %************************************************************************
164 \subsection{mkWiredInTyCon}
166 %************************************************************************
169 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
170 pcRecDataTyCon = pcTyCon DataTyCon Recursive
172 pcTyCon new_or_data is_rec name tyvars argvrcs cons
175 tycon = mkAlgTyCon name kind
181 [] -- No record selectors
186 mod = nameModule name
187 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
188 gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
190 -- We generate names for the generic to/from Ids by incrementing
191 -- the TyCon unique. So each Prelude tycon needs 3 slots, one
192 -- for itself and two more for the generic Ids.
193 mk_tc_gen_info mod tc_uniq tc_name tycon
194 = mkTyConGenInfo tycon [name1, name2]
196 tc_occ_name = nameOccName tc_name
197 occ_name1 = mkGenOcc1 tc_occ_name
198 occ_name2 = mkGenOcc2 tc_occ_name
199 fn1_key = incrUnique tc_uniq
200 fn2_key = incrUnique fn1_key
201 name1 = mkWiredInName mod occ_name1 fn1_key
202 name2 = mkWiredInName mod occ_name2 fn2_key
204 pcDataCon :: Name -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> DataCon
205 -- The unique is the first of two free uniques;
206 -- the first is used for the datacon itself and the worker;
207 -- the second is used for the wrapper.
209 pcDataCon name tyvars context arg_tys tycon
212 data_con = mkDataCon name
213 [ NotMarkedStrict | a <- arg_tys ]
214 [ {- no labelled fields -} ]
215 tyvars context [] [] arg_tys tycon work_id wrap_id
217 wrap_rdr = nameRdrName name
218 wrap_occ = rdrNameOcc wrap_rdr
220 mod = nameModule name
221 wrap_id = mkDataConWrapId data_con
223 work_occ = mkWorkerOcc wrap_occ
224 work_key = incrUnique (nameUnique name)
225 work_name = mkWiredInName mod work_occ work_key
226 work_id = mkDataConId work_name data_con
230 %************************************************************************
232 \subsection[TysWiredIn-tuples]{The tuple types}
234 %************************************************************************
237 tupleTyCon :: Boxity -> Arity -> TyCon
238 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
239 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
240 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
242 tupleCon :: Boxity -> Arity -> DataCon
243 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
244 tupleCon Boxed i = snd (boxedTupleArr ! i)
245 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
247 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
248 boxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i) | i <- [0..mAX_TUPLE_SIZE]]
249 unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
251 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
252 mk_tuple boxity arity = (tycon, tuple_con)
254 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
255 tc_name = mkWiredInName mod (mkOccFS tcName name_str) tc_uniq
256 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
257 res_kind | isBoxed boxity = liftedTypeKind
258 | otherwise = unliftedTypeKind
260 tyvars | isBoxed boxity = take arity alphaTyVars
261 | otherwise = take arity openAlphaTyVars
263 tuple_con = pcDataCon name tyvars [] tyvar_tys tycon
264 tyvar_tys = mkTyVarTys tyvars
265 (mod_name, name_str) = mkTupNameStr boxity arity
266 name = mkWiredInName mod (mkOccFS dataName name_str) dc_uniq
267 tc_uniq = mkTupleTyConUnique boxity arity
268 dc_uniq = mkTupleDataConUnique boxity arity
269 mod = mkPrelModule mod_name
270 gen_info = mk_tc_gen_info mod tc_uniq tc_name tycon
272 unitTyCon = tupleTyCon Boxed 0
273 unitDataConId = dataConId (head (tyConDataCons unitTyCon))
275 pairTyCon = tupleTyCon Boxed 2
277 unboxedSingletonTyCon = tupleTyCon Unboxed 1
278 unboxedSingletonDataCon = tupleCon Unboxed 1
280 unboxedPairTyCon = tupleTyCon Unboxed 2
281 unboxedPairDataCon = tupleCon Unboxed 2
284 %************************************************************************
286 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
288 %************************************************************************
291 -- The Void type is represented as a data type with no constructors
292 -- It's a built in type (i.e. there's no way to define it in Haskell;
293 -- the nearest would be
295 -- data Void = -- No constructors!
297 -- ) It's lifted; there is only one value of this
298 -- type, namely "void", whose semantics is just bottom.
300 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
307 charTy = mkTyConTy charTyCon
309 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
310 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
312 stringTy = mkListTy charTy -- convenience only
316 intTy = mkTyConTy intTyCon
318 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
319 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
321 isIntTy :: Type -> Bool
322 isIntTy = isTyCon intTyConKey
327 wordTy = mkTyConTy wordTyCon
329 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
330 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
334 addrTy = mkTyConTy addrTyCon
336 addrTyCon = pcNonRecDataTyCon addrTyConName [] [] [addrDataCon]
337 addrDataCon = pcDataCon addrDataConName [] [] [addrPrimTy] addrTyCon
339 isAddrTy :: Type -> Bool
340 isAddrTy = isTyCon addrTyConKey
344 ptrTy = mkTyConTy ptrTyCon
346 ptrTyCon = pcNonRecDataTyCon ptrTyConName alpha_tyvar [(True,False)] [ptrDataCon]
347 ptrDataCon = pcDataCon ptrDataConName alpha_tyvar [] [addrPrimTy] ptrTyCon
351 funPtrTy = mkTyConTy funPtrTyCon
353 funPtrTyCon = pcNonRecDataTyCon funPtrTyConName alpha_tyvar [(True,False)] [funPtrDataCon]
354 funPtrDataCon = pcDataCon funPtrDataConName alpha_tyvar [] [addrPrimTy] funPtrTyCon
358 floatTy = mkTyConTy floatTyCon
360 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
361 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
363 isFloatTy :: Type -> Bool
364 isFloatTy = isTyCon floatTyConKey
368 doubleTy = mkTyConTy doubleTyCon
370 isDoubleTy :: Type -> Bool
371 isDoubleTy = isTyCon doubleTyConKey
373 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
374 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
379 = pcNonRecDataTyCon stablePtrTyConName
380 alpha_tyvar [(True,False)] [stablePtrDataCon]
383 = pcDataCon stablePtrDataConName
384 alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
389 = pcNonRecDataTyCon foreignObjTyConName
390 [] [] [foreignObjDataCon]
393 = pcDataCon foreignObjDataConName
394 [] [] [foreignObjPrimTy] foreignObjTyCon
396 isForeignObjTy :: Type -> Bool
397 isForeignObjTy = isTyCon foreignObjTyConKey
402 = pcNonRecDataTyCon foreignPtrTyConName
403 alpha_tyvar [(True,False)] [foreignPtrDataCon]
406 = pcDataCon foreignPtrDataConName
407 alpha_tyvar [] [foreignObjPrimTy] foreignPtrTyCon
409 isForeignPtrTy :: Type -> Bool
410 isForeignPtrTy = isTyCon foreignPtrTyConKey
413 %************************************************************************
415 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
417 %************************************************************************
419 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
422 integerTy = mkTyConTy integerTyCon
424 integerTyCon = pcNonRecDataTyCon integerTyConName
425 [] [] [smallIntegerDataCon, largeIntegerDataCon]
427 smallIntegerDataCon = pcDataCon smallIntegerDataConName
428 [] [] [intPrimTy] integerTyCon
429 largeIntegerDataCon = pcDataCon largeIntegerDataConName
430 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
433 isIntegerTy :: Type -> Bool
434 isIntegerTy = isTyCon integerTyConKey
438 %************************************************************************
440 \subsection[TysWiredIn-ext-type]{External types}
442 %************************************************************************
444 The compiler's foreign function interface supports the passing of a
445 restricted set of types as arguments and results (the restricting factor
449 isFFIArgumentTy :: DynFlags -> Bool -> Type -> Bool
450 -- Checks for valid argument type for a 'foreign import'
451 isFFIArgumentTy dflags is_safe ty
452 = checkRepTyCon (legalOutgoingTyCon dflags is_safe) ty
454 isFFIExternalTy :: Type -> Bool
455 -- Types that are allowed as arguments of a 'foreign export'
456 isFFIExternalTy ty = checkRepTyCon legalFEArgTyCon ty
458 isFFIImportResultTy :: DynFlags -> Type -> Bool
459 isFFIImportResultTy dflags ty
460 = checkRepTyCon (legalFIResultTyCon dflags) ty
462 isFFIExportResultTy :: Type -> Bool
463 isFFIExportResultTy ty = checkRepTyCon legalFEResultTyCon ty
465 isFFIDynArgumentTy :: Type -> Bool
466 -- The argument type of a foreign import dynamic must be Ptr, FunPtr, Addr,
467 -- or a newtype of either.
468 isFFIDynArgumentTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
470 isFFIDynResultTy :: Type -> Bool
471 -- The result type of a foreign export dynamic must be Ptr, FunPtr, Addr,
472 -- or a newtype of either.
473 isFFIDynResultTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
475 isFFILabelTy :: Type -> Bool
476 -- The type of a foreign label must be Ptr, FunPtr, Addr,
477 -- or a newtype of either.
478 isFFILabelTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
480 checkRepTyCon :: (TyCon -> Bool) -> Type -> Bool
481 -- look through newtypes
482 checkRepTyCon check_tc ty = checkTyCon check_tc (repType ty)
484 checkTyCon :: (TyCon -> Bool) -> Type -> Bool
485 checkTyCon check_tc ty = case splitTyConApp_maybe ty of
486 Just (tycon, _) -> check_tc tycon
489 isTyCon :: Unique -> Type -> Bool
490 isTyCon uniq ty = checkTyCon (\tc -> uniq == getUnique tc) ty
493 ----------------------------------------------
494 These chaps do the work; they are not exported
495 ----------------------------------------------
498 legalFEArgTyCon :: TyCon -> Bool
499 -- It's illegal to return foreign objects and (mutable)
500 -- bytearrays from a _ccall_ / foreign declaration
501 -- (or be passed them as arguments in foreign exported functions).
503 | getUnique tc `elem` [ foreignObjTyConKey, foreignPtrTyConKey,
504 byteArrayTyConKey, mutableByteArrayTyConKey ]
506 -- It's also illegal to make foreign exports that take unboxed
507 -- arguments. The RTS API currently can't invoke such things. --SDM 7/2000
509 = boxedMarshalableTyCon tc
511 legalFIResultTyCon :: DynFlags -> TyCon -> Bool
512 legalFIResultTyCon dflags tc
513 | getUnique tc `elem`
514 [ foreignObjTyConKey, foreignPtrTyConKey,
515 byteArrayTyConKey, mutableByteArrayTyConKey ] = False
516 | tc == unitTyCon = True
517 | otherwise = marshalableTyCon dflags tc
519 legalFEResultTyCon :: TyCon -> Bool
520 legalFEResultTyCon tc
521 | getUnique tc `elem`
522 [ foreignObjTyConKey, foreignPtrTyConKey,
523 byteArrayTyConKey, mutableByteArrayTyConKey ] = False
524 | tc == unitTyCon = True
525 | otherwise = boxedMarshalableTyCon tc
527 legalOutgoingTyCon :: DynFlags -> Bool -> TyCon -> Bool
528 -- Checks validity of types going from Haskell -> external world
529 -- The boolean is true for a 'safe' call (when we don't want to
530 -- pass Haskell pointers to the world)
531 legalOutgoingTyCon dflags be_safe tc
532 | be_safe && getUnique tc `elem` [byteArrayTyConKey, mutableByteArrayTyConKey]
535 = marshalableTyCon dflags tc
537 marshalableTyCon dflags tc
538 = (dopt Opt_GlasgowExts dflags && isUnLiftedTyCon tc)
539 || boxedMarshalableTyCon tc
541 boxedMarshalableTyCon tc
542 = getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey
543 , int32TyConKey, int64TyConKey
544 , wordTyConKey, word8TyConKey, word16TyConKey
545 , word32TyConKey, word64TyConKey
546 , floatTyConKey, doubleTyConKey
547 , addrTyConKey, ptrTyConKey, funPtrTyConKey
548 , charTyConKey, foreignObjTyConKey
551 , byteArrayTyConKey, mutableByteArrayTyConKey
557 %************************************************************************
559 \subsection[TysWiredIn-Bool]{The @Bool@ type}
561 %************************************************************************
563 An ordinary enumeration type, but deeply wired in. There are no
564 magical operations on @Bool@ (just the regular Prelude code).
566 {\em BEGIN IDLE SPECULATION BY SIMON}
568 This is not the only way to encode @Bool@. A more obvious coding makes
569 @Bool@ just a boxed up version of @Bool#@, like this:
572 data Bool = MkBool Bool#
575 Unfortunately, this doesn't correspond to what the Report says @Bool@
576 looks like! Furthermore, we get slightly less efficient code (I
577 think) with this coding. @gtInt@ would look like this:
580 gtInt :: Int -> Int -> Bool
581 gtInt x y = case x of I# x# ->
583 case (gtIntPrim x# y#) of
587 Notice that the result of the @gtIntPrim@ comparison has to be turned
588 into an integer (here called @b#@), and returned in a @MkBool@ box.
590 The @if@ expression would compile to this:
593 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
596 I think this code is a little less efficient than the previous code,
597 but I'm not certain. At all events, corresponding with the Report is
598 important. The interesting thing is that the language is expressive
599 enough to describe more than one alternative; and that a type doesn't
600 necessarily need to be a straightforwardly boxed version of its
601 primitive counterpart.
603 {\em END IDLE SPECULATION BY SIMON}
606 boolTy = mkTyConTy boolTyCon
608 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
609 [] [] [falseDataCon, trueDataCon]
611 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
612 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
614 falseDataConId = dataConId falseDataCon
615 trueDataConId = dataConId trueDataCon
618 %************************************************************************
620 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
622 %************************************************************************
624 Special syntax, deeply wired in, but otherwise an ordinary algebraic
627 data [] a = [] | a : (List a)
629 data (,) a b = (,,) a b
634 mkListTy :: Type -> Type
635 mkListTy ty = mkTyConApp listTyCon [ty]
637 listTyCon = pcRecDataTyCon listTyConName
638 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
640 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
641 consDataCon = pcDataCon consDataConName
642 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
643 -- Interesting: polymorphic recursion would help here.
644 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
645 -- gets the over-specific type (Type -> Type)
648 %************************************************************************
650 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
652 %************************************************************************
654 The tuple types are definitely magic, because they form an infinite
659 They have a special family of type constructors, of type @TyCon@
660 These contain the tycon arity, but don't require a Unique.
663 They have a special family of constructors, of type
664 @Id@. Again these contain their arity but don't need a Unique.
667 There should be a magic way of generating the info tables and
668 entry code for all tuples.
670 But at the moment we just compile a Haskell source
671 file\srcloc{lib/prelude/...} containing declarations like:
674 data Tuple2 a b = Tup2 a b
675 data Tuple3 a b c = Tup3 a b c
676 data Tuple4 a b c d = Tup4 a b c d
679 The print-names associated with the magic @Id@s for tuple constructors
680 ``just happen'' to be the same as those generated by these
684 The instance environment should have a magic way to know
685 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
686 so on. \ToDo{Not implemented yet.}
689 There should also be a way to generate the appropriate code for each
690 of these instances, but (like the info tables and entry code) it is
691 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
695 mkTupleTy :: Boxity -> Int -> [Type] -> Type
696 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
698 unitTy = mkTupleTy Boxed 0 []
701 %************************************************************************
703 \subsection{Wired In Type Constructors for Representation Types}
705 %************************************************************************
707 The following code defines the wired in datatypes cross, plus, unit
708 and c_of needed for the generic methods.
710 Ok, so the basic story is that for each type constructor I need to
711 create 2 things - a TyCon and a DataCon and then we are basically
712 ok. There are going to be no arguments passed to these functions
713 because -well- there is nothing to pass to these functions.
717 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
719 crossDataCon :: DataCon
720 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
723 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
725 inlDataCon, inrDataCon :: DataCon
726 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
727 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy] plusTyCon
729 genUnitTyCon :: TyCon -- The "1" type constructor for generics
730 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
732 genUnitDataCon :: DataCon
733 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon