[project @ 2000-11-06 08:15:20 by simonpj]
[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         ( PackageRuleBase )
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, isLocalThing )
23 import Rules            ( extendRuleBase )
24 import Inst             ( LIE, emptyLIE, plusLIEs, instToId )
25 import Id               ( idType, idName, mkVanillaId )
26 import Module           ( Module )
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 :: PackageRuleBase -> Module -> [RenamedHsDecl] 
36         -> TcM (PackageRuleBase, LIE, [TypecheckedRuleDecl])
37 tcRules pkg_rule_base mod decls 
38   = mapAndUnzipTc tcRule [rule | RuleD rule <- decls]   `thenTc` \ (lies, new_rules) ->
39     let
40         (local_rules, imported_rules) = partition is_local new_rules
41         new_rule_base = foldl add pkg_rule_base imported_rules
42     in
43     returnTc (new_rule_base, plusLIEs lies, local_rules)
44   where
45     add rule_base (IfaceRuleOut id rule) = extendRuleBase rule_base (id, rule)
46
47         -- When relinking this module from its interface-file decls
48         -- we'll have IfaceRules that are in fact local to this module
49     is_local (IfaceRuleOut n _) = isLocalThing mod n
50     is_local other              = True
51
52 tcRule :: RenamedRuleDecl -> TcM (LIE, TypecheckedRuleDecl)
53   -- No zonking necessary!
54 tcRule (IfaceRule name vars fun args rhs src_loc)
55   = tcAddSrcLoc src_loc                 $
56     tcAddErrCtxt (ruleCtxt name)        $
57     tcVar fun                           `thenTc` \ fun' ->
58     tcCoreLamBndrs vars                 $ \ vars' ->
59     mapTc tcCoreExpr args               `thenTc` \ args' ->
60     tcCoreExpr rhs                      `thenTc` \ rhs' ->
61     returnTc (emptyLIE, IfaceRuleOut fun' (Rule name vars' args' rhs'))
62
63 tcRule (HsRule name sig_tvs vars lhs rhs src_loc)
64   = tcAddSrcLoc src_loc                                 $
65     tcAddErrCtxt (ruleCtxt name)                        $
66     newTyVarTy openTypeKind                             `thenNF_Tc` \ rule_ty ->
67
68         -- Deal with the tyvars mentioned in signatures
69     tcTyVars sig_tvs (mapTc_ kcHsSigType sig_tys)       `thenTc` \ sig_tyvars ->
70     tcExtendTyVarEnv sig_tyvars (
71
72                 -- Ditto forall'd variables
73         mapNF_Tc new_id vars                                    `thenNF_Tc` \ ids ->
74         tcExtendLocalValEnv [(idName id, id) | id <- ids]       $
75         
76                 -- Now LHS and RHS
77         tcExpr lhs rule_ty                                      `thenTc` \ (lhs', lhs_lie) ->
78         tcExpr rhs rule_ty                                      `thenTc` \ (rhs', rhs_lie) ->
79         
80         returnTc (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie)
81     )                                           `thenTc` \ (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie) ->
82
83                 -- Check that LHS has no overloading at all
84     tcSimplifyToDicts lhs_lie                           `thenTc` \ (lhs_dicts, lhs_binds) ->
85     checkSigTyVars sig_tyvars emptyVarSet               `thenTc_`
86
87         -- Gather the template variables and tyvars
88     let
89         tpl_ids = map instToId (bagToList lhs_dicts) ++ ids
90
91         -- IMPORTANT!  We *quantify* over any dicts that appear in the LHS
92         -- Reason: 
93         --      a) The particular dictionary isn't important, because its value
94         --         depends only on the type
95         --              e.g     gcd Int $fIntegralInt
96         --         Here we'd like to match against (gcd Int any_d) for any 'any_d'
97         --
98         --      b) We'd like to make available the dictionaries bound 
99         --         on the LHS in the RHS, so quantifying over them is good
100         --         See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
101     in
102
103         -- Gather type variables to quantify over
104         -- and turn them into real TyVars (just as in TcBinds.tcBindWithSigs)
105     zonkTcTypes (rule_ty : map idType tpl_ids)                          `thenNF_Tc` \ zonked_tys ->
106     mapTc zonkTcTyVarToTyVar (varSetElems (tyVarsOfTypes zonked_tys))   `thenTc` \ tvs ->
107
108         -- RHS can be a bit more lenient.  In particular,
109         -- we let constant dictionaries etc float outwards
110     tcSimplifyAndCheck (text "tcRule") (mkVarSet tvs)
111                        lhs_dicts rhs_lie                `thenTc` \ (lie', rhs_binds) ->
112
113     returnTc (lie', HsRule      name tvs
114                                 (map RuleBndr tpl_ids)  -- yuk
115                                 (mkHsLet lhs_binds lhs')
116                                 (mkHsLet rhs_binds rhs')
117                                 src_loc)
118   where
119     sig_tys = [t | RuleBndrSig _ t <- vars]
120
121     new_id (RuleBndr var)          = newTyVarTy openTypeKind    `thenNF_Tc` \ ty ->
122                                      returnNF_Tc (mkVanillaId var ty)
123     new_id (RuleBndrSig var rn_ty) = tcHsSigType rn_ty  `thenTc` \ ty ->
124                                      returnNF_Tc (mkVanillaId var ty)
125
126 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+> 
127                 doubleQuotes (ptext name)
128 \end{code}
129
130
131
132