[project @ 1996-07-19 18:36:04 by partain]
[ghc-hetmet.git] / ghc / compiler / typecheck / Inst.lhs
index d0615f6..6b8a7af 100644 (file)
@@ -10,59 +10,67 @@ module Inst (
        Inst(..),       -- Visible only to TcSimplify
 
        InstOrigin(..), OverloadedLit(..),
-       LIE(..), emptyLIE, unitLIE, plusLIE, consLIE, zonkLIE, plusLIEs,
+       SYN_IE(LIE), emptyLIE, unitLIE, plusLIE, consLIE, zonkLIE, plusLIEs,
 
-        InstanceMapper(..),
+        SYN_IE(InstanceMapper),
 
        newDicts, newDictsAtLoc, newMethod, newMethodWithGivenTy, newOverloadedLit,
 
-       instType, tyVarsOfInst, lookupInst,
+       instType, tyVarsOfInst, lookupInst, lookupSimpleInst,
 
        isDict, isTyVarDict, 
 
        zonkInst, instToId,
 
        matchesInst,
-       instBindingRequired, instCanBeGeneralised
-
+       instBindingRequired, instCanBeGeneralised,
+       
+       pprInst
     ) where
 
-import Ubiq
+IMP_Ubiq()
+IMPORT_1_3(Ratio(Rational))
 
 import HsSyn   ( HsLit(..), HsExpr(..), HsBinds, 
-                 InPat, OutPat, Stmt, Qual, Match,
+                 InPat, OutPat, Stmt, Qualifier, Match,
                  ArithSeqInfo, PolyType, Fake )
-import RnHsSyn ( RenamedArithSeqInfo(..), RenamedHsExpr(..) )
-import TcHsSyn ( TcIdOcc(..), TcExpr(..), TcIdBndr(..),
-                 mkHsTyApp, mkHsDictApp )
+import RnHsSyn ( SYN_IE(RenamedArithSeqInfo), SYN_IE(RenamedHsExpr),
+                 RnName{-instance NamedThing-}
+               )
+import TcHsSyn ( TcIdOcc(..), SYN_IE(TcExpr), SYN_IE(TcIdBndr),
+                 mkHsTyApp, mkHsDictApp, tcIdTyVars )
 
-import TcMonad
-import TcEnv   ( tcLookupGlobalValueByKey )
-import TcType  ( TcType(..), TcRhoType(..), TcMaybe, TcTyVarSet(..),
-                 tcInstType, tcInstTcType, zonkTcType )
+import TcMonad hiding ( rnMtoTcM )
+import TcEnv   ( tcLookupGlobalValueByKey, tcLookupTyConByKey )
+import TcType  ( SYN_IE(TcType), SYN_IE(TcRhoType), TcMaybe, SYN_IE(TcTyVarSet),
+                 tcInstType, zonkTcType )
 
 import Bag     ( emptyBag, unitBag, unionBags, unionManyBags, listToBag, consBag )
-import Class   ( Class(..), GenClass, ClassInstEnv(..), classInstEnv )
+import Class   ( isCcallishClass, isNoDictClass, classInstEnv,
+                 SYN_IE(Class), GenClass, SYN_IE(ClassInstEnv), SYN_IE(ClassOp)
+               )
+import ErrUtils ( addErrLoc, SYN_IE(Error) )
 import Id      ( GenId, idType, mkInstId )
 import MatchEnv        ( lookupMEnv, insertMEnv )
 import Name    ( mkLocalName, getLocalName, Name )
 import Outputable
-import PprType ( GenClass, TyCon, GenType, GenTyVar )  
+import PprType ( GenClass, TyCon, GenType, GenTyVar, pprParendGenType )        
 import PprStyle        ( PprStyle(..) )
 import Pretty
-import RnHsSyn ( RnName{-instance NamedThing-} )
-import SpecEnv ( SpecEnv(..) )
+import SpecEnv ( SpecEnv )
 import SrcLoc  ( SrcLoc, mkUnknownSrcLoc )
-import Type    ( GenType, eqSimpleTy,
+import Type    ( GenType, eqSimpleTy, instantiateTy,
                  isTyVarTy, mkDictTy, splitForAllTy, splitSigmaTy,
-                 splitRhoTy, matchTy, tyVarsOfType, tyVarsOfTypes )
-import TyVar   ( GenTyVar )
+                 splitRhoTy, matchTy, tyVarsOfType, tyVarsOfTypes,
+                 mkSynTy
+               )
+import TyVar   ( unionTyVarSets, GenTyVar )
 import TysPrim   ( intPrimTy )
