2013-06-20 38 views
1

考慮利用鏡頭,管道和單子,循環以下Haskell代碼:管道單子whileJust_問題

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進行處理。


+1

51行標記。 – kvanberendonck

回答

2

這工作:

import Control 
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 a SessionIO Packet 
processBroadcast b = undefined 

-- | Handles networking 
protocol :: Conduit Packet SessionIO Packet 
protocol = do 
    ch <- lift $ use broadcastChan 
    -- line 51: 
    whileJust_ (liftIO . atomically $ tryReadTChan ch) processBroadcast 
    liftIO $ putStrLn "End" 

由於功能processBroadcast只處理單個廣播(我認爲),沒有必要限制管道的輸入型播出。爲了在協議中使用它,該通道的輸入類型必須與協議Conduit的輸入類型(即Packet)相匹配,因此在協議中,processBroadcast中的a被實例化爲Packet。

+0

爲了更進一步,我做了一些研究,正確的類型別名是' - > Producer SessionIO Packet'。謝謝! – kvanberendonck