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, 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.
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.
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:
- 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. This formulation is also referred to as denotative continuous time programming (DCTP).
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.
It has been pointed out that the ordinary FRP model, from inputs to outputs, is poorly suited to interactive programs. 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.
- 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 and, more generally, Monadic Stream Functions.
- The novel approach is to allow actions to be run now (in the IO monad) but defer the receipt of their results until later. 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)
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 has to wait up to the duration of one computation step to find out about 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 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.
- Yampa is an arrowized, efficient, pure Haskell implementation with SDL, SDL2, OpenGL and HTML DOM support.
- The programming language Elm used to support FRP  but has since replaced it with a different pattern.
- 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.
- 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#.
- Dunai is a fast implementation in Haskell using Monadic Stream Functions that supports Classic and Arrowized FRP.
- ObservableComputations, a cross-platform .NET implementation.
- Stella is an actor-based reactive programming language that demonstrates a model of "actors" and "reactors" which aims to avoid the issues of combining imperative code with reactive code (by separating them in actors and reactors). Actors are suitable for use in distributed reactive systems.
- TidalCycles is a pure FRP domain specific language for musical pattern, embedded in the Haskell programming language.
- ^ Elliott, Conal; Hudak, Paul. "Functional Reactive Animation". Functional Reactive Animation. ICFP ’97. Retrieved 14 July 2018.
- ^ Nilsson, Henrik; Courtney, Antony; Peterson, John (Feb 2011) , "Functional Reactive Programming, Continued", Haskell Workshop (PDF).
- ^ Elliott, Conal; Hudak, Paul (1997), "Functional Reactive Animation", ICFP.
- ^ Courtney, Antony; Elliott, Conal (Feb 2011) , "Genuinely Functional User Interfaces", Haskell Workshop, Yale.
- ^ Elliot, Conal (2014), "Denotational Design" (PDF), LambdaJam, retrieved 5 May 2023
- ^ Taha, Walid; Wan, Zhanyong; Hudak, Paul (2002), "Event-Driven FRP", PADL (PDF), Yale, archived from the original (PDF) on 2013-09-28, retrieved 2013-09-23.
- ^ Czaplicki, Evan; Chong, Stephen (2013), "Asynchronous Functional Reactive Programming for GUIs", PLDI, Harvard.
- ^ Wan, Zhanyong; Taha, Walid; Hudak, Paul (Feb 2011), "Real-Time FRP", ICFP (PDF), archived from the original (PDF) on 2013-09-28, retrieved 2013-09-23.
- ^ "Conal Elliott » Why classic FRP does not fit interactive behavior". December 9, 2008. Archived from the original on 2022-10-12.
- ^ Borning, Alan. "I/O in Purely Functional Languages" (PDF). Archived (PDF) from the original on 2022-04-28.
- ^ Carlsson, Magnus; Hallgren, Thomas (1998). "Fudgets – Purely Functional Processes with applications to Graphical User Interfaces" (PDF). Archived (PDF) from the original on 2022-10-15.
- ^ Perez, Ivan; Barenz, Manuel; Nilsson, Henrik (July 2016), "Functional Reactive Programming, Refactored", Haskell Symposium (PDF).
- ^ Atze van der Ploeg; Claessen, Koen. "Practical Principled FRP" (PDF). Archived from the original (PDF) on 2015-07-01. Retrieved 2015-07-24.
- ^ Czaplicki, Evan (Apr 2012), Elm: Concurrent FRP for Functional GUIs (PDF) (thesis), Harvard, archived from the original (PDF) on 2016-06-04, retrieved 2015-02-17.
- ^ Czaplicki, Evan. "A Farewell to FRP". elm. Retrieved 14 July 2018.
- ^ Van den Vonder, Sam; Renaux, Thierry; Oeyen, Bjarno; De Koster, Joeri; De Meuter, Wolfgang (2020), "Tackling the Awkward Squad for Reactive Programming: The Actor-Reactor Model", Leibniz International Proceedings in Informatics (LIPIcs), vol. 166, pp. 19:1–19:29, doi:10.4230/LIPIcs.ECOOP.2020.19, ISBN 9783959771542.
- ^ Van den Vonder, Sam; Renaux, Thierry; De Meuter, Wolfgang (2022), "Topology-Level Reactivity in Distributed Reactive Programs: Reactive Acquaintance Management using Flocks", The Art, Science, and Engineering of Programming, vol. 6:3, pp. 14:1–14:36, doi:10.22152/programming-journal.org/2022/6/14
- ^ "ReactiveX". ReactiveX.io. Retrieved July 3, 2022.