[project @ 2005-01-26 15:04:08 by simonmar]
[ghc-hetmet.git] / ghc / compiler / iface / IfaceSyn.lhs
index 041a5f5..c6a8eb2 100644 (file)
@@ -14,11 +14,14 @@ We could either use this, or parameterise @GenCoreExpr@ on @Types@ and
 module IfaceSyn (
        module IfaceType,               -- Re-export all this
 
-       IfaceDecl(..), IfaceClassOp(..), IfaceConDecl(..),
+       IfaceDecl(..), IfaceClassOp(..), IfaceConDecl(..), IfaceConDecls(..),
        IfaceExpr(..), IfaceAlt, IfaceNote(..),
        IfaceBinding(..), IfaceConAlt(..), IfaceIdInfo(..),
        IfaceInfoItem(..), IfaceRule(..), IfaceInst(..), 
 
+       -- Misc
+       visibleIfConDecls,
+
        -- Converting things to IfaceSyn
        tyThingToIfaceDecl, dfunToIfaceInst, coreRuleToIfaceRule,
 
@@ -27,7 +30,7 @@ module IfaceSyn (
        eqIfDecl, eqIfInst, eqIfRule, 
        
        -- Pretty printing
-       pprIfaceExpr, pprIfaceDecl
+       pprIfaceExpr, pprIfaceDecl, pprIfaceDeclHead 
     ) where
 
 #include "HsVersions.h"
@@ -37,33 +40,34 @@ import IfaceType
 
 import FunDeps         ( pprFundeps )
 import NewDemand       ( StrictSig, pprIfaceStrictSig )
-import TcType          ( deNoteType, mkSigmaTy, tcSplitDFunTy, mkClassPred )
-import Type            ( TyThing(..), mkForAllTys, mkFunTys, splitForAllTys, funResultTy,
-                         mkTyVarTys, mkTyConApp, mkTyVarTys, mkPredTy, tidyTopType )
+import TcType          ( deNoteType, tcSplitDFunTy, mkClassPred )
+import Type            ( TyThing(..), mkForAllTys, splitForAllTys, funResultTy,
+                         mkPredTy, tidyTopType )
 import InstEnv         ( DFunId )
 import Id              ( Id, idName, idType, idInfo, idArity, isDataConWorkId_maybe, isFCallId_maybe )
 import NewDemand       ( isTopSig )
 import IdInfo          ( IdInfo, CafInfo(..), WorkerInfo(..), 
                          arityInfo, cafInfo, newStrictnessInfo, 
                          workerInfo, unfoldingInfo, inlinePragInfo )
-import TyCon           ( ArgVrcs, DataConDetails(..), isRecursiveTyCon, isForeignTyCon,
-                         isSynTyCon, isNewTyCon, isAlgTyCon, isPrimTyCon, isFunTyCon,
+import TyCon           ( TyCon, ArgVrcs, AlgTyConRhs(..), isRecursiveTyCon, isForeignTyCon,
+                         isSynTyCon, isAlgTyCon, isPrimTyCon, isFunTyCon,
                          isTupleTyCon, tupleTyConBoxity,
-                         tyConHasGenerics, tyConArgVrcs, tyConTheta, getSynTyConDefn,
-                         tyConArity, tyConTyVars, tyConDataConDetails, tyConExtName  )
+                         tyConHasGenerics, tyConArgVrcs, getSynTyConDefn,
+                         tyConArity, tyConTyVars, algTyConRhs, tyConExtName  )
 import DataCon         ( dataConName, dataConSig, dataConFieldLabels, dataConStrictMarks,
-                         dataConTyCon )
+                         dataConTyCon, dataConIsInfix, isVanillaDataCon )
 import Class           ( FunDep, DefMeth, classExtraBigSig, classTyCon )
 import OccName         ( OccName, OccEnv, lookupOccEnv, emptyOccEnv, 
                          lookupOccEnv, extendOccEnv, emptyOccEnv,
                          OccSet, unionOccSets, unitOccSet )
-import Name            ( Name, NamedThing(..), getOccName, nameOccName, nameModuleName, isExternalName )
-import Module          ( ModuleName )
+import Name            ( Name, NamedThing(..), getOccName, nameOccName, nameModule, isExternalName )
+import NameSet         ( NameSet, elemNameSet )
+import Module          ( Module )
 import CostCentre      ( CostCentre, pprCostCentreCore )
 import Literal         ( Literal )
 import ForeignCall     ( ForeignCall )
 import TysPrim         ( alphaTyVars )
