[project @ 2000-03-23 17:45:17 by simonpj]
[ghc-hetmet.git] / ghc / compiler / prelude / TysWiredIn.lhs
index 514682d..a8bcf25 100644 (file)
@@ -1,5 +1,5 @@
 %
-% (c) The GRASP Project, Glasgow University, 1994-1995
+% (c) The GRASP Project, Glasgow University, 1994-1998
 %
 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
 
@@ -10,8 +10,6 @@ This module tracks the ``state interface'' document, ``GHC prelude:
 types and operations.''
 
 \begin{code}
-#include "HsVersions.h"
-
 module TysWiredIn (
        addrDataCon,
        addrTy,
@@ -24,116 +22,230 @@ module TysWiredIn (
        consDataCon,
        doubleDataCon,
        doubleTy,
+       isDoubleTy,
        doubleTyCon,
-       eqDataCon,
-       falseDataCon,
+       falseDataCon, falseDataConId,
        floatDataCon,
        floatTy,
+       isFloatTy,
        floatTyCon,
-       getStatePairingConInfo,
-       gtDataCon,
+
        intDataCon,
        intTy,
        intTyCon,
+       isIntTy,
+
        integerTy,
        integerTyCon,
-       integerDataCon,
-       liftDataCon,
-       liftTyCon,
+       smallIntegerDataCon,
+       largeIntegerDataCon,
+       isIntegerTy,
+
        listTyCon,
-       ltDataCon,
-       mallocPtrTyCon,
-       mkLiftTy,
+
        mkListTy,
-       mkPrimIoTy,
-       mkStateTransformerTy,
-       mkTupleTy,
        nilDataCon,
-       orderingTy,
-       orderingTyCon,
-       primIoTyCon,
-       ratioDataCon,
-       ratioTyCon,
-       rationalTy,
-       rationalTyCon,
-       realWorldStateTy,
-       return2GMPsTyCon,
-       returnIntAndGMPTyCon,
-       stTyCon,
+
+       -- tuples
+       mkTupleTy,
+       tupleTyCon, tupleCon, unitTyCon, unitDataConId, pairTyCon, 
+
+       -- unboxed tuples
+       mkUnboxedTupleTy,
+       unboxedTupleTyCon, unboxedTupleCon, 
+       unboxedPairTyCon, unboxedPairDataCon,
+
        stablePtrTyCon,
-       stateAndAddrPrimTyCon,
-       stateAndArrayPrimTyCon,
-       stateAndByteArrayPrimTyCon,
-       stateAndCharPrimTyCon,
-       stateAndDoublePrimTyCon,
-       stateAndFloatPrimTyCon,
-       stateAndIntPrimTyCon,
-       stateAndMallocPtrPrimTyCon,
-       stateAndMutableArrayPrimTyCon,
-       stateAndMutableByteArrayPrimTyCon,
-       stateAndPtrPrimTyCon,
-       stateAndStablePtrPrimTyCon,
-       stateAndSynchVarPrimTyCon,
-       stateAndWordPrimTyCon,
-       stateDataCon,
-       stateTyCon,
        stringTy,
-       stringTyCon,
-       trueDataCon,
+       trueDataCon, trueDataConId,
        unitTy,
+       voidTy,
        wordDataCon,
        wordTy,
-       wordTyCon
+       wordTyCon,
+
+       isFFIArgumentTy,  -- :: Bool -> Type -> Bool
+       isFFIResultTy,    -- :: Type -> Bool
+       isFFIExternalTy,  -- :: Type -> Bool
+       isAddrTy,         -- :: Type -> Bool
+       isForeignObjTy    -- :: Type -> Bool
 
     ) where
 
-import Ubiq
-import TyLoop          ( mkDataCon, StrictnessMark(..) )
+#include "HsVersions.h"
+
+import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
 
 -- friends:
 import PrelMods
 import TysPrim
 
 -- others:
-import SpecEnv         ( SpecEnv(..) )
-import NameTypes       ( mkPreludeCoreName, mkShortName )
-import Kind            ( mkBoxedTypeKind, mkArrowKind )
-import SrcLoc          ( mkBuiltinSrcLoc )
-import TyCon           ( mkDataTyCon, mkTupleTyCon, mkSynTyCon,
-                         ConsVisible(..), NewOrData(..), TyCon )
-import Type            ( mkTyConTy, applyTyCon, mkSynTy, mkSigmaTy,
-                         mkFunTys, maybeAppDataTyCon,
-                         GenType(..), ThetaType(..), TauType(..) )
-import TyVar           ( getTyVarKind, alphaTyVar, betaTyVar )
+import Constants       ( mAX_TUPLE_SIZE )
+import Module          ( Module, mkPrelModule )
+import Name            ( mkWiredInTyConName, mkWiredInIdName, mkSrcOccFS, mkWorkerOcc, dataName )
+import DataCon         ( DataCon, StrictnessMark(..),  mkDataCon, dataConId )
+import Var             ( TyVar, tyVarKind )
+import TyCon           ( TyCon, ArgVrcs, mkAlgTyCon, mkSynTyCon, mkTupleTyCon )
+import BasicTypes      ( Arity, NewOrData(..), RecFlag(..) )
+import Type            ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys, 
+                         mkArrowKinds, boxedTypeKind, unboxedTypeKind,
+                         mkFunTy, mkFunTys, isUnLiftedType,
+                         splitTyConApp_maybe, splitAlgTyConApp_maybe,
+                         TauType, ClassContext )
+import PrimRep         ( PrimRep(..) )
 import Unique
