[project @ 2000-10-17 13:22:10 by simonmar]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcTyClsDecls.lhs
index 8d803fd..ae7e4d2 100644 (file)
@@ -12,43 +12,43 @@ module TcTyClsDecls (
 
 import HsSyn           ( HsDecl(..), TyClDecl(..),
                          HsType(..), HsTyVarBndr,
-                         ConDecl(..), ConDetails(..), BangType(..),
+                         ConDecl(..), ConDetails(..), 
                          Sig(..), HsPred(..), HsTupCon(..),
                          tyClDeclName, hsTyVarNames, isClassDecl, isSynDecl, isClassOpSig, getBangType
                        )
 import RnHsSyn         ( RenamedHsDecl, RenamedTyClDecl, listTyCon_name )
-import BasicTypes      ( RecFlag(..), NewOrData(..), Arity )
+import BasicTypes      ( RecFlag(..), NewOrData(..) )
 
 import TcMonad
-import TcEnv           ( ValueEnv, TyThing(..), TyThingDetails(..), tyThingKind,
-                         tcExtendTypeEnv, tcExtendKindEnv, tcLookupTy
+import TcEnv           ( TcEnv, TyThing(..), TyThingDetails(..), tyThingKind,
+                         tcExtendTypeEnv, tcExtendKindEnv, tcLookupGlobal
                        )
 import TcTyDecls       ( tcTyDecl1, kcConDetails, mkNewTyConRep )
 import TcClassDcl      ( tcClassDecl1 )
 import TcMonoType      ( kcHsTyVars, kcHsType, kcHsBoxedSigType, kcHsContext, mkTyClTyVars )
-import TcType          ( TcKind, newKindVar, newKindVars, zonkKindEnv )
+import TcType          ( TcKind, newKindVar, zonkKindEnv )
 
 import TcUnify         ( unifyKind )
+import TcInstDcls      ( tcAddDeclCtxt )
 import Type            ( Kind, mkArrowKind, boxedTypeKind, zipFunTys )
 import Variance         ( calcTyConArgVrcs )
 import Class           ( Class, mkClass, classTyCon )
-import TyCon           ( TyCon, ArgVrcs, AlgTyConFlavour(..), mkSynTyCon, mkAlgTyCon, mkClassTyCon )
+import TyCon           ( TyCon, ArgVrcs, AlgTyConFlavour(..), mkSynTyCon, mkAlgTyConRep, mkClassTyCon )
 import DataCon         ( isNullaryDataCon )
-import Var             ( TyVar, tyVarKind, varName )
-import VarEnv
+import Var             ( varName )
 import FiniteMap
-import Bag     
 import Digraph         ( stronglyConnComp, SCC(..) )
 import Name            ( Name, NamedThing(..), NameEnv, getSrcLoc, isTvOcc, nameOccName,
                          mkNameEnv, lookupNameEnv_NF
                        )
 import Outputable
 import Maybes          ( mapMaybe, catMaybes )
-import UniqSet         ( UniqSet, emptyUniqSet,
-                         unitUniqSet, unionUniqSets, 
+import UniqSet         ( emptyUniqSet, unitUniqSet, unionUniqSets, 
                          unionManyUniqSets, uniqSetToList ) 
 import ErrUtils                ( Message )
 import Unique          ( Unique, Uniquable(..) )
+import HsDecls          ( fromClassDeclNameList )
+import Generics         ( mkTyConGenInfo )
 \end{code}
 
 
@@ -61,9 +61,9 @@ import Unique         ( Unique, Uniquable(..) )
 The main function
 ~~~~~~~~~~~~~~~~~
 \begin{code}
-tcTyAndClassDecls :: ValueEnv          -- Knot tying stuff
+tcTyAndClassDecls :: TcEnv             -- Knot tying stuff
                  -> [RenamedHsDecl]
-                 -> TcM s TcEnv
+                 -> TcM TcEnv
 
 tcTyAndClassDecls unf_env decls
   = sortByDependency decls             `thenTc` \ groups ->
@@ -81,7 +81,6 @@ tcGroups unf_env (group:groups)
 
 Dealing with a group
 ~~~~~~~~~~~~~~~~~~~~
-
 Consider a mutually-recursive group, binding 
 a type constructor T and a class C.
 
@@ -112,7 +111,7 @@ The knot-tying parameters: @rec_details_list@ is an alist mapping @Name@s to
 @TyThing@s.  @rec_vrcs@ is a finite map from @Name@s to @ArgVrcs@s.
 
 \begin{code}
-tcGroup :: ValueEnv -> SCC RenamedTyClDecl -> TcM s TcEnv
+tcGroup :: TcEnv -> SCC RenamedTyClDecl -> TcM TcEnv
 tcGroup unf_env scc
   =    -- Step 1
     mapNF_Tc getInitialKind decls                              `thenNF_Tc` \ initial_kinds ->
@@ -131,7 +130,7 @@ tcGroup unf_env scc
            rec_details = mkNameEnv rec_details_list
 
            tyclss, all_tyclss :: [(Name, TyThing)]
-           tyclss      = map (buildTyConOrClass is_rec kind_env rec_vrcs rec_details) decls
+           tyclss = map (buildTyConOrClass is_rec kind_env rec_vrcs rec_details) decls
 
                -- Add the tycons that come from the classes
                -- We want them in the environment because 
@@ -144,11 +143,11 @@ tcGroup unf_env scc
             rec_vrcs    = calcTyConArgVrcs [tc | (_, ATyCon tc) <- all_tyclss]
        in
                -- Step 5
-       tcExtendTypeEnv all_tyclss              $
+       tcExtendGlobalEnv all_tyclss            $
        mapTc (tcTyClDecl1 unf_env) decls       `thenTc` \ tycls_details ->
        tcGetEnv                                `thenNF_Tc` \ env -> 
        returnTc (tycls_details, env)
-    )                                                          `thenTc` \ (_, env) ->
+    )                                          `thenTc` \ (_, env) ->
     returnTc env
   where
     is_rec = case scc of
@@ -159,11 +158,12 @@ tcGroup unf_env scc
                AcyclicSCC decl -> [decl]
                CyclicSCC decls -> decls
 
-tcTyClDecl1  :: ValueEnv -> RenamedTyClDecl -> TcM s (Name, TyThingDetails)
-
 tcTyClDecl1 unf_env decl
-  | isClassDecl decl = tcClassDecl1 unf_env decl
-  | otherwise       = tcTyDecl1 decl
+  = tcAddDeclCtxt decl                 $
+    if isClassDecl decl then
+       tcClassDecl1 unf_env decl
+    else
+       tcTyDecl1 decl
 \end{code}
 
 
@@ -174,17 +174,17 @@ tcTyClDecl1 unf_env decl
 %************************************************************************
 
 \begin{code}
-getInitialKind :: RenamedTyClDecl -> NF_TcM s (Name, TcKind)
+getInitialKind :: RenamedTyClDecl -> NF_TcM (Name, TcKind)
 getInitialKind (TySynonym name tyvars _ _)
  = kcHsTyVars tyvars   `thenNF_Tc` \ arg_kinds ->
    newKindVar          `thenNF_Tc` \ result_kind  ->
    returnNF_Tc (name, mk_kind arg_kinds result_kind)
 
-getInitialKind (TyData _ _ name tyvars _ _ _ _ _)
+getInitialKind (TyData _ _ name tyvars _ _ _ _ _ _ _)
  = kcHsTyVars tyvars   `thenNF_Tc` \ arg_kinds ->
    returnNF_Tc (name, mk_kind arg_kinds boxedTypeKind)
 
-getInitialKind (ClassDecl _ name tyvars _ _ _ _ _ _ _ _ _)
+getInitialKind (ClassDecl _ name tyvars _ _ _ _ _ _ )
  = kcHsTyVars tyvars   `thenNF_Tc` \ arg_kinds ->
    returnNF_Tc (name, mk_kind arg_kinds boxedTypeKind)
 
@@ -212,7 +212,7 @@ depends on *all the uses of class D*.  For example, the use of
 Monad c in bop's type signature means that D must have kind Type->Type.
 
 \begin{code}
-kcTyClDecl :: RenamedTyClDecl -> TcM s ()
+kcTyClDecl :: RenamedTyClDecl -> TcM ()
 
 kcTyClDecl decl@(TySynonym tycon_name hs_tyvars rhs loc)
   = tcAddDeclCtxt decl                 $
@@ -220,7 +220,7 @@ kcTyClDecl decl@(TySynonym tycon_name hs_tyvars rhs loc)
     kcHsType rhs                       `thenTc` \ rhs_kind ->
     unifyKind result_kind rhs_kind
 
-kcTyClDecl decl@(TyData _ context tycon_name hs_tyvars con_decls _ _ _ loc)
+kcTyClDecl decl@(TyData _ context tycon_name hs_tyvars con_decls _ _ _ loc _ _)
   = tcAddDeclCtxt decl                 $
     kcTyClDeclBody tycon_name hs_tyvars        $ \ result_kind ->
     kcHsContext context                        `thenTc_` 
@@ -234,7 +234,7 @@ kcTyClDecl decl@(TyData _ context tycon_name hs_tyvars con_decls _ _ _ loc)
 
 kcTyClDecl decl@(ClassDecl context class_name
                           hs_tyvars fundeps class_sigs
-                          _ _ _ _ _ _ loc)
+                          _ _ _ loc)
   = tcAddDeclCtxt decl                 $
     kcTyClDeclBody class_name hs_tyvars        $ \ result_kind ->
     kcHsContext context                        `thenTc_`