-import BasicTypes      ( Arity, Activation(..), StrictnessMark, NewOrData(..),
+import BasicTypes      ( Arity, Activation(..), StrictnessMark, 
                          RecFlag(..), boolToRecFlag, Boxity(..), 
                          tupleParens )
 import Outputable
@@ -88,11 +92,9 @@ data IfaceDecl
              ifType   :: IfaceType, 
              ifIdInfo :: IfaceIdInfo }
 
-  | IfaceData { ifND      :: NewOrData,
-               ifCtxt     :: IfaceContext,     -- Context
-               ifName     :: OccName,          -- Type constructor
+  | IfaceData { ifName     :: OccName,         -- Type constructor
                ifTyVars   :: [IfaceTvBndr],    -- Type variables
-               ifCons     :: DataConDetails IfaceConDecl,
+               ifCons     :: IfaceConDecls,    -- Includes new/data info
                ifRec      :: RecFlag,          -- Recursive or not?
                ifVrcs     :: ArgVrcs,
                ifGeneric  :: Bool              -- True <=> generic converter functions available
@@ -106,16 +108,16 @@ data IfaceDecl
                ifSynRhs :: IfaceType           -- synonym expansion
     }
 
-  | IfaceClass { ifCtxt    :: IfaceContext,            -- Context...
-                ifName    :: OccName,                  -- Name of the class
-                ifTyVars  :: [IfaceTvBndr],            -- Type variables
-                ifFDs     :: [FunDep OccName],         -- Functional dependencies
-                ifSigs    :: [IfaceClassOp],           -- Method signatures
-                ifRec     :: RecFlag,                  -- Is newtype/datatype associated with the class recursive?
-                ifVrcs    :: ArgVrcs                   -- ... and what are its argument variances ...
+  | IfaceClass { ifCtxt    :: IfaceContext,    -- Context...
+                ifName    :: OccName,          -- Name of the class
+                ifTyVars  :: [IfaceTvBndr],    -- Type variables
+                ifFDs     :: [FunDep OccName], -- Functional dependencies
+                ifSigs    :: [IfaceClassOp],   -- Method signatures
+                ifRec     :: RecFlag,          -- Is newtype/datatype associated with the class recursive?
+                ifVrcs    :: ArgVrcs           -- ... and what are its argument variances ...
     }
 
-  | IfaceForeign { ifName :: OccName,                  -- Needs expanding when we move beyond .NET
+  | IfaceForeign { ifName :: OccName,          -- Needs expanding when we move beyond .NET
                   ifExtName :: Maybe FastString }
 
 data IfaceClassOp = IfaceClassOp OccName DefMeth IfaceType
@@ -123,13 +125,32 @@ data IfaceClassOp = IfaceClassOp OccName DefMeth IfaceType
        -- Just False => ordinary polymorphic default method
        -- Just True  => generic default method
 
+data IfaceConDecls
+  = IfAbstractTyCon            -- No info
+  | IfDataTyCon                -- data type decls
+       (Maybe IfaceContext)    -- See TyCon.AlgTyConRhs; H98 or GADT
+       [IfaceConDecl]
+  | IfNewTyCon  IfaceConDecl   -- newtype decls
+
+visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
+visibleIfConDecls IfAbstractTyCon    = []
+visibleIfConDecls (IfDataTyCon _ cs) = cs
+visibleIfConDecls (IfNewTyCon c)     = [c]
+
 data IfaceConDecl 
-  = IfaceConDecl OccName               -- Constructor name
-                [IfaceTvBndr]          -- Existental tyvars
-                IfaceContext           -- Existential context
-                [IfaceType]            -- Arg types
-                [StrictnessMark]       -- Empty (meaning all lazy), or 1-1 corresp with arg types
-                [OccName]              -- ...ditto... (field labels)
+  = IfVanillaCon {
+       ifConOcc     :: OccName,                -- Constructor name
+       ifConInfix   :: Bool,                   -- True <=> declared infix
+       ifConArgTys  :: [IfaceType],            -- Arg types
+       ifConStricts :: [StrictnessMark],       -- Empty (meaning all lazy), or 1-1 corresp with arg types
+       ifConFields  :: [OccName] }             -- ...ditto... (field labels)
+  | IfGadtCon {
+       ifConOcc     :: OccName,                -- Constructor name
+       ifConTyVars  :: [IfaceTvBndr],          -- All tyvars
+       ifConCtxt    :: IfaceContext,           -- Non-stupid context
+       ifConArgTys  :: [IfaceType],            -- Arg types
+       ifConResTys  :: [IfaceType],            -- Result type args
+       ifConStricts :: [StrictnessMark] }      -- Empty (meaning all lazy), or 1-1 corresp with arg types
                        
 data IfaceInst = IfaceInst { ifInstHead :: IfaceType,  -- Just the instance head type, quantified
                                                        -- so that it'll compare alpha-wise
@@ -156,32 +177,26 @@ data IfaceRule
 data IfaceIdInfo
   = NoInfo                     -- When writing interface file without -O
   | HasInfo [IfaceInfoItem]    -- Has info, and here it is
-  | DiscardedInfo              -- HasInfo in the .hi file, but discarded 
-                               -- when it was read in
--- Here's why we need this NoInfo/DiscardedInfo stuff
+
+-- Here's a tricky case:
 --   * Compile with -O module A, and B which imports A.f
 --   * Change function f in A, and recompile without -O
---   * If we read in A.hi and discard IdInfo, the 
---     new (empty) IdInfo for f looks like the 
---     old (discarded) IdInfo for f
---     => no new version # for f
---   * But that might mean that we fail to recompile B, when 
---     actually we should
---
---   * We also want to ensure that if A.hi was *already* compiled 
---     without -O we *don't* then recompile B
---
--- When we discard IdInfo on *reading* we make it into DiscardedInfo
--- On *writing* we make it NoInfo
--- DiscardedInfo is never written into a file
+--   * When we read in old A.hi we read in its IdInfo (as a thunk)
+--     (In earlier GHCs we used to drop IdInfo immediately on reading,
+--      but we do not do that now.  Instead it's discarded when the
+--      ModIface is read into the various decl pools.)
+--   * The version comparsion sees that new (=NoInfo) differs from old (=HasInfo *)
+--     and so gives a new version.
 
 data IfaceInfoItem
   = HsArity     Arity
   | HsStrictness StrictSig
   | HsUnfold    Activation IfaceExpr
   | HsNoCafRefs
-  | HsWorker    OccName Arity  -- Worker, if any see IdInfo.WorkerInfo
-                               -- for why we want arity here.
+  | HsWorker    IfaceExtName Arity     -- Worker, if any see IdInfo.WorkerInfo
+                                       -- for why we want arity here.
+       -- NB: we need IfaceExtName (not just OccName) because the worker
+       --     can simplify to a function in another module.
 -- NB: Specialisations and rules come in separately and are
 -- only later attached to the Id.  Partial reason: some are orphans.
 
@@ -193,7 +208,8 @@ data IfaceExpr
   | IfaceTuple         Boxity [IfaceExpr]              -- Saturated; type arguments omitted
   | IfaceLam   IfaceBndr IfaceExpr
   | IfaceApp   IfaceExpr IfaceExpr
-  | IfaceCase  IfaceExpr OccName [IfaceAlt]
+-- gaw 2004
+  | IfaceCase  IfaceExpr OccName IfaceType [IfaceAlt]
   | IfaceLet   IfaceBinding  IfaceExpr
   | IfaceNote  IfaceNote IfaceExpr
   | IfaceLit   Literal
@@ -241,44 +257,71 @@ pprIfaceDecl (IfaceForeign {ifName = tycon})
   = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
 
 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars, ifSynRhs = mono_ty, ifVrcs = vrcs})
-  = hang (ptext SLIT("type") <+> pp_decl_head [] tycon tyvars)
+  = hang (ptext SLIT("type") <+> pprIfaceDeclHead [] tycon tyvars)
        4 (vcat [equals <+> ppr mono_ty,
                pprVrcs vrcs])
 
-pprIfaceDecl (IfaceData {ifND = new_or_data, ifCtxt = context, ifName = tycon,
-                        ifTyVars = tyvars, ifCons = condecls, ifRec = isrec, ifVrcs = vrcs})
-  = hang (ppr new_or_data <+> pp_decl_head context tycon tyvars)
-       4 (vcat [pprVrcs vrcs, pprRec isrec, pp_condecls condecls])
+pprIfaceDecl (IfaceData {ifName = tycon, ifGeneric = gen,
+                        ifTyVars = tyvars, ifCons = condecls, 
+                        ifRec = isrec, ifVrcs = vrcs})
+  = hang (pp_nd <+> pprIfaceDeclHead context tycon tyvars)
+       4 (vcat [pprVrcs vrcs, pprRec isrec, pprGen gen, pp_condecls tycon condecls])
+  where
+    (context, pp_nd) 
+       = case condecls of
+               IfAbstractTyCon        -> ([], ptext SLIT("data"))
+               IfDataTyCon Nothing _  -> ([], ptext SLIT("data"))
+               IfDataTyCon (Just c) _ -> (c, ptext SLIT("data"))
+               IfNewTyCon _           -> ([], ptext SLIT("newtype"))
 
 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars, 
                          ifFDs = fds, ifSigs = sigs, ifVrcs = vrcs, ifRec = isrec})