-import Util            ( assoc, panic )
-
-nullSpecEnv =  error "TysWiredIn:nullSpecEnv =  "
-addOneToSpecEnv =  error "TysWiredIn:addOneToSpecEnv =  "
-pc_gen_specs = error "TysWiredIn:pc_gen_specs  "
-mkSpecInfo = error "TysWiredIn:SpecInfo"
-
-pcDataTyCon :: Unique{-TyConKey-} -> FAST_STRING -> FAST_STRING -> [TyVar] -> [Id] -> TyCon
-pcDataTyCon key mod name tyvars cons
-  = mkDataTyCon key tycon_kind full_name tyvars
-               [{-no context-}] cons [{-no derivings-}]
-               ConsVisible DataType
+import CmdLineOpts      ( opt_GlasgowExts )
+import Util            ( assoc )
+import Panic           ( panic )
+import Array
+
+alpha_tyvar      = [alphaTyVar]
+alpha_ty         = [alphaTy]
+alpha_beta_tyvars = [alphaTyVar, betaTyVar]
+
+pcRecDataTyCon, pcNonRecDataTyCon, pcNonRecNewTyCon
+       :: Unique{-TyConKey-} -> Module -> FAST_STRING
+       -> [TyVar] -> ArgVrcs -> [DataCon] -> TyCon
+
+pcRecDataTyCon    = pcTyCon DataType Recursive
+pcNonRecDataTyCon = pcTyCon DataType NonRecursive
+pcNonRecNewTyCon  = pcTyCon NewType  NonRecursive
+
+pcTyCon new_or_data is_rec key mod str tyvars argvrcs cons
+  = tycon
+  where
+    tycon = mkAlgTyCon name kind 
+               tyvars 
+               []              -- No context
+                argvrcs
+               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 argvrcs  -- this fun never used!
+  = tycon
   where
-    full_name = mkPreludeCoreName mod name
-    tycon_kind = foldr (mkArrowKind . getTyVarKind) mkBoxedTypeKind tyvars
-
-pcDataCon :: Unique{-DataConKey-} -> FAST_STRING -> FAST_STRING -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> SpecEnv -> Id
-pcDataCon key mod name tyvars context arg_tys tycon specenv
-  = mkDataCon key (mkPreludeCoreName mod name)
-       [ NotMarkedStrict | a <- arg_tys ]
-       tyvars context arg_tys tycon
-       -- specenv
-
-pcGenerateDataSpecs :: Type -> SpecEnv
-pcGenerateDataSpecs ty
-  = pc_gen_specs False err err err ty
+    tycon = mkSynTyCon name kind arity tyvars expansion argvrcs
+    name  = mkWiredInTyConName key mod str tycon
+
+pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
+         -> [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 mod str tyvars context arg_tys tycon
+  = data_con
   where
-    err = panic "PrelUtils:GenerateDataSpecs"
+    data_con = mkDataCon wrap_name 
+               [ NotMarkedStrict | a <- arg_tys ]
+               [ {- no labelled fields -} ]
+               tyvars context [] [] arg_tys tycon work_id wrap_id
+
+    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
+    
+    wrap_occ  = mkSrcOccFS dataName str
+    wrap_name = mkWiredInIdName wrap_key mod wrap_occ wrap_id
+    wrap_id   = mkDataConWrapId data_con
+\end{code}
+
+%************************************************************************
+%*                                                                     *
+\subsection[TysWiredIn-tuples]{The tuple types}
+%*                                                                     *
+%************************************************************************
+
+\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
+
+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)
+
+tuples :: [(TyCon,DataCon)]
+tuples = [mk_tuple i | i <- [0..mAX_TUPLE_SIZE]]
+
+mk_tuple :: Int -> (TyCon,DataCon)
+mk_tuple arity = (tycon, tuple_con)
+  where
+       tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con True
+       tc_name = mkWiredInTyConName tc_uniq mod name_str tycon
+       tc_kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
+
+       tuple_con = pcDataCon dc_uniq mod 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
+       mod       = mkPrelModule mod_name
+
+unitTyCon = tupleTyCon 0
+pairTyCon = tupleTyCon 2
+
+unitDataConId = dataConId (tupleCon 0)
+\end{code}
+
+%************************************************************************
+%*                                                                     *
+\subsection[TysWiredIn-ubx-tuples]{Unboxed 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)
+  where
+       tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con False
+       tc_name = mkWiredInTyConName tc_uniq mod name_str tycon
+       tc_kind = mkArrowKinds (map tyVarKind tyvars) unboxedTypeKind
+
+       tuple_con = pcDataCon dc_uniq mod 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
+       mod       = mkPrelModule mod_name
+
+unboxedPairTyCon   = unboxedTupleTyCon 2
+unboxedPairDataCon = unboxedTupleCon 2
 \end{code}
 
 %************************************************************************
