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