I have the following piece of code which typechecks perfectly:
import Control.Applicative ((<*))
import Control.Monad (liftM)
import Data.Attoparsec.Text
import Pipes
import Pipes.Attoparsec (parsed, ParsingError)
import qualified Pipes.Prelude as P
import Pipes.Text.IO (fromHandle)
import qualified System.IO as IO
type LinkID = Int
data Link = Link {
lid :: LinkID,
llength :: Int
}
data Snap = Snap {
slid :: LinkID,
slength :: Int
}
getSnap
:: Monad m
=> Int
-> Producer Link m ()
-> Producer Snap m ()
-> Producer (LinkID, [Snap]) m ()
getSnap n lks snp = do
snp' <- lift (P.toListM snp)
lks >-> P.filter (\x -> lid x > n)
>-> P.map (\x -> (lid x, filter (\y -> lid x == slid y) snp'))
drawData :: (LinkID, [Snap]) -> IO ()
drawData = undefined
linkFile = "some file path"
snapshotFile = "some file path"
parseLink :: Parser Link
parseLink = undefined
snapshotParser :: Parser Snap
snapshotParser = undefined
Note that some parts of the code are defined as undefined for the sake of
brevity. Now when I include main function it doesn't typecheck:
main = IO.withFile linkFile IO.ReadMode $ \linkHandle ->
IO.withFile snapshotFile IO.ReadMode $ \snapHandle -> runEffect $
for (getSnap 2 (parsed (parseLink <* endOfLine) (fromHandle linkHandle))
(parsed (snapshotParser <* endOfLine) (fromHandle snapHandle))) $ \d
->
lift (drawData d)
The error:
Expected type: Producer Link IO ()
Actual type: Producer
Link
IO
(Either (ParsingError, Producer
Data.Text.Internal.Text IO ()) ())
In the return type of a call of `parsed'
In the second argument of `getSnap', namely
Now, in order to fix this problem, I change getSnap function:
getSnap
:: Monad m
=> Int
-> Producer Link m r
-> Producer Snap m r
-> Producer (LinkID, [Snap]) m r
getSnap n lks snp = do
snp' <- lift (P.toListM snp)
lks >-> P.filter (\x -> lid x > n)
>-> P.map (\x -> (lid x, filter (\y -> lid x == slid y) snp'))
Now, it seems P.toListM only operates on Producer a m () so, I want to
convert Producer a m r to that. What is the recommended way of solving this
problem ?
--
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].