[project @ 2001-02-26 15:06:57 by simonmar]
[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            ( 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 )
41 import RdrName          ( RdrName )
42
43 import TyCon            ( tyConTyVars, tyConDataCons,
44                           tyConTheta, maybeTyConSingleCon, isDataTyCon,
45                           isEnumerationTyCon, TyCon
46                         )
47 import Type             ( TauType, PredType(..), mkTyVarTys, mkTyConApp, 
48                           isUnLiftedType )
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 = [(Class, [TauType])]    -- Same as a ThetaType!
147                 --[PredType]   -- ... | Class Class [Type==TauType]
148
149 type DerivSoln = DerivRhs
150 \end{code}
151
152
153 A note about contexts on data decls
154 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155 Consider
156
157         data (RealFloat a) => Complex a = !a :+ !a deriving( Read )
158
159 We will need an instance decl like:
160
161         instance (Read a, RealFloat a) => Read (Complex a) where
162           ...
163
164 The RealFloat in the context is because the read method for Complex is bound
165 to construct a Complex, and doing that requires that the argument type is
166 in RealFloat. 
167
168 But this ain't true for Show, Eq, Ord, etc, since they don't construct
169 a Complex; they only take them apart.
170
171 Our approach: identify the offending classes, and add the data type
172 context to the instance decl.  The "offending classes" are
173
174         Read, Enum?
175
176
177 %************************************************************************
178 %*                                                                      *
179 \subsection[TcDeriv-driver]{Top-level function for \tr{derivings}}
180 %*                                                                      *
181 %************************************************************************
182
183 \begin{code}
184 tcDeriving  :: PersistentRenamerState
185             -> Module                   -- name of module under scrutiny
186             -> InstEnv                  -- What we already know about instances
187             -> (Name -> Maybe Fixity)   -- used in deriving Show and Read
188             -> [RenamedTyClDecl]        -- All type constructors
189             -> TcM ([InstInfo],         -- The generated "instance decls".
190                     RenamedHsBinds)     -- Extra generated bindings
191
192 tcDeriving prs mod inst_env_in get_fixity tycl_decls
193   = recoverTc (returnTc ([], EmptyBinds)) $
194
195         -- Fish the "deriving"-related information out of the TcEnv
196         -- and make the necessary "equations".
197     makeDerivEqns tycl_decls            `thenTc` \ eqns ->
198     if null eqns then
199         returnTc ([], EmptyBinds)
200     else
201
202         -- Take the equation list and solve it, to deliver a list of
203         -- solutions, a.k.a. the contexts for the instance decls
204         -- required for the corresponding equations.
205     solveDerivEqns inst_env_in eqns             `thenTc` \ new_dfuns ->
206
207         -- Now augment the InstInfos, adding in the rather boring
208         -- actual-code-to-do-the-methods binds.  We may also need to
209         -- generate extra not-one-inst-decl-specific binds, notably
210         -- "con2tag" and/or "tag2con" functions.  We do these
211         -- separately.
212
213     gen_taggery_Names new_dfuns                 `thenTc` \ nm_alist_etc ->
214
215     tcGetEnv                                    `thenNF_Tc` \ env ->
216     getDOptsTc                                  `thenTc` \ dflags ->
217     let
218         extra_mbind_list = map gen_tag_n_con_monobind nm_alist_etc
219         extra_mbinds     = foldr AndMonoBinds EmptyMonoBinds extra_mbind_list
220         method_binds_s   = map (gen_bind get_fixity) new_dfuns
221         mbinders         = collectLocatedMonoBinders extra_mbinds
222         
223         -- Rename to get RenamedBinds.
224         -- The only tricky bit is that the extra_binds must scope over the
225         -- method bindings for the instances.
226         (rn_method_binds_s, rn_extra_binds)
227                 = renameDerivedCode dflags mod prs (
228                         bindLocatedLocalsRn (ptext (SLIT("deriving"))) mbinders $ \ _ ->
229                         rnTopMonoBinds extra_mbinds []          `thenRn` \ (rn_extra_binds, _) ->
230                         mapRn rn_meths method_binds_s           `thenRn` \ rn_method_binds_s ->
231                         returnRn (rn_method_binds_s, rn_extra_binds)
232                   )
233
234         new_inst_infos = zipWith gen_inst_info new_dfuns rn_method_binds_s
235     in
236
237     ioToTc (dumpIfSet_dyn dflags Opt_D_dump_deriv "Derived instances" 
238               (ddump_deriving new_inst_infos rn_extra_binds))   `thenTc_`
239
240     returnTc (new_inst_infos, rn_extra_binds)
241   where
242     ddump_deriving :: [InstInfo] -> RenamedHsBinds -> SDoc
243     ddump_deriving inst_infos extra_binds
244       = vcat (map pprInstInfo inst_infos) $$ ppr extra_binds
245       where
246
247         -- Make a Real dfun instead of the dummy one we have so far
248     gen_inst_info :: DFunId -> RenamedMonoBinds -> InstInfo
249     gen_inst_info dfun binds
250       = InstInfo { iDFunId = dfun, 
251                    iBinds = binds, iPrags = [] }
252
253     rn_meths meths = rnMethodBinds [] meths `thenRn` \ (meths', _) -> returnRn meths'
254         -- Ignore the free vars returned
255 \end{code}
256
257
258 %************************************************************************
259 %*                                                                      *
260 \subsection[TcDeriv-eqns]{Forming the equations}
261 %*                                                                      *
262 %************************************************************************
263
264 @makeDerivEqns@ fishes around to find the info about needed derived
265 instances.  Complicating factors:
266 \begin{itemize}
267 \item
268 We can only derive @Enum@ if the data type is an enumeration
269 type (all nullary data constructors).
270
271 \item
272 We can only derive @Ix@ if the data type is an enumeration {\em
273 or} has just one data constructor (e.g., tuples).
274 \end{itemize}
275
276 [See Appendix~E in the Haskell~1.2 report.] This code here deals w/
277 all those.
278
279 \begin{code}
280 makeDerivEqns :: [RenamedTyClDecl] -> TcM [DerivEqn]
281
282 makeDerivEqns tycl_decls
283   = mapTc mk_eqn derive_these           `thenTc` \ maybe_eqns ->
284     returnTc (catMaybes maybe_eqns)
285   where
286     ------------------------------------------------------------------
287     derive_these :: [(Name, Name)]
288         -- Find the (Class,TyCon) pairs that must be `derived'
289         -- NB: only source-language decls have deriving, no imported ones do
290     derive_these = [ (clas,tycon) 
291                    | TyData {tcdName = tycon, tcdDerivs = Just classes} <- tycl_decls,
292                      clas <- nub classes ]
293
294     ------------------------------------------------------------------
295     mk_eqn :: (Name, Name) -> NF_TcM (Maybe DerivEqn)
296         -- we swizzle the tyvars and datacons out of the tycon
297         -- to make the rest of the equation
298
299     mk_eqn (clas_name, tycon_name)
300       = tcLookupClass clas_name                                 `thenNF_Tc` \ clas ->
301         tcLookupTyCon tycon_name                                `thenNF_Tc` \ tycon ->
302         let
303             clas_key  = classKey clas
304             tyvars    = tyConTyVars tycon
305             tyvar_tys = mkTyVarTys tyvars
306             ty        = mkTyConApp tycon tyvar_tys
307             data_cons = tyConDataCons tycon
308             locn      = getSrcLoc tycon
309             constraints = extra_constraints ++ concat (map mk_constraints data_cons)
310
311             -- "extra_constraints": see notes above about contexts on data decls
312             extra_constraints
313               | offensive_class = tyConTheta tycon
314               | otherwise       = []
315
316             offensive_class = clas_key `elem` needsDataDeclCtxtClassKeys
317     
318             mk_constraints data_con
319                = [ (clas, [arg_ty])
320                  | arg_ty <- dataConArgTys data_con tyvar_tys,
321                    not (isUnLiftedType arg_ty)  -- No constraints for unlifted types?
322                  ]
323         in
324         case chk_out clas tycon of
325            Just err ->  addErrTc err                            `thenNF_Tc_` 
326                         returnNF_Tc Nothing
327            Nothing  ->  newDFunName clas [ty] locn `thenNF_Tc` \ dfun_name ->
328                         returnNF_Tc (Just (dfun_name, clas, tycon, tyvars, constraints))
329
330
331
332     ------------------------------------------------------------------
333     chk_out :: Class -> TyCon -> Maybe Message
334     chk_out clas tycon
335         | clas `hasKey` enumClassKey    && not is_enumeration         = bog_out nullary_why
336         | clas `hasKey` boundedClassKey && not is_enumeration_or_single = bog_out single_nullary_why
337         | clas `hasKey` ixClassKey      && not is_enumeration_or_single = bog_out single_nullary_why
338         | any isExistentialDataCon (tyConDataCons tycon)              = Just (existentialErr clas tycon)
339         | otherwise                                                   = Nothing
340         where
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
348             bog_out why = Just (derivingThingErr clas tycon why)
349 \end{code}
350
351 %************************************************************************
352 %*                                                                      *
353 \subsection[TcDeriv-fixpoint]{Finding the fixed point of \tr{deriving} equations}
354 %*                                                                      *
355 %************************************************************************
356
357 A ``solution'' (to one of the equations) is a list of (k,TyVarTy tv)
358 terms, which is the final correct RHS for the corresponding original
359 equation.
360 \begin{itemize}
361 \item
362 Each (k,TyVarTy tv) in a solution constrains only a type
363 variable, tv.
364
365 \item
366 The (k,TyVarTy tv) pairs in a solution are canonically
367 ordered by sorting on type varible, tv, (major key) and then class, k,
368 (minor key)
369 \end{itemize}
370
371 \begin{code}
372 solveDerivEqns :: InstEnv
373                -> [DerivEqn]
374                -> TcM [DFunId]  -- Solns in same order as eqns.
375                                 -- This bunch is Absolutely minimal...
376
377 solveDerivEqns inst_env_in orig_eqns
378   = iterateDeriv initial_solutions
379   where
380         -- The initial solutions for the equations claim that each
381         -- instance has an empty context; this solution is certainly
382         -- in canonical form.
383     initial_solutions :: [DerivSoln]
384     initial_solutions = [ [] | _ <- orig_eqns ]
385
386     ------------------------------------------------------------------
387         -- iterateDeriv calculates the next batch of solutions,
388         -- compares it with the current one; finishes if they are the
389         -- same, otherwise recurses with the new solutions.
390         -- It fails if any iteration fails
391     iterateDeriv :: [DerivSoln] ->TcM [DFunId]
392     iterateDeriv current_solns
393       = checkNoErrsTc (iterateOnce current_solns)
394                                                 `thenTc` \ (new_dfuns, new_solns) ->
395         if (current_solns == new_solns) then
396             returnTc new_dfuns
397         else
398             iterateDeriv new_solns
399
400     ------------------------------------------------------------------
401     iterateOnce current_solns
402       =     -- Extend the inst info from the explicit instance decls
403             -- with the current set of solutions, giving a
404         getDOptsTc                              `thenTc` \ dflags ->
405         let (new_dfuns, inst_env) =
406                 add_solns dflags inst_env_in orig_eqns current_solns
407         in
408             -- Simplify each RHS
409         tcSetInstEnv inst_env (
410           listTc [ tcAddErrCtxt (derivCtxt tc) $
411                    tcSimplifyThetas deriv_rhs
412                  | (_, _,tc,_,deriv_rhs) <- orig_eqns ]  
413         )                                       `thenTc` \ next_solns ->
414
415             -- Canonicalise the solutions, so they compare nicely
416         let canonicalised_next_solns = [ sortLt (<) next_soln | next_soln <- next_solns ]
417         in
418         returnTc (new_dfuns, canonicalised_next_solns)
419 \end{code}
420
421 \begin{code}
422 add_solns :: DynFlags
423           -> InstEnv                            -- The global, non-derived ones
424           -> [DerivEqn] -> [DerivSoln]
425           -> ([DFunId], InstEnv)
426     -- the eqns and solns move "in lockstep"; we have the eqns
427     -- because we need the LHS info for addClassInstance.
428
429 add_solns dflags inst_env_in eqns solns
430   = (new_dfuns, inst_env)
431     where
432       new_dfuns     = zipWithEqual "add_solns" mk_deriv_dfun eqns solns
433       (inst_env, _) = extendInstEnv dflags inst_env_in new_dfuns
434         -- Ignore the errors about duplicate instances.
435         -- We don't want repeated error messages
436         -- They'll appear later, when we do the top-level extendInstEnvs
437
438       mk_deriv_dfun (dfun_name, clas, tycon, tyvars, _) theta
439         = mkDictFunId dfun_name clas tyvars [mkTyConApp tycon (mkTyVarTys tyvars)] 
440                       (map pair2PredType theta)
441
442       pair2PredType (clas, tautypes) = Class clas tautypes
443 \end{code}
444
445 %************************************************************************
446 %*                                                                      *
447 \subsection[TcDeriv-normal-binds]{Bindings for the various classes}
448 %*                                                                      *
449 %************************************************************************
450
451 After all the trouble to figure out the required context for the
452 derived instance declarations, all that's left is to chug along to
453 produce them.  They will then be shoved into @tcInstDecls2@, which
454 will do all its usual business.
455
456 There are lots of possibilities for code to generate.  Here are
457 various general remarks.
458
459 PRINCIPLES:
460 \begin{itemize}
461 \item
462 We want derived instances of @Eq@ and @Ord@ (both v common) to be
463 ``you-couldn't-do-better-by-hand'' efficient.
464
465 \item
466 Deriving @Show@---also pretty common--- should also be reasonable good code.
467
468 \item
469 Deriving for the other classes isn't that common or that big a deal.
470 \end{itemize}
471
472 PRAGMATICS:
473
474 \begin{itemize}
475 \item
476 Deriving @Ord@ is done mostly with the 1.3 @compare@ method.
477
478 \item
479 Deriving @Eq@ also uses @compare@, if we're deriving @Ord@, too.
480
481 \item
482 We {\em normally} generate code only for the non-defaulted methods;
483 there are some exceptions for @Eq@ and (especially) @Ord@...
484
485 \item
486 Sometimes we use a @_con2tag_<tycon>@ function, which returns a data
487 constructor's numeric (@Int#@) tag.  These are generated by
488 @gen_tag_n_con_binds@, and the heuristic for deciding if one of
489 these is around is given by @hasCon2TagFun@.
490
491 The examples under the different sections below will make this
492 clearer.
493
494 \item
495 Much less often (really just for deriving @Ix@), we use a
496 @_tag2con_<tycon>@ function.  See the examples.
497
498 \item
499 We use the renamer!!!  Reason: we're supposed to be
500 producing @RenamedMonoBinds@ for the methods, but that means
501 producing correctly-uniquified code on the fly.  This is entirely
502 possible (the @TcM@ monad has a @UniqueSupply@), but it is painful.
503 So, instead, we produce @RdrNameMonoBinds@ then heave 'em through
504 the renamer.  What a great hack!
505 \end{itemize}
506
507 \begin{code}
508 -- Generate the method bindings for the required instance
509 -- (paired with class name, as we need that when generating dict
510 --  names.)
511 gen_bind :: (Name -> Maybe Fixity) -> DFunId -> RdrNameMonoBinds
512 gen_bind get_fixity dfun
513   | clas `hasKey` showClassKey   = gen_Show_binds get_fixity tycon
514   | clas `hasKey` readClassKey   = gen_Read_binds get_fixity tycon
515   | otherwise
516   = assoc "gen_bind:bad derived class"
517            [(eqClassKey,      gen_Eq_binds)
518            ,(ordClassKey,     gen_Ord_binds)
519            ,(enumClassKey,    gen_Enum_binds)
520            ,(boundedClassKey, gen_Bounded_binds)
521            ,(ixClassKey,      gen_Ix_binds)
522            ]
523            (classKey clas)
524            tycon
525   where
526     (clas, tycon) = simpleDFunClassTyCon dfun
527 \end{code}
528
529
530 %************************************************************************
531 %*                                                                      *
532 \subsection[TcDeriv-taggery-Names]{What con2tag/tag2con functions are available?}
533 %*                                                                      *
534 %************************************************************************
535
536
537 data Foo ... = ...
538
539 con2tag_Foo :: Foo ... -> Int#
540 tag2con_Foo :: Int -> Foo ...   -- easier if Int, not Int#
541 maxtag_Foo  :: Int              -- ditto (NB: not unlifted)
542
543
544 We have a @con2tag@ function for a tycon if:
545 \begin{itemize}
546 \item
547 We're deriving @Eq@ and the tycon has nullary data constructors.
548
549 \item
550 Or: we're deriving @Ord@ (unless single-constructor), @Enum@, @Ix@
551 (enum type only????)
552 \end{itemize}
553
554 We have a @tag2con@ function for a tycon if:
555 \begin{itemize}
556 \item
557 We're deriving @Enum@, or @Ix@ (enum type only???)
558 \end{itemize}
559
560 If we have a @tag2con@ function, we also generate a @maxtag@ constant.
561
562 \begin{code}
563 gen_taggery_Names :: [DFunId]
564                   -> TcM [(RdrName,     -- for an assoc list
565                            TyCon,       -- related tycon
566                            TagThingWanted)]
567
568 gen_taggery_Names dfuns
569   = foldlTc do_con2tag []           tycons_of_interest `thenTc` \ names_so_far ->
570     foldlTc do_tag2con names_so_far tycons_of_interest
571   where
572     all_CTs = map simpleDFunClassTyCon dfuns
573     all_tycons              = map snd all_CTs
574     (tycons_of_interest, _) = removeDups compare all_tycons
575     
576     do_con2tag acc_Names tycon
577       | isDataTyCon tycon &&
578         ((we_are_deriving eqClassKey tycon
579             && any isNullaryDataCon (tyConDataCons tycon))
580          || (we_are_deriving ordClassKey  tycon
581             && not (maybeToBool (maybeTyConSingleCon tycon)))
582          || (we_are_deriving enumClassKey tycon)
583          || (we_are_deriving ixClassKey   tycon))
584         
585       = returnTc ((con2tag_RDR tycon, tycon, GenCon2Tag)
586                    : acc_Names)
587       | otherwise
588       = returnTc acc_Names
589
590     do_tag2con acc_Names tycon
591       | isDataTyCon tycon &&
592          (we_are_deriving enumClassKey tycon ||
593           we_are_deriving ixClassKey   tycon
594           && isEnumerationTyCon tycon)
595       = returnTc ( (tag2con_RDR tycon, tycon, GenTag2Con)
596                  : (maxtag_RDR  tycon, tycon, GenMaxTag)
597                  : acc_Names)
598       | otherwise
599       = returnTc acc_Names
600
601     we_are_deriving clas_key tycon
602       = is_in_eqns clas_key tycon all_CTs
603       where
604         is_in_eqns clas_key tycon [] = False
605         is_in_eqns clas_key tycon ((c,t):cts)
606           =  (clas_key == classKey c && tycon == t)
607           || is_in_eqns clas_key tycon cts
608 \end{code}
609
610 \begin{code}
611 derivingThingErr :: Class -> TyCon -> FAST_STRING -> Message
612
613 derivingThingErr clas tycon why
614   = sep [hsep [ptext SLIT("Can't make a derived instance of"), quotes (ppr clas)],
615          hsep [ptext SLIT("for the type"), quotes (ppr tycon)],
616          parens (ptext why)]
617
618 existentialErr clas tycon
619   = sep [ptext SLIT("Can't derive any instances for type") <+> quotes (ppr tycon),
620          ptext SLIT("because it has existentially-quantified constructor(s)")]
621
622 derivCtxt tycon
623   = ptext SLIT("When deriving classes for") <+> quotes (ppr tycon)
624 \end{code}