2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1993-1998
6 TcRules: Typechecking transformation rules
9 module TcRules ( tcRules ) where
27 Note [Typechecking rules]
28 ~~~~~~~~~~~~~~~~~~~~~~~~~
29 We *infer* the typ of the LHS, and use that type to *check* the type of
30 the RHS. That means that higher-rank rules work reasonably well. Here's
31 an example (test simplCore/should_compile/rule2.hs) produced by Roman:
33 foo :: (forall m. m a -> m b) -> m a -> m b
36 bar :: (forall m. m a -> m a) -> m a -> m a
39 {-# RULES "foo/bar" foo = bar #-}
41 He wanted the rule to typecheck.
44 tcRules :: [LRuleDecl Name] -> TcM [LRuleDecl TcId]
45 tcRules decls = mapM (wrapLocM tcRule) decls
47 tcRule :: RuleDecl Name -> TcM (RuleDecl TcId)
48 tcRule (HsRule name act vars lhs fv_lhs rhs fv_rhs)
49 = addErrCtxt (ruleCtxt name) $ do
50 traceTc (ptext (sLit "---- Rule ------") <+> ppr name)
52 -- Deal with the tyvars mentioned in signatures
53 (ids, lhs', rhs', lhs_lie, rhs_lie, rule_ty) <-
54 tcRuleBndrs vars $ \ ids -> do
55 -- Now LHS and RHS; see Note [Typechecking rules]
56 ((lhs', rule_ty), lhs_lie) <- getLIE (tcInferRho lhs)
57 (rhs', rhs_lie) <- getLIE (tcMonoExpr rhs rule_ty)
58 return (ids, lhs', rhs', lhs_lie, rhs_lie, rule_ty)
60 -- Check that LHS has no overloading at all
61 (lhs_dicts, lhs_binds) <- tcSimplifyRuleLhs lhs_lie
63 -- Gather the template variables and tyvars
65 tpl_ids = map instToId lhs_dicts ++ ids
67 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
69 -- a) The particular dictionary isn't important, because its value
70 -- depends only on the type
71 -- e.g gcd Int $fIntegralInt
72 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
74 -- b) We'd like to make available the dictionaries bound
75 -- on the LHS in the RHS, so quantifying over them is good
76 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
78 -- We initially quantify over any tyvars free in *either* the rule
79 -- *or* the bound variables. The latter is important. Consider
80 -- ss (x,(y,z)) = (x,z)
81 -- RULE: forall v. fst (ss v) = fst v
82 -- The type of the rhs of the rule is just a, but v::(a,(b,c))
84 -- We also need to get the free tyvars of the LHS; but we do that
85 -- during zonking (see TcHsSyn.zonkRule)
87 forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
89 -- RHS can be a bit more lenient. In particular,
90 -- we let constant dictionaries etc float outwards
92 -- NB: tcSimplifyInferCheck zonks the forall_tvs, and
93 -- knocks out any that are constrained by the environment
94 loc <- getInstLoc (SigOrigin (RuleSkol name))
95 (forall_tvs1, rhs_binds) <- tcSimplifyInferCheck loc
99 return (HsRule name act
100 (map (RuleBndr . noLoc) (forall_tvs1 ++ tpl_ids)) -- yuk
101 (mkHsDictLet lhs_binds lhs') fv_lhs
102 (mkHsDictLet rhs_binds rhs') fv_rhs)
104 tcRuleBndrs :: [RuleBndr Name] -> ([Id] -> TcM a) -> TcM a
105 tcRuleBndrs [] thing_inside = thing_inside []
106 tcRuleBndrs (RuleBndr var : vars) thing_inside
107 = do { ty <- newFlexiTyVarTy openTypeKind
108 ; let id = mkLocalId (unLoc var) ty
109 ; tcExtendIdEnv [id] $
110 tcRuleBndrs vars (\ids -> thing_inside (id:ids)) }
111 tcRuleBndrs (RuleBndrSig var rn_ty : vars) thing_inside
113 -- The tyvar 'a' is brought into scope first, just as if you'd written
115 = do { let ctxt = FunSigCtxt (unLoc var)
116 ; (tyvars, ty) <- tcHsPatSigType ctxt rn_ty
117 ; let skol_tvs = tcSkolSigTyVars (SigSkol ctxt) tyvars
118 id_ty = substTyWith tyvars (mkTyVarTys skol_tvs) ty
119 id = mkLocalId (unLoc var) id_ty
120 ; tcExtendTyVarEnv skol_tvs $
122 tcRuleBndrs vars (\ids -> thing_inside (id:ids)) }
124 ruleCtxt :: FastString -> SDoc
125 ruleCtxt name = ptext (sLit "When checking the transformation rule") <+>
126 doubleQuotes (ftext name)