[project @ 2001-10-31 15:22:53 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcInstDcls.lhs
index f0c5950..ad07abc 100644 (file)
@@ -23,10 +23,11 @@ import TcHsSyn              ( TcMonoBinds, mkHsConApp )
 import TcBinds         ( tcSpecSigs )
 import TcClassDcl      ( tcMethodBind, badMethodErr )
 import TcMonad       
-import TcMType         ( tcInstTyVars, checkValidTheta, checkValidInstHead, instTypeErr,
+import TcMType         ( tcInstSigTyVars, checkValidTheta, checkValidInstHead, instTypeErr, 
                          UserTypeCtxt(..), SourceTyCtxt(..) )
-import TcType          ( tcSplitDFunTy, mkClassPred, mkTyVarTy,
-                         tcSplitSigmaTy, tcSplitPredTy_maybe, getClassPredTys
+import TcType          ( tcSplitDFunTy, mkClassPred, mkTyVarTy, mkTyVarTys,
+                         tcSplitSigmaTy, tcSplitPredTy_maybe, getClassPredTys,
+                         TyVarDetails(..)
                        )
 import Inst            ( InstOrigin(..),
                          newDicts, instToId,
@@ -52,12 +53,13 @@ import DataCon              ( classDataCon )
 import Class           ( Class, DefMeth(..), classBigSig )
 import Var             ( idName, idType )
 import VarSet          ( emptyVarSet )
+import Id              ( setIdLocalExported )
 import MkId            ( mkDictFunId )
 import FunDeps         ( checkInstFDs )
 import Generics                ( validGenericInstanceType )
 import Module          ( Module, foldModuleEnv )
 import Name            ( getSrcLoc )
-import NameSet         ( unitNameSet, nameSetToList )
+import NameSet         ( unitNameSet, emptyNameSet, nameSetToList )
 import PrelInfo                ( eRROR_ID )
 import TyCon           ( TyCon )
 import Subst           ( mkTopTyVarSubst, substTheta )
@@ -65,6 +67,7 @@ import TysWiredIn     ( genericTyCons )
 import Name             ( Name )
 import SrcLoc           ( SrcLoc )
 import Unique          ( Uniquable(..) )
+import Util             ( lengthExceeds )
 import BasicTypes      ( NewOrData(..), Fixity )
 import ErrUtils                ( dumpIfSet_dyn )
 import ListSetOps      ( Assoc, emptyAssoc, plusAssoc_C, mapAssoc, 
@@ -329,7 +332,7 @@ get_generics decl@(ClassDecl {tcdName = class_name, tcdMeths = Just def_methods,
   | null groups                
   = returnTc [] -- The comon case: no generic default methods
 
-  | otherwise  -- A local class decl with generic default methods
+  | otherwise  -- A source class decl with generic default methods
   = recoverNF_Tc (returnNF_Tc [])                              $
     tcAddDeclCtxt decl                                         $
     tcLookupClass class_name                                   `thenTc` \ clas ->
@@ -347,7 +350,7 @@ get_generics decl@(ClassDecl {tcdName = class_name, tcdMeths = Just def_methods,
        tc_inst_infos = [(simpleInstInfoTyCon i, i) | i <- inst_infos]
 
        bad_groups = [group | group <- equivClassesByUniq get_uniq tc_inst_infos,
-                             length group > 1]
+                             group `lengthExceeds` 1]
        get_uniq (tc,_) = getUnique tc
     in
     mapTc (addErrTc . dupGenericInsts) bad_groups      `thenTc_`
@@ -522,8 +525,9 @@ tcInstDecl2 (InstInfo { iDFunId = dfun_id,
     let
        (inst_tyvars, dfun_theta, clas, inst_tys) = tcSplitDFunTy (idType dfun_id)
     in
-    tcInstTyVars inst_tyvars           `thenNF_Tc` \ (inst_tyvars', _, tenv) ->
+    tcInstSigTyVars InstTv inst_tyvars         `thenNF_Tc` \ inst_tyvars' ->
     let
+       tenv        = mkTopTyVarSubst inst_tyvars (mkTyVarTys inst_tyvars')
        inst_tys'   = map (substTy tenv) inst_tys
        dfun_theta' = substTheta tenv dfun_theta
        origin      = InstanceDeclOrigin
@@ -603,14 +607,22 @@ tcInstDecl2 (InstInfo { iDFunId = dfun_id,
 
        -- Create the result bindings
     let
+       local_dfun_id = setIdLocalExported dfun_id
+               -- Reason for setIdLocalExported: see notes with MkId.mkDictFunId
+
         dict_constr   = classDataCon clas
        scs_and_meths = map instToId (sc_dicts ++ meth_insts)
        this_dict_id  = instToId this_dict
-       inlines       = unitNameSet (idName dfun_id)
+       inlines       | null dfun_arg_dicts = emptyNameSet
+                     | otherwise           = unitNameSet (idName dfun_id)
                -- Always inline the dfun; this is an experimental decision
                -- because it makes a big performance difference sometimes.
                -- Often it means we can do the method selection, and then
                -- inline the method as well.  Marcin's idea; see comments below.
+               --
+               -- BUT: don't inline it if it's a constant dictionary;
+               -- we'll get all the benefit without inlining, and we get
+               -- a **lot** of code duplication if we inline it
 
        dict_rhs
          | null scs_and_meths
@@ -642,7 +654,7 @@ tcInstDecl2 (InstInfo { iDFunId = dfun_id,
          = AbsBinds
                 zonked_inst_tyvars
                 (map instToId dfun_arg_dicts)
-                [(inst_tyvars', dfun_id, this_dict_id)] 
+                [(inst_tyvars', local_dfun_id, this_dict_id)] 
                 inlines
                 (lie_binds1    `AndMonoBinds` 
                  lie_binds2    `AndMonoBinds`