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 ( tcLookupClass )
15 import TcHsType ( tcHsSigType, UserTypeCtxt( DefaultDeclCtxt ) )
16 import TcSimplify ( tcSimplifyDefault )
17 import TcType ( Type, mkClassPred, isTauTy )
18 import PrelNames ( numClassName )
23 tcDefaults :: [DefaultDecl Name]
24 -> TcM (Maybe [Type]) -- Defaulting types to heave
25 -- into Tc monad for later use
29 = getDefaultTys -- No default declaration, so get the
30 -- default types from the envt;
31 -- i.e. use the curent ones
32 -- (the caller will put them back there)
33 -- It's important not to return defaultDefaultTys here (which
34 -- we used to do) because in a TH program, tcDefaults [] is called
35 -- repeatedly, once for each group of declarations between top-level
36 -- splices. We don't want to carefully set the default types in
37 -- one group, only for the next group to ignore them and install
40 tcDefaults [DefaultDecl [] locn]
41 = returnM (Just []) -- Default declaration specifying no types
43 tcDefaults [DefaultDecl mono_tys locn]
45 addErrCtxt defaultDeclCtxt $
46 tcLookupClass numClassName `thenM` \ num_class ->
47 mappM tc_default_ty mono_tys `thenM` \ tau_tys ->
49 -- Check that all the types are instances of Num
50 -- We only care about whether it worked or not
51 tcSimplifyDefault [mkClassPred num_class [ty] | ty <- tau_tys] `thenM_`
53 returnM (Just tau_tys)
55 tcDefaults decls@(DefaultDecl _ loc : _) =
57 failWithTc (dupDefaultDeclErr decls)
61 = tcHsSigType DefaultDeclCtxt hs_ty `thenM` \ ty ->
62 checkTc (isTauTy ty) (polyDefErr hs_ty) `thenM_`
65 defaultDeclCtxt = ptext SLIT("when checking that each type in a default declaration")
66 $$ ptext SLIT("is an instance of class Num")
69 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
70 = hang (ptext SLIT("Multiple default declarations"))
71 4 (vcat (map pp dup_things))
73 pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn
76 = hang (ptext SLIT("Illegal polymorphic type in default declaration") <> colon) 4 (ppr ty)