lutin-tuto-html.html 86.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>A Lutin Tutorial</title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<meta name="title" content="A Lutin Tutorial"/>
<meta name="generator" content="Org-mode"/>
erwan's avatar
erwan committed
10
<meta name="generated" content="2017-03-22 14:17:49 CET"/>
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
<meta name="author" content="Erwan Jahier"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<style type="text/css">
 <!--/*--><![CDATA[/*><!--*/
  html { font-family: Times, serif; font-size: 12pt; }
  .title  { text-align: center; }
  .todo   { color: red; }
  .done   { color: green; }
  .tag    { background-color: #add8e6; font-weight:normal }
  .target { }
  .timestamp { color: #bebebe; }
  .timestamp-kwd { color: #5f9ea0; }
  .right  {margin-left:auto; margin-right:0px;  text-align:right;}
  .left   {margin-left:0px;  margin-right:auto; text-align:left;}
  .center {margin-left:auto; margin-right:auto; text-align:center;}
  p.verse { margin-left: 3% }
  pre {
	border: 1pt solid #AEBDCC;
	background-color: #F3F5F7;
	padding: 5pt;
	font-family: courier, monospace;
        font-size: 90%;
        overflow:auto;
  }
  table { border-collapse: collapse; }
  td, th { vertical-align: top;  }
  th.right  { text-align:center;  }
  th.left   { text-align:center;   }
  th.center { text-align:center; }
  td.right  { text-align:right;  }
  td.left   { text-align:left;   }
  td.center { text-align:center; }
  dt { font-weight: bold; }
  div.figure { padding: 0.5em; }
  div.figure p { text-align: center; }
  div.inlinetask {
    padding:10px;
    border:2px solid gray;
    margin:10px;
    background: #ffffcc;
  }
  textarea { overflow-x: auto; }
  .linenr { font-size:smaller }
  .code-highlighted {background-color:#ffff00;}
  .org-info-js_info-navigation { border-style:none; }
  #org-info-js_console-label { font-size:10px; font-weight:bold;
                               white-space:nowrap; }
  .org-info-js_search-highlight {background-color:#ffff00; color:#000000;
                                 font-weight:bold; }
  /*]]>*/-->
</style>
<style type="text/css">pre{background-color: #232323; color: #E6E1DC;}</style>
erwan's avatar
erwan committed
64
65
<style type="text/css">blockquote{border: 1px solid #CCC; background-color: #E6E1DC; color: #232323;}</style>
<link rel="stylesheet" type="text/css" href="worg-zenburn.css" />
66
67
<script type="text/javascript" src="http://orgmode.org/org-info.js"></script>
<script type="text/javascript" >
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
<!--/*--><![CDATA[/*><!--*/
org_html_manager.set("TOC_DEPTH", "2");
org_html_manager.set("LINK_HOME", "http://www-verimag.imag.fr/Lutin.html");
org_html_manager.set("LINK_UP", "http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lurette/doc/lutin-tuto/lutin-tuto-html.html");
org_html_manager.set("LOCAL_TOC", "1");
org_html_manager.set("VIEW_BUTTONS", "1");
org_html_manager.set("MOUSE_HINT", "underline");
org_html_manager.set("FIXED_TOC", "0");
org_html_manager.set("TOC", "2");
org_html_manager.set("VIEW", "info");
org_html_manager.setup();  // activate after the parameters are set
/*]]>*///-->
</script>
<script type="text/javascript">
<!--/*--><![CDATA[/*><!--*/
 function CodeHighlightOn(elem, id)
 {
   var target = document.getElementById(id);
   if(null != target) {
     elem.cacheClassElem = elem.className;
     elem.cacheClassTarget = target.className;
     target.className = "code-highlighted";
     elem.className   = "code-highlighted";
   }
 }
 function CodeHighlightOff(elem, id)
 {
   var target = document.getElementById(id);
   if(elem.cacheClassElem)
     elem.className = elem.cacheClassElem;
   if(elem.cacheClassTarget)
     target.className = elem.cacheClassTarget;
 }
/*]]>*///-->
</script>
<script type="text/javascript" src="http://orgmode.org/mathjax/MathJax.js">
<!--/*--><![CDATA[/*><!--*/
    MathJax.Hub.Config({
        // Only one of the two following lines, depending on user settings
        // First allows browser-native MathML display, second forces HTML/CSS
        //  config: ["MMLorHTML.js"], jax: ["input/TeX"],
            jax: ["input/TeX", "output/HTML-CSS"],
        extensions: ["tex2jax.js","TeX/AMSmath.js","TeX/AMSsymbols.js",
                     "TeX/noUndefined.js"],
        tex2jax: {
            inlineMath: [ ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"], ["\\begin{displaymath}","\\end{displaymath}"] ],
            skipTags: ["script","noscript","style","textarea","pre","code"],
            ignoreClass: "tex2jax_ignore",
            processEscapes: false,
            processEnvironments: true,
            preview: "TeX"
        },
        showProcessingMessages: true,
        displayAlign: "center",
        displayIndent: "2em",

        "HTML-CSS": {
             scale: 100,
             availableFonts: ["STIX","TeX"],
             preferredFont: "TeX",
             webFont: "TeX",
             imageFont: "TeX",
             showMathMenu: true,
        },
        MMLorHTML: {
             prefer: {
                 MSIE:    "MML",
                 Firefox: "MML",
                 Opera:   "HTML",
                 other:   "HTML"
             }
        }
    });
/*]]>*///-->
</script>
</head>
<body>
<div id="org-div-home-and-up" style="text-align:right;font-size:70%;white-space:nowrap;">
 <a accesskey="h" href="http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lurette/doc/lutin-tuto/lutin-tuto-html.html"> UP </a>
 |
 <a accesskey="H" href="http://www-verimag.imag.fr/Lutin.html"> HOME </a>
</div>

<div id="preamble">

</div>

<div id="content">
<h1 class="title">A Lutin Tutorial</h1>
















erwan's avatar
erwan committed
174
175
176
177




178
179
180
181
182
183
184
185
186
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1 Forewords</a>
<ul>
<li><a href="#sec-1-1">1.1 Motivations: testing reactive programs</a></li>
<li><a href="#sec-1-2">1.2 Lutin in one slide</a></li>
<li><a href="#sec-1-3">1.3 In order to run this tutorial</a></li>
erwan's avatar
erwan committed
187
<li><a href="#sec-1-4">1.4 In order to run the demo from the pdf slides</a></li>
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
</ul>
</li>
<li><a href="#sec-2">2 Execute Lutin programs</a>
<ul>
<li><a href="#sec-2-1">2.1 Stimulate Lutin programs</a>
<ul>
<li><a href="#sec-2-1-1">2.1.1 A program that increments its input</a></li>
<li><a href="#sec-2-1-2">2.1.2 A program with no input</a></li>
<li><a href="#sec-2-1-3">2.1.3 Be quiet</a></li>
</ul>
</li>
<li><a href="#sec-2-2">2.2 Stimulate Lutin programs graphically with <code>luciole</code></a></li>
<li><a href="#sec-2-3">2.3 Store and Display the produced data: <code>sim2chro</code> and <code>gnuplot-rif</code></a>
<ul>
<li><a href="#sec-2-3-1">2.3.1 Generate a RIF file</a></li>
<li><a href="#sec-2-3-2">2.3.2 Visualize a  RIF file</a></li>
<li><a href="#sec-2-3-3">2.3.3 Visualize a  RIF file (bis)</a></li>
<li><a href="#sec-2-3-4">2.3.4 Visualize a  RIF file (ter)</a></li>
</ul>
</li>
<li><a href="#sec-2-4">2.4 Automatic stimulation of Lutin programs</a></li>
</ul>
</li>
erwan's avatar
erwan committed
211
<li><a href="#sec-3">3 The Lutin Language</a>
212
213
214
<ul>
<li><a href="#sec-3-1">3.1 Back to programs of Section 1</a>
<ul>
erwan's avatar
erwan committed
215
<li><a href="#sec-3-1-1">3.1.1 We said said that  the  first  one,  incr.lut  increments    its input by one. Let's explain why.</a></li>
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
</ul>
</li>
<li><a href="#sec-3-2">3.2 Non deterministic programs</a>
<ul>
<li><a href="#sec-3-2-1">3.2.1 A First non-deterministic program</a></li>
<li><a href="#sec-3-2-2">3.2.2 It is possible to set the variable range at declaration time,</a></li>
</ul>
</li>
<li><a href="#sec-3-3">3.3 Non deterministic programs (cont)</a></li>
<li><a href="#sec-3-4">3.4 Non deterministic programs (cont)</a></li>
<li><a href="#sec-3-5">3.5 Non deterministic programs (cont)</a></li>
<li><a href="#sec-3-6">3.6 Controlled non-determinism: the choice operator</a></li>
<li><a href="#sec-3-7">3.7 Controlled non-determinism: the choice operator</a></li>
<li><a href="#sec-3-8">3.8 Combinators</a>
<ul>
<li><a href="#sec-3-8-1">3.8.1 A simple combinator</a></li>
</ul>
</li>
<li><a href="#sec-3-9">3.9 A parametric combinator</a></li>
<li><a href="#sec-3-10">3.10 Combinators  (cont)</a>
<ul>
<li><a href="#sec-3-10-1">3.10.1 A combinator that needs memory   (<code>ref</code>)</a></li>
</ul>
</li>
<li><a href="#sec-3-11">3.11 Local variables</a></li>
<li><a href="#sec-3-12">3.12 Local variables again</a></li>
<li><a href="#sec-3-13">3.13 Distribute a constraint into a scope: <code>assert</code></a></li>
<li><a href="#sec-3-14">3.14 External code</a></li>
<li><a href="#sec-3-15">3.15 Exceptions</a>
<ul>
<li><a href="#sec-3-15-1">3.15.1 Global exceptions can be declared outside the main node:</a></li>
<li><a href="#sec-3-15-2">3.15.2 or locally within a trace statement:</a></li>
<li><a href="#sec-3-15-3">3.15.3 An existing  exception ident can  be raised with the  statement:</a></li>
<li><a href="#sec-3-15-4">3.15.4 An exception can be caught with the statement:</a></li>
</ul>
</li>
<li><a href="#sec-3-16">3.16 Exceptions (cont)</a></li>
<li><a href="#sec-3-17">3.17 Exceptions (cont)</a></li>
<li><a href="#sec-3-18">3.18 About exceptions</a></li>
<li><a href="#sec-3-19">3.19 Parallelism: <code>&amp;&gt;</code></a></li>
<li><a href="#sec-3-20">3.20 Combinators (again)</a>
<ul>
<li><a href="#sec-3-20-1">3.20.1 Trace Combinators</a></li>
</ul></li>
</ul>
</li>
262
<li><a href="#sec-4">4 The <code>run</code> operator</a>
263
264
265
<ul>
<li><a href="#sec-4-1">4.1 Cheap parallelism: Calling Lutin nodes <code>run/in</code></a></li>
<li><a href="#sec-4-2">4.2 Cheap parallelism: Calling Lutin nodes <code>run/in</code></a></li>
erwan's avatar
erwan committed
266
<li><a href="#sec-4-3">4.3 Why does the <code>run/in</code> statement is important?</a></li>
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
</ul>
</li>
<li><a href="#sec-5">5 Advanced examples</a>
<ul>
<li><a href="#sec-5-1">5.1 Wearing sensors</a></li>
<li><a href="#sec-5-2">5.2 Waiting for the stability of a signal</a>
<ul>
<li><a href="#sec-5-2-1">5.2.1 The Lutin version</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>

<div id="outline-container-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> Forewords</h2>
<div class="outline-text-2" id="text-1">


</div>

<div id="outline-container-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> Motivations: testing reactive programs</h3>
<div class="outline-text-3" id="text-1-1">


<p>
<img src="./demarche-it-en.png"  alt="./demarche-it-en.png" />
</p>

<p>
Lutin aims at facilitating the writing of SUT environments, seen as
non-deterministic reactive machines. 
</p>

</div>

</div>

<div id="outline-container-1-2" class="outline-3">
<h3 id="sec-1-2"><span class="section-number-3">1.2</span> Lutin in one slide</h3>
<div class="outline-text-3" id="text-1-2">


<ul>
<li>Lustre-like: Dataflow, parallelism, modular, logic time, <code>pre</code>.
</li>
<li>But not exactly Lustre though
<ul>
<li>Plus
<ul>
<li><b>Control structure</b> operators (regular expressions)
</li>
<li><b>Stochastic</b> (controlled and pseudo-Aleatory) 

</li>
</ul>

</li>
<li>Minus
<ul>
<li>No implicit top-level loop 
</li>
<li>No topological sort of equations
</li>
</ul>

</li>
</ul>

</li>
</ul>


</div>

</div>

<div id="outline-container-1-3" class="outline-3">
<h3 id="sec-1-3"><span class="section-number-3">1.3</span> In order to run this tutorial</h3>
<div class="outline-text-3" id="text-1-3">


erwan's avatar
erwan committed
352
353
354
355
<p> 
You first need to install opam. For instance, on debian-like boxes do
</p>

356
357


erwan's avatar
erwan committed
358
359
360
361
362
363
364
365
366
<pre class="src src-sh">sudo apt-get install opam 
opam init ; opam switch 4.04.0 ; <span style="color: #DCDCCC; font-weight: bold;">eval</span> <span style="color: #CC9393;">`opam config env`</span>
</pre>


<p>
 and then do:
</p>

367

erwan's avatar
erwan committed
368
369

<pre class="src src-sh">sudo apt-get install gnuplot tcl
370
371
372
373
374
375
376
377
378
379
opam repo add verimag-sync-repo <span style="color: #CC9393;">"http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/opam-repository"</span>
opam update
opam install lutin
</pre>


<p>
and also the Lustre V4 distribution (for luciole and sim2chro) 
<a href="http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lustre-v4/distrib/index.html">http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lustre-v4/distrib/index.html</a>
</p>
380
381


erwan's avatar
erwan committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

<p>
Basically, to get all the tools necessary to run this tutorial accessible
from ypour path, you should have something like that in your <code>.bashrc</code> :
</p>



<pre class="src src-sh"><span style="color: #5F7F5F;"># </span><span style="color: #7F9F7F;">for lutin, gnuplot-rif, luciole-rif</span>
. ~/.opam/opam-init/init.sh &gt; /dev/null 2&gt; /dev/null || <span style="color: #DCDCCC; font-weight: bold;">true</span>
<span style="color: #5F7F5F;"># </span><span style="color: #7F9F7F;">for simec (luciole), sim2chrogtk </span>
<span style="color: #DCDCCC; font-weight: bold;">export</span> <span style="color: #DFAF8F;">LUSTRE_INSTALL</span>=~/lustre-v4-xxx
<span style="color: #DCDCCC; font-weight: bold;">source</span> $<span style="color: #DFAF8F;">LUSTRE_INSTALL</span>/setenv.sh
</pre>



</div>

</div>

<div id="outline-container-1-4" class="outline-3">
<h3 id="sec-1-4"><span class="section-number-3">1.4</span> In order to run the demo from the pdf slides</h3>
<div class="outline-text-3" id="text-1-4">


<p>
You can edit (on linux boxes) your <code>~/.xpdfrc</code> resource
file modify the <code>urlCommand</code> rule:
</p>



<pre class="src src-sh">urlCommand      <span style="color: #CC9393;">"browserhook.sh '%s'"</span>
</pre>


419
<ul>
erwan's avatar
erwan committed
420
421
422
423
424
425
426
427
428
429
<li>Download the bash  script  <a href="http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lurette/sh/browserhook.sh">browserhook.sh  (link  to the  script)</a>
</li>
<li>Make it executable and available from your path
</li>
<li>Then the green link should launch the corresponding shell command,
<ul>
<li>A <a href="http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lurette/doc/lutin-tuto/lutin-tuto-handout.pdf">pdf version of this tutorial</a>
</li>
</ul>

430
431
432
433
434
</li>
</ul>



435
436


erwan's avatar
erwan committed
437
438
439



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
</div>
</div>

</div>

<div id="outline-container-2" class="outline-2">
<h2 id="sec-2"><span class="section-number-2">2</span> Execute Lutin programs</h2>
<div class="outline-text-2" id="text-2">




</div>

<div id="outline-container-2-1" class="outline-3">
<h3 id="sec-2-1"><span class="section-number-3">2.1</span> Stimulate Lutin programs</h3>
<div class="outline-text-3" id="text-2-1">


erwan's avatar
erwan committed
459
460


461
<p>
erwan's avatar
erwan committed
462
463
Before  learning the  <a href="http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lurette/doc/lutin-man.pdf">language</a>, let's have  a look  at a
couple of tools  that will allow us to play  with our Lutin programs.
464
465
466
</p>

<p>
erwan's avatar
erwan committed
467
468
469
 The Lutin  program command-line  interpreter is  a stand-alone
 executable  named <code>lutin</code>.  This program ougth to be 
   accessible from your path if you have followed the instr
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
</p>



</div>

<div id="outline-container-2-1-1" class="outline-4">
<h4 id="sec-2-1-1"><span class="section-number-4">2.1.1</span> A program that increments its input</h4>
<div class="outline-text-4" id="text-2-1-1">


<p>
Let's consider the following Lutin program named <a href="./incr.lut">incr.lut</a>.
</p>


<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> incr(x:<span style="color: #7CB8BB;">int</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) = 
     <span style="color: #DCDCCC; font-weight: bold;">loop</span> [10] y = x+1 
</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin incr.lut 
</pre>




500
<pre class="example"># This is lutin Version 1.54 (9e783b6)
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# The random engine was initialized with the seed 817281206
#inputs "x":int 
#outputs "y":int 
#step 1
</pre>


<p>
This is a <a href="#reactive-program">reactive program</a>  that returns its input, incremented by 1,
forever.   We'll explain  why later.   For the  moment,  let's simply
execute it using <code>lutin</code> from a shell:
</p>
<p>
At this stage, <code>lutin</code> waits for an integer input to continue.  If we
feed it with a <code>1</code> (type <code>1&lt;enter&gt;</code>), it returns 2.  If we enter 41,
it returns 42.  To quit this infinite loop gently, just enter the
character <code>q</code>.
</p>



<pre class="src src-rif">&lt;promt&gt; lutin incr.lut
$ 1
1 <span style="color: #93E0E3;">#</span><span style="color: #CC9393;">outs</span> 2 
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">step</span> 2
$ 41
41 <span style="color: #93E0E3;">#</span><span style="color: #CC9393;">outs</span> 42
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">step</span> 3
$ q
q<span style="color: #93E0E3;">#</span> bye!
<span style="color: #93E0E3;">#</span>end. 
</pre>




</div>

</div>

<div id="outline-container-2-1-2" class="outline-4">
<h4 id="sec-2-1-2"><span class="section-number-4">2.1.2</span> A program with no input</h4>
<div class="outline-text-4" id="text-2-1-2">


erwan's avatar
erwan committed
546
547


548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563


<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> one() <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) = 
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> y = 1
</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin -l 5 one.lut 
</pre>




564
<pre class="src src-rif"><span style="color: #93E0E3;">#</span> This is lutin Version 1.54 (9e783b6)
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<span style="color: #93E0E3;">#</span> The random engine was initialized with the seed 931220738
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">inputs</span> 
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">outputs</span> "y":<span style="color: #DFAF8F;">int</span> 
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">step</span> 1
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">outs</span> 1 
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">step</span> 2
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">outs</span> 1 
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">step</span> 3
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">outs</span> 1 
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">step</span> 4
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">outs</span> 1 
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">step</span> 5
<span style="color: #93E0E3;">#</span><span style="color: #CC9393;">outs</span> 1 
</pre>

<p>
A Lutin program might have no input at all. In such a case, it might  
be to helpful  to know that the <code>--max-steps</code>  (<code>-l</code> for short) allows
erwan's avatar
erwan committed
583
one  to set a  maximum number  of simulation  steps to  perform.  
584
585
</p>

erwan's avatar
erwan committed
586
587
588
589
590
591
592
593
<blockquote>

<p><b>Question</b> : 
 Try to launch <code>lutin one.lut -l 5</code> on one.lut
</p>
</blockquote>


594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643



</div>

</div>

<div id="outline-container-2-1-3" class="outline-4">
<h4 id="sec-2-1-3"><span class="section-number-4">2.1.3</span> Be quiet</h4>
<div class="outline-text-4" id="text-2-1-3">







<pre class="src src-sh">&lt;prompt&gt; lutin -l 5 -quiet one.lut 
</pre>




<pre class="src src-rif">1 
1 
1 
1 
1 
</pre>

<p>
The previous  simulation basically produces a (wordy) sequence of five "1". To
obtain quieter sequence, one can use the <code>-quiet</code> option (<code>-q</code> for short): 
</p>



</div>
</div>

</div>

<div id="outline-container-2-2" class="outline-3">
<h3 id="sec-2-2"><span class="section-number-3">2.2</span> Stimulate Lutin programs graphically with <code>luciole</code></h3>
<div class="outline-text-3" id="text-2-2">





erwan's avatar
erwan committed
644
645
<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> incr(x:<span style="color: #7CB8BB;">int</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) = 
     <span style="color: #DCDCCC; font-weight: bold;">loop</span> [10] y = x+1 
646
647
648
649
650
651
</pre>





652
<pre class="src src-sh">&lt;prompt&gt; luciole-rif lutin incr.lut 
653
654
655
656
657
658
659
660
661
662
</pre>



<p>
It is also  possible to feed Lutin programs  with <code>luciole</code>, a tcl/tk
based GUI that  was originally crafted for Lustre  programs. You will
need to have tcl/tk installed to be able to use it.
</p>
<p>
erwan's avatar
erwan committed
663
<img src="./jpg/Screenshot-Luciole-incr.jpg" :border 2 :rules all :frame border :class striped table-striped alt="./jpg/Screenshot-Luciole-incr.jpg" />
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
</p>
<p>
The use of Luciole is  straightforward. Some useful features that you
should try to play with are:
</p><ul>
<li>The "Compose" mode (accessible via the "Clocks" menu)
</li>
<li>The "Real Time Clock" mode (accessible via the "Clocks" menu)
</li>
<li>The Sim2chro display (accessible via the "Tools" menu)
</li>
</ul>




</div>

</div>

<div id="outline-container-2-3" class="outline-3">
<h3 id="sec-2-3"><span class="section-number-3">2.3</span> Store and Display the produced data: <code>sim2chro</code> and <code>gnuplot-rif</code></h3>
<div class="outline-text-3" id="text-2-3">



</div>

<div id="outline-container-2-3-1" class="outline-4">
<h4 id="sec-2-3-1"><span class="section-number-4">2.3.1</span> Generate a RIF file</h4>
<div class="outline-text-4" id="text-2-3-1">


<p>
It is  possible to store the lutin  RIF output into a  file using the
<code>-o</code> option.
</p>





<pre class="src src-sh">&lt;prompt&gt; lutin -l 10 -o ten.rif N.lut ; ls -lh ten.rif 
</pre>



<p>
Produces the <a href="ten.rif">ten.rif</a> file <a href="#from-file-N.lut">N.lut</a>.
</p>


</div>

</div>

<div id="outline-container-2-3-2" class="outline-4">
<h4 id="sec-2-3-2"><span class="section-number-4">2.3.2</span> Visualize a  RIF file</h4>
<div class="outline-text-4" id="text-2-3-2">





<pre class="src src-sh">&lt;prompt&gt; cat ten.rif 
</pre>



<p>
The <code>#outs</code>, <code>#in</code>, etc., produced by <code>lutin</code> are RIF directives. RIF
stands for "Reactive Input Format" 
</p>



</div>

</div>

<div id="outline-container-2-3-3" class="outline-4">
<h4 id="sec-2-3-3"><span class="section-number-4">2.3.3</span> Visualize a  RIF file (bis)</h4>
<div class="outline-text-4" id="text-2-3-3">






<pre class="src src-sh">&lt;prompt&gt; cat ten.rif | sim2chrogtk -ecran &gt; /dev/null 
</pre>



<p>
In order  to graphically display the  content of this  .rif file, one
can  use  two  tools  that  are part  of  the  <code>lutin</code>  distribution:
<code>sim2chro</code> (or <code>sim2chrogtk</code>), and <code>gnuplot-rif</code> (requires <a href="http://www.gnuplot.info/download.html">gnuplot</a>).
</p>



</div>

</div>

<div id="outline-container-2-3-4" class="outline-4">
<h4 id="sec-2-3-4"><span class="section-number-4">2.3.4</span> Visualize a  RIF file (ter)</h4>
<div class="outline-text-4" id="text-2-3-4">







<pre class="src src-sh">&lt;prompt&gt; gnuplot-rif ten.rif 
</pre>



<p>
<img src="./png/Screenshot-Gnuplot.png"  alt="./png/Screenshot-Gnuplot.png" />
</p>

</div>
</div>

</div>

<div id="outline-container-2-4" class="outline-3">
<h3 id="sec-2-4"><span class="section-number-3">2.4</span> Automatic stimulation of Lutin programs</h3>
<div class="outline-text-3" id="text-2-4">





<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> incr(x:<span style="color: #7CB8BB;">int</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) = 
     <span style="color: #DCDCCC; font-weight: bold;">loop</span> y = x+1 
<span style="color: #F0DFAF; font-weight: bold;">node</span> decr(y:<span style="color: #7CB8BB;">int</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (x:<span style="color: #7CB8BB;">int</span>) = 
     x = 42 <span style="color: #DCDCCC; font-weight: bold;">fby</span> <span style="color: #DCDCCC; font-weight: bold;">loop</span> x = y-1
</pre>





erwan's avatar
erwan committed
812
813
<pre class="src src-sh">&lt;prompt&gt; lurette -sut <span style="color: #CC9393;">"lutin decr.lut -n incr"</span> -env <span style="color: #CC9393;">"lutin decr.lut -n decr"</span>
&lt;prompt&gt; cat rdbg.rif | sim2chrogtk -ecran &gt; /dev/null 
814
815
816
817
818
</pre>



<p>
erwan's avatar
erwan committed
819
<img src="./png/lurette-sim2chro.png"  alt="./png/lurette-sim2chro.png" />
820
821
822
823
824
825
826
827
828
829
830
831
</p>



<p>
<img src="jpg/geluck-echec.jpg"  alt="jpg/geluck-echec.jpg" />
</p>
<ul>
<li>I've bought 2 electronic chess games
</li>
<li>connected one to another
</li>
832
<li>And now I'm at peace
833
834
835
836
837
838
839
840
841
842
843
</li>
</ul>



</div>
</div>

</div>

<div id="outline-container-3" class="outline-2">
erwan's avatar
erwan committed
844
<h2 id="sec-3"><span class="section-number-2">3</span> The Lutin Language</h2>
845
846
847
<div class="outline-text-2" id="text-3">


erwan's avatar
erwan committed
848

849
850
851
<p>
The  aim of this  tutorial is  to be  complementary to  the <a href="http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lurette/doc/lutin-man.pdf">Reference Manual</a>.  The  idea here is to  present the language  via examples. If
you  want  precise definitions  of  the  various language  statements
erwan's avatar
erwan committed
852
syntax and semantics, please refer to the <a href="http://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lurette/doc/lutin-man.pdf">Reference Manual</a>.
853
854
855
856
857
858
859
860
861
862
</p>


</div>

<div id="outline-container-3-1" class="outline-3">
<h3 id="sec-3-1"><span class="section-number-3">3.1</span> Back to programs of Section 1</h3>
<div class="outline-text-3" id="text-3-1">


863
864
865
866
<ul>
<li>Let's come back to the Lutin  programs mentioned so far. 
</li>
</ul>
867
868
869
870
871
872
873
874
875
876




<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> incr(x:<span style="color: #7CB8BB;">int</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) = 
     <span style="color: #DCDCCC; font-weight: bold;">loop</span> [10] y = x+1 
</pre>



erwan's avatar
erwan committed
877

878
879
</div>

880
<div id="outline-container-3-1-1" class="outline-4">
erwan's avatar
erwan committed
881
<h4 id="sec-3-1-1"><span class="section-number-4">3.1.1</span> We said said that  the  first  one,  <a href="incr.lut">incr.lut</a>  increments    its input by one. Let's explain why.</h4>
882
<div class="outline-text-4" id="text-3-1-1">
883
884
885
886



<ul>
887
<li>Those programs illustrate the 2 kinds of expressions we have in Lutin.
erwan's avatar
erwan committed
888
<ul>
889
<li><b>constraint  expressions</b> (<code>y = x+1</code>) that  asserts facts outputs
erwan's avatar
erwan committed
890
     variables. 
891
892
</li>
<li><b>trace expression</b> (<code>loop</code> &lt;te&gt;) that allows one to combine 
erwan's avatar
erwan committed
893
894
895
896
    constraint expressions.
</li>
</ul>

897
898
899
900
901
</li>
</ul>



erwan's avatar
erwan committed
902

903
904
905
<p>
The behavior  of a <i>satisfiable</i> constraint expression  is to produce
as outputs one  of its possible solutions during  one logical instant
erwan's avatar
erwan committed
906
907
908
909
910
(i.e.,  during one  <i>step</i>).  The  behavior of  the  trace expression
<code>loop te</code> is to behave as <code>te</code> as long as <code>te</code> is satisfiable.
</p>
<p>
Hence, the Lutin program <a href="incr.lut">incr.lut</a> could be read like that:
911
</p>
erwan's avatar
erwan committed
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
<blockquote>

<p>I am a reactive program that  takes an integer variable in input, and
that returns  an integer output. As  long as possible, I  will use the
constraint <code>y = x+1</code> to produce my output.
</p>
</blockquote>


<p>
Since this constraint  always has a solution, that  program will loop
forever.   Since  this  constraint  has exactly  one  solution,  this
program is deterministic.
</p>

<p>
Now let's have a look at the second program we've seen.
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> N() <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) = 
   y = 0 <span style="color: #DCDCCC; font-weight: bold;">fby</span> <span style="color: #DCDCCC; font-weight: bold;">loop</span> y = <span style="color: #BFEBBF;">pre</span> y+1
</pre>


<p>
The  program <a href="N.lut">N.lut</a>  illustrates  the use  of  two central  Lutin
keywords: <code>fby</code> and <code>pre</code>.
</p>
<p>
<code>fby</code> is the trace sequence  operator.  <code>b1 fby b2</code> means: behaves as
<code>b1</code>, and  when no more behavior  is possible, behaves  as <code>b2</code>.  The
constraint <code>y=0</code> is  always satisfiable; its behavior is  to bind the
output  <code>y</code> to  0 for  one step.   <code>y=0 fby  loop y  = pre  y+1</code> will
therefore behave  as <code>y=0</code> for the first  step, and as <code>loop  y = pre y+1</code> for the remaining steps.
</p>
<p>
<code>pre</code> is the  delay operator (as in Lustre). <code>pre  y</code> is an expression
that is bound to the value of <code>y</code> at the previous instant.   <code>y = pre y+1</code> will  therefore increment the value of  <code>y</code> at each instant.
Again this program is deterministic and runs infinitely.
</p>
<p>
Of  course, this means  that <code>y</code>  should be  defined at  the previous
instant. This  is the reason  why we've distinguished the  first step
from the  others. However, it  is possible to set  variables previous
values at  declarations time.  The program  <a href="N2.lut">N2.lut</a> below behaves
exactly as <a href="N.lut">N.lut</a>:
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> N2() <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span> = -1) = 
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> y = <span style="color: #BFEBBF;">pre</span> y+1
</pre>


<blockquote>

<p><b>Question</b>: 
Write a Lutin program with no input that generates one output <code>y</code>
with  the following sequence of values : 1 3 5 7 9 &hellip; 
</p>
</blockquote>


<blockquote>

<p><b>Question</b>: 
Write a Lutin program with no input that generates one output <code>y</code>
with  the following sequence of values : 1 2 4 8 16 32 &hellip; 
</p>
</blockquote>

986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195



</div>
</div>

</div>

<div id="outline-container-3-2" class="outline-3">
<h3 id="sec-3-2"><span class="section-number-3">3.2</span> Non deterministic programs</h3>
<div class="outline-text-3" id="text-3-2">



</div>

<div id="outline-container-3-2-1" class="outline-4">
<h4 id="sec-3-2-1"><span class="section-number-4">3.2.1</span> A First non-deterministic program</h4>
<div class="outline-text-4" id="text-3-2-1">






<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> trivial() <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x:<span style="color: #7CB8BB;">int</span>; f:<span style="color: #7CB8BB;">real</span> ; b:<span style="color: #7CB8BB;">bool</span>) =
  <span style="color: #DCDCCC; font-weight: bold;">loop</span> <span style="color: #BFEBBF;">true</span>
</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin -l 10 -q trivial.lut 
</pre>




<pre class="example">&lt;prompt&gt;  lutin -l 10 -q trivial.lut
129787787 89770690.47 f 
-202124065 60018934.21 t 
-74063075 -224861409.44 t 
-120351222 37052102.10 t 
-84618074 238667445.54 t 
186689955 -188684349.70 t 
211369478 -253941536.69 t 
-135268511 52434720.53 t 
113139954 241863809.02 t 
-114970522 -70907151.75 t 
</pre>


<p>
The programs  we've seen  so far were  deterministic because  all the
constraint expression were  affectations (i.e., equalities between an
input  and  an  output  variables).   As a  matter  of  fact,  writing
non-deterministic  is  even  simpler:  you  just  need  to  write  no
constraint at all!  Indeed, observe how this <a href="trivial.lut">trivial.lut</a> program
behaves:
</p>



</div>

</div>

<div id="outline-container-3-2-2" class="outline-4">
<h4 id="sec-3-2-2"><span class="section-number-4">3.2.2</span> It is possible to set the variable range at declaration time,</h4>
<div class="outline-text-4" id="text-3-2-2">

<p>as done in <a href="trivial2.lut">trivial2.lut</a>:
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> trivial() <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x:<span style="color: #7CB8BB;">int</span>  [-100;100]; 
                       f:<span style="color: #7CB8BB;">real</span> [-100.0;100.0];b:<span style="color: #7CB8BB;">bool</span>)=  
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> <span style="color: #BFEBBF;">true</span>

</pre>






<pre class="src src-sh">&lt;prompt&gt; lutin -l 10 -q trivial2.lut 
</pre>




<pre class="example">71 42.08 f 
-61 27.28 f 
-49 -64.96 f 
44 63.08 t 
-15 73.06 f 
0 -74.39 f
87 -1.58 f 
-20 89.62 f 
3 -84.20 t 
89 28.64 f 
</pre>



</div>
</div>

</div>

<div id="outline-container-3-3" class="outline-3">
<h3 id="sec-3-3"><span class="section-number-3">3.3</span> Non deterministic programs (cont)</h3>
<div class="outline-text-3" id="text-3-3">

<p> As you've just seen, a  Lutin program is by default chaotic. To
make it less chaotic, one has to add constraints which narrow the set
of  possible behaviors.   When  only one  behavior  is possible,  the
program is said to be <i>deterministic</i>.  When no behavior is possible,
the program stops.
</p>

<p>
 Now consider the <a href="range.lut">range.lut</a> program:
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> range() <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) = <span style="color: #DCDCCC; font-weight: bold;">loop</span> 0 &lt;= y <span style="color: #BFEBBF;">and</span> y &lt;= 42
</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin range.lut -l 10 -q 
</pre>


<p>
 The  constraint expression  <code>0 &lt;=  y and y  &lt;= 42</code>  has several
solutions. One of  those solutions will be drawn  and used to produce
the output of the current step.
</p>



<pre class="example">&lt;prompt&gt; lutin range.lut -l 10 -q
24 
38 
12 
30 
5 
5 
40 
8 
26 
39 
</pre>


<p>
Lutin constraints  can only  be linear, which  means that the  set of
 solutions  is a union  of convex  polyhedra. Several  heuristics are
 defined to  perform the  solution draw, that  are controlled  by the
 following options:
</p>




<ul>
<li><code>--step-inside</code> (<code>-si</code>): draw inside the polyhedra 
  (the default)
</li>
<li><code>--step-vertices</code> (<code>-sv</code>) draw among the polyhedra vertices 
</li>
<li><code>--step-edges</code> (<code>-se</code>): promote edges 
</li>
</ul>








<pre class="src src-sh">&lt;prompt&gt; lutin range.lut -l 10 -q --step-vertices 
</pre>




<pre class="example">42 
0 
42 
42 
0 
42 
42 
0 
0 
42 
</pre>



erwan's avatar
erwan committed
1196
<blockquote>
1197

erwan's avatar
erwan committed
1198
1199
<p><b>Question</b>: write  a simpler  program than <a href="range.lut">range.lut</a>  that behaves
the same.
1200
</p>
erwan's avatar
erwan committed
1201
1202
1203
1204
1205
</blockquote>




1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
</div>

</div>

<div id="outline-container-3-4" class="outline-3">
<h3 id="sec-3-4"><span class="section-number-3">3.4</span> Non deterministic programs (cont)</h3>
<div class="outline-text-3" id="text-3-4">


<ul>
<li>A 3D non-deterministic example 
</li>
</ul>


<p>
erwan's avatar
erwan committed
1222
Of course constraints can be more complex as in <a href="polyhedron.lut">polyhedron.lut</a>:
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> polyhedron() <span style="color: #F0DFAF; font-weight: bold;">returns</span>(a,b,c:<span style="color: #7CB8BB;">real</span>) =
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> (0.0 &lt; c <span style="color: #BFEBBF;">and</span> c  &lt; 4.0 <span style="color: #BFEBBF;">and</span> 
         a + 1.0 * b &gt; 0.0 <span style="color: #BFEBBF;">and</span> 
         a + 1.0 * b - 3.0 &lt; 0.0 <span style="color: #BFEBBF;">and</span> 
         a - 1.0 * b &lt; 0.0 <span style="color: #BFEBBF;">and</span> 
         a - 1.0 * b + 3.0 &gt; 0.0)

</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin polyhedron.lut -l 1000 -q &gt; poly.data;<span style="color: #CC9393;">\\</span> 
1241
<span style="color: #DCDCCC; font-weight: bold;">echo</span> <span style="color: #CC9393;">'set point 0.2; splot "poly.data" using 1:2:3;pause mouse close'</span>| gnuplot 
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
</pre>



<p>
However, constraint expressions should  be linear.  For example, <code>x * x &gt;0</code> is rejected by the current  solver.  But of course, <code>x * y &gt; 0</code>
is ok if <code>x</code> (or <code>y</code>) is an input.
</p>
<p>
<code>gnuplot-rif</code> is  not designed to  display 3d data; however,  one can
generate and  visualize data generated  by this program using  the 3D
plotting facilities of gnuplot like that:
</p>








<ul>
<li>One   can   observe   the    effect   of  
    &ndash;step-edges 
</li>
</ul>

<p>and
&ndash;step-vertices )
options on the repartition of generated points
</p>





