[project @ 2000-10-12 11:47:25 by sewardj]
[ghc-hetmet.git] / ghc / compiler / prelude / TysWiredIn.lhs
index a03554c..afd537f 100644 (file)
@@ -11,6 +11,8 @@ types and operations.''
 
 \begin{code}
 module TysWiredIn (
+       wiredInTyCons, genericTyCons,
+
        addrDataCon,
        addrTy,
        addrTyCon,
@@ -24,7 +26,7 @@ module TysWiredIn (
        doubleTy,
        isDoubleTy,
        doubleTyCon,
-       falseDataCon,
+       falseDataCon, falseDataConId,
        floatDataCon,
        floatTy,
        isFloatTy,
@@ -34,17 +36,11 @@ module TysWiredIn (
        intTy,
        intTyCon,
        isIntTy,
-       inIntRange,
-
-       int8TyCon,
-       int16TyCon,
-       int32TyCon,
-
-       int64TyCon,
 
        integerTy,
        integerTyCon,
-       integerDataCon,
+       smallIntegerDataCon,
+       largeIntegerDataCon,
        isIntegerTy,
 
        listTyCon,
@@ -54,196 +50,232 @@ module TysWiredIn (
 
        -- tuples
        mkTupleTy,
-       tupleTyCon, tupleCon, unitTyCon, unitDataCon, pairTyCon, pairDataCon,
-
-       -- unboxed tuples
-       mkUnboxedTupleTy,
-       unboxedTupleTyCon, unboxedTupleCon, 
+       tupleTyCon, tupleCon, 
+       unitTyCon, unitDataConId, pairTyCon, 
+       unboxedSingletonTyCon, unboxedSingletonDataCon,
        unboxedPairTyCon, unboxedPairDataCon,
 
+       -- Generics
+        genUnitTyCon, genUnitDataCon, 
+       plusTyCon, inrDataCon, inlDataCon,
+       crossTyCon, crossDataCon,
+
        stablePtrTyCon,
        stringTy,
-       trueDataCon,
+       trueDataCon, trueDataConId,
        unitTy,
        voidTy,
        wordDataCon,
        wordTy,
        wordTyCon,
 
-       word8TyCon,
-       word16TyCon,
-       word32TyCon,
-       word64TyCon,
-       
-       isFFIArgumentTy,  -- :: Type -> Bool
-       isFFIResultTy,    -- :: Type -> Bool
-       isFFIExternalTy,  -- :: Type -> Bool
-       isAddrTy,         -- :: Type -> Bool
+       isFFIArgumentTy,    -- :: Bool -> Type -> Bool
+       isFFIResultTy,      -- :: Type -> Bool
+       isFFIExternalTy,    -- :: Type -> Bool
+        isFFIDynArgumentTy, -- :: Type -> Bool
+       isFFIDynResultTy,   -- :: Type -> Bool
+       isFFILabelTy,       -- :: Type -> Bool
+       isAddrTy,           -- :: Type -> Bool
+       isForeignObjTy      -- :: Type -> Bool
 
     ) where
 
 #include "HsVersions.h"
 
-import {-# SOURCE #-} MkId( mkDataConId )
+import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
+import {-# SOURCE #-} Generics( mkTyConGenInfo )
 
 -- friends:
-import PrelMods
+import PrelNames
 import TysPrim
 
 -- others:
 import Constants       ( mAX_TUPLE_SIZE )
-import Name            ( Module, varOcc, mkWiredInTyConName, mkWiredInIdName )
-import DataCon         ( DataCon, mkDataCon )
+import Module          ( Module, mkPrelModule )
+import Name            ( mkWiredInTyConName, mkWiredInIdName, nameOccName )
+import OccName         ( mkOccFS, tcName, dataName, mkWorkerOcc, mkGenOcc1, mkGenOcc2 )
+import RdrName         ( RdrName, mkPreludeQual, rdrNameOcc, rdrNameModule )
+import DataCon         ( DataCon, StrictnessMark(..),  mkDataCon, dataConId )
 import Var             ( TyVar, tyVarKind )
-import TyCon           ( TyCon, mkAlgTyCon, mkSynTyCon, mkTupleTyCon )
-import BasicTypes      ( Arity, NewOrData(..), 
-                         RecFlag(..), StrictnessMark(..) )
+import TyCon           ( TyCon, AlgTyConFlavour(..), ArgVrcs, tyConDataCons,
+                         mkSynTyCon, mkTupleTyCon, 
+                         isUnLiftedTyCon, mkAlgTyConRep,tyConName
+                       )
+
+import BasicTypes      ( Arity, RecFlag(..), EP(..), Boxity(..), isBoxed )
+
 import Type            ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys, 
                          mkArrowKinds, boxedTypeKind, unboxedTypeKind,
-                         mkFunTy, mkFunTys, isUnLiftedType,
-                         splitTyConApp_maybe, splitAlgTyConApp_maybe,
-                         ThetaType, TauType )
-import PrimRep         ( PrimRep(..) )
-import Unique
-import CmdLineOpts      ( opt_GlasgowExts )
-import Util            ( assoc )
-import Panic           ( panic )
+                         mkFunTy, mkFunTys, 
+                         splitTyConApp_maybe, repType, mkTyVarTy,
+                         TauType, ClassContext )
+import Unique          ( incrUnique, mkTupleTyConUnique, mkTupleDataConUnique )
+import PrelNames
+import CmdLineOpts      ( DynFlags, dopt_GlasgowExts )
 import Array
+import Maybe           ( fromJust )
+import FiniteMap       ( lookupFM )
 
 alpha_tyvar      = [alphaTyVar]
 alpha_ty         = [alphaTy]
 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
-
-pcRecDataTyCon, pcNonRecDataTyCon, pcNonRecNewTyCon
-       :: Unique{-TyConKey-} -> Module -> FAST_STRING
-       -> [TyVar] -> [DataCon] -> TyCon
-
-pcRecDataTyCon    = pcTyCon DataType Recursive
-pcNonRecDataTyCon = pcTyCon DataType NonRecursive
-pcNonRecNewTyCon  = pcTyCon NewType  NonRecursive
-
-pcTyCon new_or_data is_rec key mod str tyvars cons
-  = tycon
-  where
-    tycon = mkAlgTyCon name kind 
-               tyvars 
-               []              -- No context
-               cons
-               []              -- No derivings
-               Nothing         -- Not a dictionary
-               new_or_data
-               is_rec
-
-    name = mkWiredInTyConName key mod str tycon
-    kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
-
-pcSynTyCon key mod str kind arity tyvars expansion
-  = tycon
-  where
-    tycon = mkSynTyCon name kind arity tyvars expansion
-    name  = mkWiredInTyConName key mod str tycon
-
-pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
-         -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> DataCon
-pcDataCon key mod str tyvars context arg_tys tycon
-  = data_con
-  where
-    data_con = mkDataCon name 
-               [ NotMarkedStrict | a <- arg_tys ]
-               [ {- no labelled fields -} ]
-               tyvars context [] [] arg_tys tycon id
-    name = mkWiredInIdName key mod (varOcc str) id
-    id   = mkDataConId data_con
 \end{code}
 
+
 %************************************************************************
 %*                                                                     *
-\subsection[TysWiredIn-tuples]{The tuple types}
+\subsection{Wired in type constructors}
 %*                                                                     *
 %************************************************************************
 
 \begin{code}
-tupleTyCon :: Arity -> TyCon
-tupleTyCon i | i > mAX_TUPLE_SIZE = fst (mk_tuple i)   -- Build one specially
-            | otherwise          = tupleTyConArr!i
-
-tupleCon :: Arity -> DataCon
-tupleCon i | i > mAX_TUPLE_SIZE = snd (mk_tuple i)     -- Build one specially
-          | otherwise          = tupleConArr!i
-
-tupleTyCons :: [TyCon]
-tupleTyCons = elems tupleTyConArr
+wiredInTyCons :: [TyCon]
+wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
+
+data_tycons = genericTyCons ++
+             [ addrTyCon
+             , boolTyCon
+             , charTyCon
+             , doubleTyCon
+             , floatTyCon
+             , intTyCon
+             , integerTyCon
+             , listTyCon
+             , wordTyCon
+             ]
+
+genericTyCons :: [TyCon]
+genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
+
+
+tuple_tycons = unitTyCon : [tupleTyCon Boxed i | i <- [2..37] ]
+unboxed_tuple_tycons = [tupleTyCon Unboxed i | i <- [1..37] ]
+\end{code}
 
-tupleTyConArr :: Array Int TyCon
-tupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst tuples)
 
-tupleConArr :: Array Int DataCon
-tupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd tuples)
+%************************************************************************
+%*                                                                      *
+\subsection{mkWiredInTyCon}
+%*                                                                      *
+%************************************************************************
 
-tuples :: [(TyCon,DataCon)]
-tuples = [mk_tuple i | i <- [0..mAX_TUPLE_SIZE]]
+\begin{code}
+pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
+pcRecDataTyCon = pcTyCon DataTyCon Recursive
 
-mk_tuple :: Int -> (TyCon,DataCon)
-mk_tuple arity = (tycon, tuple_con)
+pcTyCon new_or_data is_rec key rdr_name tyvars argvrcs cons
+  = tycon
+  where
+    tycon = mkAlgTyConRep name kind
+                tyvars
+                []              -- No context
+                argvrcs
+                cons
+                (length cons)
+                []              -- No derivings
+                new_or_data
+                is_rec
+               gen_info
+
+    mod      = mkPrelModule (rdrNameModule rdr_name)
+    occ      = rdrNameOcc rdr_name
+    name     = mkWiredInTyConName key mod occ tycon
+    kind     = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
+    gen_info = mk_tc_gen_info mod key name tycon
+
+pcDataCon :: Unique    -- DataConKey
+         -> RdrName    -- Qualified
+          -> [TyVar] -> ClassContext -> [TauType] -> TyCon -> DataCon
+-- The unique is the first of two free uniques;
+-- the first is used for the datacon itself and the worker;
+-- the second is used for the wrapper.
+
+pcDataCon wrap_key rdr_name tyvars context arg_tys tycon
+  = data_con
   where
-       tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con True
-       tc_name = mkWiredInTyConName tc_uniq mod_name name_str tycon
-       tc_kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
+    mod      = mkPrelModule (rdrNameModule rdr_name)
+    wrap_occ = rdrNameOcc rdr_name
 
-       tuple_con = pcDataCon dc_uniq mod_name name_str tyvars [] tyvar_tys tycon
-       tyvars    = take arity alphaTyVars
-       tyvar_tys = mkTyVarTys tyvars
-       (mod_name, name_str) = mkTupNameStr arity
-       tc_uniq   = mkTupleTyConUnique   arity
-       dc_uniq   = mkTupleDataConUnique arity
+    data_con = mkDataCon wrap_name
+                [ NotMarkedStrict | a <- arg_tys ]
+                [ {- no labelled fields -} ]
+                tyvars context [] [] arg_tys tycon work_id wrap_id
 
-unitTyCon = tupleTyCon 0
-pairTyCon = tupleTyCon 2
+    work_occ  = mkWorkerOcc wrap_occ
+    work_key  = incrUnique wrap_key
+    work_name = mkWiredInIdName work_key mod work_occ work_id
+    work_id   = mkDataConId work_name data_con
 
-unitDataCon = tupleCon 0
-pairDataCon = tupleCon 2
+    wrap_name = mkWiredInIdName wrap_key mod wrap_occ wrap_id
+    wrap_id   = mkDataConWrapId data_con
 \end{code}
 
+
 %************************************************************************
 %*                                                                     *
-\subsection[TysWiredIn-ubx-tuples]{Unboxed Tuple Types}
+\subsection[TysWiredIn-tuples]{The tuple types}
 %*                                                                     *
 %************************************************************************
 
 \begin{code}
-unboxedTupleTyCon :: Arity -> TyCon
-unboxedTupleTyCon i | i > mAX_TUPLE_SIZE = fst (mk_unboxed_tuple i)
-                   | otherwise          = unboxedTupleTyConArr!i
-
-unboxedTupleCon :: Arity -> DataCon
-unboxedTupleCon i | i > mAX_TUPLE_SIZE = snd (mk_unboxed_tuple i)
-                 | otherwise          = unboxedTupleConArr!i
-
-unboxedTupleTyConArr :: Array Int TyCon
-unboxedTupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst ubx_tuples)
-
-unboxedTupleConArr :: Array Int DataCon
-unboxedTupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd ubx_tuples)
-
-ubx_tuples :: [(TyCon,DataCon)]
-ubx_tuples = [mk_unboxed_tuple i | i <- [0..mAX_TUPLE_SIZE]]
-
-mk_unboxed_tuple :: Int -> (TyCon,DataCon)
-mk_unboxed_tuple arity = (tycon, tuple_con)
+tupleTyCon :: Boxity -> Arity -> TyCon
+tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i)     -- Build one specially
+tupleTyCon Boxed   i = fst (boxedTupleArr   ! i)
+tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
+
+tupleCon :: Boxity -> Arity -> DataCon
+tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i)       -- Build one specially
+tupleCon Boxed   i = snd (boxedTupleArr   ! i)
+tupleCon Unboxed i = snd (unboxedTupleArr ! i)
+
+boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
+boxedTupleArr   = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i)   | i <- [0..mAX_TUPLE_SIZE]]
+unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
+
+mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
+mk_tuple boxity arity = (tycon, tuple_con)
   where
