{"id":802,"date":"2009-09-06T18:08:21","date_gmt":"2009-09-06T18:08:21","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2009\/09\/06\/the-one-the-only-brainfck\/"},"modified":"2020-02-24T09:12:05","modified_gmt":"2020-02-24T14:12:05","slug":"the-one-the-only-brainfck","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2009\/09\/06\/the-one-the-only-brainfck\/","title":{"rendered":"The One&#8230; The Only&#8230; Brainf*ck"},"content":{"rendered":"<p><em>I&#8217;m currently away on a family vacation, and as soon as vacation is over, I&#8217;m off on a business trip for a week. And along the way, I&#8217;ve got some deadlines for my book. So to fill in, I&#8217;m recycling some old posts. I decided that it&#8217;s been entirely too long since there was any pathological programming &#8217;round these parts, so I&#8217;m going to repost some of my favorites.<\/em><\/p>\n<p>As long-time readers know by now, in real life, I&#8217;m <em>not<\/em> a mathematician; I&#8217;m a computer scientist. I&#8217;m still a math <em>geek<\/em>, mind you, but what I really do is very much in the realm of applied math, working on building systems to help people build programs.<\/p>\n<p>One of my pathological obsessions is programming languages. Since I first got exposed to TRS-80 Model 1 BASIC back in middle school, I&#8217;ve been absolutely\u00a0 nuts programming languages. Last time I counted, I&#8217;d learned about 150 different languages; and I&#8217;ve picked up more since then. I&#8217;ve written programs most of them. Like I said, I&#8217;m nuts.<\/p>\n<p>These pathological programming language posts are my way of inflicting my obsession on you in a (hopefully) amusing way. You see, in this screwed up world of ours, there are lots and lots of thoroughly <em>crazy <\/em>people out there. In the world of geekery, many of those crazy people like to invent programming languages. Some very small number of them try to design good languages and succeed; a much larger number try to design good languages and fail; and <em>then<\/em> there are ones whose work I&#8217;m writing about. The ones who deliberately set out to design strange, warped, twisted, and nearly unusable languages, and succeed brilliantly. Most of the people who design them call them &#8220;esoteric&#8221; programming languages. I call them evil.<\/p>\n<p>Today, the beautiful grand-daddy of the esoteric language family: the one, the only, the truly and deservedly infamous: <a href=\"http:\/\/www.muppetlabs.com\/~breadbox\/bf\/\">Brainfuck<\/a>, designed by Urban M\u00fcller. (There are a number of different implementations available; just follow the link.)<\/p>\n<p>Only 8 commands &#8211; including input and output &#8211; all written using symbols. And yet it&#8217;s Turing complete. In fact, it&#8217;s one-up on just being Turing complete &#8211; it&#8217;s actually been formally specified with a complete formal theoretical design, called <a href=\"http:\/\/en.wikipedia.org\/wiki\/P_prime_prime\">P&#8221;<\/a>. And it&#8217;s even been implemented <a href=\"http:\/\/www.robos.org\/?bfcomp\" data-wplink-edit=\"true\">in hardware!<\/a>.<\/p>\n<p><!--more--><\/p>\n<p>BrainFuck is based on something very much like a twisted cross between a<br \/><a href=\"http:\/\/scienceblogs.com\/goodmath\/2007\/02\/basics_the_turing_machine_with_1.php\">Turing machine<\/a> and a <a href=\"http:\/\/goodmath.blogspot.com\/2006\/05\/minsky-machine.html\">Minsky machine<\/a>: it&#8217;s got a tape, like a turing machine. But unlike the turing machine, each cell of the tape can store an arbitrary number, which can be incremented or decremented &#8212; like a Minsky machine&#8217;s registers. Also like a Minsky machine, it&#8217;s got a notion of control flow based on zero-tests.<\/p>\n<p>The 8 brainfuck instructions are:<\/p>\n<ol>\n<li><code>&gt;<\/code>: move the tape head one cell forward.<\/li>\n<li><code>&lt;<\/code>: move the tape head one cell backward.<\/li>\n<li><code>+<\/code>: increment the value on the current tape cell.<\/li>\n<li><code>-<\/code>: decrement the value on the current tape cell.<\/li>\n<li><code>.<\/code>: output the value on the current tape cell as a character.<\/li>\n<li><code>,<\/code>: input a character and write its numeric value onto the current tape cell.<\/li>\n<li><code>[<\/code>: compare and jump forward: compare the current tape cell to 0. If it&#8217;s zero, jump forward to the first instruction after the matching &#8220;<code>]<\/code>&#8220;; otherwise, go on to the next instruction.<\/li>\n<li><code>]<\/code>: compare and jump backward: if the current tape cell is <em>not<\/em> zero, then jump backward to the matching &#8220;[&#8220;.<\/li>\n<li>Any character which is <em>not<\/em> one of those eight instruction characters is a no-op &#8211; that is, it does nothing &#8211; execution will skip forward to the next command character. This means that you don&#8217;t need any special syntax to write comments in brainfuck &#8211; you just intersperse them with the program instructions. (But you need to do it carefully; if you use punctuation, you&#8217;ll probably accidentally create instructions which break your program. So Brainfuck makes it <em>impossible<\/em> to write grammatical comments!)<\/li>\n<\/ol>\n<p>Here&#8217;s a hello-world program in BF:<\/p>\n<pre style=\"background: #BBBBFF; color: black;\">++++++++[&gt;+++++++++&lt;-]&gt;.&lt;+++++[&gt;++++++&lt;-]&gt;-.\n+++++++..+++.&lt;++++++++[&gt;&gt;++++&lt;&lt;-]&gt;&gt;.\n&lt;&lt;++++[&gt;------&lt;-]&gt;.&lt;++++[&gt;++++++&lt;-]&gt;.\n+++.------.--------.&gt;+.\n<\/pre>\n<p>Let&#8217;s pull that apart just a bit so that we can hope to understand.<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>&#8220;<code>++++++++<\/code>&#8220;: store the number &#8220;8&#8221; in the current tape cell. We&#8217;re going to use that as a loop index, so the loop is going to repeat 8 times.<\/li>\n<li>&#8220;<code>[&gt;+++++++++&lt;-]<\/code>&#8220;: Run a loop: using the tape cell <em>after<\/em> the loop index, add &#8220;9&#8221; to it. Then go back to the loop index, decrement it, and branch back to the beginning of the loop if it&#8217;s not zero. So we wind up with the number 72 in the second cell. That&#8217;s the ascii code for &#8220;H&#8221;.<\/li>\n<li>&#8220;<code>&gt;.<\/code>&#8220;: go to the cell after the loop index, and output what&#8217;s there. That outputs the &#8220;72&#8221; as a character: &#8220;H&#8221;.<\/li>\n<li>&#8220;<code>&lt;+++++<\/code>&#8220;: back to the loop index. This time store 5 in it.<\/li>\n<li>&#8220;<code>[&gt;++++++&lt;-]<\/code>&#8220;: same idea as the loop to generate the &#8220;H&#8221;: this time, we&#8217;re going to add 5 * 6 to the value in the second cell. (Remember that we didn&#8217;t get rid of the value in that cell &#8211; so it&#8217;s <em>still <\/em>72.) So now the second cell contains 102.<\/li>\n<li>&#8220;<code>&gt;-.<\/code>&#8220;: Advance past the index, subtract one, and output. That&#8217;s 101, or &#8220;e&#8221;.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>After that, it continues in pretty much the same vein, using a couple of tape cells, and running loops to generate the values of the characters. It&#8217;s quite beautiful in its way. But at the same time, that&#8217;s an astonishingly complicated way of just printing out &#8220;Hello world&#8221;! Remarkably, isn&#8217;t it?<\/p>\n<p>If that didn&#8217;t seem impressive enough,<a href=\"http:\/\/esoteric.sange.fi\/brainfuck\/bf-source\/prog\/fibonacci.txt\">here \u00a0<\/a>is a really gorgeous implementation of a fibonacci sequence generator, complete with documentation. The BF compiler used to write this ignores any character other than the 8 commands, so the comments don&#8217;t need to be marked in any way; they just need to be really careful not to use punctuation.<\/p>\n<pre style=\"background: #BBBBFF; color: black;\">+++++++++++ number of digits to output\n&gt; #1\n+ initial number\n&gt;&gt;&gt;&gt; #5\n++++++++++++++++++++++++++++++++++++++++++++ (comma)\n&gt; #6\n++++++++++++++++++++++++++++++++ (space)\n&lt;&lt;&lt;&lt;&lt;&lt; #0\n[\n&gt; #1\ncopy #1 to #7\n[&gt;&gt;&gt;&gt;&gt;&gt;+&gt;+&lt;&lt;&lt;&lt;&lt;&lt;&lt;-]&gt;&gt;&gt;&gt;&gt;&gt;&gt;[&lt;&lt;&lt;&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;&gt;&gt;&gt;-]\n&lt;\ndivide #7 by 10 (begins in #7)\n[\n&gt;\n++++++++++  set the divisor #8\n[\nsubtract from the dividend and divisor\n-&lt;-\nif dividend reaches zero break out\ncopy dividend to #9\n[&gt;&gt;+&gt;+&lt;&lt;&lt;-]&gt;&gt;&gt;[&lt;&lt;&lt;+&gt;&gt;&gt;-]\nset #10\n+\nif #9 clear #10\n&lt;[&gt;[-]&lt;[-]]\nif #10 move remaining divisor to #11\n&gt;[&lt;&lt;[&gt;&gt;&gt;+&lt;&lt;&lt;-]&gt;&gt;[-]]\njump back to #8 (divisor possition)\n&lt;&lt;\n]\nif #11 is empty (no remainder) increment the quotient #12\n&gt;&gt;&gt; #11\ncopy to #13\n[&gt;&gt;+&gt;+&lt;&lt;&lt;-]&gt;&gt;&gt;[&lt;&lt;&lt;+&gt;&gt;&gt;-]\nset #14\n+\nif #13 clear #14\n&lt;[&gt;[-]&lt;[-]]\nif #14 increment quotient\n&gt;[&lt;&lt;+&gt;&gt;[-]]\n&lt;&lt;&lt;&lt;&lt;&lt;&lt; #7\n]\nquotient is in #12 and remainder is in #11\n&gt;&gt;&gt;&gt;&gt; #12\nif #12 output value plus offset to ascii 0\n[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]\nsubtract #11 from 10\n++++++++++  #12 is now 10\n&lt; #11\n[-&gt;-&lt;]\n&gt; #12\noutput #12 even if it's zero\n++++++++++++++++++++++++++++++++++++++++++++++++.[-]\n&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; #1\ncheck for final number\ncopy #0 to #3\n&lt;[&gt;&gt;&gt;+&gt;+&lt;&lt;&lt;&lt;-]&gt;&gt;&gt;&gt;[&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;-]\n&lt;- #3\nif #3 output (comma) and (space)\n[&gt;&gt;.&gt;.&lt;&lt;&lt;[-]]\n&lt;&lt; #1\n[&gt;&gt;+&gt;+&lt;&lt;&lt;-]&gt;&gt;&gt;[&lt;&lt;&lt;+&gt;&gt;&gt;-]&lt;&lt;[&lt;+&gt;-]&gt;[&lt;+&gt;-]&lt;&lt;&lt;-\n]\n<\/pre>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m currently away on a family vacation, and as soon as vacation is over, I&#8217;m off on a business trip for a week. And along the way, I&#8217;ve got some deadlines for my book. So to fill in, I&#8217;m recycling some old posts. I decided that it&#8217;s been entirely too long since there was any [&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":[92],"tags":[],"class_list":["post-802","post","type-post","status-publish","format-standard","hentry","category-pathological-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-cW","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/802","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=802"}],"version-history":[{"count":3,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/802\/revisions"}],"predecessor-version":[{"id":3831,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/802\/revisions\/3831"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}