<pre class="example">&lt;prompt&gt; lutin polyhedron.lut -l 1000 -quiet -se &gt; poly-se.data 
&lt;prompt&gt; lutin polyhedron.lut -l 1000 -quiet -sv &gt; poly-sv.data 
1280
1281
&lt;prompt&gt; echo 'set pointsize 0.2; splot "poly-se.data" using 1:2:3;pause mouse close'| gnuplot
&lt;prompt&gt; echo 'set pointsize 0.8; splot "poly-sv.data" using 1:2:3;pause mouse close'| gnuplot
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
</pre>


<p>
hint: use the mouse inside the gnuplot window to change the perspective.
</p>
<p>
<img src="./png/Screenshot-Gnuplot-se.png"  alt="./png/Screenshot-Gnuplot-se.png" />
</p>

</div>

</div>

<div id="outline-container-3-5" class="outline-3">
<h3 id="sec-3-5"><span class="section-number-3">3.5</span> Non deterministic programs (cont)</h3>
<div class="outline-text-3" id="text-3-5">



<p>
erwan's avatar
erwan committed
1303
1304
1305
1306
1307
Constraint   may   also  depend   on   inputs.   
</p>

<p>
Try   to  play   the <a href="range-bis.lut">range-bis.lut</a> program:
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> range_bis(i:<span style="color: #7CB8BB;">int</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) = 
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> 0 &lt;= y <span style="color: #BFEBBF;">and</span> y &lt;= i
</pre>




