Search code examples
haskellfrpconduitloopshaskell-pipes

What is the connection between Iteratees and FRP?


It seems to me that there is a strong connection between the two ideas. My guess is that FRP could be implemented in terms of Iteratees if there would be a way to express arbitrary graphs with Iteratees. But afaik they only support chain-like structures.

Could someone shed some light on this?


Solution

  • It's the other way around. There is a strong connection between AFRP and stream processing. In fact AFRP is a form of stream processing, and you can use the idiom to implement something very similar to pipes:

    data Pipe m a b =
        Pipe {
          cleanup :: m (),
          feed    :: [a] -> m (Maybe [b], Pipe m a b)
        }
    

    That's an extension of wire categories as found in Netwire. It receives the next chunk of input and returns Nothing when it stops producing. Using this a file reader would have the following type:

    readFile :: (MonadIO m) => FilePath -> Pipe m a ByteString
    

    Pipe is a family of applicative functors, so to apply a simple function to the stream elements you could just use fmap:

    fmap (B.map toUpper) . readFile
    

    For your convenience it's also a family of profunctors.

    The most interesting feature is that this is a family of Alternative functors. That allows you to route streams around and allow multiple stream processors to "try" before giving up. This can be extended to a full-fledged parsing library that can even use some static information for optimization purposes.