这个问题是关于 Haskell Pipes 库的
背景:
在上一个问题中,我问如何使用管道形成循环,我得到的答案是"不要那样做。 请改用request
和response
。 虽然有一个优秀且写得很清楚的教程,用简单的英语涵盖了Producers
、Consumers
、Pipes
和Effects
。request
、response
Client
和Server
的文档首先定义了类别,并提到了其他一些 CompSci 概念,如"生成器设计模式"和"迭代设计模式",这些概念从未解释过。所以我不知道如何"使用request
和response
代替"。
设置的设置
我有两个类似状态机的东西,需要反复来回传递数据,robot
和intCode
。
机器人非常简单:
robot :: Pipe Int Int m r -- robot never returns so its return type is polymorphic
robot = go newRobot
where
go r = do
yield $ color r
c <- toColor <$> await
turn <- toTurn <$> await
go $ update c turn r
它yield
一个值,await
两条指令(一种新颜色和一条转弯(,更新机器人的状态(r
(,然后重新开始。
intCode
虚拟机运行编程以与机器人通信。它需要一个程序(称为code
(并创建一个管道,该管道将await
传感器从机器人读取,然后向其yield
两条指令。
(boot code) :: Pipe Int Int m ()
让我们假设 IntCode VM 不容易修改,但机器人很容易修改。
问题:
request
和respond
与await
和yield
有何不同?
如何使用它们来促进机器人和虚拟机之间的持续通信?
await
和yield
的定义是:
await = request ()
yield = respond
所以它们与request
和respond
密切相关。await
和yield
版本刚刚专门用于基于单向拉取的流(Producer
s、Pipe
s 和Consumer
s(。
若要在两个终结点之间执行双向通信,需要设置Client
和Server
并连接它们。
Client
是发出请求的 monadic 操作:
y <- request x
通过发送请求x
并接收响应y
。Server
是响应以下一元操作:
x <- respond y
通过接受请求x
并发送响应y
。 请注意,这些操作是对称的,因此在给定的应用程序中,哪一半是Client
,哪一半是Server
是任意的。
现在,您可能会注意到,虽然Client
发送x
并接收y
作为响应,但Server
似乎向后。 它在收到请求x
之前发送响应y
! 事实上,它只需要落后一步操作——基于拉取的流中的服务器将希望将其响应y
发送到上一个请求,以便接收下一个请求x
。
举一个简单的例子,下面是一个请求将数字相加以计算 2 的幂的Client
:
-- |Client to generate powers of two
power2 :: Client (Int, Int) Int IO ()
power2 = go 1
where go n | n <= 1024 = do
liftIO $ print n
n' <- request (n,n) -- ask adder to add "n" and "n"
go n'
go n = liftIO $ print "Done"
编写服务器以添加数字有点棘手,因为这种"落后一步"的业务。 我们可以从写开始:
-- |Server to sum numbers
sum2 :: Server (Int, Int) Int IO ()
sum2 = do
(n,n) <- respond ??? -- send previous response to get current request
let n' = n+n
??? <- respond n' -- send current reponse to get next request
诀窍是通过接受第一个请求作为 monadic 操作的参数来开始工作:
-- |Server to sum numbers
sum2 :: (Int, Int) -> Server (Int, Int) Int IO ()
sum2 (m, n) = do
(m', n') <- respond (m+n) -- send response to get next request
sum2 (m', n') -- and loop
幸运的是,拉点连接器+>>
具有连接这些的正确类型:
mypipe :: Effect IO ()
mypipe = sum2 +>> power2
我们可以以通常的方式运行结果效果:
main :: IO ()
main = runEffect mypipe
ghci> main
1
2
4
8
16
32
64
128
256
512
1024
"Done"
请注意,对于这种类型的双向通信,请求和响应需要以同步锁步运行,因此您不能执行等效的屈服一次和等待两次。 如果要重新设计上面的示例以分两部分发送请求,则需要开发具有合理请求和响应类型的协议,例如:
data Req = First Int | Second Int
data Res = AckFirst | Answer Int
power2 = ...
AckFirst <- request n
Answer n' <- request n
sum2 = ...
First m' <- respond (Answer (m+n))
Second n' <- respond AckFirst
...
对于您的大脑/机器人应用程序,您可以将机器人设计为客户端:
robotC :: Client Color (Color,Turn) Identity ()
robotC = go newRobot
where
go r = do
(c, turn) <- request (color r)
go $ update c turn r
或服务器:
robotS :: Server (Color,Turn) Color Identity ()
robotS = go newRobot
where
go r = do
(c, turn) <- respond (color r)
go $ update c turn r
由于机器人在消耗输入之前产生输出,因此作为客户端,它将适合带有大脑服务器的基于拉动的流:
brainS :: Color -> Server Color (Color,Turn) Identity ()
brainS = ...
approach1 = brainS +>> robotC
或者作为服务器,它将适合带有大脑客户端的基于推送的流:
brainC :: Color -> Client (Color,Turn) Color Identity ()
brainC = ...
approach2 = robotS >>~ brainC