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 ( HsDecl(..), RuleDecl(..), RuleBndr(..) )
12 import CoreSyn ( CoreRule(..) )
13 import RnHsSyn ( RenamedHsDecl, RenamedRuleDecl )
14 import HscTypes ( PackageRuleBase )
15 import TcHsSyn ( TypecheckedRuleDecl, mkHsLet )
17 import TcSimplify ( tcSimplifyToDicts, tcSimplifyAndCheck )
18 import TcType ( zonkTcTypes, zonkTcTyVarToTyVar, newTyVarTy )
19 import TcIfaceSig ( tcCoreExpr, tcCoreLamBndrs, tcVar )
20 import TcMonoType ( kcHsSigType, tcHsSigType, tcTyVars, checkSigTyVars )
21 import TcExpr ( tcExpr )
22 import TcEnv ( tcExtendLocalValEnv, tcExtendTyVarEnv, isLocalThing )
23 import Rules ( extendRuleBase )
24 import Inst ( LIE, emptyLIE, plusLIEs, instToId )
25 import Id ( idType, idName, mkVanillaId )
26 import Module ( Module )
28 import Type ( tyVarsOfTypes, openTypeKind )
29 import Bag ( bagToList )
30 import List ( partition )
35 tcRules :: PackageRuleBase -> Module -> [RenamedHsDecl]
36 -> TcM (PackageRuleBase, LIE, [TypecheckedRuleDecl])
37 tcRules pkg_rule_base mod decls
38 = mapAndUnzipTc tcRule [rule | RuleD rule <- decls] `thenTc` \ (lies, new_rules) ->
40 (local_rules, imported_rules) = partition is_local new_rules
41 new_rule_base = foldl add pkg_rule_base imported_rules
43 returnTc (new_rule_base, plusLIEs lies, local_rules)
45 add rule_base (IfaceRuleOut id rule) = extendRuleBase rule_base (id, rule)
47 -- When relinking this module from its interface-file decls
48 -- we'll have IfaceRules that are in fact local to this module
49 is_local (IfaceRuleOut n _) = isLocalThing mod n
52 tcRule :: RenamedRuleDecl -> TcM (LIE, TypecheckedRuleDecl)
53 -- No zonking necessary!
54 tcRule (IfaceRule name vars fun args rhs src_loc)
55 = tcAddSrcLoc src_loc $
56 tcAddErrCtxt (ruleCtxt name) $
57 tcVar fun `thenTc` \ fun' ->
58 tcCoreLamBndrs vars $ \ vars' ->
59 mapTc tcCoreExpr args `thenTc` \ args' ->
60 tcCoreExpr rhs `thenTc` \ rhs' ->
61 returnTc (emptyLIE, IfaceRuleOut fun' (Rule name vars' args' rhs'))
63 tcRule (HsRule name sig_tvs vars lhs rhs src_loc)
64 = tcAddSrcLoc src_loc $
65 tcAddErrCtxt (ruleCtxt name) $
66 newTyVarTy openTypeKind `thenNF_Tc` \ rule_ty ->
68 -- Deal with the tyvars mentioned in signatures
69 tcTyVars sig_tvs (mapTc_ kcHsSigType sig_tys) `thenTc` \ sig_tyvars ->
70 tcExtendTyVarEnv sig_tyvars (
72 -- Ditto forall'd variables
73 mapNF_Tc new_id vars `thenNF_Tc` \ ids ->
74 tcExtendLocalValEnv [(idName id, id) | id <- ids] $
77 tcExpr lhs rule_ty `thenTc` \ (lhs', lhs_lie) ->
78 tcExpr rhs rule_ty `thenTc` \ (rhs', rhs_lie) ->
80 returnTc (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie)
81 ) `thenTc` \ (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie) ->
83 -- Check that LHS has no overloading at all
84 tcSimplifyToDicts lhs_lie `thenTc` \ (lhs_dicts, lhs_binds) ->
85 checkSigTyVars sig_tyvars emptyVarSet `thenTc_`
87 -- Gather the template variables and tyvars
89 tpl_ids = map instToId (bagToList lhs_dicts) ++ ids
91 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
93 -- a) The particular dictionary isn't important, because its value
94 -- depends only on the type
95 -- e.g gcd Int $fIntegralInt
96 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
98 -- b) We'd like to make available the dictionaries bound
99 -- on the LHS in the RHS, so quantifying over them is good
100 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
103 -- Gather type variables to quantify over
104 -- and turn them into real TyVars (just as in TcBinds.tcBindWithSigs)
105 zonkTcTypes (rule_ty : map idType tpl_ids) `thenNF_Tc` \ zonked_tys ->
106 mapTc zonkTcTyVarToTyVar (varSetElems (tyVarsOfTypes zonked_tys)) `thenTc` \ tvs ->
108 -- RHS can be a bit more lenient. In particular,
109 -- we let constant dictionaries etc float outwards
110 tcSimplifyAndCheck (text "tcRule") (mkVarSet tvs)
111 lhs_dicts rhs_lie `thenTc` \ (lie', rhs_binds) ->
113 returnTc (lie', HsRule name tvs
114 (map RuleBndr tpl_ids) -- yuk
115 (mkHsLet lhs_binds lhs')
116 (mkHsLet rhs_binds rhs')
119 sig_tys = [t | RuleBndrSig _ t <- vars]
121 new_id (RuleBndr var) = newTyVarTy openTypeKind `thenNF_Tc` \ ty ->
122 returnNF_Tc (mkVanillaId var ty)
123 new_id (RuleBndrSig var rn_ty) = tcHsSigType rn_ty `thenTc` \ ty ->
124 returnNF_Tc (mkVanillaId var ty)
126 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
127 doubleQuotes (ptext name)