2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1993-1998
5 \section[TcDefaults]{Typechecking \tr{default} declarations}
8 module TcDefaults ( tcDefaults ) where
10 #include "HsVersions.h"
25 tcDefaults :: [LDefaultDecl Name]
26 -> TcM (Maybe [Type]) -- Defaulting types to heave
27 -- into Tc monad for later use
31 = getDefaultTys -- No default declaration, so get the
32 -- default types from the envt;
33 -- i.e. use the curent ones
34 -- (the caller will put them back there)
35 -- It's important not to return defaultDefaultTys here (which
36 -- we used to do) because in a TH program, tcDefaults [] is called
37 -- repeatedly, once for each group of declarations between top-level
38 -- splices. We don't want to carefully set the default types in
39 -- one group, only for the next group to ignore them and install
42 tcDefaults [L locn (DefaultDecl [])]
43 = returnM (Just []) -- Default declaration specifying no types
45 tcDefaults [L locn (DefaultDecl mono_tys)]
47 addErrCtxt defaultDeclCtxt $
48 tcLookupClass numClassName `thenM` \ num_class ->
49 mappM tc_default_ty mono_tys `thenM` \ tau_tys ->
51 -- Check that all the types are instances of Num
52 -- We only care about whether it worked or not
53 tcSimplifyDefault [mkClassPred num_class [ty] | ty <- tau_tys] `thenM_`
55 returnM (Just tau_tys)
57 tcDefaults decls@(L locn (DefaultDecl _) : _) =
59 failWithTc (dupDefaultDeclErr decls)
63 = tcHsSigType DefaultDeclCtxt hs_ty `thenM` \ ty ->
64 checkTc (isTauTy ty) (polyDefErr hs_ty) `thenM_`
67 defaultDeclCtxt = ptext SLIT("when checking that each type in a default declaration")
68 $$ ptext SLIT("is an instance of class Num")
71 dupDefaultDeclErr (L _ (DefaultDecl _) : dup_things)
72 = hang (ptext SLIT("Multiple default declarations"))
73 4 (vcat (map pp dup_things))
75 pp (L locn (DefaultDecl _)) = ptext SLIT("here was another default declaration") <+> ppr locn
78 = hang (ptext SLIT("Illegal polymorphic type in default declaration") <> colon) 4 (ppr ty)