Haskell 此实例有什么问题:ArrowApply自动机?
我希望自动机应用实例Arrow,但Control.Arrow.Transformer.Automaton没有。 我认为以下代码将表现良好:Haskell 此实例有什么问题:ArrowApply自动机?,haskell,Haskell,我希望自动机应用实例Arrow,但Control.Arrow.Transformer.Automaton没有。 我认为以下代码将表现良好: data Automaton b c = Auto {runAuto :: b -> (c, Automaton b c) } app :: Automaton (Automaton b c, b) c app = Auto $ \(f,x) -> let (u, m) = runAuto f x nextApp m = Aut
data Automaton b c = Auto {runAuto :: b -> (c, Automaton b c) }
app :: Automaton (Automaton b c, b) c
app = Auto $ \(f,x) -> let
(u, m) = runAuto f x
nextApp m = Auto $ \(_,x) -> let
(u', n) = runAuto m x
in (u', nextApp n)
in (u, nextApp m)
也许,未使用的论点的存在将是不好的。
但是我对坏榜样没有任何具体的概念,请告诉我任何一个。它不会让我满意
它实际上不符合第一定律:
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'
)
我很确定,如果arrowsapply
forAutomaton
是可能的,那么它将位于arrows
包中。很难解释为什么没有。我试图解释我的直觉。ArrowApply
相当于Monad
,app
是一种单子join
。自动机
是一种有状态计算,但每个自动机
都携带自己的状态,而不是像状态
单子中那样的全局状态。在纯设置中,在结果对的每次迭代中,自动机的下一个状态都会提供给我们。然而,如果我们有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
asg
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]
我有
一个坏主意是利用IORef或
但这可能是使用
Kleisli(ST-s)
或Kleisli IO
的尴尬方式,将其与箭头和类别相关联。如果没有<代码>箭头<代码>和<代码>类别< /代码>实例,考虑应用程序是否正确是没有意义的。我假设您正在使用来自的自动机(->)
的箭头和类别
实例?谢谢!事实上,我想制作一个箭头
,它与Statomaton
相同。能否将其作为箭头应用的正确实例?还是实施“地方政府”的错误方式?(现在我想,arrowcuit
可以表示变量,所以我正在尝试。你怎么看?)最近的ICFP论文中介绍了一些不错的技巧。我没有做这个练习,但是您应该能够使用ArrowChoice
和delay
从ArrowCircuit
模拟本地状态:
*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]
data STAutomaton s a b c = STAutomaton (STRef s (Automaton a b c))