Functional reactive programming

Short description: Programming paradigm

Not to be confused with factory reset protection (FRP), a feature in some Android devices.

## Contents

* 1 Formulations of FRP
* 1.1 Continuous
* 1.2 Discrete
* 2 Interactive FRP
* 3 Implementation issues
* 4 Implementations
* 5 See also
* 6 References

## 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.[2]

### 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.[3] 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.[4] This formulation is also referred to as denotative continuous time programming (DCTP).[5]

### Discrete

Formulations such as Event-Driven FRP and versions of Elm prior to 0.17 require that updates are discrete and event-driven.[6] 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.[7]

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.[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 must 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 input/output (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] and, more generally, Monadic Stream Functions.[12][13]
* The novel approach is to allow actions to be run now (in the IO monad) but defer the receipt of their results until later.[14] 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, 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:

* Yampa is an arrowized, efficient, pure Haskell implementation with SDL, SDL2, OpenGL and HTML DOM support.
* The language Elm used to support FRP[15] but has since replaced it with a different pattern.[16]
* reflex is an efficient push–pull FRP implementation in Haskell with hosts for 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 user interface (UI) framework for several languages, such as Java, TypeScript, and C#.[17]
* Dunai is a fast implementation in Haskell using Monadic Stream Functions[13] that supports Classic and Arrowized FRP.
* ObservableComputations, a cross-platform .NET implementation.
* Stella is an actor model-based reactive 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).[18] Actors are suitable for use in distributed reactive systems.[19]
* TidalCycles is a pure FRP domain specific language for musical pattern, embedded in the Haskell language.

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

## See also

* Incremental computing
* Stream processing

## References

1. ↑ Elliott, Conal; Hudak, Paul. "Functional Reactive Animation". ICFP ’97. http://conal.net/papers/icfp97/.
2. ↑ Nilsson, Henrik; Courtney, Antony; Peterson, John (Feb 2011), "Functional Reactive Programming, Continued", Haskell Workshop, https://www.antonycourtney.com/pubs/frpcont.pdf
3. ↑ Elliott, Conal; Hudak, Paul (1997), "Functional Reactive Animation", ICFP, http://conal.net/papers/icfp97/
4. ↑ Courtney, Antony; Elliott, Conal (Feb 2011), "Genuinely Functional User Interfaces", Haskell Workshop, Yale, https://www.antonycourtney.com/pubs/genuinely-functional-guis.ps.gz
5. ↑ Elliot, Conal (2014), "Denotational Design", LambdaJam, http://conal.net/talks/denotational-design-lambdajam-2014.pdf, retrieved 5 May 2023
6. ↑ Taha, Walid; Wan, Zhanyong; Hudak, Paul (2002), "Event-Driven FRP", PADL, Yale, http://www.cs.yale.edu/homes/zwan/papers/mcu/efrp.pdf, retrieved 2013-09-23
7. ↑ Czaplicki, Evan; Chong, Stephen (2013), "Asynchronous Functional Reactive Programming for GUIs", PLDI, Harvard, http://people.seas.harvard.edu/~chong/abstracts/CzaplickiC13.html
8. ↑ Wan, Zhanyong; Taha, Walid; Hudak, Paul (Feb 2011), "Real-Time FRP", ICFP, http://haskell.cs.yale.edu/wp-content/uploads/2011/02/rt-frp.pdf, retrieved 2013-09-23
9. ↑ Elliott, Conal (December 9, 2008). "Why classic FRP does not fit interactive behavior". http://conal.net/blog/posts/why-classic-frp-does-not-fit-interactive-behavior.
10. ↑ Borning, Alan. "I/O in Purely Functional Languages". https://courses.cs.washington.edu/courses/cse505/01au/functional/functional-io.pdf.
11. ↑ Carlsson, Magnus; Hallgren, Thomas (1998). "Fudgets – Purely Functional Processes with applications to Graphical User Interfaces". https://cth.altocumulus.org/~hallgren/Thesis/fudgets_thesis_color.pdf.
12. ↑ Perez, Ivan; Barenz, Manuel; Nilsson, Henrik (July 2016), "Functional Reactive Programming, Refactored", Haskell Symposium, http://www.cs.nott.ac.uk/~psxip1/papers/2016-HaskellSymposium-Perez-Barenz-Nilsson-FRPRefactored-short.pdf
13. ↑ 13.0 13.1 "Data.MonadicStreamFunction". https://hackage-content.haskell.org/package/dunai-0.14.1/docs/Data-MonadicStreamFunction.html.
14. ↑ "Practical Principled FRP". http://www.cse.chalmers.se/~atze/papers/prprfrp.pdf.
15. ↑ Czaplicki, Evan (Apr 2012), Elm: Concurrent FRP for Functional GUIs, Harvard, https://www.seas.harvard.edu/sites/default/files/files/archived/Czaplicki.pdf, retrieved 2015-02-17
16. ↑ Czaplicki, Evan. "A Farewell to FRP". http://elm-lang.org/blog/farewell-to-frp.
17. ↑ "Sodium FRP Library on Github". https://github.com/SodiumFRP/sodium.
18. ↑ 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), 166, pp. 19:1–19:29, doi:10.4230/LIPIcs.ECOOP.2020.19, ISBN 9783959771542, https://drops.dagstuhl.de/opus/volltexte/2020/13176/
19. ↑ 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, 6, pp. 14:1–14:36, doi:10.22152/programming-journal.org/2022/6/14, https://programming-journal.org/2022/6/14/
20. ↑ "ReactiveX". https://reactivex.io/intro.html.

