All,
I've been trying to use pipes to tackle a problem recently, but I'm a bit
stuck.
> {-# LANGUAGE NoMonomorphismRestriction #-}
> import Pipes
> import qualified Pipes.Prelude as P
> import Pipes.Parse (input)
> import Pipes.Binary (DecodingError, Get, decodeGet, decodeGetMany)
> import Data.ByteString (ByteString)
> import Control.Monad.Trans.State.Strict (StateT, runStateT)
Here's the deal: I have an input consisting of a stream of bytes (or
`ByteString`s, more exactly):
> myInput :: Monad m => Producer' ByteString m ()
> myInput = undefined
This input is actually a `Header`, followed by zero-or-more `Packet`s. Both
have a `Get a` (as found in the `binary` package):
> data Header = Header deriving Show
> getHeader :: Get Header
> getHeader = return Header
> data Packet = Packet deriving Show
> getPacket :: Get Packet
> getPacket = return Packet
I'd like to create a function which somehow performs this parsing. It looks
like it's common to use `StateT` to provide a producer to a parsing thing,
so here's +- the type I expected to write:
> type ParseResult a = Either DecodingError a
> type Packets m r = Producer Packet m (ParseResult r)
> parseStream :: StateT (Producer ByteString m r) m (ParseResult (Header,
Packets m r))
Finally, the code I came up with (which doesn't type-check with the above,
and drives me crazy while trying to interpret the inferred type):
> parseStream = do
> res <- decodeGet getHeader
> case res of
> Left e -> return $ Left e
> Right (_, h) ->
> return $ Right (h, decodeGetMany getPacket input >-> P.map
snd)
Here's what GHC infers for the above:
parseStream :: forall (m :: * -> *) r a' a r1 (m1 :: * -> *).
(Monad m, Monad m1) =>
StateT
(Producer ByteString m r)
m
(Either
DecodingError
(Header,
Proxy
a'
a
()
Packet
(StateT (Producer ByteString m1 r1) m1)
(Either
(DecodingError,
Producer ByteString (StateT (Producer
ByteString m1 r1) m1) r1)
r1)))
This doesn't feel like the above is what I'm trying to achieve.
Any pointers how to handle this situation correctly? I guess some fidling
with State* and ErrorT and whatnot would be required, but after a fair
number of attempts I can't get things aligned correctly.
Thanks,
Nicolas
--
You received this message because you are subscribed to the Google Groups
"Haskell Pipes" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].