Llayland’s Food, Oracle, and Haskell

March 25, 2012

Trying to figure out FRP

Filed under: Haskell — Tags: , , — llayland @ 10:12 pm

This is my attempt at learning FRP and sharing as I go.

I’ll be using Reactive-Banana as it looks simple and seems to have the most activity.

> import Reactive.Banana
> import Control.Monad

To keep things simple I will forgo a gui library. A console application is not the usual target for FRP programs, but it allows me to focus on the logic instead of the framework.
So, we need an event loop for the console that will allow us to fire events.

> eventLoop fire = loop
>    where
>    loop = do
>       s <- getLine
>       fire s
>       when (s /= “quit”) loop

A non FRP implementation of our program is very simple; simply make the fire function generate an IO action:

> echoNonFRP = eventLoop $ putStrLn . (“You typed: “++)

In this trivial example, it makes sense to handle the input right here. FRP is definitely overkill, but I will implement an FRP solution to show how it allows us to seperate the firing of an event from response to the event.

> echoFRP = do

first we use the newAddHandler action to generate a pair of a handler and a firing function. These are linked together somehow so that the handler can be used to recognize events that are created by the firing function

>   (addHandler,fire) <- newAddHandler

we can use the addhandler to build a network description. We’ll flesh out the details of the description later.  For now, we just compile it into a nework and then actuate the network to turn it into an IO action.

>   compile (fromAddHandler addHandler >>= networkDescription’) >>= actuate

The type of fire (a -> IO ()) is exactly what we need to pass into our eventLoop.

>   eventLoop fire

Now we just need to fill in our network description. The reactimate function is used to emit the value of an event when it occurs. We need an IO action instead of a String, so we map a function (a -> IO) over the event.

> networkDescription’ lineE = reactimate $ fmap (putStrLn . (“You typed: “++)) lineE

and that is it. This functions identically to the non FRP version.

here it is without interuption and special handling for the quit command:

> networkDescription lineE = do
>   let run p f = lineE >>= \a -> reactimate $ fmap f (filterE p a)
>   run (/=”quit”) $ putStrLn . (“You typed: “++)
>   run (==”quit”) $ const (putStrLn “Goodbye”)

> echo = do
>   (addHandler,fire) <- newAddHandler
>   compile (networkDescription $ fromAddHandler addHandler ) >>= actuate
>   eventLoop fire

About these ads

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: