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 ( HsDecl(..), DefaultDecl(..) )
12 import RnHsSyn ( RenamedHsDecl )
15 import TcEnv ( tcLookupGlobal_maybe )
16 import TcMonoType ( tcHsType )
17 import TcSimplify ( tcSimplifyDefault )
18 import TcType ( Type, mkClassPred, isTauTy )
19 import PrelNames ( numClassName )
21 import HscTypes ( TyThing(..) )
25 tcDefaults :: [RenamedHsDecl]
26 -> TcM [Type] -- defaulting types to heave
27 -- into Tc monad for later use
29 tcDefaults decls = tc_defaults [default_decl | DefD default_decl <- decls]
31 tc_defaults [] = returnM defaultDefaultTys
33 tc_defaults [DefaultDecl [] locn]
34 = returnM [] -- no defaults
36 tc_defaults [DefaultDecl mono_tys locn]
37 = tcLookupGlobal_maybe numClassName `thenM` \ maybe_num ->
39 Just (AClass num_class) -> common_case num_class
41 -- In the Nothing case, Num has not been sucked in, so the
42 -- defaults will never be used; so simply discard the default decl.
43 -- This slightly benefits modules that don't use any
44 -- numeric stuff at all, by avoid the necessity of
45 -- always sucking in Num
49 addErrCtxt defaultDeclCtxt $
50 mappM tc_default_ty mono_tys `thenM` \ tau_tys ->
52 -- Check that all the types are instances of Num
53 -- We only care about whether it worked or not
54 tcSimplifyDefault [mkClassPred num_class [ty] | ty <- tau_tys] `thenM_`
58 tc_defaults decls@(DefaultDecl _ loc : _) =
60 failWithTc (dupDefaultDeclErr decls)
64 = tcHsType hs_ty `thenM` \ ty ->
65 checkTc (isTauTy ty) (polyDefErr hs_ty) `thenM_`
68 defaultDeclCtxt = ptext SLIT("when checking that each type in a default declaration")
69 $$ ptext SLIT("is an instance of class Num")
72 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
73 = hang (ptext SLIT("Multiple default declarations"))
74 4 (vcat (map pp dup_things))
76 pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn
79 = hang (ptext SLIT("Illegal polymorphic type in default declaration") <> colon) 4 (ppr ty)