1319
<pre class="src src-sh">&lt;prompt&gt; luciole-rif lutin range-bis.lut 
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
</pre>




<p>
The  major  difference with  <a href="range.lut">range.lut</a>  is  that the  constraint
expression <code>0  &lt;= y and  y &lt;= i</code>  is not always satisfiable.   If one
enters a negative value, that program will stop.
</p>

erwan's avatar
erwan committed
1331
<blockquote>
1332

erwan's avatar
erwan committed
1333
<p><b>Question:</b>  modify the  <a href="range-bis.lut">range-bis.lut</a> program  (with  the concept
1334
1335
1336
introduced  so far) so  that when  a negative  input is  provided, it
returns -1.
</p>
erwan's avatar
erwan committed
1337
1338
1339
1340
1341

</blockquote>



1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454

</div>

</div>

<div id="outline-container-3-6" class="outline-3">
<h3 id="sec-3-6"><span class="section-number-3">3.6</span> Controlled non-determinism: the choice operator</h3>
<div class="outline-text-3" id="text-3-6">


<p>
<a href="choice.lut">choice.lut</a>
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> choice() <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x:<span style="color: #7CB8BB;">int</span>) =
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> {
      | x = 42
      | x = 1
}
</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin -l 10 -q choice.lut 
</pre>