@@ -243,15 +243,20 @@ kcTyClDecl decl@(ClassDecl context class_name
     kc_sig (ClassOpSig _ _ op_ty loc) = tcAddSrcLoc loc (kcHsBoxedSigType op_ty)
 
 kcTyClDeclBody :: Name -> [HsTyVarBndr Name]   -- Kind of the tycon/cls and its tyvars
-              -> (Kind -> TcM s a)             -- Thing inside
-              -> TcM s a
+              -> (Kind -> TcM a)               -- Thing inside
+              -> TcM a
 -- Extend the env with bindings for the tyvars, taken from
 -- the kind of the tycon/class.  Give it to the thing inside, and 
 -- check the result kind matches
 kcTyClDeclBody tc_name hs_tyvars thing_inside
-  = tcLookupTy tc_name         `thenNF_Tc` \ tc ->
+  = tcLookupGlobal tc_name             `thenNF_Tc` \ thing ->
     let
-       (tyvars_w_kinds, result_kind) = zipFunTys (hsTyVarNames hs_tyvars) (tyThingKind tc)
+       kind = case thing of
+                 ATyCon tc -> tyConKind tc
+                 AClass cl -> tyConKind (classTyCon cl)
+               -- For some odd reason, a class doesn't include its kind
+
+       (tyvars_w_kinds, result_kind) = zipFunTys (hsTyVarNames hs_tyvars) kind
     in
     tcExtendKindEnv tyvars_w_kinds (thing_inside result_kind)
 \end{code}
@@ -283,13 +288,14 @@ buildTyConOrClass is_rec kenv rec_vrcs rec_details
         argvrcs                    = lookupWithDefaultFM rec_vrcs bogusVrcs tycon
 
 buildTyConOrClass is_rec kenv rec_vrcs  rec_details
-                 (TyData data_or_new context tycon_name tyvar_names _ nconstrs _ _ src_loc)
+                 (TyData data_or_new context tycon_name tyvar_names _ nconstrs _ _ src_loc name1 name2)
   = (tycon_name, ATyCon tycon)
   where
-       tycon = mkAlgTyCon tycon_name tycon_kind tyvars ctxt argvrcs
+       tycon = mkAlgTyConRep tycon_name tycon_kind tyvars ctxt argvrcs
                           data_cons nconstrs
                           derived_classes
-                          flavour is_rec
+                          flavour is_rec gen_info
+       gen_info = mkTyConGenInfo tycon name1 name2
 
        DataTyDetails ctxt data_cons derived_classes = lookupNameEnv_NF rec_details tycon_name
 
@@ -304,10 +310,11 @@ buildTyConOrClass is_rec kenv rec_vrcs  rec_details
 
 buildTyConOrClass is_rec kenv rec_vrcs  rec_details
                   (ClassDecl context class_name
-                            tyvar_names fundeps class_sigs def_methods pragmas 
-                            tycon_name datacon_name datacon_wkr_name sc_sel_names src_loc)
+                            tyvar_names fundeps class_sigs def_methods pragmas
+                            name_list src_loc)
   = (class_name, AClass clas)
   where
