Old-reactive
From HaskellWiki
m (→Data.SFuture) |
(Category:Reactivity removed (overlap with FRP), Category:Event removed (it is for meetings)) |
||
(20 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
[[Category:FRP]] | [[Category:FRP]] | ||
[[Category:Packages]] | [[Category:Packages]] | ||
Line 6: | Line 4: | ||
== Abstract == | == Abstract == | ||
− | '''Reactive''' is a simple foundation for programming reactive systems functionally. Like Fran/FRP, it has a notions of (reactive) behaviors and events. Like [[DataDriven]], Reactive has an efficient, data-driven implementation. The main difference between Reactive and DataDriven are | + | '''Note:''' I have retired the old version of Reactive on darcs.haskell.org. |
+ | Please use the new [[Reactive]]. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | '''Reactive''' is a simple foundation for [[Functional Reactive Programming|programming reactive systems functionally]]. Like Fran/FRP, it has a notions of (reactive) behaviors and events. Like [[DataDriven]], Reactive has an efficient, data-driven implementation. The main difference between Reactive and DataDriven are | ||
* Reactive provides and builds on "functional futures", which in turn build on [[GHC/Concurrency|Concurrent Haskell]] threads, while DataDriven builds on continuation-based computations; and | * Reactive provides and builds on "functional futures", which in turn build on [[GHC/Concurrency|Concurrent Haskell]] threads, while DataDriven builds on continuation-based computations; and | ||
* The algebras of events and reactive values (called events and sources in DataDriven) are ''purely'' functional. I couldn't figure out how to accomplish that in DataDriven. | * The algebras of events and reactive values (called events and sources in DataDriven) are ''purely'' functional. I couldn't figure out how to accomplish that in DataDriven. | ||
* Reactive manages (I hope) to get the efficiency of data-driven computation with a (sort-of) demand-driven architecture. For that reason, Reactive is garbage-collector-friendly, while DataDriven depends on weak references (because [[DataDriven#GC_favors_demand-driven_computation|GC favors demand-driven computation]].) | * Reactive manages (I hope) to get the efficiency of data-driven computation with a (sort-of) demand-driven architecture. For that reason, Reactive is garbage-collector-friendly, while DataDriven depends on weak references (because [[DataDriven#GC_favors_demand-driven_computation|GC favors demand-driven computation]].) | ||
* Reactive elegantly and efficiently ''caches'' values. | * Reactive elegantly and efficiently ''caches'' values. | ||
− | * Reactive uses the term " | + | * Reactive uses the term "reactive values" (<hask>Reactive</hask>), where DataDriven uses "sources" (<hask>Source</hask>). |
− | The inspiration for Reactive was Mike Sperber's | + | The inspiration for Reactive was Mike Sperber's [http://www-pu.informatik.uni-tuebingen.de/lula/deutsch/publications.html Lula] implementation of FRP. Mike used blocking threads, which I had never considered for FRP before a conversation with him at ICFP 2007. While playing with the idea, I realized that I could give a very elegant and efficient solution to caching, which DataDriven doesn't do. (For an application <hask>f <*> a</hask> of a varying function to a varying argument, caching remembers the latest function to apply to a new argument and the latest argument to which to apply a new function.) |
As with [[DataDriven]], Reactive provides instances for <hask>Monoid</hask>, <hask>Functor</hask>, <hask>Applicative</hask>, and <hask>Monad</hask>. | As with [[DataDriven]], Reactive provides instances for <hask>Monoid</hask>, <hask>Functor</hask>, <hask>Applicative</hask>, and <hask>Monad</hask>. | ||
Besides this wiki page, here are more ways to find out about Reactive: | Besides this wiki page, here are more ways to find out about Reactive: | ||
− | * Read [http://darcs.haskell.org/packages/reactive/doc/html | + | * Read [http://darcs.haskell.org/packages/reactive/doc/html the Haddock docs]. |
− | * Get the code repository: '''<tt>darcs get http://darcs.haskell.org/packages/reactive</tt>'''. | + | * Get the code repository: '''<tt>darcs get http://darcs.haskell.org/packages/old-reactive/</tt>'''. (Old version, to be replaced in September 08.) |
* Install from [http://hackage.haskell.org Hackage]. | * Install from [http://hackage.haskell.org Hackage]. | ||
* See the [[Reactive/Versions| version history]]. | * See the [[Reactive/Versions| version history]]. | ||
− | + | Also, the paper ''[http://conal.net/papers/simply-reactive/ Simply Efficient Functional Reactivity]'', and its [http://conal.net/blog/posts/simply-efficient-functional-reactivity/ blog post with discussion], describe a (not-yet-released) successor to Reactive that solves the determinacy problem mentioned below. | |
== Modules == | == Modules == | ||
Line 29: | Line 32: | ||
=== Data.Future === | === Data.Future === | ||
− | A | + | A ''[http://en.wikipedia.org/wiki/Futures_and_promises future]'' is a value that will become knowable only later. Primitive futures can be things like "the value of the next key you press", or "the value of LambdaPix stock at noon next Monday". The term "[http://en.wikipedia.org/wiki/Futures_and_promises promise]" might be more fitting. |
Composition is via standard type classes: <hask>Functor</hask>, <hask>Applicative</hask>, <hask>Monad</hask>, and <hask>Monoid</hask>. | Composition is via standard type classes: <hask>Functor</hask>, <hask>Applicative</hask>, <hask>Monad</hask>, and <hask>Monoid</hask>. | ||
* <hask>Monoid</hask>: <hask>mempty</hask> is a future that never becomes knowable. <hask>a `mappend` b</hask> is whichever of <hask>a</hask> and <hask>b</hask> is knowable first. | * <hask>Monoid</hask>: <hask>mempty</hask> is a future that never becomes knowable. <hask>a `mappend` b</hask> is whichever of <hask>a</hask> and <hask>b</hask> is knowable first. | ||
* <hask>Functor</hask>: apply a function to a future. The result is knowable when the given future is knowable. | * <hask>Functor</hask>: apply a function to a future. The result is knowable when the given future is knowable. | ||
− | * <hask>Applicative</hask>: <hask>pure</hask> gives value knowable since the beginning of time. <hask>(<*>)</hask> applies a future function to a future argument. Result available when | + | * <hask>Applicative</hask>: <hask>pure</hask> gives value knowable since the beginning of time. <hask>(<*>)</hask> applies a future function to a future argument. Result available when ''both'' are available, i.e., it becomes knowable when the later of the two futures becomes knowable. |
* Monad: <hask>return</hask> is the same as <hask>pure</hask> (as always). <hask>(>>=)</hask> cascades futures. <hask>join</hask> resolves a future future value into a future value. | * Monad: <hask>return</hask> is the same as <hask>pure</hask> (as always). <hask>(>>=)</hask> cascades futures. <hask>join</hask> resolves a future future value into a future value. | ||
The current implementation is nondeterministic in <hask>mappend</hask> for futures that become knowable at the same time or nearly the same time. I want to make a deterministic implementation. | The current implementation is nondeterministic in <hask>mappend</hask> for futures that become knowable at the same time or nearly the same time. I want to make a deterministic implementation. | ||
+ | |||
+ | ==== Garbage collection of futures ==== | ||
+ | |||
+ | Baker & Hewitt's 1977 paper ''[http://citeseer.ist.psu.edu/baker77incremental.html The Incremental Garbage Collection of Processes]'' discusses using garbage collection to prevent the useless threads from consuming resources. In particular, consider <hask>Future</hask>'s <hask>mappend</hask> (sometimes called "parallel or"). Once one thread completes, the other threads are then useless, and some might consume resources forever. My current implementation kill the losing threads. Baker & Hewitt suggest instead using garbage collection. I'm stumped about how to GC non-winning threads in a race between futures ("parallel or"). The winner-kills-loser approach seems to work fine, though is potentially dangerous w.r.t locked resources. Still, the elegance of a GC-based solution appeals to me. | ||
+ | |||
+ | ==== Concurrent Haskell vs STM ==== | ||
+ | |||
+ | Futures are implemented using Concurrent Haskell's <hask>MVar</hask>s. I first tried using STM and <hask>TVar</hask>s, simply using <hask>orElse</hask> to implement <hask>mappend</hask> for futures. However, I didn't see how to avoid nesting <hask>atomically</hask>, which yielded a run-time error. | ||
=== Data.SFuture === | === Data.SFuture === | ||
Line 45: | Line 56: | ||
=== Data.Reactive === | === Data.Reactive === | ||
− | This module defines ''events'' and ''reactive values''. An event is stream of future values in order of availability. A reactive value is a discretly time-varying value. These two types are closely linked: a reactive value is | + | This module defines ''events'' and ''reactive values''. An event is stream of future values in order of availability. A reactive value is a discretly time-varying value. These two types are closely linked: a reactive value is defined by an initial value and an event that yields future values; while an event is simply a future reactive value. |
<haskell> | <haskell> | ||
+ | data Reactive a = a `Stepper` Event a | ||
newtype Event a = Event (Future (Reactive a)) | newtype Event a = Event (Future (Reactive a)) | ||
− | |||
</haskell> | </haskell> | ||
− | This <hask>Reactive</hask> representation can be thought of a | + | This <hask>Reactive</hask> representation can be thought of a ''reactive weak head normal form'', to which arbitrary reactive expressions may be rewritten. The rewrite rules and their justification in terms of simple denotational semantics will be described in an upcoming paper. |
+ | |||
+ | Many of the operations on events and reactive values are packaged as instances of standard classes, as described below. See the module documentation for the other operations. | ||
+ | |||
+ | ==== Instances for Event ==== | ||
+ | |||
+ | * '''<hask>Monoid</hask>''': <hask>mempty</hask> is the event that never occurs, and <hask>e `mappend` e'</hask> is the event that combines occurrences from <hask>e</hask> and <hask>e'</hask>. (Fran's <hask>neverE</hask> and <hask>(.|.)</hask>.) | ||
+ | * '''<hask>Functor</hask>''': <hask>fmap f e</hask> is the event that occurs whenever <hask>e</hask> occurs, and whose occurrence values come from applying <hask>f</hask> to the values from <hask>e</hask>. (Fran's <hask>(==>)</hask>.) | ||
+ | * '''<hask>Applicative</hask>''': <hask>pure a</hask> is an event with a single occurrence, available from the beginning of time. <hask>ef <*> ex</hask> is an event whose occurrences are made from the ''product'' of the occurrences of <hask>ef</hask> and <hask>ex</hask>. For every occurrence <hask>f</hask> at time <hask>tf</hask> of <hask>ef</hask> and occurrence <hask>x</hask> at time <hask>tx</hask> of <hask>ex</hask>, <hask>ef <*> ex</hask> has an occurrence <hask>f x</hask> at time <hask>max tf tx</hask>. | ||
+ | * '''<hask>Monad</hask>''': <hask>return a</hask> is the same as <hask>pure a</hask> (as always). In <hask>e >>= f</hask>, each occurrence of <hask>e</hask> leads, through <hask>f</hask>, to a new event. Similarly for <hask>join ee</hask>, which is somehow simpler for me to think about. The occurrences of <hask>e >>= f</hask> (or <hask>join ee</hask>) correspond to the union of the occurrences of all such events. For example, suppose we're playing Asteroids and tracking collisions. Each collision can break an asteroid into more of them, each of which has to be tracked for more collisions. Another example: A chat room has an "enter" event, whose occurrences contain new events like "speak". | ||
+ | |||
+ | ==== Instances for Reactive ==== | ||
+ | |||
+ | The instances for <hask>Reactive</hask> can be understood in terms of (a) a simple semantics of reactive values as functions of time, and (b) the corresponding instances for functions. The semantics is given by the function <hask>at :: Reactive a -> (Time -> a)</hask>. | ||
+ | * '''<hask>Monoid</hask>''': a typical lifted monoid. If <hask>o</hask> is a monoid, then <hask>Reactive o</hask> is a monoid, with <hask>mempty = pure mempty</hask>, and <hask>mappend = liftA2 mappend</hask>. In other words, <hask>mempty `at` t == mempty</hask>, and <hask>(r `mappend` s) `at` t == (r `at` t) `mappend` (s `at` t).</hask> | ||
+ | * '''<hask>Functor</hask>''': <hask>fmap f r `at` t == f (r `at` t)</hask>. | ||
+ | * '''<hask>Applicative</hask>''': <hask>pure a `at` t == a</hask>, and <hask>(s <*> r) `at` t == (s `at` t) (r `at` t)</hask>. | ||
+ | * '''<hask>Monad</hask>''': <hask>return a `at` t == a</hask>, and <hask>join rr `at` t == (rr `at` t) `at` t</hask>. As always, <hask>(r >>= f) == join (fmap f r)</hask>. | ||
+ | |||
+ | ==== Continuous reactive behaviors ==== | ||
Although the basic <hask>Reactive</hask> type describes ''discretely''-changing values, ''continuously''-changing are defined simply by composing <hask>Reactive</hask> and a simple type functions of time (see below). | Although the basic <hask>Reactive</hask> type describes ''discretely''-changing values, ''continuously''-changing are defined simply by composing <hask>Reactive</hask> and a simple type functions of time (see below). |
Latest revision as of 10:40, 17 July 2011
Contents |
[edit] 1 Abstract
Note: I have retired the old version of Reactive on darcs.haskell.org. Please use the new Reactive.
Reactive is a simple foundation for programming reactive systems functionally. Like Fran/FRP, it has a notions of (reactive) behaviors and events. Like DataDriven, Reactive has an efficient, data-driven implementation. The main difference between Reactive and DataDriven are
- Reactive provides and builds on "functional futures", which in turn build on Concurrent Haskell threads, while DataDriven builds on continuation-based computations; and
- The algebras of events and reactive values (called events and sources in DataDriven) are purely functional. I couldn't figure out how to accomplish that in DataDriven.
- Reactive manages (I hope) to get the efficiency of data-driven computation with a (sort-of) demand-driven architecture. For that reason, Reactive is garbage-collector-friendly, while DataDriven depends on weak references (because GC favors demand-driven computation.)
- Reactive elegantly and efficiently caches values.
- Reactive uses the term "reactive values" (), where DataDriven uses "sources" (Reactive).Source
Besides this wiki page, here are more ways to find out about Reactive:
- Read the Haddock docs.
- Get the code repository: darcs get http://darcs.haskell.org/packages/old-reactive/. (Old version, to be replaced in September 08.)
- Install from Hackage.
- See the version history.
Also, the paper Simply Efficient Functional Reactivity, and its blog post with discussion, describe a (not-yet-released) successor to Reactive that solves the determinacy problem mentioned below.
[edit] 2 Modules
[edit] 2.1 Data.Future
A future is a value that will become knowable only later. Primitive futures can be things like "the value of the next key you press", or "the value of LambdaPix stock at noon next Monday". The term "promise" might be more fitting.
Composition is via standard type classes:- :Monoidis a future that never becomes knowable.memptyis whichever ofa `mappend` bandais knowable first.b
- : apply a function to a future. The result is knowable when the given future is knowable.Functor
- :Applicativegives value knowable since the beginning of time.pureapplies a future function to a future argument. Result available when both are available, i.e., it becomes knowable when the later of the two futures becomes knowable.(<*>)
- Monad: is the same asreturn(as always).purecascades futures.(>>=)resolves a future future value into a future value.join
[edit] 2.1.1 Garbage collection of futures
Baker & Hewitt's 1977 paper The Incremental Garbage Collection of Processes discusses using garbage collection to prevent the useless threads from consuming resources. In particular, consider[edit] 2.1.2 Concurrent Haskell vs STM
Futures are implemented using Concurrent Haskell's[edit] 2.2 Data.SFuture
A target denotational semantics for Data.Future -- simple, precise, and deterministic, in terms of time/value pairs.
[edit] 2.3 Data.Reactive
This module defines events and reactive values. An event is stream of future values in order of availability. A reactive value is a discretly time-varying value. These two types are closely linked: a reactive value is defined by an initial value and an event that yields future values; while an event is simply a future reactive value.
data Reactive a = a `Stepper` Event a newtype Event a = Event (Future (Reactive a))
Many of the operations on events and reactive values are packaged as instances of standard classes, as described below. See the module documentation for the other operations.
[edit] 2.3.1 Instances for Event
- :Monoidis the event that never occurs, andmemptyis the event that combines occurrences frome `mappend` e'ande. (Fran'se'andneverE.)(.|.)
- :Functoris the event that occurs wheneverfmap f eoccurs, and whose occurrence values come from applyingeto the values fromf. (Fran'se.)(==>)
- :Applicativeis an event with a single occurrence, available from the beginning of time.pure ais an event whose occurrences are made from the product of the occurrences ofef <*> exandef. For every occurrenceexat timefoftfand occurrenceefat timexoftx,exhas an occurrenceef <*> exat timef x.max tf tx
- :Monadis the same asreturn a(as always). Inpure a, each occurrence ofe >>= fleads, throughe, to a new event. Similarly forf, which is somehow simpler for me to think about. The occurrences ofjoin ee(ore >>= f) correspond to the union of the occurrences of all such events. For example, suppose we're playing Asteroids and tracking collisions. Each collision can break an asteroid into more of them, each of which has to be tracked for more collisions. Another example: A chat room has an "enter" event, whose occurrences contain new events like "speak".join ee
[edit] 2.3.2 Instances for Reactive
The instances for- : a typical lifted monoid. IfMonoidis a monoid, thenois a monoid, withReactive o, andmempty = pure mempty. In other words,mappend = liftA2 mappend, andmempty `at` t == mempty(r `mappend` s) `at` t == (r `at` t) `mappend` (s `at` t).
- :Functor.fmap f r `at` t == f (r `at` t)
- :Applicative, andpure a `at` t == a.(s <*> r) `at` t == (s `at` t) (r `at` t)
- :Monad, andreturn a `at` t == a. As always,join rr `at` t == (rr `at` t) `at` t.(r >>= f) == join (fmap f r)
[edit] 2.3.3 Continuous reactive behaviors
Although the basictype Time = Double type ReactiveB = Reactive :. Fun Time