import TyCon ( TyCon, isSynTyCon )
import Type ( splitDFunTy, isTyVarTy,
splitTyConApp_maybe, splitDictTy,
- splitAlgTyConApp_maybe, splitForAllTys,
- unUsgTy, tyVarsOfTypes, mkClassPred, mkTyVarTy,
+ splitForAllTys,
+ tyVarsOfTypes, mkClassPred, mkTyVarTy,
getClassTys_maybe
)
import Subst ( mkTopTyVarSubst, substClasses )
-> TcEnv -- Contains IdInfo for dfun ids
-> (Name -> Maybe Fixity) -- for deriving Show and Read
-> Module -- Module for deriving
- -> [TyCon]
-> [RenamedHsDecl]
-> TcM (PackageInstEnv, InstEnv, [InstInfo], RenamedHsBinds)
-tcInstDecls1 inst_env0 prs hst unf_env get_fixity mod tycons decls
+tcInstDecls1 inst_env0 prs hst unf_env get_fixity mod decls
= let
inst_decls = [inst_decl | InstD inst_decl <- decls]
tycl_decls = [decl | TyClD decl <- decls]
clas_decls = filter isClassDecl tycl_decls
in
-- (1) Do the ordinary instance declarations
- mapNF_Tc (tcInstDecl1 mod unf_env) inst_decls `thenNF_Tc` \ inst_infos ->
+ mapNF_Tc (tcInstDecl1 unf_env) inst_decls `thenNF_Tc` \ inst_infos ->
-- (2) Instances from generic class declarations
- getGenericInstances mod clas_decls `thenTc` \ generic_inst_info ->
+ getGenericInstances clas_decls `thenTc` \ generic_inst_info ->
-- Next, construct the instance environment so far, consisting of
-- a) cached non-home-package InstEnv (gotten from pcs) pcs_insts pcs
addInstDFuns :: InstEnv -> [DFunId] -> NF_TcM InstEnv
addInstDFuns dfuns infos
= getDOptsTc `thenTc` \ dflags ->
- extendInstEnv dflags dfuns infos `bind` \ (inst_env', errs) ->
+ let
+ (inst_env', errs) = extendInstEnv dflags dfuns infos
+ in
addErrsTc errs `thenNF_Tc_`
returnTc inst_env'
- where
- bind x f = f x
-
\end{code}
\begin{code}
-tcInstDecl1 :: Module -> TcEnv -> RenamedInstDecl -> NF_TcM [InstInfo]
+tcInstDecl1 :: TcEnv -> RenamedInstDecl -> NF_TcM [InstInfo]
-- Deal with a single instance declaration
-tcInstDecl1 mod unf_env (InstDecl poly_ty binds uprags maybe_dfun_name src_loc)
+tcInstDecl1 unf_env (InstDecl poly_ty binds uprags maybe_dfun_name src_loc)
= -- Prime error recovery, set source location
recoverNF_Tc (returnNF_Tc []) $
tcAddSrcLoc src_loc $
mapNF_Tc scrutiniseInstanceConstraint theta `thenNF_Tc_`
-- Make the dfun id and return it
- newDFunName mod clas inst_tys src_loc `thenNF_Tc` \ dfun_name ->
+ newDFunName clas inst_tys src_loc `thenNF_Tc` \ dfun_name ->
returnNF_Tc (True, dfun_name)
Just dfun_name -> -- An interface-file instance declaration
\begin{code}
-getGenericInstances :: Module -> [RenamedTyClDecl] -> TcM [InstInfo]
-getGenericInstances mod class_decls
- = mapTc (get_generics mod) class_decls `thenTc` \ gen_inst_infos ->
+getGenericInstances :: [RenamedTyClDecl] -> TcM [InstInfo]
+getGenericInstances class_decls
+ = mapTc get_generics class_decls `thenTc` \ gen_inst_infos ->
let
gen_inst_info = concat gen_inst_infos
in
`thenNF_Tc_`
returnTc gen_inst_info
-get_generics mod decl@(ClassDecl context class_name tyvar_names
- fundeps class_sigs def_methods
- name_list loc)
+get_generics decl@(ClassDecl {tcdMeths = Nothing})
+ = returnTc [] -- Imported class decls
+
+get_generics decl@(ClassDecl {tcdName = class_name, tcdMeths = Just def_methods, tcdLoc = loc})
| null groups
- = returnTc [] -- The comon case:
- -- no generic default methods, or
- -- its an imported class decl (=> has no methods at all)
+ = returnTc [] -- The comon case: no generic default methods
| otherwise -- A local class decl with generic default methods
= recoverNF_Tc (returnNF_Tc []) $
tcLookupClass class_name `thenTc` \ clas ->
-- Make an InstInfo out of each group
- mapTc (mkGenericInstance mod clas loc) groups `thenTc` \ inst_infos ->
+ mapTc (mkGenericInstance clas loc) groups `thenTc` \ inst_infos ->
-- Check that there is only one InstInfo for each type constructor
-- The main way this can fail is if you write
= plusAssoc_C AndMonoBinds (getGenericBinds m1) (getGenericBinds m2)
getGenericBinds (FunMonoBind id infixop matches loc)
- = mapAssoc wrap (foldr add emptyAssoc matches)
+ = mapAssoc wrap (foldl add emptyAssoc matches)
+ -- Using foldl not foldr is vital, else
+ -- we reverse the order of the bindings!
where
- add match env = case maybeGenericMatch match of
+ add env match = case maybeGenericMatch match of
Nothing -> env
Just (ty, match') -> extendAssoc_C (++) env (ty, [match'])
wrap ms = FunMonoBind id infixop ms loc
---------------------------------
-mkGenericInstance :: Module -> Class -> SrcLoc
+mkGenericInstance :: Class -> SrcLoc
-> (RenamedHsType, RenamedMonoBinds)
-> TcM InstInfo
-mkGenericInstance mod clas loc (hs_ty, binds)
+mkGenericInstance clas loc (hs_ty, binds)
-- Make a generic instance declaration
-- For example: instance (C a, C b) => C (a+b) where { binds }
(badGenericInstanceType binds) `thenTc_`
-- Make the dictionary function.
- newDFunName mod clas [inst_ty] loc `thenNF_Tc` \ dfun_name ->
+ newDFunName clas [inst_ty] loc `thenNF_Tc` \ dfun_name ->
let
inst_theta = [mkClassPred clas [mkTyVarTy tv] | tv <- tyvars]
inst_tys = [inst_ty]
methods_lie = plusLIEs insts_needed_s
in
- -- Ditto method bindings
+ -- Simplify the constraints from methods
tcAddErrCtxt methodCtxt (
tcSimplifyAndCheck
(ptext SLIT("instance declaration context"))
methods_lie
) `thenTc` \ (const_lie1, lie_binds1) ->
- -- Now do the simplification again, this time to get the
- -- bindings; this time we use an enhanced "avails"
- -- Ignore errors because they come from the *previous* tcSimplify
- discardErrsTc (
- tcSimplifyAndCheck
+ -- Figure out bindings for the superclass context
+ tcAddErrCtxt superClassCtxt (
+ tcSimplifyAndCheck
(ptext SLIT("instance declaration context"))
inst_tyvars_set
dfun_arg_dicts -- NB! Don't include this_dict here, else the sc_dicts
-- emit an error message. This in turn means that we don't
-- mention the constructor, which doesn't exist for CCallable, CReturnable
-- Hardly beautiful, but only three extra lines.
- HsApp (TyApp (HsVar eRROR_ID) [(unUsgTy . idType) this_dict_id])
+ HsApp (TyApp (HsVar eRROR_ID) [idType this_dict_id])
(HsLit (HsString msg))
| otherwise -- The common case
maybe_tycon_app = splitTyConApp_maybe first_inst_tau
Just (tycon, arg_tys) = maybe_tycon_app
- -- Stuff for an *algebraic* data type
- alg_tycon_app_maybe = splitAlgTyConApp_maybe first_inst_tau
- -- The "Alg" part looks through synonyms
- Just (alg_tycon, _, _) = alg_tycon_app_maybe
-
ccallable_type dflags ty = isFFIArgumentTy dflags False {- Not safe call -} ty
creturnable_type ty = isFFIResultTy ty
\end{code}
\begin{code}
tcAddDeclCtxt decl thing_inside
- = tcAddSrcLoc loc $
+ = tcAddSrcLoc (tcdLoc decl) $
tcAddErrCtxt ctxt $
thing_inside
where
- (name, loc, thing)
- = case decl of
- (ClassDecl _ name _ _ _ _ _ loc) -> (name, loc, "class")
- (TySynonym name _ _ loc) -> (name, loc, "type synonym")
- (TyData NewType _ name _ _ _ _ loc _ _) -> (name, loc, "newtype")
- (TyData DataType _ name _ _ _ _ loc _ _) -> (name, loc, "data type")
+ thing = case decl of
+ ClassDecl {} -> "class"
+ TySynonym {} -> "type synonym"
+ TyData {tcdND = NewType} -> "newtype"
+ TyData {tcdND = DataType} -> "data type"
ctxt = hsep [ptext SLIT("In the"), text thing,
- ptext SLIT("declaration for"), quotes (ppr name)]
+ ptext SLIT("declaration for"), quotes (ppr (tcdName decl))]
\end{code}
\begin{code}
ppr inst_ty])
methodCtxt = ptext SLIT("When checking the methods of an instance declaration")
+superClassCtxt = ptext SLIT("When checking the super-classes of an instance declaration")
\end{code}
-
-