/usr/share/doc/libreact-ocaml-dev/html/React.html is in libreact-ocaml-dev 0.9.4-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<link rel="Start" href="index.html">
<link rel="Up" href="index.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Index of module types" rel=Appendix href="index_module_types.html">
<link title="React" rel="Chapter" href="React.html"><link title="Interface" rel="Section" href="#1_Interface">
<link title="Semantics" rel="Section" href="#sem">
<link title="Basics" rel="Section" href="#basics">
<link title="Examples" rel="Section" href="#ex">
<link title="Events" rel="Subsection" href="#evsem">
<link title="Signals" rel="Subsection" href="#sigsem">
<link title="Primitive events and signals" rel="Subsection" href="#primitives">
<link title="The update cycle and thread safety" rel="Subsection" href="#update">
<link title="Simultaneous events" rel="Subsection" href="#simultaneity">
<link title="Side effects" rel="Subsection" href="#sideeffects">
<link title="Lifting" rel="Subsection" href="#lifting">
<link title="Mutual and self reference" rel="Subsection" href="#recursion">
<link title="Clock" rel="Subsection" href="#clock">
<title>React</title>
</head>
<body>
<div class="navbar"> <a class="up" href="index.html" title="Index">Up</a>
</div>
<h1>Module <a href="type_React.html">React</a></h1>
<pre><span class="keyword">module</span> React: <code class="code"><span class="keyword">sig</span></code> <a href="React.html">..</a> <code class="code"><span class="keyword">end</span></code></pre>Declarative events and signals.
<p>
React is a module for functional reactive programming (frp). It
provides support to program with time varying values : declarative
<a href="React.E.html">events</a> and <a href="React.S.html">signals</a>. React
doesn't define any primitive event or signal, this lets the client
choose the concrete timeline.
<p>
Consult the <a href="#sem">semantics</a>, the <a href="#basics">basics</a> and
<a href="#ex">examples</a>. Open the module to use it, this defines only two
types and modules in your scope.
<p>
<em>Release 0.9.4 - Daniel Bünzli <daniel.buenzli at erratique.ch> </em><br>
<hr width="100%">
<br>
<span id="1_Interface"><h1>Interface</h1></span><br>
<pre><span id="TYPEevent"><span class="keyword">type</span> <code class="type">'a</code> event</span> </pre>
<div class="info">
The type for events of type <code class="code"><span class="keywordsign">'</span>a</code>.<br>
</div>
<pre><span id="TYPEsignal"><span class="keyword">type</span> <code class="type">'a</code> signal</span> </pre>
<div class="info">
The type for signals of type <code class="code"><span class="keywordsign">'</span>a</code>.<br>
</div>
<pre><span class="keyword">module</span> <a href="React.E.html">E</a>: <code class="code"><span class="keyword">sig</span></code> <a href="React.E.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info">
Event combinators.
</div>
<pre><span class="keyword">module</span> <a href="React.S.html">S</a>: <code class="code"><span class="keyword">sig</span></code> <a href="React.S.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info">
Signal combinators.
</div>
<br>
<span id="sem"><h1>Semantics</h1></span>
<p>
The following notations are used to give precise meaning to the
combinators. It is important to note that in these semantic
descriptions the origin of time t = 0 is <em>always</em> fixed at
the time at which the combinator creates the event or the signal and
the semantics of the dependents is evaluated relative to this timeline.
<p>
We use dt to denote an infinitesimal amount of time.
<span id="evsem"><h2>Events</h2></span>
<p>
An event is a value with discrete occurrences over time.
<p>
The semantic function [] <code class="code">: <span class="keywordsign">'</span>a event <span class="keywordsign">-></span> time <span class="keywordsign">-></span> <span class="keywordsign">'</span>a option</code> gives
meaning to an event <code class="code">e</code> by mapping it to a function of time
[<code class="code">e</code>] returning <code class="code"><span class="constructor">Some</span> v</code> whenever the event occurs with value
<code class="code">v</code> and <code class="code"><span class="constructor">None</span></code> otherwise. We write [<code class="code">e</code>]<sub class="subscript">t</sub> the evaluation of
this <em>semantic</em> function at time t.
<p>
As a shortcut notation we also define []<sub class="subscript"><t</sub> <code class="code">: <span class="keywordsign">'</span>a event <span class="keywordsign">-></span> <span class="keywordsign">'</span>a option</code>
(resp. []<sub class="subscript"><=t</sub>) to denote the last occurrence, if any, of an
event before (resp. before or at) <code class="code">t</code>. More precisely :
<ul>
<li>[<code class="code">e</code>]<sub class="subscript"><t</sub> <code class="code">=</code> [<code class="code">e</code>]<sub class="subscript">t'</sub> with t' the greatest t' < t
(resp. <code class="code"><=</code>) such that
[<code class="code">e</code>]<sub class="subscript">t'</sub> <code class="code"><> <span class="constructor">None</span></code>.</li>
<li>[<code class="code">e</code>]<sub class="subscript"><t</sub> <code class="code">= <span class="constructor">None</span></code> if there is no such t'.</li>
</ul>
<p>
<span id="sigsem"><h2>Signals</h2></span>
<p>
A signal is a value that varies continuously over time. In
contrast to <a href="#evsem">events</a> which occur at specific point
in time, a signal has a value at every point in time.
<p>
The semantic function [] <code class="code">: <span class="keywordsign">'</span>a signal <span class="keywordsign">-></span> time <span class="keywordsign">-></span> <span class="keywordsign">'</span>a</code> gives
meaning to a signal <code class="code">s</code> by mapping it to a function of time
[<code class="code">s</code>] that returns its value at a given time. We write [<code class="code">s</code>]<sub class="subscript">t</sub>
the evaluation of this <em>semantic</em> function at time t.
<span id="sigeq"><h3>Equality</h3></span>
<p>
Most signal combinators have an optional <code class="code">eq</code> parameter that
defaults to structural equality. <code class="code">eq</code> specifies the equality
function used to detect changes in the value of the resulting
signal. This function is needed for the efficient update of
signals and to deal correctly with signals that perform
<a href="#sideeffects">side effects</a>.
<p>
Given an equality function on a type the combinators can be automatically
<a href="React.S.html#special">specialized</a> via a functor.
<span id="sigcont"><h3>Continuity</h3></span>
<p>
Ultimately signal updates depend on
<a href="#primitives">primitive</a> updates. Thus a signal can
only approximate a real continuous signal. The accuracy of the
approximation depends on the variation rate of the real signal and
the primitive's update frequency.
<p>
<span id="basics"><h1>Basics</h1></span>
<p>
<span id="primitives"><h2>Primitive events and signals</h2></span>
<p>
React doesn't define primitive events and signals, they must be
created and updated by the client.
<p>
Primitive events are created with <a href="React.E.html#VALcreate"><code class="code"><span class="constructor">React</span>.<span class="constructor">E</span>.create</code></a>. This function
returns a new event and an update function that generates an
occurrence for the event at the time it is called. The following
code creates a primitive integer event <code class="code">x</code> and generates three
occurrences with value <code class="code">1</code>, <code class="code">2</code>, <code class="code">3</code>. Those occurrences are printed
on stdout by the effectful event <code class="code">pr_x</code>. <code class="code"><span class="keyword">open</span> <span class="constructor">React</span>;;<br>
<br>
<span class="keyword">let</span> x, send_x = <span class="constructor">E</span>.create ()<br>
<span class="keyword">let</span> pr_x = <span class="constructor">E</span>.map print_int x<br>
<span class="keyword">let</span> () = <span class="constructor">List</span>.iter send_x [1; 2; 3]</code>
Primitive signals are created with <a href="React.S.html#VALcreate"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span>.create</code></a>. This function
returns a new signal and an update function that sets the signal's value
at the time it is called. The following code creates an
integer signal <code class="code">x</code> initially set to <code class="code">1</code> and updates it three time with
values <code class="code">2</code>, <code class="code">2</code>, <code class="code">3</code>. The signal's values are printed on stdout by the
effectful signal <code class="code">pr_x</code>. Note that only updates that change
the signal's value are printed, hence the program prints <code class="code">123</code>, not <code class="code">1223</code>.
See the discussion on
<a href="#sideeffects">side effects</a> for more details.
<code class="code"><span class="keyword">open</span> <span class="constructor">React</span>;;<br>
<br>
<span class="keyword">let</span> x, set_x = <span class="constructor">S</span>.create 1<br>
<span class="keyword">let</span> pr_x = <span class="constructor">S</span>.map print_int x<br>
<span class="keyword">let</span> () = <span class="constructor">List</span>.iter set_x [2; 2; 3]</code>
The <a href="#clock">clock</a> example shows how a realtime time
flow can be defined.
<p>
<span id="update"><h2>The update cycle and thread safety</h2></span>
<p>
<a href="#primitives">Primitives</a> are the only mean to drive the reactive
system and they are entirely under the control of the client. When
the client invokes a primitive's update function, React performs
an update cycle. The update cycle automatically updates events and
signals that transitively depend on the updated primitive. The
dependents of a signal are updated iff the signal's value changed
according to its <a href="#sigeq">equality function</a>.
<p>
To ensure correctness in the presence of threads, update cycles
must be executed in a critical section. Let uset(<code class="code">p</code>) be the set
of events and signals that need to be updated whenever the
primitive <code class="code">p</code> is updated. Updating two primitives <code class="code">p</code> and <code class="code">p'</code>
concurrently is only allowed if uset(<code class="code">p</code>) and uset(<code class="code">p'</code>) are
disjoint. Otherwise the updates must be properly serialized.
<p>
Below updates to <code class="code">x</code> and <code class="code">y</code> must be serialized, but z can
be updated concurently to both <code class="code">x</code> and <code class="code">y</code>.
<code class="code"><span class="keyword">open</span> <span class="constructor">React</span>;;<br>
<br>
<span class="keyword">let</span> x, set_x = <span class="constructor">S</span>.create 0<br>
<span class="keyword">let</span> y, send_y = <span class="constructor">E</span>.create ()<br>
<span class="keyword">let</span> z, set_z = <span class="constructor">S</span>.create 0<br>
<span class="keyword">let</span> max_xy = <span class="constructor">S</span>.l2 (<span class="keyword">fun</span> x y <span class="keywordsign">-></span> <span class="keyword">if</span> x > y <span class="keyword">then</span> x <span class="keyword">else</span> y) x (<span class="constructor">S</span>.hold 0 y)<br>
<span class="keyword">let</span> succ_z = <span class="constructor">S</span>.map succ z</code>
<span id="simultaneity"><h2>Simultaneous events</h2></span>
<p>
<a href="#update">Update cycles</a> are made under a
<a href="http://dx.doi.org/10.1016/0167-6423(92)90005-V">synchrony hypothesis</a> :
the update cycle takes no time, it is instantenous.
<p>
Two event occurrences are <em>simultaneous</em> if they occur in the
same update cycle; in other words if there exists a primitive on
which they both depend. By definition a primitive doesn't depend
on any primitive it is therefore impossible for two primitive
events to occur simultaneously.
<p>
In the code below <code class="code">w</code>, <code class="code">x</code> and <code class="code">y</code> will have simultaneous occurrences while
<code class="code">z</code> will never have simultaneous occurrences with them.
<code class="code"><span class="keyword">let</span> w, send_w = <span class="constructor">E</span>.create ()<br>
<span class="keyword">let</span> x = <span class="constructor">E</span>.map succ w<br>
<span class="keyword">let</span> y = <span class="constructor">E</span>.map succ x<br>
<span class="keyword">let</span> z, send_z = <span class="constructor">E</span>.create ()</code>
<span id="sideeffects"><h2>Side effects</h2></span>
<p>
Effectful events and signals perform their side effect
exactly <em>once</em> in each <a href="#update">update cycle</a> in which there
is an update of at least one of the event or signal it depends on.
<p>
Remember that a signal updates in a cycle iff its
<a href="#sigeq">equality function</a> determined that the signal
value changed. Signal initialization is unconditionally considered as
an update.
<p>
It is important to keep references on effectful events and
signals. Otherwise they may be reclaimed by the garbage collector.
The following program prints only a <code class="code">1</code>.
<code class="code"><span class="keyword">let</span> x, set_x = <span class="constructor">S</span>.create 1<br>
<span class="keyword">let</span> () = ignore (<span class="constructor">S</span>.map print_int x)<br>
<span class="keyword">let</span> () = <span class="constructor">Gc</span>.full_major (); <span class="constructor">List</span>.iter set_x [2; 2; 3]</code>
<span id="lifting"><h2>Lifting</h2></span>
<p>
Lifting transforms a regular function to make it act on signals.
The combinators
<a href="React.S.html#VALconst"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span>.const</code></a> and <a href="React.S.html#VALapp"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span>.app</code></a> allow to lift functions of arbitrary arity n,
but this involves the inefficient creation of n-1 intermediary
closure signals. The fixed arity <a href="React.S.html#lifting">lifting
functions</a> are more efficient. For example :
<code class="code"><span class="keyword">let</span> f x y = x <span class="keyword">mod</span> y<br>
<span class="keyword">let</span> fl x y = <span class="constructor">S</span>.app (<span class="constructor">S</span>.app ~eq:(==) (<span class="constructor">S</span>.const f) x) y <span class="comment">(* inefficient *)</span><br>
<span class="keyword">let</span> fl' x y = <span class="constructor">S</span>.l2 f x y <span class="comment">(* efficient *)</span><br>
</code>
Besides, some of <code class="code"><span class="constructor">Pervasives</span></code>'s functions and operators are
already lifted and availables in submodules of <a href="React.S.html"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span></code></a>. They can be
be opened in specific scopes. For example if you are dealing with
float signals you can open <a href="React.S.Float.html"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span>.<span class="constructor">Float</span></code></a>.
<code class="code"><span class="keyword">open</span> <span class="constructor">React</span> <br>
<span class="keyword">open</span> <span class="constructor">React</span>.<span class="constructor">S</span>.<span class="constructor">Float</span> <br>
<br>
<span class="keyword">let</span> f t = sqrt t *. sin t <span class="comment">(* f is defined on float signals *)</span><br>
...<br>
<span class="keyword">open</span> <span class="constructor">Pervasives</span> <span class="comment">(* back to pervasives floats *)</span><br>
</code>
If you are using OCaml 3.12 or later you can also use the <code class="code"><span class="keyword">let</span> <span class="keyword">open</span></code>
construct
<code class="code"><span class="keyword">let</span> <span class="keyword">open</span> <span class="constructor">React</span>.<span class="constructor">S</span>.<span class="constructor">Float</span> <span class="keyword">in</span> <br>
<span class="keyword">let</span> f t = sqrt t *. sin t <span class="keyword">in</span> <span class="comment">(* f is defined on float signals *)</span><br>
...<br>
</code>
<p>
<span id="recursion"><h2>Mutual and self reference</h2></span>
<p>
Mutual and self reference among time varying values occurs naturally
in programs. However a mutually recursive definition of two signals
in which both need the value of the other at time t to define
their value at time t has no least fixed point. To break this
tight loop one signal must depend on the value the other had at time
t-dt where dt is an infinitesimal delay.
<p>
The fixed point combinators <a href="React.E.html#VALfix"><code class="code"><span class="constructor">React</span>.<span class="constructor">E</span>.fix</code></a> and <a href="React.S.html#VALfix"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span>.fix</code></a> allow to refer to
the value an event or signal had an infinitesimal amount of time
before. These fixed point combinators act on a function <code class="code">f</code> that takes
as argument the infinitesimally delayed event or signal that <code class="code">f</code>
itself returns.
<p>
In the example below <code class="code">history s</code> returns a signal whose value
is the history of <code class="code">s</code> as a list.
<code class="code"><span class="keyword">let</span> history ?(eq = ( = )) s = <br>
<span class="keyword">let</span> push v = <span class="keyword">function</span> <br>
<span class="keywordsign">|</span> [] <span class="keywordsign">-></span> [ v ] <br>
<span class="keywordsign">|</span> v' :: _ <span class="keyword">as</span> l <span class="keyword">when</span> eq v v' <span class="keywordsign">-></span> l<br>
<span class="keywordsign">|</span> l <span class="keywordsign">-></span> v :: l <br>
<span class="keyword">in</span><br>
<span class="keyword">let</span> define h = <br>
<span class="keyword">let</span> h' = <span class="constructor">S</span>.l2 push s h <span class="keyword">in</span> <br>
h', h'<br>
<span class="keyword">in</span><br>
<span class="constructor">S</span>.fix [] define</code>
When a program has infinitesimally delayed values a
<a href="#primitives">primitive</a> may trigger more than one update
cycle. For example if a signal <code class="code">s</code> is infinitesimally delayed, then
its update in a cycle <code class="code">c</code> will trigger a new cycle <code class="code">c'</code> at the end
of the cycle in which the delayed signal of <code class="code">s</code> will have the value
<code class="code">s</code> had in <code class="code">c</code>. This means that the recursion occuring between a
signal (or event) and its infinitesimally delayed counterpart must
be well-founded otherwise this may trigger an infinite number
of update cycles, like in the following examples.
<code class="code"><span class="keyword">let</span> start, send_start = <span class="constructor">E</span>.create ()<br>
<span class="keyword">let</span> diverge = <br>
<span class="keyword">let</span> define e = <br>
<span class="keyword">let</span> e' = <span class="constructor">E</span>.select [e; start] <span class="keyword">in</span> <br>
e', e'<br>
<span class="keyword">in</span><br>
<span class="constructor">E</span>.fix define<br>
<br>
<span class="keyword">let</span> () = send_start () <span class="comment">(* diverges *)</span><br>
<br>
<span class="keyword">let</span> diverge = <span class="comment">(* diverges *)</span><br>
<span class="keyword">let</span> define s = <br>
<span class="keyword">let</span> s' = <span class="constructor">S</span>.<span class="constructor">Int</span>.succ s <span class="keyword">in</span><br>
s', s'<br>
<span class="keyword">in</span><br>
<span class="constructor">S</span>.fix 0 define</code>
For technical reasons, delayed events and signals (those given to
fixing functions) are not allowed to directly depend on each
other. Fixed point combinators will raise <code class="code"><span class="constructor">Invalid_argument</span></code> if
such dependencies are created. This limitation can be
circumvented by mapping these values with the identity.
<p>
<span id="ex"><h1>Examples</h1></span>
<p>
<span id="clock"><h2>Clock</h2></span>
<p>
The following program defines a primitive event <code class="code">seconds</code> holding
the UNIX time and occuring on every second. An effectful event
converts these occurences to local time and prints them on stdout
along with an
<a href="http://www.ecma-international.org/publications/standards/Ecma-048.htm">ANSI
escape sequence</a> to control the cursor position.
<code class="code"><span class="keyword">let</span> pr_time t = <br>
<span class="keyword">let</span> tm = <span class="constructor">Unix</span>.localtime t <span class="keyword">in</span><br>
<span class="constructor">Printf</span>.printf <span class="string">"\x1B[8D%02d:%02d:%02d%!"</span> <br>
tm.<span class="constructor">Unix</span>.tm_hour tm.<span class="constructor">Unix</span>.tm_min tm.<span class="constructor">Unix</span>.tm_sec<br>
<br>
<span class="keyword">open</span> <span class="constructor">React</span>;;<br>
<br>
<span class="keyword">let</span> seconds, run = <br>
<span class="keyword">let</span> e, send = <span class="constructor">E</span>.create () <span class="keyword">in</span><br>
<span class="keyword">let</span> run () = <br>
<span class="keyword">while</span> <span class="keyword">true</span> <span class="keyword">do</span> send (<span class="constructor">Unix</span>.gettimeofday ()); <span class="constructor">Unix</span>.sleep 1 <span class="keyword">done</span><br>
<span class="keyword">in</span><br>
e, run<br>
<br>
<span class="keyword">let</span> printer = <span class="constructor">E</span>.map pr_time seconds<br>
<br>
<span class="keyword">let</span> () = run ()</code><br>
</body></html>
|