<pre class="example">42 
42 
42 
1 
42 
42 
42 
42 
1 
1 
</pre>

<p>
In  the  previous  programs,  it  is  not  possible  to  control  the
non-determinism. It is possible  to change the drawing heuristics,
but that's all.   In order to control the  non-determinism, one as
to use the choice operator (<code>|</code>).
</p>
<p>
When executing the <a href="choice.lut">choice.lut</a>,  the Lutin interpreter performs a
fair choice  among the 2 satisfiable  constraints (<code>x = 42</code>  and <code>x = 1</code>).
</p>


<p>
It is possible to favor one branch over the other using 
weight directives (<code>:3</code>):
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> choice() <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x:<span style="color: #7CB8BB;">int</span>) =
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> {
      |3: x = 42
      |1: x = 1
}
</pre>


<p>
In <a href="choice2.lut">choice2.lut</a>, <code>x=42</code> is chosen with a probability of 3/4.
</p>






<pre class="src src-sh">&lt;prompt&gt; lutin -l 10000 -q choice2.lut | grep 42 | wc -l 
</pre>




<pre class="example">7429
</pre>


<p>
nb: "|" is a shortcut for "|1:". 
</p>
<p>
nb 2: having 3 choices with a weight of 1, that does not necessarily
means that each branch is  chosen with a probability of 1/3.  Indeed,
the  choice in done  among satisfiable  constraints. For  instance in
<a href="choice3.lut">choice3.lut</a> below,  not all the branches of  the alternative can
be satisfiable.
</p>

