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