newDicts, newDictsAtLoc, newMethod, newMethodWithGivenTy, newOverloadedLit,
- instType, tyVarsOfInst, lookupInst,
+ instType, tyVarsOfInst, lookupInst, lookupSimpleInst,
isDict, isTyVarDict,
import TcHsSyn ( TcIdOcc(..), TcExpr(..), TcIdBndr(..),
mkHsTyApp, mkHsDictApp )
-import TcMonad
+import TcMonad hiding ( rnMtoTcM )
import TcEnv ( tcLookupGlobalValueByKey )
import TcType ( TcType(..), TcRhoType(..), TcMaybe, TcTyVarSet(..),
- tcInstType, tcInstTcType, zonkTcType )
+ tcInstType, zonkTcType )
import Bag ( emptyBag, unitBag, unionBags, unionManyBags, listToBag, consBag )
-import Class ( Class(..), GenClass, ClassInstEnv(..), getClassInstEnv )
+import Class ( Class(..), GenClass, ClassInstEnv(..), classInstEnv )
import Id ( GenId, idType, mkInstId )
import MatchEnv ( lookupMEnv, insertMEnv )
import Name ( mkLocalName, getLocalName, Name )
import RnHsSyn ( RnName{-instance NamedThing-} )
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 )
import Unique ( Unique, showUnique,
fromRationalClassOpKey, fromIntClassOpKey, fromIntegerClassOpKey )
import Util ( panic, zipEqual, zipWithEqual, assoc, assertPanic )
-
\end{code}
%************************************************************************
-> [(Class, TcType s)]
-> NF_TcM s (LIE s, [TcIdOcc s])
newDicts orig theta
- = tcGetSrcLoc `thenNF_Tc` \ loc ->
- tcGetUniques (length theta) `thenNF_Tc` \ new_uniqs ->
- let
+ = tcGetSrcLoc `thenNF_Tc` \ loc ->
+ 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
- in
- returnNF_Tc (listToBag dicts, map instToId dicts)
+ dicts = zipWithEqual "newDicts" mk_dict new_uniqs theta
+ in
+ returnNF_Tc (listToBag dicts, map instToId dicts)
newDictsAtLoc orig loc theta -- Local function, similar to newDicts,
-- but with slightly different interface
- = tcGetUniques (length theta) `thenNF_Tc` \ new_uniqs ->
- let
+ = 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
- in
- returnNF_Tc (dicts, map instToId dicts)
+ dicts = zipWithEqual "newDictsAtLoc" mk_dict new_uniqs theta
+ in
+ returnNF_Tc (dicts, map instToId dicts)
newMethod :: InstOrigin s
-> TcIdOcc s
-> [TcType s]
-> NF_TcM s (LIE s, TcIdOcc s)
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
- TcId id -> let (tyvars, rho) = splitForAllTy (idType id)
- in tcInstTcType (tyvars `zipEqual` tys) rho
- ) `thenNF_Tc` \ rho_ty ->
-
- -- Our friend does the rest
- newMethodWithGivenTy orig id tys rho_ty
+ = -- Get the Id type and instantiate it at the specified types
+ (case id of
+ RealId id -> let (tyvars, rho) = splitForAllTy (idType id)
+ in tcInstType (zipEqual "newMethod" tyvars tys) rho
+ TcId id -> let (tyvars, rho) = splitForAllTy (idType id)
+ 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
newMethodWithGivenTy orig id tys rho_ty
- = tcGetSrcLoc `thenNF_Tc` \ loc ->
- tcGetUnique `thenNF_Tc` \ new_uniq ->
- let
+ = tcGetSrcLoc `thenNF_Tc` \ loc ->
+ tcGetUnique `thenNF_Tc` \ new_uniq ->
+ let
meth_inst = Method new_uniq id tys rho_ty orig loc
- in
- returnNF_Tc (unitLIE meth_inst, instToId meth_inst)
+ in
+ returnNF_Tc (unitLIE meth_inst, instToId meth_inst)
newMethodAtLoc :: InstOrigin s -> SrcLoc -> Id -> [TcType s] -> NF_TcM s (Inst s, TcIdOcc s)
newMethodAtLoc orig loc real_id tys -- Local function, similar to newMethod but with
-- slightly different interface
- = -- Get the Id type and instantiate it at the specified types
- let
- (tyvars,rho) = splitForAllTy (idType real_id)
- in
- tcInstType (tyvars `zipEqual` tys) rho `thenNF_Tc` \ rho_ty ->
- tcGetUnique `thenNF_Tc` \ new_uniq ->
- let
+ = -- Get the Id type and instantiate it at the specified types
+ let
+ (tyvars,rho) = splitForAllTy (idType real_id)
+ in
+ 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
- returnNF_Tc (meth_inst, instToId meth_inst)
+ in
+ returnNF_Tc (meth_inst, instToId meth_inst)
newOverloadedLit :: InstOrigin s
-> OverloadedLit
-> TcType s
-> NF_TcM s (LIE s, TcIdOcc s)
newOverloadedLit orig lit ty
- = tcGetSrcLoc `thenNF_Tc` \ loc ->
- tcGetUnique `thenNF_Tc` \ new_uniq ->
- let
+ = tcGetSrcLoc `thenNF_Tc` \ loc ->
+ tcGetUnique `thenNF_Tc` \ new_uniq ->
+ let
lit_inst = LitInst new_uniq lit ty orig loc
- in
- returnNF_Tc (unitLIE lit_inst, instToId lit_inst)
+ in
+ returnNF_Tc (unitLIE lit_inst, instToId lit_inst)
\end{code}
\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}
\begin{code}
instance Outputable (Inst s) where
ppr sty (LitInst uniq lit ty orig loc)
- = ppHang (ppSep [case lit of
+ = 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)
+ ppStr "at",
+ ppr sty ty,
+ show_uniq sty uniq
+ ]
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)
+ = ppSep [ppr sty clas,
+ ppStr "at",
+ ppr sty ty,
+ show_uniq sty uniq
+ ]
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)
+ = ppSep [ppr sty id,
+ ppStr "at",
+ ppr sty tys,
+ show_uniq sty uniq
+ ]
show_uniq PprDebug uniq = ppr PprDebug uniq
show_uniq sty uniq = ppNil
-show_origin sty orig = ppBesides [ppLparen, pprOrigin sty orig, ppRparen]
\end{code}
Printing in error messages
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 orig) $
+ failTc (noInstanceErr dict)
Just (dfun_id, tenv)
-> let
ambiguous dictionaries.
\begin{code}
-lookupClassInstAtSimpleType :: Class -> Type -> Maybe Id
-
-lookupClassInstAtSimpleType clas ty
- = case (lookupMEnv matchTy (getClassInstEnv 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}
mkInstSpecEnv clas inst_ty inst_tvs inst_theta
= mkSpecEnv (catMaybes (map maybe_spec_info matches))
where
- matches = matchMEnv matchTy (getClassInstEnv clas) inst_ty
+ matches = matchMEnv matchTy (classInstEnv clas) inst_ty
maybe_spec_info (_, match_info, MkInstTemplate dfun _ [])
= Just (SpecInfo (map (assocMaybe match_info) inst_tvs) (length inst_theta) dfun)
| 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
-- 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
-- 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 = getClassInstEnv clas
+get_inst_env clas other_orig = classInstEnv clas
-pprOrigin :: PprStyle -> InstOrigin s -> Pretty
+pprOrigin :: InstOrigin s -> PprStyle -> Pretty
-pprOrigin sty (OccurrenceOf id)
+pprOrigin (OccurrenceOf id) sty
= ppBesides [ppPStr SLIT("at a use of an overloaded identifier: `"),
ppr sty id, ppChar '\'']
-pprOrigin sty (OccurrenceOfCon id)
+pprOrigin (OccurrenceOfCon id) sty
= ppBesides [ppPStr SLIT("at a use of an overloaded constructor: `"),
ppr sty id, ppChar '\'']
-pprOrigin sty (InstanceDeclOrigin)
+pprOrigin (InstanceDeclOrigin) sty
= ppStr "in an instance declaration"
-pprOrigin sty (LiteralOrigin lit)
+pprOrigin (LiteralOrigin lit) sty
= ppCat [ppStr "at an overloaded literal:", ppr sty lit]
-pprOrigin sty (ArithSeqOrigin seq)
+pprOrigin (ArithSeqOrigin seq) sty
= ppCat [ppStr "at an arithmetic sequence:", ppr sty seq]
-pprOrigin sty (SignatureOrigin)
+pprOrigin (SignatureOrigin) sty
= ppStr "in a type signature"
-pprOrigin sty (DoOrigin)
+pprOrigin (DoOrigin) sty
= ppStr "in a do statement"
-pprOrigin sty (ClassDeclOrigin)
+pprOrigin (ClassDeclOrigin) sty
= 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)
+-- pprOrigin (DerivingOrigin _ clas tycon) sty
+-- = ppBesides [ppStr "in a `deriving' clause; class `",
+-- ppr sty clas,
+-- ppStr "'; offending type `",
+-- ppr sty tycon,
+-- ppStr "'"]
+pprOrigin (InstanceSpecOrigin _ clas ty) sty
= 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)
+-- pprOrigin (DefaultDeclOrigin) sty
+-- = ppStr "in a `default' declaration"
+pprOrigin (ValSpecOrigin name) sty
= ppBesides [ppStr "in a SPECIALIZE user-pragma for `",
ppr sty name, ppStr "'"]
-pprOrigin sty (CCallOrigin clabel Nothing{-ccall result-})
+pprOrigin (CCallOrigin clabel Nothing{-ccall result-}) sty
= ppBesides [ppStr "in the result of the _ccall_ to `",
ppStr clabel, ppStr "'"]
-pprOrigin sty (CCallOrigin clabel (Just arg_expr))
+pprOrigin (CCallOrigin clabel (Just arg_expr)) sty
= ppBesides [ppStr "in an argument in the _ccall_ to `",
ppStr clabel, ppStr "', namely: ", ppr sty arg_expr]
-pprOrigin sty (LitLitOrigin s)
+pprOrigin (LitLitOrigin s) sty
= ppBesides [ppStr "in this ``literal-literal'': ", ppStr s]
-pprOrigin sty UnknownOrigin
+pprOrigin UnknownOrigin sty
= ppStr "in... oops -- I don't know where the overloading came from!"
\end{code}