Sophie

Sophie

distrib > Mageia > 5 > x86_64 > media > core-release > by-pkgid > 55ca3e426f1ba810750df60d13ae969f > files > 24

ocaml-react-devel-1.2.0-3.mga5.x86_64.rpm

<!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="Update steps" rel="Subsection" href="#steps">
<link title="Simultaneous events" rel="Subsection" href="#simultaneity">
<link title="The update step and thread safety" rel="Subsection" href="#update">
<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="Strong stops" rel="Subsection" href="#strongstop">
<link title="Clock" rel="Subsection" href="#clock">
<title>React</title>
</head>
<body>
<div class="navbar">&nbsp;<a class="up" href="index.html" title="Index">Up</a>
&nbsp;</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><div class="info module top">
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="React.html#sem">semantics</a>, the <a href="React.html#basics">basics</a> and
    <a href="React.html#ex">examples</a>. Open the module to use it, this defines only two
    types and modules in your scope.
<p>

    <em>Release 1.2.0 - Daniel Bünzli &lt;daniel.buenzl i@erratique.ch&gt; </em><br>
</div>
<hr width="100%">
<br>
<h1 id="1_Interface">Interface</h1><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 id="TYPEstep"><span class="keyword">type</span> <code class="type"></code>step</span> </pre>
<div class="info ">
The type for update steps.<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>

<pre><span class="keyword">module</span> <a href="React.Step.html">Step</a>: <code class="code"><span class="keyword">sig</span></code> <a href="React.Step.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info">
Update steps.
</div>
<br>
<h1 id="sem">Semantics</h1>
<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.
    <h2 id="evsem">Events</h2>
