[project @ 2003-07-02 14:59:00 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            ( RuleDecl(..), RuleBndr(..), collectRuleBndrSigTys )
12 import CoreSyn          ( CoreRule(..) )
13 import RnHsSyn          ( RenamedRuleDecl )
14 import TcHsSyn          ( TypecheckedRuleDecl, mkHsLet )
15 import TcRnMonad
16 import TcSimplify       ( tcSimplifyToDicts, tcSimplifyInferCheck )
17 import TcMType          ( newTyVarTy )
18 import TcType           ( tyVarsOfTypes, openTypeKind )
19 import TcIfaceSig       ( tcCoreExpr, tcCoreLamBndrs )
20 import TcMonoType       ( tcHsSigType, UserTypeCtxt(..), tcAddScopedTyVars )
21 import TcExpr           ( tcCheckRho )
22 import TcEnv            ( tcExtendLocalValEnv, tcLookupGlobalId, tcLookupId )
23 import Inst             ( instToId )
24 import Id               ( idType, mkLocalId )
25 import Outputable
26 \end{code}
27
28 \begin{code}
29 tcRules :: [RenamedRuleDecl] -> TcM [TypecheckedRuleDecl]
30 tcRules decls = mappM tcRule decls
31
32 tcRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
33 tcRule (IfaceRule name act vars fun args rhs src_loc)
34   = addSrcLoc src_loc           $
35     addErrCtxt (ruleCtxt name)  $
36     tcLookupGlobalId fun                `thenM` \ fun' ->
37     tcCoreLamBndrs vars                 $ \ vars' ->
38     mappM tcCoreExpr args               `thenM` \ args' ->
39     tcCoreExpr rhs                      `thenM` \ rhs' ->
40     returnM (IfaceRuleOut fun' (Rule name act vars' args' rhs'))
41
42 tcRule (IfaceRuleOut fun rule)  -- Built-in rules, and only built-in rules, 
43                                 -- come this way.  Usually IfaceRuleOut is only
44                                 -- used for the *output* of the type checker
45   = tcLookupId fun              `thenM` \ fun' ->
46         -- NB: tcLookupId, not tcLookupGlobalId
47         -- Reason: when compiling GHC.Base, where eqString is defined,
48         --         we'll get the builtin rule for eqString, but eqString
49         --         will be in the *local* type environment.
50         -- Seems like a bit of a hack
51     returnM (IfaceRuleOut fun' rule)   
52
53 tcRule (HsRule name act vars lhs rhs src_loc)
54   = addSrcLoc src_loc                           $
55     addErrCtxt (ruleCtxt name)                  $
56     newTyVarTy openTypeKind                             `thenM` \ rule_ty ->
57
58         -- Deal with the tyvars mentioned in signatures
59     tcAddScopedTyVars (collectRuleBndrSigTys vars) (
60
61                 -- Ditto forall'd variables
62         mappM new_id vars                       `thenM` \ ids ->
63         tcExtendLocalValEnv ids                 $
64         
65                 -- Now LHS and RHS
66         getLIE (tcCheckRho lhs rule_ty) `thenM` \ (lhs', lhs_lie) ->
67         getLIE (tcCheckRho rhs rule_ty) `thenM` \ (rhs', rhs_lie) ->
68         
69         returnM (ids, lhs', rhs', lhs_lie, rhs_lie)
70     )                           `thenM` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
71
72                 -- Check that LHS has no overloading at all
73     getLIE (tcSimplifyToDicts lhs_lie)  `thenM` \ (lhs_binds, lhs_dicts) ->
74
75         -- Gather the template variables and tyvars
76     let
77         tpl_ids = map instToId lhs_dicts ++ ids
78
79         -- IMPORTANT!  We *quantify* over any dicts that appear in the LHS
80         -- Reason: 
81         --      a) The particular dictionary isn't important, because its value
82         --         depends only on the type
83         --              e.g     gcd Int $fIntegralInt
84         --         Here we'd like to match against (gcd Int any_d) for any 'any_d'
85         --
86         --      b) We'd like to make available the dictionaries bound 
87         --         on the LHS in the RHS, so quantifying over them is good
88         --         See the 'lhs_dicts' in tcSimplifyAndCheck for the RHS
89
90         -- We initially quantify over any tyvars free in *either* the rule
91         -- *or* the bound variables.  The latter is important.  Consider
92         --      ss (x,(y,z)) = (x,z)
93         --      RULE:  forall v. fst (ss v) = fst v
94         -- The type of the rhs of the rule is just a, but v::(a,(b,c))
95         --
96         -- We also need to get the free tyvars of the LHS; but we do that
97         -- during zonking (see TcHsSyn.zonkRule)
98         --
99         forall_tvs = tyVarsOfTypes (rule_ty : map idType tpl_ids)
100     in
101         -- RHS can be a bit more lenient.  In particular,
102         -- we let constant dictionaries etc float outwards
103         --
104         -- NB: tcSimplifyInferCheck zonks the forall_tvs, and 
105         --     knocks out any that are constrained by the environment
106     tcSimplifyInferCheck (text "tcRule")
107                          forall_tvs
108                          lhs_dicts rhs_lie      `thenM` \ (forall_tvs1, rhs_binds) ->
109
110     returnM (HsRule name act
111                     (map RuleBndr (forall_tvs1 ++ tpl_ids))     -- yuk
112                     (mkHsLet lhs_binds lhs')
113                     (mkHsLet rhs_binds rhs')
114                     src_loc)
115   where
116     new_id (RuleBndr var)          = newTyVarTy openTypeKind                    `thenM` \ ty ->
117                                      returnM (mkLocalId var ty)
118     new_id (RuleBndrSig var rn_ty) = tcHsSigType (RuleSigCtxt var) rn_ty        `thenM` \ ty ->
119                                      returnM (mkLocalId var ty)
120
121 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+> 
122                 doubleQuotes (ftext name)
123 \end{code}
124
125
126
127