{"id":357,"date":"2007-03-22T20:36:04","date_gmt":"2007-03-22T20:36:04","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2007\/03\/22\/building-things-in-calculus-storage-cells\/"},"modified":"2007-03-22T20:36:04","modified_gmt":"2007-03-22T20:36:04","slug":"building-things-in-calculus-storage-cells","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2007\/03\/22\/building-things-in-calculus-storage-cells\/","title":{"rendered":"Building Things in &#960;-Calculus: Storage Cells"},"content":{"rendered":"<p> As a refresher for me, and to give some examples to help you guys understand it, I&#8217;m going to go through a couple of examples of interesting things you can build with &pi;-calculus. We&#8217;ll start with a simple way of building mutable storage.<\/p>\n<p><!--more--><\/p>\n<p> Before getting started, I&#8217;m going to be annoying and change the syntax a bit. Typeset &pi;-calculus uses a channel name by itself for &#8220;receive message on channel&#8221;, and the channel name with an overbar for &#8220;send message on channel&#8221;. In yesterdays post, I used the unmarked channel name for receive, and added a &#8220;*&#8221; prefix for send. Playing with that, I find it very hard to read. So I&#8217;m going to start using a prefix for all uses of channel names: &#8220;?&#8221; for receive, and &#8220;!&#8221; for send.<\/p>\n<p> First,we&#8217;ll try to build a storage cell. A storage cell is a process that can store a value; and it provides channels that you can use to either receive a copy of its current value, or send it a new value.<\/p>\n<p> Let&#8217;s start with a very naive attempt at a cell.<\/p>\n<pre>\nCell[val]= (&nu;read,write)( !read(val).Cell[val]\n+ ?write(v).Cell[v])\n<\/pre>\n<p> What that says is: &#8220;Cell&#8221; is a shorthand for a process expression which is parametric in a value, called &#8220;$val&#8221;. Cell introduces its own read and write channels, and then either allows a client to read (which means cell <em>writes<\/em> to the channel that others might want to read from), followed by recursively invoking &#8220;Cell&#8221; on the same value; or it allows a client to update the value by writing a value to its &#8220;write&#8221; channel (in which case Cell <em>reads<\/em> from the channel and then recursively invokes cell with the new value.)<\/p>\n<p> So this is a simple updatable storage cell. Almost.<\/p>\n<p> What&#8217;s wrong with it? A couple of things about the channel names:<\/p>\n<ol>\n<li> The (&nu;read,write) creates two channels, <em>local to the Cell process<\/em>. That<br \/>\nmeans that <em>no one outside of the cell process can see them!<\/em> They&#8217;re initially scoped<br \/>\nto Cell, and there&#8217;s no method inside of Cell to send the channel name outside of its<br \/>\nname-scope. So it&#8217;s a storage cell, but no one can ever actually read it or write to it.<\/li>\n<li> The &nu; that creates the channel is <em>inside<\/em> of the part that is invoked<br \/>\nrecursively. So each time that &#8220;Cell&#8221; is invoked, it <em>creates new channel names<\/em>. <\/li>\n<\/ol>\n<p> So, how can we fix that? Let&#8217;s start by fixing the second problem, and make it always use the same names. We&#8217;ll split it into two different definitions: one which creates a new cell (and allocates its channel names), and one which takes the channels that it should use or read and write.<\/p>\n<pre>\nNewCell[val]=(&nu;read,write)Cell[read,write,val]\nCell[read,write,val]=  !read(val).Cell[read,write,val]\n+ ?write(v).Cell[read,write,v])\n<\/pre>\n<p> Now we&#8217;ve got a cell which creates its channels once, and then just keeps reusing the same channels. That&#8217;s a good start. Now, the remaining problem is that we don&#8217;t have any way to tell a client what the names o the channels are. To fix that, we&#8217;ll just add a parameter to <code>NewCell<\/code>, which is the name of a channel where we should send the names of the read and write channels.<\/p>\n<pre>\nNewCell[creator,initval]=(&nu;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> So now, we&#8217;re in good shape. We can create a storage cell. When we create it, it gets one set of channel names for reading and writing, and it sends those back to whoever created the cell, so that they&#8217;ll be able to read from and write to it.<\/p>\n<p> How would we use it? Let&#8217;s just throw together a bit of code.<\/p>\n<pre>\n(&nu; me)( NewCell[me,0]\n| ?me(r,w).!w(7).?r(x).!out(x) )\n<\/pre>\n<p> This creates a new cell; receives a message from the cell containing its read and write channels; then it sets the cell contents to 7; reads the cell contents into a variable &#8220;x&#8221;,<br \/>\nand sends the contents it just read to an output channel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a refresher for me, and to give some examples to help you guys understand it, I&#8217;m going to go through a couple of examples of interesting things you can build with &pi;-calculus. We&#8217;ll start with a simple way of building mutable storage.<\/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-357","post","type-post","status-publish","format-standard","hentry","category-pi-calculus"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-5L","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/357","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=357"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/357\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}