[project @ 1998-11-08 17:09:14 by sof]
[ghc-hetmet.git] / ghc / compiler / prelude / TysWiredIn.lhs
index 06c91a3..9980396 100644 (file)
@@ -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,37 +22,58 @@ module TysWiredIn (
        consDataCon,
        doubleDataCon,
        doubleTy,
+       isDoubleTy,
        doubleTyCon,
        falseDataCon,
        floatDataCon,
        floatTy,
+       isFloatTy,
        floatTyCon,
        getStatePairingConInfo,
+
        intDataCon,
        intTy,
        intTyCon,
+       isIntTy,
+       inIntRange,
+
+       int8TyCon,
+       int16TyCon,
+       int32TyCon,
+
+       int64TyCon,
+       int64DataCon,
+--     int64Ty,
+
        integerTy,
        integerTyCon,
        integerDataCon,
+       isIntegerTy,
+
        liftDataCon,
        liftTyCon,
        listTyCon,
        foreignObjTyCon,
+
        mkLiftTy,
        mkListTy,
-       mkPrimIoTy,
-       mkStateTy,
-       mkStateTransformerTy,
-       tupleTyCon, tupleCon, unitTyCon, unitDataCon, pairTyCon, pairDataCon,
        mkTupleTy,
+       tupleTyCon, tupleCon, unitTyCon, unitDataCon, pairTyCon, pairDataCon,
        nilDataCon,
-       primIoTyCon,
        realWorldStateTy,
        return2GMPsTyCon,
        returnIntAndGMPTyCon,
+
+       -- ST and STret types
+       mkStateTy,
+       mkStateTransformerTy,
+       mkSTretTy,
        stTyCon,
        stDataCon,
-       stablePtrTyCon,
+       stRetDataCon,
+       stRetTyCon,
+
+       -- CCall result types
        stateAndAddrPrimTyCon,
        stateAndArrayPrimTyCon,
        stateAndByteArrayPrimTyCon,
@@ -62,33 +81,46 @@ module TysWiredIn (
        stateAndDoublePrimTyCon,
        stateAndFloatPrimTyCon,
        stateAndIntPrimTyCon,
+       stateAndInt64PrimTyCon,
        stateAndForeignObjPrimTyCon,
        stateAndMutableArrayPrimTyCon,
        stateAndMutableByteArrayPrimTyCon,
        stateAndPtrPrimTyCon,
+       stateAndPtrPrimDataCon,
        stateAndStablePtrPrimTyCon,
        stateAndSynchVarPrimTyCon,
        stateAndWordPrimTyCon,
+       stateAndWord64PrimTyCon,
        stateDataCon,
        stateTyCon,
+
+       stablePtrTyCon,
        stringTy,
        trueDataCon,
        unitTy,
        wordDataCon,
        wordTy,
-       wordTyCon
+       wordTyCon,
+
+       word8TyCon,
+       word16TyCon,
+       word32TyCon,
+
+       word64DataCon,
+--     word64Ty,
+       word64TyCon,
+       
+       isFFIArgumentTy,  -- :: Type -> Bool
+       isFFIResultTy,    -- :: Type -> Bool
+       isFFIExternalTy,  -- :: Type -> Bool
+       isAddrTy,         -- :: Type -> Bool
+
     ) where
 
---ToDo:rm
---import Pretty
---import Util
---import PprType
---import PprStyle
---import Kind
+#include "HsVersions.h"
 
-IMP_Ubiq()
-IMPORT_DELOOPER(TyLoop)        ( mkDataCon, mkTupleCon, StrictnessMark(..) )
-IMPORT_DELOOPER(IdLoop)        ( SpecEnv )
+import {-# SOURCE #-} MkId ( mkDataCon, mkTupleCon )
+import {-# SOURCE #-} Id ( Id, StrictnessMark(..) )
 
 -- friends:
 import PrelMods
@@ -96,40 +128,45 @@ import TysPrim
 
 -- others:
 import Kind            ( mkBoxedTypeKind, mkArrowKind )
-import Name            ( mkWiredInTyConName, mkWiredInIdName, mkTupNameStr )
+import Name            ( mkWiredInTyConName, mkWiredInIdName )
 import TyCon           ( mkDataTyCon, mkTupleTyCon, mkSynTyCon,
-                         NewOrData(..), TyCon
+                         TyCon, Arity
                        )
-import Type            ( mkTyConTy, applyTyCon, mkSigmaTy, mkTyVarTys, 
-                         mkFunTy, mkFunTys, maybeAppTyCon,
-                         GenType(..), SYN_IE(ThetaType), SYN_IE(TauType) )
-import TyVar           ( tyVarKind, alphaTyVars, alphaTyVar, betaTyVar )
+import BasicTypes      ( Module, NewOrData(..), RecFlag(..) )
+import Type            ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys, 
+                         mkFunTy, mkFunTys, splitTyConApp_maybe, splitAlgTyConApp_maybe,
+                         GenType(..), ThetaType, TauType, isUnpointedType )
+import TyVar           ( GenTyVar, TyVar, tyVarKind, alphaTyVars, alphaTyVar, betaTyVar )
 import Lex             ( mkTupNameStr )
 import Unique
+import CmdLineOpts      ( opt_GlasgowExts )
 import Util            ( assoc, panic )
 
-nullSpecEnv =  error "TysWiredIn:nullSpecEnv =  "
-addOneToSpecEnv =  error "TysWiredIn:addOneToSpecEnv =  "
-pc_gen_specs = error "TysWiredIn:pc_gen_specs  "
-mkSpecInfo = error "TysWiredIn:SpecInfo"
 
 alpha_tyvar      = [alphaTyVar]
 alpha_ty         = [alphaTy]
 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
 
-pcDataTyCon, pcNewTyCon
+pcRecDataTyCon, pcNonRecDataTyCon, pcNonRecNewTyCon
        :: Unique{-TyConKey-} -> Module -> FAST_STRING
        -> [TyVar] -> [Id] -> TyCon
 
-pcDataTyCon = pc_tycon DataType
-pcNewTyCon  = pc_tycon NewType
+pcRecDataTyCon    = pc_tycon DataType Recursive
+pcNonRecDataTyCon = pc_tycon DataType NonRecursive
+pcNonRecNewTyCon  = pc_tycon NewType  NonRecursive
 
-pc_tycon new_or_data key mod str tyvars cons
+pc_tycon new_or_data is_rec key mod str tyvars cons
   = tycon
   where
     tycon = mkDataTyCon name tycon_kind 
-               tyvars [{-no context-}] cons [{-no derivings-}]
+               tyvars 
+               []              -- No context
+               cons
+               []              -- No derivings
+               Nothing         -- Not a dictionary
                new_or_data
+               is_rec
+
     name = mkWiredInTyConName key mod str tycon
     tycon_kind = foldr (mkArrowKind . tyVarKind) mkBoxedTypeKind tyvars
 
@@ -140,21 +177,15 @@ pcSynTyCon key mod str kind arity tyvars expansion
     name  = mkWiredInTyConName key mod str tycon
 
 pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
-         -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> SpecEnv -> Id
-pcDataCon key mod str tyvars context arg_tys tycon specenv
+         -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> Id
+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
+               tyvars context [] [] arg_tys tycon
     name = mkWiredInIdName key mod str data_con
-
-pcGenerateDataSpecs :: Type -> SpecEnv
-pcGenerateDataSpecs ty
-  = pc_gen_specs False err err err ty
-  where
-    err = panic "PrelUtils:GenerateDataSpecs"
 \end{code}
 
 %************************************************************************
@@ -183,7 +214,7 @@ tupleCon arity
     name      = mkWiredInIdName uniq mod_name (mkTupNameStr arity) tuple_con
     mod_name  | arity == 0 = pREL_BASE
              | otherwise  = pREL_TUP
-    ty                 = mkSigmaTy tyvars [] (mkFunTys tyvar_tys (applyTyCon tycon tyvar_tys))
+    ty                 = mkSigmaTy tyvars [] (mkFunTys tyvar_tys (mkTyConApp tycon tyvar_tys))
     tyvars     = take arity alphaTyVars
     tyvar_tys  = mkTyVarTys tyvars
     tycon      = tupleTyCon arity
@@ -205,8 +236,8 @@ pairDataCon = tupleCon 2
 \begin{code}
 charTy = mkTyConTy charTyCon
 
-charTyCon = pcDataTyCon charTyConKey  pREL_BASE  SLIT("Char") [] [charDataCon]
-charDataCon = pcDataCon charDataConKey pREL_BASE 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}
@@ -214,66 +245,134 @@ stringTy = mkListTy charTy -- convenience only
 \begin{code}
 intTy = mkTyConTy intTyCon 
 
-intTyCon = pcDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [intDataCon]
-intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon nullSpecEnv
+intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [intDataCon]
+intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
+
+isIntTy :: GenType flexi -> Bool
+isIntTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> uniqueOf 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
+
+int64Ty = mkTyConTy int64TyCon 
+
+int64TyCon = pcNonRecDataTyCon int64TyConKey pREL_CCALL SLIT("Int64") [] [int64DataCon]
+int64DataCon = pcDataCon int64DataConKey pREL_CCALL SLIT("I64#") [] [] [int64PrimTy] int64TyCon
 \end{code}
 
 \begin{code}
+
 wordTy = mkTyConTy wordTyCon
 
-wordTyCon = pcDataTyCon wordTyConKey   fOREIGN SLIT("Word") [] [wordDataCon]
-wordDataCon = pcDataCon wordDataConKey gHC__ SLIT("W#") [] [] [wordPrimTy] wordTyCon nullSpecEnv
+wordTyCon = pcNonRecDataTyCon wordTyConKey   pREL_FOREIGN SLIT("Word") [] [wordDataCon]
+wordDataCon = pcDataCon wordDataConKey pREL_FOREIGN 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
+
+word64Ty = mkTyConTy word64TyCon
+
+word64TyCon = pcNonRecDataTyCon word64TyConKey   pREL_CCALL SLIT("Word64") [] [word64DataCon]
+word64DataCon = pcDataCon word64DataConKey pREL_CCALL SLIT("W64#") [] [] [word64PrimTy] word64TyCon
 \end{code}
 
 \begin{code}
 addrTy = mkTyConTy addrTyCon
 
-addrTyCon = pcDataTyCon addrTyConKey   fOREIGN SLIT("Addr") [] [addrDataCon]
-addrDataCon = pcDataCon addrDataConKey gHC__ SLIT("A#") [] [] [addrPrimTy] addrTyCon nullSpecEnv
+addrTyCon = pcNonRecDataTyCon addrTyConKey   pREL_ADDR SLIT("Addr") [] [addrDataCon]
+addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
+
+isAddrTy :: GenType flexi -> Bool
+isAddrTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> uniqueOf tycon == addrTyConKey
+       _                   -> False
+
 \end{code}
 
 \begin{code}
 floatTy        = mkTyConTy floatTyCon
 
-floatTyCon = pcDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [floatDataCon]
-floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon nullSpecEnv
+floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [floatDataCon]
+floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon
+
+isFloatTy :: GenType flexi -> Bool
+isFloatTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> uniqueOf tycon == floatTyConKey
+       _                   -> False
+
 \end{code}
 
 \begin{code}
 doubleTy = mkTyConTy doubleTyCon
 
-doubleTyCon = pcDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [doubleDataCon]
-doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon nullSpecEnv
+isDoubleTy :: GenType flexi -> Bool
+isDoubleTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> uniqueOf tycon == doubleTyConKey
+       _                   -> False
+
+doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [doubleDataCon]
+doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon
 \end{code}
 
 \begin{code}
-mkStateTy ty    = applyTyCon stateTyCon [ty]
+mkStateTy ty    = mkTyConApp stateTyCon [ty]
 realWorldStateTy = mkStateTy realWorldTy -- a common use
 
-stateTyCon = pcDataTyCon stateTyConKey sT_BASE SLIT("State") alpha_tyvar [stateDataCon]
+stateTyCon = pcNonRecDataTyCon stateTyConKey pREL_ST SLIT("State") alpha_tyvar [stateDataCon]
 stateDataCon
-  = pcDataCon stateDataConKey sT_BASE SLIT("S#")
-       alpha_tyvar [] [mkStatePrimTy alphaTy] stateTyCon nullSpecEnv
+  = pcDataCon stateDataConKey pREL_ST SLIT("S#")
+       alpha_tyvar [] [mkStatePrimTy alphaTy] stateTyCon
 \end{code}
 
 \begin{code}
 stablePtrTyCon
-  = pcDataTyCon stablePtrTyConKey fOREIGN SLIT("StablePtr")
+  = pcNonRecDataTyCon stablePtrTyConKey pREL_FOREIGN SLIT("StablePtr")
        alpha_tyvar [stablePtrDataCon]
   where
     stablePtrDataCon
-      = pcDataCon stablePtrDataConKey fOREIGN SLIT("StablePtr")
-           alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon nullSpecEnv
+      = pcDataCon stablePtrDataConKey pREL_FOREIGN SLIT("StablePtr")
+           alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
 \end{code}
 
 \begin{code}
 foreignObjTyCon
-  = pcDataTyCon foreignObjTyConKey fOREIGN SLIT("ForeignObj")
+  = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj")
        [] [foreignObjDataCon]
   where
     foreignObjDataCon
-      = pcDataCon foreignObjDataConKey fOREIGN SLIT("ForeignObj")
-           [] [] [foreignObjPrimTy] foreignObjTyCon nullSpecEnv
+      = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
+           [] [] [foreignObjPrimTy] foreignObjTyCon
 \end{code}
 
 %************************************************************************
@@ -284,31 +383,37 @@ foreignObjTyCon
 
 @Integer@ and its pals are not really primitive.  @Integer@ itself, first:
 \begin{code}
-integerTy :: GenType t u
+integerTy :: GenType t
 integerTy    = mkTyConTy integerTyCon
 
-integerTyCon = pcDataTyCon integerTyConKey pREL_BASE SLIT("Integer") [] [integerDataCon]
+integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_BASE SLIT("Integer") [] [integerDataCon]
 
 integerDataCon = pcDataCon integerDataConKey pREL_BASE SLIT("J#")
-               [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon nullSpecEnv
+               [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon
+
+isIntegerTy :: GenType flexi -> Bool
+isIntegerTy ty
+  = case (splitAlgTyConApp_maybe ty) of
+       Just (tycon, [], _) -> uniqueOf tycon == integerTyConKey
+       _                   -> False
 \end{code}
 
 And the other pairing types:
 \begin{code}
-return2GMPsTyCon = pcDataTyCon return2GMPsTyConKey
+return2GMPsTyCon = pcNonRecDataTyCon return2GMPsTyConKey
        pREL_NUM SLIT("Return2GMPs") [] [return2GMPsDataCon]
 
 return2GMPsDataCon
   = pcDataCon return2GMPsDataConKey pREL_NUM SLIT("Return2GMPs") [] []
        [intPrimTy, intPrimTy, byteArrayPrimTy,
-        intPrimTy, intPrimTy, byteArrayPrimTy] return2GMPsTyCon nullSpecEnv
+        intPrimTy, intPrimTy, byteArrayPrimTy] return2GMPsTyCon
 
-returnIntAndGMPTyCon = pcDataTyCon returnIntAndGMPTyConKey
+returnIntAndGMPTyCon = pcNonRecDataTyCon returnIntAndGMPTyConKey
        pREL_NUM SLIT("ReturnIntAndGMP") [] [returnIntAndGMPDataCon]
 
 returnIntAndGMPDataCon
   = pcDataCon returnIntAndGMPDataConKey pREL_NUM SLIT("ReturnIntAndGMP") [] []
-       [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy] returnIntAndGMPTyCon nullSpecEnv
+       [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy] returnIntAndGMPTyCon
 \end{code}
 
 %************************************************************************
@@ -326,120 +431,136 @@ We fish one of these \tr{StateAnd<blah>#} things with
 
 \begin{code}
 stateAndPtrPrimTyCon
-  = pcDataTyCon stateAndPtrPrimTyConKey sT_BASE SLIT("StateAndPtr#")
+  = pcNonRecDataTyCon stateAndPtrPrimTyConKey pREL_ST SLIT("StateAndPtr#")
                alpha_beta_tyvars [stateAndPtrPrimDataCon]
 stateAndPtrPrimDataCon
-  = pcDataCon stateAndPtrPrimDataConKey sT_BASE SLIT("StateAndPtr#")
+  = pcDataCon stateAndPtrPrimDataConKey pREL_ST SLIT("StateAndPtr#")
                alpha_beta_tyvars [] [mkStatePrimTy alphaTy, betaTy]
-               stateAndPtrPrimTyCon nullSpecEnv
+               stateAndPtrPrimTyCon
 
 stateAndCharPrimTyCon
-  = pcDataTyCon stateAndCharPrimTyConKey sT_BASE SLIT("StateAndChar#")
+  = pcNonRecDataTyCon stateAndCharPrimTyConKey pREL_ST SLIT("StateAndChar#")
                alpha_tyvar [stateAndCharPrimDataCon]
 stateAndCharPrimDataCon
-  = pcDataCon stateAndCharPrimDataConKey sT_BASE SLIT("StateAndChar#")
+  = pcDataCon stateAndCharPrimDataConKey pREL_ST SLIT("StateAndChar#")
                alpha_tyvar [] [mkStatePrimTy alphaTy, charPrimTy]
-               stateAndCharPrimTyCon nullSpecEnv
+               stateAndCharPrimTyCon
 
 stateAndIntPrimTyCon
-  = pcDataTyCon stateAndIntPrimTyConKey sT_BASE SLIT("StateAndInt#")
+  = pcNonRecDataTyCon stateAndIntPrimTyConKey pREL_ST SLIT("StateAndInt#")
                alpha_tyvar [stateAndIntPrimDataCon]
 stateAndIntPrimDataCon
-  = pcDataCon stateAndIntPrimDataConKey sT_BASE SLIT("StateAndInt#")
+  = pcDataCon stateAndIntPrimDataConKey pREL_ST SLIT("StateAndInt#")
                alpha_tyvar [] [mkStatePrimTy alphaTy, intPrimTy]
-               stateAndIntPrimTyCon nullSpecEnv
+               stateAndIntPrimTyCon
+
+stateAndInt64PrimTyCon
+  = pcNonRecDataTyCon stateAndInt64PrimTyConKey pREL_ST SLIT("StateAndInt64#")
+               alpha_tyvar [stateAndInt64PrimDataCon]
+stateAndInt64PrimDataCon
+  = pcDataCon stateAndInt64PrimDataConKey pREL_ST SLIT("StateAndInt64#")
+               alpha_tyvar [] [mkStatePrimTy alphaTy, int64PrimTy]
+               stateAndInt64PrimTyCon
 
 stateAndWordPrimTyCon
-  = pcDataTyCon stateAndWordPrimTyConKey sT_BASE SLIT("StateAndWord#")
+  = pcNonRecDataTyCon stateAndWordPrimTyConKey pREL_ST SLIT("StateAndWord#")
                alpha_tyvar [stateAndWordPrimDataCon]
 stateAndWordPrimDataCon
-  = pcDataCon stateAndWordPrimDataConKey sT_BASE SLIT("StateAndWord#")
+  = pcDataCon stateAndWordPrimDataConKey pREL_ST SLIT("StateAndWord#")
                alpha_tyvar [] [mkStatePrimTy alphaTy, wordPrimTy]
-               stateAndWordPrimTyCon nullSpecEnv
+               stateAndWordPrimTyCon
+
+stateAndWord64PrimTyCon
+  = pcNonRecDataTyCon stateAndWord64PrimTyConKey pREL_ST SLIT("StateAndWord64#")
+               alpha_tyvar [stateAndWord64PrimDataCon]
+stateAndWord64PrimDataCon
+  = pcDataCon stateAndWord64PrimDataConKey pREL_ST SLIT("StateAndWord64#")
+               alpha_tyvar [] [mkStatePrimTy alphaTy, word64PrimTy]
+               stateAndWord64PrimTyCon
 
 stateAndAddrPrimTyCon
-  = pcDataTyCon stateAndAddrPrimTyConKey sT_BASE SLIT("StateAndAddr#")
+  = pcNonRecDataTyCon stateAndAddrPrimTyConKey pREL_ST SLIT("StateAndAddr#")
                alpha_tyvar [stateAndAddrPrimDataCon]
 stateAndAddrPrimDataCon
-  = pcDataCon stateAndAddrPrimDataConKey sT_BASE SLIT("StateAndAddr#")
+  = pcDataCon stateAndAddrPrimDataConKey pREL_ST SLIT("StateAndAddr#")
                alpha_tyvar [] [mkStatePrimTy alphaTy, addrPrimTy]
-               stateAndAddrPrimTyCon nullSpecEnv
+               stateAndAddrPrimTyCon
 
 stateAndStablePtrPrimTyCon
-  = pcDataTyCon stateAndStablePtrPrimTyConKey fOREIGN SLIT("StateAndStablePtr#")
+  = pcNonRecDataTyCon stateAndStablePtrPrimTyConKey pREL_FOREIGN SLIT("StateAndStablePtr#")
                alpha_beta_tyvars [stateAndStablePtrPrimDataCon]
 stateAndStablePtrPrimDataCon
-  = pcDataCon stateAndStablePtrPrimDataConKey fOREIGN SLIT("StateAndStablePtr#")
+  = pcDataCon stateAndStablePtrPrimDataConKey pREL_FOREIGN SLIT("StateAndStablePtr#")
                alpha_beta_tyvars []
-               [mkStatePrimTy alphaTy, applyTyCon stablePtrPrimTyCon [betaTy]]
-               stateAndStablePtrPrimTyCon nullSpecEnv
+               [mkStatePrimTy alphaTy, mkTyConApp stablePtrPrimTyCon [betaTy]]
+               stateAndStablePtrPrimTyCon
 
 stateAndForeignObjPrimTyCon
-  = pcDataTyCon stateAndForeignObjPrimTyConKey fOREIGN SLIT("StateAndForeignObj#")
+  = pcNonRecDataTyCon stateAndForeignObjPrimTyConKey pREL_IO_BASE SLIT("StateAndForeignObj#")
                alpha_tyvar [stateAndForeignObjPrimDataCon]
 stateAndForeignObjPrimDataCon
-  = pcDataCon stateAndForeignObjPrimDataConKey fOREIGN SLIT("StateAndForeignObj#")
+  = pcDataCon stateAndForeignObjPrimDataConKey pREL_IO_BASE SLIT("StateAndForeignObj#")
                alpha_tyvar []
-               [mkStatePrimTy alphaTy, applyTyCon foreignObjPrimTyCon []]
-               stateAndForeignObjPrimTyCon nullSpecEnv
+               [mkStatePrimTy alphaTy, mkTyConTy foreignObjPrimTyCon]
+               stateAndForeignObjPrimTyCon
 
 stateAndFloatPrimTyCon
-  = pcDataTyCon stateAndFloatPrimTyConKey sT_BASE SLIT("StateAndFloat#")
+  = pcNonRecDataTyCon stateAndFloatPrimTyConKey pREL_ST SLIT("StateAndFloat#")
                alpha_tyvar [stateAndFloatPrimDataCon]
 stateAndFloatPrimDataCon
-  = pcDataCon stateAndFloatPrimDataConKey sT_BASE SLIT("StateAndFloat#")
+  = pcDataCon stateAndFloatPrimDataConKey pREL_ST SLIT("StateAndFloat#")
                alpha_tyvar [] [mkStatePrimTy alphaTy, floatPrimTy]
-               stateAndFloatPrimTyCon nullSpecEnv
+               stateAndFloatPrimTyCon
 
 stateAndDoublePrimTyCon
-  = pcDataTyCon stateAndDoublePrimTyConKey sT_BASE SLIT("StateAndDouble#")
+  = pcNonRecDataTyCon stateAndDoublePrimTyConKey pREL_ST SLIT("StateAndDouble#")
                alpha_tyvar [stateAndDoublePrimDataCon]
 stateAndDoublePrimDataCon
-  = pcDataCon stateAndDoublePrimDataConKey sT_BASE SLIT("StateAndDouble#")
+  = pcDataCon stateAndDoublePrimDataConKey pREL_ST SLIT("StateAndDouble#")
                alpha_tyvar [] [mkStatePrimTy alphaTy, doublePrimTy]
-               stateAndDoublePrimTyCon nullSpecEnv
+               stateAndDoublePrimTyCon
 \end{code}
 
 \begin{code}
 stateAndArrayPrimTyCon
-  = pcDataTyCon stateAndArrayPrimTyConKey aRR_BASE SLIT("StateAndArray#")
+  = pcNonRecDataTyCon stateAndArrayPrimTyConKey pREL_ARR SLIT("StateAndArray#")
                alpha_beta_tyvars [stateAndArrayPrimDataCon]
 stateAndArrayPrimDataCon
-  = pcDataCon stateAndArrayPrimDataConKey aRR_BASE SLIT("StateAndArray#")
+  = pcDataCon stateAndArrayPrimDataConKey pREL_ARR SLIT("StateAndArray#")
                alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkArrayPrimTy betaTy]
-               stateAndArrayPrimTyCon nullSpecEnv
+               stateAndArrayPrimTyCon
 
 stateAndMutableArrayPrimTyCon
-  = pcDataTyCon stateAndMutableArrayPrimTyConKey aRR_BASE SLIT("StateAndMutableArray#")
+  = pcNonRecDataTyCon stateAndMutableArrayPrimTyConKey pREL_ARR SLIT("StateAndMutableArray#")
                alpha_beta_tyvars [stateAndMutableArrayPrimDataCon]
 stateAndMutableArrayPrimDataCon
-  = pcDataCon stateAndMutableArrayPrimDataConKey aRR_BASE SLIT("StateAndMutableArray#")
+  = pcDataCon stateAndMutableArrayPrimDataConKey pREL_ARR SLIT("StateAndMutableArray#")
                alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkMutableArrayPrimTy alphaTy betaTy]
-               stateAndMutableArrayPrimTyCon nullSpecEnv
+               stateAndMutableArrayPrimTyCon
 
 stateAndByteArrayPrimTyCon
-  = pcDataTyCon stateAndByteArrayPrimTyConKey aRR_BASE SLIT("StateAndByteArray#")
+  = pcNonRecDataTyCon stateAndByteArrayPrimTyConKey pREL_ARR SLIT("StateAndByteArray#")
                alpha_tyvar [stateAndByteArrayPrimDataCon]
 stateAndByteArrayPrimDataCon
-  = pcDataCon stateAndByteArrayPrimDataConKey aRR_BASE SLIT("StateAndByteArray#")
+  = pcDataCon stateAndByteArrayPrimDataConKey pREL_ARR SLIT("StateAndByteArray#")
                alpha_tyvar [] [mkStatePrimTy alphaTy, byteArrayPrimTy]
-               stateAndByteArrayPrimTyCon nullSpecEnv
+               stateAndByteArrayPrimTyCon
 
 stateAndMutableByteArrayPrimTyCon
-  = pcDataTyCon stateAndMutableByteArrayPrimTyConKey aRR_BASE SLIT("StateAndMutableByteArray#")
+  = pcNonRecDataTyCon stateAndMutableByteArrayPrimTyConKey pREL_ARR SLIT("StateAndMutableByteArray#")
                alpha_tyvar [stateAndMutableByteArrayPrimDataCon]
 stateAndMutableByteArrayPrimDataCon
-  = pcDataCon stateAndMutableByteArrayPrimDataConKey aRR_BASE SLIT("StateAndMutableByteArray#")
-               alpha_tyvar [] [mkStatePrimTy alphaTy, applyTyCon mutableByteArrayPrimTyCon alpha_ty]
-               stateAndMutableByteArrayPrimTyCon nullSpecEnv
+  = pcDataCon stateAndMutableByteArrayPrimDataConKey pREL_ARR SLIT("StateAndMutableByteArray#")
+               alpha_tyvar [] [mkStatePrimTy alphaTy, mkTyConApp mutableByteArrayPrimTyCon alpha_ty]
+               stateAndMutableByteArrayPrimTyCon
 
 stateAndSynchVarPrimTyCon
-  = pcDataTyCon stateAndSynchVarPrimTyConKey cONC_BASE SLIT("StateAndSynchVar#")
+  = pcNonRecDataTyCon stateAndSynchVarPrimTyConKey pREL_CONC SLIT("StateAndSynchVar#")
                alpha_beta_tyvars [stateAndSynchVarPrimDataCon]
 stateAndSynchVarPrimDataCon
-  = pcDataCon stateAndSynchVarPrimDataConKey cONC_BASE SLIT("StateAndSynchVar#")
+  = pcDataCon stateAndSynchVarPrimDataConKey pREL_CONC SLIT("StateAndSynchVar#")
                alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkSynchVarPrimTy alphaTy betaTy]
