Functional Reactive Programming

Simply put, FRP allows one to easily create time based programs (such as games and simulations) using functional languages, such as Haskell. These tutorials were developed by me and Tom Poliquin to help beginners understand functional reactive programming from a pratical point of view. We will explore two FRP libraries in Haskell: Reactive and Elerea. We will use them to build four programs of increasing complexity. Implementations of FRP are unfortunately very unfriendly to new comers. They contain large numbers of functions, many of which are of concern only to the internals of reactive, and are not of interest to the user. The user thus becomes lost in this vast sea of functionality. Another problem facing newbies is the esoteric naming structure of the system. Many of the names of functions (e.g. stepper) were derived from previous iterations of FRP, whose names were derived from still previous iterations, and so-on. We will attempt to present only the functions of concern to the user, and to sift out the unnecessary ones. We will use a variety of informal and perhaps inaccurate language in this discussion. In addition, we have created a forum for these tutorials.

Reactive

Reactive is a implementation of FRP developed by Conal Elliott. To fully understand Reactive, we suggest you study the formal papers found at Conal Elliott's website.

Elerea

Elerea is a implementation of FRP developed by Gergely Patai. It is much simpler than Reactive because it contains no events, only behaviors.

Running the Sample Programs

This page lists a variety of demos of FRP. The tar files include the source files and bash scripts that automatically build and run the programs. The Reactive tutorials are tested with GHC version 6.12.1, while the Elerea tutorials are tested with both GHC version 6.12.3, and GHC version 7.0.1. Reactive does not appear to install properly on the newer GHC versions due to package conflicts. You can learn more about how the demos were constructed by following the above links.



Program 1: The Furnace

As a result of Tony's interest in temperature control for chemical processes, we choose to create the furnace simulator. The program was designed to be extremely simple, yet still demonstrate a wide range of reactive functionality (all four functions are used). There are two behaviors in this program: the temperature and the controller. The temperature represents the temperature of the furnace. The controller represents the amount of heat which is feed to the furnace by an electric heater. Each of these behaviors is updated every time at tick occurs. The temperature is updated using a simple model of heat flow. The heater is turned on if the temperature is too low, and it is turned off if the temperature is too high. It will print out the temperature every tick.
Using Reactive
Using Elerea


Program 2: The Furnace with Human Control

This upgraded furnace is now human controlled. Press the up and down keys to control the goal temperature of the furnace. The furnace will try to reach the temperature the human (you) supplied. There is now a new behavior which contains the goal temperature. AccumE is used on the keyboard events. Note that this also serves as an example of keyboard input in reactive.
Using Reactive
Using Elerea


Program 3: The Hybrid Robot Simulator

The robot simulator simulates a wall following robot that drives around a closed box. It follows the right wall using simple on-off algorithm. The robot will also turn to the left if a encounters a wall infront of it. The simulator is made up of two programs: 1. a reactive-based generator 2. a non-reactive opengl GUI. The reactive simulator prints out the position of the robot and pipes it to the opengl GUI, which recives the data and displays the robot. This program requires haskell opengl support. There have been problems with version 1 of the robot simulator due to breaking changes in the haskell opengl system. If you get errors about MatrixComponent, please try v2
Using Reactive
Using Reactive (if you recieve compilation errors)
Using Elerea


Program 4: The FRP Robot Simulator

The robot simulator simulates a wall following robot that drives around a closed box. It follows the right wall using simple on-off algorithm. The robot will also turn to the left if a encounters a wall infront of it. This simulator consists of a single program that uses reactive GLUT to display the robot, handle camera view changes, and move the screen. This program requires haskell opengl support. Please note that reactive glut is incompatible with the new opengl changes, so we have attempted to patch it in version 2.
Using Reactive
Using Reactive (if you recieve compilation errors)
Using Elerea