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 ( 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 )
20 import TcMonoType ( tcHsSigType, UserTypeCtxt(..), tcAddScopedTyVars )
21 import TcExpr ( tcCheckRho )
22 import TcEnv ( tcExtendLocalValEnv, tcLookupGlobalId, tcLookupId )
23 import Inst ( instToId )
24 import Id ( idType, mkLocalId )
29 tcRules :: [RenamedRuleDecl] -> TcM [TypecheckedRuleDecl]
30 tcRules decls = mappM tcRule decls
32 tcRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
33 tcRule (IfaceRule name act vars fun args rhs src_loc)
35 addErrCtxt (ruleCtxt name) $
36 tcLookupGlobalId fun `thenM` \ fun' ->
37 tcCoreLamBndrs vars $ \ vars' ->
38 mappM tcCoreExpr args `thenM` \ args' ->
39 tcCoreExpr rhs `thenM` \ rhs' ->
40 returnM (IfaceRuleOut fun' (Rule name act vars' args' rhs'))
42 tcRule (IfaceRuleOut fun rule) -- Built-in rules, and only built-in rules,
43 -- come this way. Usually IfaceRuleOut is only
44 -- used for the *output* of the type checker
45 = tcLookupId fun `thenM` \ fun' ->
46 -- NB: tcLookupId, not tcLookupGlobalId
47 -- Reason: when compiling GHC.Base, where eqString is defined,
48 -- we'll get the builtin rule for eqString, but eqString
49 -- will be in the *local* type environment.
50 -- Seems like a bit of a hack
51 returnM (IfaceRuleOut fun' rule)
53 tcRule (HsRule name act vars lhs rhs src_loc)
55 addErrCtxt (ruleCtxt name) $
56 newTyVarTy openTypeKind `thenM` \ rule_ty ->
58 -- Deal with the tyvars mentioned in signatures
59 tcAddScopedTyVars (collectRuleBndrSigTys vars) (
61 -- Ditto forall'd variables
62 mappM new_id vars `thenM` \ ids ->
63 tcExtendLocalValEnv ids $
66 getLIE (tcCheckRho lhs rule_ty) `thenM` \ (lhs', lhs_lie) ->
67 getLIE (tcCheckRho rhs rule_ty) `thenM` \ (rhs', rhs_lie) ->
69 returnM (ids, lhs', rhs', lhs_lie, rhs_lie)
70 ) `thenM` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
72 -- Check that LHS has no overloading at all
73 getLIE (tcSimplifyToDicts lhs_lie) `thenM` \ (lhs_binds, lhs_dicts) ->
75 -- Gather the template variables and tyvars
77 tpl_ids = map instToId lhs_dicts ++ ids
79 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
81 -- a) The particular dictionary isn't important, because its value
82 -- depends only on the type
83 -- e.g gcd Int $fIntegralInt
84 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
86 -- b) We'd like to make available the dictionaries bound
87 -- on the LHS in the RHS, so quantifying over them is good
88 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
90 -- We initially quantify over any tyvars free in *either* the rule
91 -- *or* the bound variables. The latter is important. Consider
92 -- ss (x,(y,z)) = (x,z)
93 -- RULE: forall v. fst (ss v) = fst v
94 -- The type of the rhs of the rule is just a, but v::(a,(b,c))
96 -- We also need to get the free tyvars of the LHS; but we do that
97 -- during zonking (see TcHsSyn.zonkRule)
99 forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
101 -- RHS can be a bit more lenient. In particular,
102 -- we let constant dictionaries etc float outwards
104 -- NB: tcSimplifyInferCheck zonks the forall_tvs, and
105 -- knocks out any that are constrained by the environment
106 tcSimplifyInferCheck (text "tcRule")
108 lhs_dicts rhs_lie `thenM` \ (forall_tvs1, rhs_binds) ->
110 returnM (HsRule name act
111 (map RuleBndr (forall_tvs1 ++ tpl_ids)) -- yuk
112 (mkHsLet lhs_binds lhs')
113 (mkHsLet rhs_binds rhs')
116 new_id (RuleBndr var) = newTyVarTy openTypeKind `thenM` \ ty ->
117 returnM (mkLocalId var ty)
118 new_id (RuleBndrSig var rn_ty) = tcHsSigType (RuleSigCtxt var) rn_ty `thenM` \ ty ->
119 returnM (mkLocalId var ty)
121 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
122 doubleQuotes (ftext name)