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
28 tcRules :: [LRuleDecl Name] -> TcM [LRuleDecl TcId]
29 tcRules decls = mapM (wrapLocM tcRule) decls
31 tcRule :: RuleDecl Name -> TcM (RuleDecl TcId)
32 tcRule (HsRule name act vars lhs fv_lhs rhs fv_rhs)
33 = addErrCtxt (ruleCtxt name) $ do
34 traceTc (ptext (sLit "---- Rule ------") <+> ppr name)
35 rule_ty <- newFlexiTyVarTy openTypeKind
37 -- Deal with the tyvars mentioned in signatures
38 (ids, lhs', rhs', lhs_lie, rhs_lie) <-
39 tcRuleBndrs vars $ \ ids -> do
41 (lhs', lhs_lie) <- getLIE (tcMonoExpr lhs rule_ty)
42 (rhs', rhs_lie) <- getLIE (tcMonoExpr rhs rule_ty)
43 return (ids, lhs', rhs', lhs_lie, rhs_lie)
45 -- Check that LHS has no overloading at all
46 (lhs_dicts, lhs_binds) <- tcSimplifyRuleLhs lhs_lie
48 -- Gather the template variables and tyvars
50 tpl_ids = map instToId lhs_dicts ++ ids
52 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
54 -- a) The particular dictionary isn't important, because its value
55 -- depends only on the type
56 -- e.g gcd Int $fIntegralInt
57 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
59 -- b) We'd like to make available the dictionaries bound
60 -- on the LHS in the RHS, so quantifying over them is good
61 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
63 -- We initially quantify over any tyvars free in *either* the rule
64 -- *or* the bound variables. The latter is important. Consider
65 -- ss (x,(y,z)) = (x,z)
66 -- RULE: forall v. fst (ss v) = fst v
67 -- The type of the rhs of the rule is just a, but v::(a,(b,c))
69 -- We also need to get the free tyvars of the LHS; but we do that
70 -- during zonking (see TcHsSyn.zonkRule)
72 forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
74 -- RHS can be a bit more lenient. In particular,
75 -- we let constant dictionaries etc float outwards
77 -- NB: tcSimplifyInferCheck zonks the forall_tvs, and
78 -- knocks out any that are constrained by the environment
79 loc <- getInstLoc (SigOrigin (RuleSkol name))
80 (forall_tvs1, rhs_binds) <- tcSimplifyInferCheck loc
84 return (HsRule name act
85 (map (RuleBndr . noLoc) (forall_tvs1 ++ tpl_ids)) -- yuk
86 (mkHsDictLet lhs_binds lhs') fv_lhs
87 (mkHsDictLet rhs_binds rhs') fv_rhs)
89 tcRuleBndrs :: [RuleBndr Name] -> ([Id] -> TcM a) -> TcM a
90 tcRuleBndrs [] thing_inside = thing_inside []
91 tcRuleBndrs (RuleBndr var : vars) thing_inside
92 = do { ty <- newFlexiTyVarTy openTypeKind
93 ; let id = mkLocalId (unLoc var) ty
94 ; tcExtendIdEnv [id] $
95 tcRuleBndrs vars (\ids -> thing_inside (id:ids)) }
96 tcRuleBndrs (RuleBndrSig var rn_ty : vars) thing_inside
98 -- The tyvar 'a' is brought into scope first, just as if you'd written
100 = do { let ctxt = FunSigCtxt (unLoc var)
101 ; (tyvars, ty) <- tcHsPatSigType ctxt rn_ty
102 ; let skol_tvs = tcSkolSigTyVars (SigSkol ctxt) tyvars
103 id_ty = substTyWith tyvars (mkTyVarTys skol_tvs) ty
104 id = mkLocalId (unLoc var) id_ty
105 ; tcExtendTyVarEnv skol_tvs $
107 tcRuleBndrs vars (\ids -> thing_inside (id:ids)) }
109 ruleCtxt :: FastString -> SDoc
110 ruleCtxt name = ptext (sLit "When checking the transformation rule") <+>
111 doubleQuotes (ftext name)