2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcClassDcl]{Typechecking class declarations}
7 module TcClassDcl ( tcClassDecl1, tcClassDecls2,
8 MethodSpec, tcMethodBind, mkMethodBind, badMethodErr
11 #include "HsVersions.h"
13 import HsSyn ( TyClDecl(..), Sig(..), MonoBinds(..),
14 HsExpr(..), HsLit(..), Pat(WildPat),
15 mkSimpleMatch, andMonoBinds, andMonoBindList,
16 isClassOpSig, isPragSig,
19 import BasicTypes ( RecFlag(..), StrictnessMark(..) )
20 import RnHsSyn ( RenamedTyClDecl, RenamedSig,
21 RenamedClassOpSig, RenamedMonoBinds,
24 import RnEnv ( lookupSysName )
25 import TcHsSyn ( TcMonoBinds )
27 import Inst ( Inst, InstOrigin(..), instToId, newDicts, newMethod )
28 import TcEnv ( TyThingDetails(..),
29 tcLookupClass, tcExtendTyVarEnv2,
32 import TcTyDecls ( tcMkDataCon )
33 import TcBinds ( tcMonoBinds )
34 import TcMonoType ( TcSigInfo(..), tcHsType, tcHsTheta, mkTcSig )
35 import TcSimplify ( tcSimplifyCheck )
36 import TcUnify ( checkSigTyVars, sigCtxt )
37 import TcMType ( tcInstTyVars )
38 import TcType ( Type, TyVarDetails(..), TcType, TcThetaType, TcTyVar,
39 mkTyVarTys, mkPredTys, mkClassPred, tcSplitSigmaTy, tcSplitFunTys,
40 tcIsTyVarTy, tcSplitTyConApp_maybe, tcSplitForAllTys, tcSplitPhiTy,
41 getClassPredTys_maybe, mkPhiTy
44 import Generics ( mkGenericRhs )
45 import PrelInfo ( nO_METHOD_BINDING_ERROR_ID )
46 import Class ( classTyVars, classBigSig, classTyCon,
47 Class, ClassOpItem, DefMeth (..) )
48 import TyCon ( tyConGenInfo )
49 import Subst ( substTyWith )
50 import MkId ( mkDictSelId, mkDefaultMethodId )
51 import Id ( Id, idType, idName, mkUserLocal, setIdLocalExported, setInlinePragma )
52 import Name ( Name, NamedThing(..) )
53 import NameEnv ( NameEnv, lookupNameEnv, emptyNameEnv, unitNameEnv, plusNameEnv )
54 import NameSet ( emptyNameSet, unitNameSet )
55 import OccName ( mkClassTyConOcc, mkClassDataConOcc, mkWorkerOcc,
56 mkSuperDictSelOcc, reportIfUnused )
60 import UnicodeUtil ( stringToUtf8 )
61 import ErrUtils ( dumpIfSet )
62 import Util ( count, lengthIs, isSingleton )
63 import Maybes ( seqMaybe )
64 import Maybe ( isJust )
72 Every class implicitly declares a new data type, corresponding to dictionaries
73 of that class. So, for example:
75 class (D a) => C a where
77 op2 :: forall b. Ord b => a -> b -> b
79 would implicitly declare
81 data CDict a = CDict (D a)
83 (forall b. Ord b => a -> b -> b)
85 (We could use a record decl, but that means changing more of the existing apparatus.
88 For classes with just one superclass+method, we use a newtype decl instead:
91 op :: forallb. a -> b -> b
95 newtype CDict a = CDict (forall b. a -> b -> b)
97 Now DictTy in Type is just a form of type synomym:
98 DictTy c t = TyConTy CDict `AppTy` t
100 Death to "ExpandingDicts".
103 %************************************************************************
105 \subsection{Type checking}
107 %************************************************************************
111 tcClassDecl1 :: RenamedTyClDecl -> TcM (Name, TyThingDetails)
112 tcClassDecl1 (ClassDecl {tcdCtxt = context, tcdName = class_name,
113 tcdTyVars = tyvar_names, tcdFDs = fundeps,
114 tcdSigs = class_sigs, tcdMeths = def_methods,
116 = -- LOOK THINGS UP IN THE ENVIRONMENT
117 tcLookupClass class_name `thenM` \ clas ->
119 tyvars = classTyVars clas
120 op_sigs = filter isClassOpSig class_sigs
121 op_names = [n | ClassOpSig n _ _ _ <- op_sigs]
123 tcExtendTyVarEnv tyvars $
125 checkDefaultBinds clas op_names def_methods `thenM` \ mb_dm_env ->
128 -- The renamer has already checked that the context mentions
129 -- only the type variable of the class decl.
130 -- Context is already kind-checked
131 tcHsTheta context `thenM` \ sc_theta ->
133 -- CHECK THE CLASS SIGNATURES,
134 mappM (tcClassSig clas tyvars mb_dm_env) op_sigs `thenM` \ sig_stuff ->
136 -- MAKE THE CLASS DETAILS
137 lookupSysName class_name mkClassTyConOcc `thenM` \ tycon_name ->
138 lookupSysName class_name mkClassDataConOcc `thenM` \ datacon_name ->
139 mapM (lookupSysName class_name . mkSuperDictSelOcc)
140 [1..length context] `thenM` \ sc_sel_names ->
141 -- We number off the superclass selectors, 1, 2, 3 etc so that we
142 -- can construct names for the selectors. Thus
143 -- class (C a, C b) => D a b where ...
144 -- gives superclass selectors
146 -- (We used to call them D_C, but now we can have two different
147 -- superclasses both called C!)
149 (op_tys, op_items) = unzip sig_stuff
150 sc_tys = mkPredTys sc_theta
151 dict_component_tys = sc_tys ++ op_tys
152 sc_sel_ids = [mkDictSelId sc_name clas | sc_name <- sc_sel_names]
154 tcMkDataCon datacon_name
155 [{- No strictness -}]
156 [{- No labelled fields -}]
157 tyvars [{-No context-}]
158 [{-No existential tyvars-}] [{-Or context-}]
160 (classTyCon clas) `thenM` \ dict_con ->
162 returnM (class_name, ClassDetails sc_theta sc_sel_ids op_items dict_con tycon_name)
166 checkDefaultBinds :: Class -> [Name] -> Maybe RenamedMonoBinds
167 -> TcM (Maybe (NameEnv Bool))
168 -- The returned environment says
169 -- x not in env => no default method
170 -- x -> True => generic default method
171 -- x -> False => polymorphic default method
173 -- Check default bindings
174 -- a) must be for a class op for this class
175 -- b) must be all generic or all non-generic
176 -- and return a mapping from class-op to DefMeth info
178 -- But do all this only for source binds
180 checkDefaultBinds clas ops Nothing
183 checkDefaultBinds clas ops (Just mbs)
184 = go mbs `thenM` \ dm_env ->
185 returnM (Just dm_env)
187 go EmptyMonoBinds = returnM emptyNameEnv
189 go (AndMonoBinds b1 b2)
190 = go b1 `thenM` \ dm_info1 ->
191 go b2 `thenM` \ dm_info2 ->
192 returnM (dm_info1 `plusNameEnv` dm_info2)
194 go (FunMonoBind op _ matches loc)
197 -- Check that the op is from this class
198 checkTc (op `elem` ops) (badMethodErr clas op) `thenM_`
200 -- Check that all the defns ar generic, or none are
201 checkTc (all_generic || none_generic) (mixedGenericErr op) `thenM_`
203 returnM (unitNameEnv op all_generic)
205 n_generic = count (isJust . maybeGenericMatch) matches
206 none_generic = n_generic == 0
207 all_generic = matches `lengthIs` n_generic
212 tcClassSig :: Class -- ...ditto...
213 -> [TyVar] -- The class type variable, used for error check only
214 -> Maybe (NameEnv Bool) -- Info about default methods;
215 -- Nothing => imported class defn with no method binds
217 -> TcM (Type, -- Type of the method
218 ClassOpItem) -- Selector Id, default-method Id, True if explicit default binding
220 -- This warrants an explanation: we need to separate generic
221 -- default methods and default methods later on in the compiler
222 -- so we distinguish them in checkDefaultBinds, and pass this knowledge in the
223 -- Class.DefMeth data structure.
225 tcClassSig clas clas_tyvars maybe_dm_env
226 (ClassOpSig op_name sig_dm op_ty src_loc)
227 = addSrcLoc src_loc $
229 -- Check the type signature. NB that the envt *already has*
230 -- bindings for the type variables; see comments in TcTyAndClassDcls.
231 tcHsType op_ty `thenM` \ local_ty ->
234 theta = [mkClassPred clas (mkTyVarTys clas_tyvars)]
236 -- Build the selector id and default method id
237 sel_id = mkDictSelId op_name clas
238 DefMeth dm_name = sig_dm
240 dm_info = case maybe_dm_env of
242 Just dm_env -> mk_src_dm_info dm_env
244 mk_src_dm_info dm_env = case lookupNameEnv dm_env op_name of
246 Just True -> GenDefMeth
247 Just False -> DefMeth dm_name
249 returnM (local_ty, (sel_id, dm_info))
253 %************************************************************************
255 \subsection[Default methods]{Default methods}
257 %************************************************************************
259 The default methods for a class are each passed a dictionary for the
260 class, so that they get access to the other methods at the same type.
261 So, given the class decl
265 op2 :: Ord b => a -> b -> b -> b
268 op2 x y z = if (op1 x) && (y < z) then y else z
270 we get the default methods:
272 defm.Foo.op1 :: forall a. Foo a => a -> Bool
273 defm.Foo.op1 = /\a -> \dfoo -> \x -> True
275 defm.Foo.op2 :: forall a. Foo a => forall b. Ord b => a -> b -> b -> b
276 defm.Foo.op2 = /\ a -> \ dfoo -> /\ b -> \ dord -> \x y z ->
277 if (op1 a dfoo x) && (< b dord y z) then y else z
280 When we come across an instance decl, we may need to use the default
283 instance Foo Int where {}
287 const.Foo.Int.op1 :: Int -> Bool
288 const.Foo.Int.op1 = defm.Foo.op1 Int dfun.Foo.Int
290 const.Foo.Int.op2 :: forall b. Ord b => Int -> b -> b -> b
291 const.Foo.Int.op2 = defm.Foo.op2 Int dfun.Foo.Int
293 dfun.Foo.Int :: Foo Int
294 dfun.Foo.Int = (const.Foo.Int.op1, const.Foo.Int.op2)
296 Notice that, as with method selectors above, we assume that dictionary
297 application is curried, so there's no need to mention the Ord dictionary
298 in const.Foo.Int.op2 (or the type variable).
301 instance Foo a => Foo [a] where {}
303 dfun.Foo.List :: forall a. Foo a -> Foo [a]
305 = /\ a -> \ dfoo_a ->
307 op1 = defm.Foo.op1 [a] dfoo_list
308 op2 = defm.Foo.op2 [a] dfoo_list
309 dfoo_list = (op1, op2)
314 The function @tcClassDecls2@ just arranges to apply @tcClassDecl2@ to
315 each local class decl.
318 tcClassDecls2 :: [RenamedTyClDecl] -> TcM (TcMonoBinds, [Id])
322 (returnM (EmptyMonoBinds, []))
323 [tcClassDecl2 cls_decl | cls_decl@(ClassDecl {tcdMeths = Just _}) <- decls]
324 -- The 'Just' picks out source ClassDecls
326 combine tc1 tc2 = tc1 `thenM` \ (binds1, ids1) ->
327 tc2 `thenM` \ (binds2, ids2) ->
328 returnM (binds1 `AndMonoBinds` binds2,
332 @tcClassDecl2@ generates bindings for polymorphic default methods
333 (generic default methods have by now turned into instance declarations)
336 tcClassDecl2 :: RenamedTyClDecl -- The class declaration
337 -> TcM (TcMonoBinds, [Id])
339 tcClassDecl2 (ClassDecl {tcdName = class_name, tcdSigs = sigs,
340 tcdMeths = Just default_binds, tcdLoc = src_loc})
341 = -- The 'Just' picks out source ClassDecls
342 recoverM (returnM (EmptyMonoBinds, [])) $
344 tcLookupClass class_name `thenM` \ clas ->
346 -- We make a separate binding for each default method.
347 -- At one time I used a single AbsBinds for all of them, thus
348 -- AbsBind [d] [dm1, dm2, dm3] { dm1 = ...; dm2 = ...; dm3 = ... }
349 -- But that desugars into
350 -- ds = \d -> (..., ..., ...)
351 -- dm1 = \d -> case ds d of (a,b,c) -> a
352 -- And since ds is big, it doesn't get inlined, so we don't get good
353 -- default methods. Better to make separate AbsBinds for each
355 (tyvars, _, _, op_items) = classBigSig clas
356 prags = filter isPragSig sigs
357 tc_dm = tcDefMeth clas tyvars default_binds prags
359 mapAndUnzipM tc_dm op_items `thenM` \ (defm_binds, dm_ids_s) ->
361 returnM (andMonoBindList defm_binds, concat dm_ids_s)
364 tcDefMeth clas tyvars binds_in prags (_, NoDefMeth) = returnM (EmptyMonoBinds, [])
365 tcDefMeth clas tyvars binds_in prags (_, GenDefMeth) = returnM (EmptyMonoBinds, [])
366 -- Generate code for polymorphic default methods only
367 -- (Generic default methods have turned into instance decls by now.)
368 -- This is incompatible with Hugs, which expects a polymorphic
369 -- default method for every class op, regardless of whether or not
370 -- the programmer supplied an explicit default decl for the class.
371 -- (If necessary we can fix that, but we don't have a convenient Id to hand.)
373 tcDefMeth clas tyvars binds_in prags op_item@(sel_id, DefMeth dm_name)
374 = tcInstTyVars ClsTv tyvars `thenM` \ (clas_tyvars, inst_tys, _) ->
376 dm_ty = idType sel_id -- Same as dict selector!
377 -- The default method's type should really come from the
378 -- iface file, since it could be usage-generalised, but this
379 -- requires altering the mess of knots in TcModule and I'm
380 -- too scared to do that. Instead, I have disabled generalisation
381 -- of types of default methods (and dict funs) by annotating them
382 -- TyGenNever (in MkId). Ugh! KSW 1999-09.
384 theta = [mkClassPred clas inst_tys]
385 local_dm_id = mkDefaultMethodId dm_name dm_ty
386 xtve = tyvars `zip` clas_tyvars
388 newDicts origin theta `thenM` \ [this_dict] ->
390 mkMethodBind origin clas inst_tys binds_in op_item `thenM` \ (_, meth_info) ->
391 getLIE (tcMethodBind xtve clas_tyvars theta
392 [this_dict] prags meth_info) `thenM` \ (defm_bind, insts_needed) ->
394 addErrCtxt (defltMethCtxt clas) $
398 (ptext SLIT("class") <+> ppr clas)
401 insts_needed `thenM` \ dict_binds ->
403 -- Simplification can do unification
404 checkSigTyVars clas_tyvars `thenM` \ clas_tyvars' ->
407 (_,dm_inst_id,_) = meth_info
411 [(clas_tyvars', local_dm_id, dm_inst_id)]
412 emptyNameSet -- No inlines (yet)
413 (dict_binds `andMonoBinds` defm_bind)
415 returnM (full_bind, [local_dm_id])
417 origin = ClassDeclOrigin
422 %************************************************************************
424 \subsection{Typechecking a method}
426 %************************************************************************
428 @tcMethodBind@ is used to type-check both default-method and
429 instance-decl method declarations. We must type-check methods one at a
430 time, because their signatures may have different contexts and
434 type MethodSpec = (Id, -- Global selector Id
435 Id, -- Local Id (class tyvars instantiated)
436 RenamedMonoBinds) -- Binding for the method
439 :: [(TyVar,TcTyVar)] -- Bindings for type environment
440 -> [TcTyVar] -- Instantiated type variables for the
441 -- enclosing class/instance decl.
442 -- They'll be signature tyvars, and we
443 -- want to check that they don't get bound
444 -- Always equal the range of the type envt
445 -> TcThetaType -- Available theta; it's just used for the error message
446 -> [Inst] -- Available from context, used to simplify constraints
447 -- from the method body
448 -> [RenamedSig] -- Pragmas (e.g. inline pragmas)
449 -> MethodSpec -- Details of this method
452 tcMethodBind xtve inst_tyvars inst_theta avail_insts prags
453 (sel_id, meth_id, meth_bind)
454 = -- Check the bindings; first adding inst_tyvars to the envt
455 -- so that we don't quantify over them in nested places
456 mkTcSig meth_id `thenM` \ meth_sig ->
458 tcExtendTyVarEnv2 xtve (
459 addErrCtxt (methodCtxt sel_id) $
460 getLIE (tcMonoBinds meth_bind [meth_sig] NonRecursive)
461 ) `thenM` \ ((meth_bind, _, _), meth_lie) ->
463 -- Now do context reduction. We simplify wrt both the local tyvars
464 -- and the ones of the class/instance decl, so that there is
467 -- op :: Eq a => a -> b -> a
469 -- We do this for each method independently to localise error messages
472 TySigInfo meth_id meth_tvs meth_theta _ local_meth_id _ _ = meth_sig
474 addErrCtxtM (sigCtxt sel_id inst_tyvars inst_theta (idType meth_id)) $
475 newDicts SignatureOrigin meth_theta `thenM` \ meth_dicts ->
477 all_tyvars = meth_tvs ++ inst_tyvars
478 all_insts = avail_insts ++ meth_dicts
481 (ptext SLIT("class or instance method") <+> quotes (ppr sel_id))
482 all_tyvars all_insts meth_lie `thenM` \ lie_binds ->
484 checkSigTyVars all_tyvars `thenM` \ all_tyvars' ->
487 -- Attach inline pragmas as appropriate
488 (final_meth_id, inlines)
489 | (InlineSig inl _ phase _ : _) <- filter is_inline prags
490 = (meth_id `setInlinePragma` phase,
491 if inl then unitNameSet (idName meth_id) else emptyNameSet)
493 = (meth_id, emptyNameSet)
495 is_inline (InlineSig _ name _ _) = name == idName sel_id
496 is_inline other = False
498 meth_tvs' = take (length meth_tvs) all_tyvars'
499 poly_meth_bind = AbsBinds meth_tvs'
500 (map instToId meth_dicts)
501 [(meth_tvs', final_meth_id, local_meth_id)]
503 (lie_binds `andMonoBinds` meth_bind)
505 returnM poly_meth_bind
508 mkMethodBind :: InstOrigin
509 -> Class -> [TcType] -- Class and instance types
510 -> RenamedMonoBinds -- Method binding (pick the right one from in here)
512 -> TcM (Maybe Inst, -- Method inst
514 -- Find the binding for the specified method, or make
515 -- up a suitable default method if it isn't there
517 mkMethodBind origin clas inst_tys meth_binds (sel_id, dm_info)
518 = mkMethId origin clas sel_id inst_tys `thenM` \ (mb_inst, meth_id) ->
520 meth_name = idName meth_id
522 -- Figure out what method binding to use
523 -- If the user suppplied one, use it, else construct a default one
524 getSrcLocM `thenM` \ loc ->
525 (case find_bind (idName sel_id) meth_name meth_binds of
526 Just user_bind -> returnM user_bind
527 Nothing -> mkDefMethRhs origin clas inst_tys sel_id loc dm_info `thenM` \ rhs ->
528 returnM (FunMonoBind meth_name False -- Not infix decl
529 [mkSimpleMatch [] rhs placeHolderType loc] loc)
530 ) `thenM` \ meth_bind ->
532 returnM (mb_inst, (sel_id, meth_id, meth_bind))
534 mkMethId :: InstOrigin -> Class
535 -> Id -> [TcType] -- Selector, and instance types
536 -> TcM (Maybe Inst, Id)
538 -- mkMethId instantiates the selector Id at the specified types
540 mkMethId origin clas sel_id inst_tys
542 (tyvars,rho) = tcSplitForAllTys (idType sel_id)
543 rho_ty = ASSERT( length tyvars == length inst_tys )
544 substTyWith tyvars inst_tys rho
545 (preds,tau) = tcSplitPhiTy rho_ty
546 first_pred = head preds
548 -- The first predicate should be of form (C a b)
549 -- where C is the class in question
550 ASSERT( not (null preds) &&
551 case getClassPredTys_maybe first_pred of
552 { Just (clas1,tys) -> clas == clas1 ; Nothing -> False }
554 if isSingleton preds then
555 -- If it's the only one, make a 'method'
556 getInstLoc origin `thenM` \ inst_loc ->
557 newMethod inst_loc sel_id inst_tys preds tau `thenM` \ meth_inst ->
558 returnM (Just meth_inst, instToId meth_inst)
560 -- If it's not the only one we need to be careful
561 -- For example, given 'op' defined thus:
563 -- op :: (?x :: String) => a -> a
564 -- (mkMethId op T) should return an Inst with type
565 -- (?x :: String) => T -> T
566 -- That is, the class-op's context is still there.
567 -- BUT: it can't be a Method any more, because it breaks
568 -- INVARIANT 2 of methods. (See the data decl for Inst.)
569 newUnique `thenM` \ uniq ->
570 getSrcLocM `thenM` \ loc ->
572 real_tau = mkPhiTy (tail preds) tau
573 meth_id = mkUserLocal (getOccName sel_id) uniq real_tau loc
575 returnM (Nothing, meth_id)
577 -- The user didn't supply a method binding,
578 -- so we have to make up a default binding
579 -- The RHS of a default method depends on the default-method info
580 mkDefMethRhs origin clas inst_tys sel_id loc (DefMeth dm_name)
581 = -- An polymorphic default method
582 traceRn (text "mkDefMeth" <+> ppr dm_name) `thenM_`
583 returnM (HsVar dm_name)
585 mkDefMethRhs origin clas inst_tys sel_id loc NoDefMeth
586 = -- No default method
587 -- Warn only if -fwarn-missing-methods
588 doptM Opt_WarnMissingMethods `thenM` \ warn ->
589 warnTc (isInstDecl origin
591 && reportIfUnused (getOccName sel_id))
592 (omittedMethodWarn sel_id) `thenM_`
595 error_rhs = HsLam (mkSimpleMatch wild_pats simple_rhs placeHolderType loc)
596 simple_rhs = HsApp (HsVar (getName nO_METHOD_BINDING_ERROR_ID))
597 (HsLit (HsStringPrim (mkFastString (stringToUtf8 error_msg))))
598 error_msg = showSDoc (hcat [ppr loc, text "|", ppr sel_id ])
600 -- When the type is of form t1 -> t2 -> t3
601 -- make a default method like (\ _ _ -> noMethBind "blah")
602 -- rather than simply (noMethBind "blah")
603 -- Reason: if t1 or t2 are higher-ranked types we get n
604 -- silly ambiguity messages.
605 -- Example: f :: (forall a. Eq a => a -> a) -> Int
607 -- Here, tcSub tries to force (error "urk") to have the right type,
608 -- thus: f = \(x::forall a. Eq a => a->a) -> error "urk" (x t)
609 -- where 't' is fresh ty var. This leads directly to "ambiguous t".
611 -- NB: technically this changes the meaning of the default-default
612 -- method slightly, because `seq` can see the lambdas. Oh well.
613 (_,_,tau1) = tcSplitSigmaTy (idType sel_id)
614 (_,_,tau2) = tcSplitSigmaTy tau1
615 -- Need two splits because the selector can have a type like
616 -- forall a. Foo a => forall b. Eq b => ...
617 (arg_tys, _) = tcSplitFunTys tau2
618 wild_pats = [WildPat placeHolderType | ty <- arg_tys]
620 mkDefMethRhs origin clas inst_tys sel_id loc GenDefMeth
621 = -- A generic default method
622 -- If the method is defined generically, we can only do the job if the
623 -- instance declaration is for a single-parameter type class with
624 -- a type constructor applied to type arguments in the instance decl
625 -- (checkTc, so False provokes the error)
626 ASSERT( isInstDecl origin ) -- We never get here from a class decl
628 checkTc (isJust maybe_tycon)
629 (badGenericInstance sel_id (notSimple inst_tys)) `thenM_`
630 checkTc (isJust (tyConGenInfo tycon))
631 (badGenericInstance sel_id (notGeneric tycon)) `thenM_`
633 ioToTcRn (dumpIfSet opt_PprStyle_Debug "Generic RHS" stuff) `thenM_`
636 rhs = mkGenericRhs sel_id clas_tyvar tycon
638 stuff = vcat [ppr clas <+> ppr inst_tys,
639 nest 4 (ppr sel_id <+> equals <+> ppr rhs)]
641 -- The tycon is only used in the generic case, and in that
642 -- case we require that the instance decl is for a single-parameter
643 -- type class with type variable arguments:
644 -- instance (...) => C (T a b)
645 clas_tyvar = head (classTyVars clas)
646 Just tycon = maybe_tycon
647 maybe_tycon = case inst_tys of
648 [ty] -> case tcSplitTyConApp_maybe ty of
649 Just (tycon, arg_tys) | all tcIsTyVarTy arg_tys -> Just tycon
653 isInstDecl InstanceDeclOrigin = True
654 isInstDecl ClassDeclOrigin = False
659 -- The renamer just puts the selector ID as the binder in the method binding
660 -- but we must use the method name; so we substitute it here. Crude but simple.
661 find_bind sel_name meth_name (FunMonoBind op_name fix matches loc)
662 | op_name == sel_name = Just (FunMonoBind meth_name fix matches loc)
663 find_bind sel_name meth_name (AndMonoBinds b1 b2)
664 = find_bind sel_name meth_name b1 `seqMaybe` find_bind sel_name meth_name b2
665 find_bind sel_name meth_name other = Nothing -- Default case
667 -- Find the prags for this method, and replace the
668 -- selector name with the method name
669 find_prags sel_name meth_name [] = []
670 find_prags sel_name meth_name (SpecSig name ty loc : prags)
671 | name == sel_name = SpecSig meth_name ty loc : find_prags sel_name meth_name prags
672 find_prags sel_name meth_name (InlineSig sense name phase loc : prags)
673 | name == sel_name = InlineSig sense meth_name phase loc : find_prags sel_name meth_name prags
674 find_prags sel_name meth_name (prag:prags) = find_prags sel_name meth_name prags
682 = ptext SLIT("When checking the default methods for class") <+> quotes (ppr clas)
685 = ptext SLIT("In the definition for method") <+> quotes (ppr sel_id)
688 = hsep [ptext SLIT("Class"), quotes (ppr clas),
689 ptext SLIT("does not have a method"), quotes (ppr op)]
691 omittedMethodWarn sel_id
692 = ptext SLIT("No explicit method nor default method for") <+> quotes (ppr sel_id)
694 badGenericInstance sel_id because
695 = sep [ptext SLIT("Can't derive generic code for") <+> quotes (ppr sel_id),
699 = vcat [ptext SLIT("because the instance type(s)"),
700 nest 2 (ppr inst_tys),
701 ptext SLIT("is not a simple type of form (T a b c)")]
704 = vcat [ptext SLIT("because the instance type constructor") <+> quotes (ppr tycon) <+>
705 ptext SLIT("was not compiled with -fgenerics")]
708 = ptext SLIT("Can't mix generic and non-generic equations for class method") <+> quotes (ppr op)