{"id":2998,"date":"2013-11-23T15:15:24","date_gmt":"2013-11-23T21:15:24","guid":{"rendered":"http:\/\/augerhandle.net\/blogs\/jumpingfish\/?p=2998"},"modified":"2013-11-23T15:50:36","modified_gmt":"2013-11-23T21:50:36","slug":"unity-and-nothingness","status":"publish","type":"post","link":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/2013\/11\/23\/unity-and-nothingness\/","title":{"rendered":"Unit and Nothingness"},"content":{"rendered":"<p>On a different note&#8230;<\/p>\n<h3>1. On Procedures and Functions<\/h3>\n<p>In FORTRAN and Pascal and Ada, programming languages that I revelled in long ago, there is a distinction drawn between so-called <em>procedures<\/em> on the one hand and <em>functions<\/em> on the other. Both are software constructs for doing some work, but whereas a function calculates a tangible result and gives it back to the caller, a procedure does no such thing, performing instead some background operation on behalf of the caller (for instance printing some text to the screen or saving some data in a database).\u00a0<\/p>\n<p>So in Ada you might write<\/p>\n<pre>function Translate( english_word : String ) return String is\nbegin\n\t...translation algorithm here...\n\treturn swahili_word;\nend\n<\/pre>\n<p>and<\/p>\n<pre>procedure Display( sentence : String ) is\nbegin\n\t...output algorithm here...\nend\n<\/pre>\n<p>In a sense, functions produce stuff that you can take with you whereas procedures just do stuff.<\/p>\n<h3>2. Unifying Them Both<\/h3>\n<p>In contrast to this, the languages C, C++, C# and Java have no such distinction. They only have functions.<\/p>\n<p>Well, that&#8217;s not quite right, because of course there&#8217;s still a need for algorithms that do stuff. And so in place of procedures, these languages define so-called <em>void functions<\/em>. They produce nothing whence <em>void<\/em>.<\/p>\n<p>In a hypothetical language might this could take the following form.<\/p>\n<pre>function Display( sentence : String ) return void is\nbegin\n\t...output algorithm here...\nend\n<\/pre>\n<p>To an engineer encountering this stuff for the first time, this notion of <em>void functions<\/em> is entertaining. It&#8217;s novel to reflect on the notion that <em>the void<\/em> has entered into an otherwise dry world of physics and equations of motion and all that.<\/p>\n<p>But it&#8217;s not a confusing concept, so after a few minutes the novelty fades, and you proceed as you always have, writing some functions that\u00a0produce something and other <span style=\"text-decoration: line-through;\">procedures<\/span> functions that don&#8217;t.<\/p>\n<h3>3. <code>void<\/code> Isn&#8217;t a Type<\/h3>\n<p>Now, the languages mentioned above are examples of so-called <em>statically typed languages<\/em>. That is to say, every algorithm expressed in these languages manipulates <em>things<\/em> that have a so-called <em>type<\/em>\u00a0(String, Integer, Character, &#8230;), and these types are explicitly specified by the programmer. So\u00a0you might wonder, <em>What is this type called <code>void<\/code>?<\/em><\/p>\n<p>Well, I am not a programming expert, but my understanding is that in fact in these cases, <code>void<\/code> isn&#8217;t really a type per se. It&#8217;s just a hint to the computer that this particular function actually doesn&#8217;t return anything.<\/p>\n<p>Hm&#8230; It&#8217;s a hint that this function isn&#8217;t producing anything; that it&#8217;s only doing something. In other words, <code>void<\/code> is just an annotation that says, <em>Hah! Fooled you. This isn&#8217;t really a function at all; it&#8217;s a procedure.<\/em><\/p>\n<h3>4. Introducing <code>Unit<\/code><\/h3>\n<p>This state of affairs changes a bit in the Scala programming language, a language built on the conceptual foundations of C and Java but one that goes in some fundamentally different directions.<\/p>\n<p>Scala generalizes the notion of <code>void<\/code> to a full-fledged type called <code>Unit<\/code>. So in Scala you might write something like this.<\/p>\n<pre>def Display( sentence : String ) : Unit =\u00a0\n{\n\t...output algorithm here...\n}\n<\/pre>\n<p>Or using the Ada-like pseudocode we&#8217;ve used above,<\/p>\n<pre>function Display( sentence : String ) return Unit is\nbegin\n\t...output algorithm here...\nend\n<\/pre>\n<p>The only real difference here is the presence of <code>Unit<\/code> instead of <code>void<\/code>. But in Scala, this is no minor detail. Whereas the <code>void<\/code> of C is a kind of hint, Scala&#8217;s <code>Unit<\/code> is a bonafide type.<\/p>\n<h3>5. A Tangent on the Notion of <code>Unit<\/code><\/h3>\n<p><code>Unit<\/code> made me do a double-take when I first saw it. After all, using <code>Unit<\/code> to represent &#8220;nothing&#8221; sounds perilously close to the heresy of claiming that <code>1=0<\/code>. It turns out that I&#8217;ve run into this heresy before.\u00a0<\/p>\n<p>Once upon a time I was working with a bunch of rocket scientists. We were building a new space transportation system that was going to take us to orbit, take us to the moon and eventually take us to Mars. One of the projects I was working on involved designing a schema for representing complex datasets in a way that future engineers and software developers could understand long after the original authors were gone. (This system was being designed for the long-haul, an irony given that the project was eventually cancelled.)<\/p>\n<p>I worked with a couple computer science-y, math-y guys from California. They were bright, and they were determined to apply their dreams of ontologies to the problem I was working on. One day we were discussing how to specify &#8230; nothing &#8212; how to formally indicate that there was <em>nothing there there<\/em>.<\/p>\n<p>&#8220;Can we call this <em>Unit<\/em>?&#8221; Oliver asked.<\/p>\n<p>I was puzzled. Why would we call nothing one?<\/p>\n<p>See the connection, here?<\/p>\n<h3>6. Another Tangent<\/h3>\n<p>I&#8217;m not a computer scientist. Indeed, in an interview question long ago, I was asked some computer science-y, algorithm-theoretical question to which said, &#8220;I confess I&#8217;m not strong in &#8230;&#8221; to which the questioner responded, &#8220;Aren&#8217;t all computer scientists strong in &#8230;?&#8221; to which I reminded him that my background was in engineering.<\/p>\n<p>Neither am I a mathematician. But let me point out something about <a href=\"http:\/\/en.wikipedia.org\/wiki\/Monoid\">Monoids<\/a>. In abstract algebra, a monoid is a pair (<code>S<\/code>,<code>op<\/code>) where <code>S<\/code> is some set and <code>op<\/code> is a binary operation (function) on that set that returns elements of the set. There are some axioms surrounding <code>S<\/code> and <code>op<\/code>, namely the axioms of <em>closure<\/em>, <em>associativity<\/em> and <em>identity<\/em>.<\/p>\n<p>It&#8217;s the notion of <em>identity<\/em> that is relevant, here. The identity axiom simply stipulates that there is some element in the set <code>S<\/code>, let&#8217;s call it <code>I<\/code>, for which <code>(I op x) = x<\/code> for all elements <code>x<\/code> in <code>S<\/code>.<\/p>\n<p>Hmm&#8230; This is a bit abstract. But wait, don&#8217;t leave.\u00a0<\/p>\n<p>Let&#8217;s consider the case where <code>op<\/code> is the binary operation of adding two numbers together. In this case, <code>I<\/code> is <code>0<\/code> (zero), since any number plus zero gives you the original number.<\/p>\n<p>Now let&#8217;s consider the case where <code>op<\/code> is the binary operation of multiplying two numbers together. Here, <code>I<\/code> is <code>1<\/code> (one), since any number times one gives you the original number.<\/p>\n<p>And are you seeing my point, here? When those ontology guys asked me if it was ok to use the term &#8220;unit&#8221; to refer to &#8220;nothing,&#8221; it must have had something to do with monoids. <code>Unit<\/code> must be related to some theoretical notion of <em>identity<\/em>, and in the context of multiplication, the notion of identity is indeed&#8230; one. Hence the term <em>unit<\/em>?<\/p>\n<p>Anyway, let&#8217;s return to Scala&#8217;s type system&#8230;<\/p>\n<h3>7. <code>Unit<\/code> and <code>Nothing<\/code><\/h3>\n<p>So Scala has this thing called <code>Unit<\/code>. It&#8217;s a type in the language used in situations where a function actually returns nothing.<\/p>\n<p>In some sense <em><code>Unit<\/code> means nothing<\/em>. Personally, I would have chosen <code>Nothing<\/code> rather than <code>Unit<\/code> as way to say &#8230;you know&#8230; &#8220;nothing&#8221;. But it turns out that the type <code>Nothing<\/code> is used in Scala for something else.<\/p>\n<p><code>Nothing<\/code> is one of the so-called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bottom_type\"><em>bottom types<\/em><\/a> in the language. It is a subtype of every other type in the language. And rather than denoting &#8220;nothing&#8221; as the term suggests, <code>Nothing<\/code> actually denotes &#8220;never&#8221;. When a function is intentionally designed <em>never to return<\/em> you specify its return type as <code>Nothing<\/code>. Personally, I would have chosen &#8230;you know&#8230; <code>Never<\/code> to capture this. But remember, I am not a computer scientist.<\/p>\n<p>Using the same pseudocode as above, you might write such a function as follows.<\/p>\n<pre>function Event_Loop() returns Nothing is\nbegin\n\twhile(true) begin\n\t\t...event processing here...\n\tend\nend\n<\/pre>\n<p>You can see from that never-terminating loop that the function never returns, it just loops around again and again, processing events <em>ad infinitum<\/em>. (You have to admit, <code>return Never<\/code> would have fit this situation really well.)<\/p>\n<p>Admittedly, all this <em>one is nothing<\/em> and <em>nothing is never<\/em> is&#8230; well, confusing. But frankly, once you get passed this minor hurdle, the details of the Scala type system are frankly stunning.<\/p>\n<p>But that, dear reader, is a topic for another day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On a different note&#8230; 1. On Procedures and Functions In FORTRAN and Pascal and Ada, programming languages that I revelled in long ago, there is a distinction drawn between so-called procedures on the one hand and functions on the other. Both are software constructs for doing some work, but whereas a function calculates a tangible [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[18],"_links":{"self":[{"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/posts\/2998"}],"collection":[{"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/comments?post=2998"}],"version-history":[{"count":3,"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/posts\/2998\/revisions"}],"predecessor-version":[{"id":3001,"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/posts\/2998\/revisions\/3001"}],"wp:attachment":[{"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/media?parent=2998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/categories?post=2998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augerhandle.net\/blogs\/jumpingfish\/wp-json\/wp\/v2\/tags?post=2998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}