Hi, 

While tinkering with the foldl package I found a way to convert some pipes 
from pipes-parse into folds from foldl. 

It was inspired by this old post by Paul Chiusano: 
http://pchiusano.blogspot.com.es/2011/12/programmatic-translation-to-iteratees.html
 

The idea is to have parsers that are polymorphic in the base monad, and 
then sneak a iteratee-like monad underneath.

The resulting folds work in the Except monad to allow for early 
termination. 

When a parser fails, the fold is terminated right away. When the parser 
"returns early" all further inputs to the fold are silently ignored and the 
result of the parser is returned at the end. And the "done" action of the 
fold sends an EOF to the producer used by the parser.  

The code can be found here: 
https://github.com/danidiaz/pipetastic-foldl/blob/master/src/Control/Foldl/Pipes.hs

There are two functions, "fromParser" for pure parsers, and "fromParserIO" 
for parsers with effects in IO: 


fromParser :: (forall m. Monad m => Parser a m (Either e r)) 
           -> L.FoldM (Except e) a r

fromParserIO :: MonadIO m 
             => (forall t. (MonadTrans t, MonadIO (t m)) => Parser a (t m) 
(Either e r))
             -> L.FoldM (ExceptT e m) a r

To be honest, I don't really understand the second function very well. The 
types for the internal state are hairy and I arrived at the implementation 
mainly by playing type tetris. But is seems to work, in the limited tests I 
made.

But maybe there is a more direct conversion?

-- 
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 haskell-pipes+unsubscr...@googlegroups.com.
To post to this group, send email to haskell-pipes@googlegroups.com.

Reply via email to