不会满足ArrowApply laws,
第一定律实际上失败了:
first (arr (\x -> arr (\y -> (x,y)))) >>> app = id
:: ArrowApply a => a (t, d) (t, d)
我们先定义一个辅助函数:
iterateAuto :: [b] -> Auto b c -> [c]
iterateAuto [] _ = []
iterateAuto (x:xs) a = let (y, a') = runAuto a x
in y : iterateAuto xs a'
在右边我们得到:
*Main> iterateAuto [(0,0), (1,0)] (id :: Auto (Int, Int) (Int, Int))
[(0,0),(1,0)]
但是在左侧(这里我必须将您的实现命名为app')
iterateAuto [(0,0), (1,0)] (first (arr (\x -> arr (\y -> (x,y)))) >>> app' :: Auto (Int, Int) (Int, Int))
[(0,0),(0,0)]
我很确定,如果ArrowApply 可能用于Automaton,它会在arrows 包中。很难解释为什么不能有一个。我试图解释我的直觉。 ArrowApply 等价于Monad,而app 是一种一元的join。 Automaton 是一种有状态的计算,但每个 Automaton 都有自己的状态,而不是 State monad 中的全局状态。在纯设置中,自动机的下一个状态会在结果对中的每次迭代中提供给我们。然而,如果我们有app,内部自动机的状态就会丢失。
app 的另一个幼稚实现:
app'' :: Auto (Auto b c, b) c
app'' = Automaton $ \(f,x) -> let
(u, m) = runAuto f x
nextApp = app''
in (u, nextApp)
第二定律会失败
first (arr (g >>>)) >>> app = second g >>> app
让我们把有状态的incr 当作g
incr :: Auto Int Int
incr = incr' 0
where incr' n = Automaton $ \x -> (x + n, incr' $ n + 1)
和辅助方法
helper :: Arrow a => (Int, Int) -> (a Int Int, Int)
helper (x, y) = (arr (+x), y)
然后我们看到这个等式也不适用于非常简单的输入:
*Main> iterateAuto (map helper [(0,0),(0,0)]) $ first (arr (incr >>>)) >>> app''
[0,0]
*Main> iterateAuto (map helper [(0,0),(0,0)]) $ second incr >>> app''
[0,1]
我有the runnable code as a gist
一个邪恶的想法是通过利用 IORef 或 STRef 来制作 Automaton 的一个版本
data STAutomaton s a b c = STAutomaton (STRef s (Automaton a b c))
但这可能是使用Kleisli (ST s) 或Kleisli IO 的尴尬方式。