[project @ 2002-02-11 15:16:25 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, defaultDefaultTys ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn            ( HsDecl(..), DefaultDecl(..) )
12 import RnHsSyn          ( RenamedHsDecl )
13
14 import TcMonad
15 import TcEnv            ( tcLookupGlobal_maybe )
16 import TcMonoType       ( tcHsType )
17 import TcSimplify       ( tcSimplifyDefault )
18
19 import TysWiredIn       ( integerTy, doubleTy )
20 import TcType           ( Type, mkClassPred, isTauTy )
21 import PrelNames        ( numClassName )
22 import Outputable
23 import HscTypes         ( TyThing(..) )
24 \end{code}
25
26 \begin{code}
27 defaultDefaultTys = [integerTy, doubleTy]
28
29 tcDefaults :: [RenamedHsDecl]
30            -> TcM [Type]            -- defaulting types to heave
31                                     -- into Tc monad for later use
32                                     -- in Disambig.
33 tcDefaults decls = tc_defaults [default_decl | DefD default_decl <- decls]
34
35 tc_defaults [] = returnTc defaultDefaultTys
36
37 tc_defaults [DefaultDecl [] locn]
38   = returnTc []         -- no defaults
39
40 tc_defaults [DefaultDecl mono_tys locn]
41   = tcLookupGlobal_maybe numClassName   `thenNF_Tc` \ maybe_num ->
42     case maybe_num of
43         Just (AClass num_class) -> common_case num_class
44         other                   -> returnTc []
45                 -- In the Nothing case, Num has not been sucked in, so the 
46                 -- defaults will never be used; so simply discard the default decl.
47                 -- This slightly benefits modules that don't use any
48                 -- numeric stuff at all, by avoid the necessity of
49                 -- always sucking in Num
50   where
51     common_case num_class
52       = tcAddSrcLoc locn                $
53         tcAddErrCtxt defaultDeclCtxt    $
54         mapTc tc_default_ty 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         tcSimplifyDefault [mkClassPred num_class [ty] | ty <- tau_tys]  `thenTc_`
59     
60         returnTc tau_tys
61
62 tc_defaults decls@(DefaultDecl _ loc : _) =
63     tcAddSrcLoc loc $
64     failWithTc (dupDefaultDeclErr decls)
65
66
67 tc_default_ty hs_ty 
68  = tcHsType hs_ty                               `thenTc` \ ty ->
69    checkTc (isTauTy ty) (polyDefErr hs_ty)      `thenTc_`
70    returnTc ty
71
72 defaultDeclCtxt =  ptext SLIT("when checking that each type in a default declaration")
73                     $$ ptext SLIT("is an instance of class Num")
74
75
76 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things)
77   = hang (ptext SLIT("Multiple default declarations"))
78       4  (vcat (map pp dup_things))
79   where
80     pp (DefaultDecl _ locn) = ptext SLIT("here was another default declaration") <+> ppr locn
81
82 polyDefErr ty 
83   = hang (ptext SLIT("Illegal polymorphic type in default declaration") <> colon) 4 (ppr ty) 
84 \end{code}
85