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