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