2 % (c) The AQUA Project, Glasgow University, 1993-1998
4 \section[TcDefaults]{Typechecking \tr{default} declarations}
7 module TcDefaults ( tcDefaults ) where
9 #include "HsVersions.h"
11 import HsSyn ( DefaultDecl(..) )
14 import TcEnv ( tcLookupGlobal_maybe )
15 import TcMonoType ( tcHsType )
16 import TcSimplify ( tcSimplifyDefault )
17 import TcType ( Type, mkClassPred, isTauTy )
18 import PrelNames ( numClassName )
20 import HscTypes ( TyThing(..) )
24 tcDefaults :: [DefaultDecl Name]
25 -> TcM [Type] -- defaulting types to heave
26 -- into Tc monad for later use
29 tcDefaults [] = returnM defaultDefaultTys
31 tcDefaults [DefaultDecl [] locn]
32 = returnM [] -- no defaults
34 tcDefaults [DefaultDecl mono_tys locn]
35 = tcLookupGlobal_maybe numClassName `thenM` \ maybe_num ->
37 Just (AClass num_class) -> common_case num_class
39 -- In the Nothing case, Num has not been sucked in, so the
40 -- defaults will never be used; so simply discard the default decl.
41 -- This slightly benefits modules that don't use any
42 -- numeric stuff at all, by avoid the necessity of
43 -- always sucking in Num
47 addErrCtxt defaultDeclCtxt $
48 mappM tc_default_ty mono_tys `thenM` \ tau_tys ->
50 -- Check that all the types are instances of Num
51 -- We only care about whether it worked or not
52 tcSimplifyDefault [mkClassPred num_class [ty] | ty <- tau_tys] `thenM_`
56 tc_defaults decls@(DefaultDecl _ loc : _) =
58 failWithTc (dupDefaultDeclErr decls)
62 = tcHsType hs_ty `thenM` \ ty ->
63 checkTc (isTauTy ty) (polyDefErr hs_ty) `thenM_`
66 defaultDeclCtxt = ptext SLIT("when checking that each type in a default declaration")
67 $$ ptext SLIT("is an instance of class Num")
70 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
71 = hang (ptext SLIT("Multiple default declarations"))
72 4 (vcat (map pp dup_things))
74 pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn
77 = hang (ptext SLIT("Illegal polymorphic type in default declaration") <> colon) 4 (ppr ty)