{"id":52,"date":"2005-06-28T18:01:49","date_gmt":"2005-06-28T08:01:49","guid":{"rendered":"http:\/\/www.somethinkodd.com\/oddthinking\/?p=52"},"modified":"2009-09-12T05:22:20","modified_gmt":"2009-09-11T19:22:20","slug":"disgusting-code","status":"publish","type":"post","link":"https:\/\/www.somethinkodd.com\/oddthinking\/2005\/06\/28\/disgusting-code\/","title":{"rendered":"Disgusting Code"},"content":{"rendered":"<h3>Aside on Church Numerals<\/h3>\n<p>Let me start with an aside.<\/p>\n<p>Functional programmer nerds tend to play happily with ideas that either revolutionise the way you look at the rest of programming or leave your brain so far underwater, a mere snorkel won&#8217;t do &#8211; SCUBA equipment is required.<\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Church_numeral\" title=\"Wikipedia definition of Church_numeral\" class=\"wikipedia\">Church Numerals<\/a> are an example of one of those ideas applied to the whole of mathematics.<\/p>\n<p>It throws away the whole concept of integers, and replaces them with functions &#8211; functions that themselves take another function as a parameters, and apply those parameters to themselves, over and over. <\/p>\n<p>I won&#8217;t try to explain them in detail here. If you don&#8217;t know what they are, ask a functional programming nerd one day when you&#8217;re next to a whiteboard, and when you have plenty of time available afterwards for your brain to slowly rise up from the depths to avoid the bends.<\/p>\n<h3>Aside on the Language<\/h3>\n<p>I was once paid to work on a compiler for a small functional programming language.<\/p>\n<p>The functional language was cute and very small. I noticed once that the following code was valid:<\/p>\n<p><code><br \/>\n    where where where where where where where where = @where<br \/>\n<\/code><\/p>\n<p>For the record, it compiled perfectly, first time, and resulting code returned the identity function. I showed it to my supervisor, and he made me add <em>extra code<\/em> to make &#8220;where&#8221; a reserved word, and prevent this from compiling!<\/p>\n<h3>The Challenge: The Oddness of Two<\/h3>\n<p>I set out one week to implement the most complex program ever written in in this little language: a tool to determine whether two was odd or even!<\/p>\n<p>Sure it sounds trivial; you might implement it in a typical language as:<\/p>\n<p><code><br \/>\n    isEven(x) = 2*floor(x\/2) == x<br \/>\n<\/code><\/p>\n<p>or<\/p>\n<p><code><br \/>\n    isEven(x) = x % 2 == 0<br \/>\n<\/code><\/p>\n<p>or even, as Bream Le Fish once suggested,<\/p>\n<p><code><br \/>\n    isEven(x) = (-1)^x == 1<br \/>\n<\/code><\/p>\n<p>However, this language had no fancy-pants features like exponentiation, modulo or floor functions. For that matter, it didn&#8217;t have any built-in types or integer literals like &#8220;2&#8221;.<\/p>\n<p>I was confident enough to swim at the deep-end of the lambda-calculus swimming pool back then, so that was not an issue. Church Numerals would provide the concepts of integers, dual-recursion between the isOdd and isEven functions would provide the definitions of odd and even.<\/p>\n<h3>A Stumbling Block<\/h3>\n<p>I was surprised to find that I couldn&#8217;t get the Church Numerals to work. My compiler (perhaps &#8220;instrumenter&#8221; is a more accurate name) accepted the source, and correctly generated <a href=\"http:\/\/en.wikipedia.org\/wiki\/Miranda_programming_language\" title=\"Wikipedia definition of Miranda_programming_language\" class=\"wikipedia\">Miranda<\/a> code, without bothering to specify the typing information. That was normal. Miranda has a type-inference compiler. It looks at the way a variable is used, and determines the appropriate type based on its usage.<\/p>\n<p>The Miranda type-inference engine, however, was balking at my generated code. It was complaining that this Church Numeral thing was a function that took a single parameter which was a function that took a single parameter, which was a function that took a single&#8230; well you get the idea. This infinite recursion had it confused &#8211; not unreasonably, I might add. I don&#8217;t know many languages whose typing systems <em>would<\/em> accept it.<\/p>\n<p>I played with it for a while, trying different approaches, but I was stuck. I explained to my supervisor my problem, and explained it coudn&#8217;t be done.<\/p>\n<p>&#8220;Hmmm,&#8221; he said, &#8220;I seem to remember something about this being mentioned at a conference I was at with <a href=\"http:\/\/en.wikipedia.org\/wiki\/David_Turner\" title=\"Wikipedia definition of David_Turner\" class=\"wikipedia\">David Turner<\/a>.&#8221; I blinked at the name. David Turner invented Miranda, and was a semi-legendary creature to me. &#8220;Why don&#8217;t you email him and ask him.&#8221;<\/p>\n<p>That was the first time I had ever written to a real-live language designer before &#8211; I was a little nervous. I was impressed to get a quick response, which explained that this was known issue &#8211; I was trying to push the typing system further than it was meant to go. However, there was a work-around. It was an undocumented pragma which, if put at the very, very top of a Miranda program, would switch off the type-inference system. Effectively, that turned Miranda from a strictly typed system (with type-inference to hide that fact) to a loosely-typed system.<\/p>\n<p>Professor Turner was very polite, but I think I could guess what his opinion of this sort of behaviour was.  My big clue was the name of the pragma: &#8220;%disgusting&#8221;.<\/p>\n<p>For the rest of the project, all of my generated code was required to have, at the very, very top, a declaration that this code was, in fact, disgusting!<\/p>\n<p>I meekly accepted my fate, and as a result, I can categorically confirm to you a long suspected result. After only half-a-page of debug output, the result was crystal clear. Two is an <em>even<\/em> number!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An anecdote about Church Numerals, Miranda and the way language designers make their opinions known.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[23,28,35,34],"tags":[106,127,69],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-based-on-a-true-story","category-doubleplus-geek","category-heroic-failures","category-software-development","tag-anecdote","tag-miranda","tag-software"],"_links":{"self":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":1,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":1049,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/52\/revisions\/1049"}],"wp:attachment":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}