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

  <title>Articles tagged lang at null program</title>
  <link rel="alternate" type="text/html"
        href="https://nullprogram.com/tags/lang/"/>
  <link rel="self" type="application/atom+xml"
        href="https://nullprogram.com/tags/lang/feed/"/>
  <updated>2026-10-17T21:36:09Z</updated>
  <id>urn:uuid:a6275ef4-a851-4550-918f-4cfc74bc9568</id>

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

  <entry>
    <title>Unintuitive JSON Parsing</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2019/12/28/"/>
    <id>urn:uuid:721eda6d-a78a-41e1-9d78-db3666208a71</id>
    <updated>2019-12-28T17:23:09Z</updated>
    <category term="javascript"/><category term="compsci"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p><em>This article was discussed <a href="https://news.ycombinator.com/item?id=21900715">on Hacker News</a> and <a href="https://old.reddit.com/r/programming/comments/egvq11/unintuitive_json_parsing/">on reddit</a>.</em></p>

<p>Despite the goal of JSON being a subset of JavaScript — which <a href="http://seriot.ch/parsing_json.php">it failed
to achieve</a> (update: <a href="https://github.com/tc39/proposal-json-superset">this was fixed</a>) — parsing JSON is
quite unlike parsing a programming language. For invalid inputs, the
specific cause of error is often counter-intuitive. Normally this
doesn’t matter, but I recently <a href="https://github.com/skeeto/pdjson/pull/19/commits/1500ca73f2ed44ed8a6129fd1fa164bd7e326874#diff-eb030bc5ad128fc13160acab7d06f3a0R702">ran into a case where it does</a>.</p>

<!--more-->

<p>Consider this invalid input to a JSON parser:</p>

<pre class="highlight"><code>[01]
</code></pre>

<p>To a human this might be interpreted as an array containing a number.
Either the leading zero is ignored, or it indicates octal, as it does in
many languages, including JavaScript. In either case the number in the
array would be 1.</p>

<p>However, JSON does not support leading zeros, neither ignoring them nor
supporting octal notation. Here’s the railroad diagram for numbers <a href="https://www.json.org/json-en.html">from
the JSON specficaiton</a>:</p>

<p><img src="/img/diagram/json-number.png" alt="" />
<!-- Copyright (C) 2017 Ecma International --></p>

<p>Or in regular expression form:</p>

<pre class="highlight"><code>-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?
</code></pre>

<p>If a token starts with <code>0</code> then it can only be followed by <code>.</code>, <code>e</code>,
or <code>E</code>. It cannot be followed by a digit. So, the natural human
response to mentally parsing <code>[01]</code> is: This input is invalid because
it contains a number with a leading zero, and leading zeros are not
accepted. <em>But this is not actually why parsing fails!</em></p>

<p>A simple model for the parser is as consuming tokens from a lexer. The
lexer’s job is to read individual code points (characters) from the
input and group them into tokens. The possible tokens are string,
number, left brace, right brace, left bracket, right bracket, comma,
true, false, and null. The lexer skips over insignificant whitespace,
and it doesn’t care about structure, like matching braces and
brackets. That’s the parser’s job.</p>

<p>In some instances the lexer can fail to parse a token. For example, if
while looking for a new token the lexer reads the character <code>%</code>, then
the input must be invalid. No token starts with this character. So in
some cases invalid input will be detected by the lexer.</p>

<p>The parser consumes tokens from the lexer and, using some state, ensures
the sequence of tokens is valid. For example, arrays must be a well
formed sequence of left bracket, value, comma, value, comma, etc., right
bracket. One way to reject input with trailing garbage, is for the lexer
to also produce an EOF (end of file/input) token when there are no more
tokens, and the parser could specifically check for that token before
accepting the input as valid.</p>

<p>Getting back to the input <code>[01]</code>, a JSON parser receives a left bracket
token, then updates its bookkeeping to track that it’s parsing an array.
When looking for the next token, the lexer sees the character <code>0</code>
followed by <code>1</code>. According to the railroad diagram, this is a number
token (starts with <code>0</code>), but <code>1</code> cannot be part of this token, so it
produces a number token with the contents “0”. Everything is still fine.</p>

<p>Next the lexer sees <code>1</code> followed by <code>]</code>. Since <code>]</code> cannot be part of a
number, it produces another number token with the contents “1”. The
parser receives this token but, since it’s parsing an array, it expects
either a comma token or a right bracket. Since this is neither, the
parser fails with an error about an unexpected number. <strong>The parser will
not complain about leading zeros because JSON has no concept of leading
zeros.</strong> Human intuition is right, but for the wrong reasons.</p>

<p>Try this for yourself in your favorite JSON parser. Or even just pop up
the JavaScript console in your browser and try it out:</p>

<pre class="highlight"><code>JSON.parse('[01]');
</code></pre>

<p>Firefox reports:</p>

<blockquote>
  <p>SyntaxError: JSON.parse: expected ‘,’ or ‘]’ after array element</p>
</blockquote>

<p>Chromium reports:</p>

<blockquote>
  <p>SyntaxError: Unexpected number in JSON</p>
</blockquote>

<p>Edge reports (note it says “number” not “digit”):</p>

<blockquote>
  <p>Error: Invalid number at position:3</p>
</blockquote>

<p>In all cases the parsers accepted a zero as the first array element,
then rejected the input after the second number token for being a bad
sequence of tokens. In other words, this is a parser error rather than
a lexer error, as a human might intuit.</p>

<p><a href="https://github.com/skeeto/pdjson">My JSON parser</a> comes with a testing tool that shows the token
stream up until the parser rejects the input, useful for understanding
these situations:</p>

<pre class="highlight"><code>$ echo '[01]' | tests/stream
struct expect seq[] = {
    {JSON_ARRAY},
    {JSON_NUMBER, "0"},
    {JSON_ERROR},
};
</code></pre>

<p>There’s an argument to be made here that perhaps the human readable
error message <em>should</em> mention leading zeros, since that’s likely the
cause of the invalid input. That is, a human probably thought JSON
allowed leading zeros, and so the clearer message would tell the human
that JSON does not allow leading zeros. This is the “more art than
science” part of parsing.</p>

<p>It’s the same story with this invalid input:</p>

<pre class="highlight"><code>[truefalse]
</code></pre>

<p>From this input, the lexer <em>unambiguously</em> produces left bracket,
true, false, right bracket. It’s still up to the parser to reject this
input. The only reason we never see <code>truefalse</code> in valid JSON is that
the overall structure never allows these tokens to be adjacent, not
because they’d be ambiguous. Programming languages have identifiers,
and in a programming language this would parse as the identifier
<code>truefalse</code> rather than <code>true</code> followed by <code>false</code>. From this point of
view, JSON seems quite strange.</p>

<p>Just as before, Firefox reports:</p>

<blockquote>
  <p>SyntaxError: JSON.parse: expected ‘,’ or ‘]’ after array element</p>
</blockquote>

<p>Chromium reports the same error as it does for <code>[true false]</code>:</p>

<blockquote>
  <p>SyntaxError: Unexpected token f in JSON</p>
</blockquote>

<p>Edge’s message is probably a minor bug in their JSON parser:</p>

<blockquote>
  <p>Error: Expected ‘]’ at position:10</p>
</blockquote>

<p>Position 10 is the last character in <code>false</code>. The lexer consumed <code>false</code>
from the input, produced a “false” token, then the parser rejected the
input. When it reported the error, it chose the <em>end</em> of the invalid
token as the error position rather than the start, despite the fact that
the only two valid tokens (comma, right bracket) are both a single
character. It should also say “Expected ‘]’ or ‘,’” (as Firefox does)
rather than just “]”.</p>

<h3 id="concatenated-json">Concatenated JSON</h3>

<p>That’s all pretty academic. Except for producing nice error messages,
nobody really cares so much <em>why</em> the input was rejected. The mismatch
between intuition and reality isn’t important.</p>

<p>However, it <em>does</em> come up with concatenated JSON. Some parsers,
including mine, will optionally consume multiple JSON values, one after
another, from the same input. Here’s an example from <a href="/blog/2016/09/15/">one of my
favorite</a> command line tools, <a href="https://stedolan.github.io/jq/">jq</a>:</p>

<pre class="highlight"><code>echo '{"x":0,"y":1}{"x":2,"y":3}{"x":4,"y":5}' | jq '.x + .y'
1
5
9
</code></pre>

<p>The input contains three unambiguously-concatenated JSON objects, so
the parser produces three distinct objects. Now consider this input,
this time outside of the context of an array:</p>

<pre class="highlight"><code>01
</code></pre>

<p>Is this invalid, one number, or two numbers? According to the lexer and
parser model described above, this is valid and unambiguously two
concatenated numbers. Here’s what my parser says:</p>

<pre class="highlight"><code>$ echo '01' | tests/stream
struct expect seq[] = {
    {JSON_NUMBER, "0"},
    {JSON_DONE},
    {JSON_NUMBER, "1"},
    {JSON_DONE},
    {JSON_ERROR},
};
</code></pre>

<p>Note: The <code>JSON_DONE</code> “token” indicates acceptance, and the <code>JSON_ERROR</code>
token is an EOF indicator, not a hard error. Since jq allows leading
zeros in its JSON input, it’s ambiguous and parses this as the number 1,
so asking its opinion on this input isn’t so interesting. I surveyed
some other JSON parsers that accept concatenated JSON:</p>

<ul>
  <li><a href="https://github.com/FasterXML/jackson">Jackson</a>: Reject as leading zero.</li>
  <li><a href="https://github.com/yonik/noggit">Noggit</a>: Reject as leading zero.</li>
  <li><a href="https://lloyd.github.io/yajl/">yajl</a>: Accept as two numbers.</li>
</ul>

<p>For my parser it’s the same story for <code>truefalse</code>:</p>

<pre class="highlight"><code>echo 'truefalse' | tests/stream
struct expect seq[] = {
    {JSON_TRUE, "true"},
    {JSON_DONE},
    {JSON_FALSE, "false"},
    {JSON_DONE},
    {JSON_ERROR},
};
</code></pre>

<p>Neither rejecting nor accepting this input is wrong, per se.
Concatenated JSON is outside of the scope of JSON itself, and
concatenating arbitrary JSON objects without a whitespace delimiter can
lead to weird and ill-formed input. This is all a great argument in
favor or <a href="http://ndjson.org/">Newline Delimited JSON</a>, and its two simple rules:</p>

<ol>
  <li>Line separator is <code>'\n'</code></li>
  <li>Each line is a valid JSON value</li>
</ol>

<p>This solves the concatenation issue, and, even more, it works well with
parsers not supporting concatenation: Split the input on newlines and
pass each line to your JSON parser.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>No, PHP Doesn't Have Closures</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2019/09/25/"/>
    <id>urn:uuid:286e6fd2-0532-4848-8d4a-10101d1ffa53</id>
    <updated>2019-09-25T21:10:43Z</updated>
    <category term="lang"/><category term="rant"/>
    <content type="html">
      <![CDATA[<p>The PHP programming language is bizarre and, if nothing else, worthy of
anthropological study. The only consistent property of PHP <a href="https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/">is how badly
it’s designed</a>, yet it somehow remains widely popular. There’s
a social dynamic at play here that science has yet to unlock.</p>

<p>I don’t say this because I hate PHP. There’s no reason for that: I don’t
write programs in PHP, never had to use it, and don’t expect to ever
need it. Despite this, I just can’t look away from PHP in the same way I
can’t look away from a car accident.</p>

<p>I recently came across a link to the PHP manual, and morbid curiosity
that caused me to look through it. It’s fun to pick an arbitrary section
of the manual and see how many crazy design choices I can spot, or at
least see what sort of strange terminology the manual has invented to
describe a common concept. This time around, one such section was on
<a href="https://www.php.net/manual/en/functions.anonymous.php">anonymous functions</a>, including closures. It was even worse than
I expected.</p>

<p>In some circumstances, closures can be a litmus test. Closure semantics
are not complex, but they’re subtle and <a href="/blog/2014/06/06/">a little tricky</a> until you
get hang of them. If you’re interviewing a candidate, toss in a question
or two about closures. Either they’re familiar and get it right away, or
they’re unfamiliar and get nothing right. The latter is when it’s most
informative. PHP itself falls clearly into the latter. Not only that,
the example of a “closure” in the manual demonstrates a “closure”
closing over a global variable!</p>

<p>I’d been told for years that PHP has closures, and I took that claim at
face value. In fact, PHP has had “closures” since 5.3.0, released in
June 2009, so I’m over a decade late in investigating it. However, as
far as I can tell, nobody’s ever pointed out that PHP “closures” are, in
fact, not actually closures.</p>

<h3 id="anonymous-functions-and-closures">Anonymous functions and closures</h3>

<p>Before getting into why they’re not closures, let’s go over how it
works, starting with a plain old anonymous function. PHP <em>does</em> have
anonymous functions — the easy part.</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>() {
    <span class="k">return</span> <span class="k">function</span>() {
        <span class="k">return</span> <span class="m">1</span>;
    };
}
</code></pre>

<p>The function <code>foo</code> returns a function that returns 1. In PHP 7 you can
call the returned function immediately like so:</p>

<pre class="highlight"><code><span class="v">$r</span> = foo()();  <span class="c">// $r = 1</span>
</code></pre>

<p>In PHP 5 this is a syntax error because, well, it’s PHP and its parser
is <a href="/blog/2008/08/29/">about as clunky as Matlab’s</a>.</p>

<p>In a well-designed language, you’d expect that this could also be a
closure. That is, it <em>closes over</em> local variables, and the function may
continue to access those variables later. For example:</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">bar</span>(<span class="v">$n</span>) {
    <span class="k">return</span> <span class="k">function</span>() {
        <span class="k">return</span> <span class="v">$n</span>;
    };
}

bar(<span class="m">1</span>)();  <span class="c">// error: Undefined variable: n</span>
</code></pre>

<p>This fails because you must explicitly tell PHP what variables you
intend to access inside the anonymous function with <code>use</code>:</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">bar</span>(<span class="v">$n</span>) {
    <span class="k">return</span> <span class="k">function</span>() <span class="k">use</span> (<span class="v">$n</span>) {
        <span class="k">return</span> <span class="v">$n</span>;
    };
}

bar(<span class="m">1</span>)();  <span class="c">// 1</span>
</code></pre>

<p>If this actually closed over <code>$n</code>, this would be a legitimate closure.
Having to tell the language exactly which variables are being closed
over would be pretty dumb, but it still meets the definition of a
closure.</p>

<p>But here’s the catch: It’s not actually closing over any variables. The
names listed in <code>use</code> are actually extra, hidden parameters bound to the
current value of those variables. In other words, <strong>this is nothing more
than partial function evaluation</strong>.</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">bar</span>(<span class="v">$n</span>) {
    <span class="v">$f</span> = <span class="k">function</span>() <span class="k">use</span> (<span class="v">$n</span>) {
        <span class="k">return</span> <span class="v">$n</span>;
    };
    <span class="v">$n</span>++;  <span class="c">// never used!</span>
    <span class="k">return</span> <span class="v">$f</span>;
}

<span class="v">$r</span> = bar(<span class="m">1</span>)();  <span class="c">// $r = 1</span>
</code></pre>

<p>Here’s the equivalent in JavaScript using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind#Syntax">the <code>bind()</code> method</a>:</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">bar</span>(n) {
    <span class="k">let</span> f = <span class="k">function</span>(m) {
        <span class="k">return</span> m;
    };
    <span class="k">return</span> f.bind(<span class="k">null</span>, n);
}
</code></pre>

<p>This is actually more powerful than PHP’s “closures” since any arbitrary
expression can be used for the bound argument. In PHP it’s limited to a
couple of specific forms. If JavaScript didn’t have proper closures, and
instead we all had to rely on <code>bind()</code>, nobody would claim that
JavaScript had closures. It shouldn’t be different for PHP.</p>

<h3 id="references">References</h3>

<p>PHP <em>does</em> have references, and binding a reference to an anonymous
function is kinda, sorta like a closure. But that’s still just partial
function evaluation, but where that argument is a reference.</p>

<p>Here’s how to tell these reference captures aren’t actually closures:
They work equally well for global variables as local variables. So it’s
still not <em>closing over</em> a lexical environment, just binding a reference
to a parameter.</p>

<pre class="highlight"><code><span class="v">$counter</span> = <span class="m">0</span>;

<span class="k">function</span> <span class="f">bar</span>(<span class="v">$n</span>) {
    <span class="k">global</span> <span class="v">$counter</span>;
    <span class="v">$f</span> = <span class="k">function</span>() <span class="k">use</span> (&amp;<span class="v">$n</span>, &amp;<span class="v">$counter</span>) {
        <span class="v">$counter</span>++;
        <span class="k">return</span> <span class="v">$n</span>;
    };
    <span class="v">$n</span>++;  <span class="c">// now has an effect</span>
    <span class="k">return</span> <span class="v">$f</span>;
}

<span class="v">$r</span> = bar(<span class="m">1</span>)();  <span class="c">// $r = 2, $counter = 1</span>
</code></pre>

<p>In the example above, there’s no difference between <code>$n</code>, a local
variable, and <code>$counter</code>, a global variable. It wouldn’t make sense for
a closure to close over a global variable.</p>

<h3 id="emacs-lisp-partial-function-application">Emacs Lisp partial function application</h3>

<p>Emacs Lisp famously didn’t get lexical scope, and therefore closures,
<a href="/blog/2016/12/22/">until fairly recently</a>. It was — and still is by default — a
dynamic scope oddball. However, it’s long had an <code>apply-partially</code>
function for partial function application. It returns a closure-like
object, and did so when the language didn’t have proper closures. So it
can be used to create a “closure” just like PHP:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">bar</span> (n)
  (apply-partially (<span class="k">lambda</span> (m) m) n))
</code></pre>

<p>This works regardless of lexical or dynamic scope, which is because this
construct isn’t really a closure, just like PHP’s isn’t a closure. In
PHP, its partial function evaluation is built directly into the language
with special <code>use</code> syntax.</p>

<h3 id="monkey-see-monkey-do">Monkey see, monkey do</h3>

<p>Why does the shell command language use sigils? Because it’s built atop
interactive command line usage, where bare words are taken literally and
variables are the exception. Why does Perl use sigils? Because it was
originally designed as an alternative to shell scripts, so it mimicked
that syntax. Why does PHP use sigils? Because Perl did.</p>

<p>The situation with closures follows that pattern, and it comes up all
over PHP. Its designers see a feature in another language, but don’t
really understand its purpose or semantics. So when they attempt to add
that feature to PHP, they get it disastrously wrong.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>UTF-8 String Indexing Strategies</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2019/05/29/"/>
    <id>urn:uuid:12e9ed44-b5c1-495f-8750-dfaf1ab008e2</id>
    <updated>2019-05-29T21:52:06Z</updated>
    <category term="elisp"/><category term="emacs"/><category term="go"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p><em>This article was discussed <a href="https://news.ycombinator.com/item?id=20049491">on Hacker News</a>.</em></p>

<p>When designing or, in some cases, implementing a programming language
with built-in support for Unicode strings, an important decision must be
made about how to represent or encode those strings in memory. Not all
representations are equal, and there are trade-offs between different
choices.</p>

<!--more-->

<p>One issue to consider is that strings typically feature random access
indexing of code points with a time complexity resembling constant
time (<code>O(1)</code>). However, not all string representations actually
support this well. Strings using variable length encoding, such as
UTF-8 or UTF-16, have <code>O(n)</code> time complexity indexing, ignoring
special cases (discussed below). The most obvious choice to achieve
<code>O(1)</code> time complexity — an array of 32-bit values, as in UCS-4 —
makes very inefficient use of memory, especially with typical strings.</p>

<p>Despite this, UTF-8 is still chosen in a number of programming
languages, or at least in their implementations. In this article I’ll
discuss three examples — Emacs Lisp, Julia, and Go — and how each takes a
slightly different approach.</p>

<h3 id="emacs-lisp">Emacs Lisp</h3>

<p>Emacs Lisp has two different types of strings that generally can be used
interchangeably: <em>unibyte</em> and <em>multibyte</em>. In fact, the difference
between them is so subtle that I bet that most people writing Emacs Lisp
don’t even realize there are two kinds of strings.</p>

<p>Emacs Lisp uses UTF-8 internally to encode all “multibyte” strings and
buffers. To fully support arbitrary sequences of bytes in the files
being edited, Emacs uses <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Text-Representations.html">its own extension of Unicode</a> to
precisely and unambiguously represent raw bytes intermixed with text.
Any arbitrary sequence of bytes can be decoded into Emacs’ internal
representation, then losslessly re-encoded back into the exact same
sequence of bytes.</p>

<p>Unibyte strings and buffers are really just byte-strings. In practice,
they’re essentially ISO/IEC 8859-1, a.k.a. <em>Latin-1</em>. It’s a Unicode
string where all code points are below 256. Emacs prefers the smallest
and simplest string representation when possible, <a href="https://www.python.org/dev/peps/pep-0393/">similar to CPython
3.3+</a>.</p>

<pre class="highlight"><code>(multibyte-string-p <span class="s">"hello"</span>)
<span class="c">;; =&gt; nil</span>

(multibyte-string-p <span class="s">"π ≈ 3.14"</span>)
<span class="c">;; =&gt; t</span>
</code></pre>

<p>Emacs Lisp strings are mutable, and therein lies the kicker: As soon as
you insert a code point above 255, Emacs quietly converts the string to
multibyte.</p>

<pre class="highlight"><code>(<span class="k">defvar</span> <span class="f">fish</span> <span class="s">"fish"</span>)

(multibyte-string-p fish)
<span class="c">;; =&gt; nil</span>

(<span class="k">setf</span> (aref fish <span class="m">2</span>) <span class="s">?ŝ</span>
      (aref fish <span class="m">3</span>) <span class="s">?o</span>)

fish
<span class="c">;; =&gt; "fiŝo"</span>

(multibyte-string-p fish)
<span class="c">;; =&gt; t</span>
</code></pre>

<p>Constant time indexing into unibyte strings is straightforward, and
Emacs does the obvious thing when indexing into unibyte strings. It
helps that most strings in Emacs are probably unibyte, even when the
user isn’t working in English.</p>

<p>Most buffers are multibyte, even if those buffers are generally just
ASCII. Since <a href="/blog/2017/09/07/">Emacs uses gap buffers</a> it generally doesn’t matter:
Nearly all accesses are tightly clustered around the point, so O(n)
indexing doesn’t often matter.</p>

<p>That leaves multibyte strings. Consider these idioms for iterating
across a string in Emacs Lisp:</p>

<pre class="highlight"><code>(<span class="k">dotimes</span> (i (length string))
  (<span class="k">let</span> ((c (aref string i)))
    ...))

(<span class="k">cl-loop</span> for c being the elements of string
         ...)
</code></pre>

<p>The latter expands into essentially the same as the former: An
incrementing index that uses <code>aref</code> to index to that code point. So is
iterating over a multibyte string — a common operation — an O(n^2)
operation?</p>

<p>The good news is that, at least in this case, no! It’s essentially just
as efficient as iterating over a unibyte string. Before going over why,
consider this little puzzle. Here’s a little string comparison function
that compares two strings a code point at a time, returning their first
difference:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">compare</span> (string-a string-b)
  (<span class="k">cl-loop</span> for a being the elements of string-a
           for b being the elements of string-b
           unless (eql a b)
           return (cons a b)))
</code></pre>

<p>Let’s examine benchmarks with some long strings (100,000 code points):</p>

<pre class="highlight"><code>(benchmark-run
    (<span class="k">let</span> ((a (make-string <span class="m">100000</span> <span class="m">0</span>))
          (b (make-string <span class="m">100000</span> <span class="m">0</span>)))
      (compare a b)))
<span class="c">;; =&gt; (0.012568031 0 0.0)</span>
</code></pre>

<p>With using two, zeroed unibyte strings it takes 13ms. How about changing
the last code point in one of them to 256, converting it to a multibyte
string:</p>

<pre class="highlight"><code>(benchmark-run
    (<span class="k">let</span> ((a (make-string <span class="m">100000</span> <span class="m">0</span>))
          (b (make-string <span class="m">100000</span> <span class="m">0</span>)))
      (<span class="k">setf</span> (aref a (1- (length a))) <span class="m">256</span>)
      (compare a b)))
<span class="c">;; =&gt; (0.012680513 0 0.0)</span>
</code></pre>

<p>Same running time, so that multibyte string cost nothing more to iterate
across. Let’s try making them both multibyte:</p>

<pre class="highlight"><code>(benchmark-run
    (<span class="k">let</span> ((a (make-string <span class="m">100000</span> <span class="m">0</span>))
          (b (make-string <span class="m">100000</span> <span class="m">0</span>)))
      (<span class="k">setf</span> (aref a (1- (length a))) <span class="m">256</span>
            (aref b (1- (length b))) <span class="m">256</span>)
      (compare a b)))
<span class="c">;; =&gt; (2.327959762 0 0.0)</span>
</code></pre>

<p>That took 2.3 seconds: about 2000x longer to run! Iterating over two
multibyte strings concurrently seems to have broken an optimization.
Can you reason about what’s happened?</p>

<p>To avoid the O(n) cost on this common indexing operating, Emacs keeps
a “bookmark” for the last indexing location into a multibyte string.
If the next access is nearby, it can starting looking from this
bookmark, forwards or backwards. Like a gap buffer, this gives a big
advantage to clustered accesses, including iteration.</p>

<p>However, this string bookmark is <em>global</em>, one per Emacs instance, not
once per string. In the last benchmark, the two multibyte strings are
constantly fighting over a single string bookmark, and indexing in
comparison function is reduced to O(n^2) time complexity.</p>

<p>So, Emacs <em>pretends</em> it has constant time access into its UTF-8 text
data, but it’s only faking it with some simple optimizations. This
usually works out just fine.</p>

<h3 id="julia">Julia</h3>

<p>Another approach is to not pretend at all, and to make this limitation
of UTF-8 explicit in the interface. Julia took this approach, and it
<a href="/blog/2014/03/06/">was one of my complaints about the language</a>. I don’t think
this is necessarily a bad choice, but I do still think it’s
inappropriate considering Julia’s target audience (i.e. Matlab users).</p>

<p>Julia strings are explicitly byte strings containing valid UTF-8 data.
All indexing occurs on bytes, which is trivially constant time, and
always decodes the multibyte code point starting at that byte. <em>But</em>
it is an error to index to a byte that doesn’t begin a code point.
That error is also trivially checked in constant time.</p>

<pre class="highlight"><code>s = <span class="s">"π"</span>

s[<span class="m">1</span>]
<span class="c"># =&gt; 'π'</span>

s[<span class="m">2</span>]
<span class="c"># ERROR: UnicodeError: invalid character index</span>
<span class="c">#  in getindex at ./strings/basic.jl:37</span>
</code></pre>

