[project @ 2001-12-06 10:45:42 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcDeriv.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[TcDeriv]{Deriving}
5
6 Handles @deriving@ clauses on @data@ declarations.
7
8 \begin{code}
9 module TcDeriv ( tcDeriving ) where
10
11 #include "HsVersions.h"
12
13 import HsSyn            ( HsBinds(..), MonoBinds(..), TyClDecl(..),
14                           collectLocatedMonoBinders )
15 import RdrHsSyn         ( RdrNameMonoBinds )
16 import RnHsSyn          ( RenamedHsBinds, RenamedMonoBinds, RenamedTyClDecl )
17 import CmdLineOpts      ( DynFlag(..), DynFlags )
18
19 import TcMonad
20 import TcEnv            ( tcSetInstEnv, newDFunName, InstInfo(..), pprInstInfo,
21                           tcLookupClass, tcLookupTyCon
22                         )
23 import TcGenDeriv       -- Deriv stuff
24 import InstEnv          ( InstEnv, simpleDFunClassTyCon, extendInstEnv )
25 import TcSimplify       ( tcSimplifyThetas )
26
27 import RnBinds          ( rnMethodBinds, rnTopMonoBinds )
28 import RnEnv            ( bindLocatedLocalsRn )
29 import RnMonad          ( renameDerivedCode, thenRn, mapRn, returnRn )
30 import HscTypes         ( DFunId, PersistentRenamerState )
31
32 import BasicTypes       ( Fixity )
33 import Class            ( className, classKey, Class )
34 import ErrUtils         ( dumpIfSet_dyn, Message )
35 import MkId             ( mkDictFunId )
36 import DataCon          ( dataConArgTys, isNullaryDataCon, isExistentialDataCon )
37 import PrelInfo         ( needsDataDeclCtxtClassKeys )
38 import Maybes           ( maybeToBool, catMaybes )
39 import Module           ( Module )
40 import Name             ( Name, getSrcLoc, nameUnique )
41 import RdrName          ( RdrName )
42
43 import TyCon            ( tyConTyVars, tyConDataCons,
44                           tyConTheta, maybeTyConSingleCon, isDataTyCon,
45                           isEnumerationTyCon, TyCon
46                         )
47 import TcType           ( ThetaType, mkTyVarTys, mkTyConApp, 
48                           isUnLiftedType, mkClassPred )
49 import Var              ( TyVar )
50 import PrelNames
51 import Util             ( zipWithEqual, sortLt )
52 import ListSetOps       ( removeDups,  assoc )
53 import Outputable
54 import List             ( nub )
55 \end{code}
56
57 %************************************************************************
58 %*                                                                      *
59 \subsection[TcDeriv-intro]{Introduction to how we do deriving}
60 %*                                                                      *
61 %************************************************************************
62
63 Consider
64
65         data T a b = C1 (Foo a) (Bar b)
66                    | C2 Int (T b a)
67                    | C3 (T a a)
68                    deriving (Eq)
69
70 [NOTE: See end of these comments for what to do with 
71         data (C a, D b) => T a b = ...
72 ]
73
74 We want to come up with an instance declaration of the form
75
76         instance (Ping a, Pong b, ...) => Eq (T a b) where
77                 x == y = ...
78
79 It is pretty easy, albeit tedious, to fill in the code "...".  The
80 trick is to figure out what the context for the instance decl is,
81 namely @Ping@, @Pong@ and friends.
82
83 Let's call the context reqd for the T instance of class C at types
84 (a,b, ...)  C (T a b).  Thus:
85
86         Eq (T a b) = (Ping a, Pong b, ...)
87
88 Now we can get a (recursive) equation from the @data@ decl:
89
90         Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
91                    u Eq (T b a) u Eq Int        -- From C2
92                    u Eq (T a a)                 -- From C3
93
94 Foo and Bar may have explicit instances for @Eq@, in which case we can
95 just substitute for them.  Alternatively, either or both may have
96 their @Eq@ instances given by @deriving@ clauses, in which case they
97 form part of the system of equations.
98
99 Now all we need do is simplify and solve the equations, iterating to
100 find the least fixpoint.  Notice that the order of the arguments can
101 switch around, as here in the recursive calls to T.
102
103 Let's suppose Eq (Foo a) = Eq a, and Eq (Bar b) = Ping b.
104
105 We start with:
106
107         Eq (T a b) = {}         -- The empty set
108
109 Next iteration:
110         Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
111                    u Eq (T b a) u Eq Int        -- From C2
112                    u Eq (T a a)                 -- From C3
113
114         After simplification:
115                    = Eq a u Ping b u {} u {} u {}
116                    = Eq a u Ping b
117
118 Next iteration:
119
120         Eq (T a b) = Eq (Foo a) u Eq (Bar b)    -- From C1
121                    u Eq (T b a) u Eq Int        -- From C2
122                    u Eq (T a a)                 -- From C3
123
124         After simplification:
125                    = Eq a u Ping b
126                    u (Eq b u Ping a)
127                    u (Eq a u Ping a)
128
129                    = Eq a u Ping b u Eq b u Ping a
130
131 The next iteration gives the same result, so this is the fixpoint.  We
132 need to make a canonical form of the RHS to ensure convergence.  We do
133 this by simplifying the RHS to a form in which
134
135         - the classes constrain only tyvars
136         - the list is sorted by tyvar (major key) and then class (minor key)
137         - no duplicates, of course
138
139 So, here are the synonyms for the ``equation'' structures:
140
141 \begin{code}
142 type DerivEqn = (Name, Class, TyCon, [TyVar], DerivRhs)
143                 -- The Name is the name for the DFun we'll build
144                 -- The tyvars bind all the variables in the RHS
145
146 type DerivRhs  = ThetaType
147 type DerivSoln = DerivRhs
148 \end{code}
149
150
151 A note about contexts on data decls
152 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153 Consider
154
155         data (RealFloat a) => Complex a = !a :+ !a deriving( Read )
156
157 We will need an instance decl like:
158
159         instance (Read a, RealFloat a) => Read (Complex a) where
160           ...
161
162 The RealFloat in the context is because the read method for Complex is bound
163 to construct a Complex, and doing that requires that the argument type is
164 in RealFloat. 
165
166 But this ain't true for Show, Eq, Ord, etc, since they don't construct
167 a Complex; they only take them apart.
168
169 Our approach: identify the offending classes, and add the data type
170 context to the instance decl.  The "offending classes" are
171
172         Read, Enum?
173
174
175 %************************************************************************
176 %*                                                                      *
177 \subsection[TcDeriv-driver]{Top-level function for \tr{derivings}}
178 %*                                                                      *
179 %************************************************************************
180
181 \begin{code}
182 tcDeriving  :: PersistentRenamerState
183             -> Module                   -- name of module under scrutiny
184             -> InstEnv                  -- What we already know about instances
185             -> (Name -> Maybe Fixity)   -- used in deriving Show and Read
186             -> [RenamedTyClDecl]        -- All type constructors
187             -> TcM ([InstInfo],         -- The generated "instance decls".
188                     RenamedHsBinds)     -- Extra generated bindings
189
190 tcDeriving prs mod inst_env_in get_fixity tycl_decls
191   = recoverTc (returnTc ([], EmptyBinds)) $
192
193         -- Fish the "deriving"-related information out of the TcEnv
194         -- and make the necessary "equations".
195     makeDerivEqns tycl_decls            `thenTc` \ eqns ->
196     if null eqns then
197         returnTc ([], EmptyBinds)
198     else
199
200         -- Take the equation list and solve it, to deliver a list of
201         -- solutions, a.k.a. the contexts for the instance decls
202         -- required for the corresponding equations.
203     solveDerivEqns inst_env_in eqns             `thenTc` \ new_dfuns ->
204
205         -- Now augment the InstInfos, adding in the rather boring
206         -- actual-code-to-do-the-methods binds.  We may also need to
207         -- generate extra not-one-inst-decl-specific binds, notably
208         -- "con2tag" and/or "tag2con" functions.  We do these
209         -- separately.
210
211     gen_taggery_Names new_dfuns                 `thenTc` \ nm_alist_etc ->
212
213     tcGetEnv                                    `thenNF_Tc` \ env ->
214     getDOptsTc                                  `thenTc` \ dflags ->
215     let
216         extra_mbind_list = map gen_tag_n_con_monobind nm_alist_etc
217         extra_mbinds     = foldr AndMonoBinds EmptyMonoBinds extra_mbind_list
218         method_binds_s   = map (gen_bind get_fixity) new_dfuns
219         mbinders         = collectLocatedMonoBinders extra_mbinds
220         
221         -- Rename to get RenamedBinds.
222         -- The only tricky bit is that the extra_binds must scope over the
223         -- method bindings for the instances.
224         (rn_method_binds_s, rn_extra_binds)
225                 = renameDerivedCode dflags mod prs (
226                         bindLocatedLocalsRn (ptext (SLIT("deriving"))) mbinders $ \ _ ->
227                         rnTopMonoBinds extra_mbinds []          `thenRn` \ (rn_extra_binds, _) ->
228                         mapRn rn_meths method_binds_s           `thenRn` \ rn_method_binds_s ->
229                         returnRn (rn_method_binds_s, rn_extra_binds)
230                   )
231
232         new_inst_infos = zipWith gen_inst_info new_dfuns rn_method_binds_s
233     in
234
235     ioToTc (dumpIfSet_dyn dflags Opt_D_dump_deriv "Derived instances" 
236               (ddump_deriving new_inst_infos rn_extra_binds))   `thenTc_`
237
238     returnTc (new_inst_infos, rn_extra_binds)
239   where
240     ddump_deriving :: [InstInfo] -> RenamedHsBinds -> SDoc
241     ddump_deriving inst_infos extra_binds
242       = vcat (map pprInstInfo inst_infos) $$ ppr extra_binds
243       where
244
245         -- Make a Real dfun instead of the dummy one we have so far
246     gen_inst_info :: DFunId -> RenamedMonoBinds -> InstInfo
247     gen_inst_info dfun binds
248       = InstInfo { iDFunId = dfun, iBinds = binds, iPrags = [] }
249
250     rn_meths (cls, meths) = rnMethodBinds cls [] meths `thenRn` \ (meths', _) -> 
251                             returnRn meths'     -- Ignore the free vars returned
252 \end{code}
253
254
255 %************************************************************************
256 %*                                                                      *
257 \subsection[TcDeriv-eqns]{Forming the equations}
258 %*                                                                      *
259 %************************************************************************
260
261 @makeDerivEqns@ fishes around to find the info about needed derived
262 instances.  Complicating factors:
263 \begin{itemize}
264 \item
265 We can only derive @Enum@ if the data type is an enumeration
266 type (all nullary data constructors).
267
268 \item
269 We can only derive @Ix@ if the data type is an enumeration {\em
270 or} has just one data constructor (e.g., tuples).
271 \end{itemize}
272
273 [See Appendix~E in the Haskell~1.2 report.] This code here deals w/
274 all those.
275
276 \begin{code}
277 makeDerivEqns :: [RenamedTyClDecl] -> TcM [DerivEqn]
278
279 makeDerivEqns tycl_decls
280   = mapTc mk_eqn derive_these           `thenTc` \ maybe_eqns ->
281     returnTc (catMaybes maybe_eqns)
282   where
283     ------------------------------------------------------------------
284     derive_these :: [(Name, Name)]
285         -- Find the (Class,TyCon) pairs that must be `derived'
286         -- NB: only source-language decls have deriving, no imported ones do
287     derive_these = [ (clas,tycon) 
288                    | TyData {tcdName = tycon, tcdDerivs = Just classes} <- tycl_decls,
289                      clas <- nub classes ]
290
291     ------------------------------------------------------------------
292     mk_eqn :: (Name, Name) -> NF_TcM (Maybe DerivEqn)
293         -- we swizzle the tyvars and datacons out of the tycon
294         -- to make the rest of the equation
295
296     mk_eqn (clas_name, tycon_name)
297       = tcLookupClass clas_name                                 `thenNF_Tc` \ clas ->
298         tcLookupTyCon tycon_name                                `thenNF_Tc` \ tycon ->
299         let
300             clas_key  = classKey clas
301             tyvars    = tyConTyVars tycon
302             tyvar_tys = mkTyVarTys tyvars
303             ty        = mkTyConApp tycon tyvar_tys
304             data_cons = tyConDataCons tycon
305             locn      = getSrcLoc tycon
306             constraints = extra_constraints ++ concat (map mk_constraints data_cons)
307
308             -- "extra_constraints": see notes above about contexts on data decls
309             extra_constraints
310               | offensive_class = tyConTheta tycon
311               | otherwise       = []
312
313             offensive_class = clas_key `elem` needsDataDeclCtxtClassKeys
314     
315             mk_constraints data_con
316                = [ mkClassPred clas [arg_ty]
317                  | arg_ty <- dataConArgTys data_con tyvar_tys,
318                    not (isUnLiftedType arg_ty)  -- No constraints for unlifted types?
319                  ]
320         in
321         case chk_out clas tycon of
322            Just err ->  tcAddSrcLoc (getSrcLoc tycon)   $
323                         addErrTc err                    `thenNF_Tc_` 
324                         returnNF_Tc Nothing
325            Nothing  ->  newDFunName clas [ty] locn `thenNF_Tc` \ dfun_name ->
326                         returnNF_Tc (Just (dfun_name, clas, tycon, tyvars, constraints))
327
328
329
330     ------------------------------------------------------------------
331     chk_out :: Class -> TyCon -> Maybe Message
332     chk_out clas tycon
333         | clas `hasKey` enumClassKey    && not is_enumeration           = bog_out nullary_why
334         | clas `hasKey` boundedClassKey && not is_enumeration_or_single = bog_out single_nullary_why
335         | clas `hasKey` ixClassKey      && not is_enumeration_or_single = bog_out single_nullary_why
336         | null data_cons                     = bog_out no_cons_why
337         | any isExistentialDataCon data_cons = Just (existentialErr clas tycon)
338         | otherwise                          = Nothing
339         where
340             data_cons = tyConDataCons tycon
341             is_enumeration = isEnumerationTyCon tycon
342             is_single_con  = maybeToBool (maybeTyConSingleCon tycon)
343             is_enumeration_or_single = is_enumeration || is_single_con
344
345             single_nullary_why = SLIT("one constructor data type or type with all nullary constructors expected")
346             nullary_why        = SLIT("data type with all nullary constructors expected")
347             no_cons_why        = SLIT("type has no data constructors")
348
349             bog_out why = Just (derivingThingErr clas tycon why)
350 \end{code}
351
352 %************************************************************************
353 %*                                                                      *
354 \subsection[TcDeriv-fixpoint]{Finding the fixed point of \tr{deriving} equations}
355 %*                                                                      *
356 %************************************************************************
357
358 A ``solution'' (to one of the equations) is a list of (k,TyVarTy tv)
359 terms, which is the final correct RHS for the corresponding original
360 equation.
361 \begin{itemize}
362 \item
363 Each (k,TyVarTy tv) in a solution constrains only a type
364 variable, tv.
365
366 \item
367 The (k,TyVarTy tv) pairs in a solution are canonically
368 ordered by sorting on type varible, tv, (major key) and then class, k,
369 (minor key)
370 \end{itemize}
371
372 \begin{code}
373 solveDerivEqns :: InstEnv
374                -> [DerivEqn]
375                -> TcM [DFunId]  -- Solns in same order as eqns.
376                                 -- This bunch is Absolutely minimal...
377
378 solveDerivEqns inst_env_in orig_eqns
379   = iterateDeriv initial_solutions
380   where
381         -- The initial solutions for the equations claim that each
382         -- instance has an empty context; this solution is certainly
383         -- in canonical form.
384     initial_solutions :: [DerivSoln]
385     initial_solutions = [ [] | _ <- orig_eqns ]
386
387     ------------------------------------------------------------------
388         -- iterateDeriv calculates the next batch of solutions,
389         -- compares it with the current one; finishes if they are the
390         -- same, otherwise recurses with the new solutions.
391         -- It fails if any iteration fails
392     iterateDeriv :: [DerivSoln] ->TcM [DFunId]
393     iterateDeriv current_solns
394       = checkNoErrsTc (iterateOnce current_solns)
395                                                 `thenTc` \ (new_dfuns, new_solns) ->
396         if (current_solns == new_solns) then
397             returnTc new_dfuns
398         else
399             iterateDeriv new_solns
400
401     ------------------------------------------------------------------
402     iterateOnce current_solns
403       =     -- Extend the inst info from the explicit instance decls
404             -- with the current set of solutions, giving a
405         getDOptsTc                              `thenTc` \ dflags ->
406         let (new_dfuns, inst_env) =
407                 add_solns dflags inst_env_in orig_eqns current_solns
408         in
409             -- Simplify each RHS
410         tcSetInstEnv inst_env (
411           listTc [ tcAddSrcLoc (getSrcLoc tc)   $
412                    tcAddErrCtxt (derivCtxt tc)  $
413                    tcSimplifyThetas deriv_rhs
414                  | (_, _,tc,_,deriv_rhs) <- orig_eqns ]  
415         )                                       `thenTc` \ next_solns ->
416
417             -- Canonicalise the solutions, so they compare nicely
418         let canonicalised_next_solns = [ sortLt (<) next_soln | next_soln <- next_solns ]
419         in
420         returnTc (new_dfuns, canonicalised_next_solns)
421 \end{code}
422
423 \begin{code}
424 add_solns :: DynFlags
425           -> InstEnv                            -- The global, non-derived ones
426           -> [DerivEqn] -> [DerivSoln]
427           -> ([DFunId], InstEnv)
428     -- the eqns and solns move "in lockstep"; we have the eqns
429     -- because we need the LHS info for addClassInstance.
430
431 add_solns dflags inst_env_in eqns solns
432   = (new_dfuns, inst_env)
433     where
434       new_dfuns     = zipWithEqual "add_solns" mk_deriv_dfun eqns solns
435       (inst_env, _) = extendInstEnv dflags inst_env_in new_dfuns
436         -- Ignore the errors about duplicate instances.
437         -- We don't want repeated error messages
438         -- They'll appear later, when we do the top-level extendInstEnvs
439
440       mk_deriv_dfun (dfun_name, clas, tycon, tyvars, _) theta
441         = mkDictFunId dfun_name clas tyvars 
442                       [mkTyConApp tycon (mkTyVarTys tyvars)] 
443                       theta
444 \end{code}
445
446 %************************************************************************
447 %*                                                                      *
448 \subsection[TcDeriv-normal-binds]{Bindings for the various classes}
449 %*                                                                      *
450 %************************************************************************
451
452 After all the trouble to figure out the required context for the
453 derived instance declarations, all that's left is to chug along to
454 produce them.  They will then be shoved into @tcInstDecls2@, which
455 will do all its usual business.
456
457 There are lots of possibilities for code to generate.  Here are
458 various general remarks.
459
460 PRINCIPLES:
461 \begin{itemize}
462 \item
463 We want derived instances of @Eq@ and @Ord@ (both v common) to be
464 ``you-couldn't-do-better-by-hand'' efficient.
465
466 \item
467 Deriving @Show@---also pretty common--- should also be reasonable good code.
468
469 \item
470 Deriving for the other classes isn't that common or that big a deal.
471 \end{itemize}
472
473 PRAGMATICS:
474
475 \begin{itemize}
476 \item
477 Deriving @Ord@ is done mostly with the 1.3 @compare@ method.
478
479 \item
480 Deriving @Eq@ also uses @compare@, if we're deriving @Ord@, too.
481
482 \item
483 We {\em normally} generate code only for the non-defaulted methods;
484 there are some exceptions for @Eq@ and (especially) @Ord@...
485
486 \item
487 Sometimes we use a @_con2tag_<tycon>@ function, which returns a data
488 constructor's numeric (@Int#@) tag.  These are generated by
489 @gen_tag_n_con_binds@, and the heuristic for deciding if one of
490 these is around is given by @hasCon2TagFun@.
491
492 The examples under the different sections below will make this
493 clearer.
494
495 \item
496 Much less often (really just for deriving @Ix@), we use a
497 @_tag2con_<tycon>@ function.  See the examples.
498
499 \item
500 We use the renamer!!!  Reason: we're supposed to be
501 producing @RenamedMonoBinds@ for the methods, but that means
502 producing correctly-uniquified code on the fly.  This is entirely
503 possible (the @TcM@ monad has a @UniqueSupply@), but it is painful.
504 So, instead, we produce @RdrNameMonoBinds@ then heave 'em through
505 the renamer.  What a great hack!
506 \end{itemize}
507
508 \begin{code}
509 -- Generate the method bindings for the required instance
510 -- (paired with class name, as we need that when renaming
511 --  the method binds)
512 gen_bind :: (Name -> Maybe Fixity) -> DFunId -> (Name, RdrNameMonoBinds)
513 gen_bind get_fixity dfun
514   = (cls_nm, binds)
515   where
516     cls_nm        = className clas
517     (clas, tycon) = simpleDFunClassTyCon dfun
518
519     binds = assoc "gen_bind:bad derived class" gen_list 
520                   (nameUnique cls_nm) tycon
521
522     gen_list = [(eqClassKey,      gen_Eq_binds)
523                ,(ordClassKey,     gen_Ord_binds)
524                ,(enumClassKey,    gen_Enum_binds)
525                ,(boundedClassKey, gen_Bounded_binds)
526                ,(ixClassKey,      gen_Ix_binds)
527                ,(showClassKey,    gen_Show_binds get_fixity)
528                ,(readClassKey,    gen_Read_binds get_fixity)
529                ]
530 \end{code}
531
532
533 %************************************************************************
534 %*                                                                      *
535 \subsection[TcDeriv-taggery-Names]{What con2tag/tag2con functions are available?}
536 %*                                                                      *
537 %************************************************************************
538
539
540 data Foo ... = ...
541
542 con2tag_Foo :: Foo ... -> Int#
543 tag2con_Foo :: Int -> Foo ...   -- easier if Int, not Int#
544 maxtag_Foo  :: Int              -- ditto (NB: not unlifted)
545
546
547 We have a @con2tag@ function for a tycon if:
548 \begin{itemize}
549 \item
550 We're deriving @Eq@ and the tycon has nullary data constructors.
551
552 \item
553 Or: we're deriving @Ord@ (unless single-constructor), @Enum@, @Ix@
554 (enum type only????)
555 \end{itemize}
556
557 We have a @tag2con@ function for a tycon if:
558 \begin{itemize}
559 \item
560 We're deriving @Enum@, or @Ix@ (enum type only???)
561 \end{itemize}
562
563 If we have a @tag2con@ function, we also generate a @maxtag@ constant.
564
565 \begin{code}
566 gen_taggery_Names :: [DFunId]
567                   -> TcM [(RdrName,     -- for an assoc list
568                            TyCon,       -- related tycon
569                            TagThingWanted)]
570
571 gen_taggery_Names dfuns
572   = foldlTc do_con2tag []           tycons_of_interest `thenTc` \ names_so_far ->
573     foldlTc do_tag2con names_so_far tycons_of_interest
574   where
575     all_CTs = map simpleDFunClassTyCon dfuns
576     all_tycons              = map snd all_CTs
577     (tycons_of_interest, _) = removeDups compare all_tycons
578     
579     do_con2tag acc_Names tycon
580       | isDataTyCon tycon &&
581         ((we_are_deriving eqClassKey tycon
582             && any isNullaryDataCon (tyConDataCons tycon))
583          || (we_are_deriving ordClassKey  tycon
584             && not (maybeToBool (maybeTyConSingleCon tycon)))
585          || (we_are_deriving enumClassKey tycon)
586          || (we_are_deriving ixClassKey   tycon))
587         
588       = returnTc ((con2tag_RDR tycon, tycon, GenCon2Tag)
589                    : acc_Names)
590       | otherwise
591       = returnTc acc_Names
592
593     do_tag2con acc_Names tycon
594       | isDataTyCon tycon &&
595          (we_are_deriving enumClassKey tycon ||
596           we_are_deriving ixClassKey   tycon
597           && isEnumerationTyCon tycon)
598       = returnTc ( (tag2con_RDR tycon, tycon, GenTag2Con)
599                  : (maxtag_RDR  tycon, tycon, GenMaxTag)
600                  : acc_Names)
601       | otherwise
602       = returnTc acc_Names
603
604     we_are_deriving clas_key tycon
605       = is_in_eqns clas_key tycon all_CTs
606       where
607         is_in_eqns clas_key tycon [] = False
608         is_in_eqns clas_key tycon ((c,t):cts)
609           =  (clas_key == classKey c && tycon == t)
610           || is_in_eqns clas_key tycon cts
611 \end{code}
612
613 \begin{code}
614 derivingThingErr :: Class -> TyCon -> FAST_STRING -> Message
615
616 derivingThingErr clas tycon why
617   = sep [hsep [ptext SLIT("Can't make a derived instance of"), quotes (ppr clas)],
618          hsep [ptext SLIT("for the type"), quotes (ppr tycon)],
619          parens (ptext why)]
620
621 existentialErr clas tycon
622   = sep [ptext SLIT("Can't derive any instances for type") <+> quotes (ppr tycon),
623          ptext SLIT("because it has existentially-quantified constructor(s)")]
624
625 derivCtxt tycon
626   = ptext SLIT("When deriving classes for") <+> quotes (ppr tycon)
627 \end{code}