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 )
23 import Rules ( extendRuleBase )
24 import Inst ( LIE, emptyLIE, plusLIEs, instToId )
25 import Id ( idType, idName, mkVanillaId )
26 import Name ( nameModule )
27 import Module ( Module )
29 import Type ( tyVarsOfTypes, openTypeKind )
30 import Bag ( bagToList )
31 import List ( partition )
36 tcRules :: PackageRuleBase -> Module -> [RenamedHsDecl]
37 -> TcM (PackageRuleBase, LIE, [TypecheckedRuleDecl])
38 tcRules pkg_rule_base mod decls
39 = mapAndUnzipTc tcRule [rule | RuleD rule <- decls] `thenTc` \ (lies, new_rules) ->
41 (local_rules, imported_rules) = partition is_local new_rules
42 new_rule_base = foldl add pkg_rule_base imported_rules
44 returnTc (new_rule_base, plusLIEs lies, local_rules)
46 add rule_base (IfaceRuleOut id rule) = extendRuleBase rule_base (id, rule)
48 -- When relinking this module from its interface-file decls
49 -- we'll have IfaceRules that are in fact local to this module
50 is_local (IfaceRuleOut n _) = mod == nameModule (idName n)
53 tcRule :: RenamedRuleDecl -> TcM (LIE, TypecheckedRuleDecl)
54 -- No zonking necessary!
55 tcRule (IfaceRule name vars fun args rhs src_loc)
56 = tcAddSrcLoc src_loc $
57 tcAddErrCtxt (ruleCtxt name) $
58 tcVar fun `thenTc` \ fun' ->
59 tcCoreLamBndrs vars $ \ vars' ->
60 mapTc tcCoreExpr args `thenTc` \ args' ->
61 tcCoreExpr rhs `thenTc` \ rhs' ->
62 returnTc (emptyLIE, IfaceRuleOut fun' (Rule name vars' args' rhs'))
64 tcRule (HsRule name sig_tvs vars lhs rhs src_loc)
65 = tcAddSrcLoc src_loc $
66 tcAddErrCtxt (ruleCtxt name) $
67 newTyVarTy openTypeKind `thenNF_Tc` \ rule_ty ->
69 -- Deal with the tyvars mentioned in signatures
70 tcTyVars sig_tvs (mapTc_ kcHsSigType sig_tys) `thenTc` \ sig_tyvars ->
71 tcExtendTyVarEnv sig_tyvars (
73 -- Ditto forall'd variables
74 mapNF_Tc new_id vars `thenNF_Tc` \ ids ->
75 tcExtendLocalValEnv [(idName id, id) | id <- ids] $
78 tcExpr lhs rule_ty `thenTc` \ (lhs', lhs_lie) ->
79 tcExpr rhs rule_ty `thenTc` \ (rhs', rhs_lie) ->
81 returnTc (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie)
82 ) `thenTc` \ (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie) ->
84 -- Check that LHS has no overloading at all
85 tcSimplifyToDicts lhs_lie `thenTc` \ (lhs_dicts, lhs_binds) ->
86 checkSigTyVars sig_tyvars emptyVarSet `thenTc_`
88 -- Gather the template variables and tyvars
90 tpl_ids = map instToId (bagToList lhs_dicts) ++ ids
92 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
94 -- a) The particular dictionary isn't important, because its value
95 -- depends only on the type
96 -- e.g gcd Int $fIntegralInt
97 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
99 -- b) We'd like to make available the dictionaries bound
100 -- on the LHS in the RHS, so quantifying over them is good
101 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
104 -- Gather type variables to quantify over
105 -- and turn them into real TyVars (just as in TcBinds.tcBindWithSigs)
106 zonkTcTypes (rule_ty : map idType tpl_ids) `thenNF_Tc` \ zonked_tys ->
107 mapTc zonkTcTyVarToTyVar (varSetElems (tyVarsOfTypes zonked_tys)) `thenTc` \ tvs ->
109 -- RHS can be a bit more lenient. In particular,
110 -- we let constant dictionaries etc float outwards
111 tcSimplifyAndCheck (text "tcRule") (mkVarSet tvs)
112 lhs_dicts rhs_lie `thenTc` \ (lie', rhs_binds) ->
114 returnTc (lie', HsRule name tvs
115 (map RuleBndr tpl_ids) -- yuk
116 (mkHsLet lhs_binds lhs')
117 (mkHsLet rhs_binds rhs')
120 sig_tys = [t | RuleBndrSig _ t <- vars]
122 new_id (RuleBndr var) = newTyVarTy openTypeKind `thenNF_Tc` \ ty ->
123 returnNF_Tc (mkVanillaId var ty)
124 new_id (RuleBndrSig var rn_ty) = tcHsSigType rn_ty `thenTc` \ ty ->
125 returnNF_Tc (mkVanillaId var ty)
127 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
128 doubleQuotes (ptext name)