<p>Slices are still over bytes, but they “round up” to the end of the
current code point:</p>

<pre class="highlight"><code>s[<span class="m">1</span>:<span class="m">1</span>]
<span class="c"># =&gt; "π"</span>
</code></pre>

<p>Iterating over a string requires helper functions which keep an internal
“bookmark” so that each access is constant time:</p>

<pre class="highlight"><code><span class="k">for</span> i <span class="k">in</span> eachindex(string)
    c = string[i]
    <span class="c"># ...</span>
<span class="k">end</span>
</code></pre>

<p>So Julia doesn’t pretend, it makes the problem explicit.</p>

<h3 id="go">Go</h3>

<p>Go is very similar to Julia, but takes an even more explicit view of
strings. All strings are byte strings and there are no restrictions on
their contents. Conventionally strings contain UTF-8 encoded text, but
this is not strictly required. There’s a <code>unicode/utf8</code> package for
working with strings containing UTF-8 data.</p>

<p>Beyond convention, the <code>range</code> clause also assumes the string contains
UTF-8 data, and it’s not an error if it does not. Bytes not containing
valid UTF-8 data appear as a <code>REPLACEMENT CHARACTER</code> (U+FFFD).</p>

<pre class="highlight"><code><span class="k">func</span> <span class="f">main</span>() {
    s := <span class="s">"π\xff"</span>
    <span class="k">for</span> _, r := <span class="k">range</span> s {
        fmt.Printf(<span class="s">"U+%04x\n"</span>, r)
    }
}

<span class="c">// U+03c0</span>
<span class="c">// U+fffd</span>
</code></pre>

<p>A further case of the language favoring UTF-8 is that casting a string
to <code>[]rune</code> decodes strings into code points, like UCS-4, again using
<code>REPLACEMENT CHARACTER</code>:</p>

<pre class="highlight"><code><span class="k">func</span> <span class="f">main</span>() {
    s := <span class="s">"π\xff"</span>
    r := []<span class="t">rune</span>(s)
    fmt.Printf(<span class="s">"U+%04x\n"</span>, r[<span class="m">0</span>])
    fmt.Printf(<span class="s">"U+%04x\n"</span>, r[<span class="m">1</span>])
}

<span class="c">// U+03c0</span>
<span class="c">// U+fffd</span>
</code></pre>

<p>So, like Julia, there’s no pretending, and the programmer explicitly
must consider the problem.</p>

<h3 id="preferences">Preferences</h3>

<p>All-in-all I probably prefer how Julia and Go are explicit with
UTF-8’s limitations, rather than Emacs Lisp’s attempt to cover it up
with an internal optimization. Since the abstraction is leaky, it may
as well be made explicit.</p>

]]>
    </content>
  </entry>
  <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>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>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>lexical-binding</code> is <code>t</code> for all examples. Besides, lexical scope
required by the generator library, and therefore also required by <code>aio</code>.</p>

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

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">fetch-fortune-1</span> (url)
  (<span class="k">let</span> ((buffer (url-retrieve-synchronously url)))
    (<span class="k">with-current-buffer</span> buffer
      (<span class="k">prog1</span> (buffer-string)
        (kill-buffer)))))
</code></pre>

<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>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">fetch-fortune-2</span> (url callback)
  (url-retrieve url (<span class="k">lambda</span> (_status)
                      (funcall callback (buffer-string)))))
</code></pre>

<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>

<pre class="highlight"><code>(aio-defun fetch-fortune-3 (url)
  (<span class="k">let</span> ((buffer (aio-await (aio-url-retrieve url))))
    (<span class="k">with-current-buffer</span> buffer
      (<span class="k">prog1</span> (buffer-string)
        (kill-buffer)))))
</code></pre>

<p>A function defined with <code>aio-defun</code> is just like <code>defun</code> except that
it can use <code>aio-await</code> to pause and wait on any other function defined
with <code>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>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>

<pre class="highlight"><code>(<span class="k">defcustom</span> <span class="f">fortune-url</span> ...)

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

<p>How wonderfully clean that looks! And, yes, it even works with
<code>interactive</code> like that. I can <code>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>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>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>cl-struct</code></a>.</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">aio-promise</span> ()
  <span class="s">"Create a new promise object."</span>
  (record <span class="v">'aio-promise</span> <span class="k">nil</span> ()))

(<span class="k">defsubst</span> <span class="f">aio-promise-p</span> (object)
  (<span class="k">and</span> (eq <span class="v">'aio-promise</span> (type-of object))
       (= <span class="m">3</span> (length object))))

(<span class="k">defsubst</span> <span class="f">aio-result</span> (promise)
  (aref promise <span class="m">1</span>))
</code></pre>

<p>To subscribe to a promise, use <code>aio-listen</code>:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">aio-listen</span> (promise callback)
  (<span class="k">let</span> ((result (aio-result promise)))
    (<span class="k">if</span> result
        (run-at-time <span class="m">0</span> <span class="k">nil</span> callback result)
      (push callback (aref promise <span class="m">2</span>)))))
</code></pre>

<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>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>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>aio-resolve</code>. Unlike other promises, <code>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>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">aio-resolve</span> (promise value-function)
  (<span class="k">unless</span> (aio-result promise)
    (<span class="k">let</span> ((callbacks (nreverse (aref promise <span class="m">2</span>))))
      (<span class="k">setf</span> (aref promise <span class="m">1</span>) value-function
            (aref promise <span class="m">2</span>) ())
      (<span class="k">dolist</span> (callback callbacks)
        (run-at-time <span class="m">0</span> <span class="k">nil</span> callback value-function)))))
</code></pre>

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

<pre class="highlight"><code>(aio-listen promise (<span class="k">lambda</span> (v)
                      (message <span class="s">"%s"</span> (funcall v))))