-       tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con False
-       tc_name = mkWiredInTyConName tc_uniq mod_name name_str tycon
-       tc_kind = mkArrowKinds (map tyVarKind tyvars) unboxedTypeKind
+       tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info 
+       tc_name = mkWiredInTyConName tc_uniq mod (mkOccFS tcName name_str) tycon
+       tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
+       res_kind | isBoxed boxity = boxedTypeKind
+                | otherwise      = unboxedTypeKind
 
-       tuple_con = pcDataCon dc_uniq mod_name name_str tyvars [] tyvar_tys tycon
-       tyvars    = take arity openAlphaTyVars
-       tyvar_tys = mkTyVarTys tyvars
-       (mod_name, name_str) = mkUbxTupNameStr arity
-       tc_uniq   = mkUbxTupleTyConUnique   arity
-       dc_uniq   = mkUbxTupleDataConUnique arity
+       tyvars   | isBoxed boxity = take arity alphaTyVars
+                | otherwise      = take arity openAlphaTyVars
 
-unboxedPairTyCon   = unboxedTupleTyCon 2
-unboxedPairDataCon = unboxedTupleCon 2
+       tuple_con = pcDataCon dc_uniq rdr_name tyvars [] tyvar_tys tycon
+       tyvar_tys = mkTyVarTys tyvars
+       (mod_name, name_str) = mkTupNameStr boxity arity
+       rdr_name  = mkPreludeQual dataName mod_name name_str
+       tc_uniq   = mkTupleTyConUnique   boxity arity
+       dc_uniq   = mkTupleDataConUnique boxity arity
+       mod       = mkPrelModule mod_name
+       gen_info  = mk_tc_gen_info mod tc_uniq tc_name tycon
+
+mk_tc_gen_info mod tc_uniq tc_name tycon
+  = gen_info
+  where
+       tc_occ_name = nameOccName tc_name
+       occ_name1   = mkGenOcc1 tc_occ_name
+       occ_name2   = mkGenOcc2 tc_occ_name
+       fn1_key     = incrUnique tc_uniq
+       fn2_key     = incrUnique fn1_key
+       name1       = mkWiredInIdName fn1_key mod occ_name1 id1
+       name2       = mkWiredInIdName fn2_key mod occ_name2 id2
+       gen_info    = mkTyConGenInfo tycon name1 name2
+       Just (EP id1 id2) = gen_info
+
+unitTyCon     = tupleTyCon Boxed 0
+unitDataConId = dataConId (head (tyConDataCons unitTyCon))
+
+pairTyCon = tupleTyCon Boxed 2
+
+unboxedSingletonTyCon   = tupleTyCon Unboxed 1
+unboxedSingletonDataCon = tupleCon   Unboxed 1
+
+unboxedPairTyCon   = tupleTyCon Unboxed 2
+unboxedPairDataCon = tupleCon   Unboxed 2
 \end{code}
 
 %************************************************************************
