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(..), LRuleDecl, RuleBndr(..), collectRuleBndrSigTys, mkHsLet )
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 )
22 import SrcLoc ( noLoc, unLoc )
27 tcRules :: [LRuleDecl Name] -> TcM [LRuleDecl TcId]
28 tcRules decls = mappM (wrapLocM tcRule) decls
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 ->
37 -- Deal with the tyvars mentioned in signatures
38 tcAddScopedTyVars (collectRuleBndrSigTys vars) (
40 -- Ditto forall'd variables
41 mappM new_id vars `thenM` \ ids ->
42 tcExtendLocalValEnv ids $
45 getLIE (tcCheckRho lhs rule_ty) `thenM` \ (lhs', lhs_lie) ->
46 getLIE (tcCheckRho rhs rule_ty) `thenM` \ (rhs', rhs_lie) ->
48 returnM (ids, lhs', rhs', lhs_lie, rhs_lie)
49 ) `thenM` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
51 -- Check that LHS has no overloading at all
52 getLIE (tcSimplifyToDicts lhs_lie) `thenM` \ (lhs_binds, lhs_dicts) ->
54 -- Gather the template variables and tyvars
56 tpl_ids = map instToId lhs_dicts ++ ids
58 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
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'
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
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))
75 -- We also need to get the free tyvars of the LHS; but we do that
76 -- during zonking (see TcHsSyn.zonkRule)
78 forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
80 -- RHS can be a bit more lenient. In particular,
81 -- we let constant dictionaries etc float outwards
83 -- NB: tcSimplifyInferCheck zonks the forall_tvs, and
84 -- knocks out any that are constrained by the environment
85 tcSimplifyInferCheck (text "tcRule")
87 lhs_dicts rhs_lie `thenM` \ (forall_tvs1, rhs_binds) ->
89 returnM (HsRule name act
90 (map (RuleBndr . noLoc) (forall_tvs1 ++ tpl_ids)) -- yuk
91 (mkHsLet lhs_binds lhs')
92 (mkHsLet rhs_binds rhs'))
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)
101 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
102 doubleQuotes (ftext name)