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