@@ -271,8 +303,8 @@ voidTy = unitTy
 \begin{code}
 charTy = mkTyConTy charTyCon
 
-charTyCon = pcNonRecDataTyCon charTyConKey  pREL_BASE  SLIT("Char") [] [charDataCon]
-charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon
+charTyCon   = pcNonRecDataTyCon charTyConKey charTyCon_RDR [] [] [charDataCon]
+charDataCon = pcDataCon charDataConKey charDataCon_RDR [] [] [charPrimTy] charTyCon
 
 stringTy = mkListTy charTy -- convenience only
 \end{code}
@@ -280,122 +312,72 @@ stringTy = mkListTy charTy -- convenience only
 \begin{code}
 intTy = mkTyConTy intTyCon 
 
-intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [intDataCon]
-intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
+intTyCon = pcNonRecDataTyCon intTyConKey intTyCon_RDR [] [] [intDataCon]
+intDataCon = pcDataCon intDataConKey mkInt_RDR [] [] [intPrimTy] intTyCon
 
 isIntTy :: Type -> Bool
-isIntTy ty
-  = case (splitAlgTyConApp_maybe ty) of
-       Just (tycon, [], _) -> getUnique tycon == intTyConKey
-       _                   -> False
-
-inIntRange :: Integer -> Bool  -- Tells if an integer lies in the legal range of Ints
-inIntRange i = (min_int <= i) && (i <= max_int)
-
-max_int, min_int :: Integer
-max_int = toInteger maxInt  
-min_int = toInteger minInt
-
-int8TyCon = pcNonRecDataTyCon int8TyConKey iNT SLIT("Int8") [] [int8DataCon]
-  where
-   int8DataCon = pcDataCon int8DataConKey iNT SLIT("I8#") [] [] [intPrimTy] int8TyCon
-
-int16TyCon = pcNonRecDataTyCon int16TyConKey iNT SLIT("Int16") [] [int16DataCon]
-  where
-   int16DataCon = pcDataCon int16DataConKey iNT SLIT("I16#") [] [] [intPrimTy] int16TyCon
-
-int32TyCon = pcNonRecDataTyCon int32TyConKey iNT SLIT("Int32") [] [int32DataCon]
-  where
-   int32DataCon = pcDataCon int32DataConKey iNT SLIT("I32#") [] [] [intPrimTy] int32TyCon
-
-int64TyCon = pcNonRecDataTyCon int64TyConKey pREL_ADDR SLIT("Int64") [] [int64DataCon]
-  where
-   int64DataCon = pcDataCon int64DataConKey pREL_ADDR SLIT("I64#") [] [] [int64PrimTy] int64TyCon
+isIntTy = isTyCon intTyConKey
 \end{code}
 
 \begin{code}
 
 wordTy = mkTyConTy wordTyCon
 