-  = hang (ptext SLIT("class") <+> pp_decl_head context clas tyvars <+> pprFundeps fds)
+  = hang (ptext SLIT("class") <+> pprIfaceDeclHead context clas tyvars <+> pprFundeps fds)
        4 (vcat [pprVrcs vrcs, 
                pprRec isrec,
                sep (map ppr sigs)])
 
 pprVrcs vrcs = ptext SLIT("Variances") <+> ppr vrcs
 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
+pprGen True  = ptext SLIT("Generics: yes")
+pprGen False = ptext SLIT("Generics: no")
 
 instance Outputable IfaceClassOp where
    ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
 
-pp_decl_head :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
-pp_decl_head context thing tyvars 
+pprIfaceDeclHead :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
+pprIfaceDeclHead context thing tyvars 
   = hsep [pprIfaceContext context, ppr thing, pprIfaceTvBndrs tyvars]
 
-pp_condecls Unknown      = ptext SLIT("{- abstract -}")
-pp_condecls (DataCons cs) = equals <+> sep (punctuate (ptext SLIT(" |")) (map ppr cs))
-
-instance Outputable IfaceConDecl where
-  ppr (IfaceConDecl name ex_tvs ex_ctxt arg_tys strs fields)
-    = pprIfaceForAllPart ex_tvs ex_ctxt $
-      sep [ppr name <+> sep (map pprParendIfaceType arg_tys),
+pp_condecls tc IfAbstractTyCon    = ptext SLIT("{- abstract -}")
+pp_condecls tc (IfNewTyCon c)     = equals <+> pprIfaceConDecl tc c
+pp_condecls tc (IfDataTyCon _ cs) = equals <+> sep (punctuate (ptext SLIT(" |"))
+                                                    (map (pprIfaceConDecl tc) cs))
+
+pprIfaceConDecl tc (IfVanillaCon { 
+                     ifConOcc = name, ifConInfix = is_infix, 
+                     ifConArgTys = arg_tys, 
+                     ifConStricts = strs, ifConFields = fields })
+    = sep [ppr name <+> sep (map pprParendIfaceType arg_tys),
+          if is_infix then ptext SLIT("Infix") else empty,
           if null strs then empty 
              else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs)),
           if null fields then empty
              else nest 4 (ptext SLIT("Fields:") <+> hsep (map ppr fields))]
 
