考虑以下使用 Lens、Conduit 和 Monad-Loops 的 Haskell 代码:
type Broadcast = Int
type BroadcastChan = TChan Broadcast
data SessionState = SessionState
{ _broadcastChan :: BroadcastChan
}
makeLenses ''SessionState
type Session m = StateT SessionState m
type SessionIO = Session IO
-- This function requires the state and IO...
processBroadcast :: Broadcast -> Conduit Broadcast SessionIO Packet
processBroadcast = undefined
-- | Handles networking
protocol :: Conduit Packet SessionIO Packet
protocol = do
ch <- lift $ use broadcastChan
-- line 51:
whileJust_ (liftIO . atomically $ tryReadTChan ch) (\x -> yield $ processBroadcast x)
liftIO $ putStrLn "End"
通过大量的提升,我无法解决以下类型错误:
src\Burrito\Network.hs:51:61:
Couldn't match expected type `Packet'
with actual type `Conduit Broadcast SessionIO Packet'
Expected type: ConduitM Packet Packet SessionIO b0
Actual type: ConduitM
Packet (Conduit Broadcast SessionIO Packet) m0 ()
In the expression: yield $ processBroadcast x
In the second argument of `whileJust_', namely
`(\ x -> yield $ processBroadcast x)'
一些指导或建议将不胜感激。请记住,这processBroadcast
需要 Session 携带的状态进行处理。
monad-loops 文档:http ://hackage.haskell.org/packages/archive/monad-loops/0.4.2/doc/html/Control-Monad-Loops.html
管道文档: http ://hackage.haskell.org/packages/archive/conduit/1.0.6/doc/html/Data-Conduit.html
stm (TChan) 文档: http ://hackage.haskell.org/packages/archive/stm/2.4.2/doc/html/Control-Concurrent-STM-TChan.html