2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1993-1998
6 TcRules: Typechecking transformation rules
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 module TcRules ( tcRules ) where
18 #include "HsVersions.h"
37 tcRules :: [LRuleDecl Name] -> TcM [LRuleDecl TcId]
38 tcRules decls = mapM (wrapLocM tcRule) decls
40 tcRule :: RuleDecl Name -> TcM (RuleDecl TcId)
41 tcRule (HsRule name act vars lhs fv_lhs rhs fv_rhs)
42 = addErrCtxt (ruleCtxt name) $ do
43 traceTc (ptext SLIT("---- Rule ------") <+> ppr name)
44 rule_ty <- newFlexiTyVarTy openTypeKind
46 -- Deal with the tyvars mentioned in signatures
47 (ids, lhs', rhs', lhs_lie, rhs_lie) <-
48 tcRuleBndrs vars $ \ ids -> do
50 (lhs', lhs_lie) <- getLIE (tcMonoExpr lhs rule_ty)
51 (rhs', rhs_lie) <- getLIE (tcMonoExpr rhs rule_ty)
52 return (ids, lhs', rhs', lhs_lie, rhs_lie)
54 -- Check that LHS has no overloading at all
55 (lhs_dicts, lhs_binds) <- tcSimplifyRuleLhs lhs_lie
57 -- Gather the template variables and tyvars
59 tpl_ids = map instToId lhs_dicts ++ ids
61 -- IMPORTANT! We *quantify* over any dicts that appear in the LHS
63 -- a) The particular dictionary isn't important, because its value
64 -- depends only on the type
65 -- e.g gcd Int $fIntegralInt
66 -- Here we'd like to match against (gcd Int any_d) for any 'any_d'
68 -- b) We'd like to make available the dictionaries bound
69 -- on the LHS in the RHS, so quantifying over them is good
70 -- See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
72 -- We initially quantify over any tyvars free in *either* the rule
73 -- *or* the bound variables. The latter is important. Consider
74 -- ss (x,(y,z)) = (x,z)
75 -- RULE: forall v. fst (ss v) = fst v
76 -- The type of the rhs of the rule is just a, but v::(a,(b,c))
78 -- We also need to get the free tyvars of the LHS; but we do that
79 -- during zonking (see TcHsSyn.zonkRule)
81 forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
83 -- RHS can be a bit more lenient. In particular,
84 -- we let constant dictionaries etc float outwards
86 -- NB: tcSimplifyInferCheck zonks the forall_tvs, and
87 -- knocks out any that are constrained by the environment
88 loc <- getInstLoc (SigOrigin (RuleSkol name))
89 (forall_tvs1, rhs_binds) <- tcSimplifyInferCheck loc
93 return (HsRule name act
94 (map (RuleBndr . noLoc) (forall_tvs1 ++ tpl_ids)) -- yuk
95 (mkHsDictLet lhs_binds lhs') fv_lhs
96 (mkHsDictLet rhs_binds rhs') fv_rhs)
99 tcRuleBndrs [] thing_inside = thing_inside []
100 tcRuleBndrs (RuleBndr var : vars) thing_inside
101 = do { ty <- newFlexiTyVarTy openTypeKind
102 ; let id = mkLocalId (unLoc var) ty
103 ; tcExtendIdEnv [id] $
104 tcRuleBndrs vars (\ids -> thing_inside (id:ids)) }
105 tcRuleBndrs (RuleBndrSig var rn_ty : vars) thing_inside
107 -- The tyvar 'a' is brought into scope first, just as if you'd written
109 = do { let ctxt = FunSigCtxt (unLoc var)
110 ; (tyvars, ty) <- tcHsPatSigType ctxt rn_ty
111 ; let skol_tvs = tcSkolSigTyVars (SigSkol ctxt) tyvars
112 id_ty = substTyWith tyvars (mkTyVarTys skol_tvs) ty
113 id = mkLocalId (unLoc var) id_ty
114 ; tcExtendTyVarEnv skol_tvs $
116 tcRuleBndrs vars (\ids -> thing_inside (id:ids)) }
118 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+>
119 doubleQuotes (ftext name)