@@ -143,75 +255,110 @@ pcGenerateDataSpecs ty
 %************************************************************************
 
 \begin{code}
+-- The Void type is represented as a data type with no constructors
+-- It's a built in type (i.e. there's no way to define it in Haskell;
+--     the nearest would be
+--
+--             data Void =             -- No constructors!
+--
+-- ) It's boxed; there is only one value of this
+-- type, namely "void", whose semantics is just bottom.
+--
+-- Haskell 98 drops the definition of a Void type, so we just 'simulate'
+-- voidTy using ().
+voidTy = unitTy
+\end{code}
+
+
+\begin{code}
 charTy = mkTyConTy charTyCon
 
-charTyCon = pcDataTyCon charTyConKey pRELUDE_BUILTIN SLIT("Char") [] [charDataCon]
-charDataCon = pcDataCon charDataConKey pRELUDE_BUILTIN SLIT("C#") [] [] [charPrimTy] charTyCon nullSpecEnv
+charTyCon = pcNonRecDataTyCon charTyConKey  pREL_BASE  SLIT("Char") [] [] [charDataCon]
+charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon
+
+stringTy = mkListTy charTy -- convenience only
 \end{code}
 
 \begin{code}
 intTy = mkTyConTy intTyCon 
 
-intTyCon = pcDataTyCon intTyConKey pRELUDE_BUILTIN SLIT("Int") [] [intDataCon]
-intDataCon = pcDataCon intDataConKey pRELUDE_BUILTIN SLIT("I#") [] [] [intPrimTy] intTyCon nullSpecEnv
+intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [] [intDataCon]
+intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
+
+isIntTy :: Type -> Bool
+isIntTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> getUnique tycon == intTyConKey
+       _                   -> False
 \end{code}
 
 \begin{code}
+
 wordTy = mkTyConTy wordTyCon
 
-wordTyCon = pcDataTyCon wordTyConKey pRELUDE_BUILTIN SLIT("_Word") [] [wordDataCon]
-wordDataCon = pcDataCon wordDataConKey pRELUDE_BUILTIN SLIT("W#") [] [] [wordPrimTy] wordTyCon nullSpecEnv
+wordTyCon = pcNonRecDataTyCon wordTyConKey   pREL_ADDR SLIT("Word") [] [] [wordDataCon]
+wordDataCon = pcDataCon wordDataConKey pREL_ADDR SLIT("W#") [] [] [wordPrimTy] wordTyCon
 \end{code}
 
 \begin{code}
 addrTy = mkTyConTy addrTyCon
 
-addrTyCon = pcDataTyCon addrTyConKey pRELUDE_BUILTIN SLIT("_Addr") [] [addrDataCon]
-addrDataCon = pcDataCon addrDataConKey pRELUDE_BUILTIN SLIT("A#") [] [] [addrPrimTy] addrTyCon nullSpecEnv
+addrTyCon = pcNonRecDataTyCon addrTyConKey   pREL_ADDR SLIT("Addr") [] [] [addrDataCon]
+addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
+
+isAddrTy :: Type -> Bool
+isAddrTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> getUnique tycon == addrTyConKey
+       _                   -> False
+
 \end{code}
 
 \begin{code}
 floatTy        = mkTyConTy floatTyCon
 
-floatTyCon = pcDataTyCon floatTyConKey pRELUDE_BUILTIN SLIT("Float") [] [floatDataCon]
-floatDataCon = pcDataCon floatDataConKey pRELUDE_BUILTIN SLIT("F#") [] [] [floatPrimTy] floatTyCon nullSpecEnv
-\end{code}
+floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_FLOAT SLIT("Float") [] [] [floatDataCon]
+floatDataCon = pcDataCon floatDataConKey pREL_FLOAT SLIT("F#") [] [] [floatPrimTy] floatTyCon
 
-\begin{code}
-doubleTy = mkTyConTy doubleTyCon
+isFloatTy :: Type -> Bool
+isFloatTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> getUnique tycon == floatTyConKey
+       _                   -> False
 
-doubleTyCon = pcDataTyCon doubleTyConKey pRELUDE_BUILTIN SLIT("Double") [] [doubleDataCon]
-doubleDataCon = pcDataCon doubleDataConKey pRELUDE_BUILTIN SLIT("D#") [] [] [doublePrimTy] doubleTyCon nullSpecEnv
 \end{code}
 
 \begin{code}
-mkStateTy ty    = applyTyCon stateTyCon [ty]
-realWorldStateTy = mkStateTy realWorldTy -- a common use
+doubleTy = mkTyConTy doubleTyCon
+
+isDoubleTy :: Type -> Bool
+isDoubleTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> getUnique tycon == doubleTyConKey
+       _                   -> False
 
-stateTyCon = pcDataTyCon stateTyConKey pRELUDE_BUILTIN SLIT("_State") [alphaTyVar] [stateDataCon]
-stateDataCon
-  = pcDataCon stateDataConKey pRELUDE_BUILTIN SLIT("S#")
-       [alphaTyVar] [] [mkStatePrimTy alphaTy] stateTyCon nullSpecEnv
+doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_FLOAT SLIT("Double") [] [] [doubleDataCon]
+doubleDataCon = pcDataCon doubleDataConKey pREL_FLOAT SLIT("D#") [] [] [doublePrimTy] doubleTyCon
 \end{code}
 
 \begin{code}
 stablePtrTyCon
