<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Articles tagged lisp at null program</title>
  <link rel="alternate" type="text/html"
        href="https://nullprogram.com/tags/lisp/"/>
  <link rel="self" type="application/atom+xml"
        href="https://nullprogram.com/tags/lisp/feed/"/>
  <updated>2026-04-09T13:25:45Z</updated>
  <id>urn:uuid:dffd8000-5e5e-41d1-ab8e-ee2a25ee30a9</id>

  <author>
    <name>Christopher Wellons</name>
    <uri>https://nullprogram.com</uri>
    <email>wellons@nullprogram.com</email>
  </author>

  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>An Async / Await Library for Emacs Lisp</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2019/03/10/"/>
    <id>urn:uuid:5d1462fa-a30d-432e-9a4f-827eb67862b2</id>
    <updated>2019-03-10T20:57:03Z</updated>
    <category term="emacs"/><category term="elisp"/><category term="lisp"/><category term="python"/><category term="javascript"/><category term="lang"/><category term="asyncio"/>
    <content type="html">
      <![CDATA[<p>As part of <a href="/blog/2019/02/24/">building my Python proficiency</a>, I’ve learned how to
use <a href="https://docs.python.org/3/library/asyncio.html">asyncio</a>. This new language feature <a href="https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-492">first appeared in
Python 3.5</a> (<a href="https://www.python.org/dev/peps/pep-0492/">PEP 492</a>, September 2015). JavaScript grew <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">a
nearly identical feature</a> in ES2017 (June 2017). An async function
can pause to await on an asynchronously computed result, much like a
generator pausing when it yields a value.</p>

<p>In fact, both Python and JavaScript async functions are essentially just
fancy generator functions with some specialized syntax and semantics.
That is, they’re <a href="https://blog.varunramesh.net/posts/stackless-vs-stackful-coroutines/">stackless coroutines</a>. Both languages already had
generators, so their generator-like async functions are a natural
extension that — unlike <a href="/blog/2017/06/21/"><em>stackful</em> coroutines</a> — do not require
significant, new runtime plumbing.</p>

<p>Emacs <a href="/blog/2018/05/31/">officially got generators in 25.1</a> (September 2016),
though, unlike Python and JavaScript, it didn’t require any additional
support from the compiler or runtime. It’s implemented entirely using
Lisp macros. In other words, it’s just another library, not a core
language feature. In theory, the generator library could be easily
backported to the first Emacs release to <a href="/blog/2016/12/22/">properly support lexical
closures</a>, Emacs 24.1 (June 2012).</p>

<p>For the same reason, stackless async/await coroutines can also be
implemented as a library. So that’s what I did, letting Emacs’ generator
library do most of the heavy lifting. The package is called <code class="language-plaintext highlighter-rouge">aio</code>:</p>

<ul>
  <li><strong><a href="https://github.com/skeeto/emacs-aio">https://github.com/skeeto/emacs-aio</a></strong></li>
</ul>

<p>It’s modeled more closely on JavaScript’s async functions than Python’s
asyncio, with the core representation being <em>promises</em> rather than a
coroutine objects. I just have an easier time reasoning about promises
than coroutines.</p>

<p>I’m definitely <a href="https://github.com/chuntaro/emacs-async-await">not the first person to realize this was
possible</a>, and was beaten to the punch by two years. Wanting to
<a href="http://www.winestockwebdesign.com/Essays/Lisp_Curse.html">avoid fragmentation</a>, I set aside all formality in my first
iteration on the idea, not even bothering with namespacing my
identifiers. It was to be only an educational exercise. However, I got
quite attached to my little toy. Once I got my head wrapped around the
problem, everything just sort of clicked into place so nicely.</p>

<p>In this article I will show step-by-step one way to build async/await
on top of generators, laying out one concept at a time and then
building upon each. But first, some examples to illustrate the desired
final result.</p>

<h3 id="aio-example">aio example</h3>

<p>Ignoring <a href="/blog/2016/06/16/">all its problems</a> for a moment, suppose you want to use
<code class="language-plaintext highlighter-rouge">url-retrieve</code> to fetch some content from a URL and return it. To keep
this simple, I’m going to omit error handling. Also assume that
<code class="language-plaintext highlighter-rouge">lexical-binding</code> is <code class="language-plaintext highlighter-rouge">t</code> for all examples. Besides, lexical scope
required by the generator library, and therefore also required by <code class="language-plaintext highlighter-rouge">aio</code>.</p>

<p>The most naive approach is to fetch the content synchronously:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">fetch-fortune-1</span> <span class="p">(</span><span class="nv">url</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">buffer</span> <span class="p">(</span><span class="nv">url-retrieve-synchronously</span> <span class="nv">url</span><span class="p">)))</span>
    <span class="p">(</span><span class="nv">with-current-buffer</span> <span class="nv">buffer</span>
      <span class="p">(</span><span class="nb">prog1</span> <span class="p">(</span><span class="nv">buffer-string</span><span class="p">)</span>
        <span class="p">(</span><span class="nv">kill-buffer</span><span class="p">)))))</span>
</code></pre></div></div>

<p>The result is returned directly, and errors are communicated by an error
signal (e.g. Emacs’ version of exceptions). This is convenient, but the
function will block the main thread, locking up Emacs until the result
has arrived. This is obviously very undesirable, so, in practice,
everyone nearly always uses the asynchronous version:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">fetch-fortune-2</span> <span class="p">(</span><span class="nv">url</span> <span class="nv">callback</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">url-retrieve</span> <span class="nv">url</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">_status</span><span class="p">)</span>
                      <span class="p">(</span><span class="nb">funcall</span> <span class="nv">callback</span> <span class="p">(</span><span class="nv">buffer-string</span><span class="p">)))))</span>
</code></pre></div></div>

<p>The main thread no longer blocks, but it’s a whole lot less
convenient. The result isn’t returned to the caller, and instead the
caller supplies a callback function. The result, whether success or
failure, will be delivered via callback, so the caller must split
itself into two pieces: the part before the callback and the callback
itself. Errors cannot be delivered using a error signal because of the
inverted flow control.</p>

<p>The situation gets worse if, say, you need to fetch results from two
different URLs. You either fetch results one at a time (inefficient),
or you manage two different callbacks that could be invoked in any
order, and therefore have to coordinate.</p>

<p><em>Wouldn’t it be nice for the function to work like the first example,
but be asynchronous like the second example?</em> Enter async/await:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">aio-defun</span> <span class="nv">fetch-fortune-3</span> <span class="p">(</span><span class="nv">url</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">buffer</span> <span class="p">(</span><span class="nv">aio-await</span> <span class="p">(</span><span class="nv">aio-url-retrieve</span> <span class="nv">url</span><span class="p">))))</span>
    <span class="p">(</span><span class="nv">with-current-buffer</span> <span class="nv">buffer</span>
      <span class="p">(</span><span class="nb">prog1</span> <span class="p">(</span><span class="nv">buffer-string</span><span class="p">)</span>
        <span class="p">(</span><span class="nv">kill-buffer</span><span class="p">)))))</span>
</code></pre></div></div>

<p>A function defined with <code class="language-plaintext highlighter-rouge">aio-defun</code> is just like <code class="language-plaintext highlighter-rouge">defun</code> except that
it can use <code class="language-plaintext highlighter-rouge">aio-await</code> to pause and wait on any other function defined
with <code class="language-plaintext highlighter-rouge">aio-defun</code> — or, more specifically, any function that returns a
promise. Borrowing Python parlance: Returning a promise makes a
function <em>awaitable</em>. If there’s an error, it’s delivered as a error
signal from <code class="language-plaintext highlighter-rouge">aio-url-retrieve</code>, just like the first example. When
called, this function returns immediately with a promise object that
represents a future result. The caller might look like this:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">defcustom</span> <span class="nv">fortune-url</span> <span class="o">...</span><span class="p">)</span>

<span class="p">(</span><span class="nv">aio-defun</span> <span class="nv">display-fortune</span> <span class="p">()</span>
  <span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">message</span> <span class="s">"%s"</span> <span class="p">(</span><span class="nv">aio-await</span> <span class="p">(</span><span class="nv">fetch-fortune-3</span> <span class="nv">fortune-url</span><span class="p">))))</span>
</code></pre></div></div>

<p>How wonderfully clean that looks! And, yes, it even works with
<code class="language-plaintext highlighter-rouge">interactive</code> like that. I can <code class="language-plaintext highlighter-rouge">M-x display-fortune</code> and a fortune is
printed in the minibuffer as soon as the result arrives from the
server. In the meantime Emacs doesn’t block and I can continue my
work.</p>

<p>You can’t do anything you couldn’t already do before. It’s just a
nicer way to organize the same callbacks: <em>implicit</em> rather than
<em>explicit</em>.</p>

<h3 id="promises-simplified">Promises, simplified</h3>

<p>The core object at play is the <em>promise</em>. Promises are already a
rather simple concept, but <code class="language-plaintext highlighter-rouge">aio</code> promises have been distilled to their
essence, as they’re only needed for this singular purpose. More on
this later.</p>

<p>As I said, a promise represents a future result. In practical terms, a
promise is just an object to which one can subscribe with a callback.
When the result is ready, the callbacks are invoked. Another way to
put it is that <em>promises <a href="https://en.wikipedia.org/wiki/Reification_(computer_science)">reify</a> the concept of callbacks</em>. A
callback is no longer just the idea of extra argument on a function.
It’s a first-class <em>thing</em> that itself can be passed around as a
value.</p>

<p>Promises have two slots: the final promise <em>result</em> and a list of
<em>subscribers</em>. A <code class="language-plaintext highlighter-rouge">nil</code> result means the result hasn’t been computed
yet. It’s so simple I’m not even <a href="/blog/2018/02/14/">bothering with <code class="language-plaintext highlighter-rouge">cl-struct</code></a>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">aio-promise</span> <span class="p">()</span>
  <span class="s">"Create a new promise object."</span>
  <span class="p">(</span><span class="nv">record</span> <span class="ss">'aio-promise</span> <span class="no">nil</span> <span class="p">()))</span>

<span class="p">(</span><span class="nv">defsubst</span> <span class="nv">aio-promise-p</span> <span class="p">(</span><span class="nv">object</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nb">eq</span> <span class="ss">'aio-promise</span> <span class="p">(</span><span class="nb">type-of</span> <span class="nv">object</span><span class="p">))</span>
       <span class="p">(</span><span class="nb">=</span> <span class="mi">3</span> <span class="p">(</span><span class="nb">length</span> <span class="nv">object</span><span class="p">))))</span>

<span class="p">(</span><span class="nv">defsubst</span> <span class="nv">aio-result</span> <span class="p">(</span><span class="nv">promise</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">aref</span> <span class="nv">promise</span> <span class="mi">1</span><span class="p">))</span>
</code></pre></div></div>

<p>To subscribe to a promise, use <code class="language-plaintext highlighter-rouge">aio-listen</code>:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">aio-listen</span> <span class="p">(</span><span class="nv">promise</span> <span class="nv">callback</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">result</span> <span class="p">(</span><span class="nv">aio-result</span> <span class="nv">promise</span><span class="p">)))</span>
    <span class="p">(</span><span class="k">if</span> <span class="nv">result</span>
        <span class="p">(</span><span class="nv">run-at-time</span> <span class="mi">0</span> <span class="no">nil</span> <span class="nv">callback</span> <span class="nv">result</span><span class="p">)</span>
      <span class="p">(</span><span class="nb">push</span> <span class="nv">callback</span> <span class="p">(</span><span class="nb">aref</span> <span class="nv">promise</span> <span class="mi">2</span><span class="p">)))))</span>
</code></pre></div></div>

<p>If the result isn’t ready yet, add the callback to the list of
subscribers. If the result is ready <em>call the callback in the next
event loop turn</em> using <code class="language-plaintext highlighter-rouge">run-at-time</code>. This is important because it
keeps all the asynchronous components isolated from one another. They
won’t see each others’ frames on the call stack, nor frames from
<code class="language-plaintext highlighter-rouge">aio</code>. This is so important that the <a href="https://promisesaplus.com/">Promises/A+ specification</a>
is explicit about it.</p>

<p>The other half of the equation is resolving a promise, which is done
with <code class="language-plaintext highlighter-rouge">aio-resolve</code>. Unlike other promises, <code class="language-plaintext highlighter-rouge">aio</code> promises don’t care
whether the promise is being <em>fulfilled</em> (success) or <em>rejected</em>
(error). Instead a promise is resolved using a <em>value function</em> — or,
usually, a <em>value closure</em>. Subscribers receive this value function
and extract the value by invoking it with no arguments.</p>

<p>Why? This lets the promise’s resolver decide the semantics of the
result. Instead of returning a value, this function can instead signal
an error, propagating an error signal that terminated an async function.
Because of this, the promise doesn’t need to know how it’s being
resolved.</p>

<p>When a promise is resolved, subscribers are each scheduled in their own
event loop turns in the same order that they subscribed. If a promise
has already been resolved, nothing happens. (Thought: Perhaps this
should be an error in order to catch API misuse?)</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">aio-resolve</span> <span class="p">(</span><span class="nv">promise</span> <span class="nv">value-function</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nv">aio-result</span> <span class="nv">promise</span><span class="p">)</span>
    <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">callbacks</span> <span class="p">(</span><span class="nb">nreverse</span> <span class="p">(</span><span class="nb">aref</span> <span class="nv">promise</span> <span class="mi">2</span><span class="p">))))</span>
      <span class="p">(</span><span class="nb">setf</span> <span class="p">(</span><span class="nb">aref</span> <span class="nv">promise</span> <span class="mi">1</span><span class="p">)</span> <span class="nv">value-function</span>
            <span class="p">(</span><span class="nb">aref</span> <span class="nv">promise</span> <span class="mi">2</span><span class="p">)</span> <span class="p">())</span>
      <span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nv">callback</span> <span class="nv">callbacks</span><span class="p">)</span>
        <span class="p">(</span><span class="nv">run-at-time</span> <span class="mi">0</span> <span class="no">nil</span> <span class="nv">callback</span> <span class="nv">value-function</span><span class="p">)))))</span>
</code></pre></div></div>

<p>If you’re not an async function, you might subscribe to a promise like
so:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">aio-listen</span> <span class="nv">promise</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">v</span><span class="p">)</span>
                      <span class="p">(</span><span class="nv">message</span> <span class="s">"%s"</span> <span class="p">(</span><span class="nb">funcall</span> <span class="nv">v</span><span class="p">))))</span>
</code></pre></div></div>

<p>The simplest example of a non-async function that creates and delivers
on a promise is a “sleep” function:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">aio-sleep</span> <span class="p">(</span><span class="nv">seconds</span> <span class="k">&amp;optional</span> <span class="nv">result</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">promise</span> <span class="p">(</span><span class="nv">aio-promise</span><span class="p">))</span>
        <span class="p">(</span><span class="nv">value-function</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="nv">result</span><span class="p">)))</span>
    <span class="p">(</span><span class="nb">prog1</span> <span class="nv">promise</span>
      <span class="p">(</span><span class="nv">run-at-time</span> <span class="nv">seconds</span> <span class="no">nil</span>
                   <span class="nf">#'</span><span class="nv">aio-resolve</span> <span class="nv">promise</span> <span class="nv">value-function</span><span class="p">))))</span>
</code></pre></div></div>

<p>Similarly, here’s a “timeout” promise that delivers a special timeout
error signal at a given time in the future.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">aio-timeout</span> <span class="p">(</span><span class="nv">seconds</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">promise</span> <span class="p">(</span><span class="nv">aio-promise</span><span class="p">))</span>
        <span class="p">(</span><span class="nv">value-function</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="p">(</span><span class="nb">signal</span> <span class="ss">'aio-timeout</span> <span class="no">nil</span><span class="p">))))</span>
    <span class="p">(</span><span class="nb">prog1</span> <span class="nv">promise</span>
      <span class="p">(</span><span class="nv">run-at-time</span> <span class="nv">seconds</span> <span class="no">nil</span>
                   <span class="nf">#'</span><span class="nv">aio-resolve</span> <span class="nv">promise</span> <span class="nv">value-function</span><span class="p">))))</span>
</code></pre></div></div>

<p>That’s all there is to promises.</p>

<h3 id="evaluate-in-the-context-of-a-promise">Evaluate in the context of a promise</h3>

<p>Before we get into pausing functions, lets deal with the slightly
simpler matter of delivering their return values using a promise. What
we need is a way to evaluate a “body” and capture its result in a
promise. If the body exits due to a signal, we want to capture that as
well.</p>

<p>Here’s a macro that does just this:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defmacro</span> <span class="nv">aio-with-promise</span> <span class="p">(</span><span class="nv">promise</span> <span class="k">&amp;rest</span> <span class="nv">body</span><span class="p">)</span>
  <span class="o">`</span><span class="p">(</span><span class="nv">aio-resolve</span> <span class="o">,</span><span class="nv">promise</span>
                <span class="p">(</span><span class="nv">condition-case</span> <span class="nb">error</span>
                    <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">result</span> <span class="p">(</span><span class="k">progn</span> <span class="o">,@</span><span class="nv">body</span><span class="p">)))</span>
                      <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="nv">result</span><span class="p">))</span>
                  <span class="p">(</span><span class="nb">error</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span>
                           <span class="p">(</span><span class="nb">signal</span> <span class="p">(</span><span class="nb">car</span> <span class="nb">error</span><span class="p">)</span> <span class="c1">; rethrow</span>
                                   <span class="p">(</span><span class="nb">cdr</span> <span class="nb">error</span><span class="p">)))))))</span>
</code></pre></div></div>

<p>The body result is captured in a closure and delivered to the promise.
If there’s an error signal, it’s “<em>rethrown</em>” into subscribers by the
promise’s value function.</p>

<p>This is where Emacs Lisp has a serious weak spot. There’s not really a
concept of rethrowing a signal. Unlike a language with explicit
exception objects that can capture a snapshot of the backtrace, the
original backtrace is completely lost where the signal is caught.
There’s no way to “reattach” it to the signal when it’s rethrown. This
is unfortunate because it would greatly help debugging if you got to see
the full backtrace on the other side of the promise.</p>

<h3 id="async-functions">Async functions</h3>

<p>So we have promises and we want to pause a function on a promise.
Generators have <code class="language-plaintext highlighter-rouge">iter-yield</code> for pausing an iterator’s execution. To
tackle this problem:</p>

<ol>
  <li>Yield the promise to pause the iterator.</li>
  <li>Subscribe a callback on the promise that continues the generator
(<code class="language-plaintext highlighter-rouge">iter-next</code>) with the promise’s result as the yield result.</li>
</ol>

<p>All the hard work is done in either side of the yield, so <code class="language-plaintext highlighter-rouge">aio-await</code> is
just a simple wrapper around <code class="language-plaintext highlighter-rouge">iter-yield</code>:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defmacro</span> <span class="nv">aio-await</span> <span class="p">(</span><span class="nv">expr</span><span class="p">)</span>
  <span class="o">`</span><span class="p">(</span><span class="nb">funcall</span> <span class="p">(</span><span class="nv">iter-yield</span> <span class="o">,</span><span class="nv">expr</span><span class="p">)))</span>
</code></pre></div></div>

<p>Remember, that <code class="language-plaintext highlighter-rouge">funcall</code> is here to extract the promise value from the
value function. If it signals an error, this propagates directly into
the iterator just as if it had been a direct call — minus an accurate
backtrace.</p>

<p>So <code class="language-plaintext highlighter-rouge">aio-lambda</code> / <code class="language-plaintext highlighter-rouge">aio-defun</code> needs to wrap the body in a generator
(<code class="language-plaintext highlighter-rouge">iter-lamba</code>), invoke it to produce a generator, then drive the
generator using callbacks. Here’s a simplified, unhygienic definition of
<code class="language-plaintext highlighter-rouge">aio-lambda</code>:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defmacro</span> <span class="nv">aio-lambda</span> <span class="p">(</span><span class="nv">arglist</span> <span class="k">&amp;rest</span> <span class="nv">body</span><span class="p">)</span>
  <span class="o">`</span><span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="k">&amp;rest</span> <span class="nv">args</span><span class="p">)</span>
     <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">promise</span> <span class="p">(</span><span class="nv">aio-promise</span><span class="p">))</span>
           <span class="p">(</span><span class="nv">iter</span> <span class="p">(</span><span class="nb">apply</span> <span class="p">(</span><span class="nv">iter-lambda</span> <span class="o">,</span><span class="nv">arglist</span>
                          <span class="p">(</span><span class="nv">aio-with-promise</span> <span class="nv">promise</span>
                            <span class="o">,@</span><span class="nv">body</span><span class="p">))</span>
                        <span class="nv">args</span><span class="p">)))</span>
       <span class="p">(</span><span class="nb">prog1</span> <span class="nv">promise</span>
         <span class="p">(</span><span class="nv">aio--step</span> <span class="nv">iter</span> <span class="nv">promise</span> <span class="no">nil</span><span class="p">)))))</span>
</code></pre></div></div>

<p>The body is evaluated inside <code class="language-plaintext highlighter-rouge">aio-with-promise</code> with the result
delivered to the promise returned directly by the async function.</p>

<p>Before returning, the iterator is handed to <code class="language-plaintext highlighter-rouge">aio--step</code>, which drives
the iterator forward until it delivers its first promise. When the
iterator yields a promise, <code class="language-plaintext highlighter-rouge">aio--step</code> attaches a callback back to
itself on the promise as described above. Immediately driving the
iterator up to the first yielded promise “primes” it, which is
important for getting the ball rolling on any asynchronous operations.</p>

<p>If the iterator ever yields something other than a promise, it’s
delivered right back into the iterator.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">aio--step</span> <span class="p">(</span><span class="nv">iter</span> <span class="nv">promise</span> <span class="nv">yield-result</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">condition-case</span> <span class="nv">_</span>
      <span class="p">(</span><span class="nv">cl-loop</span> <span class="nv">for</span> <span class="nv">result</span> <span class="nb">=</span> <span class="p">(</span><span class="nv">iter-next</span> <span class="nv">iter</span> <span class="nv">yield-result</span><span class="p">)</span>
               <span class="nv">then</span> <span class="p">(</span><span class="nv">iter-next</span> <span class="nv">iter</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="nv">result</span><span class="p">))</span>
               <span class="nv">until</span> <span class="p">(</span><span class="nv">aio-promise-p</span> <span class="nv">result</span><span class="p">)</span>
               <span class="nv">finally</span> <span class="p">(</span><span class="nv">aio-listen</span> <span class="nv">result</span>
                                   <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">value</span><span class="p">)</span>
                                     <span class="p">(</span><span class="nv">aio--step</span> <span class="nv">iter</span> <span class="nv">promise</span> <span class="nv">value</span><span class="p">))))</span>
    <span class="p">(</span><span class="nv">iter-end-of-sequence</span><span class="p">)))</span>
</code></pre></div></div>

<p>When the iterator is done, nothing more needs to happen since the
iterator resolves its own return value promise.</p>

<p>The definition of <code class="language-plaintext highlighter-rouge">aio-defun</code> just uses <code class="language-plaintext highlighter-rouge">aio-lambda</code> with <code class="language-plaintext highlighter-rouge">defalias</code>.
There’s nothing to it.</p>

<p>That’s everything you need! Everything else in the package is merely
useful, awaitable functions like <code class="language-plaintext highlighter-rouge">aio-sleep</code> and <code class="language-plaintext highlighter-rouge">aio-timeout</code>.</p>

<h3 id="composing-promises">Composing promises</h3>

<p>Unfortunately <code class="language-plaintext highlighter-rouge">url-retrieve</code> doesn’t support timeouts. We can work
around this by composing two promises: a <code class="language-plaintext highlighter-rouge">url-retrieve</code> promise and
<code class="language-plaintext highlighter-rouge">aio-timeout</code> promise. First define a promise-returning function,
<code class="language-plaintext highlighter-rouge">aio-select</code> that takes a list of promises and returns (as another
promise) the first promise to resolve:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">aio-select</span> <span class="p">(</span><span class="nv">promises</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">result</span> <span class="p">(</span><span class="nv">aio-promise</span><span class="p">)))</span>
    <span class="p">(</span><span class="nb">prog1</span> <span class="nv">result</span>
      <span class="p">(</span><span class="nb">dolist</span> <span class="p">(</span><span class="nv">promise</span> <span class="nv">promises</span><span class="p">)</span>
        <span class="p">(</span><span class="nv">aio-listen</span> <span class="nv">promise</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">_</span><span class="p">)</span>
                              <span class="p">(</span><span class="nv">aio-resolve</span>
                               <span class="nv">result</span>
                               <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="nv">promise</span><span class="p">))))))))</span>
</code></pre></div></div>

<p>We give <code class="language-plaintext highlighter-rouge">aio-select</code> both our <code class="language-plaintext highlighter-rouge">url-retrieve</code> and <code class="language-plaintext highlighter-rouge">timeout</code> promises, and
it tells us which resolved first:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">aio-defun</span> <span class="nv">fetch-fortune-4</span> <span class="p">(</span><span class="nv">url</span> <span class="nv">timeout</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">promises</span> <span class="p">(</span><span class="nb">list</span> <span class="p">(</span><span class="nv">aio-url-retrieve</span> <span class="nv">url</span><span class="p">)</span>
                         <span class="p">(</span><span class="nv">aio-timeout</span> <span class="nv">timeout</span><span class="p">)))</span>
         <span class="p">(</span><span class="nv">fastest</span> <span class="p">(</span><span class="nv">aio-await</span> <span class="p">(</span><span class="nv">aio-select</span> <span class="nv">promises</span><span class="p">)))</span>
         <span class="p">(</span><span class="nv">buffer</span> <span class="p">(</span><span class="nv">aio-await</span> <span class="nv">fastest</span><span class="p">)))</span>
    <span class="p">(</span><span class="nv">with-current-buffer</span> <span class="nv">buffer</span>
      <span class="p">(</span><span class="nb">prog1</span> <span class="p">(</span><span class="nv">buffer-string</span><span class="p">)</span>
        <span class="p">(</span><span class="nv">kill-buffer</span><span class="p">)))))</span>
</code></pre></div></div>

<p>Cool! Note: This will not actually cancel the URL request, just move
the async function forward earlier and prevent it from getting the
result.</p>

<h3 id="threads">Threads</h3>

<p>Despite <code class="language-plaintext highlighter-rouge">aio</code> being entirely about managing concurrent, asynchronous
operations, it has nothing at all to do with threads — as in Emacs 26’s
support for kernel threads. All async functions and promise callbacks
are expected to run <em>only</em> on the main thread. That’s not to say an
async function can’t await on a result from another thread. It just must
be <a href="/blog/2017/02/14/">done very carefully</a>.</p>

<h3 id="processes">Processes</h3>

<p>The package also includes two functions for realizing promises on
processes, whether they be subprocesses or network sockets.</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">aio-process-filter</code></li>
  <li><code class="language-plaintext highlighter-rouge">aio-process-sentinel</code></li>
</ul>

<p>For example, this function loops over each chunk of output (typically
4kB) from the process, as delivered to a filter function:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">aio-defun</span> <span class="nv">process-chunks</span> <span class="p">(</span><span class="nv">process</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">cl-loop</span> <span class="nv">for</span> <span class="nv">chunk</span> <span class="nb">=</span> <span class="p">(</span><span class="nv">aio-await</span> <span class="p">(</span><span class="nv">aio-process-filter</span> <span class="nv">process</span><span class="p">))</span>
           <span class="nv">while</span> <span class="nv">chunk</span>
           <span class="nb">do</span> <span class="p">(</span><span class="o">...</span> <span class="nv">process</span> <span class="nv">chunk</span> <span class="o">...</span><span class="p">)))</span>
</code></pre></div></div>

<p>Exercise for the reader: Write an awaitable function that returns a line
at at time rather than a chunk at a time. You can build it on top of
<code class="language-plaintext highlighter-rouge">aio-process-filter</code>.</p>

<p>I considered wrapping functions like <code class="language-plaintext highlighter-rouge">start-process</code> so that their <code class="language-plaintext highlighter-rouge">aio</code>
versions would return a promise representing some kind of result from
the process. However there are <em>so</em> many different ways to create and
configure processes that I would have ended up duplicating all the
process functions. Focusing on the filter and sentinel, and letting the
caller create and configure the process is much cleaner.</p>

<p>Unfortunately Emacs has no asynchronous API for writing output to a
process. Both <code class="language-plaintext highlighter-rouge">process-send-string</code> and <code class="language-plaintext highlighter-rouge">process-send-region</code> will block
if the pipe or socket is full. There is no callback, so you cannot await
on writing output. Maybe there’s a way to do it with a dedicated thread?</p>

<p>Another issue is that the <code class="language-plaintext highlighter-rouge">process-send-*</code> functions <a href="/blog/2013/01/14/">are
preemptible</a>, made necessary because they block. The
<code class="language-plaintext highlighter-rouge">aio-process-*</code> functions leave a gap (i.e. between filter awaits)
where no filter or sentinel function is attached. It’s a consequence
of promises being single-fire. The gap is harmless so long as the
async function doesn’t await something else or get preempted. This
needs some more thought.</p>

<p><strong><em>Update</em></strong>: These process functions no longer exist and have been
replaced by a small framework for building chains of promises. See
<code class="language-plaintext highlighter-rouge">aio-make-callback</code>.</p>

<h3 id="testing-aio">Testing aio</h3>

<p>The test suite for <code class="language-plaintext highlighter-rouge">aio</code> is a bit unusual. Emacs’ built-in test suite,
ERT, doesn’t support asynchronous tests. Furthermore, tests are
generally run in batch mode, where Emacs invokes a single function and
then exits rather than pump an event loop. Batch mode can only handle
asynchronous process I/O, not the async functions of <code class="language-plaintext highlighter-rouge">aio</code>. So it’s
not possible to run the tests in batch mode.</p>

<p>Instead I hacked together a really crude callback-based test suite. It
runs in non-batch mode and writes the test results into a buffer
(run with <code class="language-plaintext highlighter-rouge">make check</code>). Not ideal, but it works.</p>

<p>One of the tests is a sleep sort (with reasonable tolerances). It’s a
pretty neat demonstration of what you can do with <code class="language-plaintext highlighter-rouge">aio</code>:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">aio-defun</span> <span class="nv">sleep-sort</span> <span class="p">(</span><span class="nb">values</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">promises</span> <span class="p">(</span><span class="nb">mapcar</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">v</span><span class="p">)</span> <span class="p">(</span><span class="nv">aio-sleep</span> <span class="nv">v</span> <span class="nv">v</span><span class="p">))</span> <span class="nb">values</span><span class="p">)))</span>
    <span class="p">(</span><span class="nv">cl-loop</span> <span class="nv">while</span> <span class="nv">promises</span>
             <span class="nv">for</span> <span class="nv">next</span> <span class="nb">=</span> <span class="p">(</span><span class="nv">aio-await</span> <span class="p">(</span><span class="nv">aio-select</span> <span class="nv">promises</span><span class="p">))</span>
             <span class="nb">do</span> <span class="p">(</span><span class="nb">setf</span> <span class="nv">promises</span> <span class="p">(</span><span class="nv">delq</span> <span class="nv">next</span> <span class="nv">promises</span><span class="p">))</span>
             <span class="nv">collect</span> <span class="p">(</span><span class="nv">aio-await</span> <span class="nv">next</span><span class="p">))))</span>
</code></pre></div></div>

<p>To see it in action (<code class="language-plaintext highlighter-rouge">M-x sleep-sort-demo</code>):</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">aio-defun</span> <span class="nv">sleep-sort-demo</span> <span class="p">()</span>
  <span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nb">values</span> <span class="o">'</span><span class="p">(</span><span class="mf">0.1</span> <span class="mf">0.4</span> <span class="mf">1.1</span> <span class="mf">0.2</span> <span class="mf">0.8</span> <span class="mf">0.6</span><span class="p">)))</span>
    <span class="p">(</span><span class="nv">message</span> <span class="s">"%S"</span> <span class="p">(</span><span class="nv">aio-await</span> <span class="p">(</span><span class="nv">sleep-sort</span> <span class="nb">values</span><span class="p">)))))</span>
</code></pre></div></div>

<h3 id="asyncawait-is-pretty-awesome">Async/await is pretty awesome</h3>

<p>I’m quite happy with how this all came together. Once I had the
concepts straight — particularly resolving to value functions —
everything made sense and all the parts fit together well, and mostly
by accident. That feels good.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Emacs 26 Brings Generators and Threads</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2018/05/31/"/>
    <id>urn:uuid:395c5e11-2088-32fa-53c8-0c749dca2064</id>
    <updated>2018-05-31T17:45:16Z</updated>
    <category term="emacs"/><category term="elisp"/><category term="lisp"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>Emacs 26.1 was <a href="https://lists.gnu.org/archive/html/emacs-devel/2018-05/msg00765.html">recently released</a>. As you would expect from a
major release, it comes with lots of new goodies. Being <a href="/tags/emacs/">a bit of an
Emacs Lisp enthusiast</a>, the two most interesting new features
are <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Generators.html">generators</a> (<code class="language-plaintext highlighter-rouge">iter</code>) and <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Threads.html">native threads</a>
(<code class="language-plaintext highlighter-rouge">thread</code>).</p>

<p><strong>Correction</strong>: Generators were actually introduced in Emacs 25.1
(Sept. 2016), not Emacs 26.1. Doh!</p>

<p><strong>Update</strong>: <a href="https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual">ThreadSanitizer (TSan)</a> quickly shows that Emacs’
threading implementation has many data races, making it <a href="https://hboehm.info/boehm-hotpar11.pdf">completely
untrustworthy</a>. Until this is fixed, <strong><em>nobody</em> should use Emacs
threads for any purpose</strong>, and threads should disabled at compile time.</p>

<!--more-->

<h3 id="generators">Generators</h3>

<p>Generators are one of those cool language features that provide a lot of
power at a small implementation cost. They’re like a constrained form of
coroutines, but, unlike coroutines, they’re typically built entirely on
top of first-class functions (e.g. closures). This means <em>no additional
run-time support is needed</em> in order to add generators to a language.
The only complications are the changes to the compiler. Generators are
not compiled the same way as normal functions despite looking so
similar.</p>

<p>What’s perhaps coolest of all about lisp-family generators, including
Emacs Lisp, is that the compiler component can be <em>implemented
entirely with macros</em>. The compiler need not be modified at all,
making generators no more than a library, and not actually part of the
language. That’s exactly how they’ve been implemented in Emacs Lisp
(<code class="language-plaintext highlighter-rouge">emacs-lisp/generator.el</code>).</p>

<p>So what’s a generator? It’s a function that returns an <em>iterator
object</em>. When an iterator object is invoked (e.g. <code class="language-plaintext highlighter-rouge">iter-next</code>) it
evaluates the body of the generator. Each iterator is independent.
What makes them unusual (and useful) is that the evaluation is
<em>paused</em> in the middle of the body to return a value, saving all the
internal state in the iterator. Normally pausing in the middle of
functions isn’t possible, which is what requires the special compiler
support.</p>

<p>Emacs Lisp generators appear to be most closely modeled after <a href="https://wiki.python.org/moin/Generators">Python
generators</a>, though it also shares some similarities to
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators">JavaScript generators</a>. What makes it most like Python is the use
of signals for flow control — something I’m <a href="http://wiki.c2.com/?DontUseExceptionsForFlowControl">not personally enthused
about</a>. When a Python generator
completes, it throws a <code class="language-plaintext highlighter-rouge">StopItertion</code> exception. In Emacs Lisp, it’s
an <code class="language-plaintext highlighter-rouge">iter-end-of-sequence</code> signal. A signal is out-of-band and avoids
the issue relying on some special in-band value to communicate the end
of iteration.</p>

<p>In contrast, JavaScript’s solution is to return a “rich” object wrapping
the actual yield value. This object has a <code class="language-plaintext highlighter-rouge">done</code> field that communicates
whether iteration has completed. This avoids the use of exceptions for
flow control, but the caller has to unpack the rich object.</p>

<p>Fortunately the flow control issue isn’t normally exposed to Emacs Lisp
code. Most of the time you’ll use the <code class="language-plaintext highlighter-rouge">iter-do</code> macro or (my preference)
the new <code class="language-plaintext highlighter-rouge">cl-loop</code> keyword <code class="language-plaintext highlighter-rouge">iter-by</code>.</p>

<p>To illustrate how a generator works, here’s a really simple iterator
that iterates over a list:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">iter-defun</span> <span class="nv">walk</span> <span class="p">(</span><span class="nb">list</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">while</span> <span class="nb">list</span>
    <span class="p">(</span><span class="nv">iter-yield</span> <span class="p">(</span><span class="nb">pop</span> <span class="nb">list</span><span class="p">))))</span>
</code></pre></div></div>

<p>Here’s how it might be used:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">setf</span> <span class="nv">i</span> <span class="p">(</span><span class="nv">walk</span> <span class="o">'</span><span class="p">(</span><span class="ss">:a</span> <span class="ss">:b</span> <span class="ss">:c</span><span class="p">)))</span>

<span class="p">(</span><span class="nv">iter-next</span> <span class="nv">i</span><span class="p">)</span>  <span class="c1">; =&gt; :a</span>
<span class="p">(</span><span class="nv">iter-next</span> <span class="nv">i</span><span class="p">)</span>  <span class="c1">; =&gt; :b</span>
<span class="p">(</span><span class="nv">iter-next</span> <span class="nv">i</span><span class="p">)</span>  <span class="c1">; =&gt; :c</span>
<span class="p">(</span><span class="nv">iter-next</span> <span class="nv">i</span><span class="p">)</span>  <span class="c1">; error: iter-end-of-sequence</span>
</code></pre></div></div>

<p>The iterator object itself is <em>opaque</em> and you shouldn’t rely on any
part of its structure. That being said, I’m a firm believer that we
should understand how things work underneath the hood so that we can
make the most effective use of at them. No program should rely on the
particulars of the iterator object internals for <em>correctness</em>, but a
well-written program should employ them in a way that <a href="/blog/2017/01/30/">best exploits
their expected implementation</a>.</p>

<p>Currently iterator objects are closures, and <code class="language-plaintext highlighter-rouge">iter-next</code> invokes the
closure with its own internal protocol. It asks the closure to return
the next value (<code class="language-plaintext highlighter-rouge">:next</code> operation), and <code class="language-plaintext highlighter-rouge">iter-close</code> asks it to clean
itself up (<code class="language-plaintext highlighter-rouge">:close</code> operation).</p>

<p>Since they’re just closures, another <em>really</em> cool thing about Emacs
Lisp generators is that <a href="/blog/2013/12/30/">iterator objects are generally readable</a>.
That is, you can serialize them out with <code class="language-plaintext highlighter-rouge">print</code> and bring them back to
life with <code class="language-plaintext highlighter-rouge">read</code>, even in another instance of Emacs. They exist
independently of the original generator function. This will not work if
one of the values captured in the iterator object is not readable (e.g.
buffers).</p>

<p>How does pausing work? Well, one of other exciting new features of
Emacs 26 is the introduction of a jump table opcode, <code class="language-plaintext highlighter-rouge">switch</code>. I’d
lamented in the past that large <code class="language-plaintext highlighter-rouge">cond</code> and <code class="language-plaintext highlighter-rouge">cl-case</code> expressions could
be a lot more efficient if Emacs’ byte code supported jump tables. It
turns an O(n) sequence of comparisons into an O(1) lookup and jump.
It’s essentially the perfect foundation for a generator since it can
be used to <a href="https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html">jump straight back to the position</a> where evaluation
was paused.</p>

<p><em>Buuut</em>, generators do not currently use jump tables. The generator
library predates the new <code class="language-plaintext highlighter-rouge">switch</code> opcode, and, being independent of it,
its author, Daniel Colascione, went with the best option at the time.
Chunks of code between yields are packaged as individual closures. These
closures are linked together a bit like nodes in a graph, creating a
sort of state machine. To get the next value, the iterator object
invokes the closure representing the next state.</p>

<p>I’ve <em>manually</em> macro expanded the <code class="language-plaintext highlighter-rouge">walk</code> generator above into a form
that <em>roughly</em> resembles the expansion of <code class="language-plaintext highlighter-rouge">iter-defun</code>:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">walk</span> <span class="p">(</span><span class="nb">list</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">(</span><span class="nv">state</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">cl-flet*</span> <span class="p">((</span><span class="nv">state-2</span> <span class="p">()</span>
                 <span class="p">(</span><span class="nb">signal</span> <span class="ss">'iter-end-of-sequence</span> <span class="no">nil</span><span class="p">))</span>
               <span class="p">(</span><span class="nv">state-1</span> <span class="p">()</span>
                 <span class="p">(</span><span class="nb">prog1</span> <span class="p">(</span><span class="nb">pop</span> <span class="nb">list</span><span class="p">)</span>
                   <span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">null</span> <span class="nb">list</span><span class="p">)</span>
                     <span class="p">(</span><span class="nb">setf</span> <span class="nv">state</span> <span class="nf">#'</span><span class="nv">state-2</span><span class="p">))))</span>
               <span class="p">(</span><span class="nv">state-0</span> <span class="p">()</span>
                 <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">null</span> <span class="nb">list</span><span class="p">)</span>
                     <span class="p">(</span><span class="nv">state-2</span><span class="p">)</span>
                   <span class="p">(</span><span class="nb">setf</span> <span class="nv">state</span> <span class="nf">#'</span><span class="nv">state-1</span><span class="p">)</span>
                   <span class="p">(</span><span class="nv">state-1</span><span class="p">))))</span>
      <span class="p">(</span><span class="nb">setf</span> <span class="nv">state</span> <span class="nf">#'</span><span class="nv">state-0</span><span class="p">)</span>
      <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span>
        <span class="p">(</span><span class="nb">funcall</span> <span class="nv">state</span><span class="p">)))))</span>
</code></pre></div></div>

<p>This omits the protocol I mentioned, and it doesn’t have yield results
(values passed to the iterator). The actual expansion is a whole lot
messier and less optimal than this, but hopefully my hand-rolled
generator is illustrative enough. Without the protocol, this iterator is
stepped using <code class="language-plaintext highlighter-rouge">funcall</code> rather than <code class="language-plaintext highlighter-rouge">iter-next</code>.</p>

<p>The <code class="language-plaintext highlighter-rouge">state</code> variable keeps track of where in the body of the generator
this iterator is currently “paused.” Continuing the iterator is
therefore just a matter of invoking the closure that represents this
state. Each state closure may update <code class="language-plaintext highlighter-rouge">state</code> to point to a new part of
the generator body. The terminal state is obviously <code class="language-plaintext highlighter-rouge">state-2</code>. Notice
how state transitions occur around branches.</p>

<p>I had said generators can be implemented as a library in Emacs Lisp.
Unfortunately theres a hole in this: <code class="language-plaintext highlighter-rouge">unwind-protect</code>. It’s not valid to
yield inside an <code class="language-plaintext highlighter-rouge">unwind-protect</code> form. Unlike, say, a throw-catch,
there’s no mechanism to trap an unwinding stack so that it can be
restarted later. The state closure needs to return and fall through the
<code class="language-plaintext highlighter-rouge">unwind-protect</code>.</p>

<p>A jump table version of the generator might look like the following.
I’ve used <code class="language-plaintext highlighter-rouge">cl-labels</code> since it allows for recursion.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">walk</span> <span class="p">(</span><span class="nb">list</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">state</span> <span class="mi">0</span><span class="p">))</span>
    <span class="p">(</span><span class="nv">cl-labels</span>
        <span class="p">((</span><span class="nv">closure</span> <span class="p">()</span>
           <span class="p">(</span><span class="nv">cl-case</span> <span class="nv">state</span>
             <span class="p">(</span><span class="mi">0</span> <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">null</span> <span class="nb">list</span><span class="p">)</span>
                    <span class="p">(</span><span class="nb">setf</span> <span class="nv">state</span> <span class="mi">2</span><span class="p">)</span>
                  <span class="p">(</span><span class="nb">setf</span> <span class="nv">state</span> <span class="mi">1</span><span class="p">))</span>
                <span class="p">(</span><span class="nv">closure</span><span class="p">))</span>
             <span class="p">(</span><span class="mi">1</span> <span class="p">(</span><span class="nb">prog1</span> <span class="p">(</span><span class="nb">pop</span> <span class="nb">list</span><span class="p">)</span>
                  <span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">null</span> <span class="nb">list</span><span class="p">)</span>
                    <span class="p">(</span><span class="nb">setf</span> <span class="nv">state</span> <span class="mi">2</span><span class="p">))))</span>
             <span class="p">(</span><span class="mi">2</span> <span class="p">(</span><span class="nb">signal</span> <span class="ss">'iter-end-of-sequence</span> <span class="no">nil</span><span class="p">)))))</span>
      <span class="nf">#'</span><span class="nv">closure</span><span class="p">)))</span>
</code></pre></div></div>

<p>When byte compiled on Emacs 26, that <code class="language-plaintext highlighter-rouge">cl-case</code> is turned into a jump
table. This “switch” form is closer to how generators are implemented in
other languages.</p>

<p>Iterator objects can <a href="/blog/2017/12/14/">share state between themselves</a> if they
close over a common environment (or, of course, use the same global
variables).</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">setf</span> <span class="nv">foo</span>
      <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nb">list</span> <span class="o">'</span><span class="p">(</span><span class="ss">:a</span> <span class="ss">:b</span> <span class="ss">:c</span><span class="p">)))</span>
        <span class="p">(</span><span class="nb">list</span>
         <span class="p">(</span><span class="nb">funcall</span>
          <span class="p">(</span><span class="nv">iter-lambda</span> <span class="p">()</span>
            <span class="p">(</span><span class="nv">while</span> <span class="nb">list</span>
              <span class="p">(</span><span class="nv">iter-yield</span> <span class="p">(</span><span class="nb">pop</span> <span class="nb">list</span><span class="p">)))))</span>
         <span class="p">(</span><span class="nb">funcall</span>
          <span class="p">(</span><span class="nv">iter-lambda</span> <span class="p">()</span>
            <span class="p">(</span><span class="nv">while</span> <span class="nb">list</span>
              <span class="p">(</span><span class="nv">iter-yield</span> <span class="p">(</span><span class="nb">pop</span> <span class="nb">list</span><span class="p">))))))))</span>

<span class="p">(</span><span class="nv">iter-next</span> <span class="p">(</span><span class="nb">nth</span> <span class="mi">0</span> <span class="nv">foo</span><span class="p">))</span>  <span class="c1">; =&gt; :a</span>
<span class="p">(</span><span class="nv">iter-next</span> <span class="p">(</span><span class="nb">nth</span> <span class="mi">1</span> <span class="nv">foo</span><span class="p">))</span>  <span class="c1">; =&gt; :b</span>
<span class="p">(</span><span class="nv">iter-next</span> <span class="p">(</span><span class="nb">nth</span> <span class="mi">0</span> <span class="nv">foo</span><span class="p">))</span>  <span class="c1">; =&gt; :c</span>
</code></pre></div></div>

<p>For years there has been a <em>very</em> crude way to “pause” a function and
allow other functions to run: <code class="language-plaintext highlighter-rouge">accept-process-output</code>. It only works in
the context of processes, but five years ago this was <a href="/blog/2013/01/14/">sufficient for me
to build primitives on top of it</a>. Unlike this old process
function, generators do not block threads, including the user interface,
which is really important.</p>

<h3 id="threads">Threads</h3>

<p>Emacs 26 also bring us threads, which have been attached in a very
bolted on fashion. It’s not much more than a subset of pthreads: shared
memory threads, recursive mutexes, and condition variables. The
interfaces look just like they do in pthreads, and there hasn’t been
much done to integrate more naturally into the Emacs Lisp ecosystem.</p>

<p>This is also only the first step in bringing threading to Emacs Lisp.
Right now there’s effectively a global interpreter lock (GIL), and
threads only run one at a time cooperatively. Like with generators, the
Python influence is obvious. In theory, sometime in the future this
interpreter lock will be removed, making way for actual concurrency.</p>

<p>This is, again, where I think it’s useful to contrast with JavaScript,
which was also initially designed to be single-threaded. Low-level
threading primitives weren’t exposed — though mostly because
JavaScript typically runs sandboxed and there’s no safe way to expose
those primitives. Instead it got a <a href="/blog/2013/01/26/">web worker API</a> that exposes
concurrency at a much higher level, along with an efficient interface
for thread coordination.</p>

<p>For Emacs Lisp, I’d prefer something safer, more like the JavaScript
approach. Low-level pthreads are now a great way to wreck Emacs with
deadlocks (with no <code class="language-plaintext highlighter-rouge">C-g</code> escape). Playing around with the new
threading API for just a few days, I’ve already had to restart Emacs a
bunch of times. Bugs in Emacs Lisp are normally a lot more forgiving.</p>

<p>One important detail that has been designed well is that dynamic
bindings are thread-local. This is really essential for correct
behavior. This is also an easy way to create thread-local storage
(TLS): dynamically bind variables in the thread’s entrance function.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">;;; -*- lexical-binding: t; -*-</span>

<span class="p">(</span><span class="nb">defvar</span> <span class="nv">foo-counter-tls</span><span class="p">)</span>
<span class="p">(</span><span class="nb">defvar</span> <span class="nv">foo-path-tls</span><span class="p">)</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">foo-make-thread</span> <span class="p">(</span><span class="nv">path</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">make-thread</span>
   <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span>
     <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">foo-counter-tls</span> <span class="mi">0</span><span class="p">)</span>
           <span class="p">(</span><span class="nv">foo-name-tls</span> <span class="nv">path</span><span class="p">))</span>
       <span class="o">...</span><span class="p">))))</span>
</code></pre></div></div>

<p>However, <strong><code class="language-plaintext highlighter-rouge">cl-letf</code> “bindings” are <em>not</em> thread-local</strong>, which makes
this <a href="/blog/2017/10/27/">otherwise incredibly useful macro</a> quite dangerous in the
presence of threads. This is one way that the new threading API feels
bolted on.</p>

<h4 id="building-generators-on-threads">Building generators on threads</h4>

<p>In <a href="/blog/2017/06/21/">my stack clashing article</a> I showed a few different ways to
add coroutine support to C. One method spawned per-coroutine threads,
and coordinated using semaphores. With the new threads API in Emacs,
it’s possible to do exactly the same thing.</p>

<p>Since generators are just a limited form of coroutines, this means
threads offer another, <em>very</em> different way to implement them. The
threads API doesn’t provide semaphores, but condition variables can fill
in for them. To “pause” in the middle of the generator, just wait on a
condition variable.</p>

<p>So, naturally, I just had to see if I could make it work. I call it a
“thread iterator” or “thriter.” The API is <em>very</em> similar to <code class="language-plaintext highlighter-rouge">iter</code>:</p>

<p><strong><a href="https://github.com/skeeto/thriter">https://github.com/skeeto/thriter</a></strong></p>

<p>This is merely a proof of concept so don’t actually use this library
for anything. These thread-based generators are about 5x slower than
<code class="language-plaintext highlighter-rouge">iter</code> generators, and they’re a lot more heavy-weight, needing an
entire thread per iterator object. This makes <code class="language-plaintext highlighter-rouge">thriter-close</code> all the
more important. On the other hand, these generators have no problem
yielding inside <code class="language-plaintext highlighter-rouge">unwind-protect</code>.</p>

<p>Originally this article was going to dive into the details of how
these thread-iterators worked, but <code class="language-plaintext highlighter-rouge">thriter</code> turned out to be quite a
bit more complicated than I anticipated, especially as I worked
towards feature matching <code class="language-plaintext highlighter-rouge">iter</code>.</p>

<p>The gist of it is that each side of a next/yield transaction gets its
own condition variable, but share a common mutex. Values are passed
between the threads using slots on the iterator object. The side that
isn’t currently running waits on a condition variable until the other
side frees it, after which the releaser waits on its own condition
variable for the result. This is similar to <a href="/blog/2017/02/14/">asynchronous requests in
Emacs dynamic modules</a>.</p>

<p>Rather than use signals to indicate completion, I modeled it after
JavaScript generators. Iterators return a cons cell. The car indicates
continuation and the cdr holds the yield result. To terminate an
iterator early (<code class="language-plaintext highlighter-rouge">thriter-close</code> or garbage collection), <code class="language-plaintext highlighter-rouge">thread-signal</code>
is used to essentially “cancel” the thread and knock it off the
condition variable.</p>

<p>Since threads aren’t (and shouldn’t be) garbage collected, failing to
run a thread-iterator to completion would normally cause a memory leak,
as the thread <a href="https://www.youtube.com/watch?v=AK3PWHxoT_E">sits there forever waiting on a “next” that will never
come</a>. To deal with this, there’s a finalizer is attached to the
iterator object in such a way that it’s not visible to the thread. A
lost iterator is eventually cleaned up by the garbage collector, but, as
usual with finalizers, this is <a href="https://utcc.utoronto.ca/~cks/space/blog/programming/GoFinalizersStopLeaks">only a last resort</a>.</p>

<h4 id="the-future-of-threads">The future of threads</h4>

<p>This thread-iterator project was my initial, little experiment with
Emacs Lisp threads, similar to why I <a href="/blog/2016/11/05/">connected a joystick to Emacs
using a dynamic module</a>. While I don’t expect the current thread
API to go away, it’s not really suitable for general use in its raw
form. Bugs in Emacs Lisp programs should virtually never bring down
Emacs and require a restart. Outside of threads, the few situations
that break this rule are very easy to avoid (and very obvious that
something dangerous is happening). Dynamic modules are dangerous by
necessity, but concurrency doesn’t have to be.</p>

<p>There really needs to be a safe, high-level API with clean thread
isolation. Perhaps this higher-level API will eventually build on top of
the low-level threading API.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>RSA Signatures in Emacs Lisp</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2015/10/30/"/>
    <id>urn:uuid:9d9ef14d-d513-3cad-b053-fb016f3c3bf0</id>
    <updated>2015-10-30T22:35:13Z</updated>
    <category term="emacs"/><category term="elisp"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<p>Emacs comes with a wonderful arbitrary-precision computer algebra
system called <a href="http://www.gnu.org/software/emacs/manual/html_mono/calc.html">calc</a>. I’ve <a href="/blog/2009/06/23/">discussed it previously</a> and
continue to use it on a daily basis. That’s right, people, <em>Emacs can
do calculus</em>. Like everything Emacs, it’s programmable and extensible
from Emacs Lisp. In this article, I’m going to implement the <a href="https://en.wikipedia.org/wiki/RSA_(cryptosystem)">RSA
public-key cryptosystem</a> in Emacs Lisp using calc.</p>

<p>If you want to dive right in first, here’s the repository:</p>

<ul>
  <li><a href="https://github.com/skeeto/emacs-rsa">https://github.com/skeeto/emacs-rsa</a></li>
</ul>

<p>This is only a toy implementation and not really intended for serious
cryptographic work. It’s also far too slow when using keys of
reasonable length.</p>

<h3 id="evaluation-with-calc">Evaluation with calc</h3>

<p>The calc package is particularly useful when considering Emacs’
limited integer type. Emacs uses a tagged integer scheme where
integers are embedded within pointers. It’s a lot faster than the
alternative (individually-allocated integer objects), but it means
they’re always a few bits short of the platform’s native integer type.</p>

<p>calc has a large API, but the user-friendly porcelain for it is the
under-documented <code class="language-plaintext highlighter-rouge">calc-eval</code> function. It evaluates an expression
string with format-like argument substitutions (<code class="language-plaintext highlighter-rouge">$n</code>).</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"2^16 - 1"</span><span class="p">)</span>
<span class="c1">;; =&gt; "65535"</span>

<span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"2^$1 - 1"</span> <span class="no">nil</span> <span class="mi">128</span><span class="p">)</span>
<span class="c1">;; =&gt; "340282366920938463463374607431768211455"</span>
</code></pre></div></div>

<p>Notice it returns strings, which is one of the ways calc represents
arbitrary precision numbers. For arguments, it accepts regular Elisp
numbers and strings just like this function returns. The implicit
radix is 10. To explicitly set the radix, prefix the number with the
radix and <code class="language-plaintext highlighter-rouge">#</code>. This is the same as in the user interface of calc. For
example:</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"16#deadbeef"</span><span class="p">)</span>
<span class="c1">;; =&gt; "3735928559"</span>
</code></pre></div></div>

<p>The second argument (optional) to <code class="language-plaintext highlighter-rouge">calc-eval</code> adjusts its behavior.
Given <code class="language-plaintext highlighter-rouge">nil</code>, it simply evaluates the string and returns the result.
The manual documents the different options, but the only other
relevant option for RSA is the symbol <code class="language-plaintext highlighter-rouge">pred</code>, which asks it to return
a boolean “predicate” result.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 &lt; $2"</span> <span class="ss">'pred</span> <span class="s">"4000"</span> <span class="s">"5000"</span><span class="p">)</span>
<span class="c1">;; =&gt; t</span>
</code></pre></div></div>

<h3 id="generating-primes">Generating primes</h3>

<p>RSA is founded on the difficulty of factoring large composites with
large factors. Generating an RSA keypair starts with generating two
prime numbers, <code class="language-plaintext highlighter-rouge">p</code> and <code class="language-plaintext highlighter-rouge">q</code>, and using these primes to compute two
mathematically related composite numbers.</p>

<p>calc has a function <code class="language-plaintext highlighter-rouge">calc-next-prime</code> for finding the next prime
number following any arbitrary number. It uses a probabilistic
primarily test — the <del>Fermat</del> Miller-Rabin primality test
— to efficiently test large integers. It increments the input until
it finds a result that passes enough iterations of the primality test.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"nextprime($1)"</span> <span class="no">nil</span> <span class="s">"100000000000000000"</span><span class="p">)</span>
<span class="c1">;; =&gt; "100000000000000003"</span>
</code></pre></div></div>

<p>So to generate a random n-bit prime, first generate a random n-bit
number and then increment it until a prime number is found.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">;; Generate a 128-bit prime, 10 iterations (0.000084% error rate)</span>
<span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"nextprime(random(2^$1), 10)"</span> <span class="no">nil</span> <span class="mi">128</span><span class="p">)</span>
<span class="s">"111618319598394878409654851283959105123"</span>
</code></pre></div></div>

<p>Unfortunately calc’s <code class="language-plaintext highlighter-rouge">random</code> function is based on Emacs’ <code class="language-plaintext highlighter-rouge">random</code>
function, which is entirely unsuitable for cryptography. In the real
implementation I read n bits from <code class="language-plaintext highlighter-rouge">/dev/urandom</code> to generate an n-bit
number.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">with-temp-buffer</span>
  <span class="p">(</span><span class="nv">set-buffer-multibyte</span> <span class="no">nil</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">call-process</span> <span class="s">"head"</span> <span class="s">"/dev/urandom"</span> <span class="no">t</span> <span class="no">nil</span> <span class="s">"-c"</span> <span class="p">(</span><span class="nb">format</span> <span class="s">"%d"</span> <span class="p">(</span><span class="nb">/</span> <span class="nv">bits</span> <span class="mi">8</span><span class="p">)))</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">f</span> <span class="p">(</span><span class="nv">apply-partially</span> <span class="nf">#'</span><span class="nb">format</span> <span class="s">"%02x"</span><span class="p">)))</span>
    <span class="p">(</span><span class="nv">concat</span> <span class="s">"16#"</span> <span class="p">(</span><span class="nv">mapconcat</span> <span class="nv">f</span> <span class="p">(</span><span class="nv">buffer-string</span><span class="p">)</span> <span class="s">""</span><span class="p">))))</span>
</code></pre></div></div>

<p>(Note: <code class="language-plaintext highlighter-rouge">/dev/urandom</code> <em>is</em> the right choice. There’s <a href="http://www.2uo.de/myths-about-urandom/">no reason to use
<code class="language-plaintext highlighter-rouge">/dev/random</code> for generating keys</a>.)</p>

<h3 id="computing-e-and-d">Computing e and d</h3>

<p>From here the code just follows along from the Wikipedia article.
After generating the primes <code class="language-plaintext highlighter-rouge">p</code> and <code class="language-plaintext highlighter-rouge">q</code>, two composites are computed,
<code class="language-plaintext highlighter-rouge">n = p * q</code> and <code class="language-plaintext highlighter-rouge">i = (p - 1) * (q - 1)</code>. Lacking any reason to do
otherwise, I chose 65,537 for the public exponent <code class="language-plaintext highlighter-rouge">e</code>.</p>

<p>The function <code class="language-plaintext highlighter-rouge">rsa--inverse</code> is just a straight Emacs Lisp + calc
implementation of the extended Euclidean algorithm from <a href="https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm">the Wikipedia
article pseudocode</a>, computing <code class="language-plaintext highlighter-rouge">d ≡ e^-1 (mod i)</code>. It’s not much
use sharing it here, so take a look at the repository if you’re
curious.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">rsa-generate-keypair</span> <span class="p">(</span><span class="nv">bits</span><span class="p">)</span>
  <span class="s">"Generate a fresh RSA keypair plist of BITS length."</span>
  <span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">p</span> <span class="p">(</span><span class="nv">rsa-generate-prime</span> <span class="p">(</span><span class="nb">+</span> <span class="mi">1</span> <span class="p">(</span><span class="nb">/</span> <span class="nv">bits</span> <span class="mi">2</span><span class="p">))))</span>
         <span class="p">(</span><span class="nv">q</span> <span class="p">(</span><span class="nv">rsa-generate-prime</span> <span class="p">(</span><span class="nb">+</span> <span class="mi">1</span> <span class="p">(</span><span class="nb">/</span> <span class="nv">bits</span> <span class="mi">2</span><span class="p">))))</span>
         <span class="p">(</span><span class="nv">n</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 * $2"</span> <span class="no">nil</span> <span class="nv">p</span> <span class="nv">q</span><span class="p">))</span>
         <span class="p">(</span><span class="nv">i</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"($1 - 1) * ($2 - 1)"</span> <span class="no">nil</span> <span class="nv">p</span> <span class="nv">q</span><span class="p">))</span>
         <span class="p">(</span><span class="nv">e</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"2^16+1"</span><span class="p">))</span>
         <span class="p">(</span><span class="nv">d</span> <span class="p">(</span><span class="nv">rsa--inverse</span> <span class="nv">e</span> <span class="nv">i</span><span class="p">)))</span>
    <span class="o">`</span><span class="p">(</span><span class="ss">:public</span>  <span class="p">(</span><span class="ss">:n</span> <span class="o">,</span><span class="nv">n</span> <span class="ss">:e</span> <span class="o">,</span><span class="nv">e</span><span class="p">)</span> <span class="ss">:private</span> <span class="p">(</span><span class="ss">:n</span> <span class="o">,</span><span class="nv">n</span> <span class="ss">:d</span> <span class="o">,</span><span class="nv">d</span><span class="p">))))</span>
</code></pre></div></div>

<p>The public key is <code class="language-plaintext highlighter-rouge">n</code> and <code class="language-plaintext highlighter-rouge">e</code> and the private key is <code class="language-plaintext highlighter-rouge">n</code> and <code class="language-plaintext highlighter-rouge">d</code>. From
here we can compute and verify cryptographic signatures.</p>

<h3 id="signatures">Signatures</h3>

<p>To compute signature <code class="language-plaintext highlighter-rouge">s</code> of an integer <code class="language-plaintext highlighter-rouge">m</code> (where <code class="language-plaintext highlighter-rouge">m &lt; n</code>), compute
<code class="language-plaintext highlighter-rouge">s ≡ m^d (mod n)</code>. I chose the right-to-left binary method, again
straight from <a href="https://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method">the Wikipedia pseudocode</a> (lazy!). I’ll share this
one since it’s short. The backslash denotes integer division.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">rsa--mod-pow</span> <span class="p">(</span><span class="nv">base</span> <span class="nv">exponent</span> <span class="nv">modulus</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">result</span> <span class="mi">1</span><span class="p">))</span>
    <span class="p">(</span><span class="nb">setf</span> <span class="nv">base</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 % $2"</span> <span class="no">nil</span> <span class="nv">base</span> <span class="nv">modulus</span><span class="p">))</span>
    <span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 &gt; 0"</span> <span class="ss">'pred</span> <span class="nv">exponent</span><span class="p">)</span>
      <span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 % 2 == 1"</span> <span class="ss">'pred</span> <span class="nv">exponent</span><span class="p">)</span>
        <span class="p">(</span><span class="nb">setf</span> <span class="nv">result</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"($1 * $2) % $3"</span> <span class="no">nil</span> <span class="nv">result</span> <span class="nv">base</span> <span class="nv">modulus</span><span class="p">)))</span>
      <span class="p">(</span><span class="nb">setf</span> <span class="nv">exponent</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 \\ 2"</span> <span class="no">nil</span> <span class="nv">exponent</span><span class="p">)</span>
            <span class="nv">base</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"($1 * $1) % $2"</span> <span class="no">nil</span> <span class="nv">base</span> <span class="nv">modulus</span><span class="p">)))</span>
    <span class="nv">result</span><span class="p">))</span>
</code></pre></div></div>

<p>Verifying the signature is the same process, but with the public key’s
<code class="language-plaintext highlighter-rouge">e</code>: <code class="language-plaintext highlighter-rouge">m ≡ s^e (mod n)</code>. If the signature is valid, <code class="language-plaintext highlighter-rouge">m</code> will be
recovered. In theory, only someone who knows <code class="language-plaintext highlighter-rouge">d</code> can feasibly compute
<code class="language-plaintext highlighter-rouge">s</code> from <code class="language-plaintext highlighter-rouge">m</code>. If <code class="language-plaintext highlighter-rouge">n</code> is <a href="http://crypto.stackexchange.com/a/5942">small enough to factor</a>, revealing
<code class="language-plaintext highlighter-rouge">p</code> and <code class="language-plaintext highlighter-rouge">q</code>, then <code class="language-plaintext highlighter-rouge">d</code> can be feasibly recomputed from the public key.
So mind your Ps and Qs.</p>

<p>So that leaves one problem: generally users want to sign strings and
files and such, not integers. A hash function is used to reduce an
arbitrary quantity of data into an integer suitable for signing. Emacs
comes with a bunch of them, accessible through <code class="language-plaintext highlighter-rouge">secure-hash</code>. It
hashes strings and buffers.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">secure-hash</span> <span class="ss">'sha224</span> <span class="s">"Hello, world!"</span><span class="p">)</span>
<span class="c1">;; =&gt; "8552d8b7a7dc5476cb9e25dee69a8091290764b7f2a64fe6e78e9568"</span>
</code></pre></div></div>

<p>Since the result is hexadecimal, just prefix <code class="language-plaintext highlighter-rouge">16#</code> to turn it into a
calc integer.</p>

<p>Here’s the signature and verification functions. Any string or buffer
can be signed.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">rsa-sign</span> <span class="p">(</span><span class="nv">private-key</span> <span class="nv">object</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">n</span> <span class="p">(</span><span class="nv">plist-get</span> <span class="nv">private-key</span> <span class="ss">:n</span><span class="p">))</span>
        <span class="p">(</span><span class="nv">d</span> <span class="p">(</span><span class="nv">plist-get</span> <span class="nv">private-key</span> <span class="ss">:d</span><span class="p">))</span>
        <span class="p">(</span><span class="nv">hash</span> <span class="p">(</span><span class="nv">concat</span> <span class="s">"16#"</span> <span class="p">(</span><span class="nv">secure-hash</span> <span class="ss">'sha384</span> <span class="nv">object</span><span class="p">))))</span>
    <span class="c1">;; truncate hash such that hash &lt; n</span>
    <span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 &gt; $2"</span> <span class="ss">'pred</span> <span class="nv">hash</span> <span class="nv">n</span><span class="p">)</span>
      <span class="p">(</span><span class="nb">setf</span> <span class="nv">hash</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 \\ 2"</span> <span class="no">nil</span> <span class="nv">hash</span><span class="p">)))</span>
    <span class="p">(</span><span class="nv">rsa--mod-pow</span> <span class="nv">hash</span> <span class="nv">d</span> <span class="nv">n</span><span class="p">)))</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">rsa-verify</span> <span class="p">(</span><span class="nv">public-key</span> <span class="nv">object</span> <span class="nv">sig</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">n</span> <span class="p">(</span><span class="nv">plist-get</span> <span class="nv">public-key</span> <span class="ss">:n</span><span class="p">))</span>
        <span class="p">(</span><span class="nv">e</span> <span class="p">(</span><span class="nv">plist-get</span> <span class="nv">public-key</span> <span class="ss">:e</span><span class="p">))</span>
        <span class="p">(</span><span class="nv">hash</span> <span class="p">(</span><span class="nv">concat</span> <span class="s">"16#"</span> <span class="p">(</span><span class="nv">secure-hash</span> <span class="ss">'sha384</span> <span class="nv">object</span><span class="p">))))</span>
    <span class="c1">;; truncate hash such that hash &lt; n</span>
    <span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 &gt; $2"</span> <span class="ss">'pred</span> <span class="nv">hash</span> <span class="nv">n</span><span class="p">)</span>
      <span class="p">(</span><span class="nb">setf</span> <span class="nv">hash</span> <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 \\ 2"</span> <span class="no">nil</span> <span class="nv">hash</span><span class="p">)))</span>
    <span class="p">(</span><span class="k">let*</span> <span class="p">((</span><span class="nv">result</span> <span class="p">(</span><span class="nv">rsa--mod-pow</span> <span class="nv">sig</span> <span class="nv">e</span> <span class="nv">n</span><span class="p">)))</span>
      <span class="p">(</span><span class="nv">calc-eval</span> <span class="s">"$1 == $2"</span> <span class="ss">'pred</span> <span class="nv">result</span> <span class="nv">hash</span><span class="p">))))</span>
</code></pre></div></div>

<p>Note the hash truncation step. If this is actually necessary, then
your <code class="language-plaintext highlighter-rouge">n</code> is <em>very</em> easy to factor! It’s in there since this is just a
toy and I want it to work with small keys.</p>

<h3 id="putting-it-all-together">Putting it all together</h3>

<p>Here’s the whole thing in action with an extremely small, 128-bit key.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">setf</span> <span class="nv">message</span> <span class="s">"hello, world!"</span><span class="p">)</span>

<span class="p">(</span><span class="nb">setf</span> <span class="nv">keypair</span> <span class="p">(</span><span class="nv">rsa-generate-keypair</span> <span class="mi">128</span><span class="p">))</span>
<span class="c1">;; =&gt; (:public  (:n "74924929503799951536367992905751084593"</span>
<span class="c1">;;               :e "65537")</span>
<span class="c1">;;     :private (:n "74924929503799951536367992905751084593"</span>
<span class="c1">;;               :d "36491277062297490768595348639394259869"))</span>

<span class="p">(</span><span class="nb">setf</span> <span class="nv">sig</span> <span class="p">(</span><span class="nv">rsa-sign</span> <span class="p">(</span><span class="nv">plist-get</span> <span class="nv">keypair</span> <span class="ss">:private</span><span class="p">)</span> <span class="nv">message</span><span class="p">))</span>
<span class="c1">;; =&gt; "31982247477262471348259501761458827454"</span>

<span class="p">(</span><span class="nv">rsa-verify</span> <span class="p">(</span><span class="nv">plist-get</span> <span class="nv">keypair</span> <span class="ss">:public</span><span class="p">)</span> <span class="nv">message</span> <span class="nv">sig</span><span class="p">)</span>
<span class="c1">;; =&gt; t</span>

<span class="p">(</span><span class="nv">rsa-verify</span> <span class="p">(</span><span class="nv">plist-get</span> <span class="nv">keypair</span> <span class="ss">:public</span><span class="p">)</span> <span class="p">(</span><span class="nv">capitalize</span> <span class="nv">message</span><span class="p">)</span> <span class="nv">sig</span><span class="p">)</span>
<span class="c1">;; =&gt; nil</span>
</code></pre></div></div>

<p>Each of these operations took less than a second. For larger,
secure-length keys, this implementation is painfully slow. For
example, generating a 2048-bit key takes my laptop about half an hour,
and computing a signature with that key (any size message) takes about
a minute. That’s probably a little too slow for, say, signing ELPA
packages.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Emacs Lisp Defstruct Namespace Convention</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2014/03/19/"/>
    <id>urn:uuid:624f92a9-6696-33bb-f955-d6c83da56fc1</id>
    <updated>2014-03-19T01:41:52Z</updated>
    <category term="emacs"/><category term="lisp"/><category term="elisp"/>
    <content type="html">
      <![CDATA[<p>One of the drawbacks of Emacs Lisp is the lack of namespaces. Every
<code class="language-plaintext highlighter-rouge">defun</code>, <code class="language-plaintext highlighter-rouge">defvar</code>, <code class="language-plaintext highlighter-rouge">defcustom</code>, <code class="language-plaintext highlighter-rouge">defface</code>, <code class="language-plaintext highlighter-rouge">defalias</code>, <code class="language-plaintext highlighter-rouge">defstruct</code>,
and <code class="language-plaintext highlighter-rouge">defclass</code> establishes one or more names in the global scope. To
work around this, package authors are strongly encouraged to prefix
every global name with the name of its package. That way there should
never be a naming conflict between two different packages.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defvar</span> <span class="nv">mypackage-foo-limit</span> <span class="mi">10</span><span class="p">)</span>

<span class="p">(</span><span class="nb">defvar</span> <span class="nv">mypackage--bar-counter</span> <span class="mi">0</span><span class="p">)</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">mypackage-init</span> <span class="p">()</span>
  <span class="o">...</span><span class="p">)</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">mypackage-compute-children</span> <span class="p">(</span><span class="nv">node</span><span class="p">)</span>
  <span class="o">...</span><span class="p">)</span>

<span class="p">(</span><span class="nb">provide</span> <span class="ss">'mypackage</span><span class="p">)</span>
</code></pre></div></div>

<p>While this has solved the problem for the time being, attaching the
package name to almost every identifier, including private function
and variable names, is quite cumbersome. Namespaces can <em>almost</em> be
hacked into the language by using multiple obarrays,
<a href="/blog/2011/08/18/">but symbols have internal linked lists</a> that prohibit
inclusion in multiple obarrays.</p>

<p>By convention, private names are given a double-dash after the
namespace. If a “bar counter” is an implementation detail that may
disappear in the future, it will be called <code class="language-plaintext highlighter-rouge">mypackage--bar-counter</code> to
warn users and other package authors not to rely on it.</p>

<p>There’s been a recent push to follow this namespace-prefix policy more
strictly, particularly with the depreciation of <code class="language-plaintext highlighter-rouge">cl</code> and introduction
of <code class="language-plaintext highlighter-rouge">cl-lib</code>. I suspect someday when namespaces are finally introduced,
packages with strictly clean namespaces with be at an advantage,
somehow automatically supported. <a href="http://nic.ferrier.me.uk/blog/2013_06/adding-namespaces-to-elisp">Nic Ferrier has proposed ideas</a>
for how to move forward on this.</p>

<h3 id="how-strict-are-we-talking">How strict are we talking?</h3>

<p>Over the last few years I’ve gotten much stricter in my own packages
when it comes to namespace prefixes. You can see the progression going
from <a href="https://github.com/skeeto/javadoc-lookup">javadoc-lookup</a> (2010) where I was completely sloppy about
it, to <a href="https://github.com/skeeto/emacsql">EmacSQL</a> (2014) where every single global identifier
is meticulously prefixed.</p>

<p>For a time I considered names such as <code class="language-plaintext highlighter-rouge">make-*</code> and <code class="language-plaintext highlighter-rouge">with-*</code> to be
exceptions to the rule, since these names are idioms inherited from
Common Lisp. The namespace comes <em>after</em> the expected prefix. I’ve
changed my mind about this, which has caused me to change my usage of
<code class="language-plaintext highlighter-rouge">defstruct</code> (now <code class="language-plaintext highlighter-rouge">cl-defstruct</code>).</p>

<p>Just as in Common Lisp, by default <code class="language-plaintext highlighter-rouge">cl-defstruct</code> defines a
constructor starting with <code class="language-plaintext highlighter-rouge">make-*</code>. This is fine in Common Lisp, where
it’s a package-private function by default, but in Emacs Lisp this
pollutes the global namespace.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">require</span> <span class="ss">'cl-lib</span><span class="p">)</span>

<span class="c1">;; Defines make-circle, circle-x, circle-y, circle-radius, circle-p</span>
<span class="p">(</span><span class="nv">cl-defstruct</span> <span class="nv">circle</span>
  <span class="nv">x</span> <span class="nv">y</span> <span class="nv">radius</span><span class="p">)</span>

<span class="p">(</span><span class="nb">defvar</span> <span class="nv">unit-circle</span> <span class="p">(</span><span class="nv">make-circle</span> <span class="ss">:x</span> <span class="mf">0.0</span> <span class="ss">:y</span> <span class="mf">0.0</span> <span class="ss">:radius</span> <span class="mf">1.0</span><span class="p">))</span>

<span class="nv">unit-circle</span>
<span class="c1">;; =&gt; [cl-struct-circle 0.0 0.0 1.0]</span>

<span class="p">(</span><span class="nv">circle-radius</span> <span class="nv">unit-circle</span><span class="p">)</span>
<span class="c1">;; =&gt; 1.0</span>
</code></pre></div></div>

<p>This constructor isn’t namespace clean, so package authors should
avoid defstruct’s default. If the package is named <code class="language-plaintext highlighter-rouge">circle</code> then all
of the accessors are perfectly fine, though.</p>

<p>To fix this, I now use another, more recent Emacs Lisp idiom: name the
constructor <code class="language-plaintext highlighter-rouge">create</code>. That is, for the package <code class="language-plaintext highlighter-rouge">circle</code>, we desire
<code class="language-plaintext highlighter-rouge">circle-create</code>. To get this behavior from <code class="language-plaintext highlighter-rouge">cl-defstruct</code>, use the
<code class="language-plaintext highlighter-rouge">:constructor</code> option.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">;; Clean!</span>
<span class="p">(</span><span class="nv">cl-defstruct</span> <span class="p">(</span><span class="nv">circle</span> <span class="p">(</span><span class="ss">:constructor</span> <span class="nv">circle-create</span><span class="p">))</span>
  <span class="nv">x</span> <span class="nv">y</span> <span class="nv">radius</span><span class="p">)</span>

<span class="p">(</span><span class="nv">circle-create</span> <span class="ss">:x</span> <span class="mi">0</span> <span class="ss">:y</span> <span class="mi">0</span> <span class="ss">:radius</span> <span class="mi">1</span><span class="p">)</span>
<span class="c1">;; =&gt; [cl-struct-circle 0 0 1]</span>

<span class="p">(</span><span class="nb">provide</span> <span class="ss">'circle</span><span class="p">)</span>
</code></pre></div></div>

<p>This affords a new opportunity to craft a better constructor. Have
<code class="language-plaintext highlighter-rouge">cl-defstruct</code> define a private constructor, then manually write a
constructor with a nicer interface. It may also do additional work,
like enforce invariants or initialize dependent slots.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">cl-defstruct</span> <span class="p">(</span><span class="nv">circle</span> <span class="p">(</span><span class="ss">:constructor</span> <span class="nv">circle--create</span><span class="p">))</span>
  <span class="nv">x</span> <span class="nv">y</span> <span class="nv">radius</span><span class="p">)</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">circle-create</span> <span class="p">(</span><span class="nv">x</span> <span class="nv">y</span> <span class="nv">radius</span><span class="p">)</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">circle</span> <span class="p">(</span><span class="nv">circle--create</span> <span class="ss">:x</span> <span class="nv">x</span> <span class="ss">:y</span> <span class="nv">y</span> <span class="ss">:radius</span> <span class="nv">radius</span><span class="p">)))</span>
    <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">&lt;</span> <span class="nv">radius</span> <span class="mi">0</span><span class="p">)</span>
        <span class="p">(</span><span class="nb">error</span> <span class="s">"must have non-negative radius"</span><span class="p">)</span>
      <span class="nv">circle</span><span class="p">)))</span>

<span class="p">(</span><span class="nv">circle-create</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span><span class="p">)</span>
<span class="c1">;; =&gt; [cl-struct-circle 0 0 1]</span>

<span class="p">(</span><span class="nv">circle-create</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">-1</span><span class="p">)</span>
<span class="c1">;; error: "must have non-negative radius"</span>
</code></pre></div></div>

<p>This is now how I always use <code class="language-plaintext highlighter-rouge">cl-defstruct</code> in Emacs Lisp. It’s a tidy
convention that will probably become more common in the future.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Emacs Byte-code Internals</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2014/01/04/"/>
    <id>urn:uuid:c03869b5-fca0-3f9e-8dda-c3f361b287a8</id>
    <updated>2014-01-04T05:07:26Z</updated>
    <category term="emacs"/><category term="lisp"/><category term="elisp"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>Byte-code compilation is an underdocumented — and in the case of the
recent lexical binding updates, undocumented — part of Emacs. Most
users know that Elisp is usually compiled into a byte-code saved to
<code class="language-plaintext highlighter-rouge">.elc</code> files, and that byte-code loads and runs faster than uncompiled
Elisp. That’s all users really need to know, and the <em>GNU Emacs Lisp
Reference Manual</em> specifically discourages poking around too much.</p>

<blockquote>
  <p><strong>People do not write byte-code;</strong> that job is left to the byte
compiler. But we provide a disassembler to satisfy a cat-like
curiosity.</p>
</blockquote>

<p>Screw that! What if I want to handcraft some byte-code myself? :-) The
purpose of this article is to introduce the internals of Elisp
byte-code interpreter. I will explain how it works, why lexically
scoped code is faster, and demonstrate writing some byte-code by hand.</p>

<h3 id="the-humble-stack-machine">The Humble Stack Machine</h3>

<p>The byte-code interpreter is a simple stack machine. The stack holds
arbitrary lisp objects. The interpreter is backwards compatible but
not forwards compatible (old versions can’t run new byte-code). Each
instruction is between 1 and 3 bytes. The first byte is the opcode and
the second and third bytes are either a single operand or a single
intermediate value. Some operands are packed into the opcode byte.</p>

<p>As of this writing (Emacs 24.3) there are 142 opcodes, 6 of which have
been declared obsolete. Most opcodes refer to commonly used built-in
functions for fast access. (Looking at the selection, Elisp really is
geared towards text!) Considering packed operands, there are up to 27
potential opcodes unused, reserved for the future.</p>

<ul>
  <li>opcodes 48 - 55</li>
  <li>opcode 97</li>
  <li>opcode 128</li>
  <li>opcodes 169 - 174</li>
  <li>opcodes 180 - 181</li>
  <li>opcodes 183 - 191</li>
</ul>

<p>The easiest place to access the opcode listing is in
<a href="http://cvs.savannah.gnu.org/viewvc/emacs/emacs/lisp/emacs-lisp/bytecomp.el?view=markup">bytecomp.el</a>. Beware that some of the opcode comments are
currently out of date.</p>

<h3 id="segmentation-fault-warning">Segmentation Fault Warning</h3>

<p>Byte-code does not offer the same safety as normal Elisp. <strong>Bad
byte-code can, and will, cause Emacs to crash.</strong> You can try out for
yourself right now,</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>emacs -batch -Q --eval '(print (#[0 "\300\207" [] 0]))'
</code></pre></div></div>

<p>Or evaluate the code manually in a buffer (save everything first!),</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="err">#</span><span class="nv">[0</span> <span class="s">"\300\207"</span> <span class="nv">[]</span> <span class="nv">0]</span><span class="p">)</span>
</code></pre></div></div>

<p>This segfault, caused by referencing beyond the end of the constants
vector, is <em>not</em> an Emacs bug. Doing a boundary test would slow down
the byte-code interpreter. Not performing this test at run-time is a
practical engineering decision. The Emacs developers have instead
chosen to rely on valid byte-code output from the compiler, making a
disclaimer to anyone wanting to write their own byte-code,</p>

<blockquote>
  <p>You should not try to come up with the elements for a byte-code
function yourself, because if they are inconsistent, Emacs may crash
when you call the function. Always leave it to the byte compiler to
create these objects; it makes the elements consistent (we hope).</p>
</blockquote>

<p>You’ve been warned. Now it’s time to start playing with firecrackers.</p>

<h3 id="the-byte-code-object">The Byte-code Object</h3>

<p>A byte-code object is functionally equivalent to a normal Elisp vector
<em>except</em> that it can be evaluated as a function. Elements are accessed
in constant time, the syntax is similar to vector syntax (<code class="language-plaintext highlighter-rouge">[...]</code> vs.
<code class="language-plaintext highlighter-rouge">#[...]</code>), and it can be of any length, though valid functions must
have at least 4 elements.</p>

<p>There are two ways to create a byte-code object: using a byte-code
object literal or with <code class="language-plaintext highlighter-rouge">make-byte-code</code>.
<a href="/blog/2012/07/17/">Like vector literals</a>, byte-code literals don’t need to be
quoted.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">make-byte-code</span> <span class="mi">0</span> <span class="s">""</span> <span class="nv">[]</span> <span class="mi">0</span><span class="p">)</span>
<span class="c1">;; =&gt; #[0 "" [] 0]</span>

<span class="err">#</span><span class="nv">[1</span> <span class="mi">2</span> <span class="mi">3</span> <span class="nv">4]</span>
<span class="c1">;; =&gt; #[1 2 3 4]</span>

<span class="p">(</span><span class="err">#</span><span class="nv">[0</span> <span class="s">""</span> <span class="nv">[]</span> <span class="nv">0]</span><span class="p">)</span>
<span class="c1">;; error: Invalid byte opcode</span>
</code></pre></div></div>

<p>The elements of an object literal are:</p>

<ul>
  <li>Function parameter (lambda) list</li>
  <li>Unibyte string of byte-code</li>
  <li>Constants vector</li>
  <li>Maximum stack usage</li>
  <li>Docstring (optional, nil for none)</li>
  <li>Interactive specification (optional)</li>
</ul>

<h4 id="parameter-list">Parameter List</h4>

<p>The parameter list takes on two different forms depending on if the
function is lexically or dynamically scoped. If the function is
dynamically scoped, the argument list is exactly what appears in lisp
code.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">byte-compile</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">a</span> <span class="nv">b</span> <span class="k">&amp;optional</span> <span class="nv">c</span><span class="p">)))</span>
<span class="c1">;; =&gt; #[(a b &amp;optional c) "\300\207" [nil] 1]</span>
</code></pre></div></div>

<p>There’s really no shorter way to represent the parameter list because
preserving the argument names is critical. Remember that, in dynamic
scope, while the function body is being evaluated these variables are
<em>globally</em> bound (eww!) to the function’s arguments.</p>

<p>When the function is lexically scoped, the parameter list is packed
into an Elisp integer, indicating the counts of the different kinds of
parameters: required, <code class="language-plaintext highlighter-rouge">&amp;optional</code>, and <code class="language-plaintext highlighter-rouge">&amp;rest</code>.</p>

<p><img src="/img/diagram/elisp-params.png" alt="" /></p>

<p>The least significant 7 bits indicate the number of required
arguments. Notice that this limits compiled, lexically-scoped
functions to 127 required arguments. The 8th bit is the number of
<code class="language-plaintext highlighter-rouge">&amp;rest</code> arguments (up to 1). The remaining bits indicate the total
number of optional and required arguments (not counting <code class="language-plaintext highlighter-rouge">&amp;rest</code>). It’s
really easy to parse these in your head when viewed as hexadecimal
because each portion almost always fits inside its own “digit.”</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">byte-compile-make-args-desc</span> <span class="o">'</span><span class="p">())</span>
<span class="c1">;; =&gt; #x000  (0 args, 0 rest, 0 required)</span>

<span class="p">(</span><span class="nv">byte-compile-make-args-desc</span> <span class="o">'</span><span class="p">(</span><span class="nv">a</span> <span class="nv">b</span><span class="p">))</span>
<span class="c1">;; =&gt; #x202  (2 args, 0 rest, 2 required)</span>

<span class="p">(</span><span class="nv">byte-compile-make-args-desc</span> <span class="o">'</span><span class="p">(</span><span class="nv">a</span> <span class="nv">b</span> <span class="k">&amp;optional</span> <span class="nv">c</span><span class="p">))</span>
<span class="c1">;; =&gt; #x302  (3 args, 0 rest, 2 required)</span>

<span class="p">(</span><span class="nv">byte-compile-make-args-desc</span> <span class="o">'</span><span class="p">(</span><span class="nv">a</span> <span class="nv">b</span> <span class="k">&amp;optional</span> <span class="nv">c</span> <span class="k">&amp;rest</span> <span class="nv">d</span><span class="p">))</span>
<span class="c1">;; =&gt; #x382  (3 args, 1 rest, 2 required)</span>
</code></pre></div></div>

<p>The names of the arguments don’t matter in lexical scope: they’re
purely positional. This tighter argument specification is one of the
reasons lexical scope is faster: the byte-code interpreter doesn’t
need to parse the entire lambda list and assign all of the variables
on each function invocation.</p>

<h4 id="unibyte-string-byte-code">Unibyte String Byte-code</h4>

<p>The second element is a unibyte string — it strictly holds octets and
is not to be interpreted as any sort of Unicode encoding. These
strings should be created with <code class="language-plaintext highlighter-rouge">unibyte-string</code> because <code class="language-plaintext highlighter-rouge">string</code> may
return a multibyte string. To disambiguate the string type to the lisp
reader when higher values are present (&gt; 127), the strings are printed
in an escaped octal notation, keeping the string literal inside the
ASCII character set.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">unibyte-string</span> <span class="mi">100</span> <span class="mi">200</span> <span class="mi">250</span><span class="p">)</span>
<span class="c1">;; =&gt; "d\310\372"</span>
</code></pre></div></div>

<p>It’s unusual to see a byte-code string that doesn’t end with 135
(#o207, byte-return). Perhaps this should have been implicit? I’ll
talk more about the byte-code below.</p>

<h4 id="constants-vector">Constants Vector</h4>

<p>The byte-code has very limited operands. Most operands are only a few
bits, some fill an entire byte, and occasionally two bytes. The meat
of the function that holds all the constants, function symbols, and
variables symbols is the constants vector. It’s a normal Elisp vector
and can be created with <code class="language-plaintext highlighter-rouge">vector</code> or a vector literal. Operands
reference either this vector or they index into the stack itself.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">byte-compile</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">a</span> <span class="nv">b</span><span class="p">)</span> <span class="p">(</span><span class="nv">my-func</span> <span class="nv">b</span> <span class="nv">a</span><span class="p">)))</span>
<span class="c1">;; =&gt; #[(a b) "\302\134\011\042\207" [b a my-func] 3]</span>
</code></pre></div></div>

<p>Note that the constants vector lists the variable symbols as well as
the external function symbol. If this was a lexically scoped function
the constants vector wouldn’t have the variables listed, being only
<code class="language-plaintext highlighter-rouge">[my-func]</code>.</p>

<h4 id="maximum-stack-usage">Maximum Stack Usage</h4>

<p>This is the maximum stack space used by this byte-code. This value can
be derived from the byte-code itself, but it’s pre-computed so that
the byte-code interpreter can quickly check for stack overflow.
Under-reporting this value is probably another way to crash Emacs.</p>

<h4 id="docstring">Docstring</h4>

<p>The simplest component and completely optional. It’s either the
docstring itself, or if the docstring is especially large it’s a cons
cell indicating a compiled <code class="language-plaintext highlighter-rouge">.elc</code> and a position for lazy access. Only
one position, the start, is needed because the lisp reader is used to
load it and it knows how to recognize the end.</p>

<h4 id="interactive-specification">Interactive Specification</h4>

<p>If this element is present and non-nil then the function is an
interactive function. It holds the exactly contents of <code class="language-plaintext highlighter-rouge">interactive</code>
in the uncompiled function definition.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">byte-compile</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span> <span class="p">(</span><span class="nv">interactive</span> <span class="s">"nNumber: "</span><span class="p">)</span> <span class="nv">n</span><span class="p">))</span>
<span class="c1">;; =&gt; #[(n) "\010\207" [n] 1 nil "nNumber: "]</span>

<span class="p">(</span><span class="nv">byte-compile</span> <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span> <span class="p">(</span><span class="nv">interactive</span> <span class="p">(</span><span class="nb">list</span> <span class="p">(</span><span class="nb">read</span><span class="p">)))</span> <span class="nv">n</span><span class="p">))</span>
<span class="c1">;; =&gt; #[(n) "\010\207" [n] 1 nil (list (read))]</span>
</code></pre></div></div>

<p>The interactive expression is always interpreted, never byte-compiled.
This is usually fine because, by definition, this code is going to be
waiting on user input. However, it slows down keyboard macro playback.</p>

<h3 id="opcodes">Opcodes</h3>

<p>The bulk of the established opcode bytes is for variable, stack, and
constant access opcodes, most of which use packed operands.</p>

<ul>
  <li>0 - 7   : (<code class="language-plaintext highlighter-rouge">stack-ref</code>) stack reference</li>
  <li>8 - 15  : (<code class="language-plaintext highlighter-rouge">varref</code>) variable reference (from constants vector)</li>
  <li>16 - 23 : (<code class="language-plaintext highlighter-rouge">varset</code>) variable set (from constants vector)</li>
  <li>24 - 31 : (<code class="language-plaintext highlighter-rouge">varbind</code>) variable binding (from constants vector)</li>
  <li>32 - 39 : (<code class="language-plaintext highlighter-rouge">call</code>) function call (immediate = number of arguments)</li>
  <li>40 - 47 : (<code class="language-plaintext highlighter-rouge">unbind</code>) variable unbinding (from constants vector)</li>
  <li>129, 192-255 : (<code class="language-plaintext highlighter-rouge">constant</code>) direct constants vector access</li>
</ul>

<p>Except for the last item, each kind of instruction comes in sets of 8.
The nth such instruction means access the nth thing. For example, the
instruction “<code class="language-plaintext highlighter-rouge">2</code>” copies the third stack item to the top of the stack.
An instruction of “<code class="language-plaintext highlighter-rouge">9</code>” pushes onto the stack the value of the
variable named by the second element listed in the constants vector.</p>

<p>However, the 7th and 8th such instructions in each set take an operand
byte or two. The 7th instruction takes a 1-byte operand and the 8th
takes a 2-byte operand. A 2-byte operand is written in little-endian
byte-order regardless of the host platform.</p>

<p>For example, let’s manually craft an instruction that returns the
value of the global variable <code class="language-plaintext highlighter-rouge">foo</code>. Each opcode has a named constant
of <code class="language-plaintext highlighter-rouge">byte-X</code> so we don’t have to worry about their actual byte-code
number.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">require</span> <span class="ss">'bytecomp</span><span class="p">)</span>  <span class="c1">; named opcodes</span>

<span class="p">(</span><span class="nb">defvar</span> <span class="nv">foo</span> <span class="s">"hello"</span><span class="p">)</span>

<span class="p">(</span><span class="nv">defalias</span> <span class="ss">'get-foo</span>
  <span class="p">(</span><span class="nv">make-byte-code</span>
    <span class="m">#x000</span>                 <span class="c1">; no arguments</span>
    <span class="p">(</span><span class="nv">unibyte-string</span>
      <span class="p">(</span><span class="nb">+</span> <span class="mi">0</span> <span class="nv">byte-varref</span><span class="p">)</span>   <span class="c1">; ref variable under first constant</span>
      <span class="nv">byte-return</span><span class="p">)</span>        <span class="c1">; pop and return</span>
    <span class="nv">[foo]</span>                 <span class="c1">; constants</span>
    <span class="mi">1</span><span class="p">))</span>                   <span class="c1">; only using 1 stack space</span>

<span class="p">(</span><span class="nv">get-foo</span><span class="p">)</span>
<span class="c1">;; =&gt; "hello"</span>
</code></pre></div></div>

<p>Ta-da! That’s a handcrafted byte-code function. I left a “+ 0” in
there so that I can change the offset. This function has the exact
same behavior, it’s just less optimal,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">defalias</span> <span class="ss">'get-foo</span>
  <span class="p">(</span><span class="nv">make-byte-code</span>
    <span class="m">#x000</span>
    <span class="p">(</span><span class="nv">unibyte-string</span>
      <span class="p">(</span><span class="nb">+</span> <span class="mi">3</span> <span class="nv">byte-varref</span><span class="p">)</span>     <span class="c1">; 4th form of varref</span>
      <span class="nv">byte-return</span><span class="p">)</span>
    <span class="nv">[nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="nv">foo]</span>
    <span class="mi">1</span><span class="p">))</span>
</code></pre></div></div>

<p>If <code class="language-plaintext highlighter-rouge">foo</code> was the 10th constant, we would need to use the 1-byte
operand version. Again, the same behavior, just less optimal.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">defalias</span> <span class="ss">'get-foo</span>
  <span class="p">(</span><span class="nv">make-byte-code</span>
    <span class="m">#x000</span>
    <span class="p">(</span><span class="nv">unibyte-string</span>
      <span class="p">(</span><span class="nb">+</span> <span class="mi">6</span> <span class="nv">byte-varref</span><span class="p">)</span>     <span class="c1">; 7th form of varref</span>
      <span class="mi">9</span>                     <span class="c1">; operand, (constant index 9)</span>
      <span class="nv">byte-return</span><span class="p">)</span>
    <span class="nv">[nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="nv">foo]</span>
    <span class="mi">1</span><span class="p">))</span>
</code></pre></div></div>

<p>Dynamically-scoped code makes heavy use of <code class="language-plaintext highlighter-rouge">varref</code> but
lexically-scoped code rarely uses it (global variables only), instead
relying heavily on <code class="language-plaintext highlighter-rouge">stack-ref</code>, which is faster. This is where the
different calling conventions come into play.</p>

<h3 id="calling-convention">Calling Convention</h3>

<p>Each kind of scope gets its own calling convention. Here we finally
get to glimpse some of the really great work by Stefan Monnier
updating the compiler for lexical scope.</p>

<h4 id="dynamic-scope-calling-convention">Dynamic Scope Calling Convention</h4>

<p>Remembering back to the parameter list element of the byte-code
object, dynamically scoped functions keep track of all its argument
names. Before executing a function the interpreter examines the lambda
list and binds (<code class="language-plaintext highlighter-rouge">varbind</code>) every variable globally to an argument.</p>

<p>If the caller was byte-compiled, each argument started on the stack,
was popped and bound to a variable, and, to be accessed by the
function, will be pushed back right onto the stack (<code class="language-plaintext highlighter-rouge">varref</code>). There’s
a lot of argument indirection for each function call.</p>

<h4 id="lexical-scope-calling-convention">Lexical Scope Calling Convention</h4>

<p>With lexical scope, the argument names are not actually bound for the
evaluation byte-code. The names are completely gone because the
compiler has converted local variables into stack offsets.</p>

<p>When calling a lexically-scoped function, the byte-code interpreter
examines the integer parameter descriptor. It checks to make sure the
appropriate number of arguments have been provided, and for each
unprovided <code class="language-plaintext highlighter-rouge">&amp;optional</code> argument it pushes a nil onto the stack. If the
function has a <code class="language-plaintext highlighter-rouge">&amp;rest</code> parameter, any extra arguments are popped off
into a list and that list is pushed onto the stack.</p>

<p>From here the function can access its arguments directly on the stack
without any named variable misdirection. It can even consume them
directly.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">;; -*- lexical-binding: t -*-</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span> <span class="nv">x</span><span class="p">)</span>

<span class="p">(</span><span class="nb">symbol-function</span> <span class="nf">#'</span><span class="nv">foo</span><span class="p">)</span>
<span class="c1">;; =&gt; #[#x101 "\207" [] 2]</span>
</code></pre></div></div>

<p>The byte-code for <code class="language-plaintext highlighter-rouge">foo</code> is a single instruction: <code class="language-plaintext highlighter-rouge">return</code>. The
function’s argument is already on the stack so it doesn’t have to do
anything. Strangely the maximum stack usage element is wrong here (2),
but it won’t cause a crash.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">;; (As of this writing `byte-compile' always uses dynamic scope.)</span>

<span class="p">(</span><span class="nv">byte-compile</span> <span class="ss">'foo</span><span class="p">)</span>
<span class="c1">;; =&gt; #[(x) "\010\207" [x] 1]</span>
</code></pre></div></div>

<p>It takes longer to set up (x is implicitly bound), it has to make an
explicit variable dereference (<code class="language-plaintext highlighter-rouge">varref</code>), then it has to clean up by
unbinding x (implicit <code class="language-plaintext highlighter-rouge">unbind</code>). It’s no wonder lexical scope is
faster!</p>

<p>Note that there’s also a <code class="language-plaintext highlighter-rouge">disassemble</code> function for examining
byte-code, but it only reveals part of the story.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">disassemble</span> <span class="nf">#'</span><span class="nv">foo</span><span class="p">)</span>
<span class="c1">;; byte code:</span>
<span class="c1">;;   args: (x)</span>
<span class="c1">;; 0       varref    x</span>
<span class="c1">;; 1       return</span>
</code></pre></div></div>

<h3 id="compiler-intermediate-lapcode">Compiler Intermediate “lapcode”</h3>

<p>The Elisp byte-compiler has an intermediate language called lapcode
(“Lisp Assembly Program”), which is much easier to optimize than
byte-code. It’s basically an assembly language built out of
s-expressions. Opcodes are referenced by name and operands, including
packed operands, are handled whole. Each instruction is a cons cell,
<code class="language-plaintext highlighter-rouge">(opcode . operand)</code>, and a program is a list of these.</p>

<p>Let’s rewrite our last <code class="language-plaintext highlighter-rouge">get-foo</code> using lapcode.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">defalias</span> <span class="ss">'get-foo</span>
  <span class="p">(</span><span class="nv">make-byte-code</span>
    <span class="m">#x000</span>
    <span class="p">(</span><span class="nv">byte-compile-lapcode</span>
      <span class="o">'</span><span class="p">((</span><span class="nv">byte-varref</span> <span class="o">.</span> <span class="mi">9</span><span class="p">)</span>
        <span class="p">(</span><span class="nv">byte-return</span><span class="p">)))</span>
    <span class="nv">[nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="no">nil</span> <span class="nv">foo]</span>
    <span class="mi">1</span><span class="p">))</span>
</code></pre></div></div>

<p>We didn’t have to worry about which form of <code class="language-plaintext highlighter-rouge">varref</code> we were using or
even how to encode a 2-byte operand. The lapcode “assembler” took care
of that detail.</p>

<h3 id="project-ideas">Project Ideas?</h3>

<p>The Emacs byte-code compiler and interpreter are fascinating. Having
spent time studying them I’m really tempted to build a project on top
of it all. Perhaps implementing a programming language that targets
the byte-code interpreter, improving compiler optimization, or, for a
really big project, JIT compiling Emacs byte-code.</p>

<p><strong>People <em>can</em> write byte-code!</strong></p>

]]>
    </content>
  </entry>
    
  
    
  <entry>
    <title>Emacs Lisp Readable Closures</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2013/12/30/"/>
    <id>urn:uuid:84f86fb6-e029-3a57-1450-1d25be3fdee0</id>
    <updated>2013-12-30T23:52:38Z</updated>
    <category term="emacs"/><category term="elisp"/><category term="lisp"/><category term="javascript"/>
    <content type="html">
      <![CDATA[<p>I’ve stated before that one of the unique features of Emacs Lisp is
that its closures are <em>readable</em>. Closures can be serialized by the
printer and read back in with the reader. I am unaware of any other
programming language that has this feature. In fact it’s essential for
Elisp byte-code compilation because byte-compiled Elisp files are
merely s-expressions of byte-code dumped out as source.</p>

<h3 id="lisp-printing">Lisp Printing</h3>

<p>The Lisp family of languages are <em>homoiconic</em>. Lisp source code is
written in the syntax of its own data structures, s-expressions. Since
a compiler/interpreter is usually provided at run-time, a consequence
of this is that reading and printing are a fundamental feature of
Lisps. A value can be handed to the printer, which will serialize the
value into an s-expression as a sequence of characters. Later on the
reader can parse the s-expression back into an <code class="language-plaintext highlighter-rouge">equal</code> value.</p>

<p>To compare, JavaScript originally had half of this in place.
JavaScript has convenient object syntax for defining an associative
array, known today as JSON. The <code class="language-plaintext highlighter-rouge">eval</code> function could (dangerously) be
used as a reader for parsing a string containing JSON-encoded data
into a value. But until <code class="language-plaintext highlighter-rouge">JSON.stringify()</code> became standard, developers
had to write their own printer. Lisp s-expression syntax is much more
powerful (and complicated) than JSON, maintaining
<a href="/blog/2013/03/28/">both identity and cycles</a> (e.g. <code class="language-plaintext highlighter-rouge">*print-circle*</code>).</p>

<p>Not all values can be read. They’ll still print (when <code class="language-plaintext highlighter-rouge">*print-readably*</code>
is nil) but will do so using special syntax that will signal an error
in the reader: <code class="language-plaintext highlighter-rouge">#&lt;</code>. For example, in Emacs Lisp buffers cannot be
serialized so they print using this syntax.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">prin1-to-string</span> <span class="p">(</span><span class="nv">current-buffer</span><span class="p">))</span>
<span class="c1">;; =&gt; "#&lt;buffer *scratch*&gt;"</span>
</code></pre></div></div>

<p>It doesn’t matter what’s between the angle brackets, or even that
there’s a closing angle bracket. The reader will signal an error as
soon as it hits a <code class="language-plaintext highlighter-rouge">#&lt;</code>.</p>

<h4 id="almost-everything-prints-readably">Almost Everything Prints Readably</h4>

<p>Elisp has a small set of primitive data types. All of these primitive
types print readably:</p>

<ul>
  <li>integer (<code class="language-plaintext highlighter-rouge">1024</code>, <code class="language-plaintext highlighter-rouge">?a</code>)</li>
  <li>float (<code class="language-plaintext highlighter-rouge">1.7</code>)</li>
  <li>cons/list (<code class="language-plaintext highlighter-rouge">(...)</code>)</li>
  <li>vector (one-dimensional, <code class="language-plaintext highlighter-rouge">[...]</code>)</li>
  <li>bool-vector (<code class="language-plaintext highlighter-rouge">#&amp;n"..."</code>)</li>
  <li>string (<code class="language-plaintext highlighter-rouge">"..."</code>)</li>
  <li>char-table (<code class="language-plaintext highlighter-rouge">#^[...]</code>)</li>
  <li>hash-table (readable as of Emacs 23.3, <code class="language-plaintext highlighter-rouge">#s(hash-table ...)</code>)</li>
  <li>byte-code function object (<code class="language-plaintext highlighter-rouge">#[...]</code>)</li>
  <li>symbol</li>
</ul>

<p>Here are all the non-readable types. Each one has a good reason for
not being serializable.</p>

<ul>
  <li>buffer</li>
  <li>process (external state)</li>
  <li>frame (user interface element)</li>
  <li>marker (live, automatically updates)</li>
  <li>overlay (belongs to a buffer)</li>
  <li>built-in functions (native code)</li>
  <li>user-ptr (opaque pointers from Emacs 25 dynamic modules)</li>
</ul>

<p>And that’s it. Every other value in Elisp is constructed from one or
more of these primitives, including keymaps, functions, macros, syntax
tables, <code class="language-plaintext highlighter-rouge">defstruct</code> structs, and EIEIO objects. This means that as
long as these values don’t refer to an unreadable value, they
themselves can be printed.</p>

<p>An interesting note here is that, unlike the Common Lisp Object System
(CLOS), EIEIO objects are readable by default. To Elisp they’re just
vectors, so of course they print. CLOS objects are unreadable without
manually defining a print method per class.</p>

<h3 id="elisp-closures">Elisp Closures</h3>

<p>Elisp got lexical scoping in Emacs 24, released in June 2012. It’s now
one of the relatively few languages to have both dynamic and lexical
scope. Like Common Lisp, variables declared with <code class="language-plaintext highlighter-rouge">defvar</code> (and family)
continue to have dynamic scope. For backwards compatibility with old
Lisp code, lexical scope is disabled by default. It’s enabled for a
specific file or buffer by setting <code class="language-plaintext highlighter-rouge">lexical-binding</code> to non-nil.</p>

<p>With lexical scope, anonymous functions become closures, a powerful
functional programming primitive: a function plus a captured lexical
environment. It also provides some performance benefits. In my own
tests, compiled Elisp with lexical scope enabled is about 10% to 15%
faster than with the default dynamic scope.</p>

<p>What do closures look like in Emacs Lisp? It takes on two forms
depending on whether the closure is compiled or not. For example,
consider this function, <code class="language-plaintext highlighter-rouge">foo</code>, that takes two arguments and returns a
closure that returns the first argument.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">;; -*- lexical-binding: t; -*-</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="nv">x</span> <span class="nv">y</span><span class="p">)</span>
  <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="nv">x</span><span class="p">))</span>

<span class="p">(</span><span class="nv">foo</span> <span class="ss">:bar</span> <span class="ss">:ignored</span><span class="p">)</span>
<span class="c1">;; =&gt; (closure ((y . :ignored) (x . :bar) t) () x)</span>
</code></pre></div></div>

<p>An uncompiled closure is a list beginning with the symbol <code class="language-plaintext highlighter-rouge">closure</code>.
The second element is the lexical environment, the third is the
argument list (lambda list), and the rest is the body of the function.
Here we can see that both <code class="language-plaintext highlighter-rouge">x</code> and <code class="language-plaintext highlighter-rouge">y</code> have been “closed over.” This is
a little bit sloppy because the function never makes use of <code class="language-plaintext highlighter-rouge">y</code>.
Capturing it has a few problems.</p>

<ul>
  <li>The closure has a larger footprint than necessary.</li>
  <li>Values are held longer than necessary, delaying collection.</li>
  <li>It affects the readability of the closure, which I’ll get to later.</li>
</ul>

<p>Fortunately the compiler is smart enough to see this and will avoid
capturing unused variables. To prove this, I’ve now compiled <code class="language-plaintext highlighter-rouge">foo</code> so
that it returns a compiled closure.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">foo</span> <span class="ss">:bar</span> <span class="ss">:ignored</span><span class="p">)</span>
<span class="c1">;; =&gt; #[0 "\300\207" [:bar] 1]</span>
</code></pre></div></div>

<p>What’s returned here is a byte-code function object, with the <code class="language-plaintext highlighter-rouge">#[...]</code>
syntax. It has these elements:</p>

<ol>
  <li>The function’s lambda list (zero arguments)</li>
  <li>Byte-codes stored in a unibyte string</li>
  <li>Constants vector</li>
  <li>Maximum stack space needed by this function</li>
</ol>

<p>Notice that the lexical environment has been captured in the constants
vector, specifically noting the lack of <code class="language-plaintext highlighter-rouge">:ignored</code> in this vector. The
compiler didn’t capture it.</p>

<p>For those curious about the byte-code here’s an explanation. The
string syntax shown is in octal, representing a string containing two
bytes: 192 and 135. The
<a href="/blog/2014/01/04/">Elisp byte-code interpreter is stack-based</a>. The 192
(<code class="language-plaintext highlighter-rouge">constant 0</code>) says to push the first constant onto the stack. The 135
(<code class="language-plaintext highlighter-rouge">return</code>) says to pop the top element from the stack and return it.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">coerce</span> <span class="s">"\300\207"</span> <span class="ss">'list</span><span class="p">)</span>
<span class="c1">;; =&gt; (192 135)</span>
</code></pre></div></div>

<h3 id="the-readable-closures-catch">The Readable Closures Catch</h3>

<p>Since closures are byte-code function objects, they print readably.
You can capture an environment in a closure, serialize it, read it
back in, and evaluate it. That’s pretty cool! This means closures can
be transmitted to other Emacs instances in a multi-processing setup
(i.e. <a href="https://github.com/nicferrier/elnode">Elnode</a>, <a href="https://github.com/jwiegley/emacs-async">Async</a>)</p>

<p>The catch is that it’s easy to accidentally capture an unreadable
value, especially buffers. Consider this function <code class="language-plaintext highlighter-rouge">bar</code> which uses a
temporary buffer as an efficient string builder. It returns a closure
that returns the result. (Weird, but stick with me here!)</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">bar</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">with-temp-buffer</span>
    <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">standard-output</span> <span class="p">(</span><span class="nv">current-buffer</span><span class="p">)))</span>
      <span class="p">(</span><span class="nb">loop</span> <span class="nv">for</span> <span class="nv">i</span> <span class="nv">from</span> <span class="mi">0</span> <span class="nv">to</span> <span class="nv">n</span> <span class="nb">do</span> <span class="p">(</span><span class="nb">princ</span> <span class="nv">i</span><span class="p">))</span>
      <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nb">string</span> <span class="p">(</span><span class="nv">buffer-string</span><span class="p">)))</span>
        <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="nb">string</span><span class="p">)))))</span>
</code></pre></div></div>

<p>The compiled form looks fine,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">bar</span> <span class="mi">3</span><span class="p">)</span>
<span class="c1">;; =&gt; #[0 "\300\207" ["0123"] 1]</span>
</code></pre></div></div>

<p>But the interpreted form of the closure has a problem. The
<code class="language-plaintext highlighter-rouge">with-temp-buffer</code> macro silently introduced a new binding — an
abstraction leak.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">bar</span> <span class="mi">3</span><span class="p">)</span>
<span class="c1">;; =&gt; (closure ((string . "0123")</span>
<span class="c1">;;              (temp-buffer . #&lt;killed buffer&gt;)</span>
<span class="c1">;;              (n . 3) t)</span>
<span class="c1">;;      () string)</span>
</code></pre></div></div>

<p>The temporary buffer is mistakenly captured in the closure making it
unreadable, but <em>only</em> in its uncompiled form. This creates the
awkward situation where compiled and uncompiled code has <a href="/blog/2016/12/22/#accidental-closures">different
behavior</a>.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>An HTML5 Canvas Design Pattern</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2013/06/16/"/>
    <id>urn:uuid:841e3d35-6d30-380d-ffdc-96b37f991ce7</id>
    <updated>2013-06-16T00:00:00Z</updated>
    <category term="javascript"/><category term="java"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<p>I’ve been falling into a particular design pattern when using the
HTML5 Canvas element. By “design pattern” I don’t mean some pretty
arrangement but rather a <a href="http://en.wikipedia.org/wiki/Software_design_pattern">software design pattern</a>. This one’s a
very Lisp-like pattern, and I wonder if I would have come up with it
if I hadn’t first seen it in Lisp. It can also be applied to the Java
2D API, though less elegantly.</p>

<p>First, a review.</p>

<h3 id="drawing-basics">Drawing Basics</h3>

<p>A canvas is just another element in the page.</p>

<div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;canvas</span> <span class="na">id=</span><span class="s">"display"</span> <span class="na">width=</span><span class="s">"200"</span> <span class="na">height=</span><span class="s">"200"</span><span class="nt">&gt;&lt;/canvas&gt;</span>
</code></pre></div></div>

<p>To draw onto it, get a context and call drawing methods on it.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">ctx</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="dl">'</span><span class="s1">display</span><span class="dl">'</span><span class="p">).</span><span class="nx">getContext</span><span class="p">(</span><span class="dl">'</span><span class="s1">2d</span><span class="dl">'</span><span class="p">);</span>
<span class="nx">ctx</span><span class="p">.</span><span class="nx">fillStyle</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">blue</span><span class="dl">'</span><span class="p">;</span>
<span class="nx">ctx</span><span class="p">.</span><span class="nx">beginPath</span><span class="p">();</span>
<span class="nx">ctx</span><span class="p">.</span><span class="nx">arc</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">75</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">PI</span> <span class="o">*</span> <span class="mi">3</span> <span class="o">/</span> <span class="mi">2</span><span class="p">);</span>
<span class="nx">ctx</span><span class="p">.</span><span class="nx">fill</span><span class="p">();</span>
</code></pre></div></div>

<p>This will result in a canvas that looks like this,</p>

<p><img src="/img/screenshot/canvas-arc.png" alt="" /></p>

<p>Here’s how to do the same thing with Java 2D. Very similar, except the
canvas is called a JComponent and the context is called a Graphics2D.
As you could imagine from this example, Java 2D API is much richer,
and more object-oriented than the Canvas API. The cast from Graphics
to Graphics2D is required due to legacy.</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Example</span> <span class="kd">extends</span> <span class="nc">JComponent</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">paintComponent</span><span class="o">(</span><span class="nc">Graphics</span> <span class="n">graphics</span><span class="o">)</span> <span class="o">{</span>
        <span class="nc">Graphics2D</span> <span class="n">g</span> <span class="o">=</span> <span class="o">(</span><span class="nc">Graphics2D</span><span class="o">)</span> <span class="n">graphics</span><span class="o">;</span>
        <span class="n">g</span><span class="o">.</span><span class="na">setColor</span><span class="o">(</span><span class="nc">Color</span><span class="o">.</span><span class="na">BLUE</span><span class="o">);</span>
        <span class="n">g</span><span class="o">.</span><span class="na">fill</span><span class="o">(</span><span class="k">new</span> <span class="nc">Arc2D</span><span class="o">.</span><span class="na">Float</span><span class="o">(</span><span class="mi">25</span><span class="o">,</span> <span class="mi">25</span><span class="o">,</span> <span class="mi">150</span><span class="o">,</span> <span class="mi">150</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="mi">360</span><span class="o">,</span> <span class="nc">Arc2D</span><span class="o">.</span><span class="na">CHORD</span><span class="o">));</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>

<p>An important feature of both is the ability to globally apply
transforms — translate, scale, shear, and rotate — to all drawing
commands. For example, drawings on the canvas can be vertically scaled
using the <code class="language-plaintext highlighter-rouge">scale()</code> method. Graphics2D also has a <code class="language-plaintext highlighter-rouge">scale()</code> method.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// ...</span>
<span class="nx">ctx</span><span class="p">.</span><span class="nx">scale</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">);</span>
<span class="c1">// ...</span>
</code></pre></div></div>

<p><img src="/img/screenshot/canvas-arc-scaled.png" alt="" /></p>

<p>For both JavaScript and Java the rendered image isn’t being
<em>stretched</em>. Instead, the input vertices are being transformed before
rendering to pixels. This is what makes it possible to decouple the
<em>screen</em> coordinate system from the program’s internal coordinate
system. Outside of rare performance concerns, the program’s internal
logic shouldn’t be written in terms of pixels. It should rely on these
transforms to convert between coordinate systems at rendering time,
allowing for a moving camera.</p>

<h3 id="the-transform-stack">The Transform Stack</h3>

<p>Both cases also allow the current transform to be captured and
restored. Not only does this make it easier for a function to clean up
after itself and properly share the canvas with other functions, but
also multiple different coordinate transforms can be <em>stacked</em> on top
of each other. For example, the bottom transform might convert between
internal coordinates and screen coordinates. When it comes time to
draw a minimap, another transform can be pushed on top and the same
exact drawing methods applied to the canvas.</p>

<p>This is where Canvas and Java 2D start to differ. Both got some aspect
right and some aspect wrong, and I wish I could easily have the best
of both.</p>

<p>In canvas, this is literally a stack, and there are a pair of methods,
<code class="language-plaintext highlighter-rouge">save()</code> and <code class="language-plaintext highlighter-rouge">restore()</code> for pushing and popping the transform matrix
on an internal stack. The above JavaScript example may be in a
function that is called more than once, so it should restore the
transform matrix before returning.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">ctx</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span>
<span class="nx">ctx</span><span class="p">.</span><span class="nx">scale</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">);</span>
<span class="c1">// ... draw ...</span>
<span class="nx">ctx</span><span class="p">.</span><span class="nx">restore</span><span class="p">();</span>
</code></pre></div></div>

<p>In Java this stack is managed manually, and it (typically) sits inside
the call stack itself as a variable.</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">AffineTransform</span> <span class="n">tf</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="na">getTransform</span><span class="o">();</span>
<span class="n">g</span><span class="o">.</span><span class="na">scale</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="mf">0.5</span><span class="o">);</span>
<span class="c1">// ... draw ...</span>
<span class="n">g</span><span class="o">.</span><span class="na">setTransform</span><span class="o">(</span><span class="n">tf</span><span class="o">);</span>
</code></pre></div></div>

<p>I think Canvas’s built-in stack is more elegant than managing an
extraneous variable and object. However, what’s significant about Java
2D is that <strong>we actually have access to the transform matrix</strong>. It’s
that <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html">AffineTransform</a> object. The Canvas transform matrix is an
internal, inaccessible data structure. It has an established external
representation, <a href="http://www.w3.org/TR/SVGTiny12/svgudom.html#svg__SVGMatrix">SVGMatrix</a>, but it won’t provide a copy. If one
of these is needed, a separate matrix must to be maintained in
parallel. What a pain!</p>

<p>Why would we need the transform matrix? <strong>So that we can transform
coordinates in reverse!</strong> When a user interacts with the display, the
program receives <em>screen</em> coordinates. To be useful, these need to be
converted into internal coordinates so that the program can determine
where in the world the user clicked. The Java AffineTransform class
has a <code class="language-plaintext highlighter-rouge">createInverse()</code> method for computing this inverse transform.
This is something I really miss having when using Canvas. It’s such an
odd omission.</p>

<h3 id="the-design-pattern">The Design Pattern</h3>

<p>So, back to the design pattern. When it comes time draw something, a
transform is established on the context, something is drawn to the
context, then finally the transform is removed. The word “finally”
should stand out here. If we’re being careful, we should put the
teardown step inside a <code class="language-plaintext highlighter-rouge">finally</code> block. If something goes wrong, the
context will be left in a clean state. This has personally helped me
in debugging.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">ctx</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span>
<span class="nx">ctx</span><span class="p">.</span><span class="nx">scale</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">);</span>
<span class="k">try</span> <span class="p">{</span>
    <span class="c1">// ... draw ...</span>
<span class="p">}</span> <span class="k">finally</span> <span class="p">{</span>
    <span class="nx">ctx</span><span class="p">.</span><span class="nx">restore</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div></div>

<p>In Lisp, this pattern is typically captured as a <code class="language-plaintext highlighter-rouge">with-</code> macro.</p>

<ol>
  <li>Perform setup</li>
  <li>Run body</li>
  <li>Teardown</li>
  <li>Return the body’s return value</li>
</ol>

<p>Instead of <code class="language-plaintext highlighter-rouge">finally</code>, the special form <code class="language-plaintext highlighter-rouge">unwind-protect</code> is used to
clean up regardless of any error condition. Here’s a simplified
version of Emacs’ <code class="language-plaintext highlighter-rouge">with-temp-buffer</code> macro, which itself is built on
another <code class="language-plaintext highlighter-rouge">with-</code> macro, <code class="language-plaintext highlighter-rouge">with-current-buffer</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defmacro</span> <span class="nv">with-temp-buffer</span> <span class="p">(</span><span class="k">&amp;rest</span> <span class="nv">body</span><span class="p">)</span>
  <span class="o">`</span><span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">temp-buffer</span> <span class="p">(</span><span class="nv">generate-new-buffer</span> <span class="s">" *temp*"</span><span class="p">)))</span>
     <span class="p">(</span><span class="nv">with-current-buffer</span> <span class="nv">temp-buffer</span>
       <span class="p">(</span><span class="k">unwind-protect</span>
           <span class="p">(</span><span class="k">progn</span> <span class="o">,@</span><span class="nv">body</span><span class="p">)</span>
         <span class="p">(</span><span class="nv">kill-buffer</span> <span class="nv">temp-buffer</span><span class="p">)))))</span>
</code></pre></div></div>

<p>The setup is to create a new buffer and switch to it. The teardown
destroys the buffer, regardless of what happens in the body. An
example from Common Lisp would be <code class="language-plaintext highlighter-rouge">with-open-file</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">with-open-file</span> <span class="p">(</span><span class="nc">stream</span> <span class="s">"/etc/passwd"</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">loop</span> <span class="nv">while</span> <span class="p">(</span><span class="nb">listen</span> <span class="nc">stream</span><span class="p">)</span>
     <span class="nv">collect</span> <span class="p">(</span><span class="nb">read-line</span> <span class="nc">stream</span><span class="p">)))</span>
</code></pre></div></div>

<p>This macro ensures that the stream is closed when the body exits, no
matter what. (Side note: this can be very surprising when combined
with Clojure’s laziness!)</p>

<p>There are no macros in JavaScript, let alone Lisp’s powerful macro
system, but the pattern can still be captured using closures. Replace
the body with a callback.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">Transform</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// ...</span>
<span class="p">}</span>

<span class="c1">// ...</span>

<span class="nx">Transform</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">withDraw</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">ctx</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">ctx</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">applyTransform</span><span class="p">(</span><span class="nx">ctx</span><span class="p">);</span>
    <span class="k">try</span> <span class="p">{</span>
        <span class="nx">callback</span><span class="p">();</span>
    <span class="p">}</span> <span class="k">finally</span> <span class="p">{</span>
        <span class="nx">ctx</span><span class="p">.</span><span class="nx">restore</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">};</span>
</code></pre></div></div>

<p>The callback is called once the context is in the proper state. Here’s
how it would be used.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">transform</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Transform</span><span class="p">().</span><span class="nx">scale</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">);</span>  <span class="c1">// (fluent API)</span>

<span class="kd">function</span> <span class="nx">render</span><span class="p">(</span><span class="nx">step</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">transform</span><span class="p">.</span><span class="nx">withDraw</span><span class="p">(</span><span class="nx">ctx</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="c1">// ... draw ...</span>
    <span class="p">});</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Since JavaScript has proper closures, that <code class="language-plaintext highlighter-rouge">step</code> variable is
completely available to the callback. This function-as-body pattern
comes up a lot (e.g. <a href="http://requirejs.org/docs/whyamd.html">AMD</a>), and seeing it work so well makes me
think of JavaScript as a “suitable Lisp.”</p>

<p>Java can just barely pull off the pattern using anonymous classes, but
it’s very clunky.</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">class</span> <span class="nc">Transform</span> <span class="o">{</span>
    <span class="c1">// ...</span>

    <span class="nc">AffineTransform</span> <span class="n">transform</span><span class="o">;</span>

    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">withDraw</span><span class="o">(</span><span class="nc">Graphics2D</span> <span class="n">g</span><span class="o">,</span> <span class="nc">Runnable</span> <span class="n">callback</span><span class="o">)</span> <span class="o">{</span>
        <span class="nc">AffineTransform</span> <span class="n">original</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="na">getTransform</span><span class="o">();</span>
        <span class="n">g</span><span class="o">.</span><span class="na">transform</span><span class="o">(</span><span class="n">transform</span><span class="o">);</span>
        <span class="k">try</span> <span class="o">{</span>
            <span class="n">callback</span><span class="o">.</span><span class="na">run</span><span class="o">();</span>
        <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
            <span class="n">g</span><span class="o">.</span><span class="na">setTransform</span><span class="o">(</span><span class="n">original</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>

<span class="kd">class</span> <span class="nc">Foo</span> <span class="o">{</span>
    <span class="c1">// ...</span>

    <span class="nc">Transform</span> <span class="n">transform</span><span class="o">;</span>

    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">render</span><span class="o">(</span><span class="nc">Graphics2D</span> <span class="n">g</span><span class="o">,</span> <span class="kt">double</span> <span class="n">step</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">transform</span><span class="o">.</span><span class="na">withDraw</span><span class="o">(</span><span class="n">g</span><span class="o">,</span> <span class="k">new</span> <span class="nc">Runnable</span><span class="o">()</span> <span class="o">{</span>
            <span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
                <span class="c1">// ... draw ...</span>
            <span class="o">}</span>
        <span class="o">});</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>

<p>Java’s anonymous classes are closures, but, unlike Lisp and
JavaScript, they close over <em>values</em> rather than <em>bindings</em>. Purely in
attempt to hide this complexity, Java requires that variables accessed
from the anonymous class be declared as <code class="language-plaintext highlighter-rouge">final</code>. It’s awkward and
confusing enough that I probably wouldn’t try to apply it in Java.</p>

<p>I think this pattern works very well with JavaScript, and if you dig
around in some of my graphical JavaScript you’ll see that I’ve already
put it to use. JavaScript functions work pretty well as a stand in for
some kinds of Lisp macros.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Userspace Threading in JavaScript</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2013/04/28/"/>
    <id>urn:uuid:5f81e774-9521-30a6-f816-62a7a3edcf10</id>
    <updated>2013-04-28T00:00:00Z</updated>
    <category term="javascript"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<p>There was an interesting Daily Programmer problem posted a couple of
weeks ago: <a href="http://redd.it/1ceai7">write a userspace threading library</a>. I decided to do
it in JavaScript, building it on top of <code class="language-plaintext highlighter-rouge">setTimeout</code>. Remember that
JavaScript is single-threaded by specification, so this will be a
nonpreemptive, cooperative system.</p>

<p>Start by creating the Thread prototype. As thread constructors usually
work, it accepts the function to be run in that thread.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">Thread</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">alive</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">schedule</span><span class="p">(</span><span class="nx">f</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<p>The <code class="language-plaintext highlighter-rouge">schedule</code> method schedules a function to be run in that thread.
It’s not really meant for users to use directly. I’ll define it in a
moment.</p>

<p>Only one thread actually runs at a time, so globally keep track of the
which one is running at the moment.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">Thread</span><span class="p">.</span><span class="nx">current</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
</code></pre></div></div>

<p>Now here’s the core method that makes everything work, <code class="language-plaintext highlighter-rouge">runner</code>. It
accepts a function of arbitrary arity and returns a function that runs
the provided function in <code class="language-plaintext highlighter-rouge">this</code> thread.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">Thread</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">runner</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">_this</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
    <span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">_this</span><span class="p">.</span><span class="nx">alive</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">try</span> <span class="p">{</span>
                <span class="nx">Thread</span><span class="p">.</span><span class="nx">current</span> <span class="o">=</span> <span class="nx">_this</span><span class="p">;</span>
                <span class="nx">f</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
            <span class="p">}</span> <span class="k">finally</span> <span class="p">{</span>
                <span class="nx">Thread</span><span class="p">.</span><span class="nx">current</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">};</span>
<span class="p">};</span>
</code></pre></div></div>

<p>The runner sets the current thread to the proper value, calls the
function, then clears the current thread. If the thread is no longer
active, nothing happens.</p>

<p>With that in place, <code class="language-plaintext highlighter-rouge">schedule</code> is defined like this,</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">Thread</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">schedule</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">setTimeout</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">runner</span><span class="p">(</span><span class="nx">f</span><span class="p">),</span> <span class="mi">0</span><span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>It creates a runner function for <code class="language-plaintext highlighter-rouge">f</code> and schedules it to run as soon
as possible on JavaScript’s event loop using <code class="language-plaintext highlighter-rouge">setTimeout</code>. Queuing up
on the event loop is the cooperative part of all this. Other threads
and events may already be queued with a timeout of 0, so they run
first.</p>

<p>Technically this is all that’s needed. To yield, schedule a function
and return.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// ... do some work ...</span>
    <span class="nx">Thread</span><span class="p">.</span><span class="nx">current</span><span class="p">.</span><span class="nx">schedule</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="c1">// ... do more work ...</span>
    <span class="p">});</span>
<span class="p">}</span>
</code></pre></div></div>

<p>I don’t want the user to need to think about <code class="language-plaintext highlighter-rouge">Thread.current</code>, so
here’s a convenience <code class="language-plaintext highlighter-rouge">yield</code> function.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">Thread</span><span class="p">.</span><span class="k">yield</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">Thread</span><span class="p">.</span><span class="nx">current</span><span class="p">.</span><span class="nx">schedule</span><span class="p">(</span><span class="nx">f</span><span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>Now to use it,</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// ... do some work ...</span>
    <span class="nx">Thread</span><span class="p">.</span><span class="k">yield</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="c1">// ... do more work ...</span>
    <span class="p">});</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Halting a thread is easy. Any scheduled functions for this thread will
not be invoked, as specified in the <code class="language-plaintext highlighter-rouge">runner</code> method.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">Thread</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">destroy</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">alive</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
<span class="p">};</span>
</code></pre></div></div>

<p>There’s one more situation to worry about: callbacks. Imagine an
asynchronous storage API.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// ... in thread context ...</span>
<span class="nx">storage</span><span class="p">.</span><span class="nx">getValue</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// doesn't run in thread context</span>
<span class="p">});</span>
</code></pre></div></div>

<p>In order to run in the thread the library user would need to create a
<code class="language-plaintext highlighter-rouge">runner</code> function for the current thread. To avoid making them worry
about <code class="language-plaintext highlighter-rouge">Thread.current</code> and <code class="language-plaintext highlighter-rouge">runner</code>, provide another convenience
function, <code class="language-plaintext highlighter-rouge">wrap</code>. There may be a better name for it, but I couldn’t
think of it.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">Thread</span><span class="p">.</span><span class="nx">wrap</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">Thread</span><span class="p">.</span><span class="nx">current</span><span class="p">.</span><span class="nx">runner</span><span class="p">(</span><span class="nx">f</span><span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>Fixing the callback,</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// ... in thread context ...</span>
<span class="nx">storage</span><span class="p">.</span><span class="nx">getValue</span><span class="p">(</span><span class="nx">Thread</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// ... also in thread context ...</span>
<span class="p">}));</span>
</code></pre></div></div>

<h3 id="threading-demo">Threading Demo</h3>

<p>To demonstrate threading I’ll make a thread that continuously fetches
random numbers from a server and displays them.</p>

<p>Here’s a <a href="/blog/2012/08/20/">simple-httpd</a> servlet for generating
numbers. The route for this servlet will be <code class="language-plaintext highlighter-rouge">/random</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">defservlet</span> <span class="nb">random</span> <span class="nv">text/plain</span> <span class="p">()</span>
  <span class="p">(</span><span class="nb">princ</span> <span class="p">(</span><span class="nv">random*</span> <span class="mf">1.0</span><span class="p">)))</span>
</code></pre></div></div>

<p>Since I’m doing this interactively with <a href="/blog/2012/10/31/">Skewer</a> on
the blank demo page, make a tag for displaying the number.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">h1</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="dl">'</span><span class="s1">h1</span><span class="dl">'</span><span class="p">);</span>
<span class="nb">document</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">appendChild</span><span class="p">(</span><span class="nx">h1</span><span class="p">);</span>
</code></pre></div></div>

<p>Here’s the function that will run in the thread. It fetches a number
asynchronously, displays it, then recurses. Notice that
<code class="language-plaintext highlighter-rouge">Thread.yield()</code> acts like a trampoline, providing free tail-call
optimization! This is because the stack is cleared before the provided
function is invoked.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">random</span><span class="p">()</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">xhr</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">XMLHttpRequest</span><span class="p">();</span>
    <span class="nx">xhr</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="dl">'</span><span class="s1">GET</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">/random</span><span class="dl">'</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
    <span class="nx">xhr</span><span class="p">.</span><span class="nx">send</span><span class="p">();</span>
    <span class="nx">xhr</span><span class="p">.</span><span class="nx">onload</span> <span class="o">=</span> <span class="nx">Thread</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="nx">h1</span><span class="p">.</span><span class="nx">innerHTML</span> <span class="o">=</span> <span class="nx">xhr</span><span class="p">.</span><span class="nx">responseText</span><span class="p">;</span>
        <span class="nx">Thread</span><span class="p">.</span><span class="k">yield</span><span class="p">(</span><span class="nx">random</span><span class="p">);</span>
    <span class="p">});</span>
<span class="p">};</span>
</code></pre></div></div>

<p>I set <code class="language-plaintext highlighter-rouge">onload</code> after calling <code class="language-plaintext highlighter-rouge">send</code> just for code organization
purposes. That code is evaluated <em>after</em> <code class="language-plaintext highlighter-rouge">send</code> is called. As far as I
know this should work fine.</p>

<p>Now to create a thread!</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">foo</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Thread</span><span class="p">(</span><span class="nx">random</span><span class="p">);</span>
</code></pre></div></div>

<p>The heading flashes with random numbers as soon as the thread is
created. Even though this thread is continuously running, it’s
frequently yielding. Everything remains responsive, including the
ability to stop the thread.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">foo</span><span class="p">.</span><span class="nx">destroy</span><span class="p">();</span>
</code></pre></div></div>

<p>As soon as this is evaluated, the heading stops being updated. I think
that’s pretty neat!</p>

<h3 id="performance">Performance</h3>

<p>I haven’t tested performance, but I imagine it’s awful. Especially
because of that frequent use of the <code class="language-plaintext highlighter-rouge">apply</code> method. You wouldn’t want
CPU-intensive operations to cooperate like this. Fortunately, in my
demo above I’m manipulating the DOM and waiting on a server response,
so the performance penalties of threading should be negligible.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Fast Monte Carlo Method with JavaScript</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2013/02/25/"/>
    <id>urn:uuid:0208230e-3f57-334e-5d57-7a18f3794288</id>
    <updated>2013-02-25T00:00:00Z</updated>
    <category term="emacs"/><category term="lisp"/><category term="c"/><category term="javascript"/>
    <content type="html">
      <![CDATA[<blockquote>
  <p>How many times should a random number from <code class="language-plaintext highlighter-rouge">[0, 1]</code> be drawn to have
it sum over 1?</p>
</blockquote>

<p>If you want to figure it out for yourself, stop reading now and come
back when you’re done.</p>

<p><a href="http://bayesianthink.blogspot.com/2013/02/the-expected-draws-to-sum-over-one.html">The answer</a> is <em>e</em>. When I came across this question I took
the lazy programmer route and, rather than work out the math, I
estimated the answer using the Monte Carlo method. I used the language
I always use for these scratchpad computations: Emacs Lisp. All I need
to do is switch to the <code class="language-plaintext highlighter-rouge">*scratch*</code> buffer and start hacking. No
external program needed.</p>

<p>The downside is that Elisp is incredibly slow. Fortunately, Elisp is
so similar to Common Lisp that porting to it is almost trivial. My
preferred Common Lisp implementation, SBCL, is very, very fast so it’s
a huge speed upgrade with little cost, should I need it. As far as I
know, SBCL is the fastest Common Lisp implementation.</p>

<p>Even though Elisp was fast enough to determine that the answer is
probably <em>e</em>, I wanted to play around with it. This little test
program doubles as a way to estimate the value of <em>e</em>,
<a href="http://math.fullerton.edu/mathews/n2003/montecarlopimod.html">similar to estimating <em>pi</em></a>. The more trial runs I give it the
more accurate my answer will get — to a point.</p>

<p>Here’s the Common Lisp version. (I love the loop macro, obviously.)</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">trial</span> <span class="p">()</span>
  <span class="p">(</span><span class="nb">loop</span> <span class="nv">for</span> <span class="nb">count</span> <span class="nv">upfrom</span> <span class="mi">1</span>
     <span class="nv">sum</span> <span class="p">(</span><span class="nb">random</span> <span class="mf">1.0</span><span class="p">)</span> <span class="nv">into</span> <span class="nv">total</span>
     <span class="nv">until</span> <span class="p">(</span><span class="nb">&gt;</span> <span class="nv">total</span> <span class="mi">1</span><span class="p">)</span>
     <span class="nv">finally</span> <span class="p">(</span><span class="nb">return</span> <span class="nb">count</span><span class="p">)))</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">monte-carlo</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">loop</span> <span class="nv">repeat</span> <span class="nv">n</span>
     <span class="nv">sum</span> <span class="p">(</span><span class="nv">trial</span><span class="p">)</span> <span class="nv">into</span> <span class="nv">total</span>
     <span class="nv">finally</span> <span class="p">(</span><span class="nb">return</span> <span class="p">(</span><span class="nb">/</span> <span class="nv">total</span> <span class="mf">1.0</span> <span class="nv">n</span><span class="p">))))</span>
</code></pre></div></div>

<p>Using SBCL 1.0.57.0.debian on an Intel Core i7-2600 CPU, once
everything’s warmed up this takes about 9.4 seconds with 100 million
trials.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>(time (monte-carlo 100000000))
Evaluation took:
  9.423 seconds of real time
  9.388587 seconds of total run time (9.380586 user, 0.008001 system)
  99.64% CPU
  31,965,834,356 processor cycles
  99,008 bytes consed
2.7185063
</code></pre></div></div>

<p>Since this makes for an interesting benchmark I gave it a whirl in
JavaScript,</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">trial</span><span class="p">()</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="nx">sum</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">sum</span> <span class="o">+=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">();</span>
        <span class="nx">count</span><span class="o">++</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="nx">count</span><span class="p">;</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">monteCarlo</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">total</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">n</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">total</span> <span class="o">+=</span> <span class="nx">trial</span><span class="p">();</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="nx">total</span> <span class="o">/</span> <span class="nx">n</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>I ran this on Chromium 24.0.1312.68 Debian 7.0 (180326) which uses V8,
currently the fastest JavaScript engine. With 100 million trials,
<strong>this only took about 2.7 seconds</strong>!</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">monteCarlo</span><span class="p">(</span><span class="mi">100000000</span><span class="p">);</span> <span class="c1">// ~2.7 seconds, according to Skewer</span>
<span class="c1">// =&gt; 2.71850356</span>
</code></pre></div></div>

<p>Whoa! It beat SBCL! I was shocked. Let’s try using C as a
baseline. Surely C will be the fastest.</p>

<div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp">
#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">trial</span><span class="p">()</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="kt">double</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">while</span> <span class="p">(</span><span class="n">sum</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">.</span><span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">sum</span> <span class="o">+=</span> <span class="n">rand</span><span class="p">()</span> <span class="o">/</span> <span class="p">(</span><span class="kt">double</span><span class="p">)</span> <span class="n">RAND_MAX</span><span class="p">;</span>
        <span class="n">count</span><span class="o">++</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">count</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">double</span> <span class="nf">monteCarlo</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">total</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">total</span> <span class="o">+=</span> <span class="n">trial</span><span class="p">();</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">total</span> <span class="o">/</span> <span class="p">(</span><span class="kt">double</span><span class="p">)</span> <span class="n">n</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"%f</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">monteCarlo</span><span class="p">(</span><span class="mi">100000000</span><span class="p">));</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>I used the highest optimization setting on the compiler.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ gcc -ansi -W -Wall -Wextra -O3 temp.c
$ time ./a.out
2.718359

real	0m3.782s
user	0m3.760s
sys	0m0.000s
</code></pre></div></div>

<p>Incredible! <strong>JavaScript was faster than C!</strong> That was completely
unexpected.</p>

<h3 id="the-circumstances">The Circumstances</h3>

<p>Both the Common Lisp and C code could probably be carefully tweaked to
improve performance. In Common Lisp’s case I could attach type
information and turn down safety. For C I could use more compiler
flags to squeeze out a bit more performance. Then <em>maybe</em> they could
beat JavaScript.</p>

<p>In contrast, as far as I can tell the JavaScript code is already as
optimized as it can get. There just aren’t many knobs to tweak. Note
that minifying the code will make no difference, especially since I’m
not measuring the parsing time. Except for the functions themselves,
the variables are all local, so they are never “looked up” at
run-time. Their name length doesn’t matter. Remember, in JavaScript
<em>global</em> variables are expensive, because they’re (generally) hash
table lookups on the global object at run-time. For any decent
compiler, local variables are basically precomputed memory offsets —
very fast.</p>

<p>The function names themselves are global variables, but the V8
compiler appears to eliminate this cost (inlining?). Wrapping the
entire thing in another function, turning the two original functions
into local variables, makes no difference in performance.</p>

<p>While Common Lisp and C <em>may</em> be able to beat JavaScript if time is
invested in optimizing them — something to be done rarely — in a
casual implementation of this algorithm, JavaScript beats them both. I
find this really exciting.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Web Distributed Computing Revisited</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2013/01/26/"/>
    <id>urn:uuid:ab83f362-cc7f-308f-309b-5f3af5ae9be9</id>
    <updated>2013-01-26T00:00:00Z</updated>
    <category term="javascript"/><category term="web"/><category term="lisp"/><category term="reddit"/>
    <content type="html">
      <![CDATA[<p>Four years ago I investigated the idea of using
<a href="/blog/2009/06/09/">browsers as nodes for distributed computing</a>. I concluded
that due to the platform’s constraints there were few problems that it
was suited to solve. However, the situation has since changed quite a
bit! In fact, this weekend I made practical use of web browsers across
a number of geographically separated computers to solve a
computational problem.</p>

<h3 id="what-changed">What changed?</h3>

<p><a href="http://en.wikipedia.org/wiki/Web_worker">Web workers</a> came into existence, not just as a specification
but as an implementation across all the major browsers. It allows for
JavaScript to be run in an isolated, dedicated background thread. This
eliminates the <code class="language-plaintext highlighter-rouge">setTimeout()</code> requirement from before, which not only
caused a performance penalty but really hampered running any sort of
lively interface alongside the computation. The interface and
computation were competing for time on the same thread.</p>

<p>The worker isn’t <em>entirely</em> isolated; otherwise it would be useless
for anything but wasting resources. As pubsub events, it can pass
<a href="https://developer.mozilla.org/en-US/docs/DOM/The_structured_clone_algorithm">structured clones</a> to and from the main thread running in the
page. Other than this, it has no access to the DOM or other data on
the page.</p>

<p>The interface is a bit unfriendly to <a href="/blog/2012/10/31/">live development</a>, but
it’s manageable. It’s invoked by passing the URL of a script to the
constructor. This script is the code that runs in the dedicated thread.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">worker</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Worker</span><span class="p">(</span><span class="dl">'</span><span class="s1">script/worker.js</span><span class="dl">'</span><span class="p">);</span>
</code></pre></div></div>

<p>The sort of interface that would have been more convenient for live
interaction would be something like what is found on most
multi-threaded platforms: a thread constructor that accepts a function
as an argument.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cm">/* This doesn't work! */</span>
<span class="kd">var</span> <span class="nx">worker</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Worker</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// ...</span>
<span class="p">});</span>
</code></pre></div></div>

<p>I completely understand why this isn’t the case. The worker thread
needs to be totally isolated and the above example is insufficient.
I’m passing a closure to the constructor, which means I would be
sharing bindings, and therefore data, with the worker thread. This
interface could be faked using a <a href="http://en.wikipedia.org/wiki/Data_URI_scheme">data URI</a> and taking
advantage of the fact that most browsers return function source code
from <code class="language-plaintext highlighter-rouge">toString()</code>.</p>

<s>Another difficulty is libraries. Ignoring the stupid idea of
passing code through the event API and evaling it, that single URL
must contain *all* the source code the worker will use as one
script. This means if you want to use any libraries you'll need to
concatenate them with your script. That complicates things slightly,
but I imagine many people will be minifying their worker JavaScript
anyway.</s>

<p>Libraries can be loaded by the worker with the <code class="language-plaintext highlighter-rouge">importScripts()</code>
function, so not everything needs to be packed into one
script. Furthermore, workers can make HTTP requests with
XMLHttpRequest, so that data don’t need to be embedded either. Note
that it’s probably worth making these requests synchronously (third
argument <code class="language-plaintext highlighter-rouge">false</code>), because blocking isn’t an issue in workers.</p>

<p>The other big change was the effect Google Chrome, especially its V8
JavaScript engine, had on the browser market. Browser JavaScript is
probably about two orders of magnitude faster than it was when I wrote
my previous post. It’s
<a href="http://youtu.be/UJPdhx5zTaw">incredible what the V8 team has accomplished</a>. If written
carefully, V8 JavaScript performance can beat out most other languages.</p>

<p>Finally, I also now have much, much better knowledge of JavaScript
than I did four years ago. I’m not fumbling around like I was before.</p>

<h3 id="applying-these-changes">Applying these Changes</h3>

<p><a href="http://redd.it/178vsz">This weekend’s Daily Programmer challenge</a> was to find a “key” —
a permutation of the alphabet — that when applied to a small
dictionary results in the maximum number of words with their letters
in alphabetical order. That’s a keyspace of 26!, or
403,291,461,126,605,635,584,000,000.</p>

<p>When I’m developing, I use both a laptop and a desktop simultaneously,
and I really wanted to put them both to work searching that huge space
for good solutions. Initially I was going to accomplish this by
writing my program in Clojure and running it on each machine. But what
about involving my wife’s computer, too? I wasn’t going to bother her
with setting up an environment to run my stuff. Writing it in
JavaScript as a web application would be the way to go. To coordinate
this work I’d use <a href="/blog/2012/08/20/">simple-httpd</a>. And so it was born,</p>

<ul>
  <li><a href="https://github.com/skeeto/key-collab">https://github.com/skeeto/key-collab</a></li>
</ul>

<p>Here’s what it looks like in action. Each tab open consumes one CPU
core, allowing users to control their commitment by choosing how many
tabs to keep open. All of those numbers update about twice per second,
so users can get a concrete idea of what’s going on. I think it’s fun
to watch.</p>

<p><a href="/img/screenshot/key-collab.png"><img src="/img/screenshot/key-collab-thumb.png" alt="" /></a></p>

<p>(I’m obviously a fan of blues and greens on my web pages. I don’t know why.)</p>

<p>I posted the server’s URL on reddit in the challenge thread, so
various reddit users from around the world joined in on the
computation.</p>

<h3 id="strict-mode">Strict Mode</h3>

<p>I had an accidental discovery with <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode">strict mode</a> and
Chrome. I’ve always figured using strict mode had an effect on the
performance of code, but had no idea how much. From the beginning, I
had intended to use it in my worker script. Being isolated already,
there are absolutely no downsides.</p>

<p>However, while I was developing and experimenting I accidentally
turned it off and left it off. It was left turned off for a short time
in the version I distributed to the clients, so I got to see how
things were going without it. When I noticed the mistake and
uncommented the <code class="language-plaintext highlighter-rouge">"use strict"</code> line, <strong>I saw a 6-fold speed boost in
Chrome</strong>. Wow! Just making those few promises to Chrome allowed it to
make some massive performance optimizations.</p>

<p>With Chrome moving at full speed, it was able to inspect 560 keys per
second on <a href="http://www.50ply.com/">Brian’s</a> laptop. I was getting about 300 keys per
second on my own (less-capable) computers. I haven’t been able to get
anything close to these speeds in any other language/platform (but I
didn’t try in C yet).</p>

<p>Furthermore, I got a noticeable speed boost in Chrome by using proper
object oriented programming, versus a loose collection of functions
and ad-hoc structures. I think it’s because it made me construct my
data structures consistently, allowing V8’s hidden classes to work
their magic. It also probably helped the compiler predict type
information. I’ll need to investigate this further.</p>

<p>Use strict mode whenever possible, folks!</p>

<h3 id="what-made-this-problem-work">What made this problem work?</h3>

<p>Having web workers available was a big help. However, this problem met
the original constraints fairly well.</p>

<ul>
  <li>
    <p>It was <strong>low bandwidth</strong>. No special per-client instructions were
required. The client only needed to report back a 26-character
string.</p>
  </li>
  <li>
    <p>There was <strong>no state</strong> to worry about. The original version of my
script tried keys at random. The later version used a hill-climbing
algorithm, so there was <em>some</em> state but it was only needed for a
few seconds at a time. It wasn’t worth holding onto.</p>
  </li>
</ul>

<p>This project was a lot of fun so I hope I get another opportunity to
do it again in the future, hopefully with a lot more nodes
participating.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  <entry>
    <title>Parameter Lists in Common Lisp and Clojure</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2013/01/20/"/>
    <id>urn:uuid:01182f77-6dfa-3448-2c3a-7bb428d5b430</id>
    <updated>2013-01-20T00:00:00Z</updated>
    <category term="lisp"/><category term="clojure"/>
    <content type="html">
      <![CDATA[<p>Parameter lists in Common Lisp, called <a href="http://www.lispworks.com/documentation/HyperSpec/Body/03_d.htm">lambda lists</a>,
are written in their own mini-language, making it convenient to write
functions with a flexible call interface. A lambda list can specify
optional parameters, optionally with default arguments, named
(keyword) parameters, and whether or not the function is
variadic. It’s something I miss a lot when using other languages,
especially JavaScript.</p>

<h3 id="common-lisp-parameters">Common Lisp Parameters</h3>

<p>Here some some examples. This function, <code class="language-plaintext highlighter-rouge">foo</code>, has three required
parameters, <code class="language-plaintext highlighter-rouge">a</code>, <code class="language-plaintext highlighter-rouge">b</code>, and <code class="language-plaintext highlighter-rouge">c</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="nv">a</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">)</span>
  <span class="o">...</span><span class="p">)</span>
</code></pre></div></div>

<p>To make <code class="language-plaintext highlighter-rouge">b</code> and <code class="language-plaintext highlighter-rouge">c</code> optional, place them after the symbol <code class="language-plaintext highlighter-rouge">&amp;optional</code>,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="nv">a</span> <span class="k">&amp;optional</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">)</span>
  <span class="o">...</span><span class="p">)</span>
</code></pre></div></div>

<p>If second and third arguments are not provided, <code class="language-plaintext highlighter-rouge">b</code> and <code class="language-plaintext highlighter-rouge">c</code> will be
bound to <code class="language-plaintext highlighter-rouge">nil</code>. To provide a default argument, put that parameter
inside a list. Below, when a third argument is not provided, <code class="language-plaintext highlighter-rouge">c</code> will
be bound to <code class="language-plaintext highlighter-rouge">"bar"</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="nv">a</span> <span class="k">&amp;optional</span> <span class="nv">b</span> <span class="p">(</span><span class="nv">c</span> <span class="s">"bar"</span><span class="p">))</span>
  <span class="o">...</span><span class="p">)</span>
</code></pre></div></div>

<p>To write a function that accepts any number of arguments, use <code class="language-plaintext highlighter-rouge">&amp;rest</code>
followed by the parameter to hold the list of the remaining
arguments. Below, <code class="language-plaintext highlighter-rouge">args</code> will be a list of all arguments after the
third. Note how this can be combined with <code class="language-plaintext highlighter-rouge">&amp;optional</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="nv">a</span> <span class="k">&amp;optional</span> <span class="nv">b</span> <span class="nv">c</span> <span class="k">&amp;rest</span> <span class="nv">args</span><span class="p">)</span>
  <span class="o">...</span><span class="p">)</span>
</code></pre></div></div>

<p>Often, the <em>position</em> of a parameter may be hard to remember or read,
especially if there are many parameters. It may be more convenient to
name them with <code class="language-plaintext highlighter-rouge">&amp;key</code>. Below, the function has three named parameters,
specified at the call site using <em>keywords</em> — special symbols from
the keyword package that always evaluate to themselves.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="k">&amp;key</span> <span class="nv">a</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">)</span>
  <span class="o">...</span><span class="p">)</span>

<span class="p">(</span><span class="nv">foo</span> <span class="ss">:b</span> <span class="s">"world"</span> <span class="ss">:a</span> <span class="s">"hello"</span><span class="p">)</span>
</code></pre></div></div>

<p>Like optional parameters, when a parameter is not provided it is bound
to <code class="language-plaintext highlighter-rouge">nil</code>. In the same way, it can be given a default argument.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="k">&amp;key</span> <span class="p">(</span><span class="nv">a</span> <span class="s">"hello"</span><span class="p">)</span> <span class="p">(</span><span class="nv">b</span> <span class="s">"world"</span><span class="p">)</span> <span class="nv">c</span><span class="p">)</span>
  <span class="o">...</span><span class="p">)</span>
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">&amp;key</code> can be combined with <code class="language-plaintext highlighter-rouge">&amp;optional</code> and <code class="language-plaintext highlighter-rouge">&amp;rest</code>. However, the
<code class="language-plaintext highlighter-rouge">&amp;rest</code> argument will be filled with all of key-value pairs, so it’s
generally not useful to use them together.</p>

<p>Lambda lists are not exclusive to <code class="language-plaintext highlighter-rouge">defun</code> and can be used in any place
that needs to receive values in parameters, such as <code class="language-plaintext highlighter-rouge">flet</code> (<em>function</em>
let), <code class="language-plaintext highlighter-rouge">defmethod</code>, and so on.</p>

<h3 id="clojure-parameters">Clojure Parameters</h3>

<p>Clojure forgoes these complex lambda lists in preference for
overloading by arity. When a function is being defined, multiple
functions of different arities can be defined at once. This makes for
optional parameters. Note how this leaves no room for a default
argument of <code class="language-plaintext highlighter-rouge">nil</code> for unspecified optional arguments.</p>

<p>Here, <code class="language-plaintext highlighter-rouge">b</code> is an optional parameter for <code class="language-plaintext highlighter-rouge">foo</code>, defaulting to <code class="language-plaintext highlighter-rouge">"bar"</code>
when not provided by the caller. The first definition has an arity of
one and it calls the second definition with the optional argument
filled in.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">foo</span><span class="w">
  </span><span class="p">([</span><span class="n">a</span><span class="p">]</span><span class="w"> </span><span class="p">(</span><span class="nf">foo</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="s">"bar"</span><span class="p">))</span><span class="w">
  </span><span class="p">([</span><span class="n">a</span><span class="w"> </span><span class="n">b</span><span class="p">]</span><span class="w"> </span><span class="n">...</span><span class="p">))</span><span class="w">
</span></code></pre></div></div>

<p>Variadic functions are specified with <code class="language-plaintext highlighter-rouge">&amp;</code>, similar to <code class="language-plaintext highlighter-rouge">&amp;rest</code> in
Common Lisp. Below, <code class="language-plaintext highlighter-rouge">xs</code> is a sequence of all of the arguments
provided after the first.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="n">xs</span><span class="p">]</span><span class="w">
  </span><span class="n">...</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>As far as <em>parameters</em> are concerned, this is all Clojure
has. However, Clojure’s parameter specification is actually <em>more</em>
flexible than Common Lisp’s lambda lists in two important ways. One is
that <strong>parameter position can vary with the number of provided
arguments</strong>. The Clojure core functions use this a lot (ex.
<a href="http://clojuredocs.org/clojure_core/1.2.0/clojure.core/reduce"><code class="language-plaintext highlighter-rouge">reduce</code></a>).</p>

<p>The following in Common Lisp would require manually parsing the
parameters on some level. The <code class="language-plaintext highlighter-rouge">last</code> parameter can be either second or
third depending on whether a middle name was provided.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">make-name</span><span class="w">
  </span><span class="p">([</span><span class="nb">first</span><span class="w"> </span><span class="nb">last</span><span class="p">]</span><span class="w">
     </span><span class="p">(</span><span class="nf">make-name</span><span class="w"> </span><span class="nb">first</span><span class="w"> </span><span class="s">"Q"</span><span class="w"> </span><span class="nb">last</span><span class="p">))</span><span class="w">
  </span><span class="p">([</span><span class="nb">first</span><span class="w"> </span><span class="n">middle</span><span class="w"> </span><span class="nb">last</span><span class="p">]</span><span class="w">
     </span><span class="p">{</span><span class="no">:first</span><span class="w"> </span><span class="n">first,</span><span class="w"> </span><span class="no">:middle</span><span class="w"> </span><span class="n">middle,</span><span class="w"> </span><span class="no">:last</span><span class="w"> </span><span class="nb">last</span><span class="p">}))</span><span class="w">

</span><span class="p">(</span><span class="nf">make-name</span><span class="w"> </span><span class="s">"John"</span><span class="w"> </span><span class="s">"Public"</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; {:first "John", :middle "Q", :last "Public"}</span><span class="w">
</span></code></pre></div></div>

<p>That covers optional parameters with default arguments and variadic
functions. What about keyword parameters? Well, to cover that we need
to talk about <em>destructuring</em>, which is another way that Clojure
parameters are more powerful than lambda lists.</p>

<h3 id="destructuring">Destructuring</h3>

<p>A powerful Lisp idiom is destructuring bindings. Variables can be
bound to values in a structure by position in the structure. In Common
Lisp there are three macros for making destructuring bindings,
<code class="language-plaintext highlighter-rouge">destructuring-bind</code>, <code class="language-plaintext highlighter-rouge">loop</code> and <code class="language-plaintext highlighter-rouge">with-slots</code> (CLOS).</p>

<p>Below, in the body of the form, <code class="language-plaintext highlighter-rouge">a</code>, <code class="language-plaintext highlighter-rouge">b</code>, and <code class="language-plaintext highlighter-rouge">c</code> are bound to 1, 2,
and 3 respectively. The form <code class="language-plaintext highlighter-rouge">(a (b c))</code> is mapped into the quoted
structure of the same shape to the right.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">destructuring-bind</span> <span class="p">(</span><span class="nv">a</span> <span class="p">(</span><span class="nv">b</span> <span class="nv">c</span><span class="p">))</span> <span class="o">'</span><span class="p">(</span><span class="mi">1</span> <span class="p">(</span><span class="mi">2</span> <span class="mi">3</span><span class="p">))</span>
  <span class="p">(</span><span class="nb">+</span> <span class="nv">a</span> <span class="p">(</span><span class="nb">*</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">)))</span>
<span class="c1">;; =&gt; 7</span>
</code></pre></div></div>

<p>Because of Common Lisp’s concept of <em>cons cells</em>, the <em>cdr</em> of a cell
can be bound to a variable if that variable appears in the <code class="language-plaintext highlighter-rouge">cdr</code>
position. This is similar to the <code class="language-plaintext highlighter-rouge">&amp;rest</code> parameter (and is how Scheme
does variadic functions). I like using this to match the head and tail
of a list,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">destructuring-bind</span> <span class="p">(</span><span class="nv">x</span> <span class="o">.</span> <span class="nv">xs</span><span class="p">)</span> <span class="o">'</span><span class="p">(</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span> <span class="mi">4</span> <span class="mi">5</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">list</span> <span class="nv">x</span> <span class="nv">xs</span><span class="p">))</span>
<span class="c1">;; =&gt; (1 (2 3 4 5))</span>
</code></pre></div></div>

<p>Perhaps the neatest use of destructuring is in the <code class="language-plaintext highlighter-rouge">loop</code> macro. This
loop walks over a list two at a time, binding a variable to each side
of the pair,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">loop</span> <span class="nv">for</span> <span class="p">(</span><span class="kt">keyword</span> <span class="nv">value</span><span class="p">)</span> <span class="nv">on</span> <span class="o">'</span><span class="p">(</span><span class="ss">:a</span> <span class="mi">1</span> <span class="ss">:b</span> <span class="mi">2</span> <span class="ss">:c</span> <span class="mi">3</span><span class="p">)</span> <span class="nv">by</span> <span class="nf">#'</span><span class="nb">cddr</span>
   <span class="nv">collect</span> <span class="kt">keyword</span> <span class="nv">into</span> <span class="nv">keywords</span>
   <span class="nv">collect</span> <span class="nv">value</span> <span class="nv">into</span> <span class="nb">values</span>
   <span class="nv">finally</span> <span class="p">(</span><span class="nb">return</span> <span class="p">(</span><span class="nb">values</span> <span class="nv">keywords</span> <span class="nb">values</span><span class="p">)))</span>
<span class="c1">;; =&gt; (:A :B :C), (1 2 3)</span>
</code></pre></div></div>

<p>Unfortunately destructuring in Common Lisp is limited to these few
cases, or where ever else you write your own destructuring macros.</p>

<p>Clojure takes destructuring to its logical conclusion: <strong>destructuring
can be used any place bindings are established</strong>! This includes
parameter lists. It works on any core data structure, not just lists.</p>

<p>Below, I’m doing destructuring inside of a standard <code class="language-plaintext highlighter-rouge">let</code> form.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">greet-dr</span><span class="w"> </span><span class="p">[</span><span class="n">fullname</span><span class="p">]</span><span class="w">
  </span><span class="p">(</span><span class="k">let</span><span class="w"> </span><span class="p">[[</span><span class="nb">first</span><span class="w"> </span><span class="nb">last</span><span class="p">]</span><span class="w"> </span><span class="p">(</span><span class="nf">clojure.string/split</span><span class="w"> </span><span class="n">fullname</span><span class="w"> </span><span class="o">#</span><span class="s">" +"</span><span class="p">)]</span><span class="w">
    </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="s">"Hello, Dr. "</span><span class="w"> </span><span class="nb">last</span><span class="w"> </span><span class="s">". "</span><span class="w">
         </span><span class="s">"It's good to see you again, "</span><span class="w"> </span><span class="nb">first</span><span class="w"> </span><span class="s">"."</span><span class="p">)))</span><span class="w">

</span><span class="p">(</span><span class="nf">greet-dr</span><span class="w"> </span><span class="s">"John Doe"</span><span class="p">)</span><span class="w">
</span><span class="c1">;; "Hello, Dr. Doe. It's good to see you again, John."</span><span class="w">
</span></code></pre></div></div>

<p>Similarly, I could destructure an argument into my parameters. (Note
the double square brackets.)</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">greet-dr-2</span><span class="w"> </span><span class="p">[[</span><span class="nb">first</span><span class="w"> </span><span class="nb">last</span><span class="p">]]</span><span class="w">
  </span><span class="n">...</span><span class="p">)</span><span class="w">

</span><span class="p">(</span><span class="nf">greet-dr-2</span><span class="w"> </span><span class="p">[</span><span class="s">"John"</span><span class="w"> </span><span class="s">"Doe"</span><span class="p">])</span><span class="w">
</span></code></pre></div></div>

<p>Because hashmaps are a core language feature in Clojure, they can also
be destructured. The syntax is a bit like flipping the hashmap inside
out. The variable is specified, then the key it’s mapped to.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">let</span><span class="w"> </span><span class="p">[{</span><span class="n">a</span><span class="w"> </span><span class="no">:a,</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="no">:b</span><span class="p">}</span><span class="w"> </span><span class="p">{</span><span class="no">:a</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="no">:b</span><span class="w"> </span><span class="mi">2</span><span class="p">}]</span><span class="w">
  </span><span class="p">(</span><span class="nb">list</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">b</span><span class="p">))</span><span class="w">
</span><span class="c1">;; =&gt; (1 2)</span><span class="w">
</span></code></pre></div></div>

<p>When variables and keys have the same name, there’s a shorthand with
<code class="language-plaintext highlighter-rouge">:keys</code>.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">let</span><span class="w"> </span><span class="p">[{</span><span class="no">:keys</span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="w"> </span><span class="n">b</span><span class="p">]}</span><span class="w"> </span><span class="p">{</span><span class="no">:a</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="no">:b</span><span class="w"> </span><span class="mi">2</span><span class="p">}]</span><span class="w">
  </span><span class="n">...</span><span class="p">)</span><span class="w">
</span></code></pre></div></div>

<p>Variables default to <code class="language-plaintext highlighter-rouge">nil</code> when the corresponding key is not in the
map. They can be given default values with <code class="language-plaintext highlighter-rouge">:or</code>.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">let</span><span class="w"> </span><span class="p">[{</span><span class="n">a</span><span class="w"> </span><span class="no">:a,</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="no">:b</span><span class="w"> </span><span class="no">:or</span><span class="w"> </span><span class="p">{</span><span class="n">a</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="mi">0</span><span class="p">}}</span><span class="w"> </span><span class="p">{}]</span><span class="w">
  </span><span class="p">(</span><span class="nb">list</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">b</span><span class="p">))</span><span class="w">
</span><span class="c1">;; =&gt; (0 0)</span><span class="w">
</span></code></pre></div></div>

<p>Now, here’s where it gets really neat. In Common Lisp, the <code class="language-plaintext highlighter-rouge">&amp;key</code> part
of a lambda list is a special case. In Clojure it comes for free as
part of destructuring. Just destructure the <em>rest</em> argument!</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">height-opinion</span><span class="w"> </span><span class="p">[</span><span class="nb">name</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="p">{</span><span class="n">height</span><span class="w"> </span><span class="no">:height</span><span class="p">}]</span><span class="w">
  </span><span class="p">(</span><span class="nf">if-not</span><span class="w"> </span><span class="n">height</span><span class="w">
    </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="s">"I have no opinion on "</span><span class="w"> </span><span class="nb">name</span><span class="w"> </span><span class="s">"."</span><span class="p">)</span><span class="w">
    </span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">&lt;</span><span class="w"> </span><span class="n">height</span><span class="w"> </span><span class="mi">6</span><span class="p">)</span><span class="w">
      </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="nb">name</span><span class="w"> </span><span class="s">" is short."</span><span class="p">)</span><span class="w">
      </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="nb">name</span><span class="w"> </span><span class="s">" is tall."</span><span class="p">))))</span><span class="w">

</span><span class="p">(</span><span class="nf">height-opinion</span><span class="w"> </span><span class="s">"Chris"</span><span class="w"> </span><span class="no">:height</span><span class="w"> </span><span class="mf">6.25</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; "Chris is tall."</span><span class="w">
</span></code></pre></div></div>

<p>We can still access the entire rest argument at the same time, using
<code class="language-plaintext highlighter-rouge">:as</code>, so it covers everything Common Lisp covers.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="w"> </span><span class="p">{</span><span class="n">a</span><span class="w"> </span><span class="no">:a,</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="no">:b</span><span class="w"> </span><span class="no">:as</span><span class="w"> </span><span class="n">args</span><span class="p">}]</span><span class="w">
  </span><span class="n">args</span><span class="p">)</span><span class="w">

</span><span class="p">(</span><span class="nf">foo</span><span class="w"> </span><span class="no">:b</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; {:b 10}</span><span class="w">
</span></code></pre></div></div>

<p>(A side note while we’re making comparisons: keywords in Clojure are
<em>not</em> symbols, but rather a whole type of their own.)</p>

<h3 id="conclusion">Conclusion</h3>

<p>Clojure parameter lists are simpler than Common Lisp’s lambda lists
and, thanks to destructuring anywhere, they end up being <em>more
powerful</em> at the same time. It’s a full super set of lambda lists, so
there’s no practical trade-off.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  <entry>
    <title>Clojure and Emacs for Lispers</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2013/01/07/"/>
    <id>urn:uuid:5a316444-8f60-30c0-2a94-b87eb239eb13</id>
    <updated>2013-01-07T00:00:00Z</updated>
    <category term="lisp"/><category term="emacs"/><category term="clojure"/>
    <content type="html">
      <![CDATA[<p>According to my e-mail archives I’ve been interested in
<a href="http://clojure.org/">Clojure</a> for about three and a half years now. During that
period I would occasionally spend an evening trying to pick it up,
only to give up after getting stuck on some installation or
configuration issue. With a little bit of pushing from <a href="http://50ply.com/">Brian</a>,
and the fact that this installation and configuration <em>is now
trivial</em>, I finally broke that losing streak last week.</p>

<h3 id="im-damn-picky">I’m Damn Picky</h3>

<p>Personally, there’s a high barrier in place to learn new programming
languages. It’s entirely my own fault. I’m <em>really</em> picky about my
development environment. If I’m going to write code in a language I
need Emacs to support a comfortable workflow around it. Otherwise
progress feels agonizingly sluggish. If at all possible this means
live interaction with the runtime (Lisp, JavaScript). If not, then I
need to be able to invoke builds and run tests from within Emacs (C,
Java). Basically, <strong>I want to leave the Emacs window as infrequently
possible</strong>.</p>

<p>I also need a major mode with decent indentation support. This tends
to be the hardest part to create. Automatic indentation in Emacs is
considered a black magic. Fortunately, it’s unusual to come across a
language that doesn’t already have a major mode written for it. It’s
only happened once for me and that’s because it was a custom language
for a computer languages course. To remedy this, I ended up
<a href="/blog/2012/09/20/">writing my own major mode</a>, including in-Emacs evaluation.</p>

<p>Unsatisfied with JDEE, I did the same for Java,
<a href="/blog/2011/11/19/">growing my own extensions</a> to support my development for the
couple of years when Java was my primary programming language. The
dread of having to switch back and forth between Emacs and my browser
kept me away from web development for years. That changed this past
October when I <a href="/blog/2012/10/31/">wrote skewer-mode</a> to support interactive
JavaScript development. JavaScript is now one of my favorite
programming languages.</p>

<p>I’ve wasted enough time in my life configuring and installing
software. I hate sinking time into doing so without capturing that
work in source control, so that I never need to spend time on that
particular thing again. I don’t mean the installation itself but the
configuration — the difference from the defaults. (And the better the
defaults, the smaller my configuration needs to be.) With
<a href="/blog/2012/06/23/">my dotfiles repository</a> and Debian, I can go from a
computer with no operating system to a fully productive development
environment inside of about one hour. Almost all of that time is just
waiting on Debian to install all its packages. Any new language
development workflow needs to be compatible with this.</p>

<h3 id="clojure-installation">Clojure Installation</h3>

<p>Until last year sometime the standard way to interact with Clojure
from Emacs was through <a href="https://github.com/technomancy/swank-clojure">swank-clojure</a> with
SLIME. Well, installing <a href="/blog/2010/01/15/">SLIME itself can be a pain</a>.
<a href="http://www.quicklisp.org/">Quicklisp</a> now makes this part trivial but it’s specific
to Common Lisp. This is also a conflict with Common Lisp, so I’d
basically need to choose one language or the other.</p>

<p>SLIME doesn’t have any official stable releases. On top of this, the
SWANK protocol is undocumented and subject to change at any time. As a
result, SWANK backends are generally tied to a very specific version
of SLIME and it’s not unusual for something to break when upgrading
one or the other. I know because I wrote
<a href="/blog/2011/01/30/">my own SWANK backend</a> for BrianScheme. Thanks to
Quicklisp, today this isn’t an issue for Common Lisp users, but it’s
not as much help for Clojure.</p>

<p>The good news is that <strong>swank-clojure is now depreciated</strong>. The
replacement is a similar, but entirely independent, library called
<strong>nREPL</strong>. (I’d link to it but there doesn’t seem to be a website.)
Additionally, there’s an excellent Emacs interface to it:
<a href="https://github.com/kingtim/nrepl.el">nrepl.el</a>. It’s available on MELPA, so installation is
trivial.</p>

<p>There’s also a clojure-mode package on MELPA, so install that, too.</p>

<p>That covers the Emacs side of things, so what about Clojure itself?
The Clojure community is a fast-moving target and the Debian packages
can’t quite keep up. At the time of this writing they’re too old to
use nREPL. The good news is that there’s an alternative that’s just as
good, if not better: <a href="http://leiningen.org/">Leiningen</a>.</p>

<p>Leiningen is the standard Clojure build tool and dependency
manager. Here, “dependencies” includes Clojure itself. If you have
Leiningen you have Clojure. Installing Leiningen is as simple as
placing a single shell script in your <code class="language-plaintext highlighter-rouge">$PATH</code>. Since I always have
<code class="language-plaintext highlighter-rouge">~/bin</code> in my <code class="language-plaintext highlighter-rouge">$PATH</code>, all I need to do is wget/curl the script there
and <code class="language-plaintext highlighter-rouge">chmod +x</code> it. The first time it runs it pulls down all of its own
dependencies automatically. Right now the biggest downside seems to be
that it’s really slow to start. I think the JVM warmup time is to
blame.</p>

<p>Let’s review. To install a working Emacs live-interaction Clojure
development environment,</p>

<ul>
  <li>
    <p><strong>Install the nrepl.el package in Emacs.</strong> For me this happens
automatically by the configuration in my
<a href="/blog/2011/10/19/">.emacs.d repository</a>. I only had to do this step once.</p>
  </li>
  <li>
    <p><strong>Install the clojure-mode package.</strong> Same deal.</p>
  </li>
  <li>
    <p><strong>Install a JDK.</strong> OpenJDK is probably in your system’s package
manager, so this is trivial.</p>
  </li>
  <li>
    <p><strong>Put the <code class="language-plaintext highlighter-rouge">lein</code> shell script in the <code class="language-plaintext highlighter-rouge">$PATH</code>.</strong> This takes about
five seconds. If even this was too much for my precious
sensibilities I could put this script in my dotfiles repository.</p>
  </li>
</ul>

<p>With this all in place, do <code class="language-plaintext highlighter-rouge">M-x nrepl-jack-in</code> in Emacs and any
clojure-mode buffer will be ready to evaluate code as expected. It’s
wonderful.</p>

<h3 id="further-extending-emacs">Further Extending Emacs</h3>

<p>I made some tweaks to further increase my comfort. Perhaps nREPL’s
biggest annoyance is not focusing the error buffer, like all the other
interactive modes. Once I’m done glancing at it I’ll dismiss it with
<code class="language-plaintext highlighter-rouge">q</code>. This advice fixes that.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">defadvice</span> <span class="nv">nrepl-default-err-handler</span> <span class="p">(</span><span class="nv">after</span> <span class="nv">nrepl-focus-errors</span> <span class="nv">activate</span><span class="p">)</span>
  <span class="s">"Focus the error buffer after errors, like Emacs normally does."</span>
  <span class="p">(</span><span class="nv">select-window</span> <span class="p">(</span><span class="nv">get-buffer-window</span> <span class="s">"*nrepl-error*"</span><span class="p">)))</span>
</code></pre></div></div>

<p>I also like having expressions flash when I evaluate them. Both SLIME
and Skewer do this. This uses <code class="language-plaintext highlighter-rouge">slime-flash-region</code> to do so when
available.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">defadvice</span> <span class="nv">nrepl-eval-last-expression</span> <span class="p">(</span><span class="nv">after</span> <span class="nv">nrepl-flash-last</span> <span class="nv">activate</span><span class="p">)</span>
  <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">fboundp</span> <span class="ss">'slime-flash-region</span><span class="p">)</span>
      <span class="p">(</span><span class="nv">slime-flash-region</span> <span class="p">(</span><span class="nv">save-excursion</span> <span class="p">(</span><span class="nv">backward-sexp</span><span class="p">)</span> <span class="p">(</span><span class="nv">point</span><span class="p">))</span> <span class="p">(</span><span class="nv">point</span><span class="p">))))</span>

<span class="p">(</span><span class="nv">defadvice</span> <span class="nv">nrepl-eval-expression-at-point</span> <span class="p">(</span><span class="nv">after</span> <span class="nv">nrepl-flash-at</span> <span class="nv">activate</span><span class="p">)</span>
  <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">fboundp</span> <span class="ss">'slime-flash-region</span><span class="p">)</span>
      <span class="p">(</span><span class="nb">apply</span> <span class="nf">#'</span><span class="nv">slime-flash-region</span> <span class="p">(</span><span class="nv">nrepl-region-for-expression-at-point</span><span class="p">))))</span>
</code></pre></div></div>

<p>For Lisp modes I use parenface to de-emphasize parenthesis. Reading
Lisp is more about indentation than parenthesis. Clojure uses square
brackets (<code class="language-plaintext highlighter-rouge">[]</code>) and curly braces (<code class="language-plaintext highlighter-rouge">{}</code>) heavily, so these now also get
special highlighting. See <a href="/blog/2011/10/19/">my .emacs.d</a> for that. Here’s
what it looks like,</p>

<p><img src="/img/screenshot/clojure-brackets.png" alt="" /></p>

<h3 id="learning-clojure">Learning Clojure</h3>

<p>The next step is actually learning Clojure. I already know Common Lisp
very well. It has a lot in common with Clojure so I didn’t want to
start from a pure introductory text. More importantly, I needed to
know upfront <a href="http://clojure.org/lisps">which of my pre-conceptions were wrong</a>. This was
an issue I had, and still have, with JavaScript. Nearly all the
introductory texts for JavaScript are aimed at beginner
programmers. It’s a lot of text for very little new information.</p>

<p>More good news! There’s a very thorough Clojure introductory guide
that starts at a reasonable level of knowledge.</p>

<ul>
  <li><a href="http://java.ociweb.com/mark/clojure/article.html">Clojure - Functional Programming for the JVM</a></li>
</ul>

<p>A few hours going through that while experimenting in a <code class="language-plaintext highlighter-rouge">*clojure*</code>
scratch buffer and I was already feeling pretty comfortable. With a
few months of studying <a href="http://clojure.github.com/clojure/">the API</a>, learning the idioms, and
practicing, I expect to be a fluent speaker.</p>

<p>I think it’s ultimately a good thing I didn’t get into Clojure a
couple of years ago. That gave me time to build up — as a sort of
rite of passage — needed knowledge and experience with Java, which
deliberately, through the interop, plays a significant role in
Clojure.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  <entry>
    <title>A Use For Macrolet</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/12/06/"/>
    <id>urn:uuid:271d91dd-abad-31c8-4e26-f8dfdd5ffa92</id>
    <updated>2012-12-06T00:00:00Z</updated>
    <category term="lisp"/><category term="elisp"/>
    <content type="html">
      <![CDATA[<p>I recently had a good use for Common Lisp’s <code class="language-plaintext highlighter-rouge">macrolet</code> special
operator. Just as <code class="language-plaintext highlighter-rouge">let</code> establishes a new variable bindings and <code class="language-plaintext highlighter-rouge">flet</code>
establishes new function bindings, <code class="language-plaintext highlighter-rouge">macrolet</code> establishes a new macro
definitions.</p>

<p>For example, here’s a locally-defined <a href="http://en.wikipedia.org/wiki/Anaphoric_macro">anaphoric</a> <code class="language-plaintext highlighter-rouge">lambda</code>
macro called <code class="language-plaintext highlighter-rouge">fn</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">macrolet</span> <span class="p">((</span><span class="nv">fn</span> <span class="p">(</span><span class="k">&amp;body</span> <span class="nv">body</span><span class="p">)</span> <span class="o">`</span><span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">_</span><span class="p">)</span> <span class="o">,@</span><span class="nv">body</span><span class="p">)))</span>
  <span class="p">(</span><span class="nb">map</span> <span class="ss">'string</span> <span class="p">(</span><span class="nv">fn</span> <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">standard-char-p</span> <span class="nv">_</span><span class="p">)</span> <span class="nv">_</span> <span class="sc">#\*</span><span class="p">))</span> <span class="s">"naïve"</span><span class="p">))</span>
<span class="c1">;; =&gt; "na*ve"</span>
</code></pre></div></div>

<p>My particular use case was about making my code cleaner for
<a href="http://redd.it/137f7h">a brainfuck interpreter</a>. The state of the machine was being
tracked by this struct. (Interesting side note: SBCL warns about using
<code class="language-plaintext highlighter-rouge">p</code> as a slot name because the accessor function will look like a
predicate.)</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defstruct</span> <span class="nv">bf</span>
  <span class="p">(</span><span class="nv">p</span> <span class="mi">0</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">mem</span> <span class="p">(</span><span class="nb">make-array</span> <span class="mi">30000</span> <span class="ss">:initial-element</span> <span class="mi">0</span><span class="p">)))</span>
</code></pre></div></div>

<p>The BF instructions <code class="language-plaintext highlighter-rouge">+</code> and <code class="language-plaintext highlighter-rouge">-</code> increment the byte at the data
pointer. The Common Lisp <code class="language-plaintext highlighter-rouge">incf</code> and <code class="language-plaintext highlighter-rouge">decf</code> macros can be used to do
this. Similarly, the <code class="language-plaintext highlighter-rouge">,</code> instruction sets the byte at the data
pointer, which can be done with <code class="language-plaintext highlighter-rouge">setf</code>. All three of these macros are
<em>place</em>-modifying.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">interp</span> <span class="p">(</span><span class="nv">program</span> <span class="nv">state</span><span class="p">)</span>
  <span class="c1">;; ...</span>
  <span class="p">(</span><span class="nb">incf</span> <span class="p">(</span><span class="nb">aref</span> <span class="p">(</span><span class="nv">bf-mem</span> <span class="nv">state</span><span class="p">)</span> <span class="p">(</span><span class="nv">bf-p</span> <span class="nv">state</span><span class="p">)))</span>
  <span class="c1">;; ...</span>
  <span class="p">(</span><span class="nb">decf</span> <span class="p">(</span><span class="nb">aref</span> <span class="p">(</span><span class="nv">bf-mem</span> <span class="nv">state</span><span class="p">)</span> <span class="p">(</span><span class="nv">bf-p</span> <span class="nv">state</span><span class="p">)))</span>
  <span class="c1">;; ...</span>
  <span class="p">(</span><span class="nb">setf</span> <span class="p">(</span><span class="nb">aref</span> <span class="p">(</span><span class="nv">bf-mem</span> <span class="nv">state</span><span class="p">)</span> <span class="p">(</span><span class="nv">bf-p</span> <span class="nv">state</span><span class="p">))</span> <span class="p">(</span><span class="nb">char-code</span> <span class="p">(</span><span class="nb">read-char</span><span class="p">))))</span>
</code></pre></div></div>

<p>That’s a whole lot of redundancy for a Lisp program. Under similar
circumstances elsewhere I might use <code class="language-plaintext highlighter-rouge">flet</code> to reduce it.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">;; This won't work.</span>
<span class="p">(</span><span class="nb">defun</span> <span class="nv">interp</span> <span class="p">(</span><span class="nv">program</span> <span class="nv">state</span><span class="p">)</span>
  <span class="p">(</span><span class="k">flet</span> <span class="p">((</span><span class="nv">ref</span> <span class="p">()</span> <span class="p">(</span><span class="nb">aref</span> <span class="p">(</span><span class="nv">bf-mem</span> <span class="nv">state</span><span class="p">)</span> <span class="p">(</span><span class="nv">bf-p</span> <span class="nv">state</span><span class="p">))))</span>
    <span class="c1">;; ...</span>
    <span class="p">(</span><span class="nb">incf</span> <span class="p">(</span><span class="nv">ref</span><span class="p">))</span>
    <span class="c1">;; ...</span>
    <span class="p">(</span><span class="nb">decf</span> <span class="p">(</span><span class="nv">ref</span><span class="p">))))</span>
</code></pre></div></div>

<p>The problem is that <code class="language-plaintext highlighter-rouge">ref</code> isn’t a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/05_aa.htm"><em>generalized reference</em></a>,
which <code class="language-plaintext highlighter-rouge">incf</code>, <code class="language-plaintext highlighter-rouge">decf</code>, and <code class="language-plaintext highlighter-rouge">setf</code> all require. Common Lisp’s
<em>place</em>-modifying utilities are implemented as macros. It’s known at
compile-time what kind of place they are modifying: a variable, array
index, object/struct slot, car, cdr, or many other things (Emacs <code class="language-plaintext highlighter-rouge">cl</code>
package allows all sorts of things to be <code class="language-plaintext highlighter-rouge">setf</code>ed, like
<code class="language-plaintext highlighter-rouge">(point)</code>). The macro expands into the proper form for setting that
kind of place.</p>

<p>The specific expansion is implementation-dependent, but, for example,
<code class="language-plaintext highlighter-rouge">setf</code> could expand into a <code class="language-plaintext highlighter-rouge">setq</code> when the first argument is a
symbol. New generalized references can be defined with <code class="language-plaintext highlighter-rouge">defsetf</code>.</p>

<p>In my case, a simple macro expansion can fill the role. Below, the
place-modifying macro will expand <code class="language-plaintext highlighter-rouge">ref</code>
(<a href="http://www.lispworks.com/documentation/lw60/CLHS/Body/05_abg.htm"><em>after</em> looking elsewhere</a>) to decide what to do, and <code class="language-plaintext highlighter-rouge">ref</code>
will expand to an <code class="language-plaintext highlighter-rouge">aref</code> form.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">interp</span> <span class="p">(</span><span class="nv">program</span> <span class="nv">state</span><span class="p">)</span>
  <span class="p">(</span><span class="k">macrolet</span> <span class="p">((</span><span class="nv">ref</span> <span class="p">()</span> <span class="o">'</span><span class="p">(</span><span class="nb">aref</span> <span class="p">(</span><span class="nv">bf-mem</span> <span class="nv">state</span><span class="p">)</span> <span class="p">(</span><span class="nv">bf-p</span> <span class="nv">state</span><span class="p">))))</span>
    <span class="c1">;; ...</span>
    <span class="p">(</span><span class="nb">incf</span> <span class="p">(</span><span class="nv">ref</span><span class="p">))</span>
    <span class="c1">;; ...</span>
    <span class="p">(</span><span class="nb">decf</span> <span class="p">(</span><span class="nv">ref</span><span class="p">))</span>
    <span class="c1">;; ...</span>
    <span class="p">(</span><span class="nb">setf</span> <span class="p">(</span><span class="nv">ref</span><span class="p">)</span> <span class="p">(</span><span class="nb">char-code</span> <span class="p">(</span><span class="nb">read-char</span><span class="p">)))))</span>
</code></pre></div></div>

<p>Because the macro has no parameters I could have even more easily used
<code class="language-plaintext highlighter-rouge">symbol-macrolet</code>. I just didn’t think of it at the time.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  <entry>
    <title>JavaScript Strings as Arrays</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/11/15/"/>
    <id>urn:uuid:4e3a5238-827b-38f0-242e-caae33f379d8</id>
    <updated>2012-11-15T00:00:00Z</updated>
    <category term="javascript"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<h3 id="lisp">Lisp</h3>

<p>One thing I enjoy about Common Lisp is its general treatment of
sequences. (In fact, I wish it went further with it!) Functions that
don’t depend on list-specific features generally work with any kind of
sequence. For example, <code class="language-plaintext highlighter-rouge">remove-duplicates</code> doesn’t just work with lists,
it works on any sequence.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">remove-duplicates</span> <span class="o">'</span><span class="p">(</span><span class="nv">a</span> <span class="nv">b</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">))</span>  <span class="c1">; list</span>
<span class="nv">=&gt;</span> <span class="p">(</span><span class="nv">A</span> <span class="nv">B</span> <span class="nv">C</span><span class="p">)</span>

<span class="p">(</span><span class="nb">remove-duplicates</span> <span class="o">#(</span><span class="nv">a</span> <span class="nv">b</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">))</span>  <span class="c1">; array</span>
<span class="nv">=&gt;</span> <span class="o">#(</span><span class="nv">A</span> <span class="nv">B</span> <span class="nv">C</span><span class="p">)</span>
</code></pre></div></div>

<p>Functions like <code class="language-plaintext highlighter-rouge">member</code> and <code class="language-plaintext highlighter-rouge">mapcar</code> require lists because their
behavior explicitly uses them. The general sequence version of these
are <code class="language-plaintext highlighter-rouge">find</code> and <code class="language-plaintext highlighter-rouge">map</code>. Writing a new sequence function means sticking
to these generic sequence functions, particularly <code class="language-plaintext highlighter-rouge">elt</code> and <code class="language-plaintext highlighter-rouge">subseq</code>
rather than the more specialized accessors.</p>

<p>A string is just a one-dimensional array — a vector — with elements
of the type <em>character</em>. This means all sequence functions also work
on strings.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">make-array</span> <span class="mi">10</span> <span class="ss">:element-type</span> <span class="ss">'character</span> <span class="ss">:initial-element</span> <span class="sc">#\a</span><span class="p">)</span>
<span class="nv">=&gt;</span> <span class="s">"aaaaaaaaaa"</span>

<span class="p">(</span><span class="nb">remove-duplicates</span> <span class="s">"abbc"</span><span class="p">)</span>
<span class="nv">=&gt;</span> <span class="s">"abc"</span>

<span class="p">(</span><span class="nb">map</span> <span class="ss">'string</span> <span class="nf">#'</span><span class="nb">char-upcase</span> <span class="s">"foo"</span><span class="p">)</span>
<span class="nv">=&gt;</span> <span class="s">"FOO"</span>

<span class="p">(</span><span class="nb">reverse</span> <span class="s">"foo"</span><span class="p">)</span>
<span class="nv">=&gt;</span> <span class="s">"oof"</span>
</code></pre></div></div>

<p>There is no special set of functions just for operating on strings
(except those for string-specific operations). Strings are as powerful
and flexible as any other sequence. This is very convenient.</p>

<h3 id="javascript">JavaScript</h3>

<p>Unfortunately, JavaScript strings aren’t <em>quite</em> arrays. They look and
act a little bit like arrays, but they’re missing a few of the useful
methods.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">foo</span> <span class="o">=</span> <span class="dl">"</span><span class="s2">abcdef</span><span class="dl">"</span><span class="p">;</span>

<span class="nx">foo</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="o">=&gt;</span> <span class="dl">"</span><span class="s2">b</span><span class="dl">"</span>

<span class="nx">foo</span><span class="p">.</span><span class="nx">length</span>
<span class="o">=&gt;</span> <span class="mi">6</span>

<span class="nx">foo</span><span class="p">.</span><span class="nx">reverse</span><span class="p">()</span>  <span class="c1">// error, no method 'reverse'</span>
</code></pre></div></div>

<p>Notice that, when indexing, it returns a one-character string, not a
single character. This is because there’s no character type in
JavaScript. It would have been interesting if JavaScript had gone the
Elisp route, where there’s no character type but instead characters
are represented by integers, with some sort of character literal for
using characters in code. This sort of thing can be emulated with the
<code class="language-plaintext highlighter-rouge">charCodeAt()</code> method.</p>

<p>To work around the strings-are-not-arrays thing, strings can be
converted to arrays with <code class="language-plaintext highlighter-rouge">split()</code>, manipulated as an array, and
restored with <code class="language-plaintext highlighter-rouge">join()</code>.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">foo</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="dl">''</span><span class="p">).</span><span class="nx">reverse</span><span class="p">().</span><span class="nx">join</span><span class="p">(</span><span class="dl">''</span><span class="p">)</span>
<span class="o">=&gt;</span> <span class="dl">"</span><span class="s2">fedcba</span><span class="dl">"</span>
</code></pre></div></div>

<p>The string method <code class="language-plaintext highlighter-rouge">replace</code> can act as a stand-in for <code class="language-plaintext highlighter-rouge">map</code> and
<code class="language-plaintext highlighter-rouge">filter</code>. The replacement argument can be a function, which will be
called on each match. If a single character at a time is selected for
replacement then what’s left is the <code class="language-plaintext highlighter-rouge">map</code> method.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Map over each character</span>
<span class="nx">foo</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/./g</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nb">String</span><span class="p">.</span><span class="nx">fromCharCode</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="mi">10</span><span class="p">);</span>
<span class="p">});</span>
<span class="o">=&gt;</span> <span class="dl">"</span><span class="s2">klmnop</span><span class="dl">"</span>
</code></pre></div></div>

<p>For <code class="language-plaintext highlighter-rouge">filter</code>, an empty string would be returned in the case of the
predicate returning <code class="language-plaintext highlighter-rouge">false</code> and the original match in the case of
<code class="language-plaintext highlighter-rouge">true</code>.</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">foo</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/./g</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="dl">"</span><span class="s2">xyeczd</span><span class="dl">"</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span>
        <span class="k">return</span> <span class="nx">c</span><span class="p">;</span>
    <span class="k">else</span>
        <span class="k">return</span> <span class="dl">''</span><span class="p">;</span>
<span class="p">});</span>
<span class="o">=&gt;</span> <span class="dl">"</span><span class="s2">cde</span><span class="dl">"</span>
</code></pre></div></div>

<p>In most cases, typical use of regular expressions would serve the need
for the <code class="language-plaintext highlighter-rouge">filter()</code> method, so this is mostly unnecessary. For example,
the above could also be done like so,</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">foo</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/</span><span class="se">[^</span><span class="sr">xyeczd</span><span class="se">]</span><span class="sr">/g</span><span class="p">,</span> <span class="dl">''</span><span class="p">);</span>
</code></pre></div></div>

<p>Another way to fix the missing methods would be to simply implement
the Array methods for strings and add them to the String prototype,
but that’s generally considered bad practice.</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Elisp Recursive Descent Parser (rdp)</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/09/20/"/>
    <id>urn:uuid:0cb87ff3-6862-3772-6d64-3222ff8e56fe</id>
    <updated>2012-09-20T00:00:00Z</updated>
    <category term="emacs"/><category term="elisp"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<p>I recently developed a recursive descent parser, named rdp, for use in
Emacs Lisp programs. I’ve already used it to write a compiler.</p>

<ul>
  <li><a href="https://github.com/skeeto/rdp">https://github.com/skeeto/rdp</a></li>
</ul>

<p>It’s available as a package on <a href="http://melpa.milkbox.net/">MELPA</a>.</p>

<h3 id="the-long-story">The Long Story</h3>

<p>Last month <a href="http://www.50ply.com/">Brian</a> invited me to take
<a href="(http://www.cs.brown.edu/courses/cs173/2012/)">a free, online programming languages course</a> with him. You
may recall that <a href="/blog/2011/01/11/">we developed a programming language
together</a> so it was only natural we would take
this class.</p>

<p>The first part of the class is oriented around a small programming
language created just for this class called <a href="http://www.cs.brown.edu/courses/cs173/2012/Assignments/ParselTest/">ParselTongue</a>.  It
looks like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>deffun evenp(x)
    if ==(x, 0) then
        true
    else if ==(x, 1) then
            false
        else evenp(-(x, 2))
in defvar x = 14 in {
    while (evenp(x)) { x--; };   # Make sure x odd
    print("This is an odd number: ");
    print(x);
    ""; # No output
}
</code></pre></div></div>

<p>I’ve gotten so used to having a solid Emacs major mode when coding
that I can’t stand writing code without the support of a major
mode. Since this language was invented recently <em>just</em> for this class
there was no mode for it, nor would there be unless someone stepped up
to make one. I ended up taking that role. It was an opportunity to
learn how to create a major mode, something I had never done before.</p>

<p>It’s called <a href="https://github.com/skeeto/psl-mode">psl-mode</a>.</p>

<p>At first it was just some syntax highlighting (very easy) and some
poor automatic indentation. The indentation function would get
confused by anything non-trivial. It’s actually <em>really</em> hard to get
it right. I’ve grown a much better appreciation for automatic
indentation in other modes.</p>

<p>In an attempt to improve this I decided I would try to fully parse the
language and use the resulting parse tree to determine indentation —
something like the depth of the pointer in the
tree. <a href="/blog/2009/01/04/">My experience with Perl’s Parse::RecDescent</a>
some years ago was very positive and I wanted to reproduce that
effect. However, rather than write the grammar in a separate language
that mixes in the programming language, which I find extremely messy,
instead I wanted to use pure s-expressions. A grammar looks very nice
as an alist of symbols.</p>

<h4 id="arithmetic-parser">Arithmetic Parser</h4>

<p>For example, here’s a grammar for simple arithmetic expressions,
including operator precedence and grouping (i.e. “4 + 5 * 2.5”,
“(4 + 5) * 2.5”, etc.).</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defvar</span> <span class="nv">arith-tokens</span>
  <span class="o">'</span><span class="p">((</span><span class="nv">sum</span>       <span class="nv">prod</span>  <span class="nv">[</span><span class="p">(</span><span class="nv">[+</span> <span class="nv">-]</span> <span class="nv">sum</span><span class="p">)</span>  <span class="nv">no-sum]</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">prod</span>      <span class="nv">value</span> <span class="nv">[</span><span class="p">(</span><span class="nv">[*</span> <span class="nv">/]</span> <span class="nv">prod</span><span class="p">)</span> <span class="nv">no-prod]</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">num</span>     <span class="o">.</span> <span class="s">"-?[0-9]+\\(\\.[0-9]*\\)?"</span><span class="p">)</span>
    <span class="p">(</span><span class="nb">+</span>       <span class="o">.</span> <span class="s">"\\+"</span><span class="p">)</span>
    <span class="p">(</span><span class="nb">-</span>       <span class="o">.</span> <span class="s">"-"</span><span class="p">)</span>
    <span class="p">(</span><span class="nb">*</span>       <span class="o">.</span> <span class="s">"\\*"</span><span class="p">)</span>
    <span class="p">(</span><span class="nb">/</span>       <span class="o">.</span> <span class="s">"/"</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">pexpr</span>     <span class="s">"("</span> <span class="nv">[sum</span> <span class="nv">prod</span> <span class="nv">num</span> <span class="nv">pexpr]</span> <span class="s">")"</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">value</span>   <span class="o">.</span> <span class="nv">[pexpr</span> <span class="nv">num]</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">no-prod</span> <span class="o">.</span> <span class="s">""</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">no-sum</span>  <span class="o">.</span> <span class="s">""</span><span class="p">)))</span>
</code></pre></div></div>

<p>Strings are regular expressions , the only thing to actually match
input text (<em>terminals</em>). Lists are <em>sequences</em>, where each element in
the list must match in order. Vectors (in brackets) are <em>choices</em>
where one of the elements must match. Symbols name an expression so
that it can be referred to by other expression recursively.</p>

<p>Give this alist to the parser and it will return an s-expression of
the parse tree of the current buffer. Due to the way the grammar must
be written this parse tree isn’t really pleasant to handle
directly. For example, a series of multiplications (“1 * 2 * 3 * 4”)
wouldn’t parse to a nice flat list but with further depth for each
additional operand.</p>

<p>To help squash these, the parser will accept an alist of symbols and
functions which process the parse tree at parse time. For example,
these corresponding functions will make sure <code class="language-plaintext highlighter-rouge">"4 * 5 * 6"</code> gets parsed
into <code class="language-plaintext highlighter-rouge">(* 4 (* 5 (* 6 1)))</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">arith-op</span> <span class="p">(</span><span class="nv">expr</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">destructuring-bind</span> <span class="p">(</span><span class="nv">a</span> <span class="p">(</span><span class="nv">op</span> <span class="nv">b</span><span class="p">))</span> <span class="nv">expr</span>
    <span class="p">(</span><span class="nb">list</span> <span class="nv">op</span> <span class="nv">a</span> <span class="nv">b</span><span class="p">)))</span>

<span class="p">(</span><span class="nb">defvar</span> <span class="nv">arith-funcs</span>
  <span class="o">`</span><span class="p">((</span><span class="nv">sum</span>     <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nv">arith-op</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">prod</span>    <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nv">arith-op</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">num</span>     <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nv">string-to-number</span><span class="p">)</span>
    <span class="p">(</span><span class="nb">+</span>       <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nb">intern</span><span class="p">)</span>
    <span class="p">(</span><span class="nb">-</span>       <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nb">intern</span><span class="p">)</span>
    <span class="p">(</span><span class="nb">*</span>       <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nb">intern</span><span class="p">)</span>
    <span class="p">(</span><span class="nb">/</span>       <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nb">intern</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">pexpr</span>   <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nb">cadr</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">value</span>   <span class="o">.</span> <span class="o">,</span><span class="nf">#'</span><span class="nb">identity</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">no-prod</span> <span class="o">.</span> <span class="o">,</span><span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">e</span><span class="p">)</span> <span class="o">'</span><span class="p">(</span><span class="nb">*</span> <span class="mi">1</span><span class="p">)))</span>
    <span class="p">(</span><span class="nv">no-sum</span>  <span class="o">.</span> <span class="o">,</span><span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">e</span><span class="p">)</span> <span class="o">'</span><span class="p">(</span><span class="nb">+</span> <span class="mi">0</span><span class="p">)))))</span>
</code></pre></div></div>

<p>Notice how normal Emacs functions could be supplied directly in most
cases! That makes this approach so elegant in my opinion.</p>

<p>Also, in <code class="language-plaintext highlighter-rouge">arith-op</code> note the use of <code class="language-plaintext highlighter-rouge">destructuring-bind</code>. I’ve found
that macro to be invaluable when writing these syntax tree functions.</p>

<p>In this case, we can be even more clever. Rather than build a nice
parse tree, the expression can be evaluated directly. All it takes is
one small change,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">arith-op</span> <span class="p">(</span><span class="nv">expr</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">destructuring-bind</span> <span class="p">(</span><span class="nv">a</span> <span class="p">(</span><span class="nv">op</span> <span class="nv">b</span><span class="p">))</span> <span class="nv">expr</span>
    <span class="p">(</span><span class="nb">funcall</span> <span class="nv">op</span> <span class="nv">a</span> <span class="nv">b</span><span class="p">)))</span>
</code></pre></div></div>

<p>With this, the parser returns the computed value directly. So this
evaluates to 120.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">rdp-parse-string</span> <span class="s">"4 * 5 * 6"</span> <span class="nv">arith-tokens</span> <span class="nv">arith-funcs</span><span class="p">)</span>
</code></pre></div></div>

<h4 id="parseltongue-compiler">ParselTongue Compiler</h4>

<p>I discovered this useful side effect while making my ParselTongue
parser. The original intention was that I’d parse the buffer for use
in indentation, then maybe I’d create an interpreter to evaluate the
parser output. However, the resulting parse tree was looking a lot
like Elisp. In an epiphany I realized I could simply emit valid Elisp
directly and forgo writing the interpreter altogether. And so I
accidentally created a ParselTongue compiler! This was incredibly
exciting for me to realize.</p>

<p>This ParselTongue program,</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>defvar obj = {x: 1} in { obj.x }
</code></pre></div></div>

<p>Compiles to this Elisp,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">obj</span> <span class="p">(</span><span class="nb">list</span> <span class="p">(</span><span class="nb">cons</span> <span class="ss">'x</span> <span class="mi">1</span><span class="p">))))</span>
  <span class="p">(</span><span class="k">progn</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nv">assq</span> <span class="ss">'x</span> <span class="nv">obj</span><span class="p">))))</span>
</code></pre></div></div>

<p>Because it compiles to such a high level language, and because
ParselTongue is very Lisp-like semantically, it’s a bit
unconventional: the compiler emits code <em>during</em> parsing. In fact,
when the parser backtracks, some emitted code is thrown away.</p>

<p>By the end of the first evening I had implemented the majority of the
compiler, which quickly took precedence over indentation. The compiler
is now integrated as part of psl-mode. The current buffer can be
evaluated at any time with <code class="language-plaintext highlighter-rouge">psl-eval-buffer</code>. This function compiles
the buffer and has Emacs <code class="language-plaintext highlighter-rouge">eval</code> the result, printing the output in the
minibuffer. Compiler output can be viewed with
<code class="language-plaintext highlighter-rouge">psl-show-elisp-compilation</code> (mostly for my own debugging).</p>

<p>After a few days I integrated indentation with parsing, which required
modifying the parser (changes included in rdp itself). The parser
needed to keep track of where the point is in the parse tree. For
indentation it basically counts the depth into the parse tree, plus a
few more checks for special cases.</p>

<p>The parser was intentionally isolated from the rest of psl-mode so
that it could be separated for general use, which I have now
done. It’s been a <em>really</em> handy general purpose tool since then. That
arithmetic parser is only 35 lines of code and took about half-an-hour
to create.</p>

<h4 id="future-directions">Future Directions</h4>

<p>I also <a href="(https://github.com/skeeto/emacs-torrent/blob/master/bencode.el)">wrote a bencode parser</a> — <em>only</em> the
<code class="language-plaintext highlighter-rouge">bencode-tokens</code> and <code class="language-plaintext highlighter-rouge">bencode-funcs</code> alists are needed to parse
bencode, about 30 LOC. Careful observation will reveal that I cheated
and the result is a little hackish. Due to the way strings work,
bencode is <em>not</em> context-free so it can’t be parsed purely by the
grammar. I can work around it by having the parse tree function for
strings consume input, since it’s called during parsing.</p>

<p>I’ll be using rdp to parse many more things in the future, I’m
sure. It’s much more powerful than I expected.</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Implemented Is Simple Data Compression</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/09/04/"/>
    <id>urn:uuid:2dd8265c-e498-333f-7f07-4fd93d873975</id>
    <updated>2012-09-04T00:00:00Z</updated>
    <category term="emacs"/><category term="lisp"/><category term="ai"/>
    <content type="html">
      <![CDATA[<p><em>Update</em>: This post shouldn’t make sense to anyone
(hopefully). <a href="/blog/2012/09/05/">Read the follow-up</a> for an
explanation.</p>

<hr />

<p>When a branch of my posts remains simple.</p>

<p>This is necessary when one will assume Alan is more important than
number 12. By using numbers to repeat them, but this won’t work with
any sort of thing you want to load what’s needed. This includes
reimplementing the reader as it seems you still need to specify any
video-specific parameters, <code class="language-plaintext highlighter-rouge">ppmtoy4m</code> is the whole thing is just that,
decorated with some tips on how the current space as visited, then
recurse from the client to read a great story, I recommend you use to
launch a daemon process and prints the variable information to
stdout. As an added bonus, when a second variable for accumulation and
a second argument is relevant.</p>

<p>Suppose you want to read a great story, I recommend it.</p>

<p>This servlet uses the Term::ProgressBar, if it’s any good, but it’s
funny. As anyone with cats knows, it’s not <em>too</em> stupid to call
<code class="language-plaintext highlighter-rouge">fsync()</code> to force the write to the snapshot and uninterns any new
symbols. These symbols will be added to the the second experiment.</p>

<p>At this line, you can perform a number from a couple of these and give
them back any other language that can turn out even from a large
header comment in the logs, so getting someone into my honeypot
wouldn’t take long at all. The only proof I could then
cherry-pick/pull the issues from that repository and see the
polynomial interpolation at that time, presented in order. This makes
so much of web development (I think that’s his name). I am an Emacs
person myself, which I use branches all the time, now that they can be
written.</p>

<p>We will run your build system in a web front-end to it, and made a
couple of seconds.</p>

<p>You should also be a good head start, though. The SPARC is big-endian
and the results to seed their program accordingly. You could do this
is by mounting the compromised filesystem in a list. In the
decentralized model, everyone has their own solutions in parallel when
it comes across 10 it emits 0.</p>

<p>Here’s an example of some of the fire gem activated and exploded,
causing no blindness to me. They take a look at the same level as the
printed string. You can grab my source code in response to abuse by
spammers who hide fraudulent URLs behind shortened ones. If these
services ever went down all at once, these shortened URLs would rot,
destroying many of the image, with the FFI.</p>

<p>Because I wrote a shell script that will also remove the execs and
live with nested shells because the zeros cancel out everything else?
Here is the protocol.</p>

<p>Generate a 10-byte random IV. This need not implement this.</p>

<p>Note that the shell script, and the arcfour key scheduler at least n
days.</p>

<p>However, generating a series of commits to all other encounters
nothing changes.</p>

<p>Your program should simulate this by having the user to reseed
somewhere. There’s no direct way to install it to dominate for
awhile. It is strange that Matlab itself doesn’t have any sort of
syntax highlighting. Boring! I finally ran into this image. After each
paste, make a saving throw to prevent an explosion.</p>

<p>Because Gnohkk would also suffer from the bottom are arranged around
the cats in the logs, so getting someone into my honeypot wouldn’t
take long at the link in the block. Another was going to used a
stationary magnet.</p>

<p>Our team went with this array (and replaced the current layer 5). Now,
duplicate the work was done just once by freeing the entire number, it
can perform both compression and decompression on both sides don’t pay
attention to the development loop is just an ordered list of 50 H’s
and T’s. If you implement this in the same time. This is along the
way, clone my repository right into the official website so I had to
do this for any long-blocking function that I use <code class="language-plaintext highlighter-rouge">ppmtoy4m</code> to pipe
the new frames to keep, such as n^p mod M, which this will handle
efficiently. For example, to add a new compression algorithm in terms
of brute-force attacks it requires using numbers long enough to fit
three Emacs’ windows side-by-side at 78 columns each. The leftmost one
contains my active work buffer where I do most useful things, a fresh
array every time it sees a free musical.  Unfortunately, my writing
skills are even worse. I have gotten good mileage out of a file based
on their website demonstrating how to increment the iterator. I have
to type a negative comment about zip archives and moved on. I <em>am</em>
using a constant amount of memory.</p>

<p>It turns out that everyone is free to share his source code samples,
particularly more recent entries, was that producing the relief
surface was an e-mail address, I get home from work I don’t recommend
doing this with secret Java applets.</p>

<p>There are a few weeks since I last used KOffice, so I could easily
plug it into Emacs and run the test above, I would rather not do
damage, but rather a patient human being. Getting tired of manually
synchronizing them. It was finally time to document the effort as a
single mine is destroyed, the neighboring mines will replicate a
replacement. The minefield itself could therefore hold no secrets
whatsoever. This leaves out any possibility of a rumor among a group
of people. At any given time, each person in the background. My shell
habits looked like the ones you’re seeing after <code class="language-plaintext highlighter-rouge">end-package</code>.</p>

<p>It’s really simple way to detect edges all over the weekend I came up
with some rough edges. So I got it right while IE, Opera, Safari, and
Chrome all do it again.</p>

<p>Numbers can be found inside the fake closure provided by
lexical-let. In a previous post about Lua, another about a third of my
name generation code.</p>

<p>S-expressions are handy anywhere.</p>

<p>Two months ago I was so happy when I run the program with the proper
Perl regular expression contains quotes and these will not be worth
it.</p>

<p>I can’t help but think that a knight moving according to the current
symbol table to the existing mountain of elisp code out there,
requiring a massive increase in speed when using OpenCL. In fact,
there is virtually no computation involved. So what I want to look
like SBCL. Fortunately, that’s not all!!! There is a fake service or
computer on a chess board such that it’s somewhat easier to tell when
the handler can present any contents it wants. In this case, rather
than just one, even though I don’t know what it looks good, except you
want to italicize a few bits smaller than a minute. All the other day
I will probably be ordered by their own directory. Modern applications
have moved into a directory under <code class="language-plaintext highlighter-rouge">~/.config/</code>. Your script needs to
be broken into small computation units, because Emacs lacked network
functionality until recently was the package manager, <code class="language-plaintext highlighter-rouge">package</code>, and
the Emacs Lisp Package Archive.</p>

<p>One of the info field in the list, which sounds like a .emacs file in
your program. If the slot is already taken, the symbol was in an
external system.</p>

<p>After all this, I thought I’d give it a YouTube URL and a single
password if the required artifacts, digitally signs them, and bundles
them up.</p>

<p>The demo at the same length as the variable declarations are exactly
the right magical string of, say, 31 fractions.</p>

<p>The story is really happening. Optimizing away variables that point to
it.</p>

<p>Oh, and I was just a tiny subset of the memory at once became a lot of
memory. For example, here’s my laptop’s /bin/ls, very roughly
labeled.</p>

<p>The different segments of the game area was a mistake on my rolls and
had some wires, connected to some sort of bad things this may happen
subconsciously, which is given in ImageMagick’s montage tool, which
made the final montage out of the image functions described below.</p>

<p>You can write a lexer or tokenizer without one. Because of this tool,
Samuel Stoddard, gives some in-game context to the light of day. I
just use your own program, the script in your load-path somewhere.</p>

<p>I’ve frequently thought that a Lisp-based shell would be produced by
first individually gzipping each file in first.</p>

<p>For a long ways away from a simple double-click shortcut. If you just
want to duplicate the remaining canines. Her reward for victory was a
very similar process, but without any sort of thing is
transparent. I’ve already used it with a degree in, say, a few
months. I’ve used POSIX threads, Pthreads, before, so it suits my
needs for the first two arguments from filter2, as well as some more
to see my changes, but I don’t know much about it, user AJR spoiled it
with ssh-add and it queries for your passphrase, storing it in two
obarrays at once, these shortened URLs would rot, destroying many of
its input. For example, this is what registration looks like,</p>

<p>Unfortunately, the HTML output is a Harsh Mistress. If you know that
the opposite way that the adventures and characters are riddled with
mistakes and very unbalanced. For an easier way to set up properly in
your configuration.</p>

<p>I strongly recommend that you generally want to have a master pad, K,
that you often generate very improbable series of commits.</p>

<p>To all other encounters nothing changes.</p>

<p>And that’s it! I put this line in your program. If you are subscribed
to the rescue!</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Literal Arrays and Vectors in Lisp</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/07/17/"/>
    <id>urn:uuid:71e44f9e-2e92-30e9-8b29-8418229a7ce1</id>
    <updated>2012-07-17T00:00:00Z</updated>
    <category term="lisp"/><category term="elisp"/>
    <content type="html">
      <![CDATA[<p>Despite being a Lisper, Unlike <a href="http://www.50ply.com/">Brian</a> I
haven’t gotten into Clojure yet. I’ve been following along at a safe
distance. Due to
<a href="http://www.50ply.com/blog/2012/07/06/asynchronous-sequential-code-shape/">a recent post of his</a>
I learned about a significant difference between Clojure and other
Lisps when it comes to arrays/vectors.</p>

<p>In this recent post, Brian wrote a ClojureScript <code class="language-plaintext highlighter-rouge">let</code>-like macro to
hide JavaScript asynchronous function chains so that they can be used
just like regular synchronous functions. Follow Clojure’s style, the
asynchronous functions are written inside a vector rather than a list
to indicate to the macro that they’re special.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nf">doasync</span><span class="w">
  </span><span class="p">[</span><span class="n">text</span><span class="w"> </span><span class="p">[</span><span class="n">fetch</span><span class="w"> </span><span class="s">"/foo/json"</span><span class="p">]</span><span class="w">
   </span><span class="n">url</span><span class="w"> </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="n">text</span><span class="w"> </span><span class="s">".html"</span><span class="p">)</span><span class="w">
   </span><span class="n">result</span><span class="w"> </span><span class="p">[</span><span class="n">fetch</span><span class="w"> </span><span class="n">url</span><span class="p">]</span><span class="w">
   </span><span class="n">_</span><span class="w"> </span><span class="p">(</span><span class="nf">.show</span><span class="w"> </span><span class="n">view</span><span class="w"> </span><span class="n">result</span><span class="p">)</span><span class="w">
   </span><span class="n">_</span><span class="w"> </span><span class="p">[</span><span class="n">timeout</span><span class="w"> </span><span class="mi">1000</span><span class="p">]</span><span class="w">
   </span><span class="n">_</span><span class="w"> </span><span class="p">(</span><span class="nf">.makeEditable</span><span class="w"> </span><span class="n">view</span><span class="p">)])</span><span class="w">
</span></code></pre></div></div>

<p>That sounded completely reasonable to me, since array literals are
rarely used inside code Common Lisp. When they are used, it’s as a
global constant.</p>

<p>A few days later when I was talking to Brian at the metaphorical water
cooler he mentioned that the macro was actually conflicting with what
he would normally write. Sometimes he really did want to use a vector
literal in a <code class="language-plaintext highlighter-rouge">let</code> binding. Why would he do that? In Common Lisp,
that’s just asking for trouble — same for Elisp and Scheme.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">v</span> <span class="o">#(</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)))</span>
  <span class="p">(</span><span class="nv">foo</span> <span class="nv">v</span><span class="p">))</span>
</code></pre></div></div>

<p>The reason why this is a bad idea is that the <em>same exact</em> array will
always be passed to <code class="language-plaintext highlighter-rouge">foo</code>. The array is created once at <em>read time</em> by
the reader and re-used for the life of that code. If anyone makes a
modification to the array it will damage the array for everyone using
it.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">()</span>
  <span class="o">#(</span><span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">))</span>
<span class="p">(</span><span class="nb">eq</span> <span class="p">(</span><span class="nv">foo</span><span class="p">)</span> <span class="p">(</span><span class="nv">foo</span><span class="p">))</span>
<span class="nv">=&gt;</span> <span class="nv">T</span>
</code></pre></div></div>

<p>The safer method is to create a fresh array every time by <em>not</em> using
a literal but instead calling <code class="language-plaintext highlighter-rouge">vector</code>.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">v</span> <span class="p">(</span><span class="nb">vector</span> <span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)))</span>
  <span class="p">(</span><span class="nv">foo</span> <span class="nv">v</span><span class="p">))</span>
</code></pre></div></div>

<p>Clojure data structures are immutable, including vectors, so using the
same exact vector in multiple places is safe. That makes use literal
vectors in code less awkward. But that still left a question hanging:
why was Brian using literal vectors so often that he needed one so
soon after writing this macro?</p>

<p>In Common Lisp, they’re not very useful because the elements are not
evaluated by the parser. When this vector is evaluated the result is a
vector where the second element is a list containing three atoms.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">#(</span><span class="mi">1</span> <span class="p">(</span><span class="nb">+</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)</span> <span class="mi">4</span><span class="p">)</span>
<span class="nv">=&gt;</span> <span class="o">#(</span><span class="mi">1</span> <span class="p">(</span><span class="nb">+</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)</span> <span class="mi">4</span><span class="p">)</span>
</code></pre></div></div>

<p>Evaluated arrays return themselves unchanged. To do most useful
things, a fresh vector needs to be constructed piecemeal. If somehow
the uniqueness of a literal array wasn’t an issue, they still couldn’t
be used for much.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
  <span class="o">#(</span><span class="nv">x</span> <span class="nv">x</span> <span class="nv">x</span><span class="p">))</span>
<span class="p">(</span><span class="nv">foo</span> <span class="mi">10</span><span class="p">)</span>
<span class="nv">=&gt;</span> <span class="o">#(</span><span class="nv">X</span> <span class="nv">X</span> <span class="nv">X</span><span class="p">)</span>
</code></pre></div></div>

<p>To achieve the desired effect, the <code class="language-plaintext highlighter-rouge">vector</code> function needs to be used
again. Because it’s a normal function call, the arguments are
evaluated.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">foo</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">vector</span> <span class="nv">x</span> <span class="nv">x</span> <span class="nv">x</span><span class="p">))</span>
<span class="p">(</span><span class="nv">foo</span> <span class="mi">10</span><span class="p">)</span>
<span class="nv">=&gt;</span> <span class="o">#(</span><span class="mi">10</span> <span class="mi">10</span> <span class="mi">10</span><span class="p">)</span>
</code></pre></div></div>

<p>However, to my surprise, Clojure doesn’t work like this! Literal
vectors have their elements evaluated and, if necessary, are created
fresh on every use — exactly like a call to <code class="language-plaintext highlighter-rouge">vector</code>.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="w">
  </span><span class="p">[</span><span class="n">x</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">x</span><span class="p">])</span><span class="w">
</span><span class="p">(</span><span class="nf">foo</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w">
</span><span class="n">=&gt;</span><span class="w"> </span><span class="p">[</span><span class="mi">10</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="mi">10</span><span class="p">]</span><span class="w">
</span><span class="p">(</span><span class="nb">identical?</span><span class="w"> </span><span class="p">(</span><span class="nf">foo</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nf">foo</span><span class="w"> </span><span class="mi">10</span><span class="p">))</span><span class="w">
</span><span class="n">=&gt;</span><span class="w"> </span><span class="n">false</span><span class="w">
</span></code></pre></div></div>

<p>If the exact form of the vector is needed unevaluated, it needs to be
quoted just like lists.</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="w">
  </span><span class="o">'</span><span class="p">[</span><span class="n">x</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">x</span><span class="p">])</span><span class="w">
</span><span class="p">(</span><span class="nf">foo</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w">
</span><span class="n">=&gt;</span><span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">x</span><span class="p">]</span><span class="w">
</span><span class="p">(</span><span class="nb">identical?</span><span class="w"> </span><span class="p">(</span><span class="nf">foo</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nf">foo</span><span class="w"> </span><span class="mi">10</span><span class="p">))</span><span class="w">
</span><span class="n">=&gt;</span><span class="w"> </span><span class="n">true</span><span class="w">
</span></code></pre></div></div>

<p>After further reflection, I now feel like this is the <em>right</em> way to
go about implementing vectors. When I was first learning Lisp the
non-evaluating nature of arrays really caught me by surprise. Vectors
should evaluate their elements by default; if the Common Lisp behavior
is needed it can always be quoted. It’s impossible to “fix” any
established Lisp of course, so I’m merely wishing this was the
behavior defined decades ago.</p>

<p>To recap: normally in Lisp, vectors evaluate to themselves, like
numbers and strings. Instead, evaluation of a vector should return a
<em>new</em> vector containing the results of each of the element
evaluated. Since Clojure’s data structures are immutable, the compiler
can take a shortcut when it can guarantee each of a vector’s elements
always evaluate to themselves, and have the vector evaluate to itself
— purely as an optimization.</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Lisp Let in GNU Octave</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/02/08/"/>
    <id>urn:uuid:05e5318e-0cf4-3d80-4bf5-da695dbe9e47</id>
    <updated>2012-02-08T00:00:00Z</updated>
    <category term="octave"/><category term="trick"/><category term="lisp"/><category term="media"/><category term="math"/><category term="video"/>
    <content type="html">
      <![CDATA[<p>In <a href="/blog/2011/01/30/">BrianScheme</a>, the standard Lisp binding form <code class="language-plaintext highlighter-rouge">let</code> isn’t a
special form. That is, it’s not a hard-coded language feature, or
<em>special form</em>. It’s built on top of <code class="language-plaintext highlighter-rouge">lambda</code>. In any lexically-scoped
Lisp, the expression,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nv">x</span> <span class="mi">10</span><span class="p">)</span>
      <span class="p">(</span><span class="nv">y</span> <span class="mi">20</span><span class="p">))</span>
  <span class="p">(</span><span class="nb">*</span> <span class="mi">10</span> <span class="mi">20</span><span class="p">))</span>
</code></pre></div></div>

<p>Can also be written as,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">((</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span> <span class="nv">y</span><span class="p">)</span>
   <span class="p">(</span><span class="nb">*</span> <span class="nv">x</span> <span class="nv">y</span><span class="p">))</span>
 <span class="mi">10</span> <span class="mi">20</span><span class="p">)</span>
</code></pre></div></div>

<p>BrianScheme’s <code class="language-plaintext highlighter-rouge">let</code> is just a macro that transforms into a lambda
expression. This is also what made it so important to implement lambda
lifting, to optimize these otherwise-expensive forms.</p>

<p>It’s possible to achieve a similar effect in GNU Octave (but not
Matlab, due to <a href="/blog/2008/08/29/">its flawed parser design</a>). The language permits
simple lambda expressions, much like Python.</p>

<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">&gt;</span> <span class="n">f</span> <span class="o">=</span> <span class="o">@</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">10</span><span class="p">;</span>
<span class="o">&gt;</span> <span class="n">f</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="nb">ans</span> <span class="o">=</span> <span class="mi">14</span>
</code></pre></div></div>

<p>It can be used to create a scope in a language that’s mostly devoid of
scope. For example, I can avoid assigning a value to a temporary
variable just because I need to use it in two places. This one-liner
generates a random 3D unit vector.</p>

<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="o">@</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="n">v</span> <span class="p">/</span> <span class="nb">norm</span><span class="p">(</span><span class="n">v</span><span class="p">))(</span><span class="nb">randn</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
</code></pre></div></div>

<p>The anonymous function is called inside the same expression where it’s
created. In practice, doing this is stupid. It’s confusing and there’s
really nothing to gain by being clever, doing it in one line instead
of two. Most importantly, there’s no macro system that can turn this
into a new language feature. <em>However</em>, I enjoyed using this technique
to create a one-liner that generates <code class="language-plaintext highlighter-rouge">n</code> random unit vectors.</p>

<div class="language-matlab highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">n</span> <span class="o">=</span> <span class="mi">1000</span><span class="p">;</span>
<span class="n">p</span> <span class="o">=</span> <span class="p">(</span><span class="o">@</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="n">v</span> <span class="o">.</span><span class="p">/</span> <span class="nb">repmat</span><span class="p">(</span><span class="nb">sqrt</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">.^</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)),</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">))(</span><span class="nb">randn</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">3</span><span class="p">));</span>
</code></pre></div></div>

<p>Why was I doing this? I was using the Monte Carlo method to
double-check my solution to <a href="http://godplaysdice.blogspot.com/2011/12/geometric-probability-problem.html">this math problem</a>:</p>

<blockquote>
  <p>What is the average straight line distance between two points on a
sphere of radius 1?</p>
</blockquote>

<p>I was also demonstrating to <a href="http://devrand.org/">Gavin</a> that simply choosing two
<em>angles</em> is insufficient, because the points the angles select are not
evenly distributed over the surface of the sphere. I generated this
video, where the poles are clearly visible due to the uneven selection
by two angles.</p>

<video src="https://s3.amazonaws.com/nullprogram/sphere/sphere-dark.webm" controls="controls" height="340" width="340">
</video>

<p>This took hours to render with gnuplot! Here are stylized versions:
<a href="https://s3.amazonaws.com/nullprogram/sphere/dark.html">Dark</a> and <a href="https://s3.amazonaws.com/nullprogram/sphere/light.html">Light</a>.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>BrianScheme Update: Bootstrapping and Images</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2011/01/30/"/>
    <id>urn:uuid:abab3588-769a-31d4-16db-9eaa33791e48</id>
    <updated>2011-01-30T00:00:00Z</updated>
    <category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 30 January 2011 -->
<p>
I <a href="/blog/2011/01/11/">previously talked about BrianScheme</a>
(BS) and we've had some exciting updates since then. I've since caught
up with Brian so we're committing code at about the same rate. It's
really coming together to be something bigger than we both
expected. We're adding what we feel is the best of Common List and
Scheme. The Git repository log reveals just how much time we've spent
hacking at this.
</p>
<p>
The first big milestone was full bootstrapping. BS has both an
interpreter, written in C, and a compiler written in BS. The compiler
targets a VM, which is written in C. The interpreter is no longer used
directly; it's there simply for bootstrapping the compiler. It sets up
an initial environment, then loads and runs the compiler which
immediately compiles itself, and then compiles its environment, and
then compiles the main user environment. Afterward the whole thing is
lifted up on top of the VM and the interpreter is abandonded.
</p>
<p>
Once everything was bootstrapped and running in the VM, continuations
became a practical possibility, and Brian soon added them. So now
BrianScheme now has all the major components of a Scheme.
</p>
<p>
I began ramping up my contributions by adding a really solid random
number generator,
the <a href="http://en.wikipedia.org/wiki/Mersenne_twister"> Mersenne
twister</a>, and providing functions to generate numbers on all sorts
of distributions: normal, Poisson, gamma, exponential, beta, and
Chi-squared. It's pretty reasonable at seeding itself, too.
</p>
<p>
In the meantime, this bootstrap process, while incredibly useful, was
<i>really</i> slowing down development. It was taking BS about 10
seconds to boot itself every time it was started. That can really kill
the usefulness of the system. I started to look into ways to mitigate
this, perhaps through FASLs or some kind of image dump.
</p>
<p>
After discussing it with Brian I decided to try for a memory dump,
<a href="http://www.sbcl.org/manual/Saving-a-Core-Image.html">
SBCL-style</a>. My old memory pool
allocator, which I thought I've never use again, really came in
handy, and now has a home — modified of course — in BS. It no longer
uses malloc(), instead using mmap() to get more memory, and now has
the ability to free() memory, completely replacing malloc(),
realloc(), and free(). So with BrianScheme no longer use the libc
allocator we had complete control over the program's memory. It was
just a matter of dumping the handful of big mmap() chunks to disk, and
in another process, loading them back in to the same location, and
finally hooking the environment back up.
</p>
<p>
Just as the SBCL documentation warns about, there are complicated
issues still to be resolved (and may never be). The two big ones are
alien objects and open file handles, neither of which can make it into
the image. Aliens could be in there potentially, if the foreign
library let us select its allocator. Brian made a change that gives
the FFI a hook to rebind its symbols after load, so <i>some</i> of the
FFI can survive the jump. The three stdin, stdout, and stderr file
handles are reconnected on load, but the old, dead handles can
potentially linger in places, waiting to cause errors when someone
tries to read them.
</p>
<p>
It was really, really exciting to see the images come back to life for
the first time. With that success the lengthy bootstrap process was no
longer a big problem because it could be bypassed much of the
time. Saving images is really simple to do, too.
</p>
<pre>
(save-image "brianscheme.img")
</pre>
<p>
This will create the image "brianscheme.img", which can be loaded
again later with the <code>-l</code> switch. Once loaded, it will
either execute a script if given one on the command line, or it will
provide a new REPL. Because the image is <code>mmapp()</code>ed into
place, loading is practically instantaneous, even if the image is
dozens of megabytes (which can happen easily).
</p>
<pre>
bsch -l brianscheme.img
</pre>
<p>
The BS image in its booted state is about 15MB right now on 64-bit
systems, and 7MB on 32-bit systems. They are low entropy and compress
down to about 2MB, so it's not <i>too</i> bad. If you write a large
program in BrianScheme and save it as an image it may be even larger.
</p>
<p>
Over the weekend I took this even further, continuing to follow in the
footsteps of SBCL: I added a feature to wrap the image in the BS
executable, so that the image itself is a standalone executable. To
make this more useful, a toplevel function can be selected to run
after the image loads, rather than a REPL. If you wrote a game in BS
and wanted to compile to a standalone program,
</p>
<pre>
(load "my-game.sch")
(save-image "my-game" 'executable #t 'toplevel play-my-game)
</pre>
<p>
The user would execute the file <code>my-game</code>, which would load
BS and run the function <code>play-my-game</code>. Because a 15MB
binary is a little unwieldy to hand out, you could compress it
beforehand with a tool like <code>gzexe</code>, which transparently
compresses an executable.
</p>
<p>
The wrapper is actually very simple. It's a very slightly modified BS
executable, padded out to the system's page size (4kB, generally),
ending with a special marker. The image is concatenated to this
binary. When run, the program scans itself looking for the marker
(<code>0xdeadbeef</code>), and then mmap()s the portion behind it (you
can only mmap() page-size offsets, which is why padding was
necessary).
</p>
<p>
BrianScheme should have an interesting future ahead of it.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  <entry>
    <title>BrianScheme</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2011/01/11/"/>
    <id>urn:uuid:6d2dc892-9e47-3b94-dd9b-a1027340fef9</id>
    <updated>2011-01-11T00:00:00Z</updated>
    <category term="lisp"/><category term="lang"/>
    <content type="html">
      <![CDATA[<!-- 11 January 2011 -->
<p>
Remember back a year ago <a href="/blog/2010/01/24/">I tried my hand
at a Lisp implementation called Wisp</a>? Well, currently a co-worker
of mine, <a href="http://wubo.org/">Brian Taylor</a>, is similarly
working on his own Scheme implementation — but he knows more about
what he's doing than I did, so it's more interesting. However, that
expertise doesn't extend to inventing a clever name (Zing!): it's
unsubtly called <a href="https://github.com/netguy204/brianscheme">
BrianScheme</a>.
</p>
<pre>
git clone git://github.com/netguy204/brianscheme.git
</pre>
<p>
I've been <a href="https://github.com/skeeto/brianscheme">
hacking at it a little myself</a>, cheering from the sidelines.
</p>
<pre>
git remote add wellons git://github.com/skeeto/brianscheme.git
</pre>
<p>
Like Wisp, it's written from scratch in C from the bottom up. Unlike
Wisp, it has closures, lexical scoping, mark-and-sweep garbage
collection, object system, and compiles to a bytecode (in
memory). Continuations are still a ways off, but planned. One of the
most powerful features so far is the foreign function interface
(FFI). Now that he's implemented it
with <a href="http://sourceware.org/libffi/"> libffi</a> he's barely
had to touch the C code base. In fact, thanks to the FFI, the the C
portion of BrianScheme will be shrinking.
</p>
<p>
For example, BrianScheme currently lacks floating point numbers, and
its integers are currently just native fixnums. Sometime soon it will,
like Wisp, use the GNU Multi-Precision Library (GMP) to provide
bignums. Adding this will not require making <i>any</i> changes
whatsoever to the C code. Using the object system
(<a href="http://community.schemewiki.org/?Tiny-CLOS">Tiny-CLOS</a>),
hooks in the reader and printer, and the FFI, this can be entirely
implemented in the language itself.
</p>
<p>
Just-in-time compilation (JIT) has begun to be implemented without
touching C. Again, done by pulling in
in <a href="http://freshmeat.net/projects/libjit/"> libjit</a> with
the FFI.
</p>
<p>
Because I wrote Wisp to be embeddable and a library, I was able to run
Wisp in BrianScheme, via the FFI, and expose some bindings. For
example, I can send it s-expressions to evaluate,
</p>
<pre>
> (require 'wisp)
> (wisp:eval '(expt 6 56))
37711171281396032013366321198900157303750656
</pre>
<p>
BrianScheme doesn't currently support threading, mainly because the
garbage collector isn't ready for it. But remember
how <a href="/blog/2010/12/21/">I mentioned GNU Pth last month</a>?
Again, I was able to load Pth with the FFI to add userspace threading,
which <i>is</i> safe for the garbage collector because it's
effectively an atomic operation. (Once continuations are implemented,
this could actually be implemented without Pth, just by making good
use of those continuations.) The current hangup is the REPL, which
doesn't know about Pth and so it never yields. To take advantage of
threading you have to suspend the REPL (with <code>pth:join</code>).
</p>
<p>
This REPL issue should be solved with the long term goal for
BrianScheme. The C component of BrianScheme will merely exist for the
purposes of bootstrapping the full system. During initialization, just
about everything will be redefined in BrianScheme, with the original C
definitions only living long enough to load what's needed. This
includes reimplementing the reader itself in BrianScheme, which
enables all sorts of possibilities, like the previously mentioned
bignums implemented in the language
itself, <a href="/blog/2010/04/23/"> inline regular expressions</a>,
and proper yielding to the userspace thread scheduler.
</p>
<p>
So go ahead and clone Brian's repository (and add mine as a remote,
too! :-D) and poke around at it. To compare to Wisp again, it's not
quite as stable at the moment. It exits very easily from runtime
errors, due to lacking error handling, so an instance generally
doesn't live very long at the moment. This will probably be resolved
sometime soon. Except for that, it does play well with Emacs as
an <code>inferior-lisp</code>.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Emacs Set Window to 80 Columns</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/10/06/"/>
    <id>urn:uuid:90343c39-62b8-35c2-d9a7-8b3b29cb8338</id>
    <updated>2010-10-06T00:00:00Z</updated>
    <category term="emacs"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 6 October 2010 -->
<p>
When I'm coding, I maximize Emacs and enable
<a href="http://www.emacswiki.org/emacs/WinnerMode">
<code>winner-mode</code></a>, turning my display into something much
like a <a href="http://en.wikipedia.org/wiki/Tiling_window_manager">
tiling window manager</a>. Then I try not to leave Emacs until it's
necessary. It's a really nice way to work: no mouse touching needed.
</p>
<p>
At work they gave me a nice 24" monitor, 1920 pixels across. That's
just about enough to fit three Emacs' windows side-by-side at 78
columns each. The leftmost one contains my active work buffer where I
do most of my typing. The center one is usually split
horizontally. The top half is the <code>*compilation*</code> buffer
and the bottom half is either <a href="/blog/2009/06/23/">Emacs
calculator</a> or an <code>*ansi-term*</code> buffer. The rightmost
buffer contains something more static, like some sort of reference
material.
</p>
<p>
However, I like my main editing window to be 80 columns wide. 78
columns cuts just too short. For awhile I was creating 80 dashes
(<code>C-u 80 -</code>) and adjusting the window width manually to
size. After doing it a few times I decided to extend Emacs to do it
instead. First define a function to set the current window width.
</p>

<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">set-window-width</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span>
  <span class="s">"Set the selected window's width."</span>
  <span class="p">(</span><span class="nv">adjust-window-trailing-edge</span> <span class="p">(</span><span class="nv">selected-window</span><span class="p">)</span> <span class="p">(</span><span class="nb">-</span> <span class="nv">n</span> <span class="p">(</span><span class="nv">window-width</span><span class="p">))</span> <span class="no">t</span><span class="p">))</span></code></pre></figure>

<p>
Wrap it with an interactive function and bind it.
</p>

<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">set-80-columns</span> <span class="p">()</span>
  <span class="s">"Set the selected window to 80 columns."</span>
  <span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">set-window-width</span> <span class="mi">80</span><span class="p">))</span>

<span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\C-x~"</span> <span class="ss">'set-80-columns</span><span class="p">)</span></code></pre></figure>

<p>
For those paying extra attention: instead of writing the extra
function, you could
use <a href="/blog/2010/09/29/">my <code>expose</code> function from
the other day</a>.
</p>

<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\C-x~"</span> <span class="p">(</span><span class="nv">expose</span> <span class="p">(</span><span class="nv">apply-partially</span> <span class="ss">'set-window-width</span> <span class="mi">80</span><span class="p">)))</span></code></pre></figure>

<p>
The problem with this, though, is the dynamically generated function
doesn't have a name or a docstring. Someone
using <code>describe-key</code> would have little information to go
on.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Emacs Byte Compilation</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/07/01/"/>
    <id>urn:uuid:0c22fcdf-5f7b-315f-5a59-54ce3d7804c3</id>
    <updated>2010-07-01T00:00:00Z</updated>
    <category term="emacs"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 1 July 2010 -->
<p>
A feature unique to some Lisps is the ability to compile functions
individually at any time. This could be to a bytecode or native code,
depending on the dialect and implementation. In a Lisp implementations
where compilation matters (such as <a href="http://clisp.cons.org/">
CLISP</a>), there are typically two forms in which code can be
evaluated: a slower, unoptimized uncompiled form and a fast, efficient
compiled form. The uncompiled form would have some sort of advantage,
even if it's merely not having to spend time on compilation.
</p>
<p>
In Emacs Lisp, the uncompiled form of a function is just a lambda
s-expression. The only thing that gives it a name is the symbol it's
stored in. The compiled form is a (special) vector, with the actual
byte codes stored in a string as the second element. Constants, the
docstring, and other things are stored in this function vector as
well. The Elisp function to compile functions
is <code>byte-compile</code>. It can be given a lambda function or a
symbol. In the case of a symbol, the compiled function is installed
over top of the s-expression form.
</p>
<pre>
(byte-compile (lambda (x) (* 2 x)))
  <i>=&gt; #[(x) "^H\301_\207" [x 2] 2]</i>
</pre>
<p>
The compiler will not only convert the function to bytecode and expand
macros, but also perform optimizations such as removing dead code,
evaluating safe constant forms, and inline functions. This provides a
nice performance boost (testing using my <a href="/blog/2009/05/28/">
measure-time macro</a>),
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">fib</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span>
  <span class="s">"Fibonacci sequence."</span>
  <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">&lt;=</span> <span class="nv">n</span> <span class="mi">2</span><span class="p">)</span> <span class="mi">1</span>
    <span class="p">(</span><span class="nb">+</span> <span class="p">(</span><span class="nv">fib</span> <span class="p">(</span><span class="nb">-</span> <span class="nv">n</span> <span class="mi">1</span><span class="p">))</span> <span class="p">(</span><span class="nv">fib</span> <span class="p">(</span><span class="nb">-</span> <span class="nv">n</span> <span class="mi">2</span><span class="p">)))))</span></code></pre></figure>
<pre>
(measure-time
 (fib 30))
  <i>=&gt; 1.0508708953857422</i>

(byte-compile 'fib)

(measure-time
 (fib 30))
  <i>=&gt; 0.4302399158477783</i>
</pre>
<p>
Most of the installed functions in a typical Emacs instance are
already compiled, since they are loaded already compiled. But a number
of them <i>aren't</i> compiled. So, I thought, why not spend a few
seconds to do this?
</p>
<p>
In Common Lisp, there is a predicate for testing whether a function
has been compiled or not: <code>compiled-function-p</code>. For
whatever reason, there is no equivalent predefined in Elisp, so I
wrote one,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">byte-compiled-p</span> <span class="p">(</span><span class="nv">func</span><span class="p">)</span>
  <span class="s">"Return t if function is byte compiled."</span>
  <span class="p">(</span><span class="nb">cond</span>
   <span class="p">((</span><span class="nb">symbolp</span>   <span class="nv">func</span><span class="p">)</span> <span class="p">(</span><span class="nv">byte-compiled-p</span> <span class="p">(</span><span class="nb">symbol-function</span> <span class="nv">func</span><span class="p">)))</span>
   <span class="p">((</span><span class="nb">functionp</span> <span class="nv">func</span><span class="p">)</span> <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">sequencep</span> <span class="nv">func</span><span class="p">)))</span>
   <span class="p">(</span><span class="no">t</span> <span class="no">nil</span><span class="p">)))</span></code></pre></figure>
<p>
My idea was to iterate over every interned symbol and, if the function
slot contains an uncompiled function, using the test above, I would
call <code>byte-compile</code> on it. Well, it turns out
that <code>byte-compile</code> is very flexible and will ignore
symbols with no function and symbols with already compiled functions.
</p>
<p>
So next, how do we iterate over every interned symbol? There is
a <code>mapatoms</code> function for this. Provide it a function and
it calls it on every interned symbol. Well, that's simple and
anticlimactic.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">mapatoms</span> <span class="ss">'byte-compile</span><span class="p">)</span></code></pre></figure>
<p>
That's it! It will take only a few seconds and spew a lot of
warnings. I haven't found a way to disable those warnings, so this
isn't something you'd want to have run automatically, unless you like
having an extra window thrown in your face. I've only discovered this
recently, so I'm not sure what sort of bad things this may do to your
Emacs session. Not every function was written with compilation in
mind. There are interactions with macros to consider.
</p>
<p>
I doubt there will be a noticeable performance difference. Like I said
before, most everything is already compiled, and those are the
functions that get used the most. There's just something nice about
knowing all your functions are compiled and optimized.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>The Problem with String Stored Regex</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/04/23/"/>
    <id>urn:uuid:b821299d-d1f4-307c-4d93-d8a829b7e5c2</id>
    <updated>2010-04-23T00:00:00Z</updated>
    <category term="lisp"/><category term="rant"/>
    <content type="html">
      <![CDATA[<!-- 23 April 2010 -->
<p>
While regular expressions
have <a href="http://en.wikipedia.org/wiki/Regular_expression#Patterns_for_non-regular_languages">
limited usefulness</a>, especially in larger programs, they're still
very handy to have from time to time. It's usually difficult to write
a lexer or tokenizer without one. Because of this several languages
build them right into the language itself, rather than tacked on as a
library. It allows the regular expressions to be stored literally in
the code, treated as its own type, rather than inside a string. The
problem with storing a regular expression inside a string is that it
can easily make an already complex regular expression much more
complex. This is <b>because there are two levels of parsing going
on</b>.
</p>
<p>
Consider this regular expression where we match an alphanumeric word
inside of quotes. I'm going to use slashes to delimit the regular
expression itself.
<p>
<pre>
/"\w+"/
</pre>
<p>
Notice there is no escaping going on. The backslash is there is
indicate a special sequence <code>\w</code>, which is equal
to <code>[a-zA-Z0-9_]</code>. This will
get <a href="http://swtch.com/~rsc/regexp/regexp1.html"> parsed and
compiled into some form in memory</a> before it is run by a
program. If the language doesn't directly support regular expressions
then we usually can't put it in the code as is, since the language
parser won't know how to deal with it. The solution is to store it
inside of a string.
</p>
<p>
However, our regular expression contains quotes and these will need to
be escaped when in a quote delimited string. But I no longer need
slashes to delimit my regular expression.
</p>
<pre>
"\"\w+\""
</pre>
<p>
Did you notice the error yet? If not, stop and think about it for a
minute. Our special sequence <code>\w</code> will not make it intact
to the regular expression compiler. That backslash will escape
the <code>w</code> during the string parsing step, leaving only
the <code>w</code>. The string we typed will get parsed into a series
of characters in memory, performing escapes along the way, and
then <i>that</i> sequence will be handed to the regular expression
compiler. So we have to fix it,
</p>
<pre>
"\"\\w+\""
</pre>
<p>
That's getting hard to understand, compared to the original. Now let's
throw a curve-ball into this: let's match a backslash at the beginning
of the word. The normal regular expression looks like this now,
</p>
<pre>
/"\\\w+"/
</pre>
<p>
We have to escape our backslash to make it a literal backslash, so it
takes two of them. Now, when we want to do this in a string-stored
regular expression we have to escape both of those backslashes
again. It looks like this,
</p>
<pre>
"\"\\\\\\w+\""
</pre>
<p>
Now to match a single backslash we have to insert four backslashes!
Quite unfortunately, <a href="/blog/2009/05/29/"> Emacs Lisp doesn't
directly support regular expressions</a> even though the language has
a lot of emphasis on text parsing, so a lot of Elisp code is riddled
with this sort of thing. Elisp is especially difficult because
sometimes, such as during prompts, you can enter a regular expression
directly and can ignore the layer of string parsing. It's a very
conscious effort to remember which situation you're in at different
times.
</p>
<p>
Perl, Ruby, and JavaScript have regular expressions as part of the
language and it makes a lot of sense for these languages; they tend to
do a lot of text parsing. Python does it partially, with
its <code>r'</code> syntax. Any string preceded with an <code>r</code>
loses its escape rules, but it also means you can't match both single
or double quotes without falling back to a normal string with
escaping. Common Lisp may be able to do it with a
<a href="http://www.lispworks.com/documentation/lw51/CLHS/Body/02_b.htm">
reader macro</a>, but I've never seen it done.
</p>
<p>
Remember those two levels of parsing when writing string stored
regex. It helps avoid hair-pulling annoying mistakes.
</p>
]]>
    </content>
  </entry>
    
  
    
  <entry>
    <title>Scheme Live Coding</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/04/13/"/>
    <id>urn:uuid:8b36dcc5-5a72-38b4-8e9d-a61727467941</id>
    <updated>2010-04-13T00:00:00Z</updated>
    <category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 13 April 2010 -->
<p>
<a href="http://en.wikipedia.org/wiki/Live_coding">Live coding</a> (or
livecoding) is software development as a performance art. A
programmer's screen is viewed by the audience and a program is written
and modified so that it produces sound and maybe even visual
effects. The audience gets to see the code and its effects live. I'm
not sure if "live" refers to the audience, the editing of live code,
or maybe both. There are videos all over the web of this in action so
if you haven't seen it yet do a quick search and watch one.
</p>
<p>
It's fairly easy to obtain livecoding software. For example,
there's <a href="http://www.pawfal.org/fluxus/"> Fluxus</a>, which
extends PLT Scheme to support livecoding.
</p>
<p>
I've never done livecoding myself, but something I've noticed is that
Scheme seems to be a popular choice of language for livecoding. I
think I know how and why this is. Scheme, being a Lisp dialect, is
naturally
a <a href="http://steve-yegge.blogspot.com/2007/01/pinocchio-problem.html">
living system</a>: it can be modified and extended while it's actively
running. Scheme in particular is very well suited for the task thanks
to its simplicity and optimized tail recursion.
</p>
<p>
I'll do a little text-based livecoding example in PLT Scheme to show
how it works. This will be easier to do yourself if your text editor
can interact directly with a REPL (like Emacs or DrScheme).
</p>
<p>
Let's define a function that prints a line of text to the screen and
recurses, so that it continues printing forever. The recursion is
important here and I'll get back to it. To keep things manageable I'm
also going to insert a 1 second pause.
</p>
<figure class="highlight"><pre><code class="language-scheme" data-lang="scheme"><span class="p">(</span><span class="k">define</span> <span class="p">(</span><span class="nf">print-str</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">display</span> <span class="s">"Hello!\n"</span><span class="p">)</span>
  <span class="p">(</span><span class="nf">sleep</span> <span class="mi">1</span><span class="p">)</span>
  <span class="p">(</span><span class="nf">print-str</span><span class="p">))</span></code></pre></figure>
<p>
If we call this function with <code>(print-str)</code> it will sit
there printing "Hello!" over and over. It will also lock up our REPL
preventing us from doing anything else. Not very useful. So let's put
it in a thread instead!
</p>
<figure class="highlight"><pre><code class="language-scheme" data-lang="scheme"><span class="p">(</span><span class="nf">thread</span> <span class="nv">print-str</span><span class="p">)</span></code></pre></figure>
<p>
Now our program is running and we get to keep our REPL. Why do we need
to keep our REPL alive? Well, so we can
redefine <code>print-str</code> on the fly! In my buffer I'll go back
and change "Hello!" to "Goodbye!". While I'm doing this the function
is still spitting out "Hello!".
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define</span> <span class="p">(</span><span class="nv">print-str</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">display</span> <span class="s">"Goodbye!\n"</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">sleep</span> <span class="mi">1</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">print-str</span><span class="p">))</span></code></pre></figure>
<p>
As soon as I tell my editor to pass this to the REPL
the <code>print-str</code> function gets redefined and starts printing
"Goodbye!" instead. Why did the running function change? Because of
recursion. When it called itself, it actually called the new
definition.
</p>
<p>
Since I didn't keep a handle on the thread the easiest way to
stop <code>print-str</code> from running is to redefine it without
recursion.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define</span> <span class="p">(</span><span class="nv">print-str</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">display</span> <span class="s">"Done.\n"</span><span class="p">))</span></code></pre></figure>
<p>
And it's done. If I was really fast about this my output looks
something like this.
</p><pre>
Hello!
Hello!
Hello!
Goodbye!
Goodbye!
Done.
</pre>
<p>
That's the fundamental workings of livecoding in Scheme: I changed a
program while it was running. To turn the above into the more
interesting livecoding you see in the videos all we need are some
audio and visual bindings (which is the hard part of it all).
</p>
]]>
    </content>
  </entry>
    
  
    
  <entry>
    <title>Emacs cat-safe</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/03/31/"/>
    <id>urn:uuid:47f5f041-2bfc-363f-0a94-73abb0b96ca6</id>
    <updated>2010-03-31T00:00:00Z</updated>
    <category term="emacs"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 31 March 2010 -->
<p>
<img src="/img/emacs/cat-paw.jpg" class="right"
     alt="" title="Calvin's front right paw."/>

I was inspired by an item
in <a href="http://www.terminally-incoherent.com/blog/">
Luke's</a> <a href="http://random.terminally-incoherent.com/post/484785491">
Tumblr blog</a> last night. It was a screenshot of a program
called <a href="http://www.bitboost.com/pawsense/">PawSense</a>, which
monitors a computer's keyboard for cat activity. (I don't know if it's
any good, but it's funny.) As anyone with cats knows, it's not unusual
to leave a computer only to come back later to see garbage typed in by
a wandering cat. I wrote a version for Emacs today.
</p>
<pre>
git clone <a href="https://github.com/skeeto/cat-safe">git://github.com/skeeto/cat-safe.git</a>
</pre>
<p>
Put it (<code>cat-safe.el</code>) somewhere in
your <code>load-path</code> (like <code>~/.emacs.d/</code>) and put
this line in your <code>.emacs</code> file,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">require</span> <span class="ss">'cat-safe</span><span class="p">)</span></code></pre></figure>
<p class="center">
<img src="/img/emacs/cat-safe-thumb.png" title="cat-safe in action"
     alt="Emacs switches focus to a new buffer to stop cat damage."/>
</p>
<p>
This only monitors Emacs itself; it should help protect your buffers
but not your web browser. When cat interference is detected Emacs
switches focus to a junk buffer and lets the cat make a mess there
instead. In case your
cat <a href="http://en.wikipedia.org/wiki/Infinite_monkey_theorem">happens
to type out some Shakespeare</a> you will be able to read it in the
junk buffer. Just kill the junk buffer to return to work.
</p>
<p>
It could still use some improvement. Right now it looks for a single
key being help down, excepting keys humans tend to hold down like
backspace, delete, and space. If you play around with it you'll notice
if you press several keys at once Emacs will sometimes create a
pattern with them. I need to figure out a good way to detect this.
</p>
<p>
I'm going to run it at home for awhile to make sure it remains
transparent, but still does its job. It will probably incur a
performance penalty on frequently repeated keyboard macros.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  <entry>
    <title>Common Lisp Quick Reference</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/02/06/"/>
    <id>urn:uuid:d74ba1a7-25bb-37f4-7b2e-46d2d56bfe36</id>
    <updated>2010-02-06T00:00:00Z</updated>
    <category term="lisp"/><category term="link"/><category term="reddit"/>
    <content type="html">
      <![CDATA[<!-- 6 February 2010 -->
<p>
I found this <a href="http://clqr.berlios.de/"> Common Lisp Quick
Reference</a> the other day
from <a href="http://old.reddit.com/r/lisp/"> r/lisp</a>, and I think
it's <i>fantastic</i>. It's a comprehensive, libre booklet of the
symbols defined by the Common Lisp ANSI standard. Very slick!
</p>
<p>
The main version is meant to be printed out and nested with a vertical
fold, and it works quite well. If I ever get a chance to use Common
Lisp at work (a man can dream), probably at a location without
Internet access, this could come in handy. So I printed out one for
myself,
</p>
<p class="center">
<a href="/img/clqr/front.jpg"><img src="/img/clqr/front-thumb.jpg" alt=""/></a>
<a href="/img/clqr/open.jpg"><img src="/img/clqr/open-thumb.jpg" alt=""/></a>
</p>
]]>
    </content>
  </entry>
    
  
    
  <entry>
    <title>Wisp Screencasts</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/02/04/"/>
    <id>urn:uuid:55bef550-79a4-370d-ee00-679a3f13328e</id>
    <updated>2010-02-04T00:00:00Z</updated>
    <category term="video"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 4 February 2010 -->
<p>
I've been chugging away on Wisp, <a href="/blog/2010/01/24/">announced
in my last post</a>, every day since I started it a few weeks ago, and
it's becoming a pretty solid system. There's now an exception system,
reference counting for dealing with garbage, and a reentrant
parser. It's no replacement for any other lisps, but I've found it to
be very fun to work on.
</p>
<pre>
git clone <a href="https://github.com/skeeto/wisp">git://github.com/skeeto/wisp.git</a>
</pre>
<p>
I wanted to show off some of the new features of Wisp, and since I was
inspired by <a href="http://vimeo.com/channels/fulldisclojure"> Full
Disclojure</a>, since it's so damn slick, I decided to make some
screencasts of Wisp in action. All of the screencast software for
GNU/Linux is pretty poor, but after a few hours of head-banging I
managed to hobble something together for you. Enjoy!
</p>
<p>
  <video src="/vid/wisp/wisp-memoize.ogv" controls>
    Since your browser doesn't seem to support the video tag, here's a
    link to the video: <a href="/vid/wisp/wisp-memoize.ogv">
    wisp-memoize.ogv</a>.
  </video>
</p>
<p>
That video demonstrated the memoization function. It can be pulled in
from the <code>memoize</code> library. You give it a symbol, which
should have a function definition stored in it, and it will installed a
wrapper around it. In the video I used the Fibonacci function from
the <code>examples</code> library.
</p>
<pre>
(require 'examples)
(fib 30) ; Slooooow ...
(memoize 'fib)
(fib 100) ; Fast!
</pre>
<p>
  <video src="/vid/wisp/wisp-detach.ogv" controls>
    Since your browser doesn't seem to support the video tag, here's a
    link to the video: <a href="/vid/wisp/wisp-detach.ogv">
    wisp-detach.ogv</a>.
  </video>
</p>
<p>
This demonstrated the "detachment" feature of Wisp, which is similar
to "futures" in Clojure. It forks off a new process, which executes
the given function. The <code>send</code> function can be used in the
detached process to send any lisp objects back to the parents, which
can receive them with the <code>receive</code>
function. The <code>send</code> function can be called any number of
times to continually send data back. The <code>receive</code> function
will block if there is no lisp object to receive yet.
</p>
<pre>
(require 'examples)
(setq d (detach (lambda () (send (fib)))))
(receive d) ; Gets value from child process
</pre>
<p>
  <video src="/vid/wisp/wisp-point-free.ogv" controls>
    Since your browser doesn't seem to support the video tag, here's a
    link to the video: <a href="/vid/wisp/wisp-point-free.ogv">
    wisp-point-free.ogv</a>.
  </video>
</p>
<p>
This video shows off the point-free functions that have been defined:
function composition and partial application (I accidentally say
"partial evaluation" in the video). These are actually just simple
macros that any lisp could do.
</p>
]]>
    </content>
  </entry>
    
  
    
  <entry>
    <title>Wisp Lisp</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/01/24/"/>
    <id>urn:uuid:f42662c9-f81e-361a-e342-58f9536ed0f8</id>
    <updated>2010-01-24T00:00:00Z</updated>
    <category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 24 January 2010 -->
<p class="abstract">
Update 2010-2-04: A lot of the information below is out of date. There
is an update here: <a href="/blog/2010/02/04/">Wisp Screencasts</a>.
</p>
<p>
<img src="/img/wisp/wisp.png"
     alt="" title="A wisp of a lisp" class="right"/>

This is a project I've been wanting to do for some time, and I finally
got around to doing it. I spent the last few days implementing my own
lisp interpreter in C. Today, after sinking in about 48 hours of work,
I believe I completed enough of it to consider it in a working state,
with a code base stable enough that other interested people could
contribute. It was <i>really</i> exciting to see everything come together
today.
</p>
<p>
You can make a clone of the Wisp repository with Git. Go ahead; don't
be shy,
</p>
<pre>
git clone <a href="https://github.com/skeeto/wisp">git://github.com/skeeto/wisp.git</a>
</pre>
<p>
To build it, all you need is a C99 compiler, make, yacc
(i.e. <a href="http://www.gnu.org/software/bison/"> Bison</a>), and
lex (i.e. <a href="http://flex.sourceforge.net/"> Flex</a>).
</p>
<p>
It doesn't use the readline library or one of it's clones to make a
nice interaction command line, so it's a good idea to run it
with <a href="http://utopia.knoware.nl/~hlub/uck/rlwrap/">rlwrap</a>. That's
what I've been doing. If you plan on writing Wisp code in Emacs,
putting this in your <code>.emacs</code> will give you all the syntax
niceties,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'auto-mode-alist</span> <span class="o">'</span><span class="p">(</span><span class="s">".wisp\\'"</span> <span class="o">.</span> <span class="nv">lisp-mode</span><span class="p">))</span></code></pre></figure>
<p>
You should also be able to run it as an inferior lisp and send code to
it like a normal lisp. I haven't done this yet myself.
</p>
<p>
I think the name is apt, because it really is a wisp of a lisp. As of
this writing, it weighs in at 1500 lines of code and is still very
feature-light. I haven't actually read any material about writing lisp
interpreters, so I've been winging it based on my experience with
it. It's already taught me a lot of subtle things about lisp that I
hadn't been aware of before.
</p>
<p>
Right know it's very simple. It doesn't yet support any syntax beyond
parenthesis (no <code>'</code> quoting). No closures. No garbage
collection, as I'm still working out how I'm going to do
that. Dynamically scoped, since that's a lot easier to do. And, like
Scheme, it's a lisp-1, meaning functions and variables share a common
namespace. I hope to expand it to include some features from other
lisps like Arc (particularly the anonymous function syntactical
sugar), Common Lisp, and Scheme.
</p>
<p>
However, it <i>does</i> have already anonymous functions, which many
popular languages <i>still</i> don't have. :-) It's far enough along
to let you define crazy stuff like this,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">example</span> <span class="p">(</span><span class="nv">n</span><span class="p">)</span>
  <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">&lt;=</span> <span class="nv">n</span> <span class="mi">0</span><span class="p">)</span>
      <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span> <span class="p">(</span><span class="nb">*</span> <span class="nv">x</span> <span class="mi">10</span><span class="p">))</span>
    <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span> <span class="p">(</span><span class="nb">*</span> <span class="nv">x</span> <span class="mf">2.0</span><span class="p">))))</span></code></pre></figure>
<p>
Which you can call, interactively in this case, like so,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="nv">wisp&gt;</span> <span class="p">((</span><span class="nv">example</span>  <span class="mi">1</span><span class="p">)</span> <span class="mi">20</span><span class="p">)</span>
<span class="mf">40.000000</span>
<span class="nv">wisp&gt;</span> <span class="p">((</span><span class="nv">example</span> <span class="mi">-1</span><span class="p">)</span> <span class="mi">20</span><span class="p">)</span>
<span class="nv">200</span></code></pre></figure>
<p>
Because there's no garbage collection yet, it leaks memory like a
sieve. For garbage collection, I think I'll do a mark-and-sweep,
marking objects based on their reachability from the symbol table.
That still leaves some corner cases — such as worrying about objects
in limbo in the evaluator — that I'm not sure about. I need to be
careful not to free objects still in use. Still working that one out.
</p>
<p>
It has the multiplication, addition, subtraction, and division
implemented, as well as the greater-than and less-than operators. Lisp
macros are implemented. A number of special forms are defined, like
let, if, set, defun, defmacro, car, cdr, not, progn, lambda, and
while. All of the predicates are implemented. It has a C interface,
which is how all the above got defined. I already have some functions
and macros defined in terms of Wisp code, too. Most of the needed
functions at this point are trivial to add, though a bit tedious, so
I'm mostly trying to focus on the core parts of the interpreter right
now.
</p>
<p>
It's amazing how much the internal code looks like lisp written in a C
dialect. I have CAR and CDR macros defined, which get used all over
the place, and code frequently uses them to walk lists like lisp code
would.
</p>
<p>
The core struct that everything works with the the object struct. It's
defined as such,
</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="k">typedef</span> <span class="k">struct</span> <span class="n">object</span>
<span class="p">{</span>
  <span class="n">type_t</span> <span class="n">type</span><span class="p">;</span>
  <span class="kt">void</span> <span class="o">*</span><span class="n">val</span><span class="p">;</span>
<span class="p">}</span> <span class="n">object_t</span><span class="p">;</span></code></pre></figure>
<p>
The <code>type_t</code> field comes from this enumeration,
</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="k">typedef</span> <span class="k">enum</span> <span class="n">types</span>
  <span class="p">{</span> <span class="n">INT</span><span class="p">,</span> <span class="n">FLOAT</span><span class="p">,</span> <span class="n">STRING</span><span class="p">,</span> <span class="n">SYMBOL</span><span class="p">,</span> <span class="n">CONS</span><span class="p">,</span> <span class="n">CFUNC</span><span class="p">,</span> <span class="n">SPECIAL</span> <span class="p">}</span> <span class="n">type_t</span><span class="p">;</span></code></pre></figure>
<p>
The type indicates what type of data the void pointer points to,
making it sort-of polymorphic. Note the CONS type, the cons cell, used
to create lists,
</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="k">typedef</span> <span class="k">struct</span> <span class="n">cons</span>
<span class="p">{</span>
  <span class="n">object_t</span> <span class="o">*</span><span class="n">car</span><span class="p">;</span>
  <span class="n">object_t</span> <span class="o">*</span><span class="n">cdr</span><span class="p">;</span>
<span class="p">}</span> <span class="n">cons_t</span><span class="p">;</span></code></pre></figure>
<p>
There's the familiar car and cdr pointers. There are a bunch of helper
functions to manipulate and build these. For
example, <code>c_cons()</code> creates a cons cell,
</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="n">object_t</span> <span class="o">*</span><span class="nf">c_cons</span> <span class="p">(</span><span class="n">object_t</span> <span class="o">*</span> <span class="n">car</span><span class="p">,</span> <span class="n">object_t</span> <span class="o">*</span> <span class="n">cdr</span><span class="p">);</span></code></pre></figure>
<p>
Look familiar? Yup, that's the lisp <code>cons</code> function. Since
the <code>nil</code> symbol is available in C code as <code>NIL</code>
you can chain these together in C to make a list,
</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="n">object_t</span> <span class="o">*</span><span class="n">lst</span> <span class="o">=</span> <span class="n">c_cons</span> <span class="p">(</span><span class="n">c_int</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">c_cons</span> <span class="p">(</span><span class="n">c_str</span> <span class="p">(</span><span class="s">"hello"</span><span class="p">),</span> <span class="n">NIL</span><span class="p">));</span></code></pre></figure>
<p>
Which puts together the simple list,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="mi">10</span> <span class="s">"hello"</span><span class="p">)</span></code></pre></figure>
<p>
Since that's so cumbersome to write out, there's a parser that can
read nice lisp code and use all those same functions to make the
lists. Hence, the lisp reader.
</p>
<p>
If you want to help, it's pretty easy to add more CFUNCs, C functions
that are exposed to Wisp lisp code. Right now, I'd like to expose the
whole C math.h library, provide a nice I/O interface, and expose a
bunch of string functions. The <code>TODO</code> file in the
repository contains more things to be done.
</p>
<p>
Wisp will probably be getting it's own "project page" here at some
point in the future. When it does, I'll update this post to point to
it.
</p>
<p>
Oh, and I decided to make this available under a 2-clause BSD license,
so someone could easily plug it into another program as an extension
language (once Wisp has matured first, of course). That would be
cool.
</p>
]]>
    </content>
  </entry>
    
  
    
  <entry>
    <title>Setting up a Common Lisp Environment</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2010/01/15/"/>
    <id>urn:uuid:3d8b15b6-6f09-3969-b991-b37fa4c13ab4</id>
    <updated>2010-01-15T00:00:00Z</updated>
    <category term="emacs"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 15 January 2010 -->
<p class="abstract">
Update August 2011: Things have changed again, which has always been
the problem with Slime, and the reason I originally wrote
this. Currently, I think the best way to install Slime is
with <a href="http://www.quicklisp.org/">Quicklisp</a>
using <code>quicklisp-slime-helper</code>.
</p>
<p>
Common Lisp is possibly the most advanced programming language. Think
of pretty much any programming language feature and Common Lisp
probably has it. Since lisp is the programmable programming language,
when someone invents a new language feature it can probably be added
to Common Lisp without even touching the language core.
</p>
<p>
<img src="/img/emacs/slime.png" alt="" title="SBCL SLIME REPL"
     class="right"/>

However, if you're interested in digging into Common Lisp to try it
out, you may find yourself quickly running into walls just getting
started. It's a lot different than other programming environments you
may be used to. The Common Lisp tutorials generally skip this step,
assuming the user has an environment, or leaving that setup for the
"vendor" to handle. So, here's a guide to setting up a great Common
Lisp environment with <a href="http://www.gnu.org/software/emacs/">
Emacs</a> and <a href="http://common-lisp.net/project/slime/">
SLIME</a>. It should work with any Common Lisp implementation and any
operating system that can run Emacs (i.e. most of them). Even a much
less capable one like Windows.
</p>
<p>
First, you need to pick a Common Lisp implementation and install
it. Ideally, it should end up in your PATH. Like C, the language is
defined solely by its standardized specification, rather than some
canonical implementation. <a href="http://www.sbcl.org/"> Steel Bank
Common Lisp</a> (SBCL) is currently
the <a href="http://www.cliki.net/Benchmarks">
highest</a> <a href="http://john.freml.in/sbcl-optimise">
performing</a> implementation, it's Free Software, and it runs on a
wide variety of platforms, so take a look at that one if you're not
sure.
</p>
<p>
Next, install Emacs. We're using Emacs not just because it's the best
text editor ever created. <code>:-D</code> It's because that's what
SLIME is written for, and Emacs is a lisp-aware editor. Really, Emacs
is a lisp interpreter that <i>happens</i> to be geared towards
text-editing. It's accused of breaking the rules of unix by being a
single, monolithic program, but it's really a whole bunch of small
lisp programs. You can even have a lisp REPL in Emacs
(<code>ielm</code>), similar to what we will have once we're done
here. It's plays very well with Common Lisp.
</p>
<p>
If you're unfamiliar with Emacs, you should stop here and familiarize
yourself with it a bit. Really, you could spend a decade learning
Emacs and still have more to learn. The tutorial should be good enough
for now. Fire up Emacs and run the tutorial by pressing
<code>control+h</code> then <code>t</code>. In Emacs notation,
that's <code>C-h t</code>. <code>C-h</code> is the help/documentation
prefix, which can be used to look up variables/symbols
(<code>v</code>), functions (<code>f</code>), key bindings
(<code>k</code>), info manuals (<code>i</code>), the current mode
(<code>m</code>), and apropos (searching) (<code>a</code>). In the
info manuals, you should be able to find the full Emacs manual, Elisp
reference, and Elisp tutorial, since they are generally installed
alongside Emacs these days. Nearly anything you might need to know can
be found inside the included documentation.
</p>
<p>
Next, install SLIME. I'll be a bit more specific for this one. Make
a <code>.emacs.d</code> directory in your home directory (whatever
your HOME environmental variable is set to). This is a common place to
put user-installed Emacs extensions. You will be putting
your <code>slime</code> directory in here. There are two basic ways to
obtain SLIME, as indicated right on their main page. You can do a CVS
checkout of the SLIME repository, which allows you to follow it and
run the latest version. Or you can grab a snapshot of the repository,
which is provided, and dump it in there. Since I like you so much,
I'll give you a third option. Here's a Git repository, maintained by
someone very kind, that follows SLIME's CVS repository,
</p>
<pre>
git clone git://git.boinkor.net/slime.git
</pre>
<p>
Ultimately, you should have a directory <code>~/.emacs.d/slime/</code>
that contains a bunch of SLIME source files directly inside.
</p>
<p>
Now, we tell Emacs where SLIME is and how to use it. Make
a <code>.emacs</code> file in your home directory, if you haven't
already, and put this in it,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-to-list</span> <span class="ss">'load-path</span> <span class="s">"~/.emacs.d/slime/"</span><span class="p">)</span>
<span class="p">(</span><span class="nb">require</span> <span class="ss">'slime</span><span class="p">)</span>
<span class="p">(</span><span class="nv">slime-setup</span> <span class="o">'</span><span class="p">(</span><span class="nv">slime-repl</span><span class="p">))</span></code></pre></figure>
<p>
Once it's saved, either restart Emacs, or simply evaluate those lines
by putting the cursor after each them in turn and typing <code>C-x
C-e</code>. If you did everything right so far, you shouldn't have any
errors. (If you did, go back up and see what you did wrong.) If your
Common Lisp installation didn't end up in your PATH as
"<code>lisp</code>" (not uncommon) for some reason, you may need to
tell Emacs where it is. For example, I can point directly to my SBCL
installation with this line,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="k">setq</span> <span class="nv">inferior-lisp-program</span> <span class="s">"/usr/bin/sbcl"</span><span class="p">)</span></code></pre></figure>
<p>
If everything is set up right, fire up SLIME with "<code>M-x
slime</code>". It should compile the back-end, called swank, and run a
Common Lisp REPL as an inferior process to Emacs. You should end up
with a nice prompt like this,
</p>
<pre>
CL-USER>
</pre>
<p>
At this line, you can start evaluating lisp expressions as you
please. But this isn't where the true power of SLIME comes in
yet. I'll give you an example: make a new file with
a <code>.lisp</code> extension and open it. Throw some lisp in there,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">adder</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
  <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">y</span><span class="p">)</span> <span class="p">(</span><span class="nb">+</span> <span class="nv">x</span> <span class="nv">y</span><span class="p">)))</span></code></pre></figure>
<p>
Type <code>C-x C-k</code> and it will send the current buffer over to
be compiled and loaded. This code here uses a closure, so you know you
aren't accidentally using Emacs lisp, as it doesn't have closures. At
the REPL you can call it,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="nv">CL-USER&gt;</span> <span class="p">(</span><span class="nb">funcall</span> <span class="p">(</span><span class="nv">adder</span> <span class="mi">5</span><span class="p">)</span> <span class="mi">6</span><span class="p">)</span></code></pre></figure>
<p>
Which will print the return value, <code>11</code>. That's all there
is to it. You write code in the buffer, then with a simple keystroke
send it to the Common Lisp system to be evaluated and loaded. Because
the SLIME key bindings eclipse the Emacs lisp key bindings, you can
type this same line in the lisp source buffer place the cursor at the
end, and type C-x C-e, which will send it out to Common Lisp to be
evaluated. Look at the mode help (<code>C-h m</code>) to see all the
key bindings made available.
</p>
<p>
This is a great programming environment that makes Common Lisp all the
more fun to use. You run a single, continuous instance if your program
growing it gradually. (This is exactly how I
built <a href="/blog/2009/05/17/">my Emacs web server</a> with elisp.)
You can test your code as soon as soon as it's written.
</p>
<p>
The setup can get even more advanced. The Common Lisp REPL need not be
running on the same computer. It can be running on another computer,
as long as SLIME is able to connect to it over the network. Several
developers could even share a single Common Lisp process running on a
common machine. Lots of possibilities.
</p>
<p>
If you don't have a Common Lisp book yet,
there's <a href="http://gigamonkeys.com/book/">Practical Common
Lisp</a>, which you can read at no cost online
or <a href="http://www.computer-books.us/lisp_0004.php">download</a>
for reading offline. It's based on an Emacs and SLIME setup, so you'll
be right on track.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  <entry>
    <title>Tweaking Emacs for Ant and Java</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2009/12/06/"/>
    <id>urn:uuid:42b07d86-b8d5-3992-5b5e-ad5c41b9256d</id>
    <updated>2009-12-06T00:00:00Z</updated>
    <category term="emacs"/><category term="java"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 6 December 2009 -->
<p class="abstract">
Update: This is now part of my
<a href="https://github.com/skeeto/emacs-java">
<code>java-mode-plus</code></a> Emacs extension.
</p>
<p>
Developing C in Emacs is a real joy, and it's mostly thanks to the
compile command. Once you have your Makefile — or SConstruct or
whatever build system you like — setup and you want to compile your
latest changes, just run <code>M-x compile</code>, which will run your
build system in a buffer. You can then step through the errors and
warnings with <code>C-x `</code>, and Emacs will take you to them.
It's a very nice way to write code.
</p>
<p>
I use the compile command so much that I bound it to <code>C-x
C-k</code> (<code>C-k</code> tends to be part of compile key
bindings),
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">global-set-key</span> <span class="s">"\C-x\C-k"</span> <span class="ss">'compile</span><span class="p">)</span></code></pre></figure>
<p>
Until recently, I didn't have as nice of a setup for Java. Since they
generally force offensive IDEs onto me at work this wasn't something I
needed yet anyway, but <i>I</i> get to choose my environment on a new
project this time. If you're using Makefiles for some reason when
building your Java project, it still works out fairly well because
they're usually called recursively. It gets more complicated with <a
href="http://ant.apache.org/">Ant</a>, where there is only one
top-level build file. Emacs' compile command only runs the build
command in the buffer's current directory.
</p>
<p>
I know three solutions to this problem. One is to provide the build
file's absolute path when <code>compile</code> asks for the command
with the <code>-buildfile</code> (<code>-f</code>) option. You only
need to type it once per Emacs session, so that's not <i>too</i> bad.
</p>
<pre>
ant -emacs -buildfile /path/to/build.xml
</pre>
<p>
It's not well documented, but there is a <code>-find</code> option
that can be given to Ant that will cause it to search for the build
file itself. This is even nicer than the previous solution. Just
remember to place it last, unless you give it the build filename
too. For example, if you wanted to run the <code>clean</code> target,
</p>
<pre>
ant -emacs clean -find
</pre>
<p>
To keep the actual call as simple as possible, I wrote a wrapper for
<code>compile</code>, and put a hook in <code>java-mode</code> to
change the local binding. The wrapper, <code>ant-compile</code>,
searches for the build file the same way <code>-find</code> would do.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">ant-compile</span> <span class="p">()</span>
  <span class="s">"Traveling up the path, find build.xml file and run compile."</span>
  <span class="p">(</span><span class="nv">interactive</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">with-temp-buffer</span>
    <span class="p">(</span><span class="nv">while</span> <span class="p">(</span><span class="nb">and</span> <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nv">file-exists-p</span> <span class="s">"build.xml"</span><span class="p">))</span>
                <span class="p">(</span><span class="nb">not</span> <span class="p">(</span><span class="nb">equal</span> <span class="s">"/"</span> <span class="nv">default-directory</span><span class="p">)))</span>
      <span class="p">(</span><span class="nv">cd</span> <span class="s">".."</span><span class="p">))</span>
    <span class="p">(</span><span class="nv">call-interactively</span> <span class="ss">'compile</span><span class="p">)))</span></code></pre></figure>
<p>
So I can transparently keep using my muscle memory compile binding, I
set up the key binding in a hook,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">add-hook</span> <span class="ss">'java-mode-hook</span>
          <span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="p">(</span><span class="nv">local-set-key</span> <span class="s">"\C-x\C-k"</span> <span class="ss">'ant-compile</span><span class="p">)))</span></code></pre></figure>
<p>
Voila! Java works looks a little bit more like C.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Lisp Fantasy Name Generator</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2009/07/03/"/>
    <id>urn:uuid:492b2b80-c4ba-3655-9811-1b183558d806</id>
    <updated>2009-07-03T00:00:00Z</updated>
    <category term="elisp"/><category term="lisp"/><category term="game"/>
    <content type="html">
      <![CDATA[<!-- 3 July 2009 -->
<p>
Earlier this year <a href="/blog/2009/01/04">I implemented the
RinkWorks fantasy name generator in Perl</a>. I think lisp lends
itself even better for that, and so I have a partial elisp
implementation for you.
</p>
<p>
What stands out for me is that the patterns can easily be represented
as a S-expression. We represent substitutions with symbols, literals
with strings, and groups with lists. For example, this pattern,
</p>
<pre>
s(ith|&lt;'C&gt;)V
</pre>
<p>
can be represented in code as,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">s</span> <span class="p">(</span><span class="s">"ith"</span> <span class="p">(</span><span class="s">"'"</span> <span class="nv">C</span><span class="p">))</span> <span class="nv">V</span><span class="p">)</span></code></pre></figure>
<p>
I want a function I can apply to this to generate a name. First, I set
up an association list with symbols and its replacements,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defvar</span> <span class="nv">namegen-subs</span>
  <span class="o">'</span><span class="p">((</span><span class="nv">s</span> <span class="nv">ach</span> <span class="nv">ack</span> <span class="nv">ad</span> <span class="nv">age</span> <span class="nv">ald</span> <span class="nv">ale</span> <span class="nv">an</span> <span class="nv">ang</span> <span class="nv">ar</span> <span class="nv">ard</span> <span class="nv">as</span> <span class="nb">ash</span> <span class="nv">at</span> <span class="nv">ath</span> <span class="nv">augh</span>
       <span class="nv">aw</span> <span class="nv">ban</span> <span class="nv">bel</span> <span class="nv">bur</span> <span class="nv">cer</span> <span class="nv">cha</span> <span class="nv">che</span> <span class="nv">dan</span> <span class="nv">dar</span> <span class="nv">del</span> <span class="nv">den</span> <span class="nv">dra</span> <span class="nv">dyn</span>
       <span class="nv">ech</span> <span class="nv">eld</span> <span class="nv">elm</span> <span class="nv">em</span> <span class="nv">en</span> <span class="nv">end</span> <span class="nv">eng</span> <span class="nv">enth</span> <span class="nv">er</span> <span class="nv">ess</span> <span class="nv">est</span> <span class="nv">et</span> <span class="nv">gar</span> <span class="nv">gha</span>
       <span class="nv">hat</span> <span class="nv">hin</span> <span class="nv">hon</span> <span class="nv">ia</span> <span class="nv">ight</span> <span class="nv">ild</span> <span class="nv">im</span> <span class="nv">ina</span> <span class="nv">ine</span> <span class="nv">ing</span> <span class="nv">ir</span> <span class="nv">is</span> <span class="nv">iss</span> <span class="nv">it</span>
       <span class="nv">kal</span> <span class="nv">kel</span> <span class="nv">kim</span> <span class="nv">kin</span> <span class="nv">ler</span> <span class="nv">lor</span> <span class="nv">lye</span> <span class="nv">mor</span> <span class="nv">mos</span> <span class="nv">nal</span> <span class="nv">ny</span> <span class="nv">nys</span> <span class="nv">old</span> <span class="nv">om</span>
       <span class="nv">on</span> <span class="nb">or</span> <span class="nv">orm</span> <span class="nv">os</span> <span class="nv">ough</span> <span class="nv">per</span> <span class="nv">pol</span> <span class="nv">qua</span> <span class="nv">que</span> <span class="nv">rad</span> <span class="nv">rak</span> <span class="nv">ran</span> <span class="nv">ray</span> <span class="nv">ril</span>
       <span class="nv">ris</span> <span class="nv">rod</span> <span class="nv">roth</span> <span class="nv">ryn</span> <span class="nv">sam</span> <span class="nv">say</span> <span class="nv">ser</span> <span class="nv">shy</span> <span class="nv">skel</span> <span class="nv">sul</span> <span class="nv">tai</span> <span class="nb">tan</span> <span class="nv">tas</span>
       <span class="nv">ther</span> <span class="nv">tia</span> <span class="nv">tin</span> <span class="nv">ton</span> <span class="nv">tor</span> <span class="nv">tur</span> <span class="nv">um</span> <span class="nv">und</span> <span class="nv">unt</span> <span class="nv">urn</span> <span class="nv">usk</span> <span class="nv">ust</span> <span class="nv">ver</span>
       <span class="nv">ves</span> <span class="nv">vor</span> <span class="nv">war</span> <span class="nv">wor</span> <span class="nv">yer</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">v</span> <span class="nv">a</span> <span class="nv">e</span> <span class="nv">i</span> <span class="nv">o</span> <span class="nv">u</span> <span class="nv">y</span><span class="p">)</span>
    <span class="o">...</span>
    <span class="p">(</span><span class="nv">d</span> <span class="nv">elch</span> <span class="nv">idiot</span> <span class="nv">ob</span> <span class="nv">og</span> <span class="nv">ok</span> <span class="nv">olph</span> <span class="nv">olt</span> <span class="nv">omph</span> <span class="nv">ong</span> <span class="nv">onk</span> <span class="nv">oo</span> <span class="nv">oob</span> <span class="nv">oof</span> <span class="nv">oog</span>
       <span class="nv">ook</span> <span class="nv">ooz</span> <span class="nv">org</span> <span class="nv">ork</span> <span class="nv">orm</span> <span class="nv">oron</span> <span class="nv">ub</span> <span class="nv">uck</span> <span class="nv">ug</span> <span class="nv">ulf</span> <span class="nv">ult</span> <span class="nv">um</span> <span class="nv">umb</span> <span class="nv">ump</span> <span class="nv">umph</span>
       <span class="nv">un</span> <span class="nv">unb</span> <span class="nv">ung</span> <span class="nv">unk</span> <span class="nv">unph</span> <span class="nv">unt</span> <span class="nv">uzz</span><span class="p">))</span>
  <span class="s">"Substitutions for the name generator."</span><span class="p">)</span></code></pre></figure>
<p>
Since we will need this in a couple places, make a function to
randomly select an element from a list,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">randth</span> <span class="p">(</span><span class="nv">lst</span><span class="p">)</span>
  <span class="s">"Select random element from the given list."</span>
  <span class="p">(</span><span class="nb">nth</span> <span class="p">(</span><span class="nb">random</span> <span class="p">(</span><span class="nb">length</span> <span class="nv">lst</span><span class="p">))</span> <span class="nv">lst</span><span class="p">))</span></code></pre></figure>
<p>
A function for replacing a symbol,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">namegen-select</span> <span class="p">(</span><span class="nv">sym</span><span class="p">)</span>
  <span class="s">"Select a replacement for the given symbol."</span>
  <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">null</span> <span class="p">(</span><span class="nb">assoc</span> <span class="nv">sym</span> <span class="nv">namegen-subs</span><span class="p">))</span>
      <span class="p">(</span><span class="k">throw</span> <span class="ss">'bad-symbol</span>
             <span class="p">(</span><span class="nv">concat</span> <span class="s">"Invalid substitution symbol: "</span> <span class="p">(</span><span class="nb">format</span> <span class="s">"%s"</span> <span class="nv">sym</span><span class="p">)))</span>
    <span class="p">(</span><span class="nb">symbol-name</span> <span class="p">(</span><span class="nv">randth</span> <span class="p">(</span><span class="nb">cdr</span> <span class="p">(</span><span class="nb">assoc</span> <span class="nv">sym</span> <span class="nv">namegen-subs</span><span class="p">))))))</span></code></pre></figure>
<p>
And finally, the generator. Find a string, pass it through, find a
symbol, substitute it, find a list, pick one element and recurse on
it.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nb">defun</span> <span class="nv">namegen</span> <span class="p">(</span><span class="nv">sexp</span><span class="p">)</span>
  <span class="s">"Generate a name from the given sexp generator."</span>
  <span class="p">(</span><span class="nb">cond</span>
   <span class="p">((</span><span class="nb">null</span> <span class="nv">sexp</span><span class="p">)</span> <span class="s">""</span><span class="p">)</span>
   <span class="p">((</span><span class="nb">stringp</span> <span class="nv">sexp</span><span class="p">)</span> <span class="nv">sexp</span><span class="p">)</span>
   <span class="p">((</span><span class="nb">symbolp</span> <span class="nv">sexp</span><span class="p">)</span> <span class="p">(</span><span class="nv">namegen-select</span> <span class="nv">sexp</span><span class="p">))</span>
   <span class="p">((</span><span class="nb">listp</span> <span class="nv">sexp</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">concat</span> <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">listp</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">sexp</span><span class="p">))</span> <span class="p">(</span><span class="nv">namegen</span> <span class="p">(</span><span class="nv">randth</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">sexp</span><span class="p">)))</span>
              <span class="p">(</span><span class="nv">namegen</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">sexp</span><span class="p">)))</span>
            <span class="p">(</span><span class="nv">namegen</span> <span class="p">(</span><span class="nb">cdr</span> <span class="nv">sexp</span><span class="p">))))))</span></code></pre></figure>
<p>
That's it! We can apply it to the expression above,
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">namegen</span> <span class="o">'</span><span class="p">(</span><span class="nv">s</span> <span class="p">(</span><span class="s">"ith"</span> <span class="p">(</span><span class="s">"'"</span> <span class="nv">C</span><span class="p">))</span> <span class="nv">V</span><span class="p">))</span>
<span class="nv">-&gt;</span> <span class="s">"rynithi"</span></code></pre></figure>
<p>
But that's really the easy part. The hard part would be converting the
original pattern into the S-expression, which I don't plan on doing
right now.
</p>
<p>
Something else to note: this is thousands of times faster than the
Perl version I wrote earlier.
</p>
<p>
I threw the code in with the rest of my name generation code
(namegen.el),
</p>
<pre>
git clone <a href="https://github.com/skeeto/fantasyname">git://github.com/skeeto/fantasyname.git</a>
</pre>
<p>
S-expressions are handy anywhere.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  <entry>
    <title>United States Hamiltonian Paths</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2009/06/21/"/>
    <id>urn:uuid:4124ec41-1b0a-3a60-a8a2-9667a2243cad</id>
    <updated>2009-06-21T00:00:00Z</updated>
    <category term="math"/><category term="elisp"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 21 June 2009 -->
<p>
Awhile ago I wanted to <a
href="http://threesixty360.wordpress.com/2009/04/22/traveling-the-lower-48/">
find every Hamiltonian path in the contiguous 48 states</a>. That is,
trips that visit each state exactly once. Writing a program to search
for Hamiltonian paths is easy (<a href="/blog/2009/05/27">I did this
already</a>). The most time consuming part was actually putting
together the data that specified the graph to be searched. I hope
someone somewhere finds it useful. Here is a map for reference,
</p>
<p class="center">
<a href="/img/diagram/us48.png">
  <img src="/img/diagram/us48-small.png" alt=""/>
</a>
</p>
<p>
It took me several passes before I stopped finding errors. I
<i>think</i> I have it all right now, but there could still be some
mistakes. If you see one, leave a comment and I'll fix it here. Here
is the graph as an S-expression <a
href="http://en.wikipedia.org/wiki/Association_list#Association_lists">
alist</a>; the car (first) element in each list is a state, and the
cdr (rest) is the unordered list of states that can be reached from
it.
</p>
<pre>
((me nh)
 (nh vt ma me)
 (vt ny ma nh)
 (ma ri ct ny nh vt)
 (ny pa nj ma ct vt)
 (ri ma ct)
 (ct ri ma ny)
 (nj pa ny de)
 (de md pa nj)
 (pa nj ny de md wv oh)
 (md pa de va wv)
 (va md wv ky tn nc)
 (nc va tn ga sc)
 (sc nc ga)
 (ga fl sc al nc tn)
 (al ms fl ga tn)
 (ms la ar tn al)
 (tn ms al ga nc va ky mo ar)
 (ky wv va tn mo il in oh)
 (wv md pa oh ky va)
 (oh pa wv ky in mi)
 (fl al ga)
 (mi wi oh in)
 (wi mn ia il mi)
 (il in ky mo ia wi)
 (in oh ky il mi)
 (mo il ky tn ar ok ks ne ia)
 (ar mo tn ms la tx ok)
 (la ms ar tx)
 (tx ok nm ar la)
 (ok ks mo ar tx nm co)
 (ks ok co ne mo)
 (ne sd ia mo ks co wy)
 (sd nd mn ia ne wy mt)
 (nd mt sd mn)
 (ia ne mo il wi mn sd)
 (mn wi ia sd nd)
 (mt id wy sd nd)
 (wy id ut co ne sd mt)
 (co ne ks ok nm ut wy)
 (nm co ok tx az)
 (az nm ut ca nv)
 (ut nv id wy co az)
 (id mt wy ut nv or wa)
 (wa or id)
 (or wa id nv ca)
 (nv or id ut az ca)
 (ca az nv or))
</pre>
<p>
Note that all paths must start or end in Maine because it connects to
only one other state.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  <entry>
    <title>Elisp Wishlist</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2009/05/29/"/>
    <id>urn:uuid:41fa774c-1f9e-3ef1-1029-69b775475150</id>
    <updated>2009-05-29T00:00:00Z</updated>
    <category term="rant"/><category term="emacs"/><category term="elisp"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 29 May 2009 -->
<p class="abstract">
<b>Update:</b> It looks like all these wishes, except the last one,
may actually be coming
true! <a href="http://lists.gnu.org/archive/html/emacs-devel/2010-04/msg00665.html">
Guile can run Elisp better than Emacs</a>! The idea is that the Elisp
engine is replaced with Guile — the GNU project's Scheme
implementation designed to be used as an extension language — and
written in Scheme is an Elisp compiler that targets Guile's VM. The
extension language of Emacs then becomes Scheme, but Emacs is still
able to run all the old Elisp code. At the same time Elisp itself,
which I'm sure many people will continue to use, gets an upgrade of
arbitrary precision, closures, and better performance.
</p>
<p>
I've been using elisp a lot lately, but unfortunately it's missing a
lot of features that one would find in a more standard lisp. The
following are some features I wish elisp had. Many of these could be
fit into a generic "be more like Scheme or Common Lisp". Some of these
features would break the existing mountain of elisp code out there,
requiring a massive rewrite, which is likely the main reason they are
being held back.
</p>
<p>
<b>Closures</b>, and maybe continuations. Closures are one of the
features I miss the most when writing elisp. They would allow the
implementation of Scheme-style lazy evaluation with <code>delay</code>
and <code>force</code>, among other neat tools. Continuations would
just be a neat thing to have, though they come with a performance
penalty.
</p>
<p>
Closures would also pretty much require Emacs switch to lexical
scoping.
</p>
<p>
<b>Arbitrary precision</b>. Really, any higher order language's
numbers should be bignums. Emacs 22 <i>does</i> come with the Calc
package which provides arbitrary precision via
<code>defmath</code>. Perl does something like this with the bignum
module.
</p>
<p>
<b>Packages/namespaces</b>. Without namespaces all of the Emacs
packages prefix their functions and variables with its name
(i.e. <code>dired-</code>). Some real namespaces would be useful for
large projects.
</p>
<p>
<b>C interface</b>. This is something GNU Emacs will never have
because Richard Stallman considers Emacs shared libraries support to
be <a href="http://www.emacswiki.org/emacs/DynamicallyExtendingEmacs">
a GPL threat</a>. If Emacs could be dynamically extended some useful
libraries could be linked in and exposed to elisp.
</p>
<p>
<b>Concurrency</b>. If some elisp is being executed Emacs will lock
up. This is a particular problem for Gnus. Again, Emacs would really
need to switch to lexical scoping before this could happen. Threading
would be nice.
</p>
<p>
<b>Speed</b>. Emacs lisp is pretty slow, even when compiled. Lexical
scoping would help with performance (compile time vs. run time
binding).
</p>
<p>
<b>Regex type</b>. I mention this last because I think this would be
really cool, and I am not aware of any other lisps that do it. Emacs
does regular expressions with strings, which is silly and
cumbersome. Backslashes need extra escaping, for example. Instead, I
would rather have a regex type like Perl and Javascript have. So
instead of,
</p>
<pre>
(string-match "\\w[0-9]+" "foo525")
</pre>
<p>
we have,
</p>
<pre>
(string-match /\w[0-9]+/ "foo525")
</pre>
<p>
Naturally there would be a <code>regexpp</code> predicate for checking
its type. There could also be a function for compiling a regexp from a
string into a regexp object. As a bonus, I would also like to use it
directly as a function,
</p>
<pre>
(/\w[0-9]+/ "foo525")
</pre>
<p>
I think a regexp price would really give elisp an edge, and would be
entirely appropriate for a text editor. It could also be done without
breaking anything (keep string-style regexp support).
</p>
<p>
There is more commentary over at EmacsWiki: <a
href="http://www.emacswiki.org/emacs/WhyDoesElispSuck"> Why Does Elisp
Suck</a>.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>The Lazy Fibonacci List</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2009/04/10/"/>
    <id>urn:uuid:2b973f3b-c231-39f2-6570-ead12ef963f5</id>
    <updated>2009-04-10T00:00:00Z</updated>
    <category term="lisp"/>
    <content type="html">
      <![CDATA[<!-- 10 April 2009 -->
<p>
In a project I am working on, I want to implement a large list using
<a href="http://en.wikipedia.org/wiki/Lazy_evaluation">lazy
evaluation</a> in Scheme. The list is large enough to be too unwieldy
to store entirely in memory, but I still want to represent it in my
program as if it was. The solution is lazy evaluation.
</p>
<p>
One use of lazy evaluation is allowing a program to have infinitely
sized data structures without going into the impossible task of
actually creating them. Instead, the structure is created on the fly
as needed. As a prototype for getting it right, I made an infinitely
long list in Scheme that contains the entire Fibonacci series.
</p>
<p>
This function, given two numbers from the series, returns the lazy
list. It uses <code>delay</code> to delay evaluation of the list.
</p>
<figure class="highlight"><pre><code class="language-scheme" data-lang="scheme"><span class="p">(</span><span class="k">define</span> <span class="p">(</span><span class="nf">fib</span> <span class="nv">f</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">cons</span> <span class="p">(</span><span class="nb">cadr</span> <span class="nv">f</span><span class="p">)</span>
        <span class="p">(</span><span class="k">delay</span> <span class="p">(</span><span class="nf">fib</span> <span class="p">(</span><span class="nb">list</span> <span class="p">(</span><span class="nb">cadr</span> <span class="nv">f</span><span class="p">)</span>
                          <span class="p">(</span><span class="nb">apply</span> <span class="nv">+</span> <span class="nv">f</span><span class="p">))))))</span></code></pre></figure>
<p>
Notice the recursion here as no <i>base case</i>, so without lazy
evaluation it would continue along forever without halting. Now run
it,
</p>
<figure class="highlight"><pre><code class="language-scheme" data-lang="scheme"><span class="nv">&gt;</span> <span class="p">(</span><span class="nf">fib</span> <span class="o">'</span><span class="p">(</span><span class="nf">0</span> <span class="mi">1</span><span class="p">))</span>
<span class="p">(</span><span class="nf">1</span> <span class="o">.</span> <span class="o">#</span><span class="nv">&lt;promise&gt;</span><span class="p">)</span></code></pre></figure>
<p>
The rest of the list is stored as a <i>promise</i>, which will later
be teased out using <code>force</code>. This forces evaluation of the
promise. Here is a function to traverse the list to the
<code>n</code>th element and return it. Notice, this does have a base
case.
</p>
<figure class="highlight"><pre><code class="language-cl" data-lang="cl"><span class="p">(</span><span class="nv">define</span> <span class="p">(</span><span class="nv">nth-fib</span> <span class="nv">f</span> <span class="nv">n</span><span class="p">)</span>
  <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">=</span> <span class="nv">n</span> <span class="mi">1</span><span class="p">)</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">f</span><span class="p">)</span>
      <span class="p">(</span><span class="nv">nth-fib</span> <span class="p">(</span><span class="nv">force</span> <span class="p">(</span><span class="nb">cdr</span> <span class="nv">f</span><span class="p">))</span> <span class="p">(</span><span class="nb">-</span> <span class="nv">n</span> <span class="mi">1</span><span class="p">))))</span></code></pre></figure>
<p>
Here it is in action. It is retrieving the 30th element.
</p>
<figure class="highlight"><pre><code class="language-scheme" data-lang="scheme"><span class="nv">&gt;</span> <span class="p">(</span><span class="k">define</span> <span class="nv">f</span> <span class="p">(</span><span class="nf">fib</span> <span class="o">'</span><span class="p">(</span><span class="nf">0</span> <span class="mi">1</span><span class="p">)))</span>
<span class="nv">&gt;</span> <span class="nv">f</span>
<span class="p">(</span><span class="nf">1</span> <span class="o">.</span> <span class="o">#</span><span class="nv">&lt;promise&gt;</span><span class="p">)</span>
<span class="nv">&gt;</span> <span class="p">(</span><span class="nf">nth-fib</span> <span class="nv">f</span> <span class="mi">30</span><span class="p">)</span>
<span class="mi">832040</span></code></pre></figure>
<p>
If you examine <code>f</code>, it contains the first 30 numbers until
running into an unevaluated promise. This behavior is very similar to
<a href="/blog/2008/03/25">memoization</a>, as calculated values
are stored instead of being recalculated later.
</p>
<p>
These two functions are also behaving as <a
href="http://en.wikipedia.org/wiki/Coroutines"> coroutines</a>. When
<code>nth-fib</code> reaches a promise, it yields to <code>fib</code>,
which continues its non-halting definition. After producing a new
value in <code>f</code>, it yields back to <code>nth-fib</code>.
</p>
<p>
The way I called these functions above, however, can lead to
problems. We are storing all the calculated values in <code>f</code>,
which can take up a lot of memory. For example, this probably won't
work,
</p>
<figure class="highlight"><pre><code class="language-scheme" data-lang="scheme"><span class="nv">&gt;</span> <span class="p">(</span><span class="nf">nth-fib</span> <span class="nv">f</span> <span class="mi">1000000</span><span class="p">)</span></code></pre></figure>
<p>
We will run out of memory before it halts. Instead, we can do this,
</p>
<figure class="highlight"><pre><code class="language-scheme" data-lang="scheme"><span class="nv">&gt;</span> <span class="p">(</span><span class="nf">nth-fib</span> <span class="p">(</span><span class="nf">fib</span> <span class="o">'</span><span class="p">(</span><span class="nf">0</span> <span class="mi">1</span><span class="p">))</span> <span class="mi">1000000</span><span class="p">)</span></code></pre></figure>
<p>
Because <code>nth-fib</code> uses tail recursion as it traverses the
list, unneeded calculated values are tossed (which the garbage
collector will handle) and no additional function stack is used. All
Scheme implementations optimize tail recursion in this way. This will
continue along until it hits the millionth Fibonacci number, all while
using a constant amount of memory.
</p>
<p>
It turns out that Scheme calls this type of data structure a
<i>stream</i>, and some implementations have functions and macros
defined so that they are ready to use.
</p>
<p>
So there you go: memoization, lazy evaluation, and coroutines all
packed into one example.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Lisp Number Representations</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2008/03/15/"/>
    <id>urn:uuid:32c3088b-0e23-3299-0efe-03bbcdd5ce52</id>
    <updated>2008-03-15T00:00:00Z</updated>
    <category term="lisp"/><category term="compsci"/>
    <content type="html">
      <![CDATA[<!-- 15 March 2008 -->
<p>
This exercise partly comes from a couple different chapters in the
book <a href="http://www.ccs.neu.edu/home/matthias/BTLS/"><i>The
Little Schemer</i></a>. The book is an introduction to the Scheme
programming language, a dialect of Lisp. The purpose to to teach basic
programming concepts in a way that anyone can follow along just as
well as someone with a degree in, say, computer science. It is still
very useful for us programmer types because there are some good
practice you get from reading and playing along.
</p>
<p>
First of all, Lisp is famous (infamous?) for lacking syntax. Any Lisp
program is simply
an <a href="http://en.wikipedia.org/wiki/S-expression">S-expression</a>,
put simply, a list of lists. There is no operator precedence because
operators are treated just like functions. This leads to prefix
notation for mathematical expressions,
</p>
<pre>
(+ 4 5)
=&gt; 9
</pre>
<p>
where the <code>=&gt;</code> indicates the result of evaluating the
expression. We can apply as many operands as we want,
</p>
<pre>
(+ 2 3 4 5 10)
=&gt; 24
</pre>
<p>
We can put another list right in there as an operand,
</p>
<pre>
(+ 3 (* 2 5) 4)
=&gt; 17
</pre>
<p>
You get the idea. In a function, the value of the last expression is
the return value. For example, here is the <code>square</code>
function in Scheme, which squares its input,
</p>
<pre>
(define (square x)
  (* x x))
</pre>
<p>
Then we can use it,
</p>
<pre>
(+ (square 2) (square 5))
=&gt; 29
</pre>
<p>
There are three important list operators to understand as
well: <code>car</code>, <code>cdr</code>,
and <code>cons</code>. <code>car</code> returns the first element in a
list. In the example below, the <code>'</code>, a single quote, tells
the interpreter or compiler that the list is to be treated as data and
not to be executed. This is shorthand, or syntactic sugar, for
the <code>quote</code> operator: <code>(quote (stallman
moglen))</code> is the same as <code>'(stallman moglen)</code>.
</p>
<pre>
(car '(stallman moglen lessig))
=&gt; stallman
</pre>
<p>
<code>cdr</code> returns the "rest" of a list (everything <i>but</i>
the <code>car</code> of the list). When passing a list with only one
element <code>cdr</code> returns the empty list: <code>()</code>.
</p>
<pre>
(cdr '(stallman moglen lessig))
=&gt; (moglen lessig)
(cdr '(stallman))
=&gt; ()
</pre>
<p>
We can ask if a list is empty or not
with <code>null?</code>. <code>#t</code> and <code>#f</code> are true
and false.
</p>
<pre>
(null? '(stallman moglen lessig))
=&gt; #f
(null? '())
=&gt; #t
</pre>
<p>
And finally, for lists, we have <code>cons</code>. This function
allows us to build a list. It glues the first argument to the front of
the list in the second argument,
</p>
<pre>
(cons 'stallman '(moglen lessig))
=&gt; (stallman moglen lessig)
(cons 'stallman '())
=&gt; (stallman)
</pre>
<p>
And one last function you need to know: <code>eq?</code>. It
determines the two atoms are the same atom,
</p>
<pre>
(eq? 'stallman 'moglen)
=&gt; #f
(eq? 'stallman 'stallman)
=&gt; #t
</pre>
<p>
Now, for this exercise we will pretend that the basic arithmetic
functions have not been defined for us. Instead all we have
is <code>add1</code> and <code>sub1</code>, each of which adds or
subtracts 1 from its argument respectively.
</p>
<pre>
(add1 5)
=&gt; 6
(sub1 5)
=&gt; 4
</pre>
<p>
Oh, I almost forgot. We also have the <code>zero?</code> function
defined for us, which tells us if its argument is 0 or not. Notice
that functions that return true or false, called predicates, have
a <code>?</code> on the end.
</p>
<pre>
(zero? 2)
=&gt; #f
(zero? 0)
=&gt; #t
</pre>
<p>
To make things simple, these definitions will only consider positive
numbers. We can define the <code>+</code> function (for only two
arguments) in terms of the three basic functions shown above. It might
be interesting to try to write this yourself before you look any
further. (Hint: define it recursively!)
</p>
<pre>
;; Adds together n and m
(define (+ n m)
  (if (zero? m) n
      (add1 (+ n (sub1 m)))))
</pre>
<p>
If the second argument is 0 we are done and simply return the first
argument. If not, we add 1 to <code>n + (m -
1)</code>. The <code>-</code> function is defined similarly.
</p>
<pre>
;; Subtracts m from n
(define (- n m)
  (if (zero? m) n
      (sub1 (- n (sub1 m)))))
</pre>
<p>
Multiplication is the act of performing addition many times. We can go
on defining it in terms of addition,
</p>
<pre>
(define (* n m)
  (if (zero? m) 0
      (+ n (* n (sub1 m)))))
</pre>
<p>
(We'll leave division as an exercise for the reader as it gets a
little more complicated than I need to go in order to get my overall
point across.)
</p>
<p>
We will leave math behind for a moment take a look at
<a href="http://www.paulgraham.com/rootsofLisp.html">The Roots of
Lisp</a>. In that link is an excellent paper written by Paul Graham
about John McCarthy, the inventor (or perhaps discoverer?) of Lisp,
and how Lisp came to be. It turns out that in order to have a fully
functional Lisp engine we only need seven primitive operators:
operators defined outside of the language itself as building blocks
for the language. For Lisp these seven operators are (Scheme-ized for
our purposes): <code>eq?</code>, <code>atom?</code>, <code>car</code>,
<code>cdr</code>, <code>cons</code>, <code>quote</code>, and
<code>if</code>.
</p>
<p>
Notice how none of these are math operators. You may wonder how we can
possibly perform mathematical operations when we lack these
facilities. The answer: we have to define our own representation for
numbers! Let's try this, define a number as a list of empty lists. So,
the number 3 is,
</p>
<pre>
'(() () ())
</pre>
<p>
And here is 0, 2, and 4,
</p>
<pre>
'()
'(() ())
'(() () () ())
</pre>
<p>
See how that works? Before, when we wanted to define addition and
subtraction, we needed three other
functions: <code>zero?</code>, <code>add1</code>,
and <code>sub1</code>. With our number representation, how could we
define <code>add1</code> with our seven primitive operators? Our
numbers are defined as lists, so we can use our list operators. To add
1 to a number, we append another empty list. Hey, that sounds a lot
like <code>cons</code>!
</p>
<pre>
(define (add1 n)
  (cons '() n))
</pre>
<p>
Subtraction is removing an element from the list, which sounds a lot
like <code>cdr</code>,
</p>
<pre>
(define (sub1 n)
  (cdr n))
</pre>
<p>
And to define <code>zero?</code> we need to check for an empty
list. Notice this will also be the definition for <code>null?</code>.
</p>
<pre>
(define (zero? n)
  (eq? '() n))
</pre>
<p>
And now we are back where we started. In fact, you can use the exact
definitions above to define <code>+</code>, <code>-</code>,
and <code>*</code>. Our entire method number representation depends on
how we define <code>add1</code>, <code>sub1</code>,
and <code>zero?</code>. Let's try it out,
</p>
<pre>
;; 3 + 4
(+ '(() () ()) '(() () () ()))
=&gt; (() () () () () () ())

;; 5 - 2
(- '(() () () () ()) '(() ()))
=&gt; (() () ())

;; 2 * 2
(* '(() ()) '(() ()))
=&gt; (() () () ())

;; 3 + 4 * 2   bolded for clarity
(+ (* '(<b>() () () ()</b>) '(<b>() ()</b>)) '(<b>() () ()</b>))
=&gt; (() () () () () () () () () () ())
</pre>
<p>
Pretty cool, huh? We just added arithmetic (albeit extremely simple)
to our basic Lisp engine. With some modifications we should be able to
define and operate on negative integers and even define any rational
number (limited by how much memory your computer's hardware can
provide).
</p>
<p>
Now, thank goodness this isn't how real Lisp implementations actually
handle numbers. It would be incredibly slow and impractical, not to
mention annoying to read. Normally, numbers and math operators are
primitive so that they are fast.
</p>
]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  <entry>
    <title>Iterated Prisoner's Dilemma</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2007/11/06/"/>
    <id>urn:uuid:4068d624-311a-30f4-a291-63713ffbc932</id>
    <updated>2007-11-06T00:00:00Z</updated>
    <category term="ai"/><category term="video"/><category term="lisp"/>
    <content type="html">
      <![CDATA[<p><img src="/img/prison/top.gif" alt="" /></p>

<p>I was reading about the <a href="http://en.wikipedia.org/wiki/Prisoner's_dilemma">prisoner’s dilemma</a> game the other day
and was inspired to simulate it myself. It would also be a good
project to start learning Common Lisp. All of the source code is
available in its original source file here:</p>

<ul>
  <li><a href="/download/prison/prison.lisp">/download/prison/prison.lisp</a></li>
</ul>

<p>I have only tried this code in my favorite Common Lisp implementation,
<a href="http://clisp.cons.org/">CLISP</a>, as well <a href="http://www.cons.org/cmucl/">CMUCL</a>.</p>

<p>In prisoner’s dilemma, two players acting as prisoners are given the
option of cooperating with or betraying (defecting) the other player.
Each player’s decision along with his opponents decision determines
the length of his prison sentence. It is bad news for the cooperating
player when the other player is defecting.</p>

<p>Prisoner’s dilemma becomes more interesting in the iterated version of
the game, where the same two players play repeatedly. This allows
players to “punish” each other for uncooperative play. Scoring
generally works as so (higher is better),</p>

<table>
<tr><td colspan="2"></td><th colspan="2">Player A</th></tr>
<tr><td colspan="2"></td><td>coop</td><td>defect</td></tr>
<tr><th rowspan="2">Player B</th><td>coop</td>
<td>(3,3)</td><td>(0,5)</td></tr>
<tr><td>defect</td><td>(5,0)</td><td>(1,1)</td></tr>
</table>

<p>The most famous, and strongest individual strategy, is tit-for-tat.
This player begins by playing cooperatively, then does whatever the
its opponent did last. Here is the Common Lisp code to run a
tit-for-tat strategy,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">tit-for-tat</span> <span class="p">()</span>
  <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
    <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">null</span> <span class="nv">x</span><span class="p">)</span> <span class="ss">:coop</span> <span class="nv">x</span><span class="p">)))</span>
</code></pre></div></div>

<p>If you are unfamiliar with Common Lisp, the <code class="language-plaintext highlighter-rouge">lambda</code> part is returning
an anonymous function that actually plays the tit-for-tat strategy.
The <code class="language-plaintext highlighter-rouge">tit-for-tat</code> function generates a tit-for-tat player along with
its own closure. The argument to the anonymous function supplies the
opponent’s last move, which is one of the symbols <code class="language-plaintext highlighter-rouge">:coop</code> or
<code class="language-plaintext highlighter-rouge">:defect</code>. In the case of the first move, <code class="language-plaintext highlighter-rouge">nil</code> is passed. These are
some really simple strategies that ignore their arguments,</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nb">defun</span> <span class="nv">rand-play</span> <span class="p">()</span>
  <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
    <span class="p">(</span><span class="k">declare</span> <span class="p">(</span><span class="k">ignore</span> <span class="nv">x</span><span class="p">))</span>
    <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">&gt;</span> <span class="p">(</span><span class="nb">random</span> <span class="mi">2</span><span class="p">)</span> <span class="mi">0</span><span class="p">)</span> <span class="ss">:coop</span> <span class="ss">:defect</span><span class="p">)))</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">switcher-coop</span> <span class="p">()</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nb">last</span> <span class="ss">:coop</span><span class="p">))</span>
    <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
      <span class="p">(</span><span class="k">declare</span> <span class="p">(</span><span class="k">ignore</span> <span class="nv">x</span><span class="p">))</span>
      <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">eq</span> <span class="nb">last</span> <span class="ss">:coop</span><span class="p">)</span>
          <span class="p">(</span><span class="nb">setf</span> <span class="nb">last</span> <span class="ss">:defect</span><span class="p">)</span>
          <span class="p">(</span><span class="nb">setf</span> <span class="nb">last</span> <span class="ss">:coop</span><span class="p">)))))</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">switcher-defect</span> <span class="p">()</span>
  <span class="p">(</span><span class="k">let</span> <span class="p">((</span><span class="nb">last</span> <span class="ss">:defect</span><span class="p">))</span>
    <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
      <span class="p">(</span><span class="k">declare</span> <span class="p">(</span><span class="k">ignore</span> <span class="nv">x</span><span class="p">))</span>
      <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nb">eq</span> <span class="nb">last</span> <span class="ss">:coop</span><span class="p">)</span>
          <span class="p">(</span><span class="nb">setf</span> <span class="nb">last</span> <span class="ss">:defect</span><span class="p">)</span>
          <span class="p">(</span><span class="nb">setf</span> <span class="nb">last</span> <span class="ss">:coop</span><span class="p">)))))</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">always-coop</span> <span class="p">()</span>
  <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
    <span class="p">(</span><span class="k">declare</span> <span class="p">(</span><span class="k">ignore</span> <span class="nv">x</span><span class="p">))</span>
    <span class="ss">:coop</span><span class="p">))</span>

<span class="p">(</span><span class="nb">defun</span> <span class="nv">always-defect</span> <span class="p">()</span>
  <span class="p">(</span><span class="k">lambda</span> <span class="p">(</span><span class="nv">x</span><span class="p">)</span>
    <span class="p">(</span><span class="k">declare</span> <span class="p">(</span><span class="k">ignore</span> <span class="nv">x</span><span class="p">))</span>
    <span class="ss">:defect</span><span class="p">))</span>
</code></pre></div></div>

<p>Patrick Grim did an interesting study about ten years ago on iterated
prisoner’s dilemma involving competing strategies in a 2-dimensional
area: <a href="http://www.sunysb.edu/philosophy/faculty/pgrim/SPATIALP.HTM">Undecidability in the Spatialized Prisoner’s Dilemma: Some
Philosophical Implications</a>. It is very interesting, but I really
wanted to play around with some different configurations myself. So
what I did was extend my iterated prisoner’s dilemma engine above to
run over a 2-dimensional grid.</p>

<p>Grim’s idea was this: place different strategies in a 2-dimensional
grid. Each strategy competes against its immediate neighbors. (The
paper doesn’t specify which kind of neighbor, 4-connected or
8-connected, so I went with 4-connected.) The sum of these
competitions are added up to make that cell’s final score. After
scoring, each cell takes on the strategy of its highest neighbor, if
any of its neighbors have a higher score than itself. Repeat.</p>

<p>The paper showed some interesting results, where the tit-for-tat
strategy would sometimes dominate, and, in other cases, be quickly
wiped out, depending on starting conditions. Here was my first real
test of my simulation. Three strategies were placed randomly in a
50x50 grid: tit-for-tat, always-cooperate, and always-defect. This is
the first twenty iterations. It stabilizes after 16 iterations.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">run-random-matrix</span> <span class="mi">50</span> <span class="mi">100</span> <span class="mi">20</span> <span class="o">'</span><span class="p">(</span><span class="nv">tit-for-tat</span> <span class="nv">always-coop</span> <span class="nv">always-defect</span><span class="p">))</span>
</code></pre></div></div>

<p><img src="/img/prison/random.gif" alt="" /></p>

<p>White is always-cooperate, black is always-defect, and cyan is
tit-for-tat. Notice how the always-defect quickly exploits the
always-cooperate and dominates the first few iterations. However, as
the always-cooperate resource becomes exhausted, the tit-for-tat
cooperative strategy works together with itself, as well as the
remaining always-cooperate, to eliminate the always-defect invaders,
who have no one left to exploit. In the end, a few always-defect cells
are left in equilibrium, feeding off of always-cooperate neighbors,
who themselves have enough cooperating neighbors to hold their ground.</p>

<p>The effect can be seen more easily here. Around the outside is
tit-for-tat, in the middle is always-cooperate, and a single
always-defect cell is placed in the middle.</p>

<div class="language-cl highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">(</span><span class="nv">run-matrix</span> <span class="p">(</span><span class="nv">create-three-box</span><span class="p">)</span> <span class="mi">100</span> <span class="mi">30</span><span class="p">)</span>
</code></pre></div></div>

<p><img src="/img/prison/boxes.gif" alt="" /></p>

<p>The asymmetric pattern is due to the way that ties are broken.</p>

<p>The lisp code only spits out text, which isn’t very easy to follow
whats going on. To generate these gifs, I first used this Octave
script to convert the text into images. Just dump the lisp output to a
text file and remove the hash table dump at the end. Then run this
script on that file:</p>

<ul>
  <li><a href="/download/prison/pd_plot.m">/download/prison/pd_plot.m</a></li>
</ul>

<p>The text file input should look like this:</p>

<ul>
  <li><a href="/download/prison/example.txt">/download/prison/example.txt</a></li>
</ul>

<p><del>You will need Octave-Forge.</del></p>

<p>The script will make PNGs. You can either change the script to make
GIFs (didn’t try this myself), or use something like
<a href="http://www.imagemagick.org/">ImageMagick</a> to convert the images afterward. Then, you
compile frames into the animated GIF using <a href="http://www.lcdf.org/gifsicle/">Gifsicle</a>.</p>

<p>See if you can come up with some different strategies and make some
special patterns for them. You may be able to observe some interesting
interactions. The image at the beginning of the article uses all of
the listed strategies in a random matrix.</p>

<p>I will continue to try out some more to see if I can find something
particularly interesting.</p>

]]>
    </content>
  </entry>
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
    
  

</feed>