-               stateAndSynchVarPrimTyCon nullSpecEnv
+               stateAndSynchVarPrimTyCon
 \end{code}
 
 The ccall-desugaring mechanism uses this function to figure out how to
@@ -453,12 +574,12 @@ getStatePairingConInfo
            Type)       -- type of state pair
 
 getStatePairingConInfo prim_ty
-  = case (maybeAppTyCon prim_ty) of
+  = case (splitTyConApp_maybe prim_ty) of
       Nothing -> panic "getStatePairingConInfo:1"
       Just (prim_tycon, tys_applied) ->
        let
            (pair_con, pair_tycon, num_tys) = assoc "getStatePairingConInfo" tbl prim_tycon
-           pair_ty = applyTyCon pair_tycon (realWorldTy : drop num_tys tys_applied)
+           pair_ty = mkTyConApp pair_tycon (realWorldTy : drop num_tys tys_applied)
        in
        (pair_con, pair_ty)
   where
@@ -466,6 +587,8 @@ getStatePairingConInfo prim_ty
        (charPrimTyCon, (stateAndCharPrimDataCon, stateAndCharPrimTyCon, 0)),
        (intPrimTyCon, (stateAndIntPrimDataCon, stateAndIntPrimTyCon, 0)),
        (wordPrimTyCon, (stateAndWordPrimDataCon, stateAndWordPrimTyCon, 0)),
