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(..), collectRuleBndrSigTys )
12 import CoreSyn ( CoreRule(..) )
13 import RnHsSyn ( RenamedRuleDecl )
14 import TcHsSyn ( TypecheckedRuleDecl, mkHsLet )
16 import TcSimplify ( tcSimplifyToDicts, tcSimplifyInferCheck )
17 import TcMType ( newTyVarTy )
18 import TcType ( tyVarsOfTypes, openTypeKind )
19 import TcIfaceSig ( tcCoreExpr, tcCoreLamBndrs, tcVar )
20 import TcMonoType ( tcHsSigType, UserTypeCtxt(..), tcAddScopedTyVars )
21 import TcExpr ( tcMonoExpr )
22 import TcEnv ( tcExtendLocalValEnv, tcLookupId )
23 import Inst ( LIE, plusLIEs, emptyLIE, instToId )
24 import Id ( idType, mkLocalId )
29 tcIfaceRules :: [RenamedRuleDecl] -> TcM [TypecheckedRuleDecl]
30 tcIfaceRules decls = mapTc tcIfaceRule decls
32 tcIfaceRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
33 -- No zonking necessary!
34 tcIfaceRule (IfaceRule name act vars fun args rhs src_loc)
35 = tcAddSrcLoc src_loc $
36 tcAddErrCtxt (ruleCtxt name) $
37 tcVar fun `thenTc` \ fun' ->
38 tcCoreLamBndrs vars $ \ vars' ->
39 mapTc tcCoreExpr args `thenTc` \ args' ->
40 tcCoreExpr rhs `thenTc` \ rhs' ->
41 returnTc (IfaceRuleOut fun' (Rule name act vars' args' rhs'))
43 tcIfaceRule (IfaceRuleOut fun rule) -- Built-in rules come this way
44 = tcVar fun `thenTc` \ fun' ->
45 returnTc (IfaceRuleOut fun' rule)
47 tcSourceRules :: [RenamedRuleDecl] -> TcM (LIE, [TypecheckedRuleDecl])
49 = mapAndUnzipTc tcSourceRule decls `thenTc` \ (lies, decls') ->
50 returnTc (plusLIEs lies, decls')
52 tcSourceRule (IfaceRuleOut fun rule) -- Built-in rules come this way
53 -- if they are from the module being compiled
54 = tcLookupId fun `thenTc` \ fun' ->
55 returnTc (emptyLIE, IfaceRuleOut fun' rule)
57 tcSourceRule (HsRule name act vars lhs rhs src_loc)
58 = tcAddSrcLoc src_loc $
59 tcAddErrCtxt (ruleCtxt name) $
60 newTyVarTy openTypeKind `thenNF_Tc` \ rule_ty ->
62 -- Deal with the tyvars mentioned in signatures
63 tcAddScopedTyVars (collectRuleBndrSigTys vars) (
65 -- Ditto forall'd variables
66 mapNF_Tc new_id vars `thenNF_Tc` \ ids ->
67 tcExtendLocalValEnv ids $
70 tcMonoExpr lhs rule_ty `thenTc` \ (lhs', lhs_lie) ->
71 tcMonoExpr rhs rule_ty `thenTc` \ (rhs', rhs_lie) ->
73 returnTc (ids, lhs', rhs', lhs_lie, rhs_lie)
74 ) `thenTc` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
76 -- Check that LHS has no overloading at all
77 tcSimplifyToDicts lhs_lie `thenTc` \ (lhs_dicts, lhs_binds) ->
79 -- Gather the template variables and tyvars
81 tpl_ids = map instToId lhs_dicts ++ ids
83 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
85 -- a) The particular dictionary isn't important, because its value
86 -- depends only on the type
87 -- e.g gcd Int $fIntegralInt
88 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
90 -- b) We'd like to make available the dictionaries bound
91 -- on the LHS in the RHS, so quantifying over them is good
92 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
94 -- We initially quantify over any tyvars free in *either* the rule
95 -- *or* the bound variables. The latter is important. Consider
96 -- ss (x,(y,z)) = (x,z)
97 -- RULE: forall v. fst (ss v) = fst v
98 -- The type of the rhs of the rule is just a, but v::(a,(b,c))
100 -- It's still conceivable that there may be type variables mentioned
101 -- in the LHS, but not in the type of the lhs, nor in the binders.
102 -- They'll get zapped to (), but that's over-constraining really.
103 -- Let's see if we get a problem.
104 forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
107 -- RHS can be a bit more lenient. In particular,
108 -- we let constant dictionaries etc float outwards
111 tcSimplifyInferCheck (text "tcRule")
113 lhs_dicts rhs_lie `thenTc` \ (forall_tvs1, lie', rhs_binds) ->
115 returnTc (lie', HsRule name act
116 (map RuleBndr (forall_tvs1 ++ tpl_ids)) -- yuk
117 (mkHsLet lhs_binds lhs')
118 (mkHsLet rhs_binds rhs')
121 new_id (RuleBndr var) = newTyVarTy openTypeKind `thenNF_Tc` \ ty ->
122 returnNF_Tc (mkLocalId var ty)
123 new_id (RuleBndrSig var rn_ty) = tcHsSigType (RuleSigCtxt var) rn_ty `thenTc` \ ty ->
124 returnNF_Tc (mkLocalId var ty)
126 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
127 doubleQuotes (ftext name)