* v
* t
* e

Programming paradigms

Imperative|

| Structured|

* Jackson structures
* Block-structured
* Modular
* Non-structured
* Procedural
* Programming in the large and in the small
* Design by contract
* Invariant-based
* Nested function

|

Object-oriented|

* Class-based, Prototype-based, Object-based
* Agent
* Immutable object
* Persistent
* Uniform function call syntax

Declarative|

| Functional|

* Recursive
* Anonymous function (Partial application)
* Higher-order
* Purely functional
* Total
* Strict
* GADTs
* Dependent types
* Functional logic
* Point-free style
* Expression-oriented
* Applicative, Concatenative
* Function-level, Value-level
* Monad

|

Dataflow|

* Flow-based
* Reactive (Functional reactive)
* Signals
* Streams
* Synchronous

Logic|

* Abductive logic
* Answer set
* Constraint (Constraint logic)
* Inductive logic
* Nondeterministic
* Ontology
* Probabilistic logic
* Query

Domain-
specific
language
(DSL)|

* Algebraic modeling
* Array
* Automata-based (Action)
* Command (Spacecraft)
* Differentiable
* End-user
* Grammar-oriented
* Interface description
* Language-oriented
* List comprehension
* Low-code
* Modeling
* Natural language
* Non-English-based
* Page description
* Pipes and filters
* Probabilistic
* Quantum
* Scientific
* Scripting
* Set-theoretic
* Simulation
* Stack-based
* System
* Tactile
* Templating
* Transformation (Graph rewriting, Production, Pattern)
* Visual

Concurrent,
parallel|

* Actor-based
* Automatic mutual exclusion
* Choreographic programming
* Concurrent logic (Concurrent constraint logic)
* Concurrent OO
* Macroprogramming
* Multitier programming
* Organic computing
* Parallel programming models
* Partitioned global address space
* Process-oriented
* Relativistic programming
* Service-oriented
* Structured concurrency

Metaprogramming|

* Attribute-oriented
* Automatic (Inductive)
* Dynamic
* Extensible
* Generic
* Homoiconicity
* Interactive
* Macro (Hygienic)
* Metalinguistic abstraction
* Multi-stage
* Program synthesis (Bayesian, by demonstration, by example, vibe coding)
* Reflective
* Self-modifying code
* Symbolic
* Template

Separation
of concerns|

* Aspects
* Components
* Data-driven
* Data-oriented
* Event-driven
* Features
* Literate
* Roles
* Subjects

Comparisons/Lists|

Comparison (multi-paradigm, object-oriented, functional), List (OO, by type)

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Functional reactive programming. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Functional_reactive_programming&oldid=4151653"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template