2 % (c) The AQUA Project, Glasgow University, 1993-1998
4 \section[TcRules]{Typechecking transformation rules}
7 module TcRules ( tcRules ) where
9 #include "HsVersions.h"
11 import HsSyn ( RuleDecl(..), RuleBndr(..), collectRuleBndrSigTys )
12 import RnHsSyn ( RenamedRuleDecl )
13 import TcHsSyn ( TypecheckedRuleDecl, mkHsLet )
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 )
27 tcRules :: [RenamedRuleDecl] -> TcM [TypecheckedRuleDecl]
28 tcRules decls = mappM tcRule decls
30 tcRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
31 tcRule (HsRule name act vars lhs rhs src_loc)
33 addErrCtxt (ruleCtxt name) $
34 traceTc (ptext SLIT("---- Rule ------")
35 <+> ppr name) `thenM_`
36 newTyVarTy openTypeKind `thenM` \ rule_ty ->
38 -- Deal with the tyvars mentioned in signatures
39 tcAddScopedTyVars (collectRuleBndrSigTys vars) (
41 -- Ditto forall'd variables
42 mappM new_id vars `thenM` \ ids ->
43 tcExtendLocalValEnv ids $
46 getLIE (tcCheckRho lhs rule_ty) `thenM` \ (lhs', lhs_lie) ->
47 getLIE (tcCheckRho rhs rule_ty) `thenM` \ (rhs', rhs_lie) ->
49 returnM (ids, lhs', rhs', lhs_lie, rhs_lie)
50 ) `thenM` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
52 -- Check that LHS has no overloading at all
53 getLIE (tcSimplifyToDicts lhs_lie) `thenM` \ (lhs_binds, lhs_dicts) ->
55 -- Gather the template variables and tyvars
57 tpl_ids = map instToId lhs_dicts ++ ids
59 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
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'
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
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))
76 -- We also need to get the free tyvars of the LHS; but we do that
77 -- during zonking (see TcHsSyn.zonkRule)
79 forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
81 -- RHS can be a bit more lenient. In particular,
82 -- we let constant dictionaries etc float outwards
84 -- NB: tcSimplifyInferCheck zonks the forall_tvs, and
85 -- knocks out any that are constrained by the environment
86 tcSimplifyInferCheck (text "tcRule")
88 lhs_dicts rhs_lie `thenM` \ (forall_tvs1, rhs_binds) ->
90 returnM (HsRule name act
91 (map RuleBndr (forall_tvs1 ++ tpl_ids)) -- yuk
92 (mkHsLet lhs_binds lhs')
93 (mkHsLet rhs_binds rhs')
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)
101 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
102 doubleQuotes (ftext name)