+       (int64PrimTyCon, (stateAndInt64PrimDataCon, stateAndInt64PrimTyCon, 0)),
+       (word64PrimTyCon, (stateAndWord64PrimDataCon, stateAndWord64PrimTyCon, 0)),
        (addrPrimTyCon, (stateAndAddrPrimDataCon, stateAndAddrPrimTyCon, 0)),
        (stablePtrPrimTyCon, (stateAndStablePtrPrimDataCon, stateAndStablePtrPrimTyCon, 0)),
        (foreignObjPrimTyCon, (stateAndForeignObjPrimDataCon, stateAndForeignObjPrimTyCon, 0)),
@@ -480,39 +603,99 @@ getStatePairingConInfo prim_ty
        ]
 \end{code}
 
+
 %************************************************************************
 %*                                                                     *
-\subsection[TysWiredIn-ST]{The basic @_ST@ state-transformer type}
+\subsection[TysWiredIn-ext-type]{External types}
 %*                                                                     *
 %************************************************************************
 
-This is really just an ordinary synonym, except it is ABSTRACT.
+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}
-mkStateTransformerTy s a = applyTyCon stTyCon [s, a]
-
-stTyCon = pcNewTyCon stTyConKey sT_BASE SLIT("ST") alpha_beta_tyvars [stDataCon]
-
-stDataCon = pcDataCon stDataConKey sT_BASE SLIT("ST")
-                       alpha_beta_tyvars [] [ty] stTyCon nullSpecEnv
-  where
-    ty = mkFunTy (mkStateTy alphaTy) (mkTupleTy 2 [betaTy, mkStateTy alphaTy])
+isFFIArgumentTy :: Type -> Bool
+isFFIArgumentTy ty =
+  (opt_GlasgowExts && isUnpointedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
+  case (splitAlgTyConApp_maybe ty) of
+    Just (tycon, _, _) -> (uniqueOf 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 && isUnpointedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
+  case (splitAlgTyConApp_maybe ty) of
+    Just (tycon, _, _) -> 
+       let 
+        u_tycon = uniqueOf tycon
+       in  
+       (u_tycon `elem` primArgTyConKeys) &&
+       not (u_tycon `elem` notLegalExternalTyCons)
+    _                 -> False
+
+
+isFFIResultTy :: Type -> Bool
+isFFIResultTy ty =
+   not (isUnpointedType ty) &&
+   case (splitAlgTyConApp_maybe ty) of
+    Just (tycon, _, _) -> 
+       let
+        u_tycon = uniqueOf tycon
+       in
+       (u_tycon == uniqueOf 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 ]
+    
 \end{code}
 
+
 %************************************************************************
 %*                                                                     *
-\subsection[TysWiredIn-IO]{The @PrimIO@ monadic-I/O type}
+\subsection[TysWiredIn-ST]{The basic @_ST@ state-transformer type}
 %*                                                                     *
 %************************************************************************
 
+The only reason this is wired in is because we have to represent the
+type of runST.
+
 \begin{code}
-mkPrimIoTy a = mkStateTransformerTy realWorldTy a
+mkStateTransformerTy s a = mkTyConApp stTyCon [s, a]
+
+stTyCon = pcNonRecNewTyCon stTyConKey pREL_ST SLIT("ST") alpha_beta_tyvars [stDataCon]
+
+stDataCon = pcDataCon stDataConKey pREL_ST SLIT("ST")
+                       alpha_beta_tyvars [] [ty] stTyCon
+  where
+    ty = mkFunTy (mkStatePrimTy alphaTy) (mkSTretTy alphaTy betaTy)
+
+mkSTretTy alpha beta = mkTyConApp stRetTyCon [alpha,beta]
 
-primIoTyCon
-  = pcSynTyCon
-     primIoTyConKey iO_BASE SLIT("PrimIO")
-     (mkBoxedTypeKind `mkArrowKind` mkBoxedTypeKind)
-     1 alpha_tyvar (mkPrimIoTy alphaTy)
+stRetTyCon
+  = pcNonRecDataTyCon stRetTyConKey pREL_ST SLIT("STret") 
+       alpha_beta_tyvars [stRetDataCon]
+stRetDataCon
+  = pcDataCon stRetDataConKey pREL_ST SLIT("STret")
+       alpha_beta_tyvars [] [mkStatePrimTy alphaTy, betaTy] 
+               stRetTyCon
 \end{code}
 
 %************************************************************************
@@ -566,10 +749,10 @@ primitive counterpart.
 \begin{code}
 boolTy = mkTyConTy boolTyCon
 
-boolTyCon = pcDataTyCon boolTyConKey pREL_BASE SLIT("Bool") [] [falseDataCon, trueDataCon]
+boolTyCon = pcNonRecDataTyCon boolTyConKey pREL_BASE SLIT("Bool") [] [falseDataCon, trueDataCon]
 
-falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon nullSpecEnv
-trueDataCon  = pcDataCon trueDataConKey         pREL_BASE SLIT("True")  [] [] [] boolTyCon nullSpecEnv
+falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
+trueDataCon  = pcDataCon trueDataConKey         pREL_BASE SLIT("True")  [] [] [] boolTyCon
 \end{code}
 
 %************************************************************************
@@ -588,19 +771,17 @@ data (,) a b = (,,) a b
 \end{verbatim}
 
 \begin{code}
-mkListTy :: GenType t u -> GenType t u
-mkListTy ty = applyTyCon listTyCon [ty]
+mkListTy :: GenType t -> GenType t
+mkListTy ty = mkTyConApp listTyCon [ty]
 
-alphaListTy = mkSigmaTy alpha_tyvar [] (applyTyCon listTyCon alpha_ty)
+alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty)
 
-listTyCon = pcDataTyCon listTyConKey pREL_BASE SLIT("[]") 
+listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]") 
                        alpha_tyvar [nilDataCon, consDataCon]
 
 nilDataCon  = pcDataCon nilDataConKey  pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon
