Functional reactive programming

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, and music, aiming to simplify these problems by explicitly modeling time.

Formulations of FRP

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

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.[3]

Discrete

Formulations such as Event-Driven FRP and Elm before version 0.17 require that updates are discrete and event-driven.[4] 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.[5]

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.[6]

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.[7] The key properties of this formulation are:

  • Modeling values that vary over continuous time, called "behaviors" and later "signals".
  • Modeling "events" which have occurrences at discrete points in time.
  • The system can be changed in response to events, generally termed "switching."
  • The separation of evaluation details such as sampling rate from the reactive model.

This semantic model of FRP in side-effect free languages is typically in terms of continuous functions, and typically over time.[8]

Interactive FRP

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

  • Create a data structure of actions which appear as the outputs. The actions must be run by an external interpreter or environment. This inherits all of the difficulties of the original stream I/O system of Haskell.[10]
  • Use Arrowized FRP and embed arrows which are capable of performing actions. The actions may also have identities, which allows them to maintain separate mutable stores for example. This is the approach taken by the Fudgets library.[11]
  • The novel approach is to allow actions to be run now (in the IO monad) but defer the receipt of their results until later.[12] This makes use of an interaction between the Event and IO monads, and is compatible with a more expression-oriented FRP:
planNow :: Event (IO a) -> IO (Event a)

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. On a regular interval, samples are pushed through a signal network. This approach has two drawbacks: it is very computation intensive to process samples on a regular interval, and the network has to wait up to the duration of the sampling interval to find out about changes to the input. Sampling is an example of push-based FRP.

The Reactive and Etage libraries on Hackage introduced an approach called push-pull FRP, which combines the best of push-based and pull-based FRP. In this approach, 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

  • The programming language Elm used to support FRP [13] but has since replaced it with a different pattern [14]
  • reflex is an efficient push/pull FRP implementation in Haskell with hosts for browser/DOM, SDL and Gloss.
  • reactive-banana is a target agnostic push FRP implementation in Haskell.
  • netwire and varying are arrowized, pull FRP implementations in Haskell.
  • Flapjax is a behavior/event FRP implementation in JavaScript.
  • React is an OCaml module for functional reactive programming.
  • Sodium is a push FRP implementation independent of a specific UI framework for several programming languages, such as Java, TypeScript and C#.
  • ReactiveX, popularized by its JavaScript implementation rxjs is a comprehensive cross-platform paradigm for implementing functional reactive programming by treating data as streams of observables.

See also

References

  1. Czaplicki, Evan (Apr 2012), Elm: Concurrent FRP for Functional GUIs (PDF) (thesis), Harvard .
  2. Elliott, Conal; Hudak, Paul. "Functional Reactive Animation". Functional Reactive Animation. ICFP ’97. Retrieved 14 July 2018.
  3. Nilsson, Henrik; Courtney, Antony; Peterson, John (Feb 2011) [2002], "Functional Reactive Programming, Continued" (PDF), Haskell Workshop (PDF)|format= requires |url= (help) (2) .
  4. Taha, Walid; Wan, Zhanyong; Hudak, Paul (2002), "Event-Driven FRP", PADL (PDF), Yale .
  5. Czaplicki, Evan; Chong, Stephen (2013), "Asynchronous Functional Reactive Programming for GUIs", PLDI, Harvard .
  6. Wan, Zhanyong; Taha, Walid; Hudak, Paul (Feb 2011), "Real-Time FRP", ICFP (PDF) (1) .
  7. Elliott, Conal; Hudak, Paul (1997), "Functional Reactive Animation", ICFP .
  8. Courtney, Antony; Elliott, Conal (Feb 2011) [2001], "Genuinely Functional User Interfaces" (PDF), Haskell Workshop, Yale .
  9. http://conal.net/blog/posts/why-classic-frp-does-not-fit-interactive-behavior
  10. https://courses.cs.washington.edu/courses/cse505/01au/functional/functional-io.pdf
  11. http://www.cse.chalmers.se/~hallgren/Thesis/
  12. http://www.cse.chalmers.se/~atze/papers/prprfrp.pdf
  13. Czaplicki, Evan (Apr 2012), Elm: Concurrent FRP for Functional GUIs (PDF) (thesis), Harvard .
  14. Czaplicki, Evan. "A Farewell to FRP". elm. Retrieved 14 July 2018.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.