[project @ 2000-10-31 09:58:13 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcClassDcl.lhs
index 1b1a7b0..d7da12c 100644 (file)
@@ -11,10 +11,10 @@ module TcClassDcl ( tcClassDecl1, tcClassDecls2, mkImplicitClassBinds,
 #include "HsVersions.h"
 
 import HsSyn           ( HsDecl(..), TyClDecl(..), Sig(..), MonoBinds(..),
-                         HsExpr(..), HsLit(..), HsType(..), HsPred(..),
+                         HsExpr(..), HsLit(..), HsType(..), HsPred(..), 
                          mkSimpleMatch, andMonoBinds, andMonoBindList, 
                          isClassDecl, isClassOpSig, isPragSig,
-                         fromClassDeclNameList, tyClDeclName
+                         getClassDeclSysNames, tyClDeclName
                        )
 import BasicTypes      ( TopLevelFlag(..), RecFlag(..) )
 import RnHsSyn         ( RenamedTyClDecl, 
@@ -26,28 +26,29 @@ import TcHsSyn              ( TcMonoBinds, idsToMonoBinds )
 
 import Inst            ( InstOrigin(..), LIE, emptyLIE, plusLIE, plusLIEs, 
                          newDicts, newMethod )
-import TcEnv           ( TcId, TcEnv, TyThingDetails(..), tcAddImportedIdInfo,
+import TcEnv           ( TcId, TcEnv, RecTcEnv, TyThingDetails(..), tcAddImportedIdInfo,
                          tcLookupClass, tcExtendTyVarEnvForMeths, tcExtendGlobalTyVars,
                          tcExtendLocalValEnv, tcExtendTyVarEnv, newDefaultMethodName
                        )
 import TcBinds         ( tcBindWithSigs, tcSpecSigs )
-import TcMonoType      ( tcHsSigType, tcClassContext, checkSigTyVars, sigCtxt, mkTcSig )
+import TcMonoType      ( tcHsSigType, tcClassContext, checkSigTyVars, checkAmbiguity, sigCtxt, mkTcSig )
 import TcSimplify      ( tcSimplifyAndCheck, bindInstsOfLocalFuns )
 import TcType          ( TcType, TcTyVar, tcInstTyVars, zonkTcSigTyVars )
 import TcMonad
 import Generics                ( mkGenericRhs, validGenericMethodType )
 import PrelInfo                ( nO_METHOD_BINDING_ERROR_ID )
-import Class           ( classTyVars, classBigSig, classSelIds, classTyCon, 
-                         Class, ClassOpItem, DefMeth (..) )
+import Class           ( classTyVars, classBigSig, classSelIds, classTyCon, classTvsFds,
+                         Class, ClassOpItem, DefMeth (..), FunDep )
 import MkId            ( mkDictSelId, mkDataConId, mkDataConWrapId, mkDefaultMethodId )
 import DataCon         ( mkDataCon, notMarkedStrict )
 import Id              ( Id, idType, idName )
-import Name            ( Name, isLocallyDefined, NamedThing(..),
+import Module          ( Module )
+import Name            ( Name, NamedThing(..), isFrom,
                          NameEnv, lookupNameEnv, emptyNameEnv, unitNameEnv, 
                          plusNameEnv, nameEnvElts )
 import NameSet         ( emptyNameSet )
 import Outputable
-import Type            ( Type, ClassContext, mkTyVarTys, mkDictTys, mkSigmaTy, mkClassPred,
+import Type            ( Type, ClassContext, mkTyVarTys, mkDictTys, mkClassPred,
                          splitTyConApp_maybe, isTyVarTy
                        )
 import Var             ( TyVar )
@@ -100,10 +101,10 @@ Death to "ExpandingDicts".
 %************************************************************************
 
 \begin{code}
-tcClassDecl1 :: TcEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
+tcClassDecl1 :: RecTcEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
 tcClassDecl1 rec_env
             (ClassDecl context class_name
-                       tyvar_names fundeps class_sigs def_methods pragmas 
+                       tyvar_names fundeps class_sigs def_methods
                        sys_names src_loc)
   =    -- CHECK ARITY 1 FOR HASKELL 1.4
     doptsTc Opt_GlasgowExts                            `thenTc` \ glaExts ->
@@ -113,10 +114,10 @@ tcClassDecl1 rec_env
        -- LOOK THINGS UP IN THE ENVIRONMENT
     tcLookupClass class_name                           `thenTc` \ clas ->
     let
-       tyvars   = classTyVars clas
+       (tyvars, fds) = classTvsFds clas
        op_sigs  = filter isClassOpSig class_sigs
        op_names = [n | ClassOpSig n _ _ _ <- op_sigs]
-       (_, datacon_name, datacon_wkr_name, sc_sel_names) = fromClassDeclNameList sys_names
+       (_, datacon_name, datacon_wkr_name, sc_sel_names) = getClassDeclSysNames sys_names
     in
     tcExtendTyVarEnv tyvars                            $ 
 
@@ -128,7 +129,8 @@ tcClassDecl1 rec_env
     tcSuperClasses clas context sc_sel_names   `thenTc` \ (sc_theta, sc_sel_ids) ->
 
        -- CHECK THE CLASS SIGNATURES,
-    mapTc (tcClassSig rec_env clas tyvars dm_info) op_sigs     `thenTc` \ sig_stuff ->
+    mapTc (tcClassSig rec_env clas tyvars fds dm_info) 
+         op_sigs                               `thenTc` \ sig_stuff ->
 
        -- MAKE THE CLASS DETAILS
     let
@@ -235,9 +237,10 @@ tcSuperClasses clas context sc_sel_names
     is_tyvar other      = False
 
 
-tcClassSig :: TcEnv                    -- Knot tying only!
+tcClassSig :: RecTcEnv
           -> Class                     -- ...ditto...
           -> [TyVar]                   -- The class type variable, used for error check only
+          -> [FunDep TyVar]
           -> NameEnv (DefMeth Name)    -- Info about default methods
           -> RenamedClassOpSig
           -> TcM (Type,                -- Type of the method
@@ -248,21 +251,21 @@ tcClassSig :: TcEnv                       -- Knot tying only!
 -- so we distinguish them in checkDefaultBinds, and pass this knowledge in the
 -- Class.DefMeth data structure. 
 
-tcClassSig rec_env clas clas_tyvars dm_info
+tcClassSig unf_env clas clas_tyvars fds dm_info
           (ClassOpSig op_name maybe_dm op_ty src_loc)
   = tcAddSrcLoc src_loc $
 
        -- Check the type signature.  NB that the envt *already has*
        -- bindings for the type variables; see comments in TcTyAndClassDcls.
 
-    -- NB: Renamer checks that the class type variable is mentioned in local_ty,
-    -- and that it is not constrained by theta
     tcHsSigType op_ty                          `thenTc` \ local_ty ->
     let
-       global_ty   = mkSigmaTy clas_tyvars 
-                               [mkClassPred clas (mkTyVarTys clas_tyvars)]
-                               local_ty
+       theta = [mkClassPred clas (mkTyVarTys clas_tyvars)]
+    in
+       -- Check for ambiguous class op types
+    checkAmbiguity True clas_tyvars theta local_ty      `thenTc` \ global_ty ->
 
+    let
        -- Build the selector id and default method id
        sel_id      = mkDictSelId op_name clas
 
@@ -271,7 +274,7 @@ tcClassSig rec_env clas clas_tyvars dm_info
        dm_info_id = case dm_info_name of 
                        NoDefMeth       -> NoDefMeth
                        GenDefMeth      -> GenDefMeth
-                       DefMeth dm_name -> DefMeth (tcAddImportedIdInfo rec_env dm_id)
+                       DefMeth dm_name -> DefMeth (tcAddImportedIdInfo unf_env dm_id)
                                        where
                                           dm_id = mkDefaultMethodId dm_name clas global_ty
     in
@@ -294,8 +297,8 @@ tcClassSig rec_env clas clas_tyvars dm_info
 and superclass dictionary.
 
 \begin{code}
-mkImplicitClassBinds :: [Class] -> NF_TcM ([Id], TcMonoBinds)
-mkImplicitClassBinds classes
+mkImplicitClassBinds :: Module -> [Class] -> NF_TcM ([Id], TcMonoBinds)
+mkImplicitClassBinds this_mod classes
   = returnNF_Tc (concat cls_ids_s, andMonoBindList binds_s)
        -- The selector binds are already in the selector Id's unfoldings
        -- We don't return the data constructor etc from the class,
@@ -306,8 +309,8 @@ mkImplicitClassBinds classes
     mk_implicit clas = (sel_ids, binds)
                     where
                        sel_ids = classSelIds clas
-                       binds | isLocallyDefined clas = idsToMonoBinds sel_ids
-                             | otherwise             = EmptyMonoBinds
+                       binds | isFrom this_mod clas = idsToMonoBinds sel_ids
+                             | otherwise            = EmptyMonoBinds
 \end{code}
 
 
@@ -377,14 +380,14 @@ The function @tcClassDecls2@ just arranges to apply @tcClassDecl2@ to
 each local class decl.
 
 \begin{code}
-tcClassDecls2 :: [RenamedHsDecl] -> NF_TcM (LIE, TcMonoBinds)
+tcClassDecls2 :: Module -> [RenamedHsDecl] -> NF_TcM (LIE, TcMonoBinds)
 
-tcClassDecls2 decls
+tcClassDecls2 this_mod decls
   = foldr combine
          (returnNF_Tc (emptyLIE, EmptyMonoBinds))
          [tcClassDecl2 cls_decl | TyClD cls_decl <- decls, 
                                   isClassDecl cls_decl,
-                                  isLocallyDefined (tyClDeclName cls_decl)]
+                                  isFrom this_mod (tyClDeclName cls_decl)]
   where
     combine tc1 tc2 = tc1 `thenNF_Tc` \ (lie1, binds1) ->
                      tc2 `thenNF_Tc` \ (lie2, binds2) ->
@@ -400,7 +403,7 @@ tcClassDecl2 :: RenamedTyClDecl             -- The class declaration
             -> NF_TcM (LIE, TcMonoBinds)
 
 tcClassDecl2 (ClassDecl context class_name
-                       tyvar_names _ sigs default_binds pragmas _ src_loc)
+                       tyvar_names _ sigs default_binds _ src_loc)
   =    -- A locally defined class
     recoverNF_Tc (returnNF_Tc (emptyLIE, EmptyMonoBinds)) $ 
     tcAddSrcLoc src_loc                                          $