-               (pcGenerateDataSpecs alphaListTy)
 consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":")
-               alpha_tyvar [] [alphaTy, applyTyCon listTyCon alpha_ty] listTyCon
-               (pcGenerateDataSpecs alphaListTy)
+               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)
@@ -653,9 +834,9 @@ done by enumeration\srcloc{lib/prelude/InTup?.hs}.
 \end{itemize}
 
 \begin{code}
-mkTupleTy :: Int -> [GenType t u] -> GenType t u
+mkTupleTy :: Int -> [GenType t] -> GenType t
 
-mkTupleTy arity tys = applyTyCon (tupleTyCon arity) tys
+mkTupleTy arity tys = mkTyConApp (tupleTyCon arity) tys
 
 unitTy    = mkTupleTy 0 []
 \end{code}
@@ -669,16 +850,16 @@ unitTy    = mkTupleTy 0 []
 Again, deeply turgid: \tr{data _Lift a = _Lift a}.
 
 \begin{code}
-mkLiftTy ty = applyTyCon liftTyCon [ty]
+mkLiftTy ty = mkTyConApp liftTyCon [ty]
 
 {-
 mkLiftTy ty
-  = mkSigmaTy tvs theta (applyTyCon liftTyCon [tau])
+  = mkSigmaTy tvs theta (mkTyConApp liftTyCon [tau])
   where
     (tvs, theta, tau) = splitSigmaTy ty
 
 isLiftTy ty
-  = case (maybeAppDataTyConExpandingDicts tau) of
+  = case (splitAlgTyConApp_maybeExpandingDicts tau) of
       Just (tycon, tys, _) -> tycon == liftTyCon
       Nothing -> False
   where
@@ -686,16 +867,14 @@ isLiftTy ty
 -}
 
 
-alphaLiftTy = mkSigmaTy alpha_tyvar [] (applyTyCon liftTyCon alpha_ty)
+alphaLiftTy = mkSigmaTy alpha_tyvar [] (mkTyConApp liftTyCon alpha_ty)
 
 liftTyCon
-  = pcDataTyCon liftTyConKey pREL_BASE SLIT("Lift") alpha_tyvar [liftDataCon]
+  = pcNonRecDataTyCon liftTyConKey pREL_BASE SLIT("Lift") alpha_tyvar [liftDataCon]
 
 liftDataCon
   = pcDataCon liftDataConKey pREL_BASE SLIT("Lift")
                alpha_tyvar [] alpha_ty liftTyCon
-               ((pcGenerateDataSpecs alphaLiftTy) `addOneToSpecEnv`
-                (mkSpecInfo [Just realWorldStatePrimTy] 0 bottom))
   where
     bottom = panic "liftDataCon:State# _RealWorld"
 \end{code}