[project @ 2003-11-17 14:45:17 by simonmar]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcRules.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[TcRules]{Typechecking transformation rules}
5
6 \begin{code}
7 module TcRules ( tcRules ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn            ( RuleDecl(..), RuleBndr(..), collectRuleBndrSigTys )
12 import RnHsSyn          ( RenamedRuleDecl )
13 import TcHsSyn          ( TypecheckedRuleDecl, mkHsLet )
14 import TcRnMonad
15 import TcSimplify       ( tcSimplifyToDicts, tcSimplifyInferCheck )
16 import TcMType          ( newTyVarTy )
17 import TcType           ( tyVarsOfTypes, openTypeKind )
18 import TcHsType         ( tcHsSigType, UserTypeCtxt(..), tcAddScopedTyVars )
19 import TcExpr           ( tcCheckRho )
20 import TcEnv            ( tcExtendLocalValEnv )
21 import Inst             ( instToId )
22 import Id               ( idType, mkLocalId )
23 import Outputable
24 \end{code}
25
26 \begin{code}
27 tcRules :: [RenamedRuleDecl] -> TcM [TypecheckedRuleDecl]
28 tcRules decls = mappM tcRule decls
29
30 tcRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
31 tcRule (HsRule name act vars lhs rhs src_loc)
32   = addSrcLoc src_loc                           $
33     addErrCtxt (ruleCtxt name)                  $
34     traceTc (ptext SLIT("---- Rule ------")
35                  <+> ppr name)                  `thenM_` 
36     newTyVarTy openTypeKind                     `thenM` \ rule_ty ->
37
38         -- Deal with the tyvars mentioned in signatures
39     tcAddScopedTyVars (collectRuleBndrSigTys vars) (
40
41                 -- Ditto forall'd variables
42         mappM new_id vars                       `thenM` \ ids ->
43         tcExtendLocalValEnv ids                 $
44         
45                 -- Now LHS and RHS
46         getLIE (tcCheckRho lhs rule_ty) `thenM` \ (lhs', lhs_lie) ->
47         getLIE (tcCheckRho rhs rule_ty) `thenM` \ (rhs', rhs_lie) ->
48         
49         returnM (ids, lhs', rhs', lhs_lie, rhs_lie)
50     )                           `thenM` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
51
52                 -- Check that LHS has no overloading at all
53     getLIE (tcSimplifyToDicts lhs_lie)  `thenM` \ (lhs_binds, lhs_dicts) ->
54
55         -- Gather the template variables and tyvars
56     let
57         tpl_ids = map instToId lhs_dicts ++ ids
58
59         -- IMPORTANT!  We *quantify* over any dicts that appear in the LHS
60         -- Reason: 
61         --      a) The particular dictionary isn't important, because its value
62         --         depends only on the type
63         --              e.g     gcd Int $fIntegralInt
64         --         Here we'd like to match against (gcd Int any_d) for any 'any_d'
65         --
66         --      b) We'd like to make available the dictionaries bound 
67         --         on the LHS in the RHS, so quantifying over them is good
68         --         See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
69
70         -- We initially quantify over any tyvars free in *either* the rule
71         -- *or* the bound variables.  The latter is important.  Consider
72         --      ss (x,(y,z)) = (x,z)
73         --      RULE:  forall v. fst (ss v) = fst v
74         -- The type of the rhs of the rule is just a, but v::(a,(b,c))
75         --
76         -- We also need to get the free tyvars of the LHS; but we do that
77         -- during zonking (see TcHsSyn.zonkRule)
78         --
79         forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
80     in
81         -- RHS can be a bit more lenient.  In particular,
82         -- we let constant dictionaries etc float outwards
83         --
84         -- NB: tcSimplifyInferCheck zonks the forall_tvs, and 
85         --     knocks out any that are constrained by the environment
86     tcSimplifyInferCheck (text "tcRule")
87                          forall_tvs
88                          lhs_dicts rhs_lie      `thenM` \ (forall_tvs1, rhs_binds) ->
89
90     returnM (HsRule name act
91                     (map RuleBndr (forall_tvs1 ++ tpl_ids))     -- yuk
92                     (mkHsLet lhs_binds lhs')
93                     (mkHsLet rhs_binds rhs')
94                     src_loc)
95   where
96     new_id (RuleBndr var)          = newTyVarTy openTypeKind                    `thenM` \ ty ->
97                                      returnM (mkLocalId var ty)
98     new_id (RuleBndrSig var rn_ty) = tcHsSigType (RuleSigCtxt var) rn_ty        `thenM` \ ty ->
99                                      returnM (mkLocalId var ty)
100
101 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+> 
102                 doubleQuotes (ftext name)
103 \end{code}
104
105
106
107