[project @ 2002-03-08 15:50:53 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(..), collectRuleBndrSigTys )
12 import CoreSyn          ( CoreRule(..) )
13 import RnHsSyn          ( RenamedRuleDecl )
14 import TcHsSyn          ( TypecheckedRuleDecl, mkHsLet )
15 import TcMonad
16 import TcSimplify       ( tcSimplifyToDicts, tcSimplifyInferCheck )
17 import TcMType          ( newTyVarTy )
18 import TcType           ( tyVarsOfTypes, openTypeKind )
19 import TcIfaceSig       ( tcCoreExpr, tcCoreLamBndrs, tcVar )
20 import TcMonoType       ( tcHsSigType, UserTypeCtxt(..), tcAddScopedTyVars )
21 import TcExpr           ( tcMonoExpr )
22 import TcEnv            ( tcExtendLocalValEnv, tcLookupId )
23 import Inst             ( LIE, plusLIEs, emptyLIE, instToId )
24 import Id               ( idType, mkLocalId )
25 import Outputable
26 \end{code}
27
28 \begin{code}
29 tcIfaceRules :: [RenamedRuleDecl] -> TcM [TypecheckedRuleDecl]
30 tcIfaceRules decls = mapTc tcIfaceRule decls
31
32 tcIfaceRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
33   -- No zonking necessary!
34 tcIfaceRule (IfaceRule name act vars fun args rhs src_loc)
35   = tcAddSrcLoc src_loc                 $
36     tcAddErrCtxt (ruleCtxt name)        $
37     tcVar fun                           `thenTc` \ fun' ->
38     tcCoreLamBndrs vars                 $ \ vars' ->
39     mapTc tcCoreExpr args               `thenTc` \ args' ->
40     tcCoreExpr rhs                      `thenTc` \ rhs' ->
41     returnTc (IfaceRuleOut fun' (Rule name act vars' args' rhs'))
42
43 tcIfaceRule (IfaceRuleOut fun rule)     -- Built-in rules come this way
44   = tcVar fun                           `thenTc` \ fun' ->
45     returnTc (IfaceRuleOut fun' rule)   
46
47 tcSourceRules :: [RenamedRuleDecl] -> TcM (LIE, [TypecheckedRuleDecl])
48 tcSourceRules decls
49   = mapAndUnzipTc tcSourceRule decls    `thenTc` \ (lies, decls') ->
50     returnTc (plusLIEs lies, decls')
51
52 tcSourceRule (IfaceRuleOut fun rule)    -- Built-in rules come this way
53                                         -- if they are from the module being compiled
54   = tcLookupId fun                      `thenTc` \ fun' ->
55     returnTc (emptyLIE, IfaceRuleOut fun' rule)   
56
57 tcSourceRule (HsRule name act vars lhs rhs src_loc)
58   = tcAddSrcLoc src_loc                                 $
59     tcAddErrCtxt (ruleCtxt name)                        $
60     newTyVarTy openTypeKind                             `thenNF_Tc` \ rule_ty ->
61
62         -- Deal with the tyvars mentioned in signatures
63     tcAddScopedTyVars (collectRuleBndrSigTys vars) (
64
65                 -- Ditto forall'd variables
66         mapNF_Tc new_id vars                            `thenNF_Tc` \ ids ->
67         tcExtendLocalValEnv ids                         $
68         
69                 -- Now LHS and RHS
70         tcMonoExpr lhs rule_ty                          `thenTc` \ (lhs', lhs_lie) ->
71         tcMonoExpr rhs rule_ty                          `thenTc` \ (rhs', rhs_lie) ->
72         
73         returnTc (ids, lhs', rhs', lhs_lie, rhs_lie)
74     )                                           `thenTc` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
75
76                 -- Check that LHS has no overloading at all
77     tcSimplifyToDicts lhs_lie                   `thenTc` \ (lhs_dicts, lhs_binds) ->
78
79         -- Gather the template variables and tyvars
80     let
81         tpl_ids = map instToId lhs_dicts ++ ids
82
83         -- IMPORTANT!  We *quantify* over any dicts that appear in the LHS
84         -- Reason: 
85         --      a) The particular dictionary isn't important, because its value
86         --         depends only on the type
87         --              e.g     gcd Int $fIntegralInt
88         --         Here we'd like to match against (gcd Int any_d) for any 'any_d'
89         --
90         --      b) We'd like to make available the dictionaries bound 
91         --         on the LHS in the RHS, so quantifying over them is good
92         --         See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
93
94         -- We initially quantify over any tyvars free in *either* the rule
95         -- *or* the bound variables.  The latter is important.  Consider
96         --      ss (x,(y,z)) = (x,z)
97         --      RULE:  forall v. fst (ss v) = fst v
98         -- The type of the rhs of the rule is just a, but v::(a,(b,c))
99         --
100         -- It's still conceivable that there may be type variables mentioned
101         -- in the LHS, but not in the type of the lhs, nor in the binders.
102         -- They'll get zapped to (), but that's over-constraining really.
103         -- Let's see if we get a problem.
104         forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
105     in
106
107         -- RHS can be a bit more lenient.  In particular,
108         -- we let constant dictionaries etc float outwards
109         --
110         -- 
111     tcSimplifyInferCheck (text "tcRule")
112                          forall_tvs
113                          lhs_dicts rhs_lie      `thenTc` \ (forall_tvs1, lie', rhs_binds) ->
114
115     returnTc (lie', HsRule      name act
116                                 (map RuleBndr (forall_tvs1 ++ tpl_ids)) -- yuk
117                                 (mkHsLet lhs_binds lhs')
118                                 (mkHsLet rhs_binds rhs')
119                                 src_loc)
120   where
121     new_id (RuleBndr var)          = newTyVarTy openTypeKind                    `thenNF_Tc` \ ty ->
122                                      returnNF_Tc (mkLocalId var ty)
123     new_id (RuleBndrSig var rn_ty) = tcHsSigType (RuleSigCtxt var) rn_ty        `thenTc` \ ty ->
124                                      returnNF_Tc (mkLocalId var ty)
125
126 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+> 
127                 doubleQuotes (ptext name)
128 \end{code}
129
130
131
132