remove empty dir
[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(..), LDefaultDecl )
12 import Name             ( Name )
13 import TcRnMonad
14 import TcEnv            ( tcLookupClass )
15 import TcHsType         ( tcHsSigType, UserTypeCtxt( DefaultDeclCtxt ) )
16 import TcSimplify       ( tcSimplifyDefault )
17 import TcType           ( Type, mkClassPred, isTauTy )
18 import PrelNames        ( numClassName )
19 import SrcLoc           ( Located(..) )
20 import Outputable
21 \end{code}
22
23 \begin{code}
24 tcDefaults :: [LDefaultDecl Name]
25            -> TcM (Maybe [Type])    -- Defaulting types to heave
26                                     -- into Tc monad for later use
27                                     -- in Disambig.
28
29 tcDefaults [] 
30   = getDefaultTys               -- No default declaration, so get the
31                                 -- default types from the envt; 
32                                 -- i.e. use the curent ones
33                                 -- (the caller will put them back there)
34         -- It's important not to return defaultDefaultTys here (which
35         -- we used to do) because in a TH program, tcDefaults [] is called
36         -- repeatedly, once for each group of declarations between top-level
37         -- splices.  We don't want to carefully set the default types in
38         -- one group, only for the next group to ignore them and install
39         -- defaultDefaultTys
40
41 tcDefaults [L locn (DefaultDecl [])]
42   = returnM (Just [])           -- Default declaration specifying no types
43
44 tcDefaults [L locn (DefaultDecl mono_tys)]
45   = setSrcSpan locn                     $
46     addErrCtxt defaultDeclCtxt          $
47     tcLookupClass numClassName          `thenM` \ num_class ->
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 (Just tau_tys)
55
56 tcDefaults decls@(L locn (DefaultDecl _) : _) =
57     setSrcSpan locn $
58     failWithTc (dupDefaultDeclErr decls)
59
60
61 tc_default_ty hs_ty 
62  = tcHsSigType DefaultDeclCtxt 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 (L _ (DefaultDecl _) : dup_things)
71   = hang (ptext SLIT("Multiple default declarations"))
72       4  (vcat (map pp dup_things))
73   where
74     pp (L locn (DefaultDecl _)) = 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