2 % (c) The AQUA Project, Glasgow University, 1993-1998
4 \section[TcDefaults]{Typechecking \tr{default} declarations}
7 module TcDefaults ( tcDefaults, defaultDefaultTys ) 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 )
20 import TcType ( Type, mkClassPred )
21 import PrelNames ( numClassName )
23 import HscTypes ( TyThing(..) )
27 defaultDefaultTys = [integerTy, doubleTy]
29 tcDefaults :: [RenamedHsDecl]
30 -> TcM [Type] -- defaulting types to heave
31 -- into Tc monad for later use
33 tcDefaults decls = tc_defaults [default_decl | DefD default_decl <- decls]
35 tc_defaults [] = returnTc defaultDefaultTys
37 tc_defaults [DefaultDecl [] locn]
38 = returnTc [] -- no defaults
40 tc_defaults [DefaultDecl mono_tys locn]
41 = tcLookupGlobal_maybe numClassName `thenNF_Tc` \ maybe_num ->
43 Just (AClass num_class) -> common_case num_class
45 -- In the Nothing case, Num has not been sucked in, so the
46 -- defaults will never be used; so simply discard the default decl.
47 -- This slightly benefits modules that don't use any
48 -- numeric stuff at all, by avoid the necessity of
49 -- always sucking in Num
53 mapTc tcHsType mono_tys `thenTc` \ tau_tys ->
55 -- Check that all the types are instances of Num
56 -- We only care about whether it worked or not
57 tcAddErrCtxt defaultDeclCtxt $
60 [ mkClassPred num_class [ty] | ty <- tau_tys ] `thenTc_`
65 tc_defaults decls@(DefaultDecl _ loc : _) =
67 failWithTc (dupDefaultDeclErr decls)
70 defaultDeclCtxt = ptext SLIT("when checking that each type in a default declaration")
71 $$ ptext SLIT("is an instance of class Num")
74 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
75 = hang (ptext SLIT("Multiple default declarations"))
76 4 (vcat (map pp dup_things))
78 pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn