2
votes

I am still trying to wrap my head around Haskell and FRP. Specifically, I have worked through some examples using reactive-banana package and starting to get FRP.

However, I still don't understand how the event network knows when an input event has occurred. My understanding is that, unlike NodeJS which has an event loop constantly checking for user inputs, FRP utilizes a different framework for "waiting" or "checking" user inputs or external signals.

From my reading, FRP makes time explicit. By coupling time with either an event or behavior, somehow the network always knows when an external stimulus has fired.

I have read many papers by Conal, Hudak, et al. and the explanations are too technical. Please provide less technical explanation.

Thanks for your help.

1
I think most just cheat those into their events using real word IO stuff - for example this uses SDL-timers to pump in time - from what I understand you use similar things (your usual imperative/async/event-based IO stuff) to build your basis (see here)Random Dev
I've written a few examples of connecting FRP libraries to real world UI stuff. An example connecting reactive-banana to GLUT demonstrates firing events from real world IO stuff. An example connecting reactive-banana to gloss demonstrates stepping events in an explicit event loop, but taking a detour through IO to use the existing support in Reactive.Banana.Frameworks.Cirdec

1 Answers

3
votes

It is useful to keep in mind the distinction between FRP, which is concerned with building interesting Events and Behaviors from basic ones, and platform specific "glue code", which provides a collection of basic Events, like the current mouse position or keyboard presses.

In the Reactive-Banana library, this distinction is reflected in the module structure, Reactive.Banana.Combinators is concerned with the first part, while Reactive.Banana.Frameworks is concerned with the second part.

Now, understanding how the second part (basic Events) works is not important for understanding how the first part (FRP) works; in fact, different libraries may make very different implementation choices.

That said, in the Reactive-Banana library, an event network is essentially a huge callback function that registers itself to external event sources (called AddHandler in the library). Whenever one of these external sources call the callback function, the latter will traverse the graph of Event and Behavior in dependency order, perform the necessary updates to internal state, and finally run the actions previously registered with reactimate.

The magic of FRP is that the library user doesn't see any of these implementation details, though it is sometimes useful to know that "event network = a huge callback function".