<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">-&gt;</span> time <span class="keywordsign">-&gt;</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">&lt;t</sub> <code class="code">: <span class="keywordsign">'</span>a event <span class="keywordsign">-&gt;</span> <span class="keywordsign">'</span>a option</code>
    (resp. []<sub class="subscript">&lt;=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">&lt;t</sub> <code class="code">=</code> [<code class="code">e</code>]<sub class="subscript">t'</sub> with t' the greatest t' &lt; t
      (resp. <code class="code">&lt;=</code>) such that
       [<code class="code">e</code>]<sub class="subscript">t'</sub> <code class="code">&lt;&gt; <span class="constructor">None</span></code>.</li>
<li>[<code class="code">e</code>]<sub class="subscript">&lt;t</sub> <code class="code">= <span class="constructor">None</span></code> if there is no such t'.</li>
</ul>

<p>

    <h2 id="sigsem">Signals</h2>
<p>

    A signal is a value that varies continuously over time. In
    contrast to <a href="React.html#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">-&gt;</span> time <span class="keywordsign">-&gt;</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.
    <h3 id="sigeq">Equality</h3>
<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="React.html#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.
<p>

    <h3 id="sigcont">Continuity</h3>
<p>

    Ultimately signal updates depend on
    <a href="React.html#primitives">primitives</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>

    <h1 id="basics">Basics</h1>
<p>

    <h2 id="primitives">Primitive events and signals</h2>
<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>.  <pre class="codepre"><code class="code"><span class="keyword">open</span>&nbsp;<span class="constructor">React</span>;;<br>
<br>
<span class="keyword">let</span>&nbsp;x,&nbsp;send_x&nbsp;=&nbsp;<span class="constructor">E</span>.create&nbsp;()<br>
<span class="keyword">let</span>&nbsp;pr_x&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;print_int&nbsp;x<br>
<span class="keyword">let</span>&nbsp;()&nbsp;=&nbsp;<span class="constructor">List</span>.iter&nbsp;send_x&nbsp;[1;&nbsp;2;&nbsp;3]</code></pre>
    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="React.html#sideeffects">side effects</a> for more details.
<p>

<pre class="codepre"><code class="code"><span class="keyword">open</span>&nbsp;<span class="constructor">React</span>;;<br>
<br>
<span class="keyword">let</span>&nbsp;x,&nbsp;set_x&nbsp;=&nbsp;<span class="constructor">S</span>.create&nbsp;1<br>
<span class="keyword">let</span>&nbsp;pr_x&nbsp;=&nbsp;<span class="constructor">S</span>.map&nbsp;print_int&nbsp;x<br>
<span class="keyword">let</span>&nbsp;()&nbsp;=&nbsp;<span class="constructor">List</span>.iter&nbsp;set_x&nbsp;[2;&nbsp;2;&nbsp;3]</code></pre>
    The <a href="React.html#clock">clock</a> example shows how a realtime time
    flow can be defined.
<p>

   <h2 id="steps">Update steps</h2>
<p>

   The <a href="React.E.html#VALcreate"><code class="code"><span class="constructor">React</span>.<span class="constructor">E</span>.create</code></a> and <a href="React.S.html#VALcreate"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span>.create</code></a> functions return update functions
   used to generate primitive event occurences and set the value of
   primitive signals. Upon invocation as in the preceding section
   these functions immediatly create and invoke an update step.
   The <em>update step</em> 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="React.html#sigeq">equality function</a>.
<p>

   The update functions have an optional <code class="code">step</code> argument. If they are
   given a concrete <code class="code">step</code> value created with <a href="React.Step.html#VALcreate"><code class="code"><span class="constructor">React</span>.<span class="constructor">Step</span>.create</code></a>, then it
   updates the event or signal but doesn't update its dependencies. It
   will only do so whenever <code class="code">step</code> is executed with
   <a href="React.Step.html#VALexecute"><code class="code"><span class="constructor">React</span>.<span class="constructor">Step</span>.execute</code></a>. This allows to make primitive event occurences and
   signal changes simultaneous. See next section for an example.
<p>

    <h2 id="simultaneity">Simultaneous events</h2>
<p>

    <a href="React.html#steps">Update steps</a> are made under a
    <a href="http://dx.doi.org/10.1016/0167-6423(92)90005-V">synchrony hypothesis</a> :
    the update step takes no time, it is instantenous. Two event occurrences
    are <em>simultaneous</em> if they occur in the same update step.
<p>

    In the code below <code class="code">w</code>, <code class="code">x</code> and <code class="code">y</code> will always have simultaneous
    occurrences. They <em>may</em> have simulatenous occurences with <code class="code">z</code>
    if <code class="code">send_w</code> and <code class="code">send_z</code> are used with the same update step.
<p>

<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;w,&nbsp;send_w&nbsp;=&nbsp;<span class="constructor">E</span>.create&nbsp;()<br>
<span class="keyword">let</span>&nbsp;x&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;succ&nbsp;w<br>
<span class="keyword">let</span>&nbsp;y&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;succ&nbsp;x<br>
<span class="keyword">let</span>&nbsp;z,&nbsp;send_z&nbsp;=&nbsp;<span class="constructor">E</span>.create&nbsp;()<br>
<br>
<span class="keyword">let</span>&nbsp;()&nbsp;=<br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;()&nbsp;=&nbsp;send_w&nbsp;3&nbsp;<span class="comment">(*&nbsp;w&nbsp;x&nbsp;y&nbsp;occur&nbsp;simultaneously,&nbsp;z&nbsp;doesn't&nbsp;occur&nbsp;*)</span>&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;step&nbsp;=&nbsp;<span class="constructor">Step</span>.create&nbsp;()&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;send_w&nbsp;~step&nbsp;3;<br>
&nbsp;&nbsp;send_z&nbsp;~step&nbsp;4;<br>
&nbsp;&nbsp;<span class="constructor">Step</span>.execute&nbsp;step&nbsp;<span class="comment">(*&nbsp;w&nbsp;x&nbsp;z&nbsp;y&nbsp;occur&nbsp;simultaneously&nbsp;*)</span><br>
</code></pre>
<p>

    <h2 id="update">The update step and thread safety</h2>
<p>

    <a href="React.html#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 without the
    <code class="code">step</code> argument or when it invokes <a href="React.Step.html#VALexecute"><code class="code"><span class="constructor">React</span>.<span class="constructor">Step</span>.execute</code></a> on a <code class="code">step</code>
    value, React performs an update step.
<p>

    To ensure correctness in the presence of threads, update steps
    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, concurrent, updates to <code class="code">x</code> and <code class="code">y</code> must be serialized (or
    performed on the same step if it makes sense semantically), but z
    can be updated concurently to both <code class="code">x</code> and <code class="code">y</code>.
<p>

<pre class="codepre"><code class="code"><span class="keyword">open</span>&nbsp;<span class="constructor">React</span>;;<br>
<br>
<span class="keyword">let</span>&nbsp;x,&nbsp;set_x&nbsp;=&nbsp;<span class="constructor">S</span>.create&nbsp;0<br>
<span class="keyword">let</span>&nbsp;y,&nbsp;send_y&nbsp;=&nbsp;<span class="constructor">E</span>.create&nbsp;()<br>
<span class="keyword">let</span>&nbsp;z,&nbsp;set_z&nbsp;=&nbsp;<span class="constructor">S</span>.create&nbsp;0<br>
<span class="keyword">let</span>&nbsp;max_xy&nbsp;=&nbsp;<span class="constructor">S</span>.l2&nbsp;(<span class="keyword">fun</span>&nbsp;x&nbsp;y&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;<span class="keyword">if</span>&nbsp;x&nbsp;&gt;&nbsp;y&nbsp;<span class="keyword">then</span>&nbsp;x&nbsp;<span class="keyword">else</span>&nbsp;y)&nbsp;x&nbsp;(<span class="constructor">S</span>.hold&nbsp;0&nbsp;y)<br>
<span class="keyword">let</span>&nbsp;succ_z&nbsp;=&nbsp;<span class="constructor">S</span>.map&nbsp;succ&nbsp;z</code></pre>
<p>

    <h2 id="sideeffects">Side effects</h2>
<p>

    Effectful events and signals perform their side effect
    exactly <em>once</em> in each <a href="React.html#steps">update step</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 step iff its
    <a href="React.html#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>.
<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;x,&nbsp;set_x&nbsp;=&nbsp;<span class="constructor">S</span>.create&nbsp;1<br>
<span class="keyword">let</span>&nbsp;()&nbsp;=&nbsp;ignore&nbsp;(<span class="constructor">S</span>.map&nbsp;print_int&nbsp;x)<br>
<span class="keyword">let</span>&nbsp;()&nbsp;=&nbsp;<span class="constructor">Gc</span>.full_major&nbsp;();&nbsp;<span class="constructor">List</span>.iter&nbsp;set_x&nbsp;[2;&nbsp;2;&nbsp;3]</code></pre>
    <h2 id="lifting">Lifting</h2>
<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 :
<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;f&nbsp;x&nbsp;y&nbsp;=&nbsp;x&nbsp;<span class="keyword">mod</span>&nbsp;y<br>
<span class="keyword">let</span>&nbsp;fl&nbsp;x&nbsp;y&nbsp;=&nbsp;<span class="constructor">S</span>.app&nbsp;(<span class="constructor">S</span>.app&nbsp;~eq:(==)&nbsp;(<span class="constructor">S</span>.const&nbsp;f)&nbsp;x)&nbsp;y&nbsp;<span class="comment">(*&nbsp;inefficient&nbsp;*)</span><br>
<span class="keyword">let</span>&nbsp;fl'&nbsp;x&nbsp;y&nbsp;=&nbsp;<span class="constructor">S</span>.l2&nbsp;f&nbsp;x&nbsp;y&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">(*&nbsp;efficient&nbsp;*)</span><br>
</code></pre>
    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>.
<pre class="codepre"><code class="code"><span class="keyword">open</span>&nbsp;<span class="constructor">React</span><br>
<span class="keyword">open</span>&nbsp;<span class="constructor">React</span>.<span class="constructor">S</span>.<span class="constructor">Float</span><br>
<br>
<span class="keyword">let</span>&nbsp;f&nbsp;t&nbsp;=&nbsp;sqrt&nbsp;t&nbsp;*.&nbsp;sin&nbsp;t&nbsp;<span class="comment">(*&nbsp;f&nbsp;is&nbsp;defined&nbsp;on&nbsp;float&nbsp;signals&nbsp;*)</span><br>
...<br>
<span class="keyword">open</span>&nbsp;<span class="constructor">Pervasives</span>&nbsp;<span class="comment">(*&nbsp;back&nbsp;to&nbsp;pervasives&nbsp;floats&nbsp;*)</span><br>
</code></pre>
   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
<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;<span class="keyword">open</span>&nbsp;<span class="constructor">React</span>.<span class="constructor">S</span>.<span class="constructor">Float</span>&nbsp;<span class="keyword">in</span><br>
<span class="keyword">let</span>&nbsp;f&nbsp;t&nbsp;=&nbsp;sqrt&nbsp;t&nbsp;*.&nbsp;sin&nbsp;t&nbsp;<span class="keyword">in</span>&nbsp;<span class="comment">(*&nbsp;f&nbsp;is&nbsp;defined&nbsp;on&nbsp;float&nbsp;signals&nbsp;*)</span><br>
...<br>
</code></pre>
<p>

  <h2 id="recursion">Mutual and self reference</h2>
<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.
<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;history&nbsp;?(eq&nbsp;=&nbsp;(&nbsp;=&nbsp;))&nbsp;s&nbsp;=<br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;push&nbsp;v&nbsp;=&nbsp;<span class="keyword">function</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keywordsign">|</span>&nbsp;[]&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;[&nbsp;v&nbsp;]<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keywordsign">|</span>&nbsp;v'&nbsp;::&nbsp;_&nbsp;<span class="keyword">as</span>&nbsp;l&nbsp;<span class="keyword">when</span>&nbsp;eq&nbsp;v&nbsp;v'&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;l<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keywordsign">|</span>&nbsp;l&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;v&nbsp;::&nbsp;l<br>
&nbsp;&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;define&nbsp;h&nbsp;=<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;h'&nbsp;=&nbsp;<span class="constructor">S</span>.l2&nbsp;push&nbsp;s&nbsp;h&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;h',&nbsp;h'<br>
&nbsp;&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="constructor">S</span>.fix&nbsp;[]&nbsp;define</code></pre>
  When a program has infinitesimally delayed values a
  <a href="React.html#primitives">primitive</a> may trigger more than one update
  step. For example if a signal <code class="code">s</code> is infinitesimally delayed, then
  its update in a step <code class="code">c</code> will trigger a new step <code class="code">c'</code> at the end
  of the step 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 steps, like in the following examples.
<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;start,&nbsp;send_start&nbsp;=&nbsp;<span class="constructor">E</span>.create&nbsp;()<br>
<span class="keyword">let</span>&nbsp;diverge&nbsp;=<br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;define&nbsp;e&nbsp;=<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;e'&nbsp;=&nbsp;<span class="constructor">E</span>.select&nbsp;[e;&nbsp;start]&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;e',&nbsp;e'<br>
&nbsp;&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="constructor">E</span>.fix&nbsp;define<br>
<br>
<span class="keyword">let</span>&nbsp;()&nbsp;=&nbsp;send_start&nbsp;()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">(*&nbsp;diverges&nbsp;*)</span><br>
<br>
<span class="keyword">let</span>&nbsp;diverge&nbsp;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">(*&nbsp;diverges&nbsp;*)</span><br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;define&nbsp;s&nbsp;=<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;s'&nbsp;=&nbsp;<span class="constructor">S</span>.<span class="constructor">Int</span>.succ&nbsp;s&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;s',&nbsp;s'<br>
&nbsp;&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="constructor">S</span>.fix&nbsp;0&nbsp;define</code></pre>
  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>

  <h2 id="strongstop">Strong stops</h2>
<p>

  Strong stops should only be used on platforms where weak arrays have
  a strong semantics (i.e. JavaScript). You can safely ignore that
  section and the <code class="code">strong</code> argument of <a href="React.E.html#VALstop"><code class="code"><span class="constructor">React</span>.<span class="constructor">E</span>.stop</code></a> and <a href="React.S.html#VALstop"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span>.stop</code></a>
  if that's not the case.
<p>

  Whenever <a href="React.E.html#VALstop"><code class="code"><span class="constructor">React</span>.<span class="constructor">E</span>.stop</code></a> and <a href="React.S.html#VALstop"><code class="code"><span class="constructor">React</span>.<span class="constructor">S</span>.stop</code></a> is called with <code class="code">~strong:<span class="keyword">true</span></code> on a
  reactive value <code class="code">v</code>, it is first stopped and then it walks over the
  list <code class="code">prods</code> of events and signals that it depends on and
  unregisters itself from these ones as a dependent (something that is
  normally automatically done when <code class="code">v</code> is garbage collected since
  dependents are stored in a weak array). Then for each element of
  <code class="code">prod</code> that has no dependents anymore and is not a primitive it
  stops them aswell and recursively.
<p>

  A stop call with <code class="code">~strong:<span class="keyword">true</span></code> is more involved. But it allows to
  prevent memory leaks when used judiciously on the leaves of the
  reactive system that are no longer used.
<p>

  <b>Warning.</b> It should be noted that if direct references are kept
  on an intermediate event or signal of the reactive system it may
  suddenly stop updating if all its dependents were strongly stopped. In
  the example below, <code class="code">e1</code> will <em>never</em> occur:
<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;e,&nbsp;e_send&nbsp;=&nbsp;<span class="constructor">E</span>.create&nbsp;()<br>
<span class="keyword">let</span>&nbsp;e1&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;(<span class="keyword">fun</span>&nbsp;x&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;x&nbsp;+&nbsp;1)&nbsp;e&nbsp;<span class="comment">(*&nbsp;never&nbsp;occurs&nbsp;*)</span><br>
<span class="keyword">let</span>&nbsp;()&nbsp;=<br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;e2&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;(<span class="keyword">fun</span>&nbsp;x&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;x&nbsp;+&nbsp;1)&nbsp;e1&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="constructor">E</span>.stop&nbsp;~strong:<span class="keyword">true</span>&nbsp;e2<br>
</code></pre>
  This can be side stepped by making an artificial dependency to keep
  the reference:
<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;e,&nbsp;e_send&nbsp;=&nbsp;<span class="constructor">E</span>.create&nbsp;()<br>
<span class="keyword">let</span>&nbsp;e1&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;(<span class="keyword">fun</span>&nbsp;x&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;x&nbsp;+&nbsp;1)&nbsp;e&nbsp;<span class="comment">(*&nbsp;may&nbsp;still&nbsp;occur&nbsp;*)</span><br>
<span class="keyword">let</span>&nbsp;e1_ref&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;(<span class="keyword">fun</span>&nbsp;x&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;x)&nbsp;e1<br>
<span class="keyword">let</span>&nbsp;()&nbsp;=<br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;e2&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;(<span class="keyword">fun</span>&nbsp;x&nbsp;<span class="keywordsign">-&gt;</span>&nbsp;x&nbsp;+&nbsp;1)&nbsp;e1&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="constructor">E</span>.stop&nbsp;~strong:<span class="keyword">true</span>&nbsp;e2<br>
</code></pre>
<p>

  <h1 id="ex">Examples</h1>
<p>

  <h2 id="clock">Clock</h2>
<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.
<pre class="codepre"><code class="code"><span class="keyword">let</span>&nbsp;pr_time&nbsp;t&nbsp;=<br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;tm&nbsp;=&nbsp;<span class="constructor">Unix</span>.localtime&nbsp;t&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="constructor">Printf</span>.printf&nbsp;<span class="string">"\x1B[8D%02d:%02d:%02d%!"</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;tm.<span class="constructor">Unix</span>.tm_hour&nbsp;tm.<span class="constructor">Unix</span>.tm_min&nbsp;tm.<span class="constructor">Unix</span>.tm_sec<br>
<br>
<span class="keyword">open</span>&nbsp;<span class="constructor">React</span>;;<br>
<br>
<span class="keyword">let</span>&nbsp;seconds,&nbsp;run&nbsp;=<br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;e,&nbsp;send&nbsp;=&nbsp;<span class="constructor">E</span>.create&nbsp;()&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;<span class="keyword">let</span>&nbsp;run&nbsp;()&nbsp;=<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">while</span>&nbsp;<span class="keyword">true</span>&nbsp;<span class="keyword">do</span>&nbsp;send&nbsp;(<span class="constructor">Unix</span>.gettimeofday&nbsp;());&nbsp;<span class="constructor">Unix</span>.sleep&nbsp;1&nbsp;<span class="keyword">done</span><br>
&nbsp;&nbsp;<span class="keyword">in</span><br>
&nbsp;&nbsp;e,&nbsp;run<br>
<br>
<span class="keyword">let</span>&nbsp;printer&nbsp;=&nbsp;<span class="constructor">E</span>.map&nbsp;pr_time&nbsp;seconds<br>
<br>
<span class="keyword">let</span>&nbsp;()&nbsp;=&nbsp;run&nbsp;()</code></pre><br>
</body></html>