[project @ 1998-12-02 13:17:09 by simonm]
[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 )
16 import TcMonoType       ( tcHsType )
17 import TcSimplify       ( tcSimplifyCheckThetas )
18
19 import TysWiredIn       ( intTy, 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 = [intTy, doubleTy]         -- language-specified default `default'
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 mono_tys locn]
39   = tcAddSrcLoc locn $
40     mapTc tcHsType mono_tys     `thenTc` \ tau_tys ->
41
42     case tau_tys of
43       [] -> returnTc []         -- no defaults
44
45       _  ->
46             -- Check that all the types are instances of Num
47             -- We only care about whether it worked or not
48
49         tcAddErrCtxt defaultDeclCtxt            $
50         tcLookupClassByKey numClassKey          `thenNF_Tc` \ num ->
51         tcSimplifyCheckThetas
52                 [{- Nothing given -}]
53                 [ (num, [ty]) | ty <- tau_tys ] `thenTc_`
54
55         returnTc tau_tys
56
57 tc_defaults decls
58   = failWithTc (dupDefaultDeclErr decls)
59
60
61 defaultDeclCtxt =  ptext SLIT("when checking that each type in a default declaration")
62                     $$ ptext SLIT("is an instance of class Num")
63
64
65 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
66   = vcat (item1 : map dup_item dup_things)
67   where
68     item1
69       = addShortErrLocLine locn1 (ptext SLIT("multiple default declarations"))
70
71     dup_item (DefaultDecl _ locn)
72       = addShortErrLocLine locn (ptext SLIT("here was another default declaration"))
73 \end{code}