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 ( tcSimplifyDefault )
19 import TysWiredIn ( integerTy, doubleTy )
20 import TcType ( Type, mkClassPred, isTauTy )
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 tcAddErrCtxt defaultDeclCtxt $
54 mapTc tc_default_ty mono_tys `thenTc` \ tau_tys ->
56 -- Check that all the types are instances of Num
57 -- We only care about whether it worked or not
58 tcSimplifyDefault [mkClassPred num_class [ty] | ty <- tau_tys] `thenTc_`
62 tc_defaults decls@(DefaultDecl _ loc : _) =
64 failWithTc (dupDefaultDeclErr decls)
68 = tcHsType hs_ty `thenTc` \ ty ->
69 checkTc (isTauTy ty) (polyDefErr hs_ty) `thenTc_`
72 defaultDeclCtxt = ptext SLIT("when checking that each type in a default declaration")
73 $$ ptext SLIT("is an instance of class Num")
76 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
77 = hang (ptext SLIT("Multiple default declarations"))
78 4 (vcat (map pp dup_things))
80 pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn
83 = hang (ptext SLIT("Illegal polymorphic type in default declaration") <> colon) 4 (ppr ty)