<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rough Book &#187; turing completeness</title>
	<atom:link href="http://vivin.net/tag/turing-completeness/feed/" rel="self" type="application/rss+xml" />
	<link>http://vivin.net</link>
	<description>random musings of just another computer nerd</description>
	<lastBuildDate>Wed, 02 May 2012 18:20:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>A brainfuck interpreter in bAdkOde</title>
		<link>http://vivin.net/2010/01/23/a-brainfuck-interpreter-in-badkode/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2010/01/23/a-brainfuck-interpreter-in-badkode/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 21:44:23 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[badkode]]></category>
		<category><![CDATA[brainf*ck]]></category>
		<category><![CDATA[brainfuck]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[turing complete]]></category>
		<category><![CDATA[turing completeness]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1379</guid>
		<description><![CDATA[A few days before I left India, I started writing a brainfuck interpreter in bAdkOde. I finished implemented all the instructions, excepting for looping. I actually finished the code (and fixed all bugs) while I was in the air, flying from Dubai to Los Angeles. Emirates Airlines has power-plugs for your laptop on the seat. [...]]]></description>
			<content:encoded><![CDATA[<p>A few days before I left India, I started writing a <a href="http://www.muppetlabs.com/~breadbox/bf/">brainfuck</a> interpreter in <a href="http://vivin.net/projects/badkode/">bAdkOde</a>. I finished implemented all the instructions, excepting for looping. I actually finished the code (and fixed all bugs) while I was in the air, flying from Dubai to Los Angeles. Emirates Airlines has power-plugs for your laptop on the seat. It&#8217;s pretty sweet!</p>
<p>An interesting thing I noticed was that I couldn&#8217;t perfectly emulate the input instruction. I&#8217;m feeding the brainfuck code to the interpreter from STDIN and so that might be the problem. I&#8217;ve noticed that brainfuck interpreters written in brainfuck have the same problem. You have to specify program input before hand. This is what I&#8217;ve decided to do. You write brainfuck code, and then mark the end of program code by an exclamation mark. After the exclamation mark, you provide any input, and then mark the end of input by another exclamation mark. Programs that do not have any input end with two exclamation marks. After I finished writing the interpreter, I commented it. While I was doing this, I noticed a lack of labels in bAdkOde. So, I decided to update the interpreter to include them. Speaking of which, I really ought to rewrite the interpreter sometime&#8230;</p>
<p>Anyway, here is the code to the interpreter. I&#8217;m <a target="_blank" href="http://vivin.net/pub/bAdkOde/brainfuck_commented.bad">providing</a> a link to it, because the commented version is rather large. But here&#8217;s the expanded (without labels or macros), unformatted version:</p>
<pre class="brush: php">
&gt;3a&gt;1b{!b?b&gt;b[a)b-91b{=b+1[b&gt;1b}+91b-93b{=b-1[b&gt;1b}+93b(b+1a-33b}&gt;0[a+1a&gt;2b&gt;a[b&gt;1b{!b?b&gt;b[a+1a-33b}&gt;1b&gt;a[b&gt;0a&gt;[aa&gt;ab{=a&gt;1a&gt;[ab&gt;3a{![a)a&gt;[aa-62a{=a+1b-30000b)a&gt;ba{+b&gt;1b&gt;[bb)b&gt;0b-1b&gt;0a}{-a+30000b)b&gt;0a}(b(a+62a}+62a-60a{=a-1b)a&gt;1a&gt;[aa-ab&gt;ba{-b&gt;1a&gt;[aa+30000a&gt;ab)b&gt;0b&gt;0a-1a}{+a&gt;1a&gt;[aa+ab)b&gt;0a-1a}(b(a+60a}+60a-43a{=a+1[b+43a}+43a-45a{=a-1[b+45a}+45a-46a{=a&quot;[b+46a}+46a-44a{=a)a&gt;2a&gt;[aa&gt;[a[b&gt;2a+1[a(a+44a}+44a-91a{=a(a)b)a&gt;[bb{=b&gt;0b&gt;1[b{![b(a+1a)a&gt;[aa)a-91a{=a+1[a&gt;1a}+91a-93a{=a-1[a&gt;1a}+93a(a}(a-1a)a&gt;1b}(a(b)a+91a}+91a-93a{=a(a)b)a&gt;[bb{!b&gt;0b&gt;0[b-1[b{![b(a-1a)a&gt;[aa)a-91a{=a+1[a&gt;1a}+91a-93a{=a-1[a&gt;1a}+93a(a}&gt;0b}(a(b)a+93a}+93a(a+1a}&gt;1a&gt;0b}{!b&quot;85&quot;110&quot;109&quot;97&quot;116&quot;99&quot;104&quot;101&quot;100&quot;32&quot;98&quot;114&quot;97&quot;99&quot;107&quot;101&quot;116&quot;115&gt;0b}
</pre>
<p>Also, this is additional proof for Turing Completeness. Yes, I&#8217;m a nerd! <img src='http://vivin.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br /><a href="http://vivin.net/?p=1379#comments" title="Comments on &quot;A brainfuck interpreter in bAdkOde&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1379" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1379&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2010/01/23/a-brainfuck-interpreter-in-badkode/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1379" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1379" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1379&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>bAdkOde is Turing Complete</title>
		<link>http://vivin.net/2009/12/26/badkode-is-turing-complete/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2009/12/26/badkode-is-turing-complete/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 10:26:24 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[badkode]]></category>
		<category><![CDATA[brainf*ck]]></category>
		<category><![CDATA[brainfuck]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[turing complete]]></category>
		<category><![CDATA[turing completeness]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1346</guid>
		<description><![CDATA[In my previous post, I suggested that bAdkOde might be Turing Complete by writing a quine. One of the ways to actually prove Turing Completeness is to try and write an interpreter for another Turing-Complete language in bAdkOde. Another approach involves providing a direct translation from another Turing-Complete language into bAdkOde. Here, I prove the [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://vivin.net/2009/12/25/a-badkode-quine-which-suggests-that-badkode-is-turing-complete/">previous post</a>, I suggested that <a href="http://vivin.net/projects/badkode/">bAdkOde</a> might be <a href="http://en.wikipedia.org/wiki/Turing_complete">Turing Complete</a> by writing a <a href="http://en.wikipedia.org/wiki/Quine_(computing)">quine</a>. One of the ways to actually prove Turing Completeness is to try and write an interpreter for another Turing-Complete language in bAdkOde. Another approach involves providing a direct translation from another Turing-Complete language into bAdkOde. Here, I prove the Turing Completeness of bAdkOde by providing a direct translation from <a href="http://www.muppetlabs.com/~breadbox/bf/">Brainfuck</a> into bAdkOde.<br />
<span id="more-1346"></span><br />
<strong><u>Assumptions</u></strong></p>
<ol>
<li>The <em>a</em> register is our memory pointer.</li>
<li>We initialize the <em>a</em> register to 0.</li>
</ol>
<p><strong><u>Proof</u></strong></p>
<p>We translate Brainfuck to bAdkOde using the following mappings or rules:</p>
<ol>
<li><em>&gt;</em> maps to <em>+1a</em></li>
<li><em>&lt;</em> maps to <em>-1a</em></li>
<li><em>+</em> maps to <em>+1[a</em></li>
<li><em>-</em> maps to <em>-1[a</em></li>
<li><em>.</em> maps to <em>"[a</em></li>
<li><em>,</em> maps to <em>?[a</em></li>
<li><em>[</em> maps to <em>{![a</em></li>
<li><em>]</em> maps to <em>}</em></li>
</ol>
<p><strong><u>Conclusion</u></strong></p>
<p>Since we can completely translate or map Brainfuck, an already Turing-Complete language into bAdkOde, we can say that bAdkOde is also Turing Complete.</p>
<p><strong><u>Translated example</u></strong></p>
<p><strong>helloworld.bf:</strong></p>
<pre class="brush: php">
+++ +++ +++ +
[
    &gt; +++ +++ +
    &gt; +++ +++ +++ +
    &gt; +++
    &gt; +
    &lt;&lt;&lt; &lt; -
]
&gt;++ .
&gt;+.
+++ +++ +.
.
+++ .
&gt;++ .
&lt;&lt;+ +++ +++ +++ +++ ++.
&gt;.
+++ .
--- --- .
--- --- --.
&gt;+.
</pre>
<p><strong>helloworld.bad</strong></p>
<pre class="brush: php">
&gt;0a+1[a+1[a+1[a +1[a+1[a+1[a +1[a+1[a+1[a +1[a
{![a
    +1a +1[a+1[a+1[a +1[a+1[a+1[a +1[a
    +1a +1[a+1[a+1[a +1[a+1[a+1[a +1[a+1[a+1[a +1[a
    +1a +1[a+1[a+1[a
    +1a +1[a
    -1a-1a-1a -1a -1[a
}
+1a+1[a+1[a &quot;[a
+1a+1[a&quot;[a
+1[a+1[a+1[a +1[a+1[a+1[a +1[a&quot;[a
&quot;[a
+1[a+1[a+1[a &quot;[a
+1a+1[a+1[a &quot;[a
-1a-1a+1[a +1[a+1[a+1[a +1[a+1[a+1[a +1[a+1[a+1[a +1[a+1[a+1[a +1[a+1[a&quot;[a
+1a&quot;[a
+1[a+1[a+1[a &quot;[a
-1[a-1[a-1[a -1[a-1[a-1[a &quot;[a
-1[a-1[a-1[a -1[a-1[a-1[a -1[a-1[a&quot;[a
+1a+1[a&quot;[a
</pre>
<p>Here's a quick perl script that I whipped up, to translate Brainfuck to bAdkOde:</p>
<pre class="brush: perl">
#!/usr/bin/perl

 use strict;

 my $terminator = $/;
 undef $/;
 my $bf = &lt;STDIN&gt;;
 $bf =~ s/[^&lt;&gt;\[\]\.,\+\-\s\n]//g;
 $/ = $terminator;

 my $i = 0;

 print &quot;&gt;0a&quot;;

 while($i &lt; length($bf)) {

    my $bf_instruction = substr($bf, $i, 1);

    if($bf_instruction eq &#039;&gt;&#039;) {
       print &quot;+1a&quot;;
    }

    elsif($bf_instruction eq &#039;&lt;&#039;) {
       print &quot;-1a&quot;;
    }

    elsif($bf_instruction eq &#039;+&#039;) {
       print &quot;+1[a&quot;;
    }

    elsif($bf_instruction eq &#039;-&#039;) {
       print &quot;-1[a&quot;;
    }

    elsif($bf_instruction eq &#039;.&#039;) {
       print &quot;\&quot;[a&quot;;
    }

    elsif($bf_instruction eq &#039;,&#039;) {
       print &quot;?[a&quot;;
    }

    elsif($bf_instruction eq &#039;[&#039;) {
       print &quot;{![a&quot;;
    }

    elsif($bf_instruction eq &#039;]&#039;) {
       print &quot;}&quot;;
    }

    else {
       print $bf_instruction;
    }

    $i++;
 }
</pre>
<p><strong><u>Additional proof</u></strong></p>
<p>Since there exist self-interpreters for Brainfuck (i.e., Brainfuck interpreters written <em>in</em> Brainfuck), and since it has been shown that there exists a valid translation from Brainfuck to bAdkOde, it follows that it is possible to write a Brainfuck interpreter in bAdkOde. This provides additional proof for the Turing Completeness of bAdkOde.</p>
<br /><a href="http://vivin.net/?p=1346#comments" title="Comments on &quot;bAdkOde is Turing Complete&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1346" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1346&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2009/12/26/badkode-is-turing-complete/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1346" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1346" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1346&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>A bAdkOde quine (which suggests that bAdkOde is Turing Complete)</title>
		<link>http://vivin.net/2009/12/25/a-badkode-quine-which-suggests-that-badkode-is-turing-complete/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2009/12/25/a-badkode-quine-which-suggests-that-badkode-is-turing-complete/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 11:12:41 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[badkode]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[quine]]></category>
		<category><![CDATA[turing complete]]></category>
		<category><![CDATA[turing completeness]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1335</guid>
		<description><![CDATA[On the bAdkOde project page, I mentioned that I didn&#8217;t know whether bAdkOde is Turing Complete (although I suspected it). I also mentioned that if I was able to write a quine in bAdkOde, then it would probably mean that it is Turing Complete. I was able to write one after going through this excellent [...]]]></description>
			<content:encoded><![CDATA[<p>On the bAdkOde <a href="http://vivin.net/projects/badkode/">project page</a>, I mentioned that I didn&#8217;t know whether bAdkOde is <a href="http://en.wikipedia.org/wiki/Turing_completeness">Turing Complete</a> (although I suspected it). I also mentioned that if I was able to write a <a href="http://en.wikipedia.org/wiki/Quine_%28computing%29">quine</a> in bAdkOde, then it would probably mean that it is Turing Complete. I was able to write one after going through this <a href="http://dwcope.freeshell.org/projects/quine/">excellent quine tutorial</a> by Dave Cope.<br />
<span id="more-1335"></span><br />
A quine is basically a program that prints a copy of its source code. Writing a quine seems trivial, but can be, and is in most cases, fiendishly difficult. The quine tutorial that I mentioned earlier describes a rather simple method to create a quine. In any quine, you need to store a representation of part of the source code. This representation is referred to as the <strong>core</strong> or <strong>body</strong>. In the method described in the tutorial, and the method that I used, the core is represented as ASCII codes. In some approaches, the core is represented as a string or character array (which, when it comes down to it, is probably represented in memory as an array of ASCII codes). In the approach that I used, there are two parts to the core. The first part prints out the code for the data structure that will contain a representation of the core, and the second part prints out the actual core. For example, let&#8217;s say I wanted to make a Perl quine. I start off with the core:</p>
<pre class="brush: perl">
 print &quot; \@body = (\n&quot;;
 map { printf &quot;0x%x, &quot;, $_ } @body;
 print &quot; );\n&quot;;
 map { printf &quot;%c&quot;, $_ } @body;
</pre>
<p>Here, you can see that I&#8217;ve decided to use an array called <em>@body</em> to store the ASCII values (in hexadecimal). The first three lines print out the definition for the array that will hold the ASCII values. The last line reads the array, translates the code into actual characters, and prints them out. Now all we have to do is get the hexadecimal codes and create the array. To do that, we use a utility called <em>xxd</em>:</p>
<pre>
vivin@dauntless ~/Projects/code/perl/quine
$ xxd -i quinegen.pl >qhex.pl
</pre>
<p>The output is an array definition in C:</p>
<pre class="brush: c">
unsigned char quinegen_pl[] = {
  0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x22, 0x20, 0x5c, 0x40, 0x62,
  0x6f, 0x64, 0x79, 0x20, 0x3d, 0x20, 0x28, 0x5c, 0x6e, 0x22, 0x3b, 0x0a,
  0x20, 0x6d, 0x61, 0x70, 0x20, 0x7b, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74,
  0x66, 0x20, 0x22, 0x30, 0x78, 0x25, 0x78, 0x2c, 0x20, 0x22, 0x2c, 0x20,
  0x24, 0x5f, 0x20, 0x7d, 0x20, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x3b, 0x0a,
  0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x22, 0x20, 0x29, 0x3b, 0x5c,
  0x6e, 0x22, 0x3b, 0x0a, 0x20, 0x6d, 0x61, 0x70, 0x20, 0x7b, 0x20, 0x70,
  0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x25, 0x63, 0x22, 0x2c, 0x20,
  0x24, 0x5f, 0x20, 0x7d, 0x20, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x3b, 0x0a
};
unsigned int quinegen_pl_len = 108;
</pre>
<p>We need to change this to valid Perl, and that&#8217;s quite simple:</p>
<pre class="brush: perl">
 @body = (
   0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x22, 0x20, 0x5c, 0x40, 0x62,
   0x6f, 0x64, 0x79, 0x20, 0x3d, 0x20, 0x28, 0x5c, 0x6e, 0x22, 0x3b, 0x0a,
   0x20, 0x6d, 0x61, 0x70, 0x20, 0x7b, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74,
   0x66, 0x20, 0x22, 0x30, 0x78, 0x25, 0x78, 0x2c, 0x20, 0x22, 0x2c, 0x20,
   0x24, 0x5f, 0x20, 0x7d, 0x20, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x3b, 0x0a,
   0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x22, 0x20, 0x29, 0x3b, 0x5c,
   0x6e, 0x22, 0x3b, 0x0a, 0x20, 0x6d, 0x61, 0x70, 0x20, 0x7b, 0x20, 0x70,
   0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x25, 0x63, 0x22, 0x2c, 0x20,
   0x24, 0x5f, 0x20, 0x7d, 0x20, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x3b, 0x0a
 );
</pre>
<p>We take this and paste it on top of our core:</p>
<pre class="brush: perl">
 @body = (
   0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x22, 0x20, 0x5c, 0x40, 0x62,
   0x6f, 0x64, 0x79, 0x20, 0x3d, 0x20, 0x28, 0x5c, 0x6e, 0x22, 0x3b, 0x0a,
   0x20, 0x6d, 0x61, 0x70, 0x20, 0x7b, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74,
   0x66, 0x20, 0x22, 0x30, 0x78, 0x25, 0x78, 0x2c, 0x20, 0x22, 0x2c, 0x20,
   0x24, 0x5f, 0x20, 0x7d, 0x20, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x3b, 0x0a,
   0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x22, 0x20, 0x29, 0x3b, 0x5c,
   0x6e, 0x22, 0x3b, 0x0a, 0x20, 0x6d, 0x61, 0x70, 0x20, 0x7b, 0x20, 0x70,
   0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x25, 0x63, 0x22, 0x2c, 0x20,
   0x24, 0x5f, 0x20, 0x7d, 0x20, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x3b, 0x0a
 );
 print &quot; \@body = (\n&quot;;
 map { printf &quot;0x%x, &quot;, $_ } @body;
 print &quot; );\n&quot;;
 map { printf &quot;%c&quot;, $_ } @body;
</pre>
<p>Now the interesting thing here is that this piece of code is not the actual quine. Rather, it <em>generates</em> the quine. But the generated code isn&#8217;t that different from the generator:</p>
<pre class="brush: perl">
 @body = (
0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x22, 0x20, 0x5c, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x3d, 0x20, 0x28, 0x5c, 0x6e, 0x22, 0x3b, 0xa, 0x20, 0x6d, 0x61, 0x70, 0x20, 0x7b, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x30, 0x78, 0x25, 0x78, 0x2c, 0x20, 0x22, 0x2c, 0x20, 0x24, 0x5f, 0x20, 0x7d, 0x20, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x3b, 0xa, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x20, 0x22, 0x20, 0x29, 0x3b, 0x5c, 0x6e, 0x22, 0x3b, 0xa, 0x20, 0x6d, 0x61, 0x70, 0x20, 0x7b, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x20, 0x22, 0x25, 0x63, 0x22, 0x2c, 0x20, 0x24, 0x5f, 0x20, 0x7d, 0x20, 0x40, 0x62, 0x6f, 0x64, 0x79, 0x3b, 0xa,  );
 print &quot; \@body = (\n&quot;;
 map { printf &quot;0x%x, &quot;, $_ } @body;
 print &quot; );\n&quot;;
 map { printf &quot;%c&quot;, $_ } @body;
</pre>
<p>The only difference is the extra comma in the array definition, and the fact that all the ASCII codes are on one line. If you run this perl script, you will get an exact copy of its source code. Now how do we write a quine in bAdkOde? We use a similar approach. First we write the core:</p>
<pre class="brush: php">
&gt;0a
&quot;62&quot;48&quot;97&quot;10
{![a
 &quot;62&#039;[a&quot;91&quot;97&quot;43&quot;49&quot;97
 +1a
}
&quot;10
&gt;0a
{![a
 &quot;[a
 +1a
}
</pre>
<p>To implement a quine in bAdkOde, I decided to store the representation of the core (in decimal ASCII values) in memory starting at location zero. The first line initializes the a register to zero. The second line prints out the string <em>>0a</em> with a newline. This line is analogous to the <em>print " \@body = (\n";</em> line in the perl quine. Within our first loop, we're printing out the actual code that will store the representation of the core in memory. So the first line within the loop prints <em>>ASCII[a+1a</em> where <em>ASCII</em> is the ASCII value of a character from the core. The line is analogous to the <em>printf "0x%x, ", $_</em> line in the perl quine. There is another print statement right outside the first loop, which simply prints a newline. This line is analogous to the <em>print " );\n";</em> line in the perl quine. The second loop performs the actual translation of the ASCII code into the core. Now, just like the perl quine, we use <em>xxd</em>:</p>
<pre>
vivin@dauntless ~/Projects/code/perl/bAdkOde
$ xxd -i quine.bad >qhex.txt
</pre>
<p>After doing this, we get C code that looks like this:</p>
<pre class="brush: c">
unsigned char quine_bad[] = {
  0x3e, 0x30, 0x61, 0x0a, 0x22, 0x36, 0x32, 0x22, 0x34, 0x38, 0x22, 0x39,
  0x37, 0x22, 0x31, 0x30, 0x0a, 0x7b, 0x21, 0x5b, 0x61, 0x0a, 0x20, 0x22,
  0x36, 0x32, 0x27, 0x5b, 0x61, 0x22, 0x39, 0x31, 0x22, 0x39, 0x37, 0x22,
  0x34, 0x33, 0x22, 0x34, 0x39, 0x22, 0x39, 0x37, 0x0a, 0x20, 0x2b, 0x31,
  0x61, 0x0a, 0x7d, 0x0a, 0x22, 0x31, 0x30, 0x0a, 0x3e, 0x30, 0x61, 0x0a,
  0x7b, 0x21, 0x5b, 0x61, 0x0a, 0x20, 0x22, 0x5b, 0x61, 0x0a, 0x20, 0x2b,
  0x31, 0x61, 0x0a, 0x7d, 0x0a
};
unsigned int quine_bad_len = 77;
</pre>
<p>Translating this into bAdkOde is not as straightforward, but it&#8217;s not that difficult either. I removed the first line, and the last two lines from <em>qhex.txt</em> and then used a oneliner:</p>
<pre>
vivin@dauntless ~/Projects/code/perl/bAdkOde
$ cat qhex.txt | perl -e 'chomp($a=&lt;STDIN&gt;); map { print ">" . hex($_) . "[a+1a"; } \
   split(",", $a);' > qdec.txt
</pre>
<p>You end up with this snippet of bAdkOde:</p>
<pre class="brush: php">
&gt;62[a+1a&gt;48[a+1a&gt;97[a+1a&gt;10[a+1a&gt;34[a+1a&gt;54[a+1a&gt;50[a+1a&gt;34[a+1a&gt;52[a+1a&gt;56[a+1a&gt;34[a+1a&gt;57[a+1a&gt;55[a+1a&gt;34[a+1a&gt;49[a+1a&gt;48[a+1a&gt;10[a+1a&gt;123[a+1a&gt;33[a+1a&gt;91[a+1a&gt;97[a+1a&gt;10[a+1a&gt;32[a+1a&gt;34[a+1a&gt;54[a+1a&gt;50[a+
1a&gt;39[a+1a&gt;91[a+1a&gt;97[a+1a&gt;34[a+1a&gt;57[a+1a&gt;49[a+1a&gt;34[a+1a&gt;57[a+1a&gt;55[a+1a&gt;34[a+1a&gt;52[a+1a&gt;51[a+1a&gt;34[a+1a&gt;52[a+1a&gt;57[a+1a&gt;34[a+1a&gt;57[a+1a&gt;55[a+1a&gt;10[a+1a&gt;32[a+1a&gt;43[a+1a&gt;49[a+1a&gt;97[a+1a&gt;10[a+1a&gt;125[a+1a&gt;10[
a+1a&gt;34[a+1a&gt;49[a+1a&gt;48[a+1a&gt;10[a+1a&gt;62[a+1a&gt;48[a+1a&gt;97[a+1a&gt;10[a+1a&gt;123[a+1a&gt;33[a+1a&gt;91[a+1a&gt;97[a+1a&gt;10[a+1a&gt;32[a+1a&gt;34[a+1a&gt;91[a+1a&gt;97[a+1a&gt;10[a+1a&gt;32[a+1a&gt;43[a+1a&gt;49[a+1a&gt;97[a+1a&gt;10[a+1a&gt;125[a+1a&gt;10[a+1a
</pre>
<p>Now all you need to do is copy this snippet into <em>quine.bad</em> (with one minor change) to get a bAdkOde quine:</p>
<pre class="brush: php">
&gt;0a
&gt;62[a+1a&gt;48[a+1a&gt;97[a+1a&gt;10[a+1a&gt;34[a+1a&gt;54[a+1a&gt;50[a+1a&gt;34[a+1a&gt;52[a+1a&gt;56[a+1a&gt;34[a+1a&gt;57[a+1a&gt;55[a+1a&gt;34[a+1a&gt;49[a+1a&gt;48[a+1a&gt;10[a+1a&gt;123[a+1a&gt;33[a+1a&gt;91[a+1a&gt;97[a+1a&gt;10[a+1a&gt;32[a+1a&gt;34[a+1a&gt;54[a+1a&gt;50[a+
1a&gt;39[a+1a&gt;91[a+1a&gt;97[a+1a&gt;34[a+1a&gt;57[a+1a&gt;49[a+1a&gt;34[a+1a&gt;57[a+1a&gt;55[a+1a&gt;34[a+1a&gt;52[a+1a&gt;51[a+1a&gt;34[a+1a&gt;52[a+1a&gt;57[a+1a&gt;34[a+1a&gt;57[a+1a&gt;55[a+1a&gt;10[a+1a&gt;32[a+1a&gt;43[a+1a&gt;49[a+1a&gt;97[a+1a&gt;10[a+1a&gt;125[a+1a&gt;10[
a+1a&gt;34[a+1a&gt;49[a+1a&gt;48[a+1a&gt;10[a+1a&gt;62[a+1a&gt;48[a+1a&gt;97[a+1a&gt;10[a+1a&gt;123[a+1a&gt;33[a+1a&gt;91[a+1a&gt;97[a+1a&gt;10[a+1a&gt;32[a+1a&gt;34[a+1a&gt;91[a+1a&gt;97[a+1a&gt;10[a+1a&gt;32[a+1a&gt;43[a+1a&gt;49[a+1a&gt;97[a+1a&gt;10[a+1a&gt;125[a+1a&gt;10[a+1a
&gt;0a
&quot;62&quot;48&quot;97&quot;10
{![a
 &quot;62&#039;[a&quot;91&quot;97&quot;43&quot;49&quot;97
 +1a
}
&quot;10
&gt;0a
{![a
 &quot;[a
 +1a
}
</pre>
<p>The small change I had to make, was to add <em>>0a</em> at the beginning. If you run this bAdkOde program, it will print its source code as output. So there you have it. A bAdkOde quine, which proves that bAdkOde is Turing Complete. I have to thank <a href="http://dwcope.freeshell.org/">Dave Cope</a> for his quine tutorial, without which it would have been extremely difficult for me to write a bAdkOde quine!</p>
<p><strong>Update (12/26/2009)</strong></p>
<p>Dave mentioned that writing a quine only <em>suggests</em> that a language is Turing Complete, but not that it is. He suggested that I write an interpreter for <a href = "http://www.muppetlabs.com/~breadbox/bf/">Brainfuck</a> which would then prove that bAdkOde is Turing Complete. That was actually my next project!</p>
<p><strong>Update (12/26/2009)</strong></p>
<p>I&#8217;ve been able to provide a <a href="http://vivin.net/2009/12/26/badkode-is-turing-complete/">direct translation</a> of Brainfuck into bAdkOde, which proves the Turing Completeness of bAdkOde.</p>
<br /><a href="http://vivin.net/?p=1335#comments" title="Comments on &quot;A bAdkOde quine (which suggests that bAdkOde is Turing Complete)&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1335" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1335&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2009/12/25/a-badkode-quine-which-suggests-that-badkode-is-turing-complete/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1335" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1335" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1335&#38;type=feed" medium="image" />
	</item>
	</channel>
</rss>