</code></pre>

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

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">aio-sleep</span> (seconds <span class="k">&amp;optional</span> result)
  (<span class="k">let</span> ((promise (aio-promise))
        (value-function (<span class="k">lambda</span> () result)))
    (<span class="k">prog1</span> promise
      (run-at-time seconds <span class="k">nil</span>
                   <span class="v">#'aio-resolve</span> promise value-function))))
</code></pre>

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

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">aio-timeout</span> (seconds)
  (<span class="k">let</span> ((promise (aio-promise))
        (value-function (<span class="k">lambda</span> () (signal <span class="v">'aio-timeout</span> <span class="k">nil</span>))))
    (<span class="k">prog1</span> promise
      (run-at-time seconds <span class="k">nil</span>
                   <span class="v">#'aio-resolve</span> promise value-function))))
</code></pre>

<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>

<pre class="highlight"><code>(<span class="k">defmacro</span> <span class="f">aio-with-promise</span> (promise <span class="k">&amp;rest</span> body)
  `(aio-resolve ,promise
                (<span class="k">condition-case</span> error
                    (<span class="k">let</span> ((result (<span class="k">progn</span> ,@body)))
                      (<span class="k">lambda</span> () result))
                  (error (<span class="k">lambda</span> ()
                           (signal (car error) <span class="c">; rethrow</span>
                                   (cdr error)))))))
</code></pre>

<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>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>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>aio-await</code> is
just a simple wrapper around <code>iter-yield</code>:</p>

<pre class="highlight"><code>(<span class="k">defmacro</span> <span class="f">aio-await</span> (expr)
  `(funcall (iter-yield ,expr)))
</code></pre>

<p>Remember, that <code>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>aio-lambda</code> / <code>aio-defun</code> needs to wrap the body in a generator
(<code>iter-lamba</code>), invoke it to produce a generator, then drive the
generator using callbacks. Here’s a simplified, unhygienic definition of
<code>aio-lambda</code>:</p>

<pre class="highlight"><code>(<span class="k">defmacro</span> <span class="f">aio-lambda</span> (arglist <span class="k">&amp;rest</span> body)
  `(<span class="k">lambda</span> (<span class="k">&amp;rest</span> args)
     (<span class="k">let</span> ((promise (aio-promise))
           (iter (apply (iter-lambda ,arglist
                          (aio-with-promise promise
                            ,@body))
                        args)))
       (<span class="k">prog1</span> promise
         (aio--step iter promise <span class="k">nil</span>)))))
</code></pre>

<p>The body is evaluated inside <code>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>aio--step</code>, which drives
the iterator forward until it delivers its first promise. When the
iterator yields a promise, <code>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>

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

<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>aio-defun</code> just uses <code>aio-lambda</code> with <code>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>aio-sleep</code> and <code>aio-timeout</code>.</p>

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

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

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">aio-select</span> (promises)
  (<span class="k">let</span> ((result (aio-promise)))
    (<span class="k">prog1</span> result
      (<span class="k">dolist</span> (promise promises)
        (aio-listen promise (<span class="k">lambda</span> (_)
                              (aio-resolve
                               result
                               (<span class="k">lambda</span> () promise))))))))
</code></pre>

<p>We give <code>aio-select</code> both our <code>url-retrieve</code> and <code>timeout</code> promises, and
it tells us which resolved first:</p>

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

<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>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>aio-process-filter</code></li>
  <li><code>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>

<pre class="highlight"><code>(aio-defun process-chunks (process)
  (<span class="k">cl-loop</span> for chunk = (aio-await (aio-process-filter process))
           while chunk
           do (... process chunk ...)))
</code></pre>

<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>aio-process-filter</code>.</p>

<p>I considered wrapping functions like <code>start-process</code> so that their <code>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>process-send-string</code> and <code>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>process-send-*</code> functions <a href="/blog/2013/01/14/">are
preemptible</a>, made necessary because they block. The
<code>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>aio-make-callback</code>.</p>

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

<p>The test suite for <code>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>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>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>aio</code>:</p>

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

<p>To see it in action (<code>M-x sleep-sort-demo</code>):</p>

<pre class="highlight"><code>(aio-defun sleep-sort-demo ()
  (<span class="k">interactive</span>)
  (<span class="k">let</span> ((values '(<span class="m">0.1</span> <span class="m">0.4</span> <span class="m">1.1</span> <span class="m">0.2</span> <span class="m">0.8</span> <span class="m">0.6</span>)))
    (message <span class="s">"%S"</span> (aio-await (sleep-sort values)))))
</code></pre>

<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>Python Decorators: Syntactic Artificial Sweetener</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2019/03/08/"/>
    <id>urn:uuid:588255e6-70a2-4733-bf58-ca9a857930f3</id>
    <updated>2019-03-08T23:00:49Z</updated>
    <category term="python"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>Python has a feature called <em>function decorators</em>. With a little bit of
syntax, the behavior of a function or class can be modified in useful
ways. Python comes with a few decorators, but most of the useful ones
are found in third-party libraries.</p>

<p><a href="https://www.python.org/dev/peps/pep-0318/">PEP 318</a> suggests a very simple, but practical decorator called
<code>synchronized</code>, though it doesn’t provide a concrete example. Consider
this function that increments a global counter:</p>

<pre class="highlight"><code>counter = <span class="m">0</span>

<span class="k">def</span> <span class="f">increment</span>():
    <span class="k">global</span> counter
    counter = counter + <span class="m">1</span>
</code></pre>

<p>If this function is called from multiple threads, there’s a <em>race
condition</em> — though, at least for CPython, it’s <a href="https://blog.regehr.org/archives/490">not a <em>data
race</em></a> thanks to the Global Interpreter Lock (GIL). Incrementing
the counter is not an atomic operation, as illustrated by <a href="/blog/2019/02/24/">its byte
code</a>:</p>

<pre class="highlight"><code> 0 LOAD_GLOBAL              0 (counter)
 3 LOAD_CONST               1 (1)
 6 BINARY_ADD
 7 STORE_GLOBAL             0 (counter)
10 LOAD_CONST               0 (None)
13 RETURN_VALUE
</code></pre>

<p>The variable is loaded, operated upon, and stored. Another thread
could be scheduled between any of these instructions and cause an
undesired result. It’s easy to see that in practice:</p>

<pre class="highlight"><code><span class="k">from</span> threading <span class="k">import</span> Thread

<span class="k">def</span> <span class="f">worker</span>():
    <span class="k">for</span> i <span class="k">in</span> range(<span class="m">200000</span>):
        increment()

threads = [Thread(target=worker) <span class="k">for</span> _ <span class="k">in</span> range(<span class="m">8</span>)];
<span class="k">for</span> thread <span class="k">in</span> threads:
    thread.start()
<span class="k">for</span> thread <span class="k">in</span> threads:
    thread.join()

print(counter)
</code></pre>

<p>The increment function is called exactly 1.6 million times, but on my
system I get different results on each run:</p>

<pre class="highlight"><code>$ python3 example.py 
1306205
$ python3 example.py 
1162418
$ python3 example.py 
1076801
</code></pre>

<p>I could change the definition of <code>increment()</code> to use synchronization,
but wouldn’t it be nice if I could just tell Python to synchronize this
function? This is where a function decorator shines:</p>

<pre class="highlight"><code><span class="k">from</span> threading <span class="k">import</span> Lock

<span class="k">def</span> <span class="f">synchronized</span>(f):
    lock = Lock()
    <span class="k">def</span> <span class="f">wrapper</span>():
        <span class="k">with</span> lock:
            <span class="k">return</span> f()
    <span class="k">return</span> wrapper
</code></pre>

<p>The <code>synchronized</code> function is a higher order function that accepts a
function and returns a function — or, more specifically, a <em>callable</em>.
The purpose is to wrap and <em>decorate</em> the function it’s given. In this
case the function is wrapped in a mutual exclusion lock. Note: This
implementation is very simple and only works for functions that accept
no arguments.</p>

<p>To use it, I just add a single line to <code>increment</code>:</p>

<pre class="highlight"><code><span class="p">@synchronized</span>
<span class="k">def</span> <span class="f">increment</span>():
    <span class="k">global</span> counter
    counter = counter + <span class="m">1</span>
</code></pre>

<p>With this change my program now always prints 1600000.</p>

<h3 id="syntactic-sugar">Syntactic “sugar”</h3>

<p>Everyone is quick to point out that this is just syntactic sugar, and
that you can accomplish this without the <code>@</code> syntax. For example, the
last definition of <code>increment</code> is equivalent to:</p>

<pre class="highlight"><code><span class="k">def</span> <span class="f">increment</span>():
    ...

increment = synchronized(increment)
</code></pre>

<p>Decorators can also be parameterized. For example, Python’s
<code>functools</code> module has an <code>lru_cache</code> decorator for memoizing a
function:</p>

<pre class="highlight"><code><span class="p">@lru_cache</span>(maxsize=<span class="m">32</span>)
<span class="k">def</span> <span class="f">expensive</span>(n):
    ...
</code></pre>

<p>Which is equivalent to this very direct source transformation:</p>

<pre class="highlight"><code><span class="k">def</span> <span class="f">expensive</span>(n):
    ...

expensive = lru_cache(maxsize=<span class="m">32</span>)(expensive)
</code></pre>

<p>So what comes after the <code>@</code> isn’t just a name. In fact, it <em>looks</em>
like it can be any kind of expression that evaluates to a function
decorator. Or is it?</p>

<h3 id="syntactic-artificial-sweetener">Syntactic artificial sweetener</h3>

<p>Reality is often disappointing. Let’s try using an “identity” decorator
defined using <code>lambda</code>. This decorator will accomplish nothing, but it
will test if we can decorate a function using a lambda expression.</p>

<pre class="highlight"><code>@<span class="k">lambda</span> f: f
<span class="k">def</span> <span class="f">foo</span>():
    <span class="k">pass</span>
</code></pre>

<p>But Python complains:</p>

<pre class="highlight"><code>    @lambda f: f
          ^
SyntaxError: invalid syntax
</code></pre>

<p>Maybe Python is absolutely literal about the syntax sugar thing, and
it’s more like a kind of macro replacement. Let’s try wrapping it in
parentheses:</p>

<pre class="highlight"><code>@(<span class="k">lambda</span> f: f)
<span class="k">def</span> <span class="f">foo</span>(n):
    <span class="k">pass</span>
</code></pre>

<p>Nope, same error, but now pointing at the opening parenthesis. Getting
desperate now:</p>

<pre class="highlight"><code>@[synchronized][<span class="m">0</span>]
<span class="k">def</span> <span class="f">foo</span>():
    <span class="k">pass</span>
</code></pre>

<p>Again, syntax error. What’s going on?</p>

<h3 id="pattern-matching">Pattern matching</h3>

<p>The problem is that the Python language reference doesn’t parse an
expression after <code>@</code>. It <a href="https://docs.python.org/3/reference/compound_stmts.html#function-definitions">matches a very specific pattern</a> that
just so happens to <em>look</em> like a Python expression. It’s not syntactic
sugar, it’s syntactic artificial sweetener!</p>

<pre class="highlight"><code>ator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
</code></pre>

<p>In a way, this puts Python in the ranks of PHP 5 and Matlab: two
languages with completely screwed up grammars that can only parse
specific constructions that the developers had anticipated. For
example, in PHP 5 (fixed in PHP 7):</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>() {
    <span class="k">return</span> <span class="k">function</span>() {
        <span class="k">return</span> <span class="m">0</span>;
    };
}

foo()();
</code></pre>

<p>That is a syntax error:</p>

<pre class="highlight"><code>PHP Parse error:  syntax error, unexpected '(', expecting ',' or ';'
</code></pre>

<p>Or <a href="/blog/2008/08/29/">in any version of Matlab</a>:</p>

<pre class="highlight"><code>    magic(<span class="m">4</span>)(:)
</code></pre>

<p>That is a syntax error:</p>

<pre class="highlight"><code>Unbalanced or unexpected parenthesis or bracket
</code></pre>

<p>In Python’s defense, this strange, limited syntax is only in a single
place rather than everywhere, but I still wonder why it was defined
that way.</p>

<p>Update: Clément Pit-Claudel pointed out the explanation in the PEP,
which references <a href="https://mail.python.org/pipermail/python-dev/2004-August/046711.html">a 2004 email by Guido van Rossum</a>:</p>

<blockquote>
  <p>I have a gut feeling about this one.  I’m not sure where it comes
from, but I have it.  It may be that I want the compiler to be able to
recognize certain decorators.</p>

  <p>So while it would be quite easy to change the syntax to @test in the
future, I’d like to stick with the more restricted form unless a real
use case is presented where allowing @test would increase readability.
(@foo().bar() doesn’t count because I don’t expect you’ll ever need
that).</p>
</blockquote>

]]>
    </content>
  </entry>
  <entry>
    <title>The CPython Bytecode Compiler is Dumb</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2019/02/24/"/>
    <id>urn:uuid:4348d611-858b-4f48-a6f5-6e4b93f71a34</id>
    <updated>2019-02-24T21:56:35Z</updated>
    <category term="python"/><category term="lua"/><category term="lang"/><category term="elisp"/><category term="optimization"/>
    <content type="html">
      <![CDATA[<p><em>This article was <a href="https://news.ycombinator.com/item?id=19241545">discussed on Hacker News</a>.</em></p>

<p>Due to sheer coincidence of several unrelated tasks converging on
Python at work, I recently needed to brush up on my Python skills. So
far for me, Python has been little more than <a href="/blog/2017/05/15/">a fancy extension
language for BeautifulSoup</a>, though I also used it to participate
in the recent tradition of <a href="https://github.com/skeeto/qualbum">writing one’s own static site
generator</a>, in this case for <a href="http://photo.nullprogram.com/">my wife’s photo blog</a>.
I’ve been reading through <em>Fluent Python</em> by Luciano Ramalho, and it’s
been quite effective at getting me up to speed.</p>

<!--more-->

<p>As I write Python, <a href="/blog/2014/01/04/">like with Emacs Lisp</a>, I can’t help but
consider what exactly is happening inside the interpreter. I wonder if
the code I’m writing is putting undue constraints on the bytecode
compiler and limiting its options. Ultimately I’d like the code I
write <a href="/blog/2017/01/30/">to drive the interpreter efficiently and effectively</a>.
<a href="https://www.python.org/dev/peps/pep-0020/">The Zen of Python</a> says there should “only one obvious way to do
it,” but in practice there’s a lot of room for expression. Given
multiple ways to express the same algorithm or idea, I tend to prefer
the one that compiles to the more efficient bytecode.</p>

<p>Fortunately CPython, the main and most widely used implementation of
Python, is very transparent about its bytecode. It’s easy to inspect
and reason about its bytecode. The disassembly listing is easy to read
and understand, and I can always follow it without consulting the
documentation. This contrasts sharply with modern JavaScript engines
and their opaque use of JIT compilation, where performance is guided
by obeying certain patterns (<a href="https://www.youtube.com/watch?v=UJPdhx5zTaw">hidden classes</a>, etc.), helping the
compiler <a href="https://blog.mozilla.org/javascript/2013/11/07/efficient-float32-arithmetic-in-javascript/">understand my program’s types</a>, and being careful
not to unnecessarily constrain the compiler.</p>

<p>So, besides just catching up with Python the language, I’ve been
studying the bytecode disassembly of the functions that I write. One
fact has become quite apparent: <strong>the CPython bytecode compiler is
pretty dumb</strong>. With a few exceptions, it’s a very literal translation
of a Python program, and there is almost <a href="https://legacy.python.org/workshops/1998-11/proceedings/papers/montanaro/montanaro.html">no optimization</a>.
Below I’ll demonstrate a case where it’s possible to detect one of the
missed optimizations without inspecting the bytecode disassembly
thanks to a small abstraction leak in the optimizer.</p>

<p>To be clear: This isn’t to say CPython is bad, or even that it should
necessarily change. In fact, as I’ll show, <strong>dumb bytecode compilers
are par for the course</strong>. In the past I’ve lamented how the Emacs Lisp
compiler could do a better job, but CPython and Lua are operating at
the same level. There are benefits to a dumb and straightforward
bytecode compiler: the compiler itself is simpler, easier to maintain,
and more amenable to modification (e.g. as Python continues to
evolve). It’s also easier to debug Python (<code>pdb</code>) because it’s such a
close match to the source listing.</p>

<p><em>Update</em>: <a href="https://codewords.recurse.com/issues/seven/dragon-taming-with-tailbiter-a-bytecode-compiler">Darius Bacon points out</a> that Guido van Rossum
himself said, “<a href="https://books.google.com/books?id=bIxWAgAAQBAJ&amp;pg=PA26&amp;lpg=PA26&amp;dq=%22Python+is+about+having+the+simplest,+dumbest+compiler+imaginable.%22&amp;source=bl&amp;ots=2OfDoWX321&amp;sig=ACfU3U32jKZBE3VkJ0gvkKbxRRgD0bnoRg&amp;hl=en&amp;sa=X&amp;ved=2ahUKEwjZ1quO89bgAhWpm-AKHfckAxUQ6AEwAHoECAkQAQ#v=onepage&amp;q=%22Python%20is%20about%20having%20the%20simplest%2C%20dumbest%20compiler%20imaginable.%22&amp;f=false">Python is about having the simplest, dumbest compiler
imaginable.</a>” So this is all very much by design.</p>

<p>The consensus seems to be that if you want or need better performance,
use something other than Python. (And if you can’t do that, at least use
<a href="https://pypy.org/">PyPy</a>.) That’s a fairly reasonable and healthy goal. Still, if
I’m writing Python, I’d like to do the best I can, which means
exploiting the optimizations that <em>are</em> available when possible.</p>

<h3 id="disassembly-examples">Disassembly examples</h3>

<p>I’m going to compare three bytecode compilers in this article: CPython
3.7, Lua 5.3, and Emacs 26.1. Each of these languages are dynamically
typed, are primarily executed on a bytecode virtual machine, and it’s
easy to access their disassembly listings. One caveat: CPython and Emacs
use a stack-based virtual machine while Lua uses a register-based
virtual machine.</p>

<p>For CPython I’ll be using the <code>dis</code> module. For Emacs Lisp I’ll use <code>M-x
disassemble</code>, and all code will use lexical scoping. In Lua I’ll use
<code>lua -l</code> on the command line.</p>

<h3 id="local-variable-elimination">Local variable elimination</h3>

<p>Will the bytecode compiler eliminate local variables? Keeping the
variable around potentially involves allocating memory for it, assigning
to it, and accessing it. Take this example:</p>

<pre class="highlight"><code><span class="k">def</span> <span class="f">foo</span>():
    x = <span class="m">0</span>
    y = <span class="m">1</span>
    <span class="k">return</span> x
</code></pre>

<p>This function is equivalent to:</p>

<pre class="highlight"><code><span class="k">def</span> <span class="f">foo</span>():
    <span class="k">return</span> <span class="m">0</span>
</code></pre>

<p>Despite this, CPython completely misses this optimization for both <code>x</code>
and <code>y</code>:</p>

<pre class="highlight"><code>  2           0 LOAD_CONST               1 (0)
              2 STORE_FAST               0 (x)
  3           4 LOAD_CONST               2 (1)
              6 STORE_FAST               1 (y)
  4           8 LOAD_FAST                0 (x)
             10 RETURN_VALUE
</code></pre>

<p>It assigns both variables, and even loads again from <code>x</code> for the return.
Missed optimizations, but, as I said, by keeping these variables around,
debugging is more straightforward. Users can always inspect variables.</p>

<p>How about Lua?</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>()
    <span class="k">local</span> x = <span class="m">0</span>
    <span class="k">local</span> y = <span class="m">1</span>
    <span class="k">return</span> x
<span class="k">end</span>
</code></pre>

<p>It also misses this optimization, though it matters a little less due to
its architecture (the return instruction references a register
regardless of whether or not that register is allocated to a local
variable):</p>

<pre class="highlight"><code>        1       [2]     LOADK           0 -1    ; 0
        2       [3]     LOADK           1 -2    ; 1
        3       [4]     RETURN          0 2
        4       [5]     RETURN          0 1
</code></pre>

<p>Emacs Lisp also misses it:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">foo</span> ()
  (<span class="k">let</span> ((x <span class="m">0</span>)
        (y <span class="m">1</span>))
    x))
</code></pre>

<p>Disassembly:</p>

<pre class="highlight"><code>0	constant  0
1	constant  1
2	stack-ref 1
3	return
</code></pre>

<p>All three are on the same page.</p>

<h3 id="constant-folding">Constant folding</h3>

<p>Does the bytecode compiler evaluate simple constant expressions at
compile time? This is simple and everyone does it.</p>

<pre class="highlight"><code><span class="k">def</span> <span class="f">foo</span>():
    <span class="k">return</span> <span class="m">1</span> + <span class="m">2</span> * <span class="m">3</span> / <span class="m">4</span>
</code></pre>

<p>Disassembly:</p>

<pre class="highlight"><code>  2           0 LOAD_CONST               1 (2.5)
              2 RETURN_VALUE
</code></pre>

<p>Lua:</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>()
    <span class="k">return</span> <span class="m">1</span> + <span class="m">2</span> * <span class="m">3</span> / <span class="m">4</span>
<span class="k">end</span>
</code></pre>

<p>Disassembly:</p>

<pre class="highlight"><code>        1       [2]     LOADK           0 -1    ; 2.5
        2       [2]     RETURN          0 2
        3       [3]     RETURN          0 1
</code></pre>

<p>Emacs Lisp:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">foo</span> ()
  (+ <span class="m">1</span> (/ (* <span class="m">2</span> <span class="m">3</span>) <span class="m">4.0</span>))
</code></pre>

<p>Disassembly:</p>

<pre class="highlight"><code>0	constant  2.5
1	return
</code></pre>

<p>That’s something we can count on so long as the operands are all
numeric literals (or also, for Python, string literals) that are
visible to the compiler. Don’t count on your operator overloads to
work here, though.</p>

<h3 id="allocation-optimization">Allocation optimization</h3>

<p>Optimizers often perform <em>escape analysis</em>, to determine if objects
allocated in a function ever become visible outside of that function. If
they don’t then these objects could potentially be stack-allocated
(instead of heap-allocated) or even be eliminated entirely.</p>

<p>None of the bytecode compilers are this sophisticated. However CPython
does have a trick up its sleeve: tuple optimization. Since tuples are
immutable, in certain circumstances CPython will reuse them and avoid
both the constructor and the allocation.</p>

<pre class="highlight"><code><span class="k">def</span> <span class="f">foo</span>():
    <span class="k">return</span> (<span class="m">1</span>, <span class="m">2</span>, <span class="m">3</span>)
</code></pre>

<p>Check it out, the tuple is used as a constant:</p>

<pre class="highlight"><code>  2           0 LOAD_CONST               1 ((1, 2, 3))
              2 RETURN_VALUE
</code></pre>

<p>Which we can detect by evaluating <code>foo() is foo()</code>, which is <code>True</code>.
Though deviate from this too much and the optimization is disabled.
Remember how CPython can’t optimize away variables, and that they
break constant folding? The break this, too:</p>

<pre class="highlight"><code><span class="k">def</span> <span class="f">foo</span>():
    x = <span class="m">1</span>
    <span class="k">return</span> (x, <span class="m">2</span>, <span class="m">3</span>)
</code></pre>

<p>Disassembly:</p>

<pre class="highlight"><code>  2           0 LOAD_CONST               1 (1)
              2 STORE_FAST               0 (x)
  3           4 LOAD_FAST                0 (x)
              6 LOAD_CONST               2 (2)
              8 LOAD_CONST               3 (3)
             10 BUILD_TUPLE              3
             12 RETURN_VALUE
</code></pre>

<p>This function might document that it always returns a simple tuple,
but we can tell if its being optimized or not using <code>is</code> like before:
<code>foo() is foo()</code> is now <code>False</code>! In some future version of Python with
a cleverer bytecode compiler, that expression might evaluate to
<code>True</code>. (Unless the <a href="https://docs.python.org/3/reference/">Python language specification</a> is specific
about this case, which I didn’t check.)</p>

<p>Note: Curiously PyPy replicates this exact behavior when examined with
<code>is</code>. Was that deliberate? I’m impressed that PyPy matches CPython’s
semantics so closely here.</p>

<p>Putting a mutable value, such as a list, in the tuple will also break
this optimization. But that’s not the compiler being dumb. That’s a
hard constraint on the compiler: the caller might change the mutable
component of the tuple, so it must always return a fresh copy.</p>

<p>Neither Lua nor Emacs Lisp have a language-level concept equivalent of
an immutable tuple, so there’s nothing to compare.</p>

<p>Other than the tuples situation in CPython, none of the bytecode
compilers eliminate unnecessary intermediate objects.</p>

<pre class="highlight"><code><span class="k">def</span> <span class="f">foo</span>():
    <span class="k">return</span> [<span class="m">1024</span>][<span class="m">0</span>]
</code></pre>

<p>Disassembly:</p>

<pre class="highlight"><code>  2           0 LOAD_CONST               1 (1024)
              2 BUILD_LIST               1
              4 LOAD_CONST               2 (0)
              6 BINARY_SUBSCR
              8 RETURN_VALUE
</code></pre>

<p>Lua:</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>()
    <span class="k">return</span> ({<span class="m">1024</span>})[<span class="m">1</span>]
<span class="k">end</span>
</code></pre>

<p>Disassembly:</p>

<pre class="highlight"><code>        1       [2]     NEWTABLE        0 1 0
        2       [2]     LOADK           1 -1    ; 1024
        3       [2]     SETLIST         0 1 1   ; 1
        4       [2]     GETTABLE        0 0 -2  ; 1
        5       [2]     RETURN          0 2
        6       [3]     RETURN          0 1
</code></pre>

<p>Emacs Lisp:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">foo</span> ()
  (car (list <span class="m">1024</span>)))
</code></pre>

<p>Disassembly:</p>

<pre class="highlight"><code>0	constant  1024
1	list1
2	car
3	return
</code></pre>

<h3 id="dont-expect-too-much">Don’t expect too much</h3>

<p>I could go on with lots of examples, looking at loop optimizations and
so on, and each case is almost certainly unoptimized. The general rule
of thumb is to simply not expect much from these bytecode compilers.
They’re very literal in their translation.</p>

<p>Working so much in C has put me in the habit of expecting all obvious
optimizations from the compiler. This frees me to be more expressive
in my code. Lots of things are cost-free thanks to these
optimizations, such as breaking a complex expression up into several
variables, naming my constants, or not using a local variable to
manually cache memory accesses. I’m confident the compiler will
optimize away my expressiveness. The catch is that <a href="/blog/2018/05/01/">clever compilers
can take things too far</a>, so I’ve got to be mindful of how it might
undermine my intentions — i.e. when I’m doing something unusual or not
strictly permitted.</p>

<p>These bytecode compilers will never truly surprise me. The cost is
that being more expressive in Python, Lua, or Emacs Lisp may reduce
performance at run time because it shows in the bytecode. Usually this
doesn’t matter, but sometimes it does.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>A JavaScript Typed Array Gotcha</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2019/01/23/"/>
    <id>urn:uuid:da8bd8c0-6003-3d77-5409-16db63420368</id>
    <updated>2019-01-23T02:50:30Z</updated>
    <category term="c"/><category term="javascript"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>JavaScript’s prefix increment and decrement operators can be
surprising when applied to typed arrays. It caught be by surprise when
I was <a href="https://github.com/skeeto/ulid-c">porting some C code</a> over <a href="https://github.com/skeeto/ulid-js">to JavaScript</a> Just
using your brain to execute this code, what do you believe is the
value of <code>r</code>?</p>

<pre class="highlight"><code><span class="k">let</span> array = <span class="k">new</span> Uint8Array([<span class="m">255</span>]);
<span class="k">let</span> r = ++array[<span class="m">0</span>];
</code></pre>

<p>The increment and decrement operators originated in the B programming
language. Its closest living relative today is C, and, as far as these
operators are concered, C can be considered an ancestor of JavaScript.
So what is the value of <code>r</code> in this similar C code?</p>

<pre class="highlight"><code><span class="t">uint8_t</span> array[] = {<span class="m">255</span>};
<span class="t">int</span> r = ++array[<span class="m">0</span>];
</code></pre>

<p>Of course, if they were the same then there would be nothing to write
about, so that should make it easier to guess if you aren’t sure. The
answer: In JavaScript, <code>r</code> is 256. In C, <code>r</code> is 0.</p>

<p>What happened to me was that I wrote an 80-bit integer increment
routine in C like this:</p>

<pre class="highlight"><code><span class="t">uint8_t</span> array[<span class="m">10</span>];
<span class="c">/* ... */</span>
<span class="k">for</span> (<span class="t">int</span> i = <span class="m">9</span>; i &gt;= <span class="m">0</span>; i--)
    <span class="k">if</span> (++array[i])
        <span class="k">break</span>;
</code></pre>

<p>But I was getting the wrong result over in JavaScript from essentially
the same code:</p>

<pre class="highlight"><code><span class="k">let</span> array = <span class="k">new</span> Uint8Array(<span class="m">10</span>);
<span class="c">/* ... */</span>
<span class="k">for</span> (<span class="k">let</span> i = <span class="m">9</span>; i &gt;= <span class="m">0</span>; i--)
    <span class="k">if</span> (++array[i])
        <span class="k">break</span>;
</code></pre>

<p>So what’s going on here?</p>

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

<p>The ES5 specification says this about <a href="https://es5.github.io/#x11.4.4">the prefix increment
operator</a>:</p>

<blockquote>
  <p>Let <em>expr</em> be the result of evaluating UnaryExpression.</p>

  <ol>
    <li>
      <p>Throw a SyntaxError exception if the following conditions are all
true: [omitted]</p>
    </li>
    <li>
      <p>Let <em>oldValue</em> be ToNumber(GetValue(<em>expr</em>)).</p>
    </li>
    <li>
      <p>Let <em>newValue</em> be the result of adding the value 1 to <em>oldValue</em>,
using the same rules as for the + operator (see 11.6.3).</p>
    </li>
    <li>
      <p>Call PutValue(<em>expr</em>, <em>newValue</em>).</p>
    </li>
  </ol>

  <p>Return <em>newValue</em>.</p>
</blockquote>

<p>So, <em>oldValue</em> is 255. This is a double precision float because all
numbers in JavaScript (outside of the bitwise operations) are double
precision floating point. Add 1 to this value to get 256, which is
<em>newValue</em>. When <em>newValue</em> is stored in the array via PutValue(), it’s
converted to an unsigned 8-bit integer, which truncates it to 0.</p>

<p>However, <em>newValue</em> is returned, not the value that was actually stored
in the array!</p>

<p>Since JavaScript is dynamically typed, this difference did not
actually matter until typed arrays are involved. I suspect if typed
arrays were in JavaScript from the beginning, the specified behavior
would be more in line with C.</p>

<p>This behavior isn’t limited to the prefix operators. Consider
assignment:</p>

<pre class="highlight"><code><span class="k">let</span> array = <span class="k">new</span> Uint8Array([<span class="m">255</span>]);
<span class="k">let</span> r = (array[<span class="m">0</span>] = array[<span class="m">0</span>] + <span class="m">1</span>);
<span class="k">let</span> s = (array[<span class="m">0</span>] += <span class="m">1</span>);
</code></pre>

<p>Both <code>r</code> and <code>s</code> will still be 256. The result of the assignment
operators is a similar story:</p>

<blockquote>
  <p>LeftHandSideExpression = AssignmentExpression is evaluated as
 follows:</p>

  <ol>
    <li>
      <p>Let <em>lref</em> be the result of evaluating LeftHandSideExpression.</p>
    </li>
    <li>
      <p>Let <em>rref</em> be the result of evaluating AssignmentExpression.</p>
    </li>
    <li>
      <p>Let <em>rval</em> be GetValue(<em>rref</em>).</p>
    </li>
    <li>
      <p>Throw a SyntaxError exception if the following conditions are all
true: [omitted]</p>
    </li>
    <li>
      <p>Call PutValue(<em>lref</em>, <em>rval</em>).</p>
    </li>
    <li>
      <p>Return <em>rval</em>.</p>
    </li>
  </ol>
</blockquote>

<p>Again, the result of the expression is independent of how it was stored
with PutValue().</p>

<h3 id="c-specification">C specification</h3>

<p>I’ll be referencing the original C89/C90 standard. The C specification
requires a little more work to get to the bottom of the issue. Starting
with 3.3.3.1 (Prefix increment and decrement operators):</p>

<blockquote>
  <p>The value of the operand of the prefix ++ operator is incremented. The
result is the new value of the operand after incrementation. The
expression ++E is equivalent to (E+=1).</p>
</blockquote>

<p>Later in 3.3.16.2 (Compound assignment):</p>

<blockquote>
  <p>A compound assignment of the form E1 op = E2 differs from the simple
assignment expression E1 = E1 op (E2) only in that the lvalue E1 is
evaluated only once.</p>
</blockquote>

<p>Then finally in 3.3.16 (Assignment operators):</p>

<blockquote>
  <p>An assignment operator stores a value in the object designated by
the left operand. An assignment expression has the value of the left
operand <strong>after the assignment</strong>, but is not an lvalue.</p>
</blockquote>

<p>So the result is explicitly the value after assignment. Let’s look at
this step by step after rewriting the expression.</p>

<pre class="highlight"><code><span class="t">int</span> r = (array[<span class="m">0</span>] = array[<span class="m">0</span>] + <span class="m">1</span>);
</code></pre>

<p>In C, all integer operations are performed with <em>at least</em> <code>int</code>
precision. Smaller integers are implicitly promoted to <code>int</code> before the
operation. The value of array[0] is 255, and, since <code>uint8_t</code> is smaller
than <code>int</code>, it gets promoted to <code>int</code>. Additionally, the literal
constant 1 is also an <code>int</code>, so there are actually two reasons for this
promotion.</p>

<p>So since these are <code>int</code> values, the result of the addition is 256, like
in JavaScript. To store the result, this value is then demoted to
<code>uint8_t</code> and truncated to 0. Finally, this post-assignment 0 is the
result of the expression, not the right-hand result as in JavaScript.</p>

<h3 id="specifications-are-useful">Specifications are useful</h3>

<p>These situations are why I prefer programming languages that have a
formal and approachable specification. If there’s no specification and
I’m observing <a href="https://old.reddit.com/r/matlab/comments/ae68bh/_/edmysxr/">undocumented, idiosyncratic behavior</a>, is this
just some subtle quirk of the current implementation — e.g. something
that might change without notice in the future — or is it intended
behavior that I can rely upon for correctness?</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>iter</code>) and <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Threads.html">native threads</a>
(<code>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>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>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>StopItertion</code> exception. In Emacs Lisp, it’s
an <code>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>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>iter-do</code> macro or (my preference)
the new <code>cl-loop</code> keyword <code>iter-by</code>.</p>

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

<pre class="highlight"><code>(iter-defun walk (list)
  (<span class="k">while</span> list
    (iter-yield (pop list))))
</code></pre>

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

<pre class="highlight"><code>(<span class="k">setf</span> i (walk '(<span class="v">:a</span> <span class="v">:b</span> <span class="v">:c</span>)))

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

<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>iter-next</code> invokes the
closure with its own internal protocol. It asks the closure to return
the next value (<code>:next</code> operation), and <code>iter-close</code> asks it to clean
itself up (<code>: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>print</code> and bring them back to
life with <code>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>switch</code>. I’d
lamented in the past that large <code>cond</code> and <code>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>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>walk</code> generator above into a form
that <em>roughly</em> resembles the expansion of <code>iter-defun</code>:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">walk</span> (list)
  (<span class="k">let</span> (state)
    (cl-flet* ((state-2 ()
                 (signal <span class="v">'iter-end-of-sequence</span> <span class="k">nil</span>))
               (state-1 ()
                 (<span class="k">prog1</span> (pop list)
                   (<span class="k">when</span> (null list)
                     (<span class="k">setf</span> state <span class="v">#'state-2</span>))))
               (state-0 ()
                 (<span class="k">if</span> (null list)
                     (state-2)
                   (<span class="k">setf</span> state <span class="v">#'state-1</span>)
                   (state-1))))
      (<span class="k">setf</span> state <span class="v">#'state-0</span>)
      (<span class="k">lambda</span> ()
        (funcall state)))))
</code></pre>

<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>funcall</code> rather than <code>iter-next</code>.</p>

<p>The <code>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>state</code> to point to a new part of
the generator body. The terminal state is obviously <code>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>unwind-protect</code>. It’s not valid to
yield inside an <code>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>unwind-protect</code>.</p>

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

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">walk</span> (list)
  (<span class="k">let</span> ((state <span class="m">0</span>))
    (<span class="k">cl-labels</span>
        ((closure ()
           (<span class="k">cl-case</span> state
             (<span class="m">0</span> (<span class="k">if</span> (null list)
                    (<span class="k">setf</span> state <span class="m">2</span>)
                  (<span class="k">setf</span> state <span class="m">1</span>))
                (closure))
             (<span class="m">1</span> (<span class="k">prog1</span> (pop list)
                  (<span class="k">when</span> (null list)
                    (<span class="k">setf</span> state <span class="m">2</span>))))
             (<span class="m">2</span> (signal <span class="v">'iter-end-of-sequence</span> <span class="k">nil</span>)))))
      <span class="v">#'closure</span>)))
</code></pre>

<p>When byte compiled on Emacs 26, that <code>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>

<pre class="highlight"><code>(<span class="k">setf</span> foo
      (<span class="k">let</span> ((list '(<span class="v">:a</span> <span class="v">:b</span> <span class="v">:c</span>)))
        (list
         (funcall
          (iter-lambda ()
            (<span class="k">while</span> list
              (iter-yield (pop list)))))
         (funcall
          (iter-lambda ()
            (<span class="k">while</span> list
              (iter-yield (pop list))))))))

(iter-next (nth <span class="m">0</span> foo))  <span class="c">; =&gt; :a</span>
(iter-next (nth <span class="m">1</span> foo))  <span class="c">; =&gt; :b</span>
(iter-next (nth <span class="m">0</span> foo))  <span class="c">; =&gt; :c</span>
</code></pre>

<p>For years there has been a <em>very</em> crude way to “pause” a function and
allow other functions to run: <code>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>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>

<pre class="highlight"><code><span class="c">;;; -*- lexical-binding: t; -*-</span>

(<span class="k">defvar</span> <span class="f">foo-counter-tls</span>)
(<span class="k">defvar</span> <span class="f">foo-path-tls</span>)

(<span class="k">defun</span> <span class="f">foo-make-thread</span> (path)
  (make-thread
   (<span class="k">lambda</span> ()
     (<span class="k">let</span> ((foo-counter-tls <span class="m">0</span>)
           (foo-name-tls path))
       ...))))
</code></pre>

<p>However, <strong><code>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>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>iter</code> generators, and they’re a lot more heavy-weight, needing an
entire thread per iterator object. This makes <code>thriter-close</code> all the
more important. On the other hand, these generators have no problem
yielding inside <code>unwind-protect</code>.</p>

<p>Originally this article was going to dive into the details of how
these thread-iterators worked, but <code>thriter</code> turned out to be quite a
bit more complicated than I anticipated, especially as I worked
towards feature matching <code>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>thriter-close</code> or garbage collection), <code>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>Emacs Lisp Lambda Expressions Are Not Self-Evaluating</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2018/02/22/"/>
    <id>urn:uuid:7a3cd1d1-a48c-3c9b-1564-eacde8b9aa4d</id>
    <updated>2018-02-22T21:30:57Z</updated>
    <category term="emacs"/><category term="elisp"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>This week I made a mistake that ultimately enlightened me about the
nature of function objects in Emacs Lisp. There are three kinds of
function objects, but they each behave very differently when evaluated
as objects.</p>

<p>But before we get to that, let’s talk about one of Emacs’
embarrassing, old missteps: <code>eval-after-load</code>.</p>

<h3 id="taming-an-old-dragon">Taming an old dragon</h3>

<p>One of the long-standing issues with Emacs is that loading Emacs Lisp
files (.el and .elc) is a slow process, even when those files have
been byte compiled. There are a number of dirty hacks in place to deal
with this issue, and the biggest and nastiest of them all is the
<a href="https://lwn.net/Articles/707615/"><em>dumper</em></a>, also known as <em>unexec</em>.</p>

<p>The Emacs you routinely use throughout the day is actually a previous
instance of Emacs that’s been resurrected from the dead. Your undead
Emacs was probably created months, if not years, earlier, back when it
was originally compiled. The first stage of compiling Emacs is to
compile a minimal C core called <code>temacs</code>. The second stage is loading
a bunch of Emacs Lisp files, then dumping a memory image in an
unportable, platform-dependent way. On Linux, this actually <a href="https://lwn.net/Articles/707615/">requires
special hooks in glibc</a>. The Emacs you know and love is this
dumped image loaded back into memory, continuing from where it left
off just after it was compiled. Regardless of your own feelings on the
matter, you have to admit <a href="/blog/2011/01/30/">this <em>is</em> a very lispy thing to do</a>.</p>

<p>There are two notable costs to Emacs’ dumper:</p>

<ol>
  <li>
    <p>The dumped image contains hard-coded memory addresses. This means
Emacs can’t be a <em>Position Independent Executable</em> (PIE). It can’t
take advantage of a security feature called <em>Address Space Layout
Randomization</em> (ASLR), which would increase the difficulty of
<a href="/blog/2017/07/19/">exploiting</a> some <a href="/blog/2012/09/28/">classes of bugs</a>. This might be
important to you if Emacs processes untrusted data, such as when it’s
used as <a href="/blog/2013/09/03/">a mail client</a>, <a href="https://github.com/skeeto/emacs-web-server">a web server</a> or generally
<a href="https://github.com/skeeto/elfeed">parses data downloaded across the network</a>.</p>
  </li>
  <li>
    <p>It’s not possible to cross-compile Emacs since it can only be dumped
by running <code>temacs</code> on its target platform. As an experiment I’ve
attempted to dump the Windows version of Emacs on Linux using
<a href="https://www.winehq.org/">Wine</a>, but was unsuccessful.</p>
  </li>
</ol>

<p>The good news is that there’s <a href="https://lists.gnu.org/archive/html/emacs-devel/2018-02/msg00347.html">a portable dumper</a> in the works
that makes this a lot less nasty. If you’re adventurous, you can
already disable dumping and run <code>temacs</code> directly by setting
<a href="https://lists.gnu.org/archive/html/bug-gnu-emacs/2016-11/msg00729.html"><code>CANNOT_DUMP=yes</code> at compile time</a>. Be warned, though, that a
non-dumped Emacs takes several seconds, or worse, to initialize
<em>before</em> it even begins loading your own configuration. It’s also
somewhat buggy since it seems nobody ever runs it this way
productively.</p>

<p>The other major way Emacs users have worked around slow loading is
aggressive use of lazy loading, generally via <em>autoloads</em>. The major
package interactive entry points are defined ahead of time as stub
functions. These stubs, when invoked, load the full package, which
overrides the stub definition, then finally the stub re-invokes the
new definition with the same arguments.</p>

<p>To further assist with lazy loading, an evaluated <code>defvar</code> form will
not override an existing global variable binding. This means you can,
to a certain extent, configure a package before it’s loaded. The
package will not clobber any existing configuration when it loads.
This also explains the bizarre interfaces for the various hook
functions, like <code>add-hook</code> and <code>run-hooks</code>. These accept symbols — the
<em>names</em> of the variables — rather than <em>values</em> of those variables as
would normally be the case. The <code>add-to-list</code> function does the same
thing. It’s all intended to cooperate with lazy loading, where the
variable may not have been defined yet.</p>

<h4 id="eval-after-load">eval-after-load</h4>

<p>Sometimes this isn’t enough and you need some some configuration to
take place after the package has been loaded, but without forcing it
to load early. That is, you need to tell Emacs “evaluate this code
after this particular package loads.” That’s where <code>eval-after-load</code>
comes into play, except for its fatal flaw: it takes the word “eval”
completely literally.</p>

<p>The first argument to <code>eval-after-load</code> is the name of a package. Fair
enough. The second argument is a form that will be passed to <code>eval</code>
after that package is loaded. Now hold on a minute. The general rule
of thumb is that if you’re calling <code>eval</code>, you’re probably doing
something seriously wrong, and this function is no exception. This is
<em>completely</em> the wrong mechanism for the task.</p>

<p>The second argument should have been a function — either a (sharp
quoted) symbol or a function object. And then instead of <code>eval</code> it
would be something more sensible, like <code>funcall</code>. Perhaps this
improved version would be named <code>call-after-load</code> or <code>run-after-load</code>.</p>

<p>The big problem with passing an s-expression is that it will be left
uncompiled due to being quoted. <a href="/blog/2017/12/14/">I’ve talked before about the
importance of evaluating your lambdas</a>. <code>eval-after-load</code> not
only encourages badly written Emacs Lisp, it demands it.</p>

<pre class="highlight"><code><span class="c">;;; BAD!</span>
(eval-after-load <span class="v">'simple-httpd</span>
                 '(push '(<span class="s">"c"</span> . <span class="s">"text/plain"</span>) httpd-mime-types))
</code></pre>

<p>This was all corrected in Emacs 25. If the second argument to
<code>eval-after-load</code> is a function — the result of applying <code>functionp</code> is
non-nil — then it uses <code>funcall</code>. There’s also a new macro,
<code>with-eval-after-load</code>, to package it all up nicely.</p>

<pre class="highlight"><code><span class="c">;;; Better (Emacs &gt;= 25 only)</span>
(eval-after-load <span class="v">'simple-httpd</span>
  (<span class="k">lambda</span> ()
    (push '(<span class="s">"c"</span> . <span class="s">"text/plain"</span>) httpd-mime-types)))

<span class="c">;;; Best (Emacs &gt;= 25 only)</span>
(with-eval-after-load <span class="v">'simple-httpd</span>
  (push '(<span class="s">"c"</span> . <span class="s">"text/plain"</span>) httpd-mime-types))
</code></pre>

<p>Though in both of these examples the compiler will likely warn about
<code>httpd-mime-types</code> not being defined. That’s a problem for another
day.</p>

<h4 id="a-workaround">A workaround</h4>

<p>But what if you <em>need</em> to use Emacs 24, as was the <a href="https://github.com/skeeto/elfeed/pull/268">situation that
sparked this article</a>? What can we do with the bad version of
<code>eval-after-load</code>? We could situate a lambda such that it’s evaluated,
but then smuggle the resulting function object into the form passed to
<code>eval-after-load</code>, all using a backquote.</p>

<pre class="highlight"><code><span class="c">;;; Note: this is subtly broken</span>
(eval-after-load <span class="v">'simple-httpd</span>
  `(funcall
    ,(<span class="k">lambda</span> ()
       (push '(<span class="s">"c"</span> . <span class="s">"text/plain"</span>) httpd-mime-types)))
</code></pre>

<p>When everything is compiled, the backquoted form evalutes to this:</p>

<pre class="highlight"><code>(funcall #[<span class="m">0</span> &lt;bytecode&gt; [httpd-mime-types (<span class="s">"c"</span> . <span class="s">"text/plain"</span>)] <span class="m">2</span>])
</code></pre>

<p>Where the second value (<code>#[...]</code>) is a <a href="/blog/2014/01/04/">byte-code object</a>.
However, as the comment notes, this is subtly broken. A cleaner and
correct way to solve all this is with a named function. The damage
caused by <code>eval-after-load</code> will have been (mostly) minimized.</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">my-simple-httpd-hook</span> ()
  (push '(<span class="s">"c"</span> . <span class="s">"text/plain"</span>) httpd-mime-types))

(eval-after-load <span class="v">'simple-httpd</span>
  '(funcall <span class="v">#'my-simple-httpd-hook</span>))
</code></pre>

<p>But, let’s go back to the anonymous function solution. What was broken
about it? It all has to do with evaluating function objects.</p>

<h3 id="evaluating-function-objects">Evaluating function objects</h3>

<p>So what happens when we evaluate an expression like the one above with
<code>eval</code>? Here’s what it looks like again.</p>

<pre class="highlight"><code>(funcall #[...])
</code></pre>

<p>First, <code>eval</code> notices it’s been given a non-empty list, so it’s probably
a function call. The first argument is the name of the function to be
called (<code>funcall</code>) and the remaining elements are its arguments. <em>But</em>
each of these elements must be evaluated first, and the <em>result</em> of that
evaluation becomes the arguments.</p>

<p>Any value that isn’t a list or a symbol is <em>self-evaluating</em>. That is,
it evaluates to its own value:</p>

<pre class="highlight"><code>(eval <span class="m">10</span>)
<span class="c">;; =&gt; 10</span>
</code></pre>

<p>If the value is a symbol, it’s treated as a variable. If the value is a
list, it goes through the function call process I’m describing (or one
of a number of other special cases, such as macro expansion, lambda
expressions, and special forms).</p>

<p>So, conceptually <code>eval</code> recurses on the function object <code>#[...]</code>. A
function object is not a list or a symbol, so it’s self-evaluating. No
problem.</p>

<pre class="highlight"><code><span class="c">;; Byte-code objects are self-evaluating</span>

(<span class="k">let</span> ((x (byte-compile (<span class="k">lambda</span> ()))))
  (eq x (eval x)))
<span class="c">;; =&gt; t</span>
</code></pre>

<p>What if this code <em>wasn’t</em> compiled? Rather than a byte-code object,
we’d have some other kind of function object for the interpreter.
Let’s examine the dynamic scope (<em>shudder</em>) case. Here, a lambda
<em>appears</em> to evaluate to itself, but appearances can be deceiving:</p>

<pre class="highlight"><code>(eval (<span class="k">lambda</span> ())
<span class="c">;; =&gt; (lambda ())</span>
</code></pre>

<p>However, this is not self-evaluation. <strong>Lambda expressions are not
self-evaluating</strong>. It’s merely <em>coincidence</em> that the result of
evaluating a lambda expression looks like the original expression.
This is just how the Emacs Lisp interpreter is currently implemented
and, strictly speaking, it’s an implementation detail that <em>just so
happens</em> to be mostly compatible with byte-code objects being
self-evaluating. It would be a mistake to rely on this.</p>

<p>Instead, <strong>dynamic scope lambda expression evaluation is
<a href="https://labs.spotify.com/2013/06/18/creative-usernames/">idempotent</a>.</strong> Applying <code>eval</code> to the result will return
an <code>equal</code>, but not identical (<code>eq</code>), expression. In contrast, a
self-evaluating value is also idempotent under evaluation, but with
<code>eq</code> results.</p>

<pre class="highlight"><code><span class="c">;; Not self-evaluating:</span>

(<span class="k">let</span> ((x '(<span class="k">lambda</span> ())))
  (eq x (eval x)))
<span class="c">;; =&gt; nil</span>

<span class="c">;; Evaluation is idempotent:</span>

(<span class="k">let</span> ((x '(<span class="k">lambda</span> ())))
  (equal x (eval x)))
<span class="c">;; =&gt; t</span>

(<span class="k">let</span> ((x '(<span class="k">lambda</span> ())))
  (equal x (eval (eval x))))
<span class="c">;; =&gt; t</span>
</code></pre>

<p>So, with dynamic scope, the subtly broken backquote example will still
work, but only by sheer luck. Under lexical scope, the situation isn’t
so lucky:</p>

<pre class="highlight"><code><span class="c">;;; -*- lexical-scope: t; -*-</span>

(<span class="k">lambda</span> ())
<span class="c">;; =&gt; (closure (t) nil)</span>
</code></pre>

<p>These interpreted lambda functions are neither self-evaluating nor
idempotent. Passing <code>t</code> as the second argument to <code>eval</code> tells it to
use lexical scope, as shown below:</p>

<pre class="highlight"><code><span class="c">;; Not self-evaluating:</span>

(<span class="k">let</span> ((x '(<span class="k">lambda</span> ())))
  (eq x (eval x <span class="k">t</span>)))
<span class="c">;; =&gt; nil</span>

<span class="c">;; Not idempotent:</span>

(<span class="k">let</span> ((x '(<span class="k">lambda</span> ())))
  (equal x (eval x <span class="k">t</span>)))
<span class="c">;; =&gt; nil</span>

(<span class="k">let</span> ((x '(<span class="k">lambda</span> ())))
  (equal x (eval (eval x <span class="k">t</span>) <span class="k">t</span>)))
<span class="c">;; error: (void-function closure)</span>
</code></pre>

<p>I can <a href="/blog/2017/05/03/">imagine an implementation</a> of Emacs Lisp where dynamic
scope lambda expressions are in the same boat, where they’re not even
idempotent. For example:</p>

<pre class="highlight"><code><span class="c">;;; -*- lexical-binding: nil; -*-</span>

(<span class="k">lambda</span> ())
<span class="c">;; =&gt; (totally-not-a-closure ())</span>
</code></pre>

<p>Most Emacs Lisp would work just fine under this change, and only code
that makes some kind of logical mistake — where there’s nested
evaluation of lambda expressions — would break. This essentially
already happened when lots of code was quietly switched over to
lexical scope after Emacs 24. Lambda idempotency was lost and
well-written code didn’t notice.</p>

<p>There’s a temptation here for Emacs to define a <code>closure</code> function or
special form that would allow interpreter closure objects to be either
self-evaluating or idempotent. This would be a mistake. It would only
serve as a hack that covers up logical mistakes that lead to nested
evaluation. Much better to catch those problems early.</p>

<h3 id="solving-the-problem-with-one-character">Solving the problem with one character</h3>

<p>So how do we fix the subtly broken example? With a strategically
placed quote right before the comma.</p>

<pre class="highlight"><code>(eval-after-load <span class="v">'simple-httpd</span>
  `(funcall
    ',(<span class="k">lambda</span> ()
        (push '(<span class="s">"c"</span> . <span class="s">"text/plain"</span>) httpd-mime-types)))
</code></pre>

<p>So the form passed to <code>eval-after-load</code> becomes:</p>

<pre class="highlight"><code><span class="c">;; Compiled:</span>
(funcall (<span class="k">quote</span> #[...]))

<span class="c">;; Dynamic scope:</span>
(funcall (<span class="k">quote</span> (<span class="k">lambda</span> () ...)))

<span class="c">;; Lexical scope:</span>
(funcall (<span class="k">quote</span> (closure (<span class="k">t</span>) () ...)))
</code></pre>

<p>The quote prevents <code>eval</code> from evaluating the function object, which
would be either needless or harmful. There’s also an argument to be
made that this is a perfect situation for a sharp-quote (<code>#'</code>), which
exists to quote functions.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>What's in an Emacs Lambda</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2017/12/14/"/>
    <id>urn:uuid:efcc8cf7-11d3-3bd3-9fc9-a23e80f7bf33</id>
    <updated>2017-12-14T18:18:57Z</updated>
    <category term="emacs"/><category term="elisp"/><category term="compsci"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>There was recently some <a href="https://old.reddit.com/r/emacs/comments/7h23ed/dynamically_construct_a_lambda_function/">interesting discussion</a> about correctly
using backquotes to express a mixture of data and code. Since lambda
expressions <em>seem</em> to evaluate to themselves, what’s the difference?
For example, an association list of operations:</p>

<pre class="highlight"><code>'((add . (<span class="k">lambda</span> (a b) (+ a b)))
  (sub . (<span class="k">lambda</span> (a b) (- a b)))
  (mul . (<span class="k">lambda</span> (a b) (* a b)))
  (div . (<span class="k">lambda</span> (a b) (/ a b))))
</code></pre>

<p>It looks like it would work, and indeed it does work in this case.
However, there are good reasons to actually evaluate those lambda
expressions. Eventually invoking the lambda expressions in the quoted
form above are equivalent to using <code>eval</code>. So, instead, prefer the
backquote form:</p>

<pre class="highlight"><code>`((add . ,(<span class="k">lambda</span> (a b) (+ a b)))
  (sub . ,(<span class="k">lambda</span> (a b) (- a b)))
  (mul . ,(<span class="k">lambda</span> (a b) (* a b)))
  (div . ,(<span class="k">lambda</span> (a b) (/ a b))))
</code></pre>

<p>There are a lot of interesting things to say about this, but let’s
first reduce it to two very simple cases:</p>

<pre class="highlight"><code>(<span class="k">lambda</span> (x) x)

'(<span class="k">lambda</span> (x) x)
</code></pre>

<p>What’s the difference between these two forms? The first is a lambda
expression, and it evaluates to a function object. The other is a quoted
list that <em>looks like</em> a lambda expression, and it evaluates to a list —
a piece of data.</p>

<p>A naive evaluation of these expressions in <code>*scratch*</code> (<code>C-x C-e</code>)
suggests they are are identical, and so it would seem that quoting a
lambda expression doesn’t really matter:</p>

<pre class="highlight"><code>(<span class="k">lambda</span> (x) x)
<span class="c">;; =&gt; (lambda (x) x)</span>

'(<span class="k">lambda</span> (x) x)
<span class="c">;; =&gt; (lambda (x) x)</span>
</code></pre>

<p>However, there are two common situations where this is not the case:
<strong>byte compilation</strong> and <strong>lexical scope</strong>.</p>

<h3 id="lambda-under-byte-compilation">Lambda under byte compilation</h3>

<p>It’s a little trickier to evaluate these forms byte compiled in the
scratch buffer since that doesn’t happen automatically. But if it did,
it would look like this:</p>

<pre class="highlight"><code><span class="c">;;; -*- lexical-binding: nil; -*-</span>

(<span class="k">lambda</span> (x) x)
<span class="c">;; =&gt; #[(x) "\010\207" [x] 1]</span>

'(<span class="k">lambda</span> (x) x)
<span class="c">;; =&gt; (lambda (x) x)</span>
</code></pre>

<p>The <code>#[...]</code> is the syntax for a byte-code function object. As
discussed in detail in <a href="/blog/2014/01/04/">my byte-code internals article</a>, it’s a
special vector object that contains byte-code, and other metadata, for
evaluation by Emacs’ virtual stack machine. Elisp is one of very few
languages with <a href="/blog/2013/12/30/">readable function objects</a>, and this feature is
core to its ahead-of-time byte compilation.</p>

<p>The quote, by definition, prevents evaluation, and so inhibits byte
compilation of the lambda expression. It’s vital that the byte compiler
does not try to guess the programmer’s intent and compile the expression
anyway, since that would interfere with lists that just so happen to
look like lambda expressions — i.e. any list containing the <code>lambda</code>
symbol.</p>

<p>There are three reasons you want your lambda expressions to get byte
compiled:</p>

<ul>
  <li>
    <p>Byte-compiled functions are significantly faster. That’s the main
purpose for byte compilation after all.</p>
  </li>
  <li>
    <p>The compiler performs static checks, producing warnings and errors
ahead of time. This lets you spot certain classes of problems before
they occur. The static analysis is even better under lexical scope due
to its tighter semantics.</p>
  </li>
  <li>
    <p>Under lexical scope, byte-compiled closures may use less memory. More
specifically, they won’t accidentally keep objects alive longer than
necessary. I’ve never seen a name for this implementation issue, but I
call it <em>overcapturing</em>. More on this later.</p>
  </li>
</ul>

<p>While it’s common for personal configurations to skip byte compilation,
Elisp should still generally be written as if it were going to be byte
compiled. General rule of thumb: <strong>Ensure your lambda expressions are
actually evaluated.</strong></p>

<h3 id="lambda-in-lexical-scope">Lambda in lexical scope</h3>

<p>As I’ve stressed many times, <a href="/blog/2016/12/22/">you should <em>always</em> use lexical
scope</a>. There’s no practical disadvantage or trade-off involved.
Just do it.</p>

<p>Once lexical scope is enabled, the two expressions diverge even without
byte compilation:</p>

<pre class="highlight"><code><span class="c">;;; -*- lexical-binding: t; -*-</span>

(<span class="k">lambda</span> (x) x)
<span class="c">;; =&gt; (closure (t) (x) x)</span>

'(<span class="k">lambda</span> (x) x)
<span class="c">;; =&gt; (lambda (x) x)</span>
</code></pre>

<p>Under lexical scope, lambda expressions evaluate to <em>closures</em>.
Closures capture their lexical environment in their closure object —
nothing in this particular case. It’s a type of function object,
making it a valid first argument to <code>funcall</code>.</p>

<p>Since the quote prevents the second expression from being evaluated,
semantically it evaluates to a list that just so happens to look like
a (non-closure) function object. <strong>Invoking a <em>data</em> object as a
function is like using <code>eval</code></strong> — i.e. executing data as code.
Everyone already knows <code>eval</code> should not be used lightly.</p>

<p>It’s a little more interesting to look at a closure that actually
captures a variable, so here’s a definition for <code>constantly</code>, a
higher-order function that returns a closure that accepts any number of
arguments and returns a particular constant:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">constantly</span> (x)
  (<span class="k">lambda</span> (<span class="k">&amp;rest</span> _) x))
</code></pre>

<p>Without byte compiling it, here’s an example of its return value:</p>

<pre class="highlight"><code>(constantly <span class="v">:foo</span>)
<span class="c">;; =&gt; (closure ((x . :foo) t) (&amp;rest _) x)</span>
</code></pre>

<p>The environment has been captured as an association list (with a
trailing <code>t</code>), and we can plainly see that the variable <code>x</code> is bound to
the symbol <code>:foo</code> in this closure. Consider that we could manipulate
this data structure (e.g. <code>setcdr</code> or <code>setf</code>) to change the binding of
<code>x</code> for this closure. <em>This is essentially how closures mutate their own
environment.</em> Moreover, closures from the same environment share
structure, so such mutations are also shared. More on this later.</p>

<p>Semantically, closures are distinct objects (via <code>eq</code>), even if the
variables they close over are bound to the same value. This is because
they each have a distinct environment attached to them, even if in
some invisible way.</p>

<pre class="highlight"><code>(eq (constantly <span class="v">:foo</span>) (constantly <span class="v">:foo</span>))
<span class="c">;; =&gt; nil</span>
</code></pre>

<p>Without byte compilation, this is true <em>even when there’s no lexical
environment to capture</em>:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">dummy</span> ()
  (<span class="k">lambda</span> () <span class="k">t</span>))

(eq (dummy) (dummy))
<span class="c">;; =&gt; nil</span>
</code></pre>

<p>The byte compiler is smart, though. <a href="/blog/2017/01/30/">As an optimization</a>, the
same closure object is reused when possible, avoiding unnecessary
work, including multiple object allocations. Though this is a bit of
an abstraction leak. A function can (ab)use this to introspect whether
it’s been byte compiled:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">have-i-been-compiled-p</span> ()
  (<span class="k">let</span> ((funcs (vector <span class="k">nil</span> <span class="k">nil</span>)))
    (<span class="k">dotimes</span> (i <span class="m">2</span>)
      (<span class="k">setf</span> (aref funcs i) (<span class="k">lambda</span> ())))
    (eq (aref funcs <span class="m">0</span>) (aref funcs <span class="m">1</span>))))

(have-i-been-compiled-p)
<span class="c">;; =&gt; nil</span>

(byte-compile <span class="v">'have-i-been-compiled-p</span>)

(have-i-been-compiled-p)
<span class="c">;; =&gt; t</span>
</code></pre>

<p>The trick here is to evaluate the exact same non-capturing lambda
expression twice, which requires a loop (or at least some sort of
branch). <em>Semantically</em> we should think of these closures as being
distinct objects, but, if we squint our eyes a bit, we can see the
effects of the behind-the-scenes optimization.</p>

<p>Don’t actually do this in practice, of course. That’s what
<code>byte-code-function-p</code> is for, which won’t rely on a subtle
implementation detail.</p>

<h3 id="overcapturing">Overcapturing</h3>

<p>I mentioned before that one of the potential gotchas of not byte
compiling your lambda expressions is overcapturing closure variables in
the interpreter.</p>

<p>To evaluate lisp code, Emacs has both an interpreter and a virtual
machine. The interpreter evaluates code in list form: cons cells,
numbers, symbols, etc. The byte compiler is like the interpreter, but
instead of directly executing those forms, it emits byte-code that, when
evaluated by the virtual machine, produces identical visible results to
the interpreter — <em>in theory</em>.</p>

<p>What this means is that <strong>Emacs contains two different implementations
of Emacs Lisp</strong>, one in the interpreter and one in the byte compiler.
The Emacs developers have been maintaining and expanding these
implementations side-by-side for decades. A pitfall to this approach
is that the <em>implementations can, and do, diverge in their behavior</em>.
We saw this above with that introspective function, and it <a href="/blog/2013/01/22/">comes up
in practice with advice</a>.</p>

<p>Another way they diverge is in closure variable capture. For example:</p>

<pre class="highlight"><code><span class="c">;;; -*- lexical-binding: t; -*-</span>

(<span class="k">defun</span> <span class="f">overcapture</span> (x y)
  (<span class="k">when</span> y
    (<span class="k">lambda</span> () x)))

(overcapture <span class="v">:x</span> <span class="v">:some-big-value</span>)
<span class="c">;; =&gt; (closure ((y . :some-big-value) (x . :x) t) nil x)</span>
</code></pre>

<p>Notice that the closure captured <code>y</code> even though it’s unnecessary.
This is because the interpreter doesn’t, and shouldn’t, take the time
to analyze the body of the lambda to determine which variables should
be captured. That would need to happen at run-time each time the
lambda is evaluated, which would make the interpreter much slower.
Overcapturing can get pretty messy if macros are introducing their own
hidden variables.</p>

<p>On the other hand, the byte compiler can do this analysis just once at
compile-time. And it’s already doing the analysis as part of its job.
It can avoid this problem easily:</p>

<pre class="highlight"><code>(overcapture <span class="v">:x</span> <span class="v">:some-big-value</span>)
<span class="c">;; =&gt; #[0 "\300\207" [:x] 1]</span>
</code></pre>

<p>It’s clear that <code>:some-big-value</code> isn’t present in the closure.</p>

<p>But… how does this work?</p>

<h3 id="how-byte-compiled-closures-are-constructed">How byte compiled closures are constructed</h3>

<p>Recall from the <a href="/blog/2014/01/04/">internals article</a> that the four core elements of a
byte-code function object are:</p>

<ol>
  <li>Parameter specification</li>
  <li>Byte-code string (opcodes)</li>
  <li>Constants vector</li>
  <li>Maximum stack usage</li>
</ol>

<p>While a closure <em>seems</em> like compiling a whole new function each time
the lambda expression is evaluated, there’s actually not that much to
it! Namely, <a href="/blog/2017/01/08/">the <em>behavior</em> of the function remains the same</a>. Only
the closed-over environment changes.</p>

<p>What this means is that closures produced by a common lambda
expression can all share the same byte-code string (second element).
Their bodies are identical, so they compile to the same byte-code.
Where they differ are in their constants vector (third element), which
gets filled out according to the closed over environment. It’s clear
just from examining the outputs:</p>

<pre class="highlight"><code>(constantly <span class="v">:a</span>)
<span class="c">;; =&gt; #[128 "\300\207" [:a] 2]</span>

(constantly <span class="v">:b</span>)
<span class="c">;; =&gt; #[128 "\300\207" [:b] 2]</span>

</code></pre>

<p><code>constantly</code> has three of the four components of the closure in its own
constant pool. Its job is to construct the constants vector, and then
assemble the whole thing into a byte-code function object (<code>#[...]</code>).
Here it is with <code>M-x disassemble</code>:</p>

<pre class="highlight"><code>0       constant  make-byte-code
1       constant  128
2       constant  "\300\207"
4       constant  vector
5       stack-ref 4
6       call      1
7       constant  2
8       call      4
9       return
</code></pre>

<p>(Note: since byte compiler doesn’t produce perfectly optimal code, I’ve
simplified it for this discussion.)</p>

<p>It pushes most of its constants on the stack. Then the <code>stack-ref 5</code> (5)
puts <code>x</code> on the stack. Then it calls <code>vector</code> to create the constants
vector (6). Finally, it constructs the function object (<code>#[...]</code>) by
calling <code>make-byte-code</code> (8).</p>

<p>Since this might be clearer, here’s the same thing expressed back in
terms of Elisp:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">constantly</span> (x)
  (make-byte-code <span class="m">128</span> <span class="s">"\300\207"</span> (vector x) <span class="m">2</span>))
</code></pre>

<p>To see the disassembly of the closure’s byte-code:</p>

<pre class="highlight"><code>(disassemble (constantly <span class="v">:x</span>))
</code></pre>

<p>The result isn’t very surprising:</p>

<pre class="highlight"><code>0       constant  :x
1       return
</code></pre>

<p>Things get a little more interesting when mutation is involved. Consider
this adder closure generator, which mutates its environment every time
it’s called:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">adder</span> ()
  (<span class="k">let</span> ((total <span class="m">0</span>))
    (<span class="k">lambda</span> () (cl-incf total))))

(<span class="k">let</span> ((count (adder)))
  (funcall count)
  (funcall count)
  (funcall count))
<span class="c">;; =&gt; 3</span>

(adder)
<span class="c">;; =&gt; #[0 "\300\211\242T\240\207" [(0)] 2]</span>
</code></pre>

<p>The adder essentially works like this:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">adder</span> ()
  (make-byte-code <span class="m">0</span> <span class="s">"\300\211\242T\240\207"</span> (vector (list <span class="m">0</span>)) <span class="m">2</span>))
</code></pre>

<p><em>In theory</em>, this closure could operate by mutating its constants vector
directly. But that wouldn’t be much of a <em>constants</em> vector, now would
it!? Instead, mutated variables are <em>boxed</em> inside a cons cell. Closures
don’t share constant vectors, so the main reason for boxing is to share
variables between closures from the same environment. That is, they have
the same cons in each of their constant vectors.</p>

<p>There’s no equivalent Elisp for the closure in <code>adder</code>, so here’s the
disassembly:</p>

<pre class="highlight"><code>0       constant  (0)
1       dup
2       car-safe
3       add1
4       setcar
5       return
</code></pre>

<p>It puts two references to boxed integer on the stack (<code>constant</code>,
<code>dup</code>), unboxes the top one (<code>car-safe</code>), increments that unboxed
integer, stores it back in the box (<code>setcar</code>) via the bottom reference,
leaving the incremented value behind to be returned.</p>

<p>This all gets a little more interesting when closures interact:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">fancy-adder</span> ()
  (<span class="k">let</span> ((total <span class="m">0</span>))
    `(<span class="v">:add</span> ,(<span class="k">lambda</span> () (cl-incf total))
      <span class="v">:set</span> ,(<span class="k">lambda</span> (v) (<span class="k">setf</span> total v))
      <span class="v">:get</span> ,(<span class="k">lambda</span> () total))))

(<span class="k">let</span> ((counter (fancy-adder)))
  (funcall (plist-get counter <span class="v">:set</span>) <span class="m">100</span>)
  (funcall (plist-get counter <span class="v">:add</span>))
  (funcall (plist-get counter <span class="v">:add</span>))
  (funcall (plist-get counter <span class="v">:get</span>)))
<span class="c">;; =&gt; 102</span>

(fancy-adder)
<span class="c">;; =&gt; (:add #[0 "\300\211\242T\240\207" [(0)] 2]</span>
<span class="c">;;     :set #[257 "\300\001\240\207" [(0)] 3]</span>
<span class="c">;;     :get #[0 "\300\242\207" [(0)] 1])</span>
</code></pre>

<p>This is starting to resemble object oriented programming, with methods
acting upon fields stored in a common, closed-over environment.</p>

<p>All three closures share a common variable, <code>total</code>. Since I didn’t
use <code>print-circle</code>, this isn’t obvious from the last result, but each
of those <code>(0)</code> conses are the same object. When one closure mutates
the box, they all see the change. Here’s essentially how <code>fancy-adder</code>
is transformed by the byte compiler:</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">fancy-adder</span> ()
  (<span class="k">let</span> ((box (list <span class="m">0</span>)))
    (list <span class="v">:add</span> (make-byte-code <span class="m">0</span> <span class="s">"\300\211\242T\240\207"</span> (vector box) <span class="m">2</span>)
          <span class="v">:set</span> (make-byte-code <span class="m">257</span> <span class="s">"\300\001\240\207"</span> (vector box) <span class="m">3</span>)
          <span class="v">:get</span> (make-byte-code <span class="m">0</span> <span class="s">"\300\242\207"</span> (vector box) <span class="m">1</span>))))
</code></pre>

<p>The backquote in the original <code>fancy-adder</code> brings this article full
circle. This final example wouldn’t work correctly if those lambdas
weren’t evaluated properly.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>The Adversarial Implementation</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2017/05/03/"/>
    <id>urn:uuid:e6f370f9-1d35-3295-3bd5-74ae20c52a0e</id>
    <updated>2017-05-03T17:51:53Z</updated>
    <category term="c"/><category term="python"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>When <a href="/blog/2017/03/30/">coding against a standard</a>, whether it’s a programming
language specification or an open API with multiple vendors, a common
concern is the conformity of a particular construct to the standard.
This cannot be determined simply by experimentation, since a piece of
code may work correctly due only to the specifics of a particular
implementation. It works <em>today</em> with <em>this</em> implementation, but it
may not work <em>tomorrow</em> or with a <em>different</em> implementation.
Sometimes an implementation will warn about the use of non-standard
behavior, but this isn’t always the case.</p>

<p>When I’m reasoning about whether or not something is allowed, I like to
imagine an <em>adversarial implementation</em>. If the standard allows some
freedom, this implementation takes an imaginative or unique approach. It
chooses <a href="/blog/2016/05/30/">non-obvious interpretations</a> with possibly unexpected,
but valid, results. This is nearly the opposite of <a href="https://groups.google.com/forum/m/#!msg/boring-crypto/48qa1kWignU/o8GGp2K1DAAJ">djb’s hypothetical
boringcc</a>, though some of the ideas are similar.</p>

<p>Many argue that <a href="http://yarchive.net/comp/linux/gcc.html">this is already the case</a> with modern C and C++
optimizing compilers. Compiler writers are already <a href="http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html">creative with the
standard</a> in order to squeeze out more performance, even if it’s
at odds with the programmer’s actual intentions. The most prominent
example in C and C++ is <em>strict aliasing</em>, where the optimizer is
deliberately blinded to certain kinds of aliasing because the standard
allows it to be, eliminating some (possibly important) loads. This
happens despite the compiler’s ability to trivially prove that two
particular objects really do alias.</p>

<p>I want to be clear that I’m not talking about the <a href="http://www.catb.org/jargon/html/N/nasal-demons.html">nasal daemon</a>
kind of creativity. That’s not a helpful thought experiment. What I
mean is this: <strong>Can I imagine a conforming implementation that breaks
any assumptions made by the code?</strong></p>

<p>In practice, compilers typically have to bridge multiple
specifications: the language standard, the <a href="/blog/2016/11/17/">platform ABI</a>, and
operating system interface (process startup, syscalls, etc.). This
really ties its hands on how creative it can be with any one of the
specifications. Depending on the situation, the imaginary adversarial
implementation isn’t necessarily running on any particular platform.
If our program is expected to have a long life, useful for many years
to come, we should avoid making too many assumptions about future
computers and imagine an adversarial compiler with few limitations.</p>

<h3 id="c-example">C example</h3>

<p>Take this bit of C:</p>

<pre class="highlight"><code>printf(<span class="s">"%d"</span>, <span class="k">sizeof</span>(foo));
</code></pre>

<p>The <code>printf</code> function is variadic, and it relies entirely on the format
string in order to correctly handle all its arguments. The <code>%d</code>
specifier means that its matching argument is of type <code>int</code>. The result
of the <code>sizeof</code> operator is an integer of type <code>size_t</code>, which has a
different sign and may even be a different size.</p>

<p>Typically this code will work just fine. An <code>int</code> and <code>size_t</code> are
generally passed the same way, the actual value probably fits in an
<code>int</code>, and two’s complement means the signedness isn’t an issue due to
the value being positive. From the <code>printf</code> point of view, it
typically can’t detect that the type is wrong, so everything works by
chance. In fact, it’s hard to imagine a real situation where this
wouldn’t work fine.</p>

<p>However, this still undefined behavior — a scenario where a creative
adversarial implementation can break things. In this case there are a
few options for an adversarial implementation:</p>

<ol>
  <li>Arguments of type <code>int</code> and <code>size_t</code> are passed differently, so
<code>printf</code> will load the argument it from the wrong place.</li>
  <li>The implementation doesn’t use two’s complement and even small
positive values have different bit representations.</li>
  <li>The type of <code>foo</code> is given crazy padding for arbitrary reasons that
makes it so large it doesn’t fit in an <code>int</code>.</li>
</ol>

<p>What’s interesting about #1 is that <em>this has actually happened</em>. For
example, here’s a C source file.</p>

<pre class="highlight"><code><span class="t">float</span> <span class="f">foo</span>(<span class="t">float</span> x, <span class="t">int</span> y);

<span class="t">float</span>
<span class="f">bar</span>(<span class="t">int</span> y)
{
    <span class="k">return</span> foo(<span class="m">0.0f</span>, y);
}
</code></pre>

<p>And in another source file:</p>

<pre class="highlight"><code><span class="t">float</span>
<span class="f">foo</span>(<span class="t">int</span> x, <span class="t">int</span> y)
{
    (<span class="t">void</span>)x;  <span class="c">// ignore x</span>
    <span class="k">return</span> y * <span class="m">2.0f</span>;
}
</code></pre>

<p>The type of argument <code>x</code> differs between the prototype and the
definition, which is undefined behavior. However, since this argument
is ignored, this code will still work correctly on many different
real-world computers, particularly where <code>float</code> and <code>int</code> arguments
are passed the same way (i.e. on the stack).</p>

<p>However, in 2003 the x86-64 CPU arrived with its new System V ABI.
Floating point and integer arguments were now passed differently, and
the types of preceding arguments mattered when deciding which register
to use. Some constructs that worked fine, by chance, prior to 2003 would
soon stop working due to what may have seemed like an adversarial
implementation years before.</p>

<h3 id="python-example">Python example</h3>

<p>Let’s look at some Python. This snippet opens a file a million times
without closing any handles.</p>

<pre class="highlight"><code><span class="k">for</span> i <span class="k">in</span> range(<span class="m">1</span>, <span class="m">1000000</span>):
    f = open(<span class="s">"/dev/null"</span>, <span class="s">"r"</span>)
</code></pre>

<p>Assuming you have a <code>/dev/null</code>, this code will work fine without
throwing any exceptions on CPython, the most widely used Python
implementation. CPython uses a deterministic reference counting scheme,
and the handle is automatically closed as soon as its variable falls out
of scope. It’s like having an invisible <code>f.close()</code> at the end of the
block.</p>

<p>However, this code is incorrect. The deterministic handle closing an
implementation behavior, <a href="https://docs.python.org/3/reference/datamodel.html">not part of the specification</a>. The
operating system limits the number of files a process can have open at
once, and there’s a risk that this resource will run out even though
none of those handles are reachable. Imagine an adversarial Python
implementation trying to break this code. It could sufficiently delay
garbage collection, or even <a href="https://web.archive.org/web/0/https://blogs.msdn.microsoft.com/oldnewthing/20100809-00/?p=13203">have infinite memory</a>, omitting
garbage collection altogether.</p>

<p>Like before, such an implementation eventually did come about: PyPy, a
Python implementation written in Python with a JIT compiler. It uses (by
default) something closer to mark-and-sweep, not reference counting, and
those handles <a href="https://utcc.utoronto.ca/~cks/space/blog/programming/NondeterministicGCII">are left open</a> until the next collection.</p>

<pre class="highlight"><code>&gt;&gt;&gt;&gt; for i in range(1, 1000000):
....     f = open("/dev/null", "r")
.... 
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 2, in &lt;module&gt;
IOError: [Errno 24] Too many open files: '/dev/null'
</code></pre>

<h3 id="a-tool-for-understanding-specifications">A tool for understanding specifications</h3>

<p>This fits right in with a broader method of self-improvement:
Occasionally put yourself in the implementor’s shoes. Think about what
it would take to correctly implement the code that you write, either
as a language or the APIs that you call. On reflection, you may find
that some of those things that <em>seem</em> cheap may not be. Your
assumptions may be reasonable, but not guaranteed. (Though it may be
that “reasonable” is perfectly sufficient for your situation.)</p>

<p>An adversarial implementation is one that challenges an assumption
you’ve taken for granted by turning it on its head.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>The Vulgarness of Abbreviated Function Templates</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2016/10/02/"/>
    <id>urn:uuid:048f746a-de7f-3357-8409-cfd531363726</id>
    <updated>2016-10-02T23:59:59Z</updated>
    <category term="c"/><category term="cpp"/><category term="rant"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>The <code>auto</code> keyword has been a part of C and C++ since the very
beginning, originally as a one of the four <em>storage class specifiers</em>:
<code>auto</code>, <code>register</code>, <code>static</code>, and <code>extern</code>. An <code>auto</code> variable has
“automatic storage duration,” meaning it is automatically allocated at
the beginning of its scope and deallocated at the end. It’s the
default storage class for any variable without external linkage or
without <code>static</code> storage, so the vast majority of variables in a
typical C program are automatic.</p>

<p>In C and C++ <em>prior to C++11</em>, the following definitions are
equivalent because the <code>auto</code> is implied.</p>

<pre class="highlight"><code><span class="t">int</span>
<span class="f">square</span>(<span class="t">int</span> x)
{
    <span class="t">int</span> x2 = x * x;
    <span class="k">return</span> x2;
}

<span class="t">int</span>
<span class="f">square</span>(<span class="t">int</span> x)
{
    <span class="k">auto</span> <span class="t">int</span> x2 = x * x;
    <span class="k">return</span> x2;
}
</code></pre>

<p>As a holdover from <em>really</em> old school C, unspecified types in C are
implicitly <code>int</code>, and even today you can get away with weird stuff
like this:</p>

<pre class="highlight"><code><span class="c">/* C only */</span>
square(x)
{
    <span class="k">auto</span> x2 = x * x;
    <span class="k">return</span> x2;
}
</code></pre>

<p>By “get away with” I mean in terms of the compiler accepting this as
valid input. Your co-workers, on the other hand, may become violent.</p>

<p>Like <code>register</code>, as a storage class <code>auto</code> is an historical artifact
without direct practical use in modern code. However, as a <em>concept</em>
it’s indispensable for the specification. In practice, automatic
storage means the variables lives on “the” stack (or <a href="http://clang.llvm.org/docs/SafeStack.html">one of the
stacks</a>), but the specifications make no mention of a
stack. In fact, the word “stack” doesn’t appear even once. Instead
it’s all described in terms of “automatic storage,” rightfully leaving
the details to the implementations. A stack is the most sensible
approach the vast majority of the time, particularly because it’s both
thread-safe and re-entrant.</p>

<h3 id="c11-type-inference">C++11 Type Inference</h3>

<p>One of the major changes in C++11 was repurposing the <code>auto</code> keyword,
moving it from a storage class specifier to a a <em>type specifier</em>. In
C++11, the compiler <strong>infers the type of an <code>auto</code> variable from its
initializer</strong>. In C++14, it’s also permitted for a function’s return
type, inferred from the <code>return</code> statement.</p>

<p>This new specifier is very useful in idiomatic C++ with its
ridiculously complex types. Transient variables, such as variables
bound to iterators in a loop, don’t need a redundant type
specification. It keeps code <em>DRY</em> (“Don’t Repeat Yourself”). Also,
templates easier to write, since it makes the compiler do more of the
work. The necessary type information is already semantically present,
and the compiler is a lot better at dealing with it.</p>

<p>With this change, the following is valid in both C and C++11, and, by
<em>sheer coincidence</em>, has the same meaning, but for entirely different
reasons.</p>

<pre class="highlight"><code><span class="t">int</span>
<span class="f">square</span>(<span class="t">int</span> x)
{
    <span class="k">auto</span> x2 = x * x;
    <span class="k">return</span> x2;
}
</code></pre>

<p>In C the type is implied as <code>int</code>, and in C++11 the type is inferred
from the type of <code>x * x</code>, which, in this case, is <code>int</code>. The prior
example with <code>auto int x2</code>, valid in C++98 and C++03, is no longer
valid in C++11 since <code>auto</code> and <code>int</code> are redundant type specifiers.</p>

<p>Occasionally I wish I had something like <code>auto</code> in C. If I’m writing a
<code>for</code> loop from 0 to <code>n</code>, I’d like the loop variable to be the same
type as <code>n</code>, even if I decide to change the type of <code>n</code> in the future.
For example,</p>

<pre class="highlight"><code><span class="k">struct</span> <span class="t">foo</span> *foo = foo_create();
<span class="k">for</span> (<span class="t">int</span> i = <span class="m">0</span>; i &lt; foo-&gt;n; i++)
    <span class="c">/* ... */</span>;
</code></pre>

<p>The loop variable <code>i</code> should be the same type as <code>foo-&gt;n</code>. If I decide
to change the type of <code>foo-&gt;n</code> in the struct definition, I’d have to
find and update every loop. The idiomatic C solution is to <code>typedef</code>
the integer, using the new type both in the struct and in loops, but I
don’t think that’s much better.</p>

<h3 id="abbreviated-function-templates">Abbreviated Function Templates</h3>

<p>Why is all this important? Well, I was recently reviewing some C++ and
came across this odd specimen. I’d never seen anything like it before.
Notice the use of <code>auto</code> for the parameter types.</p>

<pre class="highlight"><code><span class="t">void</span>
<span class="f">set_odd</span>(<span class="k">auto</span> first, <span class="k">auto</span> last, <span class="k">const</span> <span class="k">auto</span> &amp;x)
{
    <span class="t">bool</span> toggle = <span class="k">false</span>;
    <span class="k">for</span> (; first != last; first++, toggle = !toggle)
        <span class="k">if</span> (toggle)
            *first = x;
}
</code></pre>

<p>Given the other uses of <code>auto</code> as a type specifier, this kind of makes
sense, right? The compiler infers the type from the input argument.
But, as you should often do, put yourself in the compiler’s shoes for
a moment. Given this function definition in isolation, can you
generate any code? Nope. The compiler needs to see the call site
before it can infer the type. Even more, different call sites may use
different types. That <strong>sounds an awful lot like a template</strong>, eh?</p>

<pre class="highlight"><code><span class="k">template</span>&lt;<span class="k">typename</span> <span class="t">T</span>, <span class="k">typename</span> <span class="t">V</span>&gt;
<span class="t">void</span>
<span class="f">set_odd</span>(<span class="t">T</span> first, <span class="t">T</span> last, <span class="k">const</span> <span class="t">V</span> &amp;x)
{
    <span class="t">bool</span> toggle = <span class="k">false</span>;
    <span class="k">for</span> (; first != last; first++, toggle = !toggle)
        <span class="k">if</span> (toggle)
            *first = x;
}
</code></pre>

<p>This is <strong>a proposed feature called <em>abbreviated function
templates</em></strong>, part of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4361.pdf"><em>C++ Extensions for Concepts</em></a>. It’s
intended to be shorthand for the template version of the function. GCC
4.9 implements it as an extension, which is why the author was unaware
of its unofficial status. In March 2016 it was established that
<a href="http://honermann.net/blog/2016/03/06/why-concepts-didnt-make-cxx17/">abbreviated function templates <strong>would <em>not</em> be part of
C++17</strong></a>, but may still appear in a future revision.</p>

<p>Personally, I find this use of <code>auto</code> to be vulgar. It overloads the
keyword with a third definition. This isn’t unheard of — <code>static</code> also
serves a number of unrelated purposes — but while similar to the
second form of <code>auto</code> (type inference), this proposed third form is
very different in its semantics (far more complex) and overhead
(potentially very costly). I’m glad it’s been rejected so far.
Templates better reflect the nature of this sort of code.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>Makefile Assignments are Turing-Complete</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2016/04/30/"/>
    <id>urn:uuid:49f54bce-b7da-374e-1e0e-1724b92e3e1f</id>
    <updated>2016-04-30T03:01:22Z</updated>
    <category term="lang"/><category term="compsci"/><category term="posix"/>
    <content type="html">
      <![CDATA[<p>For over a decade now, GNU Make has almost exclusively been my build
system of choice, either directly or indirectly. Unfortunately this
means I unnecessarily depend on some GNU extensions — an annoyance when
porting to the BSDs. In an effort to increase the portability of my
Makefiles, I recently read <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html">the POSIX make specification</a>. I
learned two important things: 1) <del>POSIX make is so barren it’s not
really worth striving for</del> (<em>update</em>: I’ve <a href="/blog/2017/08/20/">changed my mind</a>),
and 2) <strong>make’s macro assignment mechanism is Turing-complete</strong>.</p>

<p>If you want to see it in action for yourself before reading further,
here’s a Makefile that implements Conway’s Game of Life (40x40) using
only macro assignments.</p>

<ul>
  <li><a href="/download/life.mak"><strong>life.mak</strong></a> (174kB) [<a href="https://github.com/skeeto/makefile-game-of-life">or generate your own</a>]</li>
</ul>

<p>Run it with any make program in an ANSI terminal. It <em>must</em> literally
be named <code>life.mak</code>. Beware: if you run it longer than a few minutes,
your computer may begin thrashing.</p>

<pre class="highlight"><code>make -f life.mak
</code></pre>

<p>It’s 100% POSIX-compatible except for the <code>sleep 0.1</code> (fractional
sleep), which is only needed for visual effect.</p>

<h3 id="a-posix-workaround">A POSIX workaround</h3>

<p>Unlike virtually every real world implementation, POSIX make doesn’t
support conditional parts. For example, you might want your Makefile’s
behavior to change depending on the value of certain variables. In GNU
Make it looks like this:</p>

<pre class="highlight"><code>ifdef USE_FOO
    EXTRA_FLAGS = -ffoo -lfoo
else
    EXTRA_FLAGS = -Wbar
endif
</code></pre>

<p>Or BSD-style:</p>

<pre class="highlight"><code>.ifdef USE_FOO
    EXTRA_FLAGS = -ffoo -lfoo
.else
    EXTRA_FLAGS = -Wbar
.endif
</code></pre>

<p>If the goal is to write a strictly POSIX Makefile, how could I work
around the lack of conditional parts and maintain a similar interface?
The selection of macro/variable to evaluate can be dynamically
selected, allowing for some useful tricks. First define the option’s
default:</p>

<pre class="highlight"><code>USE_FOO = 0
</code></pre>

<p>Then define both sets of flags:</p>

<pre class="highlight"><code>EXTRA_FLAGS_0 = -Wbar
EXTRA_FLAGS_1 = -ffoo -lfoo
</code></pre>

<p>Now dynamically select one of these macros for assignment to
<code>EXTRA_FLAGS</code>.</p>

<pre class="highlight"><code>EXTRA_FLAGS = $(EXTRA_FLAGS_$(USE_FOO))
</code></pre>

<p>The assignment on the command line overrides the assignment in the
Makefile, so the user gets to override <code>USE_FOO</code>.</p>

<pre class="highlight"><code>$ make              # EXTRA_FLAGS = -Wbar
$ make USE_FOO=0    # EXTRA_FLAGS = -Wbar
$ make USE_FOO=1    # EXTRA_FLAGS = -ffoo -lfoo
</code></pre>

<p>Before reading the POSIX specification, I didn’t realize that the
<em>left</em> side of an assignment can get the same treatment. For example,
if I really want the “if defined” behavior back, I can use the macro
to mangle the left-hand side. For example,</p>

<pre class="highlight"><code>EXTRA_FLAGS = -O0 -g3
EXTRA_FLAGS$(DEBUG) = -O3 -DNDEBUG
</code></pre>

<p>Caveat: If <code>DEBUG</code> is set to empty, it may still result in true for
<code>ifdef</code> depending on which make flavor you’re using, but will always
<em>appear</em> to be unset in this hack.</p>

<pre class="highlight"><code>$ make             # EXTRA_FLAGS = -O3 -DNDEBUG
$ make DEBUG=yes   # EXTRA_FLAGS = -O0 -g3
</code></pre>

<p>This last case had me thinking: This is very similar to the (ab)use of
the x86 <code>mov</code> instruction in <a href="https://www.cl.cam.ac.uk/~sd601/papers/mov.pdf">mov is Turing-complete</a>. These
macro assignments alone should be enough to compute <em>any</em> algorithm.</p>

<h3 id="macro-operations">Macro Operations</h3>

<p>Macro names are just keys to a global associative array. This can be
used to build lookup tables. Here’s a Makefile to “compute” the square
root of integers between 0 and 10.</p>

<pre class="highlight"><code>sqrt_0  = 0.000000
sqrt_1  = 1.000000
sqrt_2  = 1.414214
sqrt_3  = 1.732051
sqrt_4  = 2.000000
sqrt_5  = 2.236068
sqrt_6  = 2.449490
sqrt_7  = 2.645751
sqrt_8  = 2.828427
sqrt_9  = 3.000000
sqrt_10 = 3.162278
result := $(sqrt_$(n))
</code></pre>

<p>The BSD flavors of make have a <code>-V</code> option for printing variables,
which is an easy way to retrieve output. I used an “immediate”
assignment (<code>:=</code>) for <code>result</code> since some versions of make won’t
evaluate the expression before <code>-V</code> printing.</p>

<pre class="highlight"><code>$ make -f sqrt.mak -V result n=8
2.828427
</code></pre>

<p>Without <code>-V</code>, a default target could be used instead:</p>

<pre class="highlight"><code>output :
        @printf "$(result)\n"
</code></pre>

<p>There are no math operators, so performing arithmetic <a href="/blog/2008/03/15/">requires some
creativity</a>. For example, integers could be represented as a
series of x characters. The number 4 is <code>xxxx</code>, the number 6 is
<code>xxxxxx</code>, etc. Addition is concatenation (note: macros can have <code>+</code> in
their names):</p>

<pre class="highlight"><code>A      = xxx
B      = xxxx
A+B    = $(A)$(B)
</code></pre>

<p>However, since there’s no way to “slice” a value, subtraction isn’t
possible. A more realistic approach to arithmetic would require lookup
tables.</p>

<h3 id="branching">Branching</h3>

<p>Branching could be achieved through more lookup tables. For example,</p>

<pre class="highlight"><code>square_0  = 1
square_1  = 2
square_2  = 4
# ...
result := $($(op)_$(n))
</code></pre>

<p>And called as:</p>

<pre class="highlight"><code>$ make n=5 op=sqrt    # 2.236068
$ make n=5 op=square  # 25
</code></pre>

<p>Or using the <code>DEBUG</code> trick above, use the condition to mask out the
results of the unwanted branch. This is similar to the <code>mov</code> paper.</p>

<pre class="highlight"><code>result           := $(op)($(n)) = $($(op)_$(n))
result$(verbose) := $($(op)_$(n))
</code></pre>

<p>And its usage:</p>

<pre class="highlight"><code>$ make n=5 op=square             # 25
$ make n=5 op=square verbose=1   # square(5) = 25
</code></pre>

<h3 id="what-about-loops">What about loops?</h3>

<p>Looping is a tricky problem. However, one of the most common build
(<a href="http://aegis.sourceforge.net/auug97.pdf">anti</a>?)patterns is the recursive Makefile. Borrowing from the
<code>mov</code> paper, which used an unconditional jump to restart the program
from the beginning, for a Makefile Turing-completeness I can invoke
the Makefile recursively, restarting the program with a new set of
inputs.</p>

<p>Remember the print target above? I can loop by invoking make again
with new inputs in this target,</p>

<pre class="highlight"><code>output :
    @printf "$(result)\n"
    @$(MAKE) $(args)
</code></pre>

<p>Before going any further, now that loops have been added, the natural
next question is halting. In reality, the operating system will take
care of that after some millions of make processes have carelessly
been invoked by this horribly inefficient scheme. However, we can do
better. The program can clobber the <code>MAKE</code> variable when it’s ready to
halt. Let’s formalize it.</p>

<pre class="highlight"><code>loop = $(MAKE) $(args)
output :
    @printf "$(result)\n"
    @$(loop)
</code></pre>

<p>To halt, the program just needs to clear <code>loop</code>.</p>

<p>Suppose we want to count down to 0. There will be an initial count:</p>

<pre class="highlight"><code>count = 6
</code></pre>

<p>A decrement table:</p>

<pre class="highlight"><code>6  = 5
5  = 4
4  = 3
3  = 2
2  = 1
1  = 0
0  = loop
</code></pre>

<p>The last line will be used to halt by clearing the name on the right
side. This is <a href="http://c2.com/cgi/wiki?ThreeStarProgrammer">three star</a> territory.</p>

<pre class="highlight"><code>$($($(count))) =
</code></pre>

<p>The result (current iteration) loop value is computed from the lookup
table.</p>

<pre class="highlight"><code>result = $($(count))
</code></pre>

<p>The next loop value is passed via <code>args</code>. If <code>loop</code> was cleared above,
this result will be discarded.</p>

<pre class="highlight"><code>args = count=$(result)
</code></pre>

<p>With all that in place, invoking the Makefile will print a countdown
from 5 to 0 and quit. This is the general structure for the Game of
Life macro program.</p>

<h3 id="game-of-life">Game of Life</h3>

<p>A universal Turing machine has <a href="http://rendell-attic.org/gol/tm.htm">been implemented in Conway’s Game of
Life</a>. With all that heavy lifting done, one of the easiest
methods today to prove a language’s Turing-completeness is to
implement Conway’s Game of Life. Ignoring the criminal inefficiency of
it, the Game of Life Turing machine could be run on the Game of Life
simulation running on make’s macro assignments.</p>

<p>In the Game of Life program — the one linked at the top of this
article — each cell is stored in a macro named xxyy, after its
position. The top-left most cell is named 0000, then going left to
right, 0100, 0200, etc. Providing input is a matter of assigning each
of these macros. I chose <code>X</code> for alive and <code>-</code> for dead, but, as
you’ll see, any two characters permitted in macro names would work as
well.</p>

<pre class="highlight"><code>$ make 0000=X 0100=- 0200=- 0300=X ...
</code></pre>

<p>The next part should be no surprise: The rules of the Game of Life are
encoded as a 512-entry lookup table. The key is formed by
concatenating the cell’s value along with all its neighbors, with
itself in the center.</p>

<p><img src="/img/diagram/make-gol.png" alt="" /></p>

<p>The “beginning” of the table looks like this:</p>

<pre class="highlight"><code>--------- = -
X-------- = -
-X------- = -
XX------- = -
--X------ = -
X-X------ = -
-XX------ = -
XXX------ = X
---X----- = -
X--X----- = -
-X-X----- = -
XX-X----- = X
# ...
</code></pre>

<p>Note: The two right-hand <code>X</code> values here are the cell coming to life
(exactly three living neighbors). Computing the <em>next</em> value (n0101)
for 0101 is done like so:</p>

<pre class="highlight"><code>n0101 = $($(0000)$(0100)$(0200)$(0001)$(0101)$(0201)$(0002)$(0102)$(0202))
</code></pre>

<p>Given these results, constructing the input to the next loop is
simple:</p>

<pre class="highlight"><code>args = 0000=$(n0000) 0100=$(n0100) 0200=$(n0200) ...
</code></pre>

<p>The display output, to be given to <code>printf</code>, is built similarly:</p>

<pre class="highlight"><code>output = $(n0000)$(n0100)$(n0200)$(n0300)...
</code></pre>

<p>In the real version, this is decorated with an ANSI escape code that
clears the terminal. The <code>printf</code> interprets the escape byte (<code>\033</code>)
so that it doesn’t need to appear literally in the source.</p>

<p>And that’s all there is to it: Conway’s Game of Life running in a
Makefile. <a href="https://www.youtube.com/watch?v=dMjQ3hA9mEA">Life, uh, finds a way</a>.</p>

<!-- Obviously the following image is not public domain. -->
<p><img src="/img/humor/life-finds-a-way.jpg" alt="" /></p>

]]>
    </content>
  </entry>
  <entry>
    <title>Per Loop vs. Per Iteration Bindings</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2014/06/06/"/>
    <id>urn:uuid:42b38d5e-1781-3354-c1b2-1228fff81ebd</id>
    <updated>2014-06-06T20:18:58Z</updated>
    <category term="lang"/><category term="javascript"/>
    <content type="html">
      <![CDATA[<p>The <a href="http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts">April 5th, 2014 draft</a> of the <a href="https://people.mozilla.org/~jorendorff/es6-draft.html#sec-for-statement-runtime-semantics-labelledevaluation">ECMA-262 6th Edition
specification</a> — a.k.a the next major version of
JavaScript/ECMAScript — contained a subtle, though very significant,
change to the semantics of the <code>for</code> loop (13.6.3.3). Loop variables
are now fresh bindings for each iteration of the loop: a
<em>per-iteration</em> binding. Previously loop variables were established
once for the entire loop, a <em>per-loop</em> binding. The purpose is an
attempt to <a href="https://bugzil.la/449811">fix an old gotcha</a> that effects many languages.</p>

<p>If you couldn’t already tell, this is going to be another language
lawyer post!</p>

<h3 id="backup-to-c">Backup to C</h3>

<p>To try to explain what this all means this in plain English, let’s
step back a moment and discuss what a <code>for</code> loop really is. I can’t
find a source for this, but I’m pretty confident the three-part <code>for</code>
loop originated in K&amp;R C.</p>

<pre class="highlight"><code>for (INITIALIZATION; CONDITION; ITERATION) {
    BODY;
}
</code></pre>

<ol>
  <li>Evaluate INITIALIZATION.</li>
  <li>Evaluate CONDITION. If zero (false), exit the <code>for</code>.</li>
  <li>Evaluate BODY.</li>
  <li>Evaluate ITERATION and go to 2.</li>
</ol>

<p>In the original C, and all the way up to C89, no variable declarations
were allowed in the initialization expression. I can understand why:
there’s a subtle complication, though it’s harmless in C. We’ll get to
that soon. Here’s a typical C89 for loop.</p>

<pre class="highlight"><code><span class="t">int</span> count = <span class="m">10</span>;
<span class="c">/* ... */</span>
<span class="t">int</span> i;
<span class="k">for</span> (i = <span class="m">0</span>; i &lt; count; i++) {
    <span class="t">double</span> foo;
    <span class="c">/* ... */</span>
}
</code></pre>

<p>The variable <code>i</code> is established independent of the loop, in the scope
outside the <code>for</code> loop, alongside <code>count</code>. This isn’t even a per-loop
binding. As far as the language is concerned, it’s just a variable
that the loop happens to access and mutate. It’s very
assembly-language-like. Because C has block scoping, the body of the
for loop is another nested scope. The variable <code>foo</code> is in this scope,
reestablished on each iteration of the loop (per-iteration).</p>

<p>As an implementation detail, <code>foo</code> will reside <a href="/blog/2008/07/18/">at the same location
on the stack</a> each time around the loop. If it’s accessed
before being initialized, it will probably hold the value from the
previous iteration, but, as far as the language is concerned, this is
just a happy, though <em>undefined</em>, coincidence.</p>

<h4 id="c99-loops">C99 Loops</h4>

<p>Fast forward to the end of the 20th century. At this point, other
languages have allowed variable declarations in the initialization
part for years, so it’s time for C to catch up with C99.</p>

<pre class="highlight"><code><span class="t">int</span> count = <span class="m">10</span>;
<span class="c">/* ... */</span>
<span class="k">for</span> (<span class="t">int</span> i = <span class="m">0</span>; i &lt; count; i++) {
    <span class="t">double</span> foo;
    <span class="c">/* ... */</span>
}
</code></pre>

<p>Now consider this: <strong>in what scope is the variable</strong> <code>i</code>? The outer
scope as before? The iteration scope with <code>foo</code>? The answer is
neither. In order to make this work, a whole new loop scope is
established in between: a <em>per-loop binding</em>. This scope holds for the
entire duration of the loop.</p>

<p><img src="/img/diagram/for-scope.png" alt="" /></p>

<p>The variable <code>i</code> is constrained to the <code>for</code> loop without being
limited to the iteration scope. This is important because <code>i</code> is what
keeps track of the loop’s progress. The semantic equivalent in C89
makes the additional scope explicit with a block.</p>

<pre class="highlight"><code><span class="t">int</span> count = <span class="m">10</span>;
<span class="c">/* ... */</span>
{
    <span class="t">int</span> i;
    <span class="k">for</span> (i = <span class="m">0</span>; i &lt; count; i++) {
        <span class="t">double</span> foo;
        <span class="c">/* ... */</span>
    }
}
</code></pre>

<p>This, ladies and gentlemen, is the the C-style 3-part <code>for</code> loop.
Every language that has this statement, and has block scope, follows
these semantics. This included JavaScript up until two months ago,
where the draft now gives it its own unique behavior.</p>

<h3 id="javascripts-let">JavaScript’s Let</h3>

<p>As it exists today in its practical form, little of the above is
relevant to JavaScript. JavaScript has no block scope, just function
scope. A three-part for-loop doesn’t establish all these scopes,
because scopes like these are absent from the language.</p>

<p>An important change coming with 6th edition is the introduction of
<code>let</code> declarations. Variables declared with <code>let</code> will have block
scope.</p>

<pre class="highlight"><code><span class="k">let</span> count = <span class="m">10</span>;
<span class="c">// ...</span>
<span class="k">for</span>(<span class="k">let</span> i = <span class="m">0</span>; i &lt; count; i++) {
    <span class="k">let</span> foo;
    <span class="c">// ...</span>
}
console.log(foo); <span class="c">// error</span>
console.log(i);   <span class="c">// error</span>
</code></pre>

<p>If these variables had been declared with <code>var</code>, the last two lines
wouldn’t be errors (or worse, global references). <code>count</code>, <code>i</code>, and
<code>foo</code> would all be in the same function-level scope. This is really
great! I look forward to using <code>let</code> exclusively someday.</p>

<h4 id="the-closure-trap">The Closure Trap</h4>

<p>I mentioned a subtle complication. Most of the time programmers don’t
need to consider or even be aware of this middle scope. However, when
combined with closures it suddenly becomes an issue. Here’s an example
with Perl,</p>

<pre class="highlight"><code><span class="k">my</span> <span class="v">@closures</span>;
<span class="k">for</span> (<span class="k">my</span> <span class="v">$i</span> = <span class="m">0</span>; <span class="v">$i</span> &lt; <span class="m">2</span>; <span class="v">$i</span>++) {
    push(<span class="v">@closures</span>, <span class="k">sub</span> { <span class="k">return</span> <span class="v">$i</span>; });
}
<span class="v">$closures</span>[<span class="m">0</span>]();  <span class="c"># =&gt; 2</span>
<span class="v">$closures</span>[<span class="m">1</span>]();  <span class="c"># =&gt; 2</span>
</code></pre>

<p>Here’s one with Python. Python lacks a three-part <code>for</code> loop, but its
standard <code>for</code> loop has similar semantics.</p>

<pre class="highlight"><code>closures = []
<span class="k">for</span> i <span class="k">in</span> xrange(<span class="m">2</span>):
    closures.append(<span class="k">lambda</span>: i)
closures[<span class="m">0</span>]()  <span class="c"># =&gt; 1</span>
closures[<span class="m">1</span>]()  <span class="c"># =&gt; 1</span>
</code></pre>

<p>And now Ruby.</p>

<pre class="highlight"><code>closures = []
<span class="k">for</span> i <span class="k">in</span> (<span class="m">0</span>..<span class="m">1</span>)
  closures &lt;&lt; lambda { i }
<span class="k">end</span>
closures[<span class="m">0</span>].call  <span class="c"># =&gt; 1</span>
closures[<span class="m">1</span>].call  <span class="c"># =&gt; 1</span>
</code></pre>

<p>In all three cases, one closure is created per iteration. Each closure
captures the loop variable <code>i</code>. It’s easy to make the mistake of
thinking each closure will return a unique value. However, as pointed
out above, this is a <em>per-loop</em> variable, existing in a middle scope.
The closures all capture the same variable, merely bound to different
values at the time of capture. The solution is to establish a new
variable in the iteration scope and capture that instead. Below, I’ve
established a <code>$value</code> variable for this.</p>

<pre class="highlight"><code><span class="k">my</span> <span class="v">@closures</span>;
<span class="k">for</span> (<span class="k">my</span> <span class="v">$i</span> = <span class="m">0</span>; <span class="v">$i</span> &lt; <span class="m">2</span>; <span class="v">$i</span>++) {
    <span class="k">my</span> <span class="v">$value</span> = <span class="v">$i</span>;
    push(<span class="v">@closures</span>, <span class="k">sub</span> { <span class="k">return</span> <span class="v">$value</span>; });
}
<span class="v">$closures</span>[<span class="m">0</span>]();  <span class="c"># =&gt; 0</span>
<span class="v">$closures</span>[<span class="m">1</span>]();  <span class="c"># =&gt; 1</span>
</code></pre>

<p>This is something that newbies easily get tripped up on. Because
they’re still trying to wrap their heads around the closure concept,
this looks like some crazy bug in the interpreter/compiler. I can
understand why the ECMA-262 draft was changed to accommodate this
situation.</p>

<h4 id="the-javascript-workaround">The JavaScript Workaround</h4>

<p>The language in the new draft has two items called
<em>perIterationBindings</em> and <em>CreatePerIterationEnvironment</em> (in case
you’re searching for the relevant part of the spec). Like the <code>$value</code>
example above, <code>for</code> loops in JavaScript with “lexical” (i.e. <code>let</code>)
loop bindings will implicitly <em>mask</em> the loop variable with a variable
of the same name in the iteration scope.</p>

<pre class="highlight"><code><span class="k">let</span> closures = [];
<span class="k">for</span> (<span class="k">let</span> i = <span class="m">0</span>; i &lt; <span class="m">2</span>; i++) {
    closures.push(<span class="k">function</span>() { <span class="k">return</span> i; });
}

<span class="c">/* Before the change: */</span>
closures[<span class="m">0</span>]();  <span class="c">// =&gt; 2</span>
closures[<span class="m">1</span>]();  <span class="c">// =&gt; 2</span>

<span class="c">/* After the change: */</span>
closures[<span class="m">0</span>]();  <span class="c">// =&gt; 0</span>
closures[<span class="m">1</span>]();  <span class="c">// =&gt; 1</span>
</code></pre>

<p>Note: If you try to run this yourself, note that at the time of this
writing, the only JavaScript implementation I could find that updated
to the latest draft was <a href="https://github.com/google/traceur-compiler">Traceur</a>. You’ll probably see the
“before” behavior for now.</p>

<p>You can’t see it (I said it’s implicit!), but under an updated
JavaScript implementation there are actually two <code>i</code> variables here.
The closures capture the most inner <code>i</code>, the per-iteration version of
<code>i</code>. Let’s go back to the original example, JavaScript-style.</p>

<pre class="highlight"><code><span class="k">let</span> count = <span class="m">10</span>;
<span class="c">// ...</span>
<span class="k">for</span> (<span class="k">let</span> i = <span class="m">0</span>; i &lt; count; i++) {
   <span class="k">let</span> foo;
   <span class="c">// ...</span>
}
</code></pre>

<p>Here’s what the scope looks like for the latest draft. Notice the
second <code>i</code> in the iteration scope. The inner <code>i</code> is initially assigned
to the value of the outer <code>i</code>.</p>

<p><img src="/img/diagram/for-scope-js.png" alt="" /></p>

<p>We could emulate this in an older edition. Imagine writing a macro to
do this.</p>

<pre class="highlight"><code><span class="k">let</span> count = <span class="m">10</span>;
<span class="c">// ...</span>
<span class="k">for</span> (<span class="k">let</span> i = <span class="m">0</span>; i &lt; count; i++) {
    <span class="k">let</span> __i = i;  <span class="c">// (possible name collision)</span>
    {
        <span class="k">let</span> i = __i;
        <span class="k">let</span> foo;
        <span class="c">// ...</span>
    }
}
</code></pre>

<p>I have to use <code>__i</code> to smuggle the value across scopes without having
<code>i</code> reference itself. Unlike Lisp’s <code>let</code>, the assignment value for
<code>var</code> and <code>let</code> is evaluated in the nested scope, not the outer scope.</p>

<p>Each iteration gets its own <code>i</code>. But what happens when the loop
modifies <code>i</code>? Simple, it’s copied back out at the end of the body.</p>

<pre class="highlight"><code><span class="k">let</span> count = <span class="m">10</span>;
<span class="c">// ...</span>
<span class="k">for</span> (<span class="k">let</span> i = <span class="m">0</span>; i &lt; count; i++) {
    <span class="k">let</span> __i = i;
    {
        <span class="k">let</span> i = __i;
        <span class="k">let</span> foo;
        <span class="c">// ...</span>
        __i = i;
    }
    i = __i;
}
</code></pre>

<p>Now all the expected <code>for</code> semantics work — the body can also update
the loop variable — but we still get the closure-friendly
per-iteration variables.</p>

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

<p>I’m still not sure if I really like this change. It’s clean fix, but
the gotcha hasn’t been eliminated. Instead it’s been inverted.
Sometime someone will have the unusual circumstance of <em>wanting</em> to
capture the loop variable, and he will run into some surprising
behavior. Because the semantics are a lot more complicated, it’s hard
to reason about what’s not working unless you already know JavaScript
has magical <code>for</code> loops.</p>

<p>Perl and C# each also gained per-iteration bindings in their history,
but rather than complicate or change their standard <code>for</code> loops, they
instead introduced it as a new syntactic construction: <code>foreach</code>.</p>

<pre class="highlight"><code><span class="k">my</span> <span class="v">@closures</span>;
<span class="k">foreach</span> <span class="k">my</span> <span class="v">$i</span> (<span class="m">0</span>, <span class="m">1</span>) {
    push(<span class="v">@closures</span>, <span class="k">sub</span> { <span class="k">return</span> <span class="v">$i</span>; });
}
<span class="v">$closures</span>[<span class="m">0</span>]();  <span class="c"># =&gt; 0</span>
<span class="v">$closures</span>[<span class="m">1</span>]();  <span class="c"># =&gt; 1</span>
</code></pre>

<p>In this case, per-iteration bindings definitely make sense. The
variable <code>$i</code> is established and bound to each value in turn. As far
as control flow goes, it’s very functional. The binding is never
actually mutated.</p>

<p>I think it could be argued that Python and Ruby’s <code>for ... in</code> forms
should behave like this <code>foreach</code>. These were probably misdesigned
early on, but it’s not possible to change their semantics at this
point. Because JavaScript’s <code>var</code> was improperly designed from the
beginning, <code>let</code> offers the opportunity to fix more than just <code>var</code>.
We’re seeing this right now with these new <code>for</code> semantics.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>Three Dimensions of Type Systems</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2014/04/25/"/>
    <id>urn:uuid:b953054a-3681-3f91-a3a0-f075261f2e69</id>
    <updated>2014-04-25T22:03:01Z</updated>
    <category term="lang"/>
    <content type="html">
      <![CDATA[<p>I occasionally come across articles, and even some books, that get
terminology mixed up when discussing type systems. The author might
say “strong” when what they’re talking about is “lexical.” In this
article I’ll define three orthogonal properties of type systems. A new
programming language design could select from each category
independently.</p>

<h3 id="static-vs-dynamic">Static vs Dynamic</h3>

<p>Static versus dynamic typing is probably the most obvious type system
property when glancing at an example of a language’s source code. This
refers to whether or not variables have types.</p>

<p>In a statically typed language, variables have a compile-time
determined type. At run-time, a variable will only ever hold a value
of this type. Except where type information can be <em>inferred</em>,
variable and function declarations are generally accompanied by its
type (manifest). Type violations are generally discovered early, at
compile-time, at the cost of additional up-front planning.</p>

<p>In a dynamically typed language, only values have types. A variable
may be bound to any type of value. Though a smart compiler may reason
about a program enough to know that certain variables are only ever
bound to a limited set of types. Type violations are generally
discovered late, at run-time, but this allows for more ad hoc design.</p>

<p>Statically typed languages: C, C++, Java, C#, Haskell</p>

<p>Dynamically typed languages: Python, Ruby, JavaScript, Lisp, Clojure</p>

<p>To give a quick comparison, here’s the same function definition in C
and JavaScript.</p>

<pre class="highlight"><code><span class="t">double</span> <span class="f">distance</span>(<span class="k">struct</span> <span class="t">point</span> a, <span class="k">struct</span> <span class="t">point</span> b) {
    <span class="k">return</span> sqrt(pow(a.x - b.x, <span class="m">2</span>) + pow(a.y - b-y, <span class="m">2</span>));
}
</code></pre>

<pre class="highlight"><code><span class="k">function</span> <span class="f">distance</span>(a, b) {
    <span class="k">return</span> Math.sqrt(Math.pow(a.x - b.x, <span class="m">2</span>) + Math.pow(a.y - b.y, <span class="m">2</span>));
}
</code></pre>

<p>Dynamic type systems are more apt for <em>duck typing</em>, though there are
exceptions such as <a href="/blog/2014/04/01/">with C++ templates</a>. In the JavaScript
example above, anything that has numeric <code>x</code> and <code>y</code> properties can be
passed to <code>distance</code>. The actual type doesn’t matter. In the C
version, only that very specific type, <code>struct point</code>, may be passed
to <code>distance</code>.</p>

<p>The C example could be made more generic, circumventing its type
system through a trick called <a href="http://en.wikipedia.org/wiki/Type_punning"><em>type punning</em></a>. This is where a
value is accessed by the program as though it was a different type of
value. This requires up-front planning and may potentially
<a href="http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html">violate <em>strict aliasing</em></a>.</p>

<h3 id="lexical-vs-dynamic-scope">Lexical vs. Dynamic Scope</h3>

<p>Lexical versus dynamic scope refers not to any values or objects
themselves, but rather to how variables are accessed. Virtually all
popular programming languages in use today use lexical scope. This is
because dynamic scope has serious performance and correctness
problems. In fact, it was likely invented entirely by accident.</p>

<p>However, it’s still useful to use dynamic scope on a careful, opt-in
process. Perl, Common Lisp, Clojure, and Emacs Lisp all permit
selective dynamic scope. It’s a clean method for temporarily masking a
global variable, such as the default stream for reading/writing
input/output.</p>

<p>Under lexical scope, the scope of a variable is determined statically
at compile-time. The compiler knows about all accesses to a particular
variable. This is sometimes called static scope but I’m using the word
lexical here to help differentiate from static typing (above).</p>

<p>In dynamic scope, all variables are essentially global variables. A
new binding masks any existing global binding for any functions called
from within that binding’s extent. If a function accesses a <em>free
variable</em>, it’s not known until run-time from where that value may
come. When the last binding is removed, such as when a local
variable’s scope exited, that global variable is then said to be
<em>unbound</em>. Dynamic scope is incompatible with closures.</p>

<p>Lexically scoped languages: C, C++, Java, JavaScript, Python, Ruby, (many more)</p>

<p>Dynamically scoped languages: Emacs Lisp, bash</p>

<p>As of Emacs 24, lexical scope can be enabled by default for a
file/buffer by setting <code>lexical-binding</code> to true. I imagine this will
some day become the default, making Emacs Lisp a lexically scoped
language. This is also a perfect example of lexical scope having
better performance:
<a href="/blog/2014/01/04/">turning on lexical scope makes Elisp programs run faster</a>.</p>

<p>Here’s an example of dynamic scope in Emacs Lisp.</p>

<pre class="highlight"><code>(<span class="k">defun</span> <span class="f">reveal</span> ()
  x) <span class="c">; free variable</span>

(<span class="k">defun</span> <span class="f">foo</span> ()
  (<span class="k">let</span> ((x <span class="v">:foo</span>))
    (reveal)))

(<span class="k">defun</span> <span class="f">bar</span> ()
  (<span class="k">let</span> ((x <span class="v">:bar</span>))
    (reveal)))

(foo)
<span class="c">;; =&gt; :foo</span>

(bar)
<span class="c">;; =&gt; :bar</span>
</code></pre>

<p>The value of <code>x</code> as seen by <code>reveal</code> depends on which function called
it, since the binding leaks through. Running the exact same code in
Common Lisp, where it’s lexically scoped, would result in a run-time
error. It always tries to access <code>x</code> as a global variable. The scope
of <code>x</code> is strictly limited to <code>foo</code> or <code>bar</code>.</p>

<h3 id="strong-vs-weak">Strong vs. Weak</h3>

<p>Strong versus weak is probably the least understood property. Strong
typing is often mixed up with static typing despite being an
orthogonal concept. A language can be strongly, dynamically typed
(Python) — or weakly, statically typed (C). Strong/weak is also
sometimes confused with <em>type safety</em>.</p>

<p>This aspect refers to the tendency of values to be implicitly coerced
into another type depending on how they are used. Unlike the previous
two type system properties, this one isn’t bimodal. There’s a degree
to just how much implicit coercion occurs.</p>

<p>Strongly typed languages: Python, Common Lisp, Java, Ruby</p>

<p>Weakly typed languages: JavaScript, PHP, Perl, C</p>

<p>For example, take the following expression.</p>

<ul>
  <li><code>"8" - 5</code></li>
</ul>

<p>In strongly typed languages this will generally be an error: strings
have no definition with the subtraction operator. In weakly typed
languages, the “8” is likely to be parsed into a number as part of
being handled by the operator, with the expression evaluating to 3.</p>

<p>If a language has a triple equality operator (<code>===</code>), that’s a dead
giveaway that it’s weakly typed.</p>

<p>In the case of C, its pointers are what make it weakly typed. It’s
easy to make a pointer to a value, then dereference it as a different
type (usually leading to <em>undefined behavior</em>).</p>

<p>This is another trade-off between safety and convenience. Modern
languages tend towards strong typing.</p>

<h3 id="further-reading">Further Reading</h3>

<p>Here are three more type system properties that weren’t discussed.</p>

<ul>
  <li>
    <p>Type-safe vs. Type-unsafe : the ability of the programmer to
circumvent the language’s type system. Like strong versus weak,
this one is a spectrum, where some languages are always safe, some
offer some unsafe features, and some are very unsafe.</p>
  </li>
  <li>
    <p>Typed vs. untyped : determines if types exist at all. Sometimes
confused with static versus dynamic typing. Virtually all
programming languages are typed. An example of untyped languages
would be shell scripting languages where everything is a character
string.</p>
  </li>
  <li>
    <p><a href="http://en.wikipedia.org/wiki/Nominative_type_system">Nominal</a> vs. <a href="http://en.wikipedia.org/wiki/Structural_type_system">Structural</a> : types are compatible
depending on whether they are declared as such versus because they
have similar structure (i.e. duck typing). C++ straddles this one
with its template system.</p>
  </li>
</ul>

<p>Now a language can be summed up in six tokens (using the order I
presented them here). Unfortunately, applying the terms to languages
is somewhat subjective, especially when languages fall somewhere in
between, so not everyone will come to the same conclusions as I do
here.</p>

<ul>
  <li><strong>Common Lisp</strong>: dynamic, lexical, strong, type-safe, typed, nominal</li>
  <li><strong>Go</strong>: static, lexical, strong, type-safe, typed, structural</li>
  <li><strong>C</strong>: static, lexical, weak, type-unsafe, typed, nominal</li>
  <li><strong>JavaScript</strong>: dynamic, lexical, weak, type-safe, typed, nominal</li>
  <li><strong>Elisp</strong>: dynamic, dynamic, strong, type-safe, typed, nominal</li>
  <li><strong>bash</strong>: n/a, dynamic, n/a, untyped, type-safe, n/a</li>
</ul>

<p><em>Update May 2014</em>: Exactly one day day after I posted this article,
Robert Smallshire made a video covering the same topic in the same
way: <a href="http://www.infoq.com/presentations/dynamic-static-typing">The Unreasonable Effectiveness of Dynamic Typing for Practical
Programs</a>.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>Duck Typing vs. Type Erasure</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2014/04/01/"/>
    <id>urn:uuid:d01a1d2e-2752-35f4-949a-ff69d7f78e22</id>
    <updated>2014-04-01T21:07:31Z</updated>
    <category term="java"/><category term="cpp"/><category term="lang"/><category term="compsci"/>
    <content type="html">
      <![CDATA[<p>Consider the following C++ class.</p>

<pre class="highlight"><code><span class="p">#include</span> <span class="s">&lt;iostream&gt;</span>

<span class="k">template</span> &lt;<span class="k">typename</span> <span class="t">T</span>&gt;
<span class="k">struct</span> <span class="t">Caller</span> {
  <span class="k">const</span> <span class="t">T</span> callee_;
  <span class="t">Caller</span>(<span class="k">const</span> <span class="t">T</span> callee) : callee_(callee) {}
  <span class="t">void</span> go() { callee_.call(); }
};
</code></pre>

<p>Caller can be parameterized to <em>any</em> type so long as it has a <code>call()</code>
method. For example, introduce two types, Foo and Bar.</p>

<pre class="highlight"><code><span class="k">struct</span> <span class="t">Foo</span> {
  <span class="t">void</span> call() <span class="k">const</span> { std::cout &lt;&lt; <span class="s">"Foo"</span>; }
};

<span class="k">struct</span> <span class="t">Bar</span> {
  <span class="t">void</span> call() <span class="k">const</span> { std::cout &lt;&lt; <span class="s">"Bar"</span>; }
};

<span class="t">int</span> <span class="f">main</span>() {
  <span class="t">Caller</span>&lt;<span class="t">Foo</span>&gt; foo{<span class="t">Foo</span>()};
  <span class="t">Caller</span>&lt;<span class="t">Bar</span>&gt; bar{<span class="t">Bar</span>()};
  foo.go();
  bar.go();
  std::cout &lt;&lt; std::endl;
  <span class="k">return</span> <span class="m">0</span>;
}
</code></pre>

<p>This code compiles cleanly and, when run, emits “FooBar”. This is an
example of <em>duck typing</em> — i.e., “If it looks like a duck, swims like
a duck, and quacks like a duck, then it probably is a duck.” Foo and
Bar are unrelated types. They have no common inheritance, but by
providing the expected interface, they both work with with Caller.
This is a special case of <em>polymorphism</em>.</p>

<p>Duck typing is normally only found in dynamically typed languages.
Thanks to templates, a statically, strongly typed language like C++
can have duck typing without sacrificing any type safety.</p>

<h3 id="java-duck-typing">Java Duck Typing</h3>

<p>Let’s try the same thing in Java using generics.</p>

<pre class="highlight"><code><span class="k">class</span> <span class="f">Caller</span>&lt;T&gt; {
    <span class="k">final</span> T callee;
    Caller(T callee) {
        <span class="k">this</span>.callee = callee;
    }
    <span class="k">public</span> <span class="t">void</span> <span class="f">go</span>() {
        callee.call();  <span class="c">// compiler error: cannot find symbol call</span>
    }
}

<span class="k">class</span> <span class="f">Foo</span> {
    <span class="k">public</span> <span class="t">void</span> <span class="f">call</span>() { <span class="t">System</span>.out.print(<span class="s">"Foo"</span>); }
}

<span class="k">class</span> <span class="f">Bar</span> {
    <span class="k">public</span> <span class="t">void</span> <span class="f">call</span>() { <span class="t">System</span>.out.print(<span class="s">"Bar"</span>); }
}

<span class="k">public</span> <span class="k">class</span> <span class="f">Main</span> {
    <span class="k">public</span> <span class="k">static</span> <span class="t">void</span> <span class="f">main</span>(<span class="t">String</span> args[]) {
        <span class="t">Caller</span>&lt;<span class="t">Foo</span>&gt; f = <span class="k">new</span> <span class="t">Caller</span>&lt;&gt;(<span class="k">new</span> <span class="t">Foo</span>());
        <span class="t">Caller</span>&lt;<span class="t">Bar</span>&gt; b = <span class="k">new</span> <span class="t">Caller</span>&lt;&gt;(<span class="k">new</span> <span class="t">Bar</span>());
        f.go();
        b.go();
        <span class="t">System</span>.out.println();
    }
}
</code></pre>

<p>The program is practically identical, but this will fail with a
compile-time error. This is the result of <em>type erasure</em>. Unlike C++’s
templates, there will only ever be one compiled version of Caller, and
T will become Object. Since Object has no <code>call()</code> method, compilation
fails. The generic type is only for enabling additional compiler
checks later on.</p>

<p>C++ templates behave like a macros, expanded by the compiler once for
each different type of applied parameter. The <code>call</code> symbol is looked
up later, after the type has been fully realized, not when the
template is defined.</p>

<p>To fix this, Foo and Bar need a common ancestry. Let’s make this
<code>Callee</code>.</p>

<pre class="highlight"><code><span class="k">interface</span> <span class="f">Callee</span> {
    <span class="t">void</span> <span class="f">call</span>();
}
</code></pre>

<p>Caller needs to be redefined such that T is a subclass of Callee.</p>

<pre class="highlight"><code><span class="k">class</span> <span class="f">Caller</span>&lt;T <span class="k">extends</span> <span class="t">Callee</span>&gt; {
    <span class="c">// ...</span>
}
</code></pre>

<p>This now compiles cleanly because <code>call()</code> will be found in <code>Callee</code>.
Finally, implement Callee.</p>

<pre class="highlight"><code><span class="k">class</span> <span class="f">Foo</span> <span class="k">implements</span> <span class="t">Callee</span> {
    <span class="c">// ...</span>
}

<span class="k">class</span> <span class="f">Bar</span> <span class="k">implements</span> <span class="t">Callee</span> {
    <span class="c">// ...</span>
}
</code></pre>

<p>This is no longer duck typing, just plain old polymorphism. Type
erasure prohibits duck typing in Java (outside of dirty reflection
hacks).</p>

<h3 id="signals-and-slots-and-events-oh-my">Signals and Slots and Events! Oh My!</h3>

<p>Duck typing is useful for implementing the observer pattern without as
much boilerplate. A class can participate in the observer pattern
without <a href="http://raganwald.com/2014/03/31/class-hierarchies-dont-do-that.html">inheriting from some specialized class</a> or interface.
For example, see <a href="http://en.wikipedia.org/wiki/Signals_and_slots">the various signal and slots systems for C++</a>.
In constrast, Java <a href="http://docs.oracle.com/javase/7/docs/api/java/util/EventListener.html">has an EventListener type for everything</a>:</p>

<ul>
  <li>KeyListener</li>
  <li>MouseListener</li>
  <li>MouseMotionListener</li>
  <li>FocusListener</li>
  <li>ActionListener, etc.</li>
</ul>

<p>A class concerned with many different kinds of events, such as an
event logger, would need to inherit a large number of interfaces.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>The Julia Programming Language</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2014/03/06/"/>
    <id>urn:uuid:35f29378-6e92-3d11-43df-9c40139f142e</id>
    <updated>2014-03-06T23:55:44Z</updated>
    <category term="rant"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p><em>Update 2020: This is an old, outdated review. With the benefit of more
experience, I no longer agree with my criticsms in this article.</em></p>

<p>Julia is a new programming language primarily intended for scientific
computing. It’s attempting to take on roles that are currently
occupied by Matlab, its clones, and R. “Matlab done right” could very
well be its tag-line, but it’s more than that. It has a beautiful type
system, it’s homoiconic, and its generic function support would make a
Lisp developer jealous. It still has a long ways to go, but, except
for some unfortunate issues, it’s off to a great start.</p>

<p>Speaking strictly in terms of the language, doing better than Matlab
isn’t really a significant feat. Among major programming languages,
<a href="/blog/2008/08/29/">Matlab’s awfulness and bad design</a> is
<a href="http://old.reddit.com/r/lolphp">second only to PHP</a>. Octave fixes a lot of the Matlab language,
but it can only go so far.</p>

<p>For both Matlab and R, the real strength is the enormous library of
toolboxes and functionality available to help solve seemingly any
scientific computing task. Plus the mindshare and the community. Julia
has none of this yet. The language is mostly complete, but it will
take years to build up its own package library to similar standards.</p>

<p>If you’re curious about learning more, the <a href="http://julia.readthedocs.org/en/latest/manual/">Julia manual</a>
covers the entire language as it currently exists. Unfortunately
anything outside the language proper and its standard library is
under-documented at this time.</p>

<h3 id="a-beautiful-type-system">A Beautiful Type System</h3>

<p>One of the first things you’ll be told is that Julia is <em>dynamically
typed</em>. That is, statically typed (C++, Java, Haskell) versus
dynamically typed (Lisp, Python, JavaScript). However, Julia has the
rather unique property that it straddles between these, and it could
be argued to belong to one or the other.</p>

<p>The defining characteristic of static typing is that <em>bindings</em> (i.e.
variables) have types. In dynamic typing, only values and objects have
types. In Julia, all bindings have a type, making it like a statically
typed language. If no type is explicitly declared, that type is <code>Any</code>,
an abstract supertype of all types. This comes into play with generic
functions.</p>

<p>Both abstract and concrete types can be parameterized by other types,
and certain values. The <code>::</code> syntax it used to declare a type.</p>

<pre class="highlight"><code>type Point {T}
  x::T
  y::T
end
</code></pre>

<p>This creates a <code>Point</code> constructor function. When calling the
constructor, the parameter type can be implicit, derived from the type
of its arguments, or explicit. Because both <code>x</code> and <code>y</code> have the same
type, so must the constructor’s arguments.</p>

<pre class="highlight"><code># Implicit type:
Point(1, -1)
# =&gt; Point{Int64}(1,-1)

# Explicit type:
Point{Float64}(1.1, -1.0)
# =&gt; Point{Float64}(1.1,-1.0)

Point(1, 1.0)
# ERROR: no method Point{T}(Int64,Float64)
</code></pre>

<p>The type can be constrained using <code>&lt;:</code>. If <code>Point</code> is declared like
the following it is restricted to real numbers. This is just like
Java’s <code>Point&lt;T extends Number&gt;</code>.</p>

<pre class="highlight"><code>type Point {T &lt;: Real}
  x::T
  y::T
end
</code></pre>

<p>Unlike most languages, arrays aren’t built directly into the language.
They’re implemented almost entirely in Julia itself using this type
system. The special part is that they get literal syntax.</p>

<pre class="highlight"><code>[1, 2, 3]
# =&gt; Array{Int64,1}

[1.0 2.0; 3.0 4.0]
# =&gt; Array{Float64,2}
</code></pre>

<p>Each Array is parameterized by the type of value it holds and by an
integer, indicating its rank.</p>

<h4 id="the-billion-dollar-mistake">The Billion Dollar Mistake</h4>

<p>Julia has avoided what some call <a href="http://yinwang0.wordpress.com/2013/06/03/null/">The Billion Dollar Mistake</a>:
null references. In languages such as Java, <code>null</code> is allowed in place
of any object of any type. This allowance has lead to many run-time
bugs that, if <code>null</code> didn’t exist, would have been caught at compile
time.</p>

<p>Julia has no <code>null</code> and so there’s no way to make this mistake, though
some kinds of APIs are harder to express without it.</p>

<h3 id="generic-functions">Generic Functions</h3>

<p>All of Julia’s functions are <em>generic</em>, including that <code>Point</code>
constructor above. Different methods can be defined for the same
function name, but for different types. In Common Lisp and Clojure,
generic functions are an opt-in feature, so most functions are not
generic.</p>

<p>Note that this is significantly different than function <em>overloading</em>,
where the specific function to call is determined at compile time. In
multimethods, the method chosen is determined by the run-time type of
its arguments. One of Julia’s notable achievements is that its
multimethods have very high performance. There’s usually more of a
trade-off.</p>

<p>Julia’s operators are functions with special syntax. For example, the
<code>+</code> function,</p>

<pre class="highlight"><code>+(3, 4)
# =&gt; 7
</code></pre>

<p>A big advantage is that operators can be passed around as first-class
values.</p>

<pre class="highlight"><code>map(-, [1, 2, 3])
# [-1, -2, -3]
</code></pre>

<p>Because all functions are generic, operators can have methods defined
for specific types, effectively becoming operator overloading (but
better!).</p>

<pre class="highlight"><code>function +(p1::Point, p2::Point)
  return Point(p1.x + p1.y, p2.x + p2.y)
end

Point(1,1) + Point(1, 2)
# =&gt; Point{Int64}(2,3)
</code></pre>

<p>(Note that to write this method correctly, either <code>Point</code> or the
method should probably <a href="http://julia.readthedocs.org/en/latest/manual/conversion-and-promotion/">promote</a> its arguments.)</p>

<h3 id="foreign-function-interface">Foreign Function Interface</h3>

<p>Julia has a <em>really</em> slick foreign function interface (FFI). Libraries
don’t need to be explicitly loaded and call interfaces don’t have to
be declared ahead of time. That’s all taken care of automatically.</p>

<p>I’m not going to dive into the details, but basically all you have to
do is indicate the library, the function, the return type, and then
pass the arguments.</p>

<pre class="highlight"><code>ccall((:clock, "libc"), Int32, ())
# =&gt; 2292761
</code></pre>

<p>Generally this would be wrapped up nicely in a regular function and
the caller would have no idea an FFI is being used. Unfortunately
structs aren’t yet supported.</p>

<h3 id="julias-problems">Julia’s Problems</h3>

<p>Not everything is elegant, though. There are some strange design
decisions. The two big ones for me are strings and modules.</p>

<h4 id="confused-strings">Confused Strings</h4>

<p>Julia has a <code>Char</code> type that represents a Unicode code point. It’s a
32-bit value. So far so good. However, a <code>String</code> is <em>not</em> a sequence
of these. A Julia string is a byte-array of UTF-8 encoded characters.</p>

<p>Indexing into a string operates on <em>bytes</em> rather than characters.
Attempting to index into the middle of a character results in an
error. Yuck!</p>

<pre class="highlight"><code>"naïvety"[4]
# ERROR: invalid UTF-8 character index
</code></pre>

<p>I don’t understand why this behavior was chosen. This would make sense
if Julia was an old language and this was designed before Unicode was
established (e.g. C). But, no, this is a brand new language. There’s
no excuse not to get this right the first time. I suspect it has to do
with Julia’s FFI.</p>

<h4 id="clunky-closed-modules">Clunky, Closed Modules</h4>

<p>Julia’s module system looks like it was taken right out of Scheme’s
R6RS. This isn’t a good thing.</p>

<p>The <code>module</code> definition that wraps the entire module up in a single
syntactic unit. Here’s an example from the documentation. According to
the style guide, the body of the module is not indented out.</p>

<pre class="highlight"><code>module MyModule
using Lib
export MyType, foo

type MyType
  x
end

bar(x) = 2x
foo(a::MyType) = bar(a.x) + 1

import Base.show
show(io, a::MyType) = print(io, "MyType $(a.x)")
end
</code></pre>

<p>That final <code>end</code> seals the module for good. There’s no opening the
module back up to define or redefine new functions or types. If you
want to change something you have to reload the entire module, which
will obsolete any type instances.</p>

<p>Compare this to Clojure, where the module isn’t wrapped up in a
syntactical construct.</p>

<pre class="highlight"><code>(ns my.module
  (require : [clojure.set :refer [rename-keys]]))
</code></pre>

<p>Common Lisp’s <code>defpackage</code> also works like this. At any time you can
jump into a namespace and make new definitions.</p>

<pre class="highlight"><code>(in-ns 'my.module)
</code></pre>

<p>This is absolutely essential to <a href="/blog/2012/10/31/">interactive development</a>. The
lack of this makes Julia <em>far</em> less dynamic than it should be.
Combined with the lack of a printer, <strong>Julia is not currently suitable
as an interactive interpreter subprocess</strong> (Slime, Cider, Skewer,
etc.).</p>

<p>This is a real shame, because I’d like to start playing around with
Julia, but right now it feels like a chore. It’s needlessly restricted
to a C++/Java style workflow.</p>

<p>I’ll probably revisit Julia once it’s had a few more years to mature.
Then we’ll see if things have improved enough for real use.</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>.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>

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

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

<pre class="highlight"><code>(#[<span class="m">0</span> <span class="s">"\300\207"</span> [] <span class="m">0</span>])
</code></pre>

<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>[...]</code> vs.
<code>#[...]</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>make-byte-code</code>.
<a href="/blog/2012/07/17/">Like vector literals</a>, byte-code literals don’t need to be
quoted.</p>

<pre class="highlight"><code>(make-byte-code <span class="m">0</span> <span class="s">""</span> [] <span class="m">0</span>)
<span class="c">;; =&gt; #[0 "" [] 0]</span>

#[<span class="m">1</span> <span class="m">2</span> <span class="m">3</span> <span class="m">4</span>]
<span class="c">;; =&gt; #[1 2 3 4]</span>

(#[<span class="m">0</span> <span class="s">""</span> [] <span class="m">0</span>])
<span class="c">;; error: Invalid byte opcode</span>
</code></pre>

<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>

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

<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>&amp;optional</code>, and <code>&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>&amp;rest</code> arguments (up to 1). The remaining bits indicate the total
number of optional and required arguments (not counting <code>&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>

<pre class="highlight"><code>(byte-compile-make-args-desc '())
<span class="c">;; =&gt; #x000  (0 args, 0 rest, 0 required)</span>

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

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

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

<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>unibyte-string</code> because <code>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>

<pre class="highlight"><code>(unibyte-string <span class="m">100</span> <span class="m">200</span> <span class="m">250</span>)
<span class="c">;; =&gt; "d\310\372"</span>
</code></pre>

<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>vector</code> or a vector literal. Operands
reference either this vector or they index into the stack itself.</p>

<pre class="highlight"><code>(byte-compile (<span class="k">lambda</span> (a b) (my-func b a)))
<span class="c">;; =&gt; #[(a b) "\302\134\011\042\207" [b a my-func] 3]</span>
</code></pre>

<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>[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>.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>interactive</code>
in the uncompiled function definition.</p>

<pre class="highlight"><code>(byte-compile (<span class="k">lambda</span> (n) (<span class="k">interactive</span> <span class="s">"nNumber: "</span>) n))
<span class="c">;; =&gt; #[(n) "\010\207" [n] 1 nil "nNumber: "]</span>

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

<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>stack-ref</code>) stack reference</li>
  <li>8 - 15  : (<code>varref</code>) variable reference (from constants vector)</li>
  <li>16 - 23 : (<code>varset</code>) variable set (from constants vector)</li>
  <li>24 - 31 : (<code>varbind</code>) variable binding (from constants vector)</li>
  <li>32 - 39 : (<code>call</code>) function call (immediate = number of arguments)</li>
  <li>40 - 47 : (<code>unbind</code>) variable unbinding (from constants vector)</li>
  <li>129, 192-255 : (<code>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>2</code>” copies the third stack item to the top of the stack.
An instruction of “<code>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>foo</code>. Each opcode has a named constant
of <code>byte-X</code> so we don’t have to worry about their actual byte-code
number.</p>

<pre class="highlight"><code>(<span class="k">require</span> <span class="v">'bytecomp</span>)  <span class="c">; named opcodes</span>

(<span class="k">defvar</span> <span class="f">foo</span> <span class="s">"hello"</span>)

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

(get-foo)
<span class="c">;; =&gt; "hello"</span>
</code></pre>

<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>

<pre class="highlight"><code>(<span class="k">defalias</span> <span class="v">'get-foo</span>
  (make-byte-code
    <span class="m">#x000</span>
    (unibyte-string
      (+ <span class="m">3</span> byte-varref)     <span class="c">; 4th form of varref</span>
      byte-return)
    [<span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> foo]
    <span class="m">1</span>))
</code></pre>

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

<pre class="highlight"><code>(<span class="k">defalias</span> <span class="v">'get-foo</span>
  (make-byte-code
    <span class="m">#x000</span>
    (unibyte-string
      (+ <span class="m">6</span> byte-varref)     <span class="c">; 7th form of varref</span>
      <span class="m">9</span>                     <span class="c">; operand, (constant index 9)</span>
      byte-return)
    [<span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> foo]
    <span class="m">1</span>))
</code></pre>

<p>Dynamically-scoped code makes heavy use of <code>varref</code> but
lexically-scoped code rarely uses it (global variables only), instead
relying heavily on <code>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>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>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>&amp;optional</code> argument it pushes a nil onto the stack. If the
function has a <code>&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>

<pre class="highlight"><code><span class="c">;; -*- lexical-binding: t -*-</span>
(<span class="k">defun</span> <span class="f">foo</span> (x) x)

(symbol-function <span class="v">#'foo</span>)
<span class="c">;; =&gt; #[#x101 "\207" [] 2]</span>
</code></pre>

<p>The byte-code for <code>foo</code> is a single instruction: <code>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>

<pre class="highlight"><code><span class="c">;; (As of this writing `byte-compile' always uses dynamic scope.)</span>

(byte-compile <span class="v">'foo</span>)
<span class="c">;; =&gt; #[(x) "\010\207" [x] 1]</span>
</code></pre>

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

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

<pre class="highlight"><code>(disassemble <span class="v">#'foo</span>)
<span class="c">;; byte code:</span>
<span class="c">;;   args: (x)</span>
<span class="c">;; 0       varref    x</span>
<span class="c">;; 1       return</span>
</code></pre>

<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>(opcode . operand)</code>, and a program is a list of these.</p>

<p>Let’s rewrite our last <code>get-foo</code> using lapcode.</p>

<pre class="highlight"><code>(<span class="k">defalias</span> <span class="v">'get-foo</span>
  (make-byte-code
    <span class="m">#x000</span>
    (byte-compile-lapcode
      '((byte-varref . <span class="m">9</span>)
        (byte-return)))
    [<span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> <span class="k">nil</span> foo]
    <span class="m">1</span>))
</code></pre>

<p>We didn’t have to worry about which form of <code>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>JavaScript Function Statements vs. Expressions</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2013/05/14/"/>
    <id>urn:uuid:aa5d1d3f-60c2-3b30-41c6-833dfee56cce</id>
    <updated>2013-05-14T00:00:00Z</updated>
    <category term="javascript"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>The JavaScript <code>function</code> keyword has two meanings depending on how
it’s used: as a <em>statement</em> or as an <em>expression</em>. It’s a statement
when the keyword appears at the top-level of a block. This is known as
a <em>function declaration</em>.</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>() {
    <span class="c">// ...</span>
}
</code></pre>

<p>This statement means declare a variable called <code>foo</code> in the current
scope, create a closure named <code>foo</code>, and assign this closure to this
variable. Also, this assignment is “lifted” such that it happens
before any part of the body of the surrounding function is evaluated,
including before any variable assignments.</p>

<p>Notice that the closure’s name is separate from the variable name.
Except for a certain well-known JavaScript engine, closure/function
objects have a read-only <code>name</code> property.</p>

<pre class="highlight"><code>foo.name; <span class="c">// =&gt; "foo"</span>
</code></pre>

<p>A name is required for function declarations, otherwise they would be
no-ops. This name also appears in debugging backtraces.</p>

<p>A function’s name has different semantics in <em>function expressions</em>.
The <code>function</code> keyword is an expression when used in an expression
position of a statement.</p>

<pre class="highlight"><code><span class="k">var</span> foo = <span class="k">function</span>() {
    <span class="c">// ...</span>
}
</code></pre>

<p>The function expression above evaluates to an anonymous closure, which
is then assigned to the variable <code>foo</code>. This is <em>nearly</em> identical to
the previous function declaration except for two details.</p>

<ul>
  <li>
    <p>Explicit variable assignments are never lifted, unlike function
declarations. This assignment will happen exactly where it appears
in the code.</p>
  </li>
  <li>
    <p>The resulting closure is anonymous. The <code>name</code> property, if
available, will be an empty string. Furthermore, <strong>the lack of name
affects the scope of the function</strong>. I’ll get back to that point in
a moment.</p>
  </li>
</ul>

<h3 id="iifes">IIFEs</h3>

<p>An immediately-invoked function expression (IIFE), used to establish a
one-off local scope, is typically wrapped in parenthesis. The purpose
of the parenthesis is to put <code>function</code> in an expression position so
that it is a function expression rather than a function declaration.</p>

<pre class="highlight"><code>(<span class="k">function</span>() {
    <span class="c">// ... declare variables, etc.</span>
}());
</code></pre>

<p>Another way to put <code>function</code> in an expression position is to precede
it with an unary operator. This is an example of being clever instead
of practical.</p>

<pre class="highlight"><code>!<span class="k">function</span>() {
    <span class="c">// ... declare variables, etc.</span>
}();
</code></pre>

<p>If <code>function</code> is already in an expression position, the wrapping
parenthesis are unnecessary. For example,</p>

<pre class="highlight"><code><span class="k">var</span> foo = <span class="k">function</span>() { <span class="k">return</span> <span class="s">"bar"</span>; }();
foo; <span class="c">// =&gt; "bar"</span>
</code></pre>

<p>However, it may still be a good idea to wrap the IIFE in parenthesis
just to help other programmers read your code. A casual glance that
doesn’t notice the function invocation would assume a function is
being assigned to <code>foo</code>. Wrapping a function expression with
parenthesis is a well-known idiom for IIFEs.</p>

<h3 id="function-name-and-scope">Function Name and Scope</h3>

<p>What happens when a function expression is given a name? Two things.</p>

<ol>
  <li>
    <p>The name will appear in the <code>name</code> property of the closure (if
available). Also, the name will also show up in backtraces. This
makes naming closures a handy debugging technique.</p>
  </li>
  <li>
    <p><strong>The name becomes a variable in the scope of the function</strong>. This
means it’s possible to write recursive function expressions!</p>
  </li>
</ol>

<pre class="highlight"><code><span class="k">function</span> <span class="f">maths</span>() {
    <span class="k">return</span> {
        <span class="c">// ...</span>
        fact: <span class="k">function</span> <span class="f">fact</span>(n) {
            <span class="k">return</span> n === <span class="m">0</span> ? <span class="m">1</span> : n * fact(n - <span class="m">1</span>);
        }
    };
}

maths().fact(<span class="m">10</span>); <span class="c">// =&gt; 3628800</span>
</code></pre>

<p>The <code>fact</code> function is evaluated as a function expression as part of
this object literal. The variable <code>fact</code> is established in the scope
of the function <code>fact</code>, assigned to the function itself, allowing the
function to call itself. It’s a self-contained recursive function.</p>

<h3 id="pop-quiz-function-name-and-scope">Pop Quiz: Function Name and Scope</h3>

<p>Given this, try to determine the answer to this problem in your head.
What does the second invocation of <code>foo</code> evaluate to?</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>() {
    foo = <span class="k">function</span>() {
        <span class="k">return</span> <span class="s">"function two"</span>;
    };
    <span class="k">return</span> <span class="s">"function one"</span>;
}

foo(); <span class="c">// =&gt; "function one"</span>
foo(); <span class="c">// =&gt; ???</span>
</code></pre>

<p>Here’s where we come to the major difference between function
declarations and function expressions. The answer is <code>"function two"</code>.
Even though functions declarations create named functions, <strong>these
functions do not have the implicit self-named variable in its scope</strong>.
Unless this variable is declared explicitly, the name will refer to a
variable in a containing scope.</p>

<p>This has the useful property that a function can re-define itself
<em>and</em> be correctly named at the same time. If the function needs to
perform expensive first-time initialization, such reassignment can be
used to do it lazily without exposing any state <em>and</em> without
requiring an is-initialized check on each invocation. For example,
this trick is exactly how Emacs autoloading works.</p>

<p>If this function declaration is converted to what <em>appears</em> to be the
equivalent function expression form the difference is obvious.</p>

<pre class="highlight"><code><span class="k">var</span> foo = <span class="k">function</span> <span class="f">foo</span>() {
    foo = <span class="k">function</span>() {
        <span class="k">return</span> <span class="s">"function two"</span>;
    };
    <span class="k">return</span> <span class="s">"function one"</span>;
};

foo(); <span class="c">// =&gt; "function one"</span>
foo(); <span class="c">// =&gt; "function one"</span>
</code></pre>

<p>The reassignment happens in the function’s scope, leaving the outer
scope’s assignment intact. For better or worse, even ignoring
assignment lifting, there’s no way to perfectly emulate function
declaration using a function expression.</p>
]]>
    </content>
  </entry>
  <entry>
    <title>JavaScript Truthiness Quiz</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/11/28/"/>
    <id>urn:uuid:d621c4c0-9f17-3c95-d907-8e57d029dfe5</id>
    <updated>2012-11-28T00:00:00Z</updated>
    <category term="javascript"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>I’ve got another quirky JavaScript quiz for you. This one has two
different answers.</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>(object) {
    object.bar = <span class="k">false</span>;
    <span class="k">return</span> object.bar &amp;&amp; <span class="k">true</span>;
}

foo(___); <span class="c">// Fill in an argument such that foo() returns true.</span>
</code></pre>

<p>Obviously a normal object won’t do the job. Something more special is
needed.</p>

<pre class="highlight"><code>foo({bar: <span class="k">true</span>});  <span class="c">// =&gt; false</span>
</code></pre>

<p>The fact that <code>foo()</code> <em>can</em> return <code>true</code> could introduce a bug during
refactoring: the code initially appears to be a tautology that could
be reduced to a simpler <code>return false</code>. Since this quiz has solutions
that’s obviously not true.</p>

<p>Had I reversed the booleans — assign <code>bar</code> to <code>true</code> and make this
function return a falsy value — then almost any immutable object,
such as a string, would do.</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo2</span>(object) {
    object.bar = <span class="k">true</span>;  <span class="c">// inverse</span>
    <span class="k">return</span> object.bar &amp;&amp; <span class="k">true</span>;
}

foo2(<span class="s">"baz"</span>);  <span class="c">// =&gt; undefined</span>
</code></pre>

<p>The <code>bar</code> assignment would fail and attempting to access it would
return <code>undefined</code>, which is falsy.</p>

<h3 id="answer">Answer</h3>

<p>The two approaches are <em>getters</em> and <em>property descriptors</em>.</p>

<h4 id="getters">Getters</h4>

<p>JavaScript <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects#Defining_getters_and_setters">directly supports getter and setter properties</a>.
Without special language support, such accessors could be accomplished
with plain methods (like Java).</p>

<pre class="highlight"><code><span class="k">var</span> lovesBlue = {
    _color: <span class="s">"blue"</span>,  <span class="c">// private</span>

    getColor: <span class="k">function</span>() {
        <span class="k">return</span> <span class="k">this</span>._color;
    },

    <span class="c">/* Only blue colors are allowed! */</span>
    setColor: <span class="k">function</span>(color) {
        <span class="k">if</span> (<span class="s">/blue/</span>.test(color)) {
            <span class="k">this</span>._color = color;
        }
        <span class="k">return</span> <span class="k">this</span>._color;
    }
};

lovesBlue.getColor();              <span class="c">// =&gt; "blue"</span>
lovesBlue.setColor(<span class="s">"red"</span>);         <span class="c">// =&gt; "blue" (set fails)</span>
lovesBlue.setColor(<span class="s">"light blue"</span>);  <span class="c">// =&gt; "light blue"</span>
</code></pre>

<p>JavaScript allows properties themselves to transparently run methods,
such as to enforce invariants, even though it’s not an obvious call
site. This is how many of the browser environment objects
work. There’s a special syntax with <code>get</code> and <code>set</code> keywords. (Keep
this mind, JSON parser writers!)</p>

<pre class="highlight"><code><span class="k">var</span> lovesBlue = {
    _color: <span class="s">"blue"</span>,

    get color() {
        <span class="k">return</span> <span class="k">this</span>._color;
    },

    set color(color) {
        <span class="k">if</span> (<span class="s">/blue/</span>.test(color)) {
            <span class="k">this</span>._color = color;
        }
    }
};

lovesBlue.color = <span class="s">"red"</span>;  <span class="c">// =&gt; "red", but assignment fails</span>
lovesBlue.color;          <span class="c">// =&gt; "blue"</span>
</code></pre>

<p>This can be used to solve the quiz,</p>

<pre class="highlight"><code>foo({get bar() { <span class="k">return</span> <span class="k">true</span>; }});
</code></pre>

<p>Because <code>bar</code> is a getter with no associated setter, there’s
effectively no assigning values to <code>bar</code> and it always evaluates to
<code>true</code>.</p>

<h4 id="property-descriptors">Property descriptors</h4>

<p>Object properties <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty">themselves have properties</a>, called
<em>descriptors</em>, governing their behavior. The accessors above are
examples of the descriptors <code>get</code> and <code>set</code>. For our situation there’s
a <code>writable</code> descriptor which determines whether or not a particular
property can be assigned. If you really wanted to lock this in,
there’s even a metadescriptor (a <em>metameta</em>property?), <code>configurable</code>,
that determines whether or not a property’s descriptors, including
itself, can be modified.</p>

<p>There’s no literal syntax for them, but these descriptors can be set
with <code>Object.defineProperty()</code>. Conveniently, this function returns
the object being modified.</p>

<pre class="highlight"><code>foo(Object.defineProperty({}, <span class="s">'bar'</span>, {value: <span class="k">true</span>, writable: <span class="k">false</span>}));
</code></pre>

<p>This creates a new object, sets <code>bar</code> to <code>true</code>, and locks that in by
making the property read-only.</p>

<p>The fact that attempting to assign a read-only property <em>silently</em>
fails instead of throwing an exception is probably another mistake in
the language’s design. While this behavior newbie-friendly, it allows
bugs to slip by undetected, only to be found much later when they’re
more expensive to address. It makes JavaScript programs more brittle.</p>

<h4 id="existing-objects-a-third-approach">Existing objects: a third approach?</h4>

<p>If you’re lazy and in a browser environment, you don’t even need to
construct new objects to solve the problem. There are some already
lying around! My favorite is HTML5’s <code>localStorage</code>. It stringifies
all property assignments. This means that <code>false</code> becomes <code>"false"</code>,
which is truthy.</p>

<pre class="highlight"><code>foo(localStorage);  <span class="c">// =&gt; true</span>
</code></pre>

<p>This is arguably a third approach because the stringification behavior
can’t be accomplished with either normal accessors or descriptors
alone.</p>

]]>
    </content>
  </entry>
  <entry>
    <title>Raising the Dead with JavaScript</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/11/20/"/>
    <id>urn:uuid:8ba4b264-c7ce-33f5-d69f-51a6240a24b3</id>
    <updated>2012-11-20T00:00:00Z</updated>
    <category term="javascript"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>After my <a href="/blog/2012/11/19/">last post</a>, <a href="http://devrand.org/">Gavin</a> sent me this:
<a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Scope_Cheatsheet">Scope Cheatsheet</a>. Besides its misleading wording, an
interesting fact stood out and gave me another JavaScript challenge
question. I’ll also show you how it allows JavaScript to raise the
dead!</p>

<h3 id="background">Background</h3>

<p>Like its close cousin, Scheme, JavaScript is a <a href="http://en.wikipedia.org/wiki/Common_Lisp#The_function_namespace">Lisp-1</a>:
functions and variables share the same namespace. In Scheme, the
<code>define</code> form defines new variables.</p>

<pre class="highlight"><code>(<span class="k">define</span> <span class="f">foo</span> <span class="s">"Hello, world!"</span>)
</code></pre>

<p>Combine with the <code>lambda</code> form and it can be used to name functions,</p>

<pre class="highlight"><code>(<span class="k">define</span> <span class="f">square</span> (<span class="k">lambda</span> (x) (* x x)))

(square <span class="m">-4</span>)  <span class="c">;; =&gt; 16</span>
</code></pre>

<p>The variable <code>square</code> is assigned to an anonymous function, and
afterward it can be called as a function. Since this is so common,
there’s a syntactic shorthand (<em>sugar</em>) for this,</p>

<pre class="highlight"><code>(<span class="k">define</span> (<span class="f">square</span> x) (* x x))
</code></pre>

<p>Notice that the first argument to <code>define</code> is now a list rather than a
symbol. This is a signal to <code>define</code> that a function is being defined,
and that this should be expanded into the <code>lambda</code> example
above. (Also note that the declaration mimics a function call, which
is pretty neat.)</p>

<p>JavaScript also has syntactic sugar for the same purpose. The <code>var</code>
statement establishes a binding in the current scope. This can be used
to define both variables and functions, since they share a
namespace. For convenience, in addition to defining an anonymous
function, the <code>function</code> statement can be used to declare a variable
<em>and</em> assign it a function. These definitions below are equivalent
… most of the time.</p>

<pre class="highlight"><code><span class="k">var</span> square = <span class="k">function</span>(x) {
    <span class="k">return</span> x * x;
}

<span class="k">function</span> <span class="f">square</span>(x) {
    <span class="k">return</span> x * x;
}
</code></pre>

<p>The second definition is actually more magical than a syntactic
shorthand, which leads into my quiz.</p>

<h3 id="quiz">Quiz</h3>

<pre class="highlight"><code><span class="k">function</span> <span class="f">bar</span>() {
    <span class="k">var</span> foo = <span class="m">0</span>;
    <span class="k">function</span> <span class="f">foo</span>() {}
    <span class="k">return</span> <span class="k">typeof</span> foo;
}

bar(); <span class="c">// What does this return? Why?</span>

<span class="k">function</span> <span class="f">baz</span>() {
    <span class="k">var</span> foo;
    <span class="k">function</span> <span class="f">foo</span>() {}
    <span class="k">return</span> <span class="k">typeof</span> foo;
}

baz(); <span class="c">// How about now?</span>

<span class="k">function</span> <span class="f">quux</span>() {
    <span class="k">var</span> foo = <span class="m">0</span>;
    <span class="k">var</span> foo = <span class="k">function</span> () {}
    <span class="k">return</span> <span class="k">typeof</span> foo;
}

quux(); <span class="c">// How about now?</span>
</code></pre>

<p>We have three functions, <code>bar()</code>, <code>baz()</code>, and <code>quux()</code>, each slightly
different. Try to figure out the return value of each without running
them in a JavaScript interpreter. Reading the cheatsheet should give
you a good idea of the answer.</p>

<h3 id="answer">Answer</h3>

<p>Figured it out? The first function, <code>bar()</code>, is the surprising one. If
the special <code>function</code> form was merely syntactic sugar then all this
means is that <code>foo</code> is redundantly declared (and re-assigned before
accessing it, which the compiler could optimize). The final assignment
is a function, so it should return <code>'function'</code>.</p>

<p>However, <em>this is not the case!</em> This function returns <code>'number'</code>. The
first assignment listed in the code actually happens <em>after</em> the
second assignment, the function definition. This is because functions
defined using the special syntax are <em>hoisted</em> to the top of the
function. The function assignments are evaluated before any other part
of the function body. This is the extra magic behind the special
<code>function</code> syntax.</p>

<p>The effect is more apparent when looking at the return value of
<code>quux()</code>, which is <code>'function'</code>. The special <code>function</code> syntax isn’t
used so the assignments are performed in the order that they’re
listed. This isn’t surprising, except for the fact that variables can
be declared multiple times in a scope without any sort of warning.</p>

<p>The second function, <code>baz()</code>, returns <code>'function'</code>. The function
definition is still hoisted but the variable declaration performs no
assignment. The function assignment is not overridden. Because of the
lack of assignment, nothing actually happens at all for the variable
declaration.</p>

<p>Now, this seems to be a cloudy concept for even skilled programmers: a
variable declaration like <code>var foo = 0</code> accomplishes <em>two</em> separate
things. The merge of these two tasks into a single statement is merely
one of convenience.</p>

<ol>
  <li>
    <p><strong>Declaration</strong>: declares a variable, modifying the <em>semantics</em>
of the function’s body. It changes what <em>place</em> in memory an
<em>identifier</em> in the current scope will refer to. This is a
compile-time activity. Nothing <em>happens</em> at run time — there is
no <em>when</em>. When function definitions are hoisted, it’s the
<em>assignment</em> (part 2) that gets hoisted. In C, variables are
initially assigned to stack garbage (globals are zeroed). In
JavaScript, variables are initially assigned to <code>undefined</code>.</p>
  </li>
  <li>
    <p><strong>Assignment</strong>: <em>binds</em> a variable to a new value. This is
evaluated at run time. It matters <em>when</em> this happens in relation
to other evaluations.</p>
  </li>
</ol>

<p>Consider this,</p>

<pre class="highlight"><code><span class="k">var</span> foo = foo;
</code></pre>

<p>The expression on the right-hand side is evaluated in the same scope
as the variable declaration. <code>foo</code> is initially assigned to
<code>undefined</code>, then it is re-assigned to <code>undefined</code>. This permits
recursive functions to be defined with <code>var</code> — otherwise the
identifier used to make the recursive call wouldn’t refer to the
function itself.</p>

<pre class="highlight"><code><span class="k">var</span> factorial = <span class="k">function</span>(n) {
    <span class="k">if</span> (n === <span class="m">0</span>)
        <span class="k">return</span> <span class="m">1</span>;
    <span class="k">else</span>
        <span class="k">return</span> factorial(n - <span class="m">1</span>) * n;
};
</code></pre>

<p>In contrast, Lisp’s <code>let</code> does not evaluate the right-hand side within
the scope of the <code>let</code>, so recursive definitions are not possible with
a regular <code>let</code>. This is the purpose of <code>letrec</code> (Scheme) and <code>labels</code>
(Common Lisp).</p>

<pre class="highlight"><code><span class="c">;; Compile error, x is unbound</span>
(<span class="k">let</span> ((x x))
  x)
</code></pre>

<h3 id="why-function-hoisting">Why function hoisting?</h3>

<p>JavaScript’s original goal was to be easy for novices to program. I
think that they wanted users to be able to define functions anywhere
in a function (at the top level) without thinking about it. Novices
generally don’t think of functions as values, so this is probably more
intuitive for them. To accomplish this, the assignment needs to happen
before the real body of the function. Unfortunately, this leads to
surprising behavior, and, ultimately, it was probably a bad design
choice.</p>

<p>Below, in any other language the function definition would be dead
code, unreachable by any valid control flow, and the compiler would be
free to toss it.</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>() {
    <span class="k">return</span> baz();
    <span class="k">function</span> <span class="f">baz</span>() { <span class="k">return</span> <span class="s">'Hello'</span>; }
}

foo(); <span class="c">// =&gt; 'Hello'</span>
</code></pre>

<p>But in JavaScript you can raise the dead!</p>

]]>
    </content>
  </entry>
  <entry>
    <title>JavaScript Debugging Challenge</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/11/19/"/>
    <id>urn:uuid:51e6cdc8-6562-3ac7-e298-ddb0c989f81a</id>
    <updated>2012-11-19T00:00:00Z</updated>
    <category term="javascript"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>As I’ve been exploring JavaScript I’ve come up with a few interesting
questions that expose some of JavaScript’s quirks. This is the first
one, which I came up with over the weekend. Study it and try to come
up with an answer before looking a the explanation below. Go ahead and
use a JavaScript interpreter or debugger to poke at it if you need to.</p>

<pre class="highlight"><code><span class="k">var</span> count = <span class="m">4</span>;

<span class="k">function</span> <span class="f">foo</span>() {
    <span class="k">var</span> table = [count];

    <span class="c">/* Build the table. */</span>
    <span class="k">while</span> (count-- &gt; <span class="m">0</span>) {
        table.push([]);
    }

    <span class="c">/* Fill it with numbers. */</span>
    <span class="k">for</span> (<span class="k">var</span> count = <span class="m">1</span>; count &lt; table.length; count++) {
        table[count].push(count);
    }
    <span class="k">return</span> table;
}

foo(); <span class="c">// What does this return? And why?</span>
</code></pre>

<p>When I originally came up with the problem, I just enabled
<a href="http://50ply.com/blog/2012/08/13/introducing-impatient-mode/">impatient-mode</a> in my editor buffer to share it friends. It’s a
really convenient alternative to pastebins!</p>

<h3 id="answer">Answer</h3>

<p>If you’ve gotten this far either you figured it out or you gave up
(hopefully not right away!). Without careful attention, the expected
output would be <code>[4, [1], [2], [3], [4]]</code>. Create an array containing
<code>count</code>, push on <code>count</code> arrays, and finally iterate over the whole
thing. Seems simple.</p>

<p>However, the actual return value is <code>[undefined]</code>, which at first may
seem to defy logic. There’s a bit of a double-trick to this question
due to the way I wrote it.</p>

<p>The first trick is that this might appear to be a quirk in the Array
<code>push()</code> method. If you pass an array to <code>push()</code> does it actually
concatenate the array, flattening out the result? If it did, pushing
an empty array would result in nothing. This is not the case,
fortunately.</p>

<pre class="highlight"><code><span class="k">var</span> foo = [<span class="m">1</span>, <span class="m">2</span>, <span class="m">3</span>];
foo.push([]);  <span class="c">// foo = [1, 2, 3, []]</span>
</code></pre>

<p>The real quirk here is JavaScript’s strange scoping rules. JavaScript
only has <em>function</em> scope <sup id="fnref:let"><a href="#fn:let" class="footnote" rel="footnote" role="doc-noteref">1</a></sup>, not <em>block</em> scope like most other
languages. A loop, including <code>for</code>, doesn’t get its own scope so the
looping variables are actually hoisted into the function scope. For
the first two uses of <code>count</code>, it isn’t actually a <em>free variable</em>
like it appears to be. It refers to the <code>for</code> loop variable <code>count</code>
even though it’s declared later in the function.</p>

<p>A variable doesn’t spring into existence at the place it’s declared —
otherwise that would be a sort-of hidden nested scope. The binding’s
extent is determined at <em>compile-time</em> (i.e. <em>lexical</em> scope). If the
variable is declared anywhere in the function with <code>var</code>, it is bound
for the entire body of the function. In contrast, C requires that
variables be declared before they are used. This isn’t strictly
necessary from the compiler’s point of view, but it keeps humans from
making mistakes like above. A C variable “exists” (barring
optimizations, it’s been allocated space on the stack) for the entire
block it’s declared in, but since it can’t be referenced before the
declaration that detail has no visible effect.</p>

<p>In the code above, because <code>count</code> was not assigned any value at the
beginning of the function, it is initially bound to <code>undefined</code>, which
is coerced into <code>0</code> when used as a number. The result is that the
array is initially filled with <code>undefined</code>, then zero arrays are
pushed onto it. In the final loop, the array doesn’t have any elements
to loop over so nothing happens and <code>[undefined]</code> is returned.</p>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:let">
      <p>JavaScript 1.7 actually has <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/let">block scope when using <code>let</code></a>, but <code>let</code> is not widely supported yet. <a href="#fnref:let" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>
]]>
    </content>
  </entry>
  <entry>
    <title>JavaScript's Quirky eval</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2012/11/14/"/>
    <id>urn:uuid:8676a0fa-c6ea-3231-717c-259c363be653</id>
    <updated>2012-11-14T00:00:00Z</updated>
    <category term="javascript"/><category term="lang"/>
    <content type="html">
      <![CDATA[<p>The infamous <code>eval</code> function is a strange beast in <em>any</em> language, but
I think JavaScript’s is perhaps the strangest incarnation. Its very
presence in a function foils any possibility of optimization, because
it is capable of wreaking so much havoc.</p>

<p>The purpose of <code>eval</code> is to take an arbitrary data structure
containing a program (usually a string) and evaluate it. Most of the
time the use <code>eval</code> indicates a bad program — its use completely
unnecessary, very slow, and probably dangerous. There are exceptions,
like <a href="/blog/2012/10/31/">Skewer</a>, where a REPL is being provided to a
developer. <em>Something</em> needs to perform the “E” part of REPL.</p>

<p>If the language’s platform already has a parser and
compiler/interpreter around, like an interpreted language, it’s most
of the way to having an <code>eval</code>. <code>eval</code> just exposes the existing
functionality directly to programs. In a brute-force, trivial
approach, the string to be evaluated could be written to a file and
loaded like a regular program.</p>

<h3 id="semantics">Semantics</h3>

<p>However, executing arbitrary code in an established context is
non-trivial. When a program is compiled, the compiler maps out the
program’s various lexical bindings at compile time. For example, when
compiling C, a function’s variables become offsets from the stack
pointer. As an optimization, unused variables can be discarded, saving
precious stack space. If the code calling <code>eval</code> has been compiled
like this and the evaluation is being done in the same lexical
environment as the call, then <code>eval</code> needs to be able to access this
mapping in order to map identifiers to bindings.</p>

<p>This complication can be avoided if the <code>eval</code> is explicitly done in
the global context. For example, take <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_eval.htm">Common Lisp’s <code>eval</code></a>.</p>

<blockquote>
  <p>Evaluates form in the current <em>dynamic environment</em> and the <em>null
lexical environment</em>.</p>
</blockquote>

<p>This means lexical bindings are not considered, only dynamic (global)
bindings. In the expression below, <code>foo</code> is bound lexically so <code>eval</code>
has no access to it. The compilation and optimization of this code is
unaffected by the <code>eval</code>. It’s about as complicated as loading a new
source file with <code>load</code>.</p>

<pre class="highlight"><code>(<span class="k">let</span> ((foo <span class="v">'bar</span>))
  (eval <span class="v">'foo</span>))  <span class="c">; error, foo is unbound</span>
</code></pre>

<p><a href="http://docs.python.org/2/library/functions.html#eval">Python</a> and <a href="http://ruby.about.com/od/advancedruby/a/Bindings.htm">Ruby</a> are similar, where <code>eval</code> is done in
the global environment. In both cases, an evaluation environment can
be passed explicitly as an additional argument.</p>

<p>In <a href="http://perldoc.perl.org/functions/eval.html">Perl</a> things start to get a bit strange (string
version). <code>eval</code> <em>is</em> done in the current lexical
environment. <del>However, no assignments, either to change bindings
or modify data structures, are visible outside of the
<code>eval</code>.</del> (Fixed a string interpolation mistake.)</p>

<pre class="highlight"><code><span class="k">sub</span> <span class="f">foo</span> {
    <span class="k">my</span> <span class="v">$bar</span> = <span class="m">10</span>;
    eval <span class="s">'$bar = 5'</span>;
    <span class="k">return</span> eval <span class="s">'$bar'</span>;
}
</code></pre>

<p>This function returns <code>5</code>. The <code>eval</code> modified the lexically scoped
<code>$bar</code>.</p>

<p>Note how short Lisp’s <code>eval</code> documentation is compared to
Perl’s. Lisp’s <code>eval</code> semantics are dead simple — very important for
such a dangerous function. Perl’s description is two orders of
magnitude larger than Lisp’s and it still doesn’t fully document the
feature.</p>

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

<p>JavaScript goes much further than all of this. Not only is <code>eval</code> done
in the current lexical environment but <strong>it can introduce entirely new
bindings!</strong></p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>() {
    eval(<span class="s">'var bar = 10'</span>);
    <span class="k">return</span> bar;
}
</code></pre>

<p>This function returns 10. <code>eval</code> created a new lexical variable in
<code>foo</code> <em>at run time</em>. Because the environment can be manipulated so
drastically at run time, any hopes of effectively compiling <code>foo</code> are
thrown out the window. To have an outside function modify the local
environment is a severe side-effect. It essentially requires that
JavaScript be interpreted rather than compiled. Along with the <code>with</code>
statement, it’s strong evidence that JavaScript was at some point
designed by novices.</p>

<p><code>eval</code> also makes closures a lot heavier. Normally the compiler can
determine at compile time which variables are being accessed by a
function and minimize the environment captured by a closure. For
example,</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>(x) {
    <span class="k">var</span> y = {x: x};
    <span class="k">return</span> <span class="k">function</span>() {
        <span class="k">return</span> x * x;
    };
}
</code></pre>

<p>The function <code>foo</code> returns a closure capturing the bindings <code>x</code> and
<code>y</code>. The compiler can prove that <code>y</code> is never accessed by the closure
and omit it, <a href="http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/">freeing the object</a> bound to <code>y</code> for garbage
collection. However, if <code>eval</code> is present, <em>anything</em> could be
accessed at any time and the compiler can prove nothing. For example,</p>

<pre class="highlight"><code><span class="k">function</span> <span class="f">foo</span>(x) {
    <span class="k">return</span> <span class="k">function</span>() {
        <span class="k">return</span> eval(<span class="s">'x * x'</span>);
    };
}
</code></pre>

<p>The variable <code>x</code> is never accessed lexically, but the <code>eval</code> can tease
it out at run time. The expression <code>foo(3)()</code> will evaluate to 9,
showing that anything exposed to the closure is not free to be garbage
collected as long as the closure is accessible.</p>

<p>If that’s where the story ended, JavaScript optimization would look
pretty bleak. <em>Any</em> function call could be a call to <code>eval</code> and so any
time we call another function it may stomp all over the local
environment, preventing the compiler from proving anything useful. For
example,</p>

<pre class="highlight"><code><span class="k">var</span> secretEval = eval;
<span class="k">function</span> <span class="f">foo</span>(string) {
    <span class="c">// ...</span>
    secretEval(string);
    <span class="c">// ...</span>
}
</code></pre>

<p>There’s good news and bad news. The good news is that this is <em>not</em>
the case in the above example. <code>string</code> will be evaluated in the
global environment, not the local environment. The bad news is that
this is because of a obscure, complicated concept of
<a href="http://perfectionkills.com/global-eval-what-are-the-options/">indirect and direct <code>eval</code>s</a>.</p>

<p>In general, when <code>eval</code> is called by a name other than “<code>eval</code>” it is
an <em>indirect</em> call and is performed in the global environment (see the
linked article for a more exact description). This means the compiler
can tell at compile time whether or not <code>eval</code> will be evaluating in
the lexical environment. If not, it’s free to make optimizations that
<code>eval</code> would otherwise prohibit. Whew!</p>

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

<p>To address <code>eval</code>’s problems a bit further, along with some other
problems, ECMAScript 5 introduced <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode">strict mode</a>. Strict mode
modifies JavaScript’s semantics so that it’s a more robust and
compiler-friendly language.</p>

<p>In strict mode, <code>eval</code> still uses the local environment when called
directly but it gets its own nested environment. New bindings are
created in this nested environment, which is discarded when evaluation
is complete. JavaScript’s <code>eval</code> is still quirky, but less so than
before.</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 class="highlight"><code>git clone git://github.com/netguy204/brianscheme.git
</code></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 class="highlight"><code>git remote add wellons git://github.com/skeeto/brianscheme.git
</code></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 <em>any</em> 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 class="highlight"><code>&gt; (require 'wisp)
&gt; (wisp:eval '(expt 6 56))
37711171281396032013366321198900157303750656
</code></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 <em>is</em> 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>A GNU Octave Feature</title>
    <link rel="alternate" type="text/html" href="https://nullprogram.com/blog/2008/08/29/"/>
    <id>urn:uuid:a8aec192-263a-38d3-5d0d-86824c93fd4f</id>
    <updated>2008-08-29T00:00:00Z</updated>
    <category term="octave"/><category term="rant"/><category term="lang"/>
    <content type="html">
      <![CDATA[<!-- 29 August 2008 -->

<p>At work they recently moved me to a new project. It is a Matlab-based
data analysis thing. I haven’t really touched Matlab in over a year
(the last time I used Matlab at work), and, instead, use GNU Octave at
home when the language is appropriate. I got so used to Octave that I
found a pretty critical feature missing from Matlab’s implementation:
treat an expression as if it were of the type of its output.</p>

<p>Let’s say we want to index into the result of a function. Take, for
example, the magic square function, <code>magic()</code>. This spits
out a
<a href="http://en.wikipedia.org/wiki/Magic_square">magic square</a>
of the given size. In Octave we can generate a 4x4 magic square and
chop out the middle 2x2 portion in one line.</p>

<pre class="highlight"><code>octave&gt; magic(4)(2:3,2:3)
ans =

   11   10
    7    6
</code></pre>

<p>Or more possibly clearly,</p>

<pre class="highlight"><code>octave&gt; [magic(4)](2:3,2:3)
ans =

   11   10
    7    6
</code></pre>

<p>Try this in Matlab and you will get a big, fat error. You have to
assign the magic square to a temporary variable to do the same
thing. I kept trying to do this sort of thing in Matlab and was
thinking to myself, “I <em>know</em> I can do this somehow!”. Nope, I
was just used to having Octave.</p>

<p>Where this really shows is when you want to reshape a matrix into a
nice, simple vector. If you have a matrix <code>M</code> and want to
count the number of NaN’s it has, you can’t just apply
the <code>sum()</code> function over <code>isnan()</code> because it
only does sums of columns. You can get around this with a special
index, <code>(:)</code>.</p>

<p>So, to sum all elements in <code>M</code> directly,</p>

<pre class="highlight"><code>octave&gt; sum(M(:))
</code></pre>

<p>In Octave, to count NaN’s with <code>isnan()</code>,</p>

<pre class="highlight"><code>octave&gt; sum(isnan(M)(:))
</code></pre>

<p>Again, Matlab won’t let you index the result of <code>isnan()</code>
directly. Stupid. I guess the Matlab way to do this is to
apply <code>sum()</code> twice.</p>

<p>Every language I can think of handles this properly. C, C++, Perl,
Ruby, etc. It is strange that Matlab itself doesn’t have it. Score one
more for Octave.</p>
]]>
    </content>
  </entry>

</feed>