-wordTyCon = pcNonRecDataTyCon wordTyConKey   pREL_ADDR SLIT("Word") [] [wordDataCon]
-wordDataCon = pcDataCon wordDataConKey pREL_ADDR SLIT("W#") [] [] [wordPrimTy] wordTyCon
-
-word8TyCon = pcNonRecDataTyCon word8TyConKey   wORD SLIT("Word8") [] [word8DataCon]
-  where
-   word8DataCon = pcDataCon word8DataConKey wORD SLIT("W8#") [] [] [wordPrimTy] word8TyCon
-
-word16TyCon = pcNonRecDataTyCon word16TyConKey   wORD SLIT("Word16") [] [word16DataCon]
-  where
-   word16DataCon = pcDataCon word16DataConKey wORD SLIT("W16#") [] [] [wordPrimTy] word16TyCon
-
-word32TyCon = pcNonRecDataTyCon word32TyConKey   wORD SLIT("Word32") [] [word32DataCon]
-  where
-   word32DataCon = pcDataCon word32DataConKey wORD SLIT("W32#") [] [] [wordPrimTy] word32TyCon
-
-word64TyCon = pcNonRecDataTyCon word64TyConKey   pREL_ADDR SLIT("Word64") [] [word64DataCon]
-  where
-    word64DataCon = pcDataCon word64DataConKey pREL_ADDR SLIT("W64#") [] [] [word64PrimTy] word64TyCon
+wordTyCon = pcNonRecDataTyCon wordTyConKey wordTyCon_RDR [] [] [wordDataCon]
+wordDataCon = pcDataCon wordDataConKey wordDataCon_RDR [] [] [wordPrimTy] wordTyCon
 \end{code}
 
 \begin{code}
 addrTy = mkTyConTy addrTyCon
 