-import TysWiredIn ( intDataCon )
-import Unique  ( Unique, showUnique,
-                 fromRationalClassOpKey, fromIntClassOpKey, fromIntegerClassOpKey )
-import Util    ( panic, zipEqual, zipWithEqual, assoc, assertPanic )
-
+import TysWiredIn ( intDataCon, integerTy )
+import Unique  ( showUnique, fromRationalClassOpKey, rationalTyConKey,
+                 fromIntClassOpKey, fromIntegerClassOpKey, Unique
+               )
+import Util    ( panic, zipEqual, zipWithEqual, assoc, assertPanic, pprTrace{-ToDo:rm-} )
 \end{code}
 
 %************************************************************************
@@ -158,7 +166,7 @@ newDicts orig theta
     tcGetUniques (length theta)                `thenNF_Tc` \ new_uniqs ->
     let
        mk_dict u (clas, ty) = Dict u clas ty orig loc
-       dicts = zipWithEqual mk_dict new_uniqs theta
+       dicts = zipWithEqual "newDicts" mk_dict new_uniqs theta
     in
     returnNF_Tc (listToBag dicts, map instToId dicts)
 
@@ -167,7 +175,7 @@ newDictsAtLoc orig loc theta        -- Local function, similar to newDicts,
   = tcGetUniques (length theta)                `thenNF_Tc` \ new_uniqs ->
     let
        mk_dict u (clas, ty) = Dict u clas ty orig loc
-       dicts = zipWithEqual mk_dict new_uniqs theta
+       dicts = zipWithEqual "newDictsAtLoc" mk_dict new_uniqs theta
     in
     returnNF_Tc (dicts, map instToId dicts)
 
@@ -179,9 +187,11 @@ newMethod orig id tys
   =    -- Get the Id type and instantiate it at the specified types
     (case id of
        RealId id -> let (tyvars, rho) = splitForAllTy (idType id)
-                   in tcInstType (tyvars `zipEqual` tys) rho
+                   in
+                   (if length tyvars /= length tys then pprTrace "newMethod" (ppr PprDebug (idType id)) else \x->x) $
+                   tcInstType (zip{-Equal "newMethod"-} tyvars tys) rho
        TcId   id -> let (tyvars, rho) = splitForAllTy (idType id)
-                   in tcInstTcType (tyvars `zipEqual` tys) rho
+                   in returnNF_Tc (instantiateTy (zipEqual "newMethod(2)" tyvars tys) rho)
     )                                          `thenNF_Tc` \ rho_ty ->
         -- Our friend does the rest
     newMethodWithGivenTy orig id tys rho_ty
@@ -202,8 +212,8 @@ newMethodAtLoc orig loc real_id tys -- Local function, similar to newMethod but
     let
         (tyvars,rho) = splitForAllTy (idType real_id)
     in
-    tcInstType (tyvars `zipEqual` tys) rho     `thenNF_Tc` \ rho_ty ->
-    tcGetUnique                                        `thenNF_Tc` \ new_uniq ->
+    tcInstType (zipEqual "newMethodAtLoc" tyvars tys) rho `thenNF_Tc` \ rho_ty ->
+    tcGetUnique                                                  `thenNF_Tc` \ new_uniq ->
     let
        meth_inst = Method new_uniq (RealId real_id) tys rho_ty orig loc
     in
@@ -226,13 +236,17 @@ newOverloadedLit orig lit ty
 \begin{code}
 instToId :: Inst s -> TcIdOcc s
 instToId (Dict u clas ty orig loc)
-  = TcId (mkInstId u (mkDictTy clas ty) (mkLocalName u SLIT("dict") loc))
+  = TcId (mkInstId u (mkDictTy clas ty) (mkLocalName u str False{-emph name-} loc))
+  where
+    str = SLIT("d.") _APPEND_ (getLocalName clas)
 instToId (Method u id tys rho_ty orig loc)
-  = TcId (mkInstId u tau_ty (mkLocalName u (getLocalName id) loc))
+  = TcId (mkInstId u tau_ty (mkLocalName u str False{-emph name-} loc))
   where
     (_, tau_ty) = splitRhoTy rho_ty    -- NB The method Id has just the tau type
+    str = SLIT("m.") _APPEND_ (getLocalName id)
+
 instToId (LitInst u list ty orig loc)
-  = TcId (mkInstId u ty (mkLocalName u SLIT("lit") loc))
+  = TcId (mkInstId u ty (mkLocalName u SLIT("lit") True{-emph uniq-} loc))
 \end{code}
 
 \begin{code}