</div>

</div>

<div id="outline-container-3-7" class="outline-3">
<h3 id="sec-3-7"><span class="section-number-3">3.7</span> Controlled non-determinism: the choice operator</h3>
<div class="outline-text-3" id="text-3-7">





1455
1456
1457
<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> choice(b:<span style="color: #7CB8BB;">bool</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x:<span style="color: #7CB8BB;">int</span>) =
   x=0 <span style="color: #DCDCCC; font-weight: bold;">fby</span>
   x=-15 <span style="color: #DCDCCC; font-weight: bold;">fby</span>
1458
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> {
1459
1460
1461
      |1: x = 1 <span style="color: #BFEBBF;">and</span> b 
      |9: x = 2 <span style="color: #BFEBBF;">and</span> b 
      |   x = 3 <span style="color: #BFEBBF;">and</span> <span style="color: #BFEBBF;">not</span> b
1462
1463
1464
1465
1466
1467
}
</pre>




1468
<pre class="src src-sh">&lt;prompt&gt; luciole-rif lutin choice3.lut 
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
</pre>




</div>

</div>

<div id="outline-container-3-8" class="outline-3">
<h3 id="sec-3-8"><span class="section-number-3">3.8</span> Combinators</h3>
<div class="outline-text-3" id="text-3-8">


<p>
 A <i>combinator</i> is a well-typed  macro that eases code reuse.  
 One can define  a combinator with the <code>let/in</code>  statement, or just
 <code>let</code> for top-level combinators.
</p>


</div>

<div id="outline-container-3-8-1" class="outline-4">
<h4 id="sec-3-8-1"><span class="section-number-4">3.8.1</span> A simple combinator</h4>
<div class="outline-text-4" id="text-3-8-1">





<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">let</span> n = 3

<span style="color: #F0DFAF; font-weight: bold;">node</span> foo() <span style="color: #F0DFAF; font-weight: bold;">returns</span> (i:<span style="color: #7CB8BB;">int</span>) =
  <span style="color: #DCDCCC; font-weight: bold;">loop</span> [3] 0&lt;= i <span style="color: #BFEBBF;">and</span> i &lt; n <span style="color: #DCDCCC; font-weight: bold;">fby</span>
  <span style="color: #F0DFAF; font-weight: bold;">let</span> s=10 <span style="color: #F0DFAF; font-weight: bold;">in</span>
  <span style="color: #DCDCCC; font-weight: bold;">loop</span> [3] s&lt;= i <span style="color: #BFEBBF;">and</span> i &lt; s+n
</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin -quiet letdef.lut 
</pre>


</div>
</div>

</div>

<div id="outline-container-3-9" class="outline-3">
<h3 id="sec-3-9"><span class="section-number-3">3.9</span> A parametric combinator</h3>
<div class="outline-text-3" id="text-3-9">


<p>
The <a href="combinator.lut">combinator.lut</a> program illustrates the use of parametric combinators:
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">let</span> within(x, min, max: <span style="color: #7CB8BB;">int</span>): <span style="color: #7CB8BB;">bool</span> = 
  (min &lt;= x) <span style="color: #BFEBBF;">and</span> (x &lt;= max)

<span style="color: #F0DFAF; font-weight: bold;">node</span> random_walk() <span style="color: #F0DFAF; font-weight: bold;">returns</span> (y:<span style="color: #7CB8BB;">int</span>) =
  within(y,0,100) <span style="color: #DCDCCC; font-weight: bold;">fby</span> <span style="color: #DCDCCC; font-weight: bold;">loop</span> within(y,<span style="color: #BFEBBF;">pre</span> y-1,<span style="color: #BFEBBF;">pre</span> y+1)
</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin -l 100 combinator.lut -o walk.rif ; gnuplot-rif walk.rif 
</pre>



<p>
<img src="png/Screenshot-walk.png"  alt="png/Screenshot-walk.png" />
</p>
erwan's avatar
erwan committed
1551
1552
1553
1554
1555
1556
1557
<blockquote>

<p><b>Question:</b> Write such a randow walk for a real variable
</p>
</blockquote>


1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604

</div>

</div>

<div id="outline-container-3-10" class="outline-3">
<h3 id="sec-3-10"><span class="section-number-3">3.10</span> Combinators  (cont)</h3>
<div class="outline-text-3" id="text-3-10">



</div>

<div id="outline-container-3-10-1" class="outline-4">
<h4 id="sec-3-10-1"><span class="section-number-4">3.10.1</span> A combinator that needs memory   (<code>ref</code>)</h4>
<div class="outline-text-4" id="text-3-10-1">



<p>
If one  wants to access to the  previous value of a  variable inside a
combinator, one  has to declare  in the combinator profile  that this
variable is a <i>reference</i> using  the <code>ref</code> keyword, as illustrated in
the <a href="up-and-down.lut">up-and-down.lut</a> program:
</p>




<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">let</span> within(x, min, max: <span style="color: #7CB8BB;">real</span>): <span style="color: #7CB8BB;">bool</span> =  
    (min &lt;= x) <span style="color: #BFEBBF;">and</span> (x &lt;= max)
<span style="color: #F0DFAF; font-weight: bold;">let</span> up  (delta:<span style="color: #7CB8BB;">real</span>; x : <span style="color: #7CB8BB;">real</span> <span style="color: #7CB8BB;">ref</span>) : <span style="color: #7CB8BB;">bool</span> =    
    within(x, <span style="color: #BFEBBF;">pre</span> x, <span style="color: #BFEBBF;">pre</span> x + delta)
