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(..), classInstEnv )
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}
%************************************************************************
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)
= 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)
= -- 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 tcInstType (zipEqual "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
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
\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}
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}
| 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 = classInstEnv clas
= ppStr "in a do statement"
pprOrigin (ClassDeclOrigin) sty
= ppStr "in a class declaration"
-pprOrigin (DerivingOrigin _ clas tycon) sty
- = ppBesides [ppStr "in a `deriving' clause; class `",
- ppr sty clas,
- ppStr "'; offending type `",
- ppr sty tycon,
- ppStr "'"]
+-- 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 (DefaultDeclOrigin) sty
- = ppStr "in a `default' declaration"
+-- pprOrigin (DefaultDeclOrigin) sty
+-- = ppStr "in a `default' declaration"
pprOrigin (ValSpecOrigin name) sty
= ppBesides [ppStr "in a SPECIALIZE user-pragma for `",
ppr sty name, ppStr "'"]