Difference between revisions of "Functional Reactive Programming"

From HaskellWiki
Jump to: navigation, search
(Libraries: reflex main url)
(New history section)
(2 intermediate revisions by the same user not shown)
Line 15: Line 15:
 
newtype Behavior a =
 
newtype Behavior a =
 
Behavior {
 
Behavior {
at :: Time -> a
+
at :: Time -> a -- see page 3 of Elliott and Hudak's paper.
 
}
 
}
   
Line 48: Line 48:
 
=== Events ===
 
=== Events ===
   
''To do''
 
  +
While behaviours work well for describing properties which are continuous (e.g. the motion of an animated object), other properties are more ''discrete'' (e.g. what button on the mouse was clicked or the screen position where it happened). Events more easily accommodate information like this, by associating the data value of interest with a particular time:
  +
  +
<haskell>
  +
newtype Event a =
  +
Event {
  +
occ :: (Time, a) -- see page 3 of Elliott and Hudak's paper.
  +
}
  +
</haskell>
  +
  +
A series of events is most easily represented as a list of <code>Event</code>-values, commonly referred to as an ''event stream'' (or simply a ''stream'').
   
 
== Libraries ==
 
== Libraries ==
Line 104: Line 113:
 
* [http://mcis.western.edu/~jpeterson/ John Peterson]
 
* [http://mcis.western.edu/~jpeterson/ John Peterson]
 
* Ertugrul Söylemez
 
* Ertugrul Söylemez
  +
  +
== History ==
  +
  +
=== A possible predecessor? ===
  +
  +
In his thesis [https://core.ac.uk/download/9835633.pdf Functional Real-Time Programming: The Language Ruth And Its Semantics], some parts of the semantics Dave Harrison gives for ''Ruth'' bears a curious resemblance to those for FRP:
  +
  +
* from page 48:
  +
:{|
  +
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
  +
A channel is an infinite stream of timestamped data values, or messages, each message denoting an event in the system.
  +
</div>
  +
<sup> </sup>
  +
<haskell>
  +
type Channel a = [Event a]
  +
</haskell>
  +
|}
  +
  +
* from page 50:
  +
:{|
  +
|<div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote">
  +
A unique clock is automatically supplied to each Ruth process at run-time, to provide real-time information, [...]
  +
</div>
  +
<sup> </sup>
  +
<haskell>
  +
type Process a = Clock -> a
  +
</haskell>
  +
  +
where a <code>Clock</code> value is a tree of time-values, in contrast to the direct use of time-values in FRP:
  +
<haskell>
  +
-- from page 61
  +
type Clock = Tree Integer -- integer must be above zero
  +
  +
data Tree a = Node { contents :: a,
  +
left :: Tree a,
  +
right :: Tree a }
  +
</haskell>
  +
|}
  +
  +
As a ''Ruth'' process runs, the nodes of the clock are used incrementally:
  +
* to ascertain the current time (<code>contents</code>), and
  +
* to provide other clocks for use elsewhere (<code>left</code> and <code>right</code>).
   
 
[[Category:FRP|*]]
 
[[Category:FRP|*]]

Revision as of 22:48, 12 November 2021

Functional Reactive Programming (FRP) integrates time flow and compositional events into functional programming. This provides an elegant way to express computation in domains such as interactive animations, robotics, computer vision, user interfaces, and simulation.


Introduction

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

Behaviors

Traditionally a widget-based user interface is created by a series of imperative actions. First an action is invoked to create an edit widget, then additional actions can be invoked to read its current content, set it to a specific value or to assign an event callback for when the content changes. This is tedious and error-prone.

A better way to represent an edit widget's content is a time-varying value, called a behavior. The basic idea is that a time-varying value can be represented as a function of time:

newtype Behavior a =
    Behavior {
      at :: Time -> a  -- see page 3 of Elliott and Hudak's paper.
    }

myName :: Behavior Text

myName `at` yesterday

This is only a theoretical model, because a time-varying value can represent something impure like the content of an edit widget, the current value of a database entry as well as the system clock's current time. Using this model the current content of an edit widget would be a regular first class value:

myEditWidget :: Behavior Text

In most frameworks there is an applicative interface for behaviors, such that you can combine them easily:

liftA2 (<>) myEdit1 myEdit2

The result is a time-varying value that represents the concatenation of myEdit1 and myEdit2. This could be the value of a third widget, a label, to display the concatenation. The following is a hypothetical example:

do edit1 <- editWidget
   edit2 <- editWidget
   label <- label (liftA2 (<>) edit1 edit2)
   {- ... -}

Without behaviors you would have to write event callback actions for the edit widgets to update the label's content. With behaviors you can express this relationship declaratively.

Events

While behaviours work well for describing properties which are continuous (e.g. the motion of an animated object), other properties are more discrete (e.g. what button on the mouse was clicked or the screen position where it happened). Events more easily accommodate information like this, by associating the data value of interest with a particular time:

newtype Event a =
    Event {
      occ :: (Time, a) -- see page 3 of Elliott and Hudak's paper.
    }

A series of events is most easily represented as a list of Event-values, commonly referred to as an event stream (or simply a stream).

Libraries

A simple, practical comparison between FRP libraries is done by frp-zoo

Publications and talks

Books

Blog posts

People

History

A possible predecessor?

In his thesis Functional Real-Time Programming: The Language Ruth And Its Semantics, some parts of the semantics Dave Harrison gives for Ruth bears a curious resemblance to those for FRP:

  • from page 48:

A channel is an infinite stream of timestamped data values, or messages, each message denoting an event in the system.

type Channel a = [Event a]
  • from page 50:

A unique clock is automatically supplied to each Ruth process at run-time, to provide real-time information, [...]

type Process a = Clock -> a

where a Clock value is a tree of time-values, in contrast to the direct use of time-values in FRP:

 -- from page 61
type Clock = Tree Integer -- integer must be above zero

data Tree a = Node { contents :: a,
                     left     :: Tree a,
                     right    :: Tree a }

As a Ruth process runs, the nodes of the clock are used incrementally:

  • to ascertain the current time (contents), and
  • to provide other clocks for use elsewhere (left and right).