ec9176134b19f16f31a5ba00f34e8a250d86b212
[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            ( HsDecl(..), RuleDecl(..), RuleBndr(..), HsTyVarBndr(..) )
12 import CoreSyn          ( CoreRule(..) )
13 import RnHsSyn          ( RenamedHsDecl )
14 import TcHsSyn          ( TypecheckedRuleDecl, mkHsLet )
15 import TcMonad
16 import TcSimplify       ( tcSimplifyToDicts, tcSimplifyAndCheck )
17 import TcType           ( zonkTcTypes, newTyVarTy )
18 import TcIfaceSig       ( tcCoreExpr, tcCoreLamBndrs, tcVar )
19 import TcMonoType       ( tcHsSigType, tcHsTyVar, checkSigTyVars )
20 import TcExpr           ( tcExpr )
21 import TcEnv            ( tcExtendLocalValEnv, newLocalId,
22                           tcExtendTyVarEnv
23                         )
24 import Inst             ( LIE, emptyLIE, plusLIEs, instToId )
25 import Id               ( idType, idName, mkVanillaId )
26 import VarSet
27 import Type             ( tyVarsOfTypes, openTypeKind )
28 import Bag              ( bagToList )
29 import Outputable
30 import Util
31 \end{code}
32
33 \begin{code}
34 tcRules :: [RenamedHsDecl] -> TcM s (LIE, [TypecheckedRuleDecl])
35 tcRules decls = mapAndUnzipTc tcRule [rule | RuleD rule <- decls]       `thenTc` \ (lies, rules) ->
36                 returnTc (plusLIEs lies, rules)
37
38 tcRule (IfaceRule name vars fun args rhs src_loc)
39   = tcAddSrcLoc src_loc                 $
40     tcAddErrCtxt (ruleCtxt name)        $
41     tcVar fun                           `thenTc` \ fun' ->
42     tcCoreLamBndrs vars                 $ \ vars' ->
43     mapTc tcCoreExpr args               `thenTc` \ args' ->
44     tcCoreExpr rhs                      `thenTc` \ rhs' ->
45     returnTc (emptyLIE, IfaceRuleOut fun' (Rule name vars' args' rhs'))
46
47 tcRule (IfaceRuleOut fun rule)
48   = tcVar fun                           `thenTc` \ fun' ->
49     returnTc (emptyLIE, IfaceRuleOut fun' rule)
50
51 tcRule (HsRule name sig_tvs vars lhs rhs src_loc)
52   = tcAddSrcLoc src_loc                                 $
53     tcAddErrCtxt (ruleCtxt name)                        $
54     newTyVarTy openTypeKind                             `thenNF_Tc` \ rule_ty ->
55
56         -- Deal with the tyvars mentioned in signatures
57         -- Yuk to the UserTyVar
58     mapNF_Tc (tcHsTyVar . UserTyVar) sig_tvs            `thenNF_Tc` \ sig_tyvars ->
59     tcExtendTyVarEnv sig_tyvars                 (       
60
61                 -- Ditto forall'd variables
62         mapNF_Tc new_id vars                            `thenNF_Tc` \ ids ->
63         tcExtendLocalValEnv [(idName id, id) | id <- ids]       $
64         
65                 -- Now LHS and RHS
66         tcExpr lhs rule_ty                                      `thenTc` \ (lhs', lhs_lie) ->
67         tcExpr rhs rule_ty                                      `thenTc` \ (rhs', rhs_lie) ->
68         
69         returnTc (ids, lhs', rhs', lhs_lie, rhs_lie)
70     )                                           `thenTc` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
71
72                 -- Check that LHS has no overloading at all
73     tcSimplifyToDicts lhs_lie                           `thenTc` \ (lhs_dicts, lhs_binds) ->
74     checkSigTyVars sig_tyvars emptyVarSet               `thenTc_`
75
76         -- Gather the template variables and tyvars
77     let
78         tpl_ids = map instToId (bagToList lhs_dicts) ++ ids
79
80         -- IMPORTANT!  We *quantify* over any dicts that appear in the LHS
81         -- Reason: 
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'
86         --
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
90     in
91
92         -- Gather type variables to quantify over
93     zonkTcTypes (rule_ty : map idType tpl_ids)          `thenNF_Tc` \ zonked_tys ->
94     let
95         tpl_tvs = tyVarsOfTypes zonked_tys
96     in
97
98         -- RHS can be a bit more lenient.  In particular,
99         -- we let constant dictionaries etc float outwards
100     tcSimplifyAndCheck (text "tcRule") tpl_tvs 
101                        lhs_dicts rhs_lie                `thenTc` \ (lie', rhs_binds) ->
102
103     returnTc (lie', HsRule      name (varSetElems tpl_tvs)
104                                 (map RuleBndr tpl_ids)  -- yuk
105                                 (mkHsLet lhs_binds lhs')
106                                 (mkHsLet rhs_binds rhs')
107                                 src_loc)
108   where
109     new_id (RuleBndr var)          = newTyVarTy openTypeKind    `thenNF_Tc` \ ty ->
110                                      returnNF_Tc (mkVanillaId var ty)
111     new_id (RuleBndrSig var rn_ty) = tcHsSigType rn_ty  `thenTc` \ ty ->
112                                      returnNF_Tc (mkVanillaId var ty)
113
114 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+> 
115                 doubleQuotes (ptext name)
116 \end{code}