<span style="color: #F0DFAF; font-weight: bold;">let</span> down(delta:<span style="color: #7CB8BB;">real</span>; x : <span style="color: #7CB8BB;">real</span> <span style="color: #7CB8BB;">ref</span>) : <span style="color: #7CB8BB;">bool</span> = 
    within(x, <span style="color: #BFEBBF;">pre</span> x - delta, <span style="color: #BFEBBF;">pre</span> x)
<span style="color: #F0DFAF; font-weight: bold;">node</span> up_<span style="color: #BFEBBF;">and</span>_down(min,max,d:<span style="color: #7CB8BB;">real</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (x:<span style="color: #7CB8BB;">real</span>) =
   within(x, min, max) <span style="color: #DCDCCC; font-weight: bold;">fby</span>
   <span style="color: #DCDCCC; font-weight: bold;">loop</span> {
        | <span style="color: #DCDCCC; font-weight: bold;">loop</span> { up  (d, x) <span style="color: #BFEBBF;">and</span> <span style="color: #BFEBBF;">pre</span> x &lt; max } 
        | <span style="color: #DCDCCC; font-weight: bold;">loop</span> { down(d, x) <span style="color: #BFEBBF;">and</span> <span style="color: #BFEBBF;">pre</span> x &gt; min } 
       }
</pre>





1605
<pre class="src src-sh">&lt;prompt&gt; luciole-rif lutin up-and-down.lut 
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
</pre>


<p>
 The combinator <code>up</code>  (reps <code>down</code>) constraints the variable <code>x</code>
 to be between  its previous value and its  previous value plus (resp
 minus)  a  positive  <code>delta</code>.   The  node  <code>up-and-down</code>,  after  an
 initialization  step where  <code>x</code> is  drawn between  <code>min</code>  and <code>max</code>,
 chooses (fairly) to go  up, or to go down.  It it  chooses to go up,
 it does so as long as <code>x</code>  is smaller than <code>max</code>; then it goes down,
 until <code>min</code>, and so on.
</p>




erwan's avatar
erwan committed
1622
<blockquote>
1623

erwan's avatar
erwan committed
1624
<p><b>Question:</b> what  happens if you  guard the <code>up</code> combinator  by <code>x&lt;max</code>
1625
1626
instead of <code>pre x &lt; max</code>?
</p>
erwan's avatar
erwan committed
1627
1628
</blockquote>

1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643

</div>
</div>

</div>

<div id="outline-container-3-11" class="outline-3">
<h3 id="sec-3-11"><span class="section-number-3">3.11</span> Local variables</h3>
<div class="outline-text-3" id="text-3-11">


<p>   
Sometimes,  it is  useful to  use  auxiliary variables  that are  not
output  variables.    Such  variables  can  be   declared  using  the
<code>exist/in</code>    construct.    Its   use    is   illustrated    in   the
erwan's avatar
erwan committed
1644
<a href="true-since-n-instants.lut">true-since-n-instants.lut</a> program:
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">let</span> n = 3
<span style="color: #F0DFAF; font-weight: bold;">node</span> ok_since_n_instants(b:<span style="color: #7CB8BB;">bool</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (res:<span style="color: #7CB8BB;">bool</span>) =
  <span style="color: #F0DFAF; font-weight: bold;">exist</span> cpt: <span style="color: #7CB8BB;">int</span> = n <span style="color: #F0DFAF; font-weight: bold;">in</span> 
     <span style="color: #DCDCCC; font-weight: bold;">loop</span> { 
       cpt = (<span style="color: #F0DFAF; font-weight: bold;">if</span> b <span style="color: #F0DFAF; font-weight: bold;">then</span> (<span style="color: #BFEBBF;">pre</span> cpt-1) <span style="color: #F0DFAF; font-weight: bold;">else</span> n) <span style="color: #BFEBBF;">and</span> 
       res = (b <span style="color: #BFEBBF;">and</span> (cpt &lt;= 0)) 
     }
</pre>




erwan's avatar
erwan committed
1661
<pre class="src src-sh">&lt;prompt&gt; luciole-rif lutin true-since-n-instants.lut 
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
</pre>



<p>
It is possible  to set its previous value at  declaration time as for
interface variables. Here, the previous value of local variable <code>cpt</code>
is set to the constant n, which is bound to 3. This local variable is
used to count the number  of consecutive instants where the input <code>b</code>
is  true.   <code>cpt</code>  is reset  to  <code>n</code>  each  time  <code>b</code> is  false,  and
decremented otherwise. The node returns true when <code>cpt</code> is smaller or
equal than 0.
</p>


</div>

</div>

<div id="outline-container-3-12" class="outline-3">
<h3 id="sec-3-12"><span class="section-number-3">3.12</span> Local variables again</h3>
<div class="outline-text-3" id="text-3-12">


<p>
The  previous example  was deterministic  (it was  actually  a Lustre
program with  an explicit  <code>loop</code>), the local  variable was  a simple
(de)counter.  
</p>
<p>
Local variables  can also  plain random  variables, as
illustrated the <a href="local.lut">local.lut</a> program:
</p>





<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> local() <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x:<span style="color: #7CB8BB;">real</span> = 0.0) =
  <span style="color: #F0DFAF; font-weight: bold;">exist</span> target : <span style="color: #7CB8BB;">real</span> <span style="color: #F0DFAF; font-weight: bold;">in</span> 
  <span style="color: #DCDCCC; font-weight: bold;">loop</span> {
    0.0 &lt; target <span style="color: #BFEBBF;">and</span> target &lt; 42.0 <span style="color: #BFEBBF;">and</span> x = <span style="color: #BFEBBF;">pre</span> x 
    <span style="color: #DCDCCC; font-weight: bold;">fby</span>
    <span style="color: #DCDCCC; font-weight: bold;">loop</span> [20] { x = (<span style="color: #BFEBBF;">pre</span> x + target) / 2.0 <span style="color: #BFEBBF;">and</span> 
                target = <span style="color: #BFEBBF;">pre</span> target }
  }
</pre>


<p>
 At first  step, the local variable <code>target</code>  is chosen randomly
 between  0.0 and 42.0,  and x  keeps its  previous value  (0); then,
 during 20 steps, the output x  comes closer and closer to target (<code>x  = (pre x + target) / 2.0</code>), while <code>target</code> keeps its previous value.
 After 20 steps (<code>loop [20])</code>), another value for <code>target</code> is chosen,
 and so on so forth (because of the outer <code>loop</code>).
</p>






<pre class="src src-sh">&lt;prompt&gt; lutin local.lut -l 100 -o local.rif ; gnuplot-rif local.rif 
</pre>



<p>
<img src="png/Screenshot-Gnuplot-local.png"  alt="png/Screenshot-Gnuplot-local.png" />
</p>

erwan's avatar
erwan committed
1733
<blockquote>
1734

erwan's avatar
erwan committed
1735
1736
1737
<p> <b>Question:</b> modify  the previous program  so that x reaches  the target
    after a <a href="png/Screenshot-Gnuplot-localbis.png">damped oscillation</a>
</p>
1738
1739
1740
1741
1742
1743
1744
1745


<p>
like in the following screen-shot:
</p>
<p>
<img src="png/Screenshot-Gnuplot-localbis.png"  alt="png/Screenshot-Gnuplot-localbis.png" />
</p>
erwan's avatar
erwan committed
1746
1747
1748
1749
1750


</blockquote>


1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771

</div>

</div>

<div id="outline-container-3-13" class="outline-3">
<h3 id="sec-3-13"><span class="section-number-3">3.13</span> Distribute a constraint into a scope: <code>assert</code></h3>
<div class="outline-text-3" id="text-3-13">



<p>
 Now  let's consider  a slightly  different version  of  the previous
 program  where <code>n</code>  is  an input  of  the node.   Since the  current
 version of  the Lutin  interpreter is not  able to set  the previous
 value  of a  variable with  an input  value (this  restriction might
 change in  the future),  we need  to write for  the first  instant a
 constraint that does not involve <code>pre cpt</code>.
</p>

<p>
erwan's avatar
erwan committed
1772
Consider for instance  the  <a href="true-since-n-instants2.lut">true-since-n-instants2.lut</a> program:
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> ok_since_n_instants(b:<span style="color: #7CB8BB;">bool</span>;n:<span style="color: #7CB8BB;">int</span>)<span style="color: #F0DFAF; font-weight: bold;">returns</span>(res:<span style="color: #7CB8BB;">bool</span>)=  
  <span style="color: #F0DFAF; font-weight: bold;">exist</span> cpt: <span style="color: #7CB8BB;">int</span> <span style="color: #F0DFAF; font-weight: bold;">in</span>
  cpt = n <span style="color: #BFEBBF;">and</span> res = (b <span style="color: #BFEBBF;">and</span> (cpt &lt;= 0)) 
  <span style="color: #DCDCCC; font-weight: bold;">fby</span> 
     <span style="color: #DCDCCC; font-weight: bold;">loop</span> { 
       cpt = (<span style="color: #F0DFAF; font-weight: bold;">if</span> b <span style="color: #F0DFAF; font-weight: bold;">then</span> (<span style="color: #BFEBBF;">pre</span> cpt-1) <span style="color: #F0DFAF; font-weight: bold;">else</span> n) <span style="color: #BFEBBF;">and</span> 
       res = (b <span style="color: #BFEBBF;">and</span> (cpt &lt;= 0))  
     }
</pre>


<ul>
<li>One flaw is that <code>res = (b and (cpt&lt;=0))</code> is duplicated.

<p>
 This occurs very often, for example when you want to a variable
 to keep its  previous value during several steps,  and need to write
 boring <code>X = pre X</code> constraint  all the time. Indeed in Lutin, if one
 says nothing about a variable, it is chosen randomly. 
