No title

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
module X where
import Control.Monad.Writer.Strict
import Data.IORef
import System.IO.Unsafe

data Instruction =
  Reset !String | Set !Int !Int String
  deriving (Show)

interpret :: [Instruction] -> [String]
interpret = execWriter . go [] . reverse where
  go _state (Reset state:next) = go state next
  go state (Set s e r:next) = do tell [state]
                                 go (set s e r state) next
  go state [] = tell [state]

set :: Int -> Int -> String -> String -> String
set s e r t = take s t ++ r ++ drop e t

stack :: IORef [Instruction]
stack = unsafePerformIO (newIORef [])

{-# NOINLINE push #-}
push x a =
  unsafePerformIO
    (do modifyIORef stack (x :)
        return a)

orig_ex = (\x -> (\y -> x * y)) 5 7
        --0123456789012345678901234
        --          111111111122222

translated_ex =
  seq (push (Reset "(\\x -> (\\y -> x * y)) 5 7")
            ((\x ->
                (push (Set 0 25 ("(\\y -> " ++ show x ++ " * y)"))
                      (\y ->
                         push (Set 0 13 (show x ++ " * " ++ show y))
                              (x * y)))) 5 7))
      stack
29:1: Warning: Use camelCase
Found:
orig_ex = ...
Why not:
origEx = ...
29:12: Error: Avoid lambda
Found:
\ x -> (\ y -> x * y)
Why not:
(*)
33:1: Warning: Use camelCase
Found:
translated_ex = ...
Why not:
translatedEx = ...