Fix a pair of classic, but tricky, bugs in the type matcher; exposed by a program...
[ghc-hetmet.git] / compiler / types / InstEnv.lhs
index d8f549d..7d131d4 100644 (file)
@@ -1,4 +1,5 @@
 %
+% (c) The University of Glasgow 2006
 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
 %
 \section[InstEnv]{Utilities for typechecking instance declarations}
@@ -20,27 +21,24 @@ module InstEnv (
 
 #include "HsVersions.h"
 
-import Class           ( Class )
-import Var             ( Id, TyVar, isTcTyVar )
+import Class
+import Var
 import VarSet
-import Name            ( Name, NamedThing(..), getSrcLoc, nameIsLocalOrFrom, nameModule )
-import OccName         ( OccName )
-import NameSet         ( unionNameSets, unitNameSet, nameSetToList )
-import Type            ( TvSubst )
-import TcType          ( Type, PredType, tcEqType,
-                         tcSplitDFunTy, tyVarsOfTypes, isExistentialTyVar,
-                         pprThetaArrow, pprClassPred,
-                         tyClsNamesOfType, tcSplitTyConApp_maybe
-                       )
-import TyCon           ( tyConName )
-import TcGadt          ( tcUnifyTys, BindFlag(..) )
-import Unify           ( tcMatchTys )
+import Name
+import OccName
+import NameSet
+import Type
+import TcType
+import TyCon
+import TcGadt
+import Unify
 import Outputable
-import BasicTypes      ( OverlapFlag(..) )
-import UniqFM          ( UniqFM, lookupUFM, emptyUFM, addToUFM_C, eltsUFM )
-import Id              ( idType, idName )
-import SrcLoc          ( pprDefnLoc )
-import Maybe           ( isJust, isNothing )
+import BasicTypes
+import UniqFM
+import Id
+import SrcLoc
+
+import Data.Maybe      ( isJust, isNothing )
 \end{code}
 
 
@@ -56,11 +54,14 @@ data Instance
   = Instance { is_cls  :: Name         -- Class name
        
                -- Used for "rough matching"; see note below
+               -- INVARIANT: is_tcs = roughMatchTcs is_tys
             , is_tcs  :: [Maybe Name]  -- Top of type args
 
                -- Used for "proper matching"; see note
             , is_tvs  :: TyVarSet      -- Template tyvars for full match
             , is_tys  :: [Type]        -- Full arg types
+               -- INVARIANT: is_dfun Id has type 
+               --      forall is_tvs. (...) => is_cls is_tys
 
             , is_dfun :: DFunId
             , is_flag :: OverlapFlag   -- See detailed comments with
@@ -90,7 +91,7 @@ In is_tcs,
 
 The "proper-match" fields
 ~~~~~~~~~~~~~~~~~~~~~~~~~
-The is_tvs, is_tys fields are simply cahced values, pulled
+The is_tvs, is_tys fields are simply cached values, pulled
 out (lazily) from the dfun id. They are cached here simply so 
 that we don't need to decompose the DFunId each time we want 
 to match it.  The hope is that the fast-match fields mean