Urban Hafner : Tag dataflow, everything about dataflow http://bettong.net/tag/dataflow.rss en-us 40 CTM:Exercise 4-8 <h3>The problem</h3> <p><em>Dataflow behaviour in a concurrent setting.</em> Consider the function <code>{Filter In F}</code>, which returns the elements of <code>In</code> for which the boolean function <code>F</code> returns <strong>true</strong>. Here is a possible definition of <code>Filter</code>:</p> <div class="typocode"><pre><code class="typocode_default ">fun {Filter In F} case In of X|In2 then if {F X} then X|{Filter In2 F} else {Filter In2 F} end else nil end end</code></pre></div> <p>Executing the following:</p> <div class="typocode"><pre><code class="typocode_default ">{Show {Filter [5 1 2 4 0] fun {$ X} X&gt;2 end}}</code></pre></div> <p>displays</p> <div class="typocode"><pre><code class="typocode_default ">[5 4]</code></pre></div> <p>(We use the procedure <code>Show</code>, which displays the instantaneous value of its argument in Mozart&#8217;s emulator window. Unlike <code>Browse</code>, this output is not updated if the argument is subsequently bound.) So <code>Filter</code> works as expected in the case of a sequential execution when all the input values are available. Let us now explore the dataflow behaviour of <code>Filter</code>.</p> <p><b>a)</b> What happens when we execute the following?: <div class="typocode"><pre><code class="typocode_default ">declare A {Show {Filter [5 1 A 4 0] fun {$ X} X&gt;2 end}}</code></pre></div> One of the list elements is a variable <code>A</code> that is not yet bound to a value. Remember that the <strong>case</strong> and <strong>if</strong> statements will suspend the thread in which they execute, until they can decide which alternative path to take.</p> <p><b>b)</b> What happens when we execute the following?: <div class="typocode"><pre><code class="typocode_default ">declare Out A thread Out={Filter [5 1 A 4 0] fun {$ X} X&gt;2 end} end {Show Out}</code></pre></div> Remember calling <code>Show</code> displays its argument as it exists at the instant of the call. Several possible results can be displayed. Which and why? Is the <code>Filter</code> function deterministic? Why or why not?</p> <p><b>c)</b> What happens when we execute the following?: <div class="typocode"><pre><code class="typocode_default ">declare Out A thread Out={Filter [5 1 A 4 0] fun {$ X} X&gt;2 end} end {Delay 1000} {Show Out}</code></pre></div> Remember that the call <code>{Delay N}</code> suspends its thread for at least <code>N</code> ms. During this time, other ready threads can be executed.</p> <p><b>d)</b> What happens when we execute the following?: <div class="typocode"><pre><code class="typocode_default ">declare Out A thread Out={Filter [5 1 A 4 0] fun {$ X} X&gt;2 end} end thread A=6 end {Delay 1000} {Show Out}</code></pre></div> What is displayed and why?</p> <h3>My solution</h3> <p><b>a)</b> Nothing is displayed because <code>Filter</code> is suspended waiting on <code>A</code> to become bound.</p> <p><b>b)</b> What gets displayed on my box is <code>Out&lt;optimized&gt;</code>. But when I use the Browser I see <code>5|_</code>. Another possible value would <code>_</code> if <code>Show</code> is executed before <code>Filter</code>. But even though there can be different values shown doesn&#8217;t mean that <code>Filter</code> itself is not deterministic. What&#8217;s not deterministic is the way threads are executed and <code>Show</code> is probably not part of the declarative model.</p> <p><b>c)</b> This displays <code>5|_&lt;optimized&gt;</code>. So it seems that the calls to <code>Show</code> in <b>b)</b> that I got were all before the call to <code>Filter</code>.</p> <p><b>d)</b> This of course displays <code>[5 6 4]</code>. <code>Filter</code> can finish because <code>A</code> gets bound, and we are waiting long enough before calling <code>Show</code>.</p> Mon, 07 Nov 2005 14:31:03 +0000 urn:uuid:b64b7534-72ba-4d17-9c31-1724a6ec1278 urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/07/ctm-exercise-4-8#comments CTM exercise book mozart oz dataflow concurrency http://bettong.net/2005/11/07/ctm-exercise-4-8 CTM: Exercise 4-5 <h3>The problem</h3> <p><em>The <code>Wait</code> operation.</em> Explain why the <code>{Wait X}</code> operation could be defined as:</p> <div class="typocode"><pre><code class="typocode_default ">proc {Wait X} if X==unit then skip else skip end end</code></pre></div> <p>Use your understanding of the dataflow behavior of the <strong>if</strong> statement and <code>==</code> operation.</p> <h3>My solution</h3> <p>The idea of <code>Wait</code> is to wait until its argument become bound. The <code>==</code> (i.e. the entailment check) is used to make the procedure wait. The <strong>if</strong> statement is needed, because <code>==</code> returns a value but <code>Wait</code> doesn&#8217;t. To get rid of this value you simply wrap the call to <code>==</code> in an <strong>if</strong> statement and let the <strong>if</strong> return nothing.</p> Wed, 02 Nov 2005 16:13:41 +0000 urn:uuid:16ae32ca-247e-48cb-af53-3053f532f9e5 urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/02/ctm-exercise-4-5#comments CTM exercise book mozart oz dataflow wait http://bettong.net/2005/11/02/ctm-exercise-4-5 CTM: Exercise 4-4 <h3>The problem</h3> <p><em>Order-determining concurrency.</em> Explain what happens when executing the following:</p> <div class="typocode"><pre><code class="typocode_default ">declare A B C D in thread B=C+1 end thread C=B+1 end thread A=1 end thread B=A+1 end {Browse D}</code></pre></div> <p>In what order are the threads created? In what order are the additions done? What is the final result? Compare with the following:</p> <div class="typocode"><pre><code class="typocode_default ">declare A B C D in A=1 B=A+1 C=B+1 D=C+1 {Browse D}</code></pre></div> <p>Here there is only one thread. In what order are the additions done? What is the final result? What do you conclude?</p> <h3>My solution</h3> <p>Of course both code snippets produce the same result, namely 4. And of course the threads in the first code example are executed in the same way as in the second. That&#8217;s because the threads have to wait for the variables they are using in the additions to become bound.</p> <p>An what do I conclude? Well, that this kind of concurrency is declarative. At least that&#8217;s what I think I was supposed to answer :)</p> Wed, 02 Nov 2005 16:13:30 +0000 urn:uuid:705cd1fa-27d4-457b-8058-aa6680c62e3a urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/02/ctm-exercise-4-4#comments CTM exercise book mozart oz concurrency dataflow variable http://bettong.net/2005/11/02/ctm-exercise-4-4 CTM: Exercise 3-9 <h3>The problem</h3> <p><em>Iterative computations and dataflow variables.</em> The previous exercise shows that using dataflow variables sometimes makes it simpler to write iterative list operations. This leads to the following question. For any iterative operation defined with dataflow variables, is it possible to give another iterative definition of the same operation that does not use dataflow variables?</p> <h3>My solution</h3> <p>I&#8217;m really unsure about this one. If anyone can explain it in a nice and convincing way I&#8217;d happily replace my hand waving with it.</p> <p>If we assume that <em>iterative computation</em> means the function <code>Iterate</code> from section 3.2.4 then it seems possible to replace any use of dataflow variables. Just translate your function into one similar to <code>Iterate</code> and you have removed any dataflow variables.</p> Thu, 15 Sep 2005 18:55:46 +0000 urn:uuid:0d4d87b6f009633bc5e39ef704cd8a33 urban@bettong.net (Urban Hafner) http://bettong.net/2005/09/15/ctm-exercise-3-9#comments book CTM mozart oz exercise iterative dataflow http://bettong.net/2005/09/15/ctm-exercise-3-9