</p>
</li>
<li>The <code>assert/in</code> construct has been introduced in Lutin to avoid such
    code duplication.  
</li>
</ul>



<ul>
<li><code>assert &lt;ce&gt; in &lt;te&gt;</code> &equiv; <code>&lt;te'&gt;</code>, 

<p>
     where &lt;te'&gt;= &lt;te&gt;[c/c and ce]\(_{\forall c \in \mathcal{C}onstraints(te)}\)
</p></li>
</ul>




<p>
i.e., where the  trace expression <code>&lt;te'&gt;</code> is obtained  from the trace
expression  <code>&lt;te&gt;</code>  by substituting  all  the constraint  expressions
<code>&lt;c&gt;</code>  appearing in  <code>&lt;te&gt;</code>  by the  constraint  expression <code>&lt;c&gt;  and &lt;ce&gt;</code>.
</p>


erwan's avatar
erwan committed
1823
1824
1825
1826
1827
1828
1829
<blockquote>

<p><b>Question:</b> Rewrite the <a href="true-since-n-instants2.lut">true-since-n-instants2.lut</a> using the
  <code>assert/in</code> construct and avoid code duplication.
</p>
</blockquote>

1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849



</div>

</div>

<div id="outline-container-3-14" class="outline-3">
<h3 id="sec-3-14"><span class="section-number-3">3.14</span> External code</h3>
<div class="outline-text-3" id="text-3-14">


<p>
Lutin program can call any function defined in a shared library (<code>.so</code>)
</p>





1850
<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">extern</span> sin(x: <span style="color: #7CB8BB;">real</span>) : <span style="color: #7CB8BB;">real</span>
erwan's avatar
erwan committed
1851
1852
1853
1854
1855
1856
<span style="color: #F0DFAF; font-weight: bold;">let</span> between(x, min, max : <span style="color: #7CB8BB;">real</span>) : <span style="color: #7CB8BB;">bool</span> = ((min &lt; x) <span style="color: #BFEBBF;">and</span> (x &lt; max))
<span style="color: #F0DFAF; font-weight: bold;">node</span> bizzare() <span style="color: #F0DFAF; font-weight: bold;">returns</span> (x,res: <span style="color: #7CB8BB;">real</span>) = 
  <span style="color: #F0DFAF; font-weight: bold;">exist</span> noise: <span style="color: #7CB8BB;">real</span> <span style="color: #F0DFAF; font-weight: bold;">in</span>
  <span style="color: #F0DFAF; font-weight: bold;">assert</span> between(noise,-0.1, 0.1) <span style="color: #F0DFAF; font-weight: bold;">in</span>
  res = 0.0 <span style="color: #BFEBBF;">and</span> x = 0.0 <span style="color: #DCDCCC; font-weight: bold;">fby</span> 
  <span style="color: #DCDCCC; font-weight: bold;">loop</span>     x = <span style="color: #BFEBBF;">pre</span> x + 0.1 + noise
1857
1858
1859
1860
       <span style="color: #BFEBBF;">and</span> res = sin(<span style="color: #BFEBBF;">pre</span> x)
</pre>


1861
1862
1863



erwan's avatar
erwan committed
1864
<pre class="src src-sh">&lt;prompt&gt; lutin -L libm.so -l 200 ext-call.lut -o ext-call.rif;gnuplot-rif ext-call.rif 
1865
1866
1867
1868
</pre>



erwan's avatar
erwan committed
1869
1870
1871
1872
1873
<p>
<img src="png/Screenshot-Gnuplot-ext.png"  alt="png/Screenshot-Gnuplot-ext.png" />
</p>


1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
</div>

</div>

<div id="outline-container-3-15" class="outline-3">
<h3 id="sec-3-15"><span class="section-number-3">3.15</span> Exceptions</h3>
<div class="outline-text-3" id="text-3-15">


</div>

<div id="outline-container-3-15-1" class="outline-4">
<h4 id="sec-3-15-1"><span class="section-number-4">3.15.1</span> Global exceptions can be declared outside the main node:</h4>
<div class="outline-text-4" id="text-3-15-1">





<pre class="src src-lutin">exception ident
</pre>


</div>

</div>

<div id="outline-container-3-15-2" class="outline-4">
<h4 id="sec-3-15-2"><span class="section-number-4">3.15.2</span> or locally within a trace statement:</h4>
<div class="outline-text-4" id="text-3-15-2">




<pre class="src src-lutin">exception ident <span style="color: #F0DFAF; font-weight: bold;">in</span> st
</pre>


</div>

</div>

<div id="outline-container-3-15-3" class="outline-4">
<h4 id="sec-3-15-3"><span class="section-number-4">3.15.3</span> An existing  exception ident can  be raised with the  statement:</h4>
<div class="outline-text-4" id="text-3-15-3">




<pre class="src src-lutin">raise ident
</pre>


</div>

</div>

<div id="outline-container-3-15-4" class="outline-4">
<h4 id="sec-3-15-4"><span class="section-number-4">3.15.4</span> An exception can be caught with the statement:</h4>
<div class="outline-text-4" id="text-3-15-4">




<pre class="src src-lutin"><span style="color: #DCDCCC; font-weight: bold;">catch</span> ident <span style="color: #F0DFAF; font-weight: bold;">in</span> st1 <span style="color: #DCDCCC; font-weight: bold;">do</span> st2
</pre>


<p>
If the exception is raised  in st1, the control immediatelly passes to
st2. If the "do" part is omitted, the statement terminates normally.
</p>
</div>
</div>

</div>

<div id="outline-container-3-16" class="outline-3">
<h3 id="sec-3-16"><span class="section-number-3">3.16</span> Exceptions (cont)</h3>
<div class="outline-text-3" id="text-3-16">


<ul>
<li>The predefined Deadlock exception can only be catched
</li>
</ul>





<pre class="src src-lutin"><span style="color: #DCDCCC; font-weight: bold;">catch</span> Deadlock <span style="color: #F0DFAF; font-weight: bold;">in</span> st1 <span style="color: #DCDCCC; font-weight: bold;">do</span> st2
</pre>

<p>
&equiv;
</p>


<pre class="src src-lutin"><span style="color: #DCDCCC; font-weight: bold;">try</span> st1 <span style="color: #DCDCCC; font-weight: bold;">do</span> st2
</pre>



<ul>
<li>When  a   trace  expression  deadlocks,  
  the  Deadlock  exception is  raised.  In  fact,  this exception  is
  internal and cannot be redefined  nor raised by the user.  The only
  possible use of the Deadlock in programs is one try to catch it:
</li>
</ul>



<ul>
<li>If a deadlock is raised during the execution of st1, the control
  passes immediately to st2.   If st1 terminates normally, the whole
  statement terminates and the control passes to the sequel.  
</li>
</ul>


</div>

</div>

<div id="outline-container-3-17" class="outline-3">
<h3 id="sec-3-17"><span class="section-number-3">3.17</span> Exceptions (cont)</h3>
<div class="outline-text-3" id="text-3-17">






2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> toto(i:<span style="color: #7CB8BB;">int</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (x:<span style="color: #7CB8BB;">int</span>)=
  <span style="color: #DCDCCC; font-weight: bold;">loop</span> {
    exception Stop <span style="color: #F0DFAF; font-weight: bold;">in</span>
    <span style="color: #DCDCCC; font-weight: bold;">catch</span> Stop <span style="color: #F0DFAF; font-weight: bold;">in</span>
       <span style="color: #DCDCCC; font-weight: bold;">loop</span> [1,10] x = i <span style="color: #DCDCCC; font-weight: bold;">fby</span> raise Stop <span style="color: #DCDCCC; font-weight: bold;">fby</span> x = 43
    <span style="color: #DCDCCC; font-weight: bold;">do</span> x=42
   }
</pre>


2019
2020
2021



2022
<pre class="src src-sh">&lt;prompt&gt; luciole-rif lutin except.lut 
2023
2024
2025
2026
2027
2028
2029
2030
2031
</pre>



<p>
 <a href="except.lut">except.lut</a>
</p>

<p>
erwan's avatar
erwan committed
2032
Note that the 43 value is  generated iff i=43.
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
</p>
</div>

</div>

<div id="outline-container-3-18" class="outline-3">
<h3 id="sec-3-18"><span class="section-number-3">3.18</span> About exceptions</h3>
<div class="outline-text-3" id="text-3-18">

<ul>
erwan's avatar
erwan committed
2043
<li>Very  powerful mechanism
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
</li>
<li>Can be used to build complex trace operators
</li>
<li>But should they used to program?
</li>
</ul>


</div>

</div>

<div id="outline-container-3-19" class="outline-3">
<h3 id="sec-3-19"><span class="section-number-3">3.19</span> Parallelism: <code>&amp;&gt;</code></h3>
<div class="outline-text-3" id="text-3-19">






<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">node</span> n() <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x,y:<span style="color: #7CB8BB;">int</span>) = {
      <span style="color: #DCDCCC; font-weight: bold;">loop</span> { -10 &lt; x <span style="color: #BFEBBF;">and</span> x &lt; 10 }
   &amp;&gt; y = 0 <span style="color: #DCDCCC; font-weight: bold;">fby</span> <span style="color: #DCDCCC; font-weight: bold;">loop</span> { y = <span style="color: #BFEBBF;">pre</span> x } } 
</pre>





2074
<pre class="src src-sh">&lt;prompt&gt; luciole-rif lutin paralel.lut 
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
</pre>



<p>
<code>&lt;te1&gt; &amp;&gt; &lt;te2&gt;</code> executes the trace expression <code>&lt;te1&gt;</code> and <code>&lt;te2&gt;</code> in
 parallel.
</p>