-addrTyCon = pcNonRecDataTyCon addrTyConKey   pREL_ADDR SLIT("Addr") [] [addrDataCon]
-addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
+addrTyCon = pcNonRecDataTyCon addrTyConKey addrTyCon_RDR [] [] [addrDataCon]
+addrDataCon = pcDataCon addrDataConKey addrDataCon_RDR [] [] [addrPrimTy] addrTyCon
 
 isAddrTy :: Type -> Bool
-isAddrTy ty
-  = case (splitAlgTyConApp_maybe ty) of
-       Just (tycon, [], _) -> getUnique tycon == addrTyConKey
-       _                   -> False
-
+isAddrTy = isTyCon addrTyConKey
 \end{code}
 
 \begin{code}
 floatTy        = mkTyConTy floatTyCon
 
-floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [floatDataCon]
-floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon
+floatTyCon   = pcNonRecDataTyCon floatTyConKey   floatTyCon_RDR   [] [] [floatDataCon]
+floatDataCon = pcDataCon         floatDataConKey floatDataCon_RDR [] [] [floatPrimTy] floatTyCon
 
 isFloatTy :: Type -> Bool
-isFloatTy ty
-  = case (splitAlgTyConApp_maybe ty) of
-       Just (tycon, [], _) -> getUnique tycon == floatTyConKey
-       _                   -> False
-
+isFloatTy = isTyCon floatTyConKey
 \end{code}
 
 \begin{code}
 doubleTy = mkTyConTy doubleTyCon
 
 isDoubleTy :: Type -> Bool
