[project @ 2000-02-10 18:39:51 by lewie]
[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            ( HsDecl(..), DefaultDecl(..) )
12 import RnHsSyn          ( RenamedHsDecl )
13
14 import TcMonad
15 import TcEnv            ( tcLookupClassByKey_maybe )
16 import TcMonoType       ( tcHsType )
17 import TcSimplify       ( tcSimplifyCheckThetas )
18
19 import TysWiredIn       ( integerTy, doubleTy )
20 import Type             ( Type )
21 import Unique           ( numClassKey )
22 import ErrUtils         ( addShortErrLocLine )
23 import Outputable
24 import Util
25 \end{code}
26
27 \begin{code}
28 default_default = [integerTy, doubleTy]
29
30 tcDefaults :: [RenamedHsDecl]
31            -> TcM s [Type]          -- defaulting types to heave
32                                     -- into Tc monad for later use
33                                     -- in Disambig.
34 tcDefaults decls = tc_defaults [default_decl | DefD default_decl <- decls]
35
36 tc_defaults [] = returnTc default_default
37
38 tc_defaults [DefaultDecl [] locn]
39   = returnTc []         -- no defaults
40
41 tc_defaults [DefaultDecl mono_tys locn]
42   = tcLookupClassByKey_maybe numClassKey        `thenNF_Tc` \ maybe_num ->
43     case maybe_num of {
44         Nothing ->      -- Num has not been sucked in, so the defaults will
45                         -- never be used; so simply discard the default decl.
46                         -- This slightly benefits modules that don't use any
47                         -- numeric stuff at all, by avoid the necessity of
48                         -- always sucking in Num
49                 returnTc [] ;
50
51         Just num ->     -- The common case
52
53     tcAddSrcLoc locn $
54     mapTc tcHsType mono_tys     `thenTc` \ tau_tys ->
55
56             -- Check that all the types are instances of Num
57             -- We only care about whether it worked or not
58     tcAddErrCtxt defaultDeclCtxt                $
59     tcSimplifyCheckThetas
60                 [{- Nothing given -}]
61                 [ (num, [ty]) | ty <- tau_tys ] `thenTc_`
62
63     returnTc tau_tys
64     }
65
66 tc_defaults decls@(DefaultDecl _ loc : _) =
67     tcAddSrcLoc loc $
68     failWithTc (dupDefaultDeclErr decls)
69
70
71 defaultDeclCtxt =  ptext SLIT("when checking that each type in a default declaration")
72                     $$ ptext SLIT("is an instance of class Num")
73
74
75 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
76   = hang (ptext SLIT("Multiple default declarations"))
77       4  (vcat (map pp dup_things))
78   where
79     pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn
80 \end{code}
81