@@ -269,7 +283,9 @@ zonkInst (LitInst u lit ty orig loc)
 \begin{code}
 tyVarsOfInst :: Inst s -> TcTyVarSet s
 tyVarsOfInst (Dict _ _ ty _ _)        = tyVarsOfType  ty
-tyVarsOfInst (Method _ _ tys rho _ _) = tyVarsOfTypes tys
+tyVarsOfInst (Method _ id tys rho _ _) = tyVarsOfTypes tys `unionTyVarSets` tcIdTyVars id
+                                        -- The id might not be a RealId; in the case of
+                                        -- locally-overloaded class methods, for example
 tyVarsOfInst (LitInst _ _ ty _ _)     = tyVarsOfType  ty
 \end{code}
 
@@ -317,19 +333,12 @@ must be witnessed by an actual binding; the second tells whether an
 
 \begin{code}
 instBindingRequired :: Inst s -> Bool
-instBindingRequired inst
-  = case getInstOrigin inst of
-       CCallOrigin _ _   -> False      -- No binding required
-       LitLitOrigin  _   -> False
-       OccurrenceOfCon _ -> False
-       other             -> True
+instBindingRequired (Dict _ clas _ _ _) = not (isNoDictClass clas)
+instBindingRequired other              = True
 
 instCanBeGeneralised :: Inst s -> Bool
-instCanBeGeneralised inst
-  = case getInstOrigin inst of
-       CCallOrigin _ _ -> False        -- Can't be generalised
-       LitLitOrigin  _ -> False        -- Can't be generalised
-       other           -> True
+instCanBeGeneralised (Dict _ clas _ _ _) = not (isCcallishClass clas)
+instCanBeGeneralised other              = True
 \end{code}
 
 
@@ -340,36 +349,29 @@ relevant in error messages.
 
 \begin{code}
 instance Outputable (Inst s) where
-    ppr sty (LitInst uniq lit ty orig loc)
-      = ppHang (ppSep [case lit of
-                         OverloadedIntegral   i -> ppInteger i
-                         OverloadedFractional f -> ppRational f,
-                      ppStr "at",
-                      ppr sty ty,
-                      show_uniq sty uniq
-               ])
-         4 (show_origin sty orig)
-
-    ppr sty (Dict uniq clas ty orig loc)
-      = ppHang (ppSep [ppr sty clas, 
-                      ppStr "at",
-                      ppr sty ty,
-                      show_uniq sty uniq
-               ])
-         4 (show_origin sty orig)
-
-    ppr sty (Method uniq id tys rho orig loc)
-      = ppHang (ppSep [ppr sty id, 
-                      ppStr "at",
-                      ppr sty tys,
-                      show_uniq sty uniq
-               ])
-         4 (show_origin sty orig)
-
-show_uniq PprDebug uniq = ppr PprDebug uniq
-show_uniq sty     uniq = ppNil
-
-show_origin sty orig    = ppBesides [ppLparen, pprOrigin sty orig, ppRparen]
+    ppr sty inst = ppr_inst sty ppNil (\ o l -> ppNil) inst
+
+pprInst sty hdr inst = ppr_inst sty hdr (\ o l -> pprOrigin hdr o l sty) inst
+
+ppr_inst sty hdr ppr_orig (LitInst u lit ty orig loc)
+  = ppHang (ppr_orig orig loc)
+        4 (ppCat [case lit of
+                     OverloadedIntegral   i -> ppInteger i
+                     OverloadedFractional f -> ppRational f,
+                  ppStr "at",
+                  ppr sty ty,
+                  show_uniq sty u])
+
+ppr_inst sty hdr ppr_orig (Dict u clas ty orig loc)
+  = ppHang (ppr_orig orig loc)
+        4 (ppCat [ppr sty clas, pprParendGenType sty ty, show_uniq sty u])
+
+ppr_inst sty hdr ppr_orig (Method u id tys rho orig loc)
+  = ppHang (ppr_orig orig loc)
+        4 (ppCat [ppr sty id, ppStr "at", interppSP sty tys, show_uniq sty u])
+
+show_uniq PprDebug u = ppr PprDebug u
+show_uniq sty     u = ppNil
 \end{code}
 
 Printing in error messages
@@ -412,7 +414,9 @@ lookupInst :: Inst s
 
 lookupInst dict@(Dict _ clas ty orig loc)
   = case lookupMEnv matchTy (get_inst_env clas orig) ty of
-      Nothing  -> failTc (noInstanceErr dict)
+      Nothing  -> tcAddSrcLoc loc               $
+                  tcAddErrCtxt (pprOrigin ""{-hdr-} orig loc) $
+                  failTc (noInstanceErr dict)
 
       Just (dfun_id, tenv) 
        -> let