-  = pcDataTyCon stablePtrTyConKey gLASGOW_MISC SLIT("_StablePtr")
-       [alphaTyVar] [stablePtrDataCon]
+  = pcNonRecDataTyCon stablePtrTyConKey pREL_STABLE SLIT("StablePtr")
+       alpha_tyvar [(True,False)] [stablePtrDataCon]
   where
     stablePtrDataCon
-      = pcDataCon stablePtrDataConKey gLASGOW_MISC SLIT("_StablePtr")
-           [alphaTyVar] [] [applyTyCon stablePtrPrimTyCon [alphaTy]] stablePtrTyCon nullSpecEnv
+      = pcDataCon stablePtrDataConKey pREL_STABLE SLIT("StablePtr")
+           alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
 \end{code}
 
 \begin{code}
-mallocPtrTyCon
-  = pcDataTyCon mallocPtrTyConKey gLASGOW_MISC SLIT("_MallocPtr")
-       [] [mallocPtrDataCon]
+foreignObjTyCon
+  = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj")
+       [] [] [foreignObjDataCon]
   where
-    mallocPtrDataCon
-      = pcDataCon mallocPtrDataConKey gLASGOW_MISC SLIT("_MallocPtr")
-           [] [] [applyTyCon mallocPtrPrimTyCon []] mallocPtrTyCon nullSpecEnv
+    foreignObjDataCon
+      = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
+           [] [] [foreignObjPrimTy] foreignObjTyCon
 \end{code}
 
 %************************************************************************
@@ -222,242 +369,105 @@ mallocPtrTyCon
 
 @Integer@ and its pals are not really primitive.  @Integer@ itself, first:
 \begin{code}
