[replace most of the module renaming code with a custom monad and a type class to greatly simplify code
John Meacham <john@repetae.net>**20080218125920] hunk ./FrontEnd/Rename.hs 1
-{-------------------------------------------------------------------------------
-
-        Copyright:              The Hatchet Team (see file Contributors)
-
-        Module:                 Rename
-
-        Description:            Renames variables apart in a Module
-
-        Primary Authors:        Toby Ord, Bryn Humberstone, Bernie Pope
-
-        Notes:                  See the file License for license information
-
--------------------------------------------------------------------------------}
-
-{-------------------------------------------------------------------------------
-
-  Major changes by John, many comments probably invalid.
-  This also desugars records
-
-  Implementation:
-
-
-     The algorithm then proceeds through the syntax tree, from outermost scope
-     to innermost in a depth first manner.
-
-     On entering a new scope, updateSubTableWith* is used to get the new names
-     in this scope, putting them into the current subTable, clobbering any
-     identifiers with the same name in outer scopes. It also creates their new
-     names (although no renaming is performed yet)
-
-     Now, all identifiers the algorithm finds before entering the next nested
-     scope will have a mapping to a new name in the subTable and their old
-     names get replaced by these
-
-
-  * bugs
-
-     It should work for records but it doesn't rename them because it never adds
-     them to the scope
-
-     It also needs more testing for records
-
-     It assumes that all PatBinds have only one identifier to the left of the equals
-     ie. x     = a b c d   is OK
-         (x,y) = a b c d   is not
-     this should not be too hard to change
-
-     It doesn't add information about the identifiers in class and instance
-     definitions to the identTable.
-
-        - The correct behaviour here is not obvious as these are the only
-          identifiers that are declared multiple times, so there is no unique
-          source location.
-        - The method of declaration is also different to normal as identifiers
-          are normally added to the identTable when they are first added to the
-          scope, but these are added in the class
-          pass and can't be re-added.
-
-     It doesn't rename type signatures that do not have an associated PatBind
-     or FunBind
-
--------------------------------------------------------------------------------}
-
hunk ./FrontEnd/Rename.hs 4
+import Control.Monad.Identity
+import Control.Monad.RWS
hunk ./FrontEnd/Rename.hs 8
-import Control.Monad.Identity
-import qualified Data.Traversable as T
+import Control.Applicative
hunk ./FrontEnd/Rename.hs 14
+import qualified Data.Traversable as T
hunk ./FrontEnd/Rename.hs 36
---instance (Show a, Show b) => Show (FiniteMap a b) where
---    show fm = show (fmToList fm)
-
-
hunk ./FrontEnd/Rename.hs 48
-    currentModule  :: Module,
hunk ./FrontEnd/Rename.hs 52
-    nameMap        :: Map.Map Name (Either String Name),
-    fieldLabels    :: FieldMap,
-    errors         :: [Warning],
-    srcLoc         :: !SrcLoc
+    fieldLabels    :: FieldMap
hunk ./FrontEnd/Rename.hs 56
--- The monadic type
-type ScopeSM = State ScopeState
-runScopeSM s0 a = runState a s0
+data Env = Env {
+    envSubTable  :: Map.Map HsName HsName,  -- all these need to go away
+    envModule  :: Module,
+    envNameMap :: Map.Map Name (Either String Name),
+    envSrcLoc  :: SrcLoc
+}
+
+instance Applicative RM where
+    pure = return
+    (<*>) = ap
+
+newtype RM a = RM (RWS Env [Warning] ScopeState a)
+    deriving(Monad,Functor,MonadReader Env, MonadWriter [Warning], MonadState ScopeState)
hunk ./FrontEnd/Rename.hs 70
-instance MonadWarn ScopeSM where
-    addWarning w = modify (\s -> s { errors = w: errors s})
+unRM (RM x) = x
hunk ./FrontEnd/Rename.hs 72
-instance UniqueProducer ScopeSM where
+instance MonadWarn RM where
+    addWarning w = tell [w]
+
+instance UniqueProducer RM where
hunk ./FrontEnd/Rename.hs 82
-getCurrentModule :: ScopeSM Module
-getCurrentModule = gets currentModule
-
+getCurrentModule :: RM Module
+getCurrentModule = asks envModule
hunk ./FrontEnd/Rename.hs 85
-setSrcLoc e = modify (\s -> s { srcLoc =  e `mappend` srcLoc s })
hunk ./FrontEnd/Rename.hs 86
-instance MonadSrcLoc ScopeSM where
-    getSrcLoc = gets srcLoc
-instance MonadSetSrcLoc ScopeSM where
-    withSrcLoc sl a = modify (\s -> s { srcLoc = sl `mappend` srcLoc s}) >> a
hunk ./FrontEnd/Rename.hs 87
--- functions to modify the ScopeSM
+instance MonadSrcLoc RM where
+    getSrcLoc = asks envSrcLoc
+instance MonadSetSrcLoc RM where
+    withSrcLoc sl a = local (\s -> s { envSrcLoc = sl `mappend` envSrcLoc s}) a
hunk ./FrontEnd/Rename.hs 99
-addTopLevels ::  [HsDecl]  -> ScopeSM ()
+addTopLevels ::  [HsDecl]  -> RM ()
hunk ./FrontEnd/Rename.hs 112
-        mult xs@((n,sl):_) = warn sl "multiply-defined" (show n ++ " is defined multiple times: " ++ show xs )
+        mult xs@(~((n,sl):_)) = warn sl "multiply-defined" (show n ++ " is defined multiple times: " ++ show xs )
hunk ./FrontEnd/Rename.hs 125
-renameModule fls ns m = mapM_ addWarning (errors finalState) >> return renamedMod where
+renameModule fls ns m = mapM_ addWarning errors >> return renamedMod where
hunk ./FrontEnd/Rename.hs 128
+    nameMap = Map.fromList $ map f ns where
+        f (x,[y]) = (x,Right y)
+        f (x,ys)  = (x,Left $ ambig x ys)
hunk ./FrontEnd/Rename.hs 139
-        nameMap        = Map.empty,
-        errors         = [],
-        srcLoc         = mempty,
hunk ./FrontEnd/Rename.hs 141
-        fieldLabels    = fls,
-        currentModule  = hsModuleName m
+        fieldLabels    = fls
hunk ./FrontEnd/Rename.hs 144
-    (renamedMod, finalState) = runScopeSM startState (renameDecls m initialGlobalSubTable)
+    startEnv = Env {
+        envSubTable = initialGlobalSubTable,
+        envModule = hsModuleName m,
+        envNameMap  = nameMap,
+        envSrcLoc = mempty
+    }
+
+    (renamedMod, _, errors) = runRWS (unRM $ renameDecls m) startEnv startState
+
+renameOld :: (SubTable -> RM a) -> RM a
+renameOld rm = asks envSubTable >>= rm
+
+withSubTable :: SubTable -> RM a -> RM a
+withSubTable st action = local ( \e -> e { envSubTable = st `Map.union` envSubTable e }) action
hunk ./FrontEnd/Rename.hs 161
-renameStatement fls ns modName stmt = mapM_ addWarning (errors finalState) >> return renamedStmt where
+renameStatement fls ns modName stmt = mapM_ addWarning errors >> return renamedStmt where
hunk ./FrontEnd/Rename.hs 166
+    nameMap = Map.fromList $ map f ns where
+        f (x,[y]) = (x,Right y)
+        f (x,ys)  = (x,Left $ ambig x ys)
hunk ./FrontEnd/Rename.hs 175
-        nameMap        = Map.empty,
-        errors         = [],
-        srcLoc         = mempty,
hunk ./FrontEnd/Rename.hs 177
-        fieldLabels    = fls,
-        currentModule  = modName
+        fieldLabels    = fls
hunk ./FrontEnd/Rename.hs 179
-
-    (renamedStmt, finalState) = runScopeSM startState (renameHsStmt stmt initialGlobalSubTable)
-
--- This is Bryn's modification to make the code a bit easier to understand for
--- functions like renameHsNames, renameHsFileUpdates
-mapRename :: (a -> SubTable -> ScopeSM a) -> [a] -> SubTable -> ScopeSM [a]
-mapRename renameIndividual individuals subTable
-    = mapM (`renameIndividual` subTable) individuals
+    startEnv = Env {
+        envSubTable = initialGlobalSubTable,
+        envModule   = modName,
+        envNameMap  = nameMap,
+        envSrcLoc   = mempty
+    }
hunk ./FrontEnd/Rename.hs 186
+    (renamedStmt, _, errors) = runRWS (unRM $ rename stmt) startEnv startState
hunk ./FrontEnd/Rename.hs 189
-renameDecls :: HsModule -> SubTable -> ScopeSM HsModule
-renameDecls tidy subTable = do
+renameDecls :: HsModule -> RM HsModule
+renameDecls tidy = do
hunk ./FrontEnd/Rename.hs 192
-        subTable'a <- gets globalSubTable
-        let subTable' = subTable `Map.union` subTable'a
-        --addError (show $ fmToList subTable')
-        decls' <-  renameHsDeclsTL (hsModuleDecls tidy) subTable' ; return decls'
-        --addDiag (show syns)
-        --sta <- gets globalSubTable
-        --stb <- gets typeSubTable
-        --addDiag (show (sta, stb))
+        gst <- gets globalSubTable
+        withSubTable gst $ do
+--        local (\e -> e { envSubTable = envSubTable e `Map.union`  gst }) $ do
+        decls' <- rename (hsModuleDecls tidy)
+        mapM_ HsErrors.hsDeclTopLevel decls'
hunk ./FrontEnd/Rename.hs 207
-renameHsDecls :: [HsDecl] -> SubTable -> ScopeSM ([HsDecl])
-renameHsDecls  decls subtable = do
-    ans <- mapRename renameHsDecl (expandTypeSigs decls) subtable
-    mapM_ HsErrors.hsDeclLocal ans
-    return ans
-
-renameHsDeclsTL :: [HsDecl] -> SubTable -> ScopeSM ([HsDecl])
-renameHsDeclsTL  decls subtable = do
-    ans <- mapRename renameHsDecl (expandTypeSigs decls) subtable
-    mapM_ HsErrors.hsDeclTopLevel ans
-    return ans
-
-renameHsDeclsN :: [HsDecl] -> SubTable -> ScopeSM ([HsDecl])
-renameHsDeclsN  decls subtable = do
-    ans <- mapRename renameHsDecl (expandTypeSigs decls) subtable
-    return ans
hunk ./FrontEnd/Rename.hs 214
-renameHsDecl :: HsDecl -> SubTable -> ScopeSM (HsDecl)
-renameHsDecl (HsPatBind srcLoc hsPat hsRhs {-where-} hsDecls) subTable = do
-    setSrcLoc srcLoc
-    hsPat'    <- renameHsPat hsPat subTable
-    subTable' <- updateSubTableWithHsDecls subTable hsDecls
-    hsDecls'  <- renameHsDecls hsDecls subTable'
-    hsRhs'    <- renameHsRhs hsRhs subTable'
-    let patbind' = (HsPatBind srcLoc hsPat' hsRhs' {-where-} hsDecls')
-    return patbind'
+instance Rename HsDecl where
+    rename (HsPatBind srcLoc hsPat hsRhs {-where-} hsDecls) = do
+        withSrcLoc srcLoc $ do
+        hsPat'    <- rename hsPat
+        subTable' <- updateSubTableWithHsDecls mempty hsDecls
+        withSubTable subTable' $ do
+        hsDecls'  <- rename hsDecls
+        hsRhs'    <- rename hsRhs
+        return (HsPatBind srcLoc hsPat' hsRhs' {-where-} hsDecls')
hunk ./FrontEnd/Rename.hs 224
-renameHsDecl (HsForeignExport a b n t) subTable = do
-    setSrcLoc a
-    n <- renameHsName n subTable
-    subTable' <- updateSubTableWithHsQualType subTable t
-    t <- renameHsQualType t subTable'
-    return (HsForeignExport a b n t)
+    rename (HsForeignExport a b n t) = do
+        withSrcLoc a $ do
+        n <- rename n
+        subTable' <- updateSubTableWithHsQualType mempty t
+        withSubTable subTable' $ do
+        t <- rename t
+        return (HsForeignExport a b n t)
hunk ./FrontEnd/Rename.hs 232
-renameHsDecl (HsForeignDecl a b n t) subTable = do
-    setSrcLoc a
-    n <- renameHsName n subTable
-    subTable' <- updateSubTableWithHsQualType subTable t
-    t <- renameHsQualType t subTable'
-    return (HsForeignDecl a b n t)
+    rename (HsForeignDecl a b n t) = do
+        withSrcLoc a $ do
+        n <- rename n
+        subTable' <- updateSubTableWithHsQualType mempty t
+        withSubTable subTable' $ do
+        t <- rename t
+        return (HsForeignDecl a b n t)
hunk ./FrontEnd/Rename.hs 240
---renameHsDecl (HsFunBind srcLoc hsMatches) subTable
-renameHsDecl (HsFunBind hsMatches) subTable = do
-    hsMatches' <- renameAny hsMatches subTable
-    -- return (HsFunBind srcLoc hsMatches')
-    return (HsFunBind hsMatches')
+    rename (HsFunBind hsMatches) = do
+        hsMatches' <- rename hsMatches
+        return (HsFunBind hsMatches')
hunk ./FrontEnd/Rename.hs 244
-renameHsDecl (HsTypeSig srcLoc hsNames hsQualType) subTable = do
-    setSrcLoc srcLoc
-    hsNames' <- renameHsNames hsNames subTable
-    subTable' <- updateSubTableWithHsQualType subTable hsQualType
-    hsQualType' <- renameHsQualType hsQualType subTable'
-    return (HsTypeSig srcLoc hsNames' hsQualType')
-renameHsDecl dl@HsDataDecl { hsDeclSrcLoc = srcLoc, hsDeclContext = hsContext, hsDeclName = hsName, hsDeclArgs = hsNames1, hsDeclCons = hsConDecls, hsDeclDerives = hsNames2 } subTable = do
-    setSrcLoc srcLoc
-    hsName' <- renameTypeHsName hsName subTable
-    subTable' <- updateSubTableWithHsNames subTable hsNames1
-    hsContext' <- renameHsContext hsContext subTable'
-    hsNames1' <- renameHsNames hsNames1 subTable'
-    hsConDecls' <- renameHsConDecls hsConDecls subTable'
-    -- don't need to rename the hsNames2 as it is just a list of TypeClasses
-    hsNames2' <- mapM (`renameTypeHsName` subTable') hsNames2
-    return dl { hsDeclContext = hsContext', hsDeclName = hsName', hsDeclArgs = hsNames1', hsDeclCons = hsConDecls', hsDeclDerives = hsNames2' }
-renameHsDecl (HsTypeDecl srcLoc name hsNames t) subTable = do
-    setSrcLoc srcLoc
-    hsName' <- renameTypeHsName name subTable
-    subTable' <- updateSubTableWithHsNames subTable (Set.toList $ freeVars hsNames)
-    --subTable' <- updateSubTableWithHsNames subTable hsNames
-    hsNames' <- renameAny hsNames subTable'
-    t' <- renameHsType t subTable'
-    return (HsTypeDecl srcLoc  hsName' hsNames' t')
+    rename (HsTypeSig srcLoc hsNames hsQualType) = do
+        withSrcLoc srcLoc $ do
+        hsNames' <- rename hsNames
+        subTable' <- updateSubTableWithHsQualType mempty hsQualType
+        withSubTable subTable' $ do
+        hsQualType' <- rename hsQualType
+        return (HsTypeSig srcLoc hsNames' hsQualType')
+    rename dl@HsDataDecl { hsDeclSrcLoc = srcLoc, hsDeclContext = hsContext, hsDeclName = hsName, hsDeclArgs = hsNames1, hsDeclCons = hsConDecls, hsDeclDerives = hsNames2 } = do
+        withSrcLoc srcLoc $ do
+        hsName' <- renameTypeName hsName
+        subTable' <- updateSubTableWithHsNames mempty hsNames1
+        withSubTable subTable' $ do
+        hsContext' <- rename hsContext
+        hsNames1' <- rename hsNames1
+        hsConDecls' <- rename hsConDecls
+        -- don't need to rename the hsNames2 as it is just a list of TypeClasses
+        hsNames2' <- mapM renameTypeName hsNames2
+        return dl { hsDeclContext = hsContext', hsDeclName = hsName', hsDeclArgs = hsNames1', hsDeclCons = hsConDecls', hsDeclDerives = hsNames2' }
+    rename (HsTypeDecl srcLoc name hsNames t) = do
+        withSrcLoc srcLoc $ do
+        hsName' <- renameTypeName name
+        subTable' <- updateSubTableWithHsNames mempty (Set.toList $ freeVars hsNames)
+        withSubTable subTable' $ do
+        --subTable' <- updateSubTableWithHsNames subTable hsNames
+        hsNames' <- rename hsNames
+        t' <- rename t
+        return (HsTypeDecl srcLoc  hsName' hsNames' t')
hunk ./FrontEnd/Rename.hs 272
-renameHsDecl (HsNewTypeDecl srcLoc hsContext hsName hsNames1 hsConDecl hsNames2) subTable = do
-    setSrcLoc srcLoc
-    hsName' <- renameTypeHsName hsName subTable
-    subTable' <- updateSubTableWithHsNames subTable hsNames1
-    hsContext' <- renameHsContext hsContext subTable'
-    hsNames1' <- renameHsNames hsNames1 subTable'
-    hsConDecl' <- renameHsConDecl hsConDecl subTable'
-    -- don't need to rename the hsNames2 as it is just a list of TypeClasses
-    hsNames2' <- mapM (`renameTypeHsName` subTable') hsNames2
-    return (HsNewTypeDecl srcLoc hsContext' hsName' hsNames1' hsConDecl' hsNames2')
-renameHsDecl (HsClassDecl srcLoc hsQualType hsDecls) subTable = do
-    setSrcLoc srcLoc
-    startingSubTable <- return subTable
-    {- WAS: typeSigSubTable <- updateSubTableWithHsQualType initialSubTable hsQualType -}
-    typeSigSubTable <- updateSubTableWithHsQualType startingSubTable hsQualType
-    hsQualType' <- renameHsQualType hsQualType typeSigSubTable
-    doesClassMakeSense hsQualType'
-    hsDecls' <- renameHsDeclsN hsDecls subTable
-    return (HsClassDecl srcLoc hsQualType' hsDecls')
-renameHsDecl (HsInstDecl srcLoc hsQualType hsDecls) subTable = do
-    setSrcLoc srcLoc
-    subTable' <- updateSubTableWithHsQualType subTable hsQualType
-    hsQualType' <- renameHsQualType hsQualType subTable'
-    hsDecls' <- renameHsDeclsN hsDecls subTable'
-    return (HsInstDecl srcLoc hsQualType' hsDecls')
-renameHsDecl (HsInfixDecl srcLoc assoc int hsNames) subTable = do
-    setSrcLoc srcLoc
-    hsNames' <- renameHsNames hsNames subTable
-    return $ HsInfixDecl srcLoc assoc int hsNames'
-renameHsDecl (HsActionDecl srcLoc pat e) subTable = do
-    setSrcLoc srcLoc
-    pat <- renameAny pat subTable
-    e <- renameAny e subTable
-    return (HsActionDecl srcLoc pat e)
-renameHsDecl (HsPragmaProps srcLoc prop hsNames) subTable = do
-    setSrcLoc srcLoc
-    hsNames' <- renameHsNames hsNames subTable
-    return (HsPragmaProps  srcLoc prop hsNames')
-renameHsDecl (HsPragmaRules rs) subTable = do
-    rs' <- mapM (`renameHsRule` subTable) rs
-    return $ HsPragmaRules rs'
-renameHsDecl prules@HsPragmaSpecialize { hsDeclSrcLoc = srcLoc, hsDeclName = n, hsDeclType = t } subTable = do
-    setSrcLoc srcLoc
-    n <- renameAny n subTable
-    t <- renameAny t subTable
-    m <- getCurrentModule
-    i <- newUniq
-    return prules { hsDeclUniq = (m,i), hsDeclName = n, hsDeclType = t }
+    rename (HsNewTypeDecl srcLoc hsContext hsName hsNames1 hsConDecl hsNames2) = do
+        withSrcLoc srcLoc $ do
+        hsName' <- renameTypeName hsName
+        subTable' <- updateSubTableWithHsNames mempty hsNames1
+        withSubTable subTable' $ do
+        hsContext' <- rename hsContext
+        hsNames1' <- rename hsNames1
+        hsConDecl' <- rename hsConDecl
+        -- don't need to rename the hsNames2 as it is just a list of TypeClasses
+        hsNames2' <- mapM renameTypeName hsNames2
+        return (HsNewTypeDecl srcLoc hsContext' hsName' hsNames1' hsConDecl' hsNames2')
+    rename (HsClassDecl srcLoc hsQualType hsDecls) = do
+        withSrcLoc srcLoc $ do
+        startingSubTable <- asks envSubTable
+        {- WAS: typeSigSubTable <- updateSubTableWithHsQualType initialSubTable hsQualType -}
+        typeSigSubTable <- updateSubTableWithHsQualType startingSubTable hsQualType
+        hsQualType' <- withSubTable typeSigSubTable $ rename hsQualType
+        doesClassMakeSense hsQualType'
+        hsDecls' <- rename hsDecls
+        return (HsClassDecl srcLoc hsQualType' hsDecls')
+    rename (HsInstDecl srcLoc hsQualType hsDecls) = do
+        withSrcLoc srcLoc $ do
+        subTable' <- updateSubTableWithHsQualType mempty hsQualType
+        withSubTable subTable' $ do
+        hsQualType' <- rename hsQualType
+        hsDecls' <- rename hsDecls
+        return (HsInstDecl srcLoc hsQualType' hsDecls')
+    rename (HsInfixDecl srcLoc assoc int hsNames) = do
+        withSrcLoc srcLoc $ do
+        hsNames' <- rename hsNames
+        return $ HsInfixDecl srcLoc assoc int hsNames'
+    rename (HsActionDecl srcLoc pat e) = do
+        withSrcLoc srcLoc $ do
+        pat <- rename pat
+        e <- rename e
+        return (HsActionDecl srcLoc pat e)
+    rename (HsPragmaProps srcLoc prop hsNames) = do
+        withSrcLoc srcLoc $ do
+        hsNames' <- rename hsNames
+        return (HsPragmaProps  srcLoc prop hsNames')
+    rename (HsPragmaRules rs) = do
+        rs' <- rename rs
+        return $ HsPragmaRules rs'
+    rename prules@HsPragmaSpecialize { hsDeclSrcLoc = srcLoc, hsDeclName = n, hsDeclType = t } = do
+        withSrcLoc srcLoc $ do
+        n <- rename n
+        t <- rename t
+        m <- getCurrentModule
+        i <- newUniq
+        return prules { hsDeclUniq = (m,i), hsDeclName = n, hsDeclType = t }
hunk ./FrontEnd/Rename.hs 323
-renameHsDecl otherHsDecl _ = return otherHsDecl
hunk ./FrontEnd/Rename.hs 324
-renameHsRule prules@HsRule { hsRuleSrcLoc = srcLoc, hsRuleFreeVars = fvs, hsRuleLeftExpr = e1, hsRuleRightExpr = e2 } subTable = do
-    setSrcLoc srcLoc
-    subTable' <- updateSubTableWithHsNames subTable (fsts fvs)
-    subTable'' <- updateSubTableWithHsTypes subTable (catMaybes $ snds fvs)
-    fvs' <- sequence [ liftM2 (,) (renameAny x subTable') (renameAny y subTable'')| (x,y) <- fvs]
-    e1' <- renameHsExp e1 subTable'
-    e2' <- renameHsExp e2 subTable'
-    m <- getCurrentModule
-    i <- newUniq
-    return prules {  hsRuleUniq = (m,i), hsRuleFreeVars = fvs', hsRuleLeftExpr = e1', hsRuleRightExpr = e2' }
+instance Rename HsRule where
+    rename prules@HsRule { hsRuleSrcLoc = srcLoc, hsRuleFreeVars = fvs, hsRuleLeftExpr = e1, hsRuleRightExpr = e2 } = do
+        withSrcLoc srcLoc $ do
+        subTable' <- updateSubTableWithHsNames mempty (fsts fvs)
+        withSubTable subTable' $ do
+        subTable'' <- updateSubTableWithHsTypes mempty (catMaybes $ snds fvs)
+        fvs' <- sequence [ liftM2 (,) (rename x) (withSubTable subTable'' $ rename y)| (x,y) <- fvs]
+        e1' <- rename e1
+        e2' <- rename e2
+        m <- getCurrentModule
+        i <- newUniq
+        return prules {  hsRuleUniq = (m,i), hsRuleFreeVars = fvs', hsRuleLeftExpr = e1', hsRuleRightExpr = e2' }
hunk ./FrontEnd/Rename.hs 337
-doesClassMakeSense :: HsQualType -> ScopeSM ()
-doesClassMakeSense (HsQualType _ type_) =
- case type_ of
-  (HsTyApp (HsTyCon _) (HsTyVar _)) -> return ()
-  (HsTyApp (HsTyApp _ _) _)         -> failRename "Multiparameter typeclasses not supported"
-  (HsTyCon _)                       -> failRename "Typeclass with no parameters"
-  _                                 -> failRename $ "Invalid type in class declaration: "++show type_
+doesClassMakeSense :: HsQualType -> RM ()
+doesClassMakeSense (HsQualType _ type_) = case type_ of
+    (HsTyApp (HsTyCon _) (HsTyVar _)) -> return ()
+    (HsTyApp (HsTyApp _ _) _)         -> failRename "Multiparameter typeclasses not supported"
+    (HsTyCon _)                       -> failRename "Typeclass with no parameters"
+    _                                 -> failRename $ "Invalid type in class declaration: "++show type_
hunk ./FrontEnd/Rename.hs 344
-renameHsQualType :: HsQualType -> SubTable -> ScopeSM (HsQualType)
-renameHsQualType (HsQualType hsContext hsType) subTable = do
-      hsContext' <- renameHsContext hsContext subTable
-      hsType' <- renameHsType hsType subTable
-      return (HsQualType hsContext' hsType')
+instance Rename HsQualType where
+    rename (HsQualType hsContext hsType) = return HsQualType `ap` rename hsContext `ap` rename hsType
hunk ./FrontEnd/Rename.hs 347
-renameHsContext :: HsContext -> SubTable -> ScopeSM (HsContext)
-renameHsContext = mapRename renameHsAsst
hunk ./FrontEnd/Rename.hs 348
-renameHsAsst :: HsAsst -> SubTable -> ScopeSM HsAsst
-renameHsAsst (HsAsst hsName1  hsName2s) subTable = do
-      hsName1' <- renameTypeHsName hsName1 subTable  -- for class names
-      hsName2s' <- mapRename renameTypeHsName hsName2s subTable
-      return (HsAsst hsName1' hsName2s')
-renameHsAsst (HsAsstEq t1 t2) subTable = do
-      t1' <- renameHsType t1 subTable  -- for class names
-      t2' <- renameHsType t2 subTable  -- for class names
-      return (HsAsstEq t1' t2')
+instance Rename HsAsst where
+    rename (HsAsst hsName1  hsName2s) = do
+        hsName1' <- renameTypeName hsName1
+        hsName2s' <- mapM renameTypeName hsName2s
+        return (HsAsst hsName1' hsName2s')
+    rename (HsAsstEq t1 t2) = return HsAsstEq `ap` rename t1 `ap` rename t2
hunk ./FrontEnd/Rename.hs 355
-renameHsConDecls :: [HsConDecl] -> SubTable -> ScopeSM ([HsConDecl])
-renameHsConDecls = mapRename renameHsConDecl
hunk ./FrontEnd/Rename.hs 356
-renameHsConDecl :: HsConDecl -> SubTable -> ScopeSM (HsConDecl)
-renameHsConDecl cd@(HsConDecl { hsConDeclSrcLoc = srcLoc, hsConDeclName = hsName, hsConDeclConArg = hsBangTypes }) subTable = do
-    setSrcLoc srcLoc
-    hsName' <- renameHsName hsName subTable
-    subTable' <- updateSubTableWithHsNames subTable (map hsTyVarBindName (hsConDeclExists cd))
-    es <- renameAny (hsConDeclExists cd) subTable'
-    hsBangTypes' <- renameHsBangTypes hsBangTypes subTable'
-    return cd { hsConDeclName = hsName', hsConDeclConArg = hsBangTypes', hsConDeclExists = es }
-renameHsConDecl cd@HsRecDecl { hsConDeclSrcLoc = srcLoc, hsConDeclName = hsName, hsConDeclRecArg = stuff} subTable = do
-    setSrcLoc srcLoc
-    hsName' <- renameHsName hsName subTable
-    subTable' <- updateSubTableWithHsNames subTable (map hsTyVarBindName (hsConDeclExists cd))
-    es <- renameAny (hsConDeclExists cd) subTable'
-    stuff' <- sequence [ do ns' <- mapRename renameHsName ns subTable'; t' <- renameHsBangType t subTable; return (ns',t')  |  (ns,t) <- stuff]
-    return cd { hsConDeclName = hsName', hsConDeclRecArg = stuff', hsConDeclExists = es }
+instance Rename HsConDecl where
+    rename cd@(HsConDecl { hsConDeclSrcLoc = srcLoc, hsConDeclName = hsName, hsConDeclConArg = hsBangTypes }) = do
+        withSrcLoc srcLoc $ do
+        hsName' <- rename hsName
+        subTable' <- updateSubTableWithHsNames mempty (map hsTyVarBindName (hsConDeclExists cd))
+        withSubTable subTable' $ do
+        es <- rename (hsConDeclExists cd)
+        hsBangTypes' <- rename hsBangTypes
+        return cd { hsConDeclName = hsName', hsConDeclConArg = hsBangTypes', hsConDeclExists = es }
+    rename cd@HsRecDecl { hsConDeclSrcLoc = srcLoc, hsConDeclName = hsName, hsConDeclRecArg = stuff} = do
+        withSrcLoc srcLoc $ do
+        hsName' <- rename hsName
+        subTable <- asks envSubTable
+        subTable' <- updateSubTableWithHsNames subTable (map hsTyVarBindName (hsConDeclExists cd))
+        withSubTable subTable' $ do
+        es <- rename (hsConDeclExists cd)
+        stuff' <- sequence [ do ns' <- rename ns; t' <- withSubTable subTable $ rename t; return (ns',t')  |  (ns,t) <- stuff]
+        return cd { hsConDeclName = hsName', hsConDeclRecArg = stuff', hsConDeclExists = es }
hunk ./FrontEnd/Rename.hs 375
-renameHsBangTypes :: [HsBangType] -> SubTable -> ScopeSM ([HsBangType])
-renameHsBangTypes = mapRename renameHsBangType
hunk ./FrontEnd/Rename.hs 376
-renameHsBangType :: HsBangType -> SubTable -> ScopeSM (HsBangType)
-renameHsBangType (HsBangedTy hsType) subTable = do
-    hsType' <- renameHsType hsType subTable
-    return (HsBangedTy hsType')
-renameHsBangType (HsUnBangedTy hsType) subTable = do
-    hsType' <- renameHsType hsType subTable
-    return (HsUnBangedTy hsType')
+instance Rename HsBangType where
+    rename (HsBangedTy t) = HsBangedTy `fmap` rename t
+    rename (HsUnBangedTy t) = HsUnBangedTy `fmap` rename t
hunk ./FrontEnd/Rename.hs 380
-renameHsType t st = do
-    t <- renameHsType' True t st
-    HsErrors.hsType t
-    return t
+instance Rename HsType where
+    rename t = do
+        t <- renameHsType' True t
+        HsErrors.hsType t
+        return t
hunk ./FrontEnd/Rename.hs 386
-renameHsType' dovar t st = pp (rt t st) where
-    rt :: HsType -> SubTable -> ScopeSM (HsType)
-    rt (HsTyFun hsType1 hsType2) subTable = do
-        hsType1' <- rt hsType1 subTable
-        hsType2' <- rt hsType2 subTable
-        return (HsTyFun hsType1' hsType2')
-    rt (HsTyTuple hsTypes) subTable = do
-        hsTypes' <- mapRename rt hsTypes subTable
-        return (HsTyTuple hsTypes')
-    rt (HsTyUnboxedTuple hsTypes) subTable = do
-        hsTypes' <- mapRename rt hsTypes subTable
-        return (HsTyUnboxedTuple hsTypes')
-    rt (HsTyApp hsType1 hsType2) subTable = do
-        hsType1' <- rt hsType1 subTable
-        hsType2' <- rt hsType2 subTable
-        return (HsTyApp hsType1' hsType2')
-    rt (HsTyVar hsName) subTable | dovar = do
-        hsName' <- renameTypeHsName hsName subTable
+renameHsType' dovar t = pp (rt t) where
+    rt :: HsType -> RM HsType
+    rt (HsTyVar hsName) | dovar = do
+        hsName' <- renameTypeName hsName
hunk ./FrontEnd/Rename.hs 391
-    rt v@(HsTyVar _) _   = return v
-    rt (HsTyCon hsName) subTable = do
-        hsName' <- renameTypeHsName hsName subTable
+    rt v@(HsTyVar _)   = return v
+
+    rt (HsTyCon hsName) = do
+        hsName' <- renameTypeName hsName
hunk ./FrontEnd/Rename.hs 396
-    rt (HsTyForall ts v) subTable  = do
-        -- False <- return dovar
-        subTable' <- updateSubTableWithHsNames subTable (map hsTyVarBindName ts)
-        ts' <- renameAny ts subTable'
-        v' <- renameHsQualType v subTable'
+    rt (HsTyForall ts v) = do
+        subTable' <- updateSubTableWithHsNames mempty (map hsTyVarBindName ts)
+        withSubTable subTable' $ do
+        ts' <- rename ts
+        v' <- rename v
hunk ./FrontEnd/Rename.hs 402
-    rt (HsTyExists ts v) subTable  = do
-        -- False <- return dovar
-        subTable' <- updateSubTableWithHsNames subTable (map hsTyVarBindName ts)
-        ts' <- renameAny ts subTable'
-        v' <- renameHsQualType v subTable'
+    rt (HsTyExists ts v) = do
+        subTable' <- updateSubTableWithHsNames mempty (map hsTyVarBindName ts)
+        withSubTable subTable' $ do
+        ts' <- rename ts
+        v' <- rename v
hunk ./FrontEnd/Rename.hs 408
-    rt (HsTyAssoc) subTable = return HsTyAssoc
-    rt (HsTyEq a b) subTable = return HsTyEq `ap` (flip renameAny subTable a) `ap` (flip renameAny subTable b)
+    rt ty = traverseHsType (renameHsType' dovar) ty
hunk ./FrontEnd/Rename.hs 413
-class RenameAny a where
-    renameAny :: a -> SubTable -> ScopeSM a
-    renameAny x _ = return x
hunk ./FrontEnd/Rename.hs 414
-instance RenameAny SrcLoc where
+class Rename a where
+    rename :: a -> RM a
+    rename x = return x
hunk ./FrontEnd/Rename.hs 418
-instance RenameAny a => RenameAny [a] where
-    renameAny xs t = mapM (`renameAny` t) xs
+    updateWith :: a -> RM b -> RM b
+    updateWith _ x = x
hunk ./FrontEnd/Rename.hs 421
-instance (RenameAny a,RenameAny b) => RenameAny (a,b) where
-    renameAny (a,b) t = liftM2 (,) (renameAny a t) (renameAny b t)
+instance Rename x => Rename (Located x) where
+    rename (Located sl x) = Located sl `fmap` rename x
hunk ./FrontEnd/Rename.hs 424
-instance RenameAny a => RenameAny (Maybe a) where
-    renameAny Nothing _ = return Nothing
-    renameAny (Just x) t = liftM Just (renameAny x t)
+instance Rename SrcLoc where
+
+instance Rename a => Rename [a] where
+    rename xs = mapM rename xs
+
+    updateWith [] x = x
+    updateWith (x:xs) action = updateWith x (updateWith xs action)
+
+
+instance (Rename a,Rename b) => Rename (a,b) where
+    rename (a,b) = return (,) `ap` rename a `ap` rename b
+
+
+instance Rename a => Rename (Maybe a) where
+    rename Nothing = return Nothing
+    rename (Just x) = fmap Just $ rename x
hunk ./FrontEnd/Rename.hs 441
-instance RenameAny HsTyVarBind where
-    renameAny tvb@HsTyVarBind { hsTyVarBindName = n } t = do
-        n' <- renameTypeHsName n t
-        return tvb { hsTyVarBindName = n' }
hunk ./FrontEnd/Rename.hs 442
-instance RenameAny HsExp where
-    renameAny = renameHsExp
hunk ./FrontEnd/Rename.hs 443
-instance RenameAny HsMatch where
-    renameAny = renameHsMatch
+instance Rename HsExp where
+    rename d = renameOld (renameHsExp d)
hunk ./FrontEnd/Rename.hs 446
-instance RenameAny HsName where
-    renameAny = renameHsName
-instance RenameAny HsType where
-    renameAny = renameHsType
hunk ./FrontEnd/Rename.hs 448
+instance Rename HsTyVarBind where
+    rename tvb@HsTyVarBind { hsTyVarBindName = n } = do
+        n' <- renameTypeName n
+        return tvb { hsTyVarBindName = n' }
+
hunk ./FrontEnd/Rename.hs 455
-renameHsMatch :: HsMatch -> SubTable -> ScopeSM HsMatch
+instance Rename HsMatch where
+    rename m = renameOld $ renameHsMatch m
+
+renameHsMatch :: HsMatch -> SubTable -> RM HsMatch
hunk ./FrontEnd/Rename.hs 460
-    setSrcLoc srcLoc
+    withSrcLoc srcLoc $ do
hunk ./FrontEnd/Rename.hs 463
-    hsPats' <- renameAny hsPats subTable'
+    withSubTable subTable' $ do
+    hsPats' <- rename hsPats
hunk ./FrontEnd/Rename.hs 466
-    hsDecls' <- renameHsDecls hsDecls subTable''
-    hsRhs' <- renameHsRhs hsRhs subTable''
+    withSubTable subTable'' $ do
+    hsDecls' <- rename (expandTypeSigs hsDecls)
+    mapM_ HsErrors.hsDeclLocal hsDecls'
+    hsRhs' <- rename hsRhs
hunk ./FrontEnd/Rename.hs 474
-instance RenameAny HsPat where
-    renameAny = renameHsPat
hunk ./FrontEnd/Rename.hs 475
-renameHsPat :: HsPat -> SubTable -> ScopeSM (HsPat)
-renameHsPat (HsPVar hsName) subTable = do
-      hsName' <- renameHsName hsName subTable
-      return (HsPVar hsName')
-renameHsPat (HsPInfixApp hsPat1 hsName hsPat2) subTable = do
-      hsPat1' <- renameHsPat hsPat1 subTable
-      hsPat2' <- renameHsPat hsPat2 subTable
-      hsName' <- renameHsName hsName subTable
-      return (HsPInfixApp hsPat1' hsName' hsPat2')
-renameHsPat (HsPApp hsName hsPats) subTable = do
-      hsPats' <- renameAny hsPats subTable
-      hsName' <- renameHsName hsName subTable
-      return (HsPApp hsName' hsPats')  -- NOTE: Bryn changed this so we also rename hsName and not just the hsPats
-renameHsPat (HsPRec hsName hsPatFields) subTable = do
-      hsName' <- renameHsName hsName subTable
-      hsPatFields' <- renameHsPatFields hsPatFields subTable
-      fls <- gets fieldLabels
-      buildRecPat fls hsName' hsPatFields'
-renameHsPat (HsPAsPat hsName hsPat) subTable = do
-      hsName' <- renameHsName hsName subTable
-      hsPat' <- renameHsPat hsPat subTable
-      return (HsPAsPat hsName' hsPat')
-renameHsPat (HsPTypeSig sl hsPat qt) subTable = do
-      hsPat' <- renameHsPat hsPat subTable
-      qt' <- renameHsQualType qt subTable
-      return (HsPTypeSig sl hsPat' qt')
-renameHsPat p subTable = traverseHsPat (flip renameHsPat subTable) p
+instance Rename HsPat where
+    rename (HsPVar hsName) = HsPVar `fmap` rename hsName
+    rename (HsPInfixApp hsPat1 hsName hsPat2)  = return HsPInfixApp `ap` rename hsPat1 `ap` rename hsName `ap` rename hsPat2
+    rename (HsPApp hsName hsPats) = HsPApp <$> rename hsName <*> rename hsPats
+    rename (HsPRec hsName hsPatFields) = do
+        hsName' <- rename hsName
+        hsPatFields' <- rename hsPatFields
+        fls <- gets fieldLabels
+        buildRecPat fls hsName' hsPatFields'
+    rename (HsPAsPat hsName hsPat) = HsPAsPat <$> rename hsName <*> rename hsPat
+    rename (HsPTypeSig sl hsPat qt)  = HsPTypeSig sl <$> rename hsPat <*> rename qt
+    rename p = traverseHsPat rename p
hunk ./FrontEnd/Rename.hs 488
-buildRecPat :: FieldMap -> HsName -> [HsPatField] -> ScopeSM HsPat
+buildRecPat :: FieldMap -> HsName -> [HsPatField] -> RM HsPat
hunk ./FrontEnd/Rename.hs 505
-renameHsPatFields :: [HsPatField] -> SubTable -> ScopeSM ([HsPatField])
-renameHsPatFields = mapRename renameHsPatField
-
--- although the hsNames here must be unique (field names),
--- I rename them for the sake of completeness
-renameHsPatField :: HsPatField -> SubTable -> ScopeSM (HsPatField)
-renameHsPatField (HsPFieldPat hsName hsPat) subTable = do
-    gt <- gets globalSubTable      -- field names are not shadowed by local definitions.
-    hsName' <- renameHsName hsName gt
-    hsPat' <- renameHsPat hsPat subTable
-    return (HsPFieldPat hsName' hsPat')
-
hunk ./FrontEnd/Rename.hs 506
-renameHsRhs :: HsRhs -> SubTable -> ScopeSM HsRhs
-renameHsRhs (HsUnGuardedRhs hsExp) subTable
-  = do
-      hsExp' <- renameHsExp hsExp subTable
-      return (HsUnGuardedRhs hsExp')
-renameHsRhs (HsGuardedRhss hsGuardedRhss) subTable
-  = do
-      hsGuardedRhss' <- renameHsGuardedRhss hsGuardedRhss subTable
-      return (HsGuardedRhss hsGuardedRhss')
+instance Rename HsPatField where
+    rename (HsPFieldPat hsName hsPat) = do
+        gt <- gets globalSubTable      -- field names are not shadowed by local definitions.
+        hsName' <- renameHsName hsName gt
+        hsPat' <- rename hsPat
+        return (HsPFieldPat hsName' hsPat')
hunk ./FrontEnd/Rename.hs 514
-renameHsGuardedRhss :: [HsGuardedRhs] -> SubTable -> ScopeSM ([HsGuardedRhs])
-renameHsGuardedRhss = mapRename renameHsGuardedRhs
+instance Rename HsRhs where
+    rename (HsUnGuardedRhs hsExp) = fmap HsUnGuardedRhs $ rename hsExp
+    rename (HsGuardedRhss rs) = fmap HsGuardedRhss $ rename rs
hunk ./FrontEnd/Rename.hs 518
-renameHsGuardedRhs :: HsGuardedRhs -> SubTable -> ScopeSM HsGuardedRhs
-renameHsGuardedRhs (HsGuardedRhs srcLoc hsExp1 hsExp2) subTable = do
-    setSrcLoc srcLoc
-    hsExp1' <- renameHsExp hsExp1 subTable
-    hsExp2' <- renameHsExp hsExp2 subTable
-    return (HsGuardedRhs srcLoc hsExp1' hsExp2')
-
-
-renameHsExps :: [HsExp] -> SubTable -> ScopeSM ([HsExp])
-renameHsExps = mapRename renameHsExp
+instance Rename HsGuardedRhs where
+    rename (HsGuardedRhs srcLoc hsExp1 hsExp2) = do
+        withSrcLoc srcLoc $ do
+        hsExp1' <- rename hsExp1
+        hsExp2' <- rename hsExp2
+        return (HsGuardedRhs srcLoc hsExp1' hsExp2')
hunk ./FrontEnd/Rename.hs 544
-renameHsExp :: HsExp -> SubTable -> ScopeSM HsExp
+renameHsExp :: HsExp -> SubTable -> RM HsExp
hunk ./FrontEnd/Rename.hs 562
-    setSrcLoc srcLoc
+    withSrcLoc srcLoc $ do
hunk ./FrontEnd/Rename.hs 564
-    hsPats' <- renameAny hsPats subTable'
-    hsExp' <- renameHsExp hsExp subTable'
+    withSubTable subTable' $ do
+    hsPats' <- rename hsPats
+    hsExp' <- rename hsExp
hunk ./FrontEnd/Rename.hs 570
-    hsDecls' <- renameHsDecls hsDecls subTable'
+    withSubTable subTable' $ do
+    hsDecls' <- rename (expandTypeSigs hsDecls)
+    mapM_ HsErrors.hsDeclLocal hsDecls'
hunk ./FrontEnd/Rename.hs 577
-    hsAlts' <- renameHsAlts hsAlts subTable
+    hsAlts' <- rename hsAlts
hunk ./FrontEnd/Rename.hs 584
-    hsExps' <- renameHsExps hsExps subTable
+    hsExps' <- rename hsExps
hunk ./FrontEnd/Rename.hs 590
-    hsFieldUpdates' <- renameHsFieldUpdates hsFieldUpdates subTable
+    hsFieldUpdates' <- rename hsFieldUpdates
hunk ./FrontEnd/Rename.hs 595
-    hsFieldUpdates' <- renameHsFieldUpdates hsFieldUpdates subTable
+    hsFieldUpdates' <- rename hsFieldUpdates
hunk ./FrontEnd/Rename.hs 622
-    hsQualType' <- renameHsQualType hsQualType subTable'
+    withSubTable subTable' $ do
+    hsQualType' <- rename hsQualType
hunk ./FrontEnd/Rename.hs 630
-    setSrcLoc sl
+    withSrcLoc sl $ do
hunk ./FrontEnd/Rename.hs 639
-    sl <- gets srcLoc
+    sl <- getSrcLoc
hunk ./FrontEnd/Rename.hs 643
-    sl <- gets srcLoc
+    sl <- getSrcLoc
hunk ./FrontEnd/Rename.hs 647
-buildRecConstr ::  FieldMap -> HsName -> [HsFieldUpdate] -> ScopeSM HsExp
+buildRecConstr ::  FieldMap -> HsName -> [HsFieldUpdate] -> RM HsExp
hunk ./FrontEnd/Rename.hs 665
-buildRecUpdate ::  FieldMap -> HsExp -> [HsFieldUpdate] -> ScopeSM HsExp
+buildRecUpdate ::  FieldMap -> HsExp -> [HsFieldUpdate] -> RM HsExp
hunk ./FrontEnd/Rename.hs 667
-        sl <- gets srcLoc
+        sl <- getSrcLoc
hunk ./FrontEnd/Rename.hs 685
---    undef <- createError "Uninitialized Field"
---    case Map.lookup (toName DataConstructor n) amp of
---        Nothing -> failRename $ "Unknown Constructor: " ++ show n
---        Just t -> do
-
---buildRecUpdate ::  FieldMap -> HsExp -> [HsFieldUpdate] -> ScopeSM HsExp
---buildRecUpdate _ _ _ = failRename "Can't handle field updates just yet."
hunk ./FrontEnd/Rename.hs 686
-renameHsAlts :: [HsAlt] -> SubTable -> ScopeSM [HsAlt]
-renameHsAlts = mapRename renameHsAlt
hunk ./FrontEnd/Rename.hs 687
--- note for renameHsAlt, the 'wheres' dominate the 'pats'
-
-renameHsAlt :: HsAlt -> SubTable -> ScopeSM (HsAlt)
-renameHsAlt (HsAlt srcLoc hsPat hsGuardedAlts {-where-} hsDecls) subTable = do
-    setSrcLoc srcLoc
-    subTable' <- updateSubTableWithHsPats subTable [hsPat] srcLoc
-    hsPat' <- renameHsPat hsPat subTable'
-    subTable'' <- updateSubTableWithHsDecls subTable' hsDecls
-    hsDecls' <- renameHsDecls hsDecls subTable''
-    hsGuardedAlts' <- renameHsRhs hsGuardedAlts subTable''
-    return (HsAlt srcLoc hsPat' hsGuardedAlts' hsDecls')
+instance Rename HsAlt where
+    rename (HsAlt srcLoc hsPat hsGuardedAlts {-where-} hsDecls) = withSrcLoc srcLoc $ do
+        subTable' <- updateSubTableWithHsPats mempty [hsPat] srcLoc
+        withSubTable subTable' $ do
+        hsPat' <- rename hsPat
+        subTable'' <- updateSubTableWithHsDecls subTable' hsDecls
+        withSubTable subTable'' $ do
+        hsDecls' <- rename (expandTypeSigs hsDecls)
+        mapM_ HsErrors.hsDeclLocal hsDecls'
+        hsGuardedAlts' <- rename hsGuardedAlts
+        return (HsAlt srcLoc hsPat' hsGuardedAlts' hsDecls')
hunk ./FrontEnd/Rename.hs 709
-renameHsStmts :: [HsStmt] -> SubTable -> ScopeSM (([HsStmt],SubTable))
+renameHsStmts :: [HsStmt] -> SubTable -> RM (([HsStmt],SubTable))
hunk ./FrontEnd/Rename.hs 712
-      hsStmt' <- renameHsStmt hsStmt subTable'
+      withSubTable subTable' $ do
+      hsStmt' <- withSubTable subTable' $ rename hsStmt
hunk ./FrontEnd/Rename.hs 719
-renameHsStmt :: HsStmt -> SubTable -> ScopeSM (HsStmt)
-renameHsStmt (HsGenerator srcLoc hsPat hsExp) subTable = do
-      hsExp' <- renameHsExp hsExp subTable
-      hsPat' <- renameHsPat hsPat subTable
-      return (HsGenerator srcLoc hsPat' hsExp')
-renameHsStmt (HsQualifier hsExp) subTable = do
-      hsExp' <- renameHsExp hsExp subTable
-      return (HsQualifier hsExp')
-renameHsStmt (HsLetStmt hsDecls) subTable = do
-      hsDecls' <- renameHsDecls hsDecls subTable
-      return (HsLetStmt hsDecls')
+instance Rename HsStmt where
+    rename (HsGenerator srcLoc hsPat hsExp) = do
+        hsExp' <- rename hsExp
+        hsPat' <- rename hsPat
+        return (HsGenerator srcLoc hsPat' hsExp')
+    rename (HsQualifier hsExp) = do
+        hsExp' <- rename hsExp
+        return (HsQualifier hsExp')
+    rename (HsLetStmt hsDecls) = do
+        hsDecls' <- rename (expandTypeSigs hsDecls)
+        mapM_ HsErrors.hsDeclLocal hsDecls'
+        return (HsLetStmt hsDecls')
hunk ./FrontEnd/Rename.hs 733
-renameHsFieldUpdates :: [HsFieldUpdate] -> SubTable -> ScopeSM ([HsFieldUpdate])
-renameHsFieldUpdates = mapRename renameHsFieldUpdate
hunk ./FrontEnd/Rename.hs 734
-renameHsFieldUpdate :: HsFieldUpdate -> SubTable -> ScopeSM (HsFieldUpdate)
-renameHsFieldUpdate (HsFieldUpdate hsName hsExp) subTable = do
-    gt <- gets globalSubTable     -- field names are global and not shadowed
-    hsName' <- renameHsName hsName gt      -- TODO field names should have own namespace
-    hsExp' <- renameHsExp hsExp subTable
-    return (HsFieldUpdate hsName' hsExp')
+instance Rename HsFieldUpdate where
+    rename (HsFieldUpdate hsName hsExp) = do
+        gt <- gets globalSubTable              -- field names are global and not shadowed
+        hsName' <- renameHsName hsName gt      -- TODO field names should have own namespace
+        hsExp' <- rename hsExp
+        return (HsFieldUpdate hsName' hsExp')
+
+
hunk ./FrontEnd/Rename.hs 743
+instance Rename HsName where
+    rename n = renameOld $ renameHsName n
hunk ./FrontEnd/Rename.hs 746
-renameHsNames :: [HsName] -> SubTable -> ScopeSM ([HsName])
-renameHsNames = mapRename renameHsName
+renameTypeName n = renameOld $ renameTypeHsName n
hunk ./FrontEnd/Rename.hs 751
-renameHsName :: HsName -> SubTable -> ScopeSM (HsName)
+renameHsName :: HsName -> SubTable -> RM (HsName)
hunk ./FrontEnd/Rename.hs 760
-            sl <- gets srcLoc
+            sl <- getSrcLoc
hunk ./FrontEnd/Rename.hs 788
-clobberHsNamesAndUpdateIdentTable :: [(HsName,SrcLoc)] -> SubTable -> ScopeSM (SubTable)
+clobberHsNamesAndUpdateIdentTable :: [(HsName,SrcLoc)] -> SubTable -> RM (SubTable)
hunk ./FrontEnd/Rename.hs 795
-{-
-clobberHsNameAndUpdateIdentTable :: HsName -> SrcLoc -> SubTable -> Binding -> ScopeSM (SubTable)
-clobberHsNameAndUpdateIdentTable hsName srcLoc subTable binding
-  = do
-      unique <- getUnique
-      currModule <- getCurrentModule
-      let
-        hsName'     = renameAndQualify hsName unique currModule
-        subTable'   = addToFM (addToFM subTable hsName hsName') hsName' hsName'
-      addToIdentTable hsName' (srcLoc, binding)
-      incUnique
-      return (subTable')
--}
hunk ./FrontEnd/Rename.hs 798
-clobberHsNames :: [HsName] -> SubTable -> ScopeSM (SubTable)
+clobberHsNames :: [HsName] -> SubTable -> RM (SubTable)
hunk ./FrontEnd/Rename.hs 805
-clobberHsName :: HsName -> SubTable -> ScopeSM (SubTable)
+clobberHsName :: HsName -> SubTable -> RM (SubTable)
hunk ./FrontEnd/Rename.hs 813
+clobberName :: HsName -> RM SubTable
+clobberName hsName = do
+    unique     <- newUniq
+    currModule <- getCurrentModule
+    let hsName'     = renameAndQualify hsName unique currModule
+    return $ Map.singleton hsName hsName'
hunk ./FrontEnd/Rename.hs 823
-    = case rename name unique of
+    = case renameName name unique of
hunk ./FrontEnd/Rename.hs 828
-rename :: HsName -> Int -> HsName
-rename n unique = hsNameIdent_u (hsIdentString_u ((show unique ++ "_") ++)) n
+renameName :: HsName -> Int -> HsName
+renameName n unique = hsNameIdent_u (hsIdentString_u ((show unique ++ "_") ++)) n
hunk ./FrontEnd/Rename.hs 868
-updateSubTableWithHsDecls :: SubTable -> [HsDecl] ->  ScopeSM (SubTable)
+updateSubTableWithHsDecls :: SubTable -> [HsDecl] ->  RM (SubTable)
hunk ./FrontEnd/Rename.hs 876
-updateSubTableWithHsPats :: SubTable -> [HsPat] -> SrcLoc -> ScopeSM (SubTable)
+updateSubTableWithHsPats :: SubTable -> [HsPat] -> SrcLoc -> RM (SubTable)
hunk ./FrontEnd/Rename.hs 887
-updateSubTableWithHsStmt :: SubTable -> HsStmt -> ScopeSM (SubTable)
+updateSubTableWithHsStmt :: SubTable -> HsStmt -> RM (SubTable)
hunk ./FrontEnd/Rename.hs 901
-updateSubTableWithHsNames :: SubTable -> [HsName] -> ScopeSM (SubTable)
-updateSubTableWithHsNames subTable hsNames = do
-      subTable' <- clobberHsNames hsNames subTable
-      return (subTable')
+updateSubTableWithHsNames :: SubTable -> [HsName] -> RM (SubTable)
+updateSubTableWithHsNames subTable hsNames = clobberHsNames hsNames subTable
hunk ./FrontEnd/Rename.hs 907
-updateSubTableWithHsQualType :: SubTable -> HsQualType -> ScopeSM (SubTable)
+updateSubTableWithHsQualType :: SubTable -> HsQualType -> RM (SubTable)
hunk ./FrontEnd/Rename.hs 913
-updateSubTableWithHsTypes :: SubTable -> [HsType] -> ScopeSM (SubTable)
+updateSubTableWithHsTypes :: SubTable -> [HsType] -> RM (SubTable)
hunk ./FrontEnd/Rename.hs 923
--- This will cause errors on code with PatBinds of the form (x,y) = blah...
--- and should be changed for a more general renamer (but is fine for thih)
-getHsNamesAndASrcLocsFromHsDecl (HsPatBind sloc _ _ _)
-  = error $ "non simple pattern binding found (sloc): " ++ show sloc
--- getHsNamesAndASrcLocsFromHsDecl (HsFunBind _ hsMatches)
+getHsNamesAndASrcLocsFromHsDecl (HsPatBind sloc _ _ _) = error $ "non simple pattern binding found (sloc): " ++ show sloc
hunk ./FrontEnd/Rename.hs 933
-getHsNamesAndASrcLocsFromHsMatch (HsMatch srcLoc hsName _ _ _)
-  = [(hsName, srcLoc)]
+getHsNamesAndASrcLocsFromHsMatch (HsMatch srcLoc hsName _ _ _) = [(hsName, srcLoc)]
hunk ./FrontEnd/Rename.hs 954
-    f HsDataDecl { hsDeclSrcLoc =sl, hsDeclName = n, hsDeclCons = cs } = do tellF $ (toName TypeConstructor n,sl,snub [ x |(x,_,_) <- cs']): cs' ; zup cs where
-        cs' = concatMap (namesHsConDecl' toName) cs
+    f HsDataDecl { hsDeclSrcLoc =sl, hsDeclName = n, hsDeclCons = cs } = do
+        tellF $ (toName TypeConstructor n,sl,snub [ x |(x,_,_) <- cs']): cs' ; zup cs where
+            cs' = concatMap (namesHsConDecl' toName) cs
hunk ./FrontEnd/Rename.hs 1006
-getHsNamesAndASrcLocsFromHsStmt (HsGenerator srcLoc hsPat _hsExp)
-  = zip (getNamesFromHsPat hsPat) (repeat srcLoc)
-getHsNamesAndASrcLocsFromHsStmt (HsQualifier _hsExp)
-  = []
-getHsNamesAndASrcLocsFromHsStmt (HsLetStmt hsDecls)
-  = concat $ map getHsNamesAndASrcLocsFromHsDecl hsDecls
+getHsNamesAndASrcLocsFromHsStmt (HsGenerator srcLoc hsPat _hsExp) = zip (getNamesFromHsPat hsPat) (repeat srcLoc)
+getHsNamesAndASrcLocsFromHsStmt (HsQualifier _hsExp) = []
+getHsNamesAndASrcLocsFromHsStmt (HsLetStmt hsDecls) = concat $ map getHsNamesAndASrcLocsFromHsDecl hsDecls