<p> 
<code>&amp;&gt;st1  &amp;&gt;...  &amp;&gt;stn</code>  where  the  first <code>&amp;&gt;</code>  can  be omitted.   This
statement  executes  in  parallel   all  the  statements  <code>st1</code>,  &hellip;,
<code>stn</code>. All along  the parallel execution each branch  produces its own
constraint;  the  conjunction of  these  local  constraints gives  the
global  constraint.   If one  branch  terminates  normally, the  other
branches continue.   The whole statement terminates  when all branches
have terminated.
</p>


<p>
nota bene: this construct can be expensive because of: 
</p><ul>
<li><b>the  control  structure</b>: such  a  product is  equivalent to  an
  automata product, which, in the worst case, can be quadratic;
</li>
<li><b>the  data</b>:  the  polyhedron resolution  is  exponential in  the
  dimension of the polyhedron.
</li>
</ul>


<p>  
Use the <code>run/in</code> construct instead if performance is a problem.
</p>
</div>

</div>

<div id="outline-container-3-20" class="outline-3">
<h3 id="sec-3-20"><span class="section-number-3">3.20</span> Combinators (again)</h3>
<div class="outline-text-3" id="text-3-20">


</div>

<div id="outline-container-3-20-1" class="outline-4">
<h4 id="sec-3-20-1"><span class="section-number-4">3.20.1</span> Trace Combinators</h4>
<div class="outline-text-4" id="text-3-20-1">





<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">let</span> myloop(t:<span style="color: #7CB8BB;">trace</span>) : <span style="color: #7CB8BB;">trace</span> = <span style="color: #DCDCCC; font-weight: bold;">loop</span> <span style="color: #DCDCCC; font-weight: bold;">try</span> <span style="color: #DCDCCC; font-weight: bold;">loop</span> t
</pre>


<p>
Here we restart the loop from the beginning whenever we are
blocked somewhere inside <code>t</code>. (<a href="myloop.lut">myloop.lut</a>)
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">let</span> myloop(t:<span style="color: #7CB8BB;">trace</span>) : <span style="color: #7CB8BB;">trace</span> = <span style="color: #DCDCCC; font-weight: bold;">loop</span> <span style="color: #DCDCCC; font-weight: bold;">try</span> <span style="color: #DCDCCC; font-weight: bold;">loop</span> t
<span style="color: #F0DFAF; font-weight: bold;">node</span> use_myloop(reset:<span style="color: #7CB8BB;">bool</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x:<span style="color: #7CB8BB;">int</span>) =
  myloop(
      x = 0 <span style="color: #DCDCCC; font-weight: bold;">fby</span> 
      <span style="color: #F0DFAF; font-weight: bold;">assert</span> <span style="color: #BFEBBF;">not</span> reset <span style="color: #F0DFAF; font-weight: bold;">in</span>
      x = 1 <span style="color: #DCDCCC; font-weight: bold;">fby</span> 
      x = 2 <span style="color: #DCDCCC; font-weight: bold;">fby</span>
      x = 3 <span style="color: #DCDCCC; font-weight: bold;">fby</span>
      x = 4
  )
</pre>




2155
<pre class="src src-sh">&lt;prompt&gt; luciole-rif lutin myloop.lut 
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
</pre>



<p>
Each step you set reset to <code>true</code>, the output equals to <code>0</code>.
</p>





</div>
</div>
</div>

</div>

<div id="outline-container-4" class="outline-2">
2175
<h2 id="sec-4"><span class="section-number-2">4</span> The <code>run</code> operator</h2>
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
<div class="outline-text-2" id="text-4">


</div>

<div id="outline-container-4-1" class="outline-3">
<h3 id="sec-4-1"><span class="section-number-3">4.1</span> Cheap parallelism: Calling Lutin nodes <code>run/in</code></h3>
<div class="outline-text-3" id="text-4-1">


<p>
 The idea  is the following: when one writes:
</p>



<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">run</span> (x,y) := foo(a,b) <span style="color: #F0DFAF; font-weight: bold;">in</span>
</pre>


<p>
in order to be accepted the following rules must hold:
</p><ul>
<li><code>a</code> and <code>b</code> be uncontrollable variables (e.g., inputs or memories)
</li>
<li><code>x</code> and <code>y</code> should be controllable variables (e.g., outputs or locals)
</li>
<li>in the scope of such a <code>run/in</code>, <code>x</code> and <code>y</code> becomes uncontrollable.
</li>
</ul>



<p>
nb : it is exactly the parallelism of Lustre, with an heavier syntax.
In Lustre, one would simply write 
</p>


<pre class="src src-lustre"><span style="color: #F0DFAF; font-weight: bold;">(</span>x<span style="color: #F0DFAF; font-weight: bold;">,</span>y<span style="color: #F0DFAF; font-weight: bold;">)=</span><span style="color: #93E0E3;">foo</span><span style="color: #F0DFAF; font-weight: bold;">(</span>a<span style="color: #F0DFAF; font-weight: bold;">,</span>b<span style="color: #F0DFAF; font-weight: bold;">);</span>
</pre>


<p>
Moreover in Lutin, the order of equations matters.
</p>
</div>

</div>

<div id="outline-container-4-2" class="outline-3">
<h3 id="sec-4-2"><span class="section-number-3">4.2</span> Cheap parallelism: Calling Lutin nodes <code>run/in</code></h3>
<div class="outline-text-3" id="text-4-2">


<ul>
<li>The <code>run/in</code> construct is another (cheaper) way of executing code in parallel
</li>
<li>The only way of calling Lutin nodes.  
</li>
<li>Less powerful: constraints are not merged, but solved in sequence 
</li>
</ul>





<pre class="src src-lutin"><span style="color: #F0DFAF; font-weight: bold;">include</span> "N.lut"
<span style="color: #F0DFAF; font-weight: bold;">include</span> "incr.lut"
<span style="color: #F0DFAF; font-weight: bold;">node</span> use_<span style="color: #F0DFAF; font-weight: bold;">run</span>() <span style="color: #F0DFAF; font-weight: bold;">returns</span>(x:<span style="color: #7CB8BB;">int</span>) =
  <span style="color: #F0DFAF; font-weight: bold;">exist</span> a,b : <span style="color: #7CB8BB;">int</span> <span style="color: #F0DFAF; font-weight: bold;">in</span>
  <span style="color: #F0DFAF; font-weight: bold;">run</span> a := N() <span style="color: #F0DFAF; font-weight: bold;">in</span>
  <span style="color: #F0DFAF; font-weight: bold;">run</span> b := incr(a) <span style="color: #F0DFAF; font-weight: bold;">in</span>
  <span style="color: #F0DFAF; font-weight: bold;">run</span> x := incr(b) <span style="color: #F0DFAF; font-weight: bold;">in</span>
    <span style="color: #DCDCCC; font-weight: bold;">loop</span> <span style="color: #BFEBBF;">true</span>
</pre>





<pre class="src src-sh">&lt;prompt&gt; lutin -l 5 -q run.lut -m use<span style="color: #CC9393;">\_</span>run 
</pre>




<pre class="example">2 
3 
4 
5 
6 
</pre>


<p>
The program <a href="run.lut">run.lut</a> illustrates the use of the <code>run/in</code> statements:
This program uses nodes defined in <a href="N.lut">N.lut</a> and <a href="incr.lut">incr.lut</a>.
</p>

<p>
Another illustration of the use of  <code>run</code> can be found in the <a href="#sec-5-1">Wearing sensors</a> exemple.
</p>






2286

2287
2288
2289
2290
2291
</div>

</div>

<div id="outline-container-4-3" class="outline-3">
erwan's avatar
erwan committed
2292
<h3 id="sec-4-3"><span class="section-number-3">4.3</span> Why does the <code>run/in</code> statement is important?</h3>
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
<div class="outline-text-3" id="text-4-3">


<p>   
Using combinators and <code>&amp;&gt;</code>, it was already possible to reuse code, but
<code>run/in</code> is
</p>
<ul>
<li>Much more efficient: polyhedra dimension is smaller
</li>
</ul>


2306

2307
2308
2309
2310
2311
<ul>
<li>Mode-free (args can be in or out) combinators are error-prone
</li>
</ul>

2312

2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381

</div>
</div>

</div>

<div id="outline-container-5" class="outline-2">
<h2 id="sec-5"><span class="section-number-2">5</span> Advanced examples</h2>
<div class="outline-text-2" id="text-5">



</div>

<div id="outline-container-5-1" class="outline-3">
<h3 id="sec-5-1"><span class="section-number-3">5.1</span> Wearing sensors</h3>
<div class="outline-text-3" id="text-5-1">


<p>
The <a href="sensors.lut">sensors.lut</a>  program that  makes extensive use  fo the
run statements.
</p>



<pre class="src src-lutin"><span style="color: #7F9F7F;">-- Simulate sensors that wears out</span>
<span style="color: #F0DFAF; font-weight: bold;">node</span> temp_simu_alt (T:<span style="color: #7CB8BB;">real</span>) <span style="color: #F0DFAF; font-weight: bold;">returns</span> (Ts:<span style="color: #7CB8BB;">real</span>) =
  <span style="color: #F0DFAF; font-weight: bold;">exist</span> eps : <span style="color: #7CB8BB;">real</span> [-0.1;0.1] <span style="color: #F0DFAF; font-weight: bold;">in</span>
      Ts = T + eps
   <span style="color: #DCDCCC; font-weight: bold;">fby</span>
      <span style="color: #DCDCCC; font-weight: bold;">loop</span> { 
          |10: Ts = T + eps     <span style="color: #7F9F7F;">-- working    </span>
          |1:  <span style="color: #DCDCCC; font-weight: bold;">loop</span> Ts = <span style="color: #BFEBBF;">pre</span> Ts <span style="color: #7F9F7F;">-- not working    </span>
        }

<span style="color: #F0DFAF; font-weight: bold;">node</span> temp_simu(T:<span style="color: #7CB8BB;">real</span>) <span style="color: #F0DFAF; font-weight: bold;"