gpt4 book ai didi

haskell - 在管道内运行单态消费者

转载 作者:行者123 更新时间:2023-12-02 10:56:24 25 4
gpt4 key购买 nike

how to run a Consumer inside a Pipe的问题已经被问过,但随后提供的答案需要 Consumer' 多态类型同义词:

{-# LANGUAGE RankNTypes #-}
import Pipes

toPipe :: Monad m => Consumer' i m o -> Pipe i o m ()
toPipe consumer = consumer >>= yield

现在,我遇到的问题是 Pipes.Vector , toVector 使用单态 Consumer 同义词:

toVector :: (PrimMonad m, MVector (Mutable v) e) => Consumer e (ToVector v e m) r

因此,该答案中的 toPipe 函数在这种情况下将不起作用:

{-# LANGUAGE RankNTypes #-}
module VectorPipe where

import Control.Monad.Primitive (PrimMonad)
import qualified Data.Vector.Generic as G
import Pipes
import Pipes.Vector

toPipe :: Monad m => Consumer' i m o -> Pipe i o m ()
toPipe consumer = consumer >>= yield

vectorPipe :: (PrimMonad m, G.Vector v a) => Pipe a (v a) m ()
vectorPipe = toPipe (runToVectorP toVector)

{-

VectorPipe.hs:13:35-42: Could not deduce (y' ~ ()) …
from the context (PrimMonad m, G.Vector v a)
bound by the type signature for
vectorPipe :: (PrimMonad m, G.Vector v a) => Pipe a (v a) m ()
at /Users/casillas/GitHub/tau-sigma/VectorPipe.hs:12:15-62
‘y'’ is a rigid type variable bound by
a type expected by the context: Proxy () a y' y m (v a)
at /Users/casillas/GitHub/tau-sigma/VectorPipe.hs:13:14
Expected type: Proxy () a y' y (ToVector v a m) r0
Actual type: Consumer a (ToVector v a m) r0
In the first argument of ‘runToVectorP’, namely ‘toVector’
In the first argument of ‘toPipe’, namely ‘(runToVectorP toVector)’
VectorPipe.hs:13:35-42: Could not deduce (y ~ X) …
from the context (PrimMonad m, G.Vector v a)
bound by the type signature for
vectorPipe :: (PrimMonad m, G.Vector v a) => Pipe a (v a) m ()
at /Users/casillas/GitHub/tau-sigma/VectorPipe.hs:12:15-62
‘y’ is a rigid type variable bound by
a type expected by the context: Proxy () a y' y m (v a)
at /Users/casillas/GitHub/tau-sigma/VectorPipe.hs:13:14
Expected type: Proxy () a y' y (ToVector v a m) r0
Actual type: Consumer a (ToVector v a m) r0
In the first argument of ‘runToVectorP’, namely ‘toVector’
In the first argument of ‘toPipe’, namely ‘(runToVectorP toVector)’
Compilation failed.

-}

有什么建议吗? toVector 的签名是否过于狭窄? (我是一个管道菜鸟,无法告诉...编辑:我尝试将pipes-vector中的签名更改为Consumer' ;代码可以编译,但看起来 vectorPipe 永远不会产生结果。)

最佳答案

嗯,我在玩了几天后想出了这个:

import Control.Monad

import Pipes
import Pipes.Core ((//>), (>\\), closed)


-- | Convert a 'Consumer' into a 'Pipe' that 'yield's the consumer's
-- final result.
fromConsumer :: Monad m => Consumer i m r -> Pipe i r m ()
fromConsumer c = c //> closed >>= yield

example1 :: MonadIO m => m ()
example1 = runEffect $ each "abcde" >-> fromConsumer (example' 3) >-> P.print
where
example' :: Monad m => Int -> Consumer a m [a]
example' n = replicateM n await

-- λ> example1
-- "abc"


-- | Convert a 'Producer' into a 'Pipe' that ignores its upstream
-- and sends the producer's contents downstream.
fromProducer :: Monad m => Producer o m r -> Pipe i o m r
fromProducer p = closed >\\ p

example2 :: MonadIO m => m ()
example2 = runEffect $ P.stdinLn >-> fromProducer (each "abcde") >-> P.print

-- Ignores stdin:
--
-- λ> example2
-- 'a'
-- 'b'
-- 'c'
-- 'd'
-- 'e'

关于haskell - 在管道内运行单态消费者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30391465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com