[Haskell-cafe] Is this a correct explanation of FRP?

Peter Minten peter.minten at orange.nl
Sat Mar 31 18:15:25 CEST 2012


On Fri, 2012-03-30 at 09:15 +0300, Michael Snoyman wrote:

> First you state that we shouldn't use `union` for the `ePitch` Event,
> and then you used it for `bOctave`. Would it be more efficient to
> implement bOctave as someting like:
> 
>     eOctave :: Event t (Int -> Int)
>     eOctave =
>         filterJust toStep <$> eKey
>       where
>         toStep '+' = Just (+ 1)
>         toStep '-' = Just (subtract 1)
>         toStep _ = Nothing
> 
>     bOctave :: Behavior t Octave
>     bOctave = accumB 0 eOctave

Yes. Though it's slightly less bad, the case with ePitch was something
like 6 appends. It was mostly a case of badly copying the style from the
examples and not realizing the examples use event streams from different
outside sources. I've adapted the example to use something similar to
your eOctave.

> Also, I'm left wondering: how would you create a new event stream in
> the first place? You're telling us to just rely on `eKey`, which is
> fair, but a great follow-up would demonstrate building it. Looking
> through the docs I found `newEvent`, but I'm not quite certain how I
> would combine it all together.

The updated document, which now lives at
http://www.haskell.org/haskellwiki/FRP_explanation_using_reactive-banana
contains a "Making the example runnable" section which shows how connect
the example with the outside world.

The short version, regarding the creation of new events, is that you
have to do it in two parts. You need newAddHandler in the IO monad to
get a (a -> IO ()) function that fires the event as well as something
called an AddHandler and fromAddHandler in the NetworkDescription monad
to get an event from that AddHandler. It's not possible to get values
out of the NetworkDescription monad (without IORef tricks) and events
can only be created within a NetworkDescription monad.

The newEvent function looks like what you'd want, but because you can't
get the event firing function out of NetworkDescription its use is
limited.

Greetings,

Peter Minten




More information about the Haskell-Cafe mailing list