[project @ 1997-03-14 07:52:06 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcDefaults.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1995
3 %
4 \section[TcDefaults]{Typechecking \tr{default} declarations}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module TcDefaults ( tcDefaults ) where
10
11 IMP_Ubiq()
12
13 import HsSyn            ( HsDecl(..), TyDecl, ClassDecl, InstDecl, HsBinds,
14                           DefaultDecl(..), HsType, IfaceSig,
15                           HsExpr, HsLit, ArithSeqInfo, Fake, InPat)
16 import RnHsSyn          ( RenamedHsDecl(..), RenamedDefaultDecl(..) )
17 import TcHsSyn          ( TcIdOcc )
18
19 import TcMonad
20 import Inst             ( InstOrigin(..) )
21 import TcEnv            ( tcLookupClassByKey )
22 import SpecEnv          ( SpecEnv )
23 import TcMonoType       ( tcHsType )
24 import TcSimplify       ( tcSimplifyCheckThetas )
25
26 import TysWiredIn       ( intTy, doubleTy, unitTy )
27 import Unique           ( numClassKey )
28 import Pretty           ( ppPStr, ppAboves )
29 import ErrUtils         ( addShortErrLocLine )
30 import Util
31 \end{code}
32
33 \begin{code}
34 default_default = [intTy, doubleTy]         -- language-specified default `default'
35
36 tcDefaults :: [RenamedHsDecl]
37            -> TcM s [Type]          -- defaulting types to heave
38                                     -- into Tc monad for later use
39                                     -- in Disambig.
40 tcDefaults decls = tc_defaults [default_decl | DefD default_decl <- decls]
41
42 tc_defaults [] = returnTc default_default
43
44 tc_defaults [DefaultDecl mono_tys locn]
45   = tcAddSrcLoc locn $
46     mapTc tcHsType mono_tys     `thenTc` \ tau_tys ->
47
48     case tau_tys of
49       [] -> returnTc []         -- no defaults
50
51       _  ->
52             -- Check that all the types are instances of Num
53             -- We only care about whether it worked or not
54
55         tcLookupClassByKey numClassKey                  `thenNF_Tc` \ num ->
56         tcSimplifyCheckThetas
57                 [ (num, ty) | ty <- tau_tys ]           `thenTc_`
58
59         returnTc tau_tys
60
61 tc_defaults decls
62   = failTc (dupDefaultDeclErr decls)
63
64
65 dupDefaultDeclErr (DefaultDecl _ locn1 : dup_things) sty
66   = ppAboves (item1 : map dup_item dup_things)
67   where
68     item1
69       = addShortErrLocLine locn1 (\ sty ->
70         ppPStr SLIT("multiple default declarations")) sty
71
72     dup_item (DefaultDecl _ locn)
73       = addShortErrLocLine locn (\ sty ->
74         ppPStr SLIT("here was another default declaration")) sty
75
76 \end{code}