23ee42366781005c6f5154ec039929149dff3432
[ghc-hetmet.git] / compiler / typecheck / TcClassDcl.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 Typechecking class declarations
7
8 \begin{code}
9 module TcClassDcl ( tcClassSigs, tcClassDecl2, 
10                     findMethodBind, instantiateMethod, tcInstanceMethodBody,
11                     mkGenericDefMethBind, getGenericInstances, mkDefMethRdrName,
12                     tcAddDeclCtxt, badMethodErr, badATErr, omittedATWarn
13                   ) where
14
15 #include "HsVersions.h"
16
17 import HsSyn
18 import RnHsSyn
19 import RnExpr
20 import RnEnv
21 import Inst
22 import InstEnv
23 import TcEnv
24 import TcBinds
25 import TcSimplify
26 import TcHsType
27 import TcMType
28 import TcType
29 import TcRnMonad
30 import Generics
31 import Class
32 import TyCon
33 import MkId
34 import Id
35 import Name
36 import Var
37 import NameEnv
38 import NameSet
39 import RdrName
40 import Outputable
41 import PrelNames
42 import DynFlags
43 import ErrUtils
44 import Util
45 import ListSetOps
46 import SrcLoc
47 import Maybes
48 import BasicTypes
49 import Bag
50 import FastString
51
52 import Control.Monad
53 import Data.List
54 \end{code}
55
56
57 Dictionary handling
58 ~~~~~~~~~~~~~~~~~~~
59 Every class implicitly declares a new data type, corresponding to dictionaries
60 of that class. So, for example:
61
62         class (D a) => C a where
63           op1 :: a -> a
64           op2 :: forall b. Ord b => a -> b -> b
65
66 would implicitly declare
67
68         data CDict a = CDict (D a)      
69                              (a -> a)
70                              (forall b. Ord b => a -> b -> b)
71
72 (We could use a record decl, but that means changing more of the existing apparatus.
73 One step at at time!)
74
75 For classes with just one superclass+method, we use a newtype decl instead:
76
77         class C a where
78           op :: forallb. a -> b -> b
79
80 generates
81
82         newtype CDict a = CDict (forall b. a -> b -> b)
83
84 Now DictTy in Type is just a form of type synomym: 
85         DictTy c t = TyConTy CDict `AppTy` t
86
87 Death to "ExpandingDicts".
88
89
90 %************************************************************************
91 %*                                                                      *
92                 Type-checking the class op signatures
93 %*                                                                      *
94 %************************************************************************
95
96 \begin{code}
97 tcClassSigs :: Name                     -- Name of the class
98             -> [LSig Name]
99             -> LHsBinds Name
100             -> TcM [TcMethInfo]
101
102 type TcMethInfo = (Name, DefMeth, Type) -- A temporary intermediate, to communicate 
103                                         -- between tcClassSigs and buildClass
104 tcClassSigs clas sigs def_methods
105   = do { dm_env <- checkDefaultBinds clas op_names def_methods
106        ; mapM (tcClassSig dm_env) op_sigs }
107   where
108     op_sigs  = [sig | sig@(L _ (TypeSig _ _))       <- sigs]
109     op_names = [n   |     (L _ (TypeSig (L _ n) _)) <- op_sigs]
110
111
112 checkDefaultBinds :: Name -> [Name] -> LHsBinds Name -> TcM (NameEnv Bool)
113   -- Check default bindings
114   --    a) must be for a class op for this class
115   --    b) must be all generic or all non-generic
116   -- and return a mapping from class-op to Bool
117   --    where True <=> it's a generic default method
118 checkDefaultBinds clas ops binds
119   = do dm_infos <- mapM (addLocM (checkDefaultBind clas ops)) (bagToList binds)
120        return (mkNameEnv dm_infos)
121
122 checkDefaultBind :: Name -> [Name] -> HsBindLR Name Name -> TcM (Name, Bool)
123 checkDefaultBind clas ops (FunBind {fun_id = L _ op, fun_matches = MatchGroup matches _ })
124   = do {        -- Check that the op is from this class
125         checkTc (op `elem` ops) (badMethodErr clas op)
126
127         -- Check that all the defns ar generic, or none are
128     ;   checkTc (all_generic || none_generic) (mixedGenericErr op)
129
130     ;   return (op, all_generic)
131     }
132   where
133     n_generic    = count (isJust . maybeGenericMatch) matches
134     none_generic = n_generic == 0
135     all_generic  = matches `lengthIs` n_generic
136 checkDefaultBind _ _ b = pprPanic "checkDefaultBind" (ppr b)
137
138
139 tcClassSig :: NameEnv Bool              -- Info about default methods; 
140            -> LSig Name
141            -> TcM TcMethInfo
142
143 tcClassSig dm_env (L loc (TypeSig (L _ op_name) op_hs_ty))
144   = setSrcSpan loc $ do
145     { op_ty <- tcHsKindedType op_hs_ty  -- Class tyvars already in scope
146     ; let dm = case lookupNameEnv dm_env op_name of
147                 Nothing    -> NoDefMeth
148                 Just False -> DefMeth
149                 Just True  -> GenDefMeth
150     ; return (op_name, dm, op_ty) }
151 tcClassSig _ s = pprPanic "tcClassSig" (ppr s)
152 \end{code}
153
154
155 %************************************************************************
156 %*                                                                      *
157                 Class Declarations
158 %*                                                                      *
159 %************************************************************************
160
161 \begin{code}
162 tcClassDecl2 :: LTyClDecl Name          -- The class declaration
163              -> TcM ([Id], LHsBinds Id)
164
165 tcClassDecl2 (L loc (ClassDecl {tcdLName = class_name, tcdSigs = sigs, 
166                                 tcdMeths = default_binds}))
167   = recoverM (return ([], emptyLHsBinds))       $
168     setSrcSpan loc                              $
169     do  { clas <- tcLookupLocatedClass class_name
170
171         -- We make a separate binding for each default method.
172         -- At one time I used a single AbsBinds for all of them, thus
173         -- AbsBind [d] [dm1, dm2, dm3] { dm1 = ...; dm2 = ...; dm3 = ... }
174         -- But that desugars into
175         --      ds = \d -> (..., ..., ...)
176         --      dm1 = \d -> case ds d of (a,b,c) -> a
177         -- And since ds is big, it doesn't get inlined, so we don't get good
178         -- default methods.  Better to make separate AbsBinds for each
179         ; let
180               (tyvars, _, _, op_items) = classBigSig clas
181               rigid_info  = ClsSkol clas
182               prag_fn     = mkPragFun sigs
183               sig_fn      = mkTcSigFun sigs
184               clas_tyvars = tcSkolSigTyVars rigid_info tyvars
185               pred        = mkClassPred clas (mkTyVarTys clas_tyvars)
186         ; inst_loc <- getInstLoc (SigOrigin rigid_info)
187         ; this_dict <- newDictBndr inst_loc pred
188
189         ; let tc_dm = tcDefMeth clas clas_tyvars
190                                 this_dict default_binds
191                                 sig_fn prag_fn
192                 -- tc_dm is called only for a sel_id
193                 -- that has a binding in default_binds
194
195               dm_sel_ids  = [sel_id | (sel_id, DefMeth) <- op_items]
196               -- Generate code for polymorphic default methods only (hence DefMeth)
197               -- (Generic default methods have turned into instance decls by now.)
198               -- This is incompatible with Hugs, which expects a polymorphic 
199               -- default method for every class op, regardless of whether or not 
200               -- the programmer supplied an explicit default decl for the class.  
201               -- (If necessary we can fix that, but we don't have a convenient Id to hand.)
202
203         ; (dm_ids, defm_binds) <- tcExtendTyVarEnv clas_tyvars  $
204                                   mapAndUnzipM tc_dm dm_sel_ids
205
206         ; return (dm_ids, listToBag defm_binds) }
207
208 tcClassDecl2 d = pprPanic "tcClassDecl2" (ppr d)
209     
210 tcDefMeth :: Class -> [TyVar] -> Inst -> LHsBinds Name
211           -> TcSigFun -> TcPragFun -> Id
212           -> TcM (Id, LHsBind Id)
213 tcDefMeth clas tyvars this_dict binds_in sig_fn prag_fn sel_id
214   = do  { let sel_name = idName sel_id
215         ; dm_name <- lookupTopBndrRn (mkDefMethRdrName sel_name)
216         ; local_dm_name <- newLocalName sel_name
217           -- Base the local_dm_name on the selector name, becuase
218           -- type errors from tcInstanceMethodBody come from here
219
220                 -- See Note [Silly default-method bind]
221                 -- (possibly out of date)
222
223         ; let meth_bind = findMethodBind sel_name local_dm_name binds_in
224                           `orElse` pprPanic "tcDefMeth" (ppr sel_id)
225                 -- We only call tcDefMeth on selectors for which 
226                 -- there is a binding in binds_in
227
228               dm_sig_fn  _ = sig_fn sel_name
229               dm_ty = idType sel_id
230               dm_id = mkDefaultMethodId dm_name dm_ty
231               local_dm_type = instantiateMethod clas sel_id (mkTyVarTys tyvars)
232               local_dm_id   = mkLocalId local_dm_name local_dm_type
233
234         ; (dm_id_w_inline, spec_prags) 
235                 <- tcPrags NonRecursive False True dm_id (prag_fn sel_name)
236
237         ; tcInstanceMethodBody (instLoc this_dict) 
238                                tyvars [this_dict]
239                                ([], emptyBag)
240                                dm_id_w_inline local_dm_id
241                                dm_sig_fn spec_prags meth_bind }
242
243 ---------------
244 tcInstanceMethodBody :: InstLoc -> [TcTyVar] -> [Inst]
245                      -> ([Inst], LHsBinds Id) -> Id -> Id
246                      -> TcSigFun -> [LSpecPrag] -> LHsBind Name 
247                      -> TcM (Id, LHsBind Id)
248 tcInstanceMethodBody inst_loc tyvars dfun_dicts
249                      (this_dict, this_bind) meth_id local_meth_id
250                      meth_sig_fn spec_prags bind@(L loc _)
251   = do  {       -- Typecheck the binding, first extending the envt
252                 -- so that when tcInstSig looks up the local_meth_id to find
253                 -- its signature, we'll find it in the environment
254         ; ((tc_bind, _), lie) <- getLIE $
255                                  tcExtendIdEnv [local_meth_id] $
256                                  tcPolyBinds TopLevel meth_sig_fn no_prag_fn 
257                                              NonRecursive NonRecursive
258                                              (unitBag bind)
259
260         ; let avails = this_dict ++ dfun_dicts
261                 -- Only need the this_dict stuff if there are type 
262                 -- variables involved; otherwise overlap is not possible
263                 -- See Note [Subtle interaction of recursion and overlap]
264                 -- in TcInstDcls
265         ; lie_binds <- tcSimplifyCheck inst_loc tyvars avails lie
266
267         ; let full_bind = AbsBinds tyvars dfun_lam_vars
268                                   [(tyvars, meth_id, local_meth_id, spec_prags)]
269                                   (this_bind `unionBags` lie_binds 
270                                    `unionBags` tc_bind)
271
272               dfun_lam_vars = map instToVar dfun_dicts  -- Includes equalities
273
274         ; return (meth_id, L loc full_bind) } 
275   where
276     no_prag_fn  _ = []          -- No pragmas for local_meth_id; 
277                                 -- they are all for meth_id
278 \end{code}
279
280 \begin{code}
281 mkDefMethRdrName :: Name -> RdrName
282 mkDefMethRdrName sel_name = mkDerivedRdrName sel_name mkDefaultMethodOcc
283
284 instantiateMethod :: Class -> Id -> [TcType] -> TcType
285 -- Take a class operation, say  
286 --      op :: forall ab. C a => forall c. Ix c => (b,c) -> a
287 -- Instantiate it at [ty1,ty2]
288 -- Return the "local method type": 
289 --      forall c. Ix x => (ty2,c) -> ty1
290 instantiateMethod clas sel_id inst_tys
291   = ASSERT( ok_first_pred ) local_meth_ty
292   where
293     (sel_tyvars,sel_rho) = tcSplitForAllTys (idType sel_id)
294     rho_ty = ASSERT( length sel_tyvars == length inst_tys )
295              substTyWith sel_tyvars inst_tys sel_rho
296
297     (first_pred, local_meth_ty) = tcSplitPredFunTy_maybe rho_ty
298                 `orElse` pprPanic "tcInstanceMethod" (ppr sel_id)
299
300     ok_first_pred = case getClassPredTys_maybe first_pred of
301                       Just (clas1, _tys) -> clas == clas1
302                       Nothing -> False
303               -- The first predicate should be of form (C a b)
304               -- where C is the class in question
305
306
307 ---------------------------
308 -- The renamer just puts the selector ID as the binder in the method binding
309 -- but we must use the method name; so we substitute it here.  Crude but simple.
310 findMethodBind  :: Name -> Name         -- Selector and method name
311                 -> LHsBinds Name        -- A group of bindings
312                 -> Maybe (LHsBind Name) -- The binding, with meth_name replacing sel_name
313 findMethodBind sel_name meth_name binds
314   = foldlBag mplus Nothing (mapBag f binds)
315   where 
316         f (L loc1 bind@(FunBind { fun_id = L loc2 op_name }))
317                  | op_name == sel_name
318                  = Just (L loc1 (bind { fun_id = L loc2 meth_name }))
319         f _other = Nothing
320 \end{code}
321
322 Note [Polymorphic methods]
323 ~~~~~~~~~~~~~~~~~~~~~~~~~~
324 Consider
325     class Foo a where
326         op :: forall b. Ord b => a -> b -> b -> b
327     instance Foo c => Foo [c] where
328         op = e
329
330 When typechecking the binding 'op = e', we'll have a meth_id for op
331 whose type is
332       op :: forall c. Foo c => forall b. Ord b => [c] -> b -> b -> b
333
334 So tcPolyBinds must be capable of dealing with nested polytypes; 
335 and so it is. See TcBinds.tcMonoBinds (with type-sig case).
336
337 Note [Silly default-method bind]
338 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
339 When we pass the default method binding to the type checker, it must
340 look like    op2 = e
341 not          $dmop2 = e
342 otherwise the "$dm" stuff comes out error messages.  But we want the
343 "$dm" to come out in the interface file.  So we typecheck the former,
344 and wrap it in a let, thus
345           $dmop2 = let op2 = e in op2
346 This makes the error messages right.
347
348
349 %************************************************************************
350 %*                                                                      *
351         Extracting generic instance declaration from class declarations
352 %*                                                                      *
353 %************************************************************************
354
355 @getGenericInstances@ extracts the generic instance declarations from a class
356 declaration.  For exmaple
357
358         class C a where
359           op :: a -> a
360         
361           op{ x+y } (Inl v)   = ...
362           op{ x+y } (Inr v)   = ...
363           op{ x*y } (v :*: w) = ...
364           op{ 1   } Unit      = ...
365
366 gives rise to the instance declarations
367
368         instance C (x+y) where
369           op (Inl v)   = ...
370           op (Inr v)   = ...
371         
372         instance C (x*y) where
373           op (v :*: w) = ...
374
375         instance C 1 where
376           op Unit      = ...
377
378 \begin{code}
379 mkGenericDefMethBind :: Class -> [Type] -> Id -> Name -> TcM (LHsBind Name)
380 mkGenericDefMethBind clas inst_tys sel_id meth_name
381   =     -- A generic default method
382         -- If the method is defined generically, we can only do the job if the
383         -- instance declaration is for a single-parameter type class with
384         -- a type constructor applied to type arguments in the instance decl
385         --      (checkTc, so False provokes the error)
386     do  { checkTc (isJust maybe_tycon)
387                   (badGenericInstance sel_id (notSimple inst_tys))
388         ; checkTc (tyConHasGenerics tycon)
389                   (badGenericInstance sel_id (notGeneric tycon))
390
391         ; dflags <- getDOpts
392         ; liftIO (dumpIfSet_dyn dflags Opt_D_dump_deriv "Filling in method body"
393                    (vcat [ppr clas <+> ppr inst_tys,
394                           nest 2 (ppr sel_id <+> equals <+> ppr rhs)]))
395
396                 -- Rename it before returning it
397         ; (rn_rhs, _) <- rnLExpr rhs
398         ; return (noLoc $ mkFunBind (noLoc meth_name) [mkSimpleMatch [] rn_rhs]) }
399   where
400     rhs = mkGenericRhs sel_id clas_tyvar tycon
401
402           -- The tycon is only used in the generic case, and in that
403           -- case we require that the instance decl is for a single-parameter
404           -- type class with type variable arguments:
405           --    instance (...) => C (T a b)
406     clas_tyvar  = ASSERT (not (null (classTyVars clas))) head (classTyVars clas)
407     Just tycon  = maybe_tycon
408     maybe_tycon = case inst_tys of 
409                         [ty] -> case tcSplitTyConApp_maybe ty of
410                                   Just (tycon, arg_tys) | all tcIsTyVarTy arg_tys -> Just tycon
411                                   _                                               -> Nothing
412                         _ -> Nothing
413
414
415 ---------------------------
416 getGenericInstances :: [LTyClDecl Name] -> TcM [InstInfo Name] 
417 getGenericInstances class_decls
418   = do  { gen_inst_infos <- mapM (addLocM get_generics) class_decls
419         ; let { gen_inst_info = concat gen_inst_infos }
420
421         -- Return right away if there is no generic stuff
422         ; if null gen_inst_info then return []
423           else do 
424
425         -- Otherwise print it out
426         { dflags <- getDOpts
427         ; liftIO (dumpIfSet_dyn dflags Opt_D_dump_deriv "Generic instances"
428                  (vcat (map pprInstInfoDetails gen_inst_info))) 
429         ; return gen_inst_info }}
430
431 get_generics :: TyClDecl Name -> TcM [InstInfo Name]
432 get_generics decl@(ClassDecl {tcdLName = class_name, tcdMeths = def_methods})
433   | null generic_binds
434   = return [] -- The comon case: no generic default methods
435
436   | otherwise   -- A source class decl with generic default methods
437   = recoverM (return [])                                $
438     tcAddDeclCtxt decl                                  $ do
439     clas <- tcLookupLocatedClass class_name
440
441         -- Group by type, and
442         -- make an InstInfo out of each group
443     let
444         groups = groupWith listToBag generic_binds
445
446     inst_infos <- mapM (mkGenericInstance clas) groups
447
448         -- Check that there is only one InstInfo for each type constructor
449         -- The main way this can fail is if you write
450         --      f {| a+b |} ... = ...
451         --      f {| x+y |} ... = ...
452         -- Then at this point we'll have an InstInfo for each
453         --
454         -- The class should be unary, which is why simpleInstInfoTyCon should be ok
455     let
456         tc_inst_infos :: [(TyCon, InstInfo Name)]
457         tc_inst_infos = [(simpleInstInfoTyCon i, i) | i <- inst_infos]
458
459         bad_groups = [group | group <- equivClassesByUniq get_uniq tc_inst_infos,
460                               group `lengthExceeds` 1]
461         get_uniq (tc,_) = getUnique tc
462
463     mapM_ (addErrTc . dupGenericInsts) bad_groups
464
465         -- Check that there is an InstInfo for each generic type constructor
466     let
467         missing = genericTyConNames `minusList` [tyConName tc | (tc,_) <- tc_inst_infos]
468
469     checkTc (null missing) (missingGenericInstances missing)
470
471     return inst_infos
472   where
473     generic_binds :: [(HsType Name, LHsBind Name)]
474     generic_binds = getGenericBinds def_methods
475 get_generics decl = pprPanic "get_generics" (ppr decl)
476
477
478 ---------------------------------
479 getGenericBinds :: LHsBinds Name -> [(HsType Name, LHsBind Name)]
480   -- Takes a group of method bindings, finds the generic ones, and returns
481   -- them in finite map indexed by the type parameter in the definition.
482 getGenericBinds binds = concat (map getGenericBind (bagToList binds))
483
484 getGenericBind :: LHsBindLR Name Name -> [(HsType Name, LHsBindLR Name Name)]
485 getGenericBind (L loc bind@(FunBind { fun_matches = MatchGroup matches ty }))
486   = groupWith wrap (mapCatMaybes maybeGenericMatch matches)
487   where
488     wrap ms = L loc (bind { fun_matches = MatchGroup ms ty })
489 getGenericBind _
490   = []
491
492 groupWith :: ([a] -> b) -> [(HsType Name, a)] -> [(HsType Name, b)]
493 groupWith _  []          = []
494 groupWith op ((t,v):prs) = (t, op (v:vs)) : groupWith op rest
495     where
496       vs              = map snd this
497       (this,rest)     = partition same_t prs
498       same_t (t', _v) = t `eqPatType` t'
499
500 eqPatLType :: LHsType Name -> LHsType Name -> Bool
501 eqPatLType t1 t2 = unLoc t1 `eqPatType` unLoc t2
502
503 eqPatType :: HsType Name -> HsType Name -> Bool
504 -- A very simple equality function, only for 
505 -- type patterns in generic function definitions.
506 eqPatType (HsTyVar v1)       (HsTyVar v2)       = v1==v2
507 eqPatType (HsAppTy s1 t1)    (HsAppTy s2 t2)    = s1 `eqPatLType` s2 && t1 `eqPatLType` t2
508 eqPatType (HsOpTy s1 op1 t1) (HsOpTy s2 op2 t2) = s1 `eqPatLType` s2 && t1 `eqPatLType` t2 && unLoc op1 == unLoc op2
509 eqPatType (HsNumTy n1)       (HsNumTy n2)       = n1 == n2
510 eqPatType (HsParTy t1)       t2                 = unLoc t1 `eqPatType` t2
511 eqPatType t1                 (HsParTy t2)       = t1 `eqPatType` unLoc t2
512 eqPatType _ _ = False
513
514 ---------------------------------
515 mkGenericInstance :: Class
516                   -> (HsType Name, LHsBinds Name)
517                   -> TcM (InstInfo Name)
518
519 mkGenericInstance clas (hs_ty, binds) = do
520   -- Make a generic instance declaration
521   -- For example:       instance (C a, C b) => C (a+b) where { binds }
522
523         -- Extract the universally quantified type variables
524         -- and wrap them as forall'd tyvars, so that kind inference
525         -- works in the standard way
526     let
527         sig_tvs = map (noLoc.UserTyVar) (nameSetToList (extractHsTyVars (noLoc hs_ty)))
528         hs_forall_ty = noLoc $ mkExplicitHsForAllTy sig_tvs (noLoc []) (noLoc hs_ty)
529
530         -- Type-check the instance type, and check its form
531     forall_inst_ty <- tcHsSigType GenPatCtxt hs_forall_ty
532     let
533         (tyvars, inst_ty) = tcSplitForAllTys forall_inst_ty
534
535     checkTc (validGenericInstanceType inst_ty)
536             (badGenericInstanceType binds)
537
538         -- Make the dictionary function.
539     span <- getSrcSpanM
540     overlap_flag <- getOverlapFlag
541     dfun_name <- newDFunName clas [inst_ty] span
542     let
543         inst_theta = [mkClassPred clas [mkTyVarTy tv] | tv <- tyvars]
544         dfun_id    = mkDictFunId dfun_name tyvars inst_theta clas [inst_ty]
545         ispec      = mkLocalInstance dfun_id overlap_flag
546
547     return (InstInfo { iSpec = ispec, iBinds = VanillaInst binds [] False })
548 \end{code}
549
550
551 %************************************************************************
552 %*                                                                      *
553                 Error messages
554 %*                                                                      *
555 %************************************************************************
556
557 \begin{code}
558 tcAddDeclCtxt :: TyClDecl Name -> TcM a -> TcM a
559 tcAddDeclCtxt decl thing_inside
560   = addErrCtxt ctxt thing_inside
561   where
562      thing | isClassDecl decl  = "class"
563            | isTypeDecl decl   = "type synonym" ++ maybeInst
564            | isDataDecl decl   = if tcdND decl == NewType 
565                                  then "newtype" ++ maybeInst
566                                  else "data type" ++ maybeInst
567            | isFamilyDecl decl = "family"
568            | otherwise         = panic "tcAddDeclCtxt/thing"
569
570      maybeInst | isFamInstDecl decl = " instance"
571                | otherwise          = ""
572
573      ctxt = hsep [ptext (sLit "In the"), text thing, 
574                   ptext (sLit "declaration for"), quotes (ppr (tcdName decl))]
575
576 badMethodErr :: Outputable a => a -> Name -> SDoc
577 badMethodErr clas op
578   = hsep [ptext (sLit "Class"), quotes (ppr clas), 
579           ptext (sLit "does not have a method"), quotes (ppr op)]
580
581 badATErr :: Class -> Name -> SDoc
582 badATErr clas at
583   = hsep [ptext (sLit "Class"), quotes (ppr clas), 
584           ptext (sLit "does not have an associated type"), quotes (ppr at)]
585
586 omittedATWarn :: Name -> SDoc
587 omittedATWarn at
588   = ptext (sLit "No explicit AT declaration for") <+> quotes (ppr at)
589
590 badGenericInstance :: Var -> SDoc -> SDoc
591 badGenericInstance sel_id because
592   = sep [ptext (sLit "Can't derive generic code for") <+> quotes (ppr sel_id),
593          because]
594
595 notSimple :: [Type] -> SDoc
596 notSimple inst_tys
597   = vcat [ptext (sLit "because the instance type(s)"), 
598           nest 2 (ppr inst_tys),
599           ptext (sLit "is not a simple type of form (T a1 ... an)")]
600
601 notGeneric :: TyCon -> SDoc
602 notGeneric tycon
603   = vcat [ptext (sLit "because the instance type constructor") <+> quotes (ppr tycon) <+> 
604           ptext (sLit "was not compiled with -XGenerics")]
605
606 badGenericInstanceType :: LHsBinds Name -> SDoc
607 badGenericInstanceType binds
608   = vcat [ptext (sLit "Illegal type pattern in the generic bindings"),
609           nest 4 (ppr binds)]
610
611 missingGenericInstances :: [Name] -> SDoc
612 missingGenericInstances missing
613   = ptext (sLit "Missing type patterns for") <+> pprQuotedList missing
614           
615 dupGenericInsts :: [(TyCon, InstInfo a)] -> SDoc
616 dupGenericInsts tc_inst_infos
617   = vcat [ptext (sLit "More than one type pattern for a single generic type constructor:"),
618           nest 4 (vcat (map ppr_inst_ty tc_inst_infos)),
619           ptext (sLit "All the type patterns for a generic type constructor must be identical")
620     ]
621   where 
622     ppr_inst_ty (_,inst) = ppr (simpleInstInfoTy inst)
623
624 mixedGenericErr :: Name -> SDoc
625 mixedGenericErr op
626   = ptext (sLit "Can't mix generic and non-generic equations for class method") <+> quotes (ppr op)
627 \end{code}