{"id":360,"date":"2007-03-24T17:50:46","date_gmt":"2007-03-24T17:50:46","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2007\/03\/24\/more-calculus-games\/"},"modified":"2007-03-24T17:50:46","modified_gmt":"2007-03-24T17:50:46","slug":"more-calculus-games","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2007\/03\/24\/more-calculus-games\/","title":{"rendered":"More &#960;-Calculus Games"},"content":{"rendered":"<p> Ok. So I&#8217;m still tweaking syntax, to try to find a way of writing &pi;-calculus in a way that&#8217;s<br \/>\neasy for me to write in my editor, and for you to read in your browser. Here&#8217;s the latest version:<\/p>\n<ol>\n<li> Sequential Composition: <code><em>Process<sub>1<\/sub><\/em>.<em>Process<sub>2<\/sub><\/em><\/code>.<\/li>\n<li> Send expressions: <code>!channel(tuple).<em>Process<\/em><\/code><\/li>\n<li> Receive expressions: <code>?channel(tuple).<em>Process<\/em><\/code><\/li>\n<li> New channel expression <code>new(name,...) { <em>Process<\/em> }<\/code><\/li>\n<li> Process duplication expression: <code>*(<em>Process<\/em>)<\/code><\/li>\n<li> Parallel composition: <code><em>Process<sub>1<\/sub><\/em> | <em>Process<sub>2<\/sub><\/em><\/code>.<\/li>\n<li> Choice composition: <code><em>Process<sub>1<\/sub><\/em> + <em>Process<sub>2<\/sub><\/em><\/code>.<\/li>\n<li> Null process: <code>&empty;<\/code><\/li>\n<\/ol>\n<p> So, in this syntax, the final version of the storage cell from yesterdays post<br \/>\nis:<\/p>\n<pre>\nNewCell[creator,initval]=new(read,write){ (Cell[read,write,initval]\n| !creator(read,write)) }\nCell[read,write,val]=( !read(val).Cell[read,write,val]\n+ ?write(v).Cell[read,write,v])\n<\/pre>\n<p> Now, let&#8217;s try to use &pi;-calculus to do something that actually involves real concurrency.<\/p>\n<p><!--more--><\/p>\n<p>One thing that comes up often is the need for some kind of synchronization. You create N different worker processes, and then you need to wait until all N of them are done, and then you can go continue. One little cheat that I&#8217;m going to use is tossing in an if\/then control<br \/>\nstructure, and I&#8217;m going to use integers. (There&#8217;s a way to do both of those in &pi;-calculus, which I&#8217;ll get to later, but for now, let&#8217;s just take them as a given.)<\/p>\n<pre>\nSync[numProc,p]=new(s){!p(s).WaitFor[numProc,s].!p(done).&empty; }\nWaitFor[numProc,s]=if(numProc=0) then\n?s(done).WaitFor[numProc-1,s].\n&empty;.\n<\/pre>\n<p> Ok. So, to make that work, we needed some numbers. How can do we numbers in &pi;-calculus? We&#8217;re going to borrow the basic idea of Church numerals from &lambda; calculus. Remember in lambda calculus, we do numbers using lambda expressions:<\/p>\n<p>&lt;ul<\/p>\n<li> 0=(&amp;lamba;i z.z)<\/li>\n<li>1=(&lambda;i z.i z)<\/li>\n<li>2=(&lambda;i z.i i z)<\/li>\n<li> .. and so on.\n<\/ul>\n<p> The &lambda; calculus equivalent is:<\/p>\n<ul>\n<li>\t 0=<code>?in(p,i,z){!z(p).&empty;}<\/code><\/li>\n<li> 1=<code>?in(p,i,z){!i(p).!z(p).&empty;}<\/code><\/li>\n<li> 2=<code>?in(p,i,z){!i(p).!i(p).!z(p).&empty;}<\/code><\/li>\n<li> &#8230;<\/li>\n<\/ul>\n<p> A number N is a process that that takes two channel names &#8220;i&#8221; (for increment), and z (for zero), and one value called &#8220;p&#8221;. It\tthen sends &#8220;p&#8221; to &#8220;i&#8221; N times, followed by sending p to &#8220;z&#8221; once to say that it&#8217;s done.<\/p>\n<p> Ok. So what&#8217;s an adder look like?<\/p>\n<p> What it should two is take two &pi;-numbers X (which wound send X messages on i, and one on z),<br \/>\nand Y (which would send Y messages on i, and one on z), and produce a process which will X+Y messages on i, followed by only one on z.<\/p>\n<p><code><br \/>\nAdder[x, y, i, z, p]=new(yin) { !x(p, i, yin).&empty;<br \/>\n| ?yin(q).!y(p,i,z) }<br \/>\n<\/code><\/p>\n<p> &#8220;x&#8221; and &#8220;y&#8221; are the channel names for the two numbers being<br \/>\nadded. &#8220;i&#8221; is the &#8220;increment&#8221; value, and &#8220;z&#8221; is the zero value; and p is the value that the<br \/>\nmessages need to be sent to. So, what the adder does is first send the message to trigger &#8220;x&#8221;, but<br \/>\ninstead of using the real &#8220;z&#8221; we created a new channel and used it for z; and in parallel, we have<br \/>\na process waiting to receive the &#8220;z&#8221;-message, and then trigger &#8220;y&#8221;. That&#8217;s in.<\/p>\n<p> How about a subtracter? To keep things easy, we&#8217;ll assume that when we do &#8220;x-y&#8221;, that &#8220;x&gt;=y&#8221;. So then, what we&#8217;ll want to do is &#8220;eat&#8221; the first &#8220;y&#8221; p&#8217;s send by &#8220;x&#8221;:<\/p>\n<p><em>(The following was originally very screwed up, due to a stupid editing error. I write my posts in an editor called TextMate, and then paste them into MoveableType for posting. I pasted an incomplete copy to check the MT preview, then went back to Textmate, finished the post, and posted the version that was in MT&#8230; Stupid, stupid, stupid! Sorry about that! In addition, there were some inconsistencies in the use of &#8220;[]&#8221;s versus &#8220;()&#8221;s, as pointed out in the comments. That had nothing to do with editing errors, just with my own inconsistency. I think those have all been fixed.)<\/em><\/p>\n<pre>\nSub[x,y,i,z,p]= new(ix, iy, zx, zy) {\n!x(ix, zx, p) | !y(iy, zy)\n|  ConsumeIsWhileY[ix, zx, iy, zy]\n}\nConsumeIsWhileY[ix, zx, iy, zy, x, z] =\n?iy(p).?ix(p).ConsumeIsWhileY[ix, zx, iy, zx, x, z]\n+ ?zy(p).PassXsWhileX[ix, zx, x, z]\nPassXsWhileX[ix, zx, x, z]\t=\n?ix(p).!x(p).PassXsWhileX[ix, zx, x, z]\n+  ?zx(p).!z(p).&empty;\n<\/pre>\n<p> In any real programming, we&#8217;re obviously not going to use these &pi;-church numerals. But it&#8217;s a useful exercise to understand how to write &pi;-calculus processes for doing simple some simple controlled iteration.<\/p>\n<p> Now, as an exercise, if you&#8217;re so inclines: the implementation of subtract is very similar to how you&#8217;d implement a less-than-or-equal comparison. What would less-than-or-equal look like in  &pi;?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ok. So I&#8217;m still tweaking syntax, to try to find a way of writing &pi;-calculus in a way that&#8217;s easy for me to write in my editor, and for you to read in your browser. Here&#8217;s the latest version: Sequential Composition: Process1.Process2. Send expressions: !channel(tuple).Process Receive expressions: ?channel(tuple).Process New channel expression new(name,&#8230;) { Process } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[50],"tags":[],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-pi-calculus"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-5O","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}