上下文:我需要编写一个主要是无状态的编译器,它将VM字节码转换为机器码。大多数VM命令都可以使用纯函数进行无状态转换,如下所示:
compilePop = ["mov ax, @sp", "dec ax", "mov @sp, ax"]
compile :: VM_COMMAND -> [String]
compile STACK_POP = compilePop
-- compile whole program
compileAll :: [VM_COMMAND] -> [String]
compileAll = flatMap compile
但是有些命令需要插入标签,每次调用都应该不同。
我理解如何使用状态对象" global"对于整个编译器:
compileGt n = [label ++ ":", "cmp ax,bx", "jgt " ++ label]
where label = "cmp" ++ show n
compile :: Int -> COMPILER_STATE -> VM_COMMAND -> (COMPILER_STATE, [String])
-- here state currently contains only single integer, but it will grow larger
compile lcnt STACK_POP = (lcnt, compilePop)
compile lcnt CMP_GT = (lcnt + 1, compileGt lcnt)
compileAll commands = snd $ foldr compile commands 0
-- incorrect, but you get the idea
但我认为这很糟糕,因为每个专门的编译函数只需要一小部分状态,甚至根本不需要。例如,在这种纯函数式JavaScript中,我不会在闭包中实现具有本地状态的专用编译函数。
// compile/gt.js
var i = 0;
export default const compileGt = () => {
const label = "cmp" + i++;
return [label ++ ":", "cmp ax,bx", "jgt " ++ label];
};
// index.js
import compileGt from './compile/gt';
function compile (cmd) {
switch (cmd) {
case CMP_GT: return compileGt();
// ...
}
}
export default const compileAll = (cmds) => cmds.flatMap(compile);
所以问题是我如何在Haskell中做同样的事情或解释为什么它真的很糟糕。它应该是那样的吗?
type compileFn = State -> VM_COMMAND -> [String]
(compileFn, State) -> VM_COMMAND -> ([String], (compileFn, State))
答案 0 :(得分:8)
如果你有......
data Big = Big { little :: Little, stuff :: Whatever }
...你可以定义你的......
littleProcessor :: State Little [String]
...然后使用像这样的函数......
innerState :: Monad m
=> (s -> i) -> (i -> s -> s) -> StateT i m a -> StateT s m a
innerState getI setI (StateT m) = StateT $ \s -> do
(a, i) <- m (getI s)
return (a, setI i s)
......将其提升到更大的状态:
bigProcessor :: State Big [String]
bigProcessor = innerState little (\l b -> b {little = l}) littleProcessor
(添加辅助定义以品尝。)
在innerState
中使用getter / setter对使它看起来应该可以用镜头来表达它。实际上,来自镜头的zoom
基本上是innerState
,并且最小化了样板:
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
data Big = Big { _little :: Little, _stuff :: Whatever }
makeLenses ''Big -- little is now a lens.
bigProcessor :: State Big [String]
bigProcessor = zoom little littleProcessor