[project @ 2000-11-24 09:51:38 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcRules.lhs
index 808165d..6a2a0b3 100644 (file)
@@ -4,61 +4,76 @@
 \section[TcRules]{Typechecking transformation rules}
 
 \begin{code}
-module TcRules ( tcRules ) where
+module TcRules ( tcIfaceRules, tcSourceRules ) where
 
 #include "HsVersions.h"
 
-import HsSyn           ( HsDecl(..), RuleDecl(..), RuleBndr(..), HsTyVarBndr(..) )
+import HsSyn           ( RuleDecl(..), RuleBndr(..) )
 import CoreSyn         ( CoreRule(..) )
-import RnHsSyn         ( RenamedHsDecl )
+import RnHsSyn         ( RenamedRuleDecl )
+import HscTypes                ( PackageRuleBase )
 import TcHsSyn         ( TypecheckedRuleDecl, mkHsLet )
 import TcMonad
 import TcSimplify      ( tcSimplifyToDicts, tcSimplifyAndCheck )
 import TcType          ( zonkTcTypes, zonkTcTyVarToTyVar, newTyVarTy )
 import TcIfaceSig      ( tcCoreExpr, tcCoreLamBndrs, tcVar )
-import TcMonoType      ( kcTyVarScope, kcHsSigType, tcHsSigType, newSigTyVars, checkSigTyVars )
+import TcMonoType      ( kcHsSigType, tcHsSigType, tcTyVars, checkSigTyVars )
 import TcExpr          ( tcExpr )
-import TcEnv           ( tcExtendLocalValEnv, newLocalId,
-                         tcExtendTyVarEnv
-                       )
-import Inst            ( LIE, emptyLIE, plusLIEs, instToId )
+import TcEnv           ( tcExtendLocalValEnv, tcExtendTyVarEnv, isLocalThing )
+import Rules           ( extendRuleBase )
+import Inst            ( LIE, plusLIEs, instToId )
 import Id              ( idType, idName, mkVanillaId )
+import Module          ( Module )
 import VarSet
 import Type            ( tyVarsOfTypes, openTypeKind )
 import Bag             ( bagToList )
+import List            ( partition )
 import Outputable
-import Util
 \end{code}
 
 \begin{code}
-tcRules :: [RenamedHsDecl] -> TcM s (LIE, [TypecheckedRuleDecl])
-tcRules decls = mapAndUnzipTc tcRule [rule | RuleD rule <- decls]      `thenTc` \ (lies, rules) ->
-               returnTc (plusLIEs lies, rules)
+tcIfaceRules :: PackageRuleBase -> Module -> [RenamedRuleDecl] 
+            -> TcM (PackageRuleBase, [TypecheckedRuleDecl])
+tcIfaceRules pkg_rule_base mod decls 
+  = mapTc tcIfaceRule decls            `thenTc` \ new_rules ->
+    let
+       (local_rules, imported_rules) = partition is_local new_rules
+       new_rule_base = foldl add pkg_rule_base imported_rules
+    in
+    returnTc (new_rule_base, local_rules)
+  where
+    add rule_base (IfaceRuleOut id rule) = extendRuleBase rule_base (id, rule)
 
-tcRule (IfaceRule name vars fun args rhs src_loc)
+       -- When relinking this module from its interface-file decls
+       -- we'll have IfaceRules that are in fact local to this module
+    is_local (IfaceRuleOut n _) = isLocalThing mod n
+    is_local other             = True
+
+tcIfaceRule :: RenamedRuleDecl -> TcM TypecheckedRuleDecl
+  -- No zonking necessary!
+tcIfaceRule (IfaceRule name vars fun args rhs src_loc)
   = tcAddSrcLoc src_loc                $
     tcAddErrCtxt (ruleCtxt name)       $
     tcVar fun                          `thenTc` \ fun' ->
     tcCoreLamBndrs vars                        $ \ vars' ->
     mapTc tcCoreExpr args              `thenTc` \ args' ->
     tcCoreExpr rhs                     `thenTc` \ rhs' ->
-    returnTc (emptyLIE, IfaceRuleOut fun' (Rule name vars' args' rhs'))
+    returnTc (IfaceRuleOut fun' (Rule name vars' args' rhs'))
+
 
-tcRule (IfaceRuleOut fun rule)
-  = tcVar fun                          `thenTc` \ fun' ->
-    returnTc (emptyLIE, IfaceRuleOut fun' rule)
+tcSourceRules :: [RenamedRuleDecl] -> TcM (LIE, [TypecheckedRuleDecl])
+tcSourceRules decls
+  = mapAndUnzipTc tcSourceRule decls   `thenTc` \ (lies, decls') ->
+    returnTc (plusLIEs lies, decls')
 
-tcRule (HsRule name sig_tvs vars lhs rhs src_loc)
+tcSourceRule (HsRule name sig_tvs vars lhs rhs src_loc)
   = tcAddSrcLoc src_loc                                $
     tcAddErrCtxt (ruleCtxt name)                       $
     newTyVarTy openTypeKind                            `thenNF_Tc` \ rule_ty ->
 
        -- Deal with the tyvars mentioned in signatures
-       -- Yuk to the UserTyVar
-    kcTyVarScope (map UserTyVar sig_tvs)
-                (mapTc_ kcHsSigType sig_tys)   `thenTc` \ sig_tv_kinds ->
-    newSigTyVars sig_tv_kinds                  `thenNF_Tc` \ sig_tyvars ->
-    tcExtendTyVarEnv sig_tyvars                (       
+    tcTyVars sig_tvs (mapTc_ kcHsSigType sig_tys)      `thenTc` \ sig_tyvars ->
+    tcExtendTyVarEnv sig_tyvars (
 
                -- Ditto forall'd variables
        mapNF_Tc new_id vars                                    `thenNF_Tc` \ ids ->
@@ -68,8 +83,8 @@ tcRule (HsRule name sig_tvs vars lhs rhs src_loc)
        tcExpr lhs rule_ty                                      `thenTc` \ (lhs', lhs_lie) ->
        tcExpr rhs rule_ty                                      `thenTc` \ (rhs', rhs_lie) ->
        
-       returnTc (ids, lhs', rhs', lhs_lie, rhs_lie)
-    )                                          `thenTc` \ (ids, lhs', rhs', lhs_lie, rhs_lie) ->
+       returnTc (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie)
+    )                                          `thenTc` \ (sig_tyvars, ids, lhs', rhs', lhs_lie, rhs_lie) ->
 
                -- Check that LHS has no overloading at all
     tcSimplifyToDicts lhs_lie                          `thenTc` \ (lhs_dicts, lhs_binds) ->
@@ -117,3 +132,7 @@ tcRule (HsRule name sig_tvs vars lhs rhs src_loc)
 ruleCtxt name = ptext SLIT("When checking the transformation rule") <+> 
                doubleQuotes (ptext name)
 \end{code}
+
+
+
+