<?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/"
	>

<channel>
	<title>unsafePerformHack &#187; Esolangs</title>
	<atom:link href="http://olsner.se/category/esolangs/feed/" rel="self" type="application/rss+xml" />
	<link>http://olsner.se</link>
	<description>Perversions in Computer Science</description>
	<lastBuildDate>Tue, 03 May 2011 23:32:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>mod_rewrite revisited</title>
		<link>http://olsner.se/2011/05/04/mod_rewrite-revisited/</link>
		<comments>http://olsner.se/2011/05/04/mod_rewrite-revisited/#comments</comments>
		<pubDate>Tue, 03 May 2011 23:15:49 +0000</pubDate>
		<dc:creator>olsner</dc:creator>
				<category><![CDATA[Esolangs]]></category>
		<category><![CDATA[brainfuck]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[Thue]]></category>

		<guid isPermaLink="false">http://olsner.se/?p=50</guid>
		<description><![CDATA[I suddenly decided to continue my earlier mod_rewrite experiments, BF+Thue in mod_rewrite and my first excursion in mod_rewrite. That code all worked in theory (and for very small examples), but not in practice: Apache runs out of memory. The Problem The basic problem is that for each request, Apache will never return any memory to [...]]]></description>
			<content:encoded><![CDATA[<p>I suddenly decided to continue my earlier mod_rewrite experiments, <a href="http://olsner.se/2008/01/27/bfthue-in-mod_rewrite/">BF+Thue in mod_rewrite</a> and my first <a href="http://olsner.se/2008/01/21/an-excursion-in-mod_rewrite/">excursion in mod_rewrite</a>. That code all worked in theory (and for very small examples), but not in practice: Apache runs out of memory.</p>

<h3>The Problem</h3>

<p>The basic problem is that for each request, Apache will never return any memory to the system &#8211; Apache expects trivial things such as interpreting turing-complete languages to complete quickly and without using more than a smidgeon of memory.</p>

<p>To get around this, I decided to change the &#8220;run-time system&#8221; to make a redirect for each step, i.e. sending an error back to the user-agent and tell it to make a completely new request to a different URL instead of making mod_rewrite loop on the Apache side until it&#8217;s done. Apache allocates the memory for each of these requests separately, and its memory use stays bounded (to the size of the program&#8217;s state anyway).</p>

<p>Implementing this mostly this meant simply replacing the [N] flag (which means jumping to the start of the list of rewrite rules but continue rewriting) with the [R] flag that send a redirect to the client. But of course a few other minor shenanigans were lurking &#8211; primarily around the &#8220;bootstrapping&#8221; step.</p>

<p>The original RTS relied on Apache always having a slash at the start of the URI to bootstrap the program, and relied on Apache not complaining if you removed the slash internally while doing the rewriting. With the new redirecting approach, we will need to continue processing from a URI that has a slash (since it&#8217;s a completely new request from the client) without bootstrapping that URL again. As described in the comments below, we now use a &#8216;q&#8217; to tell a bootstrapped program from an unstarted program instead of checking whether the slash was removed or not.</p>

<p>While I was doing these changes I ended up  I made the compiler output the RTS with the program instead of requiring separate program and RTS config files.</p>

<h3>Thue mod_rewrite Compiler v2.0</h3>

<p>Without further ado, here is the updated Thue to mod_rewrite compiler in its entirety, now including all the RTS it needs! (Again, syntax highlighting is set to Perl although the displayed code is a sed program.)</p>

<p><div>
<pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Bootstrapping part: output the RTS prologue</span>
<span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#123;</span>
i\
<span style="color: #666666; font-style: italic;"># This file should be included in an Apache2 config file for a VirtualHost. \</span>
\
<span style="color: #666666; font-style: italic;"># Enable rewriting\</span>
RewriteEngine on\
&nbsp;
<span style="color: #666666; font-style: italic;"># Hack to make actual files available if you know their names\</span>
i\
RewriteCond <span style="color: #339933;">/</span>var<span style="color: #339933;">/</span>www<span style="color: #339933;">/</span>rewrite<span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">-</span>f\
RewriteRule <span style="color: #339933;">^.*</span>$ <span style="color: #339933;">-</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This should only run on the first inputed string to add the interpreter.</span>
<span style="color: #666666; font-style: italic;"># After each redirect, the client makes a new request and we return to the</span>
<span style="color: #666666; font-style: italic;"># first rule, so this must be safe and idempotent.</span>
<span style="color: #666666; font-style: italic;"># An initial q indicates whether we have boostrapped the program yet, the</span>
<span style="color: #666666; font-style: italic;"># second q is a separator between the output and the current program state.</span>
<span style="color: #666666; font-style: italic;"># We also add a ^ just for fun.</span>
i\
RewriteRule <span style="color: #339933;">^/</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">^</span>q<span style="color: #009900;">&#93;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ <a href="http://perldoc.perl.org/functions/qq.html"><span style="color: #000066;">qq</span></a><span style="color: #339933;">^</span><span style="color: #0000ff;">$1</span>
<span style="color: #666666; font-style: italic;"># Remove any leading slash to simplify the rest of our processing</span>
i\
RewriteRule <span style="color: #339933;">^/</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ <span style="color: #0000ff;">$1</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Add an 'r' to distinguish unchanged strings. This would be the termination</span>
<span style="color: #666666; font-style: italic;"># condition for our rewrite system - if the 'r' is still left after running the</span>
<span style="color: #666666; font-style: italic;"># chain of rewrites, we're done and stop looping.</span>
<span style="color: #666666; font-style: italic;"># This rule is probably completely unneccessary since we'll always redirect</span>
<span style="color: #666666; font-style: italic;"># when rewriting, which should already prevent us from reaching the</span>
<span style="color: #666666; font-style: italic;"># end-of-program rule before we're done.</span>
i\
RewriteRule <span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ <span style="color: #0000ff;">$1</span><a href="http://perldoc.perl.org/functions/qr.html"><span style="color: #000066;">qr</span></a><span style="color: #0000ff;">$2</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Strip comments</span>
<span style="color: #009966; font-style: italic;">s/^#.*$//</span>
<span style="color: #666666; font-style: italic;"># Rewrite question marks since they interfere with mod_rewrite by making the</span>
<span style="color: #666666; font-style: italic;"># productions look like requests with query strings, which apache then splits.</span>
<span style="color: #009966; font-style: italic;">s/\?/Q/g</span>
<span style="color: #666666; font-style: italic;"># .*+(){}^$&lt;&gt;[\\ |]\|\]\)/\\\.*+(){}^$&lt;</span>
<span style="color: #009966; font-style: italic;">s/\([.*+(){}^$&lt;&gt;[\\ |]\|\]\)/\\\1/g</span>
<span style="color: #666666; font-style: italic;"># Skip empty lines</span>
<span style="color: #009966; font-style: italic;">/^$/</span> d
<span style="color: #666666; font-style: italic;"># This is where we're actually doing something worthwhile:</span>
<span style="color: #009966; font-style: italic;">s/^\(.\+\)::=~\(.*\)$/RewriteRule ^q(.*)qr?(.*)\1(.*)$ q$1\2q$2$3 [R,L]/</span>
<span style="color: #009966; font-style: italic;">s/^\(.\+\)::=\(.*\)$/RewriteRule ^q(.*)qr?(.*)\1(.*)$ q$1q$2\2$3 [R,L]/</span>
<span style="color: #666666; font-style: italic;"># End-of-program marker, we want to ignore everything after this line</span>
<span style="color: #009966; font-style: italic;">/^::=$/</span> <span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">s/::=//</span><span style="color: #339933;">;</span> b end <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Restart processing and input next program line</span>
b
&nbsp;
<span style="color: #666666; font-style: italic;"># Output the RTS epilogue after the last line of program text</span>
<span style="color: #339933;">:</span>end
<span style="color: #666666; font-style: italic;"># If the string has changed, we are not yet done. Loop to the beginning and</span>
<span style="color: #666666; font-style: italic;"># remove the changed-marker. This doesn't change the string at all ('-' is a</span>
<span style="color: #666666; font-style: italic;"># special substitution string that passes along the original string), just has</span>
<span style="color: #666666; font-style: italic;"># the [N] flag to trigger apache to restart rewriting.</span>
<span style="color: #666666; font-style: italic;"># This rule is probably completely unneccessary since we'll always redirect</span>
<span style="color: #666666; font-style: italic;"># when rewriting.</span>
a\
RewriteRule <span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #339933;">.</span><span style="color: #0000ff;">*q</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">^</span>r<span style="color: #009900;">&#93;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ <span style="color: #0000ff;">$1</span> <span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># If the string is still unchanged, apply output formatting and send to the</span>
<span style="color: #666666; font-style: italic;"># interface CGI script.</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># First, remove the unchanged-marker 'r'</span>
a\
RewriteRule <span style="color: #339933;">^</span>q<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><a href="http://perldoc.perl.org/functions/qr.html"><span style="color: #000066;">qr</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ <a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #0000ff;">$1</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #0000ff;">$2</span>
<span style="color: #666666; font-style: italic;"># Finally, pass the resulting string to a simple CGI or PHP script to print it</span>
<span style="color: #666666; font-style: italic;"># to the web browser. [R] means to redirect to it, ending rewriting.</span>
a\
RewriteRule <span style="color: #339933;">^</span>q<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ <span style="color: #339933;">/</span><a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a><span style="color: #339933;">.</span>php<span style="color: #339933;">?</span><span style="color: #0000ff;">$1</span> <span style="color: #009900;">&#91;</span>R<span style="color: #009900;">&#93;</span>
<span style="color: #666666; font-style: italic;"># Done. Quit. Get out of here.</span>
<a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a></pre>
</div></p>

<h3>Testing it out</h3>

<p>For obvious reasons I wouldn&#8217;t want you to test this on my server, but if you want to set this up on your own server, setup should be very easy: set up a new virtual host and let the virtual host config stanza include the compiled output of your Thue program.</p>

<p>One &#8220;small&#8221; caveat is that pretty much every HTTP client available will only follow a very small number of recursive redirects. To actually see a largeish program (such as hello world) run to completion you will have to reconfigure your HTTP client to follow a very large (a few hundred thousand) number of redirects. Using the command-line curl program, <code>curl -L -g --max-redirs 300000</code> seems to do the trick.</p>
]]></content:encoded>
			<wfw:commentRss>http://olsner.se/2011/05/04/mod_rewrite-revisited/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Thue in Haskell</title>
		<link>http://olsner.se/2008/01/27/thue-in-haskell/</link>
		<comments>http://olsner.se/2008/01/27/thue-in-haskell/#comments</comments>
		<pubDate>Sun, 27 Jan 2008 21:27:26 +0000</pubDate>
		<dc:creator>olsner</dc:creator>
				<category><![CDATA[Esolangs]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Thue]]></category>

		<guid isPermaLink="false">http://blog.olsner.se/?p=7</guid>
		<description><![CDATA[Yesterday, I wrote a Thue implementation in Haskell. At this point, the implementation is painfully slow (about 35 minutes to run the brainfuck &#8220;Hello World!&#8221; through the BF Thue interpreter on my MacBook, as compared to about 4 minutes for the python Thue implementation run in python 2.5.1). I&#8217;m thinking I should a) should use [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote a <a href="http://esolangs.org/wiki/Thue">Thue</a> implementation in Haskell. At this point, the implementation is painfully slow (about 35 minutes to run the brainfuck &#8220;Hello World!&#8221; through the BF Thue interpreter on my MacBook, as compared to about 4 minutes for the <a href="http://catseye.tc/projects/thue/src/thue.py">python Thue implementation</a> run in python 2.5.1). I&#8217;m thinking I should a) should use ByteString&#8217;s instead of String, and b) use some smarter algorithm for matching the rules (instead of <code>untilDone (foldl applyRule input rules)</code>, it&#8217;d be something more like compiling an automaton out of all rules and <code>untilDone (run automaton)</code>). By reducing the number of rules (eliminating the 50 or so #::=# rules, which are comments in idiomatic Thue but don&#8217;t strictly need special processing), I could squeeze about twice the performance out of it &#8211; this optimization brought the execution time down to 35 minutes. I need at least 10x the performance before this program is anything to brag about!</p>

<p>Another idea for the future is to implement parallel Thue by splitting up the state and then replacing in the parts until no replacements can be done in parallel, then running a few iterations serially on the whole state. You could also just synchronize once and move the split-point &#8211; which would eliminate the problem of deciding when to start going parallell again since you&#8217;d never really go serial. With the language designed to allow random replacement order, any valid Thue program already contains the synchronization needed to make it evaluate the right thing and e.g. output things in the correct order, so this is perfectly safe provided you have a correct implementation and correct programs.</p>

<p>Although it&#8217;s fun to make things go fast, I think parallel Thue would be much more interesting as it might even be something that&#8217;s never been done before!</p>

<p>Anyway, if you want to try it out, here&#8217;s my <a href="http://darcs.olsner.se/Thue">Thue darcs repository</a>, containing the haskell source and the <a href="http://fvdp.homestead.com/files/eso_index.html#BFThue">BF interpreter in Thue</a> with the BF Hello world program I&#8217;ve been using for testing. It should take exactly 287197 steps (and heaps of time) to complete the execution and print <code>"Hello world!\n"</code> as a series of binary numbers separated by underscores.</p>
]]></content:encoded>
			<wfw:commentRss>http://olsner.se/2008/01/27/thue-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BF+Thue in mod_rewrite</title>
		<link>http://olsner.se/2008/01/27/bfthue-in-mod_rewrite/</link>
		<comments>http://olsner.se/2008/01/27/bfthue-in-mod_rewrite/#comments</comments>
		<pubDate>Sun, 27 Jan 2008 03:30:02 +0000</pubDate>
		<dc:creator>olsner</dc:creator>
				<category><![CDATA[Esolangs]]></category>
		<category><![CDATA[brainfuck]]></category>
		<category><![CDATA[mod_rewrite]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[Thue]]></category>

		<guid isPermaLink="false">http://blog.olsner.se/?p=8</guid>
		<description><![CDATA[Based on the mod_rewrite experiment from the other week, I hacked up a small Thue to mod_rewrite compiler in sed. For testing, I have used the brainfuck interpreter in Thue (as far as I can see, this is like the only example of a &#8220;real-world&#8221; Thue program). I tested this on the small 3+5 examples [...]]]></description>
			<content:encoded><![CDATA[<p>Based on the <a href="http://olsner.se/2008/01/21/an-excursion-in-mod_rewrite/">mod_rewrite experiment from the other week</a>, I hacked up a small <a href="http://www.esolangs.org/wiki/Thue">Thue</a> to mod_rewrite compiler in sed. For testing, I have used the <a href="http://fvdp.homestead.com/files/eso_bfi.t.txt">brainfuck interpreter in Thue</a> (as far as I can see, this is like the only example of a &#8220;real-world&#8221; Thue program). I tested this on the small 3+5 examples included in the BF Thue distribution and tried to test it on BF Hello World, but apache runs out of memory before completing the rewriting <img src='http://olsner.se/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

<p>Anyway, in this BF interpreter, the interpretation is bootstrapped by a circumflex, &#8216;^&#8217;, so the interpreter placeholder I introduced last week had a natural use allowing us to simply enter the brainfuck program as a URL, followed by a colon and input as binary underscore-separated numbers (terminated by <code>0_</code>), and have patience.</p>

<p>The Thue interpreter needs to keep track of a few bits of state &#8211; all of this has to be encoded in some reliable way in the URL we&#8217;re continually rewriting:</p>

<ul>
<li>Have we added the interpreter? For BF+Thue, this is &#8216;^&#8217; which is really something that has leaked from this one specific Thue program into the interpreter &#8211; which is really ugly, but hey, at least it was easy. Anyway, since we&#8217;re in the root of the virtual host, we can use the leading slash to encode this information &#8211; simply remove it after adding the interpreter, and we can match on <code>^/(.*)$</code> to determine whether we should add the bootstrap. (I have since realized that there is not much that prevents a malicious Thue program to output a slash as its first character, thereby breaking my entire system. Well, well, in version 2.0 I&#8217;ll replace this with a more robust encoding system.)</li>
<li>The current output string. Stored in the first part of the URL, terminated by a &#8216;q&#8217; (I&#8217;m having real trouble here with selecting characters unused in the program and all its possible states &#8211; otherwise matching would be a bitch)</li>
<li>Did we rewrite anything in the last pass over the state? If nothing was rewritten, we must terminate the program. After the &#8216;q&#8217; that terminates the output string, we store an &#8216;r&#8217; before starting going through the rules and then every time we apply a rule we remove the &#8216;r&#8217;. If the &#8216;r&#8217; is still there after having tried every rule in the program, we&#8217;re done and pass the output string to the printer PHP script.</li>
<li>Finally, the current program state is stored after the q and r markers and goes on to the end of the URL.</li>
</ul>

<p>In the end, this became something quite beautiful in its ugliness, although it could certainly need some robustness work. For example, it blindly relies on the Thue program to never ever use q or r anywhere (and probably a few other things I haven&#8217;t thought of). This should be changed to a totally robust encoding of anything that the rewrite rules should operate on as data, that can&#8217;t interfere with the things that are used as control structures and data formatting by the rewrite rules.</p>

<p>Here is the translator sed script in all its regexp-wtf glory, highlighted as Perl since GeSHi unfortunately doesn&#8217;t have sed highlighting.</p>

<p><div>
<pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Remove comments</span>
<span style="color: #009966; font-style: italic;">s/^#.*$//</span>
<span style="color: #666666; font-style: italic;"># Rewrite question marks since they interfere with mod_rewrite by making the</span>
<span style="color: #666666; font-style: italic;"># productions look like requests with query strings, which apache then splits.</span>
<span style="color: #009966; font-style: italic;">s/\?/Q/g</span>
<span style="color: #666666; font-style: italic;"># Escape characters with special meanings in regexps.</span>
<span style="color: #009966; font-style: italic;">s/\([.*+(){}^$&lt;&gt;[\\ |]\|\]\)/\\\1/g</span>
<span style="color: #666666; font-style: italic;"># Remove the ::= terminating line since it doesn't define a rule.</span>
<span style="color: #009966; font-style: italic;">s/^::=$//</span>
<span style="color: #666666; font-style: italic;"># Output rule: ~asf means to print the string &quot;asf&quot; and remove the matched string.</span>
<span style="color: #009966; font-style: italic;">s/^\(.*\)::=~\(.*\)$/RewriteRule ^(.*)qr?(.*)\1(.*)$ $1\2q$2$3 [N]/</span>
<span style="color: #666666; font-style: italic;"># Normal rule: match substring and replace it</span>
<a href="http://perldoc.perl.org/functions/s.html"><span style="color: #000066;">s</span></a><span style="color: #339933;">/^</span>\<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span>\<span style="color: #009900;">&#41;</span><span style="color: #339933;">::=</span>\<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span>\<span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/</span>RewriteRule <span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span><a href="http://perldoc.perl.org/functions/qr.html"><span style="color: #000066;">qr</span></a><span style="color: #339933;">?</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>\<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ <span style="color: #0000ff;">$1</span><a href="http://perldoc.perl.org/functions/q.html"><span style="color: #000066;">q</span></a><span style="color: #0000ff;">$2</span>\<span style="color: #cc66cc;">2</span><span style="color: #0000ff;">$3</span> <span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span></pre>
</div></p>

<p>Note that the resulting rewrite rules are meant to be inserted into the mod_rewrite embedded language bootstrap I built in the previous post.</p>
]]></content:encoded>
			<wfw:commentRss>http://olsner.se/2008/01/27/bfthue-in-mod_rewrite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An excursion in mod_rewrite</title>
		<link>http://olsner.se/2008/01/21/an-excursion-in-mod_rewrite/</link>
		<comments>http://olsner.se/2008/01/21/an-excursion-in-mod_rewrite/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 01:51:32 +0000</pubDate>
		<dc:creator>olsner</dc:creator>
				<category><![CDATA[Esolangs]]></category>

		<guid isPermaLink="false">http://blog2.olsner.se/?p=4</guid>
		<description><![CDATA[While setting up my blosxom blog&#8217;s apache vhost to rewrite URL:s to pass them by the blosxom CGI script, I happened to set up a redirection loop with apache&#8217;s mod_rewrite module &#8211; by accident &#8211; and realized that mod_rewrite might be Turing complete. (Always my instinct when something has the ability to loop.) After some [...]]]></description>
			<content:encoded><![CDATA[<p>While setting up my blosxom blog&#8217;s apache vhost to rewrite URL:s to pass them by the blosxom CGI script, I happened to set up a redirection loop with apache&#8217;s mod_rewrite module &#8211; by accident &#8211; and realized that mod_rewrite might be Turing complete. (Always my instinct when something has the ability to loop.) After some more thinking, I realized that this apache module really is a bona fide <a href="http://en.wikipedia.org/wiki/String_rewriting_system">String rewriting system</a> which can be used to implement both <a href="http://en.wikipedia.org/wiki/Tag_system">Tag systems</a> and <a href="http://en.wikipedia.org/wiki/Cellular_automaton">Cellular automata</a>, both of which can be Turing complete. Actually, since mod_rewrite can implement <em>any</em> tag system or cellular automaton, mod_rewrite <em>is</em> Turing complete.</p>

<p>So, if one felt like it, mod_rewrite can be configured with a Turing complete set of rewrite rules such that each accessed URL is a program, and the final URL is the out-data (or a link to a CGI-script that gives back the final output to the user, with suitable decoding). It&#8217;s possible (trivial, even) to add a first rewrite rule that bootstraps the rewriting with an interpreter string. It&#8217;s theoretically possible to have this be something like a Haskell interpreter &#8211; <em>imagine the possibilities!</em> &#8211; a dog-slow Haskell interpreter implemented in an apache config file!</p>

<p>Here&#8217;s a proof-of-concept implementation of a rewrite system framework in mod_rewrite. All that&#8217;s missing is a set of rewrite rules to implement something interesting, like <a href="http://en.wikipedia.org/wiki/99_Bottles_of_Beer">99-bottles-of-beer</a>, <a href="http://my.opera.com/cstrep/blog/2008/01/13/have-you-ever-heard-bon-digi">bon-digi</a> or a <a href="http://en.wikipedia.org/wiki/Universal_Turing_machine">Universal Turing Machine</a>. Note that apache by default limits the number of <a href="http://httpd.apache.org/docs/2.0/mod/core.html#limitinternalrecursion">&#8220;internal recursions&#8221;</a> to 10, and with any serious use of this you&#8217;re likely to hit that limit. Increase as needed.</p>

<p><div>
<pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;"># This file should be included in an Apache2 config file for a VirtualHost.</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># Enable rewriting</span>
<span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">on</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># First off, set a sensible log level so that you can see what goes wrong</span>
<span style="color: #00007f;">RewriteLogLevel</span> <span style="color: #ff0000;">3</span>
<span style="color: #00007f;">RewriteLog</span> <span style="color: #7f007f;">&quot;/var/log/apache2/rewrite/rewrite.log&quot;</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># This should only run on the first inputed string to add the interpreter.</span>
<span style="color: #adadad; font-style: italic;"># After each run of substitutions, we return to the first rule, so this must</span>
<span style="color: #adadad; font-style: italic;"># be safe and idempotent. So, we add the interpeter prefixed by '-' if the</span>
<span style="color: #adadad; font-style: italic;"># string does not start with '-'. (This could also be used for input programs</span>
<span style="color: #adadad; font-style: italic;"># to replace the interpreter if they like. We could also simply require input</span>
<span style="color: #adadad; font-style: italic;"># programs to come with their own interpreters and do nothing here.)</span>
<span style="color: #00007f;">RewriteRule</span> ^([^-].*)$ -INTERPRETER$<span style="color: #ff0000;">1</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># Add a comma to distinguish unchanged strings. This would be the termination</span>
<span style="color: #adadad; font-style: italic;"># condition for our rewrite system. Of course there are other ways to do this,</span>
<span style="color: #adadad; font-style: italic;"># for example a regexp which evaluates into the terminate-rule at the bottom.</span>
<span style="color: #adadad; font-style: italic;"># Doesn't need to be a comma, of course. If this character is part of the input</span>
<span style="color: #adadad; font-style: italic;"># alphabet, it could confuse some things though.</span>
<span style="color: #00007f;">RewriteRule</span> ^(.*)$ ,$<span style="color: #ff0000;">1</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># This is where the implementation of the entire string rewriting system would</span>
<span style="color: #adadad; font-style: italic;"># be. The general format is ,xyz -&gt; xy'z - remove the first comma to mark the</span>
<span style="color: #adadad; font-style: italic;"># string as matched and changed (if there was a comma to start with), then</span>
<span style="color: #adadad; font-style: italic;"># change the string y to y' (with x and z left unchanged).</span>
<span style="color: #00007f;">RewriteRule</span> ^,?(.*)foo(.*)$ $1bar$<span style="color: #ff0000;">2</span>
<span style="color: #adadad; font-style: italic;"># ... etc until your rewrite rules implement some turing-complete language, or</span>
<span style="color: #adadad; font-style: italic;"># a program that does something interesting.</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># If the string has changed, we are not yet done. Loop to the beginning and</span>
<span style="color: #adadad; font-style: italic;"># remove the changed-marker. This doesn't change the string at all ('-' is a</span>
<span style="color: #adadad; font-style: italic;"># special substitution string that simple uses the original string), just has</span>
<span style="color: #adadad; font-style: italic;"># the [N] flag to trigger apache to restart rewriting.</span>
<span style="color: #00007f;">RewriteRule</span> ^[^,].*$ - [N]
&nbsp;
<span style="color: #adadad; font-style: italic;"># If the string is still unchanged, terminate and apply output-rewriting.</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># First, remove the comma</span>
<span style="color: #00007f;">RewriteRule</span> ^,(.*)$ $<span style="color: #ff0000;">1</span> 
<span style="color: #adadad; font-style: italic;"># Since the example string INTERPRETER is not actually interpreted in this</span>
<span style="color: #adadad; font-style: italic;"># example, remove it after applying the rewrite rules.</span>
<span style="color: #00007f;">RewriteRule</span> ^-INTERPRETER(.*)$ $<span style="color: #ff0000;">1</span>
<span style="color: #adadad; font-style: italic;"># Finally, pass the resulting string to a simple CGI or PHP script to print it</span>
<span style="color: #adadad; font-style: italic;"># to the web browser. [L] means to abort rewriting here.</span>
<span style="color: #00007f;">RewriteRule</span> ^(.*)$ /print.php?$<span style="color: #ff0000;">1</span> [L]</pre>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://olsner.se/2008/01/21/an-excursion-in-mod_rewrite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