-isDoubleTy ty
-  = case (splitAlgTyConApp_maybe ty) of
-       Just (tycon, [], _) -> getUnique tycon == doubleTyConKey
-       _                   -> False
+isDoubleTy = isTyCon doubleTyConKey
 
-doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [doubleDataCon]
-doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon
+doubleTyCon   = pcNonRecDataTyCon doubleTyConKey   doubleTyCon_RDR     [] [] [doubleDataCon]
+doubleDataCon = pcDataCon        doubleDataConKey doubleDataCon_RDR [] [] [doublePrimTy] doubleTyCon
 \end{code}
 
 \begin{code}
 stablePtrTyCon
-  = pcNonRecDataTyCon stablePtrTyConKey pREL_STABLE SLIT("StablePtr")
-       alpha_tyvar [stablePtrDataCon]
+  = pcNonRecDataTyCon stablePtrTyConKey stablePtrTyCon_RDR
+       alpha_tyvar [(True,False)] [stablePtrDataCon]
   where
     stablePtrDataCon
-      = pcDataCon stablePtrDataConKey pREL_STABLE SLIT("StablePtr")
+      = pcDataCon stablePtrDataConKey stablePtrDataCon_RDR
            alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
 \end{code}
 
 \begin{code}
 foreignObjTyCon
-  = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj")
-       [] [foreignObjDataCon]
+  = pcNonRecDataTyCon foreignObjTyConKey foreignObjTyCon_RDR
+       [] [] [foreignObjDataCon]
   where
     foreignObjDataCon
-      = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
+      = pcDataCon foreignObjDataConKey foreignObjDataCon_RDR
            [] [] [foreignObjPrimTy] foreignObjTyCon
+
+isForeignObjTy :: Type -> Bool
+isForeignObjTy = isTyCon foreignObjTyConKey
 \end{code}
 
 %************************************************************************
@@ -409,16 +391,17 @@ foreignObjTyCon
 integerTy :: Type
 integerTy = mkTyConTy integerTyCon
 
-integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_BASE SLIT("Integer") [] [integerDataCon]
+integerTyCon = pcNonRecDataTyCon integerTyConKey integerTyCon_RDR
+                   [] [] [smallIntegerDataCon, largeIntegerDataCon]
+
+smallIntegerDataCon = pcDataCon smallIntegerDataConKey smallIntegerDataCon_RDR
+               [] [] [intPrimTy] integerTyCon
+largeIntegerDataCon = pcDataCon largeIntegerDataConKey largeIntegerDataCon_RDR
+               [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
 
-integerDataCon = pcDataCon integerDataConKey pREL_BASE SLIT("J#")
-               [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon
 
 isIntegerTy :: Type -> Bool
-isIntegerTy ty
-  = case (splitAlgTyConApp_maybe ty) of
-       Just (tycon, [], _) -> getUnique tycon == integerTyConKey
-       _                   -> False
+isIntegerTy = isTyCon integerTyConKey
 \end{code}
 
 
@@ -433,59 +416,94 @@ restricted set of types as arguments and results (the restricting factor
 being the )
 
 \begin{code}
-isFFIArgumentTy :: Type -> Bool
-isFFIArgumentTy ty =
-  (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
-  case (splitAlgTyConApp_maybe ty) of
-    Just (tycon, _, _) -> (getUnique tycon) `elem` primArgTyConKeys
-    _                 -> False
-
--- types that can be passed as arguments to "foreign" functions
-primArgTyConKeys 
-  = [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
-    , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
-    , floatTyConKey, doubleTyConKey
-    , addrTyConKey, charTyConKey, foreignObjTyConKey
-    , stablePtrTyConKey
-    , byteArrayTyConKey, mutableByteArrayTyConKey
-    ]
-
--- types that can be passed from the outside world into Haskell.
--- excludes (mutable) byteArrays.
-isFFIExternalTy :: Type -> Bool
-isFFIExternalTy ty = 
-  (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
-  case (splitAlgTyConApp_maybe ty) of
-    Just (tycon, _, _) -> 
-       let 
-        u_tycon = getUnique tycon
-       in  
-       (u_tycon `elem` primArgTyConKeys) &&
-       not (u_tycon `elem` notLegalExternalTyCons)
-    _                 -> False
+isFFIArgumentTy :: DynFlags -> Bool -> Type -> Bool
+-- Checks for valid argument type for a 'foreign import'
+isFFIArgumentTy dflags is_safe ty 
+   = checkRepTyCon (legalOutgoingTyCon dflags is_safe) ty
 
+isFFIExternalTy :: Type -> Bool
+-- Types that are allowed as arguments of a 'foreign export'
+isFFIExternalTy ty = checkRepTyCon legalIncomingTyCon ty
 
 isFFIResultTy :: Type -> Bool
-isFFIResultTy ty =
-   not (isUnLiftedType ty) &&
-   case (splitAlgTyConApp_maybe ty) of
-    Just (tycon, _, _) -> 
-       let
-        u_tycon = getUnique tycon
-       in
-       (u_tycon == getUnique unitTyCon) ||
-        ((u_tycon `elem` primArgTyConKeys) && 
-        not (u_tycon `elem` notLegalExternalTyCons))
-    _                 -> False
-
--- it's illegal to return foreign objects and (mutable)
+-- Types that are allowed as a result of a 'foreign import' or of a 'foreign export'
+-- Maybe we should distinguish between import and export, but 
+-- here we just choose the more restrictive 'incoming' predicate
+-- But we allow () as well
+isFFIResultTy ty = checkRepTyCon (\tc -> tc == unitTyCon || legalIncomingTyCon tc) ty
+
+isFFIDynArgumentTy :: Type -> Bool
+-- The argument type of a foreign import dynamic must be either Addr, or
+-- a newtype of Addr.
+isFFIDynArgumentTy = checkRepTyCon (== addrTyCon)
+
+isFFIDynResultTy :: Type -> Bool
+-- The result type of a foreign export dynamic must be either Addr, or
+-- a newtype of Addr.
+isFFIDynResultTy = checkRepTyCon (== addrTyCon)
+
+isFFILabelTy :: Type -> Bool
+-- The type of a foreign label must be either Addr, or
+-- a newtype of Addr.
+isFFILabelTy = checkRepTyCon (== addrTyCon)
+
+checkRepTyCon :: (TyCon -> Bool) -> Type -> Bool
+       -- look through newtypes
+checkRepTyCon check_tc ty = checkTyCon check_tc (repType ty)
+
+checkTyCon :: (TyCon -> Bool) -> Type -> Bool
+checkTyCon check_tc ty = case splitTyConApp_maybe ty of
+                               Just (tycon, _) -> check_tc tycon
+                               Nothing         -> False
+
+isTyCon :: Unique -> Type -> Bool
+isTyCon uniq ty = checkTyCon (\tc -> uniq == getUnique tc) ty
+\end{code}
+
+----------------------------------------------
+These chaps do the work; they are not exported
+----------------------------------------------
+
+\begin{code}
+legalIncomingTyCon :: TyCon -> Bool
+-- It's illegal to return foreign objects and (mutable)
 -- bytearrays from a _ccall_ / foreign declaration
 -- (or be passed them as arguments in foreign exported functions).
-notLegalExternalTyCons =
-  [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ]
-    
+legalIncomingTyCon tc
+  | getUnique tc `elem` [ foreignObjTyConKey, byteArrayTyConKey, 
+                                              mutableByteArrayTyConKey ] 
+  = False
+  -- It's also illegal to make foreign exports that take unboxed
+  -- arguments.  The RTS API currently can't invoke such things.  --SDM 7/2000
+  | otherwise
+  = boxedMarshalableTyCon tc
+
+legalOutgoingTyCon :: DynFlags -> Bool -> TyCon -> Bool
+-- Checks validity of types going from Haskell -> external world
+-- The boolean is true for a 'safe' call (when we don't want to
+-- pass Haskell pointers to the world)
+legalOutgoingTyCon dflags be_safe tc
+  | be_safe && getUnique tc `elem` [byteArrayTyConKey, mutableByteArrayTyConKey]
+  = False
+  | otherwise
+  = marshalableTyCon dflags tc
+
+marshalableTyCon dflags tc
+  =  (dopt_GlasgowExts dflags && isUnLiftedTyCon tc)
+  || boxedMarshalableTyCon tc
+
+boxedMarshalableTyCon tc
+   = getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
+                        , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
+                        , floatTyConKey, doubleTyConKey
+                        , addrTyConKey, charTyConKey, foreignObjTyConKey
+                        , stablePtrTyConKey
+                        , byteArrayTyConKey, mutableByteArrayTyConKey
+                        , boolTyConKey
+                        ]
 \end{code}
 
+
 %************************************************************************
 %*                                                                     *
 \subsection[TysWiredIn-Bool]{The @Bool@ type}
@@ -537,11 +555,14 @@ primitive counterpart.
 \begin{code}
 boolTy = mkTyConTy boolTyCon
 
-boolTyCon = pcTyCon EnumType NonRecursive boolTyConKey 
-                   pREL_BASE SLIT("Bool") [] [falseDataCon, trueDataCon]
+boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConKey 
+                   boolTyCon_RDR [] [] [falseDataCon, trueDataCon]
 
-falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
-trueDataCon  = pcDataCon trueDataConKey         pREL_BASE SLIT("True")  [] [] [] boolTyCon
+falseDataCon = pcDataCon falseDataConKey false_RDR [] [] [] boolTyCon
+trueDataCon  = pcDataCon trueDataConKey         true_RDR  [] [] [] boolTyCon
+
+falseDataConId = dataConId falseDataCon
+trueDataConId  = dataConId trueDataCon
 \end{code}
 
 %************************************************************************
@@ -565,11 +586,11 @@ mkListTy ty = mkTyConApp listTyCon [ty]
 
 alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty)
 
-listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]") 
-                       alpha_tyvar [nilDataCon, consDataCon]
+listTyCon = pcRecDataTyCon listTyConKey listTyCon_RDR
+                       alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
 
-nilDataCon  = pcDataCon nilDataConKey  pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon
-consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":")
+nilDataCon  = pcDataCon nilDataConKey  nil_RDR alpha_tyvar [] [] listTyCon
+consDataCon = pcDataCon consDataConKey cons_RDR
                alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
 -- Interesting: polymorphic recursion would help here.
 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