@@ -452,15 +456,22 @@ lookupInst inst@(LitInst u (OverloadedIntegral i) ty orig loc)
   =     -- Alas, it is overloaded and a big literal!
     tcLookupGlobalValueByKey fromIntegerClassOpKey     `thenNF_Tc` \ from_integer ->
     newMethodAtLoc orig loc from_integer [ty]          `thenNF_Tc` \ (method_inst, method_id) ->
-    returnTc ([method_inst], (instToId inst, HsApp (HsVar method_id) (HsLitOut (HsInt i) ty)))
+    returnTc ([method_inst], (instToId inst, HsApp (HsVar method_id) (HsLitOut (HsInt i) integerTy)))
   where
     intprim_lit    = HsLitOut (HsIntPrim i) intPrimTy
     int_lit        = HsApp (HsVar (RealId intDataCon)) intprim_lit
 
 lookupInst inst@(LitInst u (OverloadedFractional f) ty orig loc)
   = tcLookupGlobalValueByKey fromRationalClassOpKey    `thenNF_Tc` \ from_rational ->
+
+       -- The type Rational isn't wired in so we have to conjure it up
+    tcLookupTyConByKey rationalTyConKey        `thenNF_Tc` \ rational_tycon ->
+    let
+       rational_ty  = mkSynTy rational_tycon []
+       rational_lit = HsLitOut (HsFrac f) rational_ty
+    in
     newMethodAtLoc orig loc from_rational [ty]         `thenNF_Tc` \ (method_inst, method_id) ->
-    returnTc ([method_inst], (instToId inst, HsApp (HsVar method_id) (HsLitOut (HsFrac f) ty)))
+    returnTc ([method_inst], (instToId inst, HsApp (HsVar method_id) rational_lit))
 \end{code}
 
 There is a second, simpler interface, when you want an instance of a
@@ -469,15 +480,21 @@ appropriate dictionary if it exists.  It is used only when resolving
 ambiguous dictionaries.
 
 \begin{code}
-lookupClassInstAtSimpleType :: Class -> Type -> Maybe Id
-
-lookupClassInstAtSimpleType clas ty
-  = case (lookupMEnv matchTy (classInstEnv clas) ty) of
-      Nothing      -> Nothing
-      Just (dfun,_) -> ASSERT( null tyvars && null theta )
-                      Just dfun
-                   where
-                      (tyvars, theta, _) = splitSigmaTy (idType dfun)
+lookupSimpleInst :: ClassInstEnv
+                -> Class
+                -> Type                        -- Look up (c,t)
+                -> TcM s [(Class,Type)]        -- Here are the needed (c,t)s
+
+lookupSimpleInst class_inst_env clas ty
+  = case (lookupMEnv matchTy class_inst_env ty) of
+      Nothing         -> failTc (noSimpleInst clas ty)
+      Just (dfun,tenv) -> returnTc [(c,instantiateTy tenv t) | (c,t) <- theta]
+                      where
+                         (_, theta, _) = splitSigmaTy (idType dfun)
+
+noSimpleInst clas ty sty
+  = ppSep [ppStr "No instance for class", ppQuote (ppr sty clas),
+          ppStr "at type", ppQuote (ppr sty ty)]
 \end{code}
 
 
@@ -553,9 +570,10 @@ data InstOrigin s
 
   | ClassDeclOrigin            -- Manufactured during a class decl
 
-  | DerivingOrigin     InstanceMapper
-                       Class
-                       TyCon
+--     NO MORE!
+--  | DerivingOrigin   InstanceMapper
+--                     Class
+--                     TyCon
 
        -- During "deriving" operations we have an ever changing
        -- mapping of classes to instances, so we record it inside the
@@ -571,7 +589,7 @@ data InstOrigin s
        -- origin information.  This is a bit of a hack, but it works
        -- fine.  (Patrick is to blame [WDP].)
 
-  | DefaultDeclOrigin          -- Related to a `default' declaration
+--  | DefaultDeclOrigin                -- Related to a `default' declaration
 
   | ValSpecOrigin      Name    -- in a SPECIALIZE pragma for a value
 
@@ -596,58 +614,50 @@ data InstOrigin s
 -- find a mapping from classes to envts inside the dict origin.
 
 get_inst_env :: Class -> InstOrigin s -> ClassInstEnv
