2 % (c) The AQUA Project, Glasgow University, 1993-1998
4 \section[TcRules]{Typechecking transformation rules}
7 module TcRules ( tcIfaceRules, tcSourceRules ) where
9 #include "HsVersions.h"
11 import HsSyn ( RuleDecl(..), RuleBndr(..) )
12 import CoreSyn ( CoreRule(..) )
13 import RnHsSyn ( RenamedRuleDecl )
14 import HscTypes ( PackageRuleBase )
15 import TcHsSyn ( TypecheckedRuleDecl, mkHsLet )
17 import TcSimplify ( tcSimplifyToDicts, tcSimplifyInferCheck )
18 import TcType ( newTyVarTy )
19 import TcIfaceSig ( tcCoreExpr, tcCoreLamBndrs, tcVar )
20 import TcMonoType ( kcHsSigTypes, tcHsSigType, tcScopedTyVars, checkSigTyVars )
21 import TcExpr ( tcExpr )
22 import TcEnv ( tcExtendLocalValEnv, tcExtendTyVarEnv, isLocalThing )
23 import Rules ( extendRuleBase )
24 import Inst ( LIE, plusLIEs, instToId )
25 import Id ( idName, idType, mkLocalId )
26 import Module ( Module )
28 import Type ( tyVarsOfTypes, openTypeKind )
29 import List ( partition )
34 tcIfaceRules :: PackageRuleBase -> Module -> [RenamedRuleDecl]
35 -> TcM (PackageRuleBase, [TypecheckedRuleDecl])
36 tcIfaceRules pkg_rule_base mod decls
37 = mapTc tcIfaceRule decls `thenTc` \ new_rules ->
39 (local_rules, imported_rules) = partition is_local new_rules
40 new_rule_base = foldl add pkg_rule_base imported_rules
42 returnTc (new_rule_base, local_rules)
44 add rule_base (IfaceRuleOut id rule) = extendRuleBase rule_base (id, rule)
46 -- When relinking this module from its interface-file decls
47 -- we'll have IfaceRules that are in fact local to this module
48 is_local (IfaceRuleOut n _) = isLocalThing mod n
51 tcIfaceRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
52 -- No zonking necessary!
53 tcIfaceRule rule@(IfaceRule name vars fun args rhs src_loc)
54 = tcAddSrcLoc src_loc $
55 tcAddErrCtxt (ruleCtxt name) $
56 tcVar fun `thenTc` \ fun' ->
57 tcCoreLamBndrs vars $ \ vars' ->
58 mapTc tcCoreExpr args `thenTc` \ args' ->
59 tcCoreExpr rhs `thenTc` \ rhs' ->
61 new_rule :: TypecheckedRuleDecl
62 new_rule = IfaceRuleOut fun' (Rule name vars' args' rhs')
66 tcSourceRules :: [RenamedRuleDecl] -> TcM (LIE, [TypecheckedRuleDecl])
68 = mapAndUnzipTc tcSourceRule decls `thenTc` \ (lies, decls') ->
69 returnTc (plusLIEs lies, decls')
71 tcSourceRule (HsRule name sig_tvs vars lhs rhs src_loc)
72 = tcAddSrcLoc src_loc $
73 tcAddErrCtxt (ruleCtxt name) $
74 newTyVarTy openTypeKind `thenNF_Tc` \ rule_ty ->
76 -- Deal with the tyvars mentioned in signatures
77 tcScopedTyVars sig_tvs (kcHsSigTypes sig_tys) (
79 -- Ditto forall'd variables
80 mapNF_Tc new_id vars `thenNF_Tc` \ ids ->
81 tcExtendLocalValEnv [(idName id, id) | id <- ids] $
84 tcExpr lhs rule_ty `thenTc` \ (lhs', lhs_lie) ->
85 tcExpr rhs rule_ty `thenTc` \ (rhs', rhs_lie) ->
87 returnTc (ids, lhs', rhs', lhs_lie, rhs_lie)
88 ) `thenTc` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
90 -- Check that LHS has no overloading at all
91 tcSimplifyToDicts lhs_lie `thenTc` \ (lhs_dicts, lhs_binds) ->
93 -- Gather the template variables and tyvars
95 tpl_ids = map instToId lhs_dicts ++ ids
97 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
99 -- a) The particular dictionary isn't important, because its value
100 -- depends only on the type
101 -- e.g gcd Int $fIntegralInt
102 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
104 -- b) We'd like to make available the dictionaries bound
105 -- on the LHS in the RHS, so quantifying over them is good
106 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
108 -- We initially quantify over any tyvars free in *either* the rule
109 -- *or* the bound variables. The latter is important. Consider
110 -- ss (x,(y,z)) = (x,z)
111 -- RULE: forall v. fst (ss v) = fst v
112 -- The type of the rhs of the rule is just a, but v::(a,(b,c))
114 -- It's still conceivable that there may be type variables mentioned
115 -- in the LHS, but not in the type of the lhs, nor in the binders.
116 -- They'll get zapped to (), but that's over-constraining really.
117 -- Let's see if we get a problem.
118 forall_tvs = varSetElems (tyVarsOfTypes (rule_ty : map idType tpl_ids))
121 -- RHS can be a bit more lenient. In particular,
122 -- we let constant dictionaries etc float outwards
125 tcSimplifyInferCheck (text "tcRule")
127 lhs_dicts rhs_lie `thenTc` \ (forall_tvs1, lie', rhs_binds) ->
129 returnTc (lie', HsRule name forall_tvs1
130 (map RuleBndr tpl_ids) -- yuk
131 (mkHsLet lhs_binds lhs')
132 (mkHsLet rhs_binds rhs')
135 sig_tys = [t | RuleBndrSig _ t <- vars]
137 new_id (RuleBndr var) = newTyVarTy openTypeKind `thenNF_Tc` \ ty ->
138 returnNF_Tc (mkLocalId var ty)
139 new_id (RuleBndrSig var rn_ty) = tcHsSigType rn_ty `thenTc` \ ty ->
140 returnNF_Tc (mkLocalId var ty)
142 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
143 doubleQuotes (ptext name)