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