<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Ed Kellett</title><link href="https://kellett.im/" rel="alternate"/><id>https://kellett.im/</id><updated>2026-10-27T00:00:00+00:00</updated><entry><title>reverse fraction calculator</title><link href="https://kellett.im/a/reverse-fractions" rel="alternate"/><published>2026-10-27T00:00:00+00:00</published><updated>2026-10-27T00:00:00+00:00</updated><author><name>edk</name></author><id>tag:kellett.im,2026-10-27:/a/reverse-fractions</id><content type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;blockquote&gt;
&lt;p&gt;37.8% of people surveyed liked cake.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;How many people were surveyed?&lt;/p&gt;&lt;p&gt;In general, of course, we can’t know. But we &lt;em&gt;can&lt;/em&gt; say something about the smallest number that could possibly have been asked. If 14 out of 37 people liked cake, that would give us 37.8%. No smaller denominator will do.&lt;/p&gt;&lt;p&gt;After &lt;math&gt;&lt;mfrac&gt;&lt;mn&gt;14&lt;/mn&gt;&lt;mn&gt;37&lt;/mn&gt;&lt;/mfrac&gt;&lt;/math&gt;, the next simplest fractions that round to 37.8% are &lt;math&gt;&lt;mfrac&gt;&lt;mn&gt;17&lt;/mn&gt;&lt;mn&gt;45&lt;/mn&gt;&lt;/mfrac&gt;&lt;/math&gt; and &lt;math&gt;&lt;mfrac&gt;&lt;mn&gt;28&lt;/mn&gt;&lt;mn&gt;74&lt;/mn&gt;&lt;/mfrac&gt;&lt;/math&gt;, after which candidates start getting much easier to find, and so less interesting.&lt;/p&gt;&lt;p&gt;It would be fair to say that reasoning in this way is not often useful, at least on its own. Sometimes the minimum is large enough to be interesting. Really, though, I just think we owe it to the overly precise number reporters of this world to learn what we can from their output.&lt;/p&gt;&lt;p&gt;So, without further ado, here’s a little percentage-to-simplest-fraction calculator:&lt;/p&gt;&lt;p style="text-align:start"&gt;&lt;input id="percentage" min="0" step="any" style="width:6em" type="number" value="37.8"/&gt; % might be &lt;math id="fractions" style="math-style: normal"&gt;&lt;/math&gt;&lt;/p&gt;&lt;p&gt;It shows at least three fractions, then stops when the denominators get within 10% of each other. These criteria aren’t perfect, but it’s surprisingly hard to capture the notion of &lt;em&gt;interestingness&lt;/em&gt; rigorously.&lt;/p&gt;&lt;h2 id="sternbrocot"&gt;Stern–Brocot&lt;a class="headerlink" href="#sternbrocot" title="Permanent link"&gt;¶&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;It turns out to be rather easy to find simple fractions in an interval by searching the &lt;a href="https://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree"&gt;&lt;em&gt;Stern–Brocot tree&lt;/em&gt;&lt;/a&gt;. This tree contains all positive rational numbers somewhere within it, and the path to each is made up of the &lt;a href="https://en.wikipedia.org/wiki/Best_rational_approximation"&gt;best rational approximations&lt;/a&gt; to it. A binary search that explores an interval looks much like the usual kind, except that once the interval is found it may take both branches.&lt;/p&gt;&lt;p&gt;While searching for a node yields decreasingly simple&lt;sup id="fnref:antonym"&gt;&lt;a class="footnote-ref" href="#fn:antonym"&gt;1&lt;/a&gt;&lt;/sup&gt; fractions, exploring an interval does not. A priority queue would be a great tool to handle this. Unfortunately JavaScript doesn’t have one and I was too lazy to write one, so I just sort the queue of nodes to visit between iterations.&lt;/p&gt;&lt;p&gt;The tree exploration yields fractions in lowest terms. That is usually more useful, but I think undesirable here. I am rather crudely generating them between rational approximations and… sorting the results.&lt;/p&gt;&lt;p&gt;Perhaps the lesson of this post is that I really wish JS included batteries.&lt;/p&gt;&lt;p&gt;As usual, you’re more than welcome to get in touch if you have questions.&lt;script src="https://kellett.im/static/percent.js"&gt;&lt;/script&gt;&lt;script&gt;
function me(name) {
  return document.createElementNS("http://www.w3.org/1998/Math/MathML", name);
}
function calc() {
  let [m, n] = parse(document.getElementById("percentage").value);
  let results = search(m, n);
  let fractions = document.getElementById("fractions");
  fractions.textContent = "";
  for (let i = 0; i &lt; results.length; i++) {
    let q = results[i];
    let mfrac = me("mfrac");
    fractions.appendChild(mfrac);
    let mnn = me("mn");
    mnn.textContent = q[0].toString();
    mfrac.appendChild(mnn);
    let mnd = me("mn");
    mnd.textContent = q[1].toString();
    mfrac.appendChild(mnd);
    let mo = me("mo");
    switch (results.length - i) {
    case 1:
      mo.textContent = "…";
      fractions.appendChild(mo);
      break;
    default:
      mo.textContent = ", ";
      fractions.appendChild(mo);
      break;
    }
  }
}
document.getElementById("percentage").addEventListener("input", calc);
calc();
&lt;/script&gt;&lt;/p&gt;&lt;div class="footnote"&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id="fn:antonym"&gt;
&lt;p&gt;More complicated? If you know a good word for this, please tell me.&lt;a class="footnote-backref" href="#fnref:antonym" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</content></entry><entry><title>Shuffle All (with cryptography)</title><link href="https://kellett.im/a/pseudorandom-permutations" rel="alternate"/><published>2026-10-23T00:00:00+00:00</published><updated>2026-10-23T00:00:00+00:00</updated><author><name>edk</name></author><id>tag:kellett.im,2026-10-23:/a/pseudorandom-permutations</id><content type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;Just a quick note on a cryptography trick I learned many years ago, with thanks to #crypto on IRC.&lt;/p&gt;&lt;p&gt;If you wish to iterate a sequence of length &lt;math&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/math&gt; in a random order you can use an ordinary index in the range &lt;math&gt;&lt;mo&gt;[&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo&gt;,&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;)&lt;/mo&gt;&lt;/math&gt;, but use &lt;a href="https://en.wikipedia.org/wiki/Format-preserving_encryption"&gt;format-preserving encryption&lt;/a&gt; to transform it to another index. Since encryption is reversible with the key, and the output is the same size as the input, each index will appear exactly once in the output. Mathematicians and I would say format-preserving encryption is a &lt;em&gt;pseudorandom permutation&lt;/em&gt;: it appears random, and it reorders a sequence without losing or duplicating any entries. So the output is a reordered sequence that we don’t need to store:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;math display="block"&gt;
&lt;mtable&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;3&lt;/mtd&gt;&lt;/mtr&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;4&lt;/mtd&gt;&lt;/mtr&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;1&lt;/mtd&gt;&lt;/mtr&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;0&lt;/mtd&gt;&lt;/mtr&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;2&lt;/mtd&gt;&lt;/mtr&gt;
&lt;/mtable&gt;
&lt;/math&gt;&lt;/p&gt;&lt;p&gt;It gets neater. Note that we don’t even need to keep the original, in-order index anywhere; we can just decrypt the index of the current element, add or subtract something to move in either direction, and encrypt again.&lt;sup id="fnref:conj"&gt;&lt;a class="footnote-ref" href="#fn:conj"&gt;1&lt;/a&gt;&lt;/sup&gt; This also means we could jump to any point in the shuffled sequence and still easily know what's next. For the permutation above, the “next” function is:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;math display="block"&gt;
&lt;mtable&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;2&lt;/mtd&gt;&lt;/mtr&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;0&lt;/mtd&gt;&lt;/mtr&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;3&lt;/mtd&gt;&lt;/mtr&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;4&lt;/mtd&gt;&lt;/mtr&gt;
&lt;mtr&gt;&lt;mtd&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mo&gt;⟶&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd&gt;1&lt;/mtd&gt;&lt;/mtr&gt;
&lt;/mtable&gt;
&lt;/math&gt;&lt;/p&gt;&lt;p&gt;You could therefore use a format-preserving encryption primitive to implement a perfect playlist shuffle that is entirely stateless.&lt;sup id="fnref:state"&gt;&lt;a class="footnote-ref" href="#fn:state"&gt;2&lt;/a&gt;&lt;/sup&gt; Given the complexity of some media players I’ve seen, I wonder if this might be a simplification.&lt;/p&gt;&lt;div class="footnote"&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id="fn:conj"&gt;
&lt;p&gt;More formally: The successor function &lt;math&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/math&gt; is a cyclic permutation in &lt;math&gt;&lt;mo&gt;(&lt;/mo&gt;&lt;mi&gt;ℤ&lt;/mi&gt;&lt;mo lspace="0" rspace="0"&gt;∕&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;ℤ&lt;/mi&gt;&lt;/mrow&gt;&lt;mo&gt;)&lt;/mo&gt;&lt;/math&gt;. We are taking the conjugate permutation of &lt;math&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/math&gt; by &lt;math&gt;&lt;msub&gt;&lt;mi&gt;E&lt;/mi&gt; &lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;, &lt;math&gt;&lt;msub&gt;&lt;mi&gt;E&lt;/mi&gt; &lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;E&lt;/mi&gt; &lt;mi&gt;k&lt;/mi&gt; &lt;mrow&gt;&lt;mo&gt;-&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;/math&gt;. Conjugation preserves the cycle type of &lt;math&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/math&gt;.&lt;a class="footnote-backref" href="#fnref:conj" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:state"&gt;
&lt;p&gt;Or nearly stateless if you have a way to reshuffle. Still a lot easier to manage than temporary lists that take &lt;math&gt;&lt;mi mathvariant="normal"&gt;O&lt;/mi&gt;&lt;mo&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;)&lt;/mo&gt;&lt;/math&gt; space.&lt;a class="footnote-backref" href="#fnref:state" title="Jump back to footnote 2 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</content></entry><entry><title>my new favourite kind of plot</title><link href="https://kellett.im/a/new-favourite-plot" rel="alternate"/><published>2026-10-18T00:00:00+00:00</published><updated>2026-10-18T00:00:00+00:00</updated><author><name>edk</name></author><id>tag:kellett.im,2026-10-18:/a/new-favourite-plot</id><content type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;I have a new favourite way to plot metrics that feature some sort of daily progression. It looks like this:&lt;/p&gt;&lt;p&gt;&lt;picture&gt;