+pprIfaceConDecl tc (IfGadtCon { 
+                     ifConOcc = name, 
+                     ifConTyVars = tvs, ifConCtxt = ctxt,
+                     ifConArgTys = arg_tys, ifConResTys = res_tys, 
+                     ifConStricts = strs })
+    = sep [ppr name <+> dcolon <+> pprIfaceForAllPart tvs ctxt (ppr con_tau),
+          if null strs then empty 
+             else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs))]
+    where
+      con_tau = foldr1 IfaceFunTy (arg_tys ++ [tc_app])
+      tc_app  = IfaceTyConApp (IfaceTc (LocalTop tc)) res_tys  
+       -- Gruesome, but jsut for debug print
+
 instance Outputable IfaceRule where
   ppr (IfaceRule name act bndrs fn args rhs) 
     = sep [hsep [doubleQuotes (ftext name), ppr act,
@@ -323,13 +366,17 @@ pprIfaceExpr add_par e@(IfaceLam _ _)
     collect bs (IfaceLam b e) = collect (b:bs) e
     collect bs e              = (reverse bs, e)
 
-pprIfaceExpr add_par (IfaceCase scrut bndr [(con, bs, rhs)])
-  = add_par (sep [ptext SLIT("case") <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of") 
+-- gaw 2004 
+pprIfaceExpr add_par (IfaceCase scrut bndr ty [(con, bs, rhs)])
+-- gaw 2004
+  = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of") 
                        <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
                  pprIfaceExpr noParens rhs <+> char '}'])
 
-pprIfaceExpr add_par (IfaceCase scrut bndr alts)
-  = add_par (sep [ptext SLIT("case") <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of") 
+-- gaw 2004
+pprIfaceExpr add_par (IfaceCase scrut bndr ty alts)
+-- gaw 2004
+  = add_par (sep [ptext SLIT("case") <+> char '@' <+> pprParendIfaceType ty <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of") 
                        <+> ppr bndr <+> char '{',
                  nest 2 (sep (map ppr_alt alts)) <+> char '}'])
 
@@ -376,9 +423,8 @@ instance Outputable IfaceConAlt where
 
 ------------------
 instance Outputable IfaceIdInfo where
-   ppr NoInfo = empty
-   ppr DiscardedInfo = ptext SLIT("<discarded>")
-   ppr (HasInfo is)   = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
+   ppr NoInfo       = empty
+   ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
 
 ppr_hs_info (HsUnfold prag unf) = sep [ptext SLIT("Unfolding: ") <> ppr prag,
                                       parens (pprIfaceExpr noParens unf)]
@@ -397,16 +443,22 @@ ppr_hs_info (HsWorker w a)        = ptext SLIT("Worker:") <+> ppr w <+> int a
 
                 
 \begin{code}
-tyThingToIfaceDecl :: Bool -> (Name -> IfaceExtName) -> TyThing -> IfaceDecl
-tyThingToIfaceDecl discard_prags ext (AnId id)
+tyThingToIfaceDecl :: Bool 
+                  -> NameSet           -- Tycons and classes to export abstractly
+                  -> (Name -> IfaceExtName) -> TyThing -> IfaceDecl
+-- Assumption: the thing is already tidied, so that locally-bound names
+--            (lambdas, for-alls) already have non-clashing OccNames
+-- Reason: Iface stuff uses OccNames, and the conversion here does
+--        not do tidying on the way
+tyThingToIfaceDecl discard_id_info _ ext (AnId id)
   = IfaceId { ifName   = getOccName id, 
              ifType   = toIfaceType ext (idType id),
              ifIdInfo = info }
   where
-    info | discard_prags = NoInfo
-        | otherwise     = HasInfo (toIfaceIdInfo ext (idInfo id))
+    info | discard_id_info = NoInfo
+        | otherwise       = HasInfo (toIfaceIdInfo ext (idInfo id))
 
-tyThingToIfaceDecl _ ext (AClass clas)
+tyThingToIfaceDecl _ _ ext (AClass clas)
   = IfaceClass { ifCtxt          = toIfaceContext ext sc_theta,
                 ifName   = getOccName clas,
                 ifTyVars = toIfaceTvBndrs clas_tyvars,
@@ -432,7 +484,7 @@ tyThingToIfaceDecl _ ext (AClass clas)
 
     toIfaceFD (tvs1, tvs2) = (map getOccName tvs1, map getOccName tvs2)
 
-tyThingToIfaceDecl _ ext (ATyCon tycon)
+tyThingToIfaceDecl _ abstract_tcs ext (ATyCon tycon)
   | isSynTyCon tycon
   = IfaceSyn { ifName   = getOccName tycon,
                ifTyVars = toIfaceTvBndrs tyvars,
@@ -440,11 +492,9 @@ tyThingToIfaceDecl _ ext (ATyCon tycon)
                ifSynRhs = toIfaceType ext syn_ty }
 
   | isAlgTyCon tycon
-  = IfaceData {        ifND      = new_or_data,
-               ifCtxt    = toIfaceContext ext (tyConTheta tycon),
-               ifName    = getOccName tycon,
+  = IfaceData {        ifName    = getOccName tycon,
                ifTyVars  = toIfaceTvBndrs tyvars,
-               ifCons    = ifaceConDecls (tyConDataConDetails tycon),
+               ifCons    = ifaceConDecls (algTyConRhs tycon),
                ifRec     = boolToRecFlag (isRecursiveTyCon tycon),
                ifVrcs    = tyConArgVrcs tycon,
                ifGeneric = tyConHasGenerics tycon }
@@ -455,11 +505,9 @@ tyThingToIfaceDecl _ ext (ATyCon tycon)
 
   | isPrimTyCon tycon || isFunTyCon tycon
        -- Needed in GHCi for ':info Int#', for example
-  = IfaceData { ifND     = DataType,
-               ifCtxt   = [],
-               ifName   = getOccName tycon,
+  = IfaceData { ifName   = getOccName tycon,
                ifTyVars = toIfaceTvBndrs (take (tyConArity tycon) alphaTyVars),
-               ifCons   = Unknown,
+               ifCons   = IfAbstractTyCon,
                ifGeneric  = False,
                ifRec      = NonRecursive,
                ifVrcs     = tyConArgVrcs tycon }
@@ -468,45 +516,53 @@ tyThingToIfaceDecl _ ext (ATyCon tycon)
   where
     tyvars      = tyConTyVars tycon
     (_, syn_ty) = getSynTyConDefn tycon
-    new_or_data | isNewTyCon tycon = NewType
-               | otherwise        = DataType
-
-    ifaceConDecls Unknown       = Unknown
-    ifaceConDecls (DataCons cs) = DataCons (map ifaceConDecl cs)
+    abstract    = getName tycon `elemNameSet` abstract_tcs
+
+    ifaceConDecls _ | abstract       = IfAbstractTyCon
+    ifaceConDecls (NewTyCon con _ _) = IfNewTyCon (ifaceConDecl con)
+    ifaceConDecls (DataTyCon mb_theta cons _) = IfDataTyCon (ifaceDataCtxt mb_theta)
+                                                           (map ifaceConDecl cons)
+    ifaceConDecls AbstractTyCon             = IfAbstractTyCon
+       -- The last case should never happen when we are generating an
+       -- interface file (we're exporting this thing, so it's locally defined 
+       -- and should not be abstract).  But tyThingToIfaceDecl is also used
+       -- in TcRnDriver for GHCi, when browsing a module, in which case the
+       -- AbstractTyCon case is perfectly sensible.
+
+    ifaceDataCtxt Nothing      = Nothing
+    ifaceDataCtxt (Just theta) = Just (toIfaceContext ext theta)
 
     ifaceConDecl data_con 
-       = IfaceConDecl (getOccName (dataConName data_con))
-                      (toIfaceTvBndrs ex_tyvars)
-                      (toIfaceContext ext ex_theta)
-                      (map (toIfaceType ext) arg_tys)
-                      strict_marks
-                      (map getOccName field_labels)
+       | isVanillaDataCon data_con
+       = IfVanillaCon {ifConOcc = getOccName (dataConName data_con),
+                       ifConInfix = dataConIsInfix data_con,
+                       ifConArgTys = map (toIfaceType ext) arg_tys,
+                       ifConStricts = strict_marks,
+                       ifConFields = map getOccName field_labels }
+       | otherwise
+       = IfGadtCon   { ifConOcc = getOccName (dataConName data_con),
+                       ifConTyVars = toIfaceTvBndrs tyvars,
+                       ifConCtxt = toIfaceContext ext theta,
+                       ifConArgTys = map (toIfaceType ext) arg_tys,
+                       ifConResTys = map (toIfaceType ext) res_tys,
+                       ifConStricts = strict_marks }
        where
-         (_, _, ex_tyvars, ex_theta, arg_tys, _) = dataConSig data_con
+         (tyvars, theta, arg_tys, _, res_tys) = dataConSig data_con
           field_labels = dataConFieldLabels data_con
           strict_marks = dataConStrictMarks data_con
 
-       -- This case only happens in the call to ifaceThing in InteractiveUI
-       -- Otherwise DataCons are filtered out in ifaceThing_acc
-tyThingToIfaceDecl _ ext (ADataCon dc)
- = IfaceId { ifName   = getOccName dc, 
-            ifType   = toIfaceType ext full_ty,
-            ifIdInfo = NoInfo }
- where
-    (tvs, stupid_theta, ex_tvs, ex_theta, arg_tys, tycon) = dataConSig dc
-
-       -- The "stupid context" isn't part of the wrapper-Id type
-       -- (for better or worse -- see note in DataCon.lhs), so we
-       -- have to make it up here
-    full_ty = mkSigmaTy (tvs ++ ex_tvs) (stupid_theta ++ ex_theta) 
-                       (mkFunTys arg_tys (mkTyConApp tycon (mkTyVarTys tvs)))
+tyThingToIfaceDecl dis abstr ext (ADataCon dc)
+ = pprPanic "toIfaceDecl" (ppr dc)
+
 
 --------------------------
-dfunToIfaceInst :: ModuleName -> DFunId -> IfaceInst
-dfunToIfaceInst mod dfun_id
-  = IfaceInst { ifDFun     = getOccName dfun_id, 
+dfunToIfaceInst :: DFunId -> IfaceInst
+dfunToIfaceInst dfun_id
+  = IfaceInst { ifDFun     = nameOccName dfun_name, 
                ifInstHead = toIfaceType (mkLhsNameFn mod) tidy_ty }
   where
+    dfun_name = idName dfun_id
+    mod = nameModule dfun_name
     (tvs, _, cls, tys) = tcSplitDFunTy (idType dfun_id)
     head_ty = mkForAllTys tvs (mkPredTy (mkClassPred cls tys))
        -- No need to record the instance context; 
@@ -552,7 +608,7 @@ toIfaceIdInfo ext id_info
     has_worker  = case work_info of { HasWorker _ _ -> True; other -> False }
     wrkr_hsinfo = case work_info of
                    HasWorker work_id wrap_arity -> 
-                       Just (HsWorker (getOccName work_id) wrap_arity)
+                       Just (HsWorker (ext (idName work_id)) wrap_arity)
                    NoWorker -> Nothing
 
     ------------  Unfolding  --------------
@@ -565,15 +621,15 @@ toIfaceIdInfo ext id_info
                  | otherwise   = Just (HsUnfold inline_prag (toIfaceExpr ext rhs))
 
 --------------------------
-coreRuleToIfaceRule :: ModuleName -> (Name -> IfaceExtName) -> IdCoreRule -> IfaceRule
-coreRuleToIfaceRule mod ext (id, BuiltinRule _ _)
+coreRuleToIfaceRule :: Module -> (Name -> IfaceExtName) -> IdCoreRule -> IfaceRule
+coreRuleToIfaceRule mod ext (IdCoreRule id _ (BuiltinRule _ _))
   = pprTrace "toHsRule: builtin" (ppr id) (bogusIfaceRule (mkIfaceExtName (getName id)))
 
-coreRuleToIfaceRule mod ext (id, Rule name act bndrs args rhs)
-  = IfaceRule { ifRuleName = name, ifActivation = act, 
+coreRuleToIfaceRule mod ext (IdCoreRule id _ (Rule name act bndrs args rhs))
+  = IfaceRule { ifRuleName  = name, ifActivation = act, 
                ifRuleBndrs = map (toIfaceBndr ext) bndrs,
-               ifRuleHead = ext (getName id), 
-               ifRuleArgs = map (toIfaceExpr (mkLhsNameFn mod)) args,
+               ifRuleHead  = ext (idName id), 
+               ifRuleArgs  = map (toIfaceExpr (mkLhsNameFn mod)) args,
                        -- Use LHS name-fn for the args
                ifRuleRhs = toIfaceExpr ext rhs }
 
@@ -588,7 +644,8 @@ toIfaceExpr ext (Lit l)       = IfaceLit l
 toIfaceExpr ext (Type ty)     = IfaceType (toIfaceType ext ty)
 toIfaceExpr ext (Lam x b)     = IfaceLam (toIfaceBndr ext x) (toIfaceExpr ext b)
 toIfaceExpr ext (App f a)     = toIfaceApp ext f [a]
-toIfaceExpr ext (Case s x as) = IfaceCase (toIfaceExpr ext s) (getOccName x) (map (toIfaceAlt ext) as)
+-- gaw 2004
+toIfaceExpr ext (Case s x ty as) = IfaceCase (toIfaceExpr ext s) (getOccName x) (toIfaceType ext ty) (map (toIfaceAlt ext) as)
 toIfaceExpr ext (Let b e)     = IfaceLet (toIfaceBind ext b) (toIfaceExpr ext e)
 toIfaceExpr ext (Note n e)    = IfaceNote (toIfaceNote ext n) (toIfaceExpr ext e)
 
@@ -648,12 +705,12 @@ toIfaceVar ext v
 -- mkLhsNameFn ignores versioning info altogether
 -- Used for the LHS of instance decls and rules, where we 
 -- there's no point in recording version info
-mkLhsNameFn :: ModuleName -> Name -> IfaceExtName
+mkLhsNameFn :: Module -> Name -> IfaceExtName
 mkLhsNameFn this_mod name      
   | mod == this_mod = LocalTop occ
   | otherwise      = ExtPkg mod occ
   where
-    mod = nameModuleName name
+    mod = nameModule name
     occ        = nameOccName name
 \end{code}
 
@@ -715,14 +772,15 @@ eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
 
 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
   = bool (ifName d1    == ifName d2 && 
-         ifND d1      == ifND   d2 && 
          ifRec d1     == ifRec   d2 && 
          ifVrcs d1    == ifVrcs   d2 && 
          ifGeneric d1 == ifGeneric d2) &&&
     eqWith (ifTyVars d1) (ifTyVars d2) (\ env -> 
-         eq_ifContext env (ifCtxt d1) (ifCtxt d2)  &&&
-         eq_hsCD      env (ifCons d1) (ifCons d2) 
+           eq_hsCD env (ifCons d1) (ifCons d2) 
        )
+       -- The type variables of the data type do not scope
+       -- over the constructors (any more), but they do scope
+       -- over the stupid context in the IfaceConDecls
 
 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
   = bool (ifName d1 == ifName d2) &&&
@@ -761,16 +819,30 @@ eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1)
          eq_ifaceExpr env rhs1 rhs2)
 eqIfRule _ _ = NotEqual
 
-eq_hsCD env (DataCons c1) (DataCons c2) = eqListBy (eq_ConDecl env) c1 c2
-eq_hsCD env Unknown      Unknown       = Equal
-eq_hsCD env d1           d2            = NotEqual
+eq_hsCD env (IfDataTyCon st1 c1) (IfDataTyCon st2 c2) 
+  = eqMaybeBy (eq_ifContext env) st1 st2 &&& 
+    eqListBy (eq_ConDecl env) c1 c2
+
+eq_hsCD env (IfNewTyCon c1)  (IfNewTyCon c2)  = eq_ConDecl env c1 c2
+eq_hsCD env IfAbstractTyCon  IfAbstractTyCon  = Equal
+eq_hsCD env d1              d2               = NotEqual
+
+eq_ConDecl env c1@(IfVanillaCon {}) c2@(IfVanillaCon {})
+  = bool (ifConOcc c1     == ifConOcc c2 && 
+         ifConInfix c1   == ifConInfix c2 && 
+         ifConStricts c1 == ifConStricts c2 && 
+         ifConFields c1  == ifConFields c2) &&&
+   eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2)
+
+eq_ConDecl env c1@(IfGadtCon {}) c2@(IfGadtCon {})
+  = bool (ifConOcc c1     == ifConOcc c2 && 
+         ifConStricts c1 == ifConStricts c2) &&& 
+    eq_ifTvBndrs env (ifConTyVars c1) (ifConTyVars c2) (\ env ->
+       eq_ifContext env (ifConCtxt c1) (ifConCtxt c2) &&&
+       eq_ifTypes env (ifConResTys c1) (ifConResTys c2) &&&
+       eq_ifTypes env (ifConArgTys c1) (ifConArgTys c2))
 
-eq_ConDecl env (IfaceConDecl n1 tvs1 cxt1 args1 ss1 lbls1)
-              (IfaceConDecl n2 tvs2 cxt2 args2 ss2 lbls2)      
-  = bool (n1 == n2 && ss1 == ss2 && lbls1 == lbls2) &&&
-    eq_ifTvBndrs env tvs1 tvs2 (\ env ->
-       eq_ifContext env cxt1 cxt2 &&&
-       eq_ifTypes env args1 args2)
+eq_ConDecl env c1 c2 = NotEqual
 
 eq_hsFD env (ns1,ms1) (ns2,ms2)
   = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
@@ -783,7 +855,6 @@ eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
 \begin{code}
 -----------------
 eqIfIdInfo NoInfo       NoInfo        = Equal
-eqIfIdInfo DiscardedInfo DiscardedInfo = Equal -- Should not happen?
 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
 eqIfIdInfo i1 i2 = NotEqual
 
@@ -791,7 +862,7 @@ eq_item (HsArity a1)           (HsArity a2)       = bool (a1 == a2)
 eq_item (HsStrictness s1)  (HsStrictness s2)  = bool (s1 == s2)
 eq_item (HsUnfold a1 u1)   (HsUnfold a2 u2)   = bool (a1 == a2) &&& eq_ifaceExpr emptyEqEnv u1 u2
 eq_item HsNoCafRefs        HsNoCafRefs       = Equal
-eq_item (HsWorker occ1 a1) (HsWorker occ2 a2) = bool (a1==a2 && occ1==occ2)
+eq_item (HsWorker wkr1 a1) (HsWorker wkr2 a2) = bool (a1==a2) &&& (wkr1 `eqIfExt` wkr2)
 eq_item _ _ = NotEqual
 
 -----------------
@@ -806,8 +877,9 @@ eq_ifaceExpr env (IfaceLam b1 body1)  (IfaceLam b2 body2)  = eq_ifBndr env b1 b2
 eq_ifaceExpr env (IfaceApp f1 a1)     (IfaceApp f2 a2)    = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
 eq_ifaceExpr env (IfaceNote n1 r1)    (IfaceNote n2 r2)    = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
 
-eq_ifaceExpr env (IfaceCase s1 b1 as1) (IfaceCase s2 b2 as2)
+eq_ifaceExpr env (IfaceCase s1 b1 ty1 as1) (IfaceCase s2 b2 ty2 as2)
   = eq_ifaceExpr env s1 s2 &&&
+    eq_ifType env ty1 ty2 &&&
     eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
   where
     eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)