@@ -623,11 +644,48 @@ done by enumeration\srcloc{lib/prelude/InTup?.hs}.
 \end{itemize}
 
 \begin{code}
-mkTupleTy :: Int -> [Type] -> Type
-mkTupleTy arity tys = mkTyConApp (tupleTyCon arity) tys
+mkTupleTy :: Boxity -> Int -> [Type] -> Type
+mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
 
-mkUnboxedTupleTy :: Int -> [Type] -> Type
-mkUnboxedTupleTy arity tys = mkTyConApp (unboxedTupleTyCon arity) tys
+unitTy    = mkTupleTy Boxed 0 []
+\end{code}
 
-unitTy    = mkTupleTy 0 []
+%************************************************************************
+%*                                                                      *
+\subsection{Wired In Type Constructors for Representation Types}
+%*                                                                      *
+%************************************************************************
+
+The following code defines the wired in datatypes cross, plus, unit
+and c_of needed for the generic methods.
+
+Ok, so the basic story is that for each type constructor I need to
+create 2 things - a TyCon and a DataCon and then we are basically
+ok. There are going to be no arguments passed to these functions
+because -well- there is nothing to pass to these functions.
+
+\begin{code}
+crossTyCon :: TyCon
+crossTyCon = pcNonRecDataTyCon crossTyConKey crossTyCon_RDR alpha_beta_tyvars [] [crossDataCon]
+
+crossDataCon :: DataCon
+crossDataCon = pcDataCon crossDataConKey crossDataCon_RDR alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
+
+plusTyCon :: TyCon
+plusTyCon = pcNonRecDataTyCon plusTyConKey plusTyCon_RDR alpha_beta_tyvars [] [inlDataCon, inrDataCon]
+
+inlDataCon, inrDataCon :: DataCon
+inlDataCon = pcDataCon inlDataConKey inlDataCon_RDR alpha_beta_tyvars [] [alphaTy] plusTyCon
+inrDataCon = pcDataCon inrDataConKey inrDataCon_RDR alpha_beta_tyvars [] [betaTy]  plusTyCon
+
+genUnitTyCon :: TyCon  -- The "1" type constructor for generics
+genUnitTyCon = pcNonRecDataTyCon genUnitTyConKey genUnitTyCon_RDR [] [] [genUnitDataCon]
+
+genUnitDataCon :: DataCon
+genUnitDataCon = pcDataCon genUnitDataConKey genUnitDataCon_RDR [] [] [] genUnitTyCon
 \end{code}
+
+
+
+
+