variable renaming with syb, ha-src-exts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# LANGUAGE FlexibleContexts, MultiParamTypeClasses #-}
import Language.Haskell.Exts
import Data.Generics
import Control.Monad.Random
import Control.Monad.State
import qualified Data.Map as M
import Control.Applicative
import System.Environment
import Data.Maybe
import Data.Char

main = do
    x <- parseFile . head =<< getArgs
    case x of ParseFailed _ _ -> print x
              ParseOk y -> putStr $ prettyPrint $ alle y

alle = flip evalState (M.empty,0) . everywhereM (mkM makeElite)

makeElite :: (MonadState (M.Map String Int,Int) m) => Name -> m Name
makeElite (Ident x) | maybe False isLower $ listToMaybe x = liftM (Ident . ("var"++) . show) $ mke x
makeElite x = return x

mke x = do
     (m,n) <- get
     case M.lookup x m of
        Nothing -> put (M.insert x (succ n) m,succ n) >> return (succ n)
        Just y -> return y

{- runghc varChange.hs varChanges.hs produces:
 - {-# LANGUAGE FlexibleContexts, MultiParamTypeClasses #-}
 - module Main (var1) where
 - import Language.Haskell.Exts
 - import Data.Generics
 - import Control.Monad.Random
 - import Control.Monad.State
 - import qualified Data.Map as M
 - import Control.Applicative
 - import System.Environment
 - import Data.Maybe
 - import Data.Char
 - var1
 -   = do var2 <- var3 . var4 =<< var5
 -        case var2 of
 -            ParseFailed _ _ -> var6 var2
 -            ParseOk var7 -> var8 $ var9 $ var10 var7
 - var10 = var11 var12 (M.var13, 0) . var14 (var15 var16)
 -  
 - var16 ::
 -         (MonadState (M.Map String Int, Int) var17) => Name -> var17 Name
 - var16 (Ident var2)
 -   | var18 False var19 $ var20 var2 =
 -     var21 (Ident . ("var" ++) . var22) $ var23 var2
 - var16 var2 = var24 var2
 - var23 var2
 -   = do (var17, var25) <- var26
 -        case M.var27 var2 var17 of
 -            Nothing -> var28 (M.var29 var2 (var30 var25) var17, var30 var25) >>
 -                         var24 (var30 var25)
 -            Just var7 -> var24 var7%
 -}