[project @ 2003-02-21 12:28:35 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcDefaults.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[TcDefaults]{Typechecking \tr{default} declarations}
5
6 \begin{code}
7 module TcDefaults ( tcDefaults ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn            ( DefaultDecl(..) )
12 import Name             ( Name )
13 import TcRnMonad
14 import TcEnv            ( tcLookupGlobal_maybe )
15 import TcMonoType       ( tcHsType )
16 import TcSimplify       ( tcSimplifyDefault )
17 import TcType           ( Type, mkClassPred, isTauTy )
18 import PrelNames        ( numClassName )
19 import Outputable
20 import HscTypes         ( TyThing(..) )
21 \end{code}
22
23 \begin{code}
24 tcDefaults :: [DefaultDecl Name]
25            -> TcM [Type]            -- defaulting types to heave
26                                     -- into Tc monad for later use
27                                     -- in Disambig.
28
29 tcDefaults [] = returnM defaultDefaultTys
30
31 tcDefaults [DefaultDecl [] locn]
32   = returnM []          -- no defaults
33
34 tcDefaults [DefaultDecl mono_tys locn]
35   = tcLookupGlobal_maybe numClassName   `thenM` \ maybe_num ->
36     case maybe_num of
37         Just (AClass num_class) -> common_case num_class
38         other                   -> returnM []
39                 -- In the Nothing case, Num has not been sucked in, so the 
40                 -- defaults will never be used; so simply discard the default decl.
41                 -- This slightly benefits modules that don't use any
42                 -- numeric stuff at all, by avoid the necessity of
43                 -- always sucking in Num
44   where
45     common_case num_class
46       = addSrcLoc locn          $
47         addErrCtxt defaultDeclCtxt      $
48         mappM tc_default_ty mono_tys    `thenM` \ tau_tys ->
49     
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_`
53     
54         returnM tau_tys
55
56 tcDefaults decls@(DefaultDecl _ loc : _) =
57     addSrcLoc loc $
58     failWithTc (dupDefaultDeclErr decls)
59
60
61 tc_default_ty hs_ty 
62  = tcHsType hs_ty                               `thenM` \ ty ->
63    checkTc (isTauTy ty) (polyDefErr hs_ty)      `thenM_`
64    returnM ty
65
66 defaultDeclCtxt =  ptext SLIT("when checking that each type in a default declaration")
67                     $$ ptext SLIT("is an instance of class Num")
68
69
70 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
71   = hang (ptext SLIT("Multiple default declarations"))
72       4  (vcat (map pp dup_things))
73   where
74     pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn
75
76 polyDefErr ty 
77   = hang (ptext SLIT("Illegal polymorphic type in default declaration") <> colon) 4 (ppr ty) 
78 \end{code}
79