Functional reactive programming explained

Functional reactive programming (FRP) is a programming paradigm for reactive programming (asynchronous dataflow programming) using the building blocks of functional programming (e.g., map, reduce, filter). FRP has been used for programming graphical user interfaces (GUIs), robotics, games, and music, aiming to simplify these problems by explicitly modeling time.

Formulations of FRP

The original formulation of functional reactive programming can be found in the ICFP 97 paper Functional Reactive Animation by Conal Elliott and Paul Hudak.[1]

FRP has taken many forms since its introduction in 1997. One axis of diversity is discrete vs. continuous semantics. Another axis is how FRP systems can be changed dynamically.

Continuous

The earliest formulation of FRP used continuous semantics, aiming to abstract over many operational details that are not important to the meaning of a program. The key properties of this formulation are:

This semantic model of FRP in side-effect free languages is typically in terms of continuous functions, and typically over time. This formulation is also referred to as denotative continuous time programming (DCTP).

Discrete

Formulations such as Event-Driven FRP and versions of Elm prior to 0.17 require that updates are discrete and event-driven. These formulations have pushed for practical FRP, focusing on semantics that have a simple API that can be implemented efficiently in a setting such as robotics or in a web-browser.

In these formulations, it is common that the ideas of behaviors and events are combined into signals that always have a current value, but change discretely.

Interactive FRP

It has been pointed out that the ordinary FRP model, from inputs to outputs, is poorly suited to interactive programs.[2] Lacking the ability to "run" programs within a mapping from inputs to outputs may mean one of the following solutions must be used:

Implementation issues

There are two types of FRP systems, push-based and pull-based. Push-based systems take events and push them through a signal network to achieve a result. Pull-based systems wait until the result is demanded, and work backwards through the network to retrieve the value demanded.

Some FRP systems such as Yampa use sampling, where samples are pulled by the signal network. This approach has a drawback: the network must wait up to the duration of one computation step to learn of changes to the input. Sampling is an example of pull-based FRP.

The Reactive and Etage libraries on Hackage introduced an approach called push-pull FRP. In it, only when the next event on a purely defined stream (such as a list of fixed events with times) is demanded, that event is constructed. These purely defined streams act like lazy lists in Haskell. That is the pull-based half. The push-based half is used when events external to the system are brought in. The external events are pushed to consumers, so that they can find out about an event the instant it is issued.

Implementations

Implementations exist for many programming languages, including:

ReactiveX, popularized by its JavaScript implementation rxjs, is functional and reactive but differs from functional reactive programming.[7]

See also

Notes and References

  1. Web site: Elliott . Conal . Hudak . Paul . Functional Reactive Animation . Functional Reactive Animation . ICFP ’97 . 14 July 2018 . fran.
  2. Web site: Why classic FRP does not fit interactive behavior . Elliott . Conal . December 9, 2008 . https://web.archive.org/web/20221012020700/http://conal.net/blog/posts/why-classic-frp-does-not-fit-interactive-behavior . 2022-10-12 . live.
  3. Web site: I/O in Purely Functional Languages . Alan . Borning . https://web.archive.org/web/20220428171557/https://courses.cs.washington.edu/courses/cse505/01au/functional/functional-io.pdf . 2022-04-28 . live.
  4. Web site: Fudgets – Purely Functional Processes with applications to Graphical User Interfaces. Magnus. Carlsson. Thomas. Hallgren. 1998. https://web.archive.org/web/20221015141447/https://cth.altocumulus.org/~hallgren/Thesis/fudgets_thesis_color.pdf. 2022-10-15. live.
  5. Web site: Practical Principled FRP . Atze van der Ploeg . Koen . Claessen . 2015-07-24 . https://web.archive.org/web/20150701030428/http://www.cse.chalmers.se/~atze/papers/prprfrp.pdf . 2015-07-01 . dead .
  6. Web site: Czaplicki . Evan . A Farewell to FRP . elm . 14 July 2018 . farewell-to-frp.
  7. Web site: ReactiveX . ReactiveX.io . July 3, 2022.