&lt;source media="(prefers-color-scheme:dark)" srcset="https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/dark/1x/example-dark.png 1x, https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/dark/2x/example-dark.png 2x"/&gt;&lt;source srcset="https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/default/1x/example-light.png 1x, https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/default/2x/example-light.png 2x"/&gt;&lt;img alt="A plot titled “balcony” with a horizontal axis showing 24-hour time and a vertical axis in ºC. The temperature today is plotted in green. A semitransparent blue fill plot shows the same data from the previous day." class="image-process-exact-width-screenshot" height="346" loading="lazy" src="https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/default/1x/example-light.png" width="544"/&gt;
&lt;/picture&gt;&lt;/p&gt;&lt;p&gt;To explain why I like it so much, we must, regrettably, look at the sort of thing it’s replacing:&lt;/p&gt;&lt;p&gt;&lt;picture&gt;

&lt;source media="(prefers-color-scheme:dark)" srcset="https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/dark/1x/ye-olde-dark.png 1x, https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/dark/2x/ye-olde-dark.png 2x"/&gt;&lt;source srcset="https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/default/1x/ye-olde-light.png 1x, https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/default/2x/ye-olde-light.png 2x"/&gt;&lt;img alt="A plot showing three temperature readings over the last 24 hours in typical time-series presentation, with the present moment at the right-hand edge." class="image-process-exact-width-screenshot" height="346" loading="lazy" src="https://kellett.im/static/new-favourite-plot/derivatives/exact-width-screenshot/default/1x/ye-olde-light.png" width="544"/&gt;
&lt;/picture&gt;&lt;/p&gt;&lt;p&gt;I know, I know. My way displays less information!&lt;/p&gt;&lt;p&gt;But, and I may be the only one with this problem, I confess to finding it a little hard to process. I have no intuitive sense of the shape of the traditional plot, since it’s always changing. It also frustrates me that &lt;em&gt;this time yesterday&lt;/em&gt; is perpetually just out of sight.&lt;/p&gt;&lt;p&gt;I’m fixing the problem of changing shape by locking the time range. It is always a calendar day and today’s (incomplete) data is drawn as a line. The ghost of yesterday’s&lt;sup id="fnref:yesterday"&gt;&lt;a class="footnote-ref" href="#fn:yesterday"&gt;1&lt;/a&gt;&lt;/sup&gt; data allows for an intuitive comparison.&lt;/p&gt;&lt;p&gt;Since the current time is no longer anchored to the edge of the plot, a vertical line makes it easier to see. It sweeps across over the course of a day, and at midnight the green line disappears to become the ghost for the next day.&lt;/p&gt;&lt;p&gt;I find this plot much easier to understand intuitively—the fixed shape speaks to me in a way the sliding half-shape did not—as well as rather satisfying to look at. Unfortunately, I don’t know what it is called, which makes it tricky to evangelise it to my colleagues. Please do email me and let me know if this technique has a name.&lt;/p&gt;&lt;p&gt;PS. I have some notes about &lt;a href="https://kellett.im/ghostly-graphs"&gt;implementing this in Grafana&lt;/a&gt;.&lt;/p&gt;&lt;div class="footnote"&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id="fn:yesterday"&gt;
&lt;p&gt;Last week today is generally more useful to me at work, but I can’t show you that here. I think yesterday is more pleasing anyway.&lt;a class="footnote-backref" href="#fnref:yesterday" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</content></entry><entry><title>Inverse parentheses</title><link href="https://kellett.im/a/inverse-parentheses" rel="alternate"/><published>2025-12-20T00:00:00+00:00</published><updated>2025-12-20T00:00:00+00:00</updated><author><name>edk</name></author><id>tag:kellett.im,2025-12-20:/a/inverse-parentheses</id><content type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;Have you ever noticed that lots of programming languages let you use parentheses to group operands, but none use them to ungroup them? No? Well let’s pretend this is a normal thing to be thinking about, and see what we can do about it.&lt;/p&gt;&lt;p&gt;Grouping with parentheses is relatively easy to add to a language grammar. The rule that accepts atomic things like &lt;code&gt;37&lt;/code&gt; simply needs to also accept an opening paren,&lt;sup id="fnref:paren"&gt;&lt;a class="footnote-ref" href="#fn:paren"&gt;1&lt;/a&gt;&lt;/sup&gt; at which point it will recursively parse an entire expression, and then eat a closing paren.&lt;/p&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;parse_atom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lex&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'integer'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'('&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;expr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parse_expr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;')'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;ParseError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"missing close paren"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;expr&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;ParseError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"unexpected &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Anti-grouping isn’t quite as straightforward. Our parser can’t follow the structure of the parentheses, because then it wouldn’t be following the structure of the expression—the whole point is that these are dissimilar.&lt;/p&gt;&lt;p&gt;I don’t know if it’s possible to write a pure parser that does this. But purity is overrated anyway. I decided to take inspiration from another language with a weird grouping system.&lt;/p&gt;&lt;h2 id="python"&gt;Python&lt;a class="headerlink" href="#python" title="Permanent link"&gt;¶&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Did you know that Python’s grammar has braces? You just don’t type them. The tokeniser&lt;sup id="fnref:lexer"&gt;&lt;a class="footnote-ref" href="#fn:lexer"&gt;3&lt;/a&gt;&lt;/sup&gt; keeps track of the indentation level and inserts special tokens when it changes. The parser itself doesn’t need to worry about counting whitespace; it just sees blocks of statements bracketed by &lt;code&gt;INDENT&lt;/code&gt; and &lt;code&gt;DEDENT&lt;/code&gt;,&lt;sup id="fnref:tokname"&gt;&lt;a class="footnote-ref" href="#fn:tokname"&gt;4&lt;/a&gt;&lt;/sup&gt; which are easy to parse.&lt;/p&gt;&lt;p&gt;As it happens, Python’s tokeniser also knows when it’s inside a parenthesised expression. Indentation inside parens is not significant, and this is implemented by &lt;a href="https://github.com/python/cpython/blob/92d4aeafd5c678f781526583332a4deb7293f5f8/Parser/lexer/lexer.c#L1300-L1373"&gt;tracking the paren nesting depth&lt;/a&gt; and suppressing &lt;code&gt;INDENT&lt;/code&gt; and &lt;code&gt;DEDENT&lt;/code&gt; while it’s non-zero.&lt;/p&gt;&lt;p&gt;What if we used the same trick? Instead of trying to do all this in the parser somehow, the tokeniser could track its nesting depth, and emit a “friendliness” score for each token. Then we can simply parse operators in ascending order of friendliness.&lt;/p&gt;&lt;p&gt;In this model &lt;code&gt;1 + (2 * 3)&lt;/code&gt; will yield the following token stream:&lt;/p&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;1
+ (0)
(
2
* (-1)
3
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We’ll leave the parentheses in the token stream, but all the parser needs to do with them is generate a syntax error if it finds one in the wrong place. Grouping will be handled entirely by the precedence levels embedded in the token stream.&lt;sup id="fnref:precedence"&gt;&lt;a class="footnote-ref" href="#fn:precedence"&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;h2 id="a-not-so-infinite-climb"&gt;A not-so-infinite climb&lt;a class="headerlink" href="#a-not-so-infinite-climb" title="Permanent link"&gt;¶&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The tokeniser hack solves our parsing problem, but it creates another one: our language now has infinitely many precedence levels. I don’t feel like trying to do that with handrolled recursive descent, but a rummage through school textbooks suggests a &lt;a href="https://en.wikipedia.org/wiki/Operator-precedence_parser#Precedence_climbing_method"&gt;precedence climbing parser&lt;/a&gt; is what we need. It deals with operators in the order it meets them, so having infinitely many &lt;em&gt;possible&lt;/em&gt; precedences won’t bother it.&lt;/p&gt;&lt;p&gt;I &lt;a href="https://gist.github.com/edk0/6295d1765b661d5f25efa1a3f90dab98#file-silly-py"&gt;hacked this together&lt;/a&gt; and it’s appropriately silly:&lt;/p&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&amp;gt; (1 + 2) * 3
1 + 2 * 3
&amp;gt; 1 + (2 * 3)
(1 + 2) * 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Something I particularly enjoy about the implementation I landed on is that if you increase friendliness instead of decreasing it, you end up with an ordinary&lt;sup id="fnref:ordinary"&gt;&lt;a class="footnote-ref" href="#fn:ordinary"&gt;6&lt;/a&gt;&lt;/sup&gt; parser. It’s also a good platform for other questionable syntactic innovations, like a language with &lt;a href="https://gist.github.com/edk0/6295d1765b661d5f25efa1a3f90dab98#file-space-py"&gt;no parentheses at all, using whitespace to weaken binding&lt;/a&gt;.&lt;/p&gt;&lt;h2 id="future-work"&gt;Future work&lt;a class="headerlink" href="#future-work" title="Permanent link"&gt;¶&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;While we’ve achieved a lot here today,&lt;sup id="fnref:requiring-early-attention"&gt;&lt;a class="footnote-ref" href="#fn:requiring-early-attention"&gt;7&lt;/a&gt;&lt;/sup&gt; we’ve also raised some important new questions. For instance, is it always necessary to double-parenthesise expressions in more complex cases?&lt;/p&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&amp;gt; ((1 * 2)) + (3 * 4)
1 * ((2 + 3) * 4)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And is it possible to have an anti-grouping parser that gives an involution when hooked up to an ordinary printer?&lt;/p&gt;&lt;p&gt;These are promising avenues for deeper study, and I’d love to hear from anyone who chooses to take them on.&lt;/p&gt;&lt;div class="footnote"&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id="fn:paren"&gt;
&lt;p&gt;Does it bother anyone else that “parentheses” (meaning round brackets) doesn’t seem to have a satisfactory&lt;sup id="fnref:parenthesis"&gt;&lt;a class="footnote-ref" href="#fn:parenthesis"&gt;2&lt;/a&gt;&lt;/sup&gt; singular form?&lt;a class="footnote-backref" href="#fnref:paren" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:parenthesis"&gt;
&lt;p&gt;To me, “parenthesis” sounds like a pair of parentheses, possibly together with the text they bracket, rather than a single paren.&lt;a class="footnote-backref" href="#fnref:parenthesis" title="Jump back to footnote 2 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:lexer"&gt;
&lt;p&gt;Also called a lexer or occasionally a scanner. I’m going to stick with “tokeniser” because it feels the most like a real word.&lt;a class="footnote-backref" href="#fnref:lexer" title="Jump back to footnote 3 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:tokname"&gt;
&lt;p&gt;Sadly these are just internal names and not spellings, so you can’t use them to write exciting one-liners.&lt;a class="footnote-backref" href="#fnref:tokname" title="Jump back to footnote 4 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:precedence"&gt;
&lt;p&gt;I have ignored the &lt;a href="https://en.wikipedia.org/wiki/Order_of_operations"&gt;order of operations&lt;/a&gt; in this post to keep things simple, but my implementation does handle it. Operators are looked up in a table and their “natural” precedence is paired with their friendliness, so in fact the precedences of &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt; from the example would be (0,0) and (-1,1) respectively.&lt;a class="footnote-backref" href="#fnref:precedence" title="Jump back to footnote 5 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:ordinary"&gt;
&lt;p&gt;Outwardly.&lt;a class="footnote-backref" href="#fnref:ordinary" title="Jump back to footnote 6 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:requiring-early-attention"&gt;
&lt;p&gt;Actually early 2024, but it took me until today to get around to writing it up.&lt;a class="footnote-backref" href="#fnref:requiring-early-attention" title="Jump back to footnote 7 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</content></entry><entry><title>Keeping secrets, or (less than two weeks ago)</title><link href="https://kellett.im/a/keeping-secrets" rel="alternate"/><published>2025-12-17T00:00:00+00:00</published><updated>2025-12-17T00:00:00+00:00</updated><author><name>edk</name></author><id>tag:kellett.im,2025-12-17:/a/keeping-secrets</id><content type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;If you’ve used IRC in the last few years there’s a good chance you have seen this before:&lt;/p&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;- Last seen  : (about 28 weeks ago)
- User seen  : (less than two weeks ago)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;What does “about” mean here, exactly? Why less than two?&lt;/p&gt;&lt;p&gt;This is, of course, a privacy feature, designed so that a third party can tell when someone was around, but &lt;em&gt;only approximately&lt;/em&gt;; they should never learn the exact moment someone signed off.&lt;sup id="fnref:set-private"&gt;&lt;a class="footnote-ref" href="#fn:set-private"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;p&gt;One might imagine, then, that “about 28 weeks ago” is an exact time duration, rounded to the nearest whole number of weeks. But although seeing the rounded number &lt;em&gt;once&lt;/em&gt; gives you only approximate information, you could defeat rounding by querying repeatedly until you saw the number tick over.&lt;/p&gt;&lt;p&gt;The solution is to reduce the precision of the last-seen time itself, rather than that of the time duration. Since any given time will only ever round one way, doing it multiple times can’t possibly reveal any extra information.&lt;/p&gt;&lt;p&gt;The way this is done on Libera means all obfuscated last-seen times increment together. &lt;a href="https://en.wikipedia.org/wiki/Unix_time"&gt;Time started on a Thursday&lt;/a&gt; and if you &lt;code&gt;NS INFO&lt;/code&gt; an unused account you will indeed find that it gets one week more unused every Thursday.&lt;sup id="fnref:timezone"&gt;&lt;a class="footnote-ref" href="#fn:timezone"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;p&gt;But why “less than two”? What’s wrong with “one”?&lt;/p&gt;&lt;p&gt;Actually, one is fine. But if you could see one, you would also sometimes see zero—meaning someone was last seen in a week that hasn’t finished happening yet. That is fairly precise information near the beginning of the week!&lt;/p&gt;&lt;p&gt;So the system has to display zero and one the same way. It seemed odd to describe someone who just left as being seen “about a week ago”, so “less than two” it had to be.&lt;/p&gt;&lt;div class="footnote"&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id="fn:set-private"&gt;
&lt;p&gt;You can turn off the obfuscation for your own account. freenode defaulted the setting to off and Libera turned it on; I’m still not sure if that was the right decision, but it’s done now.&lt;a class="footnote-backref" href="#fnref:set-private" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:timezone"&gt;
&lt;p&gt;UTC, naturally.&lt;a class="footnote-backref" href="#fnref:timezone" title="Jump back to footnote 2 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</content></entry><entry><title>Freedom zero cannot survive a world with attestation</title><link href="https://kellett.im/a/attestation" rel="alternate"/><published>2025-12-15T12:00:00+00:00</published><updated>2025-12-15T12:00:00+00:00</updated><author><name>edk</name></author><id>tag:kellett.im,2025-12-15:/a/attestation</id><content type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;I can’t run &lt;a href="https://tfl.gov.uk/maps_/tfl-go"&gt;TfL Go&lt;/a&gt;, London’s public transport app, because my phone does not pass Google’s attestation. This is very annoying, but at least it’s only public transport. Most banks in the UK are less strict, perhaps complaining that their checks don’t work, but ultimately letting you in under protest.&lt;sup id="fnref:bank"&gt;&lt;a class="footnote-ref" href="#fn:bank"&gt;1&lt;/a&gt;&lt;/sup&gt; Not everyone is this lucky.&lt;/p&gt;&lt;p&gt;Apart from annoying me personally, this raises a question for those of us who care about software freedom: Are we trying to make it possible for people to use free software as a hobby, or are we trying to make depending on free software a viable way to live? Is it enough that someone can install a third-party OS on a spare phone that doesn’t do anything important, or do they need to be able to run whatever software they want to on their main (perhaps only) device without being locked out of chunks of society?&lt;/p&gt;&lt;p&gt;If it’s the former, I have good news: we won! You could download a bunch of free software today and sail to an uninhabited island and use a new free program every day for the rest of your life. Isn’t winning great?&lt;/p&gt;&lt;p&gt;If that doesn’t sound satisfying to you that’s probably because it’s missing the point completely. Software is now integral to our lives. The ability to exercise some control over that software is vital, both for protecting your privacy and security and for more down-to-earth reasons like squeezing a few more years out of obsolete devices.&lt;/p&gt;&lt;p&gt;So I find the software-oriented approach somewhat limiting. We need the ability to run free software in a lab environment, but equally importantly we need to be able to do things with it.&lt;/p&gt;&lt;p&gt;Here’s the problem: to have free software mediate your access to your own digital existence, it is all but inevitable that software will have to lie about its own nature. From banks to tube maps to instant messengers, all kinds of services have all kinds of incentives to restrict your software choices.&lt;sup id="fnref:nonfree"&gt;&lt;a class="footnote-ref" href="#fn:nonfree"&gt;2&lt;/a&gt;&lt;/sup&gt; The only realistic way around this is for third-party clients to impersonate official ones.&lt;sup id="fnref:unrealistic"&gt;&lt;a class="footnote-ref" href="#fn:unrealistic"&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;p&gt;A strong attestation system makes it impossible for third-party clients to impersonate official ones.&lt;/p&gt;&lt;p&gt;If Mullvad can &lt;a href="https://mullvad.net/en/blog/2019/6/3/system-transparency-future"&gt;prove to me&lt;/a&gt; that they’re running the server stack they say they are, great, but that implies you can prove to your bank that you’re running the client stack you say you are. I can’t prove that to my bank, so I’m living on borrowed time.&lt;/p&gt;&lt;p&gt;If you accept “doing online banking” and “checking train times” as purposes, attestation is an existential threat to software freedom. It has already divided phones into locked-down capitalism delivery vehicles and useless toys. Please stop trying to bring it to PCs.&lt;/p&gt;&lt;div class="footnote"&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id="fn:bank"&gt;
&lt;p&gt;According to a friend in the industry, this is because UK banks are required by their licence to let you access your money. But I’ve also heard of banks blocking rooted phones, so I’m not sure exactly what the deal is.&lt;a class="footnote-backref" href="#fnref:bank" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:nonfree"&gt;
&lt;p&gt;And yes, those services are themselves not free software, and I don’t love that. But one problem at a time. It’s easier to replace non-free services when people aren’t locked in by non-free clients.&lt;a class="footnote-backref" href="#fnref:nonfree" title="Jump back to footnote 2 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:unrealistic"&gt;
&lt;p&gt;One could put all one’s eggs in the regulatory basket, I guess, but I don’t think this is a problem regulators are motivated to solve.&lt;a class="footnote-backref" href="#fnref:unrealistic" title="Jump back to footnote 3 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</content></entry><entry><title>On openness</title><link href="https://kellett.im/a/blarg" rel="alternate"/><published>2019-04-27T15:19:00+00:00</published><updated>2019-04-27T15:19:00+00:00</updated><author><name>edk</name></author><id>tag:kellett.im,2019-04-27:/a/blarg</id><content type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;For some background, I'm heavily involved as a volunteer for an IRC network.&lt;/p&gt;&lt;p&gt;The previous evening, I learned (via IRC, naturally) that Mozilla intends to
shut down its IRC network. With hindsight, I don't think this should have been
surprising, but it was, and it precipitated a rather funereal line of thought
I'd like to share.&lt;/p&gt;&lt;p&gt;I was struck by two phrases in particular from the &lt;a href="http://exple.tive.org/blarg/2019/04/26/synchronous-text/"&gt;blog&lt;/a&gt; announcing
this news:&lt;/p&gt;&lt;blockquote&gt;
&lt;p&gt;"I wasn't in the room when IRC.mozilla.org was stood up, [...]&lt;/p&gt;
&lt;p&gt;"Like a lot of the early internet IRC is [...] built with far more of the
  optimism of the time than the paranoia the infosec community now refers to
  as 'common sense', [...]&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;It's not the first time, but hearing IRC criticized for being too optimistic has
lost none of its bite. The Internet was a place of possibilities then—the only
genuine frontier any living human has ever known.&lt;/p&gt;&lt;p&gt;IRC's reflection of that didn't survive the subsequent growth unchanged, losing,
for example, open federation relatively early on. But the foundation of openness
never left. You can connect to any of the significant IRC networks right now
with no account. Channels for a limitlessly diverse cross-section of projects
are listed side by side, and wandering between them is encouraged.&lt;/p&gt;&lt;p&gt;To Mozilla, one presumes, this openness is equatable only with spambots
and harassment.&lt;/p&gt;&lt;p&gt;To me, it remains a mirror to the optimistic Internet of yesterday, and to a
FOSS culture of free and spontaneous collaboration, not of gated communities,
polished web UIs, and planning meetings in which candidate "products" to replace
an organic medium of cultural exchange are "stood up".&lt;/p&gt;&lt;p&gt;I've spent considerable energy over the course of my life arguing with people
who fear change of various kinds. I'd still disagree with most of them, but for
once I've caught a glimpse of the world through their eyes—the horizons of the
natural world, with its openness, its warrantless optimism, its idiosyncracies
and its sheer beauty, steadily shrinking, decommissioned piece by piece by
community managers with their modern, proven services—and I'm terrified.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</content></entry></feed>