You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
down the channel <code>out</code>. The type <code>%>int</code> is an output channel endpoint
427
427
for <code>int</code>.
428
428
</p><preclass='inclusion'>
429
-
examples/ex1.flx</pre>
429
+
examples/corout/ex1.flx</pre>
430
430
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a procedure, a function with side-effects not returning a value">proc</span> source (out: %><spanclass="library" title="binding of C int type">int</span>) () {
431
431
<spanclass="lineno" id=line2></span><spanclass="big_keyword" title="Define a mutable variable">var</span> i = 0;
</pre></p></div><h2id='The_sink_h'><imgsrc='/share/src/web/images/minus.gif' id='The sink' onclick='toggle(this,"The_sink_d")' alt='+'/> 1.2 The sink</h2><divid='The_sink_d' style='display:block'>
438
438
<p>Now we have a simple sink, which just prints everything it reads:
439
439
</p><preclass='inclusion'>
440
-
examples/ex1.flx</pre>
440
+
examples/corout/ex1.flx</pre>
441
441
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a procedure, a function with side-effects not returning a value">proc</span> sink (inp: %<<spanclass="library" title="binding of C int type">int</span>) () {
</p></div><h2id='The_transducer_h'><imgsrc='/share/src/web/images/minus.gif' id='The transducer' onclick='toggle(this,"The_transducer_d")' alt='+'/> 1.3 The transducer</h2><divid='The_transducer_d' style='display:block'>
452
452
<p>Now we have a transducer which writes the square of every integer it reads.
453
453
</p><preclass='inclusion'>
454
-
examples/ex1.flx</pre>
454
+
examples/corout/ex1.flx</pre>
455
455
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a procedure, a function with side-effects not returning a value">proc</span> squarer (inp: %<<spanclass="library" title="binding of C int type">int</span>, out: %><spanclass="library" title="binding of C int type">int</span>) () {
<p>You should notice the previous three components were all infinite loops.
463
463
Since we don't want our demo to run forever we will throw in a limiter chip:
464
464
</p><preclass='inclusion'>
465
-
examples/ex1.flx</pre>
465
+
examples/corout/ex1.flx</pre>
466
466
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a procedure, a function with side-effects not returning a value">proc</span> limiter (<spanclass="big_keyword" title="Define a mutable variable">var</span> limit: <spanclass="library" title="binding of C int type">int</span>) (inp: %<<spanclass="library" title="binding of C int type">int</span>, out: %><spanclass="library" title="binding of C int type">int</span>) () {
</p></div><h2id='The_runner_h'><imgsrc='/share/src/web/images/minus.gif' id='The runner' onclick='toggle(this,"The_runner_d")' alt='+'/> 1.5 The runner</h2><divid='The_runner_d' style='display:block'>
477
477
<p>Now we need have four components so we need three channels to connect them:
478
478
</p><preclass='inclusion'>
479
-
examples/ex1.flx</pre>
479
+
examples/corout/ex1.flx</pre>
480
480
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a procedure, a function with side-effects not returning a value">proc</span> run_pipeline() {
481
481
<spanclass="lineno" id=line2></span><spanclass="comment">// make channels</span>
482
482
<spanclass="lineno" id=line3></span><spanclass="big_keyword" title="Define a mutable variable">var</span> i1,o1 = mk_ioschannel_pair[<spanclass="library" title="binding of C int type">int</span>]();
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="defines a coroutine using chip idiom">chip</span> source
512
512
<spanclass="lineno" id=line2></span><spanclass="small_keyword" title="the parameter of a chip">connector</span> io
513
513
<spanclass="lineno" id=line3></span><spanclass="small_keyword" title="a field of the chip parameter">pin</span> out: %><spanclass="library" title="binding of C int type">int</span>
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="defines a coroutine using chip idiom">chip</span> sink
529
529
<spanclass="lineno" id=line2></span><spanclass="small_keyword" title="the parameter of a chip">connector</span> io
530
530
<spanclass="lineno" id=line3></span><spanclass="small_keyword" title="a field of the chip parameter">pin</span> inp: %<<spanclass="library" title="binding of C int type">int</span>
</pre></p><p>Now here is the transducer and limiter
538
538
</p><preclass='inclusion'>
539
-
examples/ex2.flx</pre>
539
+
examples/corout/ex2.flx</pre>
540
540
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="defines a coroutine using chip idiom">chip</span> squarer
541
541
<spanclass="lineno" id=line2></span><spanclass="small_keyword" title="the parameter of a chip">connector</span> io
542
542
<spanclass="lineno" id=line3></span><spanclass="small_keyword" title="a field of the chip parameter">pin</span> inp: %<<spanclass="library" title="binding of C int type">int</span>
</pre></p><p>Now to connect these things we say this:
562
562
</p><preclass='inclusion'>
563
-
examples/ex2.flx</pre>
563
+
examples/corout/ex2.flx</pre>
564
564
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a procedure, a function with side-effects not returning a value">proc</span> run_pipeline1() {
565
565
<spanclass="lineno" id=line2></span><spanclass="small_keyword" title="a synonym for var to suit the chip idiom">device</span> limit8 = limiter 8;
566
566
<spanclass="lineno" id=line3></span><spanclass="small_keyword" title="defines the topology of chip connections">circuit</span>
</p><p>The <code>circuit</code> feature is very general. For pipelines there is an even easer
580
580
syntax:
581
581
</p><preclass='inclusion'>
582
-
examples/ex2.flx</pre>
582
+
examples/corout/ex2.flx</pre>
583
583
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a procedure, a function with side-effects not returning a value">proc</span> run_pipeline2() {
</p><p>However we will demonstrate a simpler service, namely the system alarm closk
619
619
because it has a really simply API.
620
620
</p><preclass='inclusion'>
621
-
examples/ex3.flx</pre>
621
+
examples/corout/ex3.flx</pre>
622
622
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="library" title="Print a string to standard output with newline appended">println</span>$ <spanclass="fstring">"Begin Spawning"</span>;
623
623
<spanclass="lineno" id=line2></span>
624
624
<spanclass="lineno" id=line3></span><spanclass="big_keyword" title="Spawn a cooperative fibre">spawn_fthread</span> {
</p></div></div><h1id='No_Deadlocks_h'><imgsrc='/share/src/web/images/minus.gif' id='No Deadlocks' onclick='toggle(this,"No_Deadlocks_d")' alt='+'/> 5 No Deadlocks</h1><divid='No_Deadlocks_d' style='display:block'>
740
740
<p>Most threading systems can deadlock. Here is the classical situation:
741
741
</p><preclass='inclusion'>
742
-
examples/ex5.flx</pre>
742
+
examples/corout/ex5.flx</pre>
743
743
<p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="defines a coroutine using chip idiom">chip</span> D <spanclass="small_keyword" title="the parameter of a chip">connector</span> io <spanclass="small_keyword" title="a field of the chip parameter">pin</span> inp: %<<spanclass="library" title="binding of C int type">int</span><spanclass="small_keyword" title="a field of the chip parameter">pin</span> out: %><spanclass="library" title="binding of C int type">int</span>
<spanclass="lineno" id=line2></span><spanclass="big_keyword" title="Define a function with no side-effects">fun</span> g(y:<spanclass="library" title="binding of C long type">long</span>)=> x.<spanclass="library" title="binding of C long type">long</span> + y;
</pre></p><p>The function <code>f</code> here has type {int -> (long -> long)}. This type is
547
-
the same as {int -> long -> long} since the arrow operator is right
546
+
</pre></p><p>The function <code>f</code> here has type <code>int -> (long -> long)</code>. This type is
547
+
the same as <code>int -> long -> long</code> since the arrow operator is right
548
548
associative. There's a simpler way to write the function <code>f</code>:
549
549
</p><p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a function with no side-effects">fun</span> f(x:<spanclass="library" title="binding of C int type">int</span>) (y:<spanclass="library" title="binding of C long type">long</span>) => x.<spanclass="library" title="binding of C long type">long</span> + y;
550
550
</pre></p><p>Here one says <code>f</code> has arity 2. Note it still has only
</p><p><preclass='flxbg'><spanclass="lineno" id=line1></span><spanclass="big_keyword" title="Define a primitive type by binding to a C type">type</span> vector[T] = <spanclass="fstring">"::std::vector<?1>"</span>;
408
408
<spanclass="lineno" id=line2></span><spanclass="big_keyword" title="Define a procedure, a function with side-effects not returning a value">proc</span> push_back[T]: vector[T] * T = <spanclass="fstring">"$1.push_back($2)"</span>;
409
-
</pre></p><p>In the C code {?1}, {?2} represent the
409
+
</pre></p><p>In the C code <code>?1</code>, <code>?2</code> represent the
<spanclass="lineno" id=line2></span><spanclass="big_keyword" title="Define a function with no side-effects">fun</span> f: !integers * !integers -> <spanclass="library" title="binding of C int type">int</span> = <spanclass="fstring">"f($1, $2)"</span><spanclass="big_keyword" title="specify requirements">requires</span> myf;
445
445
</pre></p><p>The reason this is <em>so</em> useful is that if we just lifted the
446
446
signature of <code>f</code> from C, we'd be ignoring the fact that the C programmer
447
-
has automatic conversions and can call {f(42L, 23u)}, which would
447
+
has automatic conversions and can call <code>f(42L, 23u)</code>, which would
448
448
fail in Felix because it has no automatic conversions. The user
449
449
would have to cast every argument to exactly the right type which is
450
450
not practical. By using constrained polymorphism, we can make
<spanclass="lineno" id=line2></span><spanclass="library" title="Print a string to standard output">print</span>$ <spanclass="fstring">"hello how are you"</span>.[4]; endl;
0 commit comments