Reactive programming
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, reactive programming is a programming paradigm
Programming paradigm
A programming paradigm is a fundamental style of computer programming. Paradigms differ in the concepts and abstractions used to represent the elements of a program and the steps that compose a computation A programming paradigm is a fundamental style of computer programming. (Compare with a...

 oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow.

For example, in an imperative programming setting, would mean that is being assigned the result of in the instant the expression is evaluated. Later, the values of and can be changed with no effect on the value of .

In reactive programming, the value of would be automatically updated based on the new values.

A modern spreadsheet
Spreadsheet
A spreadsheet is a computer application that simulates a paper accounting worksheet. It displays multiple cells usually in a two-dimensional matrix or grid consisting of rows and columns. Each cell contains alphanumeric text, numeric values or formulas...

 program is an example of reactive programming. Spreadsheet cells can contain literal values, or formulas such as "=B1+C1" that are evaluated based on other cells. Whenever the value of the other cells change, the value of the formula is automatically updated.

Another example is a hardware description language
Hardware description language
In electronics, a hardware description language or HDL is any language from a class of computer languages, specification languages, or modeling languages for formal description and design of electronic circuits, and most-commonly, digital logic...

 such as Verilog
Verilog
In the semiconductor and electronic design industry, Verilog is a hardware description language used to model electronic systems. Verilog HDL, not to be confused with VHDL , is most commonly used in the design, verification, and implementation of digital logic chips at the register-transfer level...

. In this case reactive programming allows us to model changes as they propagate through a circuit.

Reactive programming has foremost been proposed as a way to simplify the creation of interactive user interfaces, animations or in real time systems, but is essentially a general programming paradigm.

For example, in a Model-view-controller
Model-view-controller
Model–view–controller is a software architecture, currently considered an architectural pattern used in software engineering. The pattern isolates "domain logic" from the user interface , permitting independent development, testing and maintenance of each .Model View Controller...

 architecture, reactive programming can allow changes in the underlying model to automatically be reflected in the view, and vice versa.

Degrees of explicitness

Reactive programming languages can range from very explicit ones where data flows are set up by using arrows, to implicit where the data flows are derived from language constructs that looks similar to those of imperative or functional programming. For example, in implicitly lifted functional reactive programming
Functional reactive programming
Functional reactive programming is a programming paradigm for reactive programming using the building blocks of functional programmingThe key points of FRP are:* Input is viewed as a "behavior", or time-varying stream of events...

 (FRP) a function call might implicitly cause a node in a data flow graph to be constructed. Reactive programming libraries for dynamic languages (such as the Lisp "Cells" and Python "Trellis" libraries) can construct a dependency graph from runtime analysis of the values read during a function's execution, allowing data flow specifications to be both implicit and dynamic.

Sometimes the term reactive programming refers to the architectural level of software engineering, where individual nodes in the data flow graph are ordinary programs that communicate with each other.

Higher-order reactive programming

Reactive programming can be purely static where the data flows are set up statically, or be dynamic where the data flows can change during the execution of a program.

The use of data switches in the data flow graph could to some extent make a static data flow graph appear as dynamic, and blur the distinction slightly. True dynamic reactive programming however could use imperative programming to reconstruct the data flow graph. Also, reactive programming could be said to be of higher order if it supports the idea that data flows could be used to construct other data flows. That is, the resulting value out of a data flow is another data flow graph that is executed using the same evaluation model as the first.

Data flow differentiation

Ideally all data changes are propagated instantly, but this cannot be assured in practice. Instead it might be necessary to give different parts of the data flow graph different evaluation priorities. This can be called differentiated reactive programming.

For example, in a word processor the marking of spelling errors need not be totally in sync with the inserting of characters. Here differentiated reactive programming could potentially be used to give the spell checker lower priority, allowing it to be delayed while keeping other data-flows instantaneous.

However, such differentiation introduces additional design complexity. For example, deciding how to define the different data flow areas, and how to handle event passing between different data flow areas.

Evaluation models of reactive programming

Evaluation of reactive programs is not necessarily based on how stack based programming languages are evaluated. Instead, when some data is changed, the change is propagated to all data that is derived partially or completely from the data that was changed. This change propagation could be achieved in a number of ways, where perhaps the most natural way is an invalidate/lazy-revalidate scheme.

It could be problematic to just naively propagate a change using a stack, because of potential exponential update complexity if the data structure has a certain shape. One such shape can be described as "repeated diamonds shape", and has the following structure:
An→Bn→An+1, An→Cn→An+1, where n=1,2... This problem could be overcome by propagating invalidation only when some data is not already invalidated, and later re-validate the data when needed using lazy evaluation
Lazy evaluation
In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...

.

One inherent problem for reactive programming is that most computations that would be evaluated and forgotten in a normal programming language, needs to be represented in the memory as data-structures. This could potentially make RP highly memory consuming. However, research on what is called lowering could potentially overcome this problem.

On the other side, reactive programming is a form of what could be described as "explicit parallelism", and could therefore be beneficial for utilizing the power of parallel hardware.

Similarities with observer pattern

Reactive programming has principal similarities with the observer pattern
Observer pattern
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods...

 commonly used in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

. However, integrating the data flow concepts into the programming language would make it easier to express them, and could therefore increase the granularity of the data flow graph. For example, the observer pattern commonly describes data-flows between whole objects/classes, whereas object-oriented reactive programming could target the members of objects/classes.

The stack based evaluation model of common object orientation is also not entirely suitable for data flow propagation, as occurrences of the "repeated diamond shape" in the data structures could make the program face exponential complexities. But because of its relatively limited use, and low granularity, this is rarely a problem for the observer pattern in practice.

Imperative

It is possible to fuse reactive programming with ordinary imperative programming. In such a paradigm, there would be imperative programs operating upon reactive data structures. Such a set-up is analogous to constraint imperative programming, but where constraint imperative programming manages bidirectional constraints, reactive imperative programming just manages acyclic data flows.

Object-oriented

Object-oriented reactive programming (OORP) is a combination of object oriented programming and reactive programming. Perhaps the most natural way to make such a combination is as follows: Instead of methods and fields, objects have reactions that automatically re-evaluate when the other reactions they depend on have been modified.

If an OORP programming language maintains its imperative methods, it would also fall under the category of imperative reactive programming.

The OOPic
OOPic
The OOPic is an Object Oriented Programmable Integrated Circuit. Created by Savage Innovations, this PIC microcontroller comes with an IDE that supports programming in syntaxes based on the BASIC, Java and C programming languages.There are three versions currently on the market: the , the and the...

 microprocessor supports this style of programming via its virtual circuits.

External links

  • MIMOSA Project of INRIA - ENSMP, a general site about reactive programming.
  • Yampa, an attempt at functional reactive programming.
  • Traits A Python module that supports basic reactive programming using explicitly-registered dependency notifications
  • Experimenting with Cells Demonstration of a simple reactive programming application in Lisp, using the Cells library
  • Cells A dataflow extension for Common Lisp that supports higher-order reactive programming (including imperative and OO RP) using automatically-determined dependencies
  • Trellis A Python package that supports higher-order reactive programming (including imperative and OO RP in a software transactional memory
    Software transactional memory
    In computer science, software transactional memory is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. It is an alternative to lock-based synchronization. A transaction in this context is a piece of code that...

    ) using automatically-determined dependencies
  • SugarCubes A set of classes for reactive programming in Java
  • What is (functional) reactive programming? at Stack Overflow
    Stack overflow
    In software, a stack overflow occurs when too much memory is used on the call stack. The call stack contains a limited amount of memory, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture,...

  • Reactive Programming in .NET More about reactive programming and Reactive Extensions (.NET Rx) in Microsoft.NET
  • Obtics A library for practical FRP in .NET
  • Yoopf for Python, object-oriented Programming by Formula (open-source)
  • LuaGravity runtime extensions for reactive programming in Lua.
  • WPF Data Binding expresses reactive programming using .NET dynamic languages to build fully interactive and consistent user interfaces.
  • Deprecating the Observer Pattern A paper by Ingo Maier, Tiark Rompf and Martin Odersky outlining an RP framework for Scala.
  • FrTime A Language for Reactive Programs in Racket (Scheme dialect).
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK