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