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