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"
28 tcDefaults :: [LDefaultDecl Name]
29 -> TcM (Maybe [Type]) -- Defaulting types to heave
30 -- into Tc monad for later use
34 = getDeclaredDefaultTys -- No default declaration, so get the
35 -- default types from the envt;
36 -- i.e. use the curent ones
37 -- (the caller will put them back there)
38 -- It's important not to return defaultDefaultTys here (which
39 -- we used to do) because in a TH program, tcDefaults [] is called
40 -- repeatedly, once for each group of declarations between top-level
41 -- splices. We don't want to carefully set the default types in
42 -- one group, only for the next group to ignore them and install
45 tcDefaults [L locn (DefaultDecl [])]
46 = returnM (Just []) -- Default declaration specifying no types
48 tcDefaults [L locn (DefaultDecl mono_tys)]
50 addErrCtxt defaultDeclCtxt $
51 do { ovl_str <- doptM Opt_OverloadedStrings
52 ; num_class <- tcLookupClass numClassName
53 ; is_str_class <- tcLookupClass isStringClassName
54 ; let deflt_clss | ovl_str = [num_class, is_str_class]
55 | otherwise = [num_class]
57 ; tau_tys <- mappM (tc_default_ty deflt_clss) mono_tys
59 ; return (Just tau_tys) }
61 tcDefaults decls@(L locn (DefaultDecl _) : _)
63 failWithTc (dupDefaultDeclErr decls)
66 tc_default_ty deflt_clss hs_ty
67 = do { ty <- tcHsSigType DefaultDeclCtxt hs_ty
68 ; checkTc (isTauTy ty) (polyDefErr hs_ty)
70 -- Check that the type is an instance of at least one of the deflt_clss
71 ; oks <- mapM (check_instance ty) deflt_clss
72 ; checkTc (or oks) (badDefaultTy ty deflt_clss)
75 check_instance :: Type -> Class -> TcM Bool
76 -- Check that ty is an instance of cls
77 -- We only care about whether it worked or not; return a boolean
79 = do { (_, mb_res) <- tryTc (tcSimplifyDefault [mkClassPred cls [ty]])
80 ; return (isJust mb_res) }
82 defaultDeclCtxt = ptext SLIT("When checking the types in a default declaration")
84 dupDefaultDeclErr (L _ (DefaultDecl _) : dup_things)
85 = hang (ptext SLIT("Multiple default declarations"))
86 4 (vcat (map pp dup_things))
88 pp (L locn (DefaultDecl _)) = ptext SLIT("here was another default declaration") <+> ppr locn
91 = hang (ptext SLIT("Illegal polymorphic type in default declaration") <> colon) 4 (ppr ty)
93 badDefaultTy ty deflt_clss
94 = hang (ptext SLIT("The default type") <+> quotes (ppr ty) <+> ptext SLIT("is not an instance of"))
95 2 (foldr1 (\a b -> a <+> ptext SLIT("or") <+> b) (map (quotes. ppr) deflt_clss))