-integerTy :: GenType t u
-integerTy    = mkTyConTy integerTyCon
-
-integerTyCon = pcDataTyCon integerTyConKey pRELUDE_BUILTIN SLIT("Integer") [] [integerDataCon]
-
-integerDataCon = pcDataCon integerDataConKey pRELUDE_BUILTIN SLIT("J#")
-               [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon nullSpecEnv
-\end{code}
+integerTy :: Type
+integerTy = mkTyConTy integerTyCon
 
-And the other pairing types:
-\begin{code}
-return2GMPsTyCon = pcDataTyCon return2GMPsTyConKey
-       pRELUDE_BUILTIN SLIT("_Return2GMPs") [] [return2GMPsDataCon]
+integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_NUM SLIT("Integer")
+                   [] [] [smallIntegerDataCon, largeIntegerDataCon]
 
-return2GMPsDataCon
-  = pcDataCon return2GMPsDataConKey pRELUDE_BUILTIN SLIT("_Return2GMPs") [] []
-       [intPrimTy, intPrimTy, byteArrayPrimTy,
-        intPrimTy, intPrimTy, byteArrayPrimTy] return2GMPsTyCon nullSpecEnv
+smallIntegerDataCon = pcDataCon smallIntegerDataConKey pREL_NUM SLIT("S#")
+               [] [] [intPrimTy] integerTyCon
+largeIntegerDataCon = pcDataCon largeIntegerDataConKey pREL_NUM SLIT("J#")
+               [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
 
-returnIntAndGMPTyCon = pcDataTyCon returnIntAndGMPTyConKey
-       pRELUDE_BUILTIN SLIT("_ReturnIntAndGMP") [] [returnIntAndGMPDataCon]
 
-returnIntAndGMPDataCon
-  = pcDataCon returnIntAndGMPDataConKey pRELUDE_BUILTIN SLIT("_ReturnIntAndGMP") [] []
-       [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy] returnIntAndGMPTyCon nullSpecEnv
+isIntegerTy :: Type -> Bool
+isIntegerTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> getUnique tycon == integerTyConKey
+       _                   -> False
 \end{code}
 
+
 %************************************************************************
 %*                                                                     *
-\subsection[TysWiredIn-state-pairing]{``State-pairing'' types}
+\subsection[TysWiredIn-ext-type]{External types}
 %*                                                                     *
 %************************************************************************
 
-These boring types pair a \tr{State#} with another primitive type.
-They are not really primitive, so they are given here, not in
-\tr{TysPrim.lhs}.
-
-We fish one of these \tr{StateAnd<blah>#} things with
-@getStatePairingConInfo@ (given a little way down).
+The compiler's foreign function interface supports the passing of a
+restricted set of types as arguments and results (the restricting factor
+being the )
 
 \begin{code}
-stateAndPtrPrimTyCon
-  = pcDataTyCon stateAndPtrPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndPtr#")
-               [alphaTyVar, betaTyVar] [stateAndPtrPrimDataCon]
-stateAndPtrPrimDataCon
-  = pcDataCon stateAndPtrPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndPtr#")
-               [alphaTyVar, betaTyVar] [] [mkStatePrimTy alphaTy, betaTy]
-               stateAndPtrPrimTyCon nullSpecEnv
-
-stateAndCharPrimTyCon
-  = pcDataTyCon stateAndCharPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndChar#")
-               [alphaTyVar] [stateAndCharPrimDataCon]
-stateAndCharPrimDataCon
-  = pcDataCon stateAndCharPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndChar#")
-               [alphaTyVar] [] [mkStatePrimTy alphaTy, charPrimTy]
-               stateAndCharPrimTyCon nullSpecEnv
-
-stateAndIntPrimTyCon
-  = pcDataTyCon stateAndIntPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndInt#")
-               [alphaTyVar] [stateAndIntPrimDataCon]
-stateAndIntPrimDataCon
-  = pcDataCon stateAndIntPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndInt#")
-               [alphaTyVar] [] [mkStatePrimTy alphaTy, intPrimTy]
-               stateAndIntPrimTyCon nullSpecEnv
-
-stateAndWordPrimTyCon
-  = pcDataTyCon stateAndWordPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndWord#")
-               [alphaTyVar] [stateAndWordPrimDataCon]
-stateAndWordPrimDataCon
-  = pcDataCon stateAndWordPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndWord#")
-               [alphaTyVar] [] [mkStatePrimTy alphaTy, wordPrimTy]
-               stateAndWordPrimTyCon nullSpecEnv
-
-stateAndAddrPrimTyCon
-  = pcDataTyCon stateAndAddrPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndAddr#")
-               [alphaTyVar] [stateAndAddrPrimDataCon]
-stateAndAddrPrimDataCon
-  = pcDataCon stateAndAddrPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndAddr#")
-               [alphaTyVar] [] [mkStatePrimTy alphaTy, addrPrimTy]
-               stateAndAddrPrimTyCon nullSpecEnv
-
-stateAndStablePtrPrimTyCon
-  = pcDataTyCon stateAndStablePtrPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndStablePtr#")
-               [alphaTyVar, betaTyVar] [stateAndStablePtrPrimDataCon]
-stateAndStablePtrPrimDataCon
-  = pcDataCon stateAndStablePtrPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndStablePtr#")
-               [alphaTyVar, betaTyVar] []
-               [mkStatePrimTy alphaTy, applyTyCon stablePtrPrimTyCon [betaTy]]
-               stateAndStablePtrPrimTyCon nullSpecEnv
-
-stateAndMallocPtrPrimTyCon
-  = pcDataTyCon stateAndMallocPtrPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndMallocPtr#")
-               [alphaTyVar] [stateAndMallocPtrPrimDataCon]
-stateAndMallocPtrPrimDataCon
-  = pcDataCon stateAndMallocPtrPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndMallocPtr#")
-               [alphaTyVar] []
-               [mkStatePrimTy alphaTy, applyTyCon mallocPtrPrimTyCon []]
-               stateAndMallocPtrPrimTyCon nullSpecEnv
-
-stateAndFloatPrimTyCon
-  = pcDataTyCon stateAndFloatPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndFloat#")
-               [alphaTyVar] [stateAndFloatPrimDataCon]
-stateAndFloatPrimDataCon
-  = pcDataCon stateAndFloatPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndFloat#")
-               [alphaTyVar] [] [mkStatePrimTy alphaTy, floatPrimTy]
-               stateAndFloatPrimTyCon nullSpecEnv
-
-stateAndDoublePrimTyCon
-  = pcDataTyCon stateAndDoublePrimTyConKey pRELUDE_BUILTIN SLIT("StateAndDouble#")
-               [alphaTyVar] [stateAndDoublePrimDataCon]
-stateAndDoublePrimDataCon
-  = pcDataCon stateAndDoublePrimDataConKey pRELUDE_BUILTIN SLIT("StateAndDouble#")
-               [alphaTyVar] [] [mkStatePrimTy alphaTy, doublePrimTy]
-               stateAndDoublePrimTyCon nullSpecEnv
-\end{code}
-
-\begin{code}
-stateAndArrayPrimTyCon
-  = pcDataTyCon stateAndArrayPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndArray#")
-               [alphaTyVar, betaTyVar] [stateAndArrayPrimDataCon]
-stateAndArrayPrimDataCon
-  = pcDataCon stateAndArrayPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndArray#")
-               [alphaTyVar, betaTyVar] [] [mkStatePrimTy alphaTy, mkArrayPrimTy betaTy]
-               stateAndArrayPrimTyCon nullSpecEnv
-
-stateAndMutableArrayPrimTyCon
-  = pcDataTyCon stateAndMutableArrayPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndMutableArray#")
-               [alphaTyVar, betaTyVar] [stateAndMutableArrayPrimDataCon]
-stateAndMutableArrayPrimDataCon
-  = pcDataCon stateAndMutableArrayPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndMutableArray#")
-               [alphaTyVar, betaTyVar] [] [mkStatePrimTy alphaTy, mkMutableArrayPrimTy alphaTy betaTy]
-               stateAndMutableArrayPrimTyCon nullSpecEnv
-
-stateAndByteArrayPrimTyCon
-  = pcDataTyCon stateAndByteArrayPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndByteArray#")
-               [alphaTyVar] [stateAndByteArrayPrimDataCon]
-stateAndByteArrayPrimDataCon
-  = pcDataCon stateAndByteArrayPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndByteArray#")
-               [alphaTyVar] [] [mkStatePrimTy alphaTy, byteArrayPrimTy]
-               stateAndByteArrayPrimTyCon nullSpecEnv
-
-stateAndMutableByteArrayPrimTyCon
-  = pcDataTyCon stateAndMutableByteArrayPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndMutableByteArray#")
-               [alphaTyVar] [stateAndMutableByteArrayPrimDataCon]
-stateAndMutableByteArrayPrimDataCon
-  = pcDataCon stateAndMutableByteArrayPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndMutableByteArray#")
-               [alphaTyVar] [] [mkStatePrimTy alphaTy, applyTyCon mutableByteArrayPrimTyCon [alphaTy]]
-               stateAndMutableByteArrayPrimTyCon nullSpecEnv
-
-stateAndSynchVarPrimTyCon
-  = pcDataTyCon stateAndSynchVarPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndSynchVar#")
-               [alphaTyVar, betaTyVar] [stateAndSynchVarPrimDataCon]
-stateAndSynchVarPrimDataCon
-  = pcDataCon stateAndSynchVarPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndSynchVar#")
-               [alphaTyVar, betaTyVar] [] [mkStatePrimTy alphaTy, mkSynchVarPrimTy alphaTy betaTy]
-               stateAndSynchVarPrimTyCon nullSpecEnv
-\end{code}
-
-The ccall-desugaring mechanism uses this function to figure out how to
-rebox the result.  It's really a HACK, especially the part about
-how many types to drop from \tr{tys_applied}.
-
-\begin{code}
-getStatePairingConInfo
-       :: Type -- primitive type
-       -> (Id,         -- state pair constructor for prim type
-           Type)       -- type of state pair
-
-getStatePairingConInfo prim_ty
-  = case (maybeAppDataTyCon prim_ty) of
-      Nothing -> panic "getStatePairingConInfo:1"
-      Just (prim_tycon, tys_applied, _) ->
+isFFIArgumentTy :: Bool -> Type -> Bool
+isFFIArgumentTy forASafeCall ty =
+  (opt_GlasgowExts && isUnLiftedType ty) ||
+  case (splitAlgTyConApp_maybe ty) of
+    Just (tycon, _, _) -> 
+               let
+                u = getUnique tycon
+               in
+               u `elem` primArgTyConKeys &&   -- it has a suitable prim type, and
+               (not forASafeCall || not ( u `elem` notSafeExternalTyCons)) -- it is safe to pass out.
+    _                 -> 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
+
+
+isFFIResultTy :: Type -> Bool
+isFFIResultTy ty =
+   not (isUnLiftedType ty) &&
+   case (splitAlgTyConApp_maybe ty) of
+    Just (tycon, _, _) -> 
        let
-           (pair_con, pair_tycon, num_tys) = assoc "getStatePairingConInfo" tbl prim_tycon
-           pair_ty = applyTyCon pair_tycon (realWorldTy : drop num_tys tys_applied)
+        u_tycon = getUnique tycon
        in
-       (pair_con, pair_ty)
-  where
-    tbl = [
-       (charPrimTyCon, (stateAndCharPrimDataCon, stateAndCharPrimTyCon, 0)),
-       (intPrimTyCon, (stateAndIntPrimDataCon, stateAndIntPrimTyCon, 0)),
-       (wordPrimTyCon, (stateAndWordPrimDataCon, stateAndWordPrimTyCon, 0)),
-       (addrPrimTyCon, (stateAndAddrPrimDataCon, stateAndAddrPrimTyCon, 0)),
-       (stablePtrPrimTyCon, (stateAndStablePtrPrimDataCon, stateAndStablePtrPrimTyCon, 0)),
-       (mallocPtrPrimTyCon, (stateAndMallocPtrPrimDataCon, stateAndMallocPtrPrimTyCon, 0)),
-       (floatPrimTyCon, (stateAndFloatPrimDataCon, stateAndFloatPrimTyCon, 0)),
-       (doublePrimTyCon, (stateAndDoublePrimDataCon, stateAndDoublePrimTyCon, 0)),
-       (arrayPrimTyCon, (stateAndArrayPrimDataCon, stateAndArrayPrimTyCon, 0)),
-       (mutableArrayPrimTyCon, (stateAndMutableArrayPrimDataCon, stateAndMutableArrayPrimTyCon, 1)),
-       (byteArrayPrimTyCon, (stateAndByteArrayPrimDataCon, stateAndByteArrayPrimTyCon, 0)),
-       (mutableByteArrayPrimTyCon, (stateAndMutableByteArrayPrimDataCon, stateAndMutableByteArrayPrimTyCon, 1)),
-       (synchVarPrimTyCon, (stateAndSynchVarPrimDataCon, stateAndSynchVarPrimTyCon, 1))
-       -- (PtrPrimTyCon, (stateAndPtrPrimDataCon, stateAndPtrPrimTyCon, 0)),
-       ]
-\end{code}
-
-%************************************************************************
-%*                                                                     *
-\subsection[TysWiredIn-ST]{The basic @_ST@ state-transformer type}
-%*                                                                     *
-%************************************************************************
-
-This is really just an ordinary synonym, except it is ABSTRACT.
-
-\begin{code}
-mkStateTransformerTy s a = mkSynTy stTyCon [s, a]
-
-stTyCon
-  = mkSynTyCon
-     stTyConKey
-     (mkPreludeCoreName gLASGOW_ST SLIT("_ST"))
-     (panic "TysWiredIn.stTyCon:Kind")
-     2
-     [alphaTyVar, betaTyVar]
-     (mkFunTys [mkStateTy alphaTy] (mkTupleTy 2 [betaTy, mkStateTy alphaTy]))
-\end{code}
-
-%************************************************************************
-%*                                                                     *
-\subsection[TysWiredIn-IO]{The @PrimIO@ and @IO@ monadic-I/O types}
-%*                                                                     *
-%************************************************************************
-
-@PrimIO@ and @IO@ really are just plain synonyms.
-
-\begin{code}
-mkPrimIoTy a = mkSynTy primIoTyCon [a]
-
-primIoTyCon
-  = mkSynTyCon
-     primIoTyConKey
-     (mkPreludeCoreName pRELUDE_PRIMIO SLIT("PrimIO"))
-     (panic "TysWiredIn.primIoTyCon:Kind")
-     1
-     [alphaTyVar]
-     (mkStateTransformerTy realWorldTy alphaTy)
+       (u_tycon == getUnique unitTyCon) ||
+        ((u_tycon `elem` primArgTyConKeys) && 
+        not (u_tycon `elem` notLegalExternalTyCons))
+    _                 -> False
+
+-- 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 ]
+
+-- it's really unsafe to pass out references to objects in the heap,
+-- so for safe call-outs we simply disallow it.
+notSafeExternalTyCons =
+  [ byteArrayTyConKey, mutableByteArrayTyConKey ]
+
+
+isForeignObjTy :: Type -> Bool
+isForeignObjTy ty =
+  case (splitAlgTyConApp_maybe ty) of
+    Just (tycon, _, _) -> (getUnique tycon) == foreignObjTyConKey
+    _                 -> False
+    
 \end{code}
 
 %************************************************************************
@@ -511,31 +521,14 @@ primitive counterpart.
 \begin{code}
 boolTy = mkTyConTy boolTyCon
 
-boolTyCon = pcDataTyCon boolTyConKey pRELUDE_CORE SLIT("Bool") [] [falseDataCon, trueDataCon]
-
-falseDataCon = pcDataCon falseDataConKey pRELUDE_CORE SLIT("False") [] [] [] boolTyCon nullSpecEnv
-trueDataCon  = pcDataCon trueDataConKey         pRELUDE_CORE SLIT("True")  [] [] [] boolTyCon nullSpecEnv
-\end{code}
-
-%************************************************************************
-%*                                                                     *
-\subsection[TysWiredIn-Ordering]{The @Ordering@ type}
-%*                                                                     *
-%************************************************************************
-
-\begin{code}
----------------------------------------------
--- data Ordering = LT | EQ | GT deriving ()
----------------------------------------------
+boolTyCon = pcTyCon EnumType NonRecursive boolTyConKey 
+                   pREL_BASE SLIT("Bool") [] [] [falseDataCon, trueDataCon]
 
-orderingTy = mkTyConTy orderingTyCon
+falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
+trueDataCon  = pcDataCon trueDataConKey         pREL_BASE SLIT("True")  [] [] [] boolTyCon
 
-orderingTyCon = pcDataTyCon orderingTyConKey pRELUDE_BUILTIN SLIT("Ordering") []
-                           [ltDataCon, eqDataCon, gtDataCon]
-
-ltDataCon  = pcDataCon ltDataConKey pRELUDE_BUILTIN SLIT("LT") [] [] [] orderingTyCon nullSpecEnv
-eqDataCon  = pcDataCon eqDataConKey pRELUDE_BUILTIN SLIT("EQ") [] [] [] orderingTyCon nullSpecEnv
-gtDataCon  = pcDataCon gtDataConKey pRELUDE_BUILTIN SLIT("GT") [] [] [] orderingTyCon nullSpecEnv
+falseDataConId = dataConId falseDataCon
+trueDataConId  = dataConId trueDataCon
 \end{code}
 
 %************************************************************************
@@ -545,28 +538,26 @@ gtDataCon  = pcDataCon gtDataConKey pRELUDE_BUILTIN SLIT("GT") [] [] [] ordering
 %************************************************************************
 
 Special syntax, deeply wired in, but otherwise an ordinary algebraic
-data type:
+data types:
 \begin{verbatim}
-data List a = Nil | a : (List a)
-ToDo: data [] a = [] | a : (List a)
-ToDo: data () = ()
-      data (,,) a b c = (,,) a b c
+data [] a = [] | a : (List a)
+data () = ()
+data (,) a b = (,,) a b
+...
 \end{verbatim}
 
 \begin{code}
-mkListTy :: GenType t u -> GenType t u
-mkListTy ty = applyTyCon listTyCon [ty]
+mkListTy :: Type -> Type
+mkListTy ty = mkTyConApp listTyCon [ty]
 
-alphaListTy = mkSigmaTy [alphaTyVar] [] (applyTyCon listTyCon [alphaTy])
+alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty)
 
-listTyCon = pcDataTyCon listTyConKey pRELUDE_BUILTIN SLIT("[]") 
-                       [alphaTyVar] [nilDataCon, consDataCon]
+listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]") 
+                       alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
 
-nilDataCon  = pcDataCon nilDataConKey  pRELUDE_BUILTIN SLIT("[]") [alphaTyVar] [] [] listTyCon
-               (pcGenerateDataSpecs alphaListTy)
-consDataCon = pcDataCon consDataConKey pRELUDE_BUILTIN SLIT(":")
-               [alphaTyVar] [] [alphaTy, applyTyCon listTyCon [alphaTy]] listTyCon
-               (pcGenerateDataSpecs alphaListTy)
+nilDataCon  = pcDataCon nilDataConKey  pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon
+consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":")
+               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
 -- gets the over-specific type (Type -> Type)
