{"id":91,"date":"2006-07-28T08:30:00","date_gmt":"2006-07-28T08:30:00","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2006\/07\/28\/friday-pathological-programming-language-whenever\/"},"modified":"2006-07-28T08:30:00","modified_gmt":"2006-07-28T08:30:00","slug":"friday-pathological-programming-language-whenever","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2006\/07\/28\/friday-pathological-programming-language-whenever\/","title":{"rendered":"Friday Pathological Programming Language: Whenever"},"content":{"rendered":"<p>I was hoping for a bit of a vanity post for todays pathological programming language in honor of my 40th birthday (tomorrow), but I didn&#8217;t have time to finish implementing my own little piece of insanity. So it&#8217;ll have to wait for some other occasion.<br \/>\nTodays pathological programming language is a really simple monstrosity called [&#8220;Whenever&#8221;][whenever].  Whenever is a programming language where programs get executed in *random* order, and there are *no* variables. The only state, the only way of manipulating information in a Whenever program is by manipulating the collection of executable statements itself: the program is both the code *and* the data at the same time.<br \/>\nThe basic program model of Wheneveris: you write a set of statements. The statements are inserted into a grab-bag by the interpreter. The interpreter then repeatedly picks a statement out of the bag *at random* and executes it. It keeps doing that until there are no more statements in the bag.<br \/>\nEverything in Whenever is done by manipulating the statement bag.<br \/>\nEach statement is labeled by a number. The number has no direct meaning; it&#8217;s just an identifier that will be used to reference the statement. The numbers assigned to lines don&#8217;t have to match the order in which the lines were written in the source file; and they have no effect on the order by which statements are pulled out of the bag.<br \/>\nSo how do you do anything in this crazy language?<br \/>\nThere&#8217;s a print statement for printing things out. So, for example,<br \/>\n23 print(&#8220;Hello worldn&#8221;);<br \/>\nis the hello world program. Since there&#8217;s only one statement, it&#8217;ll get grabbed from the statement bag, and executed.<br \/>\nThere&#8217;s also a read statement, which reads a number from standard input, and then acts as if the statement were the number that it read.<br \/>\nThe simplest statement is just a number. A number statement *adds* a copy of the line identifier by that number to the bag. If the number is negative, then it *removes* a copy of the statement from the bag. So, for example:<br \/>\n23 print(&#8220;Hello worldn&#8221;);<br \/>\n11 23;<br \/>\nwould print &#8220;Hello world&#8221; twice. If 23 were executed first, it would print &#8220;Hello world&#8221;, and then only 11 would be in the bag, so it would execute 11, which would add 23 to the bag. If 11 went first, then it would add 23 to the bag, and there would be two 23s in the bag, which would get executed.<br \/>\nYou can add multiple copies of a line to the bag: 5#3 adds 3 copies of statement 5 to the bag. And you can add multiple statements to the bag at once, by separating them with a comma. So:<br \/>\n17 21, -2, 3#4, -5#2;<br \/>\nWould insert one copy of statement 21 and four copies of statement 3 to the  bag; and remove one copy of statement 2, and two copies of statement 5.<br \/>\nYou can also do any normal arithmetic operation on numbers. The result of the arithmetic operation is interpreter as a line number.<br \/>\nThere&#8217;s also two kinds of conditionals, &#8220;defer&#8221; and &#8220;again&#8221;.  Defer takes a parameter which is evaluated to a line number, and if there are any copies of that line number in the bag, then it reinserts itself, and doesn&#8217;t do anything else. If there are no copies of the parameter in the bag, then the statement on the rest of the line is executed.<br \/>\nAgain, an example:<br \/>\n1 print(&#8220;Hello&#8221;);<br \/>\n2 defer(1) print(&#8220;Worldn&#8221;);<br \/>\nis a more complicated version of hello world.<br \/>\nThe &#8220;again&#8221; statement is very similar to the &#8220;defer&#8221; statement; but if its argument is true (i.e., evaluates to a statement that is present in the bag), then it adds a copy of itself to the bag; whether the parameter is true or false, it then executes the rest of the statement.<br \/>\nThere&#8217;s one helpful built-in function: N(x) returns the number of copies of statement x in the bag.<br \/>\nSo, a couple of interesting example programs:<br \/>\n1 defer (4 || N(1)&lt;N(2) &amp;&amp; N(2)&lt;N(3)) print(N(1)+&quot; bottles of beer on the wall, &quot;+N(1)+&quot; bottles of beer,&quot;);<br \/>\n2 defer (4 || N(1)==N(2)) print(&quot;Take one down and pass it around,&quot;);<br \/>\n3 defer (4 || N(2)==N(3)) print(N(1)+&quot; bottles of beer on the wall.&quot;);<br \/>\n4 1#98,2#98,3#98;<br \/>\nThis first ensures that statement four runs first: statements 1, 2, and 3 will all defer until 4 has been executed. Once four is run, there are 99 copies of statements 1, 2, and 3 in the bag. The rest of the defer statement makes sure that 1 executes before 2, and 2 before 3; so it cycles through 1, 2, 3 99 times.  Pretty simple, right?<br \/>\nHow about this?<br \/>\n1 again (1) defer (3 || N(1)99) 2#N(1),3,7;<br \/>\n2 again (2) defer (3 || N(2)99) 1#N(2),3,7;<br \/>\n3 defer (5) print(N(1)+N(2));<br \/>\n4 defer (5) print(&#8220;1&#8221;);<br \/>\n5 4,-3,7;<br \/>\n6 defer (4) 3;<br \/>\n7 7;<br \/>\n8 defer (N(7)&lt;100) -1#N(1),-2#N(2),-7#100;<br \/>\n9 defer (3 || 6) 1,3;<br \/>\nIf you look carefully.. It generates the first 100 fibonacci numbers.<br \/>\nIt&#039;s an incredibly simple language. Simple, but quite twisted, and seriously mind-warping to try to program: you need to always keep track of the fact that the statements that represent your data could get selected for execution, which will modify the data unless you used a &quot;defer&quot; as a guard, but then you  need to make sure that the guard gets preserved correctly&#8230;  It&#039;s quite devious.<br \/>\n[whenever]: http:\/\/www.dangermouse.net\/esoteric\/whenever.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was hoping for a bit of a vanity post for todays pathological programming language in honor of my 40th birthday (tomorrow), but I didn&#8217;t have time to finish implementing my own little piece of insanity. So it&#8217;ll have to wait for some other occasion. Todays pathological programming language is a really simple monstrosity called [&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-91","post","type-post","status-publish","format-standard","hentry","category-pathological-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-1t","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/91","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=91"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}