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