@@ -619,99 +610,11 @@ done by enumeration\srcloc{lib/prelude/InTup?.hs}.
 \end{itemize}
 
 \begin{code}
-mkTupleTy :: Int -> [GenType t u] -> GenType t u
+mkTupleTy :: Int -> [Type] -> Type
+mkTupleTy arity tys = mkTyConApp (tupleTyCon arity) tys
 
-mkTupleTy arity tys = applyTyCon (mkTupleTyCon arity) tys
+mkUnboxedTupleTy :: Int -> [Type] -> Type
+mkUnboxedTupleTy arity tys = mkTyConApp (unboxedTupleTyCon arity) tys
 
 unitTy    = mkTupleTy 0 []
 \end{code}
-
-%************************************************************************
-%*                                                                     *
-\subsection[TysWiredIn-Ratios]{@Ratio@ and @Rational@}
-%*                                                                     *
-%************************************************************************
-
-ToDo: make this (mostly) go away.
-
-\begin{code}
-rationalTy :: GenType t u
-
-mkRatioTy ty = applyTyCon ratioTyCon [ty]
-rationalTy   = mkRatioTy integerTy
-
-ratioTyCon = pcDataTyCon ratioTyConKey pRELUDE_RATIO SLIT("Ratio") [alphaTyVar] [ratioDataCon]
-
-ratioDataCon = pcDataCon ratioDataConKey pRELUDE_RATIO SLIT(":%")
-               [alphaTyVar] [{-(integralClass,alphaTy)-}] [alphaTy, alphaTy] ratioTyCon nullSpecEnv
-       -- context omitted to match lib/prelude/ defn of "data Ratio ..."
-
-rationalTyCon
-  = mkSynTyCon
-      rationalTyConKey
-      (mkPreludeCoreName pRELUDE_RATIO SLIT("Rational"))
-      mkBoxedTypeKind
-      0         -- arity
-      [] -- tyvars
-      rationalTy -- == mkRatioTy integerTy
-\end{code}
-
-%************************************************************************
-%*                                                                     *
-\subsection[TysWiredIn-_Lift]{@_Lift@ type: to support array indexing}
-%*                                                                     *
-%************************************************************************
-
-Again, deeply turgid: \tr{data _Lift a = _Lift a}.
-
-\begin{code}
-mkLiftTy ty = applyTyCon liftTyCon [ty]
-
-{-
-mkLiftTy ty
-  = mkSigmaTy tvs theta (applyTyCon liftTyCon [tau])
-  where
-    (tvs, theta, tau) = splitSigmaTy ty
-
-isLiftTy ty
-  = case maybeAppDataTyCon tau of
-      Just (tycon, tys, _) -> tycon == liftTyCon
-      Nothing -> False
-  where
-    (tvs, theta, tau) = splitSigmaTy ty
--}
-
-
-alphaLiftTy = mkSigmaTy [alphaTyVar] [] (applyTyCon liftTyCon [alphaTy])
-
-liftTyCon
-  = pcDataTyCon liftTyConKey pRELUDE_BUILTIN SLIT("_Lift") [alphaTyVar] [liftDataCon]
-
-liftDataCon
-  = pcDataCon liftDataConKey pRELUDE_BUILTIN SLIT("_Lift")
-               [alphaTyVar] [] [alphaTy] liftTyCon
-               ((pcGenerateDataSpecs alphaLiftTy) `addOneToSpecEnv`
-                (mkSpecInfo [Just realWorldStatePrimTy] 0 bottom))
-  where
-    bottom = panic "liftDataCon:State# _RealWorld"
-\end{code}
-
-
-%************************************************************************
-%*                                                                     *
-\subsection[TysWiredIn-for-convenience]{Types wired in for convenience (e.g., @String@)}
-%*                                                                     *
-%************************************************************************
-
-\begin{code}
-stringTy = mkListTy charTy
-
-stringTyCon
- = mkSynTyCon
-     stringTyConKey
-     (mkPreludeCoreName pRELUDE_CORE SLIT("String"))
-     mkBoxedTypeKind
-     0
-     []   -- type variables
-     stringTy
-\end{code}