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 ( tcSimplifyCheckThetas )
19 import TysWiredIn ( integerTy, doubleTy )
21 import PrelNames ( numClassKey )
26 default_default = [integerTy, doubleTy]
28 tcDefaults :: [RenamedHsDecl]
29 -> TcM [Type] -- defaulting types to heave
30 -- into Tc monad for later use
32 tcDefaults decls = tc_defaults [default_decl | DefD default_decl <- decls]
34 tc_defaults [] = returnTc default_default
36 tc_defaults [DefaultDecl [] locn]
37 = returnTc [] -- no defaults
39 tc_defaults [DefaultDecl mono_tys locn]
40 = tcLookupGlobal_maybe numClassName `thenNF_Tc` \ maybe_num ->
42 Just (AClass num_class) -> common_case num_class
43 other -> returnTc [] ;
44 -- In the Nothing case, Num has not been sucked in, so the
45 -- defaults will never be used; so simply discard the default decl.
46 -- This slightly benefits modules that don't use any
47 -- numeric stuff at all, by avoid the necessity of
48 -- always sucking in Num
52 mapTc tcHsType mono_tys `thenTc` \ tau_tys ->
54 -- Check that all the types are instances of Num
55 -- We only care about whether it worked or not
56 tcAddErrCtxt defaultDeclCtxt $
59 [ (num_class, [ty]) | ty <- tau_tys ] `thenTc_`
64 tc_defaults decls@(DefaultDecl _ loc : _) =
66 failWithTc (dupDefaultDeclErr decls)
69 defaultDeclCtxt = ptext SLIT("when checking that each type in a default declaration")
70 $$ ptext SLIT("is an instance of class Num")
73 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
74 = hang (ptext SLIT("Multiple default declarations"))
75 4 (vcat (map pp dup_things))
77 pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn