Pipes and plug with Control.Concurrent.MonadIO

In the following code, I am trying to combine 2 Producerinto 1. Everyone is of the same type. They will be combined by each of the two inputs Producer, executed in a separate thread and consumed Consumer, which puts the values ​​in the unagi chan (I use unagi chan for performance). The producer that reads from chan returns. I want to be able to use any Producerif it can run IO, so I am restricting the monad class to MonadIOand HasFork.

import Pipes (Producer, Consumer, (>->), yield, await, runEffect)
import Control.Concurrent.Chan.Unagi (InChan, OutChan, newChan, readChan, writeChan)
import Control.Monad (forever, void)
import Control.Concurrent.MonadIO (MonadIO, HasFork, liftIO, fork)

combine :: (MonadIO m, HasFork m) => Producer a m r -> Producer a m r -> Producer a m r
combine p1 p2 = do
  (inChan, outChan) <- liftIO $ newChan
  t1 <- fork . void . runEffect $ p1 >-> (consumer inChan)
  t2 <- fork . void . runEffect $ p2 >-> (consumer inChan)
  producer outChan

producer :: (MonadIO m) => OutChan a -> Producer a m r
producer outChan = forever $ do
  msg <- liftIO $ readChan outChan
  yield msg

consumer :: (MonadIO m) => InChan a -> Consumer a m r
consumer inChan = forever $ do
  msg <- await
  liftIO $ writeChan inChan msg

However, I get the following error:

    • Couldn't match type ‘m’
                 with ‘Pipes.Internal.Proxy Pipes.Internal.X () () a m’
  ‘m’ is a rigid type variable bound by
    the type signature for:
      combine :: forall (m :: * -> *) a r.
                 (MonadIO m, HasFork m) =>
                 Producer a m r -> Producer a m r -> Producer a m r
    at src/Pipes/Unagi.hs:8:12
  Expected type: Pipes.Internal.Proxy
                   Pipes.Internal.X () () a m GHC.Conc.Sync.ThreadId
    Actual type: m GHC.Conc.Sync.ThreadId

Am I trying to do this?

+4
source share
1 answer

lift fork.

t1 <- lift . fork . ...

fork . ... m ThreadId, do Producer a m.

, lifted-base fork.

+2

Source: https://habr.com/ru/post/1685335/


All Articles