<?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>Algorithm.co.il &#187; Projects</title>
	<atom:link href="http://www.algorithm.co.il/blogs/category/projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Tue, 21 Jun 2011 20:37:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Javascript Element Creator</title>
		<link>http://www.algorithm.co.il/blogs/programming/javascript-element-creator/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/javascript-element-creator/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 14:29:22 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[startup]]></category>
		<category><![CDATA[web-design]]></category>
		<category><![CDATA[element creator]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=542</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/javascript-element-creator/' addthis:title='Javascript Element Creator'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Some time ago I was working on optimizing the client side code of my website, plnnr.com, an online trip planner. This website does automatic trip planning, and the problem was that recalculating trips was slow. After profiling, I found out &#8230; <a href="http://www.algorithm.co.il/blogs/programming/javascript-element-creator/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/javascript-element-creator/' addthis:title='Javascript Element Creator' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Some time ago I was working on optimizing the client side code of my website, <a href="http://plnnr.com/">plnnr.com, an online trip planner</a>.<br />
This website does automatic trip planning, and the problem was that recalculating trips was slow. After profiling, I found out that most of the time wasn&#8217;t actually taken up by the algorithm, but by the UI. Rendering the trip to html was the costly part. The process was like so:</p>
<p>Client-side Javascript code generates new trip prefs -> application calculates new trip -> Client-side Javascript gets the new trip, and creates new html.</p>
<p>It&#8217;s important to note that the app is &#8220;ajax based&#8221;, so the actual trip html was generated by the Javascript code, and not the server. At the time I was using Mochikit to generate the new html. Mochikit has a pretty nifty API for generating html, but it&#8217;s quite a bit on the slow side. Basically, this API is a wrapper around createElement.</p>
<p>Well, first I did a little test, and found out that generating html with cloneNode and innerHTML is much faster than createElement. Still, there was a problem &#8211; I needed to generate many similar elements &#8211; similar but not identical. Consider entries on a trip itinerary &#8211; they all look the same, yet each one has a different name, a different time string, and a different onclick event.</p>
<p>What I needed was a Javascript based html template library. My requirements:<br />
1. Speed. Html had to be generated quickly.<br />
2.  Expressiveness. It had to be able to create pretty arbitrary html with a given context. For example, an anchor element (&lt;a> tag) with a given href property, and a given text content.<br />
3. References to inner elements: Many elements inside the generated html need various events attached to them, or various code processing. This should be easy to achieve.<br />
4. The library has to allow the template html to be written as html, and not only as javascript strings.</p>
<p>So, I sat down with <a href="http://twitter.com/daonb">Benny</a>, and we wrote the Javascript Element Creator, which we are now releasing under the BSD license. I originally wrote it to work with Mochikit and the Sizzle library, and Benny changed his version to worked with jquery.</p>
<p>After adding the code to my project, I got two things: first, everything worked much, much faster. Second, it was much easier changing the generated html when it was generated according to a template, and not directly in code.</p>
<h4>Instructions</h4>
<p>1. Write your html somewhere visible to the javascript code. Add the &#8220;template&#8221; class to the upper node, and the id will be the name of the template. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;some_div&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>2. Similarly to other template engines, add double brackets to signify where text should be inserted:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;some_div&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;[[link_url]]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>[[link_text]]<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>3. Create a creator object. It will &#8220;collect&#8221; your template, and will make it available to your code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> creator <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ElementCreator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>4. Generate your DOM object, and add it to the document;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> creator.<span style="color: #660066;">generate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;some_div&quot;</span><span style="color: #339933;">,</span>
                           <span style="color: #009900;">&#123;</span>link_url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/url/'</span><span style="color: #339933;">,</span>
                            link_text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'hello world'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
appendChildNodes<span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">,</span> obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>The code</h4>
<p>We decided to publish for now only the jquery version. I might publish the mochikit version as well at a later date. Since Benny wrote the jquery version, he also wrote the tests for that version.</p>
<p>All in all, the final code is pretty short, and could probably be even shorter. Still, it&#8217;s good enough, and gave me a very real performance boost.</p>
<p>Here is the <a href="http://www.algorithm.co.il/sitecode/elementcreator.zip">code</a>, have fun with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/javascript-element-creator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Call for Volunteers: Open Knesset &#8211; oknesset.org</title>
		<link>http://www.algorithm.co.il/blogs/projects/call-for-volunteers-open-knesset-oknesset-org/</link>
		<comments>http://www.algorithm.co.il/blogs/projects/call-for-volunteers-open-knesset-oknesset-org/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 20:05:10 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[open knesset]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=533</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/projects/call-for-volunteers-open-knesset-oknesset-org/' addthis:title='Call for Volunteers: Open Knesset &#8211; oknesset.org'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Over the last few weeks, I&#8217;ve been lightly involved in work on open knesset. Mostly I&#8217;ve been helping two of the main developers, Benny and Ofri, and joining the discussions on the discussion group. (For the non-Israelis: the Knesset is &#8230; <a href="http://www.algorithm.co.il/blogs/projects/call-for-volunteers-open-knesset-oknesset-org/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/projects/call-for-volunteers-open-knesset-oknesset-org/' addthis:title='Call for Volunteers: Open Knesset &#8211; oknesset.org' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Over the last few weeks, I&#8217;ve been lightly involved in work on <a href="http://oknesset.org/">open knesset</a>.<br />
Mostly I&#8217;ve been helping two of the main developers, <a href="http://twitter.com/daonb">Benny</a> and <a href="http://twitter.com/ofri">Ofri</a>, and joining the discussions on the <a href="http://groups.google.com/group/open-knesset/">discussion group</a>.</p>
<p>(For the non-Israelis: <a href="http://en.wikipedia.org/wiki/Knesset">the Knesset</a> is Israel&#8217;s congress, where laws are passed.)</p>
<p>The website&#8217;s mission is to improve Israeli citizens&#8217; involvement in our democracy, and the first step in doing so is giving people more information. Ever wanted to know who keeps his promises? Who voted how? Who never votes? Which Member of Knesset is never present in discussions and votes?</p>
<p>Open Knesset is the place to put this information. If you know a bit of Python &#038; Django, you can join development either on the content harvesting front, or on the front-end front (pun not intended :).</p>
<p>&#8220;What about algorithms?&#8221; you may ask, or &#8220;what does this project has to do with algorithm.co.il?&#8221;. Well, there&#8217;s also plenty of room for innovation and interesting features. For example, finding the correlation between Members of Knesset that always vote together. Knowing which vote is for which law. Understanding if the vote is for or against that law. Plotting the party graph, and working with it. These are all things that still need to be done. See <a href="http://wiki.hamakor.org.il/index.php/Open-Knesset/%D7%AA%D7%A6%D7%95%D7%92%D7%95%D7%AA">these graphical examples</a> to see what I&#8217;m talking about.</p>
<p>The website is still in its infancy, but it already has lots of content and lots of features. Nevertheless, it still needs more work.<br />
If you&#8217;re looking for an open-source project where your work will have great impact, this might just be it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/projects/call-for-volunteers-open-knesset-oknesset-org/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fast Peak Autocorrelation</title>
		<link>http://www.algorithm.co.il/blogs/math/fast-peak-autocorrelation/</link>
		<comments>http://www.algorithm.co.il/blogs/math/fast-peak-autocorrelation/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 12:58:55 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Utility Functions]]></category>
		<category><![CDATA[autocorellation]]></category>
		<category><![CDATA[automatic guitar improviser]]></category>
		<category><![CDATA[geekcon]]></category>
		<category><![CDATA[pyimprov]]></category>
		<category><![CDATA[signal processing]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=413</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/fast-peak-autocorrelation/' addthis:title='Fast Peak Autocorrelation'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>So, I was at geekcon. It was a blast. There were many interesting projects, and I didn&#8217;t get to play with them all. I did get to work a bit on the Lunar Lander from last year, and this year &#8230; <a href="http://www.algorithm.co.il/blogs/math/fast-peak-autocorrelation/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/fast-peak-autocorrelation/' addthis:title='Fast Peak Autocorrelation' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>So, I was at <a href="http://geekcon.org/">geekcon</a>. It was a blast.<br />
<img src="http://www.algorithm.co.il/sitecode/scaled_lunar_lander.jpg" alt="The lunar lander" style="border: 1px solid black; float: right; margin-left: 10px;"/><br />
There were many interesting projects, and I didn&#8217;t get to play with them all. I did get to work a bit on the Lunar Lander from last year, and this year it was finished successfully. My part was the PC game which interfaced with the microcontroller controlling the lander. As you probably guessed, it was written in Python.</p>
<p>This year, as promised, I worked on the Automatic Improviser. I worked on it with Ira Cherkes.</p>
<p>While the final version worked, it didn&#8217;t work well enough, and there is still much work to be done. Still, we had excellent progress.<br />
By the way, I know this subject has been tackled before, and I still wanted to try it myself, without reading too much literature about it.</p>
<p>One of the components of the system is a beat recognizer. My idea to discover the beat is simple: find the envelope (similar to removing AM modulation), and then find the low &#8220;frequency&#8221; of the envelope.<br />
Instead of doing a Fast Fourier Transform for beat recognition, we were advised that autocorellation will do the trick better and faster. However, when trying to autocorellate using scipy.signal.correlate we discovered that autocorellation was too slow for real time beat recognition, and certainly wasteful.</p>
<p>To solve this issue, we decided to first do peak detection on the envelope, and then autocorellate the peaks. Since there shouldn&#8217;t be too many peaks, this has the potential of being really quick. However, there was no standard function to do autocorellation of peaks, so we implemented it ourselves. We were pretty rushed, so we worked fast. Here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> autocorrelate_peaks<span style="color: black;">&#40;</span>peaks<span style="color: black;">&#41;</span>:
    peaks_dict = <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>peaks<span style="color: black;">&#41;</span>
    indexes = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span>peaks_dict.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    deltas = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> indexes:
        <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> indexes:
            <span style="color: #ff7700;font-weight:bold;">if</span> j<span style="color: #66cc66;">&gt;</span>i:
                <span style="color: #ff7700;font-weight:bold;">continue</span>
            deltas.<span style="color: black;">add</span><span style="color: black;">&#40;</span>i-j<span style="color: black;">&#41;</span>
&nbsp;
    result = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> deltas:
        moved = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span>i+d <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> indexes<span style="color: black;">&#41;</span>
        to_mult = moved <span style="color: #66cc66;">&amp;</span> indexes
        <span style="color: #ff7700;font-weight:bold;">assert</span> to_mult <span style="color: #66cc66;">&lt;</span>= indexes
        s = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>peaks_dict<span style="color: black;">&#91;</span>i-d<span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>peaks_dict<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> to_mult<span style="color: black;">&#41;</span>
        result<span style="color: black;">&#91;</span>d<span style="color: black;">&#93;</span> = s
    <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></div></div>

<p>This function takes as input a list of tuples, each of the form (peak_index, peak_value), and returns a mapping between non-zero corellation offsets and their values.<br />
Here&#8217;s is a sample run:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">In <span style="color: black;">&#91;</span><span style="color: #ff4500;">7</span><span style="color: black;">&#93;</span>: autocorrelate_peaks<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">7</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">7</span><span style="color: black;">&#93;</span>: <span style="color: black;">&#123;</span><span style="color: #ff4500;">0</span>: <span style="color: #ff4500;">14</span>, <span style="color: #ff4500;">2</span>: <span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">3</span>: <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">5</span>: <span style="color: #ff4500;">6</span><span style="color: black;">&#125;</span></pre></div></div>

<p>After implementing this function, our recognition loop was back to real-time, and we didn&#8217;t have to bother with optimizing again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/math/fast-peak-autocorrelation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preparing PyImprov for GeekCon on Friday</title>
		<link>http://www.algorithm.co.il/blogs/programming/preparing-pyimprov-for-geekcon-on-friday/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/preparing-pyimprov-for-geekcon-on-friday/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 12:15:11 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Sound]]></category>
		<category><![CDATA[geekcon]]></category>
		<category><![CDATA[pyimprov]]></category>
		<category><![CDATA[PyTuner]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=407</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/preparing-pyimprov-for-geekcon-on-friday/' addthis:title='Preparing PyImprov for GeekCon on Friday'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>A long long time ago, I wrote Pytuner. It was one of the first projects I published on this website. For a long time it just sat there, doing nothing, while the library it&#8217;s based on &#8211; PyMedia, wasn&#8217;t being &#8230; <a href="http://www.algorithm.co.il/blogs/programming/preparing-pyimprov-for-geekcon-on-friday/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/preparing-pyimprov-for-geekcon-on-friday/' addthis:title='Preparing PyImprov for GeekCon on Friday' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>A long long time ago, I wrote <a href="http://www.algorithm.co.il/blogs/index.php/pytuner/">Pytuner</a>. It was one of the first projects I published on this website.<br />
For a long time it just sat there, doing nothing, while the library it&#8217;s based on &#8211; PyMedia, wasn&#8217;t being maintained anymore, and PyTuner could only work on Python 2.4.</p>
<p>Enter <a href="http://geekcon.org/">GeekCon</a> &#8211; GeekCon is a get together of people looking to work on their wildest fantasy projects &#8211; things that they don&#8217;t get do because of their regular work. Last yet I worked on a real life 3d lunar lander, and this year I thought I&#8217;d take the opportunity to work on PyImprov &#8211; my wild fantasy project.<br />
The idea is simple &#8211; I&#8217;ll play some simple chords, and the script will improvise some blues solo. To that purpose, I wrote (also a long time ago) a chord recognizer. Now I&#8217;m missing a beat recognizer, and a simple improviser, which I plan to complete during the GeekCon weekend.</p>
<p>In preparation for the event, I&#8217;ve opened up a subversion repository for PyImprov on <a href="http://www.assembla.com/wiki/show/pyimprov">Assembla</a>, and patched the scripts to work with <a href="http://people.csail.mit.edu/hubert/pyaudio/">PyAudio</a> instead of PyMedia.</p>
<p>So, onwards to GeekCon, see you on the other side, with a guitar and a Python script in hand!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/preparing-pyimprov-for-geekcon-on-friday/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PyKoan &#8211; The Logic Game</title>
		<link>http://www.algorithm.co.il/blogs/math/pykoan-the-logic-game/</link>
		<comments>http://www.algorithm.co.il/blogs/math/pykoan-the-logic-game/#comments</comments>
		<pubDate>Sun, 01 Jun 2008 18:27:07 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Assembla]]></category>
		<category><![CDATA[Expression Trees]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[PyKoan]]></category>
		<category><![CDATA[Zendo]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=108</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/pykoan-the-logic-game/' addthis:title='PyKoan &#8211; The Logic Game'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>As you can probably tell, I&#8217;m back from my undeclared hiatus. I&#8217;ve got lots of stuff to talk about, and I&#8217;ll be starting with PyKoan, one small project I&#8217;ve been working on lately in my spare time. A few weeks &#8230; <a href="http://www.algorithm.co.il/blogs/math/pykoan-the-logic-game/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/pykoan-the-logic-game/' addthis:title='PyKoan &#8211; The Logic Game' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>As you can probably tell, I&#8217;m back from my undeclared hiatus. I&#8217;ve got lots of stuff to talk about, and I&#8217;ll be starting with PyKoan, one small project I&#8217;ve been working on lately in my spare time.</p>
<p>A few weeks ago I stumbled upon an <a href="http://en.wikipedia.org/wiki/Zendo_(game)">article in wikipedia</a>, regarding a logic game. This game fascinated me, especially because of the <a href="http://en.wikipedia.org/wiki/G%C3%B6del%2C_Escher%2C_Bach">&#8220;Godel Escher Bach&#8221;</a> connection. Quoting from Wikipedia:</p>
<blockquote><p><strong>Zendo</strong> is a game of inductive logic designed by Kory Heath in which one player (the &#8220;Master&#8221;) creates a rule for structures (&#8220;koans&#8221;) to follow, and the other players (the &#8220;Students&#8221;) try to discover it by building and studying various koans which follow or break the rule. The first student to correctly state the rule wins.</p></blockquote>
<p>As it happens I&#8217;m also taking a mathematical logic course this semester. Having read about the game, I wanted to write a similar computer game. Hence &#8211; PyKoan.</p>
<p>PyKoan is a game where the goal is to discover some logical rule, for example, &#8220;For each x holds x%2 == 0&#8243;. This rule is applied to a koan &#8211; a list of integers. An example koan that &#8220;has Buddha nature&#8221; (follows the rule) is [0,2,8]. One which doesn&#8217;t is [1].</p>
<p>To implement this idea, I wrote an implementation of an expression tree very similar to the one used in vial, but with a different implemented language. I also did some experimentation with the design. Since I&#8217;ve been talking a lot about expression trees without giving a full explanation, in a future post I&#8217;ll write about the implementation used in PyKoan.</p>
<p>So far I didn&#8217;t code a lot of the game, just the expression tree framework, and a simple rule builder. When using Python&#8217;s interactive prompt, one can get a taste of how the game might feel:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">In <span style="color: black;">&#91;</span><span style="color: #ff4500;">19</span><span style="color: black;">&#93;</span>: r = rulegen.<span style="color: black;">create_rule</span><span style="color: black;">&#40;</span>rulegen.<span style="color: black;">easy_grammer</span>, rulegen.<span style="color: black;">easy_grammer_start</span><span style="color: black;">&#41;</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span>: r.<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">False</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">21</span><span style="color: black;">&#93;</span>: r.<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">21</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">False</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">22</span><span style="color: black;">&#93;</span>: r.<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">22</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">False</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">23</span><span style="color: black;">&#93;</span>: r.<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">23</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">True</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">24</span><span style="color: black;">&#93;</span>: r.<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">24</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">True</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">25</span><span style="color: black;">&#93;</span>: r.<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">25</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">True</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">26</span><span style="color: black;">&#93;</span>: r.<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">26</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">False</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">27</span><span style="color: black;">&#93;</span>: r.<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">27</span><span style="color: black;">&#93;</span>: <span style="color: #008000;">False</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">28</span><span style="color: black;">&#93;</span>: <span style="color: #ff7700;font-weight:bold;">print</span> r
x exists such that x == <span style="color: #ff4500;">2</span></pre></div></div>

<p>Here is how I would generate such an expression manually:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">In <span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>: <span style="color: #ff7700;font-weight:bold;">from</span> exptree <span style="color: #ff7700;font-weight:bold;">import</span> exps
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>: exps.<span style="color: black;">Exists</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'x'</span>,exps.<span style="color: black;">Eq</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'x'</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>: exps.<span style="color: black;">Exists</span><span style="color: black;">&#40;</span>exps.<span style="color: black;">Var</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>, exps.<span style="color: black;">Eq</span><span style="color: black;">&#40;</span>exps.<span style="color: black;">Var</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>, exps.<span style="color: black;">Imm</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>: <span style="color: #ff7700;font-weight:bold;">print</span> _
x exists such that x == <span style="color: #ff4500;">2</span></pre></div></div>

<p>The game has many interesting possibilities for research, for example, computer players. Other possibilities include &#8220;just&#8221; guessing koans (not the rule itself), creating interesting and playable rules, and so on. There&#8217;s a lot to do.</p>
<p>This time, instead of just publishing the (unfinished) code, I decided to do something different. I&#8217;ve opened a <a href="http://www.assembla.com/spaces/pykoan/">space in assembla</a>, with public read access. I&#8217;m opening this project for participation: if you want to join then leave a comment, or send me an email.</p>
<p>(Since Assembla seems to be going through some connectivity issues right now, <a href="http://www.algorithm.co.il/sitecode/pykoan.tar.gz">here&#8217;s a link</a> to a copy).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/math/pykoan-the-logic-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Issues in Writing a VM &#8211; Part 2</title>
		<link>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-2/</link>
		<comments>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-2/#comments</comments>
		<pubDate>Fri, 28 Mar 2008 23:16:59 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[delay slot]]></category>
		<category><![CDATA[Disassembly]]></category>
		<category><![CDATA[Distorm]]></category>
		<category><![CDATA[Python generators]]></category>
		<category><![CDATA[vial]]></category>
		<category><![CDATA[VM]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/python/issues-in-writing-a-vm-part-2/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-2/' addthis:title='Issues in Writing a VM &#8211; Part 2'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Writing a VM capable of executing expression trees is different from writing a VM for executing assembly instructions. Here I&#8217;ll cover several issues stemming from this difference. The first group of issues involve generality. Supporting a specific instruction set is &#8230; <a href="http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-2/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-2/' addthis:title='Issues in Writing a VM &#8211; Part 2' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.algorithm.co.il/sitecode/tim_screenshot_small.jpg" border="2" alt="The Incredible Machine" /></p>
<p>Writing a VM capable of executing expression trees is different from writing a VM for executing assembly instructions. Here I&#8217;ll cover several issues stemming from this difference.</p>
<p>The first group of issues involve generality. Supporting a specific instruction set is a straightforward job, even if a hard one. Vial has to support multiple platforms and that&#8217;s a little tricky. These are just a few of the problems:</p>
<ol>
<li><strong>Different registers for each instruction set</strong>. This one is easy enough to solve. Just have a node in the expression tree with some value to indicate the register.</li>
<li><strong>Register overlaps</strong>. A change to one register implies a change to its parents and its children. Consider RAX->EAX->AX->AH,AL. Changing EAX will affect all of the registers in the hierarchy.<br />
To handle this, we wrote a CPU class to keep all the info about the platform, including register overlaps.</li>
<li><strong>Delayed branches</strong>. Some platforms have branch delay slots. This means that after any branch instruction, right before the branch is taken, instructions in the delayed slots are executed anyway. For instance, SPARC has three delay slots, while MIPS has just one. This isn&#8217;t an easy issue to solve, and for now we didn&#8217;t tackle it. We&#8217;ve got a few ideas though.</li>
</ol>
<p>To make sure that our implementation is generic enough, we decided to write a skeleton disassembler implementation for MIPS as well.</p>
<p>The second group of issues involve the nature of expression trees versus instructions:</p>
<ol>
<li><strong>Stepping over statements or instructions</strong>? Each expression tree for an instruction usually holds more than one statement. For example, dec eax changes eax as well as the zero flag. Since some instructions like rep stosd may contain a long loop, being able to step over statements instead of expressions is preferable.
<p>The problem is, executing expression trees is done with a DFS-like walk. If implemented with recursion it makes pausing the walk for each step a bit complicated. However, recursion is the clearest way to write the execution code, and I&#8217;d rather not give it up.</p>
<p>My solution was to use Python generators. Each time a statement was executed, the function would yield, thus returning control to the calling function, while keeping its state intact.
 </li>
<li><strong>Instruction Pointer changes</strong>. The lower-level disassembler returns expression trees for each instruction. However, it does not return the jump to the next instruction as well. While this is the required behavior, it means that the VM should change the instruction pointer after executing each instruction.
<p>This is easier said than done: What should we do after jumps? Several possible solutions emerged.</p>
<p>The first was to append an IP change to each instruction&#8217;s expression. Those changes will have to be appended only where needed.<br />
A second solution was to check if the IP was changed, and if it was not, change it. This solution however will not support instructions that jump to themselves.<br />
The last and our preferred solution was to check if the IP was touched. This solution is simple and very straightforward.</li>
</ol>
<p>There are many more issues I didn&#8217;t write about, for example, self modifying code. I&#8217;ll leave those issues for future articles.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Issues in writing a VM &#8211; Part 1</title>
		<link>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/</link>
		<comments>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 21:03:20 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Compilation]]></category>
		<category><![CDATA[Disassembly]]></category>
		<category><![CDATA[Distorm]]></category>
		<category><![CDATA[fuzzing]]></category>
		<category><![CDATA[vial]]></category>
		<category><![CDATA[VM]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/issues-in-writing-a-vm-part-1/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/' addthis:title='Issues in writing a VM &#8211; Part 1'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Arkon and I decided to write a VM for vial. First though, a short explanation on what is vial: vial is a project aimed at writing a general disassembler that outputs expression trees instead of text. On top of vial, &#8230; <a href="http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/' addthis:title='Issues in writing a VM &#8211; Part 1' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Arkon and I decided to write a VM for vial. First though, a short explanation on what is vial:<br />
vial is a project aimed at writing a general disassembler that outputs expression trees instead of text. On top of vial, we intend to write various code-analysis tools. The expression trees in the output should be an accurate description of the all of the code&#8217;s actions.<br />
(note: the x86 disassembler behind vial is Arkon&#8217;s <a href="http://www.ragestorm.net/distorm/">diStorm</a>.)</p>
<p>So why do we need a VM? Apart from it being &#8216;nice and all&#8217;, it is <strong>critical</strong> for testing.</p>
<p>Some time ago, <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/writing-a-quad-interpreter/">I described</a> writing a VM <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/manually-fuzzing-my-own-compiler/">to test</a> a compiler I wrote as university homework. It is a similar issue here.<br />
The disassembler is written according to the x86 specification. If we just check its output against this specification, we are not doing much to verify the code&#8217;s correctness. This is evident when you try to implement such a testing module &#8211; you end up writing another disassembler, and testing it against the original one. There has to be a different test method, one that does not directly rely on the specification.</p>
<p>Enter the VM. If you write a program, you can disassemble it, and then try to execute the disassembly. If it yields the same output as the original program &#8211; your test passed.<br />
This is a good testing method, because it can be easily automated, reach good code coverage, and it tests against known values.<br />
Consider the following illustration:</p>
<p><img src="http://www.algorithm.co.il/sitecode/vm_explanation.png" alt="Testing Process" /></p>
<p>We are testing here a complete process on the left hand, against a known valid value, the original program&#8217;s output, on the right hand. All of the boxes on the left hand are tested along the way. Of course, one test may miss. For example, both the VM and the disassembler may generate wrong output for register overflows. We can try to cover as many such cases as possible by writing good tests for this testing framework. In this case, good tests are either c programs, or binary programs. This is essentially what I was doing when I <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/manually-fuzzing-my-own-compiler/">manually fuzzed my own compiler</a>.</p>
<p>Once the VM is finished, we can start writing various optimizations for the disassembler&#8217;s generated output. We can test these optimizations by checking the VM&#8217;s output on the optimized code against the output on the original code. This makes the VM a critical milestone on the road ahead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/computer-science/issues-in-writing-a-vm-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Art and Science of Pulling Numbers Out of Your Sleeve</title>
		<link>http://www.algorithm.co.il/blogs/projects/the-art-and-science-of-pulling-numbers-out-of-your-sleeve/</link>
		<comments>http://www.algorithm.co.il/blogs/projects/the-art-and-science-of-pulling-numbers-out-of-your-sleeve/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 21:16:48 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Estimation]]></category>
		<category><![CDATA[Most Secret War]]></category>
		<category><![CDATA[Order of Magnitude]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[R.V. Jones]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/misc/the-art-and-science-of-pulling-numbers-out-of-your-sleeve/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/projects/the-art-and-science-of-pulling-numbers-out-of-your-sleeve/' addthis:title='The Art and Science of Pulling Numbers Out of Your Sleeve'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>About a year or so ago, I was reading R. V. Jones&#8217; excellent book &#8216;Most Secret War&#8217;. One of the stories I remembered and told my colleagues about, was how Jones estimated the rocket production capabilities of the Germans. He &#8230; <a href="http://www.algorithm.co.il/blogs/projects/the-art-and-science-of-pulling-numbers-out-of-your-sleeve/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/projects/the-art-and-science-of-pulling-numbers-out-of-your-sleeve/' addthis:title='The Art and Science of Pulling Numbers Out of Your Sleeve' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>About a year or so ago, I was reading <a href="http://en.wikipedia.org/wiki/Reginald_Victor_Jones">R. V. Jones&#8217;</a> excellent book &#8216;Most Secret War&#8217;. One of the stories I remembered and told my colleagues about, was how Jones estimated the rocket production capabilities of the Germans. He did so after looking at an aerial photograph of a rocket fuel shed. One of my colleague then told me of a course she took at the Hebrew University of Jerusalem that teaches how to make such estimates. I let it go at the time, and just remembered that there is such a course.</p>
<p>A few days ago, I met up with this colleague, and I was reminded of this course. You see, right now I&#8217;m collecting data for a project I&#8217;m doing with a friend, and I needed estimation know-how. So I asked her the name of the course, and she gave it to me. Two Google search later and I had the English name of the course, &#8220;Order of Magnitude in Physics Problems&#8221;, and a <a href="http://www.inference.phy.cam.ac.uk/sanjoy/oom/">textbook</a> to look at. I just finished reading the introduction, and I know that I&#8217;m going to read the rest of it too. Not just for my current project &#8211; but because it&#8217;s such a good skill to have.</p>
<p>The book opens with a problem:</p>
<blockquote><p>We dedicate the first example to physicists who need employment outside of physics. [...] How much money is there in a fully loaded Brinks armored car?</p></blockquote>
<p>The book then goes to show how to answer such a question intelligently. I&#8217;m hooked.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/projects/the-art-and-science-of-pulling-numbers-out-of-your-sleeve/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manually fuzzing my own compiler</title>
		<link>http://www.algorithm.co.il/blogs/computer-science/manually-fuzzing-my-own-compiler/</link>
		<comments>http://www.algorithm.co.il/blogs/computer-science/manually-fuzzing-my-own-compiler/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 20:21:36 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Compilation]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[fuzzing]]></category>
		<category><![CDATA[homework]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/uncategorized/manually-fuzzing-my-own-compiler/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/manually-fuzzing-my-own-compiler/' addthis:title='Manually fuzzing my own compiler'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>As I mentioned before, I had to write a compiler for simplified CPL. An obvious requirement was that the compiler generate correct code. A less obvious requirement, but important none-the-less, was that after a syntax error, the compiler will keep &#8230; <a href="http://www.algorithm.co.il/blogs/computer-science/manually-fuzzing-my-own-compiler/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/manually-fuzzing-my-own-compiler/' addthis:title='Manually fuzzing my own compiler' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>As I <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/writing-a-quad-interpreter/">mentioned before</a>, I had to write a compiler for simplified CPL. An obvious requirement was that the compiler generate correct code. A less obvious requirement, but important none-the-less, was that after a syntax error, the compiler will keep parsing the source program.</p>
<p>Now, the default behavior of a parser generated by Bison for a synatx error is to return from the parsing function, yyparse. You may of-course call yyparse again, but this would be mostly meaningless &#8211; you lost all your context. A simple example would be a language that has to have &#8216;program&#8217; as the first word. Once you are past that token, you will not be able to parse the source-program again, because your first production (which might look like this):</p>
<p><code>program: TOK_PROGRAM declarations statements TOK_END</code></p>
<p>won&#8217;t parse.</p>
<p>This is solved in Bison by adding error productions. For example:</p>
<p><code>expression: '(' error ')'</code></p>
<p>This production means that an error encountered within parenthesis may be considered a valid expression for the purposes of parsing. A proper semantic action for that error (the code that runs when the production is parsed) will add an error message to some error list, and maybe do some general book-keeping.</p>
<p><strong>So where does the fuzzing come in?</strong><br />
Well, my compiler was mostly working, but it still had no error recovery. That means that any syntax error would cause it to exit, with just that one error. Consider your favorite compiler, (such as gcc), exiting on the first missing semicolon. This is just not acceptable. So I added my basic error recovery and was pretty satisfied.<br />
Then, I had to test the newly written error handling. So I wrote a CPL program in which my goal was to try and kill my own compiler. Thats a fun way to test your code. This also happens to be a &#8216;fuzzing mindset&#8217;. I actually managed to find some holes I didn&#8217;t think about, and closed them. Of course, these were not security holes, just &#8216;compilation holes&#8217;.<br />
Here is an excerpt from one of the programs I wrote:<br />
[c]<br />
x=x++++++++++++++100;<br />
x=x=3;<br />
}<br />
for (x=1; x<100; x=x+1; {<br />
    x = x;<br />
}<br />
else () {<br />
for (x=1; x<100; x=x+1;) {<br />
    x = -1;<br />
}<br />
}<br />
[/c]</p>
<p>It goes on like this for a bit, so I'll save you the trouble. Later I did some deep testing for the code generation. I wrote a test-case for every possible operator in the language (there aren't that many) and for each type (real and int. Did I mention it was <em>simplified</em> cpl?). Since the language doesn&#8217;t support strings, each of these test cases printed 1 for success and 0 for failure. I ran the compiled output with my <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/writing-a-quad-interpreter/">VM script</a> and then had a simple Python script collect all the results and return failure if any of them failed. I later also tested control flow structures using the same method.<br />
I had all of these tests available in my makefile, and after each change all I had to do was &#8216;make test&#8217;. Which I did, often.</p>
<p>Looking back after finishing the assignment, it occurred to me that I could have also written an actual &#8216;fuzzer&#8217; for source programs. However, in the context of such a university assignment this seems like overkill. Had I been writing a real compiler, there&#8217;s a very good chance I&#8217;d have written a &#8216;source fuzzer&#8217;.</p>
<p>All in all, after I got over the nastiness which is Bison, it was pretty fun. It has been a long time since I wrote C or C++, and it&#8217;s good to clean some of the rust away from time to time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/computer-science/manually-fuzzing-my-own-compiler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Distorm3 progress and SVN</title>
		<link>http://www.algorithm.co.il/blogs/linux/distorm3-progress-and-svn/</link>
		<comments>http://www.algorithm.co.il/blogs/linux/distorm3-progress-and-svn/#comments</comments>
		<pubDate>Sun, 28 Oct 2007 02:33:55 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Distorm]]></category>
		<category><![CDATA[SVN]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/uncategorized/distorm3-progress-and-svn/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/linux/distorm3-progress-and-svn/' addthis:title='Distorm3 progress and SVN'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Finally, Distorm3 is progressing. With heaps of work done by Gil, and some more by me, the project will soon be on its feet. One thing that really gave us a feeling of progress, is setting up Subversion. We thought &#8230; <a href="http://www.algorithm.co.il/blogs/linux/distorm3-progress-and-svn/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/linux/distorm3-progress-and-svn/' addthis:title='Distorm3 progress and SVN' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Finally, <a href="http://www.ragestorm.net/distorm/" title="Fastest Disassembler in the West!">Distorm3</a> is progressing. With heaps of work done by <a href="http://www.ragestorm.net/" title="Ragestorm - home of Distorm">Gil</a>, and some more by me, the project will soon be on its feet. One thing that really gave us a feeling of progress, is setting up <a href="http://subversion.tigris.org/">Subversion</a>. We thought of setting our own, but finally decided on using <a href="http://www.assembla.com/" title="Free SVN and Trac hosting, and much more!">Assembla</a>, after a good friend recommended it. There are other options, but Assembla seems really good, although it does have its drawbacks. (Your code goes in clear-text for one).</p>
<p>When I was using windows I worked with <a href="http://tortoisesvn.tigris.org/">TortoiseSVN</a>, but now on my Ubuntu, I&#8217;m using <a href="http://rapidsvn.tigris.org/">RapidSVN</a>, with meld as my diff tool. Although Rapid is cool, I liked Tortoise better. It was faster, much more intuitive, and I liked the commit window better. It gave me a choice on what to commit, and I could (from the commit window) run a diff on each file I changed, and using the diff write a short comment on my changes in the commit comment. While RapidSVN obviously also allows for commit comments, I have to do all the work beforehand. It&#8217;s a bit more cumbersome.</p>
<p>Another thing &#8211; I&#8217;m working on unit-testing using the excellent unittest module. Using <a href="http://nedbatchelder.com/code/modules/coverage.html">coverage.py</a> along with the testing makes my code so much better, and me so much happier. Since we have a little bit of c-code generation, one of the (a little bit hackish) tests I wrote was running gcc on some sample output, and making sure there were no errors or warnings. Fun. One of my next todos is compiling a few small executables, and making sure that they run without errors, all from within the unit-testing of the code generation module.</p>
<p>All in all, it&#8217;s good to finally be working organized.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/linux/distorm3-progress-and-svn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