-get_inst_env clas (DerivingOrigin inst_mapper _ _)
-  = fst (inst_mapper clas)
+-- get_inst_env clas (DerivingOrigin inst_mapper _ _)
+--  = fst (inst_mapper clas)
 get_inst_env clas (InstanceSpecOrigin inst_mapper _ _)
   = fst (inst_mapper clas)
 get_inst_env clas other_orig = classInstEnv clas
 
 
-pprOrigin :: PprStyle -> InstOrigin s -> Pretty
+pprOrigin :: String -> InstOrigin s -> SrcLoc -> Error
 
-pprOrigin sty (OccurrenceOf id)
-      = ppBesides [ppPStr SLIT("at a use of an overloaded identifier: `"),
+pprOrigin hdr orig locn
+  = addErrLoc locn hdr $ \ sty ->
+    case orig of
+      OccurrenceOf id ->
+        ppBesides [ppPStr SLIT("at a use of an overloaded identifier: `"),
                   ppr sty id, ppChar '\'']
-pprOrigin sty (OccurrenceOfCon id)
-      = ppBesides [ppPStr SLIT("at a use of an overloaded constructor: `"),
+      OccurrenceOfCon id ->
+        ppBesides [ppPStr SLIT("at a use of an overloaded constructor: `"),
                   ppr sty id, ppChar '\'']
-pprOrigin sty (InstanceDeclOrigin)
-      = ppStr "in an instance declaration"
-pprOrigin sty (LiteralOrigin lit)
-      = ppCat [ppStr "at an overloaded literal:", ppr sty lit]
-pprOrigin sty (ArithSeqOrigin seq)
-      = ppCat [ppStr "at an arithmetic sequence:", ppr sty seq]
-pprOrigin sty (SignatureOrigin)
-      = ppStr "in a type signature"
-pprOrigin sty (DoOrigin)
-      = ppStr "in a do statement"
-pprOrigin sty (ClassDeclOrigin)
-      = ppStr "in a class declaration"
-pprOrigin sty (DerivingOrigin _ clas tycon)
-      = ppBesides [ppStr "in a `deriving' clause; class `",
-                         ppr sty clas,
-                         ppStr "'; offending type `",
-                         ppr sty tycon,
-                         ppStr "'"]
-pprOrigin sty (InstanceSpecOrigin _ clas ty)
-      = ppBesides [ppStr "in a SPECIALIZE instance pragma; class \"",
+      InstanceDeclOrigin ->
+       ppStr "in an instance declaration"
+      LiteralOrigin lit ->
+       ppCat [ppStr "at an overloaded literal:", ppr sty lit]
+      ArithSeqOrigin seq ->
+       ppCat [ppStr "at an arithmetic sequence:", ppr sty seq]
+      SignatureOrigin ->
+       ppStr "in a type signature"
+      DoOrigin ->
+       ppStr "in a do statement"
+      ClassDeclOrigin ->
+       ppStr "in a class declaration"
+      InstanceSpecOrigin _ clas ty ->
+       ppBesides [ppStr "in a SPECIALIZE instance pragma; class \"",
                   ppr sty clas, ppStr "\" type: ", ppr sty ty]
-pprOrigin sty (DefaultDeclOrigin)
-      = ppStr "in a `default' declaration"
-pprOrigin sty (ValSpecOrigin name)
-      = ppBesides [ppStr "in a SPECIALIZE user-pragma for `",
+      ValSpecOrigin name ->
+       ppBesides [ppStr "in a SPECIALIZE user-pragma for `",
                   ppr sty name, ppStr "'"]
-pprOrigin sty (CCallOrigin clabel Nothing{-ccall result-})
-      = ppBesides [ppStr "in the result of the _ccall_ to `",
+      CCallOrigin clabel Nothing{-ccall result-} ->
+       ppBesides [ppStr "in the result of the _ccall_ to `",
                   ppStr clabel, ppStr "'"]
-pprOrigin sty (CCallOrigin clabel (Just arg_expr))
-      = ppBesides [ppStr "in an argument in the _ccall_ to `",
+      CCallOrigin clabel (Just arg_expr) ->
+       ppBesides [ppStr "in an argument in the _ccall_ to `",
                  ppStr clabel, ppStr "', namely: ", ppr sty arg_expr]
-pprOrigin sty (LitLitOrigin s)
-      = ppBesides [ppStr "in this ``literal-literal'': ", ppStr s]
-pprOrigin sty UnknownOrigin
-      = ppStr "in... oops -- I don't know where the overloading came from!"
+      LitLitOrigin s ->
+       ppBesides [ppStr "in this ``literal-literal'': ", ppStr s]
+      UnknownOrigin ->
+       ppStr "in... oops -- I don't know where the overloading came from!"
 \end{code}
-
-
-