[英]Composing Pipes into a loop or cycle in haskell
这个问题是关于 Haskell 库Pipes 的。
此问题与 2019 Advent of Code Day 11 (可能剧透警告)有关
我有两个Pipe Int Int mr
brain
和robot
,它们也需要在连续循环中相互传递信息。 这是输出brain
需要去的输入robot
的输出和robot
需要走的输入brain
。 当brain
完成时,我需要计算的结果。
如何将brain
和robot
成一个循环? 理想情况下,我可以传递给runEffect
类型为Effect mr
的循环
编辑:结果应该是这样的:
+-----------+ +-----------+
| | | |
| | | |
a ==> f ==> b ==> g ==> a=|
^ | | | | |
| | | | | | | |
| +-----|-----+ +-----|-----+ |
| v v |
| () r |
+=====================================+
最简单的解决方案是使用 danidiaz 在评论中建议的Client
和Server
,因为pipes
没有任何对循环管道的内置支持,如果正确地这样做的话,这将是非常困难的。 这主要是因为我们需要处理await
的数量与yield
的数量不匹配的情况。
编辑:我添加了一个关于其他答案问题的部分。 请参阅“另一个有问题的替代方案”部分
编辑 2:我在下面添加了一个问题较少的可能解决方案。 请参阅“可能的解决方案”部分
然而,可以借助Proxy
框架(带有Client
和Server
)和简洁的函数generalize
来模拟它,它将单向Pipe
变成双向Proxy
。
generalize f x0
+-----------+ +---------------------+
| | | |
| | x <======================== x
a ==> f ==> b becomes | |
| | a ==> f ==> b
| | | | |
+-----|-----+ +----------|----------+
v v
r r
loop :: Monad m => Pipe a a m r -> a -> Effect m r
loop p x0 = pure >\\ generalize p x0 //> pure
有这个形状
loop f
a
+-----|-----+
| | |
/====<=======/===<========\
| | | |
\=> a ==> f ==> a ==/
| |
+-----|-----+
v
r
如您所见,我们需要为a
输入初始值。 这是因为无法保证管道在产生之前不会await
,这将迫使它永远等待。
但是请注意,如果管道在await
之前多次yield
s,这将丢弃数据,因为 generalize 在内部使用状态 monad 实现,该状态 monad 在yield
时保存最后一个值并在等待时检索最后一个值。
要将它与您的管道一起使用,只需将它们组合起来并让它们loop
:
runEffect $ loop (f >-> g)
但是请不要使用它,因为如果您不小心它会随机丢弃数据
你也可以像mingmingrr建议的那样制作一个懒惰的无限管道链
infiniteChain :: Functor m => Pipe a a m r -> Producer a m r
infiniteChain f = infiniteChain >-> f
这解决了丢弃/重复值的问题,但还有其他几个问题。 首先,在让步之前先等待会导致无限循环使用无限内存,但这已经在 mingmingrr 的回答中解决了。
另一个更难解决的问题是,在相应的 yield 之前的每个 action 都会为每个 await 重复一次。 如果我们修改他们的示例以记录正在发生的事情,我们可以看到这一点:
import Pipes
import qualified Pipes.Prelude as P
f :: Monad m => Pipe Int Int m r
f = P.map (* 2)
g :: Monad m => Int -> Pipe Int Int m ()
g 0 = return ()
g n = do
lift . putStrLn $ "Awaiting. n = " ++ show n
x <- await
lift . putStrLn $ "Got: x = " ++ show x ++ " and n = "++ show n ;
yield (x + 1)
g (n - 1)
cyclic' :: Monad m => Int -> Producer Int m Int
cyclic' input = let pipe = (yield input >> pipe) >-> f >-> g 6 in pipe
现在,运行runEffect (cyclic' 0 >-> P.print)
将打印以下内容:
Awaiting. n = 6
Got: x = 0 and n = 6
1
Awaiting. n = 5
Awaiting. n = 6
Got: x = 0 and n = 6
Got: x = 2 and n = 5
3
Awaiting. n = 4
Awaiting. n = 5
Awaiting. n = 6
Got: x = 0 and n = 6
Got: x = 2 and n = 5
Got: x = 6 and n = 4
7
Awaiting. n = 3
Awaiting. n = 4
Awaiting. n = 5
Awaiting. n = 6
Got: x = 0 and n = 6
Got: x = 2 and n = 5
Got: x = 6 and n = 4
Got: x = 14 and n = 3
15
Awaiting. n = 2
Awaiting. n = 3
Awaiting. n = 4
Awaiting. n = 5
Awaiting. n = 6
Got: x = 0 and n = 6
Got: x = 2 and n = 5
Got: x = 6 and n = 4
Got: x = 14 and n = 3
Got: x = 30 and n = 2
31
Awaiting. n = 1
Awaiting. n = 2
Awaiting. n = 3
Awaiting. n = 4
Awaiting. n = 5
Awaiting. n = 6
Got: x = 0 and n = 6
Got: x = 2 and n = 5
Got: x = 6 and n = 4
Got: x = 14 and n = 3
Got: x = 30 and n = 2
Got: x = 62 and n = 1
63
如您所见,对于每个await
,我们重新执行所有内容,直到相应的yield
。 更具体地说,等待触发管道的新副本运行,直到达到产量。 当我们再次等待时,该副本将再次运行直到下一次产生,如果在此期间触发await
,它将创建另一个副本并运行它直到第一次产生,依此类推。
这意味着在最好的情况下,我们得到O(n^2)
而不是线性性能(并且使用O(n)
而不是O(1)
内存),因为我们为每个动作重复所有内容。 在最坏的情况下,例如,如果我们正在读取或写入文件,我们可能会得到完全错误的结果,因为我们正在重复副作用。
如果您真的必须使用Pipe
并且不能使用request
/ respond
代替,并且您确定您的代码永远不会await
超过(或之前)它yield
s(或者在这些情况下有一个很好的默认值),我们可以在我之前的尝试的基础上提出一个解决方案,该解决方案至少可以在yield
多于await
时处理这种情况。
诀窍是在generalize
的实现中添加一个缓冲区,这样多余的值就会被存储而不是被丢弃。 我们还可以将额外参数保留为缓冲区为空时的默认值。
import Pipes.Lift (evalStateP)
import Control.Monad.Trans.State.Strict (state, modify)
import qualified Data.Sequence
generalize' :: Monad m => Pipe a b m r -> x -> Proxy x a x b m r
generalize' p x0 = evalStateP Seq.empty $ up >\\ hoist lift p //> dn
where
up () = do
x <- lift $ state (takeHeadDef x0)
request x
dn a = do
x <- respond a
lift $ modify (Seq.|> x)
takeHeadDef :: a -> Seq.Seq a -> (a, Seq.Seq a)
takeHeadDef x0 xs = (foldr const x0 xs, Seq.drop 1 xs)
如果我们现在将其插入loop
的定义中,我们将解决丢弃多余值的问题(以保留缓冲区的内存成本为代价)。 它还可以防止复制除默认值之外的任何值,并且仅在缓冲区为空时使用默认值。
loop' :: Monad m => a -> Pipe a a m r -> Effect m r
loop' x0 p = pure >\\ generalize' p x0 //> pure
如果我们希望在yield
之前await
是一个错误,我们可以简单地将error
作为我们的默认值: loop' (error "Await without yield") somePipe
。
使用Pipes.Core
Client
和Server
。 它将解决您的问题,而不会导致大量奇怪的错误。
如果这是不可能的,在大多数情况下,我的“可能的解决方案”部分带有generalize
的修改版本应该可以完成这项工作。
您可以通过将管道的输出连接到输入来制作循环管道。
cyclic :: Functor m => Producer a m r
cyclic = cyclic >-> f >-> g
考虑以下示例:
import Pipes
import qualified Pipes.Prelude as P
f :: Monad m => Pipe Int Int m r
f = P.map (* 2)
g :: Monad m => Int -> Pipe Int Int m Int
g 0 = return 100
g n = do x <- await ; yield (x + 1) ; g (n - 1)
由于这里的f
和g
在等待之前都不会产生任何输出,因此使用cyclic = cyclic >-> f >-> g
将导致f
永远等待。 避免这种情况的关键是确保f
或g
在等待之前产生一些东西,或者像这样将初始输入输入到第一个管道:
cyclic' :: Monad m => Int -> Producer Int m Int
cyclic' input = let pipe = (yield input >> pipe) >-> f >-> g 6 in pipe
这里运行runEffect (cyclic' 0 >-> P.print)
return 100
并打印1 3 7 15 31 63
。
PS(可能出现 Code 2019 剧透)您可以使用相同的方案来完成第 7 天。如果您的 Intcode 计算机类型为StateT IntcodeState (Pipe Int Int m)
,那么您可以使用replicate 5 (evalState runIntcode initialIntcodeState)
来获得 5 个管道对应于 5 个放大器中的每一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.