3e3e90f179d3fa3232c54e10164e44d1f7ab4127
[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(..) )
12 import CoreSyn          ( CoreRule(..) )
13 import RnHsSyn          ( RenamedHsDecl, RenamedRuleDecl )
14 import HscTypes         ( PackageRuleEnv )
15 import TcHsSyn          ( TypecheckedRuleDecl, mkHsLet )
16 import TcMonad
17 import TcSimplify       ( tcSimplifyToDicts, tcSimplifyAndCheck )
18 import TcType           ( zonkTcTypes, zonkTcTyVarToTyVar, newTyVarTy )
19 import TcIfaceSig       ( tcCoreExpr, tcCoreLamBndrs, tcVar )
20 import TcMonoType       ( kcHsSigType, tcHsSigType, tcTyVars, checkSigTyVars )
21 import TcExpr           ( tcExpr )
22 import TcEnv            ( tcExtendLocalValEnv, tcExtendTyVarEnv )
23 import Rules            ( extendRuleBase )
24 import Inst             ( LIE, plusLIEs, instToId )
25 import Id               ( idType, idName, mkVanillaId )
26 import Name             ( Name, extendNameEnvList )
27 import VarSet
28 import Type             ( tyVarsOfTypes, openTypeKind )
29 import Bag              ( bagToList )
30 import List             ( partition )
31 import Outputable
32 \end{code}
33
34 \begin{code}
35 tcRules :: PackageRuleEnv -> [RenamedHsDecl] -> TcM (PackageRuleEnv, LIE, [TypecheckedRuleDecl])
36 tcRules pkg_rule_env decls 
37   = mapAndUnzipTc tcLocalRule local_rules       `thenTc` \ (lies, new_local_rules) ->
38     mapTc tcIfaceRule imported_rules            `thenTc` \ new_imported_rules ->
39     returnTc (extendRuleBaseList pkg_rule_env new_imported_rules,
40               plusLIEs lies, new_local_rules)
41   where
42     rule_decls = [rule | RuleD rule <- decls]
43     (imported_rules, local_rules) = partition is_iface_rule rule_decls
44
45     is_iface_rule (IfaceRule _ _ _ _ _ _) = True
46     is_iface_rule other                   = False
47
48 tcIfaceRule :: RenamedRuleDecl -> TcM (Id, CoreRule)
49   -- No zonking necessary!
50 tcIfaceRule (IfaceRule name vars fun args rhs src_loc)
51   = tcAddSrcLoc src_loc                 $
52     tcAddErrCtxt (ruleCtxt name)        $
53     tcVar fun                           `thenTc` \ fun' ->
54     tcCoreLamBndrs vars                 $ \ vars' ->
55     mapTc tcCoreExpr args               `thenTc` \ args' ->
56     tcCoreExpr rhs                      `thenTc` \ rhs' ->
57     returnTc (fun', Rule name vars' args' rhs')
58
59 tcLocalRule :: RenamedRuleDecl -> TcM (LIE, TypecheckedRuleDecl)
60 tcLocalRule (HsRule name sig_tvs vars lhs rhs src_loc)
61   = tcAddSrcLoc src_loc                                 $
62     tcAddErrCtxt (ruleCtxt name)                        $
63     newTyVarTy openTypeKind                             `thenNF_Tc` \ rule_ty ->
64
65         -- Deal with the tyvars mentioned in signatures
66     tcTyVars sig_tvs (mapTc_ kcHsSigType sig_tys)       `thenTc` \ sig_tyvars ->
67     tcExtendTyVarEnv sig_tyvars (
68
69                 -- Ditto forall'd variables
70         mapNF_Tc new_id vars                                    `thenNF_Tc` \ ids ->
71         tcExtendLocalValEnv [(idName id, id) | id <- ids]       $
72         
73                 -- Now LHS and RHS
74         tcExpr lhs rule_ty                                      `thenTc` \ (lhs', lhs_lie) ->
75         tcExpr rhs rule_ty                                      `thenTc` \ (rhs', rhs_lie) ->
76         
77         returnTc (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie)
78     )                                           `thenTc` \ (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie) ->
79
80                 -- Check that LHS has no overloading at all
81     tcSimplifyToDicts lhs_lie                           `thenTc` \ (lhs_dicts, lhs_binds) ->
82     checkSigTyVars sig_tyvars emptyVarSet               `thenTc_`
83
84         -- Gather the template variables and tyvars
85     let
86         tpl_ids = map instToId (bagToList lhs_dicts) ++ ids
87
88         -- IMPORTANT!  We *quantify* over any dicts that appear in the LHS
89         -- Reason: 
90         --      a) The particular dictionary isn't important, because its value
91         --         depends only on the type
92         --              e.g     gcd Int $fIntegralInt
93         --         Here we'd like to match against (gcd Int any_d) for any 'any_d'
94         --
95         --      b) We'd like to make available the dictionaries bound 
96         --         on the LHS in the RHS, so quantifying over them is good
97         --         See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
98     in
99
100         -- Gather type variables to quantify over
101         -- and turn them into real TyVars (just as in TcBinds.tcBindWithSigs)
102     zonkTcTypes (rule_ty : map idType tpl_ids)                          `thenNF_Tc` \ zonked_tys ->
103     mapTc zonkTcTyVarToTyVar (varSetElems (tyVarsOfTypes zonked_tys))   `thenTc` \ tvs ->
104
105         -- RHS can be a bit more lenient.  In particular,
106         -- we let constant dictionaries etc float outwards
107     tcSimplifyAndCheck (text "tcRule") (mkVarSet tvs)
108                        lhs_dicts rhs_lie                `thenTc` \ (lie', rhs_binds) ->
109
110     returnTc (lie', HsRule      name tvs
111                                 (map RuleBndr tpl_ids)  -- yuk
112                                 (mkHsLet lhs_binds lhs')
113                                 (mkHsLet rhs_binds rhs')
114                                 src_loc)
115   where
116     sig_tys = [t | RuleBndrSig _ t <- vars]
117
118     new_id (RuleBndr var)          = newTyVarTy openTypeKind    `thenNF_Tc` \ ty ->
119                                      returnNF_Tc (mkVanillaId var ty)
120     new_id (RuleBndrSig var rn_ty) = tcHsSigType rn_ty  `thenTc` \ ty ->
121                                      returnNF_Tc (mkVanillaId var ty)
122
123 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+> 
124                 doubleQuotes (ptext name)
125 \end{code}
126
127
128
129