\section[TcClassDcl]{Typechecking class declarations}
\begin{code}
-module TcClassDcl ( tcClassDecl1, tcClassDecls2, mkImplicitClassBinds,
+module TcClassDcl ( tcClassDecl1, tcClassDecls2,
tcMethodBind, badMethodErr
) where
#include "HsVersions.h"
-import HsSyn ( HsDecl(..), TyClDecl(..), Sig(..), MonoBinds(..),
+import HsSyn ( TyClDecl(..), Sig(..), MonoBinds(..),
HsExpr(..), HsLit(..), HsType(..), HsPred(..),
mkSimpleMatch, andMonoBinds, andMonoBindList,
isClassDecl, isClassOpSig, isPragSig,
import BasicTypes ( TopLevelFlag(..), RecFlag(..) )
import RnHsSyn ( RenamedTyClDecl,
RenamedClassOpSig, RenamedMonoBinds,
- RenamedContext, RenamedHsDecl, RenamedSig,
+ RenamedContext, RenamedSig,
maybeGenericMatch
)
-import TcHsSyn ( TcMonoBinds, idsToMonoBinds )
+import TcHsSyn ( TcMonoBinds )
import Inst ( InstOrigin(..), LIE, emptyLIE, plusLIE, plusLIEs,
newDicts, newMethod )
tcExtendLocalValEnv, tcExtendTyVarEnv, newDefaultMethodName
)
import TcBinds ( tcBindWithSigs, tcSpecSigs )
-import TcMonoType ( tcHsSigType, tcClassContext, checkSigTyVars, checkAmbiguity, sigCtxt, mkTcSig )
+import TcMonoType ( tcHsRecType, tcRecClassContext, checkSigTyVars, checkAmbiguity, sigCtxt, mkTcSig )
import TcSimplify ( tcSimplifyAndCheck, bindInstsOfLocalFuns )
import TcType ( TcType, TcTyVar, tcInstTyVars, zonkTcSigTyVars )
import TcMonad
import Generics ( mkGenericRhs, validGenericMethodType )
import PrelInfo ( nO_METHOD_BINDING_ERROR_ID )
-import Class ( classTyVars, classBigSig, classSelIds, classTyCon, classTvsFds,
- Class, ClassOpItem, DefMeth (..), FunDep )
+import Class ( classTyVars, classBigSig, classTyCon,
+ Class, ClassOpItem, DefMeth (..) )
import MkId ( mkDictSelId, mkDataConId, mkDataConWrapId, mkDefaultMethodId )
import DataCon ( mkDataCon, notMarkedStrict )
import Id ( Id, idType, idName )
%************************************************************************
\begin{code}
-tcClassDecl1 :: RecTcEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
-tcClassDecl1 rec_env
+
+tcClassDecl1 :: RecFlag -> RecTcEnv -> RenamedTyClDecl -> TcM (Name, TyThingDetails)
+tcClassDecl1 is_rec rec_env
(ClassDecl context class_name
tyvar_names fundeps class_sigs def_methods
sys_names src_loc)
-- LOOK THINGS UP IN THE ENVIRONMENT
tcLookupClass class_name `thenTc` \ clas ->
let
- (tyvars, fds) = classTvsFds clas
+ tyvars = classTyVars clas
op_sigs = filter isClassOpSig class_sigs
op_names = [n | ClassOpSig n _ _ _ <- op_sigs]
(_, datacon_name, datacon_wkr_name, sc_sel_names) = getClassDeclSysNames sys_names
checkGenericClassIsUnary clas dm_info `thenTc_`
-- CHECK THE CONTEXT
- tcSuperClasses clas context sc_sel_names `thenTc` \ (sc_theta, sc_sel_ids) ->
+ tcSuperClasses is_rec clas context sc_sel_names `thenTc` \ (sc_theta, sc_sel_ids) ->
-- CHECK THE CLASS SIGNATURES,
- mapTc (tcClassSig rec_env clas tyvars fds dm_info)
- op_sigs `thenTc` \ sig_stuff ->
+ mapTc (tcClassSig is_rec rec_env clas tyvars dm_info) op_sigs `thenTc` \ sig_stuff ->
-- MAKE THE CLASS DETAILS
let
\begin{code}
-tcSuperClasses :: Class
+tcSuperClasses :: RecFlag -> Class
-> RenamedContext -- class context
-> [Name] -- Names for superclass selectors
-> TcM (ClassContext, -- the superclass context
[Id]) -- superclass selector Ids
-tcSuperClasses clas context sc_sel_names
+tcSuperClasses is_rec clas context sc_sel_names
= -- Check the context.
-- The renamer has already checked that the context mentions
-- only the type variable of the class decl.
) `thenTc_`
-- Context is already kind-checked
- tcClassContext context `thenTc` \ sc_theta ->
+ tcRecClassContext is_rec context `thenTc` \ sc_theta ->
let
sc_sel_ids = [mkDictSelId sc_name clas | sc_name <- sc_sel_names]
in
is_tyvar other = False
-tcClassSig :: RecTcEnv
+tcClassSig :: RecFlag -> RecTcEnv -- Knot tying only!
-> Class -- ...ditto...
-> [TyVar] -- The class type variable, used for error check only
- -> [FunDep TyVar]
-> NameEnv (DefMeth Name) -- Info about default methods
-> RenamedClassOpSig
-> TcM (Type, -- Type of the method
-- so we distinguish them in checkDefaultBinds, and pass this knowledge in the
-- Class.DefMeth data structure.
-tcClassSig unf_env clas clas_tyvars fds dm_info
+tcClassSig is_rec unf_env clas clas_tyvars dm_info
(ClassOpSig op_name maybe_dm op_ty src_loc)
= tcAddSrcLoc src_loc $
-- Check the type signature. NB that the envt *already has*
-- bindings for the type variables; see comments in TcTyAndClassDcls.
- tcHsSigType op_ty `thenTc` \ local_ty ->
+ tcHsRecType is_rec op_ty `thenTc` \ local_ty ->
+
+ -- Check for ambiguous class op types
let
theta = [mkClassPred clas (mkTyVarTys clas_tyvars)]
in
- -- Check for ambiguous class op types
- checkAmbiguity True clas_tyvars theta local_ty `thenTc` \ global_ty ->
+ checkAmbiguity is_rec True clas_tyvars theta local_ty `thenTc` \ global_ty ->
+ -- The default method's type should really come from the
+ -- iface file, since it could be usage-generalised, but this
+ -- requires altering the mess of knots in TcModule and I'm
+ -- too scared to do that. Instead, I have disabled generalisation
+ -- of types of default methods (and dict funs) by annotating them
+ -- TyGenNever (in MkId). Ugh! KSW 1999-09.
let
-- Build the selector id and default method id
%************************************************************************
%* *
-\subsection[ClassDcl-pass2]{Class decls pass 2: default methods}
-%* *
-%************************************************************************
-
-@mkImplicitClassBinds@ produces a binding for the selector function for each method
-and superclass dictionary.
-
-\begin{code}
-mkImplicitClassBinds :: Module -> [Class] -> NF_TcM ([Id], TcMonoBinds)
-mkImplicitClassBinds this_mod classes
- = returnNF_Tc (concat cls_ids_s, andMonoBindList binds_s)
- -- The selector binds are already in the selector Id's unfoldings
- -- We don't return the data constructor etc from the class,
- -- because that's done via the class's TyCon
- where
- (cls_ids_s, binds_s) = unzip (map mk_implicit classes)
-
- mk_implicit clas = (sel_ids, binds)
- where
- sel_ids = classSelIds clas
- binds | isFrom this_mod clas = idsToMonoBinds sel_ids
- | otherwise = EmptyMonoBinds
-\end{code}
-
-
-
-%************************************************************************
-%* *
\subsection[Default methods]{Default methods}
%* *
%************************************************************************
each local class decl.
\begin{code}
-tcClassDecls2 :: Module -> [RenamedHsDecl] -> NF_TcM (LIE, TcMonoBinds)
+tcClassDecls2 :: Module -> [RenamedTyClDecl] -> NF_TcM (LIE, TcMonoBinds)
tcClassDecls2 this_mod decls
= foldr combine
(returnNF_Tc (emptyLIE, EmptyMonoBinds))
- [tcClassDecl2 cls_decl | TyClD cls_decl <- decls,
+ [tcClassDecl2 cls_decl | cls_decl <- decls,
isClassDecl cls_decl,
isFrom this_mod (tyClDeclName cls_decl)]
where