Вопрос как запустить Consumer
внутри 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
слишком узкая? (Я слишком большой любитель каналов, чтобы сказать... EDIT: я попытался изменить подпись в pipes-vector
на Consumer'
; код компилируется, но похоже, что vectorPipe
никогда не дает результата.)
Pipe a (v a) m ()
? Вам нужно сгруппировать входящие значения в поток векторов заданного размера или что-то в этом роде? - person danidiaz   schedule 22.05.2015Pipe
изConsumer
иProducer
, соединенных наоборот. Если нет вопроса на этот счет, я мог бы опубликовать его. - person Luis Casillas   schedule 22.05.2015toVector
с производителем, который считывает данные, запускает полученный конвейер и извлекает результирующий вектор. Позже вы можете использовать вектор в других конвейерах, если хотите. - person danidiaz   schedule 22.05.2015