+        (tycon_name, _, _, _) = fromClassDeclNameList name_list
        clas = mkClass class_name tyvars fds
                       sc_theta sc_sel_ids op_items
                       tycon
@@ -348,7 +355,7 @@ bogusVrcs = panic "Bogus tycon arg variances"
 Dependency analysis
 ~~~~~~~~~~~~~~~~~~~
 \begin{code}
-sortByDependency :: [RenamedHsDecl] -> TcM s [SCC RenamedTyClDecl]
+sortByDependency :: [RenamedHsDecl] -> TcM [SCC RenamedTyClDecl]
 sortByDependency decls
   = let                -- CHECK FOR CLASS CYCLES
        cls_sccs   = stronglyConnComp (mapMaybe mk_cls_edges tycl_decls)
@@ -386,7 +393,7 @@ Edges in Type/Class decls
 
 mk_cls_edges :: RenamedTyClDecl -> Maybe (RenamedTyClDecl, Unique, [Unique])
 
-mk_cls_edges decl@(ClassDecl ctxt name _ _ _ _ _ _ _ _ _ _)
+mk_cls_edges decl@(ClassDecl ctxt name _ _ _ _ _ _ _)
   = Just (decl, getUnique name, map getUnique (catMaybes (map get_clas ctxt)))
 mk_cls_edges other_decl
   = Nothing
