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(..), HsTyVarBndr(..) )
12 import CoreSyn ( CoreRule(..) )
13 import RnHsSyn ( RenamedHsDecl )
14 import TcHsSyn ( TypecheckedRuleDecl, mkHsLet )
16 import TcSimplify ( tcSimplifyToDicts, tcSimplifyAndCheck )
17 import TcType ( zonkTcTypes, zonkTcTyVarToTyVar, newTyVarTy )
18 import TcIfaceSig ( tcCoreExpr, tcCoreLamBndrs, tcVar )
19 import TcMonoType ( kcTyVarScope, kcHsSigType, tcHsSigType, newSigTyVars, checkSigTyVars )
20 import TcExpr ( tcExpr )
21 import TcEnv ( tcExtendLocalValEnv, newLocalId,
24 import Inst ( LIE, emptyLIE, plusLIEs, instToId )
25 import Id ( idType, idName, mkVanillaId )
27 import Type ( tyVarsOfTypes, openTypeKind )
28 import Bag ( bagToList )
34 tcRules :: [RenamedHsDecl] -> TcM s (LIE, [TypecheckedRuleDecl])
35 tcRules decls = mapAndUnzipTc tcRule [rule | RuleD rule <- decls] `thenTc` \ (lies, rules) ->
36 returnTc (plusLIEs lies, rules)
38 tcRule (IfaceRule name vars fun args rhs src_loc)
39 = tcAddSrcLoc src_loc $
40 tcAddErrCtxt (ruleCtxt name) $
41 tcVar fun `thenTc` \ fun' ->
42 tcCoreLamBndrs vars $ \ vars' ->
43 mapTc tcCoreExpr args `thenTc` \ args' ->
44 tcCoreExpr rhs `thenTc` \ rhs' ->
45 returnTc (emptyLIE, IfaceRuleOut fun' (Rule name vars' args' rhs'))
47 tcRule (IfaceRuleOut fun rule)
48 = tcVar fun `thenTc` \ fun' ->
49 returnTc (emptyLIE, IfaceRuleOut fun' rule)
51 tcRule (HsRule name sig_tvs vars lhs rhs src_loc)
52 = tcAddSrcLoc src_loc $
53 tcAddErrCtxt (ruleCtxt name) $
54 newTyVarTy openTypeKind `thenNF_Tc` \ rule_ty ->
56 -- Deal with the tyvars mentioned in signatures
57 -- Yuk to the UserTyVar
58 kcTyVarScope (map UserTyVar sig_tvs)
59 (mapTc_ kcHsSigType sig_tys) `thenTc` \ sig_tv_kinds ->
60 newSigTyVars sig_tv_kinds `thenNF_Tc` \ sig_tyvars ->
61 tcExtendTyVarEnv sig_tyvars (
63 -- Ditto forall'd variables
64 mapNF_Tc new_id vars `thenNF_Tc` \ ids ->
65 tcExtendLocalValEnv [(idName id, id) | id <- ids] $
68 tcExpr lhs rule_ty `thenTc` \ (lhs', lhs_lie) ->
69 tcExpr rhs rule_ty `thenTc` \ (rhs', rhs_lie) ->
71 returnTc (ids, lhs', rhs', lhs_lie, rhs_lie)
72 ) `thenTc` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
74 -- Check that LHS has no overloading at all
75 tcSimplifyToDicts lhs_lie `thenTc` \ (lhs_dicts, lhs_binds) ->
76 checkSigTyVars sig_tyvars emptyVarSet `thenTc_`
78 -- Gather the template variables and tyvars
80 tpl_ids = map instToId (bagToList lhs_dicts) ++ ids
82 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
84 -- a) The particular dictionary isn't important, because its value
85 -- depends only on the type
86 -- e.g gcd Int $fIntegralInt
87 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
89 -- b) We'd like to make available the dictionaries bound
90 -- on the LHS in the RHS, so quantifying over them is good
91 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
94 -- Gather type variables to quantify over
95 -- and turn them into real TyVars (just as in TcBinds.tcBindWithSigs)
96 zonkTcTypes (rule_ty : map idType tpl_ids) `thenNF_Tc` \ zonked_tys ->
97 mapTc zonkTcTyVarToTyVar (varSetElems (tyVarsOfTypes zonked_tys)) `thenTc` \ tvs ->
99 -- RHS can be a bit more lenient. In particular,
100 -- we let constant dictionaries etc float outwards
101 tcSimplifyAndCheck (text "tcRule") (mkVarSet tvs)
102 lhs_dicts rhs_lie `thenTc` \ (lie', rhs_binds) ->
104 returnTc (lie', HsRule name tvs
105 (map RuleBndr tpl_ids) -- yuk
106 (mkHsLet lhs_binds lhs')
107 (mkHsLet rhs_binds rhs')
110 sig_tys = [t | RuleBndrSig _ t <- vars]
112 new_id (RuleBndr var) = newTyVarTy openTypeKind `thenNF_Tc` \ ty ->
113 returnNF_Tc (mkVanillaId var ty)
114 new_id (RuleBndrSig var rn_ty) = tcHsSigType rn_ty `thenTc` \ ty ->
115 returnNF_Tc (mkVanillaId var ty)
117 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
118 doubleQuotes (ptext name)