6f9890bafa49a4c3e036e50e6bfcdacf4dcb7cee
[ghc-hetmet.git] / ghc / compiler / typecheck / TcRules.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[TcRules]{Typechecking transformation rules}
5
6 \begin{code}
7 module TcRules ( tcRules ) where
8
9 #include "HsVersions.h"
10
11 import HsSyn            ( RuleDecl(..), RuleBndr(..), HsExpr(..), collectRuleBndrSigTys )
12 import CoreSyn          ( CoreRule(..) )
13 import RnHsSyn          ( RenamedRuleDecl )
14 import TcHsSyn          ( TypecheckedRuleDecl, TcExpr, mkHsLet )
15 import TcRnMonad
16 import TcSimplify       ( tcSimplifyToDicts, tcSimplifyInferCheck )
17 import TcMType          ( newTyVarTy )
18 import TcType           ( TcTyVarSet, tyVarsOfTypes, openTypeKind )
19 import TcIfaceSig       ( tcCoreExpr, tcCoreLamBndrs, tcVar )
20 import TcMonoType       ( tcHsSigType, UserTypeCtxt(..), tcAddScopedTyVars )
21 import TcExpr           ( tcMonoExpr )
22 import TcEnv            ( tcExtendLocalValEnv )
23 import Inst             ( instToId )
24 import Id               ( idType, mkLocalId )
25 import VarSet
26 import Outputable
27 \end{code}
28
29 \begin{code}
30 tcRules :: [RenamedRuleDecl] -> TcM [TypecheckedRuleDecl]
31 tcRules decls = mappM tcRule decls
32
33 tcRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
34 tcRule (IfaceRule name act vars fun args rhs src_loc)
35   = addSrcLoc src_loc           $
36     addErrCtxt (ruleCtxt name)  $
37     tcVar 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'))
42
43 tcRule (IfaceRuleOut fun rule)  -- Built-in rules come this way
44   = tcVar fun                           `thenM` \ fun' ->
45     returnM (IfaceRuleOut fun' rule)   
46
47 tcRule (HsRule name act vars lhs rhs src_loc)
48   = addSrcLoc src_loc                           $
49     addErrCtxt (ruleCtxt name)                  $
50     newTyVarTy openTypeKind                             `thenM` \ rule_ty ->
51
52         -- Deal with the tyvars mentioned in signatures
53     tcAddScopedTyVars (collectRuleBndrSigTys vars) (
54
55                 -- Ditto forall'd variables
56         mappM new_id vars                       `thenM` \ ids ->
57         tcExtendLocalValEnv ids                 $
58         
59                 -- Now LHS and RHS
60         getLIE (tcMonoExpr lhs rule_ty)         `thenM` \ (lhs', lhs_lie) ->
61         getLIE (tcMonoExpr rhs rule_ty)         `thenM` \ (rhs', rhs_lie) ->
62         
63         returnM (ids, lhs', rhs', lhs_lie, rhs_lie)
64     )                           `thenM` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
65
66                 -- Check that LHS has no overloading at all
67     getLIE (tcSimplifyToDicts lhs_lie)  `thenM` \ (lhs_binds, lhs_dicts) ->
68
69         -- Gather the template variables and tyvars
70     let
71         tpl_ids = map instToId lhs_dicts ++ ids
72
73         -- IMPORTANT!  We *quantify* over any dicts that appear in the LHS
74         -- Reason: 
75         --      a) The particular dictionary isn't important, because its value
76         --         depends only on the type
77         --              e.g     gcd Int $fIntegralInt
78         --         Here we'd like to match against (gcd Int any_d) for any 'any_d'
79         --
80         --      b) We'd like to make available the dictionaries bound 
81         --         on the LHS in the RHS, so quantifying over them is good
82         --         See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
83
84         -- We initially quantify over any tyvars free in *either* the rule
85         -- *or* the bound variables.  The latter is important.  Consider
86         --      ss (x,(y,z)) = (x,z)
87         --      RULE:  forall v. fst (ss v) = fst v
88         -- The type of the rhs of the rule is just a, but v::(a,(b,c))
89         --
90         -- We also need to get the free tyvars of the LHS; see notes 
91         -- below with ruleLhsTvs.
92         --
93         forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
94                         `unionVarSet`
95                      ruleLhsTvs lhs'
96     in
97         -- RHS can be a bit more lenient.  In particular,
98         -- we let constant dictionaries etc float outwards
99         --
100         -- NB: tcSimplifyInferCheck zonks the forall_tvs, and 
101         --     knocks out any that are constrained by the environment
102     tcSimplifyInferCheck (text "tcRule")
103                          forall_tvs
104                          lhs_dicts rhs_lie      `thenM` \ (forall_tvs1, rhs_binds) ->
105
106     returnM (HsRule name act
107                     (map RuleBndr (forall_tvs1 ++ tpl_ids))     -- yuk
108                     (mkHsLet lhs_binds lhs')
109                     (mkHsLet rhs_binds rhs')
110                     src_loc)
111   where
112     new_id (RuleBndr var)          = newTyVarTy openTypeKind                    `thenM` \ ty ->
113                                      returnM (mkLocalId var ty)
114     new_id (RuleBndrSig var rn_ty) = tcHsSigType (RuleSigCtxt var) rn_ty        `thenM` \ ty ->
115                                      returnM (mkLocalId var ty)
116
117 ruleLhsTvs :: TcExpr -> TcTyVarSet
118 -- We need to gather the type variables mentioned on the LHS so we can 
119 -- quantify over them.  Example:
120 --   data T a = C
121 -- 
122 --   foo :: T a -> Int
123 --   foo C = 1
124 --
125 --   {-# RULES "myrule"  foo C = 1 #-}
126 -- 
127 -- After type checking the LHS becomes (foo a (C a))
128 -- and we do not want to zap the unbound tyvar 'a' to (), because
129 -- that limits the applicability of the rule.  Instead, we
130 -- want to quantify over it!  
131 --
132 -- Fortunately the form of the LHS is pretty limited (see RnSource.validRuleLhs)
133 -- so we don't need to deal with the whole of HsSyn.
134 --
135 ruleLhsTvs (OpApp e1 op _ e2)
136   = ruleLhsTvs e1 `unionVarSet` ruleLhsTvs op `unionVarSet` ruleLhsTvs e2
137 ruleLhsTvs (HsApp e1 e2)
138   = ruleLhsTvs e1 `unionVarSet` ruleLhsTvs e2
139 ruleLhsTvs (HsVar v) = emptyVarSet      -- I don't think we need the tyvars of the Id
140 ruleLhsTvs (TyApp e1 tys)
141   = ruleLhsTvs e1 `unionVarSet` tyVarsOfTypes tys
142 ruleLhsTvs e = pprPanic "ruleLhsTvs" (ppr e)
143
144
145 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+> 
146                 doubleQuotes (ftext name)
147 \end{code}
148
149
150
151