@@ -394,7 +401,7 @@ mk_cls_edges other_decl
 ----------------------------------------------------
 mk_edges :: RenamedTyClDecl -> (RenamedTyClDecl, Unique, [Unique])
 
-mk_edges decl@(TyData _ ctxt name _ condecls _ derivs _ _)
+mk_edges decl@(TyData _ ctxt name _ condecls _ derivs _ _ _ _)
   = (decl, getUnique name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
                                         get_cons condecls `unionUniqSets`
                                         get_deriv derivs))
@@ -402,7 +409,7 @@ mk_edges decl@(TyData _ ctxt name _ condecls _ derivs _ _)
 mk_edges decl@(TySynonym name _ rhs _)
   = (decl, getUnique name, uniqSetToList (get_ty rhs))
 
-mk_edges decl@(ClassDecl ctxt name _ _ sigs _ _ _ _ _ _ _)
+mk_edges decl@(ClassDecl ctxt name _ _ sigs _ _ _ _)
   = (decl, getUnique name, uniqSetToList (get_ctxt ctxt `unionUniqSets`
                                         get_sigs sigs))
 
@@ -426,7 +433,6 @@ get_con (ConDecl _ _ _ ctxt details _)
 ----------------------------------------------------
 get_con_details (VanillaCon btys)    = unionManyUniqSets (map get_bty btys)
 get_con_details (InfixCon bty1 bty2) = unionUniqSets (get_bty bty1) (get_bty bty2)
-get_con_details (NewCon ty _)        = get_ty ty
 get_con_details (RecCon nbtys)       = unionManyUniqSets (map (get_bty.snd) nbtys)
 
 ----------------------------------------------------
@@ -458,7 +464,6 @@ get_sigs sigs
 
 ----------------------------------------------------
 set_name name = unitUniqSet (getUnique name)
-set_to_bag set = listToBag (uniqSetToList set)
 \end{code}
 
 
@@ -469,23 +474,6 @@ set_to_bag set = listToBag (uniqSetToList set)
 %************************************************************************
 
 \begin{code}
-tcAddDeclCtxt decl thing_inside
-  = tcAddSrcLoc loc    $
-    tcAddErrCtxt ctxt  $
-    thing_inside
-  where
-     (name, loc, thing)
-       = case decl of
-           (ClassDecl _ name _ _ _ _ _ _ _ _ _ loc) -> (name, loc, "class")
-           (TySynonym name _ _ loc)                 -> (name, loc, "type synonym")
-           (TyData NewType  _ name _ _ _ _ _ loc)   -> (name, loc, "data type")
-           (TyData DataType _ name _ _ _ _ _ loc)   -> (name, loc, "newtype")
-
-     ctxt = hsep [ptext SLIT("In the"), text thing, 
-                 ptext SLIT("declaration for"), quotes (ppr name)]
-\end{code}
-
-\begin{code}
 typeCycleErr, classCycleErr :: [[RenamedTyClDecl]] -> Message
 
 typeCycleErr syn_cycles
@@ -502,4 +490,5 @@ pp_cycle str decls
       = hsep [quotes (ppr name), ptext SLIT("at"), ppr (getSrcLoc name)]
      where
         name = tyClDeclName decl
+
 \end{code}