{"id":230,"date":"2006-12-01T11:23:38","date_gmt":"2006-12-01T11:23:38","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2006\/12\/01\/pathological-programming-do-we-need-to-have-our-wires-crossed\/"},"modified":"2006-12-01T11:23:38","modified_gmt":"2006-12-01T11:23:38","slug":"pathological-programming-do-we-need-to-have-our-wires-crossed","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2006\/12\/01\/pathological-programming-do-we-need-to-have-our-wires-crossed\/","title":{"rendered":"Pathological Programming: Do we need to have our wires crossed?"},"content":{"rendered":"<p>I&#8217;m sure that in the friday pathological programming languages, I have a fondness for languages<br \/>\nthat make your code beautiful: languages like [SNUSP][snusp], [Befunge][befunge], [Piet][piet], and such. Those languages all you two-dimensional layouts for their programs.<br \/>\n[snusp]: http:\/\/scienceblogs.com\/goodmath\/2006\/08\/beautiful_insanity_pathologica.php<br \/>\n[befunge]: http:\/\/scienceblogs.com\/goodmath\/2006\/07\/friday_pathological_programmin.php<br \/>\n[piet]: http:\/\/scienceblogs.com\/goodmath\/2006\/11\/programming_in_color_fixed.php<br \/>\nAmong the nutters that make up the community of folks interested in this kind of thing, looking<br \/>\nat these languages led to a theoretical question, which was named *&#8221;the wire-crossing problem&#8221;*. The<br \/>\nbasic question was:<br \/>\n&gt;Is it possible to define a two-dimensional flow-based language which does *not* ever need<br \/>\n&gt;one execution path to *cross* another execution path?<br \/>\nAlternatively:<br \/>\n&gt;Is it possible to express every computable function as a two-dimensional graph<br \/>\n&gt;where no edges ever cross?<br \/>\nThis question raged for quite a while without an answer. Todays pathological language is<br \/>\na very simple language designed for the sole purpose of demonstrating, once and for all, that<br \/>\n*yes*, you can write every computable function with no wire-crossing.<\/p>\n<p><!--more--><br \/>\nThe language is called [Archway2][Archway2]. It&#8217;s a very simple [Brainfuck][bf] derivative<br \/>\nthat uses a two-dimensional program representation, and which has a regular translation<br \/>\nfrom brainfuck syntax to Archway2 syntax that does not require any wire crossing. Since BrainFuck is Turing complete, that means that every function *can* be written in Archway2 *without* wire-crossing by writing it in Brainfuck, and using the wire-cross-free translation to Archway2.<br \/>\nLike BrainFuck, Archway2 has 8 commands, but they&#8217;re laid out in 2-dimensional format. execution<br \/>\nstarts at the *bottom left* with the instruction pointer moving *right*. Any character that isn&#8217;t a command is a no-op; if the instruction pointer exits the rectangular grid containing the program,<br \/>\nit halts.<br \/>\nOf the eight commands in Archway2, the first six are exactly the same as BF.<br \/>\n1. &#8220;&#8220;&#8221;: move the tape-cell pointer right.<br \/>\n3. &#8220;`+`&#8221;: add one to the current tape cell.<br \/>\n4. &#8220;`-`&#8221;: subtract one from the current tape cell.<br \/>\n5. &#8220;`,`&#8221;: read a byte from the input and write it into the current tape cell.<br \/>\n6. &#8220;`.`&#8221;: write the value on the current tape cell to the output.<br \/>\nThe last two are replacements for the looping operators &#8220;`[`&#8221; and &#8220;`]`&#8221; in BrainFuck. As a reminder, in BF, &#8220;`[`&#8221; means &#8220;if the current tape cell contains the value 0, jump to the statement after the matching &#8220;`]`&#8221;, and &#8220;`]`&#8221; means if the current tape cell does *not* contain the value 0, jump<br \/>\nback to the statement after the matching &#8220;`[`&#8221;.<br \/>\nIn Archway2, the bracket loop controls are replaced by *directional* controls:<br \/>\n* &#8220;&#8220;&#8221;: LURD (left-up,right-down); if the current tape cell contains 0, do nothing (execution continues moving in the same direction); otherwise, if the current execution direction is left, switch to up, right switch to down, down switch to right, and up switch to left.<br \/>\n* &#8220;`\/`&#8221;: RULD (right-up,left-down); if the current tape cell contains 0, do nothing (execution continues moving in the same direction); otherwise, if the current execution direction is right, switch to up, left switch to down, down switch to left, and up switch to right.<br \/>\nThe trick that makes it work is that the the directional controls are very special. They *don&#8217;t* run<br \/>\nwhen the cursor reaches them. If the cursor lands *on top of* a directional command, it&#8217;s<br \/>\ntreated as a no-op. But when it&#8217;s executing any command, it looks at the *next* cell in the current direction, and if it&#8217;s either LURD or RULD, then the effect of LURD or RULD is *added* to the<br \/>\neffect of the current cell.<br \/>\nSo, for example, suppose in the code below, we were starting execution at the &#8220;$&#8221; moving right.<\/p>\n<pre>\n..\n+\n$   +++\/\n<\/pre>\n<p>What this will do is run the three &#8220;+&#8221; commands, so that the current tape cell contains the value<br \/>\n&#8220;3&#8221;. Then it will switch direction to move *up*. If it did the RULD direction switch *on* the &#8220;`\/`&#8221;<br \/>\ncharacter, it would execution the fourth &#8220;+&#8221;, and output 4. But the direction switch happens on the<br \/>\ncell &#8220;before&#8221; the RULD (that is, on the third &#8220;+&#8221;), so the program outputs &#8220;3&#8221;.<br \/>\nThere aren&#8217;t many (or indeed *any*) interesting programs in Archway2; after all, it&#8217;s just an awkward alternate syntax for Brainfuck. So any BF program can be translated into Archway2. The interesting<br \/>\nthis is: how can we translate BF code into Archway2?<br \/>\nFor everything except the loop commands, there *is* no translation at all; the BF and the Archway<br \/>\ncommands are *exactly* the same. So the only question concerns loops.<br \/>\nSuppose we have a Brainfuck program, where &#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;, and &#8220;d&#8221; represent chunks of *non-looping* Brainfuck code. Then the Brainfuck program  &#8220;`a[bc]d`&#8221; would turn into the following Archway2:<\/p>\n<pre>\n\n\/\/\n\/   bc\/+\n-a\/ + -d\n+\/     \n<\/pre>\n<p>A quick walkthrough to help you understand it:<br \/>\n1. Add one to the current tape cell (making it non-zero); and then executing the RULD that&#8217;s in the next cell. This causes the program to turn and start executing upwards.<br \/>\n2. No-op, but there&#8217;s a RULD in the next cell, so turn right.<br \/>\n3. Subtract one from the current cell; this undoes the earlier increment, so we&#8217;ve got a blank tape like we did before the two direction changes that got us here. So we&#8217;re reading to execute &#8220;a&#8221; with<br \/>\na blank tape.<br \/>\n4. Execute a. At the end of it, if the current tape cell is zero, we just keep moving right; that will execute a &#8220;+&#8221; and a &#8220;-&#8221; which cancel each other, and then run &#8220;d&#8221;, and terminate.<br \/>\n5. If the current cell was *not* zero after &#8220;a&#8221;, then we switch upward, and do a NOP followed<br \/>\nby a RULD, and turn right, executing &#8220;b&#8221; and &#8220;c&#8221;.<br \/>\n6. If the tape is zero, keep going right; do a plus, turn down, turn right, do a &#8220;-&#8221; (cancelling the earlier plus), execute &#8220;d&#8221; and then terminate.<br \/>\n7. If the tape after running &#8220;b&#8221; and &#8220;c&#8221; is non-zero, we turn up, do a couple of NOPs, turn left,<br \/>\nthen turn down, so that we land back on the B. And we keep going.<br \/>\nIt&#8217;s mostly pretty simple &#8211; the trick is a combination of two things: using the lookahead property of Archway2, which gives us a bit of room to breathe; and relying to being able to much about with one of the tape cells without damaging anything. But since we always &#8220;undo&#8221; whatever we did to the cell &#8211; and after starting the program, we *only* actually use the cell after a test in which it was zero,<br \/>\nwe can show that we never damage the state.<br \/>\nJust for kicks, let&#8217;s look at one translation of a real program.<br \/>\nHere&#8217;s a program that reverses its input, written in BF: &#8220;`&gt;,[&gt;,]&lt;[.,`&#8221; bc=&#8221;`&gt;,`&#8221;, and d=&#8221;`&lt;[.&lt;]`&quot;. So:<\/p>\n<pre>\n\n\/\/\n\/    &gt;,\/+\n-&gt;,\/ + -   &lt;[.&lt;]\n+\/      \n<\/pre>\n<p>Now, we move over to the right, and look at translating the &#8220;d&#8221; part; a=&#8221;`&lt;`&quot;, bc=&quot;`.&lt;`&quot;, and d=&quot;&quot;. So, we wind up with:<\/p>\n<pre>\n\n        \/\/\n\/\/         \/   .&lt;\/+\n\/    &gt;,\/+    -&lt;\/ + -\n-&gt;,\/ + -   +\/\n+\/      \n<\/pre>\n<p>And poof! The line-crossing problem is solved &#8211; at the cost of taking an already<br \/>\nunreadable line-noise language, and making it *even more* cryptic than it already was.<br \/>\n[Archway2]: http:\/\/www.esolangs.org\/wiki\/Archway<br \/>\n[bf]: http:\/\/scienceblogs.com\/goodmath\/2006\/07\/gmbm_friday_pathological_progr.php<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m sure that in the friday pathological programming languages, I have a fondness for languages that make your code beautiful: languages like [SNUSP][snusp], [Befunge][befunge], [Piet][piet], and such. Those languages all you two-dimensional layouts for their programs. [snusp]: http:\/\/scienceblogs.com\/goodmath\/2006\/08\/beautiful_insanity_pathologica.php [befunge]: http:\/\/scienceblogs.com\/goodmath\/2006\/07\/friday_pathological_programmin.php [piet]: http:\/\/scienceblogs.com\/goodmath\/2006\/11\/programming_in_color_fixed.php Among the nutters that make up the community of folks interested in this [&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-230","post","type-post","status-publish","format-standard","hentry","category-pathological-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-3I","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/230","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=230"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/230\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}