Skip to content
Takuo Watanabe edited this page Oct 24, 2018 · 1 revision

FRP Introduction

Programming to handle timing is a complicated and stressful task for programmers. This is because the programs handling timing are hard to be written as a simple input-process-output pattern. If a program is written as a simple input-process-output pattern, it can be easily tested by giving inputs of a test-case and checking the processed outputs. The programs handling timing implemented by call-backs or pollings have IO codes scattered in the whole program, so that it is hard to test such programs by giving certain inputs. Untestable programs are hotbeds of bugs and hard to be maintained. This is why programming to handle timing is a hard task.

Functional Programming (FP) is one way to write programs as input-process-output pattern. However, it is hard to handle timing with the only naive FP abstraction. Functional Reactive Programming (FRP) is originally invented by Conal Elliott in 1997 to handle timing easily within FP abstraction.

FRP Concepts

The FRP's essential invention is a fact that to handle timing can be regarded as to handle time-varying value. The followings are examples of handling timing.

  • handling event of mouse click
  • handling sensor's value continuously

Thus, these are regarded as:

  • handling time-varying value such that:
    • True (when mouse is clicked at that moment)
    • Flase (when mouse is not clicked at that moment)
  • handling time-varying value such that the sensor's value at that moment

Composing time-varying values

It is possible to generate a new time-varying value by composing other time-varying values. For example, you can make a time-varying value such that:

  • sensor's value at that moment (when mouse is clicked at that moment)
  • constant value 0 (when mouse is not clicked at that moment)

Referring the past value of the time-varying value

It is possible to refer the past value of the time-varying value. For example, you can make a time-varying value such that the number of times mouse is clicked so far.

Associating time-varying value to IO

To work program with time-varying values, primitive (non-composed) time-varying values must be associated to IO code to get values of that time.

For example, the above True/False time-varying value of mouse click must be associated to Input code of handling event of mouse click.

Assume that you want to display the above time-varying value such that the number of times mouse is clicked, this must be associated to Output code to print integer on display.

FRP Benefits

By putting a layer between IO codes and others, program's logic codes can be separated from complicated IO codes and written as input-process-output code which can be tested.

Clone this wiki locally