<?xml version="1.0"?>
<rss version="2.0">

<channel>
	<title>Planet Haskell</title>
	<link>http://planet.haskell.org/</link>
	<language>en</language>
	<description>Planet Haskell - http://planet.haskell.org/</description>

<item>
	<title>Well-Typed.Com: Well-Typed at MuniHac 2026</title>
	<guid isPermaLink="false">http://www.well-typed.com/blog/2026/11/munihac-2026</guid>
	<link>https://well-typed.com/blog/2026/11/munihac-2026</link>
	<description>&lt;p&gt;Well-Typed was strongly represented at this year’s &lt;a href=&quot;https://munihac.de/2026.html&quot;&gt;MuniHac&lt;/a&gt;, with our team of Haskell experts
giving one of the keynote presentations as well as joining the Hackathon to collaborate on various projects while socialising with fellow Haskellers.
If you couldn’t make it to MuniHac but are interested in some of the keynotes, the &lt;a href=&quot;https://www.youtube.com/playlist?list=PLfjdiKozwPos&quot;&gt;recordings are available&lt;/a&gt;!&lt;/p&gt;
&lt;h3 id=&quot;compiling-c-to-haskell&quot;&gt;Compiling C to Haskell&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://well-typed.com/people/dominik&quot;&gt;Dominik&lt;/a&gt; was one of the keynote speakers of this year’s MuniHac.
He presented Well-Typed’s work on the Haskell library &lt;a href=&quot;https://github.com/well-typed/hs-bindgen&quot;&gt;&lt;code&gt;hs-bindgen&lt;/code&gt;&lt;/a&gt;,
which automatically generates Haskell FFI bindings from C header files.
His talk explores the vision and architecture of &lt;code&gt;hs-bindgen&lt;/code&gt; and provides in-depth discussion of the challenges of reconciling C’s low-level memory model with Haskell’s rich type system.&lt;/p&gt;
&lt;p&gt;If that piques your interest, you should watch the talk or read our &lt;a href=&quot;https://well-typed.com/blog/tags/hs-bindgen&quot;&gt;blog posts about &lt;code&gt;hs-bindgen&lt;/code&gt;&lt;/a&gt;!
Additionally, you can immediately experiment with generating Haskell bindings in the &lt;a href=&quot;https://bindgen.well-typed.com/&quot;&gt;&lt;code&gt;hs-bindgen&lt;/code&gt; playground&lt;/a&gt;.&lt;/p&gt;

&lt;div style=&quot;width: 560px; height: 315px;&quot;&gt;

&lt;/div&gt;
&lt;h3 id=&quot;the-hackathon&quot;&gt;The Hackathon&lt;/h3&gt;
&lt;p&gt;Naturally, our team joined the Hackathon and collaborated on various ideas, concepts and prototypes together with other developers, passionate about Haskell.
Over the course of the weekend, we contributed to the Haskell Language Server project by fixing bugs and answering questions about the codebase,
tinkered with a new JIT backend idea for GHC, and worked on improving the ergonomics of &lt;code&gt;ImpredicativeTypes&lt;/code&gt; with &lt;code&gt;TemplateHaskell&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Besides the technical work, the social aspects are always a highlight at MuniHac, both during the Hackathon and afterwards at dinner.
This year, some of us even went bouldering to relax after an exciting day of hacking.
It was great to meet everyone who attended. We hope to see you all again next year!&lt;/p&gt;</description>
	<pubDate>Fri, 27 Nov 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Well-Typed.Com: Exception Best Practices</title>
	<guid isPermaLink="false">http://www.well-typed.com/blog/2026/11/exception-best-practices</guid>
	<link>https://well-typed.com/blog/2026/11/exception-best-practices</link>
	<description>&lt;p&gt;Good exception handling is difficult in any language, but all the more so in
Haskell, where asynchronous exceptions mean that code can be interrupted at
almost any point. In this blog post we will discuss a series of recommendations,
focusing on three goals:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;Ensure that allocated resources will be deallocated (avoid leaks)&lt;/li&gt;
&lt;li&gt;Ensure that code can always make progress (avoid getting stuck)&lt;/li&gt;
&lt;li&gt;Ensure that failures reach the code that can deal with them (don’t catch what
you can’t handle)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As we will see, satisfying all three of these goals together can be tricky, and
sometimes they are in direct tension.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is part one of our series on exceptions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Part 1: Exception best practices (this blog post)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Part 2: “When an &lt;code&gt;interruptible&lt;/code&gt; call isn’t interruptible”, which will be
published soon, discusses interruptible foreign calls: how they are meant to
work, why they do not always work, and how to address that.&lt;/p&gt;
&lt;p&gt;Part 3: “(Some) Exceptions in Haskell are resumable”, also to be published
soon, discusses asynchronous exceptions in pure code, resulting in resumable
exceptions, and how we might generalize that to IO code.&lt;/p&gt;
&lt;p&gt;We’re also still planning two further parts: one on composing masking
operations, and one exploring how the problems discussed in part 3 can be
resolved.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;baseline&quot;&gt;Baseline&lt;/h2&gt;
&lt;p&gt;In this first section we recap the most important concepts that we will need
to understand before we can discuss best practices.&lt;/p&gt;
&lt;h3 id=&quot;throwing-and-catching-exceptions&quot;&gt;Throwing and catching exceptions&lt;/h3&gt;
&lt;p&gt;An exception is thrown with &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:throwIO&quot;&gt;&lt;code&gt;throwIO&lt;/code&gt;&lt;/a&gt; and caught with
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:catch&quot;&gt;&lt;code&gt;catch&lt;/code&gt;&lt;/a&gt;; in both cases the type of the value that is thrown must be
an instance of the &lt;code&gt;Exception&lt;/code&gt; class:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MyException&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MyException&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; anyclass (&lt;span class=&quot;dt&quot;&gt;Exception&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;baseline ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb1-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;baseline &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; action &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; handler&lt;/span&gt;
&lt;span id=&quot;cb1-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    action ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb1-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    action &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; throwIO &lt;span class=&quot;dt&quot;&gt;MyException&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    handler ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MyException&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb1-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    handler &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;print&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It is often more convenient to use &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:handle&quot;&gt;&lt;code&gt;handle&lt;/code&gt;&lt;/a&gt;, which is just
&lt;code&gt;catch&lt;/code&gt; with its arguments flipped:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;baseline ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;baseline &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; handle handler &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; action&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Before &lt;code&gt;throwIO&lt;/code&gt; can throw the exception, it must first wrap it in the
&lt;code&gt;SomeException&lt;/code&gt; envelope, which carries dynamic typing information and
&lt;a href=&quot;https://well-typed.com/blog/2026/05/lay-annotation-land/&quot;&gt;exception annotations&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; e&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt; e&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This dynamic typing information is used by &lt;code&gt;catch&lt;/code&gt; to ensure that only
exceptions of the desired type are caught (that is, determined by the type of
the exception handler).&lt;/p&gt;
&lt;h3 id=&quot;asynchronous-exceptions&quot;&gt;Asynchronous exceptions&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;Synchronous&lt;/em&gt; exceptions are exceptions that are raised as a direct consequence
of executing the program itself. Haskell also supports &lt;em&gt;asynchronous&lt;/em&gt;
exceptions, which instead can occur at any point during execution&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;. Consider the following example:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;asyncExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;asyncExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; action &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; handler&lt;/span&gt;
&lt;span id=&quot;cb4-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    action ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb4-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    action &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; threadDelay &lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt;_000_000&lt;/span&gt;
&lt;span id=&quot;cb4-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    handler ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MyException&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb4-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    handler &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;print&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It might seem that the handler should never run, but that is not necessarily
the case:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;main ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb5-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;main &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    me &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; myThreadId&lt;/span&gt;
&lt;span id=&quot;cb5-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _  &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; forkIO &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      threadDelay &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;_000_000&lt;/span&gt;
&lt;span id=&quot;cb5-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      throwTo me &lt;span class=&quot;dt&quot;&gt;MyException&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    asyncExample&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If &lt;code&gt;throwIO&lt;/code&gt; is the prototypical way to throw a synchronous exception,
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:throwTo&quot;&gt;&lt;code&gt;throwTo&lt;/code&gt;&lt;/a&gt; is its asynchronous sibling, and throws an exception
to an arbitrary thread, although in practice it is rarely called directly;
instead, it is a building block for other abstractions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/System-Timeout.html#v:timeout&quot;&gt;&lt;code&gt;timeout&lt;/code&gt;&lt;/a&gt; throws a
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/System-Timeout.html#t:Timeout&quot;&gt;&lt;code&gt;Timeout&lt;/code&gt;&lt;/a&gt; exception to the thread when the time is
up&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Concurrent.html#v:killThread&quot;&gt;&lt;code&gt;killThread&lt;/code&gt;&lt;/a&gt; throws &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:AsyncException&quot;&gt;&lt;code&gt;ThreadKilled&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Pressing CTRL-C results in a &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:AsyncException&quot;&gt;&lt;code&gt;UserInterrupt&lt;/code&gt;&lt;/a&gt; exception
in the main thread&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;async&lt;/code&gt; library uses &lt;code&gt;throwTo&lt;/code&gt; to cancel threads whose result is no longer
needed (for example, the losing thread in a &lt;a href=&quot;https://hackage-content.haskell.org/package/async-2.2.6/docs/Control-Concurrent-Async.html#v:race&quot;&gt;&lt;code&gt;race&lt;/code&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Without asynchronous exceptions, none of this would be possible in general: the
code being cancelled would have to check, at regular intervals, whether it
should stop.&lt;/p&gt;
&lt;h3 id=&quot;async-exception-safety&quot;&gt;Async exception safety&lt;/h3&gt;
&lt;p&gt;Consider this code that attempts to read the first line of a file; it tries to
make sure that the file handle is closed again if an exception is raised during
reading:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;readFirstLineBROKEN ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;readFirstLineBROKEN &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    h &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; openFile &lt;span class=&quot;st&quot;&gt;&quot;example.txt&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ReadMode&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;co&quot;&gt;{- what if an exception is raised here? -}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    action h &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; handler h&lt;/span&gt;
&lt;span id=&quot;cb6-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    action ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Handle&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    action h &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        line &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; hGetLine h&lt;/span&gt;
&lt;span id=&quot;cb6-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        hClose h&lt;/span&gt;
&lt;span id=&quot;cb6-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; line&lt;/span&gt;
&lt;span id=&quot;cb6-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    handler ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Handle&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb6-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    handler h e &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        hClose h&lt;/span&gt;
&lt;span id=&quot;cb6-16&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        throwIO e&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The problem in this code is &lt;em&gt;in between the lines&lt;/em&gt;; specifically, after the
&lt;code&gt;openFile&lt;/code&gt; and before the &lt;code&gt;catch&lt;/code&gt;. The solution is to temporarily prevent
asynchronous exceptions from being raised using &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:mask&quot;&gt;&lt;code&gt;mask&lt;/code&gt;&lt;/a&gt;, and only
enable them again once we are safely in the scope of an exception handler:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;readFirstLineLOWLEVEL ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;readFirstLineLOWLEVEL &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    mask &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \unmask &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      h &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; openFile &lt;span class=&quot;st&quot;&gt;&quot;example.txt&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ReadMode&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      unmask (action h) &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; handler h&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;catching-exceptions&quot;&gt;Catching exceptions&lt;/h2&gt;
&lt;h3 id=&quot;bracket&quot;&gt;Bracket&lt;/h3&gt;
&lt;p&gt;The pattern in &lt;code&gt;readFirstLineLOWLEVEL&lt;/code&gt; is so common in Haskell that it has its
own combinator, called &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:bracket&quot;&gt;&lt;code&gt;bracket&lt;/code&gt;&lt;/a&gt;; it takes three arguments,
one to acquire a resource, one to release it again, and then the body that
uses the resource. This means that the recommended, and idiomatic, way to
write &lt;code&gt;readFirstLine&lt;/code&gt; is&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;readFirstLine ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;readFirstLine &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    bracket (openFile &lt;span class=&quot;st&quot;&gt;&quot;example.txt&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ReadMode&lt;/span&gt;) hClose &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \h &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      hGetLine h&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One downside of this idiom is that the exception handler no longer gets access
to the exception. For example, if we wanted to log any exceptions that occurred
while we were reading the file, along with the position in the file when the
exception was thrown, we would not be able to do so with the standard &lt;code&gt;bracket&lt;/code&gt;.
It is of course always possible to drop back down to the low-level &lt;code&gt;mask&lt;/code&gt;
combinator, but in most cases it is probably better to instead use
&lt;a href=&quot;https://hackage-content.haskell.org/package/exceptions-0.10.12/docs/Control-Monad-Catch.html#v:generalBracket&quot;&gt;&lt;code&gt;generalBracket&lt;/code&gt;&lt;/a&gt; from the
&lt;a href=&quot;https://hackage.haskell.org/package/exceptions&quot;&gt;exceptions&lt;/a&gt; package. When using &lt;code&gt;generalBracket&lt;/code&gt;, the
handler is told how the body terminated, and can also produce a result of its
own (returned only on successful termination&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn2&quot; id=&quot;fnref2&quot;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;readFirstLineLOGGING ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;readFirstLineLOGGING &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fst&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    Exceptions.generalBracket (openFile &lt;span class=&quot;st&quot;&gt;&quot;example.txt&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ReadMode&lt;/span&gt;) handler &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \h &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      hGetLine h&lt;/span&gt;
&lt;span id=&quot;cb9-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    handler ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Handle&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Exceptions.ExitCase&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb9-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    handler h exitCase &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; exitCase &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;dt&quot;&gt;Exceptions.ExitCaseException&lt;/span&gt; e &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            pos &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; hTell h&lt;/span&gt;
&lt;span id=&quot;cb9-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Exception at position &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; pos&lt;/span&gt;
&lt;span id=&quot;cb9-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                    &lt;span class=&quot;op&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;: &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;++&lt;/span&gt; displayException e&lt;/span&gt;
&lt;span id=&quot;cb9-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          _otherwise &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb9-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        hClose h&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(The reason for the &lt;code&gt;fmap&lt;/code&gt; is that &lt;code&gt;generalBracket&lt;/code&gt; allows the deallocation
callback to return an additional value; here we just ignore it.)&lt;/p&gt;
&lt;h3 id=&quot;catching-all-exceptions&quot;&gt;Catching “all” exceptions&lt;/h3&gt;
&lt;p&gt;Consider this version of &lt;code&gt;readFirstLine&lt;/code&gt; that tries again when an exception
happens; perhaps the file we’re trying to read might be locked by another
process, and we just want to keep trying until we succeed:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;keepTryingBAD ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;keepTryingBAD &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    mLine &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; readFirstLine) &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; handler&lt;/span&gt;
&lt;span id=&quot;cb10-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; mLine &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; line &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; line&lt;/span&gt;
&lt;span id=&quot;cb10-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;   &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; keepTryingBAD&lt;/span&gt;
&lt;span id=&quot;cb10-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    handler ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb10-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    handler e &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Got &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;++&lt;/span&gt; displayException e &lt;span class=&quot;op&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; while reading. Trying again&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        threadDelay &lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt;_000&lt;/span&gt;
&lt;span id=&quot;cb10-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Code like this with a blanket exception handler that catches &lt;em&gt;all&lt;/em&gt; exceptions
(by catching exceptions of type &lt;code&gt;SomeException&lt;/code&gt;) is problematic. The obvious
problem is that while some synchronous exceptions might be transient (“file
locked”), others might not (“invalid path”); you probably don’t want to retry on
the latter. The less obvious problem is that you almost certainly don’t want to
retry on asynchronous exceptions at all: exceptions such as a
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/System-Timeout.html#t:Timeout&quot;&gt;Timeout&lt;/a&gt; or &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:AsyncException&quot;&gt;&lt;code&gt;UserInterrupt&lt;/code&gt;&lt;/a&gt;
(CTRL-C) should almost certainly be honoured.&lt;/p&gt;
&lt;p&gt;My advice therefore is as follows:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;In general, do not catch all exceptions (&lt;code&gt;SomeException&lt;/code&gt;) unless you are
doing so for the purposes of resource reclamation, and you will rethrow the
exception after (in which case you’d typically use &lt;code&gt;bracket&lt;/code&gt; and friends).&lt;/li&gt;
&lt;li&gt;Prefer to catch &lt;em&gt;specific&lt;/em&gt; exceptions; that is, exceptions of a specific
type (see &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:catches&quot;&gt;&lt;code&gt;catches&lt;/code&gt;&lt;/a&gt; to catch exceptions of multiple types),
or preferably even specific &lt;em&gt;values&lt;/em&gt; (see &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:catchJust&quot;&gt;&lt;code&gt;catchJust&lt;/code&gt;&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;If you &lt;em&gt;must&lt;/em&gt; catch exceptions of unknown type, carefully consider whether
you want to catch asynchronous exceptions also.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;avoid-unbounded-handler-nesting&quot;&gt;Avoid unbounded handler nesting&lt;/h3&gt;
&lt;p&gt;Consider this variation of &lt;code&gt;keepTrying&lt;/code&gt;; unlike in the previous section, we’re
now specifically only re-trying if the file is
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/System-IO-Error.html#v:isAlreadyInUseError&quot;&gt;locked&lt;/a&gt;, but we have a different error:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;keepTryingALSOBAD ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;keepTryingALSOBAD &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    readFirstLine &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; \e &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; isAlreadyInUseError e &lt;span class=&quot;kw&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Got &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;++&lt;/span&gt; displayException e &lt;span class=&quot;op&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; while reading. Trying again&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        threadDelay &lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt;_000&lt;/span&gt;
&lt;span id=&quot;cb11-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        keepTryingALSOBAD&lt;/span&gt;
&lt;span id=&quot;cb11-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;else&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        throwIO e&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The problem with this version is that since the recursion happens &lt;em&gt;inside of&lt;/em&gt;
the exception handler, we might end up with an unbounded number of handlers,
each next recursive call running under all of the exception handlers installed
so far. In the previous section &lt;code&gt;keepTryingBAD&lt;/code&gt; did get this aspect right:
return a value that indicates whether or not we should retry, so that we can
retry outside the scope of the exception handler.&lt;/p&gt;
&lt;h3 id=&quot;distinguishing-sync-and-async-exceptions&quot;&gt;Distinguishing sync and async exceptions&lt;/h3&gt;
&lt;p&gt;Consider the following code, which spawns an auxiliary thread which catches all
exceptions, and then shows that exception using
&lt;a href=&quot;https://hackage-content.haskell.org/package/recover-rtti-0.6.1/docs/Debug-RecoverRTTI.html#v:anythingToString&quot;&gt;&lt;code&gt;anythingToString&lt;/code&gt;&lt;/a&gt; from my
&lt;a href=&quot;https://hackage-content.haskell.org/package/recover-rtti&quot;&gt;&lt;code&gt;recover-rtti&lt;/code&gt;&lt;/a&gt; package, which bypasses any kind of
&lt;code&gt;Show&lt;/code&gt; instance (indeed, any type class instance at all) and shows the exact
structure of the exception:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;main ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb12-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;main &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    otherThread &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; forkIO &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      threadDelay &lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt;_000_000 &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; \(&lt;span class=&quot;ot&quot;&gt;e ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; RTTI.anythingToString e&lt;/span&gt;
&lt;span id=&quot;cb12-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    threadDelay &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;_000_000&lt;/span&gt;
&lt;span id=&quot;cb12-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    killThread otherThread&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When we run this, we see (slightly cleaned up for readability, and some details
omitted):&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;Exception&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TrTyCon&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;ExceptionContext&lt;/span&gt; []) (&lt;/span&gt;
&lt;span id=&quot;cb13-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;SomeAsyncException&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;Exception&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TrTyCon&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) (&lt;/span&gt;
&lt;span id=&quot;cb13-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;       &lt;span class=&quot;dt&quot;&gt;ThreadKilled&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     )&lt;/span&gt;
&lt;span id=&quot;cb13-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The outermost wrapper is &lt;code&gt;SomeException&lt;/code&gt;, with the dynamic typing information
&lt;code&gt;(TrTyCon ..)&lt;/code&gt; and the (empty) list of exception annotations &lt;code&gt;(ExceptionContext [])&lt;/code&gt;, which we already mentioned &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#baseline&quot;&gt;above&lt;/a&gt;, and the innermost
constructor &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:AsyncException&quot;&gt;&lt;code&gt;ThreadKilled&lt;/code&gt;&lt;/a&gt; is the exception itself.
However, we additionally have &lt;em&gt;another&lt;/em&gt; wrapper in between:
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:SomeAsyncException&quot;&gt;&lt;code&gt;SomeAsyncException&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This wrapper serves as a marker for async exceptions. It is &lt;em&gt;not&lt;/em&gt; an indication
of how the exception was delivered (synchronously or asynchronously), and this
is as it should be: the marker indicates &lt;em&gt;intent&lt;/em&gt; (“this exception was raised
because of external circumstances, rather than the code itself”). Asynchronous
exceptions are often caught and then rethrown synchronously (for example, see
the &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#bracket&quot;&gt;section about &lt;code&gt;bracket&lt;/code&gt;&lt;/a&gt;), which changes the delivery mechanism
but not the intent.&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn3&quot; id=&quot;fnref3&quot;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This does however mean that this intent must be stated explicitly. For example,
in our &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#asynchronous-exceptions&quot;&gt;first example of asynchronous exceptions&lt;/a&gt; we
asynchronously threw &lt;code&gt;MyException&lt;/code&gt; to another thread, without ever wrapping
&lt;code&gt;MyException&lt;/code&gt; in &lt;code&gt;SomeAsyncException&lt;/code&gt;. If &lt;code&gt;MyException&lt;/code&gt; was &lt;em&gt;intended&lt;/em&gt; to be
used asynchronously, we should use a custom &lt;code&gt;Exception&lt;/code&gt; instance:&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn4&quot; id=&quot;fnref4&quot;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb14&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb14-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb14-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MyException&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb14-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  toException   &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; asyncExceptionToException&lt;/span&gt;
&lt;span id=&quot;cb14-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb14-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  fromException &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; asyncExceptionFromException&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Conversely, we can &lt;em&gt;test&lt;/em&gt; if something is an async exception by checking for
the wrapper:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb15&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb15-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb15-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; fromException e &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb15-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;SomeAsyncException&lt;/span&gt; e') &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Async: &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;++&lt;/span&gt; displayException e'&lt;/span&gt;
&lt;span id=&quot;cb15-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb15-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Not an async exception&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;cancellation&quot;&gt;Cancellation&lt;/h2&gt;
&lt;p&gt;In general, it is good practice to ensure that code that might wait for an
unbounded amount of time, such as when acquiring a lock, can always be
&lt;em&gt;cancelled&lt;/em&gt;, for example through a &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/System-Timeout.html#v:timeout&quot;&gt;&lt;code&gt;timeout&lt;/code&gt;&lt;/a&gt; or through
CTRL-C (&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:AsyncException&quot;&gt;&lt;code&gt;UserInterrupt&lt;/code&gt;&lt;/a&gt;). I’m intentionally using
cancellability instead of “interruptibility”, since the latter has a very
specific technical meaning in Haskell, as we shall discuss now.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;. Foreign calls in Haskell (&lt;code&gt;safe&lt;/code&gt; or &lt;code&gt;unsafe&lt;/code&gt;) are not cancellable.
Alongside &lt;code&gt;safe&lt;/code&gt; and &lt;code&gt;unsafe&lt;/code&gt; there are also &lt;code&gt;interruptible&lt;/code&gt; foreign calls,
which we will discuss in part 2 of this blog post series on exceptions. The term
&lt;code&gt;interruptible&lt;/code&gt;, when applied to foreign calls, is related to, but not the same
as, the “interruptible operations” we discuss here.&lt;/p&gt;
&lt;h3 id=&quot;interruptibility&quot;&gt;Interruptibility&lt;/h3&gt;
&lt;p&gt;When we discussed &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#bracket&quot;&gt;&lt;code&gt;bracket&lt;/code&gt;&lt;/a&gt;, we glossed over one detail: it does not
unmask exceptions when it invokes the &lt;code&gt;acquire&lt;/code&gt; callback to acquire the resource,
nor when it invokes the &lt;code&gt;release&lt;/code&gt; callback to release it again. This is a thorny
subject that we have written about before (&lt;a href=&quot;https://well-typed.com/blog/97/&quot;&gt;Dealing with Asynchronous Exceptions
during Resource Acquisition&lt;/a&gt;); here we highlight the
two most important concepts only.&lt;/p&gt;
&lt;p&gt;First, even with async exceptions masked, &lt;em&gt;interruptible&lt;/em&gt; operations can still
receive exceptions; paraphrasing &lt;a href=&quot;https://www.microsoft.com/en-us/research/wp-content/uploads/2016/07/asynch-exns.pdf&quot;&gt;the paper&lt;/a&gt; slightly: “Any
operation which may need to wait indefinitely for a resource may receive
asynchronous exceptions even within an enclosing &lt;code&gt;mask&lt;/code&gt;, but only while the
resource is unavailable”. The canonical example is &lt;code&gt;takeMVar&lt;/code&gt;, which can receive
exceptions as long as it hasn’t managed to take the &lt;code&gt;MVar&lt;/code&gt; yet, making a
pattern such as this safe:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb16&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb16-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;withLock ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVar&lt;/span&gt; () &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb16-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;withLock mvar criticalSection &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    bracket (takeMVar mvar) (putMVar mvar) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \() &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      criticalSection&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For example, the &lt;code&gt;timeout&lt;/code&gt; in&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb17&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb17-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb17-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;timeout &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;_000_000 &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; withLock lock &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;will still be able to interrupt the &lt;code&gt;takeMVar&lt;/code&gt;. Unfortunately, interruptibility
is not visible from a function’s type, and is typically not even documented; for
example, &lt;code&gt;putStrLn&lt;/code&gt; is interruptible (because deep down its bowels it calls
&lt;code&gt;takeMVar&lt;/code&gt; somewhere on the &lt;code&gt;MVar&lt;/code&gt; buried inside a &lt;code&gt;Handle&lt;/code&gt;), which means that
this variation on &lt;code&gt;withLock&lt;/code&gt; is broken:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb18&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb18-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb18-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;withLockBROKEN ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVar&lt;/span&gt; () &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb18-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb18-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;withLockBROKEN mvar criticalSection &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb18-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb18-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    bracket (takeMVar mvar &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Acquired!&quot;&lt;/span&gt;) (putMVar mvar) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \() &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb18-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb18-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      criticalSection&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The presence of &lt;code&gt;putStrLn&lt;/code&gt; essentially pokes a hole in the careful masked state
that &lt;code&gt;bracket&lt;/code&gt; provides: if an asynchronous exception arrives after the
&lt;code&gt;takeMVar&lt;/code&gt; succeeds but before the &lt;code&gt;putStrLn&lt;/code&gt;, the exception &lt;em&gt;can&lt;/em&gt; be thrown (if
there is contention for that handle &lt;code&gt;MVar&lt;/code&gt;), and the &lt;code&gt;putMVar&lt;/code&gt; will never run,
leaving the lock forever unusable. A good intuition is to keep &lt;code&gt;acquire&lt;/code&gt; and
&lt;code&gt;release&lt;/code&gt; as simple as possible; in the next few sections we will study some more
involved cases.&lt;/p&gt;
&lt;h3 id=&quot;acquiring-multiple-resources&quot;&gt;Acquiring multiple resources&lt;/h3&gt;
&lt;p&gt;When acquiring multiple resources, it is important that we either acquire all of
them or none of them (atomicity). This means we need to worry about previously
acquired resources when we get (synchronous or asynchronous) exceptions while
acquiring more. The easiest option is to nest &lt;code&gt;bracket&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb19&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb19-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb19-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;withMVars ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVar&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVar&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; c) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; c&lt;/span&gt;
&lt;span id=&quot;cb19-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb19-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;withMVars ma mb k &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb19-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    bracket (takeMVar ma) (putMVar ma) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb19-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    bracket (takeMVar mb) (putMVar mb) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb19-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      k a b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Sometimes however the specific way that resources must be acquired and released
does not allow for simple lexical scoping like this; or perhaps only offering a
“with” (&lt;code&gt;bracket&lt;/code&gt;) style API is too limiting at call sites. If you need to
provide an API that &lt;em&gt;acquires&lt;/em&gt; multiple resources, you have to think about how
to deal with the presence of interruptible operations; for example, you might do
something like&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb20&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb20-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb20-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Take both MVars&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb20-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb20-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- Intended to be run with async exceptions masked.&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb20-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;takeMVars ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVar&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVar&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (a, b)&lt;/span&gt;
&lt;span id=&quot;cb20-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb20-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;takeMVars ma mb &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb20-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; takeMVar ma&lt;/span&gt;
&lt;span id=&quot;cb20-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb20-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; takeMVar mb &lt;span class=&quot;ot&quot;&gt;`onException`&lt;/span&gt; putMVar ma a&lt;/span&gt;
&lt;span id=&quot;cb20-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb20-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; (a, b)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There is no point calling &lt;code&gt;mask&lt;/code&gt; &lt;em&gt;inside of&lt;/em&gt; &lt;code&gt;takeMVars&lt;/code&gt;, because if the
enclosing context does not have exceptions masked, then leaving the scope of
that inner &lt;code&gt;mask&lt;/code&gt; could result in an async exception being delivered at that
point.&lt;/p&gt;
&lt;p&gt;In general this can be quite tricky to get right, and it is often better to try
and avoid solving this problem at all. For the case of &lt;code&gt;MVar&lt;/code&gt;-like resources,
the obvious alternative is to use &lt;code&gt;TMVar&lt;/code&gt;s instead, and just take both &lt;code&gt;TMVar&lt;/code&gt;s
inside a single transaction.&lt;/p&gt;
&lt;h3 id=&quot;releasing-multiple-resources&quot;&gt;Releasing multiple resources&lt;/h3&gt;
&lt;p&gt;If you must release multiple resources, you should ensure that if one
resource fails to release cleanly and throws an exception, you still release
the remainder; one option is to chain using &lt;code&gt;finally&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb21&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb21-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb21-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;releaseA &lt;span class=&quot;ot&quot;&gt;`finally`&lt;/span&gt; releaseB &lt;span class=&quot;ot&quot;&gt;`finally`&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the standard &lt;code&gt;bracket acquire release body&lt;/code&gt; pattern, if &lt;code&gt;release&lt;/code&gt; fails with
an exception, but it itself was invoked because of an exception in &lt;code&gt;body&lt;/code&gt;, it’s
debatable which exception should be propagated. For current GHC versions the
exception from &lt;code&gt;release&lt;/code&gt; is propagated, but starting with GHC 10.0, the choice
is dissolved entirely: the exception from &lt;code&gt;release&lt;/code&gt; &lt;a href=&quot;https://well-typed.com/blog/2026/05/lay-annotation-land/#ghc-10.0&quot;&gt;will be propagated with a
&lt;code&gt;WhileHandling&lt;/code&gt; annotation of the original exception&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;We will consider &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#async-exceptions-during-release&quot;&gt;&lt;em&gt;async&lt;/em&gt; exceptions during
&lt;code&gt;release&lt;/code&gt;&lt;/a&gt; a bit further down.&lt;/p&gt;
&lt;h3 id=&quot;non-interruptible-operations&quot;&gt;Non-interruptible operations&lt;/h3&gt;
&lt;p&gt;It is also possible to mask exceptions so that even interruptible operations can
no longer receive exceptions by using
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:uninterruptibleMask&quot;&gt;&lt;code&gt;uninterruptibleMask&lt;/code&gt;&lt;/a&gt; instead of &lt;code&gt;mask&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb22&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb22-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb22-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;uninterruptibleMask &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \unmask &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb22-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; takeMVar ma&lt;/span&gt;
&lt;span id=&quot;cb22-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb22-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; takeMVar mb&lt;/span&gt;
&lt;span id=&quot;cb22-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb22-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is safer in the sense that the &lt;code&gt;takeMVar mb&lt;/code&gt; cannot throw an exception, but
&lt;em&gt;less safe&lt;/em&gt; because this code is now no longer cancellable: timeouts,
user-interrupts (CTRL-C) etc. will not have any effect. To solve that you’d have
to &lt;em&gt;poll:&lt;/em&gt; explicitly check whether any async exceptions are waiting to be
delivered, for example using &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/GHC-IO.html#v:unsafeUnmask&quot;&gt;&lt;code&gt;unsafeUnmask&lt;/code&gt;&lt;/a&gt;
(&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#v:allowInterrupt&quot;&gt;&lt;code&gt;allowInterrupt&lt;/code&gt;&lt;/a&gt;, which does exactly this under &lt;code&gt;mask&lt;/code&gt;,
has no effect under &lt;code&gt;uninterruptibleMask&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Another, possibly simpler, option to create an uninterruptible operation is to
fork an auxiliary thread to do the resource acquisition. Since this thread is
private, it is essentially immune to async exceptions.&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn5&quot; id=&quot;fnref5&quot;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt;
However, this will then still require some kind of communication protocol that
allows the child thread to clean up when it realizes the parent is no longer
interested; see also the discussion of &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#threads&quot;&gt;Threads&lt;/a&gt;, below.&lt;/p&gt;
&lt;h3 id=&quot;async-exceptions-during-release&quot;&gt;Async exceptions during &lt;code&gt;release&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The ability to cancel code is often more important during resource acquisition
than it is during release; acquiring a lock can take an unbounded amount of
time, but releasing the lock does not. However, sometimes releasing a resource
can take an unbounded amount of time also. Perhaps releasing the resource
literally &lt;em&gt;is&lt;/em&gt; acquiring a lock, perhaps to modify some kind of worker pool:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb23&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb23-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb23-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;releaseWorker ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Pool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Worker&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb23-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb23-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;releaseWorker pool _ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; withLock pool&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;lock &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or perhaps the resource is a connection to a server and disconnecting requires
some kind of closing handshake to ensure an orderly closure
(e.g. &lt;a href=&quot;https://www.rfc-editor.org/info/rfc9846/#section-6&quot;&gt;TLS uses closure alerts to avoid truncation attacks&lt;/a&gt;):&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb24&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb24-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb24-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;bracket (connectToServer &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) (closeConnection &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \conn &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case, all the same considerations apply as during resource acquisition:
the release callback is invoked with async exceptions masked, but interruptible
operations can still receive exceptions.&lt;/p&gt;
&lt;p&gt;In some cases you may want to vary what the resource release function does,
depending on whether or not the body terminated with an exception, and depending
on the nature of that exception (see also &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#bracket&quot;&gt;discussion of
&lt;code&gt;generalBracket&lt;/code&gt;&lt;/a&gt;). For example, you may (or may not) opt for an
&lt;a href=&quot;https://www.rfc-editor.org/info/rfc9846/#section-6&quot;&gt;abortive closure of a TLS connection&lt;/a&gt; after a timeout; or you may
(or may not) opt to close a file handle without flushing the buffer. Such
decisions are of course highly application specific; a timeout may simply mean
that the main action should be terminated, but the resource should still be
closed cleanly!&lt;/p&gt;
&lt;h2 id=&quot;threads&quot;&gt;Threads&lt;/h2&gt;
&lt;p&gt;Threads are a microcosm of the three goals that we set up at the start of this
blog post: we must avoid leaking threads, communication between threads can get
stuck, and exceptions thrown in threads often need to be dealt with outside of
those threads.&lt;/p&gt;
&lt;h3 id=&quot;exception-handler&quot;&gt;Exception handler&lt;/h3&gt;
&lt;p&gt;New threads run with an exception handler that &lt;a href=&quot;https://hackage-content.haskell.org/package/ghc-internal-9.1401.0/docs/src/GHC.Internal.Conc.Sync.html#real_handler&quot;&gt;silently
swallows&lt;/a&gt; the asynchronous
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:AsyncException&quot;&gt;&lt;code&gt;ThreadKilled&lt;/code&gt;&lt;/a&gt;, as well as synchronous
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:BlockedIndefinitelyOnMVar&quot;&gt;&lt;code&gt;BlockedIndefinitelyOnMVar&lt;/code&gt;&lt;/a&gt; and
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:BlockedIndefinitelyOnSTM&quot;&gt;&lt;code&gt;BlockedIndefinitelyOnSTM&lt;/code&gt;&lt;/a&gt;, which we will
revisit shortly. Any other exception is handled by the uncaught
exception handler, which &lt;a href=&quot;https://hackage-content.haskell.org/package/ghc-internal-9.1401.0/docs/src/GHC.Internal.Conc.Sync.html#uncaughtExceptionHandler&quot;&gt;by default&lt;/a&gt; simply
prints it (though &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/GHC-Conc.html#v:setUncaughtExceptionHandler&quot;&gt;this can be changed&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;In general it is good practice to ensure no exception is ever uncaught.&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn6&quot; id=&quot;fnref6&quot;&gt;&lt;sup&gt;6&lt;/sup&gt;&lt;/a&gt; In almost all cases you should therefore install an exception
handler in the new thread; what this handler should do will be application
specific, but we will consider one important class of problems in the next
section.&lt;/p&gt;
&lt;h3 id=&quot;buffered-interfaces&quot;&gt;Buffered interfaces&lt;/h3&gt;
&lt;p&gt;Consider a producer and consumer thread that communicate through a buffered
interface such as an &lt;code&gt;MVar&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb25&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb25-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb25-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;producer,&lt;span class=&quot;ot&quot;&gt; consumer ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Chunk&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;MVar&lt;/code&gt; provides back-pressure, which is good: the producer can’t race ahead
and produce an unbounded number of chunks if the consumer is not consuming them.
However, if the producer aborts with an exception, the consumer will be left
waiting for the next chunk which will never arrive.&lt;/p&gt;
&lt;p&gt;There are a number of ways to deal with this. Probably my favourite is to record
the exception in the buffer &lt;em&gt;itself&lt;/em&gt;; this means that the consumer is forced to
consider what to do when the producer fails:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb26&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb26-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb26-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;producer ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVar&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Chunk&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb26-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb26-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;producer buffer &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; (putMVar buffer &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that catching &lt;em&gt;all&lt;/em&gt; exceptions here is correct: the consumer wants to know
if the producer disappeared, even if that was because of an async exception.&lt;/p&gt;
&lt;p&gt;Another option, arguably more compositional, is to use STM, and record the
health of the producer separately from the consumer:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb27&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb27-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Interface&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Interface&lt;/span&gt;{&lt;/span&gt;
&lt;span id=&quot;cb27-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;      health ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TVar&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb27-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; buffer ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TMVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Chunk&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    }&lt;/span&gt;
&lt;span id=&quot;cb27-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;producer ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Interface&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb27-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;producer iface &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; (atomically &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; writeTVar iface&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;health &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb27-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;getChunk ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Interface&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Chunk&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;getChunk iface &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; atomically &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    hasFailed &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; readTVar iface&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;health&lt;/span&gt;
&lt;span id=&quot;cb27-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; hasFailed &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; takeTMVar iface&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;buffer&lt;/span&gt;
&lt;span id=&quot;cb27-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb27-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; e  &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; throwSTM e&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you choose this route, it is important to read both variables in a &lt;em&gt;single&lt;/em&gt;
&lt;code&gt;atomically&lt;/code&gt; block, to ensure that when the transaction is blocked waiting for
a chunk, it will be retried &lt;em&gt;either&lt;/em&gt; when a new chunk becomes available &lt;em&gt;or&lt;/em&gt;
when the health of the producer changes.&lt;/p&gt;
&lt;p&gt;If you don’t signal failure in the producer in any way to the consumer, the
consumer &lt;em&gt;may&lt;/em&gt; fail with a “blocked indefinitely on MVar” or “blocked
indefinitely on STM” exception. You should regard this strictly as a debugging
tool only, in the same category as &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Debug-Trace.html&quot;&gt;&lt;code&gt;Debug.Trace&lt;/code&gt;&lt;/a&gt;. Do &lt;strong&gt;not&lt;/strong&gt;
rely on it for your program logic: false negatives are common (where the
exception is not raised even though code might be blocked indefinitely), and
&lt;em&gt;if&lt;/em&gt; you ever catch it, then &lt;a href=&quot;https://well-typed.com/blog/2024/01/when-blocked-indefinitely-is-not-indefinite/&quot;&gt;false positives are possible&lt;/a&gt;
(where code is &lt;em&gt;not&lt;/em&gt; blocked indefinitely and yet the exception is raised).&lt;/p&gt;
&lt;h3 id=&quot;masking-state-is-inherited&quot;&gt;Masking state is inherited&lt;/h3&gt;
&lt;p&gt;New threads are spawned with &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Concurrent.html#v:forkIO&quot;&gt;&lt;code&gt;forkIO&lt;/code&gt;&lt;/a&gt;, and inherit the masking
state from their parent. We can take advantage of this fact to start a thread
which &lt;em&gt;starts&lt;/em&gt; with exceptions already masked.&lt;/p&gt;
&lt;p&gt;Consider the &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#buffered-interfaces&quot;&gt;&lt;code&gt;producer&lt;/code&gt; thread example&lt;/a&gt; we saw earlier.
A naive attempt to spawn a new producer might look like this:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb28&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb28-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb28-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;newProducerBROKEN ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Interface&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;ThreadId&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb28-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb28-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;newProducerBROKEN &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb28-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    iface    &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Interface&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; newTVarIO &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; newEmptyTMVarIO&lt;/span&gt;
&lt;span id=&quot;cb28-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb28-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    producer &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; forkIO &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb28-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                  &lt;span class=&quot;co&quot;&gt;{- what if an exception is raised here? -}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb28-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                  (&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; (atomically &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; writeTVar iface&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;health &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb28-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb28-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; (iface, producer)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If the producer is spawned in this manner, but is killed before it installs the
exception handler, the consumer will be left waiting forever. For example, the
caller of &lt;code&gt;newProducerBROKEN&lt;/code&gt; is given the &lt;code&gt;ThreadId&lt;/code&gt;, so it may kill the
producer before the forked thread is ever scheduled. Masking asynchronous
exceptions from within the thread cannot help here: the exception might arrive
before we get a chance to call &lt;code&gt;mask&lt;/code&gt;. Instead we must fork the thread &lt;em&gt;with
exceptions already masked&lt;/em&gt;, by masking them in the &lt;em&gt;parent&lt;/em&gt; thread.&lt;/p&gt;
&lt;p&gt;However, we cannot use the normal &lt;code&gt;mask&lt;/code&gt; function; the &lt;code&gt;restore&lt;/code&gt; function that
it gives us will restore the masking state to what it was prior to the call to
&lt;code&gt;mask&lt;/code&gt;; we instead want to ensure that the new thread really runs with
exceptions unmasked, independent of what the masking state in the parent was.
Function &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Concurrent.html#v:forkIOWithUnmask&quot;&gt;&lt;code&gt;forkIOWithUnmask&lt;/code&gt;&lt;/a&gt; gives us precisely this:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb29&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb29-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb29-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;newProducer ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Interface&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;ThreadId&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb29-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb29-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;newProducer &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb29-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    iface    &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Interface&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; newTVarIO &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; newEmptyTMVarIO&lt;/span&gt;
&lt;span id=&quot;cb29-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb29-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    producer &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; mask_ &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; forkIOWithUnmask &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \unmask &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb29-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                  unmask (&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;`catch`&lt;/span&gt; (atomically &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; writeTVar iface&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;health &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb29-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb29-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; (iface, producer)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that there is a function in &lt;code&gt;base&lt;/code&gt; that captures &lt;em&gt;nearly&lt;/em&gt; this pattern:
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Concurrent.html#v:forkFinally&quot;&gt;&lt;code&gt;forkFinally&lt;/code&gt;&lt;/a&gt;. However, &lt;code&gt;forkFinally&lt;/code&gt; does not use
&lt;code&gt;forkIOWithUnmask&lt;/code&gt;, but uses a regular &lt;code&gt;mask&lt;/code&gt;, making it more sensitive to the
masking state of the thread in which it is invoked; in particular, you almost
certainly don’t want to call &lt;code&gt;forkFinally&lt;/code&gt; inside the &lt;code&gt;acquire&lt;/code&gt; callback of
&lt;code&gt;bracket&lt;/code&gt; (see also &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#bracketing-thread-creation&quot;&gt;Bracketing thread creation&lt;/a&gt;
below).&lt;/p&gt;
&lt;h3 id=&quot;throwto-is-synchronous&quot;&gt;&lt;code&gt;throwTo&lt;/code&gt; is synchronous&lt;/h3&gt;
&lt;p&gt;To kill a thread, you use &lt;code&gt;throwTo&lt;/code&gt; to throw a
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Exception.html#t:AsyncException&quot;&gt;&lt;code&gt;ThreadKilled&lt;/code&gt;&lt;/a&gt; to the target thread; this is such a
common idiom, &lt;code&gt;base&lt;/code&gt; provides &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Concurrent.html#v:killThread&quot;&gt;&lt;code&gt;killThread&lt;/code&gt;&lt;/a&gt; that does exactly
this.&lt;/p&gt;
&lt;p&gt;An important aspect of &lt;code&gt;throwTo&lt;/code&gt; is that it is &lt;em&gt;synchronous&lt;/em&gt;: it does not return
until the exception has been raised in the target thread. This can be convenient;
for example, consider a networking context in which we want to send a timeout
message after a certain period:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb30&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb30-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; sendTimeout ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb30-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    sendTimeout &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      threadDelay &lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt;_000_000&lt;/span&gt;
&lt;span id=&quot;cb30-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb30-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;timer &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; forkIO sendTimeout&lt;/span&gt;
&lt;span id=&quot;cb30-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb30-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;killThread timer &lt;span class=&quot;co&quot;&gt;-- timeout no longer needed&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb30-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The parent thread is guaranteed that the child thread won’t send the
notification anymore after the &lt;code&gt;killThread&lt;/code&gt;, without the need for any explicit
synchronization with the child thread. It also has some downsides, however, as
we shall see in the next section.&lt;/p&gt;
&lt;h3 id=&quot;bracketing-thread-creation&quot;&gt;Bracketing thread creation&lt;/h3&gt;
&lt;p&gt;Threads are a resource like any other, and we should ensure that they get
cleaned up. It is tempting to write&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb31&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb31-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb31-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;bracket (forkIO &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) killThread &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \tid &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- WRONG x 2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Both the &lt;code&gt;acquire&lt;/code&gt; and the &lt;code&gt;release&lt;/code&gt; of this example are wrong. First, as we saw
above, when we fork a new thread &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#masking-state-is-inherited&quot;&gt;it inherits the masking state from its
parent&lt;/a&gt;. Since &lt;code&gt;bracket&lt;/code&gt; runs the &lt;code&gt;acquire&lt;/code&gt;
callback with exceptions masked, this means that the new thread will now run
with async exceptions masked at all times (modulo
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/GHC-IO.html#v:unsafeUnmask&quot;&gt;&lt;code&gt;unsafeUnmask&lt;/code&gt;&lt;/a&gt;). However, even if we fix this by using
&lt;code&gt;forkIOWithUnmask&lt;/code&gt; instead, there is still a problem:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb32&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb32-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb32-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;bracket (forkIOWithUnmask &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \unmask &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) killThread &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \tid &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- STILL WRONG&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We saw above that &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#throwto-is-synchronous&quot;&gt;&lt;code&gt;throwTo&lt;/code&gt; is synchronous&lt;/a&gt;: it waits
until the exception is delivered to the child thread, which means it must wait
until the child thread can receive async exceptions. Because it might have to
wait, it is &lt;em&gt;interruptible&lt;/em&gt;; and unlike &lt;code&gt;takeMVar&lt;/code&gt;, which is interruptible only
if the &lt;code&gt;MVar&lt;/code&gt; is not available, &lt;code&gt;throwTo&lt;/code&gt; is &lt;em&gt;always&lt;/em&gt; interruptible. This means
that if this &lt;code&gt;bracket&lt;/code&gt; &lt;em&gt;itself&lt;/em&gt; is executed by a thread that is killed, we might
not clean up the auxiliary thread. This is therefore an example of where the use
of &lt;code&gt;uninterruptibleMask_&lt;/code&gt; is really justified:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb33&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb33-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb33-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;bracket (forkIOWithUnmask &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;) (uninterruptibleMask_ &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; killThread) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \tid &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This does mean that the parent thread (the one running this &lt;code&gt;bracket&lt;/code&gt;) will be
cancellable only if the child thread is too; an &lt;em&gt;unjustified&lt;/em&gt; use of
&lt;code&gt;uninterruptibleMask_&lt;/code&gt; in the child thread (the &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#async-exceptions-during-release&quot;&gt;orderly closure
example&lt;/a&gt; we discussed above), resulting in
uncancellable child threads, propagates and results in uncancellable parent
threads.&lt;/p&gt;
&lt;h2 id=&quot;library-ecosystem&quot;&gt;Library ecosystem&lt;/h2&gt;
&lt;p&gt;In this blog post we focused mostly on principles, not the larger ecosystem. We
cannot possibly give an exhaustive overview here, but there are a few packages
that we should briefly discuss. Two I can unreservedly recommend: &lt;code&gt;exceptions&lt;/code&gt;
and &lt;code&gt;async&lt;/code&gt;. The third, &lt;code&gt;safe-exceptions&lt;/code&gt;, takes an opinionated stance on async
exceptions which you may or may not want to adopt; here we will only point out
some aspects to be aware of.&lt;/p&gt;
&lt;h3 id=&quot;exceptions&quot;&gt;&lt;code&gt;exceptions&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;exceptions&lt;/code&gt; package provides polymorphic versions of the standard exception
handling functions through a stack of type classes: &lt;code&gt;MonadThrow&lt;/code&gt; for monads in
which we can throw exceptions, &lt;code&gt;MonadCatch&lt;/code&gt; in which we can catch them, and
finally &lt;code&gt;MonadMask&lt;/code&gt; in which we have &lt;code&gt;bracket&lt;/code&gt; available. It’s mostly a
straight-forward generalization of the functions in &lt;code&gt;base&lt;/code&gt;, with the exception
of &lt;code&gt;generalBracket&lt;/code&gt;, &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#bracket&quot;&gt;which we discussed already&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;async&quot;&gt;&lt;code&gt;async&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;As we saw &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#threads&quot;&gt;when we discussed threads&lt;/a&gt;, dealing correctly with threads
is non-trivial, and &lt;code&gt;async&lt;/code&gt; aims to make this better. It takes care of the
following things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It provides essentially a simplified form of the &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#buffered-interfaces&quot;&gt;buffered
interface&lt;/a&gt; we discussed above, albeit for threads that
produce a single result; &lt;a href=&quot;https://hackage-content.haskell.org/package/async-2.2.6/docs/Control-Concurrent-Async.html#v:wait&quot;&gt;&lt;code&gt;wait&lt;/code&gt;&lt;/a&gt; or
&lt;a href=&quot;https://hackage-content.haskell.org/package/async-2.2.6/docs/Control-Concurrent-Async.html#v:waitCatch&quot;&gt;&lt;code&gt;waitCatch&lt;/code&gt;&lt;/a&gt; provide a way for the “consumer” to wait
for the thread result.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://hackage-content.haskell.org/package/async-2.2.6/docs/Control-Concurrent-Async.html#v:withAsync&quot;&gt;&lt;code&gt;withAsync&lt;/code&gt;&lt;/a&gt; takes care of spawning new threads,
ensuring that exceptions are masked when the new thread starts, and that the
cancellation is uninterruptible.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Be aware, however, that cancelling a thread (implicitly through leaving the
scope of &lt;code&gt;withAsync&lt;/code&gt;, or explicitly through &lt;a href=&quot;https://hackage-content.haskell.org/package/async-2.2.6/docs/Control-Concurrent-Async.html#v:cancel&quot;&gt;&lt;code&gt;cancel&lt;/code&gt;&lt;/a&gt;),
doesn’t just kill the child thread, but waits for it to terminate. That can be
important: if the parent and the child thread shared any resources, the parent
can be sure that the child will not need them anymore. However, there are also
cases where the parent does not need to, or perhaps even &lt;em&gt;should&lt;/em&gt; not, wait for
the child.&lt;/p&gt;
&lt;p&gt;If you don’t want to use &lt;code&gt;withAsync&lt;/code&gt;, some of the old problems come back:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you use &lt;code&gt;async&lt;/code&gt; inside &lt;code&gt;bracket&lt;/code&gt;, it creates a thread that will run with
exceptions forever masked; use &lt;a href=&quot;https://hackage-content.haskell.org/package/async-2.2.6/docs/Control-Concurrent-Async.html#v:asyncWithUnmask&quot;&gt;&lt;code&gt;asyncWithUnmask&lt;/code&gt;&lt;/a&gt;
instead.&lt;/li&gt;
&lt;li&gt;Similarly, you may need to use &lt;code&gt;uninterruptibleCancel&lt;/code&gt;; see &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#bracketing-thread-creation&quot;&gt;Bracketing thread
creation&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;If you never wait on the new &lt;code&gt;async&lt;/code&gt; at all, and it throws an exception, it
won’t be handled at &lt;em&gt;all&lt;/em&gt;, not even by the default exception handler (that is,
it won’t even be printed). One option here is to use
&lt;a href=&quot;https://hackage-content.haskell.org/package/async-2.2.6/docs/Control-Concurrent-Async.html#v:link&quot;&gt;&lt;code&gt;link&lt;/code&gt;&lt;/a&gt;, which will re-raise most exceptions thrown by the
child in the parent thread, albeit wrapped in
&lt;a href=&quot;https://hackage-content.haskell.org/package/async-2.2.6/docs/Control-Concurrent-Async.html#t:ExceptionInLinkedThread&quot;&gt;&lt;code&gt;ExceptionInLinkedThread&lt;/code&gt;&lt;/a&gt;, which may be
problematic for exception handlers on specific types.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;safe-exceptions&quot;&gt;&lt;code&gt;safe-exceptions&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;As mentioned, &lt;a href=&quot;https://hackage.haskell.org/package/safe-exceptions&quot;&gt;&lt;code&gt;safe-exceptions&lt;/code&gt;&lt;/a&gt; takes an opinionated
stance on async exceptions, which is provided through a series of alternative
implementations of standard &lt;code&gt;base&lt;/code&gt; functions: &lt;code&gt;catch&lt;/code&gt;, &lt;code&gt;bracket&lt;/code&gt;, &lt;code&gt;throwTo&lt;/code&gt; and
others all behave subtly differently in &lt;code&gt;safe-exceptions&lt;/code&gt; than they do
elsewhere. As such, &lt;em&gt;if&lt;/em&gt; you choose to use this package, my strong
recommendation is to import the package qualified, so that use sites
(&lt;code&gt;Safe.catch&lt;/code&gt;) are clearly distinguished from the standard functions.&lt;/p&gt;
&lt;p&gt;The use of &lt;code&gt;safe-exceptions&lt;/code&gt; should be a deliberate choice: &lt;strong&gt;it is not a
drop-in replacement&lt;/strong&gt;. Refactoring existing code to import from
&lt;code&gt;Control.Exception.Safe&lt;/code&gt; instead of &lt;code&gt;Control.Exception&lt;/code&gt;, without making
additional changes, is unlikely to be correct.&lt;/p&gt;
&lt;p&gt;The first important difference is that
&lt;a href=&quot;https://hackage.haskell.org/package/safe-exceptions-0.1.7.4/docs/Control-Exception-Safe.html#v:catch&quot;&gt;&lt;code&gt;Safe.catch&lt;/code&gt;&lt;/a&gt; and related functions do not allow
you to catch async exceptions at all. This means that you don’t have to worry
about catching async exceptions in handlers that catch “all” exceptions, though
&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#catching-all-exceptions&quot;&gt;you probably should not do that anyway&lt;/a&gt;. If you &lt;em&gt;do&lt;/em&gt;
try to catch an async exception, the code will compile just fine, but the
handler will never run.&lt;/p&gt;
&lt;p&gt;In the section on distinguishing sync and async exceptions, we discussed that
the difference is about &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#distinguishing-sync-and-async-exceptions&quot;&gt;intent, not delivery
mechanism&lt;/a&gt;. We implied, though did
not spell it out, that this intent is stated statically, through the &lt;code&gt;Exception&lt;/code&gt;
instance. However, in the implementation of &lt;code&gt;throwTo&lt;/code&gt; in &lt;code&gt;safe-exceptions&lt;/code&gt; this
intent is stated &lt;em&gt;dynamically:&lt;/em&gt; &lt;code&gt;throwTo&lt;/code&gt; will add an &lt;code&gt;AsyncExceptionWrapper&lt;/code&gt;
around the exception being thrown if &lt;code&gt;toException&lt;/code&gt; does not already add the
&lt;code&gt;SomeAsyncException&lt;/code&gt; wrapper. This gives a catcher the ability to still see that
it was “intended to be asynchronous”, but it also means that even if that
catcher is defined using standard &lt;code&gt;catch&lt;/code&gt;, and it has a handler for that exact
exception, it &lt;em&gt;won’t be able to catch it anymore&lt;/em&gt; because the type match will
fail.&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn7&quot; id=&quot;fnref7&quot;&gt;&lt;sup&gt;7&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The behaviour of &lt;code&gt;Safe.bracket&lt;/code&gt; is also subtly different from standard
&lt;code&gt;bracket&lt;/code&gt;. The most important difference is that it wraps the release callback
in &lt;code&gt;uninterruptibleMask_&lt;/code&gt;. This means that the release callback does not need to
worry about async exceptions, but as we saw in the section on &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#async-exceptions-during-release&quot;&gt;async exceptions
during &lt;code&gt;release&lt;/code&gt;&lt;/a&gt;, this may not always be
safer: in the network example, &lt;code&gt;uninterruptibleMask_&lt;/code&gt; around an orderly closure
could mean that the code will wait indefinitely for the network peer to respond,
and cannot be timed out or interrupted with CTRL-C.&lt;/p&gt;
&lt;p&gt;The other difference in &lt;code&gt;bracket&lt;/code&gt; is what happens when the release callback was
called because of an exception in the &lt;code&gt;body&lt;/code&gt;: depending on the exact nature of
the two exceptions (sync or async), exceptions thrown by the release may be
silently swallowed, making it all the more important to &lt;a href=&quot;https://well-typed.com/blog/rss2.xml#releasing-multiple-resources&quot;&gt;be careful if you’re
releasing multiple resources&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;Writing code that does the right thing even when things go wrong can be quite
difficult; this is inherent, not accidental, complexity. Asynchronous exceptions
make this significantly more difficult still, but of course also come with a lot
of benefits; for example, without asynchronous exceptions a general combinator
such as &lt;code&gt;timeout&lt;/code&gt; would not be possible. Exceptions also tend to be
non-deterministic, so leaked resources, stuck code, or unhandled failures may
not be noticed during testing. Nonetheless, with some good code hygiene many
problems can be avoided; hopefully the guidelines in this blog post can assist.&lt;/p&gt;
&lt;p&gt;There are a few topics that we have not covered in this blog post:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We have ignored pure exceptions. The (slightly but not entirely
tongue-in-cheek) TL;DR is: avoid pure exceptions, but we will come back to
this in much more detail in part 3 of this series.&lt;/li&gt;
&lt;li&gt;We have not discussed exception annotations at all, or how to preserve them;
we covered this in detail in a previous blog post &lt;a href=&quot;https://well-typed.com/blog/2026/05/lay-annotation-land/&quot;&gt;Exception Annotations: Lay
of the Land&lt;/a&gt;, as well as in &lt;a href=&quot;https://www.youtube.com/watch?v=SwOkh9N41Y8&amp;amp;list=PLD8gywOEY4HaG5VSrKVnHxCptlJv2GAn7&amp;amp;index=29&amp;amp;t=1884s&quot;&gt;Unfolder episode 29:
exceptions, annotations and backtraces&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We have not discussed finalizers (for example on a &lt;code&gt;ForeignPtr&lt;/code&gt;, or using
&lt;code&gt;System.Mem.Weak&lt;/code&gt;). Finalizers run at some point after an object has become
unreachable, but there is no guarantee of when, or indeed whether, they run;
they are therefore no substitute for &lt;code&gt;bracket&lt;/code&gt; for resources that must be
released promptly, such as locks or file handles.&lt;/li&gt;
&lt;li&gt;We have not discussed &lt;a href=&quot;https://hackage.haskell.org/package/resourcet&quot;&gt;&lt;code&gt;ResourceT&lt;/code&gt;&lt;/a&gt;, which is useful when
resource lifetimes do not nest lexically: resources are registered with an
enclosing scope, and released when that scope is exited.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Many thanks to &lt;a href=&quot;https://www.anduril.com/&quot;&gt;Anduril&lt;/a&gt; for sponsoring this
work.&lt;/p&gt;
&lt;section class=&quot;footnotes footnotes-end-of-document&quot; id=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;The main
reference here is &lt;a href=&quot;https://www.microsoft.com/en-us/research/wp-content/uploads/2016/07/asynch-exns.pdf&quot;&gt;Asynchronous Exceptions in Haskell&lt;/a&gt; by Marlow et
al.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn2&quot;&gt;&lt;p&gt;In addition to successful
termination and abnormal termination with an exception, &lt;code&gt;generalBracket&lt;/code&gt; also
has a case for abnormal termination in monad stacks such as &lt;code&gt;ExceptT&lt;/code&gt;. Since
these are not strictly speaking exceptions, we will not discuss this any further
in this blog post.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref2&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn3&quot;&gt;&lt;p&gt;There are reasons to avoid synchronously rethrowing
asynchronous exceptions; see GHC &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/work_items/2558&quot;&gt;#2558&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/work_items/24189&quot;&gt;#24189&lt;/a&gt; and
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/work_items/26368&quot;&gt;#26368&lt;/a&gt;; however, those reasons are mostly orthogonal to our
discussion here.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref3&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn4&quot;&gt;&lt;p&gt;For the
general mechanism at work here, see &lt;a href=&quot;https://simonmar.github.io/bib/papers/ext-exceptions.pdf&quot;&gt;An Extensible Dynamically-Typed Hierarchy
of Exceptions&lt;/a&gt;.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref4&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn5&quot;&gt;&lt;p&gt;It would not be immune
to async exceptions such as heap overflow, of course, but they are generally
irrecoverable anyway. Strictly speaking it is not truly private either, as
&lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/GHC-Conc.html#v:listThreads&quot;&gt;&lt;code&gt;listThreads&lt;/code&gt;&lt;/a&gt; can be used to enumerate all threads.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref5&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn6&quot;&gt;&lt;p&gt;The
only technical exception to this is &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/System-Exit.html#v:exitFailure&quot;&gt;&lt;code&gt;exitFailure&lt;/code&gt;&lt;/a&gt;, which
should only be called from the main thread, and will throw an &lt;code&gt;ExitCode&lt;/code&gt;
exception.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref6&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn7&quot;&gt;&lt;p&gt;Arguably, asynchronously throwing an exception with an &lt;code&gt;Exception&lt;/code&gt;
instance that does not use &lt;code&gt;asyncExceptionToException&lt;/code&gt; and
&lt;code&gt;asyncExceptionFromException&lt;/code&gt; is a bug, but it’s probably a common one: simply
&lt;code&gt;deriving Exception&lt;/code&gt; is an easy mistake to make.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref7&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Thu, 26 Nov 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Dan Piponi (sigfpe): Derivative flipping</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-11295132.post-7399860157393107637</guid>
	<link>http://blog.sigfpe.com/2026/11/derivative-flipping.html</link>
	<description>&lt;p&gt;
 Well this is awkward. Due to a possible conflict of interest I'm taking this down. I hope to return it when everything is resolved.
&lt;/p&gt;</description>
	<pubDate>Tue, 24 Nov 2026 19:43:46 +0000</pubDate>
	<author>noreply@blogger.com (sigfpe)</author>
</item>
<item>
	<title>Sandy Maguire: New Notation for Abstract Scales</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/new-notation/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/new-notation/index.html</link>
	<description>&lt;p&gt;(edited early 2026-11-22; the new notation now has pitch classes on the note heads)&lt;/p&gt;
&lt;p&gt;HA HA HA I SNUCK OUT AND DID SOME MUSIC!!! (after some encouragement from my wife!)&lt;/p&gt;
&lt;p&gt;Anyway, just a quick little update on some new notation I’ve been working on. Hopefully this will more clearly elucidate the points I’m trying to make with these &lt;a href=&quot;https://reasonablypolymorphic.com/blog/notes-scales/&quot;&gt;abstract scales&lt;/a&gt; and &lt;a href=&quot;https://reasonablypolymorphic.com/blog/accidentals/&quot;&gt;generalized accidentals&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here’s what the new notation looks like:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;3 Octaves of a 4-Note Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out8.cropped.svg&quot; /&gt;&lt;figcaption&gt;3 Octaves of a 4-Note Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;This is a 3-octave run in a 4-note scale. How can you tell it’s in a 4-note scale? Because the clef is a big “4”.&lt;/p&gt;
&lt;p&gt;In the first bar, we have an &lt;code&gt;-4&lt;/code&gt; ottava bracket, which means we ought to read this bar to be one octave lower than it’s notated. The second bar is missing this bracket, and is therefore one octave up. And the third bar is an octave higher again.&lt;/p&gt;
&lt;p&gt;You’ll notice that each note head has a little number inside it. These are the pitch classes of the scale. Rather than labeling these abstract pitch classes things like “C”, “D”, “E”, “F”, “G”, “A”, “B”, we instead label them by number. The “tonic” of any abstract scale is named 0, and they go up from there. The pitch class &lt;em&gt;below&lt;/em&gt; the tonic is always named &lt;code&gt;n - 1&lt;/code&gt;. So in this example, because we’re in a 4-note scale, the pitch classes are 0, 1, 2, 3.&lt;/p&gt;
&lt;p&gt;What exactly does this scale “sound” like, you might be wondering? It doesn’t sound like anything; it’s just twelve steps in some arbitrary 4-note scale. We’d have to pin down exactly what that scale was in order to be able to realize it into something audible.&lt;/p&gt;
&lt;p&gt;Let’s take another example. This next one is a 5-octave run in a 3-note scale:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;5 Octaves of a 3-Note Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out5.cropped.svg&quot; /&gt;&lt;figcaption&gt;5 Octaves of a 3-Note Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The conventions are exactly the same here. Each measure spans an octave, and each measure is an octave above the previous one.&lt;/p&gt;
&lt;p&gt;The only thing to notice here is we have a +6 ottava in the last two bars. This is indicate it’s &lt;em&gt;two octaves&lt;/em&gt; above what’s notated. It’s +6 because we’re two octaves above in a 3-note scale, and you shouldn’t be surprised to learn that &lt;code&gt;3 * 2 = 6&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Let’s take this same 5-octave in a 3-note scale and &lt;em&gt;transform&lt;/em&gt; it into a 7-note scale. The way we’re going to do that is via this mapping:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;triad ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In our 3-note scale, we only have three notes, so giving a mapping for each of &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, and &lt;code&gt;2&lt;/code&gt; is sufficient to map every pitch class out of the scale. Here we’re mapping to &lt;em&gt;every other&lt;/em&gt; note in a 7-note scale, which when you look at it, is exactly how we build diatonic triads. Take every second note in the 7-note scale, and that gives you a triad!&lt;/p&gt;
&lt;p&gt;So when we apply this mapping over our 3-note scale example, we end up with &lt;em&gt;arpeggiation of a triad&lt;/em&gt; over 5 octaves:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;5 Arpeggiations in a 7-Note Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out6.cropped.svg&quot; /&gt;&lt;figcaption&gt;5 Arpeggiations in a 7-Note Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Note that &lt;code&gt;triad&lt;/code&gt; has changed the names on the note heads.&lt;/p&gt;
&lt;p&gt;Technically this is an &lt;em&gt;abstract&lt;/em&gt; 7-note scale, but we can choose to interpret it as the usual diatonic scale. As a matter of convention, we will map the tonic of our abstract 7-note to C in the diatonic scale. This brings us back to everyday musical notation that should be familiar to practitioners:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;5 Arpeggiations in the Diatonic Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out7.cropped.svg&quot; /&gt;&lt;figcaption&gt;5 Arpeggiations in the Diatonic Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Notice that this diagram carries &lt;em&gt;exactly the same information&lt;/em&gt; as the previous one, so it can be useful as a sort-of Rosetta Stone to help get an intuition for what the abstract notation is trying to articulate.&lt;/p&gt;
&lt;h2 id=&quot;a-more-musical-example&quot;&gt;A More Musical Example&lt;/h2&gt;
&lt;p&gt;So that’s a quick little primer on the notation. But now let’s try something a little more musical. We can bring in the same motif that we &lt;a href=&quot;https://reasonablypolymorphic.com/blog/accidentals/&quot;&gt;used previously&lt;/a&gt;:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Motif in a 3-Note Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out1.cropped.svg&quot; /&gt;&lt;figcaption&gt;Motif in a 3-Note Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;You’ll notice the familiar “abstract 3 clef”. The only new thing in this example are the accidentals. Unlike in a previous post, they are now using standard sharps/flats/natural symbols. But they also have little subscripts — the third note has a subscript 7 and the fourth note has a subscript 12.&lt;/p&gt;
&lt;p&gt;These subscripts express &lt;em&gt;which scale&lt;/em&gt; we want to apply the accidental to. The &lt;code&gt;#/7&lt;/code&gt; means “move up one scale degree in a 7-note (diatonic) scale”. The “b/12” means “move down one scale degree in a 12-note (chromatic) scale.”&lt;/p&gt;
&lt;p&gt;We’ll expand this out momentarily to get some intuition as to what this means musically.&lt;/p&gt;
&lt;p&gt;Let’s repeat the motif 3 times, each time up a single scale degree:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Repeated Motif in a 3-Note Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out2.cropped.svg&quot; /&gt;&lt;figcaption&gt;Repeated Motif in a 3-Note Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Using the same &lt;code&gt;triad&lt;/code&gt; mapping as before, we can move this repeated motif into an abstract 7-note scale:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Repeated Motif in a 7-Note Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out3.cropped.svg&quot; /&gt;&lt;figcaption&gt;Repeated Motif in a 7-Note Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;There are two things to notice here. The first is that this thing looks much less like an arpeggiation than our first example. And the second is that the &lt;code&gt;#/7&lt;/code&gt;s are gone! These two facts are related to one another.&lt;/p&gt;
&lt;p&gt;Recall that &lt;code&gt;#/7&lt;/code&gt; was notation for “move up one note in a 7-note scale”. And so now that we’re &lt;em&gt;in&lt;/em&gt; a 7-note scale, we can drop the notation and &lt;em&gt;just move up one note.&lt;/em&gt; So our &lt;code&gt;triad&lt;/code&gt; mapped pitch class 1 (in the 3-note scale) to pitch class 2 (in the 7-note scale). And then the &lt;code&gt;#/7&lt;/code&gt; causes it to move up to pitch class 3.&lt;/p&gt;
&lt;p&gt;But since we haven’t yet moved into a 12-note scale, we can’t do the same procedure to the &lt;code&gt;b/12&lt;/code&gt;s, and so those accidentals remain.&lt;/p&gt;
&lt;p&gt;Let’s now reinterpret this abstract 7-note scale as the usual diatonic scale:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Repeated Motif in the Diatonic Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out4.cropped.svg&quot; /&gt;&lt;figcaption&gt;Repeated Motif in the Diatonic Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;which again should be familiar ground to musicians. You’ll notice that the &lt;code&gt;b/12&lt;/code&gt; have been replaced by simple flats, because standard music notation actually expresses the diatonic scale, and uses accidentals to express chromatic moves.&lt;/p&gt;
&lt;p&gt;Check out this music we’ve composed! What was once a very simple pattern in &lt;code&gt;Note 3&lt;/code&gt; now has some character!&lt;/p&gt;
&lt;h2 id=&quot;by-way-of-some-other-mappings&quot;&gt;By Way Of Some Other Mappings&lt;/h2&gt;
&lt;p&gt;Of course, nobody says we need to interpret our 3-note scale as &lt;em&gt;triads&lt;/em&gt;. We could instead take our same repeated motif and map it into a 5-note scale. To pick a mapping at random, let’s go with:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;toFive ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;toFive &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;toFive &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;toFive &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Applying this mapping gives us the “same motif” but now in a 5-note scale:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Repeated Motif in a 5-Note Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out9.cropped.svg&quot; /&gt;&lt;figcaption&gt;Repeated Motif in a 5-Note Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Notice now, unlike before, that since we moved into the 5 note scale, our &lt;code&gt;#/7&lt;/code&gt; hasn’t triggered yet. We hold onto all accidentals until we move into a correspondly-sized scale.&lt;/p&gt;
&lt;p&gt;We can interpret this 5-note scale as a major pentatonic, which then gives us&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Repeated Motif in Major Pentatonic&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out10.cropped.svg&quot; /&gt;&lt;figcaption&gt;Repeated Motif in Major Pentatonic&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Or we could interpret it as the minor pentatonic:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Repeated Motif in Minor Pentatonic&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out11.cropped.svg&quot; /&gt;&lt;figcaption&gt;Repeated Motif in Minor Pentatonic&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;We keep using that same 3-note scale motif, but we keep getting different results because we keep taking it through different paths on its way up to the diatonic scale.&lt;/p&gt;
&lt;h2 id=&quot;using-a-chord-as-a-scale&quot;&gt;Using a Chord as a Scale&lt;/h2&gt;
&lt;p&gt;Let’s try another trick. Let’s take our repeated motif and this time map it into a 4-note scale. So far all of our transformations have preserved octave relationships (that is to say, if two notes are one octave apart before we map them, they are one octave apart after we map them.) But music is supposed to be &lt;em&gt;free, man.&lt;/em&gt; There are no rules here. So let’s just reinterpret the motif as a 4-note scale:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Reinterpreted Motif in a 4-Note Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out12.cropped.svg&quot; /&gt;&lt;figcaption&gt;Reinterpreted Motif in a 4-Note Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;In our original motif, the last notes in the second bar were pitch class 0. But in this example, we have mapped them to pitch class 3. That’s exactly the lack of octave-preservation we mentioned. But I am sure you will agree the &lt;em&gt;contour&lt;/em&gt; of this motif is the same!&lt;/p&gt;
&lt;p&gt;Now instead of giving a constant mapping from our 4-note scale into something playable, let’s instead give a dynamic mapping. For example, here’s a nice chord progression:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;A Nice Chord Progression&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out13.cropped.svg&quot; /&gt;&lt;figcaption&gt;A Nice Chord Progression&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;You’ll notice that each of these chords has four notes. And we have a motif that is in a 4-note scale… so what if we to map our motif to the notes of the progression?&lt;/p&gt;
&lt;p&gt;The details here are pretty finicky, so I’ve notated this one as a grand staff. The first system is the chord progression, the second is the 4-note scale motif. The third system is the chord tones that get targeted by each note in the motif, and the final system is after we have applied all the accidentals from the motif to the targeted chord tones.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Derivation of Our Motif Over Chords&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out14.cropped.svg&quot; /&gt;&lt;figcaption&gt;Derivation of Our Motif Over Chords&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Remember that the bottom line on the 4-note staff represents the “tonic” note, which is the lowest note of each chord. So because the first note of the motif is on the tonic, and the lowest note of the first chord is F, the resulting note is F.&lt;/p&gt;
&lt;p&gt;The note in our motif is just the next 4-note above the tonic, which is the second note in our chord, thus we get an Ab.&lt;/p&gt;
&lt;p&gt;But the third note of the motif is a &lt;code&gt;#/7&lt;/code&gt; above the second note in the chord. So since the second note in our chord is Ab (third system), one diatonic step above that is Bb (fourth system).&lt;/p&gt;
&lt;p&gt;The fourth note of the motif is a &lt;code&gt;b/12&lt;/code&gt; below the next note in the chord. The next note of the chord is C, so we lower it by a flat and get Cb. And then the last note of the motif is just the same note of the chord, but this time without the flat — thus C.&lt;/p&gt;
&lt;p&gt;That’s the derivation of the first bar. So far, so good. The second bar makes things a little trickier. This time the motif starts on the first space, which is to say it begins on the &lt;em&gt;second&lt;/em&gt; note up the chord. Since the motif only touches three notes of the chord, in the second bar we will be ignoring the root note of the chord (the low F).&lt;/p&gt;
&lt;p&gt;It’s worth tracing through the details on this example. It’s finicky, but really makes you appreciate why we might want to use computers to calculate this stuff on our behalf.&lt;/p&gt;
&lt;p&gt;As one final step, we might want to take the last system of our derivation and “renormalize” it — changing the spelling of the notes so they feel a little more natural:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Renormalized Results&quot; src=&quot;https://reasonablypolymorphic.com/images/music8_2/out15.cropped.svg&quot; /&gt;&lt;figcaption&gt;Renormalized Results&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Whew! We made it to the end!&lt;/p&gt;
&lt;p&gt;If you’re been paying attention to the &lt;a href=&quot;https://reasonablypolymorphic.com/tags/music.html&quot;&gt;whole series&lt;/a&gt; of blog posts, there’s nothing &lt;em&gt;new&lt;/em&gt; here, but perhaps having a few more worked examples might help some of the ideas land. And again, none of these are &lt;em&gt;my&lt;/em&gt; ideas — they all come from &lt;a href=&quot;https://www.foyles.co.uk/book/tonality/dmitri-tymoczko/9780197577103&quot;&gt;Tonality&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What is mine is the notation, which I think can help with understanding. Of course, you’d never actually &lt;em&gt;want&lt;/em&gt; to do any of these transformations by hand, but seeing them illustrated in full might perhaps pull back the curtains on composition.&lt;/p&gt;
&lt;p&gt;Perhaps what set apart the great composers of yore was not their musical genius, but their patience to work through the details of all these fiddly transformations.&lt;/p&gt;</description>
	<pubDate>Sat, 21 Nov 2026 16:33:00 +0000</pubDate>
</item>
<item>
	<title>Sandy Maguire: The Third Scale Encoding is Hopepfully the Charm</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/mvec-scales/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/mvec-scales/index.html</link>
	<description>&lt;p&gt;Well, unfortunately time has run out, and I am not going to make the NaNoWriMo writing goal! I’m off to Japan on Monday and promised my wife that I’d do at least a little thinking about the trip before we head off. So that means I’m putting a (temporary) close on the music stuff for a few weeks. But don’t worry; I can’t get this stuff off my brain, so some time off is likely only going to stoke the flames.&lt;/p&gt;
&lt;p&gt;With that being said…&lt;/p&gt;
&lt;p&gt;My plan today was to write about vertical configurations and voice leadings. But I realized my notion of voice leadings didn’t agree with &lt;a href=&quot;https://dmitri.mycpanel.princeton.edu/index.html&quot;&gt;Tymoczko&lt;/a&gt;’s. And while there are a lot of music theorists whose opinions I don’t put much stock into, Tymoczko is one whose thoughts I value very much.&lt;/p&gt;
&lt;p&gt;While trying to answer those questions, I looked at my implementation of &lt;a href=&quot;https://en.wikipedia.org/wiki/Neo-Riemannian_theory&quot;&gt;Neo-Riemannian transformations&lt;/a&gt; and realized it was convoluted as all hell (which I knew). Furthermore I noticed that our work on scales the &lt;a href=&quot;https://reasonablypolymorphic.com/blog/notes-scales/&quot;&gt;other day&lt;/a&gt; did almost everything I was doing by hand in the Neo-Riemannian case. So I swapped out the bespoke stuff for everyday scale things, and the tests stayed green.&lt;/p&gt;
&lt;p&gt;Happy days. But then I changed my test generators (so that notes would include &lt;a href=&quot;https://reasonablypolymorphic.com/blog/accidentals/&quot;&gt;generalized accidentals&lt;/a&gt;) and the tests went red! For reasons unrelated to Neo-Riemannian transformations, but for darker, scarier reasons. &lt;em&gt;Scales&lt;/em&gt; were doing the wrong thing.&lt;/p&gt;
&lt;p&gt;Oh man. Not so good. The problem is that under our previous encoding, &lt;em&gt;not all scales are &lt;a href=&quot;https://en.wikipedia.org/wiki/Section_(category_theory)&quot;&gt;retractable&lt;/a&gt;.&lt;/em&gt; Check it out:&lt;/p&gt;
&lt;p&gt;We previously left &lt;code&gt;Scale&lt;/code&gt;s defined thusly:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; m n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; n)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The codomain being &lt;code&gt;Note n&lt;/code&gt; was so that we had a place to stick a scale degree and an accidental. We really only wanted to store a pitch class and an accidental, but you could rotate the pitch classes in a scale and then the lower pitch classes might only be reachable an octave up. Thus the degrees.&lt;/p&gt;
&lt;p&gt;OK fine. But look at this mapping:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;weird ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;weird &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; stepIn &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we’ve defined a scale that maps both pitch classes in &lt;code&gt;Fin 2&lt;/code&gt; to &lt;code&gt;0 :: Deg 7&lt;/code&gt;, but the second pitch class also carries a (chromatic) sharp. All is well and good, until we go to invert this thing. Inverting the image of the first pitch class is fine:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; weirder &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; invertScaleAbove weird&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;applyScale weirder (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;but inverting the image of the second, well…&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;applyScale weirder (stepIn &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; stepIn &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because application of scales dispatches only on pitch classes, it can’t branch on the accidental here. So what was once in the image of &lt;code&gt;weird&lt;/code&gt; is now somehow getting smuggled along when we apply the inverse of &lt;code&gt;weird&lt;/code&gt;. Weird!&lt;/p&gt;
&lt;p&gt;This is a fundamental problem with our scales as written. They simply do not always admit left inverses!&lt;/p&gt;
&lt;p&gt;To be clear, the problem here is that &lt;code&gt;weird&lt;/code&gt; isn’t injective in its mappings from &lt;code&gt;Fin 2&lt;/code&gt; to &lt;code&gt;Fin 7&lt;/code&gt; (both &lt;code&gt;0, 1 :: Fin 2&lt;/code&gt; end up at &lt;code&gt;0 :: Fin 7&lt;/code&gt;.) So if we could force the scale to be injective, then the error conditions wouldn’t apply, and we &lt;em&gt;always would&lt;/em&gt; be able to find a left inverse.&lt;/p&gt;
&lt;p&gt;So then, &lt;em&gt;how do we ensure we have an injective mapping between finite numbers?&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;a-quick-musical-note&quot;&gt;A Quick Musical Note&lt;/h2&gt;
&lt;p&gt;As an aside to the main thrust of this blog post, I found it quite elucidating to think about what this “injective mapping of pitch classes” constraint &lt;em&gt;actually means&lt;/em&gt; in terms of music.&lt;/p&gt;
&lt;p&gt;And I THINK that this is the underlying constraint that &lt;a href=&quot;https://en.wikipedia.org/wiki/Enharmonic_equivalence&quot;&gt;enharmonic spelling&lt;/a&gt; is attempting to solve.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://reasonablypolymorphic.com/blog/accidentals/&quot;&gt;Recall&lt;/a&gt; that the standard musical staff notates the diatonic scale (&lt;code&gt;Fin 7&lt;/code&gt;), which corresponds (roughly) to the white keys on the piano. For time immemorial we have been working in &lt;code&gt;Fin 7&lt;/code&gt;, and this is why the black keys &lt;em&gt;don’t have their own names&lt;/em&gt; – because the black keys are the five notes that you need to add in to get to &lt;code&gt;Fin 12&lt;/code&gt;. We happily talk about the white notes as A, B, C, D, E, F, G, but the black notes are relegated to mere modifiers — sometimes as D#, other times as Eb. But never as &lt;span class=&quot;music-digraph&quot;&gt;DE&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Anyway, since we can’t call it the note between D and E as &lt;span class=&quot;music-digraph&quot;&gt;DE&lt;/span&gt;, what can we do? Well we need to project this note down to &lt;code&gt;Fin 7&lt;/code&gt;. Which of course can’t be done in general, but we cheat by adding an accidental. Is it either D# or Eb? It depends! What does it depend on?&lt;/p&gt;
&lt;p&gt;Conventional spelling rules say that it ought to be offset from “the scale note it is acting in place of.” Which is to say that it must be &lt;em&gt;temporarily replacing&lt;/em&gt; some note in the scale, and it steals that note’s pitch class!&lt;/p&gt;
&lt;p&gt;If we pull this back to injective mapping of finite numbers, the weird historical rule makes sense. &lt;em&gt;In order to preserve this injective mapping&lt;/em&gt; you &lt;strong&gt;must&lt;/strong&gt; remove any other notes which would otherwise map to the same pitch class!&lt;/p&gt;
&lt;p&gt;I feel like I am not articulating this very well. Maybe some diagrams would help. But I’d actually like to go to bed sometime tonight and there is still too much to write down before I need to put the book ends on my music project for a few weeks. Alas.&lt;/p&gt;
&lt;p&gt;Let me know in the comments if any of this section makes a lick of sense. Or if it doesn’t. That’s what we call &lt;tt&gt;e n g a g e m e n t&lt;/tt&gt;, baby.&lt;/p&gt;
&lt;h2 id=&quot;dependently-typed-programming-to-the-rescue&quot;&gt;Dependently-Typed Programming to the Rescue&lt;/h2&gt;
&lt;p&gt;OK so anyway, back to trying to enforce injectivity of finite maps.&lt;/p&gt;
&lt;p&gt;Thankfully I wrote a &lt;a href=&quot;https://leanpub.com/certainty-by-construction&quot;&gt;book on Agda&lt;/a&gt; which happens to be pretty helpful for working out solutions to stuff like this.&lt;/p&gt;
&lt;p&gt;Recall that functions &lt;code&gt;Fin n -&amp;gt; a&lt;/code&gt; are isomorphic to vectors &lt;code&gt;Vec n a&lt;/code&gt; (which is to say, a product of &lt;code&gt;n&lt;/code&gt; copies of &lt;code&gt;a&lt;/code&gt;.) This fact will come up again when I eventually get around to writing about voice leadings. But you will notice that functions &lt;code&gt;Fin n -&amp;gt; a&lt;/code&gt; are not injective functions between &lt;code&gt;Fin&lt;/code&gt;s!&lt;/p&gt;
&lt;p&gt;To be concrete, let’s say we’re looking for mappings of the form &lt;code&gt;Fin m -&amp;gt; (Fin n, a)&lt;/code&gt;. Which solves our scale problem (because we can put our accidentals in &lt;code&gt;a&lt;/code&gt;.) So as a first step, we could consider encoding this mapping as:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Vec&lt;/span&gt; m (&lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;with the invariant that there be exactly &lt;code&gt;n&lt;/code&gt; elements that contain &lt;code&gt;a&lt;/code&gt;s. For example, &lt;code&gt;triad :: Scale 3 7&lt;/code&gt; would look like this:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;triad' ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad' &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;mempty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb6-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb6-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb6-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nil&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we have filled exactly 3 of the 7 total slots. We can then recover the mapping by traversing over the &lt;code&gt;Just&lt;/code&gt;s and pairing each with the next &lt;code&gt;Fin 3&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;triad'' ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb7-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad'' &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;mempty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, a)&lt;/span&gt;
&lt;span id=&quot;cb7-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, a)&lt;/span&gt;
&lt;span id=&quot;cb7-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, a)&lt;/span&gt;
&lt;span id=&quot;cb7-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nil&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now if we zip this vector with its own indices, we get this:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;triad''' ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vec&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb8-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad''' &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;mempty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, a))&lt;/span&gt;
&lt;span id=&quot;cb8-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb8-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, a))&lt;/span&gt;
&lt;span id=&quot;cb8-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb8-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, a))&lt;/span&gt;
&lt;span id=&quot;cb8-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb8-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb8-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nil&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, filter out the &lt;code&gt;Nothing&lt;/code&gt;s, and you’re left with (he said, gesticulating wildly) exactly three values, and their corresponding indices in both &lt;code&gt;Fin 3&lt;/code&gt; and &lt;code&gt;Fin 7&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;triad'''' ::&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;))]&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad'''' &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;mempty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; [ (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, a))&lt;/span&gt;
&lt;span id=&quot;cb9-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     , (&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, (&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, a))&lt;/span&gt;
&lt;span id=&quot;cb9-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     , (&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;, (&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, a))&lt;/span&gt;
&lt;span id=&quot;cb9-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     ]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since you and I both “know” that there are three elements in this list, we can give a “total” function:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;triad ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; triad'''' &lt;span class=&quot;op&quot;&gt;!!&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; n&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Sure enough this does work! But it only works because of our invariant that there are exactly&lt;a class=&quot;footnote-ref&quot; href=&quot;https://reasonablypolymorphic.com#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; 3 &lt;code&gt;Just&lt;/code&gt;s in our original vector. That’s cool and all, but it seems like the sort of thing that would be easy to get wrong. And then you’d be crashing at runtime, and that’s not the Haskell Ethos.&lt;/p&gt;
&lt;p&gt;So what can we do instead? Let’s make a type that is like a &lt;code&gt;Vec&lt;/code&gt;, but which counts slots separately from filled slots. I needed a name so I called it an &lt;code&gt;MVec&lt;/code&gt;, but the &lt;code&gt;M&lt;/code&gt; doesn’t actually stand for anything. Behold:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; slots filled a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;MNil&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt;                              &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt;  &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;           &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;           a&lt;/span&gt;
&lt;span id=&quot;cb11-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; slots filled a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; (slots &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) (filled &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) a&lt;/span&gt;
&lt;span id=&quot;cb11-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt;      &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; slots filled a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; (slots &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)  filled      a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, &lt;code&gt;MCons&lt;/code&gt; acts exactly like &lt;code&gt;:&amp;gt;&lt;/code&gt; did in our vector example — it sticks an element in front of another &lt;code&gt;MVec&lt;/code&gt;. But in doing so, it increases the slots and the fill count both by one. However, there’s also &lt;code&gt;MSkip&lt;/code&gt; which only increases the slots (and correspondingly doesn’t have a place to put an &lt;code&gt;a&lt;/code&gt;.)&lt;/p&gt;
&lt;p&gt;This structure thus perfectly encodes our &lt;code&gt;m&lt;/code&gt; slots of which exactly &lt;code&gt;n&lt;/code&gt; are filled semantics. Very cool. As a little proof, we can project a &lt;code&gt;MVec m n a&lt;/code&gt; down to either a &lt;code&gt;Vec n a&lt;/code&gt; or a &lt;code&gt;Vec m (Maybe a)&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;embedMVec ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; m n a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vec&lt;/span&gt; n a&lt;/span&gt;
&lt;span id=&quot;cb12-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;embedMVec &lt;span class=&quot;dt&quot;&gt;MNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;embedMVec (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; a as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; a &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; embedMVec as&lt;/span&gt;
&lt;span id=&quot;cb12-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;embedMVec (&lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; embedMVec as&lt;/span&gt;
&lt;span id=&quot;cb12-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;projectMVec ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; m n a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vec&lt;/span&gt; m (&lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb12-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;projectMVec &lt;span class=&quot;dt&quot;&gt;MNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;projectMVec (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; a as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; a &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; projectMVec as&lt;/span&gt;
&lt;span id=&quot;cb12-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;projectMVec (&lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; projectMVec as&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So that’s all well and good. But we can also &lt;em&gt;actually find things&lt;/em&gt; in this data structure, as given by &lt;code&gt;mlookup&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;mlookup ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; m n a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m, a)&lt;/span&gt;
&lt;span id=&quot;cb13-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;mlookup (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; a _) &lt;span class=&quot;dt&quot;&gt;FZ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;FZ&lt;/span&gt;, a)&lt;/span&gt;
&lt;span id=&quot;cb13-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;mlookup (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; _ mn) (&lt;span class=&quot;dt&quot;&gt;FS&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; mlookup mn n &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (m, a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;FS&lt;/span&gt; m, a)&lt;/span&gt;
&lt;span id=&quot;cb13-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;mlookup (&lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; mn) n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; mlookup mn n &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (m, a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;FS&lt;/span&gt; m, a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which you will happily note follows the same logic we worked through for &lt;code&gt;triad&lt;/code&gt;, and is exactly the function we want to use for making &lt;code&gt;Scale&lt;/code&gt;s.&lt;/p&gt;
&lt;p&gt;We can also go the other way, and search on the &lt;code&gt;Fin m&lt;/code&gt; indices:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb14&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb14-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;msearch ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; m n a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; n, a)&lt;/span&gt;
&lt;span id=&quot;cb14-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;msearch &lt;span class=&quot;dt&quot;&gt;MNil&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; a &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;msearch (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; a _) &lt;span class=&quot;dt&quot;&gt;FZ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;FZ&lt;/span&gt;, a)&lt;/span&gt;
&lt;span id=&quot;cb14-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;msearch (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; _ as) (&lt;span class=&quot;dt&quot;&gt;FS&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; (first &lt;span class=&quot;dt&quot;&gt;FS&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; msearch as n&lt;/span&gt;
&lt;span id=&quot;cb14-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;msearch (&lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; _) &lt;span class=&quot;dt&quot;&gt;FZ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;msearch (&lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; as) (&lt;span class=&quot;dt&quot;&gt;FS&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; msearch as n&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which I’m not super sure is important, but it was fun to write.&lt;/p&gt;
&lt;p&gt;The real reason for calling this thing &lt;code&gt;MVec&lt;/code&gt; was so that I could put &lt;code&gt;m&lt;/code&gt;s in front of all its functions’ names. Which finally pays off when we get to the &lt;code&gt;MVec&lt;/code&gt; composition function:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb15&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb15-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;malign&lt;/span&gt;
&lt;span id=&quot;cb15-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  ::&lt;/span&gt; (x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; z)&lt;/span&gt;
&lt;span id=&quot;cb15-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; c b x&lt;/span&gt;
&lt;span id=&quot;cb15-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; b a y&lt;/span&gt;
&lt;span id=&quot;cb15-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; c a z&lt;/span&gt;
&lt;span id=&quot;cb15-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;malign _ &lt;span class=&quot;dt&quot;&gt;MNil&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;malign f (&lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; bc) ab &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; malign f bc ab&lt;/span&gt;
&lt;span id=&quot;cb15-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;malign f (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; x bc) (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; y ab) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; (f x y) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; malign f bc ab&lt;/span&gt;
&lt;span id=&quot;cb15-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;malign f (&lt;span class=&quot;dt&quot;&gt;MCons&lt;/span&gt; _ bc) (&lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; ab) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MSkip&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; malign f bc ab&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This function does end-wise composition of &lt;code&gt;MVec&lt;/code&gt;s. If you have one &lt;code&gt;MVec&lt;/code&gt; that has &lt;code&gt;c&lt;/code&gt; slots and &lt;code&gt;b&lt;/code&gt; items, and another which has &lt;code&gt;b&lt;/code&gt; slots and &lt;code&gt;a&lt;/code&gt; items (sorry for the type variable shuffling!), then we ought to be able to make an &lt;code&gt;MVec&lt;/code&gt; with &lt;code&gt;c&lt;/code&gt; slots and only &lt;code&gt;a&lt;/code&gt; items. This corresponds to composition of scales, so it’s really quite important!&lt;/p&gt;
&lt;p&gt;The trick is we traverse over the b-in-c collection. Whenever we find an item there, we consider it to be a potential slot of the a-in-b collection. If a-in-b says to skip, then we skip it. But if they’re both cons cells, we can use the given function to combine their values into a &lt;code&gt;z&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Quite nice if you ask me.&lt;/p&gt;
&lt;h2 id=&quot;back-to-scales&quot;&gt;Back To Scales&lt;/h2&gt;
&lt;p&gt;Now that we have &lt;code&gt;MVec&lt;/code&gt;s, it’s pretty trivial to implement the small-to-big scale mapping that we’ve been thinking about. Really we just need to keep an &lt;code&gt;MVec m n&lt;/code&gt; around, convert it into the old representation via &lt;code&gt;mlookup&lt;/code&gt;, and then all of the last post’s machinery around scales works just fine.&lt;/p&gt;
&lt;p&gt;Except, how do you invert them? Remember that we did all of this work to invert them. If an &lt;code&gt;MVec m n&lt;/code&gt; has &lt;code&gt;m&lt;/code&gt; slots of which &lt;code&gt;n&lt;/code&gt; are full, it necessarily means that &lt;code&gt;n &amp;lt;= m&lt;/code&gt;. Which is to say that it’s slightly more complicated than just keeping a &lt;code&gt;MVec m n&lt;/code&gt; around.&lt;/p&gt;
&lt;p&gt;The trick of course is that if you invert a scale, &lt;em&gt;it’s still the same &lt;code&gt;MVec&lt;/code&gt; you need!&lt;/em&gt; Thus a &lt;code&gt;Scale a b&lt;/code&gt; is either an embedding from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;b&lt;/code&gt;, or a projection from &lt;code&gt;b&lt;/code&gt; back to &lt;code&gt;a&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb16&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb16-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Embed&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;   ::&lt;/span&gt;                 &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; b a (&lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b&lt;/span&gt;
&lt;span id=&quot;cb16-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Project&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Quantization&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; b a (&lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; b a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that it’s the same &lt;code&gt;MVec&lt;/code&gt; in both constructors, the only difference is which direction the scale goes. This &lt;code&gt;Quantization&lt;/code&gt; thing is a little enum:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb17&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb17-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb17-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Quantization&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Below&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Above&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nearest&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which corresponds exactly to the three &lt;code&gt;invertScale*&lt;/code&gt; functions from last time. Really it means “if you’re looking at a scale degree which doesn’t fit, which note should we add offsets to?” I don’t think I like having this field, and when I’m back from Japan I’ll explore just sticking the chooser in there.&lt;/p&gt;
&lt;p&gt;Once we’ve added &lt;code&gt;Project&lt;/code&gt;, however, I don’t think scales compose anymore. Not to fear, we’ll just &lt;em&gt;make&lt;/em&gt; them compose by adding a new constructor:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb18&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb18-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb18-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Embed&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;   ::&lt;/span&gt;                 &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; b a (&lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b&lt;/span&gt;
&lt;span id=&quot;cb18-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Project&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Quantization&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MVec&lt;/span&gt; b a (&lt;span class=&quot;dt&quot;&gt;Accidentals&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; b a&lt;/span&gt;
&lt;span id=&quot;cb18-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Compose&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; x b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a x                     &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Composition of scales tries to &lt;code&gt;malign&lt;/code&gt; when possible:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb19&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb19-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;compScale ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; b c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a c&lt;/span&gt;
&lt;span id=&quot;cb19-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;compScale (&lt;span class=&quot;dt&quot;&gt;Embed&lt;/span&gt; bc) (&lt;span class=&quot;dt&quot;&gt;Embed&lt;/span&gt; ab) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Embed&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; malign (&lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;) bc ab&lt;/span&gt;
&lt;span id=&quot;cb19-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;compScale bc ab &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Compose&lt;/span&gt; bc ab&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and inversion is now a little swap-er-oo of our constructors:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb20&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb20-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;invertScale ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Quantization&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; b a&lt;/span&gt;
&lt;span id=&quot;cb20-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;invertScale q (&lt;span class=&quot;dt&quot;&gt;Embed&lt;/span&gt; slots) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Project&lt;/span&gt; q slots&lt;/span&gt;
&lt;span id=&quot;cb20-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;invertScale _ (&lt;span class=&quot;dt&quot;&gt;Project&lt;/span&gt; _ slots) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Embed&lt;/span&gt; slots&lt;/span&gt;
&lt;span id=&quot;cb20-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;invertScale q (&lt;span class=&quot;dt&quot;&gt;Compose&lt;/span&gt; f g) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Compose&lt;/span&gt; (invertScale q g) (invertScale q f)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So that’s the thrust of today’s work. Having sorted out all of these details has dramatically simplified up the codebase (and found a bunch of bugs), which I always consider to be a harbinger of being on the right track. Making any of this stuff usable is its own exercise in existential types and type reflection shenanigans, but maybe we’ll cover that another day if there’s sufficient interest.&lt;/p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;Well technically it works because there are at least 3 elements…&lt;a class=&quot;footnote-back&quot; href=&quot;https://reasonablypolymorphic.com#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Thu, 19 Nov 2026 21:55:00 +0000</pubDate>
</item>
<item>
	<title>Mark Jason Dominus: Google Chrome dev team declares bug report bankruptcy</title>
	<guid isPermaLink="false">tag:,2026:/tech/bug-reports</guid>
	<link>https://blog.plover.com/tech/bug-reports.html</link>
	<description>&lt;p&gt;[ I  wrote this in August 2012 and then forgot about it. ]&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;About 18 months ago I filed a report for a bug in Google Chrome. It
wasn't a very important bug. I reported that when Chrome was asked to
inline an image, and the image turned out to be in the obscure and
seldom-used X-bitmap format, it came up as a &quot;broken image&quot; icon
(which isn't a bug) and failed to display the &lt;tt&gt;alt&lt;/tt&gt; text from
the &lt;tt&gt;img&lt;/tt&gt; tag (which is).  &lt;/p&gt;&lt;p&gt;

I then forgot about it until this week, when I got a notice that the
report was being closed because it was &quot;obsolete&quot;.  I was invited to
resubmit the bug report if I considered it valid.&lt;/p&gt;&lt;p&gt;

My first draft of this article continued politely &quot;The Chrome dev team
botched this in a number of ways.&quot;  That draft came out boring, weak,
and vague, so I'm going to try continuing a different way.&lt;/p&gt;&lt;p&gt;

That was a really jerky thing to do.  I put in my time and trouble to
construct a clear bug report with an easy test case; the bug report
contained a link to &lt;a href=&quot;http://perl.plover.com/NPC/test.html&quot;&gt;a
page that demonstrated the problem&lt;/a&gt;.  To see if the problem is still
present, all you have to do is click the link in the bug report.&lt;/p&gt;&lt;p&gt;

But after I spent my time and effort to submit this bug report, Chrome
dev decided to ignore it.  After they had ignored it for a year and a
half, they decided to pitch it in the trash.  But if I want, I can
submit it again.&lt;/p&gt;&lt;p&gt;

Now why on earth would I want to do that?  Does Chrome think I enjoy
assembling bug reports?  Maybe I do it for fun?  No.  The bug report
has no value to me.  It was an expense. It was a donation to someone I
thought would appreciate it. &lt;/p&gt;&lt;p&gt;

Suppose my neighbor has a boat.  Sometimes he lets me use the boat,
but it leaks. One day he asks if I will help me fix the boat.  I spend
the day in his garage painting and sealing some new boards to put into
the bottom of the boat.  Some time later, I see he has put the new
boards into the trash.  &quot;What did you do that for?&quot; I ask.  He shrugs.
&quot;I don't really care if the boat leaks,&quot; he says. &quot;But if you want to
come over and paint and seal some more boards, how about next week?&quot;&lt;/p&gt;&lt;p&gt;

Sorry, what am I supposed to get out of this transaction? &lt;/p&gt;&lt;p&gt;

Chrome dev says they have 40,000 bug reports, too many to deal with.
So what?  Autoclosing them isn't dealing with them either; it's
throwing them in the trash.  If your project doesn't want to get bug
reports, you don't have to solicit them.&lt;/p&gt;&lt;p&gt;

Chrome says that a lot of the reports are useless.  I'm sure that's
true.  Most people don't file very good bug reports.  A few people
do.  Who gets pissed off when you throw all the reports into the
trash?  The people who put in the most effort, that's who.  So if
you're trying to adopt a strategy that will ensure that your
&lt;i&gt;next&lt;/i&gt; 40,000 reports are even worse, this is the strategy to
adopt. &lt;/p&gt;&lt;p&gt;

What should Chrome have done here?  There is an easy thing and a hard
thing.&lt;/p&gt;&lt;p&gt;

The easy thing not to say &quot;We threw away your report, but if you think
it was a good one, please start over and resubmit it,&quot; which is likely
to piss off anyone with any sense.  Instead, you stick a button on the
web site and say &quot;We threw away your report, but if you think it was a
good one, please click the button.&quot;  I would not object to checking if
the report was still valid, and clicking a button to reactivate it.&lt;/p&gt;&lt;p&gt;

The harder thing that they should have done was to not close the
reports at all.  Managing bug reports is hard.  Perl was years behind
on bug reports for many years.  Many of those reports were indeed
obsolete.  But there is no cost to keeping them around.&lt;/p&gt;&lt;p&gt;
&lt;/p&gt;&lt;hr /&gt;

&lt;h2&gt;Addenda&lt;/h2&gt;

&lt;h3&gt;20261119&lt;/h3&gt;

&lt;ol&gt;&lt;li&gt;A Mastodon colleague was complaining today about a similar
practice, and I wanted to refer him to this article, but couldn't
because I had completely forgotten to publish it.

&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://www.jwz.org/doc/cadt.html&quot;&gt;JWZ complained about
something similar in 2003&lt;/a&gt;.
&lt;/li&gt;&lt;/ol&gt;

&lt;h3&gt;20261120&lt;/h3&gt;

Paul Irish dug up &lt;a href=&quot;https://issues.chromium.org/issues/40502297&quot;&gt;the original bug
report that was the subject of this complaint&lt;/a&gt;.  (And discovered
that he himself had replied to it at the time!)&lt;p&gt;&lt;/p&gt;</description>
	<pubDate>Wed, 18 Nov 2026 12:00:00 +0000</pubDate>
	<author>mjd@plover.com (Mark Dominus)</author>
</item>
<item>
	<title>Don Stewart (dons): AI-assisted code at scale: provenance, quality, centrality and review time</title>
	<guid isPermaLink="false">http://donsbot.com/?p=1103</guid>
	<link>https://donsbot.com/2026/11/17/ai-assisted-code-at-scale-provenance-quality-centrality-and-review-time/</link>
	<description>&lt;p&gt;A rare paper publication from me, the first for a while:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;A Preliminary Analysis of the Impact of AI Assisted/Generated Code on Quality, Centrality and Review Time at Scale&lt;/em&gt; — Audris Mockus, Peter C. Rigby, Don Stewart, Chandra Maddila, and Nachiappan Nagappan. ESEM 2026, Software Engineering in Practice track, Munich, 8–9 Oct 2026. LIPIcs vol. 394, pp. 93:1–93:23.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://drops.dagstuhl.de/storage/00lipics/lipics-vol394-esem2026/LIPIcs.ESEM.2026.93/LIPIcs.ESEM.2026.93.pdf&quot;&gt;PDF&lt;/a&gt; — &lt;a href=&quot;https://doi.org/10.4230/LIPIcs.ESEM.2026.93&quot;&gt;DOI: 10.4230/LIPIcs.ESEM.2026.93&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is the first published peer-reviewed piece on the code provenance system I’ve been working on for the last two years. The core idea: track every character’s provenance. Identify AI authorship through the characters changed in files, and track the change through source control — using accurate filesystem and source-control-based analysis, not heuristics.&lt;/p&gt;
&lt;p&gt;It’s a lot more sophisticated now, but the core idea is there.&lt;/p&gt;
&lt;p&gt;What we found, across 10,000+ developers and 400,000 diffs at Meta: AI-assisted code lands more in less central code, in larger changes, in test files, and from higher-tenure authors. Review time goes down and severe-incident rates are lower for diffs with AI code — though both are confounded by the fact that AI code shows up in less central contexts to begin with.&lt;/p&gt;
&lt;p&gt;The paper is the first published documentation of this work.&lt;/p&gt;</description>
	<pubDate>Wed, 18 Nov 2026 01:53:42 +0000</pubDate>
</item>
<item>
	<title>Bryan O'Sullivan: Announcing jj-musubi: live sync for Jujutsu</title>
	<guid isPermaLink="true">https://www.serpentine.com/posts/2026/announcing-jj-musubi/</guid>
	<link>https://www.serpentine.com/posts/2026/announcing-jj-musubi/</link>
	<description>Pick up unfinished Jujutsu work live, on another machine, without temporary commit or pushing a branch.</description>
	<pubDate>Wed, 18 Nov 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>GHC Developer Blog: GHC 9.12.5-rc5 is now available</title>
	<guid isPermaLink="true">http://haskell.org/ghc/blog/20261118-ghc-9.12.5-rc5-released.html</guid>
	<link>http://haskell.org/ghc/blog/20261118-ghc-9.12.5-rc5-released.html</link>
	<description>&lt;h1&gt;GHC 9.12.5-rc5 is now available&lt;/h1&gt;
&lt;h4 class=&quot;text-muted&quot;&gt;mangoiv - 2026-11-18&lt;/h4&gt;

&lt;p&gt;The GHC developers are very pleased to announce the availability
of the fifth release candidate for GHC 9.12.5, ghc-9.12.5-rc5.&lt;/p&gt;
&lt;p&gt;The fifth release candidate was necessary due to a regression in the RTS involving blackholing.&lt;/p&gt;
&lt;p&gt;This will be the last release candidate before release if no further critical regressions are discovered in this RC – we’re so far past due that we don’t backport anymore bugfixes for this release.&lt;/p&gt;
&lt;p&gt;See also the &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/17014&quot;&gt;fixing MR.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;See also the previous announcement of 9.12.5-rc4: &lt;a href=&quot;https://www.haskell.org/ghc/blog/20261102-ghc-9.12.5-rc4-released.html&quot;&gt;https://www.haskell.org/ghc/blog/20261102-ghc-9.12.5-rc4-released.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Binary distributions, source distributions, and documentation are available at
&lt;a href=&quot;https://downloads.haskell.org/~ghc/9.12.5-rc5/&quot;&gt;downloads.haskell.org&lt;/a&gt; and via &lt;a href=&quot;https://www.haskell.org/ghcup/&quot;&gt;GHCup&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This release candidate will have a two-week testing period. If all goes well
the final release will be available the week of 6 December 2026.&lt;/p&gt;
&lt;p&gt;GHC 9.12.5 is a bug-fix release fixing many issues of a variety of
severities and scopes, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Implement version 2 of the ghc &lt;code&gt;semaphore&lt;/code&gt; protocol for GHC’s &lt;code&gt;-jsem&lt;/code&gt; jobserver on Linux and other &lt;code&gt;POSIX&lt;/code&gt; platforms (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25087&quot;&gt;&lt;span&gt;#25087&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27253&quot;&gt;&lt;span&gt;#27253&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;several improvements to the pattern match checker (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25926&quot;&gt;&lt;span&gt;#25926&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27124&quot;&gt;&lt;span&gt;#27124&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;several improvements to the demand analyser (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27261&quot;&gt;&lt;span&gt;#27261&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27106&quot;&gt;&lt;span&gt;#27106&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26416&quot;&gt;&lt;span&gt;#26416&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;several fixes regarding the interaction of ticks and coercions (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27121&quot;&gt;&lt;span&gt;#27121&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26929&quot;&gt;&lt;span&gt;#26929&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26642&quot;&gt;&lt;span&gt;#26642&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26693&quot;&gt;&lt;span&gt;#26693&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;several fixes for blackholes (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27261&quot;&gt;&lt;span&gt;#27261&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;… and many more&lt;/p&gt;
&lt;p&gt;Mind that the very important overhaul of the semaphore-compat library to v2 is included in this release and that the semaphore feature will only start working with cabal-install 3.18 again, which is sadly a breaking but necessary change.&lt;/p&gt;
&lt;p&gt;A full accounting of these fixes can be found in the
release notes. As always, GHC’s release status, including planned future
releases, can be found on the GHC Wiki status.&lt;/p&gt;
&lt;p&gt;GHC development is sponsored by:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://juspay.com/&quot;&gt;Juspay&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://qbaylogic.com/&quot;&gt;QBayLogic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.channable.com/&quot;&gt;Channable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://haskell.foundation/&quot;&gt;Haskell Foundation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://serokell.io/&quot;&gt;Serokell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://well-typed.com/&quot;&gt;Well-Typed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.tweag.io/&quot;&gt;Tweag&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.dotcom-monitor.com/&quot;&gt;Dotcom-Monitor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.loadview-testing.com/&quot;&gt;LoadView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://webhostingbuddy.com/&quot;&gt;Web Hosting Buddy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.findmyelectric.com/&quot;&gt;Find My Electric&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.sc.com&quot;&gt;Standard Chartered&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://upcloud.com&quot;&gt;UpCloud&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mercury.com&quot;&gt;Mercury&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We would like to thank these sponsors and other anonymous contributors
whose on-going financial and in-kind support has facilitated GHC maintenance
and release management over the years. Finally, this release would not have
been possible without the hundreds of open-source contributors whose work
comprise this release.&lt;/p&gt;
&lt;p&gt;As always, do give this release a try and open a ticket if you see
anything amiss.&lt;/p&gt;</description>
	<pubDate>Wed, 18 Nov 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Haskell Interlude: 89: Trevis Elser</title>
	<guid isPermaLink="false">Buzzsprout-20133949</guid>
	<link></link>
	<description>&lt;p&gt;Matti and Farhad are joined by Trevis Elser, chairman of the Stability Working Group, to talk about the working group, Haskell in industry, and what we can learn from senior Java engineers!&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://haskell.foundation/podcast/89/&quot;&gt;Episode links&lt;/a&gt;&lt;/p&gt;</description>
	<pubDate>Mon, 16 Nov 2026 19:00:00 +0000</pubDate>
        <enclosure url="https://www.buzzsprout.com/1817535/episodes/20133949-89-trevis-elser.mp3" length="41467538" type="audio/mpeg"/>
</item>
<item>
	<title>Sandy Maguire: Generalized Accidentals</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/accidentals/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/accidentals/index.html</link>
	<description>&lt;p&gt;Just a little note on yesterday’s &lt;a href=&quot;https://reasonablypolymorphic.com/blog/notes-scales/#some-failed-attempts-at-pinnning-down-scales&quot;&gt;ornamental offsets&lt;/a&gt;. I realized overnight that this idea is way less weird to musicians than originally thought. The offset idea is really just &lt;em&gt;generalized accidentals.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Check it out: standard music notation, without ever saying so, assumes a &lt;code&gt;Fin 7&lt;/code&gt; scale. The notation maps these degrees alternatingly between lines and spaces-between-lines. Thus &lt;code&gt;0..6 :: Deg 7&lt;/code&gt; look like this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;The Diatonic Scale&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out3.cropped.svg&quot; /&gt;&lt;figcaption&gt;The Diatonic Scale&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;which correspond to seven contiguous white keys on the piano. But the piano also has five black keys between any seven contiguous white keys. Adding in the black keys gets us an additional five pitch classes, which brings us up to the &lt;code&gt;Fin 12&lt;/code&gt; scale that Western music is based around.&lt;/p&gt;
&lt;p&gt;Modulo some technicalities, there’s no way to notate these black keys directly. The black key between C and D is known either as “C sharp” or “D flat” — sharps moving one offset up in &lt;code&gt;Deg 12&lt;/code&gt;, and flats moving one offset down. Thus the twelve pitch classes of the chromatic scale are notated either as sharps:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Chromatic Sharps&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out4.cropped.svg&quot; /&gt;&lt;figcaption&gt;Chromatic Sharps&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;or equivalently, as flats:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Chromatic Flats&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out2.cropped.svg&quot; /&gt;&lt;figcaption&gt;Chromatic Flats&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Although it is usually omitted, there is also a “natural” symbol, to indicate the &lt;em&gt;absence&lt;/em&gt; of a sharp or a flat. If we include these symbols into our sharps example, we get this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Chromatic Naturals and Sharps&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out5.cropped.svg&quot; /&gt;&lt;figcaption&gt;Chromatic Naturals and Sharps&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;which represents exactly the same thing as the “Chromatic Sharps” figure. We’re just being a little more pedantic here.&lt;/p&gt;
&lt;p&gt;To drive everything home, these accidental symbols are &lt;em&gt;exactly&lt;/em&gt; the &lt;code&gt;Deg 12&lt;/code&gt; ornaments on a &lt;code&gt;Note 7&lt;/code&gt; that we were investigating last time. If we change the &lt;code&gt;♮&lt;/code&gt; symbol to &lt;code&gt;0&lt;/code&gt;, the &lt;code&gt;♯&lt;/code&gt; to &lt;code&gt;+1&lt;/code&gt; and the &lt;code&gt;♭&lt;/code&gt; to &lt;code&gt;-1&lt;/code&gt;, our chromatic scales look like this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Explicit Accidental Sharps&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out6.cropped.svg&quot; /&gt;&lt;figcaption&gt;Explicit Accidental Sharps&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;figure&gt;
&lt;img alt=&quot;Explicit Accidental Flats&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out8.cropped.svg&quot; /&gt;&lt;figcaption&gt;Explicit Accidental Flats&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Both examples now have “obvious” encodings as &lt;code&gt;Note 7&lt;/code&gt;s: use the notated pitch as the &lt;code&gt;Deg 7&lt;/code&gt;, and the offset as a &lt;code&gt;stepIn @12&lt;/code&gt;. Thus “Explicit Accidental Sharps” is encoded as:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[ &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, stepIn &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, stepIn &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, stepIn &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, stepIn &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, stepIn &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;, &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-13&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;]&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Rather unsurprisingly, when we &lt;code&gt;applyScale diatonic&lt;/code&gt; over this collection, we get &lt;code&gt;[0..11] :: Note 12&lt;/code&gt;, which is just to say that this enumerates all of the pitch classes in the chromatic scale. And of course it does, because we designed it that way in the first place!&lt;/p&gt;
&lt;p&gt;So this all brings us back to yesterday’s &lt;code&gt;Ornaments&lt;/code&gt; type, which allows us to express offsets at &lt;em&gt;any&lt;/em&gt; scale degree — not just &lt;code&gt;Deg 12&lt;/code&gt; like sharps and flats in standard musical notation allow us to target. So let’s broaden our visualization a little more, making it explicit that our chromatic offsets are in &lt;code&gt;Deg 12&lt;/code&gt;:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Deg 12 Explicit Accidentals&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out9.cropped.svg&quot; /&gt;&lt;figcaption&gt;Deg 12 Explicit Accidentals&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Now that this has been made explicit, there’s no reason why we can’t add &lt;em&gt;multiple&lt;/em&gt; accidentals to any given note. For example, we could make put a &lt;code&gt;stepIn @10 2&lt;/code&gt; on the second note, &lt;em&gt;in addition to&lt;/em&gt; a &lt;code&gt;stepsIn @12 (-1)&lt;/code&gt;!&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Mixing Accidentals&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out10.cropped.svg&quot; /&gt;&lt;figcaption&gt;Mixing Accidentals&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;This one says that if we were to map our &lt;code&gt;Note 7&lt;/code&gt; into &lt;code&gt;Note 10&lt;/code&gt;, we’d move the second note up by &lt;code&gt;2 :: Deg 10&lt;/code&gt;. Then when we mapped the resulting &lt;code&gt;Note 10&lt;/code&gt;s into &lt;code&gt;Note 12&lt;/code&gt;, we’d move that note again — this time down by &lt;code&gt;-1 :: Deg 12&lt;/code&gt;.&lt;/p&gt;




























































&lt;p&gt;So the ideas here really &lt;em&gt;are&lt;/em&gt; familiar to musicians — the standard notation just makes it hard to see what it is we’re talking about.&lt;/p&gt;
&lt;p&gt;To give a bit more of a musical example, we can give a little triadic motif:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Triadic Motif&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out11.cropped.svg&quot; /&gt;&lt;figcaption&gt;Triadic Motif&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Notice that the third note here is one &lt;code&gt;Deg 7&lt;/code&gt; above the &lt;code&gt;Note 3&lt;/code&gt;, while the fourth note is one &lt;code&gt;Deg 12&lt;/code&gt; &lt;em&gt;below&lt;/em&gt; the &lt;code&gt;Note 3&lt;/code&gt;. In musical terms, we’re arpeggiating a triad, but inserting a diatonic note above the “third” and a chromatic note below the “fifth” (scare quotes because the thirds and fifths move!)&lt;/p&gt;
&lt;p&gt;If we then repeat this motif twice more, each time one &lt;code&gt;Deg 3&lt;/code&gt; up, we get this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Triadic Motif Sequence&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out12.cropped.svg&quot; /&gt;&lt;figcaption&gt;Triadic Motif Sequence&lt;/figcaption&gt;
&lt;/figure&gt;



















































&lt;p&gt;Finally, mapping this sequence into standard notation (assuming we map via &lt;code&gt;triad&lt;/code&gt; and &lt;code&gt;diatonic&lt;/code&gt;) produces this rather pleasing melody:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Melody&quot; src=&quot;https://reasonablypolymorphic.com/images/music8/out13.cropped.svg&quot; /&gt;&lt;figcaption&gt;Melody&lt;/figcaption&gt;
&lt;/figure&gt;








&lt;audio controls=&quot;&quot;&gt;
&lt;source src=&quot;https://reasonablypolymorphic.com/images/music8/melody.mp3&quot; type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;p&gt;Notice how what was a completely regular pattern in &lt;code&gt;Note 3&lt;/code&gt; expands out unevenly each time around, producing interesting motific development.&lt;/p&gt;
&lt;p&gt;Hopefully this gives a taste of what all that &lt;code&gt;Note&lt;/code&gt; and &lt;code&gt;Ornaments&lt;/code&gt; nonsense from yesterday is all about. Not only are these ideas already familiar to musicians, they also immediately produce musically interesting results!&lt;/p&gt;</description>
	<pubDate>Mon, 16 Nov 2026 11:09:00 +0000</pubDate>
</item>
<item>
	<title>Sandy Maguire: Musical Notes and Scales</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/notes-scales/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/notes-scales/index.html</link>
	<description>&lt;p&gt;Sorry for the delay. Everyone in my household came down with egregious sicknesses. And I had a bunch of unsatisfying things in my music theory that needed to get worked out. But we’re mostly better and the theoretical issues are resolved, so we’re BACK baby.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Over the last two posts we discussed &lt;a href=&quot;https://reasonablypolymorphic.com/blog/pitches-1/&quot;&gt;abstract musical pitches&lt;/a&gt; and (some) &lt;a href=&quot;https://reasonablypolymorphic.com/blog/voice-leadings/&quot;&gt;efficient voice leadings&lt;/a&gt; which help move us smoothly between different configurations in musical space.&lt;/p&gt;
&lt;p&gt;It’s been all very interesting &lt;em&gt;musically&lt;/em&gt;, but you must admit there hasn’t been much in the way of functional programming going on. Today we’re going to get back to brass tacks and see about making all of these notions precise, compositional, and fun to work with.&lt;/p&gt;
&lt;p&gt;Where we left things is with a &lt;code&gt;Fin n&lt;/code&gt; type:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt;  &lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; n          &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Enum&lt;/span&gt;    (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; n)&lt;/span&gt;
&lt;span id=&quot;cb1-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; n, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bounded&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; n)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which has exactly &lt;code&gt;n&lt;/code&gt; distinct values. We then made a &lt;code&gt;Deg n&lt;/code&gt; type, whose semantics are “exactly one of &lt;code&gt;n&lt;/code&gt; distinct values, with an octave component.” We represent this implicitly by way of an &lt;code&gt;Int&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  {&lt;span class=&quot;ot&quot;&gt; getDeg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;
&lt;span id=&quot;cb2-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;dt&quot;&gt;Generic&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb2-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Num&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Enum&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Real&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Integral&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and give its semantics by way of a function:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;degToPC ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; n)&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;degToPC (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; d) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; (oct, pc) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;divMod&lt;/span&gt; d n&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; (oct, &lt;span class=&quot;fu&quot;&gt;toEnum&lt;/span&gt; pc)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where the output &lt;code&gt;Int&lt;/code&gt; is which octave the degree ended up in.&lt;/p&gt;
&lt;p&gt;We have then been discussing “scales” as mappings of the form &lt;code&gt;Fin m -&amp;gt; Fin n&lt;/code&gt;. These have always been in scare quotes because this is not really how musicians traditionally think about scales! Whatever this mapping concept &lt;em&gt;is,&lt;/em&gt; it doesn’t really have a standard name in musical vocabulary. But scales are the closest thing semantically, so we’re going to just shadow that name and go about our business.&lt;/p&gt;
&lt;p&gt;It’s a bit of a tricky line to balance. So whenever we’re talking about the loose musical idea, we’ll use the word “scale.” And whenever we mean this mapping, we’ll go with &lt;code&gt;Scale&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;some-failed-attempts-at-pinnning-down-scales&quot;&gt;Some Failed Attempts at Pinnning Down Scales&lt;/h2&gt;
&lt;p&gt;However, it’s been a bit of a simplification to say a &lt;code&gt;Scale&lt;/code&gt; is a mapping &lt;code&gt;Fin n -&amp;gt; Fin m&lt;/code&gt;. We’d like our &lt;code&gt;Scale&lt;/code&gt;s to be &lt;em&gt;monotone mappings,&lt;/em&gt; which is to say that if &lt;code&gt;i :: Deg m &amp;lt;= j :: Deg m&lt;/code&gt;, then &lt;code&gt;(applyScale sc i) :: Deg n &amp;lt;= (applyScale sc j) :: Deg n&lt;/code&gt;. Enforcing monotonicity on &lt;code&gt;Fin&lt;/code&gt;-to-&lt;code&gt;Fin&lt;/code&gt; mappings is possible, but it requires us to preserve the ordering of pitch classes &lt;em&gt;within a single octave.&lt;/em&gt; That is to say, it means we can’t do any sort of permutation.&lt;/p&gt;
&lt;p&gt;Thus, perhaps we should think about &lt;code&gt;Scale&lt;/code&gt;s as mappings &lt;code&gt;Fin m -&amp;gt; Deg n&lt;/code&gt;. This is much closer to what we want (although it has some shortcomings that we will see shortly.) But let’s move forward with this attempt in order to motivate some of the later decisions:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; m n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can introduce a new scale by way of &lt;code&gt;mkScale&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;mkScale ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; m, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; m) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; m n&lt;/span&gt;
&lt;span id=&quot;cb5-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;mkScale f &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; image &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        m &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;maxBound&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;(&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m)]&lt;/span&gt;
&lt;span id=&quot;cb5-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; f m&lt;/span&gt;
&lt;span id=&quot;cb5-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \m &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; image &lt;span class=&quot;op&quot;&gt;!!&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromEnum&lt;/span&gt; m&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which enumerates all values in &lt;code&gt;f&lt;/code&gt;, sorts them (in order to ensure monotonicity), and then builds a &lt;code&gt;Scale&lt;/code&gt; that uses the &lt;code&gt;Fin m&lt;/code&gt; to select from the sorted values.&lt;/p&gt;
&lt;p&gt;We can apply a scale to a &lt;code&gt;Deg&lt;/code&gt; by way of &lt;code&gt;applyScale&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;applyScale&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; m n&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; m n&lt;/span&gt;
&lt;span id=&quot;cb6-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; m&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb6-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;applyScale (&lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; f) (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; dm) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; (oct, pc) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;divMod&lt;/span&gt; dm &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;m&lt;/span&gt;
&lt;span id=&quot;cb6-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; dn &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;toEnum&lt;/span&gt; pc&lt;/span&gt;
&lt;span id=&quot;cb6-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; (dn &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; oct &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which ensures we preserve the necessary semantics of &lt;code&gt;degToPC&lt;/code&gt; given earlier.&lt;/p&gt;
&lt;p&gt;All is so far well and good. But not for long.&lt;/p&gt;
&lt;p&gt;In some cases we might want to reverse a &lt;code&gt;Scale&lt;/code&gt;’s mapping, eg going from &lt;code&gt;Deg 7&lt;/code&gt; to &lt;code&gt;Deg 3&lt;/code&gt;. This makes a lot of sense musically. For example, we might want to include a &lt;a href=&quot;https://en.wikipedia.org/wiki/Nonchord_tone#Neighbor_tone&quot;&gt;neighbor note&lt;/a&gt;, or a &lt;a href=&quot;https://en.wikipedia.org/wiki/Nonchord_tone#Passing_tone&quot;&gt;passing tone&lt;/a&gt; when writing out otherwise-triadic music. More directly, projecting down to a smaller abstract pitch class is the exact mechanism of &lt;a href=&quot;https://en.wikipedia.org/wiki/Tintinnabuli&quot;&gt;tintinnabuli&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Unfortunately however, reversing a mapping &lt;code&gt;Fin m -&amp;gt; Deg n&lt;/code&gt; is obvious hogwash from the perspective of our functional programming ivory tower. In the usual case that &lt;code&gt;m &amp;lt;= n&lt;/code&gt;, this is a very non-injective function, so we have no chance of turning it around.&lt;/p&gt;
&lt;p&gt;And yet, it’s musically meaningful. So, regardless of whether we &lt;em&gt;can&lt;/em&gt; do it in Haskell, we somehow &lt;strong&gt;must.&lt;/strong&gt; Otherwise whatever it is we’ve built is modeling the wrong thing.&lt;/p&gt;
&lt;p&gt;Our mental pattern matchers that say “you can’t reverse a non-bijective function” are absolutely correct. But it’s worth taking a moment to think about &lt;em&gt;why exactly&lt;/em&gt; we can’t in this case. Consider the musically-common case of working in a &lt;code&gt;Scale 3 7 = Fin 3 -&amp;gt; Deg 7&lt;/code&gt;. Here, some of the &lt;code&gt;Deg 7&lt;/code&gt;s are targeted by the &lt;code&gt;Fin 3&lt;/code&gt;. In particular, &lt;code&gt;0, 2, 4 :: Deg 7&lt;/code&gt; get targeted by the &lt;code&gt;triad&lt;/code&gt; scale introduced in a previous post.&lt;/p&gt;
&lt;p&gt;So reversing &lt;code&gt;0, 2, 4 :: Fin 7&lt;/code&gt; is easy enough — we just send them to &lt;code&gt;0, 1, 2 :: Deg 3&lt;/code&gt;. The hard part is deciding where ought we send &lt;code&gt;1, 3, 5, 6 :: Fin 7&lt;/code&gt;. Well, why don’t we just send them to &lt;em&gt;themselves&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;Sounds a little crazy, I know, but check this out. If we could attach &lt;em&gt;other scalar degree offsets&lt;/em&gt; to each note, we could invert a scale by mapping the image directly to the domain whenever possible. And when it’s not possible, we could represent it as an offset from the nearest thing in the domain.&lt;/p&gt;
&lt;p&gt;A little example might help. Say we have &lt;code&gt;0..7 :: Fin 7&lt;/code&gt; and we’d like to reverse them across a &lt;code&gt;Scale 3 7&lt;/code&gt;. If we represent exact points in &lt;code&gt;Deg 3&lt;/code&gt; as white note heads, and “offsets” as black note heads, one possible reverse mapping looks like this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Offsets Above&quot; src=&quot;https://reasonablypolymorphic.com/images/music7/out1.cropped.svg&quot; /&gt;&lt;figcaption&gt;Offsets Above&lt;/figcaption&gt;
&lt;/figure&gt;





















&lt;p&gt;If we assume we have some operation &lt;code&gt;(/+) :: Fin 3 -&amp;gt; Deg 7 -&amp;gt; Note 3&lt;/code&gt;, this diagram corresponds to:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;/+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;/+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;/+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;/+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Of course, such a mapping is not unique. For example, we can also do a “round up, offset down” approach:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Offsets Below&quot; src=&quot;https://reasonablypolymorphic.com/images/music7/out2.cropped.svg&quot; /&gt;&lt;figcaption&gt;Offsets Below&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;which is similarly:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;/-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;/-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;/-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;/-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All of this is to say that &lt;code&gt;Deg&lt;/code&gt;s aren’t quite what we’re looking for!&lt;/p&gt;
&lt;h2 id=&quot;enter-notes&quot;&gt;Enter Notes&lt;/h2&gt;
&lt;p&gt;As we’ve seen, &lt;code&gt;Deg&lt;/code&gt;s aren’t quite powerful enough to represent these offsets. So we’ll first need to make a type to hold the offsets. The closest standard-musical terminology here is “ornamentation,” so that’s what we’ll go with.&lt;/p&gt;
&lt;p&gt;An &lt;code&gt;Ornaments&lt;/code&gt; is a mapping from scalar sizes to offsets. Rather than keeping the scalar sizes around in the type system (as we have done for &lt;code&gt;Fin&lt;/code&gt; and &lt;code&gt;Deg&lt;/code&gt; and &lt;code&gt;Scale&lt;/code&gt;), here we’re going to simplify things down to an &lt;code&gt;IntMap&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  {&lt;span class=&quot;ot&quot;&gt; getOrnaments ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IntMap&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;
&lt;span id=&quot;cb9-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;dt&quot;&gt;Generic&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb9-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb9-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Semigroup&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Monoid&lt;/span&gt;) via &lt;span class=&quot;dt&quot;&gt;MonoidalIntMap&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Sum&lt;/span&gt; a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Two &lt;code&gt;Ornaments&lt;/code&gt; are equal when they have the same non-zero elements:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;normalizeOrnaments ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Num&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;normalizeOrnaments &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; IM.filter (&lt;span class=&quot;op&quot;&gt;/=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; getOrnaments&lt;/span&gt;
&lt;span id=&quot;cb10-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;Num&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; a) &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (&lt;span class=&quot;op&quot;&gt;==&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; on (&lt;span class=&quot;op&quot;&gt;==&lt;/span&gt;) (getOrnaments &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; normalizeOrnaments)&lt;/span&gt;
&lt;span id=&quot;cb10-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;Num&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; a) &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;compare&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; on &lt;span class=&quot;fu&quot;&gt;compare&lt;/span&gt; (getOrnaments &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; normalizeOrnaments)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can also introduce &lt;code&gt;Ornaments&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ornament ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; n a&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb11-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ornament &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; IM.singleton (scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and eliminate them:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decompOrnament&lt;/span&gt;
&lt;span id=&quot;cb12-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb12-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb12-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n)&lt;/span&gt;
&lt;span id=&quot;cb12-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decompOrnament o&lt;/span&gt;
&lt;span id=&quot;cb12-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;maybe&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (o, &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb12-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      ( (coerce (IM.filterWithKey &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; (\k _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; k &lt;span class=&quot;op&quot;&gt;/=&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n)) o, )&lt;/span&gt;
&lt;span id=&quot;cb12-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      )&lt;/span&gt;
&lt;span id=&quot;cb12-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; IM.lookup (scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n)&lt;/span&gt;
&lt;span id=&quot;cb12-13&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; getOrnaments o&lt;/span&gt;
&lt;span id=&quot;cb12-14&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-15&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;getOrnament ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; n&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb12-16&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb12-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;getOrnament &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;snd&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; decompOrnament&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here &lt;code&gt;decompOrnament @n&lt;/code&gt; returns the &lt;code&gt;Deg n&lt;/code&gt; ornament, and returns a new &lt;code&gt;Ornaments&lt;/code&gt; with that scale degree removed. If you don’t care about the removal, you’re welcome to call &lt;code&gt;getOrnament&lt;/code&gt; directly.&lt;/p&gt;
&lt;p&gt;Armed with &lt;code&gt;Ornaments&lt;/code&gt;, we can now make &lt;code&gt;Note&lt;/code&gt;, which wraps a degree with ornaments:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n) (&lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb13-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb13-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;dt&quot;&gt;Generic&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;Note&lt;/code&gt;s admit a &lt;code&gt;Num&lt;/code&gt; instance, which combines ornaments pointwise:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb14&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb14-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Num&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; n) &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;fromInteger&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;fromInteger&lt;/span&gt; n) &lt;span class=&quot;fu&quot;&gt;mempty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; a as &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; b bs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; b) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; as &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; bs&lt;/span&gt;
&lt;span id=&quot;cb14-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; a as &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; b bs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; b) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; as &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;negate&lt;/span&gt; bs&lt;/span&gt;
&lt;span id=&quot;cb14-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; a (&lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; as) &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; b (&lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; bs) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; b) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ornaments&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; IM.intersectionWith (&lt;span class=&quot;op&quot;&gt;*&lt;/span&gt;) as bs&lt;/span&gt;
&lt;span id=&quot;cb14-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt;    (&lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; a as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; a)    &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt;    as&lt;/span&gt;
&lt;span id=&quot;cb14-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;signum&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; a as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;signum&lt;/span&gt; a) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;signum&lt;/span&gt; as&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and we can add an ornamental offset to a &lt;code&gt;Note&lt;/code&gt; by way of &lt;code&gt;stepIn @c&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb15&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb15-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;stepIn ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; c n&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; c, &lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb15-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;stepIn k (&lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; a as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;c &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; k) as&lt;/span&gt;
&lt;span id=&quot;cb15-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb15-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; a &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; as &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ornament &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;c k&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;An invariant enforced by &lt;code&gt;stepIn&lt;/code&gt; is that you never have an ornament of size &lt;code&gt;n&lt;/code&gt; attached to a note also of size &lt;code&gt;n&lt;/code&gt;. If you attempt to, we just change the &lt;code&gt;Deg n&lt;/code&gt; instead.&lt;/p&gt;
&lt;h2 id=&quot;scales-revisited&quot;&gt;Scales Revisited&lt;/h2&gt;
&lt;p&gt;Now that we have &lt;code&gt;Note&lt;/code&gt;s, we can define &lt;code&gt;Scale&lt;/code&gt;s in terms of them, and we are rewarded with a convenient place to stick offsets when we invert scales. &lt;code&gt;Scale&lt;/code&gt; and &lt;code&gt;mkScale&lt;/code&gt; change only in that we reference &lt;code&gt;Note&lt;/code&gt; instead of &lt;code&gt;Deg&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb16&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb16-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nat&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; m n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; n)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb17&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb17-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb17-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;mkScale ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;KnownNat&lt;/span&gt; m, &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; m) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; m n&lt;/span&gt;
&lt;span id=&quot;cb17-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb17-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;mkScale f &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb17-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; image &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb17-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        m &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;maxBound&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;(&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m)]&lt;/span&gt;
&lt;span id=&quot;cb17-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb17-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; f m&lt;/span&gt;
&lt;span id=&quot;cb17-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb17-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \m &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; image &lt;span class=&quot;op&quot;&gt;!!&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromEnum&lt;/span&gt; m&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, &lt;code&gt;applyScale&lt;/code&gt; needs to do slightly more work: it must respect incoming and outgoing ornamentation. The &lt;code&gt;Note m&lt;/code&gt; might have a &lt;code&gt;n&lt;/code&gt;-sized ornament, in which case we must add it immediately. And then we must combine the remaining ornaments with any which might arise from the &lt;code&gt;Scale&lt;/code&gt;’s mapping:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb18&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb18-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;applyScale&lt;/span&gt;
&lt;span id=&quot;cb18-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; m n&lt;/span&gt;
&lt;span id=&quot;cb18-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; m n&lt;/span&gt;
&lt;span id=&quot;cb18-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; m&lt;/span&gt;
&lt;span id=&quot;cb18-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb18-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;applyScale (&lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; f) (&lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; dm) os) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb18-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; (oct, pc) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;divMod&lt;/span&gt; dm &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;m&lt;/span&gt;
&lt;span id=&quot;cb18-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (&lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; dn) os') &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;toEnum&lt;/span&gt; pc&lt;/span&gt;
&lt;span id=&quot;cb18-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (os'', od) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decompOrnament &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n os&lt;/span&gt;
&lt;span id=&quot;cb18-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb18-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (od &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; (dn &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; oct &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n)) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; os' &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; os''&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With all of this work, we’re now ready to tackle inverting scales. The thrust of the idea here is a relatively simple algorithm:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;Compute the image of the scale. Take the resulting pitch class, and associate it with the corresponding input pitch class. Negate the octave and ornaments when doing this.&lt;/li&gt;
&lt;li&gt;Check if the inverse pitch class exist directly in your image map. If so, return it directly.&lt;/li&gt;
&lt;li&gt;If not, select the best candidate of the image with respect to the inverse pitch class. Compute how far the best candidate is away from the input, and add that distance as an extra ornament.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Of course, the devil is in the details. Getting the “best candidate” might require looking in a different octave. We wrap the candidate logic into a chooser:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb19&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb19-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(&lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; x&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; b, x)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;, x))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which receives the desired pitch class, the image, and must return one element of the image along with an octave displacement. Since the entire thing is stuck in a &lt;code&gt;forall x&lt;/code&gt;, the chooser is allowed only to look at the &lt;code&gt;Fin b&lt;/code&gt;s to determine what is the best candidate. As a little illustration here, we might want to round up to the nearest pitch class in the image. So we’d want to filter the image by anything &lt;code&gt;&amp;gt;= pc&lt;/code&gt;. But if &lt;code&gt;pc&lt;/code&gt; is already the largest thing in the image, we’d instead want to return the smallest thing in the image with a +1 octave offset.&lt;/p&gt;
&lt;p&gt;If you’re curious, the logic in its full glory is thus:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb20&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb20-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;invertScale&lt;/span&gt;
&lt;span id=&quot;cb20-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; a b&lt;/span&gt;
&lt;span id=&quot;cb20-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb20-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; x&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; b, x)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;, x))&lt;/span&gt;
&lt;span id=&quot;cb20-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b&lt;/span&gt;
&lt;span id=&quot;cb20-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; b a&lt;/span&gt;
&lt;span id=&quot;cb20-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;invertScale choose (&lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; f) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; img ::&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; b, &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; a)]&lt;/span&gt;
&lt;span id=&quot;cb20-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      img &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;maxBound&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;(&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; a)]&lt;/span&gt;
&lt;span id=&quot;cb20-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; db) os) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; f a&lt;/span&gt;
&lt;span id=&quot;cb20-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; (o, pc) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;divMod&lt;/span&gt; db &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;b&lt;/span&gt;
&lt;span id=&quot;cb20-13&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;toEnum&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;(&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; b) pc&lt;/span&gt;
&lt;span id=&quot;cb20-14&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; ( b&lt;/span&gt;
&lt;span id=&quot;cb20-15&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;             , &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; a &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; (scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;a &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; o))&lt;/span&gt;
&lt;span id=&quot;cb20-16&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                 &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;negate&lt;/span&gt; os&lt;/span&gt;
&lt;span id=&quot;cb20-17&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;             )&lt;/span&gt;
&lt;span id=&quot;cb20-18&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-19&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;lookup&lt;/span&gt; b img &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-20&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb20-21&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-22&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; (ox, chosen) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; choose b img&lt;/span&gt;
&lt;span id=&quot;cb20-23&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            (b', &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; da os) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-24&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;              &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; find ((&lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; chosen) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;snd&lt;/span&gt;) img &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-25&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; c&lt;/span&gt;
&lt;span id=&quot;cb20-26&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;impossible&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-27&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            diff &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; b &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; b' &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; ox &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;b)&lt;/span&gt;
&lt;span id=&quot;cb20-28&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb20-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; (da &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; (ox &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; scaleSize &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;a)) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; os &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ornament &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;b diff&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which we specialize with a few different choices of chooser:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb21&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb21-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;invertScaleAbove   ::&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; b a&lt;/span&gt;
&lt;span id=&quot;cb21-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;invertScaleBelow   ::&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; b a&lt;/span&gt;
&lt;span id=&quot;cb21-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;invertScaleNearest ::&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; a b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Scale&lt;/span&gt; b a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As a little aside, I haven’t found a great way of naming the first two functions. Here “above” means “round down and offset up”, and below is the opposite. Any suggestions for more obvious names here would be greatly appreciated.&lt;/p&gt;
&lt;p&gt;That’s all for today. In the next post we’ll investigate the types behind voice leadings, with a similar attention to precision.&lt;/p&gt;</description>
	<pubDate>Sun, 15 Nov 2026 11:39:00 +0000</pubDate>
</item>
<item>
	<title>GHC Developer Blog: GHC 9.14.2-rc3 is now available</title>
	<guid isPermaLink="true">http://haskell.org/ghc/blog/20261112-ghc-9.14.2-rc3-released.html</guid>
	<link>http://haskell.org/ghc/blog/20261112-ghc-9.14.2-rc3-released.html</link>
	<description>&lt;h1&gt;GHC 9.14.2-rc3 is now available&lt;/h1&gt;
&lt;h4 class=&quot;text-muted&quot;&gt;wz1000 - 2026-11-12&lt;/h4&gt;

&lt;p&gt;The GHC developers are very pleased to announce the availability
of the third release candidate for GHC 9.14.2. Binary distributions, source
distributions, and documentation are available at &lt;a href=&quot;https://downloads.haskell.org/ghc/9.14.2-rc3&quot;&gt;downloads.haskell.org&lt;/a&gt; and
via &lt;a href=&quot;https://www.haskell.org/ghcup/&quot;&gt;GHCup&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;See also the previous announcements of &lt;a href=&quot;https://www.haskell.org/ghc/blog/20260730-ghc-9.14.2-rc1-released.html&quot;&gt;9.14.2-rc1&lt;/a&gt; and
&lt;a href=&quot;https://www.haskell.org/ghc/blog/20260917-ghc-9.14.2-rc2-released.html&quot;&gt;9.14.2-rc2&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The changes since the second release candidate are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fixes for miscompilations involving strict fields (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27831&quot;&gt;#27831&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27857&quot;&gt;#27857&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for runtime system crashes and deadlocks (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/23160&quot;&gt;#23160&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27272&quot;&gt;#27272&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for a &lt;code&gt;setInfoTableStackMap&lt;/code&gt; panic (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27368&quot;&gt;#27368&lt;/a&gt;), instances being dropped
during instance resolution (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26610&quot;&gt;#26610&lt;/a&gt;), and jump offset link errors for profiled
code on AArch64 Linux (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26994&quot;&gt;#26994&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for a segfault when interface files generated with
&lt;code&gt;-fbyte-code-and-object-code&lt;/code&gt; are reused with &lt;code&gt;-fprefer-byte-code&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27842&quot;&gt;#27842&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Exception context is now preserved when rethrowing in more places in &lt;code&gt;base&lt;/code&gt;
(&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/446&quot;&gt;CLC &lt;span&gt;#446&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/447&quot;&gt;CLC &lt;span&gt;#447&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27456&quot;&gt;#27456&lt;/a&gt;), and a WinIO fix for
misreported I/O results (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27971&quot;&gt;#27971&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Hadrian and build system fixes (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25850&quot;&gt;#25850&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27076&quot;&gt;#27076&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27786&quot;&gt;#27786&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/28053&quot;&gt;#28053&lt;/a&gt;), and an
update to &lt;code&gt;containers&lt;/code&gt; 0.8.1.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;GHC 9.14.2 is a bug-fix release fixing many issues of a variety of
severities and scopes, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Version 2 of the &lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0540-jsem.rst&quot;&gt;GHC Jobserver Protocol&lt;/a&gt; for GHC’s &lt;code&gt;-jsem&lt;/code&gt;
jobserver on Linux and other POSIX platforms (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25087&quot;&gt;#25087&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27253&quot;&gt;#27253&lt;/a&gt;). This is sadly
a necessary breaking change: &lt;code&gt;-jsem&lt;/code&gt; requires cabal-install 3.18 or later,
and is otherwise ignored with a &lt;a href=&quot;https://downloads.haskell.org/ghc/9.14.2-rc3/docs/users_guide/using-warnings.html#ghc-flag-Wsemaphore-open-failure&quot;&gt;warning&lt;/a&gt;, falling back to
sequential compilation.&lt;/li&gt;
&lt;li&gt;A new deprecation warning for static forms that mention nested let-bound
variables. This will become an error in GHC 10.0
(&lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0732-simplify-static.rst&quot;&gt;GHC Proposal &lt;span&gt;#732&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26718&quot;&gt;#26718&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for a CorePrep miscompilation that could project a field out of an
absent dictionary, resulting in a segfault (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25924&quot;&gt;#25924&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for demand analysis giving an absent demand to an argument that was
still used by the function’s stable unfolding, which could cause a runtime
crash (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26416&quot;&gt;#26416&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Numerous fixes for register allocation and Cmm register-conflict analysis
bugs, preventing incorrect code generation and corruption of vector registers
when spilling and reloading (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26411&quot;&gt;#26411&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26526&quot;&gt;#26526&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26537&quot;&gt;#26537&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26542&quot;&gt;#26542&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26550&quot;&gt;#26550&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26668&quot;&gt;#26668&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Many NCG fixes on AArch64: &lt;code&gt;MOVK&lt;/code&gt; clobbering live values (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26980&quot;&gt;#26980&lt;/a&gt;), register
clobbering and an incorrect overflow bit in &lt;code&gt;MUL2&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27046&quot;&gt;#27046&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27047&quot;&gt;#27047&lt;/a&gt;), and
incorrect sign extension (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26978&quot;&gt;#26978&lt;/a&gt;) and unsigned right shift (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26979&quot;&gt;#26979&lt;/a&gt;) at subword
widths.&lt;/li&gt;
&lt;li&gt;A fix for a stack alignment bug on x86 that could cause segfaults or corrupted
registers when using AVX/AVX-512 vector code (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26595&quot;&gt;#26595&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26822&quot;&gt;#26822&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for several black hole handling bugs that could lead to deadlocks or
crashes in multithreaded programs, showing up as hangs or “END_TSO_QUEUE
object entered” errors (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26922&quot;&gt;#26922&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26936&quot;&gt;#26936&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for many other runtime system crashes and races
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26341&quot;&gt;#26341&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26573&quot;&gt;#26573&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26640&quot;&gt;#26640&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27007&quot;&gt;#27007&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27008&quot;&gt;#27008&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27009&quot;&gt;#27009&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27082&quot;&gt;#27082&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for an “unknown/strange object” crash in the compacting garbage
collector (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27434&quot;&gt;#27434&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for split sections on Windows (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26696&quot;&gt;#26696&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26494&quot;&gt;#26494&lt;/a&gt;) and the LLVM
backend (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26770&quot;&gt;#26770&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for the Wasm backend (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26916&quot;&gt;#26916&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27257&quot;&gt;#27257&lt;/a&gt;), and the RISC-V (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27300&quot;&gt;#27300&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27303&quot;&gt;#27303&lt;/a&gt;,
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27306&quot;&gt;#27306&lt;/a&gt;) and PowerPC (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/23969&quot;&gt;#23969&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/24145&quot;&gt;#24145&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26828&quot;&gt;#26828&lt;/a&gt;) native code generators.&lt;/li&gt;
&lt;li&gt;The JavaScript backend now supports more than 128 registers, fixing runtime
&lt;code&gt;ReferenceError&lt;/code&gt; failures for functions taking very many arguments (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26558&quot;&gt;#26558&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Correctness and performance improvements in the bytecode interpreter
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/12953&quot;&gt;#12953&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26287&quot;&gt;#26287&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26946&quot;&gt;#26946&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26995&quot;&gt;#26995&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27001&quot;&gt;#27001&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for several compiler panics, including issues with SetLevels (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26681&quot;&gt;#26681&lt;/a&gt;),
the type-class specialiser (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26682&quot;&gt;#26682&lt;/a&gt;), &lt;code&gt;mkTick&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26772&quot;&gt;#26772&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27121&quot;&gt;#27121&lt;/a&gt;), CoreToStg
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27182&quot;&gt;#27182&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27386&quot;&gt;#27386&lt;/a&gt;), the pattern-match checker (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25926&quot;&gt;#25926&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27124&quot;&gt;#27124&lt;/a&gt;), module graph
reachability (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26568&quot;&gt;#26568&lt;/a&gt;), and continuation trimming for bottoming functions
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27261&quot;&gt;#27261&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for several cases where the compiler could hang or loop, including with
negative type literals (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26861&quot;&gt;#26861&lt;/a&gt;), deep subsumption (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26823&quot;&gt;#26823&lt;/a&gt;), and when
simplifying the right-hand sides of rewrite rules (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26323&quot;&gt;#26323&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26826&quot;&gt;#26826&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for cast worker/wrapper incorrectly firing on INLINE functions (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26903&quot;&gt;#26903&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for exponential-time desugaring of nested &lt;code&gt;case&lt;/code&gt; expressions
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27383&quot;&gt;#27383&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/20251&quot;&gt;#20251&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for a regression that caused overloaded functions to no longer be
specialised as effectively as in previous releases, hurting runtime
performance (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26831&quot;&gt;#26831&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for associated type family and data family instance changes not
triggering recompilation (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26183&quot;&gt;#26183&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26705&quot;&gt;#26705&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for Explicit Level Imports and Template Haskell staging
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26088&quot;&gt;#26088&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26098&quot;&gt;#26098&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26099&quot;&gt;#26099&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26616&quot;&gt;#26616&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27385&quot;&gt;#27385&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Improvements to determinism of compiler output, including the order in which
&lt;code&gt;:info&lt;/code&gt; lists instances (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26846&quot;&gt;#26846&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26858&quot;&gt;#26858&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26877&quot;&gt;#26877&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27532&quot;&gt;#27532&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Additions for GHCi and GHC API users: the &lt;code&gt;-fimport-loaded-targets&lt;/code&gt; flag
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26866&quot;&gt;#26866&lt;/a&gt;), custom external interpreter commands (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26652&quot;&gt;#26652&lt;/a&gt;), &lt;code&gt;decodeStackWithIpe&lt;/code&gt;
and related stack-decoding helpers (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27065&quot;&gt;#27065&lt;/a&gt;), an optional &lt;code&gt;SrcLoc&lt;/code&gt; on
&lt;code&gt;StackAnnotation&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26806&quot;&gt;#26806&lt;/a&gt;), and a &lt;code&gt;gc_sync_elapsed_ns&lt;/code&gt; counter in
&lt;code&gt;RTSStats&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26944&quot;&gt;#26944&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;… and many more.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A full accounting of these fixes can be found in the &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/blob/ghc-9.14/docs/users_guide/9.14.2-notes.rst?ref_type=heads&amp;amp;plain=1&quot;&gt;release notes&lt;/a&gt;. As
always, GHC’s release status, including planned future releases, can be found on
the GHC Wiki &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/wikis/GHC-status&quot;&gt;status&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This release candidate will have a two-week testing period. If all goes well
the final release will be available the week of 23 November 2026.&lt;/p&gt;
&lt;p&gt;GHC development is sponsored by:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://juspay.com/&quot;&gt;Juspay&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://qbaylogic.com/&quot;&gt;QBayLogic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.channable.com/&quot;&gt;Channable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://haskell.foundation/&quot;&gt;Haskell Foundation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://serokell.io/&quot;&gt;Serokell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://well-typed.com/&quot;&gt;Well-Typed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.tweag.io/&quot;&gt;Tweag&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.dotcom-monitor.com/&quot;&gt;Dotcom-Monitor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.loadview-testing.com/&quot;&gt;LoadView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://webhostingbuddy.com/&quot;&gt;Web Hosting Buddy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.findmyelectric.com/&quot;&gt;Find My Electric&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.sc.com&quot;&gt;Standard Chartered&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://upcloud.com&quot;&gt;UpCloud&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mercury.com&quot;&gt;Mercury&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We would like to thank these sponsors and other anonymous contributors
whose on-going financial and in-kind support has facilitated GHC maintenance
and release management over the years. Finally, this release would not have
been possible without the hundreds of open-source contributors whose work
comprise this release.&lt;/p&gt;
&lt;p&gt;As always, do give this release a try and open a &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/new&quot;&gt;ticket&lt;/a&gt; if you see
anything amiss.&lt;/p&gt;</description>
	<pubDate>Thu, 12 Nov 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>GHC Developer Blog: GHC 10.0.1-alpha2 is now available</title>
	<guid isPermaLink="true">http://haskell.org/ghc/blog/20261112-ghc-10.0.1-alpha2-released.html</guid>
	<link>http://haskell.org/ghc/blog/20261112-ghc-10.0.1-alpha2-released.html</link>
	<description>&lt;h1&gt;GHC 10.0.1-alpha2 is now available&lt;/h1&gt;
&lt;h4 class=&quot;text-muted&quot;&gt;wz1000 - 2026-11-12&lt;/h4&gt;

&lt;p&gt;The GHC developers are very pleased to announce the availability of the
second alpha prerelease of GHC 10.0.1. Binary distributions, source
distributions, and documentation are available at &lt;a href=&quot;https://downloads.haskell.org/ghc/10.0.1-alpha2&quot;&gt;downloads.haskell.org&lt;/a&gt; and
via &lt;a href=&quot;https://www.haskell.org/ghcup/&quot;&gt;GHCup&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;See also the &lt;a href=&quot;https://www.haskell.org/ghc/blog/20260918-ghc-10.0.1-alpha1-released.html&quot;&gt;previous announcement of 10.0.1-alpha1&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The changes since the first alpha are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fixes for miscompilations involving strict fields (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27831&quot;&gt;#27831&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27845&quot;&gt;#27845&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for runtime system crashes and deadlocks (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/23160&quot;&gt;#23160&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27272&quot;&gt;#27272&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/24450&quot;&gt;#24450&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for several compiler panics and crashes (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27368&quot;&gt;#27368&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27828&quot;&gt;#27828&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27916&quot;&gt;#27916&lt;/a&gt;,
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27994&quot;&gt;#27994&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/28081&quot;&gt;#28081&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for instances being dropped during instance resolution (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26610&quot;&gt;#26610&lt;/a&gt;), and
fixes for the shared unit database cache (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27834&quot;&gt;#27834&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27835&quot;&gt;#27835&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27836&quot;&gt;#27836&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for a segfault when interface files generated with
&lt;code&gt;-fbyte-code-and-object-code&lt;/code&gt; are reused with &lt;code&gt;-fprefer-byte-code&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27842&quot;&gt;#27842&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Exception context is now preserved when rethrowing in more places in &lt;code&gt;base&lt;/code&gt;
(&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/446&quot;&gt;CLC &lt;span&gt;#446&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/447&quot;&gt;CLC &lt;span&gt;#447&lt;/span&gt;&lt;/a&gt;), and a WinIO fix for misreported
I/O results (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27971&quot;&gt;#27971&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for Quick Look under &lt;code&gt;ImpredicativeTypes&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26543&quot;&gt;#26543&lt;/a&gt;), GHCi’s &lt;code&gt;:add&lt;/code&gt;
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27641&quot;&gt;#27641&lt;/a&gt;), and a performance regression when evaluating polymorphic thunks
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27979&quot;&gt;#27979&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Hadrian and build system fixes (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25850&quot;&gt;#25850&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27404&quot;&gt;#27404&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27499&quot;&gt;#27499&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27786&quot;&gt;#27786&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27823&quot;&gt;#27823&lt;/a&gt;,
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/28053&quot;&gt;#28053&lt;/a&gt;), and an update to &lt;code&gt;containers&lt;/code&gt; 0.8.1.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;GHC 10.0 will bring a number of new features and improvements, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The default language edition is now &lt;code&gt;GHC2024&lt;/code&gt;
(&lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/pull/632&quot;&gt;GHC Proposal &lt;span&gt;#632&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26039&quot;&gt;#26039&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Type&lt;/code&gt; and &lt;code&gt;Constraint&lt;/code&gt; are now completely distinct types, just as much as
&lt;code&gt;Int&lt;/code&gt; and &lt;code&gt;Bool&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/24279&quot;&gt;#24279&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The new &lt;a href=&quot;https://downloads.haskell.org/ghc/10.0.1-alpha2/docs/users_guide/exts/qualified_strings.html&quot;&gt;QualifiedStrings&lt;/a&gt; language extension
(&lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0723-qualified-strings.rst&quot;&gt;GHC Proposal &lt;span&gt;#723&lt;/span&gt;&lt;/a&gt;) allows string literals to be
qualified using module qualification syntax, so that &lt;code&gt;M.&quot;my string&quot;&lt;/code&gt;
desugars to&lt;code&gt;M.fromString &quot;my string&quot;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The treatment of static forms has been simplified, implementing
&lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0732-simplify-static.rst&quot;&gt;GHC Proposal &lt;span&gt;#732&lt;/span&gt;&lt;/a&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26556&quot;&gt;#26556&lt;/a&gt;). The free term variables of
&lt;code&gt;(static e)&lt;/code&gt; must now be bound at top level.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;-fpolymorphic-specialisation&lt;/code&gt; is now (re)enabled by default (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/23559&quot;&gt;#23559&lt;/a&gt;),&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bytecode can now be written to disk with &lt;code&gt;-fwrite-byte-code&lt;/code&gt; and packaged
into bytecode libraries with &lt;code&gt;-bytecodelib&lt;/code&gt;, which the interpreter can use
to satisfy package dependencies (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26298&quot;&gt;#26298&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Executables can now be properly statically linked with the new
&lt;code&gt;-fully-static&lt;/code&gt; and &lt;code&gt;-static-external&lt;/code&gt; flags (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26434&quot;&gt;#26434&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SIMD support in the AArch64 native code generator (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26536&quot;&gt;#26536&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A new &lt;code&gt;poll&lt;/code&gt; I/O manager, which is now the default in the single-threaded
RTS on all POSIX platforms except macOS (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/merge_requests/9677&quot;&gt;!9677&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Version 2 of the &lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0540-jsem.rst&quot;&gt;GHC Jobserver Protocol&lt;/a&gt; for GHC’s &lt;code&gt;-jsem&lt;/code&gt;
jobserver on Linux and other POSIX platforms (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25087&quot;&gt;#25087&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27253&quot;&gt;#27253&lt;/a&gt;). This is
sadly a necessary breaking change. &lt;code&gt;-jsem&lt;/code&gt; requires cabal-install 3.18 or
later, and is otherwise ignored with a warning, falling back to sequential
compilation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changes to &lt;code&gt;base&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;GHC.Num.{BigNat,Integer,Natural}&lt;/code&gt; are no longer exposed
(&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/359&quot;&gt;CLC &lt;span&gt;#359&lt;/span&gt;&lt;/a&gt;) and GHC internals in &lt;code&gt;GHC.Num&lt;/code&gt; are deprecated
(&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/360&quot;&gt;CLC &lt;span&gt;#360&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Data.List.init&lt;/code&gt; and &lt;code&gt;Data.List.last&lt;/code&gt; now carry an &lt;code&gt;x-partial&lt;/code&gt; warning
(&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/292&quot;&gt;CLC &lt;span&gt;#292&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The minimum bootstrap compiler is now GHC 9.10, LLVM versions up to 23 are
supported (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26813&quot;&gt;#26813&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27764&quot;&gt;#27764&lt;/a&gt;), and the Debian 9, Debian 10, Ubuntu 18.04
and Ubuntu 20.04 bindists have been dropped in favour of a new Debian 13
bindist (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25876&quot;&gt;#25876&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;… and many more&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A full accounting of changes can be found in the &lt;a href=&quot;https://downloads.haskell.org/ghc/10.0.1-alpha2/docs/users_guide/10.0.1-notes.html&quot;&gt;release notes&lt;/a&gt;.
See the &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/10.0&quot;&gt;migration guide&lt;/a&gt; for guidance on migrating programs to this release.
As always, GHC’s release status, including planned future releases, can be
found on the GHC Wiki &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/wikis/GHC-status&quot;&gt;status&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Given the many changes in this major release, we would very much appreciate
testing on downstream workloads.&lt;/p&gt;
&lt;p&gt;GHC development is sponsored by:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://juspay.com/&quot;&gt;Juspay&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://qbaylogic.com/&quot;&gt;QBayLogic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.channable.com/&quot;&gt;Channable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://haskell.foundation/&quot;&gt;Haskell Foundation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://serokell.io/&quot;&gt;Serokell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://well-typed.com/&quot;&gt;Well-Typed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.tweag.io/&quot;&gt;Tweag&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.dotcom-monitor.com/&quot;&gt;Dotcom-Monitor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.loadview-testing.com/&quot;&gt;LoadView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://webhostingbuddy.com/&quot;&gt;Web Hosting Buddy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.findmyelectric.com/&quot;&gt;Find My Electric&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.sc.com&quot;&gt;Standard Chartered&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://upcloud.com&quot;&gt;UpCloud&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mercury.com&quot;&gt;Mercury&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We would like to thank these sponsors and other anonymous contributors
whose on-going financial and in-kind support has facilitated GHC maintenance
and release management over the years. Finally, this release would not have
been possible without the hundreds of open-source contributors whose work
comprise this release.&lt;/p&gt;
&lt;p&gt;As always, do give this release a try and open a &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/new&quot;&gt;ticket&lt;/a&gt; if you see
anything amiss.&lt;/p&gt;</description>
	<pubDate>Thu, 12 Nov 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Sandy Maguire: Voice Leadings on Abstract Musical Pitches</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/voice-leadings/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/voice-leadings/index.html</link>
	<description>&lt;p&gt;In the last post, we discussed the notion of &lt;a href=&quot;https://reasonablypolymorphic.com/blog/pitches-1&quot;&gt;abstract musical pitches&lt;/a&gt;. I was writing that under a haze of covid-inspired single-parenting, so we’re going to take a few more minutes to try to clarify some of the claims made in that one, before moving on to doing more interesting things with the concept.&lt;/p&gt;
&lt;p&gt;In Haskell we have this function &lt;code&gt;quotRem :: Integral a =&amp;gt; a -&amp;gt; a -&amp;gt; (a, a)&lt;/code&gt;, which computes the quotient and remainder of one number divided by another. For example, &lt;code&gt;quotRem 25 5 == (5, 0)&lt;/code&gt;, &lt;code&gt;quotRem 25 6 == (4, 1)&lt;/code&gt;, &lt;code&gt;quotRem 25 7 == (3, 4)&lt;/code&gt;. You get the picture.&lt;/p&gt;
&lt;p&gt;Our &lt;code&gt;Deg n&lt;/code&gt; type from yesterday operates over &lt;code&gt;quotRem _ n&lt;/code&gt;. That is, any degree we might have, say &lt;code&gt;k :: Deg n&lt;/code&gt;, we compute its &lt;code&gt;quotRem k n&lt;/code&gt; to get &lt;code&gt;(octave, pitchClass)&lt;/code&gt;. This motivates why we were using functions &lt;code&gt;Fin n -&amp;gt; Fin m&lt;/code&gt; to model “scales” mapping from &lt;code&gt;Deg n&lt;/code&gt; into &lt;code&gt;Deg m&lt;/code&gt; — we want to map &lt;em&gt;only&lt;/em&gt; the &lt;code&gt;pitchClass&lt;/code&gt; part of the &lt;code&gt;quotRem&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To be more precise, if we want to map a &lt;code&gt;Deg n&lt;/code&gt; into &lt;code&gt;Deg m&lt;/code&gt;, we run this function:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;scale ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; m) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; m&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;scale f (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; dn) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; (oct, pc) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;quotRem&lt;/span&gt; dn n&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromEnum&lt;/span&gt; (f (&lt;span class=&quot;fu&quot;&gt;toEnum&lt;/span&gt; pc)) &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; oct &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; m&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This mapping only of pitch classes ensures we maintain the quotienting structure of &lt;code&gt;Deg n&lt;/code&gt; that requires &lt;code&gt;k + n&lt;/code&gt; to be the same pitch class as &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Musically, none of this should be unfamiliar. In a diatonic scale, you have 7 distinct notes — let’s label them “do, re, mi, fa, sol, la, ti.” These correspond to &lt;code&gt;0, 1, 2, 3, 4, 5, 6 :: Deg 7&lt;/code&gt;. If you start at any of these notes and go up (or down) 7 times, you will get back to the same pitch class an octave higher (or lower.)&lt;/p&gt;
&lt;p&gt;The same argument works for the chromatic scale, where we have 12 notes, labeled “A, A#, B, C, C#, D, D#, E, F, F#, G, G#”. Express these as &lt;code&gt;0, 1, ..., 11 :: Deg 12&lt;/code&gt;, and then go up or down 12 spots and you end up back where you were, modulo an octave.&lt;/p&gt;
&lt;p&gt;Similarly, if we want to map from the diatonic scale (&lt;code&gt;Deg 7&lt;/code&gt;) to the chromatic scale (&lt;code&gt;Deg 12&lt;/code&gt;), we choose 7 pitch classes from the 12 available, and respect the octave equivalence (this mapping is given implicitly by a &lt;em&gt;key signature&lt;/em&gt; and, in notated music, a &lt;em&gt;clef.&lt;/em&gt;)&lt;/p&gt;
&lt;p&gt;If none of these music words make any sense to you, don’t worry. It doesn’t matter.&lt;/p&gt;
&lt;h2 id=&quot;intrinsic-scales&quot;&gt;Intrinsic Scales&lt;/h2&gt;
&lt;p&gt;Check this out: nothing says we need to have a statically-known-at-compile-time &lt;code&gt;n&lt;/code&gt; for our &lt;code&gt;Deg n&lt;/code&gt;. As an illustration, here’s the beginning of the Star Wars theme:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Star Wars Theme&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out1.cropped.svg&quot; /&gt;&lt;figcaption&gt;Star Wars Theme&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Out of the 7 diatonic pitch classes, this segment of the theme uses only five: &lt;strong&gt;do&lt;/strong&gt;, &lt;strong&gt;re&lt;/strong&gt;, mi, fa, &lt;strong&gt;sol&lt;/strong&gt;, &lt;strong&gt;la&lt;/strong&gt;, &lt;strong&gt;ti&lt;/strong&gt;. So we can project the Star Wars theme down to a 5-note clef:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Intrinsic Star Wars Theme&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out2.cropped.svg&quot; /&gt;&lt;figcaption&gt;Intrinsic Star Wars Theme&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;and give a mapping:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;starWarsScale ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;starWarsScale &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;starWarsScale &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;starWarsScale &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;starWarsScale &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb2-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;starWarsScale &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which, of course, when we apply that mapping back to the 5-note version, gives us back the song we started with. But before transforming it back, we can do funny things, like &lt;code&gt;fmap (+ 1)&lt;/code&gt; in the 5-note scale:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;(+1) Intrinsic Star Wars Theme&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out3.cropped.svg&quot; /&gt;&lt;figcaption&gt;(+1) Intrinsic Star Wars Theme&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Mapping this back to &lt;code&gt;Deg 7&lt;/code&gt; (and then all the way to concrete pitches) gives us this neat little take on Star Wars:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;“Developed” Star Wars Theme&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out4.cropped.svg&quot; /&gt;&lt;figcaption&gt;“Developed” Star Wars Theme&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;audio controls=&quot;&quot;&gt;
&lt;source src=&quot;https://reasonablypolymorphic.com/images/music6/starwars2.mp3&quot; type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;p&gt;Not as good as John Williams’ to be sure, but it’s interesting that we can do this transformation!&lt;/p&gt;
&lt;p&gt;All of this is to say that for &lt;em&gt;any&lt;/em&gt; &lt;code&gt;Foldable&lt;/code&gt; and &lt;code&gt;Functor&lt;/code&gt; collection of notes, we can get its intrinsic scale via &lt;code&gt;intrinsic&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;intrinsic&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Foldable&lt;/span&gt; f, &lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt; f)&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; f (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; n)&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; i&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; f (&lt;span class=&quot;dt&quot;&gt;Deg&lt;/span&gt; i) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; r)&lt;/span&gt;
&lt;span id=&quot;cb3-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; r&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which is “you give me some collection of &lt;code&gt;Deg n&lt;/code&gt;s, and I’ll give you back a collection of &lt;code&gt;Deg i&lt;/code&gt;s, as well as a scale from &lt;code&gt;Fin i -&amp;gt; Fin n&lt;/code&gt;”. Where &lt;code&gt;i&lt;/code&gt; is some unknown-at-compile-time number corresponding to however many unique pitch-classes were in your collection.&lt;/p&gt;
&lt;p&gt;As we have seen with the Star Wars example, we can find the &lt;code&gt;intrinsic&lt;/code&gt; scale for horizontal structures.&lt;a class=&quot;footnote-ref&quot; href=&quot;https://reasonablypolymorphic.com#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; But of course, this is parameterized over any &lt;code&gt;Functor&lt;/code&gt;/&lt;code&gt;Foldable&lt;/code&gt; type constructor &lt;code&gt;f&lt;/code&gt;. So we can find lots of intrinsic scales — think chords and vertical structures. But probably also more things if I were more imaginative!&lt;/p&gt;
&lt;h2 id=&quot;efficient-voice-leadings&quot;&gt;Efficient Voice Leadings&lt;/h2&gt;
&lt;p&gt;We are going to need a few “scales” to work with in this section. We’ll take two common mappings from Western tonal music, namely the 3-in-7 mapping (“triads”), and the 7-in-12 mapping (“the diatonic scale”.)&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;triad ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;triad &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;diatonic ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Fin&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diatonic &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diatonic &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diatonic &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diatonic &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diatonic &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diatonic &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-13&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb4-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diatonic &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;11&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you are not classically-trained in music, don’t worry very much about this. You’d recognize these sounds if you heard them. For example, the triad mapping sounds like any of the examples from the Wikipedia page on &lt;a href=&quot;https://en.wikipedia.org/wiki/Arpeggio&quot;&gt;arpeggios&lt;/a&gt;. The diatonic mapping sounds like &lt;a href=&quot;https://youtu.be/vTJhpjIA0Fc?t=10&quot;&gt;this&lt;/a&gt;. We’re just arbitrarily choosing some ways of mapping between our abstract degrees (in a way that happens to line up with the arbitrariness of Western musical tradition.)&lt;/p&gt;
&lt;p&gt;In &lt;a href=&quot;https://dmitri.mycpanel.princeton.edu/tonality-an-owners-manual.html&quot;&gt;Tonality&lt;/a&gt; Tymoczko points out that these mappings happen to pretty evenly divide the octave. To illustrate what this means, look at &lt;code&gt;triad&lt;/code&gt;: it maps &lt;code&gt;0..6 :: Deg 3&lt;/code&gt; to &lt;code&gt;0, 2, 4, 7, 9, 11 :: Deg 7&lt;/code&gt;, which if you take the difference between any two subsequent degrees in the image is &lt;code&gt;2, 2, 3, 2, 2&lt;/code&gt;. That’s about as evenly as you &lt;em&gt;could&lt;/em&gt; map these things! There’s a tiny little bump in the pattern going from 4 to 7.&lt;/p&gt;
&lt;p&gt;The same thing happens in &lt;code&gt;diatonic&lt;/code&gt;. We map &lt;code&gt;0..8 :: Deg 7&lt;/code&gt; to &lt;code&gt;0, 2, 4, 5, 7, 9, 11, 12, 14 :: Deg 12&lt;/code&gt;, which again, taking the difference between subsequent numbers is &lt;code&gt;2, 2, 1, 2, 2, 2, 1, 2&lt;/code&gt;. Very evenly spaced!&lt;/p&gt;
&lt;p&gt;Note that this doesn’t actually need to have been the case! This is a property of &lt;code&gt;triad&lt;/code&gt; and &lt;code&gt;diatonic&lt;/code&gt;, not of &lt;code&gt;Deg {3,7,12}&lt;/code&gt;. And Tymoczko makes a pretty compelling argument that this property is &lt;em&gt;why&lt;/em&gt; Western music has historically been organized around &lt;code&gt;triad&lt;/code&gt; and &lt;code&gt;diatonic&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So why is this important? It’s to do with the &lt;em&gt;slight non-regularity&lt;/em&gt; in scale degrees. A big theme of &lt;a href=&quot;https://dmitri.mycpanel.princeton.edu/tonality-an-owners-manual.html&quot;&gt;Tonality&lt;/a&gt; is that we can very-nearly offset movement in &lt;code&gt;Deg m&lt;/code&gt; with an opposite movement in &lt;code&gt;Deg n&lt;/code&gt;, and that this is musically interesting (and ubiquitous!)&lt;/p&gt;
&lt;p&gt;For example, when working with &lt;code&gt;triad&lt;/code&gt;, it happens that moving &lt;em&gt;up&lt;/em&gt; by two steps in the intrinsic scale (&lt;code&gt;Deg 3&lt;/code&gt;) is almost exactly cancelled out by moving &lt;em&gt;down&lt;/em&gt; five steps in the containing scale (&lt;code&gt;Deg 7&lt;/code&gt;):&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Up by 2, down by 5&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out5.cropped.svg&quot; /&gt;&lt;figcaption&gt;Up by 2, down by 5&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The second chord (black note-heads) here is the first one moved up by &lt;code&gt;2 :: Deg 3&lt;/code&gt;, while the third chord is the second one moved down by &lt;code&gt;5 :: Deg 7&lt;/code&gt;. As you can see, we end up &lt;em&gt;almost exactly&lt;/em&gt; where we started, except that the bottom pitch has moved one step down.&lt;/p&gt;
&lt;p&gt;In fact, this phenomenon happens again and again if we continue performing these moves. As before, the movement in the intrinsic scale is shown with black note-heads, while the movement in the containing scale is shown with white ones.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Again and again&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out6.cropped.svg&quot; /&gt;&lt;figcaption&gt;Again and again&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Sure enough, this makes pretty pictures. And if you can read the musical notation, you’ll know that what we happen to be computing here are &lt;em&gt;efficient voice leadings&lt;/em&gt; from a root note to the diatonic third above it.&lt;/p&gt;
&lt;p&gt;In fact, if we remove the in-between black notes, we end up with this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Efficient Voice Leadings&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out7.cropped.svg&quot; /&gt;&lt;figcaption&gt;Efficient Voice Leadings&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;which sounds like this:&lt;/p&gt;
&lt;audio controls=&quot;&quot;&gt;
&lt;source src=&quot;https://reasonablypolymorphic.com/images/music6/voiceleading.mp3&quot; type=&quot;audio/mpeg&quot; /&gt;
&lt;/audio&gt;
&lt;p&gt;This is likely the sort of thing which is pretty exciting if you’re a musician, and “who cares” if you’re not. It doesn’t really &lt;em&gt;sound&lt;/em&gt; like music, but this is in a very real sense &lt;em&gt;a fundamental building block&lt;/em&gt; of (Western) music. It’s a way of smoothly getting from one place to another in a musical space. Any time you &lt;em&gt;don’t&lt;/em&gt; notice a jarring chord change, this is likely the mechanism by which the music is moving.&lt;/p&gt;
&lt;h2 id=&quot;modal-modulations&quot;&gt;Modal Modulations&lt;/h2&gt;
&lt;p&gt;We can do the same move with 7-in-12 modulations. If we look at the notes of the diatonic (C major) scale as a vertical chord:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;C Major&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out8.cropped.svg&quot; /&gt;&lt;figcaption&gt;C Major&lt;/figcaption&gt;
&lt;/figure&gt;















&lt;p&gt;Now if we map it up by 4 scale degrees, and down by 7 chromatic degrees we end up here:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;C Mixolydian&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out9.cropped.svg&quot; /&gt;&lt;figcaption&gt;C Mixolydian&lt;/figcaption&gt;
&lt;/figure&gt;

















&lt;p&gt;which is either &lt;a href=&quot;https://en.wikipedia.org/wiki/Mixolydian_mode&quot;&gt;C mixolydian&lt;/a&gt;, or a smooth voice-leading into F major, depending on how you look at it.&lt;/p&gt;
&lt;p&gt;Making the same up 4 down 7 move again now moves us into &lt;a href=&quot;https://en.wikipedia.org/wiki/Dorian_mode&quot;&gt;C dorian&lt;/a&gt;, or Bb major, again, depending on your perspective.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;C Dorian&quot; src=&quot;https://reasonablypolymorphic.com/images/music6/out10.cropped.svg&quot; /&gt;&lt;figcaption&gt;C Dorian&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Of course, I didn’t just pick the +4-7 thing out of my brain. Tymoczko has a &lt;a href=&quot;https://www.madmusicalscience.com/cs.html&quot;&gt;great tool&lt;/a&gt; for computing the moves involved here, and it works for any sized chord inside of any sized scale.&lt;/p&gt;
&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;/h2&gt;
&lt;p&gt;A weird one today. Not much in the way of Haskell, and either &lt;em&gt;fascinating&lt;/em&gt; or dead boring (depending on how much imagination you have) if you’re into the music theory. In the next post we’ll see about how to formalize all of this into first-class objects. And then we might be getting somewhat close to actually making some music. Maybe?&lt;/p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;With some finagling, granted. Those &lt;code&gt;Event&lt;/code&gt;s end up being pretty annoying to have to work around! This is a pretty good indication that everyone emailing me saying “you don’t need to model discreteness” are probably right, but I don’t actually know how to make that happen.&lt;a class=&quot;footnote-back&quot; href=&quot;https://reasonablypolymorphic.com#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Tue, 10 Nov 2026 06:18:00 +0000</pubDate>
</item>
<item>
	<title>Magnus Therning: Follow-up: QuickCheck: shrinking like tuples, writing like applicative</title>
	<guid isPermaLink="true">https://magnus.therning.org/2026-11-04-follow-up:-quickcheck:-shrinking-like-tuples,-writing-like-applicative.html</guid>
	<link>https://magnus.therning.org/2026-11-04-follow-up:-quickcheck:-shrinking-like-tuples,-writing-like-applicative.html</link>
	<description>&lt;p&gt;
Joachim wrote that
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
Your (\*/) operator reminds me of the applicative instance defined in
&lt;a href=&quot;https://hackage.haskell.org/package/successors-0.1.0.3/docs/Control-Applicative-Successors.html&quot;&gt;https://hackage.haskell.org/package/successors-0.1.0.3/docs/Control-Applicative-Successors.html&lt;/a&gt;
based on &lt;a href=&quot;https://stackoverflow.com/a/41944525&quot;&gt;https://stackoverflow.com/a/41944525&lt;/a&gt;.
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Ah, yes indeed! I actually started out defining an &lt;code&gt;Applicative&lt;/code&gt; for &lt;code&gt;Wane&lt;/code&gt;, but
convinced myself that it wouldn't satisfy the laws. Maybe I was a little too
quick to dismiss it.
&lt;/p&gt;
&lt;div class=&quot;taglist&quot;&gt;&lt;a href=&quot;https://magnus.therning.org/tags.html&quot;&gt;&lt;span class=&quot;tag-label&quot;&gt;Tags&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;tag-separator&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;taglist__tags&quot;&gt;&lt;a class=&quot;tag&quot; href=&quot;https://magnus.therning.org/tag-haskell.html&quot;&gt;haskell&lt;/a&gt; &lt;a class=&quot;tag&quot; href=&quot;https://magnus.therning.org/tag-quickcheck.html&quot;&gt;quickcheck&lt;/a&gt; &lt;/span&gt;&lt;/div&gt;</description>
	<pubDate>Wed, 04 Nov 2026 19:21:00 +0000</pubDate>
</item>
<item>
	<title>Magnus Therning: Follow-up: Emacs salmagundi, 2026-10-11</title>
	<guid isPermaLink="true">https://magnus.therning.org/2026-11-04-follow-up:-emacs-salmagundi,-2026-10-11.html</guid>
	<link>https://magnus.therning.org/2026-11-04-follow-up:-emacs-salmagundi,-2026-10-11.html</link>
	<description>&lt;p&gt;
A comment from Dave on &lt;a href=&quot;https://magnus.therning.org/2026-10-13-emacs-salmagundi,-2026-10-11.html&quot;&gt;Emacs salmagundi, 2026-10-11&lt;/a&gt;:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
I preload compile-history in some directory trees via .dir-locals.el,
and then I can just type M-x compile and choose something from the
history. I document this at
&lt;a href=&quot;https://davemq.github.io/2026/05/12/remote-linux-dev-with-emacs.html&quot;&gt;https://davemq.github.io/2026/05/12/remote-linux-dev-with-emacs.html&lt;/a&gt;.
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
That's a very nice tip!
&lt;/p&gt;

&lt;p&gt;
He continues
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
This multi-compile package looks interesting. But neither your article
or the README for the package have examples of running it. Do I type M-x
multi-compile?
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
No, the command is actually &lt;code&gt;M-x multi-compile-run&lt;/code&gt;. I probably should have
mentioned that at least once when writing about it.
&lt;/p&gt;

&lt;p&gt;
In the upstream repo the information is located at the very end of the &lt;a href=&quot;https://github.com/ReanGD/emacs-multi-compile/blob/master/README.md&quot;&gt;README&lt;/a&gt;.
It probably should be mentioned much earlier.
&lt;/p&gt;
&lt;div class=&quot;taglist&quot;&gt;&lt;a href=&quot;https://magnus.therning.org/tags.html&quot;&gt;&lt;span class=&quot;tag-label&quot;&gt;Tags&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;tag-separator&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;taglist__tags&quot;&gt;&lt;a class=&quot;tag&quot; href=&quot;https://magnus.therning.org/tag-emacs.html&quot;&gt;emacs&lt;/a&gt; &lt;/span&gt;&lt;/div&gt;</description>
	<pubDate>Wed, 04 Nov 2026 19:09:00 +0000</pubDate>
</item>
<item>
	<title>Magnus Therning: Follow-up: Switching to project.el</title>
	<guid isPermaLink="true">https://magnus.therning.org/2026-11-04-follow-up:-switching-to-project.el.html</guid>
	<link>https://magnus.therning.org/2026-11-04-follow-up:-switching-to-project.el.html</link>
	<description>&lt;p&gt;
A long while ago (in February this year) I got nice comment from Mike &lt;a href=&quot;https://magnus.therning.org/2026-02-18-switching-to-project.el.html&quot;&gt;on my
switch to project.el&lt;/a&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
That said, I was surprised to see your comment about having to set
&lt;code&gt;vc-handled-backends&lt;/code&gt; to &lt;code&gt;nil&lt;/code&gt; because otherwise &lt;code&gt;vc&lt;/code&gt; interferes with
&lt;code&gt;magit&lt;/code&gt;, as I &lt;b&gt;have not&lt;/b&gt; seen any such conflicts myself, and I made the
transition to &lt;code&gt;project.el&lt;/code&gt; several years ago.
&lt;/p&gt;

&lt;p&gt;
Maybe it's not worth it to get into it: you obviously found a
straightforward workaround, and perhaps there's additional background
that you simply didn't include, or I'm over-interpreting &quot;interfere&quot;
when you just mean some quirk that I just no longer notice having lived
with it for long enoughâ€”but it seemed worth it to mention that this
may not be universal, and thus might be worth looking into?
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
I did a search through my commit history and found that I disabled it in &lt;a href=&quot;https://gitlab.com/magus/mes/-/commit/e39c38057fb92860afa31e7cb76f9baf82301126&quot;&gt;August
2024&lt;/a&gt;:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
Disable VC completely
&lt;/p&gt;

&lt;p&gt;
Apparently VC was activated when loading the config as all of it is in git
repos (thanks to straight). This resulted in a question about following a
symbolic link in a git repo (thanks to 'vs-follow-links being set to 'ask). I
could have turned off the asking, but since I prefer to use magit anyway and I
don't use any other VCS I decided to turn VC off completely for now.
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Yes, there's a type in there, &lt;code&gt;vs-follow-links&lt;/code&gt; should be &lt;code&gt;vc-follow-symlinks&lt;/code&gt; I
believe.
&lt;/p&gt;

&lt;p&gt;
I reverted about &lt;a href=&quot;https://gitlab.com/magus/mes/-/commit/0dd248486cb586e74142fc4ecf544b1f9a5164ef&quot;&gt;two years later&lt;/a&gt; when I switched to elpaca:
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
Remove the config for VC
&lt;/p&gt;

&lt;p&gt;
It looks like the need for disabling VC disappeared with the move from straight.
Now only .projectile is an extra marker for projects.
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
I would be very surprised if my workaround is still needed when using straight.
&lt;/p&gt;
&lt;div class=&quot;taglist&quot;&gt;&lt;a href=&quot;https://magnus.therning.org/tags.html&quot;&gt;&lt;span class=&quot;tag-label&quot;&gt;Tags&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;tag-separator&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;taglist__tags&quot;&gt;&lt;a class=&quot;tag&quot; href=&quot;https://magnus.therning.org/tag-emacs.html&quot;&gt;emacs&lt;/a&gt; &lt;/span&gt;&lt;/div&gt;</description>
	<pubDate>Wed, 04 Nov 2026 18:59:00 +0000</pubDate>
</item>
<item>
	<title>Haskell Interlude: 88: Tom Schrijvers</title>
	<guid isPermaLink="false">Buzzsprout-20053749</guid>
	<link></link>
	<description>&lt;p&gt;In this Haskell Interlude, we sat down with Tom Schrijvers, the head of the Functional Programming team at KU Leuven. He has been a prolific contributor to the foundations of Haskell, with many papers pushing the boundaries of the Haskell type system, programming with effects, and applications of functional programming.Our conversation was a whirlwind tour through Tom's long and wide publication history, with lots of tangents and ideas about functional programming, logic programming, constraints, DSLs, composition, program calculation, and other great stuff.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
	<pubDate>Mon, 02 Nov 2026 08:00:00 +0000</pubDate>
        <enclosure url="https://www.buzzsprout.com/1817535/episodes/20053749-88-tom-schrijvers.mp3" length="32991330" type="audio/mpeg"/>
</item>
<item>
	<title>GHC Developer Blog: GHC 9.12.5-rc4 is now available</title>
	<guid isPermaLink="true">http://haskell.org/ghc/blog/20261102-ghc-9.12.5-rc4-released.html</guid>
	<link>http://haskell.org/ghc/blog/20261102-ghc-9.12.5-rc4-released.html</link>
	<description>&lt;h1&gt;GHC 9.12.5-rc4 is now available&lt;/h1&gt;
&lt;h4 class=&quot;text-muted&quot;&gt;mangoiv - 2026-11-02&lt;/h4&gt;

&lt;p&gt;The GHC developers are very pleased to announce the availability
of the third release candidate for GHC 9.12.5, ghc-9.12.5-rc4.&lt;/p&gt;
&lt;p&gt;The fourth release candidate was necessary due to a large amount of critical issues that required backporting.&lt;/p&gt;
&lt;p&gt;This will be the last release candidate before release if no critical regressions are discovered in this RC – we’re so far past due that we don’t backport anymore bugfixes for this release.&lt;/p&gt;
&lt;p&gt;See also the &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16629&quot;&gt;backport MR.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;See also the previous announcement of 9.12.5-rc3: &lt;a href=&quot;https://www.haskell.org/ghc/blog/20260731-ghc-9.12.5-rc3-released.html&quot;&gt;https://www.haskell.org/ghc/blog/20260731-ghc-9.12.5-rc3-released.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Binary distributions, source distributions, and documentation are available at
&lt;a href=&quot;https://downloads.haskell.org/~ghc/9.12.5-rc4/&quot;&gt;downloads.haskell.org&lt;/a&gt; and via &lt;a href=&quot;https://www.haskell.org/ghcup/&quot;&gt;GHCup&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This release candidate will have a two-week testing period. If all goes well
the final release will be available the week of 17 November 2026.&lt;/p&gt;
&lt;p&gt;GHC 9.12.5 is a bug-fix release fixing many issues of a variety of
severities and scopes, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Implement version 2 of the ghc &lt;code&gt;semaphore&lt;/code&gt; protocol for GHC’s &lt;code&gt;-jsem&lt;/code&gt; jobserver on Linux and other &lt;code&gt;POSIX&lt;/code&gt; platforms (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25087&quot;&gt;&lt;span&gt;#25087&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27253&quot;&gt;&lt;span&gt;#27253&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;several improvements to the pattern match checker (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25926&quot;&gt;&lt;span&gt;#25926&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27124&quot;&gt;&lt;span&gt;#27124&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;several improvements to the demand analyser (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27261&quot;&gt;&lt;span&gt;#27261&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27106&quot;&gt;&lt;span&gt;#27106&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26416&quot;&gt;&lt;span&gt;#26416&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;several fixes regarding the interaction of ticks and coercions (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27121&quot;&gt;&lt;span&gt;#27121&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26929&quot;&gt;&lt;span&gt;#26929&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26642&quot;&gt;&lt;span&gt;#26642&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26693&quot;&gt;&lt;span&gt;#26693&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;several fixes for blackholes (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27261&quot;&gt;&lt;span&gt;#27261&lt;/span&gt;&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;… and many more&lt;/p&gt;
&lt;p&gt;Mind that the very important overhaul of the semaphore-compat library to v2 is included in this release and that the semaphore feature will only start working with cabal-install 3.18 again, which is sadly a breaking but necessary change.&lt;/p&gt;
&lt;p&gt;A full accounting of these fixes can be found in the
release notes. As always, GHC’s release status, including planned future
releases, can be found on the GHC Wiki status.&lt;/p&gt;
&lt;p&gt;GHC development is sponsored by:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://juspay.com/&quot;&gt;Juspay&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://qbaylogic.com/&quot;&gt;QBayLogic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.channable.com/&quot;&gt;Channable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://haskell.foundation/&quot;&gt;Haskell Foundation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://serokell.io/&quot;&gt;Serokell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://well-typed.com/&quot;&gt;Well-Typed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.tweag.io/&quot;&gt;Tweag&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.dotcom-monitor.com/&quot;&gt;Dotcom-Monitor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.loadview-testing.com/&quot;&gt;LoadView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://webhostingbuddy.com/&quot;&gt;Web Hosting Buddy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.findmyelectric.com/&quot;&gt;Find My Electric&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.sc.com&quot;&gt;Standard Chartered&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://upcloud.com&quot;&gt;UpCloud&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mercury.com&quot;&gt;Mercury&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We would like to thank these sponsors and other anonymous contributors
whose on-going financial and in-kind support has facilitated GHC maintenance
and release management over the years. Finally, this release would not have
been possible without the hundreds of open-source contributors whose work
comprise this release.&lt;/p&gt;
&lt;p&gt;As always, do give this release a try and open a ticket if you see
anything amiss.&lt;/p&gt;</description>
	<pubDate>Mon, 02 Nov 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Abhinav Sarkar: An LC-3 Disassembler in Haskell</title>
	<guid isPermaLink="false">https://abhinavsarkar.net/posts/lc3-disassembler/</guid>
	<link>https://abhinavsarkar.net/posts/lc3-disassembler/?mtm_campaign=feed</link>
	<description>&lt;p&gt;LC-3 assembly is an educational programming language. It is simple enough that complex programs can be written directly in it without needing a compiler, and thus it is often used in teaching assembly. An &lt;a href=&quot;https://en.wikipedia.org/wiki/Little_Computer_3&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;LC-3&lt;/a&gt; textual assembly program is compiled to a binary object file by an assembler, and then executed by a &lt;a href=&quot;https://en.wikipedia.org/wiki/Virtual_machine#Process_virtual_machines&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;virtual machine&lt;/a&gt;. Writing a virtual machine for LC-3 is a fun little exercise. However, in this post, we implement a disassembler for compiled LC-3 binaries back to textual assembly programs.&lt;/p&gt;
&lt;p&gt;This post was originally published on &lt;a href=&quot;https://abhinavsarkar.net/posts/lc3-disassembler/?mtm_campaign=feed&quot;&gt;abhinavsarkar.net&lt;/a&gt;.&lt;/p&gt;
&lt;nav class=&quot;toc&quot;&gt;&lt;h2&gt;Contents&lt;/h2&gt;&lt;ol&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#introduction-to-lc-3&quot;&gt;Introduction to LC-3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#problem-in-disassembling-lc-3&quot;&gt;Problem in Disassembling LC-3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#setting-up&quot;&gt;Setting Up&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#decoding-instructions&quot;&gt;Decoding Instructions&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#basic-disassembly&quot;&gt;Basic Disassembly&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fixed-point-cleanup&quot;&gt;Fixed-Point Cleanup&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#postprocessing&quot;&gt;Postprocessing&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#pretty-printing&quot;&gt;Pretty-Printing&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#reading-the-binary&quot;&gt;Reading the Binary&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#finishing-up&quot;&gt;Finishing Up&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/nav&gt;
&lt;h2 id=&quot;introduction-to-lc-3&quot;&gt;Introduction to LC-3&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#introduction-to-lc-3&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;LC-3 has a simple &lt;a href=&quot;https://www.jmeiners.com/lc3-vm/supplies/lc3-isa.pdf&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot; title=&quot;LC-3 Instruction Set&quot;&gt;instruction set&lt;/a&gt; with only 18 instructions. It works with eight 16-bit registers (numbered from 0 to 7), and 16-bit addressable memory. LC-3 instructions are also 16-bit, with first four bits used for opcodes, and the rest for instruction arguments.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3 Instruction Set Architecture&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3 Instruction Set Architecture&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-isa.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 Instruction Set Architecture&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;There are instructions for doing arithmetic and bitwise logical operations, conditional branching, executing subroutines, loading from and storing to memory, handling interrupts and doing system calls. Some of them take source, destination, and base registers as arguments (&lt;code&gt;SR&lt;/code&gt;, &lt;code&gt;DR&lt;/code&gt;, and &lt;code&gt;BR&lt;/code&gt; respectively in the diagram above). Others take integer immediate values, program counter, and address offsets (&lt;code&gt;imm&lt;/code&gt;, &lt;code&gt;PCOffset&lt;/code&gt;, and &lt;code&gt;offset&lt;/code&gt; respectively in the diagram above). The &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;TRAP&lt;/span&gt;&lt;/code&gt; instruction take a code for the syscall to execute. Notice that we count the bits from right to left, zero-indexed.&lt;/p&gt;
&lt;p&gt;When written in the textual assembly format, we use mnemonics for the opcodes and syscalls. The eight registers are written as &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;va&quot;&gt;R0&lt;/span&gt;&lt;/code&gt; to &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;va&quot;&gt;R7&lt;/span&gt;&lt;/code&gt;. The instructions can be labeled and referred from other instructions using them. Let’s start by looking at a simple LC-3 assembly file:&lt;/p&gt;
&lt;div id=&quot;hello-world&quot;&gt;

&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot;&gt;&lt;pre class=&quot;sourceCode lc-3&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span id=&quot;1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;; LC−3 Program that prints&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;; &quot;Hello World!&quot; to the console&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;cn&quot;&gt;.ORIG&lt;/span&gt; &lt;span class=&quot;bn&quot;&gt;x3000&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;LEA&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;R0&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;HW&lt;/span&gt;                 &lt;span class=&quot;co&quot;&gt;; load address of string&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;PUTS&lt;/span&gt;                       &lt;span class=&quot;co&quot;&gt;; print string to console&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;HALT&lt;/span&gt;                       &lt;span class=&quot;co&quot;&gt;; halt program&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;HW&lt;/span&gt; &lt;span class=&quot;cn&quot;&gt;.STRINGZ&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;; string data&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;cn&quot;&gt;.END&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As seen above, the assembly file can also contain assembler directives such as &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.STRINGZ&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.FILL&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.BLKW&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.ORIG&lt;/span&gt;&lt;/code&gt;, and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.END&lt;/span&gt;&lt;/code&gt;. Quoting the &lt;a href=&quot;https://github.com/falvarezb/lc3asm&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;lc3asm&lt;/code&gt;&lt;/a&gt; docs:&lt;/p&gt;
&lt;blockquote&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.ORIG&lt;/span&gt;&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;
tells the assembler where in memory to place the LC-3 program.
&lt;/dd&gt;
&lt;dt&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.FILL&lt;/span&gt;&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;
tells the assembler to set aside the next location in the program and initialize it with the value of the operand.
&lt;/dd&gt;
&lt;dt&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.BLKW&lt;/span&gt;&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;
tells the assembler to set aside some number of sequential memory locations (BLocK of Words) in the program.
&lt;/dd&gt;
&lt;dt&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.STRINGZ&lt;/span&gt;&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;
tells the assembler to initialize a sequence of n + 1 memory locations; the argument is a sequence of n characters, inside double quotation marks; the first n words of memory are initialized with the zero-extended ASCII codes of the corresponding characters in the string; the final word of memory is initialized to 0.
&lt;/dd&gt;
&lt;dt&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.END&lt;/span&gt;&lt;/code&gt;&lt;/dt&gt;
&lt;dd&gt;
tells the assembler where the program ends; any characters that come after .END are ignored by the assembler.
&lt;/dd&gt;
&lt;/dl&gt;
&lt;/blockquote&gt;
&lt;p&gt;We assemble the program with the &lt;code&gt;lc3asm&lt;/code&gt; assembler, and then execute the resultant binary with the &lt;a href=&quot;https://www.jmeiners.com/lc3-vm/src/lc3.c&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;lc3&lt;/code&gt;&lt;/a&gt; VM&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;plain&quot;&gt;&lt;code&gt;$ lc3as hello.asm
$ lc3 hello.obj
Hello World!&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It works as expected! As much fun as it would be to write an assembler and a VM, in this post, we don’t assemble or run LC-3 programs, we &lt;strong&gt;disassemble&lt;/strong&gt; them. We don’t care &lt;em&gt;what&lt;/em&gt; the instructions do, only &lt;em&gt;how&lt;/em&gt; they are encoded.&lt;/p&gt;
&lt;h2 id=&quot;problem-in-disassembling-lc-3&quot;&gt;Problem in Disassembling LC-3&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#problem-in-disassembling-lc-3&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Unlike an object file generated from &lt;a href=&quot;https://en.wikipedia.org/wiki/X86&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;X86&lt;/a&gt; or &lt;a href=&quot;https://en.wikipedia.org/wiki/Arm&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Arm&lt;/a&gt; assembly, an LC-3 object file does not contain section tables separating code from data, symbol tables indicating function and variable names, relocation information, or debug symbols&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn2&quot; id=&quot;fnref2&quot;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;. It contains only raw binary instructions and data that are freely intermixed in a single address space. This is made necessary by the fact that LC-3 has only 16-bit addressable memory. So data blocks and read-write memory addressed from code has to be within &lt;span class=&quot;math inline&quot;&gt;\(2^{16}\)&lt;/span&gt; bits or 8192 bytes of the code addressing it.&lt;/p&gt;
&lt;p&gt;Consider these bytes:&lt;/p&gt;
&lt;pre class=&quot;plain&quot;&gt;&lt;code&gt;0100 0001 0000 0000    ; Instruction: JSRR R6
0100 0001 0000 0000    ; Or is it data?&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A disassembler looking only at binary code has no context. A 16-bit word that decodes perfectly as a legal instruction may actually be a character, a part of a packed string, a number/byte literal, or a memory address. We need context to decode correctly, so the disassembler works in multiple passes:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;First, we decode instructions that are unambiguous.&lt;/li&gt;
&lt;li&gt;Then, we use the context of the decoded instructions to speculatively decode the rest of the instructions/data. We repeat this till a fixed point is reached.&lt;/li&gt;
&lt;li&gt;Next, we clean up the data by combining bytes.&lt;/li&gt;
&lt;li&gt;Finally, we insert labels at the right places.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let’s go over the disassembler part-by-part.&lt;/p&gt;
&lt;h2 id=&quot;setting-up&quot;&gt;Setting Up&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#setting-up&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;First, the imports:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;{-# LANGUAGE BlockArguments #-}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;{-# LANGUAGE LambdaCase #-}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;{-# LANGUAGE PatternSynonyms #-}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;{-# LANGUAGE RecordWildCards #-}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;{-# LANGUAGE StrictData #-}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LC3Disassembler&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Control.Arrow&lt;/span&gt; ((&amp;gt;&amp;gt;&amp;gt;))&lt;/span&gt;
&lt;span id=&quot;cb3-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Control.Monad&lt;/span&gt; (forM, forM_, unless, when)&lt;/span&gt;
&lt;span id=&quot;cb3-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Control.Monad.ST.Strict&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt;, runST)&lt;/span&gt;
&lt;span id=&quot;cb3-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Bits&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Bits&lt;/span&gt; (..))&lt;/span&gt;
&lt;span id=&quot;cb3-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.ByteString&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BS&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Char&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Char&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Functor&lt;/span&gt; ((&amp;lt;&amp;amp;&amp;gt;))&lt;/span&gt;
&lt;span id=&quot;cb3-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Int&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb3-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.List&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;List&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.List.Split&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;List&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Vector&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;V&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Vector.Mutable&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MV&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Vector.Unboxed&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UV&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Vector.Unboxed.Mutable&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MUV&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Data.Word&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Word8&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb3-25&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;System.Exit&lt;/span&gt; (exitFailure)&lt;/span&gt;
&lt;span id=&quot;cb3-26&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Text.Printf&lt;/span&gt; (printf)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We use the &lt;a href=&quot;https://hackage.haskell.org/package/vector&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;vector&lt;/a&gt; library for instruction lists, and the &lt;a href=&quot;https://hackage.haskell.org/package/base/docs/Control-Monad-ST.html#t:ST&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt;&lt;/code&gt;&lt;/a&gt; monad for manipulating them mutably. We also use &lt;a href=&quot;https://hackage.haskell.org/package/bytestring/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;bytestring&lt;/a&gt; for doing binary IO.&lt;/p&gt;
&lt;p&gt;Here are the data types we use:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR3&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Enum&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Flag&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;FlagNeg&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;FlagZer&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;FlagPos&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapRoutine&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapGETC&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapOUT&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapPUTS&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapIN&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapPUTSP&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapHALT&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapUNKNOWN&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word8&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-25&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ADD&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-26&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      {&lt;span class=&quot;ot&quot;&gt; destReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb4-27&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;        sourceReg1 ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb4-28&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;        source2 ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-29&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      }&lt;/span&gt;
&lt;span id=&quot;cb4-30&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;AND&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-31&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      {&lt;span class=&quot;ot&quot;&gt; destReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb4-32&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;        sourceReg1 ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb4-33&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;        source2 ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-34&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      }&lt;/span&gt;
&lt;span id=&quot;cb4-35&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BR&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-36&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      {&lt;span class=&quot;ot&quot;&gt; flags ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set.Set&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Flag&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb4-37&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;        pcOffset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb4-38&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;        label ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb4-39&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;        raw ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-40&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      }&lt;/span&gt;
&lt;span id=&quot;cb4-41&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-41&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;JMP&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;baseReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-42&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-42&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RET&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-43&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-43&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;JSR&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;pcOffset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; label ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; raw ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-44&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-44&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;JSRR&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;baseReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-45&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-45&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LD&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;destReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; pcOffset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; raw ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-46&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-46&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LDI&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;destReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; pcOffset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; raw ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-47&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-47&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LDR&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;destReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; baseReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; offset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-48&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-48&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LEA&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;destReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; pcOffset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-49&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-49&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NOT&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;destReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; sourceReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-50&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-50&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RTI&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-51&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-51&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;sourceReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; pcOffset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; raw ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-52&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-52&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STI&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;sourceReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; pcOffset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; raw ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-53&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-53&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STR&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;sourceReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; baseReg ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; offset ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-54&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-54&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TRAP&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;trapRoutine ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapRoutine&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-55&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-55&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;INVALID&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-56&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-56&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-57&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-57&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Char&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-58&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-58&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-59&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-59&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LABEL&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-60&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-60&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SPECULATION&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;origInstr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; specualtedInstr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-61&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-61&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-62&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-62&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-63&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-63&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BinCode&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UV.Vector&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-64&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-64&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-65&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-65&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Object&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt;origin ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt; code ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BinCode&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb4-66&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-66&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;&lt;/code&gt; type enumerates the eight LC-3 registers. The &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;Flag&lt;/span&gt;&lt;/code&gt; type represents the condition codes used for conditional jumping, which are negative, zero and positive.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;TrapRoutine&lt;/span&gt;&lt;/code&gt; type enumerates the eight standard LC-3 trap codes used for doing IO and halting the programs:&lt;/p&gt;
&lt;div class=&quot;scrollable-table&quot;&gt;
&lt;table&gt;
&lt;colgroup&gt;
&lt;col style=&quot;width: 14%;&quot; /&gt;
&lt;col style=&quot;width: 85%;&quot; /&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align: left;&quot;&gt;Code&lt;/th&gt;
&lt;th style=&quot;text-align: left;&quot;&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;GETC&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Reads one character from STDIN.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;OUT&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Prints one character to STDOUT.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;PUTS&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Prints a null-terminated &lt;a href=&quot;https://en.wikipedia.org/wiki/ASCII&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;ASCII&lt;/a&gt; one-character-per-word string to STDOUT.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;IN&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Prints a prompt to STDOUT and reads one character from STDIN.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;PUTSP&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Prints a null-terminated ASCII two-characters-per-word string to STDOUT.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;HALT&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Halts the program execution.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;UNKNOWN&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Represents an unknown trap routine code.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;&lt;/code&gt; type is the heart of the disassembler. It doesn’t represent only the real LC-3 instructions, but also adds more constructors that are useful for disassembling:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ADD&lt;/span&gt;&lt;/code&gt; to &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;TRAP&lt;/span&gt;&lt;/code&gt; are the real LC-3 instructions.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;INVALID&lt;/span&gt;&lt;/code&gt; represents the one reserved opcode &lt;code&gt;1101&lt;/code&gt; that the ISA defines as invalid&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn3&quot; id=&quot;fnref3&quot;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;RAW&lt;/span&gt;&lt;/code&gt; is for capturing raw encoded binary data.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;CHAR&lt;/span&gt;&lt;/code&gt; represents a single character.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;STRING&lt;/span&gt;&lt;/code&gt; represents a string, that is a sequence of characters.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;LABEL&lt;/span&gt;&lt;/code&gt; is a textual label used to annotate instructions that other instructions may jump to. It doesn’t exist in binary data, and is inserted by the disassembler.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;SPECULATION&lt;/span&gt;&lt;/code&gt; is a speculated instruction. It holds both the original instruction decoded from the binary data, and the guessed instruction depending on the context.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This representation lets us handle ambiguous data. Also note that we capture the raw bytes in some instructions like &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;BR&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JSR&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;BinCode&lt;/span&gt;&lt;/code&gt; represents entire 16-bit binary code, and &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;Object&lt;/span&gt;&lt;/code&gt; is the type for representing an LC-3 object file with an origin address and the binary code.&lt;/p&gt;
&lt;h3 id=&quot;type-safe-bit-matching&quot;&gt;Type-Safe Bit Matching&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#type-safe-bit-matching&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Since we work a lot with bits here, we define some patterns to make it easier and less error-prone:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;tribit ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;tribit w &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; w &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b111&lt;/span&gt;
&lt;span id=&quot;cb5-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B000&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B001&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B010&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B011&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B100&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B101&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B110&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B111&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B000&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b000&lt;/span&gt;
&lt;span id=&quot;cb5-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B001&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b001&lt;/span&gt;
&lt;span id=&quot;cb5-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B010&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b010&lt;/span&gt;
&lt;span id=&quot;cb5-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B011&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b011&lt;/span&gt;
&lt;span id=&quot;cb5-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B100&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b100&lt;/span&gt;
&lt;span id=&quot;cb5-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B101&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b101&lt;/span&gt;
&lt;span id=&quot;cb5-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B110&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b110&lt;/span&gt;
&lt;span id=&quot;cb5-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B111&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b111&lt;/span&gt;
&lt;span id=&quot;cb5-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;{-# COMPLETE B000, B001, B010, B011, B100, B101, B110, B111 #-}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;nibble ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;nibble w &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; w &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1111&lt;/span&gt;
&lt;span id=&quot;cb5-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0000&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B0001&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B0010&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B0011&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B0100&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B0101&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B0110&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B0111&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb5-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B1000&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B1001&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B1010&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B1011&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B1100&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B1101&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B1110&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;B1111&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-25&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0000&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b0000&lt;/span&gt;
&lt;span id=&quot;cb5-26&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0001&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b0001&lt;/span&gt;
&lt;span id=&quot;cb5-27&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0010&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b0010&lt;/span&gt;
&lt;span id=&quot;cb5-28&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0011&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b0011&lt;/span&gt;
&lt;span id=&quot;cb5-29&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0100&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b0100&lt;/span&gt;
&lt;span id=&quot;cb5-30&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0101&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b0101&lt;/span&gt;
&lt;span id=&quot;cb5-31&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0110&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b0110&lt;/span&gt;
&lt;span id=&quot;cb5-32&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B0111&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b0111&lt;/span&gt;
&lt;span id=&quot;cb5-33&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B1000&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1000&lt;/span&gt;
&lt;span id=&quot;cb5-34&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B1001&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1001&lt;/span&gt;
&lt;span id=&quot;cb5-35&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B1010&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1010&lt;/span&gt;
&lt;span id=&quot;cb5-36&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B1011&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1011&lt;/span&gt;
&lt;span id=&quot;cb5-37&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B1100&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1100&lt;/span&gt;
&lt;span id=&quot;cb5-38&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B1101&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1101&lt;/span&gt;
&lt;span id=&quot;cb5-39&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B1110&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1110&lt;/span&gt;
&lt;span id=&quot;cb5-40&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;B1111&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1111&lt;/span&gt;
&lt;span id=&quot;cb5-41&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-41&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-42&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-42&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;{-# COMPLETE B0000, B0001, B0010, B0011, B0100, B0101, B0110, B0111,&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-43&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb5-43&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;             B1000, B1001, B1010, B1011, B1100, B1101, B1110, B1111 #-}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Each LC-3 instruction has a 4-bit opcode, and many have 3-bit register or flag fields. So, we define two new types: &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt;&lt;/code&gt; (3-bit) and &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt;&lt;/code&gt; (4-bit), with pattern synonyms for exhaustive pattern matching.&lt;/p&gt;
&lt;p&gt;By defining patterns &lt;code&gt;B0000&lt;/code&gt; through &lt;code&gt;B1111&lt;/code&gt;, we map directly to the ISA specification. For example, &lt;code&gt;B0101&lt;/code&gt; visually shows the bit pattern for the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;AND&lt;/span&gt;&lt;/code&gt; opcode. GHC’s &lt;code&gt;COMPLETE&lt;/code&gt; pragma enforces exhaustiveness, so no fallback cases are needed. The smart constructor functions &lt;code&gt;tribit&lt;/code&gt; and &lt;code&gt;nibble&lt;/code&gt; take the least significant 3 or 4 bits of the passed 16-bit parameter respectively, and return respective typed bits.&lt;/p&gt;
&lt;h2 id=&quot;decoding-instructions&quot;&gt;Decoding Instructions&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#decoding-instructions&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;First, we decode the registers and trap routines:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;decodeRegister ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeRegister &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; tribit &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;B000&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;B001&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;B010&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;B011&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR3&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;B100&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;B101&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;B110&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;B111&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;decodeTrapRoutine ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word8&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapRoutine&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeTrapRoutine &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bn&quot;&gt;0x20&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapGETC&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bn&quot;&gt;0x21&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapOUT&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bn&quot;&gt;0x22&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapPUTS&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bn&quot;&gt;0x23&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapIN&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bn&quot;&gt;0x24&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapPUTSP&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bn&quot;&gt;0x25&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapHALT&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb6-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapUNKNOWN&lt;/span&gt; n&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We see the bit patterns in action here. The trap routines codes are as defined in the ISA.&lt;/p&gt;
&lt;p&gt;Next, the &lt;code&gt;decodeInstruction&lt;/code&gt; function extracts the opcode nibble (the most significant four bits), and decodes the arguments by calling &lt;code&gt;decodeInstruction'&lt;/code&gt;, which dispatches on the opcodes:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;decodeInstruction ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction instr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  decodeInstruction' instr &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; nibble &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, we go over decoding of each instruction one-by-one.&lt;/p&gt;
&lt;h3 id=&quot;add&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ADD&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#add&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ADD&lt;/span&gt;&lt;/code&gt; has two variations: one that takes two source registers, and another that takes one source register and one immediate value.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-add.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ADD&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;signExtendedBits ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;signExtendedBits instr n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; (instr &lt;span class=&quot;ot&quot;&gt;`shiftL`&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; n)) &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; (&lt;span class=&quot;dv&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; n)&lt;/span&gt;
&lt;span id=&quot;cb8-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;decodeInstruction' ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nibble&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B0001&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; destReg &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      sourceReg1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      mSource2 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;`testBit`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b11 &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; decodeRegister instr&lt;/span&gt;
&lt;span id=&quot;cb8-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Right&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; mSource2 &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; source2 &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ADD&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb8-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; instr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First, we extract and decode the destination and first source register. Then depending on the sixth bit, we either decode the second source register or the value. The value is &lt;a href=&quot;https://en.wikipedia.org/wiki/Sign_extension&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;sign extended&lt;/a&gt; by calling the helper function &lt;code&gt;signExtendedBits&lt;/code&gt;. When decoding the second register, if we find that the forth and fifth bits are not &lt;code&gt;0&lt;/code&gt;, that means this is not a valid &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ADD&lt;/span&gt;&lt;/code&gt; instruction. In that case, we return the raw bytes wrapped in the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;RAW&lt;/span&gt;&lt;/code&gt; constructor, because the malformed instructions are far more likely to be data that happens to have an opcode prefix. Capturing them as &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;RAW&lt;/span&gt;&lt;/code&gt; lets us treat them as data in the later steps.&lt;/p&gt;
&lt;p&gt;This same pattern is used in decoding other instructions as well: extract and decode registers, flags, and values, and check fixed bits for validity. If valid, return the decoded instruction, or else the raw bytes.&lt;/p&gt;
&lt;h3 id=&quot;and&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;AND&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#and&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;AND&lt;/span&gt;&lt;/code&gt; instruction has exactly same format as the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ADD&lt;/span&gt;&lt;/code&gt; instruction, except for the different opcode.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-and.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;AND&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B0101&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; destReg &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      sourceReg1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      mSource2 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;`testBit`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b11 &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; decodeRegister instr&lt;/span&gt;
&lt;span id=&quot;cb9-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Right&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; mSource2 &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; source2 &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;AND&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb9-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; instr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;not&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;NOT&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#not&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;To decode the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;NOT&lt;/span&gt;&lt;/code&gt; instruction, we decode the registers and check for the fixed bits.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-not.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;NOT&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B1001&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; instr &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b111111 &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b111111 &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; destReg &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        sourceReg &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;NOT&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;}&lt;/span&gt;
&lt;span id=&quot;cb10-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; instr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;br&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;BR&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#br&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Decoding the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;BR&lt;/span&gt;&lt;/code&gt; instruction requires us to decode the condition flags, which is a straightforward mapping from their bit patterns.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-br.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;BR&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B0000&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; flagSet &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; tribit &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B000&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B001&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; Set.fromList [&lt;span class=&quot;dt&quot;&gt;FlagPos&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb11-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B010&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; Set.fromList [&lt;span class=&quot;dt&quot;&gt;FlagZer&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb11-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B100&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; Set.fromList [&lt;span class=&quot;dt&quot;&gt;FlagNeg&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb11-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B011&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; Set.fromList [&lt;span class=&quot;dt&quot;&gt;FlagZer&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;FlagPos&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb11-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B110&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; Set.fromList [&lt;span class=&quot;dt&quot;&gt;FlagNeg&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;FlagZer&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb11-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B101&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; Set.fromList [&lt;span class=&quot;dt&quot;&gt;FlagNeg&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;FlagPos&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb11-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;B111&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; Set.fromList [&lt;span class=&quot;dt&quot;&gt;FlagNeg&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;FlagZer&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;FlagPos&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb11-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; flagSet &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; c &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Char&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;chr&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; instr&lt;/span&gt;
&lt;span id=&quot;cb11-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;           &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Char&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;isAscii&lt;/span&gt; c &lt;span class=&quot;kw&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; c &lt;span class=&quot;kw&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; instr&lt;/span&gt;
&lt;span id=&quot;cb11-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; flags &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BR&lt;/span&gt; {&lt;/span&gt;
&lt;span id=&quot;cb11-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            pcOffset &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb11-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            label &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;,&lt;/span&gt;
&lt;span id=&quot;cb11-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            raw &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; instr,&lt;/span&gt;
&lt;span id=&quot;cb11-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In case no flags are set, the instruction is likely to be an ASCII character instead with the high byte set to &lt;code class=&quot;sourceCode haskell hs&quot;&gt;b0000_0000&lt;/code&gt;, so we add a special check for that. Note that we capture the raw byte in the output instruction, to be used later.&lt;/p&gt;
&lt;h3 id=&quot;retjmp&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;RET&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JMP&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#retjmp&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;RET&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JMP&lt;/span&gt;&lt;/code&gt; instructions have same opcode, but differ in the bits 7–9, which are set to &lt;code&gt;111&lt;/code&gt; for &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;RET&lt;/span&gt;&lt;/code&gt;. Note that the function also checks for the fixed bits, failing which it returns a &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;RAW&lt;/span&gt;&lt;/code&gt; instruction.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3 / Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3 / Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-ret.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;RET&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JMP&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B1100&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (tribit &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;, instr &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b111111) &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;B000&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; tribit &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;B111&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RET&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Tribit&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;JMP&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; decodeRegister n&lt;/span&gt;
&lt;span id=&quot;cb12-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; instr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;jsrjsrr&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JSR&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JSRR&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#jsrjsrr&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JSR&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JSRR&lt;/span&gt;&lt;/code&gt; instructions have the same opcode, differing in the 12th bit.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3 / Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3 / Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-jsr.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JSR&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JSRR&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B0100&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb13-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;11&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;`testBit`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb13-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;JSR&lt;/span&gt; (signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;11&lt;/span&gt;) &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; instr&lt;/span&gt;
&lt;span id=&quot;cb13-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb13-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; ((instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b11, instr &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1111_11) &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb13-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;JSRR&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb13-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; instr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;ldldildr&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LD&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDI&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDR&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#ldldildr&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Decoding the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LD&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDI&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDR&lt;/span&gt;&lt;/code&gt; instructions is a straightforward matter of decoding their registers and offsets. Notice that the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LD&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDI&lt;/span&gt;&lt;/code&gt; instructions also capture the raw bytes.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3 // Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3 // Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-ld.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LD&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDI&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDR&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb14&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb14-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B0010&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LD&lt;/span&gt; (decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) (signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) instr&lt;/span&gt;
&lt;span id=&quot;cb14-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B1010&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LDI&lt;/span&gt; (decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) (signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) instr&lt;/span&gt;
&lt;span id=&quot;cb14-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B0110&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; destReg &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      baseReg &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      offset &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb14-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LDR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;ststistr&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ST&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;STI&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;STR&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#ststistr&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Decoding the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ST&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;STI&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;STR&lt;/span&gt;&lt;/code&gt; instructions is similar to the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LD&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDI&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDR&lt;/span&gt;&lt;/code&gt; instructions.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3 // Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3 // Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-st.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ST&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;STI&lt;/span&gt;&lt;/code&gt;/&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;STR&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb15&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb15-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B0011&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; (decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) (signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) instr&lt;/span&gt;
&lt;span id=&quot;cb15-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B1011&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STI&lt;/span&gt; (decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) (signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) instr&lt;/span&gt;
&lt;span id=&quot;cb15-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B0111&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; sourceReg &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      baseReg &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      offset &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb15-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;lea&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LEA&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#lea&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Again, straightforward decoding of register and offset:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-lea.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LEA&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb16&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb16-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb16-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B1110&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb16-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LEA&lt;/span&gt; (decodeRegister &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;) (signExtendedBits instr &lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;rti&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;RTI&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#rti&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;RTI&lt;/span&gt;&lt;/code&gt; instruction is a constant byte pattern.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-rti.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;RTI&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb17&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb17-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb17-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B1000&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; instr &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1111_1111_1111 &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb17-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RTI&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb17-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; instr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;trap&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;TRAP&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#trap&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Decoding the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;TRAP&lt;/span&gt;&lt;/code&gt; instruction requires us to decode the trap routines.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-trap.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;TRAP&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb18&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb18-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb18-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' instr &lt;span class=&quot;dt&quot;&gt;B1111&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (instr &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;8&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1111 &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb18-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb18-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TRAP&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; decodeTrapRoutine &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; instr &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1111_1111&lt;/span&gt;
&lt;span id=&quot;cb18-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb18-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; instr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;invalid&quot;&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;INVALID&lt;/span&gt;&lt;/code&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#invalid&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;We don’t care about the rest of the bytes if the opcode is for the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;INVALID&lt;/span&gt;&lt;/code&gt; instruction.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;lazyload w-50pct nolink&quot; /&gt;
&amp;lt;noscript&amp;gt;&lt;img alt=&quot;LC-3  Instruction&quot; class=&quot;w-50pct nolink&quot; src=&quot;https://abhinavsarkar.net/images/lc3-disassembler/lc3-resv.svg&quot; /&gt;&amp;lt;/noscript&amp;gt;
&lt;figcaption&gt;LC-3 &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;INVALID&lt;/span&gt;&lt;/code&gt; Instruction&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb19&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb19-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb19-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;decodeInstruction' _ &lt;span class=&quot;dt&quot;&gt;B1101&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;INVALID&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s it for decoding the bytes to instructions. But that was the easy part. Up next, we get into the heuristic operations.&lt;/p&gt;
&lt;h2 id=&quot;basic-disassembly&quot;&gt;Basic Disassembly&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#basic-disassembly&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;disassembleInstrs&lt;/code&gt;, the function below, is our main entry point to disassembling the binary code. It runs in the &lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt;&lt;/code&gt; monad so that we can work with a mutable vector of instructions.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb20&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb20-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb20-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;disassembleInstrs ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BinCode&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb20-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb20-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;disassembleInstrs code &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; runST &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb20-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  instructions &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; MV.replicate (UV.length code) &lt;span class=&quot;dt&quot;&gt;INVALID&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb20-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  disassembleInstr code instructions &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb20-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  fixInstrs instructions&lt;/span&gt;
&lt;span id=&quot;cb20-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb20-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  V.freeze instructions &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;amp;&amp;gt;&lt;/span&gt; (V.toList &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; combineCharInstrs &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; insertLabels)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First we allocate the mutable vector, and then we do a basic disassembly of the binary code into it by calling &lt;code&gt;disassembleInstr&lt;/code&gt;. After that, we go over the disassembled instructions in &lt;code&gt;fixInstrs&lt;/code&gt;, and use heuristics to guess better ones. Finally, we clean up by combining the character data and inserting the labels, and return the final immutable vector of instructions.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb21&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb21-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MInstructions&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MV.MVector&lt;/span&gt; s &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;disassembleInstr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BinCode&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MInstructions&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; s ()&lt;/span&gt;
&lt;span id=&quot;cb21-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;disassembleInstr code instructions idx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; when (idx &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt; UV.length code) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  MV.read instructions idx &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; disassembleInstr code instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; disassembleInstr code instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; disassembleInstr code instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; raw &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; code &lt;span class=&quot;op&quot;&gt;UV.!&lt;/span&gt; idx&lt;/span&gt;
&lt;span id=&quot;cb21-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; instr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; decodeInstruction raw&lt;/span&gt;
&lt;span id=&quot;cb21-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      prevInstr &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; MV.readMaybe instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (instr, prevInstr) &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        (&lt;span class=&quot;dt&quot;&gt;TRAP&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapOUT&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;LD&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR0&lt;/span&gt; pcOffset _)) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; idx' &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; pcOffset&lt;/span&gt;
&lt;span id=&quot;cb21-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          disassembleChar instructions idx' &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; code &lt;span class=&quot;op&quot;&gt;UV.!?&lt;/span&gt; idx'&lt;/span&gt;
&lt;span id=&quot;cb21-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        (&lt;span class=&quot;dt&quot;&gt;TRAP&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapPUTS&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;LEA&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR0&lt;/span&gt; pcOffset)) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          disassembleW16String code instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; pcOffset&lt;/span&gt;
&lt;span id=&quot;cb21-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        (&lt;span class=&quot;dt&quot;&gt;TRAP&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapPUTSP&lt;/span&gt;,  &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;LEA&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RegR0&lt;/span&gt; pcOffset)) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          disassembleW8String code instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; pcOffset&lt;/span&gt;
&lt;span id=&quot;cb21-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb21-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      MV.write instructions idx instr&lt;/span&gt;
&lt;span id=&quot;cb21-25&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb21-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      disassembleInstr code instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;disassembleInstr&lt;/code&gt; is where our first heuristic is applied. We go over the binary code vector element-by-element, decoding each raw word using the &lt;code&gt;decodeInstruction&lt;/code&gt; function we wrote earlier. But we don’t stop there, we do an additional check. If a freshly decoded instruction is one of some &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;TRAP&lt;/span&gt;&lt;/code&gt; instructions, preceded by some particular instructions, we use it to further disassemble some data. We recognize these three patterns:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LD&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;R0&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pcOffset&lt;/span&gt;&lt;/code&gt; followed by &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;OUT&lt;/span&gt;&lt;/code&gt;: This pattern loads a character at the relative &lt;code&gt;pcOffset&lt;/code&gt; into the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;va&quot;&gt;R0&lt;/span&gt;&lt;/code&gt; registers and prints it. If we see this pattern, we know that the instruction at current index plus &lt;code&gt;pcOffset&lt;/code&gt; is a character. So we disassemble it immediately as a character in &lt;code&gt;disassembleChar&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LEA&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;R0&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pcOffset&lt;/span&gt;&lt;/code&gt; followed by &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;PUTS&lt;/span&gt;&lt;/code&gt;: This signals that the data at the relative &lt;code&gt;pcOffset&lt;/code&gt; is a null-terminated one-character-per-word string. So we disassemble it character-by-character till we reach the null bit in &lt;code&gt;disassembleW16String&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LEA&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;R0&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pcOffset&lt;/span&gt;&lt;/code&gt; followed by &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;PUTSP&lt;/span&gt;&lt;/code&gt;: This pattern is for a null-terminated two-characters-per-word string, so we disassemble accordingly in &lt;code&gt;disassembleW8String&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb22&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb22-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;disassembleChar ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MInstructions&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; s ()&lt;/span&gt;
&lt;span id=&quot;cb22-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;disassembleChar instructions idx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; raw &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; MV.write instructions idx &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Char&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;chr&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; raw&lt;/span&gt;
&lt;span id=&quot;cb22-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb22-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;disassembleW16String ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BinCode&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MInstructions&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; s ()&lt;/span&gt;
&lt;span id=&quot;cb22-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;disassembleW16String code instructions idx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; raw &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; code &lt;span class=&quot;op&quot;&gt;UV.!?&lt;/span&gt; idx&lt;/span&gt;
&lt;span id=&quot;cb22-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  disassembleChar instructions idx raw&lt;/span&gt;
&lt;span id=&quot;cb22-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; raw &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb22-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb22-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; disassembleW16String code instructions (idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb22-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;disassembleW8String ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BinCode&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MInstructions&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; s ()&lt;/span&gt;
&lt;span id=&quot;cb22-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;disassembleW8String code instructions idx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; code &lt;span class=&quot;op&quot;&gt;UV.!?&lt;/span&gt; idx &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; raw &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; b1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; raw &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1111_1111&lt;/span&gt;
&lt;span id=&quot;cb22-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        b2 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (raw &lt;span class=&quot;ot&quot;&gt;`shiftR`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;8&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&amp;amp;.&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;b1111_1111&lt;/span&gt;
&lt;span id=&quot;cb22-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    MV.write instructions idx &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Char&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;chr&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt;) [b1, b2]&lt;/span&gt;
&lt;span id=&quot;cb22-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    unless (raw &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; disassembleW8String code instructions (idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb22-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb22-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; ()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These patterns are very common and almost standard, so we can confidently use them for detecting character and string data. Also, note the checks in the beginning of &lt;code&gt;disassembleInstr&lt;/code&gt; that prevent already disassembled char/string data from being overwritten.&lt;/p&gt;
&lt;p&gt;At this point, we have decoded the instructions and char/string data. But there are more possibilities of improving the disassembly quality.&lt;/p&gt;
&lt;h2 id=&quot;fixed-point-cleanup&quot;&gt;Fixed-Point Cleanup&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fixed-point-cleanup&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this pass, we revisit the decoded instructions and correct them based on their targets and neighbours:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb23&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb23-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;fixInstrs ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MInstructions&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; s ()&lt;/span&gt;
&lt;span id=&quot;cb23-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fixInstrs instructions &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; codeSize &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; MV.length instructions&lt;/span&gt;
&lt;span id=&quot;cb23-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  backwardChanged &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; forM [codeSize &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;, codeSize &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;] (fixInstr instructions)&lt;/span&gt;
&lt;span id=&quot;cb23-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  forwardChanged &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; forM [&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; codeSize &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;] (fixInstr instructions)&lt;/span&gt;
&lt;span id=&quot;cb23-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  when (backwardChanged &lt;span class=&quot;op&quot;&gt;||&lt;/span&gt; forwardChanged) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb23-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    fixInstrs instructions&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;fixInstrs&lt;/code&gt; runs till a fixed point is reached, that is to say, till the instructions stop changing. It does a backward sweep with the &lt;code&gt;fixInstr&lt;/code&gt; function, then a forward sweep, then repeats until no changes occur. We do this because a rewrite can create conditions for another rewrite. Doing this till a fixed point is reached is safe because of the way &lt;code&gt;fixInstr&lt;/code&gt; is written: it only changes code-like instructions to data-like instructions, and never the other way. That makes the whole pass monotonic, causing it to eventually terminate. We do the sweep in both directions to make the changes order-independent. Next up is &lt;code&gt;fixInstr&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb24&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb24-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TargetCheck&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MayBeData&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MustBeCode&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NoCheck&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;fixInstr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MInstructions&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; s &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fixInstr instructions idx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; MV.read instructions idx &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LD&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; changeInstr instructions &lt;span class=&quot;dt&quot;&gt;NoCheck&lt;/span&gt; pcOffset raw idx&lt;/span&gt;
&lt;span id=&quot;cb24-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LDI&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; changeInstr instructions &lt;span class=&quot;dt&quot;&gt;NoCheck&lt;/span&gt; pcOffset raw idx&lt;/span&gt;
&lt;span id=&quot;cb24-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; changeInstr instructions &lt;span class=&quot;dt&quot;&gt;MayBeData&lt;/span&gt; pcOffset raw idx&lt;/span&gt;
&lt;span id=&quot;cb24-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STI&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; changeInstr instructions &lt;span class=&quot;dt&quot;&gt;NoCheck&lt;/span&gt; pcOffset raw idx&lt;/span&gt;
&lt;span id=&quot;cb24-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;JSR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; changeInstr instructions &lt;span class=&quot;dt&quot;&gt;MustBeCode&lt;/span&gt; pcOffset raw idx&lt;/span&gt;
&lt;span id=&quot;cb24-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;BR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; changeInstr instructions &lt;span class=&quot;dt&quot;&gt;MustBeCode&lt;/span&gt; pcOffset raw idx&lt;/span&gt;
&lt;span id=&quot;cb24-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb24-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The idea behind &lt;code&gt;fixInstr&lt;/code&gt; is that, certain instructions that point to other instructions are likely to be data. Different instructions have different confidence levels for their target addresses, encoded as &lt;code&gt;TargetCheck&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;MustBeCode&lt;/span&gt;&lt;/code&gt;, used for &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;JSR&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;BR&lt;/span&gt;&lt;/code&gt;: If the target is known to be data or does not exist, this instruction must be wrong, because these instructions are for jumping to code. Rewrite the instruction to &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;RAW&lt;/span&gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;MayBeData&lt;/span&gt;&lt;/code&gt;, used for &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ST&lt;/span&gt;&lt;/code&gt;: &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;ST&lt;/span&gt;&lt;/code&gt; may or may not point to data. But if the instruction is in a neighbourhood that mostly has data, it is very likely to be data itself. Rewrite such instruction as &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;SPECULATION&lt;/span&gt;&lt;/code&gt;, capturing both the data and instruction forms.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;sourceCode haskell hs&quot;&gt;&lt;span class=&quot;dt&quot;&gt;NoCheck&lt;/span&gt;&lt;/code&gt;, used for &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LD&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;LDI&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;kw&quot;&gt;STI&lt;/span&gt;&lt;/code&gt;: The target doesn’t strongly indicate code or data. However, if &lt;code&gt;pcOffset&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt; as well, the instruction is likely to be data if present in a data-heavy neighbourhood. In such case, rewrite as &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;SPECULATION&lt;/span&gt;&lt;/code&gt;, otherwise ignore.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;changeInstr&lt;/code&gt; below carries out the actual rewrites:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb25&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb25-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;changeInstr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MInstructions&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TargetCheck&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Word16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; s &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;changeInstr instructions targetCheck pcOffset raw idx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; newInstr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; raw&lt;/span&gt;
&lt;span id=&quot;cb25-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      targetIdx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; pcOffset&lt;/span&gt;
&lt;span id=&quot;cb25-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      writeNewInstr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; MV.write instructions idx newInstr &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      maybeWriteSpeculatedInstr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        prevInstr &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; MV.readMaybe instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        nextInstr &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; MV.readMaybe instructions &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; idx &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; isSpeculatedDataInstr prevInstr &lt;span class=&quot;op&quot;&gt;||&lt;/span&gt; isSpeculatedDataInstr nextInstr&lt;/span&gt;
&lt;span id=&quot;cb25-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;kw&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            oInstr &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; MV.read instructions idx&lt;/span&gt;
&lt;span id=&quot;cb25-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            MV.write instructions idx &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SPECULATION&lt;/span&gt; oInstr newInstr&lt;/span&gt;
&lt;span id=&quot;cb25-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;kw&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  mTargetInstr &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; MV.readMaybe instructions targetIdx&lt;/span&gt;
&lt;span id=&quot;cb25-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (targetCheck, mTargetInstr) &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;MustBeCode&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; targetInstr) &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; isDataInstr targetInstr &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; writeNewInstr&lt;/span&gt;
&lt;span id=&quot;cb25-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;MustBeCode&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; _) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;MustBeCode&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; writeNewInstr&lt;/span&gt;
&lt;span id=&quot;cb25-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;MayBeData&lt;/span&gt;, _) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; maybeWriteSpeculatedInstr&lt;/span&gt;
&lt;span id=&quot;cb25-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;NoCheck&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; _) &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; pcOffset &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; maybeWriteSpeculatedInstr&lt;/span&gt;
&lt;span id=&quot;cb25-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb25-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;NoCheck&lt;/span&gt;, _) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The two helper predicates distinguish definite data and speculative data:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb26&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb26-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;isDataInstr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;isDataInstr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;SPECULATION&lt;/span&gt; oi _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; isDataInstr oi&lt;/span&gt;
&lt;span id=&quot;cb26-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;isSpeculatedDataInstr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;isSpeculatedDataInstr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;SPECULATION&lt;/span&gt; _ si) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; isDataInstr si&lt;/span&gt;
&lt;span id=&quot;cb26-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; isDataInstr i&lt;/span&gt;
&lt;span id=&quot;cb26-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb26-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;isDataInstr&lt;/code&gt; checks for &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;CHAR&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;STRING&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;RAW&lt;/span&gt;&lt;/code&gt;, and the original side of &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;SPECULATION&lt;/span&gt;&lt;/code&gt;. &lt;code&gt;isSpeculatedDataInstr&lt;/code&gt; checks for the speculative data alternative.&lt;/p&gt;
&lt;p&gt;Note how speculative data can cause neighbouring instructions to become data, but cannot alone cause a branch/call instruction to change, thus limiting speculation from spreading.&lt;/p&gt;
&lt;h2 id=&quot;postprocessing&quot;&gt;Postprocessing&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#postprocessing&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After the previous pass the instructions become stable. We now process them to do some cleanups. First, we merge consecutive characters into strings:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb27&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb27-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;combineCharInstrs ::&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb27-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;combineCharInstrs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;concat&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; List.chop \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  l&lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;(&lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; _ &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; rest) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;span&lt;/span&gt; isNonNullChar l &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ([], _) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ([&lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;], rest)&lt;/span&gt;
&lt;span id=&quot;cb27-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (cs, rest') &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; rest' &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (&lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; &lt;span class=&quot;ch&quot;&gt;'\0'&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; rrest) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ([&lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [c &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; c &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; cs] &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; [&lt;span class=&quot;ch&quot;&gt;'\0'&lt;/span&gt;]], rrest)&lt;/span&gt;
&lt;span id=&quot;cb27-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ([toRAW c &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; c &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; cs], rest')&lt;/span&gt;
&lt;span id=&quot;cb27-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  l&lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;(&lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; _ &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; _) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; (ss, rest) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;span&lt;/span&gt; isNonNullString l&lt;/span&gt;
&lt;span id=&quot;cb27-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; rest &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          (&lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; [&lt;span class=&quot;ch&quot;&gt;'\0'&lt;/span&gt;, &lt;span class=&quot;ch&quot;&gt;'\0'&lt;/span&gt;] &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; rrest) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            ([&lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;concat&lt;/span&gt; [[c1, c2] &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; [c1, c2] &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; ss] &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; [&lt;span class=&quot;ch&quot;&gt;'\0'&lt;/span&gt;]], rrest)&lt;/span&gt;
&lt;span id=&quot;cb27-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; toRAW &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;concat&lt;/span&gt; [[c1, c2] &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; [c1, c2] &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; ss], rest)&lt;/span&gt;
&lt;span id=&quot;cb27-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;~&lt;/span&gt;(i &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; rest) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ([i], rest)&lt;/span&gt;
&lt;span id=&quot;cb27-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    isNonNullChar &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; &lt;span class=&quot;ch&quot;&gt;'\0'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    isNonNullString &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; [&lt;span class=&quot;ch&quot;&gt;'\0'&lt;/span&gt;, &lt;span class=&quot;ch&quot;&gt;'\0'&lt;/span&gt;] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-25&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-26&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb27-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    toRAW &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Char&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;ord&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;combineCharInstrs&lt;/code&gt; uses &lt;a href=&quot;https://hackage.haskell.org/package/split/docs/Data-List-Split.html#v:chop&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;chop&lt;/code&gt;&lt;/a&gt; to walk the instruction list and combine:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a run of &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;CHAR&lt;/span&gt;&lt;/code&gt;s followed by &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;CHAR&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;'&lt;/span&gt;&lt;/code&gt; to become a single &lt;code&gt;STRING&lt;/code&gt; with the &lt;a href=&quot;https://en.wikipedia.org/wiki/Null_character&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;NUL&lt;/code&gt;&lt;/a&gt; terminator, and&lt;/li&gt;
&lt;li&gt;a run of two-character &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;STRING&lt;/span&gt;&lt;/code&gt; entries followed by &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;STRING&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\0\0&quot;&lt;/span&gt;&lt;/code&gt; to become a single combined &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;STRING&lt;/span&gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If a run is not &lt;code&gt;NUL&lt;/code&gt;-terminated, we revert the characters in it to &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;RAW&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, we insert the labels before the instructions that other instructions point to:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb28&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb28-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;insertLabels ::&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb28-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;insertLabels instrs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; indexedInstrs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;zip&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;] instrs&lt;/span&gt;
&lt;span id=&quot;cb28-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      labelIdxs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; List.nub &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        [offsetIdx i pcOffset &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; (i, &lt;span class=&quot;dt&quot;&gt;BR&lt;/span&gt; {pcOffset}) &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; indexedInstrs]&lt;/span&gt;
&lt;span id=&quot;cb28-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; [offsetIdx i pcOffset &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; (i, &lt;span class=&quot;dt&quot;&gt;JSR&lt;/span&gt; {pcOffset}) &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; indexedInstrs]&lt;/span&gt;
&lt;span id=&quot;cb28-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;flip&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;concatMap&lt;/span&gt; indexedInstrs \(i, instr) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; instr &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        br&lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;BR&lt;/span&gt; {pcOffset} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [br { label &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; mkLabel &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; offsetIdx i pcOffset }]&lt;/span&gt;
&lt;span id=&quot;cb28-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        jsr&lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;JSR&lt;/span&gt; {pcOffset} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [jsr { label &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; mkLabel &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; offsetIdx i pcOffset }]&lt;/span&gt;
&lt;span id=&quot;cb28-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        _ &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; i &lt;span class=&quot;ot&quot;&gt;`elem`&lt;/span&gt; labelIdxs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;LABEL&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; mkLabel i, instr]&lt;/span&gt;
&lt;span id=&quot;cb28-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [instr]&lt;/span&gt;
&lt;span id=&quot;cb28-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    offsetIdx i offset &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; i &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; offset&lt;/span&gt;
&lt;span id=&quot;cb28-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb28-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    mkLabel i &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;LBL_&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; i&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We also change the pointing instructions to use the labels instead of offsets.&lt;/p&gt;
&lt;p&gt;This concludes the entire disassembly of LC-3 binary code. To finish off, we need functions to print the instructions and to read the binary files.&lt;/p&gt;
&lt;h2 id=&quot;pretty-printing&quot;&gt;Pretty-Printing&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#pretty-printing&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;disassemble&lt;/code&gt; is our top-level function that takes an object file and returns the disassembly as text:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb29&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb29-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb29-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;disassemble ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb29-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;disassemble &lt;span class=&quot;dt&quot;&gt;Object&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb29-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; instrs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; disassembleInstrs code&lt;/span&gt;
&lt;span id=&quot;cb29-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb29-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;unlines&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb29-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        [&lt;span class=&quot;st&quot;&gt;&quot;.ORIG x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; printf &lt;span class=&quot;st&quot;&gt;&quot;%04X&quot;&lt;/span&gt; origin]&lt;/span&gt;
&lt;span id=&quot;cb29-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb29-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; ppInstruction instrs&lt;/span&gt;
&lt;span id=&quot;cb29-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb29-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;.END&quot;&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;disassemble&lt;/code&gt; simply pretty prints each disassembled instruction and wraps the result with &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.ORIG&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.END&lt;/span&gt;&lt;/code&gt; directives from LC-3 assembler format. Here is the pretty printer:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb30&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb30-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppInstruction ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Instruction&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppInstruction &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ADD&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;st&quot;&gt;&quot;ADD &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister destReg&lt;/span&gt;
&lt;span id=&quot;cb30-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister sourceReg1&lt;/span&gt;
&lt;span id=&quot;cb30-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;either&lt;/span&gt; ppRegister ppNum source2&lt;/span&gt;
&lt;span id=&quot;cb30-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;AND&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;st&quot;&gt;&quot;AND &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister destReg&lt;/span&gt;
&lt;span id=&quot;cb30-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister sourceReg1&lt;/span&gt;
&lt;span id=&quot;cb30-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;either&lt;/span&gt; ppRegister ppNum source2&lt;/span&gt;
&lt;span id=&quot;cb30-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;BR&lt;/span&gt; {label &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; lbl, &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;BR&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppFlags flags &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; lbl&lt;/span&gt;
&lt;span id=&quot;cb30-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;BR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;BR&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppFlags flags &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum pcOffset&lt;/span&gt;
&lt;span id=&quot;cb30-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;JMP&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;JMP &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister baseReg&lt;/span&gt;
&lt;span id=&quot;cb30-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;RET&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;RET&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;JSR&lt;/span&gt; {label &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; lbl} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;JSR &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; lbl&lt;/span&gt;
&lt;span id=&quot;cb30-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;JSR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;JSR &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum pcOffset&lt;/span&gt;
&lt;span id=&quot;cb30-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;JSRR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;JSRR &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister baseReg&lt;/span&gt;
&lt;span id=&quot;cb30-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LD&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;LD &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister destReg &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum pcOffset&lt;/span&gt;
&lt;span id=&quot;cb30-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LDI&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;LDI &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister destReg &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum pcOffset&lt;/span&gt;
&lt;span id=&quot;cb30-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LDR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;st&quot;&gt;&quot;LDR &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister destReg&lt;/span&gt;
&lt;span id=&quot;cb30-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister baseReg&lt;/span&gt;
&lt;span id=&quot;cb30-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum offset&lt;/span&gt;
&lt;span id=&quot;cb30-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LEA&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;LEA &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister destReg &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum pcOffset&lt;/span&gt;
&lt;span id=&quot;cb30-25&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;NOT&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;NOT &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister destReg &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister sourceReg&lt;/span&gt;
&lt;span id=&quot;cb30-26&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;RTI&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;RTI&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-27&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ST&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;ST &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister sourceReg &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum pcOffset&lt;/span&gt;
&lt;span id=&quot;cb30-28&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STI&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;STI &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister sourceReg &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum pcOffset&lt;/span&gt;
&lt;span id=&quot;cb30-29&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STR&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-30&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;st&quot;&gt;&quot;STR &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister sourceReg&lt;/span&gt;
&lt;span id=&quot;cb30-31&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppRegister baseReg&lt;/span&gt;
&lt;span id=&quot;cb30-32&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppNum offset&lt;/span&gt;
&lt;span id=&quot;cb30-33&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;TRAP&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ppTrapRoutine trapRoutine&lt;/span&gt;
&lt;span id=&quot;cb30-34&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;INVALID&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;INVALID&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-35&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;RAW&lt;/span&gt; w &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;.FILL x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; printf &lt;span class=&quot;st&quot;&gt;&quot;%04X&quot;&lt;/span&gt; w&lt;/span&gt;
&lt;span id=&quot;cb30-36&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;CHAR&lt;/span&gt; c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; c&lt;/span&gt;
&lt;span id=&quot;cb30-37&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STRING&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;.STRINGZ \&quot;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;concatMap&lt;/span&gt; translateEscapeSeqs (&lt;span class=&quot;fu&quot;&gt;init&lt;/span&gt; s) &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-38&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;SPECULATION&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;} &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-39&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ppInstruction specualtedInstr &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; ; or &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; ppInstruction origInstr&lt;/span&gt;
&lt;span id=&quot;cb30-40&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb30-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;LABEL&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\n&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; s&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The instructions are rendered in the LC-3 assembly syntax. &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;RAW&lt;/span&gt;&lt;/code&gt; words become &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.FILL&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;xNNN&lt;/span&gt;&lt;/code&gt; directives. &lt;code&gt;STRING&lt;/code&gt; values become &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.STRINGZ&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;...&quot;&lt;/span&gt;&lt;/code&gt; with proper escape sequences. &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;fu&quot;&gt;SPECULATION&lt;/span&gt;&lt;/code&gt; instructions are rendered with both interpretations: the speculated data form followed by the original instruction as a comment. This preserves ambiguity for human review.&lt;/p&gt;
&lt;p&gt;The helper pretty-printers:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb31&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb31-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppRegister ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Register&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppRegister &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;st&quot;&gt;&quot;R&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromEnum&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppNum ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int16&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppNum &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;st&quot;&gt;&quot;#&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppFlags ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set.Set&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Flag&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppFlags flags &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;concatMap&lt;/span&gt; ppFlag &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; Set.toAscList flags &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;st&quot;&gt;&quot;nzp&quot;&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  fs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; fs&lt;/span&gt;
&lt;span id=&quot;cb31-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ppFlag &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;FlagNeg&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;n&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;FlagZer&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;z&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;FlagPos&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;p&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppTrapRoutine ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TrapRoutine&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppTrapRoutine &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;TrapGETC&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;GETC&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;TrapOUT&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;OUT&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;TrapPUTS&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;PUTS&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;TrapIN&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;IN&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;TrapPUTSP&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;PUTSP&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;TrapHALT&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;HALT&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-25&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;TrapUNKNOWN&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;TRAP &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb31-26&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-27&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;translateEscapeSeqs ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Char&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-28&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;translateEscapeSeqs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-29&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\a'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\a&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-30&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\b'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\b&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-31&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\ESC'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\e&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-32&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\f'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\f&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-33&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\n'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\n&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-34&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\r'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\r&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-35&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\t'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\t&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-36&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\v'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\v&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-37&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'\\'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\\\&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-38&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ch&quot;&gt;'&quot;'&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\\&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-39&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb31-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [c]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The registers are printed as &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;va&quot;&gt;R0&lt;/span&gt;&lt;/code&gt;–&lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;va&quot;&gt;R7&lt;/span&gt;&lt;/code&gt;. Numeric values are prefixed with &lt;code&gt;#&lt;/code&gt;, and the escape sequences (&lt;code&gt;\n&lt;/code&gt;, &lt;code&gt;\t&lt;/code&gt;, etc.) are properly encoded in strings. The trap routines are looked up by their standard codes and output as their names.&lt;/p&gt;
&lt;h2 id=&quot;reading-the-binary&quot;&gt;Reading the Binary&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#reading-the-binary&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;After all we’ve been through, this is a piece of cake:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb32&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb32-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;readObject ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BS.ByteString&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Object&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;readObject blob&lt;/span&gt;
&lt;span id=&quot;cb32-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; BS.length blob &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Blob is too short to be valid&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;odd&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; BS.length blob &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Blob has odd number of bytes&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; runST &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; instrCount &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (BS.length blob &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;`div`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          origin &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            (&lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; (BS.index blob &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;`shiftL`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;8&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb32-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;              &lt;span class=&quot;op&quot;&gt;.|.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; (BS.index blob &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb32-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      instructions &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; MUV.new instrCount&lt;/span&gt;
&lt;span id=&quot;cb32-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      forM_ [&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt; instrCount] \i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; b1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; BS.index blob &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; i &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            b2 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fromIntegral&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; BS.index blob &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; i &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            b &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; b1 &lt;span class=&quot;ot&quot;&gt;`shiftL`&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.|.&lt;/span&gt; b2&lt;/span&gt;
&lt;span id=&quot;cb32-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        MUV.write instructions (i &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) b&lt;/span&gt;
&lt;span id=&quot;cb32-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      code &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; UV.freeze instructions&lt;/span&gt;
&lt;span id=&quot;cb32-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb32-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Right&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Object&lt;/span&gt; {&lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Only important thing to note is to combine the byte pairs in the &lt;a href=&quot;https://en.wikipedia.org/wiki/big-endian&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;big-endian&lt;/a&gt; order.&lt;/p&gt;
&lt;h2 id=&quot;finishing-up&quot;&gt;Finishing Up&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#finishing-up&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;main&lt;/code&gt; function reads a binary &lt;code&gt;.obj&lt;/code&gt; file from STDIN, parses it, and disassembles it to STDOUT:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb33&quot;&gt;&lt;pre class=&quot;sourceCode haskell noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb33-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb33-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;main ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb33-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb33-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;main &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; BS.getContents &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;amp;&amp;gt;&lt;/span&gt; readObject &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb33-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; err &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; err &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; exitFailure&lt;/span&gt;
&lt;span id=&quot;cb33-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb33-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Right&lt;/span&gt; object &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;putStr&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; disassemble object&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Error messages are printed and the program exits with status 1 on failure.&lt;/p&gt;
&lt;p&gt;And, we are done!&lt;/p&gt;
&lt;p&gt;Let’s test it on the &lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#hello-world&quot;&gt;“Hello World!” program&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;plain&quot;&gt;&lt;code&gt;$ ghc -Wall --make lc3-dis.hs -O2 -o lc3d -main-is LC3Disassembler
$ cat hello.obj | ./lc3d | tee hello-dis.asm
.ORIG x3000
LEA R0, #2
PUTS
HALT
.STRINGZ &quot;Hello World!&quot;
.END
$ lc3as hello-dis.asm
$ lc3 hello-dis.obj
Hello World!&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Amazing! We assembled the disassembled program and ran it, and it produced the same output. The assembly is also pretty much the same.&lt;/p&gt;
&lt;p&gt;To make sure the disassembler works correctly, let’s run it on a the binary code for a larger program: &lt;a href=&quot;https://abhinavsarkar.net/code/lc3-fizzbuzz.html?mtm_campaign=feed&quot;&gt;lc3-fizzbuzz.asm&lt;/a&gt; that output the solution for the &lt;a href=&quot;https://en.wikipedia.org/wiki/Fizzbuzz&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Fizzbuzz&lt;/a&gt; problem.&lt;/p&gt;
&lt;pre class=&quot;plain&quot;&gt;&lt;code&gt;$ lc3as lc3-fizzbuzz.asm           # assemble
$ lc3 lc3-fizzbuzz.obj &amp;gt; out1      # run and capture output
$ cat lc3-fizzbuzz.obj | lc3d &amp;gt; lc3-fizzbuzz-dis.asm  # diassemble
$ lc3as lc3-fizzbuzz-dis.asm       # assemble the disassembly
$ lc3 lc3-fizzbuzz-dis.obj &amp;gt; out2  # run and capture output
$ diff out1 out2 &amp;amp;&amp;amp; echo &quot;Same!&quot;   # diff the outputs
Same!&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It works perfectly. You can also see the word-wise diff between original and disassembled assembly below:&lt;/p&gt;
&lt;details&gt;

Diff

&lt;pre class=&quot;terminal-output&quot;&gt;&lt;code&gt;&lt;span class=&quot;bold &quot;&gt;diff --git a/lc3-fizzbuzz.asm b/lc3-fizzbuzz-dis.asm&lt;/span&gt;
&lt;span class=&quot;bold &quot;&gt;index 4949924..6a3882a 100644&lt;/span&gt;
&lt;span class=&quot;bold &quot;&gt;--- a/lc3-fizzbuzz.asm&lt;/span&gt;
&lt;span class=&quot;bold &quot;&gt;+++ b/lc3-fizzbuzz-dis.asm&lt;/span&gt;
&lt;span class=&quot;cyan &quot;&gt;@@ -1,80 +1,79 @@&lt;/span&gt;
.ORIG x3000

AND R0, R0, #0
LD R1, &lt;span class=&quot;red &quot;&gt;[-START-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#66+}&lt;/span&gt;
LD R4, &lt;span class=&quot;red &quot;&gt;[-END-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#66+}&lt;/span&gt;
NOT R2, R4
ADD R2, R2, #1
LD R5, &lt;span class=&quot;red &quot;&gt;[-ASCII_48-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#64+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-LOOP-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_6+}&lt;/span&gt;
ADD R0, R1, R2
BRp &lt;span class=&quot;red &quot;&gt;[-DONE-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_65+}&lt;/span&gt;
ADD R3, R1, #0

&lt;span class=&quot;red &quot;&gt;[-SUB_3-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_9+}&lt;/span&gt;
ADD R0, R3, #-3
BRn &lt;span class=&quot;red &quot;&gt;[-MOD_3-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_13+}&lt;/span&gt;
ADD R3, R3, #-3
BR &lt;span class=&quot;red &quot;&gt;[-SUB_3-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_9+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-MOD_3-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_13+}&lt;/span&gt;
ADD R4, R1, #0

&lt;span class=&quot;red &quot;&gt;[-SUB_5-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_14+}&lt;/span&gt;
ADD R0, R4, #-5
BRn &lt;span class=&quot;red &quot;&gt;[-MOD_5-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_18+}&lt;/span&gt;
ADD R4, R4, #-5
BR &lt;span class=&quot;red &quot;&gt;[-SUB_5-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_14+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-MOD_5-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_18+}&lt;/span&gt;
ADD R3, R3, #0
BRz &lt;span class=&quot;red &quot;&gt;[-CHECK_MOD_5-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_24+}&lt;/span&gt;
ADD R4, R4, #0
BRz &lt;span class=&quot;red &quot;&gt;[-PRINT_BUZZ-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_29+}&lt;/span&gt;
JSR &lt;span class=&quot;red &quot;&gt;[-PRINT_NUM-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_40+}&lt;/span&gt;
BR &lt;span class=&quot;red &quot;&gt;[-PRINT_NEWLINE-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_35+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-CHECK_MOD_5-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_24+}&lt;/span&gt;
ADD R4, R4, #0
BRz &lt;span class=&quot;red &quot;&gt;[-PRINT_FIZZ-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_32+}&lt;/span&gt;
LEA R0, &lt;span class=&quot;red &quot;&gt;[-FIZZ-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#44+}&lt;/span&gt;
PUTS
BR &lt;span class=&quot;red &quot;&gt;[-PRINT_NEWLINE-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_35+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-PRINT_BUZZ-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_29+}&lt;/span&gt;
LEA R0, &lt;span class=&quot;red &quot;&gt;[-BUZZ-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#46+}&lt;/span&gt;
PUTS
BR &lt;span class=&quot;red &quot;&gt;[-PRINT_NEWLINE-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_35+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-PRINT_FIZZ-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_32+}&lt;/span&gt;
LEA R0, &lt;span class=&quot;red &quot;&gt;[-FIZZ_BUZZ-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#48+}&lt;/span&gt;
PUTS
BR &lt;span class=&quot;red &quot;&gt;[-PRINT_NEWLINE-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_35+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-PRINT_NEWLINE-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_35+}&lt;/span&gt;
AND R0, R0, #0
ADD R0, R0, #10
OUT
ADD R1, R1, #1
BR &lt;span class=&quot;red &quot;&gt;[-LOOP-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_6+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-PRINT_NUM-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_40+}&lt;/span&gt;
ST R7, &lt;span class=&quot;red &quot;&gt;[-S7-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#25+}&lt;/span&gt;
ST R1, &lt;span class=&quot;red &quot;&gt;[-S1-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#25+}&lt;/span&gt;
ADD R3, R1, #-10
BRn &lt;span class=&quot;red &quot;&gt;[-SINGLE_DIGIT-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_59+}&lt;/span&gt;
AND R4, R4, #0
AND R3, R3, #0
ADD R3, R3, R1

&lt;span class=&quot;red &quot;&gt;[-SUB_10-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_47+}&lt;/span&gt;
ADD R3, R3, #-10
BRn &lt;span class=&quot;red &quot;&gt;[-PRINT_10S_DIGIT-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_51+}&lt;/span&gt;
ADD R4, R4, #1
BR &lt;span class=&quot;red &quot;&gt;[-SUB_10-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_47+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-PRINT_10S_DIGIT-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_51+}&lt;/span&gt;
ADD R3, R3, #10
ADD R0, R4, #0
ADD R0, R0, R5
&lt;span class=&quot;cyan &quot;&gt;@@ -82,30 +81,23 @@&lt;/span&gt; OUT
ADD R0, R3, #0
ADD R0, R0, R5
OUT
BR &lt;span class=&quot;red &quot;&gt;[-RET_PRINT_NUM-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_62+}&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-SINGLE_DIGIT-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_59+}&lt;/span&gt;
ADD R0, R1, #0
ADD R0, R0, R5
OUT

&lt;span class=&quot;red &quot;&gt;[-RET_PRINT_NUM-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_62+}&lt;/span&gt;
LD R1, &lt;span class=&quot;red &quot;&gt;[-S1-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#4+}&lt;/span&gt;
LD R7, &lt;span class=&quot;red &quot;&gt;[-S7-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+#2+}&lt;/span&gt;
RET

&lt;span class=&quot;red &quot;&gt;[-DONE-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+LBL_65+}&lt;/span&gt;
HALT&lt;span class=&quot;red &quot;&gt;[-S7-]&lt;/span&gt;
.FILL x0000&lt;span class=&quot;red &quot;&gt;[-S1-]&lt;/span&gt;
.FILL x0000&lt;span class=&quot;red &quot;&gt;[-START    .FILL #1-]&lt;/span&gt;
&lt;span class=&quot;red &quot;&gt;[-END      .FILL #100-]&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-ASCII_48 .FILL x0030-]&lt;/span&gt;

&lt;span class=&quot;red &quot;&gt;[-FIZZ-]&lt;/span&gt;
.STRINGZ &lt;span class=&quot;red &quot;&gt;[-&quot;Fizz&quot;-]&lt;/span&gt;
&lt;span class=&quot;red &quot;&gt;[-BUZZ-]&lt;/span&gt;&lt;span class=&quot;green &quot;&gt;{+&quot;d0Fizz&quot;+}&lt;/span&gt;
.STRINGZ &quot;Buzz&quot;&lt;span class=&quot;red &quot;&gt;[-FIZZ_BUZZ-]&lt;/span&gt;
.STRINGZ &quot;FizzBuzz&quot;
.END
&lt;/code&gt;&lt;/pre&gt;
&lt;/details&gt;
&lt;p&gt;The data section at the end is different because the string merging pass in our code merges the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.FILL&lt;/span&gt;&lt;/code&gt; data with the &lt;code class=&quot;sourceCode lc-3&quot;&gt;&lt;span class=&quot;cn&quot;&gt;.STRINGZ&lt;/span&gt;&lt;/code&gt; data that follows. Other than that, the assembly is pretty much the same, ignoring the generated label names. Perfect!&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#conclusion&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Building a disassembler for a format without metadata is a fun exercise. You have to find the patterns in the data, come up with heuristics, and use them to write your disassembler. You may need to go over the output multiple times to refine it. While the approach here is specific to LC-3, the principles generalize to other code/data separation problems in binary analysis and reverse engineering.&lt;/p&gt;
&lt;p&gt;The full code for this disassembler is available &lt;a href=&quot;https://abhinavsarkar.net/code/lc3-dis.html?mtm_campaign=feed&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p class=&quot;like-msg&quot;&gt;
If you have any questions or comments, please leave a comment below. If you liked this post, please share it. Thanks for reading!
&lt;/p&gt;
&lt;section class=&quot;footnotes footnotes-end-of-document&quot; id=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;I patched the VM to &lt;em&gt;not&lt;/em&gt; print &lt;code&gt;HALT&lt;/code&gt; when halting.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn2&quot;&gt;&lt;p&gt;Most LC-3 assemblers produce .sym files containing the debug symbols. But we don’t use them for disassembling the binaries in this post.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref2&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn3&quot;&gt;&lt;p&gt;Actually, the ISA defines &lt;code&gt;1101&lt;/code&gt; opcode as reserved for future use.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref3&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;&lt;hr /&gt;&lt;p&gt;Thanks for reading this post via feed. Feeds are great, and you're great for using them. ♥&lt;/p&gt;&lt;p&gt;This post was originally published on &lt;a href=&quot;https://abhinavsarkar.net/posts/lc3-disassembler/?mtm_campaign=feed&quot;&gt;abhinavsarkar.net&lt;/a&gt;.&lt;/p&gt;&lt;h3&gt;Like, repost, or comment on:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a class=&quot;u-syndication&quot; href=&quot;https://fantastic.earth/@abnv/117536396086305694&quot; rel=&quot;syndication&quot; target=&quot;_blank&quot;&gt;Fediverse&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;u-syndication&quot; href=&quot;https://lobste.rs/s/qoznnh&quot; rel=&quot;syndication&quot; target=&quot;_blank&quot;&gt;Lobsters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;u-syndication&quot; href=&quot;https://www.reddit.com/r/haskell/comments/1xkxccz/&quot; rel=&quot;syndication&quot; target=&quot;_blank&quot;&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;u-syndication&quot; href=&quot;https://discourse.haskell.org/t//14918&quot; rel=&quot;syndication&quot; target=&quot;_blank&quot;&gt;Discourse&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/lc3-disassembler/?mtm_campaign=feed#comment-container&quot;&gt;My website&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Read more of my &lt;a href=&quot;https://abhinavsarkar.net/posts/&quot;&gt;posts&lt;/a&gt; and &lt;a href=&quot;https://abhinavsarkar.net/notes/&quot;&gt;notes&lt;/a&gt;.&lt;/p&gt;&lt;img alt=&quot;&quot; src=&quot;https://anna.abhinavsarkar.net/matomo.php?idsite=1&amp;amp;rec=1&quot; style=&quot;border: 0;&quot; /&gt;</description>
	<pubDate>Sat, 31 Oct 2026 00:00:00 +0000</pubDate>
	<author>abhinav@abhinavsarkar.net (Abhinav Sarkar)</author>
</item>
<item>
	<title>Well-Typed.Com: Hypha: navigating Haskell source code for humans and machines</title>
	<guid isPermaLink="false">http://www.well-typed.com/blog/2026/10/hypha-navigating-haskell-source-code</guid>
	<link>https://well-typed.com/blog/2026/10/hypha-navigating-haskell-source-code</link>
	<description>&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt; I’m releasing a beta version of &lt;a href=&quot;https://well-typed.github.io/hypha/&quot;&gt;Hypha&lt;/a&gt;,
an experimental CLI tool for Haskell development which
can be useful to explore your project dependencies, read documentation offline without the need
of generating Haddock files, and use LLM harnesses and tools without hammering Hackage or Hoogle.
Think of Hypha as an offline-first, enhanced version of your Cabal store.&lt;/p&gt;
&lt;p&gt;Albeit developed making extensive use of generative AI, Hypha itself can be used without any LLM: the data is
yours, everything it caches is held locally, and it does not make calls to
external AI services. Hypha can be used offline, and despite being designed for use together
with an AI harness, it doesn’t require one.&lt;/p&gt;
&lt;p&gt;In this blog post I’ll discuss the rationale behind this tool, what it does and how it does it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An important disclaimer:&lt;/strong&gt; Hypha was born as an experiment in AI-assisted development and as such it has been developed via
&lt;strong&gt;heavy&lt;/strong&gt; usage of LLMs, using a mix of proprietary state-of-the-art models but also open weights ones, to assess
capabilities, strengths and weaknesses of each of them. In a sense, I have been mostly wearing
the “Architect Hat” for Hypha (see my &lt;a href=&quot;https://www.youtube.com/watch?v=vDmyl0ZJRF8&amp;amp;t=971s&quot;&gt;talk from the Haskell Foundation AI workshop&lt;/a&gt;).
Hypha is currently in an early-beta stage, and while I have happily been using it for a few months now,
it’s probably not free from bugs or inconsistencies. I’d be delighted to receive feedback and/or bug
reports on the &lt;a href=&quot;https://github.com/well-typed/hypha/issues&quot;&gt;Hypha repo&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;This post is deliberately written using a personal style as it reflects my own choices, both in style
and form. For the readers who might desire to know, this post is 100% human-written!&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;the-problems&quot;&gt;The problem(s)&lt;/h2&gt;
&lt;h3 id=&quot;llm-interactions-are-mostly-stateless-and-not-cache-friendly&quot;&gt;LLM interactions are mostly stateless and not cache friendly&lt;/h3&gt;
&lt;p&gt;Generative AI is a particularly divisive topic these days, with some companies and programming language communities embracing it,
while others reject everything that an LLM has ever produced. Independently of which side you might be, the
advent of AI harnesses like Claude Code, OpenCode or Pi created a lot of stress for websites, including Hackage.&lt;/p&gt;
&lt;p&gt;Every time somebody asks an LLM “What’s the signature of &lt;code&gt;mapConcurrentlyBounded&lt;/code&gt;?”&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; or “Can you find me a
generalised version of this function?” there are two possibilities: either this information is already
cached in the AI harness context, and can be served straight away (saving time and tokens)
or it’s not. If there is a cache miss, the AI may:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;grep&lt;/code&gt; through the codebase for existing uses of that function,
try to reconstruct the type signature from usage and return
a first approximation of an answer;&lt;/li&gt;
&lt;li&gt;otherwise, try to find the exact source code for the requested function.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We might be in luck, with the source code already resident on our computer; that
would be the case at least in a few scenarios:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The package has been specified in your &lt;code&gt;cabal.project&lt;/code&gt; as a &lt;code&gt;source-repository-package&lt;/code&gt; (and thus
Cabal checked out the relevant Git repo somewhere in the project’s &lt;code&gt;dist-newstyle&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;The package has been downloaded from Hackage before, and it’s available compressed as a tarball
inside Cabal’s cache;&lt;/li&gt;
&lt;li&gt;If you are a Nix user, the package would be somewhere
in your Nix store.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the scenarios above, the LLM &lt;em&gt;might&lt;/em&gt; provide you with the right answer, but it’s a fairly
non-deterministic process, because it varies according to the quality of the LLM one is using; weaker
models might ignore the fact you want the signature for a specific version of a specific package, and
just blindly give you &lt;em&gt;any&lt;/em&gt; signature matching &lt;em&gt;any&lt;/em&gt; version of the package. Similarly, if you are
using an AI harness from within a sandbox (like you should!) the harness might not be able
to find the cached information at all if the sandbox is blocking the cache path.&lt;/p&gt;
&lt;p&gt;In unlucky cases, if we try to ask an LLM for the source code or
the precise signature of &lt;code&gt;mapConcurrentlyBounded&lt;/code&gt;, the AI could end up downloading the tarball of
the &lt;code&gt;async&lt;/code&gt; package from Hackage, unpacking it locally in a temporary directory, grepping it,
finding the definition and returning it to the user.
This is a lot of wasted effort, because if we ask the same question again in a separate session,
chances are the temporary file has been pruned or simply the LLM has no memory of the past interaction,
so the tarball ends up being downloaded afresh, hitting Hackage again.&lt;/p&gt;
&lt;p&gt;The problem is made worse by the
fact that sometimes (and I have been guilty of this in the past, as well) it’s so damn tempting to just
point the AI harness to Hackage and then ask generic questions, and that creates unnecessary stress on a
critical piece of infrastructure for the Haskell community.&lt;/p&gt;
&lt;h3 id=&quot;which-version-of-async-anyway&quot;&gt;Which version of &lt;code&gt;async&lt;/code&gt;, anyway?&lt;/h3&gt;
&lt;p&gt;Another problem that we face when using LLMs to ask questions about our source code and its dependencies
is that it might not be clear from the context which version of &lt;code&gt;async&lt;/code&gt; we mean: if we paste in a prompt
the Hackage link to &lt;code&gt;async&lt;/code&gt; , we are implicitly assuming that we mean to explore the most recent version,
which might not be true in the constraints of our project: maybe we are forced to use a specific subset of
&lt;code&gt;async&lt;/code&gt; versions which didn’t even have &lt;code&gt;mapConcurrentlyBounded&lt;/code&gt; available!&lt;/p&gt;
&lt;h3 id=&quot;what-about-token-consumption&quot;&gt;What about token consumption?&lt;/h3&gt;
&lt;p&gt;As you can imagine, doing this kind of “stateless” interaction is not just wasteful in terms of HTTP
traffic, but also in terms of token consumption; the LLM has to download the tarball, unpack it, understand
its structure (at the very least grep for the requested information, and then read chunks of the source file
to extract what’s needed), and there is very little room for caching this information given everything is
done in an ad-hoc fashion.&lt;/p&gt;
&lt;p&gt;What’s worse is that all this precious knowledge is sometimes lost the minute
we wrap up the session (or our temporary directory is pruned): once we have done the hard work
we cannot get more out of it (for example exploring reverse dependencies for a package, or figuring out when
a function got removed between versions, or how a function is implemented internally and why).&lt;/p&gt;
&lt;h3 id=&quot;what-if-a-package-is-not-on-hackage&quot;&gt;What if a package is not on Hackage?&lt;/h3&gt;
&lt;p&gt;As said above, surprisingly that’s the easiest case, as the information &lt;em&gt;would&lt;/em&gt;
be stored in the &lt;code&gt;dist-newstyle&lt;/code&gt; directory. But this relies on the LLM figuring
out that the requested package is available as a &lt;code&gt;source-repository-package&lt;/code&gt; in
our &lt;code&gt;cabal.project&lt;/code&gt;, which might not be immediately obvious and it might result
in a few wasted calls to Hackage (bad) or web searches to figure things out.
Modern LLMs are getting quite good at avoiding this, but not
everybody is running expensive state-of-the-art models.&lt;/p&gt;
&lt;p&gt;Given all the above, what do we do, then?&lt;/p&gt;
&lt;h2 id=&quot;a-quick-tour-of-hypha&quot;&gt;A quick tour of Hypha&lt;/h2&gt;
&lt;p&gt;Finally, we are ready to take a quick tour of Hypha;
hopefully at the end of this tour you will have enough information to let you decide if you want to
use it or not (and I hope you will!). If you’d like to try it yourself, see the
&lt;a href=&quot;https://well-typed.github.io/hypha/getting-started/installation.html&quot;&gt;installation instructions on the docs site&lt;/a&gt;
and run &lt;code&gt;cabal build all --dry-run&lt;/code&gt; to generate the Cabal build plan for your project.&lt;/p&gt;
&lt;p&gt;Let’s start with a concrete example. Suppose we are working on a project, and we have a type like this:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LogEntry&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LogEntry&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  {&lt;span class=&quot;ot&quot;&gt; _le_payload   ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ,&lt;span class=&quot;ot&quot;&gt; _le_timestamp ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UTCTime&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  } &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We know our basic Haskell and we know &lt;code&gt;sort&lt;/code&gt; exists, but here we want something a bit different:
we don’t want to sort based on the &lt;code&gt;Ord&lt;/code&gt; instance, but rather we would like to sort log entries on their
timestamp, so ideally we would like to have a variant of &lt;code&gt;sort&lt;/code&gt; which accepts a custom function to sort
over a field.&lt;/p&gt;
&lt;p&gt;We vaguely remember such function exists somewhere in &lt;code&gt;base&lt;/code&gt;, but we don’t remember its name.
Of course we could go on Hackage, find the definition of &lt;code&gt;sort&lt;/code&gt; and look for similarly-named functions
within that module. But that would make the tutorial much more boring! :-) Jokes aside, this is not about what
&lt;em&gt;you&lt;/em&gt;, a human, would do, &lt;strong&gt;it’s about creating the most efficient and easy-to-use path for an AI harness&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&quot;hypha-lookup&quot;&gt;&lt;code&gt;hypha lookup&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;What we can do instead is to ask &lt;code&gt;hypha&lt;/code&gt; about the existence of such function:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; hypha &lt;span class=&quot;ex&quot;&gt;lookup&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;'Ord b =&amp;gt; (a -&amp;gt; b) -&amp;gt; [a] -&amp;gt; [a]'&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;actions:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;base/Data.List/sortOn:&lt;/span&gt; hypha symbol base/Data.List/sortOn&lt;/span&gt;
&lt;span id=&quot;cb2-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;base/GHC.Exts/sortWith:&lt;/span&gt; hypha symbol base/GHC.Exts/sortWith&lt;/span&gt;
&lt;span id=&quot;cb2-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;base/GHC.OldList/sortOn:&lt;/span&gt; hypha symbol base/GHC.OldList/sortOn&lt;/span&gt;
&lt;span id=&quot;cb2-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;containers/Data.Containers.ListUtils/nubOrdOn:&lt;/span&gt; hypha symbol containers/Data.Containers.ListUtils/nubOrdOn&lt;/span&gt;
&lt;span id=&quot;cb2-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;result:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;providers:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; mod: Data.List&lt;/span&gt;
&lt;span id=&quot;cb2-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;name:&lt;/span&gt; sortOn&lt;/span&gt;
&lt;span id=&quot;cb2-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;pkg:&lt;/span&gt; base&lt;/span&gt;
&lt;span id=&quot;cb2-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;sig:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Ord b =&amp;gt; (a -&amp;gt; b) -&amp;gt; [a] -&amp;gt; [a]&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;tier:&lt;/span&gt; local-hoogle&lt;/span&gt;
&lt;span id=&quot;cb2-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; mod: GHC.Exts&lt;/span&gt;
&lt;span id=&quot;cb2-16&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;name:&lt;/span&gt; sortWith&lt;/span&gt;
&lt;span id=&quot;cb2-17&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;pkg:&lt;/span&gt; base&lt;/span&gt;
&lt;span id=&quot;cb2-18&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;sig:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Ord b =&amp;gt; (a -&amp;gt; b) -&amp;gt; [a] -&amp;gt; [a]&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-19&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;tier:&lt;/span&gt; local-hoogle&lt;/span&gt;
&lt;span id=&quot;cb2-20&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; mod: GHC.OldList&lt;/span&gt;
&lt;span id=&quot;cb2-21&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;name:&lt;/span&gt; sortOn&lt;/span&gt;
&lt;span id=&quot;cb2-22&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;pkg:&lt;/span&gt; base&lt;/span&gt;
&lt;span id=&quot;cb2-23&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;sig:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Ord b =&amp;gt; (a -&amp;gt; b) -&amp;gt; [a] -&amp;gt; [a]&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-24&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;tier:&lt;/span&gt; local-hoogle&lt;/span&gt;
&lt;span id=&quot;cb2-25&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; mod: Data.Containers.ListUtils&lt;/span&gt;
&lt;span id=&quot;cb2-26&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;name:&lt;/span&gt; nubOrdOn&lt;/span&gt;
&lt;span id=&quot;cb2-27&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;pkg:&lt;/span&gt; containers&lt;/span&gt;
&lt;span id=&quot;cb2-28&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;sig:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Ord b =&amp;gt; (a -&amp;gt; b) -&amp;gt; [a] -&amp;gt; [a]&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-29&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;tier:&lt;/span&gt; local-hoogle&lt;/span&gt;
&lt;span id=&quot;cb2-30&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb2-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;query:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Ord b =&amp;gt; (a -&amp;gt; b) -&amp;gt; [a] -&amp;gt; [a]&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is &lt;code&gt;lookup&lt;/code&gt;’s main purpose: giving you a concrete entry point for discovering functions you might
have already available transitively in your build plan but you don’t know where they are or how they are
called. Internally, &lt;code&gt;lookup&lt;/code&gt; queries Hoogle (offline first, remote once only on a cache miss), and the
result of &lt;code&gt;lookup&lt;/code&gt; is a list of candidates all satisfying the requested type signature.&lt;/p&gt;
&lt;p&gt;What makes &lt;code&gt;lookup&lt;/code&gt; useful is that it is not limited to &lt;em&gt;just&lt;/em&gt; locally-cached packages in your project.
This means you or an LLM can use &lt;code&gt;lookup&lt;/code&gt; as the initial gateway to access relevant information, saving
the time of looking things up in two different data sources, while protecting the integrity of the
infrastructure by trying to be as cache-friendly as possible.&lt;/p&gt;
&lt;h3 id=&quot;hypha-symbol&quot;&gt;&lt;code&gt;hypha symbol&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Lo and behold, we have now found that &lt;code&gt;sortOn&lt;/code&gt; exists and it seems to be the function we are looking for.
The next step would be to get a bit more information about it, which we can do via the &lt;code&gt;symbol&lt;/code&gt; command:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; hypha &lt;span class=&quot;ex&quot;&gt;symbol&lt;/span&gt; base/Data.List/sortOn&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;actions:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;module_index:&lt;/span&gt; hypha module base/Data.List&lt;/span&gt;
&lt;span id=&quot;cb3-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;package_info:&lt;/span&gt; hypha package base&lt;/span&gt;
&lt;span id=&quot;cb3-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;view_source:&lt;/span&gt; hypha source base/Data.List/sortOn&lt;/span&gt;
&lt;span id=&quot;cb3-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;result:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;defined_in:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;component:&lt;/span&gt; base&lt;/span&gt;
&lt;span id=&quot;cb3-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;module:&lt;/span&gt; Data.OldList&lt;/span&gt;
&lt;span id=&quot;cb3-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;haddock_raw:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; Sort a list by comparing the results of a key function applied to each\n element.  @sortOn f@ is equivalent to @sortBy (comparing f)@, but has the\n performance advantage of only evaluating @f@ once for each element in the\n input list.  This is called the decorate-sort-undecorate paradigm, or\n Schwartzian transform.\n\n Elements are arranged from lowest to highest, keeping duplicates in\n the order they appeared in the input.\n\n &amp;gt;&amp;gt;&amp;gt; sortOn fst [(2, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;), (4, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;), (1, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;)]\n [(1,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;),(2,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;),(4,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;)]\n\n @since 4.8.0.0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;kind:&lt;/span&gt; function&lt;/span&gt;
&lt;span id=&quot;cb3-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;module:&lt;/span&gt; Data.List&lt;/span&gt;
&lt;span id=&quot;cb3-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;name:&lt;/span&gt; sortOn&lt;/span&gt;
&lt;span id=&quot;cb3-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;package:&lt;/span&gt; base&lt;/span&gt;
&lt;span id=&quot;cb3-16&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;signature:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;sortOn :: Ord b =&amp;gt; (a -&amp;gt; b) -&amp;gt; [a] -&amp;gt; [a]&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-17&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;version:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;'4.16.4.0'&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, the output includes two major “blocks”:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An &lt;code&gt;actions&lt;/code&gt; block, which are like breadcrumbs to suggest to an LLM where to go next (as we will see later);&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;result&lt;/code&gt; block, which includes useful information like &lt;code&gt;haddock_raw&lt;/code&gt;, which allows answering some
easy questions immediately (think if the question was “what does &lt;code&gt;sortOn&lt;/code&gt; do?”).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If desired, it’s possible to select only a subset of fields from the &lt;code&gt;result&lt;/code&gt; block via the &lt;code&gt;--select&lt;/code&gt; option:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; hypha &lt;span class=&quot;ex&quot;&gt;symbol&lt;/span&gt; base/Data.List/sortOn &lt;span class=&quot;at&quot;&gt;--select&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;version, haddock_raw&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;actions:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;module_index:&lt;/span&gt; hypha module base/Data.List&lt;/span&gt;
&lt;span id=&quot;cb4-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;package_info:&lt;/span&gt; hypha package base&lt;/span&gt;
&lt;span id=&quot;cb4-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;view_source:&lt;/span&gt; hypha source base/Data.List/sortOn&lt;/span&gt;
&lt;span id=&quot;cb4-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;result:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;haddock_raw:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; Sort a list by comparing the results of a key function applied to each\n element.  @'sortOn' f@ is equivalent to @'sortBy' ('comparing' f)@, but has the\n performance advantage of only evaluating @f@ once for each element in the\n input list.  This is called the decorate-sort-undecorate paradigm, or\n &amp;lt;https://en.wikipedia.org/wiki/Schwartzian_transform Schwartzian transform&amp;gt;.\n\n Elements are arranged from lowest to highest, keeping duplicates in\n the order they appeared in the input.\n\n The argument must be finite.\n\n ==== __Examples__\n\n &amp;gt;&amp;gt;&amp;gt; sortOn fst [(2, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;), (4, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;), (1, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;)]\n [(1,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;Hello&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;),(2,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;world&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;),(4,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;)]\n\n &amp;gt;&amp;gt;&amp;gt; sortOn length [&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;jim&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;creed&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;pam&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;michael&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;dwight&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;kevin&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;]\n [&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;jim&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;pam&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;creed&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;kevin&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;dwight&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;michael&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;]\n\n ==== __Performance notes__\n\n This function minimises the projections performed, by materialising\n the projections in an intermediate list.\n\n For trivial projections, you should prefer using 'sortBy' with\n 'comparing', for example:\n\n &amp;gt;&amp;gt;&amp;gt; sortBy (comparing fst) [(3, 1), (2, 2), (1, 3)]\n [(1,3),(2,2),(3,1)]\n\n Or, for the exact same API as 'sortOn', you can use &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;sortBy&lt;/span&gt; . comparing&lt;span class=&quot;kw&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;:\n\n &amp;gt;&amp;gt;&amp;gt; (sortBy . comparing) fst [(3, 1), (2, 2), (1, 3)]\n [(1,3),(2,2),(3,1)]\n\n @since base-4.8.0.0&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;version:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;'4.20.2.0'&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;hypha-source&quot;&gt;&lt;code&gt;hypha source&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Following the same “stream of consciousness” that an LLM would also follow, the next step
(depending on the original query, of course) would be to take a look at the &lt;code&gt;source&lt;/code&gt; code for &lt;code&gt;sortOn&lt;/code&gt;,
which, as you might guess, is available via the &lt;code&gt;source&lt;/code&gt; command:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; hypha &lt;span class=&quot;bu&quot;&gt;source&lt;/span&gt; base/Data.List/sortOn&lt;/span&gt;
&lt;span id=&quot;cb5-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;result:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;line:&lt;/span&gt; 1819&lt;/span&gt;
&lt;span id=&quot;cb5-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;module:&lt;/span&gt; GHC.Internal.Data.OldList&lt;/span&gt;
&lt;span id=&quot;cb5-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;package:&lt;/span&gt; ghc-internal&lt;/span&gt;
&lt;span id=&quot;cb5-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;path:&lt;/span&gt; /home/alfredo/.cache/hypha/source/ghc-internal-9.1003.0/src/GHC/Internal/Data/OldList.hs&lt;/span&gt;
&lt;span id=&quot;cb5-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;snippet:&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; the projections in an intermediate list.&lt;/span&gt;
&lt;span id=&quot;cb5-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; For trivial projections, you should prefer using &lt;span class=&quot;st&quot;&gt;'sortBy'&lt;/span&gt; with&lt;/span&gt;
&lt;span id=&quot;cb5-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;'comparing'&lt;/span&gt;, for example:&lt;/span&gt;
&lt;span id=&quot;cb5-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; sortBy &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;comparing&lt;/span&gt; fst&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ex&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;3,&lt;/span&gt; 1&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;2,&lt;/span&gt; 2&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;1,&lt;/span&gt; 3&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; [&lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;1,3&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;2,2&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;3,1&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-16&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-17&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; Or, for the exact same API as &lt;span class=&quot;st&quot;&gt;'sortOn'&lt;/span&gt;, you can use &lt;span class=&quot;kw&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;sortBy&lt;/span&gt; . comparing&lt;span class=&quot;kw&quot;&gt;`&lt;/span&gt;:&lt;/span&gt;
&lt;span id=&quot;cb5-18&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-19&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;sortBy&lt;/span&gt; . comparing&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ex&quot;&gt;fst&lt;/span&gt; [&lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;3,&lt;/span&gt; 1&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;2,&lt;/span&gt; 2&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;1,&lt;/span&gt; 3&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-20&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; [&lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;1,3&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;2,2&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;3,1&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-21&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-22&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; @since base-4.8.0.0&lt;/span&gt;
&lt;span id=&quot;cb5-23&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;sortOn&lt;/span&gt; :: Ord b =&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; b&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;pp&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;pp&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;pp&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;pp&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-24&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;sortOn&lt;/span&gt; f =&lt;/span&gt;
&lt;span id=&quot;cb5-25&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;map&lt;/span&gt; snd . sortBy &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;comparing&lt;/span&gt; fst&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;.&lt;/span&gt; map &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;\x&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; let y = f x in y &lt;span class=&quot;kw&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;er&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;y,&lt;/span&gt; x&lt;span class=&quot;kw&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-26&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-27&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ex&quot;&gt;Construct&lt;/span&gt; a list from a single element.&lt;/span&gt;
&lt;span id=&quot;cb5-28&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-29&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; ==== __Examples__&lt;/span&gt;
&lt;span id=&quot;cb5-30&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-31&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; singleton True&lt;/span&gt;
&lt;span id=&quot;cb5-32&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;pp&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;pp&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-33&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-34&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; singleton [1, 2, 3]&lt;/span&gt;
&lt;span id=&quot;cb5-35&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--[[1,2,3]]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-36&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-37&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; singleton &lt;span class=&quot;st&quot;&gt;'c'&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-38&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-39&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;--&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-40&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;symbol:&lt;/span&gt; sortOn&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;From here we can just read the implementation, query other ancillary functions etc.&lt;/p&gt;
&lt;h3 id=&quot;hypha-module&quot;&gt;&lt;code&gt;hypha module&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;More often than not, discovering one function makes us curious about its neighbours: if &lt;code&gt;Data.List&lt;/code&gt;
has &lt;code&gt;sortOn&lt;/code&gt;, what else is in there that we have been re-implementing by hand? LLMs have a tendency to
hand-roll a lot of things, so this is a nice arrow to have in our quiver. We can use the &lt;code&gt;module&lt;/code&gt;
command for that:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; hypha &lt;span class=&quot;ex&quot;&gt;module&lt;/span&gt; base/Data.List&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;actions:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;st&quot;&gt;&quot;++&quot;&lt;/span&gt;&lt;span class=&quot;ex&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;hypha symbol base/Data.List/++&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;List:&lt;/span&gt; hypha symbol base/Data.List/List&lt;/span&gt;
&lt;span id=&quot;cb6-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;head:&lt;/span&gt; hypha symbol base/Data.List/head&lt;/span&gt;
&lt;span id=&quot;cb6-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;last:&lt;/span&gt; hypha symbol base/Data.List/last&lt;/span&gt;
&lt;span id=&quot;cb6-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;module_index:&lt;/span&gt; hypha module base/Data.List&lt;/span&gt;
&lt;span id=&quot;cb6-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;package_info:&lt;/span&gt; hypha package base&lt;/span&gt;
&lt;span id=&quot;cb6-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;tail:&lt;/span&gt; hypha symbol base/Data.List/tail&lt;/span&gt;
&lt;span id=&quot;cb6-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;result:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;exports:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: List&lt;/span&gt;
&lt;span id=&quot;cb6-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: &lt;span class=&quot;st&quot;&gt;&quot;++&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: head&lt;/span&gt;
&lt;span id=&quot;cb6-16&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: last&lt;/span&gt;
&lt;span id=&quot;cb6-17&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: tail&lt;/span&gt;
&lt;span id=&quot;cb6-18&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: init&lt;/span&gt;
&lt;span id=&quot;cb6-19&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: uncons&lt;/span&gt;
&lt;span id=&quot;cb6-20&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: unsnoc&lt;/span&gt;
&lt;span id=&quot;cb6-21&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: singleton&lt;/span&gt;
&lt;span id=&quot;cb6-22&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: null&lt;/span&gt;
&lt;span id=&quot;cb6-23&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: length&lt;/span&gt;
&lt;span id=&quot;cb6-24&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: map&lt;/span&gt;
&lt;span id=&quot;cb6-25&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: reverse&lt;/span&gt;
&lt;span id=&quot;cb6-26&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: intersperse&lt;/span&gt;
&lt;span id=&quot;cb6-27&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: intercalate&lt;/span&gt;
&lt;span id=&quot;cb6-28&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: transpose&lt;/span&gt;
&lt;span id=&quot;cb6-29&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: subsequences&lt;/span&gt;
&lt;span id=&quot;cb6-30&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: permutations&lt;/span&gt;
&lt;span id=&quot;cb6-31&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: foldl&lt;/span&gt;
&lt;span id=&quot;cb6-32&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: &lt;span class=&quot;st&quot;&gt;&quot;foldl'&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-33&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; name: foldl1&lt;/span&gt;
&lt;span id=&quot;cb6-34&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;co&quot;&gt;# ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That by default gives us the version of &lt;code&gt;base&lt;/code&gt; and &lt;code&gt;Data.List&lt;/code&gt; which is specific to our build plan,
but we can also ask for a specific version using this alternative syntax:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;hypha&lt;/span&gt; module base-4.10.0.0/Data.List&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is especially nice as it can be used by an LLM to easily diff the resulting YAML to
spot missing functions, deprecations, removals, etc.&lt;/p&gt;
&lt;h3 id=&quot;hypha-package&quot;&gt;&lt;code&gt;hypha package&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;In the same vein it’s possible to have a holistic view on a whole package using the &lt;code&gt;package&lt;/code&gt; command,
which has the effect you might imagine:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; hypha &lt;span class=&quot;ex&quot;&gt;package&lt;/span&gt; thread-supervisor-0.2.0.0&lt;/span&gt;
&lt;span id=&quot;cb8-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;actions:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;Control.Concurrent.Supervisor:&lt;/span&gt; hypha module thread-supervisor/Control.Concurrent.Supervisor&lt;/span&gt;
&lt;span id=&quot;cb8-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;Control.Concurrent.SupervisorInternal:&lt;/span&gt; hypha module thread-supervisor/Control.Concurrent.SupervisorInternal&lt;/span&gt;
&lt;span id=&quot;cb8-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;Data.DelayedQueue:&lt;/span&gt; hypha module thread-supervisor/Data.DelayedQueue&lt;/span&gt;
&lt;span id=&quot;cb8-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;module_index_hint:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;hypha module thread-supervisor/&amp;lt;Module&amp;gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;reverse_deps:&lt;/span&gt; hypha deps thread-supervisor &lt;span class=&quot;at&quot;&gt;--reverse&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;version_history:&lt;/span&gt; hypha versions thread-supervisor&lt;/span&gt;
&lt;span id=&quot;cb8-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;result:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;exposed_modules:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; Control.Concurrent.Supervisor&lt;/span&gt;
&lt;span id=&quot;cb8-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; Control.Concurrent.SupervisorInternal&lt;/span&gt;
&lt;span id=&quot;cb8-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;-&lt;/span&gt; Data.DelayedQueue&lt;/span&gt;
&lt;span id=&quot;cb8-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;is_local:&lt;/span&gt; false&lt;/span&gt;
&lt;span id=&quot;cb8-16&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;name:&lt;/span&gt; thread-supervisor&lt;/span&gt;
&lt;span id=&quot;cb8-17&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;origin:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-18&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;kind:&lt;/span&gt; hackage&lt;/span&gt;
&lt;span id=&quot;cb8-19&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;version:&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;'0.2.0.0'&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Everything we have seen so far is also available as a &lt;a href=&quot;https://code.claude.com/docs/en/plugins&quot;&gt;Claude plugin&lt;/a&gt;
which can be installed to use &lt;code&gt;hypha&lt;/code&gt; directly inside a Claude Code session. For example, we might want to ask:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/hypha:hypha-lookup mapConcurrentlyBounded&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And this is what you would see:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;● Bash(cabal run -v0 hypha -- lookup mapConcurrentlyBounded 2&amp;gt;&amp;amp;1 | head -60)
  ⎿  actions:
       async/Control.Concurrent.Stream/mapConcurrentlyBounded: hypha symbol async/Control.Concurrent.Stream/mapConcurrentlyBounded
     result:
       providers:
         - mod: Control.Concurrent.Stream
           name: mapConcurrentlyBounded
           pkg: async
           sig: &quot;mapConcurrentlyBounded :: Int -- ^ Maximum concurrency -&amp;gt; (a -&amp;gt; IO b) -- ^ Function to map over the input values -&amp;gt; [a] -- ^ List of input values -&amp;gt; IO [b] -- ^ List of output values&quot;
           tier: cache
       query: mapConcurrentlyBounded

● mapConcurrentlyBounded found, tier cache:

  mapConcurrentlyBounded
    :: Int              -- ^ Maximum concurrency
    -&amp;gt; (a -&amp;gt; IO b)      -- ^ Function to map over the input values
    -&amp;gt; [a]              -- ^ List of input values
    -&amp;gt; IO [b]           -- ^ List of output values

  Package async, module Control.Concurrent.Stream. No haddock body beyond arg comments.&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;serving-interactive-documentation&quot;&gt;Serving interactive documentation&lt;/h2&gt;
&lt;p&gt;One of the side goals of the project was to fix an old pet peeve of mine, by providing an easily-accessible,
offline-first UI to browse and navigate Haskell documentation, at least of those packages I use daily.
Back when I was on Mac OS for development,
I used Dash, which supported GHC Haskell (essentially, &lt;code&gt;base&lt;/code&gt;) documentation alongside the ability to
download so-called “docsets” for Haskell packages.&lt;/p&gt;
&lt;p&gt;Unfortunately I couldn’t find an equally-satisfying
alternative in Linux land; the closest I’m aware of is Zeal but, at the time of writing this blog post,
it didn’t support documentation from Hackage. Even when I was using Dash, though, I wasn’t completely
satisfied with it either, mostly because if you decided to download a docset for a package,
the program would silently download revisions for each new version out there, making it difficult to do
precise querying.&lt;/p&gt;
&lt;h3 id=&quot;hypha-server&quot;&gt;&lt;code&gt;hypha server&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;For all these reasons, given the fact the hard work was already done for the AIs, I have decided to also
bolt on a server which can be run locally in order to browse the documentation. You start it with a single command:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;hypha&lt;/span&gt; server&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;figure&gt;
&lt;img alt=&quot;The Hypha server homepage&quot; src=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-1.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-1.png&quot;&gt;The Hypha server homepage&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The homepage gives you an immediate sense of what Hypha knows about your project. The sidebar is split
into two groups:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Project, for the packages that make up your own codebase, and&lt;/li&gt;
&lt;li&gt;Dependencies, for everything else in the build plan.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Notice that the Project group is not limited to libraries: in the
screenshot you can see that Hypha tracks every component of the project, including its executables
(&lt;code&gt;hypha:exe:hypha&lt;/code&gt; and &lt;code&gt;hypha:exe:hypha-mcp&lt;/code&gt;), and the same goes for sub-libraries. This works because
Hypha does not stop at &lt;code&gt;plan.json&lt;/code&gt;: for every unit in the plan whose source directory is known, it also
reads the corresponding &lt;code&gt;.cabal&lt;/code&gt; stanza, so it knows exactly which components exist and which modules
each one exposes.&lt;/p&gt;
&lt;p&gt;The “docset” &lt;code&gt;hypha server&lt;/code&gt; exposes is not &lt;em&gt;any&lt;/em&gt; collection of packages,
but it’s an &lt;strong&gt;exact snapshot, version-by-version of all your project’s dependencies&lt;/strong&gt;. This eliminated for
me the all-too-frequent pattern of checking in the build plan which version of a package I was using, taking note
of its version, searching for the package on Hackage, selecting the required version and &lt;em&gt;finally&lt;/em&gt; browsing
through the documentation.&lt;/p&gt;
&lt;h3 id=&quot;browsing-the-documentation&quot;&gt;Browsing the documentation&lt;/h3&gt;
&lt;p&gt;Clicking on a module opens its documentation page. The header shows the usual metadata, the module
description is rendered in full, and on the right hand side you get a panel listing the symbols the
module exports, grouped by kind, a bit like the module pages on Hackage, or like Dash. Every entry in
the panel is interactive: clicking it brings up a symbol card with the signature and its Haddock
documentation, and from there you can jump to the source:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Navigating a module shows the exported symbols on the right&quot; src=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-navigation-1.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-navigation-1.png&quot;&gt;Navigating a module shows the exported symbols on the right&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The top search bar allows you to do fuzzy queries for packages and symbols:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Fuzzy searching for a symbol&quot; src=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-searching-1.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-searching-1.png&quot;&gt;Fuzzy searching for a symbol&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;If the same package re-exports the same symbol in multiple places, that is
tracked by a little &lt;code&gt;+N&lt;/code&gt; on the bottom of the search result (in the screenshot
that’s the case for &lt;code&gt;mapAccumL&lt;/code&gt;, and the exports are explicitly reported):&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;If a symbol is exported multiple times we can track it&quot; src=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-provenance-2.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-provenance-2.png&quot;&gt;If a symbol is exported multiple times we can track it&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Every package in the plan is tagged with its provenance, i.e. where it came
from, represented in the sidebar by a small single-letter badge:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;H&lt;/code&gt; for Hackage,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;D&lt;/code&gt; for a GHC distribution/boot library,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;L&lt;/code&gt; for a local package,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;S&lt;/code&gt; for a source-repository-package.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For packages that are published on
Hackage the page also offers a “view on Hackage” link, so opening the latter is
typically one click away:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Each package is tagged with its provenance&quot; src=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-provenance-1.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-provenance-1.png&quot;&gt;Each package is tagged with its provenance&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;It’s also possible to restrict the search results in the bar to consider only the symbols within
a given package simply by pressing the small “&lt;code&gt;in &amp;lt;pkg&amp;gt;&lt;/code&gt;” button next to the search bar. This is useful when multiple packages export heavily-overloaded
function names (e.g. &lt;code&gt;lookup&lt;/code&gt;), and it will return in the dropdown menu only symbols for the given package:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;We can focus the search inside a single package only&quot; src=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-searching-2.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-searching-2.png&quot;&gt;We can focus the search inside a single package only&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The plan-aware approach also covers dependencies that do not come from Hackage at all. If your project
pins a package via a &lt;code&gt;source-repository-package&lt;/code&gt; stanza in its &lt;code&gt;cabal.project&lt;/code&gt;, Hypha reads the
&lt;code&gt;plan.json&lt;/code&gt; and learns exactly which git repository and which commit your dependency is pinned to. It
means that it’s possible to browse not-on-Hackage dependencies as well:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;We can also track source-repository-package packages&quot; src=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-1-source-repository-package.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-1-source-repository-package.png&quot;&gt;We can also track &lt;code&gt;source-repository-package&lt;/code&gt; packages&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Last but not least, from a symbol you can jump straight to its definition. If you have already built
the Haddock documentation for a package (for example via &lt;code&gt;documentation: True&lt;/code&gt; in your Cabal config),
Hypha will happily serve that. Otherwise, Hypha tries to cheat by rendering documentation from the source code on the fly,
to not pay the full price for building a huge tree of Haddock documentation offline; in that case the
rendering is not as pretty as Haddock, and the source view is not hyperlinked like on Hackage:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;We can go to the definition, like in Hackage&quot; src=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-go-to-definition.webp&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/alfredo/announcing-hypha/hypha-server-go-to-definition.webp&quot;&gt;We can go to the definition, like in Hackage&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h3 id=&quot;alternative-documentation-browsers&quot;&gt;Alternative documentation browsers&lt;/h3&gt;
&lt;p&gt;A legitimate question is whether or not having a tool which implements interactive documentation browsing
is a useful addition to the ecosystem. Why can’t we just build the full Haddock documentation for
all our local packages and be done with it? Conversely, what are the advantages of using &lt;code&gt;hypha server&lt;/code&gt;
compared to solutions like &lt;a href=&quot;https://flora.pm/&quot;&gt;Flora&lt;/a&gt;?&lt;/p&gt;
&lt;h4 id=&quot;building-offline-haddock-documentation&quot;&gt;Building offline Haddock documentation&lt;/h4&gt;
&lt;p&gt;The most obvious alternative to the Hypha server would be to build the offline Haddock documentation,
for example by setting &lt;code&gt;documentation: True&lt;/code&gt; in your Cabal config.
That would certainly work and it’s a defensible approach; what you gain by using Hypha
is that if you already have the offline documentation, the Hypha server would happily serve it, and on top of
that you also get features like holistic search across all your transitive dependencies, whereas the typical
offline-Haddock approach would be to open the documentation of your root package and then drill down to the
individual dependencies by following the cross-package links in the type signatures. Even
&lt;code&gt;cabal haddock-project&lt;/code&gt;, which gives you a nice landing page, only indexes your local packages
(tools like &lt;code&gt;cabal-hoogle&lt;/code&gt; can get you a project-wide search, but at the price of a full documentation build).&lt;/p&gt;
&lt;p&gt;Of course, if you haven’t built (or don’t want to build) the offline documentation then the Hypha server
experience is somewhat less polished than the one you would get by using just Haddock; due to the fact
that Hypha only parses the package sources but skips the rest of the compilation pipeline, TH
splices won’t get evaluated and thus they are invisible to Hypha, and the source view is not hyperlinked
like Haddock’s – if that means a lot to you, that is certainly an important difference to be aware of.&lt;/p&gt;
&lt;h4 id=&quot;flora&quot;&gt;Flora&lt;/h4&gt;
&lt;p&gt;&lt;a href=&quot;https://flora.pm/&quot;&gt;Flora&lt;/a&gt; is a great project that gives a powerful query system to search for a specific package;
however, to the best of my knowledge Flora piggybacks on Hackage itself for browsing the documentation.
Visiting a page like the one for &lt;a href=&quot;https://flora.pm/packages/@hackage/containers&quot;&gt;&lt;code&gt;containers&lt;/code&gt;&lt;/a&gt; shows
valuable information like dependencies, dependents, the versions etc, but clicking on “Documentation”
redirects to Hackage.&lt;/p&gt;
&lt;p&gt;In a nutshell, I think every approach is valuable and defensible, and there is no silver bullet:
&lt;code&gt;hypha server&lt;/code&gt; shines if all we want is to get started quickly by having holistic search of all our
project’s dependencies at the right versions and without having to compile anything, at the cost of
the quality of documentation in some cases. Using Haddock offline is what gives you the best accuracy
at the cost of a slightly less ergonomic experience for discovering functions by fuzzy searching from the
root of the package. Flora is great if you want to search for a particular project or build artifact
(like an executable), has a great &lt;a href=&quot;https://flora.pm/documentation/api-reference&quot;&gt;API reference&lt;/a&gt; which
makes it suitable for being used programmatically and supports &lt;a href=&quot;https://flora.pm/documentation/search-features&quot;&gt;search modifiers&lt;/a&gt;
like &lt;code&gt;depends:&lt;/code&gt; and &lt;code&gt;exe:&lt;/code&gt; which Hackage doesn’t have.&lt;/p&gt;
&lt;h2 id=&quot;goals-and-related-work&quot;&gt;Goals and related work&lt;/h2&gt;
&lt;p&gt;Before designing Hypha, I explored the solution space, and to my knowledge, these are the projects
that existed last spring when I did my survey:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/tusharad/hackage-doc-mcp&quot;&gt;hackage-doc-mcp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/lazamar/haskell-docs-cli&quot;&gt;haskell-docs-cli&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both projects have their own merits, and I encourage you to look into them!
I don’t like to comment on the design and implementation of other packages,
as I think the only constructive stance for open source projects is to be grateful that somebody took the time
out of their lives to contribute back to the community.&lt;/p&gt;
&lt;p&gt;Rather than saying why I didn’t use one or the other,
perhaps it would be more interesting to say what I wanted in the first place,
and why I have decided to design Hypha. I wanted a tool which was:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Capable of figuring out which packages and which versions my project was using just by inspecting the
&lt;code&gt;plan.json&lt;/code&gt; available for each cabal project (at least after you run &lt;code&gt;cabal build all --dry-run&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;Offline-first: it should be possible to work offline on an airplane or in a coffee shop
without an internet connection;&lt;/li&gt;
&lt;li&gt;Cache-friendly: we shouldn’t hammer Hackage or any other web service with AI harnesses more than it’s
strictly necessary, i.e. we should take good care of our open source infrastructure;&lt;/li&gt;
&lt;li&gt;Implemented using the &lt;a href=&quot;https://github.com/mvanhorn/cli-printing-press&quot;&gt;CLI Printing Press&lt;/a&gt; approach rather
than as an MCP server;&lt;/li&gt;
&lt;li&gt;Useful for machines (i.e. non-interactive) but also humans (i.e. I could make sense of the output of the tool even if &lt;code&gt;hypha&lt;/code&gt;
were to be called standalone);&lt;/li&gt;
&lt;li&gt;Helpful for interactively navigating
documentation via a UI on top of what the CLI was already providing;&lt;/li&gt;
&lt;li&gt;A playground to experiment with AI-assisted development without being worried about violating
any project’s ethos or contribution guidelines, or trying to shoehorn in features which projects might
not want to support.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;mcp-vs-clis&quot;&gt;MCP vs CLIs&lt;/h3&gt;
&lt;p&gt;One of the most fundamental questions when building a piece of software which should be AI-first is to
ask ourselves “How is the LLM going to consume it?” and, up until recently, the one and only answer was
“exposed as an MCP server”. But what is exactly an MCP server?&lt;/p&gt;
&lt;p&gt;MCP stands for &lt;a href=&quot;https://modelcontextprotocol.io/docs/2026-07-28/getting-started/intro&quot;&gt;Model Context Protocol&lt;/a&gt;
and, as the documentation states, is “an open-source standard for connecting AI applications to external systems”.
The super TL;DR is best explained with a concrete example. Suppose you want to connect your AI
to your Postgres database, for some data analysis: how do you “teach” the AI how to connect to Postgres,
how to query data, etc.? How do you do this remotely? By writing an MCP server you are essentially publishing
(some or all of) the atomic operations that this service supports behind an API. So you could imagine that (naively, disregarding any consideration about security, SQL injection etc.)
you could expose an operation like &lt;code&gt;db_read(table, sql_query)&lt;/code&gt; which the AI could call and that your
MCP server could implement however it wants. Now, the next time you ask the AI to query some data, internally
the harness would make a request to the registered MCP server, calling the API corresponding to the &lt;code&gt;db_read&lt;/code&gt;
operation, and giving you back the result.&lt;/p&gt;
&lt;p&gt;The advantage of using MCP is clear:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It’s an open source standard, so it’s something that most harnesses and LLM websites support;&lt;/li&gt;
&lt;li&gt;It can be hosted anywhere and exposed remotely, so you can connect your favourite service and
access the MCP server from within a web chat, for example.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The disadvantage of using an MCP server is that you have to build it in the first place, obviously,
but you also pay a roundtrip price as the
information has to travel back and forth using &lt;a href=&quot;https://modelcontextprotocol.io/docs/2026-07-28/learn/architecture&quot;&gt;JSON-RPC 2.0&lt;/a&gt;,
and it produces verbose results, so it is not immediately the most context/token friendly option if you really care about those things.&lt;/p&gt;
&lt;p&gt;In the past few months, a new trend emerged: just use a CLI!
If you think about it, it makes sense: LLMs have been trained on a ton of Bash code, so they know very well
how to write Bash scripts and how to call Unix tools. In the example above, we could have a little CLI wrapping
our blessed operations and just ask an AI harness to use it (for example: &lt;code&gt;echo &amp;lt;my_sql_query&amp;gt; | my-cli db_read &amp;lt;table&amp;gt; -&lt;/code&gt;).
This is obviously a very local-centric solution (we cannot natively “publish” the CLI) and is not standard
(i.e. AIs wouldn’t know how to connect to your CLI natively). However, &lt;strong&gt;we can easily wrap our CLI in an MCP server&lt;/strong&gt;, to get the best of both worlds: locally our AI harness
can call the CLI directly, without paying the token cost for full JSON, but we can still get the benefit
of the MCP server when it matters. This is the approach taken by the wonderful
&lt;a href=&quot;https://github.com/mvanhorn/cli-printing-press&quot;&gt;CLI Printing Press&lt;/a&gt; project, which gives you the
ability to produce a CLI wrapping many public-facing services, including an MCP server and a cache
to make things more efficient.&lt;/p&gt;
&lt;p&gt;This is the route I went for Hypha as well; not using CLI Printing Press directly, but borrowing from
its manifesto. The Hypha repository ships a &lt;code&gt;hypha&lt;/code&gt; CLI, a &lt;code&gt;hypha-mcp&lt;/code&gt; binary, and a Claude plugin that
teaches Claude to query them.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We have covered the basics, and if you are still interested the
&lt;a href=&quot;https://well-typed.github.io/hypha/&quot;&gt;Hypha documentation site&lt;/a&gt; has everything you need to get started.
I hope I have made a convincing case on why Hypha might be useful, despite it having been mostly coded via
an LLM. Even though it’s beta and there are still bugs to be fixed (and many more to be discovered) I
have found it useful on more than one occasion, especially as a documentation browser. Please get in touch if you have
any feedback and consider &lt;a href=&quot;https://github.com/well-typed/hypha/issues&quot;&gt;filing an issue&lt;/a&gt; for anything that you think should work but doesn’t!&lt;/p&gt;
&lt;section class=&quot;footnotes footnotes-end-of-document&quot; id=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;For the entirety of this section I’m using &lt;code&gt;async&lt;/code&gt; and the &lt;code&gt;mapConcurrentlyBounded&lt;/code&gt; function
it exposes as an example to discuss the problems highlighted below, but the narrative generalizes to
any Haskell package.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Thu, 29 Oct 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Tweag I/O: Running local development services</title>
	<guid isPermaLink="true">https://tweag.io/blog/2026-10-29-running-local-dev-services-with-nix/</guid>
	<link>https://tweag.io/blog/2026-10-29-running-local-dev-services-with-nix/</link>
	<description>&lt;h2 id=&quot;intro&quot;&gt;Intro&lt;/h2&gt;

&lt;p&gt;Nix developer shells are one of my favorite Nix features.
They let you spin up a full environment with every tool you need.
That way, you never have to worry about manually installing dependencies or messing up your system.
They’re also a lifesaver for onboarding – new team members can get to work with one command:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nix develop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But real projects often need services like databases or message queues.
You usually want to run these locally so you can play with data without affecting anyone else, and this is where Nix shells currently fall short.
There’s been some progress in this area, but I think we can still do better.&lt;/p&gt;

&lt;p&gt;In this tutorial, we’ll walk through a hands-on setup to bridge this gap.
We’ll build a lightweight NixOS VM that runs battle-tested Nixpkgs services managed by systemd.
To follow along, all you need to know is how to set up a basic &lt;a href=&quot;https://nix.dev/concepts/flakes&quot;&gt;Nix flake&lt;/a&gt;, use a &lt;a href=&quot;https://nix.dev/tutorials/first-steps/declarative-shell&quot;&gt;Nix dev shell&lt;/a&gt;, and write simple &lt;a href=&quot;https://nix.dev/tutorials/module-system/&quot;&gt;NixOS modules&lt;/a&gt;.
By the end, you’ll have a reproducible, production-grade service stack ready for local development with a single command.&lt;/p&gt;

&lt;h2 id=&quot;the-current-landscape&quot;&gt;The current landscape&lt;/h2&gt;

&lt;p&gt;Running backing services locally alongside an application isn’t a new challenge.
We already have container orchestrators like &lt;a href=&quot;https://docs.docker.com/compose/&quot;&gt;Docker Compose&lt;/a&gt; and &lt;a href=&quot;https://github.com/containers/podman-compose&quot;&gt;Podman Compose&lt;/a&gt;, as well as native process supervisors like &lt;a href=&quot;https://github.com/DarthSim/hivemind&quot;&gt;Hivemind&lt;/a&gt; or &lt;a href=&quot;https://github.com/pvolok/dekit&quot;&gt;dekit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The Nix ecosystem takes this a step further by combining dependency provisioning and process management into a single workflow via tools like &lt;a href=&quot;https://devenv.sh/&quot;&gt;devenv&lt;/a&gt; or &lt;a href=&quot;https://www.jetify.com/devbox&quot;&gt;devbox&lt;/a&gt;.
(Shout out to &lt;a href=&quot;https://github.com/numtide/devshell&quot;&gt;devshell&lt;/a&gt; as well, though its service support is currently limited to PostgreSQL.)
These are great projects – I’m a &lt;a href=&quot;https://devenv.sh/&quot;&gt;devenv&lt;/a&gt; user myself – but I have two gripes with the current approach:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;We’re reinventing the wheel&lt;/p&gt;

    &lt;p&gt;Nixpkgs already has a massive library of service modules.
Yet, tools like &lt;a href=&quot;https://devenv.sh/&quot;&gt;devenv&lt;/a&gt; end up re-implementing them from scratch.
To me, that’s a waste of effort.
You end up with subtle differences and manual porting between projects instead of just using the battle-tested definitions we already have.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Yet another process manager.&lt;/p&gt;

    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;devshell&lt;/code&gt; starts PostgreSQL with a shell script, Devbox uses &lt;a href=&quot;https://f1bonacc1.github.io/&quot;&gt;Process Compose&lt;/a&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;devenv&lt;/code&gt; even has a &lt;a href=&quot;https://github.com/cachix/devenv/tree/main/devenv-processes&quot;&gt;custom process manager&lt;/a&gt;.
Don’t get me wrong, this does the job, but it’s one more thing to learn that only exists in development.
That knowledge doesn’t help you when you’re actually running things in production.
And then there’s the risk of eventually running into corner cases where the two environments behave in subtly different ways.
That’s what Nix was supposed to fix, right?&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My ideal solution?
I want to use the exact same service modules from &lt;a href=&quot;https://github.com/NixOS/nixpkgs&quot;&gt;Nixpkgs&lt;/a&gt; and orchestrate them with a tool I can use in production, like &lt;a href=&quot;https://systemd.io/&quot;&gt;systemd&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;running-a-nixos-vm&quot;&gt;Running a NixOS VM&lt;/h2&gt;

&lt;p&gt;Nixpkgs makes it incredibly easy to spin up a local VM.
Let’s explore how we can leverage this to host our development services.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;For simplicity, we’ll assume that you’re on Linux.
Support for macOS hosts is left as an exercise for the reader. &lt;sup id=&quot;fnref:darwin-support&quot;&gt;&lt;a class=&quot;footnote&quot; href=&quot;https://www.tweag.io/rss.xml#fn:darwin-support&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First, let’s define a helper function to build the VM:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# vm-helper.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;nixosVM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x86_64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;modules&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;lib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;nixosSystem&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;inherit&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;modules&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vm&lt;/code&gt; attribute contains a package with an executable that starts a QEMU virtual machine configured by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;modules&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The key here is that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;modules&lt;/code&gt; argument accepts standard &lt;a href=&quot;https://nixos.org/manual/nixos/stable/#sec-writing-modules&quot;&gt;NixOS modules&lt;/a&gt;.
This allows you to use the exact same configuration blocks you would use for a NixOS system.&lt;/p&gt;

&lt;p&gt;Using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vm-helper.nix&lt;/code&gt; we just wrote, we can define a package that launches the VM:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# flake.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;url&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;github:nixos/nixpkgs/nixos-26.05&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x86_64-linux&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;kn&quot;&gt;inherit&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;sx&quot;&gt;./vm-helper.nix&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;inherit&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;lib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nixosVM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;in&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;packages&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nixosVM&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;modules&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;sx&quot;&gt;./dev-vm.nix&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;configuring-the-guest-vm&quot;&gt;Configuring the guest VM&lt;/h3&gt;

&lt;p&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dev-vm.nix&lt;/code&gt;, we define how the VM behaves.
This file establishes the common configuration we want whenever we run a VM for local development.
Since this is strictly for local use, we can strip away the extras:
we’ll disable graphics, turn off the firewall, and even add a shortcut (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-k&lt;/code&gt;) to instantly shut down the VM:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# dev-vm.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;modulesPath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;imports&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;modulesPath&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/profiles/qemu-guest.nix&quot;&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;modulesPath&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/virtualisation/qemu-vm.nix&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;virtualisation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;graphics&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;networking&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;firewall&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;enable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;programs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;bash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;shellInit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;    bind -x '&quot;\C-k&quot;: poweroff'&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  ''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;getty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;autologinUser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;root&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;stateVersion&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;26.05&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;integrating-with-the-development-shell&quot;&gt;Integrating with the development shell&lt;/h3&gt;

&lt;p&gt;Finally, we want to make launching this VM seamless.
We can add a custom script directly to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;devShell&lt;/code&gt; attribute so we don’t have to remember any long commands:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# flake.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;let&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;legacyPackages&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;in&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;devShells&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;mkShell&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;packages&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;writeShellApplication&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;vm&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;exec run-nixos-vm&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;runtimeInputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;packages&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
          &lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;postgresql&lt;/span&gt;
          &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, whenever you’re in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;devShell&lt;/code&gt;, just run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vm&lt;/code&gt;.
You’ll have a local NixOS VM running whatever services you need, managed exactly like they would be in production.&lt;/p&gt;

&lt;h2 id=&quot;adding-services-to-our-developer-vm&quot;&gt;Adding services to our developer VM&lt;/h2&gt;

&lt;p&gt;With our basic VM in place, we can start adding the services we need.&lt;/p&gt;

&lt;p&gt;To keep things clean, we will split the configuration into two modules:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;production.nix&lt;/code&gt;&lt;/strong&gt;: The standard service configuration.
If you run NixOS in production, this can be the exact same file you deploy to your servers.
(If you deploy elsewhere, like Kubernetes, treat this as your environment-agnostic baseline).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local.nix&lt;/code&gt;&lt;/strong&gt;: Local development overrides (port forwards, relaxed authentication, dev tools).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because the NixOS module system automatically merges declarations together, any setting defined in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local.nix&lt;/code&gt; cleanly overlays onto &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;production.nix&lt;/code&gt; without requiring us to touch the base service definition.&lt;/p&gt;

&lt;p&gt;Here is the plan: we will define the service baseline, layer our local overrides on top, and then wire both modules into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flake.nix&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;1-defining-the-base-services-productionnix&quot;&gt;1. Defining the base services (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;production.nix&lt;/code&gt;)&lt;/h3&gt;

&lt;p&gt;Let’s assume our stack requires &lt;a href=&quot;https://search.nixos.org/options?query=services.postgresql&amp;amp;type=options&quot;&gt;PostgreSQL&lt;/a&gt; and &lt;a href=&quot;https://search.nixos.org/options?query=services.ferretdb&amp;amp;type=options&quot;&gt;FerretDB&lt;/a&gt;.
We declare them in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;production.nix&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# production.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;postgresql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;enable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ensureDatabases&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;app-db&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ensureUsers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;app-db&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;ensureDBOwnership&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;enableTCPIP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ferretdb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;enable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;settings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;FERRETDB_LISTEN_ADDR&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;0.0.0.0:27017&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Since NixOS uses &lt;a href=&quot;https://systemd.io/&quot;&gt;systemd&lt;/a&gt; to manage services, we can easily extend these definitions if needed.
For instance, if you want to run a custom setup script after the PostgreSQL database is up and running, you can simply hook into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postStart&lt;/code&gt; script:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# production.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;systemd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;postgresql&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;postStart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;# Do something&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Since FerretDB can use PostgreSQL as its backend, a more realistic example would be extending the FerretDB systemd unit so that it depends on the Postgres service:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# production.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;postgresql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ensureDatabases&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
      &lt;span class=&quot;s2&quot;&gt;&quot;ferret&quot;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;ensureUsers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;ferret&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;ensureDBOwnership&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ferretdb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;settings&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;FERRETDB_HANDLER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;pg&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;FERRETDB_POSTGRESQL_URL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;postgres://ferret@localhost/ferret&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;systemd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ferretdb&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;after&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;postgresql.target&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;bindsTo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;postgresql.target&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For more complex scenarios, you can even define custom systemd services that depend on PostgreSQL.
This level of control is why leveraging NixOS modules – and the full power of systemd – is so much more effective than re-implementing services inside a separate development tool.&lt;/p&gt;

&lt;h3 id=&quot;2-layering-local-overrides-localnix&quot;&gt;2. Layering local overrides (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local.nix&lt;/code&gt;)&lt;/h3&gt;

&lt;p&gt;In production, you would lock down firewall rules, bind to internal networks, and require secure passwords.
In local development, you usually want the opposite: host-accessible ports and hassle-free authentication.&lt;/p&gt;

&lt;p&gt;Instead of hacking conditionals into our base setup, we use NixOS module composition.
In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local.nix&lt;/code&gt;, we forward ports through QEMU to our host and override PostgreSQL’s auth to trust local connections:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# local.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;postgresql&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;authentication&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      host    all    all   127.0.0.1/0    trust&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;    ''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;virtualisation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;forwardPorts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5432&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5432&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;27017&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;guest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;port&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;27017&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-wiring-everything-into-the-flake&quot;&gt;3. Wiring everything into the flake&lt;/h3&gt;

&lt;p&gt;Now that both modules are defined, we update flake.nix to supply them to our VM helper.&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# flake.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;packages&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nixosVM&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;modules&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;sx&quot;&gt;./dev-vm.nix&lt;/span&gt;
        &lt;span class=&quot;sx&quot;&gt;./production.nix&lt;/span&gt;
        &lt;span class=&quot;sx&quot;&gt;./local.nix&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can also add environment variables to our devShell so we can connect to Postgres and Ferret without having to remember long URLs:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# flake.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;outputs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;nixpkgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;devShells&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;mkShell&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;PG_URI&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;postgres://app-db@localhost/app-db&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;FERRET_URI&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;mongodb://127.0.0.1:27017&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now you’re ready to run the VM:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nix run &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;monitoring-services-on-the-vm&quot;&gt;Monitoring services on the VM&lt;/h2&gt;

&lt;p&gt;One of the best things about tools like &lt;a href=&quot;https://f1bonacc1.github.io/&quot;&gt;Process Compose&lt;/a&gt; is how easily you can view service logs.
While our VM approach is more robust, we still want that same level of visibility.&lt;/p&gt;

&lt;p&gt;There are several ways to accomplish this.
For a high-quality terminal experience, you can use a tool like &lt;a href=&quot;https://github.com/Lifailon/lazyjournal&quot;&gt;lazyjournal&lt;/a&gt;.
It provides a beautiful TUI for navigating systemd logs, giving you a “dashboard” feel for your VM’s services without leaving your terminal.&lt;/p&gt;

&lt;p&gt;Add it to your local configuration and run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lazyjournal&lt;/code&gt; in the console after starting the VM:&lt;/p&gt;

&lt;div class=&quot;language-nix highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# local.nix&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}:&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# ...&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;environment&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;systemPackages&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;pkgs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;lazyjournal&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is just one example of how you can keep extending your local development VM with NixOS module options to suit your needs.&lt;/p&gt;

&lt;h2 id=&quot;closing-notes&quot;&gt;Closing notes&lt;/h2&gt;

&lt;p&gt;Running a full NixOS VM sits at one extreme of the spectrum for managing development environments.
With this approach, we trade runtime performance and iteration speed for full reproducibility.
Compared to native process managers, a VM carries a higher resource footprint and takes a few extra seconds to boot.
However, because this setup is intended for developer workstations, hardware overhead is rarely a bottleneck.
Trading some spare RAM and a slightly slower initial launch for complete host isolation, zero configuration drift, and battle-tested systemd fidelity is well worth the cost.&lt;/p&gt;

&lt;p&gt;The benefits don’t stop at local development.
We can launch this exact same VM in CI to run reproducible integration tests.&lt;/p&gt;

&lt;p&gt;While this post walked through a basic implementation, it could be refined and abstracted into a reusable library.&lt;/p&gt;

&lt;p&gt;You can find the complete working code in the &lt;a href=&quot;https://github.com/tweag/dev-services-nix-demo&quot;&gt;dev-services-nix-demo&lt;/a&gt; repository.&lt;/p&gt;

&lt;p&gt;There’s more to explore, such as customizing the development VM to match your host environment.
For example, we could set up SSH access to the VM without checking keys into version control.
We will come back to that in a future post.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:darwin-support&quot;&gt;

      &lt;p&gt;If you are on macOS, the &lt;a href=&quot;https://github.com/tweag/dev-services-nix-demo&quot;&gt;example repository&lt;/a&gt; features a &lt;a href=&quot;https://github.com/tweag/dev-services-nix-demo/tree/darwin&quot;&gt;darwin branch&lt;/a&gt;.
It adds extra virtualization complexity, so we suggest walking through the main branch on Linux first.
Also, remember that on macOS you need to set up the &lt;a href=&quot;https://nixos.org/manual/nixpkgs/stable/#sec-darwin-builder&quot;&gt;Linux builder&lt;/a&gt; &lt;a class=&quot;reversefootnote&quot; href=&quot;https://www.tweag.io/rss.xml#fnref:darwin-support&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</description>
	<pubDate>Thu, 29 Oct 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Matthew Sackman: 2026: Annus horribilis</title>
	<guid isPermaLink="true">https://wellquite.org/posts/2026_annus_horribilis/</guid>
	<link>https://wellquite.org/posts/2026_annus_horribilis/</link>
	<description>&lt;p&gt;I enjoy staring up at the stars at night and trying to wrap my tiny
brain around the vast distances and time that their light has
travelled. I find it relaxing to consider just how insignificant I am,
and how brief and unimportant my existence is. I am hypnotised when
watching the wind ripple through vast fields of reeds out in Suffolk
and wondering what it is that figures out, at a subatomic level, how
those reeds move, where each atom moves next. &lt;em&gt;Where does the universe
do its maths?&lt;/em&gt; &lt;em&gt;Is the only point of the universe to run its own
simulation?&lt;/em&gt; I think so.&lt;/p&gt;

&lt;p&gt;I am extraordinarily lucky. For the last 25 years, it’s apparently
been the case that I’m better than average at understanding computer
programs. I can anticipate unintended consequences, I’m able to chart
a course between all manner of constraints, and I can make
sufficiently accurate and well-judged trade-offs, that I’ve never had
any difficulties finding employment. A computer program is a
manifestation of the approximate human understanding of a problem. I
enjoy working with humans to understand the problems they’re trying to
solve. I enjoy building up that understanding and those relationships,
and then being able to carefully translate that to code.&lt;/p&gt;

&lt;p&gt;I enjoy too the puzzle-solving aspect of programming. In some cases,
the journey is more rewarding than the destination. Spending days
slowly exploring a failing test, gathering information, teasing apart
your faulty beliefs to finally arrive at an understanding of what’s
wrong with the code. I used to take pride in the fact that I could do
that, and the fact that every time I did, I learnt something new and
for a brief moment, knew more than anyone else on Earth about some
tiny piece of code. Which would probably get deleted in a week or
two. Knowing a code-base inside out, or even just part of it, was a
source of pride for me.&lt;/p&gt;

&lt;p&gt;All of that’s now gone.&lt;/p&gt;

&lt;p&gt;The stress caused by the changes ravaging the software industry this
year have directly made me quite ill. Painful diseases from which it
takes months to recover. We’re now well past the point where I can
pretend there’s any part of writing code at which I can still
out-perform the bots. There may still be one or two esoteric areas,
but you have to go looking hard, and it’s exceptionally unlikely such
areas will remain unconquered for long.&lt;/p&gt;

&lt;p&gt;I’ve seen multiple companies adopt the view that from now on, actually
looking at the code yourself is bad behaviour. The only acceptable
interaction with the code-base is via the bot, and specs must be
written by the bot, reviewed by the bot, and used by the bot to write
the code, which is then itself &lt;a href=&quot;https://www.youtube.com/watch?v=3TNpOD6bov8&quot;&gt;reviewed by the
bot&lt;/a&gt;. I’ve heard of
people being told that asking each other questions, discussing, and
just communicating, is &lt;em&gt;inefficient&lt;/em&gt;. We should now only talk to the
bot. What is the point of life if not to communicate with each other?&lt;/p&gt;

&lt;p&gt;I came across &lt;a href=&quot;https://rikabgambhir.com&quot;&gt;Rikab Gambhir’s&lt;/a&gt;
&lt;a href=&quot;https://terrytao.wordpress.com/2026/10/15/why-are-we-even-doing-all-this/&quot;&gt;post&lt;/a&gt;
on &lt;a href=&quot;https://terrytao.wordpress.com&quot;&gt;Terence Tao’s&lt;/a&gt; blog. This is a
reaction to OpenAI’s dump of mathematical proofs in early
October 2026. Rikab points out that most of the proofs in that dump
are horribly written. As far as I know, there is no known commercial
value to these proofs (particularly given the direction in which
they’ve been proven), so it seems reasonable to me that a human would
seek to prove them &lt;em&gt;in order to prove them&lt;/em&gt;. Intrinsically linked to
that is that the proof can be communicated to others, understood by
others, and verified by others. You can’t build on the past if you
can’t understand the past. OpenAI has no interest in these proofs
beyond advertising.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ultimately, this isn’t just about mathematics or physics – it’s
about fundamentally reimagining what it means to be human. By
outsourcing the burdens of curiosity, creativity, and understanding,
we can finally focus on what matters most: generating value,
maximizing productivity, and accelerating progress towards the
ultimate goal of science: a complete repo of potentially true facts
in a Library of Babel that nobody will bother reading.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Incidentally, I’m aware that some people struggle with those who
express themselves through irony, sarcasm, and acerbic
wit. Personally I have no problem with it and do not believe it
lessens their message nor the points being made.)&lt;/p&gt;

&lt;p&gt;This reminded a little of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Arts_and_Crafts_movement&quot;&gt;Arts and Crafts
movement&lt;/a&gt;
which started in the 1880s: “a reaction against a perceived decline in
standards that the reformers associated with machinery and factory
production”. The trouble is, although hand-made furniture can be
beautiful to behold, to touch, and to use, it takes a long time to
make, and consequently commands a high price tag. The market for
hand-made furniture today is small, and at the luxury end of the
market. I don’t think there will ever be a market for hand-made
software, despite what we sometimes tell ourselves about the beauty of
our own code.&lt;/p&gt;

&lt;p&gt;It also prompted me to think about why was it ever important for me to
understand my own code? Understanding my own code meant I could write
more code that built upon it. It meant I could debug it and correct
it. It meant that, when working within a team, I could explain my
thinking to others, and theirs to me. But the company actually didn’t
care, nor the investors, customers, or users. They care about fixing
the bug, delivering the feature, or supporting the user. If that is
best done by developers understanding each other’s code, then so be
it. But if there are alternative ways of achieving those goals which
don’t rely on understanding code, and could be faster, then they are
to be explored. Unlike finding proofs to mathematical problems, the
code itself is not the goal.&lt;/p&gt;

&lt;p&gt;I wonder whether software is moving a bit closer to other engineering
disciplines like mechanical engineering or civil engineering. I have
no direct knowledge of such disciplines, but I’m guessing that at
university they get taught various fundamental formulae, e.g. how to
calculate the maximum loading of a truss bridge. In their professional
careers, I imagine they never directly use such formulae and
algorithms themselves, and instead rely on large commercial software
packages that can do it all for them, quickly and reliably. Knowledge
and experience certainly helps spot when a result looks unlikely, but
ultimately you trust the software package. Maybe this is what software
engineering will become: you trust the bot to write the code, and it’s
helpful to know what to expect, but ultimately you’re playing a much
more creative role, closer to the customer, and are directly
commissioned by the customer. Having to listen to the customer would
be something of an ultimate irony for an industry for which the term
&lt;em&gt;enshittification&lt;/em&gt; was coined.&lt;/p&gt;

&lt;p&gt;All of which leads me to realise that it’s simply absurd to spend your
life caring about code, and perhaps it ever was. The jobs I’ve had
that I’ve enjoyed the most have been because of the people with whom
I’ve worked, not because of the code. People matter. Friends and
family matter. Being at peace with yourself, with your fragile,
flailing, and fleeting existence, and loving those around you, and
being loved, &lt;em&gt;matter&lt;/em&gt;. Code does not.&lt;/p&gt;

&lt;p&gt;I really enjoy spending time outside, especially in forests. I like
finding huge old trees and thinking about what they’ve seen, standing
for hundreds of years. Trees that were hundreds of years old before I
was born, and will out-live me by hundreds of years. It makes me smile
to think that all of us will be beaten by trees.&lt;/p&gt;</description>
	<pubDate>Wed, 28 Oct 2026 16:01:09 +0000</pubDate>
</item>
<item>
	<title>Tweag I/O: Adopting Nickel in CodeCommons</title>
	<guid isPermaLink="true">https://tweag.io/blog/2026-10-22-code-commons-x-nickel/</guid>
	<link>https://tweag.io/blog/2026-10-22-code-commons-x-nickel/</link>
	<description>&lt;p&gt;Complex architectures with multiple services require a lot of configuration.
There are a lot of knobs to be tuned, and that configuration needs to be persisted in configuration files.
These come in a large variety of formats - widely accepted standards such as YAML or TOML, ecosystem-specific formats such as key-value pairs in the Apache data engineering world, or ad-hoc custom configuration formats.
At the same time, errors in configuration files can lead to hard to detect and frustrating bugs: invalid or missing field values can lead to late crashes after minutes of start-up, silent fallbacks to unwanted defaults due to incomplete or unknown configuration error handling in external packages, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://nickel-lang.org&quot;&gt;Nickel&lt;/a&gt; is a programmable configuration language developed by Tweag that aims to provide safe and validated configurations with ease.
You’ll learn more about the most important Nickel features later, but if you’re impatient, you can read more about it on &lt;a href=&quot;https://nickel-lang.org&quot;&gt;Nickel’s website&lt;/a&gt; or in our &lt;a href=&quot;https://www.tweag.io/blog/2020-10-22-nickel-open-sourcing/&quot;&gt;Nickel series&lt;/a&gt; on this blog.&lt;/p&gt;

&lt;p&gt;This blog post is aimed at developers who have never used Nickel before. I show how we used Nickel to improve the difficult configuration management of a massive data ingestion pipeline, as part of the &lt;a href=&quot;https://codecommons.org&quot;&gt;CodeCommons&lt;/a&gt; project.
This ingestion pipeline relies on multiple industry-standard services, but also on components developed for the &lt;a href=&quot;https://softwareheritage.org&quot;&gt;Software Heritage&lt;/a&gt; (SWH) project, and thus comes with a whole zoo of configuration files formats - all the characteristics of an ideal Nickel testing ground.&lt;/p&gt;

&lt;p&gt;You’ll first learn the most important facts about CodeCommons, the ingestion part of it and Nickel, then I’ll describe how we tied Nickel into the existing architecture and what difference it makes, and finally I’ll summarize our observations.&lt;/p&gt;

&lt;h2 id=&quot;codecommons-ingestion-pipeline-architecture-and-configuration&quot;&gt;CodeCommons ingestion pipeline architecture and configuration&lt;/h2&gt;

&lt;p&gt;In the &lt;a href=&quot;https://codecommons.org&quot;&gt;CodeCommons project&lt;/a&gt;, Modus Create and French research institutions aim to build a reference source code database for training code-generation large language models (LLMs) and for research on source code.
The project builds on top of the Software Heritage archive, which is the world’s largest archive of open-source code.
In the following, I’ll discuss in some detail the configuration of a key part: the source code data ingestion pipeline Modus Create is working on.&lt;/p&gt;

&lt;h3 id=&quot;service-architecture-and-configuration&quot;&gt;Service architecture and configuration&lt;/h3&gt;

&lt;p&gt;The CodeCommons ingestion pipeline mostly relies on the existing Software Heritage ingestion pipeline when it comes to its components, but is deployed on a scientific high-performance computing (HPC) cluster.
CodeCommons uses the same data structure as Software Heritage, namely a Merkle Directed Acyclic Graph (used, for example, by Git), to represent archived source code repositories.
In this view, ingestion of source code is all about adding unique nodes to this graph.
After scheduling ingestion of a project in a Redis queue, loaders read projects from that queue and map new data into the graph data structure - see the following figure for an overview.
&lt;img src=&quot;https://www.tweag.io/ingestion_pipeline_dataflow.svg&quot; width=&quot;42%&quot; /&gt;
The leaves of this graph are source code files, and the loaders write them to custom object storage.
Intermediate nodes represent things such as releases, revisions, or directories, and the loaders write them to distinct topics in a Kafka journal.
From the Kafka journal, a custom-written “deduplicator” component reads topics and exports the graph data into two sets of Optimized Row Columnar (ORC) files:
one appropriate for CodeCommons use, and one that will be used to update the existing Software Heritage archive.&lt;/p&gt;

&lt;p&gt;Service-specific configuration options that a user might want to tune differently from run to run, and deployment-related parameters were all summarized in a simple Bash script with lines of the form &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export CONFIG_OPTION=&amp;lt;value&amp;gt;&lt;/code&gt; that served as our principal configuration file.
This setup led to quite some difficulties, as we’ll see.&lt;/p&gt;

&lt;h3 id=&quot;configuration-challenges&quot;&gt;Configuration challenges&lt;/h3&gt;

&lt;p&gt;One of the particular difficulties with this setup was the fact that service-specific configuration files contained at least three different “classes” of parameters:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a lot of parameters that rarely, if ever, needed to be adjusted (e.g., batch sizes, timeouts, etc.)&lt;/li&gt;
  &lt;li&gt;settings that users of the pipeline realistically might want to change from one pipeline run to the other (e.g., deployment environment, list of projects to ingest, etc.),&lt;/li&gt;
  &lt;li&gt;and finally, configuration options that were known only at pipeline runtime (e.g., hostnames and ports of upstream services).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These classes were non-negotiable - if we didn’t want to adapt the service’s code, we had to live with the above.
At the time, we made it work with the following approach:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;each service used a template configuration file with rarely adjusted settings filled in. This file was in the format natively expected by the service (e.g. key-value pairs for Kafka, YAML for Software Heritage services, etc.). For Redis, for example, this file looked like so:&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;save 60 100000
appendonly yes
rdbcompression yes

protected-mode no

dir ${REDIS_DATA_DIR}
port ${REDIS_PORT}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;settings that might change from pipeline run to pipeline run were stored in a global “Bash config file” &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.sh&lt;/code&gt; with lines &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export PARAMETER=&amp;lt;VALUE&amp;gt;&lt;/code&gt;. A couple example lines from this file were:&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;IS_IN_CLUSTER&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=[&lt;/span&gt;0|1]
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PYTHON_VENV&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;SHAREDWORKDIR&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;/tools/swh/venv
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;KAFKA_NUM_NETWORK_THREADS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;16
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;settings that were known only at runtime were passed from service to service via exchange files that, again, contained relevant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export PARAMETER=&amp;lt;VALUE&amp;gt;&lt;/code&gt; lines, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;REDIS_SLURM_NODELIST&lt;/code&gt; (which was, essentially, the hostname of the compute node running Redis)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, just before running a service, all the above were combined into a final, complete configuration file using tools such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;envsubst&lt;/code&gt;.
This environment variable-based approach also allowed us to use configuration settings in the orchestration Bash scripts directly, e.g. to send a command to the cluster’s job scheduler or run it locally:
For example, the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;REDIS_DEPLOYMENT&lt;/code&gt; was checked in the orchestration script, and depending on its value, the Redis server was started either on the machine the script ran on or on a compute node.&lt;/p&gt;

&lt;p&gt;But this approach had multiple weak points.
On the operational side, when running the pipeline, values were not validated on even a basic level, e.g. encoding a Boolean value wrongly as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;, and local copies of the reference &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.sh&lt;/code&gt; file became outdated quickly:
A developer maintained a config file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config_local.sh&lt;/code&gt;, but after a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git pull&lt;/code&gt; a new mandatory config option might have been added to the template file, which silently invalidated the local configuration.
And there was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config_cluster.sh&lt;/code&gt; over on the HPC cluster, too…
These issues were very common and often led to crashes very late in the pipeline, after minutes-long start-up of preceding services had completed.&lt;/p&gt;

&lt;p&gt;When extending the configuration, sensible / allowed values needed to be documented manually and this documentation needed to be maintained - what was sensible / allowed changed with time. When a new developer had to set up a configuration from scratch, no editor tooling was available that automatically gave you information such as type or allowed values or facilitated life via auto-completion.&lt;/p&gt;

&lt;p&gt;All these made this system difficult to use and to maintain.
How could we do better?&lt;/p&gt;

&lt;h2 id=&quot;reigning-in-the-chaos-nickelizing-the-codecommons-ingestion-pipeline&quot;&gt;Reigning in the chaos: nickelizing the CodeCommons ingestion pipeline&lt;/h2&gt;

&lt;h3 id=&quot;nickel&quot;&gt;Nickel&lt;/h3&gt;

&lt;p&gt;Nickel is, at its core, a bit like JSON with functions, not unlike the &lt;a href=&quot;https://nix.dev/tutorials/nix-language.html&quot;&gt;Nix language&lt;/a&gt; upon which it was designed to be an improvement.
But on top, Nickel offers “configuration safety” features: 
It features contracts and optional static typing to validate data.
While types are checked by Nickel before the program is run, contracts enforce arbitrary assertions lazily at runtime, thus covering different use cases.
Nickel also aims to help its users write configurations that can be easily factorized into reusable parts.
These can then be combined into more complex configurations.
A configuration written in Nickel can then be easily exported into standard configuration file formats such as YAML, TOML or JSON.&lt;/p&gt;

&lt;h3 id=&quot;different-shells-of-nickel&quot;&gt;Different shells of Nickel&lt;/h3&gt;

&lt;p&gt;In our specific project, we could intervene with Nickel on multiple levels, each entailing a different level of complexity:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;we could generate our Bash configuration file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.sh&lt;/code&gt; with Nickel from a nickelized version &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.ncl&lt;/code&gt; of the existing file and leave everything else as is,&lt;/li&gt;
  &lt;li&gt;we could additionally replace service config templates with Nickel configuration equivalents: translate the Kafka / Redis / etc.-specific configuration files to Nickel and merge them with the principal &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.ncl&lt;/code&gt; at pipeline runtime, filling in values only known at runtime upon exporting the service config in the specific format it requires&lt;/li&gt;
  &lt;li&gt;finally, in addition to both of the above, we could generate the actual Bash scripts that run the services with Nickel: each Bash script would be a templated string in Nickel, and completed and exported to a Bash file at runtime when needed. This would remove the need for a Bash config file in the first place, because we wouldn’t need to rely on environment variables that are read by Bash any more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We chose to adopt the first, and partially the second, in the project, but here, I will describe only the first, most straightforward change.&lt;/p&gt;

&lt;h3 id=&quot;generating-the-main-bash-configuration-file-with-nickel&quot;&gt;Generating the main Bash configuration file with Nickel&lt;/h3&gt;

&lt;p&gt;The main Bash configuration file consisted of a flat list of around 120 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export&lt;/code&gt;ed Bash variables, most of which were prefixed with the name of the component they pertained to, for example, a configuration setting for the number of loader workers was called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SWH_LOADER_CONCURRENCY&lt;/code&gt;.
Variables were grouped by component / scope.&lt;/p&gt;

&lt;h4 id=&quot;step-1-grouping-configuration-options&quot;&gt;Step 1: Grouping configuration options&lt;/h4&gt;

&lt;p&gt;To bring order into this, we structured the Nickel equivalent of this configuration as a nested record, which we could feed into an “output” field in the correct format.
A record is very similar to a JSON object, only that Nickel allows additional operations such as merging records.
The very basic nickelized version of our configuration thus looked like so (remember, there were many more settings than shown below):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-nickel&quot;&gt;# schema.ncl
{
  config = {
    swh_loader = {
      SWH_LOADER_CONCURRENCY = 4
    },
    swh_scheduler = {
      SWH_SCHEDULER_DEPLOYMENT = &quot;local&quot;
    },
    deduplicator = {
      DEDUPLICATOR_USE_NIX = 0
    },
    monitoring = {
      GRAFANA_INSTALL_DIR = &quot;/some/dir&quot;
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;schema.ncl&lt;/code&gt;, we declared all the configuration settings that would appear in the final Bash config file.
But this made the configuration neither safer nor much more user-friendly.&lt;/p&gt;

&lt;h4 id=&quot;step-2-simple-contracts&quot;&gt;Step 2: Simple contracts&lt;/h4&gt;

&lt;p&gt;A first step towards a safer configuration was then adding basic &lt;em&gt;contracts&lt;/em&gt;.
A contract serves to validate data at runtime, and is much more flexible than a static type.
For example, we can have a very basic contract &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; that checks at runtime whether a variable is a string, or we can have a more complex contract that checks whether a number is a valid port - a positive natural number within a certain range.
For most of our settings, very basic built-in contracts already did a good job in making sure we didn’t use values that didn’t make sense:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the above number of workers defined by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SWH_LOADER_CONCURRENCY&lt;/code&gt; needed to fulfill &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std.number.PosNat&lt;/code&gt; (be a positive natural number),&lt;/li&gt;
  &lt;li&gt;switches such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SWH_WINERY_ENABLED&lt;/code&gt; needed to fulfill the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Bool&lt;/code&gt; contract,&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GRAFANA_INSTALL_DIR&lt;/code&gt; needed to be a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;,&lt;/li&gt;
  &lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A slightly less trivial contract type we used were enums:
In the above example, you’ll see the string “local”, and another valid value would be “slurm” (to run the scheduler component on a Slurm node).
The contract &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[| 'local, 'slurm |]&lt;/code&gt; then allowed only the values &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'local&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'slurm&lt;/code&gt; for this and similar fields.
When output as a string, these would turn into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slurm&lt;/code&gt; (no single quotes) - just what we needed.&lt;/p&gt;

&lt;p&gt;Nickel also allows you to write more complex custom contracts.
We used such a custom contract to check for correct Boolean value encoding:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-nickel&quot;&gt;let BooleanNumber =
  std.contract.from_validator (match {
    0 =&amp;gt; 'Ok,
    1 =&amp;gt; 'Ok,
    value =&amp;gt; 'Error { message = &quot;Expected 0 or 1, got %{value}&quot; }
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which let a field take a Boolean value encoded as 0 or 1, and made sure that everything else would result in an invalid configuration.&lt;/p&gt;

&lt;p&gt;Finally, we could encode the fact that we wanted some settings to be non-optional by not assigning them a default value.&lt;/p&gt;

&lt;p&gt;In the end, our example looked as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-nickel&quot;&gt;# schema.ncl
{
  config = {
    swh_loader = {
      SWH_LOADER_CONCURRENCY | std.number.PosNat | default = 10
    },
    swh_scheduler = {
      SWH_SCHEDULER_DEPLOYMENT | [| 'local, 'slurm |]
    },
    deduplicator = {
      DEDUPLICATOR_USE_NIX | BooleanNumber
    },
    monitoring = {
      GRAFANA_INSTALL_DIR | String
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once a field is annotated with a contract or a type, the Nickel LSP will immediately be aware and validate the configuration.
This also holds for autocompletion: Nickel’s LSP will know right away and propose you any new element such as a custom contract or a field.
Running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nickel export&lt;/code&gt; on the final configuration will then fail if a field has an invalid value.
But of course Nickel can do more than just hierarchical records with contracts!&lt;/p&gt;

&lt;h4 id=&quot;step-3-modularizing-the-configuration&quot;&gt;Step 3: Modularizing the configuration&lt;/h4&gt;

&lt;p&gt;In Nickel, you can merge two records using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; operator, and you can import a Nickel file into another Nickel file with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import &amp;lt;some_file.ncl&amp;gt;&lt;/code&gt;.
These two useful features allowed us to make further improvements: composing the final configuration from multiple files.
For this step, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;schema.ncl&lt;/code&gt; discussed above stayed largely as it was, only that we removed some defaults for values that we would set later in “overrides” specific for local or cluster deployments.
We added the following modules that we finally all merged with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;schema.ncl&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/blob/v0.0.0-archive/scripts/nickel_config/base/components.ncl?ref_type=tags&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;components.ncl&lt;/code&gt;&lt;/a&gt;: contained enable / disable switches for pipeline components,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/blob/v0.0.0-archive/scripts/nickel_config/base/run_dirs.ncl?ref_type=tags&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run_dirs.ncl&lt;/code&gt;&lt;/a&gt;: contained paths for temporary directories used during ingestion,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/blob/v0.0.0-archive/scripts/nickel_config/base/software.ncl?ref_type=tags&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;software.ncl&lt;/code&gt;&lt;/a&gt;: contained paths to binaries required by the pipeline,&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/blob/v0.0.0-archive/scripts/nickel_config/local/deployment.ncl?ref_type=tags&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;deployment.ncl&lt;/code&gt;&lt;/a&gt;: contained switches that determine where certain components are deployed (login or compute node).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That way, a developer wanting to change an option knew directly where to navigate, and these files also reused common paths, prefixes and the like, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;schema.ncl&lt;/code&gt; really only gave the schema and default values of fields that were rarely touched.&lt;/p&gt;

&lt;p&gt;Finally, for launching a simple, uncustomized run of the ingestion pipeline, we asked the user to complete two short files:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/blob/9f18f163d9f3b6ab255c983b3d44008872d3d2c9/scripts/nickel_config/user_statics_template.ncl&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_statics_template.ncl&lt;/code&gt;&lt;/a&gt;: fields that were user-specific, but were otherwise static across pipeline runs (e.g. the parent directory of all binary dependencies),&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/blob/9f18f163d9f3b6ab255c983b3d44008872d3d2c9/scripts/nickel_config/local_template.ncl&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;local_template.ncl&lt;/code&gt;&lt;/a&gt; / &lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/blob/9f18f163d9f3b6ab255c983b3d44008872d3d2c9/scripts/nickel_config/cluster_template.ncl&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cluster_template.ncl&lt;/code&gt;&lt;/a&gt;: filled pipeline environment and run-specific variables such as the data output directory, and the file with the list of origins to ingest.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this setup, for “just running” the pipeline with sensible default parameters, the user didn’t have to complete a file with ~120 options any more, many of which shared common data, but only had to set a handful of mandatory, intuitive settings.
And of course, a user could customize the configuration to their liking by adding overrides to their copy of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{local,cluster}_template.ncl&lt;/code&gt;, without touching any other file.&lt;/p&gt;

&lt;p&gt;That wraps up the concrete implementation!
If you are interested in looking at the full configuration, you’ll find it on the &lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/tree/9f18f163d9f3b6ab255c983b3d44008872d3d2c9/scripts/nickel_config&quot;&gt;ingestion pipeline’s GitLab repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;lessons-learned-and-observations&quot;&gt;Lessons learned and observations&lt;/h2&gt;

&lt;p&gt;A couple of factors came together that led to introducing the above improvements to the configuration using Nickel: the team working on the ingestion pipeline was struggling a lot with the configuration management; my (now former) colleague Yann, who is mostly behind Nickel, was looking for a project that could be a test case for real-world Nickel adoption; and last but not least, I was simply curious and wanted to learn something new and less “mainstream”.
This section is thus a mix of observations through these three different lenses.&lt;/p&gt;

&lt;h3 id=&quot;impact-on-development-workflow&quot;&gt;Impact on development workflow&lt;/h3&gt;

&lt;p&gt;Running the pipeline became much easier; getting a working configuration was a matter of minutes. This made a difference to new joiners and to engineers like me who had been working on that project for a while already, but had their SSD &lt;a href=&quot;https://bugzilla.kernel.org/show_bug.cgi?id=208123&quot;&gt;self-brick&lt;/a&gt; (twice!) and needed to start from scratch.&lt;/p&gt;

&lt;p&gt;A big improvement over the existing workflow was the availability of an LSP server for Nickel.
If installed and correctly hooked up to your IDE, writing a configuration in Nickel feels just as developer-friendly as working on the actual pipeline code with a good Python LSP.
As mentioned earlier, the LSP validates your configuration on-the-fly and yells at you as soon as you make a mistake such as assigning a value of the wrong type or that doesn’t fulfill a certain contract to a field.
Note that this also applies to non-trivial, custom contracts, such as, for example, making sure a list of users had unique user IDs.
(There is a blog post on &lt;a href=&quot;https://www.tweag.io/blog/2024-05-16-nickel-programmable-lsp/&quot;&gt;Nickel LSP’s contract evaluator&lt;/a&gt;, if you are interested.)
Auto-completion is likewise available instantaneously, and of course the Nickel LSP supports formatting your code, too.
This way, you get immediate feedback about the validity of your configuration, and changes are fast and comfortable to implement.&lt;/p&gt;

&lt;h3 id=&quot;adoption-by-other-developers&quot;&gt;Adoption by other developers&lt;/h3&gt;

&lt;p&gt;It is always hard to push engineers to learn a new tool with the promise of replacing something that works with something that works better.
Are we sure it will work better?
Will it be worth the effort?
Can I just get my work done and learn this new skill later?&lt;/p&gt;

&lt;p&gt;In our case, it was possible to introduce many of the benefits of Nickel without initially forcing anyone to use it - for a while, we maintained two copies of the main configuration:
the Nickel version and the original, plain Bash version.
This was obviously a maintenance burden, which we softened by the mechanism described in the next section.
In the meantime, though, we removed the Bash config file from version control for easier maintenance, and developers were then using the Nickel config to generate their personal configuration files.
Validation of using Nickel came then notably by an engineer who joined the project after the first version of the Nickel config was finished.
This engineer did not encounter major configuration issues, and was instead very glad to be able to spend their time smoothing out other rough edges!&lt;/p&gt;

&lt;h3 id=&quot;short-detour-keeping-team-members-up-to-date-about-configuration-file-changes&quot;&gt;Short detour: keeping team members up to date about configuration file changes&lt;/h3&gt;

&lt;p&gt;As mentioned before, for a while, we kept both the old Bash template config file &lt;em&gt;and&lt;/em&gt; the Nickel config existing in parallel.
Now what happened if a team member, who didn’t use the Nickel config yet, updated the Bash template and pipeline code with a mandatory config field?
This change would be easy to miss for developers not using the Bash template any more.
To avoid team members using outdated Nickel configurations, we set up an &lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion/-/blob/9f18f163d9f3b6ab255c983b3d44008872d3d2c9/.gitlab-ci.yml#L181&quot;&gt;additional step&lt;/a&gt; in our GitLab CI pipeline:
Whenever a commit was merged on the main branch that changed the Bash config file, an issue telling developers to update the Nickel config and to adapt any local configuration files was opened.
The issue text linked to the specific MR that changed the config file.&lt;/p&gt;

&lt;p&gt;This would actually have been a good idea way earlier - we often encountered the issue of local configuration file copies being out of date: engineers would copy the template config file to, say, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.local_simeon.sh&lt;/code&gt;, but then the upstream &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.sh&lt;/code&gt; would change, rendering the engineer’s copy invalid.
Missing a single small MR was then all it needed to get possibly hard to debug crashes when running the pipeline the next time.
We imagine that this is a common problem, and even if you decide after having read this blog post that Nickel is not for you, perhaps you can still make use of this completely configuration language-neutral trick.&lt;/p&gt;

&lt;h3 id=&quot;learning-curve&quot;&gt;Learning curve&lt;/h3&gt;

&lt;p&gt;This is more of a subjective topic, but it’s difficult to report about introducing a new tool to an existing project without mentioning the learning curve.&lt;/p&gt;

&lt;p&gt;Admittedly, it was a long-time goal of mine to learn Nickel, so the basic motivation was high already.
Nevertheless, it turned out that getting to a first working (and useful!) version of the configuration was rather easy.
After having grasped the basic idea of writing a nested record and the trivial, but painful task of copying and splitting up the existing configuration into useful sections, the only slightly difficult part was writing the custom code to output the correctly formatted Bash file:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-nickel&quot;&gt;let config_string = config
  |&amp;gt; flatten_merge
  |&amp;gt; std.record.to_array
  |&amp;gt; std.array.map (fun { field, value } =&amp;gt; &quot;export %{field}=\&quot;%{escape_single_quotes value}\&quot;&quot;)
  |&amp;gt; std.string.join (&quot;\n&quot;)
  in [...]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As an imperative programmer by training who’s been functionally curious for a little while, that whopper was the one part of “real” coding that required some help from the pros.
I had a rough idea of what to do, but the required functional way of thinking turned this into an actual effort.
Yann again stepped in to help me set up the above pipeline that flattens the hierarchical record (which is the most difficult part), converts data structures, and finally constructs the correctly formatted lines.
It was then relatively straight-forward to add basic types and mark fields as required, which yielded a big improvement over editing a Bash config file template by hand.&lt;/p&gt;

&lt;p&gt;When I became more independent after a little while, the &lt;a href=&quot;https://discord.com/channels/1174731094726295632/1179430745727586407&quot;&gt;Nickel Discord channel&lt;/a&gt; with very responsive and helpful members was of great help.
But if your use case requires a common configuration format supported natively by Nickel, then there’s a good chance you can get pretty far without writing a single recursive function!&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In this tutorial / use case / experience report / blog post hybrid, I showed how we were able to significantly improve our configuration management using basic Nickel.
I hope this helps you think about what a simple Nickel use in your own project could look like, and what gains and efforts you might expect.
As always, if you have any questions, insights, or are curious about the CodeCommons project or our Nickel adoption, don’t hesitate to reach out in the Discord channel or via the &lt;a href=&quot;https://gitlab.softwareheritage.org/teams/codecommons/github-ingestion&quot;&gt;CodeCommons ingestion pipeline GitLab project&lt;/a&gt;.&lt;/p&gt;</description>
	<pubDate>Thu, 22 Oct 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Alessandro Vermeulen: A Tale of Three Months with AI: Guardrails, and How We Are More Alike Than We Wish to Admit</title>
	<guid isPermaLink="true">https://alessandrovermeulen.me/posts/a-tale-of-three-months-with-ai-guardrails-and-how-we-are-more-alike-than-we-wish-to-admit/</guid>
	<link>https://alessandrovermeulen.me/posts/a-tale-of-three-months-with-ai-guardrails-and-how-we-are-more-alike-than-we-wish-to-admit/</link>
	<description>&lt;p&gt;As I promised in &lt;a href=&quot;https://alessandrovermeulen.me/posts/a-fresh-leaf/&quot;&gt;A fresh leaf&lt;/a&gt;, I want to share my experiences with using LLMs for agentic development in the last three months.
Summarised: I started as an initial sceptic, became quite enthusiastic, and am now moderately enthusiastic and guarded for the future.
Allow me to take you through my journey.&lt;/p&gt;
&lt;p&gt;Coming from a background of rigorous engineering with up-front thinking about program structures, algorithms, and types, it seemed incomprehensible to me how a jumped-up neural net could generate working code, let alone sensible systems.
How to write understandable software has been hotly debated for decades, continuously shifting as new languages, type systems, and ecosystems introduce different trade-offs.&lt;/p&gt;
&lt;p&gt;Early in my career, coming from academia, I was fixated on finding the “just-right” solution: elegant abstractions, strict correctness, and formal soundness.
As problems grew larger and messier, and time became scarce, holding out for perfection became an obstacle.
I found far more satisfaction in achieving &lt;em&gt;something&lt;/em&gt; functional, with elegance as a welcome second.
Having both overlap is fantastic; achieving the actual goal remains both a priority and a necessity.&lt;/p&gt;
&lt;p&gt;This shift in perspective explains why LLMs, and agentic development, eventually proved so attractive to me.
Expecting a model to generate a “just-right” architecture, code, or text only causes frustration.
Conversely, it acts as an engine to reach concrete milestones in areas I was always curious about without having the time to do things “properly”, or simply eliminating the &lt;em&gt;toil&lt;/em&gt; of writing boilerplate when I can describe my intent with less mechanical precision.&lt;/p&gt;
&lt;p&gt;If your goal is simply to achieve something concrete, they are remarkably effective.
That spans from something as simple as integrating an unfamiliar library to breaking ground in an unfamiliar problem domain where you lack the tools and vocabulary.&lt;/p&gt;
&lt;h2 class=&quot;relative group&quot;&gt;The greenfield honeymoon
&lt;div class=&quot;anchor&quot; id=&quot;the-greenfield-honeymoon&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none&quot;&gt;
&lt;a class=&quot;text-primary-300 dark:text-neutral-700 !no-underline&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#the-greenfield-honeymoon&quot;&gt;#&lt;/a&gt;
&lt;/span&gt;
&lt;/h2&gt;
&lt;p&gt;My catalyst was a device sitting in my living room.
After reports emerged that its successor was aggressively hostile to user privacy, I wanted to verify whether my own hardware behaved the same way.
Since I had no background in firmware inspection or packet analysis on embedded devices, I was initially discouraged.
Luckily, I noticed right then that I had a free trial month of Codex lying around, so I decided to see how far these so-called “amazing frontier” models could carry me.&lt;/p&gt;
&lt;p&gt;I started out by talking in ChatGPT and asking questions on how to solve my problem.&lt;/p&gt;
&lt;aside class=&quot;observation-box my-6 rounded-r-xl border-l-4 border-primary-500 bg-neutral-100/70 p-4 shadow-sm dark:bg-neutral-800/50&quot;&gt;
&lt;div class=&quot;observation-header font-bold text-neutral-900 dark:text-white&quot;&gt;
&lt;span class=&quot;observation-label&quot;&gt;&lt;/span&gt;&lt;span class=&quot;observation-title text-neutral-600 dark:text-neutral-400 font-normal&quot;&gt; (Search engine on steroids)&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;observation-body mt-2 text-sm leading-relaxed text-neutral-700 dark:text-neutral-300&quot;&gt;
I realised I was using ChatGPT more and more as a search engine on steroids: give it a simple question, let it figure out where to search, and synthesise the answer.
&lt;/div&gt;
&lt;/aside&gt;
&lt;p&gt;This quickly became using Codex to work on my laptop, letting it download images, and running local scripts.
The way of work was very interactive, prompt-based, and relatively unstructured and unguided.&lt;/p&gt;
&lt;aside class=&quot;observation-box my-6 rounded-r-xl border-l-4 border-primary-500 bg-neutral-100/70 p-4 shadow-sm dark:bg-neutral-800/50&quot;&gt;
&lt;div class=&quot;observation-header font-bold text-neutral-900 dark:text-white&quot;&gt;
&lt;span class=&quot;observation-label&quot;&gt;&lt;/span&gt;&lt;span class=&quot;observation-title text-neutral-600 dark:text-neutral-400 font-normal&quot;&gt; (Over-caffeinated Junior Engineers)&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;observation-body mt-2 text-sm leading-relaxed text-neutral-700 dark:text-neutral-300&quot;&gt;
I soon began to realise that LLMs responded a lot like humans, and later like over-caffeinated junior engineers.
The more concrete and narrow the scope of an assignment or question the better.
&lt;/div&gt;
&lt;/aside&gt;
&lt;p&gt;I was amazed by how well the model worked when told to achieve a very specific goal and even more so when giving the context and constraints in a single prompt: as long as the prompt wasn’t too big.&lt;/p&gt;
&lt;p&gt;When I pointed out errors, the models complimented me lavishly on the discovery and apologised profusely for the defect.&lt;sup id=&quot;fnref:1&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;
Yet that contrition yielded no durable change in their subsequent behaviour.
It was just text, learned from reams of documents.&lt;/p&gt;
&lt;h3 class=&quot;relative group&quot;&gt;Asserting control
&lt;div class=&quot;anchor&quot; id=&quot;asserting-control&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none&quot;&gt;
&lt;a class=&quot;text-primary-300 dark:text-neutral-700 !no-underline&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#asserting-control&quot;&gt;#&lt;/a&gt;
&lt;/span&gt;
&lt;/h3&gt;
&lt;p&gt;As soon as the codebase and program to detect any nefarious behaviour was growing, I noticed the performance of the models degrade.&lt;sup id=&quot;fnref:2&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;
Responses lost their initial speed, precision, and correctness.
The model began overlooking interdependent updates that required lock-step coordination, shedding context established in earlier turns.
In other words, it required more hand-holding, for which I had neither time, nor patience, nor appetite.&lt;/p&gt;
&lt;p&gt;At that point, I also started working on some other ideas that had been lying dormant in my head.
I used an agent to create a Terraform project to spin up a small timeseries-db backup in Google Cloud constrained such that it would stay within the free tier.
Creating the provisioning scripts was the work of mere moments.
It checked the exact sizing requirements for staying within the free tier including some caveats, whipped the Terraform code together, and added the Tailscale integration.
All from a simple prompt of what I wanted to achieve and the fact that I wanted to be able to reconstruct the VM from a single command.
That was awesome.&lt;/p&gt;
&lt;p&gt;It was here that I first started suspecting a deeper link between models and humans: it all came down to how bounded the task was.
Over the years, I noticed that when you ask a person an open-ended question, their answer is mostly influenced by their recent history, their emotional state, and whatever problem they were just wrestling with.
Models work almost identically.
Ask them a broad, open-ended question and you get a generic response shaped by their pre-trained weights and immediate context window.
Getting either a human or a model to think outside that default box requires tight, explicit constraints, and careful prodding (or prompting).&lt;/p&gt;
&lt;p&gt;One of the often touted best-practices I encountered in the beginning was to have a three-phase approach.
Use one agent to plan the work interactively, producing a handover document.
Then task a second agent with implementing that handover document unattended.
Finally pass the control to a review agent, which gets the diff and the plan, and reviews it.
The obvious improvement is to make the last two communicate and work in a loop until no more findings are established.&lt;/p&gt;
&lt;p&gt;I first tried automating this division of work (planning, implementation, and review) by writing conventions as prose in &lt;code&gt;AGENTS.md&lt;/code&gt;.
As code grew and threads grew longer, however, prose instructions became less effective and easily ignored by the model.
To take this workflow further, I wanted to combine the specialised strengths of different models: Codex as the fast, local workhorse for writing code, and Gemini as the thoughtful reviewer for architecture.
To let them talk directly to each other, I built &lt;code&gt;gemini-mcp&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here I hit another quirk of model psychology: autonomous models possess such supreme self-certainty that review triggers require explicit programmatic conditions.
A loose prose directive like &lt;em&gt;“Ask Gemini for help when uncertain”&lt;/em&gt; generated precisely zero invocations; the model was never uncertain.
Conversely, a deterministic circuit breaker (“Attempt to solve each problem a maximum of three times; if still failing, consult Gemini via MCP, implement its recommendations, and repeat”) reliably prompted Codex to escalate for second opinions.&lt;/p&gt;
&lt;p&gt;This put me with an immediate security challenge: because I was using this setup to assess the privacy of the TV, I needed a classifier to prevent prompts between models from looking like offensive security probes, which could get my Google account suspended.&lt;/p&gt;
&lt;p&gt;Scaffolding the classifier was quick work.
Making it function reliably, however, involved dozens of subtle architectural choices that flew past too fast for me to properly evaluate.
Steering the model through prose in &lt;code&gt;AGENTS.md&lt;/code&gt; simply wasn’t giving me the control I needed.
That was the moment I turned to systematic, deterministic tooling, starting with a simple directive: run &lt;code&gt;make check&lt;/code&gt; after every modification to verify that the project was still coherent.&lt;/p&gt;
&lt;p&gt;This worked so well that ever since I have been extending what is behind this check continuously.
Relying on what I already knew worked well in larger projects with multiple contributors, I decided to use strict tools to check as much as possible.
This meant automatic linting, formatting, and upgrades of the different types of files in the project:
Markdown, code, YAML files, ADRs, formatting, modernisation of Go, and dependency upgrades.
Moreover, I decided to take more out of the hands from the agents and not even bother them with formatting and easy to fix linting errors by automatically running formatting, automatic imports, and adding &lt;code&gt;--fix&lt;/code&gt; to golangci-lint already as part of the &lt;em&gt;check&lt;/em&gt; step to ensure all easy fixes were implemented before the agent even saw the result.&lt;sup id=&quot;fnref:3&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:3&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;aside class=&quot;observation-box my-6 rounded-r-xl border-l-4 border-primary-500 bg-neutral-100/70 p-4 shadow-sm dark:bg-neutral-800/50&quot;&gt;
&lt;div class=&quot;observation-header font-bold text-neutral-900 dark:text-white&quot;&gt;
&lt;span class=&quot;observation-label&quot;&gt;&lt;/span&gt;&lt;span class=&quot;observation-title text-neutral-600 dark:text-neutral-400 font-normal&quot;&gt; (What helps human engineers, helps agents even more)&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;observation-body mt-2 text-sm leading-relaxed text-neutral-700 dark:text-neutral-300&quot;&gt;
Battle-tested tooling such as compilers, type systems, linters, tests, libraries, proper documentation, and proper (actionable) error messages help humans a lot.
They help agents continue forwards even faster, multiplying the leverage.
&lt;/div&gt;
&lt;/aside&gt;
&lt;p&gt;I introduced this rigorous tracking in my first big vibe-coded project, &lt;a href=&quot;https://github.com/spockz/semedit&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;semantic-editor&lt;/code&gt;&lt;/a&gt;.
The whole point of the project was to let the agent worry about intent while delegating the nitty-gritty editing work to deterministic AST tools.
I applied that same mindset to the repository itself, using strict checks for the markdown, the ADRs, and the schemas.
This approach worked pretty well, and the project was progressing smoothly.&lt;/p&gt;
&lt;p&gt;Around that time, another personal project came into my head.
I had always felt sad that in this age of search engines and lately omniscient (and often wrong) LLMs, my kids didn’t have the experience to just browse an encyclopedia, guided by interest.
For me the concrete example of this was Microsoft Encarta ‘97.
It gave me endless browsing, learning of all kinds of things, and important to my parents (and now me), relatively safe because it was curated content.&lt;/p&gt;
&lt;p&gt;As you may have guessed, I decided to vibe code the answer to this problem as well and make a viewer of Encarta data.
Because in-repository decision tracking had operated satisfactorily, I relied on that established harness to guide development.&lt;/p&gt;
&lt;p&gt;However, because this was my first GUI app, I sensed it needed another layer of constraints and decision tracking which I dubbed “GUI-invariants” as a specialised form of ADR for GUI decisions such as layout on a more generic level instead of specific decisions.
Getting the agent to consider the invariants proved relatively simple by just adding it to the agent instructions.
Letting it interpret them in a way was harder, because invariants are inherently generic.
So they needed more concrete positive and negative examples and be more prescriptive.&lt;/p&gt;
&lt;p&gt;For some reason I had a quirk that I wanted all text to be “properly” type-set such as in TeX.
And why not?
We are custom coding this project and I have tokens to burn, so let’s have at it.
The agent was very helpful in this bounded context.
A simple assignment, a starting example algorithm, the specific constraint of the SwiftUI infrastructure, and an oracle to determine whether it did it right.
This allowed me to set a simple goal and I could walk away from it.&lt;/p&gt;
&lt;p&gt;These three main projects and several side projects, all shared one important property: they were greenfield projects where I individually was the sole owner and decision maker.&lt;/p&gt;
&lt;p&gt;In urban planning terms, greenfield land is effortless to traverse and develop, precisely because it is virgin ground: untouched, open, and unencumbered by history.
Brownfield sites, by contrast, are contaminated swaths of land burdened by buried foundations, undocumented pipelines, and wrapped in a veneer of toxic legacy waste.
Left unattended, software tends to follow an identical trajectory.
When projects expand without discipline, the virgin meadow rapidly deteriorates into a muddy, hazardous swamp that engineers dread to touch.&lt;/p&gt;
&lt;p&gt;Yet that decay is avoidable.
Long-lived systems like the Linux kernel survive decades of churn not through bureaucratic upfront planning, but through uncompromising maintainer gatekeeping: strict subsystem boundaries, relentless review against established patterns, and clear commit rationale.&lt;sup id=&quot;fnref:4&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:4&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;
We should learn the lesson and apply it to agentic programming.
Because general purpose models lack this inherent institutional memory and intrinsic motivation, we must codify that gatekeeping directly into the environment: tracking architectural decisions, enforcing invariant suites, and maintaining deterministic mechanical guardrails.&lt;/p&gt;
&lt;p&gt;Handled with that discipline, a codebase avoids becoming a toxic brownfield.
Instead, it becomes a &lt;strong&gt;cultivated glasshouse&lt;/strong&gt;: a structured, highly productive environment where automated checks supply continuous nourishment, and recorded context allows development to continue to flourish at speed.&lt;/p&gt;
&lt;h2 class=&quot;relative group&quot;&gt;From the green pasture into the mud
&lt;div class=&quot;anchor&quot; id=&quot;from-the-green-pasture-into-the-mud&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none&quot;&gt;
&lt;a class=&quot;text-primary-300 dark:text-neutral-700 !no-underline&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#from-the-green-pasture-into-the-mud&quot;&gt;#&lt;/a&gt;
&lt;/span&gt;
&lt;/h2&gt;
&lt;p&gt;While agents seem to shine on greenfield projects that they manage themselves, which is why vibecoding works so well, they start falling flat when projects become larger or they need to be used in established projects.
This is because the structure agents use to locate where to make changes mirrors the latent patterns ingrained during training.&lt;sup id=&quot;fnref:5&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:5&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;
What a coincidence, this is also the same for human engineers.&lt;/p&gt;
&lt;p&gt;My experience creating the initial draft of the dynamic-fault-injection filter for an open-source “Built on Envoy” project exposed this boundary.
I had the foundational distribution formula and prior experience running it in a JVM-based sidecar; directing an agent to scaffold the initial Go plugin was straightforward.
The friction began when integrating it upstream.
Unlike self-owned projects where you can invent your own shortcuts, I needed to fit cleanly into the project’s established discipline: adhering to its battle-tested test patterns, documentation conventions, and repository layouts.&lt;/p&gt;
&lt;p&gt;Anyone can staple a quick fix onto a codebase, and just that quickly another wart is added.
The actual challenge is figuring out where the change truly belongs, and doing the painstaking rework so it looks like it was designed that way from the start.
Occasional contributors face this constantly: deciding when to live with an existing pattern, and when to roll up your sleeves and rewrite surrounding code to make it fit properly.&lt;/p&gt;
&lt;p&gt;Navigating that friction succeeded primarily because I received generous, precise guidance from the lead maintainer.
That human feedback supplied the unspoken repository context and architectural intent that neither I nor the model could extract from the code alone.&lt;/p&gt;
&lt;p&gt;I could then feed those maintainer insights directly back into prompts, giving the agent the concrete boundaries it needed to shape the implementation correctly.&lt;/p&gt;
&lt;p&gt;However, sitting down quietly with a cup of tea and looking over the code, I realised the code organisation was still a bit off.
That intuition was only available as latent skill in my head based on code scattered across the repository, and admittedly not applied consistently enough for an untrained eye like mine on the project.
This is where concrete ADRs and contributing guides earn their keep: they turn what is locked in maintainers’ heads into explicit rules agents can actually read.&lt;/p&gt;
&lt;p&gt;When tackling bigger projects, and more-insufficient usage limits, I started looking into other models and noticed that the model families tend to have similar behaviour.&lt;/p&gt;
&lt;p&gt;To me these are the qualifications of the model families, that I have personal experience with. Even though they are a mathematical probabilistic bag of parameters, they seem to have emergent personalities:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hyperactive Junior Engineer Qwen&lt;/strong&gt;: The Qwen family (both 3.6 dense, MoE, and 3.8) generates code at blistering speed, rarely pausing to ask questions. It demands highly granular guidance. The 2-bit Saluki quantisation displays this impulsiveness even more acutely.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Expensive easily-distracted CS Professor Gemini&lt;/strong&gt;: Extremely strong at reducing a problem description to its core and finding the academic consensus on the solution fit. It is particularly strong in Go and machine-learning topics, and great at explaining &lt;em&gt;why&lt;/em&gt; models behave in certain ways. Especially at the architecture level.&lt;/p&gt;
&lt;p&gt;However, it is expensive for routine coding, not &lt;em&gt;that fast or good&lt;/em&gt;, and gets stuck in repetitive read loops, endlessly inspecting files without making changes:
&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;Gemini stuck in a read-inspection loop&quot; class=&quot;my-0 rounded-md&quot; src=&quot;https://alessandrovermeulen.me/images/archive/stuck-gemini.png&quot; /&gt;&lt;/figure&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Gemini 4.0 Argon is even worse for everyday work: a review of a small piece of text burns through 50% of my weekly Pro allowance. No matter how good it is, it is unusable in this way.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dependable, rabbit-holing workhorse GPT&lt;/strong&gt;: This is the family that is actually interesting, especially given the pricing strategies. In general, I found GPT to rabbit-hole into a particular solution direction faster and refuse to get out of it, where Gemini could step back. Sol 6.1 (and prior with 5.6 Terra) is the perfect reviewer and Luna (both 5.6 and 6) is the perfect workhorse. Luna is decently smart, but especially because it is virtually free. Both Sol and Luna have the tendency to double down on a chosen path and are very hard to get to think outside the box or take a step back.&lt;/p&gt;
&lt;p&gt;Across all these model families, one shared quirk emerged: an almost infuriating default bias toward Python for writing quick scripts.
Instructing an agent explicitly &lt;em&gt;not&lt;/em&gt; to use Python invariably yielded TypeScript.
This is clearly the gravitational pull of public training data, where Python and TypeScript examples dwarf everything else.&lt;sup id=&quot;fnref:6&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:6&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;
It constantly left me longing for something like &lt;code&gt;goshell&lt;/code&gt;: a single standalone Go file declaring its dependencies inline, executed directly without scaffolding an entire module.&lt;/p&gt;
&lt;p&gt;AI companies love to shout about breakthroughs at the absolute peak of their frontier models.
Yet what actually changed my day-to-day experience was not a dramatic leap in the raw intelligence of the top tier.
The real shift was the surge in quality among the &lt;em&gt;affordable&lt;/em&gt; models: the ones actually in reach for everyday work.
A model like Sol 6.1 brings reasoning that used to belong exclusively to flagships, but at a price where you can afford to keep it running continuously in tight feedback loops.
It is not that the absolute Pareto frontier has moved, but that the &lt;em&gt;affordable&lt;/em&gt; Pareto frontier has moved.
In that sense, practical agentic development has become far more of an economics game than a science game.&lt;sup id=&quot;fnref:7&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:7&quot;&gt;7&lt;/a&gt;&lt;/sup&gt;
Slashing the price of a monster flagship like Astra or Argon would be an easy way to win the “smartest model” contest, but subsidising those gargantuan inference costs would burn through cash at a staggering rate.
The provider that wins the practical developer ecosystem won’t necessarily be the one with the cleverest paper, but the one that delivers reliable reasoning at a price where hundreds of daily iterations don’t break the bank.&lt;/p&gt;
&lt;p&gt;Choosing the right model for the task at hand is an important part of being able to work in large code-bases.
However, more is needed.&lt;/p&gt;
&lt;h2 class=&quot;relative group&quot;&gt;The Engineering harness
&lt;div class=&quot;anchor&quot; id=&quot;the-engineering-harness&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none&quot;&gt;
&lt;a class=&quot;text-primary-300 dark:text-neutral-700 !no-underline&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#the-engineering-harness&quot;&gt;#&lt;/a&gt;
&lt;/span&gt;
&lt;/h2&gt;
&lt;p&gt;Looking back at my career on what made successful teams achieve their successes besides their social skills and trust among themselves, was the fact that they spend time on great tooling. Even the best of minds have a limit to how much they can keep in their RAM, how fast they can write new code, and how fast they can diligently review code.&lt;/p&gt;
&lt;p&gt;They use tools to prevent discussions about code style by applying linters and auto formatting rules, anywhere from in a check command, a git hook, or in the PR reviews.
This avoids noise.&lt;/p&gt;
&lt;p&gt;They have an integral test-suite, that lets them move fast ahead with ideas, while mistakes are still caught.
This integral test suite goes further than mere unit tests and includes tools that have been around in various variations.&lt;/p&gt;
&lt;p&gt;It automatically tests for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Performance
&lt;ul&gt;
&lt;li&gt;Microbenchmarking&lt;/li&gt;
&lt;li&gt;Profiling&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Security
&lt;ul&gt;
&lt;li&gt;Static analysis&lt;/li&gt;
&lt;li&gt;Dynamic pentesting tools&lt;/li&gt;
&lt;li&gt;Fuzzers&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Quality
&lt;ul&gt;
&lt;li&gt;Linting&lt;/li&gt;
&lt;li&gt;Coverage and coverage trends&lt;/li&gt;
&lt;li&gt;Property tests and mutation testing&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Notice how far away this is from reams of prose.
Humans with a limited span of attention and limited brain capacity cannot map their every action against a bookwork of architecture and procedural rules, it would be too time consuming.
They use heuristics to drive their “online” work, and “offline” guard rails to prevent their oversights from hitting customer impact.
These same measures apply with agentic coding.&lt;/p&gt;
&lt;p&gt;Because agents are LLMs with extra rules, as long as you have compute power, and sufficient supply of well thought out jobs / issues, the agents can produce code faster and without bottlenecks.
This increases the leverage, arm, or multiplier of how much agents benefit from having these guardrails in place.&lt;/p&gt;
&lt;h3 class=&quot;relative group&quot;&gt;Institutional Knowledge
&lt;div class=&quot;anchor&quot; id=&quot;institutional-knowledge&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none&quot;&gt;
&lt;a class=&quot;text-primary-300 dark:text-neutral-700 !no-underline&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#institutional-knowledge&quot;&gt;#&lt;/a&gt;
&lt;/span&gt;
&lt;/h3&gt;
&lt;p&gt;Guardrails in the form of rigorous automated test suites, as introduced above, carry us only so far.
They cannot remember the &lt;em&gt;WHY&lt;/em&gt;: why do we have a certain test, why do we implement a feature, why did we choose a particular library.&lt;/p&gt;
&lt;p&gt;In smaller, one-person greenfield projects, recording the decisions in simple ADRs was good enough to keep the agent bounded and give me a mechanism for manually pointing out to adhere to the ADRs.
In sprawling brownfields and cultivated glasshouses alike, decisions come from many different directions; the institutional experience of the engineering corps is heterogeneous and in continuous flux.&lt;/p&gt;
&lt;p&gt;This is what organisational theorists and software architects also refer to as “Institutional memory”. &lt;sup id=&quot;fnref:8&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:8&quot;&gt;8&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;In humans this knowledge is gradually ingrained into the engineer through experience.
If you are lucky they stay on the projects long enough to provide continuity.&lt;/p&gt;
&lt;p&gt;Agents have no such context at all.
Actually, engineers are &lt;em&gt;also&lt;/em&gt; fallible.
They can forget. (Context compaction and fresh sessions)
They can have a lapse of focus or being disturbed. (The “lost-in-the-middle” effect&lt;sup id=&quot;fnref:9&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:9&quot;&gt;9&lt;/a&gt;&lt;/sup&gt;.)
They can move on to a new project. (Starting brand new chats.)&lt;/p&gt;
&lt;p&gt;With agents, that departure happens constantly: every time you open a brand-new chat thread, every unrecorded insight simply evaporates into /dev/null (unless you count the provider retraining on your prompts ;-) ).&lt;/p&gt;
&lt;p&gt;Thus we need a system that actually benefits both engineers &lt;em&gt;and&lt;/em&gt; agents and so I extended the harness in two ways:&lt;/p&gt;
&lt;p&gt;First: I introduced &lt;strong&gt;Research Questions&lt;/strong&gt;, capturing the &lt;em&gt;intent&lt;/em&gt; of any change or feature.
Basically it represents the inbox to the project.
Before an implementation starts, stakeholders and engineers discuss goals, trade-offs, and explore alternatives.
This leads to what to build and more importantly what &lt;strong&gt;not&lt;/strong&gt; to build.
This is context that very rarely survives into issues, pull-requests, and/or commit messages and is therefore lost.
By recording these deliberations in the research questions, it retains this information while at the same time also allows ADRs to stay small, concrete, and focussed, to stay kind to the limited context windows of Agents.&lt;/p&gt;
&lt;p&gt;Secondly: Even with this mechanism in place, I noticed that there was still drift so I added a rule to check nightly or weekly to do a full reconciliation of changes against established ADRs and of code against Research Questions to see whether new data had come in or we actually already made decisions.&lt;/p&gt;
&lt;p&gt;This now closes the loop in the harness:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Agents drive change&lt;/li&gt;
&lt;li&gt;Linters, compilers, and tests protect the quality of the implementation and documentation.&lt;/li&gt;
&lt;li&gt;Reconciliation loops at various levels and scopes (code change, daily, weekly, monthly)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This highlights a fundamental parallel: the core engineering discipline remains identical for human engineers and autonomous agents; only the execution velocity differs.
Improvements introduced for engineers benefit agents even more dramatically.
We must heed what software engineering has demonstrated over the past half-century.&lt;/p&gt;
&lt;p&gt;This entire harness stems from a core conviction: &lt;strong&gt;let the neural net focus on high-level reasoning and intent, while letting deterministic CPU tools handle the mechanical verification.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Compilers (especially with strong type checkers), linters, and test suites execute in milliseconds with absolute certainty.
Burning expensive, probabilistic tokens on verification that a local binary handles in microseconds is wasteful in the extreme.&lt;/p&gt;
&lt;p&gt;This same conviction &lt;em&gt;also&lt;/em&gt; drives my preference for dense languages such as Haskell.
Strict types, purity as a first principle, and a dense syntax help to keep functions small, isolated, and focused.
It allows an engineer to hold a function in &lt;em&gt;their&lt;/em&gt; working memory, reason about its behaviour, and ensure its correctness.
From the outside it is enough to know that the types match.
Afterwards, type signatures alone dictate how to compose functions, rarely requiring subsequent repairs due to unforeseen defects.&lt;/p&gt;
&lt;h2 class=&quot;relative group&quot;&gt;What is next: leveraging the speedup gains from agentic development and corporate fit
&lt;div class=&quot;anchor&quot; id=&quot;what-is-next-leveraging-the-speedup-gains-from-agentic-development-and-corporate-fit&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none&quot;&gt;
&lt;a class=&quot;text-primary-300 dark:text-neutral-700 !no-underline&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#what-is-next-leveraging-the-speedup-gains-from-agentic-development-and-corporate-fit&quot;&gt;#&lt;/a&gt;
&lt;/span&gt;
&lt;/h2&gt;
&lt;p&gt;With deterministic guardrails governing both code quality and institutional memory, a curious dynamic emerges.
The bottleneck shifts upwards from code generation to coordinating concurrent, sensible streams of work.&lt;/p&gt;
&lt;p&gt;Additionally, with the approach of using ADR and Research Questions as outlined in this article, the institutional memory remains siloed in a single repository, whereas architectural principles, designs, and organisational conventions span the entire organisation and therefore multiple projects.
The current approach still lacks a clean mechanism to unlock that higher-level institutional memory to the agent without flooding its context window with (potentially) irrelevant history.&lt;/p&gt;
&lt;p&gt;These two challenges are where an active body of research is currently directed, and what I will focus on next.&lt;sup id=&quot;fnref:10&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:10&quot;&gt;10&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;h2 class=&quot;relative group&quot;&gt;Conclusion
&lt;div class=&quot;anchor&quot; id=&quot;conclusion&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none&quot;&gt;
&lt;a class=&quot;text-primary-300 dark:text-neutral-700 !no-underline&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#conclusion&quot;&gt;#&lt;/a&gt;
&lt;/span&gt;
&lt;/h2&gt;
&lt;p&gt;I like to end with something that has been cycling in my head all along and my AI quoted back to me when reviewing this conclusion:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;software engineering has never been about syntax; it has always been about managing fallible, context-bounded intelligence, whether made of carbon or silicon.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;(Gemini, when repeating back what I told it above.)&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Software &lt;em&gt;Programming&lt;/em&gt; has traditionally centred on syntax, algorithms, and code generation.
In that narrow domain, modern models excel: they produce functional routines, wire up libraries, and spit out syntax with astonishing velocity.
On that level, programming as we know it has become very niche.&lt;/p&gt;
&lt;p&gt;Conversely, Software &lt;em&gt;Engineering&lt;/em&gt; has &lt;em&gt;always&lt;/em&gt; begun where raw programming ends.&lt;sup id=&quot;fnref:11&quot;&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fn:11&quot;&gt;11&lt;/a&gt;&lt;/sup&gt; It encompasses establishing architectural boundaries, aligning technical trade-offs with business priorities, and managing fallible, context-bounded intelligence across time.&lt;/p&gt;
&lt;p&gt;Although agents are tireless programmers, they possess zero business context. They lack the knowledge whether a repository is a throwaway prototype that needs to ship by Friday or a core payment engine where a subtle edge case creates legal liability.
Engineering transcends syntax because it demands commercial &lt;strong&gt;judgement&lt;/strong&gt;: knowing when to accept technical debt, how to balance elegance against commercial deadlines, and which problems actually matter to the business.
Institutional memory (captured in our ADRs and Research Questions) is simply the durable record of those business trade-offs. The agent can generate the syntax, but the engineer must supply the strategic context: the architectural boundaries, the business priorities, and the long-term intent that code alone can never express.&lt;/p&gt;
&lt;p&gt;Ultimately, in the context of software engineering, human engineers and autonomous agents are far more alike than we might care to admit.
Both are fallible, context-bounded intelligences prone to lapses in attention, recency bias, and unstated assumptions.
The hard-won disciplines of decades of software engineering (clear boundaries, explicit contracts, automated invariant testing, and durable records of intent) were always meant to safeguard us from our own limitations.
It turns out nicely that these exact same disciplines are also what allow AI to succeed.
Hopefully this marks the time where we are able to bring these practices in daily use.&lt;/p&gt;
&lt;p&gt;What we need to figure out next is how to leverage the immense speed gains agentic programming gives us while staying sane as engineers ourselves.&lt;/p&gt;
&lt;h3 class=&quot;relative group&quot;&gt;Postscript
&lt;div class=&quot;anchor&quot; id=&quot;postscript&quot;&gt;&lt;/div&gt;
&lt;span class=&quot;absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none&quot;&gt;
&lt;a class=&quot;text-primary-300 dark:text-neutral-700 !no-underline&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#postscript&quot;&gt;#&lt;/a&gt;
&lt;/span&gt;
&lt;/h3&gt;
&lt;p&gt;This blog post is itself an example of that dynamic: making something of higher quality in the same amount of time.
I spent about a day writing this post.
Using LLMs in a targeted way allowed me to test different phrasing and let the model hunt for inconsistencies in my arguments.
All text was hand-written (or rather, typed), then reviewed alongside Gemini to refine logical flow, prune meandering thoughts, and source primary references.
I hope you enjoyed the journey.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn:1&quot;&gt;
&lt;p&gt;M. Sharma et al., &lt;a href=&quot;https://doi.org/10.48550/arXiv.2310.13548&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Towards Understanding Sycophancy in Language Models”&lt;/a&gt;, arXiv:2310.13548, 2023; E. Perez et al., &lt;a href=&quot;https://doi.org/10.48550/arXiv.2212.09251&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Discovering Language Model Behaviors with Model-Written Evaluations”&lt;/a&gt;, arXiv:2212.09251, 2022. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:1&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:2&quot;&gt;
&lt;p&gt;C.-P. Hsieh et al., &lt;a href=&quot;https://doi.org/10.48550/arXiv.2404.06654&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“RULER: What’s the Real Context Size of Your Long-Context Language Models?”&lt;/a&gt;, arXiv:2404.06654, 2024. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:2&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:3&quot;&gt;
&lt;p&gt;C. Sadowski et al., &lt;a href=&quot;https://doi.org/10.1109/ICSE.2015.76&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Tricorder: Building a Program Analysis Ecosystem”&lt;/a&gt;, &lt;em&gt;Proceedings of the 37th International Conference on Software Engineering (ICSE)&lt;/em&gt;, pp. 598–608, 2015. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:3&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:4&quot;&gt;
&lt;p&gt;M.M. Lehman, &lt;a href=&quot;https://doi.org/10.1109/PROC.1980.11805&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Programs, Life Cycles, and Laws of Software Evolution”&lt;/a&gt;, &lt;em&gt;Proceedings of the IEEE&lt;/em&gt;, 68(9), pp. 1060–1076, 1980; D.E. Perry and A.L. Wolf, &lt;a href=&quot;https://doi.org/10.1145/141874.141884&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Foundations for the Study of Software Architecture”&lt;/a&gt;, &lt;em&gt;ACM SIGSOFT Software Engineering Notes&lt;/em&gt;, 17(4), pp. 40–52, 1992; M.W. Godfrey and Q. Tu, &lt;a href=&quot;https://doi.org/10.1109/ICSM.2000.883030&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Evolution in Open Source Software: A Case Study of Linux”&lt;/a&gt;, &lt;em&gt;IEEE International Conference on Software Maintenance (ICSM)&lt;/em&gt;, pp. 131–142, 2000; G. Kroah-Hartman, &lt;a href=&quot;https://www.kernel.org/doc/html/latest/process/development-process.html&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Linux Kernel Development Model”&lt;/a&gt;, &lt;em&gt;The Linux Kernel Documentation&lt;/em&gt;, 2018; D. Spinellis and P. Avgeriou, &lt;a href=&quot;https://doi.org/10.1109/TSE.2019.2892149&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Evolution of the Unix System Architecture: An Exploratory Case Study”&lt;/a&gt;, &lt;em&gt;IEEE Transactions on Software Engineering&lt;/em&gt;, 47(6), pp. 1134–1163, 2021. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:4&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:5&quot;&gt;
&lt;p&gt;R. Hu et al., &lt;a href=&quot;https://doi.org/10.48550/arXiv.2406.03444&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Schrödinger’s Code Repository: Evaluating LLMs on Dynamic Codebases”&lt;/a&gt;, arXiv:2406.03444, 2024; R. Brooks, &lt;a href=&quot;https://doi.org/10.1016/S0020-7373%2883%2980031-5&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Towards a theory of the cognitive processes in computer programming”&lt;/a&gt;, &lt;em&gt;International Journal of Man-Machine Studies&lt;/em&gt;, 18(6), pp. 543–554, 1983. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:5&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:6&quot;&gt;
&lt;p&gt;D. Kocetkov et al., &lt;a href=&quot;https://doi.org/10.48550/arXiv.2211.15533&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“The Stack: 3 TB of Permissively Licensed Source Code”&lt;/a&gt;, &lt;em&gt;Transactions on Machine Learning Research&lt;/em&gt;, 2022; F. Cassano et al., &lt;a href=&quot;https://doi.org/10.1109/TSE.2023.3267446&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“MultiPL-E: A Scalable and Polyglot Approach to Benchmarking Neural Code Generation”&lt;/a&gt;, &lt;em&gt;IEEE Transactions on Software Engineering&lt;/em&gt;, 49(7), pp. 3675–3691, 2023. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:6&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:7&quot;&gt;
&lt;p&gt;M. Zaharia et al., &lt;a href=&quot;https://doi.org/10.1145/3664802&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“The Shift from Models to Compound AI Systems”&lt;/a&gt;, &lt;em&gt;Communications of the ACM&lt;/em&gt;, 67(6), pp. 24–27, 2024; L. Chen, M. Zaharia, and J. Zou, &lt;a href=&quot;https://doi.org/10.48550/arXiv.2305.05176&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“FrugalGPT: How to Use Large Language Models While Reducing Cost and Improving Performance”&lt;/a&gt;, arXiv:2305.05176, 2023. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:7&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:8&quot;&gt;
&lt;p&gt;E.W. Stein and V. Zwass, &lt;a href=&quot;https://doi.org/10.1287/isre.6.2.85&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Actualizing Organizational Memory with Information Systems”&lt;/a&gt;, &lt;em&gt;Information Systems Research&lt;/em&gt;, 6(2), pp. 85–117, 1995; P. Kruchten, H. Obbink, and J. Stafford, &lt;a href=&quot;https://doi.org/10.1109/MS.2006.59&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“The Past, Present, and Future for Software Architecture”&lt;/a&gt;, &lt;em&gt;IEEE Software&lt;/em&gt;, 23(2), pp. 22–30, 2006. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:8&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:9&quot;&gt;
&lt;p&gt;Nelson F. Liu et al., &lt;a href=&quot;https://doi.org/10.1162/tacl_a_00638&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Lost in the Middle: How Language Models Use Long Contexts”&lt;/a&gt;, &lt;em&gt;Transactions of the Association for Computational Linguistics&lt;/em&gt;, 12, pp. 157–173, 2024. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:9&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:10&quot;&gt;
&lt;p&gt;T.R. Sumers et al., &lt;a href=&quot;https://doi.org/10.48550/arXiv.2309.02427&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“Cognitive Architectures for Language Agents”&lt;/a&gt;, &lt;em&gt;Transactions on Machine Learning Research&lt;/em&gt;, 2024; S. Hong et al., &lt;a href=&quot;https://doi.org/10.48550/arXiv.2308.00352&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;“MetaGPT: Meta Programming for A Multi-Agent Collaborative Framework”&lt;/a&gt;, &lt;em&gt;ICLR&lt;/em&gt;, 2024. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:10&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn:11&quot;&gt;
&lt;p&gt;T. Winters, T. Manshreck, and H. Wright, &lt;a href=&quot;https://abseil.io/resources/swe-book&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;Software Engineering at Google: Lessons Learned from Programming Over Time&lt;/em&gt;&lt;/a&gt;, O’Reilly Media, 2020; F.P. Brooks Jr., &lt;a href=&quot;https://doi.org/10.1145/800027.808439&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;&lt;em&gt;The Mythical Man-Month: Essays on Software Engineering&lt;/em&gt;&lt;/a&gt;, Addison-Wesley, 1975. &lt;a class=&quot;footnote-backref&quot; href=&quot;https://alessandrovermeulen.me/atom.xml#fnref:11&quot;&gt;back&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description>
	<pubDate>Tue, 20 Oct 2026 09:00:00 +0000</pubDate>
</item>
<item>
	<title>Chris Reade: PenroseKiteDart User Guide</title>
	<guid isPermaLink="false">http://readerunner.wordpress.com/?p=251</guid>
	<link>https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/</link>
	<description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;(Updated June 2026 for PenroseKiteDart version 1.10)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PenroseKiteDart is a Haskell package with tools to experiment with finite tilings of Penrose’s Kites and Darts. It uses the &lt;a href=&quot;https://diagrams.github.io&quot;&gt;Haskell Diagrams&lt;/a&gt; package for drawing tilings. As well as providing drawing tools, this package introduces tile graphs (&lt;code&gt;Tgraphs&lt;/code&gt;) for describing finite tilings. (I would like to thank Stephen Huggett for suggesting planar graphs as a way to reperesent the tilings).&lt;/p&gt;
&lt;p&gt;This document summarises the design and use of the PenroseKiteDart package.&lt;/p&gt;
&lt;p&gt;PenroseKiteDart package is now available on &lt;a href=&quot;https://hackage.haskell.org&quot;&gt;Hackage&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The source files are available on GitHub at &lt;a href=&quot;https://github.com/chrisreade/PenroseKiteDart&quot;&gt;https://github.com/chrisreade/PenroseKiteDart&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There is a small art gallery of examples created with PenroseKiteDart &lt;a href=&quot;https://github.com/chrisreade/PenroseKiteDart/tree/master/ArtGallery&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Index&lt;/strong&gt;&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/#1&quot;&gt;About Penrose’s Kites and Darts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/#2&quot;&gt;Using the PenroseKiteDart Package&lt;/a&gt; (initial set up).&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/#3&quot;&gt;Overview of Types and Operations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/#4&quot;&gt;Drawing in more detail&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/#5&quot;&gt;Forcing in more detail&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/#6&quot;&gt;Advanced Operations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/#7&quot;&gt;Other Reading&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&quot;1&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;about-penroses-kites-and-darts&quot;&gt;1. About Penroseâ€™s Kites and Darts&lt;/h2&gt;
&lt;h3 id=&quot;the-tiles&quot;&gt;The Tiles&lt;/h3&gt;
&lt;p&gt;In figure 1 we show a dart and a kite. All angles are multiples of &lt;img alt=&quot;36^{\circ}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=36%5E%7B%5Ccirc%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (a tenth of a full turn). If the shorter edges are of length 1, then the longer edges are of length &lt;img alt=&quot;\phi&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cphi&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, where &lt;img alt=&quot;\phi = (1+ \sqrt{5})/ 2&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cphi+%3D+%281%2B+%5Csqrt%7B5%7D%29%2F+2&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is the golden ratio.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 1: The Dart and Kite Tiles&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/geomtiles.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 1: The Dart and Kite Tiles&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;aperiodic-infinite-tilings&quot;&gt;Aperiodic Infinite Tilings&lt;/h3&gt;
&lt;p&gt;What is interesting about these tiles is:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;It is possible to tile the entire plane with kites and darts in an aperiodic way.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Such a tiling is non-periodic and does not contain arbitrarily large periodic regions or patches.&lt;/p&gt;
&lt;p&gt;The possibility of aperiodic tilings with kites and darts was discovered by Sir Roger Penrose in 1974. There are other shapes with this property, including a chiral aperiodic monotile discovered in 2023 by Smith, Myers, Kaplan, Goodman-Strauss. (See the Penrose Tiling &lt;a href=&quot;https://en.wikipedia.org/wiki/Penrose_tiling&quot;&gt;Wikipedia page&lt;/a&gt; for the history of aperiodic tilings)&lt;/p&gt;
&lt;p&gt;This package is entirely concerned with Penrose’s kite and dart tilings also known as P2 tilings.&lt;/p&gt;
&lt;h3 id=&quot;legal-tilings&quot;&gt;Legal Tilings&lt;/h3&gt;
&lt;p&gt;In figure 2 we add a temporary green line marking purely to illustrate a rule for making &lt;em&gt;legal tilings&lt;/em&gt;. The purpose of the rule is to exclude the possibility of periodic tilings.&lt;/p&gt;
&lt;p&gt;If all tiles are marked as shown, then whenever tiles come together at a point, they must all be marked or must all be unmarked at that meeting point. So, for example, each long edge of a kite can be placed legally on only &lt;em&gt;one&lt;/em&gt; of the two long edges of a dart. The kite wing vertex (which is marked) has to go next to the dart tip vertex (which is marked) and cannot go next to the dart wing vertex (which is unmarked) for a legal tiling.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 2: Marked Dart and Kite&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/markedtiles2.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 2: Marked Dart and Kite&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;correct-tilings&quot;&gt;Correct Tilings&lt;/h3&gt;
&lt;p&gt;Unfortunately, having a finite legal tiling is not enough to guarantee you can continue the tiling without getting stuck. Finite legal tilings which can be continued to cover the entire plane are called &lt;em&gt;correct&lt;/em&gt; and the others (which are doomed to get stuck) are called &lt;em&gt;incorrect&lt;/em&gt;. This means that decomposition and forcing (described later) become important tools for constructing correct finite tilings.&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;2&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;using-the-penrosekitedart-package&quot;&gt;2. Using the PenroseKiteDart Package&lt;/h2&gt;
&lt;p&gt;You will need the Haskell Diagrams package (See &lt;a href=&quot;https://diagrams.github.io&quot;&gt;Haskell Diagrams&lt;/a&gt;) as well as this package (PenroseKiteDart). When these are installed, you can produce diagrams with a Main.hs module. This should import a chosen backend for diagrams such as the default (SVG) along with &lt;code&gt;Diagrams.Prelude&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;module&lt;/span&gt; Main &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;main&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
    
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Diagrams.Backend.SVG.CmdLine
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; Diagrams.Prelude&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For Penrose’s Kite and Dart tilings, you also need to import the &lt;code&gt;PKD&lt;/code&gt; module and (optionally) the &lt;code&gt;TgraphExamples&lt;/code&gt; module.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; PKD
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; TgraphExamples&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then to ouput &lt;code&gt;someExample&lt;/code&gt; figure&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    fig&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt;Diagram B
    fig &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; someExample

    main &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; IO ()
    main &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; mainWith fig&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the token &lt;code&gt;B&lt;/code&gt; is used in the diagrams package to represent the chosen backend for output. So a diagram has type &lt;code&gt;Diagram B&lt;/code&gt;. In this case &lt;code&gt;B&lt;/code&gt; is bound to SVG by the import of the SVG backend. When the compiled module is executed it will generate an SVG file. (See &lt;a href=&quot;https://diagrams.github.io&quot;&gt;Haskell Diagrams&lt;/a&gt; for more details on producing diagrams and using alternative backends).&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;3&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;overview-of-types-and-operations&quot;&gt;3. Overview of Types and Operations&lt;/h2&gt;
&lt;h3 id=&quot;half-tiles&quot;&gt;Half-Tiles&lt;/h3&gt;
&lt;p&gt;In order to implement operations on tilings (&lt;code&gt;decompose&lt;/code&gt; in particular), we work with half-tiles. These are illustrated in figure 3 and labelled &lt;code&gt;RD&lt;/code&gt; (right dart), &lt;code&gt;LD&lt;/code&gt; (left dart), &lt;code&gt;LK&lt;/code&gt; (left kite), &lt;code&gt;RK&lt;/code&gt; (right kite). The &lt;em&gt;join&lt;/em&gt; edges where left and right halves come together are shown with dotted lines, leaving one short edge and one long edge on each half-tile (excluding the join edge). We have shown a red dot at the vertex we regard as the origin of each half-tile (the tip of a half-dart and the base of a half-kite).&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 3: Half-Tile pieces showing join edges (dashed) and origin vertices (red dots)&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/newpiecesfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 3: Half-Tile pieces showing join edges (dashed) and origin vertices (red dots)&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The labels are actually data constructors introduced with type operator &lt;code&gt;HalfTile&lt;/code&gt; which has an argument type (&lt;code&gt;rep&lt;/code&gt;) to allow for more than one representation of the half-tiles.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;data&lt;/span&gt; HalfTile rep 
      &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; LD rep &lt;span style=&quot;color: green;&quot;&gt;-- Left Dart&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; RD rep &lt;span style=&quot;color: green;&quot;&gt;-- Right Dart&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; LK rep &lt;span style=&quot;color: green;&quot;&gt;-- Left Kite&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; RK rep &lt;span style=&quot;color: green;&quot;&gt;-- Right Kite&lt;/span&gt;
      &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;deriving&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Show&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Eq&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;tgraphs&quot;&gt;Tgraphs&lt;/h3&gt;
&lt;p&gt;We introduce tile graphs (&lt;code&gt;Tgraph&lt;/code&gt;s) which provide a simple planar graph representation for finite patches of tiles. For &lt;code&gt;Tgraph&lt;/code&gt;s we first specialise &lt;code&gt;HalfTile&lt;/code&gt; with a triple of vertices (positive integers) to make a &lt;code&gt;TileFace&lt;/code&gt; such as &lt;code&gt;RD(1,2,3)&lt;/code&gt;, where the vertices go clockwise round the half-tile triangle starting with the origin.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; TileFace  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; HalfTile &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; Vertex    &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; Int  &lt;span style=&quot;color: green;&quot;&gt;-- must be positive&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The function&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    makeTgraph &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;then constructs a &lt;code&gt;Tgraph&lt;/code&gt; from a &lt;code&gt;TileFace&lt;/code&gt; list after checking the &lt;code&gt;TileFace&lt;/code&gt;s satisfy certain properties (described below). We also have&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    faces &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to retrieve the &lt;code&gt;TileFace&lt;/code&gt; list from a &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;As an example, the &lt;code&gt;fool&lt;/code&gt; (short for &lt;em&gt;fool’s kite&lt;/em&gt; and also called an &lt;em&gt;ace&lt;/em&gt; in the literature) consists of two kites and a dart (= 4 half-kites and 2 half-darts):&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    fool &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph
    fool &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; makeTgraph &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;RD &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; LD &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;   &lt;span style=&quot;color: green;&quot;&gt;-- right and left dart&lt;/span&gt;
                      &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;LK &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; RK &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;   &lt;span style=&quot;color: green;&quot;&gt;-- left and right kite&lt;/span&gt;
                      &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;RK &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; LK &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;   &lt;span style=&quot;color: green;&quot;&gt;-- right and left kite&lt;/span&gt;
                      &lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To produce a diagram, we simply &lt;code&gt;draw&lt;/code&gt; the &lt;code&gt;Tgraph&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    foolFigure &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Diagram B
    foolFigure &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; draw fool&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which will produce the diagram on the left in figure 4.&lt;/p&gt;
&lt;p&gt;Alternatively,&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    foolFigure &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Diagram B
    foolFigure &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; labelled drawj fool&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;will produce the diagram on the right in figure 4 (showing vertex labels and dashed join edges).&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 4: Diagram of fool without labels and join edges (left), and with (right)&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/newfool.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 4: Diagram of &lt;code&gt;fool&lt;/code&gt; without labels and join edges (left), and with (right)&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;When any (non-empty) &lt;code&gt;Tgraph&lt;/code&gt; is drawn, a default orientation and scale are chosen based on the lowest numbered join edge. This is aligned on the positive x-axis with length 1 (for darts) or length &lt;img alt=&quot;\phi&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cphi&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (for kites).&lt;/p&gt;
&lt;h3 id=&quot;tgraph-properties&quot;&gt;Tgraph Properties&lt;/h3&gt;
&lt;p&gt;Tgraphs are actually implemented as&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;newtype&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
                     &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;deriving&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Show&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but the data constructor &lt;code&gt;Tgraph&lt;/code&gt; is not exported to avoid accidentally by-passing checks for the required properties. The properties checked by &lt;code&gt;makeTgraph&lt;/code&gt; ensure the &lt;code&gt;Tgraph&lt;/code&gt; represents a legal tiling as a planar graph with positive vertex numbers, and that the collection of half-tile faces are both connected and have no crossing boundaries (see note below). Finally, there is a check to ensure two or more distinct vertex numbers are not used to represent the same vertex of the graph (a &lt;em&gt;touching vertex&lt;/em&gt; check). An error is raised if there is a problem.&lt;/p&gt;
&lt;p&gt;Note: If the &lt;code&gt;TileFace&lt;/code&gt;s are faces of a planar graph there will also be exterior (untiled) regions, and in graph theory these would also be called faces of the graph. To avoid confusion, we will refer to these only as &lt;em&gt;exterior regions&lt;/em&gt;, and unless otherwise stated, &lt;em&gt;face&lt;/em&gt; will mean a &lt;code&gt;TileFace&lt;/code&gt;. We can then define the boundary of a list of &lt;code&gt;TileFace&lt;/code&gt;s as the edges of the exterior regions. There is a &lt;em&gt;crossing boundary&lt;/em&gt; if the boundary crosses itself at a vertex. We exclude crossing boundaries from &lt;code&gt;Tgraph&lt;/code&gt;s because they prevent us from calculating relative positions of tiles locally and create touching vertex problems.&lt;/p&gt;
&lt;p&gt;For convenience, in addition to &lt;code&gt;makeTgraph&lt;/code&gt;, we also have&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    makeUncheckedTgraph &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph
    checkedTgraph   &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first of these (performing no checks) is useful when you know the required properties hold. The second performs the same checks as &lt;code&gt;makeTgraph&lt;/code&gt; except that it omits the touching vertex check. This could be used, for example, when making a &lt;code&gt;Tgraph&lt;/code&gt; from a sub-collection of &lt;code&gt;TileFace&lt;/code&gt;s of another &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;main-tiling-operations&quot;&gt;Main Tiling Operations&lt;/h3&gt;
&lt;p&gt;There are three key operations on finite tilings, namely&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    decompose &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph
    force     &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph
    compose   &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;decompose&quot;&gt;Decompose&lt;/h4&gt;
&lt;p&gt;Decomposition (also called &lt;em&gt;deflation&lt;/em&gt;) works by splitting each half-tile into either 2 or 3 new (&lt;em&gt;smaller scale&lt;/em&gt;) half-tiles, to produce a new tiling. The fact that this is possible, is used to establish the existence of infinite aperiodic tilings with kites and darts. Since our &lt;code&gt;Tgraph&lt;/code&gt;s have abstracted away from scale, the result of decomposing a &lt;code&gt;Tgraph&lt;/code&gt; is just another &lt;code&gt;Tgraph&lt;/code&gt;. However if we wish to compare before and after with a drawing, the latter should be scaled by a factor &lt;img alt=&quot;1/{\phi} = \phi - 1&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=1%2F%7B%5Cphi%7D+%3D+%5Cphi+-+1&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; times the scale of the former, to reflect the change in scale.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 5: fool (left) and decompose fool (right)&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/foolandfoold.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 5: &lt;code&gt;fool&lt;/code&gt; (left) and &lt;code&gt;decompose fool&lt;/code&gt; (right)&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;We can, of course, iterate &lt;code&gt;decompose&lt;/code&gt; to produce an infinite list of finer and finer decompositions of a &lt;code&gt;Tgraph&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    decompositions &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    decompositions &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; iterate decompose&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;force&quot;&gt;Force&lt;/h4&gt;
&lt;p&gt;Force works by adding any &lt;code&gt;TileFace&lt;/code&gt;s on the boundary edges of a &lt;code&gt;Tgraph&lt;/code&gt; which are &lt;em&gt;forced&lt;/em&gt;. That is, where there is only one legal choice of &lt;code&gt;TileFace&lt;/code&gt; addition consistent with the seven possible vertex types. Such additions are continued until either (i) there are no more forced cases, in which case a final (forced) &lt;code&gt;Tgraph&lt;/code&gt; is returned, or (ii) the process finds the tiling is stuck, in which case an error is raised indicating an incorrect tiling. [In the latter case, the argument to &lt;code&gt;force&lt;/code&gt; must have been an incorrect tiling, because the forced additions cannot produce an incorrect tiling starting from a correct tiling.]&lt;/p&gt;
&lt;p&gt;An example is shown in figure 6. When forced, the &lt;code&gt;Tgraph&lt;/code&gt; on the left produces the result on the right. The original is highlighted in red in the result to show what has been added.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 6: A Tgraph (left) and its forced result (right) with the original shown red&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/fooldandforce.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 6: A Tgraph (left) and its forced result (right) with the original shown red&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h4 id=&quot;compose&quot;&gt;Compose&lt;/h4&gt;
&lt;p&gt;Composition (also called &lt;em&gt;inflation&lt;/em&gt;) is an opposite to &lt;code&gt;decompose&lt;/code&gt; but this has complications for finite tilings, so it is not simply an inverse. (See &lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/&quot;&gt;Graphs,Kites and Darts and Theorems&lt;/a&gt; for more discussion of the problems). Figure 7 shows a &lt;code&gt;Tgraph&lt;/code&gt; (left) with the result of composing (right) where we have also shown (in pale green) the faces of the original that are not included in the composition – the remainder faces.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 7: A Tgraph (left) and its (part) composed result (right) with the remainder faces shown pale green&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/pcomposeexample.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 7: A Tgraph (left) and its (part) composed result (right) with the remainder faces shown pale green&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Under some circumstances composing can fail to produce a &lt;code&gt;Tgraph&lt;/code&gt; because there are crossing boundaries in the resulting &lt;code&gt;TileFaces&lt;/code&gt;. However, we have established that&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;g&lt;/code&gt; is a forced &lt;code&gt;Tgraph&lt;/code&gt;, then &lt;code&gt;compose g&lt;/code&gt; is defined and it is also a forced &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;try-results&quot;&gt;Try Results&lt;/h3&gt;
&lt;p&gt;It is convenient to use types of the form &lt;code&gt;Try a&lt;/code&gt; for results where we know there can be a failure. For example, &lt;code&gt;compose&lt;/code&gt; can fail if the result does not pass the connected and no crossing boundary check, and &lt;code&gt;force&lt;/code&gt; can fail if its argument is an incorrect &lt;code&gt;Tgraph&lt;/code&gt;. In situations when you would like to continue some computation rather than raise an error when there is a failure, use a &lt;em&gt;try&lt;/em&gt; version of a function.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    tryCompose &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try Tgraph
    tryForce   &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try Tgraph&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We define &lt;code&gt;Try&lt;/code&gt; as a synonym for &lt;code&gt;Either ShowS&lt;/code&gt; (which is a monad) in module &lt;code&gt;Tgraph.Try&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;type Try a = Either ShowS a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Note &lt;code&gt;ShowS&lt;/code&gt; is &lt;code&gt;String -&amp;gt; String&lt;/code&gt;). Successful results have the form &lt;code&gt;Right r&lt;/code&gt; (for some correct result &lt;code&gt;r&lt;/code&gt;) and failure results have the form &lt;code&gt;Left (s&amp;lt;&amp;gt;)&lt;/code&gt; (where &lt;code&gt;s&lt;/code&gt; is a &lt;code&gt;String&lt;/code&gt; describing the problem as a failure report).&lt;/p&gt;
&lt;p&gt;The function&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    runTry&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Try a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
    runTry &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; either error id&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;will retrieve a correct result but raise an error for failure cases. This means we can always derive an error raising version from a try version of a function by composing with &lt;code&gt;runTry&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    force &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; runTry . tryForce
    compose &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; runTry . tryCompose&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;elementary-tgraph-and-tileface-operations&quot;&gt;Elementary Tgraph and TileFace Operations&lt;/h3&gt;
&lt;p&gt;The module &lt;code&gt;Tgraph.Prelude&lt;/code&gt; defines elementary operations on &lt;code&gt;Tgraph&lt;/code&gt;s relating vertices, directed edges, and faces. We describe a few of them here.&lt;/p&gt;
&lt;p&gt;When we need to refer to particular vertices of a &lt;code&gt;TileFace&lt;/code&gt; we use&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    originV &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TileFace &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Vertex &lt;span style=&quot;color: green;&quot;&gt;-- the first vertex - red dot in figure 2&lt;/span&gt;
    oppV    &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TileFace &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Vertex &lt;span style=&quot;color: green;&quot;&gt;-- the vertex at the opposite end of the join edge from the origin&lt;/span&gt;
    wingV   &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TileFace &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Vertex &lt;span style=&quot;color: green;&quot;&gt;-- the vertex not on the join edge&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A directed edge is represented as a pair of vertices.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; Dedge &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So &lt;code&gt;(a,b)&lt;/code&gt; is regarded as a directed edge from a to b.&lt;/p&gt;
&lt;p&gt;When we need to refer to particular edges of a &lt;code&gt;TileFace&lt;/code&gt; we use&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    joinE  &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TileFace &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Dedge  &lt;span style=&quot;color: green;&quot;&gt;-- shown dotted in figure 2&lt;/span&gt;
    shortE &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TileFace &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Dedge  &lt;span style=&quot;color: green;&quot;&gt;-- the non-join short edge&lt;/span&gt;
    longE  &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TileFace &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Dedge  &lt;span style=&quot;color: green;&quot;&gt;-- the non-join long edge&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which are all directed clockwise round the &lt;code&gt;TileFace&lt;/code&gt;. In contrast, &lt;code&gt;joinOfTile&lt;/code&gt; is always directed away from the origin vertex, so is not clockwise for right darts or for left kites:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    joinOfTile&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TileFace &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Dedge
    joinOfTile face &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;originV face&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; oppV face&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the special case that a list of directed edges is symmetrically closed [(b,a) is in the list whenever (a,b) is in the list] we can think of this as an edge list rather than just a directed edge list.&lt;/p&gt;
&lt;p&gt;For example,&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    internalEdges &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;produces an edge list, whereas&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    boundary &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;produces single directions. Each directed edge in the resulting boundary will have a &lt;code&gt;TileFace&lt;/code&gt; on the left and an exterior region on the right. The function&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    dedges &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;produces all the directed edges obtained by going clockwise round each &lt;code&gt;TileFace&lt;/code&gt; so not every edge in the list has an inverse in the list.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note 1:&lt;/strong&gt; There is now a class &lt;code&gt;HasFaces&lt;/code&gt; (introduced in version 1.4) which includes instances for both &lt;code&gt;Tgraph&lt;/code&gt; and &lt;code&gt;[TileFace]&lt;/code&gt; and others. This allows some generalisations. For example&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    faces         &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasFaces a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    internalEdges &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasFaces a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    boundary      &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasFaces a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; 
    dedges        &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasFaces a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; 
    nullFaces     &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasFaces a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Bool&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Note 2:&lt;/strong&gt; There is now a class &lt;code&gt;HasGraph&lt;/code&gt; (introduced in version 1.8) which includes instances for &lt;code&gt;Tgraph&lt;/code&gt; as well as other types used in forcing. This allows some other generalisations. For example&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    compose &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph
    decompose &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph
    recoverGraph &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;patches-scaled-and-positioned-tilings&quot;&gt;Patches (Scaled and Positioned Tilings)&lt;/h3&gt;
&lt;p&gt;Behind the scenes, when a &lt;code&gt;Tgraph&lt;/code&gt; is drawn, each &lt;code&gt;TileFace&lt;/code&gt; is converted to a &lt;code&gt;Piece&lt;/code&gt;. A &lt;code&gt;Piece&lt;/code&gt; is another specialisation of &lt;code&gt;HalfTile&lt;/code&gt;. Since version 1.10 this uses two two-dimensional vectors to represent the drawn edges of the half-tile starting from the origin, fixing its scale and orientation. The whole &lt;code&gt;Tgraph&lt;/code&gt; then becomes a list of located &lt;code&gt;Piece&lt;/code&gt;s called a &lt;code&gt;Patch&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; Piece &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; HalfTile &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;V2 Double&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; Patch &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Located Piece&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;Piece&lt;/code&gt; drawing functions can use the &lt;code&gt;drawnEdges&lt;/code&gt; (two vectors) and the &lt;code&gt;joinVector&lt;/code&gt; (which is just the sum of the drawn edge vectors). In particular (in the &lt;code&gt;TileLib&lt;/code&gt; module) we have&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    drawPiece &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Piece &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B
    drawjPiece &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Piece &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B
    fillPieceDK &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Colour Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Colour Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Piece &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where the first draws the drawnEdges (the non-join edges) of a &lt;code&gt;Piece&lt;/code&gt;, the second does the same but adds a faint dashed line for the join edge, and the third takes two colours – one for darts and one for kites, which are used to fill the piece as well as using &lt;code&gt;drawPiece&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Patch&lt;/code&gt; is an instance of class &lt;code&gt;Transformable&lt;/code&gt; so a &lt;code&gt;Patch&lt;/code&gt; can be scaled, rotated, and translated for example.&lt;/p&gt;
&lt;h3 id=&quot;vertex-patches&quot;&gt;Vertex Patches&lt;/h3&gt;
&lt;p&gt;It is useful to have an intermediate form between &lt;code&gt;Tgraph&lt;/code&gt;s and &lt;code&gt;Patch&lt;/code&gt;es, that contains information about both the location of vertices (as 2D points), and the abstract &lt;code&gt;TileFace&lt;/code&gt;s. This allows us to introduce labelled drawing functions (to show the vertex labels) which we then extend to &lt;code&gt;Tgraph&lt;/code&gt;s. We call the intermediate form a &lt;code&gt;VPatch&lt;/code&gt; (short for Vertex Patch).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; VertexLocMap &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; IntMap.IntMap &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Point V2 Double&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;data&lt;/span&gt; VPatch &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; VPatch &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt;vLocs &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; VertexLocMap&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;  vpFaces&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;deriving&lt;/span&gt; Show&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    makeVP &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; VPatch&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;calculates vertex locations using a default orientation and scale.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;VPatch&lt;/code&gt; is made an instance of class &lt;code&gt;Transformable&lt;/code&gt; so a &lt;code&gt;VPatch&lt;/code&gt; can also be scaled, translated, and rotated for example.&lt;/p&gt;
&lt;p&gt;One essential use of this intermediate form is to be able to draw a &lt;code&gt;Tgraph&lt;/code&gt; with labels, transformed but without the labels themselves being transformed. We can simply convert the &lt;code&gt;Tgraph&lt;/code&gt; to a &lt;code&gt;VPatch&lt;/code&gt;, and transform that before drawing with labels.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    labelled draw &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;rotate someAngle &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;makeVP g&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can also align a &lt;code&gt;VPatch&lt;/code&gt; using vertex labels.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    alignXaxis &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; Vertex&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; VPatch &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So if &lt;code&gt;g&lt;/code&gt; is a &lt;code&gt;Tgraph&lt;/code&gt; with vertex labels &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; we can align it on the x-axis with &lt;code&gt;a&lt;/code&gt; at the origin and &lt;code&gt;b&lt;/code&gt; on the positive x-axis (after converting to a &lt;code&gt;VPatch&lt;/code&gt;), instead of accepting the default orientation.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    labelled draw &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;alignXaxis &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;makeVP g&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Another use of &lt;code&gt;VPatch&lt;/code&gt;es is to share the vertex location map when drawing only subsets of the faces (see &lt;em&gt;Overlaid examples&lt;/em&gt; in the next section).&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;4&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;drawing-in-more-detail&quot;&gt;4. Drawing in More Detail&lt;/h2&gt;
&lt;h3 id=&quot;class-drawable&quot;&gt;Class Drawable&lt;/h3&gt;
&lt;p&gt;There is a class &lt;code&gt;Drawable&lt;/code&gt; with instances &lt;code&gt;Tgraph&lt;/code&gt;, &lt;code&gt;VPatch&lt;/code&gt;, &lt;code&gt;Patch&lt;/code&gt;. When the token &lt;code&gt;B&lt;/code&gt; is in scope standing for a fixed backend then we can assume&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    draw   &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Drawable a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B  &lt;span style=&quot;color: green;&quot;&gt;-- draws non-join edges&lt;/span&gt;
    drawj  &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Drawable a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B  &lt;span style=&quot;color: green;&quot;&gt;-- as with draw but also draws dashed join edges&lt;/span&gt;
    fillDK &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Drawable a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Colour Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Colour Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B &lt;span style=&quot;color: green;&quot;&gt;-- fills with colours&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;fillDK clr1 clr2&lt;/code&gt; will fill darts with colour &lt;code&gt;clr1&lt;/code&gt; and kites with colour &lt;code&gt;clr2&lt;/code&gt; as well as drawing non-join edges.&lt;/p&gt;
&lt;p&gt;These are the main drawing tools. However they are actually defined for any suitable backend &lt;code&gt;b&lt;/code&gt; so have more general types.&lt;/p&gt;
&lt;p&gt;(&lt;em&gt;Update Sept 2024&lt;/em&gt;) From version 1.1 onwards of PenroseKiteDart, these are&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    draw &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt;   &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Drawable a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; OKBackend b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
              a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram b
    drawj &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Drawable a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; OKBackend&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
              a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram b
    fillDK &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Drawable a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; OKBackend b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;
              Colour Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Colour Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram b&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where the class &lt;code&gt;OKBackend&lt;/code&gt; is a check to ensure a backend is suitable for drawing 2D tilings with or without labels.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In these notes we will generally use the simpler description of types using &lt;code&gt;B&lt;/code&gt; for a fixed chosen backend for the sake of clarity.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The drawing tools are each defined via the class function &lt;code&gt;drawWith&lt;/code&gt; using &lt;code&gt;Piece&lt;/code&gt; drawing functions.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;class&lt;/span&gt; Drawable a &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
        drawWith &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Piece &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B
    
    draw &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; drawWith drawPiece
    drawj &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; drawWith drawjPiece
    fillDK clr1 clr2 &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; drawWith &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;fillPieceDK clr1 clr2&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To design a new drawing function, you only need to implement a function to draw a &lt;code&gt;Piece&lt;/code&gt;, (let us call it &lt;code&gt;newPieceDraw&lt;/code&gt;)&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    newPieceDraw &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Piece &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This can then be elevated to draw any &lt;code&gt;Drawable&lt;/code&gt; (including &lt;code&gt;Tgraph&lt;/code&gt;s, &lt;code&gt;VPatch&lt;/code&gt;es, and &lt;code&gt;Patch&lt;/code&gt;es) by applying the &lt;code&gt;Drawable&lt;/code&gt; class function &lt;code&gt;drawWith&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    newDraw &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Drawable a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B
    newDraw &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; drawWith newPieceDraw&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;class-drawablelabelled&quot;&gt;Class DrawableLabelled&lt;/h3&gt;
&lt;p&gt;Class &lt;code&gt;DrawableLabelled&lt;/code&gt; is defined with instances &lt;code&gt;Tgraph&lt;/code&gt; and &lt;code&gt;VPatch&lt;/code&gt;, but &lt;code&gt;Patch&lt;/code&gt; is not an instance (because this does not retain vertex label information).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;class&lt;/span&gt; DrawableLabelled a &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
        labelColourSize &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Colour Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Measure Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Patch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So &lt;code&gt;labelColourSize c m&lt;/code&gt; modifies a &lt;code&gt;Patch&lt;/code&gt; drawing function to add labels (of colour &lt;code&gt;c&lt;/code&gt; and size measure &lt;code&gt;m&lt;/code&gt;). &lt;code&gt;Measure&lt;/code&gt; is defined in Diagrams.Prelude with pre-defined measures &lt;code&gt;tiny&lt;/code&gt;, &lt;code&gt;verySmall&lt;/code&gt;, &lt;code&gt;small&lt;/code&gt;, &lt;code&gt;normal&lt;/code&gt;, &lt;code&gt;large&lt;/code&gt;, &lt;code&gt;veryLarge&lt;/code&gt;, &lt;code&gt;huge&lt;/code&gt;. For most of our diagrams of &lt;code&gt;Tgraph&lt;/code&gt;s, we use red labels and we also find &lt;code&gt;small&lt;/code&gt; is a good default size choice, so we define&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    labelSize &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; DrawableLabelled a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Measure Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Patch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B
    labelSize &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; labelColourSize red

    labelled &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; DrawableLabelled a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Patch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B
    labelled &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; labelSize small&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then &lt;code&gt;labelled draw&lt;/code&gt;, &lt;code&gt;labelled drawj&lt;/code&gt;, &lt;code&gt;labelled (fillDK clr1 clr2)&lt;/code&gt; can all be used on both &lt;code&gt;Tgraph&lt;/code&gt;s and &lt;code&gt;VPatch&lt;/code&gt;es as well as (for example) &lt;code&gt;labelSize tiny draw&lt;/code&gt;, or &lt;code&gt;labelCoulourSize blue normal drawj&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;further-drawing-functions&quot;&gt;Further drawing functions&lt;/h3&gt;
&lt;p&gt;There are a few extra drawing functions built on top of the above ones. The function &lt;code&gt;smart&lt;/code&gt; is a modifier to add dashed join edges only when they occur on the boundary of a &lt;code&gt;Tgraph&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    smart &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So &lt;code&gt;smart vpdraw g&lt;/code&gt; will draw dashed join edges on the boundary of &lt;code&gt;g&lt;/code&gt; before applying the drawing function &lt;code&gt;vpdraw&lt;/code&gt; to the &lt;code&gt;VPatch&lt;/code&gt; for &lt;code&gt;g&lt;/code&gt;. For example the following all draw dashed join edges only on the boundary for a &lt;code&gt;Tgraph g&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    smart draw g
    smart &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;labelled draw&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; g
    smart &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;labelSize normal draw&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; g&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When using labels, the function &lt;code&gt;rotating&lt;/code&gt; allows a &lt;code&gt;Tgraph&lt;/code&gt; to be drawn rotated without rotating the labels.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    rotating &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Angle Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; b
    rotating angle vpdraw &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; vpdraw . rotate angle . makeVP&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So for example,&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    rotating &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;90&lt;/span&gt;@@deg&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;labelled draw&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; g&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;makes sense for a &lt;code&gt;Tgraph g&lt;/code&gt;. Of course if there are no labels we can simply use&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    rotate &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;90&lt;/span&gt;@@deg&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;draw g&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similarly &lt;code&gt;aligning&lt;/code&gt; allows a &lt;code&gt;Tgraph&lt;/code&gt; to be aligned on the X-axis using a pair of vertex numbers before drawing.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    aligning &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; b
    aligning &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; vpdraw &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; vpdraw . alignXaxis &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; . makeVP&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, for example, if &lt;code&gt;Tgraph g&lt;/code&gt; has vertices &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, both&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    aligning &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; draw g
    aligning &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;labelled draw&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; g&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;make sense. Note that the following two examples are &lt;strong&gt;wrong&lt;/strong&gt;. Even though they type check, they re-orient &lt;code&gt;g&lt;/code&gt; without repositioning the boundary joins.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    smart &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;labelled draw . rotate angle&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; g      &lt;span style=&quot;color: green;&quot;&gt;-- WRONG&lt;/span&gt;
    smart &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;labelled draw . alignXaxis &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; g  &lt;span style=&quot;color: green;&quot;&gt;-- WRONG&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead use&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    smartRotating angle &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;labelled draw&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; g
    smartAligning &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;labelled draw&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; g&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    smartRotating &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt;  Angle Double &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B
    smartAligning  &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Vertex&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;are defined using&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    smartOn &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, &lt;code&gt;smartOn g vpdraw vp&lt;/code&gt; uses the given &lt;code&gt;vp&lt;/code&gt; for drawing boundary joins and drawing faces of &lt;code&gt;g&lt;/code&gt; (with &lt;code&gt;vpdraw&lt;/code&gt;) rather than converting &lt;code&gt;g&lt;/code&gt; to a new &lt;code&gt;VPatch&lt;/code&gt;. This assumes &lt;code&gt;vp&lt;/code&gt; has locations for vertices in &lt;code&gt;g&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;overlaid-examples-location-map-sharing&quot;&gt;Overlaid examples (location map sharing)&lt;/h3&gt;
&lt;p&gt;The function&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    drawForce &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;will (smart) draw a &lt;code&gt;Tgraph g&lt;/code&gt; in red overlaid (using &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;) on the result of &lt;code&gt;force g&lt;/code&gt; as in figure 6. Similarly&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    drawPCompose  &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;applied to a &lt;code&gt;Tgraph g&lt;/code&gt; will draw the result of a partial composition of &lt;code&gt;g&lt;/code&gt; as in figure 7. That is a drawing of &lt;code&gt;compose g&lt;/code&gt; but overlaid with a drawing of the remainder faces of &lt;code&gt;g&lt;/code&gt; shown in pale green.&lt;/p&gt;
&lt;p&gt;Both these functions make use of sharing a vertex location map to get correct alignments of overlaid diagrams. In the case of &lt;code&gt;drawForce g&lt;/code&gt;, we know that a &lt;code&gt;VPatch&lt;/code&gt; for &lt;code&gt;force g&lt;/code&gt; will contain all the vertex locations for &lt;code&gt;g&lt;/code&gt; since force only adds to a &lt;code&gt;Tgraph&lt;/code&gt; (when it succeeds). So when constructing the diagram for &lt;code&gt;g&lt;/code&gt; we can use the &lt;code&gt;VPatch&lt;/code&gt; created for &lt;code&gt;force g&lt;/code&gt; instead of starting afresh. Similarly for &lt;code&gt;drawPCompose g&lt;/code&gt; the &lt;code&gt;VPatch&lt;/code&gt; for &lt;code&gt;g&lt;/code&gt; contains locations for all the vertices of &lt;code&gt;compose g&lt;/code&gt; so &lt;code&gt;compose g&lt;/code&gt; is drawn using the &lt;code&gt;VPatch&lt;/code&gt; for &lt;code&gt;g&lt;/code&gt; instead of starting afresh.&lt;/p&gt;
&lt;p&gt;The location map sharing is done with&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    subFaces &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasFaces a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; 
                a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; VPatch&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;so that &lt;code&gt;subFaces fcs vp&lt;/code&gt; is a &lt;code&gt;VPatch&lt;/code&gt; with the same vertex locations as &lt;code&gt;vp&lt;/code&gt;, but replacing the faces of &lt;code&gt;vp&lt;/code&gt; with &lt;code&gt;fcs&lt;/code&gt;. [Of course, this can go wrong if the new faces have vertices not in the domain of the vertex location map so this needs to be used with care. Any errors would only be discovered when a diagram is created.]&lt;/p&gt;
&lt;p&gt;For cases where labels are only going to be drawn for certain faces, we need a version of &lt;code&gt;subFaces&lt;/code&gt; which also gets rid of vertex locations that are not relevant to the faces. For this situation we have&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    restrictTo&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasFaces a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; 
                 a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; VPatch&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which filters out un-needed vertex locations from the vertex location map. Unlike &lt;code&gt;subFaces&lt;/code&gt;, &lt;code&gt;restrictTo&lt;/code&gt; checks for missing vertex locations, so &lt;code&gt;restrictTo fcs vp&lt;/code&gt; raises an error if a vertex in &lt;code&gt;fcs&lt;/code&gt; is missing from the keys of the vertex location map of &lt;code&gt;vp&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;5&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;forcing-in-more-detail&quot;&gt;5. Forcing in More Detail&lt;/h2&gt;
&lt;h3 id=&quot;the-force-rules&quot;&gt;The force rules&lt;/h3&gt;
&lt;p&gt;The rules used by our force algorithm are local and derived from the fact that there are seven possible (internal) vertex types as depicted in figure 8.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 8: Seven vertex types&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/verttypesfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 8: Seven vertex types&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Our rules are shown in figure 9 (omitting mirror symmetric versions). In each case the &lt;code&gt;TileFace&lt;/code&gt; shown yellow needs to be added in the presence of the other &lt;code&gt;TileFace&lt;/code&gt;s shown with the red edge on the boundary.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 9: Rules for forcing&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2026/10/forcerulesnew.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 9: Rules for forcing&lt;/figcaption&gt;&lt;/figure&gt;
&lt;h3 id=&quot;main-forcing-operations&quot;&gt;Main Forcing Operations&lt;/h3&gt;
&lt;p&gt;To make forcing efficient we convert a &lt;code&gt;Tgraph&lt;/code&gt; to a &lt;code&gt;BoundaryState&lt;/code&gt; to keep track of boundary information of the &lt;code&gt;Tgraph&lt;/code&gt;, and then calculate a &lt;code&gt;ForceState&lt;/code&gt; which combines the &lt;code&gt;BoundaryState&lt;/code&gt; with a record of awaiting boundary edge updates (an update map), and an UpdateGenerator. Then each face addition is carried out on a &lt;code&gt;ForceState&lt;/code&gt;, converting back when all the face additions are complete. It makes sense to apply &lt;code&gt;force&lt;/code&gt; (and related functions) to a &lt;code&gt;Tgraph&lt;/code&gt;, a &lt;code&gt;BoundaryState&lt;/code&gt;, or a &lt;code&gt;ForceState&lt;/code&gt;, so we define a class &lt;code&gt;Forcible&lt;/code&gt; with instances &lt;code&gt;Tgraph&lt;/code&gt;, &lt;code&gt;BoundaryState&lt;/code&gt;, and &lt;code&gt;ForceState&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This allows us to define&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    force &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
    tryForce &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first will raise an error if a stuck tiling is encountered. The second uses a &lt;code&gt;Try&lt;/code&gt; result which produces a &lt;code&gt;Left report&lt;/code&gt; for failures and a &lt;code&gt;Right a&lt;/code&gt; for successful result &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;There are several other operations related to forcing including&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    stepForce &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
    tryStepForce  &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Int &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try a

    addHalfDart&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; addHalfKite &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Dedge &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
    tryAddHalfDart&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; tryAddHalfKite &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Dedge &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first two force (up to) a given number of steps (=face additions) and the other four add a half dart/kite on a given boundary edge.&lt;/p&gt;
&lt;h3 id=&quot;update-generators&quot;&gt;Update Generators&lt;/h3&gt;
&lt;p&gt;An update generator is used to calculate which boundary edges can have a certain update. There is an update generator for each force rule, but also a combined (all update) generator. The force operations mentioned above all use the default all update generator (&lt;code&gt;defaultAllUGen&lt;/code&gt;) but there are more general (&lt;em&gt;with&lt;/em&gt;) versions that can be passed an update generator of choice. For example&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    forceWith &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; UpdateGenerator &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
    tryForceWith &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; UpdateGenerator &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can also define&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    wholeTiles &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
    wholeTiles &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; forceWith wholeTileUpdates&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;wholeTileUpdates&lt;/code&gt; is an update generator that just finds boundary join edges to complete whole tiles.&lt;/p&gt;
&lt;p&gt;In fact &lt;code&gt;UpdateGenerator&lt;/code&gt;s are functions that take a &lt;code&gt;BoundaryState&lt;/code&gt; and a focus (list of boundary directed edges) to produce an update map. Each &lt;code&gt;Update&lt;/code&gt; is calculated as either a &lt;code&gt;SafeUpdate&lt;/code&gt; (where two of the new face edges are on the existing boundary and no new vertex is needed) or an &lt;code&gt;UnsafeUpdate&lt;/code&gt; (where only one edge of the new face is on the boundary and a new vertex needs to be created for a new face).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; UpdateGenerator &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; BoundaryState &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try UpdateMap
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; UpdateMap &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; Map.Map Dedge Update
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;data&lt;/span&gt; Update &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; SafeUpdate TileFace 
                &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; UnsafeUpdate &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Vertex &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; TileFace&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Completing (executing) an &lt;code&gt;UnsafeUpdate&lt;/code&gt; requires a touching vertex check to ensure that the new vertex does not clash with an existing boundary vertex. Using an existing (touching) vertex would create a crossing boundary so such an update has to be blocked.&lt;/p&gt;
&lt;h3 id=&quot;forcible-class-operations&quot;&gt;Forcible Class Operations&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;Forcible&lt;/code&gt; class operations are higher order and designed to allow for easy additions of further generic operations. They take care of conversions between &lt;code&gt;Tgraph&lt;/code&gt;s, &lt;code&gt;BoundaryState&lt;/code&gt;s and &lt;code&gt;ForceState&lt;/code&gt;s. The first two are designed to create functions that return the same Forcible type as the input.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;class&lt;/span&gt; Forcible a &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
      tryFSOp &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;ForceState &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try ForceState&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try a
      tryChangeBoundary &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;BoundaryState &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try BoundaryChange&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try a
      tryInitFS &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try ForceState&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example, given any &lt;code&gt;f:: ForceState -&amp;gt; Try ForceState&lt;/code&gt; , then &lt;code&gt;f&lt;/code&gt; can be generalised to work on any &lt;code&gt;Forcible&lt;/code&gt; using &lt;code&gt;tryFSOp f&lt;/code&gt;. This is used to define both &lt;code&gt;tryForce&lt;/code&gt; and &lt;code&gt;tryStepForce&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Similarly given any &lt;code&gt;f:: BoundaryState -&amp;gt; Try BoundaryChange&lt;/code&gt; , then &lt;code&gt;f&lt;/code&gt; can be generalised to work on any &lt;code&gt;Forcible&lt;/code&gt; using &lt;code&gt;tryChangeBoundary f&lt;/code&gt;. This is used to define &lt;code&gt;tryAddHalfDart&lt;/code&gt; and &lt;code&gt;tryAddHalfKite&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that the type &lt;code&gt;BoundaryChange&lt;/code&gt; contains a resulting &lt;code&gt;BoundaryState&lt;/code&gt;, the single &lt;code&gt;TileFace&lt;/code&gt; that has been added, a list of edges removed from the boundary (of the &lt;code&gt;BoundaryState&lt;/code&gt; prior to the face addition), and a list of the (3 or 4) boundary edges affected around the change that require checking or re-checking for updates.&lt;/p&gt;
&lt;p&gt;The class function &lt;code&gt;tryInitFS&lt;/code&gt; will create an initial &lt;code&gt;ForceState&lt;/code&gt; for any &lt;code&gt;Forcible&lt;/code&gt;. If the &lt;code&gt;Forcible&lt;/code&gt; is already a &lt;code&gt;ForceState&lt;/code&gt; it will do nothing. Otherwise it will calculate updates for the whole boundary using &lt;code&gt;defaultAllUGen&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The update generator is assumed to be &lt;code&gt;defaultAllUGen&lt;/code&gt; but this can be changed using&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    tryFSOpWith &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; UpdateGenerator &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;ForceState &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try ForceState&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;so, for example, we defined&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    tryForceWith ugen &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; tryFSOpWith ugen tryForce&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;efficient-chains-of-forcing-operations.&quot;&gt;Efficient chains of forcing operations.&lt;/h3&gt;
&lt;p&gt;Note that &lt;code&gt;(force . force)&lt;/code&gt; does the same as &lt;code&gt;force&lt;/code&gt;, but we might want to chain other &lt;code&gt;force&lt;/code&gt; related steps in a calculation.&lt;/p&gt;
&lt;p&gt;For example, consider the following combination which, after decomposing a &lt;code&gt;Tgraph&lt;/code&gt;, forces, then adds a half dart on a given boundary edge (&lt;code&gt;d&lt;/code&gt;) and then forces again.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    combo &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Dedge &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph
    combo d &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; force . addHalfDart d . force . decompose&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since &lt;code&gt;decompose&lt;/code&gt; produces a &lt;code&gt;Tgraph&lt;/code&gt;, the instances of &lt;code&gt;force&lt;/code&gt; and &lt;code&gt;addHalfDart d&lt;/code&gt; will have type &lt;code&gt;Tgraph -&amp;gt; Tgraph&lt;/code&gt; so each of these operations, will begin and end with conversions between &lt;code&gt;Tgraph&lt;/code&gt; and &lt;code&gt;ForceState&lt;/code&gt;. We would do better to avoid these wasted intermediate conversions working only with &lt;code&gt;ForceState&lt;/code&gt;s and keeping only those necessary conversions at the beginning and end of the whole sequence.&lt;/p&gt;
&lt;p&gt;This can be done using &lt;code&gt;tryFSOp&lt;/code&gt;. To see this, let us first re-express the forcing sequence using the &lt;code&gt;Try&lt;/code&gt; monad, so&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    force . addHalfDart d . force&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    tryForce &amp;lt;=&amp;lt; tryAddHalfDart d &amp;lt;=&amp;lt; tryForce&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that (&lt;code&gt;&amp;lt;=&amp;lt;&lt;/code&gt;) is the Kliesli arrow which replaces composition for Monads (defined in Control.Monad). (We could also have expressed this right to left sequence with a left to right version &lt;code&gt;tryForce &amp;gt;=&amp;gt; tryAddHalfDart d &amp;gt;=&amp;gt; tryForce&lt;/code&gt;). The definition of &lt;code&gt;combo&lt;/code&gt; becomes&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    combo &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Dedge &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph
    combo d &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; runTry . &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;tryForce &amp;lt;=&amp;lt; tryAddHalfDart d &amp;lt;=&amp;lt; tryForce&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; . decompose&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This has no performance improvement, but now we can pass the sequence to &lt;code&gt;tryFSOp&lt;/code&gt; to remove the unnecessary conversions between steps.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    combo &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Dedge &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Tgraph
    combo d &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; runTry . tryFSOp &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;tryForce &amp;lt;=&amp;lt; tryAddHalfDart d &amp;lt;=&amp;lt; tryForce&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; . decompose&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The sequence actually has type &lt;code&gt;Forcible a =&amp;gt; a -&amp;gt; Try a&lt;/code&gt; but when passed to &lt;code&gt;tryFSOp&lt;/code&gt; it specialises to type &lt;code&gt;ForceState -&amp;gt; Try ForseState&lt;/code&gt;. This ensures the sequence works on a &lt;code&gt;ForceState&lt;/code&gt; and any conversions are confined to the beginning and end of the sequence, avoiding unnecessary intermediate conversions.&lt;/p&gt;
&lt;h3 id=&quot;a-limitation-of-forcing&quot;&gt;A limitation of forcing&lt;/h3&gt;
&lt;p&gt;To avoid creating touching vertices (or crossing boundaries) a &lt;code&gt;BoundaryState&lt;/code&gt; keeps track of locations of boundary vertices. At around 35,000 face additions in a single &lt;code&gt;force&lt;/code&gt; operation the calculated positions of boundary vertices can become too inaccurate to prevent touching vertex problems. In such cases it is better to use&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    recalibratingForce &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a
    tryRecalibratingForce &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These work by recalculating all vertex positions at 20,000 step intervals to get more accurate boundary vertex positions. For example, 6 decompositions of the &lt;code&gt;kingGraph&lt;/code&gt; has 2,906 faces. Applying &lt;code&gt;force&lt;/code&gt; to this should result in 53,574 faces but will go wrong before it reaches that. This can be fixed by calculating either&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    recalibratingForce &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;decompositions kingGraph !!&lt;span class=&quot;hs-num&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or using an extra &lt;code&gt;force&lt;/code&gt; before the decompositions&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    force &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;decompositions &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;force kingGraph&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; !!&lt;span class=&quot;hs-num&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the latter case, the final &lt;code&gt;force&lt;/code&gt; only needs to add 17,864 faces to the 35,710 produced by &lt;code&gt;decompositions (force kingGraph) !!6&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;6&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;advanced-operations&quot;&gt;6. Advanced Operations&lt;/h2&gt;
&lt;h3 id=&quot;guided-comparison-of-tgraphs&quot;&gt;Guided comparison of &lt;code&gt;Tgraph&lt;/code&gt;s&lt;/h3&gt;
&lt;p&gt;Asking if two &lt;code&gt;Tgraph&lt;/code&gt;s are equivalent (the same apart from choice of vertex numbers) is a an np-hard problem. However, we do have an efficient &lt;em&gt;guided&lt;/em&gt; way of comparing &lt;code&gt;Tgraph&lt;/code&gt;s. In the module &lt;code&gt;Tgraph.Rellabelling&lt;/code&gt; we have&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    sameGraph &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Bool&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The expression &lt;code&gt;sameGraph (g1,d1) (g2,d2)&lt;/code&gt; asks if &lt;code&gt;g2&lt;/code&gt; can be relabelled to match &lt;code&gt;g1&lt;/code&gt; assuming that the directed edge &lt;code&gt;d2&lt;/code&gt; in &lt;code&gt;g2&lt;/code&gt; is identified with &lt;code&gt;d1&lt;/code&gt; in &lt;code&gt;g1&lt;/code&gt;. Hence the comparison is guided by the assumption that &lt;code&gt;d2&lt;/code&gt; corresponds to &lt;code&gt;d1&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It is implemented using&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    tryRelabelToMatch &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try Tgraph&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;tryRelabelToMatch (g1,d1) (g2,d2)&lt;/code&gt; will either fail with a &lt;code&gt;Left report&lt;/code&gt; if a mismatch is found when relabelling &lt;code&gt;g2&lt;/code&gt; to match &lt;code&gt;g1&lt;/code&gt; or will succeed with &lt;code&gt;Right g3&lt;/code&gt; where &lt;code&gt;g3&lt;/code&gt; is a relabelled version of &lt;code&gt;g2&lt;/code&gt;. The successful result &lt;code&gt;g3&lt;/code&gt; will match &lt;code&gt;g1&lt;/code&gt; in a maximal tile-connected collection of faces containing the face with edge &lt;code&gt;d1&lt;/code&gt; and have vertices disjoint from those of &lt;code&gt;g1&lt;/code&gt; elsewhere. The comparison tries to grow a suitable relabelling by comparing faces one at a time starting from the face with edge &lt;code&gt;d1&lt;/code&gt; in &lt;code&gt;g1&lt;/code&gt; and the face with edge &lt;code&gt;d2&lt;/code&gt; in &lt;code&gt;g2&lt;/code&gt;. (This relies on the fact that &lt;code&gt;Tgraph&lt;/code&gt;s are connected with no crossing boundaries, and hence tile-connected.)&lt;/p&gt;
&lt;p&gt;The above function is also used to implement&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    tryFullUnion&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try Tgraph&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which tries to find the union of two &lt;code&gt;Tgraph&lt;/code&gt;s guided by a directed edge identification. However, there is an extra complexity arising from the fact that &lt;code&gt;Tgraph&lt;/code&gt;s might &lt;em&gt;overlap&lt;/em&gt; in more than one tile-connected region. After calculating one overlapping region, the full union uses some geometry (calculating vertex locations) to detect further overlaps.&lt;/p&gt;
&lt;p&gt;Finally we have&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    commonFaces&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Tgraph&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;Dedge&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which will find common regions of overlapping faces of two &lt;code&gt;Tgraph&lt;/code&gt;s guided by a directed edge identification. The resulting common faces will be a sub-collection of faces from the first &lt;code&gt;Tgraph&lt;/code&gt;. These are returned as a list as they may not be a connected collection of faces and therefore not necessarily a &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;empires-and-superforce&quot;&gt;Empires and SuperForce&lt;/h3&gt;
&lt;p&gt;In &lt;a href=&quot;https://readerunner.wordpress.com/2023/04/26/graphs-kites-and-darts-empires-and-superforce/&quot;&gt;Empires and SuperForce&lt;/a&gt; we discussed forced boundary coverings which were used to implement both a &lt;code&gt;superForce&lt;/code&gt; operation&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    superForce&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Forced a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and operations to calculate empires.&lt;/p&gt;
&lt;p&gt;We will not repeat the descriptions here other than to note that&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    forcedBoundaryECovering&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Forced Tgraph&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;finds boundary edge coverings after forcing a &lt;code&gt;Tgraph&lt;/code&gt;. That is, &lt;code&gt;forcedBoundaryECovering g&lt;/code&gt; will first force &lt;code&gt;g&lt;/code&gt;, then (if it succeeds) finds a collection of (forced) extensions to &lt;code&gt;force g&lt;/code&gt; such that&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;each extension has the whole boundary of &lt;code&gt;force g&lt;/code&gt; as internal edges.&lt;/li&gt;
&lt;li&gt;each possible addition to a boundary edge of &lt;code&gt;force g&lt;/code&gt; (kite or dart) has been included in the collection.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;(&lt;em&gt;possible&lt;/em&gt; here means – not leading to a stuck &lt;code&gt;Tgraph&lt;/code&gt; when forced.) There is also&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    forcedBoundaryVCovering&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Forced Tgraph&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which does the same except that the extensions have all boundary vertices internal rather than just the boundary edges. In both cases the result is a list of explicitly forced Tgraphs (discussed next).&lt;/p&gt;
&lt;h3 id=&quot;combinations-and-explicitly-forced&quot;&gt;Combinations and Explicitly Forced&lt;/h3&gt;
&lt;p&gt;We introduced a new type &lt;code&gt;Forced&lt;/code&gt; (in v 1.3) to enable a forcible to be explictily labelled as being forced. For example&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    forceF    &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Forced a 
    tryForceF &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forcible a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Try &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Forced a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
    forgetF   &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Forced a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This allows us to restrict certain functions which expect a forced argument by making this explicit.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    composeF &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; HasGraph a &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; Forced a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Forced Tgraph&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The definition makes use of theorems established in &lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/&quot;&gt;Graphs,Kites and Darts and Theorems&lt;/a&gt; that composing a forced &lt;code&gt;Tgraph&lt;/code&gt; does not require a check (for connectedness and no crossing boundaries) and the result is also forced. This can then be used to define efficient combinations such as&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    compForce&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Forcible a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; HasGraph a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Forced Tgraph      &lt;span style=&quot;color: green;&quot;&gt;-- compose after forcing&lt;/span&gt;
    compForce &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; composeF . forceF

    allCompForce&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Forcible a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; HasGraph a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;Forced Tgraph&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;-- iterated (compose after force) while not emptyTgraph&lt;/span&gt;
    maxCompForce&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;Forcible a&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; HasGraph a&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Forced Tgraph   &lt;span style=&quot;color: green;&quot;&gt;-- last item in allCompForce (or emptyTgraph)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that &lt;code&gt;BoundaryState&lt;/code&gt;, &lt;code&gt;ForceState&lt;/code&gt; as well as &lt;code&gt;Tgraph&lt;/code&gt; and &lt;code&gt;Forced&lt;/code&gt; versions of these are all instances of class &lt;code&gt;HasGraph&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;tracked-tgraphs&quot;&gt;Tracked Tgraphs&lt;/h3&gt;
&lt;p&gt;The type&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;data&lt;/span&gt; TrackedTgraph &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; TrackedTgraph
       &lt;span style=&quot;color: red;&quot;&gt;{&lt;/span&gt; tgraph  &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph
       &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; tracked &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;TileFace&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; 
       &lt;span style=&quot;color: red;&quot;&gt;}&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;deriving&lt;/span&gt; Show&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;has proven useful in experimentation as well as in producing artwork with darts and kites. The idea is to keep a record of sub-collections of faces of a &lt;code&gt;Tgraph&lt;/code&gt; when doing both force operations and decompositions. A list of the sub-collections forms the tracked list associated with the &lt;code&gt;Tgraph&lt;/code&gt;. We make &lt;code&gt;TrackedTgraph&lt;/code&gt; an instance of class &lt;code&gt;Forcible&lt;/code&gt; by having force operations only affect the &lt;code&gt;Tgraph&lt;/code&gt; and not the tracked list. The significant idea is the implementation of&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    decomposeTracked &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TrackedTgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; TrackedTgraph&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Decomposition of a &lt;code&gt;Tgraph&lt;/code&gt; involves introducing a new vertex for each long edge and each kite join. These are then used to construct the decomposed faces. For &lt;code&gt;decomposeTracked&lt;/code&gt; we do the same for the &lt;code&gt;Tgraph&lt;/code&gt;, but when it comes to the tracked collections, we decompose them re-using the same new vertex numbers calculated for the edges in the &lt;code&gt;Tgraph&lt;/code&gt;. This keeps a consistent numbering between the &lt;code&gt;Tgraph&lt;/code&gt; and tracked faces, so each item in the tracked list remains a sub-collection of faces in the &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The function&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    drawTrackedTgraph &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;VPatch &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; TrackedTgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;is used to draw a &lt;code&gt;TrackedTgraph&lt;/code&gt;. It uses a list of functions to draw &lt;code&gt;VPatch&lt;/code&gt;es. The first drawing function is applied to a &lt;code&gt;VPatch&lt;/code&gt; for any untracked faces. Subsequent functions are applied to &lt;code&gt;VPatch&lt;/code&gt;es for the tracked list in order. Each diagram is beneath later ones in the list, with the diagram for the untracked faces at the bottom. The &lt;code&gt;VPatch&lt;/code&gt;es used are all restrictions of a single &lt;code&gt;VPatch&lt;/code&gt; for the &lt;code&gt;Tgraph&lt;/code&gt;, so will be consistent in vertex locations. When labels are used, there is also a &lt;code&gt;drawTrackedTgraphRotating&lt;/code&gt; and &lt;code&gt;drawTrackedTgraphAligning&lt;/code&gt; for rotating or aligning the &lt;code&gt;VPatch&lt;/code&gt; prior to applying the drawing functions.&lt;/p&gt;
&lt;p&gt;Note that the result of calculating empires (see &lt;a href=&quot;https://readerunner.wordpress.com/2023/04/26/graphs-kites-and-darts-empires-and-superforce/&quot;&gt;Empires and SuperForce&lt;/a&gt; ) is represented as a &lt;code&gt;TrackedTgraph&lt;/code&gt;. The result is actually the common faces of a forced boundary covering, but a particular element of the covering (the first one) is chosen as the background &lt;code&gt;Tgraph&lt;/code&gt; with the common faces as a tracked sub-collection of faces. Hence we have&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    empire1&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; empire2 &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; Tgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; TrackedTgraph
    
    drawEmpire &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; TrackedTgraph &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; Diagram B&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Figure 10 was also created using &lt;code&gt;TrackedTgraph&lt;/code&gt;s.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Figure 10: Using a TrackedTgraph for drawing&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/04/cover.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 10: Using a TrackedTgraph for drawing&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;&lt;a name=&quot;7&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;other-reading&quot;&gt;7. Other Reading&lt;/h2&gt;
&lt;p&gt;Previous related blogs are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2021/03/20/diagrams-for-penrose-tiles/&quot;&gt;Diagrams for Penrose Tiles&lt;/a&gt; – the first blog introduced drawing &lt;code&gt;Piece&lt;/code&gt;s and &lt;code&gt;Patch&lt;/code&gt;es (without using Tgraphs) and provided a version of decomposing for Patches (&lt;code&gt;decompPatch&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2022/01/06/graphs-kites-and-darts/&quot;&gt;Graphs, Kites and Darts&lt;/a&gt; intoduced Tgraphs. This gave more details of implementation and results of early explorations. (The class &lt;code&gt;Forcible&lt;/code&gt; was introduced subsequently).&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2023/04/26/graphs-kites-and-darts-empires-and-superforce/&quot;&gt;Empires and SuperForce&lt;/a&gt; – these new operations were based on observing properties of boundaries of forced Tgraphs.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/&quot;&gt;Graphs,Kites and Darts and Theorems&lt;/a&gt; established some important results relating &lt;code&gt;force&lt;/code&gt;, &lt;code&gt;compose&lt;/code&gt;, &lt;code&gt;decompose&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</description>
	<pubDate>Fri, 16 Oct 2026 17:15:51 +0000</pubDate>
</item>
<item>
	<title>Magnus Therning: QuickCheck: shrinking like tuples, writing like applicative</title>
	<guid isPermaLink="true">https://magnus.therning.org/2026-10-13-quickcheck:-shrinking-like-tuples,-writing-like-applicative.html</guid>
	<link>https://magnus.therning.org/2026-10-13-quickcheck:-shrinking-like-tuples,-writing-like-applicative.html</link>
	<description>&lt;p&gt;
The &lt;a href=&quot;https://hackage.haskell.org/package/QuickCheck&quot;&gt;QuickCheck&lt;/a&gt; documentation for &lt;a href=&quot;https://hackage-content.haskell.org/package/QuickCheck-2.19.0.0/docs/Test-QuickCheck.html#g:5&quot;&gt;Arbitrary&lt;/a&gt; says that
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
There is no generic arbitrary implementation included because we don't know how
to make a high-quality one.
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Though in practice I've found that very often it suffices to write one that uses
the fact that &lt;code&gt;Gen&lt;/code&gt; is an &lt;code&gt;Applicative&lt;/code&gt;. For instance, this type
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-keyword&quot;&gt;data&lt;/span&gt; Foo = Foo
    { tata :: &lt;span class=&quot;org-type&quot;&gt;Int&lt;/span&gt;
    , titi :: &lt;span class=&quot;org-type&quot;&gt;Double&lt;/span&gt;
    }
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
would get an implementation that looks like this
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-keyword&quot;&gt;instance&lt;/span&gt; Arbitrary &lt;span class=&quot;org-type&quot;&gt;Foo&lt;/span&gt; &lt;span class=&quot;org-keyword&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;org-function-name&quot;&gt;arbitrary&lt;/span&gt; = Foo &lt;span class=&quot;org-operator&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; arbitrary &lt;span class=&quot;org-operator&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; arbitrary
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
When it comes to writing the function for shrinking &lt;code&gt;Foo&lt;/code&gt; it &lt;i&gt;is&lt;/i&gt; possible to
rely on a generic function and the documentation says that
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;
The default implementation returns the empty list, so will not try to shrink the
value. If your data type has no special invariants, you can enable shrinking by
defining &lt;code&gt;shrink = genericShrink&lt;/code&gt;, but by customising the behaviour of shrink
you can often get simpler counterexamples.
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;
Based on some manual testing it seems that &lt;code&gt;genericShrink&lt;/code&gt; generates something
like this for the type &lt;code&gt;Foo&lt;/code&gt; above
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-keyword&quot;&gt;instance&lt;/span&gt; Arbitrary &lt;span class=&quot;org-type&quot;&gt;Foo&lt;/span&gt; &lt;span class=&quot;org-keyword&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;org-function-name&quot;&gt;shrink&lt;/span&gt; (Foo tata_ titi_) = Foo &lt;span class=&quot;org-operator&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; shrink tata_ &lt;span class=&quot;org-operator&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; shrink titi_
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
which means that if &lt;code&gt;shrink tata_&lt;/code&gt; generates a list of &lt;i&gt;m&lt;/i&gt; elements and &lt;code&gt;shrink
titi_&lt;/code&gt; generates a list of &lt;i&gt;n&lt;/i&gt; elements, then &lt;code&gt;shrink foo&lt;/code&gt; will generate a list
of &lt;i&gt;mÃ—n&lt;/i&gt; elements. It's clear that the size can become problematic with more
fields. It also means that it's very easy to get an empty list – if &lt;code&gt;shrink v&lt;/code&gt;
results in an empty list, then &lt;code&gt;shrink foo&lt;/code&gt; is also empty. Oh, and &lt;code&gt;shrink
Nothing&lt;/code&gt; is &lt;code&gt;[]&lt;/code&gt;!
&lt;/p&gt;

&lt;p&gt;
A colleague pointed out that tuples have a rather efficient implementation of
&lt;code&gt;shrink&lt;/code&gt; that grows linearly with the number of elements and doesn't reduce to
the empty list as easily. I could use it for &lt;code&gt;Foo&lt;/code&gt; like this
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-keyword&quot;&gt;instance&lt;/span&gt; Arbitrary &lt;span class=&quot;org-type&quot;&gt;Foo&lt;/span&gt; &lt;span class=&quot;org-keyword&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;org-function-name&quot;&gt;shrink&lt;/span&gt; (Foo tata_ titi_) = uncurry Foo &lt;span class=&quot;org-operator&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; shrink (tata_, titi_)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
However, that doesn't scale that well to more fields. Up to a point I can use
list comprehensions, but it gets a bit difficult to read, I find. On top of
that, the largest tuple with an &lt;code&gt;Arbitrary&lt;/code&gt; instance has 10 elements, and then
I'll have to either define more or use inner tuples.
&lt;/p&gt;

&lt;p&gt;
Wouldn't it be nice to write &lt;code&gt;shrink&lt;/code&gt; in something that looks &lt;code&gt;Applicative&lt;/code&gt;, but
behaves like tuples?
&lt;/p&gt;

&lt;p&gt;
Here's one approach to this.
&lt;/p&gt;

&lt;p&gt;
First a type to hold information about what's being shrunk, or in this case &lt;i&gt;waned&lt;/i&gt;
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-keyword&quot;&gt;data&lt;/span&gt; Wane a = Wane &lt;span class=&quot;org-type&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;[a]&lt;/span&gt;
    &lt;span class=&quot;org-keyword&quot;&gt;deriving&lt;/span&gt; (Generic, Show)

&lt;span class=&quot;org-keyword&quot;&gt;instance&lt;/span&gt; Functor &lt;span class=&quot;org-type&quot;&gt;Wane&lt;/span&gt; &lt;span class=&quot;org-keyword&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;org-function-name&quot;&gt;fmap&lt;/span&gt; f (Wane a as) = Wane (f a) (f &lt;span class=&quot;org-operator&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; as)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Then a couple of functions to bring values in and out, &lt;code&gt;unWane&lt;/code&gt; to get the list
of values out and &lt;code&gt;wane&lt;/code&gt; to wrap a value in &lt;code&gt;Wane&lt;/code&gt;
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-function-name&quot;&gt;unWane&lt;/span&gt; :: &lt;span class=&quot;org-type&quot;&gt;Wane a&lt;/span&gt; &lt;span class=&quot;org-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;[a]&lt;/span&gt;
&lt;span class=&quot;org-function-name&quot;&gt;unWane&lt;/span&gt; (Wane _ as) = as

&lt;span class=&quot;org-function-name&quot;&gt;wane&lt;/span&gt; :: (Arbitrary &lt;span class=&quot;org-type&quot;&gt;a&lt;/span&gt;) =&amp;gt; &lt;span class=&quot;org-type&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;org-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;Wane a&lt;/span&gt;
&lt;span class=&quot;org-function-name&quot;&gt;wane&lt;/span&gt; a = Wane a (shrink a)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Finally a way to combine two &lt;code&gt;Wane&lt;/code&gt; values is needed, and here I was inspired by
the implementation of &lt;code&gt;Arbitrary&lt;/code&gt; for &lt;code&gt;(a,b)&lt;/code&gt;
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-keyword&quot;&gt;infixl&lt;/span&gt; &lt;span class=&quot;org-number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;org-operator&quot;&gt;\*/&lt;/span&gt;

(&lt;span class=&quot;org-operator&quot;&gt;\*/&lt;/span&gt;) ::  &lt;span class=&quot;org-type&quot;&gt;Wane&lt;/span&gt; (&lt;span class=&quot;org-type&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;org-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;b&lt;/span&gt;) &lt;span class=&quot;org-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;Wane a&lt;/span&gt; &lt;span class=&quot;org-operator&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;org-type&quot;&gt;Wane b&lt;/span&gt;
(&lt;span class=&quot;org-operator&quot;&gt;\*/&lt;/span&gt;) (Wane f fs) (Wane a as) =  Wane (f a) ([f a' | a' &amp;lt;- as] &lt;span class=&quot;org-operator&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; [f' a| f' &amp;lt;- fs])
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
It's now possible to write &lt;code&gt;shrink&lt;/code&gt; for &lt;code&gt;Foo&lt;/code&gt; like this
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-haskell&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-keyword&quot;&gt;instance&lt;/span&gt; Arbitrary &lt;span class=&quot;org-type&quot;&gt;Foo&lt;/span&gt; &lt;span class=&quot;org-keyword&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;org-function-name&quot;&gt;shrink&lt;/span&gt; (Foo tata_ titi_) = unWane &lt;span class=&quot;org-operator&quot;&gt;$&lt;/span&gt; Foo &lt;span class=&quot;org-operator&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; wane tata_ &lt;span class=&quot;org-operator&quot;&gt;\*/&lt;/span&gt; wane titi_
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
That scales to more fields just as nicely as applicative style, and the result
matches what the variant using tuples produces (same elements, but the order is
different).
&lt;/p&gt;
&lt;div class=&quot;taglist&quot;&gt;&lt;a href=&quot;https://magnus.therning.org/tags.html&quot;&gt;&lt;span class=&quot;tag-label&quot;&gt;Tags&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;tag-separator&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;taglist__tags&quot;&gt;&lt;a class=&quot;tag&quot; href=&quot;https://magnus.therning.org/tag-haskell.html&quot;&gt;haskell&lt;/a&gt; &lt;a class=&quot;tag&quot; href=&quot;https://magnus.therning.org/tag-quickcheck.html&quot;&gt;quickcheck&lt;/a&gt; &lt;/span&gt;&lt;/div&gt;</description>
	<pubDate>Tue, 13 Oct 2026 21:21:00 +0000</pubDate>
</item>
<item>
	<title>Magnus Therning: Emacs salmagundi, 2026-10-11</title>
	<guid isPermaLink="true">https://magnus.therning.org/2026-10-13-emacs-salmagundi,-2026-10-11.html</guid>
	<link>https://magnus.therning.org/2026-10-13-emacs-salmagundi,-2026-10-11.html</link>
	<description>&lt;div class=&quot;outline-2&quot; id=&quot;outline-container-org7b28134&quot;&gt;
&lt;h2 id=&quot;org7b28134&quot;&gt;Combining lists of compilation commands&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-org7b28134&quot;&gt;
&lt;p&gt;
To my delight I found that &lt;a href=&quot;https://github.com/ReanGD/emacs-multi-compile&quot;&gt;emacs-multi-compile&lt;/a&gt; will combine the of all matches
compilation commands!
&lt;/p&gt;

&lt;p&gt;
I added this to my setup
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-emacs-lisp&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;add-to-list 'multi-compile-alist
               '&lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;(&lt;/span&gt;mes/is-in-git-project&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;)&lt;/span&gt; . &lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-string&quot;&gt;&quot;git clean -n&quot;&lt;/span&gt; &lt;span class=&quot;org-string&quot;&gt;&quot;git clean -ndx&quot;&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;(&lt;/span&gt;project-root &lt;span class=&quot;org-rainbow-delimiters-depth-6&quot;&gt;(&lt;/span&gt;project-current&lt;span class=&quot;org-rainbow-delimiters-depth-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;
                                            &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-string&quot;&gt;&quot;git clean -f&quot;&lt;/span&gt; &lt;span class=&quot;org-string&quot;&gt;&quot;git clean -fdx&quot;&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;(&lt;/span&gt;project-root &lt;span class=&quot;org-rainbow-delimiters-depth-6&quot;&gt;(&lt;/span&gt;project-current&lt;span class=&quot;org-rainbow-delimiters-depth-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
and those commands are now shown together with the ones I defined for Haskell
project (see my &lt;a href=&quot;https://magnus.therning.org/2026-09-06-emacs-salmagundi,-2026-09-06.html&quot;&gt;previous post&lt;/a&gt;) when both match.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;outline-2&quot; id=&quot;outline-container-org534bdaa&quot;&gt;
&lt;h2 id=&quot;org534bdaa&quot;&gt;Recognising a git project&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-org534bdaa&quot;&gt;
&lt;p&gt;
To add the git commands to &lt;code&gt;multi-compile-list&lt;/code&gt; I needed a function to determine
if the current buffer is in a git working tree. I failed to find something
fitting so I ended up adding this little function.
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-emacs-lisp&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;org-function-name&quot;&gt;mes/is-in-git-project&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;org-doc&quot;&gt;&quot;Check if we're currently in a git project.&quot;&lt;/span&gt;
  &lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;when-let*&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;proj-dir &lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;(&lt;/span&gt;project-root &lt;span class=&quot;org-rainbow-delimiters-depth-6&quot;&gt;(&lt;/span&gt;project-current&lt;span class=&quot;org-rainbow-delimiters-depth-6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;(&lt;/span&gt;vc-git-root proj-dir&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;outline-2&quot; id=&quot;outline-container-org4e46a61&quot;&gt;
&lt;h2 id=&quot;org4e46a61&quot;&gt;Taming compilation mode buffers&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-org4e46a61&quot;&gt;
&lt;p&gt;
I'm using &lt;code&gt;display-buffer-alist&lt;/code&gt; to control how Emacs places buffers. For
compilation mode I had the following actions.
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-emacs-lisp&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;display-buffer-reuse-mode-window display-buffer-in-side-window&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;mode compilation-mode&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;window-height . 0.25&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;side . bottom&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;post-command-select-window . t&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
That last command made switching buffers with &lt;code&gt;consult-buffer&lt;/code&gt; a bit irritating
as previewing would interrupt the buffer switching by selecting the compilation
window. However, I really want the compilation window selected after a
compilation. Or rather, as I realised after reflecting a bit, I want the
compilation window selected if there are errors.
&lt;/p&gt;

&lt;p&gt;
Compilation mode has a way of running functions when a compilation finishes so I
defined two functions and added them to &lt;code&gt;compilation-finish-functions&lt;/code&gt;.
&lt;/p&gt;

&lt;div class=&quot;org-src-container&quot;&gt;
&lt;pre class=&quot;src src-emacs-lisp&quot;&gt;&lt;code&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;org-function-name&quot;&gt;mes/close-successful-compilation&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;(&lt;/span&gt;buf msg&lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;org-doc&quot;&gt;&quot;Close compilation buffer after success.&quot;&lt;/span&gt;
  &lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;buffer-live-p buf&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;
             &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;with-current-buffer&lt;/span&gt; buf &lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;(&lt;/span&gt;eq major-mode 'compilation-mode&lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;
             &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;string-match &lt;span class=&quot;org-string&quot;&gt;&quot;finished&quot;&lt;/span&gt; msg&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;(&lt;/span&gt;run-with-timer 3 nil
                    &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;()&lt;/span&gt;
                      &lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;(&lt;/span&gt;kill-buffer buf&lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;defun&lt;/span&gt; &lt;span class=&quot;org-function-name&quot;&gt;mes/select-failed-compilation&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;(&lt;/span&gt;buf msg&lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;org-doc&quot;&gt;&quot;Select compilation buffer after failed compilation.&quot;&lt;/span&gt;
  &lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;buffer-live-p buf&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;
             &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;org-keyword&quot;&gt;with-current-buffer&lt;/span&gt; buf &lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;(&lt;/span&gt;eq major-mode 'compilation-mode&lt;span class=&quot;org-rainbow-delimiters-depth-5&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;
             &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;string-match &lt;span class=&quot;org-string&quot;&gt;&quot;abnormally&quot;&lt;/span&gt; msg&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;(&lt;/span&gt;select-window &lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;(&lt;/span&gt;get-buffer-window buf&lt;span class=&quot;org-rainbow-delimiters-depth-4&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-3&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-2&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;add-to-list 'compilation-finish-functions #'mes/close-successful-compilation&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;(&lt;/span&gt;add-to-list 'compilation-finish-functions #'mes/select-failed-compilation&lt;span class=&quot;org-rainbow-delimiters-depth-1&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
With that a successful compilation is closed automatically after 3s and on a
failed compilation the compilation buffer is selection.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;taglist&quot;&gt;&lt;a href=&quot;https://magnus.therning.org/tags.html&quot;&gt;&lt;span class=&quot;tag-label&quot;&gt;Tags&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;tag-separator&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;taglist__tags&quot;&gt;&lt;a class=&quot;tag&quot; href=&quot;https://magnus.therning.org/tag-emacs.html&quot;&gt;emacs&lt;/a&gt; &lt;/span&gt;&lt;/div&gt;</description>
	<pubDate>Tue, 13 Oct 2026 19:46:00 +0000</pubDate>
</item>
<item>
	<title>Chris Reade: Graphs, Kites and Darts â€“ and Theorems</title>
	<guid isPermaLink="false">http://readerunner.wordpress.com/?p=212</guid>
	<link>https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/</link>
	<description>&lt;p&gt;(This is a revised version Oct 2026 with corrections for lemma 1 and proof of lemma 3).&lt;/p&gt;
&lt;p&gt;We continue our exploration of properties of Penrose’s aperiodic tilings with kites and darts using &lt;a href=&quot;https://www.haskell.org&quot;&gt;Haskell&lt;/a&gt; and &lt;a href=&quot;https://diagrams.github.io&quot;&gt;Haskell Diagrams&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this blog we discuss some interesting properties we have discovered concerning the &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, and &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; operations along with some proofs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Index&lt;/strong&gt;&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/#1&quot;&gt;Quick Recap&lt;/a&gt; (including operations &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; on &lt;code&gt;Tgraph&lt;/code&gt;s)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/#2&quot;&gt;Composition Problems and a Compose Force Theorem&lt;/a&gt; (composition is not a simple inverse to decomposition)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/#3&quot;&gt;Perfect Composition Theorem&lt;/a&gt; (establishing relationships between &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/#4&quot;&gt;Multiple Compositions&lt;/a&gt; (extending the Compose Force theorem for multiple compositions)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/#5&quot;&gt;Proof of the Compose Force Theorem&lt;/a&gt; (showing &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is total on forced &lt;code&gt;Tgraph&lt;/code&gt;s)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;a name=&quot;1&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;quick-recap&quot;&gt;1. Quick Recap&lt;/h2&gt;
&lt;p&gt;Haskell diagrams allowed us to render finite patches of tiles easily as discussed in &lt;a href=&quot;https://readerunner.wordpress.com/2021/09/13/diagrams-for-penrose-tiles/&quot;&gt;Diagrams for Penrose tiles&lt;/a&gt;. Following a suggestion of Stephen Huggett, we found that the description and manipulation of such tilings is greatly enhanced by using &lt;em&gt;planar graphs&lt;/em&gt;. In &lt;a href=&quot;https://readerunner.wordpress.com/2022/01/06/graphs-kites-and-darts/&quot;&gt;Graphs, Kites and Darts&lt;/a&gt; we introduced a specialised planar graph representation for finite tilings of kites and darts which we called &lt;code&gt;Tgraph&lt;/code&gt;s (tile graphs). These enabled us to implement operations that use neighbouring tile information and in particular operations &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, and &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;For ease of reference, we reproduce the half-tiles we are working with here.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 1: Half-tile faces&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/halftiles.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 1: Half-tile faces&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Figure 1 shows the right-dart (RD), left-dart (LD), left-kite (LK) and right-kite (RK) half-tiles. Each has a join edge (shown dotted) and a short edge and a long edge. The origin vertex is shown red in each case. The vertex at the opposite end of the join edge from the origin we call the &lt;em&gt;opp&lt;/em&gt; vertex, and the remaining vertex we call the &lt;em&gt;wing&lt;/em&gt; vertex.&lt;/p&gt;
&lt;p&gt;If the short edges have unit length then the long edges have length &lt;img alt=&quot;\phi&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cphi&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (the golden ratio) and all angles are multiples of &lt;img alt=&quot;36^{\circ}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=36%5E%7B%5Ccirc%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (a tenth turn) with kite halves having two 2s and a 1, and dart halves having a 3 and two 1s. This geometry of the tiles is abstracted away from at the graph representation level but used when checking validity of tile additions and by the drawing functions.&lt;/p&gt;
&lt;p&gt;There are rules for how the tiles can be put together to make a &lt;em&gt;legal&lt;/em&gt; tiling (see e.g. &lt;a href=&quot;https://readerunner.wordpress.com/2021/09/13/diagrams-for-penrose-tiles/&quot;&gt;Diagrams for Penrose tiles&lt;/a&gt;). We defined a &lt;code&gt;Tgraph&lt;/code&gt; (in &lt;a href=&quot;https://readerunner.wordpress.com/2022/01/06/graphs-kites-and-darts/&quot;&gt;Graphs, Kites and Darts&lt;/a&gt;) as a list of such half-tiles which are constrained to form a legal tiling but must also be connected with no crossing boundaries (see below).&lt;/p&gt;
&lt;p&gt;As a simple example consider &lt;code&gt;kingGraph&lt;/code&gt; (2 kites and 3 darts round a king vertex). We represent each half-tile as a &lt;code&gt;TileFace&lt;/code&gt; with three vertex numbers, then apply &lt;code&gt;makeTgraph&lt;/code&gt; to the list of ten &lt;code&gt;Tileface&lt;/code&gt;s. The function &lt;code&gt;makeTgraph :: [TileFace] -&amp;gt; Tgraph&lt;/code&gt; performs the necessary checks to ensure the result is a valid &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span&gt;kingGraph&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Tgraph&lt;/span&gt;
&lt;span&gt;kingGraph&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;makeTgraph&lt;/span&gt; 
  &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;LD&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;RD&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;LD&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;RD&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;LD&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;11&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
  &lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;RD&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;LK&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;RK&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;RK&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;LK&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;
  &lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To view the &lt;code&gt;Tgraph&lt;/code&gt; we simply form a diagram (in this case 2 diagrams horizontally separated by 1 unit)&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;  &lt;span&gt;hsep&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;labelled&lt;/span&gt; &lt;span&gt;drawj&lt;/span&gt; &lt;span&gt;kingGraph&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;draw&lt;/span&gt; &lt;span&gt;kingGraph&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and the result is shown in figure 2 with labels and dashed join edges (left) and without labels and join edges (right).&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 2: kingGraph with labels and dashed join edges (left) and without (right).&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/kinggraphfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 2: &lt;code&gt;kingGraph&lt;/code&gt; with labels and dashed join edges (left) and without (right).&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The boundary of the &lt;code&gt;Tgraph&lt;/code&gt; consists of the edges of half-tiles which are not shared with another half-tile, so they go round untiled/external regions. The &lt;em&gt;no crossing boundary&lt;/em&gt; constraint (equivalently, &lt;em&gt;locally tile-connected&lt;/em&gt;) means that a boundary vertex has exactly two incident boundary edges and therefore has a single external angle in the tiling. This ensures we can always locally determine the relative angles of tiles at a vertex. We say a collection of half-tiles is a &lt;em&gt;valid&lt;/em&gt; &lt;code&gt;Tgraph&lt;/code&gt; if it constitutes a legal tiling but also satisfies the connectedness and no crossing boundaries constraints.&lt;/p&gt;
&lt;p&gt;Our key operations on &lt;code&gt;Tgraph&lt;/code&gt;s are &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, and &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; which are illustrated in figure 3.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 3: decompose, force, and compose&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/cdfillustrate.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 3: decompose, force, and compose&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Figure 3 shows the &lt;code&gt;kingGraph&lt;/code&gt; with its decomposition above it (left), the result of forcing the &lt;code&gt;kingGraph&lt;/code&gt; (right) and the composition of the forced &lt;code&gt;kingGraph&lt;/code&gt; (bottom right).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Decompose&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;An important property of Penrose dart and kite tilings is that it is possible to divide the half-tile faces of a tiling into smaller half-tile faces, to form a new (smaller scale) tiling.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 4: Decomposition of (left) half-tiles&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/decomphalftiles.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 4: Decomposition of (left) half-tiles&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Figure 4 illustrates the decomposition of a left-dart (top row) and a left-kite (bottom row). With our &lt;code&gt;Tgraph&lt;/code&gt; representation we simply introduce new vertices for dart and kite long edges and kite join edges and then form the new faces using these. This does not involve any geometry, because that is taken care of by drawing operations.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Force&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Figure 5 illustrates the rules used by our &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; operation (we omit a mirror-reflected version of each rule).&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 5: Force rules&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2026/10/forcerulesnew.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 5: Force rules&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;In each case the yellow half-tile is added in the presence of the other half-tiles shown where the red edge is on the boundary. The yellow half-tile is forced because, by the legal tiling rules and the seven possible vertex types, there is no choice for adding a different half-tile on the red edge.&lt;/p&gt;
&lt;p&gt;We call a &lt;code&gt;Tgraph&lt;/code&gt; &lt;em&gt;correct&lt;/em&gt; if it represents a tiling which can be continued infinitely to cover the whole plane without getting stuck, and &lt;em&gt;incorrect&lt;/em&gt; otherwise. Forcing involves adding half-tiles by the illustrated rules round the boundary until either no more rules apply (in which case the result is a forced &lt;code&gt;Tgraph&lt;/code&gt;) or a stuck tiling is encountered (in which case an incorrect &lt;code&gt;Tgraph&lt;/code&gt; error is raised). Hence &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a partial function but total on correct &lt;code&gt;Tgraph&lt;/code&gt;s.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Compose&lt;/strong&gt;: This is discussed in the next section.&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;2&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;composition-problems-and-a-theorem&quot;&gt;2. Composition Problems and a Theorem&lt;/h2&gt;
&lt;h3 id=&quot;compose-choices&quot;&gt;Compose Choices&lt;/h3&gt;
&lt;p&gt;For an infinite tiling, composition is a simple inverse to decomposition. However, for a finite tiling with boundary, composition is not so straight forward. Firstly, we may need to leave half-tiles out of a composition because the necessary parts of a composed half-tile are missing. For example, a half-dart with a boundary short edge or a whole kite with both short edges on the boundary must necessarily be excluded from a composition. Secondly, on the boundary, there can sometimes be a problem of choosing whether a half-dart should compose to become a half-dart or a half-kite. This choice in composing only arises when there is a half-dart with its wing on the boundary but insufficient local information to determine whether it should be part of a larger half-dart or a larger half-kite.&lt;/p&gt;
&lt;p&gt;In the literature (see for example &lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/#MG&quot;&gt;1&lt;/a&gt; and &lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/#BG&quot;&gt;2&lt;/a&gt;) there is an often repeated method for composing (also called &lt;em&gt;inflating&lt;/em&gt;). This method always make the kite choice when there is a choice. Whilst this is a sound method for an unbounded tiling (where there will be no choice), we show that this is an &lt;em&gt;unsound&lt;/em&gt; method for finite tilings as follows.&lt;/p&gt;
&lt;p&gt;Clearly composing should preserve correctness. However, figure 6 (left) shows a correct &lt;code&gt;Tgraph&lt;/code&gt; which is a forced queen, but the kite-favouring composition of the forced queen produces the incorrect &lt;code&gt;Tgraph&lt;/code&gt; shown in figure 6 (centre). Applying our &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; function to this reveals a stuck tiling and reports an incorrect &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 6: An erroneous and a safe composition&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/counterk.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 6: An erroneous and a safe composition&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Our algorithm (discussed in &lt;a href=&quot;https://readerunner.wordpress.com/2022/01/06/graphs-kites-and-darts/&quot;&gt;Graphs, Kites and Darts&lt;/a&gt;) detects dart wings on the boundary where there is a choice and classifies them as &lt;em&gt;unknowns&lt;/em&gt;. Our composition refrains from making a choice by not composing a half dart with an unknown wing vertex. The rightmost &lt;code&gt;Tgraph&lt;/code&gt; in figure 6 shows the result of our composition of the forced queen with the half-tile faces left out of the composition (the remainder faces) shown in green. This avoidance of making a choice (when there is a choice) guarantees our composition preserves correctness.&lt;/p&gt;
&lt;h3 id=&quot;compose-is-a-partial-function&quot;&gt;Compose is a Partial Function&lt;/h3&gt;
&lt;p&gt;A different composition problem can arise when we consider &lt;code&gt;Tgraph&lt;/code&gt;s that are not decompositions of &lt;code&gt;Tgraph&lt;/code&gt;s. In general, &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a partial function on &lt;code&gt;Tgraph&lt;/code&gt;s.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 7: Composition may fail to produce a Tgraph&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/badlybrokendartfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 7: Composition may fail to produce a Tgraph&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Figure 7 shows a &lt;code&gt;Tgraph&lt;/code&gt; (left) with its sucessful composition (centre) and the half-tile faces that would result from a second composition (right) which do not form a valid &lt;code&gt;Tgraph&lt;/code&gt; because of a crossing boundary (at vertex 6). Thus composition of a &lt;code&gt;Tgraph&lt;/code&gt; may fail to produce a &lt;code&gt;Tgraph&lt;/code&gt; when the resulting faces are disconnected or have a crossing boundary.&lt;/p&gt;
&lt;p&gt;However, we claim that &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a total function on forced &lt;code&gt;Tgraph&lt;/code&gt;s.&lt;/p&gt;
&lt;h3 id=&quot;compose-force-theorem&quot;&gt;Compose Force Theorem&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Theorem&lt;/strong&gt;: &lt;em&gt;Composition of a forced Tgraph produces a valid Tgraph.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We postpone the proof (outline) for this theorem to section &lt;a href=&quot;https://readerunner.wordpress.com/2023/09/12/graphs-kites-and-darts-and-theorems/#5&quot;&gt;5&lt;/a&gt;. Meanwhile we use the result to establish relationships between &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, and &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; in the next section.&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;3&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;perfect-composition-theorem&quot;&gt;3. Perfect Composition Theorem&lt;/h2&gt;
&lt;p&gt;In &lt;a href=&quot;https://readerunner.wordpress.com/2022/01/06/graphs-kites-and-darts/&quot;&gt;Graphs, Kites and Darts&lt;/a&gt; we produced a diagram showing relationships between multiple decompositions of a dart and the forced versions of these &lt;code&gt;Tgraph&lt;/code&gt;s. We reproduce this here along with a similar diagram for multiple decompositions of a kite.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 8: Commuting Diagrams&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/bigpic.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 8: Commuting Diagrams&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;In figure 8 we show separate (apparently) commuting diagrams for the dart and for the kite. The bottom rows show the decompositions, the middle rows show the result of forcing the decompositions, and the top rows illustrate how the compositions of the forced &lt;code&gt;Tgraph&lt;/code&gt;s work by showing both the composed faces (black edges) and the remainder faces (green edges) which are removed in the composition. The diagrams are examples of some commutativity relationships concerning &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; which we will prove.&lt;/p&gt;
&lt;p&gt;It should be noted that these diagrams break down if we consider only half-tiles as the starting points (bottom right of each diagram). The decomposition of a half-tile does not recompose to its original, but produces an empty composition. So we do not even have &lt;img alt=&quot;g = (\small\texttt{compose} \cdot \small\texttt{decompose}) \  g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g+%3D+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; in these cases. Forcing the decomposition also results in an empty composition. Clearly there is something special about the depicted cases and it is not merely that they are wholetile complete because the decompositions are not wholetile complete. [&lt;em&gt;Wholetile complete&lt;/em&gt; means there are no join edges on the boundary, so every half-tile has its other half.]&lt;/p&gt;
&lt;p&gt;Below we have captured the properties that are sufficient for the diagrams to commute as in figure 8. In the proofs we use a partial ordering on &lt;code&gt;Tgraph&lt;/code&gt;s (modulo vertex relabelling) which we define next.&lt;/p&gt;
&lt;h3 id=&quot;partial-ordering-of-tgraphs&quot;&gt;Partial ordering of Tgraphs&lt;/h3&gt;
&lt;p&gt;If &lt;img alt=&quot;g_0&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_0&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and &lt;img alt=&quot;g_1&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_1&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; are both valid &lt;code&gt;Tgraph&lt;/code&gt;s and &lt;img alt=&quot;g_0&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_0&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; consists of a subset of the (half-tile) faces of &lt;img alt=&quot;g_1&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_1&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle g_0 \subseteq g_1 &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+g_0+%5Csubseteq+g_1+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;which gives us a partial order on &lt;code&gt;Tgraph&lt;/code&gt;s. Often, though, &lt;img alt=&quot;g_0&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_0&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is only isomorphic to a subset of the faces of &lt;img alt=&quot;g_1&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_1&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, requiring a vertex relabelling to become a subset. In that case we write&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle g_0 \sqsubseteq g_1 &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+g_0+%5Csqsubseteq+g_1+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;which is also a partial ordering and induces an equivalence of &lt;code&gt;Tgraph&lt;/code&gt;s defined by&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle g_0 \equiv g_1  \text{ if and only if } g_0 \sqsubseteq g_1 \text{ and } g_1 \sqsubseteq g_0 &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+g_0+%5Cequiv+g_1++%5Ctext%7B+if+and+only+if+%7D+g_0+%5Csqsubseteq+g_1+%5Ctext%7B+and+%7D+g_1+%5Csqsubseteq+g_0+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;in which case &lt;img alt=&quot;g_0&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_0&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and &lt;img alt=&quot;g_1&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_1&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; are isomorphic as &lt;code&gt;Tgraph&lt;/code&gt;s.&lt;/p&gt;
&lt;p&gt;Both &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; are monotonic with respect to &lt;img alt=&quot;\sqsubseteq&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csqsubseteq&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; meaning:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  g_0 \sqsubseteq g_1 \text{ implies } \small\texttt{compose} \ g_0 \sqsubseteq \small\texttt{compose} \ g_1 \text{ and } \small\texttt{decompose} \ g_0 \sqsubseteq \small\texttt{decompose} \ g_1 &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++g_0+%5Csqsubseteq+g_1+%5Ctext%7B+implies+%7D+%5Csmall%5Ctexttt%7Bcompose%7D+%5C+g_0+%5Csqsubseteq+%5Csmall%5Ctexttt%7Bcompose%7D+%5C+g_1+%5Ctext%7B+and+%7D+%5Csmall%5Ctexttt%7Bdecompose%7D+%5C+g_0+%5Csqsubseteq+%5Csmall%5Ctexttt%7Bdecompose%7D+%5C+g_1+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;We also have &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is monotonic, but only when restricted to correct &lt;code&gt;Tgraph&lt;/code&gt;s. Also, when restricted to correct &lt;code&gt;Tgraph&lt;/code&gt;s, we have &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is &lt;em&gt;non decreasing&lt;/em&gt; because it only adds faces:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  g  \sqsubseteq \small\texttt{force} \ g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++g++%5Csqsubseteq+%5Csmall%5Ctexttt%7Bforce%7D+%5C+g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is idempotent (forcing a forced correct &lt;code&gt;Tgraph&lt;/code&gt; leaves it the same):&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{force}) \ g  \equiv \small\texttt{force} \ g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C+g++%5Cequiv+%5Csmall%5Ctexttt%7Bforce%7D+%5C+g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h3 id=&quot;composing-perfectly-and-perfect-compositions&quot;&gt;Composing perfectly and perfect compositions&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; A &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; &lt;em&gt;composes perfectly&lt;/em&gt; if all faces of &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; are composable (i.e there are no remainder faces of &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; when composing).&lt;/p&gt;
&lt;p&gt;We note that the composed faces must be a valid &lt;code&gt;Tgraph&lt;/code&gt; (connected with no crossing boundaries) if all faces are included in the composition because &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; has those properties. Clearly, if &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{decompose} \cdot \small\texttt{compose}) \  g \equiv g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Cequiv+g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;In general, for arbitrary &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; where the composition is defined, we only have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{decompose} \cdot \small\texttt{compose}) \  g \sqsubseteq g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Csqsubseteq+g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; A &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; &lt;em&gt;is a perfect composition&lt;/em&gt; if &lt;img alt=&quot;\small\texttt{decompose} \  g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D+%5C++g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly.&lt;/p&gt;
&lt;p&gt;Clearly if &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{compose} \cdot \small\texttt{decompose}) \  g' \equiv g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+%5Cequiv+g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;(We could use equality here because any new vertex labels introduced by &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; will be removed by &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;). In general, for arbitrary &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{compose} \cdot \small\texttt{decompose}) \  g' \sqsubseteq g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+%5Csqsubseteq+g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lemma 1&lt;/strong&gt;: &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition if &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; has the following 2 properties:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;every half-kite with a boundary join has either a half-dart or a whole kite on the short edge, and&lt;/li&gt;
&lt;li&gt;every half-dart with a boundary join has a half-kite on the short edge,&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;(Proof outline:) Firstly note that unknowns in &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (= &lt;img alt=&quot;\small\texttt{decompose} \  g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D+%5C++g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;) can only come from boundary joins in &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;. The properties 1 and 2 guarantee that &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; has no unknowns (the boundary dart wings can be classified as ace or jack vertices). Since every face of &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; has come from a decomposed face in &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, there can be no faces in &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; that will not recompose, so &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; will compose perfectly to &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;(Note: a perfect composition &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; may have unknowns even though its decomposition &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; has none.)&lt;/p&gt;
&lt;p&gt;It is easy to see two special cases:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;
&lt;p&gt;If &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is wholetile complete then &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition.&lt;/p&gt;
&lt;p&gt;Proof: Wholetile complete implies no boundary joins which implies properties 1 and 2 in lemma 1 which implies &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a decomposition then &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition.&lt;/p&gt;
&lt;p&gt;Proof: If &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a decomposition, then every half-dart has a half-kite on the short edge which implies property 2 of lemma 1. Also, any half-kite with a boundary join in &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; must have come from a decomposed half-dart since a decomposed half-kite produces a whole kite with no boundary kite join. So the half-kite must have a half-dart on the short edge which implies property 1 of lemma 1. The two properties imply &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We note that these two special cases cover all the &lt;code&gt;Tgraph&lt;/code&gt;s in the bottom rows of the diagrams in figure 8. So the &lt;code&gt;Tgraph&lt;/code&gt;s in each bottom row are perfect compositions, and furthermore, they all compose perfectly except for the rightmost &lt;code&gt;Tgraph&lt;/code&gt;s which have empty compositions.&lt;/p&gt;
&lt;p&gt;In the following results we make the assumption that a &lt;code&gt;Tgraph&lt;/code&gt; is correct, which guarantees that when &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is applied, it terminates with a correct &lt;code&gt;Tgraph&lt;/code&gt;. We also note that &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; preserves correctness as does &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (provided the composition is defined).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lemma 2:&lt;/strong&gt; If &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a forced, correct &lt;code&gt;Tgraph&lt;/code&gt; then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}) \  g_f \equiv g_f &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g_f+%5Cequiv+g_f+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;(Proof outline:) The proof uses a case analysis of boundary and internal vertices of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;. For internal vertices we just check there is no change at the vertex after &lt;img alt=&quot;(\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose})&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; using figure 12 (plus an extra case for the forced star). For boundary vertices we check the local context cases shown in figure 9.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 9: Local contexts for boundary vertices of a forced Tgraph&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/08/forcedbvfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 9: Local contexts for boundary vertices of a forced Tgraph&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;This shows two cases for a kite origin, two cases for a kite opp, four cases for a kite wing, and four cases for a dart origin. The only case for a dart wing is one of the two kite opp cases and there are no dart opp cases as these cannot be on the boundary of a forced &lt;code&gt;Tgraph&lt;/code&gt;. Actually figure 9 has a repeated case which is both a dart origin and a kite wing, but there are also 3 additional cases when we consider mirror images of those shown. Since there is no local change of the context in each case, and since this is true for all boundary vertices in any forced &lt;code&gt;Tgraph&lt;/code&gt;, there can be no non-local change either. (We omit the full details). &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lemma 3:&lt;/strong&gt; If &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition and a correct &lt;code&gt;Tgraph&lt;/code&gt;, then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle \small\texttt{force} \  g' \sqsubseteq (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}) \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csmall%5Ctexttt%7Bforce%7D+%5C++g%27+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;(Proof outline:) First we establish that when &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose}) \ g' \equiv (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{wholetiles}) \ g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C+g%27+%5Cequiv+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bwholetiles%7D%29+%5C+g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The difference between &lt;img alt=&quot;\small\texttt{decompose} \ g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D+%5C+g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and &lt;img alt=&quot;\small\texttt{decompose}(\small\texttt{wholetiles} \ g')&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D%28%5Csmall%5Ctexttt%7Bwholetiles%7D+%5C+g%27%29&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; lies in faces round the boundary dart wings of the former which have come from decomposing at boundary joins of &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;. Since &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition, &lt;img alt=&quot;\small\texttt{decompose} \ g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D+%5C+g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; can have no unknowns, so forcing will add all faces at these dart wings and the result will be the same as &lt;img alt=&quot;(\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{wholetiles}) \ g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bwholetiles%7D%29+%5C+g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Applying &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to both sides (which are forced &lt;code&gt;Tgraph&lt;/code&gt;s) gives&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}) \ g' \equiv (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{wholetiles}) \ g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C+g%27+%5Cequiv+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bwholetiles%7D%29+%5C+g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Next we establish that for any wholetile complete &lt;img alt=&quot;h'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=h%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle \small\texttt{force} \ h' \sqsubseteq (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}) \ h'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csmall%5Ctexttt%7Bforce%7D+%5C+h%27+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C+h%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The proof uses induction on force steps applied to &lt;img alt=&quot;h'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=h%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; where we can assume the context is wholetile complete. For each force rule we establish that &lt;img alt=&quot;(\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}) \ h'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C+h%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; contains both the new halftile and its other half. [A separate proof is needed to show that the ordering of applying force rules makes no difference to a final correct &lt;code&gt;Tgraph&lt;/code&gt; (apart from vertex relabelling)].&lt;/p&gt;
&lt;p&gt;Combining these results, for perfect composition &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle \small\texttt{force} \ g' \equiv \small\texttt{force}(\small\texttt{wholetile} \ g') \sqsubseteq (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}\cdot \small\texttt{wholetile}) \ g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csmall%5Ctexttt%7Bforce%7D+%5C+g%27+%5Cequiv+%5Csmall%5Ctexttt%7Bforce%7D%28%5Csmall%5Ctexttt%7Bwholetile%7D+%5C+g%27%29+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%5Ccdot+%5Csmall%5Ctexttt%7Bwholetile%7D%29+%5C+g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and for the right hand side we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}\cdot \small\texttt{wholetile}) \ g' \equiv (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}) \ g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%5Ccdot+%5Csmall%5Ctexttt%7Bwholetile%7D%29+%5C+g%27+%5Cequiv+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C+g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;as required. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lemma 4&lt;/strong&gt; If &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly and is a correct &lt;code&gt;Tgraph&lt;/code&gt; then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle \small\texttt{force} \ g \equiv (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose})\ g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csmall%5Ctexttt%7Bforce%7D+%5C+g+%5Cequiv+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29%5C+g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Proof: Assume &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly and is a correct &lt;code&gt;Tgraph&lt;/code&gt;. Since &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is non-decreasing (with respect to &lt;img alt=&quot;\sqsubseteq&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csqsubseteq&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; on correct &lt;code&gt;Tgraph&lt;/code&gt;s)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle \small\texttt{compose} \  g \sqsubseteq (\small\texttt{force} \cdot \small\texttt{compose}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csmall%5Ctexttt%7Bcompose%7D+%5C++g+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and since &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is monotonic&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{decompose} \cdot \small\texttt{compose}) \  g \sqsubseteq (\small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Since &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly, the left hand side is just &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, so&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle g \sqsubseteq (\small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+g+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and since &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is monotonic (with respect to &lt;img alt=&quot;\sqsubseteq&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csqsubseteq&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; on correct &lt;code&gt;Tgraph&lt;/code&gt;s)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (*) \ \ \ \  \ \small\texttt{force} \  g \sqsubseteq (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%2A%29+%5C+%5C+%5C+%5C++%5C+%5Csmall%5Ctexttt%7Bforce%7D+%5C++g+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;For the opposite direction, we substitute &lt;img alt=&quot;\small\texttt{compose} \  g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D+%5C++g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; for &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; in lemma 3 to get&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{force} \cdot \small\texttt{compose}) \  g \sqsubseteq (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{compose}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Then, since &lt;img alt=&quot;(\small\texttt{decompose} \cdot \small\texttt{compose}) \  g \equiv g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Cequiv+g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{force} \cdot \small\texttt{compose}) \  g \sqsubseteq (\small\texttt{compose} \cdot \small\texttt{force}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Apply &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to both sides (using monotonicity)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose}) \  g \sqsubseteq (\small\texttt{decompose} \cdot \small\texttt{compose} \cdot \small\texttt{force}) \  g   &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g+++&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;For any &lt;img alt=&quot;g''&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; for which the composition is defined we have &lt;img alt=&quot;(\small\texttt{decompose} \cdot \small\texttt{compose})\ g'' \sqsubseteq g''&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29%5C+g%27%27+%5Csqsubseteq+g%27%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; so we get&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose}) \  g \sqsubseteq \small\texttt{force} \  g   &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Csqsubseteq+%5Csmall%5Ctexttt%7Bforce%7D+%5C++g+++&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Now apply &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to both sides and note &lt;img alt=&quot;(\small\texttt{force} \cdot \small\texttt{force})\ g \equiv \small\texttt{force} \ g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29%5C+g+%5Cequiv+%5Csmall%5Ctexttt%7Bforce%7D+%5C+g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to get&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose}) \  g \sqsubseteq \small\texttt{force} \  g   &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Csqsubseteq+%5Csmall%5Ctexttt%7Bforce%7D+%5C++g+++&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Combining this with (*) above proves the required equivalence. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Theorem&lt;/strong&gt; (&lt;em&gt;Perfect Composition&lt;/em&gt;): If &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly and is a correct &lt;code&gt;Tgraph&lt;/code&gt; then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{compose} \cdot \small\texttt{force}) \  g \equiv (\small\texttt{force} \cdot \small\texttt{compose}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g+%5Cequiv+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Proof: Assume &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly and is a correct &lt;code&gt;Tgraph&lt;/code&gt;. By lemma 4 we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle \small\texttt{force} \ g \equiv (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose})\ g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Csmall%5Ctexttt%7Bforce%7D+%5C+g+%5Cequiv+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29%5C+g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Applying &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to both sides, gives&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{compose} \cdot \small\texttt{force}) \ g \equiv (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force} \cdot \small\texttt{compose})\ g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C+g+%5Cequiv+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29%5C+g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Now by lemma 2, with &lt;img alt=&quot;g_f = (\small\texttt{force} \cdot \small\texttt{compose}) \  g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f+%3D+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, the right hand side is equivalent to&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (\small\texttt{force} \cdot \small\texttt{compose}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;which establishes the result. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Corollaries&lt;/strong&gt; (of the perfect composition theorem):&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;
&lt;p&gt;If &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition and a correct &lt;code&gt;Tgraph&lt;/code&gt; then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  \small\texttt{force} \  g' \equiv (\small\texttt{compose} \cdot \small\texttt{force} \cdot \small\texttt{decompose}) \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Csmall%5Ctexttt%7Bforce%7D+%5C++g%27+%5Cequiv+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Proof: Let &lt;img alt=&quot;g' = \small\texttt{compose} \  g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27+%3D+%5Csmall%5Ctexttt%7Bcompose%7D+%5C++g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (so &lt;img alt=&quot;g \equiv \small\texttt{decompose} \  g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g+%5Cequiv+%5Csmall%5Ctexttt%7Bdecompose%7D+%5C++g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;) in the theorem. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;[This result generalises lemma 2 because any correct forced &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is necessarily wholetile complete and therefore a perfect composition, and &lt;img alt=&quot;\small\texttt{force} \ g_f \equiv g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D+%5C+g_f+%5Cequiv+g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;.]&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition and a correct &lt;code&gt;Tgraph&lt;/code&gt; then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{decompose} \cdot \small\texttt{force}) \  g' \sqsubseteq (\small\texttt{force} \cdot \small\texttt{decompose}) \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g%27+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Proof: Apply &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to both sides of the previous corollary and note that&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{decompose} \cdot \small\texttt{compose}) \  g'' \sqsubseteq g'' \textit{ for any } g'' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g%27%27+%5Csqsubseteq+g%27%27+%5Ctextit%7B+for+any+%7D+g%27%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;provided the composition is defined, which it must be for a forced &lt;code&gt;Tgraph&lt;/code&gt; by the &lt;em&gt;Compose Force&lt;/em&gt; theorem. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition and a correct &lt;code&gt;Tgraph&lt;/code&gt; then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose}) \  g' \equiv (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force}) \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+%5Cequiv+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Proof: Apply &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to both sides of the previous corollary noting &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is monotonic and idempotent for correct &lt;code&gt;Tgraph&lt;/code&gt;s&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force}) \  g' \sqsubseteq (\small\texttt{force} \cdot \small\texttt{decompose}) \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g%27+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;From the fact that &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is non decreasing and &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; are monotonic, we also have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose}) \  g' \sqsubseteq (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force}) \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Hence combining these two sub-&lt;code&gt;Tgraph&lt;/code&gt; results we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose}) \  g' \equiv (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{force}) \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D%29+%5C++g%27+%5Cequiv+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It is important to point out that if &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a correct &lt;code&gt;Tgraph&lt;/code&gt; and &lt;img alt=&quot;\small\texttt{compose} \  g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D+%5C++g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a perfect composition then this is not the same as &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly. It could be the case that &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; has more faces than &lt;img alt=&quot;(\small\texttt{decompose} \cdot \small\texttt{compose}) \  g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and so &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; could have unknowns. In this case we can only prove that&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{compose}) \  g \sqsubseteq (\small\texttt{compose} \cdot \small\texttt{force}) \  g &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Csqsubseteq+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;As an example where this is not an equivalence, choose &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to be a star. Then its composition is the empty &lt;code&gt;Tgraph&lt;/code&gt; (which is still a pefect composition) and so the left hand side is the empty &lt;code&gt;Tgraph&lt;/code&gt;, but the right hand side is a sun.&lt;/p&gt;
&lt;h3 id=&quot;perfectly-composing-generators&quot;&gt;Perfectly composing generators&lt;/h3&gt;
&lt;p&gt;The perfect composition theorem and lemmas and the three corollaries justify all the commuting implied by the diagrams in figure 8. However, one might ask more general questions like: Under what circumstances do we have (for a correct forced &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{compose}) \  g_f \equiv g_f &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g_f+%5Cequiv+g_f+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt; A &lt;em&gt;generator&lt;/em&gt; of a correct forced &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is any &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; such that &lt;img alt=&quot;g \sqsubseteq g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g+%5Csqsubseteq+g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and &lt;img alt=&quot;\small\texttt{force} \ g \equiv g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D+%5C+g+%5Cequiv+g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;We can now state that&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Corollary&lt;/strong&gt; If a correct forced &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; has a generator which composes perfectly, then&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{compose}) \  g_f \equiv g_f &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g_f+%5Cequiv+g_f+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Proof: This follows directly from lemma 4 and the perfect composition theorem. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;As an example where the required generator does not exist, consider the rightmost &lt;code&gt;Tgraph&lt;/code&gt; of the middle row in figure 10. It is generated by the &lt;code&gt;Tgraph&lt;/code&gt; directly below it, but it has no generator with a perfect composition. The &lt;code&gt;Tgraph&lt;/code&gt; directly above it in the top row is the result of applying &lt;img alt=&quot;(\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{compose})&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; which has lost the leftmost dart of the &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 10: A Tgraph without a perfectly composing generator&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/curiofig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 10: A Tgraph without a perfectly composing generator&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;We could summarise this section by saying that &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; can lose information which cannot be recovered by a subsequent &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and, similarly, &lt;img alt=&quot;\small\texttt{decompose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bdecompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; can lose information which cannot be recovered by a subsequent &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;. We have defined &lt;em&gt;perfect compositions&lt;/em&gt; which are the &lt;code&gt;Tgraph&lt;/code&gt;s that do not lose information when decomposed and &lt;code&gt;Tgraph&lt;/code&gt;s which &lt;em&gt;compose perfectly&lt;/em&gt; which are those that do not lose information when composed. Forcing does the same thing at each level of composition (that is it commutes with composition) provided information is not lost when composing.&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;4&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;multiple-compositions&quot;&gt;4. Multiple Compositions&lt;/h2&gt;
&lt;p&gt;We know from the &lt;code&gt;Compose Force&lt;/code&gt; theorem that the composition of a &lt;code&gt;Tgraph&lt;/code&gt; that is forced is always a valid &lt;code&gt;Tgraph&lt;/code&gt;. In this section we use this and the results from the last section to show that composing a forced, correct &lt;code&gt;Tgraph&lt;/code&gt; produces a forced &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First we note that:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lemma 5&lt;/strong&gt;: The composition of a forced, correct &lt;code&gt;Tgraph&lt;/code&gt; is wholetile complete.&lt;/p&gt;
&lt;p&gt;Proof: Let &lt;img alt=&quot;g' = \small\texttt{compose} \  g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27+%3D+%5Csmall%5Ctexttt%7Bcompose%7D+%5C++g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; where &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a forced, correct &lt;code&gt;Tgraph&lt;/code&gt;. A boundary join in &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; implies there must be a boundary dart wing of the composable faces of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;. (See for example figure 4 where this would be vertex 2 for the half dart case, and vertex 5 for the half-kite face). This dart wing cannot be an unknown as the half-dart is in the composable faces. However, a &lt;em&gt;known&lt;/em&gt; dart wing must be either a large kite centre or a large dart base and therefore internal in the composable faces of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (because of the force rules) and therefore not on the boundary in &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;. This is a contradiction showing that &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; can have no boundary joins and is therefore wholetile complete. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Theorem:&lt;/strong&gt; The composition of a forced, correct &lt;code&gt;Tgraph&lt;/code&gt; is a forced &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Proof: Let &lt;img alt=&quot;g' = \small\texttt{compose} \  g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27+%3D+%5Csmall%5Ctexttt%7Bcompose%7D+%5C++g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; for some forced, correct &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, then &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is wholetile complete (by lemma 5) and therefore a perfect composition. Let &lt;img alt=&quot;g = \small\texttt{decompose} \  g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g+%3D+%5Csmall%5Ctexttt%7Bdecompose%7D+%5C++g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, so &lt;img alt=&quot;g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly (&lt;img alt=&quot;g' \equiv \small\texttt{compose} \  g&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27+%5Cequiv+%5Csmall%5Ctexttt%7Bcompose%7D+%5C++g&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;). By the perfect composition theorem we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle (**) \ \ \ \  \ (\small\texttt{compose} \cdot \small\texttt{force}) \  g \equiv (\small\texttt{force} \cdot \small\texttt{compose}) \  g \equiv \small\texttt{force} \  g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%28%2A%2A%29+%5C+%5C+%5C+%5C++%5C+%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g+%5Cequiv+%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g+%5Cequiv+%5Csmall%5Ctexttt%7Bforce%7D+%5C++g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;We also have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  g = \small\texttt{decompose} \  g' = (\small\texttt{decompose} \cdot \small\texttt{compose}) \  g_f \sqsubseteq g_f &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++g+%3D+%5Csmall%5Ctexttt%7Bdecompose%7D+%5C++g%27+%3D+%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g_f+%5Csqsubseteq+g_f+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Applying &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to both sides, noting that &lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is monotonic and the identity on forced &lt;code&gt;Tgraph&lt;/code&gt;s, we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  \small\texttt{force} \  g \sqsubseteq \small\texttt{force} \  g_f \equiv g_f &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Csmall%5Ctexttt%7Bforce%7D+%5C++g+%5Csqsubseteq+%5Csmall%5Ctexttt%7Bforce%7D+%5C++g_f+%5Cequiv+g_f+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Applying &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; to both sides, noting that &lt;img alt=&quot;\small\texttt{compose}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is monotonic, we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{compose} \cdot \small\texttt{force}) \  g \sqsubseteq \small\texttt{compose} \  g_f \equiv g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bcompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bforce%7D%29+%5C++g+%5Csqsubseteq+%5Csmall%5Ctexttt%7Bcompose%7D+%5C++g_f+%5Cequiv+g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;By (**) above, the left hand side is equivalent to &lt;img alt=&quot;\small\texttt{force} \  g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D+%5C++g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; so we have&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  \small\texttt{force} \  g' \sqsubseteq g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Csmall%5Ctexttt%7Bforce%7D+%5C++g%27+%5Csqsubseteq+g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;but since we also have (&lt;img alt=&quot;\small\texttt{force}&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bforce%7D&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; being non-decreasing)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  g' \sqsubseteq \small\texttt{force} \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++g%27+%5Csqsubseteq+%5Csmall%5Ctexttt%7Bforce%7D+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;we have established that&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  g' \equiv \small\texttt{force} \  g' &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++g%27+%5Cequiv+%5Csmall%5Ctexttt%7Bforce%7D+%5C++g%27+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;which means &lt;img alt=&quot;g'&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g%27&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a forced &lt;code&gt;Tgraph&lt;/code&gt;. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;This result means that after forcing once we can repeatedly compose creating valid &lt;code&gt;Tgraph&lt;/code&gt;s until we reach the empty &lt;code&gt;Tgraph&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We can also use lemma 5 to establish the converse to a previous corollary:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Corollary&lt;/strong&gt; If a correct forced &lt;code&gt;Tgraph&lt;/code&gt; &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; satisfies:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{compose}) \  g_f \equiv g_f &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g_f+%5Cequiv+g_f+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;then &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; has a generator which composes perfectly.&lt;/p&gt;
&lt;p&gt;Proof: By lemma 5, &lt;img alt=&quot;\small\texttt{compose} \ g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csmall%5Ctexttt%7Bcompose%7D+%5C+g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is wholetile complete and hence a perfect composition. This means that &lt;img alt=&quot;(\small\texttt{decompose} \cdot \small\texttt{compose}) \ g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%28%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C+g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; composes perfectly and it is also a generator for &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; because&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;img alt=&quot;\displaystyle  (\small\texttt{force} \cdot \small\texttt{decompose} \cdot \small\texttt{compose}) \  g_f \equiv g_f &quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Csmall%5Ctexttt%7Bforce%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bdecompose%7D+%5Ccdot+%5Csmall%5Ctexttt%7Bcompose%7D%29+%5C++g_f+%5Cequiv+g_f+&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;5&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;proof-of-the-compose-force-theorem&quot;&gt;5. Proof of the Compose Force theorem&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Theorem&lt;/strong&gt; (&lt;em&gt;Compose Force&lt;/em&gt;): &lt;em&gt;Composition of a forced Tgraph produces a valid Tgraph.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof&lt;/strong&gt;: For any forced &lt;code&gt;Tgraph&lt;/code&gt; we can construct the composed faces. For the result to be a valid &lt;code&gt;Tgraph&lt;/code&gt; we need to show no crossing boundaries and connectedness for the composed faces. These are proved separately by case analysis below.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof of no crossing boundaries&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Assume &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a forced &lt;code&gt;Tgraph&lt;/code&gt; and that it has a non-empty set of composed faces (we can ignore cases where the composition is empty as the empty &lt;code&gt;Tgraph&lt;/code&gt; is valid). Consider a vertex &lt;code&gt;v&lt;/code&gt; in the composed faces of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and first take the case that &lt;code&gt;v&lt;/code&gt; is on the boundary of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; . We consider local contexts for a vertex &lt;code&gt;v&lt;/code&gt; on a forced &lt;code&gt;Tgraph&lt;/code&gt; boundary where the composition is non-empty as shown in figure 11.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 11: Forced Boundary Vertex Compose Contexts&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/08/checkfbvcompfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 11: Forced Boundary Vertex Compose Contexts&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;In each case &lt;code&gt;v&lt;/code&gt; is shown as a red dot, and the composition is shown filled yellow. The cases for &lt;code&gt;v&lt;/code&gt; are shown in rows: the first row is for dart origins, the second row is for kite origins, the third row is for kite wings, and the last rows is for kite opps. The dart wing cases are a subset of the kite opp cases, so not repeated, and dart opp vertices are excluded because they cannot be on the boundary of a forced &lt;code&gt;Tgraph&lt;/code&gt;. We only show left-hand versions, so there is a mirror symmetric set for right-hand versions.&lt;/p&gt;
&lt;p&gt;It is easy to see that there are no crossing boundaries of the composed faces at &lt;code&gt;v&lt;/code&gt; in each case. Since any boundary vertex of any forced &lt;code&gt;Tgraph&lt;/code&gt; (with a non-empty composition) must match one of these local context cases around the vertex, we can conclude that a boundary vertex of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; cannot become a crossing boundary in &lt;img alt=&quot;compose \  g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=compose+%5C++g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;Next take the case where &lt;code&gt;v&lt;/code&gt; is an internal vertex of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; .&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 12: Vertex types and their relationships&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/relatedvtypefig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 12: Vertex types and their relationships&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Figure 12 shows relationships between the forced &lt;code&gt;Tgraph&lt;/code&gt;s of the 7 (internal) vertex types (plus a kite at the top right). The red faces are those around the vertex type and the black faces are those produced by forcing (if any). Each forced &lt;code&gt;Tgraph&lt;/code&gt; has its composition directly above with empty compositions for the top row. We note that a (forced) star, jack, king, and queen vertex remains an internal vertex in the respective composition so cannot become a crossing boundary vertex. A deuce vertex becomes the centre of a larger kite and is no longer present in the composition (top right). That leaves cases for the sun vertex and ace vertex (=fool vertex). The sun &lt;code&gt;Tgraph&lt;/code&gt; (&lt;code&gt;sunGraph&lt;/code&gt;) and fool &lt;code&gt;Tgraph&lt;/code&gt; (&lt;code&gt;fool&lt;/code&gt;) consist of just the red faces at the respective vertex (shown top left and top centre). These both have empty compositions when there is no surrounding context. We thus need to check possible forced local contexts for &lt;code&gt;sunGraph&lt;/code&gt; and &lt;code&gt;fool&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;fool&lt;/code&gt; case is simple and similar to a duece vertex in that it is never part of a composition. [To see this consider inverting the decomposition arrows shown in figure 4. In both cases we see the half-dart opp vertex (labelled 4 in figure 4) is removed].&lt;/p&gt;
&lt;p&gt;For the &lt;code&gt;sunGraph&lt;/code&gt; there are only 7 local forced context cases to consider where the sun vertex is on the boundary of the composition.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 13: Forced Contexts for a sun vertex v where v is on the composition boundary&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/sunvcontextscompboundary.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 13: Forced Contexts for a sun vertex &lt;code&gt;v&lt;/code&gt; where &lt;code&gt;v&lt;/code&gt; is on the composition boundary&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Six of these are shown in figure 13 (the missing one is just a mirror reflection of the fourth case). Again, the relevant vertex &lt;code&gt;v&lt;/code&gt; is shown as a red dot and the composed faces are shown filled yellow, so it is easy to check that there is no crossing boundary of the composed faces at &lt;code&gt;v&lt;/code&gt; in each case. Every forced &lt;code&gt;Tgraph&lt;/code&gt; containing an internal sun vertex where the vertex is on the boundary of the composition must match one of the 7 cases locally round the vertex.&lt;/p&gt;
&lt;p&gt;Thus no vertex from &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; can become a crossing boundary vertex in the composed faces and since the vertices of the composed faces are a subset of those of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, we can have no crossing boundary vertex in the composed faces.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proof of Connectedness&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Assume &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is a forced &lt;code&gt;Tgraph&lt;/code&gt; as before. We refer to the half-tile faces of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; that get included in the composed faces as the &lt;em&gt;composable&lt;/em&gt; faces and the rest as the &lt;em&gt;remainder&lt;/em&gt; faces. We want to prove that the composable faces are connected as this will imply the composed faces are connected.&lt;/p&gt;
&lt;p&gt;As before we can ignore cases where the set of composable faces is empty, and assume this is not the case. We study the nature of the remainder faces of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;. Firstly, we note:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lemma&lt;/strong&gt; (remainder faces)&lt;/p&gt;
&lt;p&gt;The remainder faces of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; are made up entirely of groups of half-tiles which are either:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;Half-fools (= a half dart and both halves of the kite attached to its short edge) where the other half-fool is entirely composable faces, or&lt;/li&gt;
&lt;li&gt;Both halves of a kite with both short edges on the (&lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;) boundary (so they are not part of a half-fool) where (at most) the origin is in common with composable faces, or&lt;/li&gt;
&lt;li&gt;Whole fools where (at most) the shared kite origin in common with composable faces.&lt;/li&gt;
&lt;/ol&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 14: Remainder face groups (cases 1,2, and 3)&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2023/09/remaindergroupsfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 14: Remainder face groups (cases 1,2, and 3)&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;These 3 cases of remainder face groups are shown in figure 14. In each case the possible border in common with composable faces is shown yellow and the red edges are necessarily on the boundary of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; (the black boundary could be on the boundary of &lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; or shared with another reamainder face group). [A mirror symmetric version for the first group is not shown.] Examples can be seen in e.g. figure 13 where the first &lt;code&gt;Tgraph&lt;/code&gt; has four examples of case 1, and two of case 2, the second has six examples of case 1 and two of case 2, and the fifth &lt;code&gt;Tgraph&lt;/code&gt; has an example of case 3 as well as four of case 1. [We omit the detailed proof of this lemma which reasons about what gets excluded in a composition after forcing. However, all the local context cases are included in figure 15 (left-hand versions), where we only show those contexts where there is a non-empty composition.]&lt;/p&gt;
&lt;p&gt;We note from the (remainder faces) lemma that the common boundary of the group of remainder faces with the composable faces (shown yellow in figure 14) is at most a single vertex in cases 2 and 3. In case 1, the common boundary is just a single edge of the composed faces which is made up of 2 adjacent edges of the composable faces that constitute the join of two half-fools.&lt;/p&gt;
&lt;p&gt;This means each (remainder face) group shares boundary with exactly one connected component of the composable faces.&lt;/p&gt;
&lt;p&gt;Next we establish that if two (remainder face) groups are connected they must share boundary with the &lt;em&gt;same&lt;/em&gt; connected component of the composable faces. We need to consider how each (remainder face) group can be connected with a neighbouring such group. It is enough to consider forced contexts of boundary dart long edges (for cases 1 and 3) and boundary kite short edges (for case 2). The cases where the composition is non-empty all appear in figure 15 (left-hand versions) along with boundary kite long edges (middle two rows) which are not relevant for the proof.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure 15: Forced contexts for boundary edges&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2024/08/newfbecontextcompfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure 15: Forced contexts for boundary edges&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;We note that, whenever one group of the remainder faces (half-fool, whole-kite, whole-fool) is connected to a neighbouring group of the remainder faces, any common boundary (shared edges and vertices) with the compososable faces is also connected. The combined common boundary forms either 2 adjacent composed face boundary edges (= 4 adjacent edges of the composable faces), or a composed face boundary edge and one of its end vertices, or a single composed face boundary vertex.&lt;/p&gt;
&lt;p&gt;It follows that any connected collection of the remainder face groups shares boundary with a unique connected component of the composable faces. Since the collection of composable and remainder faces together is connected (&lt;img alt=&quot;g_f&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=g_f&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; is connected) the removal of the remainder faces cannot disconnect the composable faces. For this to happen, at least one connected collection of remainder face groups would have to be connected to more than one connected component of composable faces.&lt;/p&gt;
&lt;p&gt;This establishes connectedness of any composition of a forced &lt;code&gt;Tgraph&lt;/code&gt;, and this completes the proof of the &lt;em&gt;Compose Force&lt;/em&gt; theorem. &lt;img alt=&quot;\square&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=%5Csquare&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;MG&quot;&gt; [1] Martin Gardner (1977) &lt;em&gt;MATHEMATICAL GAMES&lt;/em&gt;. Scientific American, 236(1), (pages 110 to 121). &lt;/a&gt;&lt;a class=&quot;uri&quot; href=&quot;http://www.jstor.org/stable/24953856&quot;&gt;http://www.jstor.org/stable/24953856&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;BG&quot;&gt; [2] GrÃ¼nbaum B., Shephard G.C. (1987) &lt;em&gt;Tilings and Patterns&lt;/em&gt;. W. H. Freeman and Company, New York. ISBN 0-7167-1193-1 (Hardback) (pages 540 to 542). &lt;/a&gt;&lt;/p&gt;</description>
	<pubDate>Tue, 13 Oct 2026 12:27:21 +0000</pubDate>
</item>
<item>
	<title>Alessandro Vermeulen: A fresh leaf</title>
	<guid isPermaLink="true">https://alessandrovermeulen.me/posts/a-fresh-leaf/</guid>
	<link>https://alessandrovermeulen.me/posts/a-fresh-leaf/</link>
	<description>&lt;p&gt;It has been a very long time since I posted a new article.
The reasons for this are quite commonplace.
Starting a family and building a career captured both my interest and energy, leaving little of either for sharing.&lt;/p&gt;
&lt;p&gt;Obviously, I never stopped reading or learning; however, the vehicle for sharing changed to one with less friction: Twitter.
Educating my teammates and colleagues satisfied my need to teach and share for a long time.
It felt like this was the place where I could contribute.
I found so many great articles and ideas that I simply wanted to amplify them, feeling unable to add to their eloquence.
Ultimately, when family and career demands grew further, I stopped sharing completely.
(Twitter going down the drain also didn’t help.)&lt;/p&gt;
&lt;p&gt;Fast-forward a few years, and I found myself confronted with burnout.
I was and still am lucky enough to recover at my own pace, take the time to get back on my feet, and reflect on the road that brought me here.&lt;/p&gt;
&lt;p&gt;The burnout, while not nice to experience, also has been a great boon.
It allowed me to reinvent myself, re-establish my sense of self, and plot a new course.
Essentially, it meant going back to the roots of doing what I enjoy: learning, creating, and sharing.&lt;/p&gt;
&lt;p&gt;All this time, ideas have been floating around my head continuously, ranging from academic research topics and work tasks to household improvements and side projects.&lt;/p&gt;
&lt;p&gt;For a long time, I was quite sceptical about using LLMs for programming.
My experience stemmed from early research agents around 2018–2020.
I shared a common sense of AI fatigue in the news.
Some enthusiastic colleagues tried to convince me how great agentic programming with these “new frontier” models was and never really succeeded since I had such an underwhelming experience with LLMs in the past.&lt;/p&gt;
&lt;p&gt;Then, three things converged to change my mind: genuine skin in the game, zero skills to investigate on my own, and free access to a frontier model.&lt;/p&gt;
&lt;p&gt;I saw &lt;a href=&quot;https://youtu.be/6IFVTcM28KA&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;Gamers Nexus: “216,000,000 Spy TVs | The LG Smart TV Problem”&lt;/a&gt; on Hacker News about LG TVs allegedly containing spyware.
I own an LG TV, an earlier model than the article, and wanted to know whether my device was affected as well.
I had no experience with this type of digging into systems so I didn’t know where to start.
Now I had skin in the game and really wanted to figure this out.
Coincidentally, I got a link from someone to an OpenAI page and realised I created an account there once because my password manager suggested a login.
Curious, I logged in and saw that I had a month of free usage!
So why not use it for the investigation?&lt;/p&gt;
&lt;p&gt;Long story short, I started by using ChatGPT to explain to me how I should approach this investigation.
The approach sounded reasonable.
It quickly escalated to using Codex with many agents.
Along the way I made many mistakes blowing through five-hour usage limits, etc. But the agents were finding results.
Not the ones from the article, but close.
(Maybe more on that later.)
This was amazing: here was a tool that could do what I could not, and all I needed to do was nudge it forward, left, or right.
I was hooked.&lt;/p&gt;
&lt;p&gt;In a month, I went from an AI/LLM-sceptic to a guarded optimist.
Doing several projects, I discovered how to control agents, the effects of harnesses, their strengths (and weaknesses…), and the tendencies of the different model families.
I went from using ACP in Zed to just using Codex and Antigravity on the CLI, only rarely inspecting the code manually.
Steering the code and code quality happens with test-coverage, mutation-coverage, and linting rules.&lt;/p&gt;
&lt;p&gt;This is a journey I feel I want to share.
For that I needed to redo the site.
I didn’t want to relearn modern Hugo or figure out all the best themes out there and how to configure them; I just wanted results.&lt;/p&gt;
&lt;p&gt;There was another impetus.
I have a long-standing pet peeve with how my blog looked before, and how sites in general look.
Specifically for longer reads.
I &lt;em&gt;love&lt;/em&gt; the visual of text typeset in &lt;a href=&quot;https://www.tug.org/&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;LaTeX&lt;/a&gt; (specifically the &lt;a href=&quot;https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Knuth%E2%80%93Plass_algorithm&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;Knuth–Plass line-breaking algorithm&lt;/a&gt;), and now &lt;a href=&quot;https://typst.app/&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;Typst&lt;/a&gt;, but I cannot stand how most text looks in browsers, especially with &lt;code&gt;text-align: justify&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The solution was simple.
Clone my old site repository, point Codex at it, explain what I wanted to do, and it got cracking.
It asked me a few questions on what I liked, what design I wanted, and came back with a few themes.
I added my text layout criteria and we went forward implementing it.
(It was also straightforward to let it port the algorithm to Go, as I previously had it port the code to Swift for my &lt;a href=&quot;https://github.com/spockz/knowledgebase-viewer&quot; rel=&quot;noreferrer&quot; target=&quot;_blank&quot;&gt;KnowledgeBase Viewer&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;The result is what you are reading now.
I look forward to sharing more.&lt;/p&gt;</description>
	<pubDate>Thu, 08 Oct 2026 20:00:00 +0000</pubDate>
</item>
<item>
	<title>Well-Typed.Com: First official release of hs-bindgen</title>
	<guid isPermaLink="false">http://www.well-typed.com/blog/2026/10/hs-bindgen-official-release</guid>
	<link>https://well-typed.com/blog/2026/10/hs-bindgen-official-release</link>
	<description>&lt;p&gt;Well-Typed and the entire &lt;code&gt;hs-bindgen&lt;/code&gt; team—there are quite a few of us, see
below—are delighted to announce &lt;code&gt;hs-bindgen&lt;/code&gt; 1.0, the first official release.
In case you missed the &lt;a href=&quot;https://well-typed.com/blog/2026/02/hs-bindgen-alpha/&quot;&gt;announcement of the alpha release&lt;/a&gt;,
&lt;code&gt;hs-bindgen&lt;/code&gt; is a tool to automatically generate Haskell bindings from C
headers.&lt;/p&gt;
&lt;p&gt;In this blog post we will start with a &lt;em&gt;very&lt;/em&gt; brief intro to &lt;code&gt;hs-bindgen&lt;/code&gt;, but
we will not repeat everything that we discussed in the first two blog posts that
announced the &lt;a href=&quot;https://well-typed.com/blog/2026/02/hs-bindgen-alpha/&quot;&gt;first&lt;/a&gt; and &lt;a href=&quot;https://well-typed.com/blog/2026/03/hs-bindgen-alpha2/&quot;&gt;second&lt;/a&gt; alpha releases.
Instead, we will highlight a few of the major changes; for a full list (which is
quite long!), please see the &lt;a href=&quot;https://hackage.haskell.org/package/hs-bindgen-1.0.0.0/changelog&quot;&gt;changelog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can &lt;a href=&quot;https://hackage.haskell.org/package/hs-bindgen&quot;&gt;find &lt;code&gt;hs-bindgen&lt;/code&gt; on Hackage&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Consider this small C header:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode c&quot;&gt;&lt;code class=&quot;sourceCode c&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#include &lt;/span&gt;&lt;span class=&quot;im&quot;&gt;&amp;lt;stddef.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;size_t&lt;/span&gt; length&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;struct&lt;/span&gt; str &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; bytes&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  length len&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;}&lt;/span&gt; str&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;/**&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt; * Construct &lt;/span&gt;&lt;span class=&quot;an&quot;&gt;\ref&lt;/span&gt;&lt;span class=&quot;co&quot;&gt; &lt;/span&gt;&lt;span class=&quot;cv&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;co&quot;&gt; from null-terminated input&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt; */&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb1-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;str new_string&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; str&lt;span class=&quot;op&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When we run &lt;code&gt;hs-bindgen&lt;/code&gt; on this example&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ hs-bindgen-cli preprocess example.h --omit-field-prefixes # .. more args&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;it will generate a number of Haskell modules, which we summarize below:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Length&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Length&lt;/span&gt;{&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    unwrap ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CSize&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Str&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Str&lt;/span&gt;{&lt;/span&gt;
&lt;span id=&quot;cb3-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    bytes ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ptr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CChar&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ,&lt;span class=&quot;ot&quot;&gt; len   ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Length&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;
&lt;span id=&quot;cb3-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- .. bunch of instances ..&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;{-| Construct 'Str' from null-terminated input&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;    __C declaration:__ @new_string@&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;    .. other Haddocks elided, here and elsewhere ..&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-16&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-17&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;new_string ::&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-18&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     &lt;span class=&quot;dt&quot;&gt;PtrConst&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CChar&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- ^ __C declaration:__ @str@&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-19&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Str&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-20&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb3-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;new_string &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;{- .. -}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Some notable features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;C names are translated into Haskell names, modifying them to match Haskell’s
naming rules where needed.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;--omit-field-prefixes&lt;/code&gt; the Haskell code relies on
&lt;code&gt;DuplicateRecordFields&lt;/code&gt;; without that option it would have prefixed every
field (&lt;code&gt;lengthUnwrap&lt;/code&gt;, &lt;code&gt;strBytes&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;No Haskell definition is generated for &lt;code&gt;size_t&lt;/code&gt;, defined in &lt;code&gt;stddef.h&lt;/code&gt;;
instead, it is mapped to the existing &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Foreign-C-Types.html#t:CSize&quot;&gt;&lt;code&gt;CSize&lt;/code&gt;&lt;/a&gt; type by means of
a &lt;em&gt;binding specification&lt;/em&gt; for the standard C library. Binding specifications
make binding generation compositional: bindings generated for one library can
be reused in another.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;length&lt;/code&gt; typedef is translated to a Haskell newtype, but the &lt;code&gt;str&lt;/code&gt; typedef
around the &lt;code&gt;str&lt;/code&gt; struct is not: the latter exists in C only for syntactic
convenience.&lt;/li&gt;
&lt;li&gt;Function &lt;code&gt;new_string&lt;/code&gt; returns a struct by value, which is not natively
supported by Haskell’s FFI, and so &lt;code&gt;hs-bindgen&lt;/code&gt; will also generate the
necessary C wrappers.&lt;/li&gt;
&lt;li&gt;All Haskell declarations get Haddock comments linking them to the
corresponding C declaration; if that C declaration has associated Doxygen
comments, those are also translated.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the remainder of this blog post we will highlight some of the improvements
we’ve made since the alpha release. As you will see, there are a &lt;em&gt;lot&lt;/em&gt; of
details to get right!&lt;/p&gt;
&lt;h2 id=&quot;ffi-types&quot;&gt;FFI types&lt;/h2&gt;
&lt;p&gt;Consider a C function&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode c&quot;&gt;&lt;code class=&quot;sourceCode c&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;void&lt;/span&gt; foo&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;MyType x&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The obvious translation into Haskell is&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foreign &lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;{- .. -}&lt;/span&gt; foo_wrapper :: &lt;span class=&quot;dt&quot;&gt;MyType&lt;/span&gt; -&amp;gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, this is valid only if GHC can see that &lt;code&gt;MyType&lt;/code&gt; is a type supported
by the Haskell FFI; in particular, this means that GHC must be able to unwrap
that &lt;code&gt;MyType&lt;/code&gt; newtype until it gets a base type from a fixed set of permitted
foreign types.&lt;/p&gt;
&lt;p&gt;This is hard to ensure in general, especially if &lt;code&gt;MyType&lt;/code&gt; is not newly
generated, but comes from another library (previously generated by &lt;code&gt;hs-bindgen&lt;/code&gt;,
or handwritten). In the alpha release we resolved this by reducing every
argument to a base type, but this resulted in code that was less portable than
it could have been; for example, we might use &lt;code&gt;Int32&lt;/code&gt; instead of &lt;code&gt;MyType&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We now solve this in a better way: given a Haskell type corresponding to a C
type, a binding specification can (and should) specify an &lt;a href=&quot;https://github.com/well-typed/hs-bindgen/blob/main/manual/low-level/translation/ffi-types.md&quot;&gt;“FFI
type”&lt;/a&gt; for that Haskell type: an FFI type is a pair of a module
&lt;code&gt;M&lt;/code&gt; and type &lt;code&gt;F&lt;/code&gt; such that &lt;code&gt;F&lt;/code&gt; is legal to appear in a &lt;code&gt;foreign import&lt;/code&gt; in a
context where &lt;code&gt;M&lt;/code&gt; is in scope. For example, &lt;code&gt;Foreign.C.CSize&lt;/code&gt; is an FFI type,
because &lt;code&gt;CSize&lt;/code&gt; can appear in a &lt;code&gt;foreign import&lt;/code&gt; if &lt;code&gt;Foreign.C&lt;/code&gt; is imported:
&lt;code&gt;Foreign.C&lt;/code&gt; exports all the necessary newtype constructors.&lt;/p&gt;
&lt;p&gt;The translation from &lt;code&gt;MyType&lt;/code&gt; to its FFI type (which might be &lt;code&gt;MyType&lt;/code&gt; itself)
is now also more general; &lt;code&gt;MyType&lt;/code&gt; just needs to have an instance of the
&lt;code&gt;HasFFIType&lt;/code&gt; type class:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HasFFIType&lt;/span&gt; a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;FFIType&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; a ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  toFFIType   ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;FFIType&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  fromFFIType ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;FFIType&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;FFIType&lt;/code&gt; type instance should line up with the FFI type specified in the
binding spec. In many cases, &lt;code&gt;toFFIType&lt;/code&gt; and &lt;code&gt;fromFFIType&lt;/code&gt; can simply be
&lt;code&gt;coerce&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;As an example, if &lt;code&gt;MyType&lt;/code&gt;’s FFI type is &lt;code&gt;CSize&lt;/code&gt;, then &lt;code&gt;hs-bindgen&lt;/code&gt; will
generate code&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foreign &lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;{- .. -}&lt;/span&gt; foo_wrapper :: &lt;span class=&quot;dt&quot;&gt;CSize&lt;/span&gt; -&amp;gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb7-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;foo ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MyType&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb7-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb7-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; foo_wrapper (toFFIType x)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In general the bindings generated by &lt;code&gt;hs-bindgen&lt;/code&gt; are still specific to the
specified target platform (the host, by default), but the introduction of
FFI types reduces an avoidable source of non-portability.&lt;/p&gt;
&lt;h2 id=&quot;unnamed-declarations&quot;&gt;Unnamed declarations&lt;/h2&gt;
&lt;p&gt;In Haskell, we might define a binary tree with values in the leaves as&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Leaf&lt;/span&gt; a &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Branch&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt; a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;An analogous definition in C might look something like this:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode c&quot;&gt;&lt;code class=&quot;sourceCode c&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;enum&lt;/span&gt; tag &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt;LEAF&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt; BRANCH&lt;span class=&quot;op&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;struct&lt;/span&gt; BTree &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;enum&lt;/span&gt; tag constr&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;int&lt;/span&gt; value&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;struct&lt;/span&gt; BTree&lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; left&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;struct&lt;/span&gt; BTree&lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; right&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;}&lt;/span&gt; branch&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb9-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;union&lt;/code&gt; is &lt;a href=&quot;https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html&quot;&gt;anonymous&lt;/a&gt;: in C, its fields are accessed as if
they are fields of the parent (&lt;code&gt;struct BTree&lt;/code&gt;). In addition, the inner &lt;code&gt;struct&lt;/code&gt;
is unnamed, although the field of the union in this case &lt;em&gt;does&lt;/em&gt; have a name
(&lt;code&gt;branch&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Haskell does not have anonymous records or anonymous sums, and so we must choose
names. We have a policy in &lt;code&gt;hs-bindgen&lt;/code&gt; to never generate numbered names
(“struct 1”), and instead pick names based on surrounding clues, to avoid the
generated code changing in unpredictable ways when the C headers change. In this
case, the union is named after its first field and its parent:
&lt;code&gt;BTree_anon'value&lt;/code&gt;; similarly the struct is named after its parent and the name
of the field: &lt;code&gt;BTree_anon'value_branch&lt;/code&gt;. The &lt;code&gt;anon'&lt;/code&gt; infix avoids name clashes
with other generated names (C names cannot contain ticks).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tag&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tag&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CUInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;LEAF&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;BRANCH&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tag&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt;{&lt;/span&gt;
&lt;span id=&quot;cb10-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    constr     ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Tag&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ,&lt;span class=&quot;ot&quot;&gt; anon'value ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree_anon'value&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;
&lt;span id=&quot;cb10-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree_anon'value&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;..&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HasField&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;  &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HasField&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;branch&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree_anon'value_branch&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-13&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-14&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HasField&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;  (&lt;span class=&quot;dt&quot;&gt;Ptr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;Ptr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb10-15&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HasField&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;branch&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Ptr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;Ptr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree_anon'value_branch&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb10-16&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-17&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree_anon'value_branch&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree_anon'value_branch&lt;/span&gt;{&lt;/span&gt;
&lt;span id=&quot;cb10-18&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    left  ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ptr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-19&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ,&lt;span class=&quot;ot&quot;&gt; right ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ptr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-20&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb10-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These names are perhaps a bit awkward, but in most cases code never has to deal
with them, because &lt;code&gt;hs-bindgen&lt;/code&gt; generates “indirect” &lt;code&gt;HasField&lt;/code&gt; instances that make
it possible to treat the fields of the union as if they are fields of the
parent, just like in C. Moreover, we have these instances also for &lt;em&gt;pointers&lt;/em&gt;;
this means that when using the &lt;code&gt;OverloadedRecordDot&lt;/code&gt; extension, we can write
Haskell code that processes these C values in a very natural way:&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;toBTree ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ptr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;C.BTree&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;BTree&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb11-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;toBTree bt &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    tag &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; peek bt&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;constr&lt;/span&gt;
&lt;span id=&quot;cb11-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; tag &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;C.LEAF&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Leaf&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; peek bt&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;value&lt;/span&gt;
&lt;span id=&quot;cb11-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;C.BRANCH&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        left  &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; peek bt&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;branch&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;left&lt;/span&gt;
&lt;span id=&quot;cb11-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        right &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; peek bt&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;branch&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;right&lt;/span&gt;
&lt;span id=&quot;cb11-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb11-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Branch&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; toBTree left &lt;span class=&quot;op&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; toBTree right&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;improved-record-dot-support&quot;&gt;Improved record-dot support&lt;/h2&gt;
&lt;p&gt;Although support in GHC for overloaded record &lt;em&gt;update&lt;/em&gt; is sadly &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/overloaded_record_update.html&quot;&gt;not yet as
polished&lt;/a&gt; as overloaded record access, it can
nonetheless be quite helpful when dealing with C code that uses a lot of unions:
unions don’t have a natural representation in Haskell, and &lt;code&gt;hs-bindgen&lt;/code&gt;
therefore encodes them as opaque datatypes.&lt;/p&gt;
&lt;p&gt;To continue the example from the previous section, we can construct a value of
type &lt;code&gt;BTree&lt;/code&gt; as&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HsBindgen.Runtime.Union&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Union&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;btree1 ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;C.BTree&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;btree1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;C.BTree&lt;/span&gt;{&lt;/span&gt;
&lt;span id=&quot;cb12-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      constr     &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;C.BRANCH&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    , anon'value &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Union.zero{&lt;/span&gt;
&lt;span id=&quot;cb12-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          branch &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;C.BTree_anon'value_branch&lt;/span&gt;{&lt;/span&gt;
&lt;span id=&quot;cb12-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;              left  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; nullPtr&lt;/span&gt;
&lt;span id=&quot;cb12-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            , right &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; nullPtr&lt;/span&gt;
&lt;span id=&quot;cb12-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            }&lt;/span&gt;
&lt;span id=&quot;cb12-11&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        }&lt;/span&gt;
&lt;span id=&quot;cb12-12&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb12-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we take advantage of the indirect fields described in the previous section,
we can improve this further:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HsBindgen.Runtime.Struct&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Struct&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;btree2 ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;C.BTree&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;btree2 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Struct.zero{&lt;/span&gt;
&lt;span id=&quot;cb13-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;     constr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;C.BRANCH&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   , branch &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Struct.zero{&lt;/span&gt;
&lt;span id=&quot;cb13-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;         left  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; nullPtr&lt;/span&gt;
&lt;span id=&quot;cb13-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;       , right &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; nullPtr&lt;/span&gt;
&lt;span id=&quot;cb13-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;       }&lt;/span&gt;
&lt;span id=&quot;cb13-10&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb13-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;   }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that &lt;code&gt;OverloadedRecordUpdate&lt;/code&gt; currently also requires &lt;code&gt;RebindableSyntax&lt;/code&gt;;
&lt;code&gt;hs-bindgen-runtime&lt;/code&gt; provides a module &lt;code&gt;HsBindgen.Runtime.Overloading&lt;/code&gt; that
restores the standard environment. See also &lt;a href=&quot;https://github.com/well-typed/hs-bindgen/blob/main/manual/low-level/translation/record-dot-syntax.md#enabling-record-dot-syntax&quot;&gt;Enabling record dot
syntax&lt;/a&gt; in the manual.&lt;/p&gt;
&lt;h2 id=&quot;macros&quot;&gt;Macros&lt;/h2&gt;
&lt;p&gt;Preprocessor macros are used extensively in C development, and so for many
applications it is important they too get translated to Haskell. Macro handling
in &lt;code&gt;hs-bindgen&lt;/code&gt; has been significantly improved since the alpha. Most of those
changes are internal, though they will become user-facing once we release
&lt;a href=&quot;https://github.com/well-typed/hs-bindgen/issues/1003&quot;&gt;&lt;code&gt;hs-bindgen&lt;/code&gt;-as-a-library&lt;/a&gt;, at which point it will also be
possible to substitute your own macro languages, replacing &lt;code&gt;hs-bindgen&lt;/code&gt;’s
default &lt;a href=&quot;https://hackage.haskell.org/package/c-expr-dsl&quot;&gt;&lt;code&gt;c-expr&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The full list of improvements can be found in the
&lt;a href=&quot;https://hackage.haskell.org/package/hs-bindgen-1.0.0.0/changelog&quot;&gt;changelog&lt;/a&gt;; here we will mention just a few. First, we can
now parse declarations that use a mixture of macros we can parse and macros that
we cannot. For example, given&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb14&quot;&gt;&lt;pre class=&quot;sourceCode c&quot;&gt;&lt;code class=&quot;sourceCode c&quot;&gt;&lt;span id=&quot;cb14-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb14-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define DeviceId &lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb14-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define MUST_CHECK __attribute__&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;pp&quot;&gt;warn_unused_result&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb14-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb14-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;int&lt;/span&gt; upgradeFirmware&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;DeviceId did&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt; MUST_CHECK&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where the definition of &lt;code&gt;DeviceId&lt;/code&gt; is parsed by &lt;code&gt;hs-bindgen&lt;/code&gt; but the definition
of &lt;code&gt;MUST_CHECK&lt;/code&gt; is not, we now generate&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb15&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb15-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb15-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;DeviceId&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;DeviceId&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb15-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;upgradeFirmware ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;DeviceId&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CInt&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Second, detection of ambiguous macros (macros that do not have a unique
expansion throughout the translation unit) is improved. For example, given&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb16&quot;&gt;&lt;pre class=&quot;sourceCode c&quot;&gt;&lt;code class=&quot;sourceCode c&quot;&gt;&lt;span id=&quot;cb16-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define A &lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define B A&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define B A&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define X &lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-6&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define Y X&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-7&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#undef X&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-8&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define X &lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-9&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb16-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;#define Y X&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;we generate bindings for &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb17&quot;&gt;&lt;pre class=&quot;sourceCode hs&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb17-1&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb17-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;a ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-2&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb17-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-3&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb17-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-4&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb17-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;b ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-5&quot;&gt;&lt;a href=&quot;https://well-typed.com/blog/rss2.xml#cb17-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;b &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;but not for &lt;code&gt;X&lt;/code&gt; and &lt;code&gt;Y&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, macros can now be defined as part of the input to &lt;code&gt;hs-bindgen&lt;/code&gt;
(&lt;code&gt;--hash-define FOO 1&lt;/code&gt;). Previously such definitions had to be given twice, once
to &lt;code&gt;hs-bindgen&lt;/code&gt; and once to the C compiler via &lt;code&gt;ghc-options: -optc-DFOO&lt;/code&gt;, which
was easy to get wrong and could be hard to debug.&lt;/p&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;Although Haskell’s foreign function interface is good, writing bindings to
large C libraries is nonetheless a laborious process. The “fire and forget”
approach that &lt;code&gt;hs-bindgen&lt;/code&gt; offers takes care of a lot of the legwork. We’re
pretty proud of the fact that &lt;code&gt;hs-bindgen&lt;/code&gt; can handle almost everything that
a C header might throw at it.&lt;/p&gt;
&lt;p&gt;However, for many applications the bindings generated by &lt;code&gt;hs-bindgen&lt;/code&gt; will be a
starting point only: they are a direct translation of the C code, and are
therefore very low-level. We are working on a
&lt;a href=&quot;https://github.com/well-typed/binding-combinators&quot;&gt;&lt;code&gt;binding-combinators&lt;/code&gt;&lt;/a&gt; library that makes this
process easier, but ultimately such high-level bindings are then still written
by hand, albeit assisted by a library. The next big goal for &lt;code&gt;hs-bindgen&lt;/code&gt; is
&lt;em&gt;automatic&lt;/em&gt; generation of high-level bindings, guided by pluggable heuristics.&lt;/p&gt;
&lt;p&gt;It has taken a lot of effort to get here, and we thank
&lt;a href=&quot;https://www.anduril.com/&quot;&gt;Anduril&lt;/a&gt; for sponsoring this work. Within Well-Typed,
the &lt;code&gt;hs-bindgen&lt;/code&gt; team includes or at one point included Armando Santos, Dominik
Schrempf, Edsko de Vries, Finley McIlwaine, Joris Dral, Matthias Heinzel, Oleg
Grenrus, Sam Derbyshire and Travis Cardwell. We have also benefited from
numerous contributions from external contributors in the form of issues and PRs,
for which we are grateful!&lt;/p&gt;
&lt;p&gt;&lt;code&gt;hs-bindgen&lt;/code&gt; 1.0 is &lt;a href=&quot;https://hackage.haskell.org/package/hs-bindgen&quot;&gt;available on Hackage now&lt;/a&gt;; give it a spin and &lt;a href=&quot;https://github.com/well-typed/hs-bindgen/issues&quot;&gt;let us
know&lt;/a&gt; if you have any problems!&lt;/p&gt;
&lt;section class=&quot;footnotes footnotes-end-of-document&quot; id=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;This assumes
a prescriptive binding spec to tell &lt;code&gt;hs-bindgen&lt;/code&gt; to mark the tag as closed,
causing &lt;code&gt;hs-bindgen&lt;/code&gt; to generate a &lt;code&gt;COMPLETE&lt;/code&gt; pragma. See &lt;a href=&quot;https://github.com/well-typed/hs-bindgen/issues/2101&quot;&gt;#2101&lt;/a&gt;.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Thu, 08 Oct 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Tweag I/O: How to set up and run a Nixpkgs team</title>
	<guid isPermaLink="true">https://tweag.io/blog/2026-10-08-running-nixpkgs-team/</guid>
	<link>https://tweag.io/blog/2026-10-08-running-nixpkgs-team/</link>
	<description>&lt;h2 id=&quot;motivation-and-background&quot;&gt;Motivation and background&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/NixOS/nixpkgs&quot;&gt;Nixpkgs&lt;/a&gt; is the largest open source software distribution in the world, maintained by an ever-growing global community with &lt;a href=&quot;https://github.com/NixOS/nixpkgs/pulse?period=monthly&quot;&gt;almost a thousand active contributors every month&lt;/a&gt;.
In Nixpkgs, a package maintainer ensures that the latest version of a given piece of software is packaged and operational.
Most people contributing to Nixpkgs are volunteers, and if you meet some of them at &lt;a href=&quot;https://nixcon.org/&quot;&gt;NixCon&lt;/a&gt; or any of the &lt;a href=&quot;https://nix.ug/&quot;&gt;numerous meetups&lt;/a&gt;, you can tell they’re passionate about building a healthy software ecosystem.&lt;/p&gt;

&lt;p&gt;But volunteering obviously has limits.
As a result, there is usually only one committer who ever looks at a pull request, including those touching critical packages, such as programs in the NixOS boot chain.
And for an outside observer, it can be hard to gauge whether the software distributed is trustworthy.
Without peer review, there is a constant risk that changes might introduce security vulnerabilities – or simply degrade code quality and add friction to the next person’s contribution.&lt;/p&gt;

&lt;p&gt;I’m part of a group at Tweag that has been working on targeted improvements of supply chain security for Nixpkgs since mid-2025.
That naturally involves reviewing pull requests.
At some point it seemed sensible to establish a Tweag-branded team directly inside Nixpkgs, to get notified of package updates.
That way, we thought, users and other maintainers would know who’s taking care of those packages.&lt;/p&gt;

&lt;p&gt;However, when we raised the request with &lt;a href=&quot;https://discourse.nixos.org/t/initial-update-from-the-nixpkgs-core-team/69780&quot;&gt;the then-newly-founded Nixpkgs core team&lt;/a&gt;, it prompted &lt;a href=&quot;https://github.com/NixOS/org/issues/220#issuecomment-3678147410&quot;&gt;a thoughtful re-evaluation of the team policy&lt;/a&gt;:
corporate-affiliated teams are no longer permitted, and team members must be individuals, with or without corporate affiliation.
From a distance, such a turn of events may appear annoying at best.
But actually, it was the opposite: everyone involved immediately agreed that it was the right course of action.
Still, I ended up positively surprised by the outcome.&lt;/p&gt;

&lt;h2 id=&quot;setting-up-the-team&quot;&gt;Setting up the team&lt;/h2&gt;

&lt;p&gt;To kick it off, we drafted an outline of objectives and procedures and posted it as a &lt;a href=&quot;https://github.com/NixOS/nixpkgs/issues/494349&quot;&gt;Nixpkgs issue&lt;/a&gt;.
Our primary goal was straightforward: prevent malicious changes from reaching the repository.
We wouldn’t merge anything ourselves, but would block merging in case of problems.&lt;/p&gt;

&lt;p&gt;Fortunately, any Nixpkgs committer has permission to create GitHub teams under the Nixpkgs organisation.
So we did exactly that, leveraging the fact that the in-code &lt;a href=&quot;https://github.com/NixOS/nixpkgs/pull/450864&quot;&gt;team definitions sync automatically from GitHub&lt;/a&gt;—a piece of automation built by my former colleague Silvan Mosberger (&lt;a href=&quot;https://github.com/infinisil&quot;&gt;@infinisil&lt;/a&gt;) in 2025.&lt;/p&gt;

&lt;p&gt;To manage daily reviews transparently, we set up a &lt;a href=&quot;https://github.com/orgs/NixOS/projects/111&quot;&gt;project board&lt;/a&gt; alongside &lt;a href=&quot;https://github.com/balsoft/security-review-board-management/&quot;&gt;a custom script to keep it updated&lt;/a&gt;.
This allows others to track what we’re reviewing and step in to help.
We maintain a public &lt;a href=&quot;https://github.com/NixOS/nixpkgs/issues/494349#issuecomment-4005099033&quot;&gt;list of tracked packages&lt;/a&gt;, while keeping our internal review checklist in a private location to make it ever so slightly harder for bad actors to game our checks.
The only thing left to do was adding the new team as maintainers of the relevant packages.&lt;/p&gt;

&lt;p&gt;An open, topic-oriented team means anyone can join.
We decided to require commit access for team membership, since that already entails substantial community reputation and a high level of trust.
And we’ve learned that people are genuinely keen to help improve Nixpkgs if given the opportunity!
One volunteer joined the team right away, and another a few months later.
Team members regularly post feedback, and we established a dedicated Matrix channel to discuss best practices and review standards.&lt;/p&gt;

&lt;p&gt;Since then we’ve rotated some members, but keep going at a steady pace.
Between March and September 2026, we &lt;a href=&quot;https://github.com/NixOS/nixpkgs/pulls?q=is%3Apr+team-review-requested%3ANixOS%2Fsecurity-review+created%3A%3C2026-09-15+&quot;&gt;reviewed more than 250 pull requests&lt;/a&gt;, that is, everything that touched the packages in question.&lt;/p&gt;

&lt;p&gt;Is Nixpkgs more secure now? We haven’t discovered outright malicious code or active zero-day vulnerabilities.
That said, we did catch some genuine mistakes (such as an incorrectly applied patch) and corrected some code quality issues.
And we’re on the watch.&lt;/p&gt;

&lt;h2 id=&quot;things-to-keep-in-mind&quot;&gt;Things to keep in mind&lt;/h2&gt;

&lt;p&gt;Starting a team in Nixpkgs proved surprisingly straightforward.
I was initially hesitant about opening the team to everyone, as I expected that the work – reading through code and checking details – might be too boring or mundane for volunteers to do in their free time.
To my surprise, some individuals were eager to join and contribute.&lt;/p&gt;

&lt;p&gt;When running a community team, I recommend keeping a few practical considerations in mind:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;People you don’t know may want to join, so be prepared to welcome outside interest and integrate new reviewers.&lt;/p&gt;

    &lt;p&gt;Exercise judgement, but lean on existing trust networks and procedures.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Volunteer availability fluctuates, so expect that you will need to send gentle reminders to get things moving forward.&lt;/p&gt;

    &lt;p&gt;Try to find out early on what works best for the team.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Everyone is at their best if they can focus on one thing at a time.&lt;/p&gt;

    &lt;p&gt;Be ready to provide support: organise tasks, facilitate exchange, and keep everyone in the loop.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;take-responsibility&quot;&gt;Take responsibility!&lt;/h2&gt;

&lt;p&gt;If you want to contribute public good to Nixpkgs, setting up a team is one of the most effective steps you can take.
It is easy to start, fosters constructive discussions, and brings in fresh ideas from passionate people facing the same challenges.
If you are interested in security reviews specifically or want to see how we operate, take a look at our &lt;a href=&quot;https://github.com/NixOS/nixpkgs/issues/494349&quot;&gt;Nixpkgs issue&lt;/a&gt; and consider joining the effort.&lt;/p&gt;</description>
	<pubDate>Thu, 08 Oct 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Mark Jason Dominus: Maybe a better way to track the release history of a Git repo</title>
	<guid isPermaLink="false">tag:,2026:/prog/git/history-proposal</guid>
	<link>https://blog.plover.com/prog/git/history-proposal.html</link>
	<description>&lt;p&gt;&lt;/p&gt;

&lt;p&gt;While I was still at ZipRecruiter, our Git monorepo was developing an
annoying problem: it had too many refs.&lt;/p&gt;

&lt;p&gt;Whenever there was a deployment of any of the applications in the
repo, to staging or to production, the deployment tool would create a
tag object with a name like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;refs/release/stg.www.our-app-2019-09-06T114410.954-0700-treesha-179dab6fd11bee5195f0f8e31cea3edfb86ae23b
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The tag contained the name of the target tier of the deployment (&lt;code&gt;stg&lt;/code&gt; here, for
“staging”), the app name and class of hosts on which it ran,
&lt;code&gt;www.our-app&lt;/code&gt; in this case, the date
and time, and the hash of the tree that was deployed.  This made a
complete record of what was deployed, where, and when.&lt;/p&gt;

&lt;p&gt;After we had been doing this for a while, there were thousands of
these tag objects.  Git is well-designed for handling thousands of
commits, but not for handling thousands of refs.  Perhaps this has
changed, but at the time, Git would store refs in a file called
&lt;code&gt;packed-refs&lt;/code&gt;, which, for each ref, contained an entry with the ref's
name and the ID of the object to which it referred.  &lt;/p&gt;&lt;div class=&quot;margin-note&quot;&gt;(As with all things Git, the actual situation is
somewhat more complicated. These additional ramifications don't affect
the following discussion.)&lt;/div&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Any operation that required resolving a ref name to its object had to
search through the &lt;code&gt;packed-refs&lt;/code&gt; file, which was taking an appreciable
amount of time, far more than was desirable for an operation that
everyone did many times a day. This affected not just local operations
but operations on the Git server as well, and it was clear that it was
only going to get slower as time went on.  Also, the outputs of
commands like &lt;code&gt;git fetch&lt;/code&gt; and &lt;code&gt;git branch&lt;/code&gt; were becoming increasingly
cluttered with the names of tag objects whose only purpose was to
record release history.&lt;/p&gt;

&lt;p&gt;It seemed reasonable to me record the deployment history &lt;em&gt;in&lt;/em&gt; the
repository, but using thousands of tag objects clearly had serious
drawbacks.  In addition to the performance problems, the metadata
itself was purely a convention in the tag name format, not legible to
Git itself. There was a tree ID there, but nothing enforcing the rule
that the tag should refer to a commit with that tree.&lt;/p&gt;

&lt;p&gt;Also, the tag objects themselves seemed poorly-designed for the
purpose.  One good reason for storing history data in the respoitory is to
render it immutable.  But tags aren't immutable!  They can be deleted
and new ones can be inserted at any time, with any names desired.&lt;/p&gt;

&lt;p&gt;And more important: consider the most natural sort of question that
someone might want answered by the release history:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;When did we last deploy the &lt;code&gt;www.our-app&lt;/code&gt; app to production?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The only way to answer this question was to get the &lt;em&gt;full&lt;/em&gt; list of &lt;em&gt;all&lt;/em&gt; tags,
filter it for &lt;code&gt;prod.www.our-app&lt;/code&gt;, then sort by date and take the last item.&lt;/p&gt;

&lt;p&gt;Or similarly: What if the most recent staging deployment had turned
out to be bad, and we wanted to immediately revert to the version
before that?  Again, we (or the reversion process) would have had to
filter and sort a list of thousands of tags, then pick out the
second-last one.&lt;/p&gt;

&lt;p&gt;I proposed this alternative in 2019.  It stores the deployment history
of each app as a series of unnamed but related Git objects, using
Git's own history mechanism, so it's simple, efficient, and meshes
well with Git's commands.&lt;/p&gt;

&lt;p&gt;I don't think it was ever adopted, and it might turn out to have
serious drawbacks I haven't foreseen.  But it might be useful to
someone in a similar situation.&lt;/p&gt;

&lt;hr /&gt;

&lt;h1&gt;Maintaining an audit and reversion trail without thousands of refs&lt;/h1&gt;

&lt;p&gt;Currently, &lt;em&gt;each&lt;/em&gt; time a new version of an app is deployed, we create a ref:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
refs/release/stg.www.our-app-2019-09-06T114410.954-0700-treesha-179dab6fd11bee5195f0f8e31cea3edfb86ae23b
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As these refs proliferate, many operations in the Git repo become slower and slower.&lt;/p&gt;

&lt;p&gt;We can record this information in a way that is at least as useful and which doesn't clutter up the ref list.&lt;/p&gt;

&lt;h2&gt;Current practice&lt;/h2&gt;

&lt;p&gt;At present, the release procedure creates a ref something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A new release is created at commit &lt;em&gt;H&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A new tag object is created, pointing to &lt;em&gt;H&lt;/em&gt;, something like&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
git tag -a refs/release/stg.www.our-app-2019-09-....  H
&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every new tag object has a name in the ref database. This adds a name for every release. The structure is something like this:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;center&quot; src=&quot;https://pic.blog.plover.com/prog/git/history-proposal/current-practice.svg&quot; /&gt;
&lt;span class=&quot;caption&quot;&gt;One ref object per release, each pointing to its own head commit&lt;/span&gt;&lt;/p&gt;

&lt;h2&gt;Alternative proposal&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Each (tier, app) pair is represented by a linked list of release commits&lt;/li&gt;
&lt;li&gt;The head of the list is the most recent release&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One ref&lt;/strong&gt; points to the head of the list&lt;/li&gt;
&lt;li&gt;Older releases can be found by following the links back in time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Main point of this proposal: &lt;strong&gt;There is only one ref for each (tier, app) pair,&lt;/strong&gt; no matter how many releases have been done.&lt;/p&gt;

&lt;p&gt;It will look like this:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;center&quot; src=&quot;https://pic.blog.plover.com/prog/git/history-proposal/alternative-proposal.svg&quot; /&gt;&lt;span class=&quot;caption&quot;&gt;A chain of metainfo commits, each with the previous
metainfo commit as first parent and the actual release commit as
second parent&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;one ref&lt;/strong&gt; (which might or might not be an actual ref object) is
named &lt;code&gt;stg.www.our-app&lt;/code&gt;. That points not to the
actual release commit, but to
a commit that contains
metainformation, such as the treesha. The
metainformation could be stored in any of several ways. (See below.)
The exact time of the release doesn't need to be stored explicitly
because it is exactly the time at which the metainfo
commit was created.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;The metainfo commit has two
parent commits. One is the actual release
commit, the one that, in the current system, is the
target of the named ref object. The other parent is the
metainfo commit from the
&lt;em&gt;previous&lt;/em&gt; release.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The metainfo commits are chained
together exactly the same way Git normally chains commit history, so
the usual Git commands will work on the release history in reasonable
and useful ways. In particular, the command &lt;code&gt;git log
stg.www.our-app&lt;/code&gt; will produce a listing of
the release history.&lt;/p&gt;

&lt;h2&gt;Technical details&lt;/h2&gt;

&lt;p&gt;Creating this sort of two-parent commit is easy. Suppose the ref
&lt;code&gt;stg.www.our-app&lt;/code&gt; points to the current metainfo
commit. And suppose we want to release
new commit &lt;em&gt;H&lt;/em&gt;. We do:&lt;/p&gt;

&lt;p&gt;(Write some useful commit message into &lt;code&gt;/tmp/message.$$&lt;/code&gt;.)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git commit-tree -p refs/release/stg.www.our-app -p &lt;i&gt;H&lt;/i&gt; \
    &lt;i&gt;H&lt;/i&gt;^{tree} &amp;lt; /tmp/message.$$
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates a new metainfo commit
with two parents, given by the arguments of the &lt;code&gt;-p&lt;/code&gt; options. The
first parent is the metainfo commit
for the current release and the second is new commit
&lt;em&gt;H&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;git commit-tree&lt;/code&gt; command creates the new
metainfo commit and writes its SHA on standard
output. Say this SHA is &lt;em&gt;N&lt;/em&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git push origin &lt;i&gt;N&lt;/i&gt;:refs/release/stg.www.our-app
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The release ref now points to the metainfo commit for the new release.&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;p&gt;This will be easier to use than what we have now, because Git's
command set was designed to manage a list of commits linked in this
way; this is exactly how Git normally expects to represent
history. This natural fit with Git's command set shows up in many
places.&lt;/p&gt;

&lt;p&gt;You can fetch a complete release history for any project with a single fetch:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git fetch origin refs/release/stg.www.our-app:HISTORY
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After this, &lt;code&gt;git log HISTORY&lt;/code&gt; will list the entire release history in
reverse chronological order. All the usual git-log options make sense
to filter or search this list. For example, if we want to find the
last release before a certain date, we can use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git log -1 --until=&lt;i&gt;date&lt;/i&gt; HISTORY
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we want a forward-chronological list of release commit SHAs with
their release dates, we could use something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    git log --reverse --format=&quot;%P %cd&quot; HISTORY
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At all times:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The current release commit is accessible at &lt;code&gt;HISTORY^2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The metainfo commit from &lt;em&gt;n&lt;/em&gt; releases ago is at &lt;code&gt;HISTORY~&lt;b&gt;&lt;i&gt;n&lt;/i&gt;&lt;/b&gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The release commit from &lt;em&gt;n&lt;/em&gt; releases ago is at &lt;code&gt;HISTORY~&lt;b&gt;&lt;i&gt;n&lt;/i&gt;&lt;/b&gt;^2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice: if &lt;code&gt;M&lt;/code&gt; is a metainfo commit that describes a release, &lt;code&gt;M^&lt;/code&gt;
is the metainfo commit for the release before that, and &lt;code&gt;M^{2}&lt;/code&gt; is
the exact commit that was actually released.  We don't have to record
the tree IDs in the ref names; the tree that was released in release
&lt;code&gt;M&lt;/code&gt; is &lt;code&gt;M^{2}^{tree}&lt;/code&gt;.  These notations will work as expected in &lt;em&gt;all&lt;/em&gt;
git commands; see &lt;a href=&quot;https://git-scm.com/docs/git-rev-parse#_specifying_revisions&quot;&gt;&lt;code&gt;git-rev-parse&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Because Git naturally understands this way of organizing history, it
will be easy to locate a particular previous release for
rollback. When we roll back, we can roll back to the exact same commit
as before: the new metainfo commit
and the old one can share the exact same release
commit object.&lt;/p&gt;

&lt;p&gt;We can see the differences between the last two releases, without
knowing their exact dates, with something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    git diff HISTORY~1^2 HISTORY^2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The metainfo commit is a full
commit with a full set of Git commit metadata, such as a creation
date. It has a commit message where we can record any unstructured
text that we want. We could also require the commit message to be in
TOML format, or we could use the tooling provided by Git to store
semi-structured information in the commit message; see
&lt;a href=&quot;https://git-scm.com/docs/git-interpret-trailers&quot;&gt;&lt;code&gt;git-interpret-trailers&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If the built-in commit structure is not big enough for everything we
want to record, the metainfo commit
also has an associated tree where we could record any amount of
structured information. In the example above I had its tree be
identical to the release commit's tree,
which might be convenient. (That way, if you wanted to see the files
that were released, you could look in either the release
commit or the metainfo
commit; this would make some of the commands
simpler.) But the tree doesn't have to be the same. We could also add
metainformation files to this tree, or we could commit an entirely
unrelated tree with nothing in it other than structured
metainformation.&lt;/p&gt;

&lt;h2&gt;Performance?&lt;/h2&gt;

&lt;p&gt;Ploni Almoni asks about the performance issues of tracing out a
linked list of commits. But this is exactly the same mechanism that
Git normally uses to record history. When we do &lt;code&gt;git log&lt;/code&gt;, Git is
tracing a linked list of commits in exactly the way suggested here. If
any operation in Git is going to be fast, it will be this one.&lt;/p&gt;

&lt;p&gt;Git currently takes a few seconds to trace the 275,000-commit history
of our monorepo. Tracing the similarly-structured history of a few
hundred release commits is not going to be an issue.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;I wrote this originally, but my only copy was in the
form of a PDF.  I had Claude convert the PDF to Markdown, which is the
native format accepted by my blog, and the two diagrams to SVG.  I
then substantially edited the text and the diagrams. Along the way
Claude pointed out three minor errors, which I
corrected. I also took Claude's advice on how to adjust the colors from the
original to make them more legible to color-blind readers. &lt;/p&gt;&lt;p&gt;Every
word was written by my brain using my fingers.
&lt;/p&gt;</description>
	<pubDate>Tue, 06 Oct 2026 12:00:00 +0000</pubDate>
	<author>mjd@plover.com (Mark Dominus)</author>
</item>
<item>
	<title>Mark Jason Dominus: Recent addenda to articles 202609: Errors, omissions, and retractions</title>
	<guid isPermaLink="false">tag:,2026:/addenda/202609</guid>
	<link>https://blog.plover.com/addenda/202609.html</link>
	<description>&lt;p&gt;I was going to start with “it has been a while since we had one of
these” but then I saw that the previous one started with “Wow, has it
really been 7 months since I did one of these?” and that was in
December 2023. Maybe I can make it a more regular thing.  Anyway
blogging itself has become a more regular thing of late so there is
quite a lot to report just from last month.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href=&quot;https://blog.plover.com/lang/etym/Arabic.html&quot;&gt;In 2007 I described Naomi Wolf as “very
smart”&lt;/a&gt;. I have retracted this description.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href=&quot;https://blog.plover.com/law/crab-cakes.html&quot;&gt;My article from January about crooked policitians and crab cakes&lt;/a&gt;
now mentions that &quot;Johnny Doc” was released from prison on the orders of
Donald J. Trump.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In 2022 I published
&lt;a href=&quot;https://blog.plover.com/lang/etym/kwade.html&quot;&gt;an article about the etymology of a purported Dutch phrase&lt;/a&gt;,
which ended with “This article, which I wrote at 3:00 in the
morning, is filled with many errors, including some that I would not
have made if it had been daytime. Please disbelieve what you have
read, and await a correction.”  I have decided that it is
uncorrectable, and retracted the whole thing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dan Luu pointed out an important omission from
&lt;a href=&quot;https://blog.plover.com/games/sport-flukes-followup.html&quot;&gt;my July article following up on Steph Curry's 2015–16 crushing of the 3PFG record&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In 2021 I argued for &lt;a href=&quot;https://blog.plover.com/misc/job-interview.idea.html&quot;&gt;an interesting replacement for the usual
engineering interview process&lt;/a&gt;.  The coming of LLM technology has
rendered my suggestion completely impractical.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article was written by an opaque and
incomprehensible stoachastic process, namely me.&lt;/p&gt;</description>
	<pubDate>Sat, 03 Oct 2026 14:51:00 +0000</pubDate>
	<author>mjd@plover.com (Mark Dominus)</author>
</item>
<item>
	<title>Haskell Interlude: 87: Edward Kmett</title>
	<guid isPermaLink="false">Buzzsprout-19899290</guid>
	<link></link>
	<description>&lt;p&gt;For the new Haskell Interlude, we sat down with Edward Kmett. Ed is the Founder and Chief Scientist of Positron AI. More importantly, he's a legend in the Haskell community for authoring many, many popular Haskell packages, first and foremost lens - and generally turning up the abstraction level to 11. We talk about design principles for Haskell libraries, category theory, cache-oblivious algorithms and his current work involving Haskell, C++, and FPGAs. Ed talks fast, so strap in!&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
	<pubDate>Fri, 02 Oct 2026 13:00:00 +0000</pubDate>
        <enclosure url="https://www.buzzsprout.com/1817535/episodes/19899290-87-edward-kmett.mp3" length="45011936" type="audio/mpeg"/>
</item>
<item>
	<title>Well-Typed.Com: Memory profiling of large Haskell applications with ghc-debug</title>
	<guid isPermaLink="false">http://www.well-typed.com/blog/2026/10/memory-profiling-large-haskell-applications-with-ghc-debug</guid>
	<link>https://well-typed.com/blog/2026/10/memory-profiling-large-haskell-applications-with-ghc-debug</link>
	<description>&lt;p&gt;&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc-debug&quot;&gt;&lt;code&gt;ghc-debug&lt;/code&gt;&lt;/a&gt; is a debugging tool for performing precise heap analysis of Haskell programs
(for more background, check out our &lt;a href=&quot;https://well-typed.com/blog/2024/04/ghc-debug-improvements/&quot;&gt;previous post improving &lt;code&gt;ghc-debug&lt;/code&gt;&lt;/a&gt; and the &lt;a href=&quot;https://well-typed.com/blog/2025/10/case-study-debugging-space-leak/&quot;&gt;Case Study: Debugging a Haskell space leak&lt;/a&gt;).
While doing some memory profiling for Mercury, we took the opportunity to make some much
needed improvements and quality of life fixes to both the &lt;code&gt;ghc-debug&lt;/code&gt; set of libraries and the
&lt;a href=&quot;https://hackage.haskell.org/package/ghc-debug-brick&quot;&gt;&lt;code&gt;ghc-debug-brick&lt;/code&gt;&lt;/a&gt; terminal user interface.&lt;/p&gt;
&lt;p&gt;The 0.8 releases focused on improving responsiveness and usability on very large memory profiles.
In particular, &lt;code&gt;ghc-debug&lt;/code&gt; now streams intermediate profile results, giving immediate feedback for huge heap traversals.&lt;/p&gt;
&lt;p&gt;To summarise the improvements to &lt;code&gt;ghc-debug-brick&lt;/code&gt;, it now:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;shows partial results, and allows heap traversals to be stopped at any moment without losing existing results;&lt;/li&gt;
&lt;li&gt;implements history navigation, allowing you to switch between heap profile views; and&lt;/li&gt;
&lt;li&gt;improves the decoding of stack frames and shows stack annotations.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;streaming-of-intermediate-results&quot;&gt;Streaming of intermediate results&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;ghc-debug-brick&lt;/code&gt; TUI is the starting point for any memory profiling with &lt;code&gt;ghc-debug&lt;/code&gt;.
It provides useful heap traversal features, for example finding closures with certain properties (via an expressive filter system), summarising all live heap objects, or analysing thunk origins.&lt;/p&gt;
&lt;p&gt;Most of these features need to traverse the full heap to provide a conclusive result.
A summary of all live heap objects cannot be complete until the full heap has been traversed.
This works fine for smaller heaps, but what if the program heap is really big? Say, more than half of system RAM?
The full heap traversal will take a long time, or even worse never finish, as it may use more memory than the system can provide.&lt;/p&gt;
&lt;p&gt;To make &lt;code&gt;ghc-debug&lt;/code&gt; usable with large heaps, we added support for streaming of results as they come in.
No more waiting around: seconds after you start the heap traversal, the first results are shown on the screen.
While you wait for more results, you can interact with the profile in a limited way.
If you think you’ve gathered enough data, you can stop the heap traversal by pressing &lt;code&gt;ESC&lt;/code&gt; and then explore retainers and fields, or start a different heap traversal.&lt;/p&gt;
&lt;p&gt;Note that a stopped traversal can’t be resumed, and you have to start from scratch to find more results.
There are currently no plans to add support for resuming a stopped traversal, as this would be complex to implement and can quickly leak a lot of memory when accumulating multiple partial queries.&lt;/p&gt;
&lt;p&gt;All &lt;code&gt;ghc-debug-brick&lt;/code&gt; heap traversals support this form of streaming/incremental results.
While &lt;code&gt;ghc-debug&lt;/code&gt; can be slow because the heap traversals are slow, it now feels much more responsive and more rewarding to work with large heaps.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Incremental results of a heap traversal&quot; src=&quot;https://well-typed.com/blog/aux/images/ghc-debug-incremental/incremental_profile.gif&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/ghc-debug-incremental/incremental_profile.gif&quot;&gt;Incremental results of a heap traversal&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2 id=&quot;history-navigation&quot;&gt;History navigation&lt;/h2&gt;
&lt;p&gt;Heap traversals are expensive and take time, but &lt;code&gt;ghc-debug-brick&lt;/code&gt; had only one view: the result of the last heap traversal.
This was a constant source of frustration when analysing huge heaps, making it tedious to make progress.
We improved this by adding a history for finished (and partial) heap traversals.
You can view the history by pressing &lt;code&gt;Ctrl+r&lt;/code&gt;, or via the command picker, and see the results of previous queries.&lt;/p&gt;
&lt;p&gt;The history can be navigated with &lt;code&gt;Alt+Left Arrow&lt;/code&gt; to go back in history or &lt;code&gt;Alt+Right Arrow&lt;/code&gt; to go forward again.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;View the history of heap traversals&quot; src=&quot;https://well-typed.com/blog/aux/images/ghc-debug-incremental/history_picker.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/ghc-debug-incremental/history_picker.png&quot;&gt;View the history of heap traversals&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2 id=&quot;stack-decoding&quot;&gt;Stack decoding&lt;/h2&gt;
&lt;p&gt;We improved the stack frame decoding logic of &lt;code&gt;ghc-debug&lt;/code&gt; considerably, adding support for underflow frames, fixing &lt;code&gt;InfoTable&lt;/code&gt; decoding and tidying up the rendering.
Similar to other heap closures, stack frames have source locations (info table provenance entries) attached, which help with identifying where a particular stack frame or heap object was allocated.&lt;/p&gt;
&lt;p&gt;Stack frames themselves can be retainers of heap objects, so improving &lt;code&gt;ghc-debug&lt;/code&gt;’s support for stack frame decoding was much-needed.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Stack Frame decoding of Stack Trace annotated with source location labels&quot; src=&quot;https://well-typed.com/blog/aux/images/ghc-debug-incremental/better_stack_decoding.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/ghc-debug-incremental/better_stack_decoding.png&quot;&gt;Stack Frame decoding of Stack Trace annotated with source location labels&lt;/a&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h3 id=&quot;stack-annotations&quot;&gt;Stack annotations&lt;/h3&gt;
&lt;p&gt;We added support for &lt;a href=&quot;https://well-typed.com/blog/2025/09/better-haskell-stack-traces/&quot;&gt;stack annotations&lt;/a&gt; in supported GHC versions to &lt;code&gt;ghc-debug&lt;/code&gt;.
These allow the user to annotate the native Haskell execution stack with additional metadata, such as
additional context about the current location in the source program.
Naturally, we taught &lt;code&gt;ghc-debug&lt;/code&gt; how to decode stack annotation frames and display them as well.&lt;/p&gt;
&lt;p&gt;As stack annotations are themselves merely heap objects, they are represented as &lt;code&gt;SomeStackAnnotation&lt;/code&gt; closures that you can further drill into.
Unfortunately, the annotations themselves can’t be displayed simply via the usual &lt;code&gt;displayStackAnnotation&lt;/code&gt; rendering function, as this might evaluate thunks and change the Haskell heap.&lt;/p&gt;
&lt;p&gt;Stack annotations help to understand complicated computation flows by adding user-defined instrumentation to the RTS stack.
They were crucial in identifying multiple bugs in &lt;code&gt;ghc-debug&lt;/code&gt;’s stack frame decoding logic.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Stack Frame decoding containing Stack Annotation Frames&quot; src=&quot;https://well-typed.com/blog/aux/images/ghc-debug-incremental/stack_annotations.png&quot; /&gt;
&lt;figcaption&gt;&lt;a href=&quot;https://well-typed.com/blog/aux/images/ghc-debug-incremental/stack_annotations.png&quot;&gt;Stack Frame decoding containing Stack Annotation Frames&lt;/a&gt;&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We hope that the improvements to &lt;code&gt;ghc-debug&lt;/code&gt; and &lt;code&gt;ghc-debug-brick&lt;/code&gt; will aid the
workflows of anyone looking to perform detailed inspections of the heap of their
Haskell processes.
The improvements should especially benefit anyone who works with large heaps.
The new features have all been released to Hackage in &lt;code&gt;ghc-debug-brick-0.8&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This work has been performed in collaboration with &lt;a href=&quot;https://mercury.com/&quot;&gt;Mercury&lt;/a&gt;.
Mercury have a long-term commitment to the scalability and robustness of the Haskell
ecosystem and are supporting the development of memory profiling tools to
aid with these goals.&lt;/p&gt;
&lt;p&gt;Well-Typed are always interested in projects and looking for funding to improve
GHC and other Haskell tools. Please contact &lt;a href=&quot;mailto:info@well-typed.com&quot;&gt;info@well-typed.com&lt;/a&gt; if we
might be able to work with you!&lt;/p&gt;
&lt;section class=&quot;footnotes footnotes-end-of-document&quot; id=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;&lt;code&gt;Frame 0&lt;/code&gt; is the top of the stack, while &lt;code&gt;Frame 23&lt;/code&gt; is the bottom, indicated by the &lt;code&gt;STOP_FRAME&lt;/code&gt; which is always the first stack frame on the GHC RTS stack.&lt;a class=&quot;footnote-back&quot; href=&quot;https://well-typed.com/blog/rss2.xml#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Fri, 02 Oct 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Holden Karau: Yet Another AI Security OSS Externality</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-23427281.post-4551953626375515681</guid>
	<link>http://blog.holdenkarau.com/2026/09/yet-another-ai-security-oss-externality.html</link>
	<description>&lt;h1 dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 6pt; margin-top: 20pt;&quot;&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial,sans-serif; font-size: 20pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;&quot;&gt;Yet Another Rant About AI Security OSS Externality&lt;/span&gt;&lt;/h1&gt;&lt;h1 dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 6pt; margin-top: 20pt;&quot;&gt;&lt;span id=&quot;docs-internal-guid-0942a8c4-7fff-ed45-6330-b7e3535d62cb&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;IMPORTANT DISCLOSURE: This blog post is written in my personal capacity reflecting on the recent Apache Spark patch releases (3.5.9, 4.0.4, and 4.1.3). These are not the views of the Apache Software Foundation (ASF), the Apache Spark project, or any of my employers past or present. Both the ASF and the AI research lab have been provided an opportunity to comment on an earlier draft of this blog post and correct any information they see as incorrect and while many individuals have provided feedback in their personal capacity, there is not (as of yet) any official comment from either.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;AI has fundamentally changed the volume of security reports in all projects, especially OSS projects where the barrier to analysis is even lower. We need to revisit how we handle security disclosures in the age of AI-driven pull requests and security auditing. Open Source Software (OSS) has long been for the community, and while some of us have jobs which give us time to work on open source, it's most often largely a volunteer effort for the public good, with the &lt;/span&gt;&lt;a href=&quot;https://www.apache.org/foundation/&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;ASF focused on Software For the Public Good&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt; since it &lt;/span&gt;&lt;a href=&quot;https://apache.org/history/timeline.html&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;was incorporated in 1999&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;. Security is of the utmost importance and requires greater care in collaboration to avoid accidental disclosure.  For security teams, when working with OSS projects, please be flexible on release timelines when issues are not actively being exploited. If we have to move our release schedule to meet your planned disclosure date, it's more likely to cause us to ship more security issues rather than fewer.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;While some labs have early access programs, in my experience they're not working. Despite there being an agreement for early access to one of the labs' &quot;frontier security models&quot;, we only got access to a scan in August. While a team at that same lab, that we'll call Team X, is, seemingly, using their frontier models from at least February to report security bugs and issue deadlines for resolution and disclosure.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;img height=&quot;173&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEiJHGRXwkwlL-Vg8rU3idkjv7IA6ISTA49zsnyjBCrKOpvNkqE3Ur-T25DXAnH2WzmmVK6SiNlB2oVUxp_P2zqkT7cdjt3OmFdxjLP5kKHrUH2D-2YD8JcwzYf_8_xudUFO1TSYS7RWMOaZkfNSXhrb3da9b3Hs7yDehTF3BW9Rnl_RSmV4FRGw&quot; style=&quot;border: none;&quot; width=&quot;532&quot; /&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;The old model of &quot;here's a bug, &lt;/span&gt;&lt;a href=&quot;https://projectzero.google/vulnerability-disclosure-policy.html&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;you have ~90 days&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt; to fix your issues before we publish how to exploit your code&quot; came about because software vendors, mostly closed source but some open too, were in the habit of ignoring security reports (or being openly hostile to them). Previously security reports took a human to make, and we'd get a few every month, but now with AI our security report inbox is looking more like the promotions tab in my Gmail. Additionally, more bug fixes are landing later in the release candidate cycle from AI driven PRs, so these&lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;&quot;&gt; previously reasonable&lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt; hard security disclosure deadlines are making it more likely we ship worse security bugs while we try and play catch up. To that end, I want to call on all frontier AI labs to put more funding into mitigating the impact of their tools on the OSS projects which are vital to their success.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;img height=&quot;36&quot; src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT0AAAAkCAYAAAD7Ne16AAAQAElEQVR4AeycB5hlVZXv1znn3lu3ck4d6KYj3Ta5CZKDSBJh9PlmTKNv/EyDYfQzfMxjFMMoDM6TmTGAiUFFjAgI0jZIFkVyppvUNLnpnCpXvf9vn7tvnXvq3uqqElHHKs46e+0V99l7nbXDuU147lcvGpmG6T6YjoHpGPhriYHQpv+me2C6B6Z74K+oByac9MIwsPq6Gmtva7ZZMzpt7uwZNn/urGmY7oPpGJiOgT9KDJBjyDXkHHIPOchehr/QRmSlHIjMlctkXaLbffZM62htsYa6WqvKZi0KlS8r6UFHGfA4ZSXwcvA9TglAKwfwAHi+TOPU0+BlKQH4vgRPgeufJN/jlEA5eWjwAI9TAp4GXg48nxJAxpfg5SDJ9zglUE4eGjzA45SAp4GXA8+nBJDxJXg5SPI97sty8tCS/Eo4cmnwsr6En8SppyHJ97gv07K+nuRXwr1ssvSyvoSXxKmnIcn3uC/Tsr6e5FfCvWyy9LK+hJfEqachyfe4L9Oyvp7kV8CjMHS5pqG21uUechAJkJxk/JWz5WnJMiWrzAWlDEiptbnRZs/stIba2lEB0UcrZTDPpwQQ8SV4OUjyPU4JlJOHBg/wOCXgaeDlwPMpAWR8CV4OknyPUwLl5KHBAzxOCXgaeDnwfEoAGV+Cl4Mk3+OUQDl5aPAAj1MCngZeDjyfEkDGl+DlIMn3uC/LyUNL8ivhyKXBy/oSfhKnnoYk3+O+TMv6epJfCfeyydLL+hJeEqeehiTf475My/p6kl8J97LJ0sv6El4Sp56GJN/jvkzL+nqSXwn3somyobbW5SRyU5Gc1C8SE0iSL7xs0stpJceysqmhPqE5jU73wHQPTPfAn0cPkJvIUeSqybZoTNLL53M2o7vdqnLZydqalv8f2wOaHis92zR9ugf+RD1AjiJX5fNVZVpQOWZLkl4um7Gu9tb4vK6MmUqkkeLBV3mJmpq8zZnVXQK7qQ54OjLltUepPMaufI1Kj2JT07FdPNWofY+9su2b/FNNtn0jIyM2NDxkAwMDDoaGhw2af97xyhH1Hv7Gk0nzkEcvTd9Vfao6+NuV7SQf+an6StqZCI4f/E1E1ssgj56vT7Scqg7+JuoDOeSn6gv9chCFoXV1tFpOu9MknzgFkjSPhyMjBHIMHBKGEd82aNrEAWOVpA87eF87/uhDbL+9l5TA/qoDnn74wftZW2uTe1Uq2fJpqBK/En289pXVUZ8MDw3ZoHvZ+/XCp2HAhsQfVkJI6r9i7VMv8Uz4S/rfFY48emXl9MwuFmTb8VUfHBy04aFBW7p4nu2312ILRoZscHDA0s/t5L1eocQP/srxKtGQR68SvxJ9qjr4q2SzHB35qfoqZ288Gn7wN55Mmoc8emn6rupT1cHfrmwn+chP1VfSThoPw0AfW5U7FLMuhlXiB3++niyLK73WliarqsrFsi/TnVVce2vzhKzVVOdt/72X2kTlJ2R0ikLDWtEEennbWxutpbHemhvrilBfW62VsFlvb4+SoRKAkp9pRTRFV396NbV9WM9AggPAR/T89MHAQL/xs4FlSxbZgnlzXfLr6+vT4zJn/+mb/pfRgpeplRonxiUNGoypOZA9p0s5WQvoeCinK55vp/NRQQael6Ok7qCc/C5o5C5y2C7EHNslPZaGTXq5HeWPeNvZ02svbdhU0cOfS+Lj5TcbtqMPP9hec/ShdtzRhxXhxOOOtDe+/nh706knWLeW1SQBEsRUB6tiZ7wSDAWnb/uyPRbYq/aY736LySoWGNBKd+vWrcWWbN26XYm+Xyu9YfuLfN7ik/zlICQDJiJikvHo7+83D9Shw0eOMRkeGtYOZVAQT8jU008LbXCQVbvk3Gp+guNJvGBfOoMOhrQTkG7BAW2gLfBom2/n4MBgiVzsf9A9h5fz5aDaBb9gclIFOYxctisll/QQLgqWm8TL0YoKQsrxy9BWXPcbu/l3dzmQVtlrl4mvjF0tysraKhInqcMA9OzcWVQvhzCzHHrwcrfqY3BJHvGgD7sBZuAAaCPDhQYUgga6hxGtqkY83zsqI+flKUekQ5kEAt6pF1zF+EhJW7y8lx2WX9re399nS13SW2g1+pBFIifpYePe+x+yi3/4U7v08ivt5ltvs0D/QVcTZWasfdomhpKipHxbJAwd8G3wJTRJjl7oSN7zfenkRB8VTGDoJKoOLUdzjMINPlCoFotytCJTSDl+OZpEi1c5fjlaUUGI+Dw741CrM/Gli+a5lTZHDUVYtLtBz2YjTUYDDnp7e621ud5NYC1N9ZqgShMTSWlAK3hk0N1jwVyXfIYVU/I6esn/aEWY+p6E1N/Xa3Nmdzu/s2a0x8cdSoS+rfjv7my15fsstVcv39vmzZmpWBguyMVt4Yhk1owO18alPIOOTyiZdLs6WpysPI5e6bbAKUcT3ecyNbdsWoAeBkFodbW1Ei9cgUoMelDVoFF68DxKaPDBPXgaZQHcCq/A37Gzp0AtX5QkvoJOUTLpyxOheZxyPB14yFTSEZ9A48VHDLj4kh/be/7xw/b+D37UzjjzM7Zi5bWQLZOJbMHuc6ynp8cFzoBWRiSQPgUGg9+rEhoBNaQZbFCzI3zke3p2Gvw+bReHdG5G0IwoCVEiR2AQYABbaeR75KdPNl2iUunpnkZAK8J0jSjZDSl4Bq1P9tHrkb8+6eB/QDOva4/89ukl2bZ1i3sebju2b7ft27ZppTBoYRhaTU2NPfv8i7b6sTVWXV1jGXdgHCivDdsAqw7Z9O3gmfv7B+RXwe1eohHDD/7ohz61Jfk8fdJ18mrPCPKKSPoK+T7Jert9Tq5fdgf1XFpZaIxKIpqx9DRKHgQapQfoHqDBB5I0T6f0UI5fjublKcvxPY0SGXxTeoDuAZr4I3pI+i2fy9qypYtsr1ftYXsvW1KEvYRDV2azXsUGE/WeS+bbMUe8WglloVu196kf6VNMElvY69ckd8iB+zmbC+fPtZ6dO2xAsQlfLhG19Ds/pMTWr/Guq622g/bf2+l2d7TJb69h08Wr7B5/zKF2+KsPcMchc+fMsgMle9Jrj7RMFNiA9JHbqQXF/Lm72Z5LF9tey5a45+FZqHO81a8YKrbD9Ke+cPVU/4gzehV45DLiNkBHXIWUYlVI4YIe1tZUG0iBphemiMVIwZhzGlPG3pFJUqkDCRoPs0RZnYS2RLNWglUWRY4zvrb0mWDSLngS0pbgQfMlOEA9CdASwAvoAwVyVVWVtbd3WveMWXrpq+yqq6+xrVu3wdKEUa2B7zE+nx9z+EF27BEHu9XSaScdY2970ykKgP0VVDutR0HJ+eDBy/eyU044yt4q3kmvOVwBtKfVK5D69GKT7Aig7s42w9ah+rjT0d5ibzjlOHv7355qb1R54H57yt9OO0h2/uZ1r3F2Tjj2cOP8tLe3zwhOnzTw9+oDRv2dWPDXUFft2tMv+f33XWannPRa9yzcDli+j532uuO08pvvEkxba6v9/VveZO982/+2uXNmIyIfQ05/991m2uGHLLfTTj7W3vzGk+21+mC1z7JFFuhogOCmHf0K9FdpJud5lizc3f2w9GS9BG/5X6+zU088xpbpJe3XyzLkzhWHXJKuq6nS8x9ob1A76KfXn3C0HbjfMq2qAyW+AdcGd2MMQXwJDlD3QL0SION54Enw9HSJjKeBJ8HTfQkP3JfgAHUP1CuBZBjPdes32O9+f6fddMutdv2Nt9ivr7/RHnzoYadFzKxfv174sJ143BG2955LLQjiN55E2KtJbURv/ggTqj689WjyO2DfPTWB5aVjNqxDf5IQk5MjcJNf975Tqk4yJC77dI596MH7ixJf0LDHWGNj+T7LrL2t1bZv32HX/Pp6u+yKq+yll9brHamxow49SPTtLm527thhvN9Yue6Gm9wz3XjzrXbr7263J558yoaUgOEVodCOkjo0oEiMER69VjnN1RJ8dYFLfpRhtf+NCwKAk65w2xW/gponk+xOOPZQ94J62nglHcMy2SU+fAPjKeyKP57uOLxcrsoam5qttbXNmpUEGhoazf/xsjLwrGA62lutU7MfM21dXbx65gvwli2brUMfRTgfnKcZrqmxwa2iWpqbbL5WiscraTXU1SiZ9SgodloYmGGru7NdSXO5AqTauavRYDIzn3bycU6vVqswZjUOcF994L6Wr8pIv8cljo62ZhvPX6MS37btW62xvs5mdHc6+9xaW1qsu6vTamvytrNnhw0ODVhXZ4cD2kUS69GLs/eyxUq8eyuJdSuoay2Tybg2L1m8wL18A9pC9WmVsVOriGptmXmeudrqHPbqA6y5qdGiKDL6iNl9zyULbeeOnWp3r+WyoR1/7BFqQ4dWmdW2ceMmV9JPJ732KOMlt8n8VYqJSnRvGz5Qqe7pyTIpn6SPh1fSKdCjKLTNm7faTb+5TcnuFvvVNddp0l1pjBNmr7hyhb2kpFir2OjsaLcXXnjR1q59GpZbvZFAmMCJ015Ncq3NjZrQFqqv+50MyY2xGlHyiwmld58s2RG8askC+W0u6vJzJuKBcSY2Fi+c55S/8e3/thXXXG+36DjkvK+cbyTEtrYW69QEjh0mOcZ+06bNdullV9qVV6+0X117vZLfb+yJNU9bpFhyhugDwFUq3Mrwq5XTSG4VNCys0tK5ErMsPe0kXS+rNHViSeKbiJl0e9L1cjZSMqnqqEZgls1k7cTjj7WGwr9WeV5BRlCxFfOCeXX6ipW/tou+/0M3Q8M76IB9Hbuvr99+cdUK+/aF3zNkeIlDbSOXa/YlQZBQmKERzuVytk4z5Xdl52c/v8J6NWtDb1ayfOjhR+w7F31fNuKtNnR+Y4mN7UpmB+y3FyQXoOX8HajzFhLNtdfdaBd97xIny23ltdfJ7sVq913ajgwYq0HoAMFN27JRqBXaIkj24rp1RtsuvEg6t93uaHU6LlmmM8IeJTzkB7Tth9Hc1GTPPfe8fe/iH6kPrrZt2kpD32PRfNuxY7t7OWZ2dyiBRobOOeeeZ5/+7BftrM+dbVu15X7xxXVaUWdQGYWRUbQilpZJ1ysqToGRtp2ulzOZlinUgyBQX2StRv3Z3NJqbe0dbvJ9x9veYl2amJ5ScrvrnvuNOGFFyErpi+d+2TZt3mzuT28+poa1whvQJMTEfORhBzvWj37yM1eyhSZ+JaqVENKOXLzBI2bzVRk7YL+9NUY9tuJXKx1/hNWjkuWAxrettcmtMF9c95Kt37jZOjo6raOzSwksa4+sfszJd2pB0KcdTb0WBEEQKHZesiVLFtspJ59gy/fb15qamy1fXe0mRKcw0dtIqWDOHcGU0pK1kBk6SRjh6UWgLKCuM5J1sUsueJ7gcUqv73lTLROJz7UFO84+iMDhcpZ6dnHiC36MWak+OgLPS5ZJnde/7gT7xEfeb6e/++/tg+97px12yEFOtFcz530PPOxWbcykjqjbtdfdoOX9Dfbk2mdsjWC3WTPcBdbQ0AAAEABJREFU6kYs++GPf2bX3XCzrXr0cTdrX/nLFZCtu6vDra6YCYd01uaIuv3ghz+xO+++1667/iZ7ZNVqUUzJr88u+NZ/2336yHDFlVcXg5xkS9Lr1KzKahLhtD8SIPRuvTQtmvWff2Gd2vg0JAfMvk+tfVZJZrteuIzb/jiGbmxjepV4Fy2Y656ZhP21879tt952hz286lG7RM923/0PStJs0YJ5tlMrQoKcFwfikLawF3zrQvc8K6+9wVasuAayVVXlrCqXsX6tDHdo6wMxq8BdIBuLFi3UimXIvnjuf9jFP7pUK7/CC41QAZJj5XFKDwWxkgKeJ6TxZN3L+NLzfAkdHAAvB0mexyk9VNIJNRlGWhHntNOo0ao+r4SwcP4823uvVzkV+rChsVFJsc42aNV0862/d3gYRo7vborvIZ0n9+h4ZZ+9lliTdhm3/f4Oe3LNU44dWKB3wqHuRptAKPkxOgmNievIww5yyehnP7/c+jhzQ0gworPYIW1Ha9U2VY34qa9rsCbtjJqaWrQ4aNRKdQss7QhqdPY9YI2FBcMeixfa6e99lx11+CH2hlNPsn94+99ah2IX306hcEvWPU7poSBWLDJaKcLzhDQehlqXQvSAIDilUoSuEXVLXFNFHTSiIgbkALiUALiEnM5OdbSrvww3l/j4HV9bs8zLPzY1oKo4X1TBXRsKdHAAHiUAjlzgkPjm6GN0Cj5ikZI7gcBK6/xvfMcNYiaTlclR+Y0KwLa2DmtpbrV8Pu/OObwBkkNHR5d1dc9wM/dDjzzqWZJvdNs3nyRg8DOf1rZ2a9G2miZCY9VTrY8KzPwE15CCGjpj2dfTW0yw0B5RMkr6wz90oEPnLzmdV9bW1FJ1wAtW39CgM594yzqi2dwxdCNpkZTZnqpqzzz7nPXLd6dm9K6uGWpjWzEx19bWWFU2454HWeAFrdT6B4ass7Pb2js6rE8rBOhAqJkf+/fe95Bt0JYW2hv/5vX24Q+81z7yoffbMUcdbh3avoWRXmg6QsC4AchSAuAajNGYEMHRJQ8dHBBZVa1zoMeVOK7BBU4GngDcg1hOr1iK72OpKCOahHSN2i/yYsUJty8IAjfBhEFoI0ow++y11Ph7cs1ae04fmOrq6o2EWFtbZw2NTdago5eIPkJIXoilgcEBq62usn2VLJlULr3sF1alcXci7G8FI1qxqbUFUhzLfBjr00S0YN5sHYF02eNPPGm/1QTHhIRgEATGCpPJz+vir0o7nVxV3vKK/SqVI/SHFJRsbFirzvUbNsrO7W6yZoFwyY9+6s7yqqvzxjFNryZLkw56gFRVlQfRwFXRkznM3ZwMPAF4EMYjAg4UhAw98JDbVCDulvE1d+zstZ2C8aUmzq2pydvyveNZbldaE2lf2kaJTqJyjbZ8//W1C4wkd/aXzrOPn/Fp++7FP7a+wWFraGoyAigK9TIWDJIEa+vqDAAvkNXnI1atBNPU3OS2KY0KUnQ9X6NiPoF5Wj5f4wK5rr7BCBroDGm96gR8rYLeFHzQh/nCNtDnAos6A56Xv8akP60a4AGBRh+bxTMUEcGr89WWzeUs1DMFAd7E0IU9znGCYJRGG3iORm1N6vXChdHo9hN52iTV+FJQIsPqpKGhSduyqpheuIcKVl6a8795oVbK1xtbYWx0Ktkdd8yR9u53vsVmzeh00onhcfWJ3MbTKceDBkzEdlpmKnrj6dDvQ9oB+I9Jd959j4svtr7ZbE4JptoYt7zGLgji8aFAp1dfS48+4hC3UvvJpZcXJ2rfZs6d07+lG1GCZXIP9VGKn2b1a3X3/R/8WGOWU1yETpWxYfU/pBV8n3Y9EPNKdMRNFEpGzQiCwCVleD2akEPRmcivXnm9nX3uf9q1191kTHQc4WCvo73NnWmP1xfYKgclOoo1vXBFsSQvpMFFThkEYcCzAtNy2FcqlMgDO3f22I2/vbOC1NTIJL6kJn4AT5tM+4o6GpikDU18ZqJZ4Y+V21pt+V5Yt0H9GNrMWbvZ7Nlz3UqtTjNspJk1IW4Zvfi5XNaVDPLmLfHyPggCm7vbLJkOXeDAm7Pb7IIXsw2aAUl8RYIQZtUstrSaRF4kd2WyWReAGS3lA0eR5siw0XbaCykIYn+sEtAF5hS+wMLfqLMXz6MOREo8cV8wsw6LFNeEyIEurQjYwlAnGZG0wyjSMwUK1sBmaytv+uOF2bhpk/oLGyJwqT05JVNWlzwX/QY5hhGLwsjy2ibllJjvufcB++K/fdk+9skz7QeX/ERb+l4nNntGF4/o8Eo3Wgx4fhDsOmaRTetA2xWgA3g5xiJZ9/RkCR/wtCAYv32suOjPLp2JMd7oPfb4GiPh5bI5dwwRhaHKyMWVFf5IIiSzdn1A6NJHMcjvfPtb7Mtf+oJ97CMfoGq1Oi884+Mfspnd7Ubyww/QrzPAPh1lLF40z8UZ8fzpMz9p50n31FNOcrpLdR53xsc+qNX8gDufgziDj2JKOINK0OQWkuLMGd2wdD79kmvfXMX8MUceZvvrHI9dDBMmW2YmPASjKKSoCPQd4AWCoLT/6C/PUwAStA7oD+rhoLYnRYEJIHoVJiA1KkLiu/raW0YJfyB2xz0Pjmthsu3DmOsMkApQpSU6W8kObeM6urq1zerUSq1JAVNnWQVdqIBLqgZKHEEQWhAECsSMEaC9hZnw5BNfoxk5Zzt1iN9QX2vHHXukU920ebNxlhZGkav7WxAELpnwMpn/UwWfYplQT7UgCCyS/urVjytJ9Bl/4/lb+8xzltEWtEfBTYAiv8fiRW411d7abNCSfRPouaIwsvseiMegWgnqFD3PiAKcnyHwT9WW77cPZuzBhx4xZMOo9HlC6YdqZxgmW+5i0iWz44453D7+kdPtfe/+P7Zg4QJD/t77H9DqNQ5z2uQcTOKWfIaJqk1FB9txK8EmDuP5godNElKbxgSrbP+HtKqv0uQQRpH6KITsQF3r4sBVdHOJRElI6LgXHyvYylblMjqOaTa+tA4ND9qI/IyrKCY+1j79rDEZstI7/JADrE9HW9u3bbVlSxbZbrNnulh6WEc5YRjagvlz7SitPI/QR5W8ttkZTej777uXRXqW/v7+wuRvE/6jj5LC4+Y0dVDIwyYV/hi4W86+DImPhPfU08//MZo4rk1WJbXaRrKVq9XKrlqHyqxwMlplMYhBEFoQlL7EQRAY/Ew2a4EG+udXXOl8tOls7h1vfZMOcN9hb/u7N1hjQ4MmnxG77PKrzNsMZM8J6xYE2A0MG6qmrlI6evhElnMbhCv6u+KXllcy59nQe+LJNYjb/Hlz7bRTTrCli+frC2rhZw2OYyZvxirtuefXuTMZ09/iRQvsPf/wVvvAe99hr1MCJHC3K6HzE4QqbXWoS8xd6AcFzJy1uGb6I3BZFaxaHX/pq6+vsw+f/h776D+dbmee8TGrqal2/XT/gw9L+q/rGtY52KA+FvAzD558k1bQjENGyYL4gxbDaH/GdVPcBPaUEtI/f+rz9sF/+oT94wc/au9+/4f0VfwLxt82fRX/6Cf+r61+7HFte3ttj4W72zFHHGz77LlEH5b67YZbfmuf/OfP2Ac+/HF73+kfsXe99wP6GPdT4++ee++3T5xxlovznFbwV1y1wo3R/vrK+653/J29U3H+mmOOQFTb2ButRxN/GEZ2l744M96sPt/+5jfam990qp32+nj1+NDDqyQ/9jlEnPA1bk7TBBDSkAlb+wME/9DE90omvFCrkEizjn/cjJIbyQGADoRRZEEQDw7yDKaXj7S9DQLxdOWyOSNZrnr0CePHmgSvJf6Y2X7440vt6Wdf0BlNvSGPfS+C7zAMLYpCC1VCZ0sayQf1KIxG6VFoVTrTqdN53679PS9/dUq0eavRR5HLf7HC+PkN9oFerf7YGoF7yGhVSKLkea765TV2x513e1axZLb/5re/a8MjgVXLbk4vQxiGjh8EgYVRZKH6F1qUicz/ZYRDe2TVY7bymuuN1QO8bn1lrq+vVwIecP33tFan0KcMf2GKJIe4ySMaqyqH9ut8rUorpCgKLXCU0VsURhYEMTUUznjxcYMPZ3PmzrPZc3a3mbNmW0tLq1MKw9A4Z81p1ci2dkZXh6PfpGSHFXS7Z860ObvPszlzpTtzltvhIMT70KSz3DqdX/MR5elnnreLdRTBD5JzuazVa+LaofNEfpr129vudGfTtJtjFX5fyLvQ1NRos2Z2u/G+86577OqV17kjDuxPFXaV08IdOndzHcsaWlDE0x7Fc/sPyiRP9aKO8CTL4dAAVaaa+FzCW6sVnuwUfcleySXeH9o+b4+Esn1Hj33qs+fYmWd90e5/cJXl9PKyggqCwIp+5BMa8pu2bHPy//KZs+3JNc9YJpu1SEGXzeVckLS2tNk99z5kn/38vxkH9XwUuUAH9p9RfdVjT7ggrKmpdQN+930POr9nff5cJYiMZTWjV2lVdoUSzZlnfcG+/s2LjG1BNpuzvJLcVy+40MmvvPZGrYhqrFZ2WlsL/v51rL/Vjz0pf23GqhX9Gq1eh/Us//mVb9j/O++rdu6Xv2KXX/kr98ybNm9zz/UpPdeap561vLa09Vqdtsj+ZVdcbWfr3O38b1xo53/jO/bV879lZ3/pP6y3b9CaGpuU9KqtWh9ifi65M9WP3/nuJZZ3q7+MS+6PP7FWts8WnKMvedutSs9Yqxfod7ffZZ/5/DnOJn309Qu+bWd97hw3DryEfpz8OBRjQs9Q5IFQ90Ddg2joAM6Gp/tSfEenTNH+IJ2kPexS90Ddg2j4AQKL/8soBn6h8ScmL79ypVWpr0LFlxGPTi9QrETuA9Rlv/iVffYL/253akVVpf6u1bkdvyTo6Oi0Ln055+t5vz7CEavnfvlr1tTUovHIWltLszU2Nrht6iodkWQUw3wwa21tt06Odgq6Dzy42tBlXNGtqanTxF5nxMRTTz9n/37e1+xzXzjXzvuv89243Xb73UaSrdVuqVoTIe154KFVkvmSffM737WvfP2b9i+KaybefL5WH/tqzP2pHxgH+oHSgWMUbgV+ko7sDr27lECSV9Ay9yFj67btvu7KEUmioJVg8QV3NOiAGEW+00CsICGeRCA4KFDdshcdkuzV19xS0Np1ccfdD9qatc+VCDqb8qPL+cCfowlxpRj4UlHUc3T4EBOdlaTHOiN62as0KzVYl87vmCGZzapyeSWx0AL9V9SRvcDMckpsvIz8bKNTOo2avao0c5IQWZUxuzETdnR1WX1Ts23YuMXWrd9k61W2KKAIxPrGRpcQqrV1JkC7u2e4QKtVEiBwCZZWJZqu7pnW3t6pVVq9YRc+543ItyixkvA44G6UH+evcaw/ApjE5bdInM1xoNw1Q2cvFloU5YyfwpAMKemHDvdcJLIalyybtVKgLUEmp+fYbOs2bLYtW3eoz2bEH3iUGPNKyLQF27Svra1DSbnW6C98NzY1WYdeJOw3CScJ1yztGwAAAAqMSURBVNc3FvSbbOPm7bZ+wxbbvGWH0U+8tHWy68eJYVT3u6s4JsnxFSdJ93oiFy/HT+rIqKNpbF0pHnpFBSGeLpFx40+ixcvpSMHZkg+hjpekwwMcw98UYJFWx4w/cUGMtba1uX7MaAcSBDrEVxvDMDTqyDW3tChGOswlpOpa9Xfeckp+eU1Y1bU1WoE1WItiZYbGG3uNir1ItvyXYX7knFO8M365qpwRf+AkK+K4raPDuqXboURYp1V4lVadyDdoh9GuxMrKMC+/A0qsM2bMcnHcpHjEBmNfJ7nWtnZrVLLduGmr7djZF8eBYr6pWTGmuEn3Q7KfXN/Rh0b3FzjqA3TIZX6XILZExRdPCMIOQtNLvFmrFLFEL1iy+E/97RB4DincAunEKJy0ThCzdIerYsy1Q4ecV19z8xh6mrBGs8aawhketoCkjPc0lp7kTL59YRRatWakRg0Ug1VX12DMekEQju2jMLIwiiRfrUFssibp8PIiH4kXBIGFYWhV+SrNok1GUBBo3QqaLg1ya3u7215Ua6CzuZzlslUuoAlYAqxagco2Iqfgq9NM2ajkQMLCHnRKEi7y8HMKQHxDb2zEX5eRiMv54ytzpLYjT5uZjWlfmwKyvqHR8npRSFqNeib/XFWyn83m9Lw11qgAJRHxHCS1Tn6rpxeSRFylpJ/JZl1irlf/0b56JaycniOMIrciwSd07JPseZ58dd4aG2l3p2s3tjs0WZDYXX+onwILLP5j5Cc/vmjF+vE9KNrjnSi1ZwUeOoAV/4IiVko3acQ86IAl/gJxfbWUF3jymBgLw0iJq0qrqXrj5z51igPGIVRcWcEetkIdG0CHT5/Sv4wBdGTDKLKMVozIkMCQwV5OY0Xf337Xffav55xnDz682urq6wxaKN9hGFqo45SMEiO69UpaxAN+iJGMbGbE47d50JqbWzVxdQo6rEWxxLjlFP8cYWATG8QsccZvSNs7upwscsRBpHaa/ngmQGjxCgrPC6GUF0DSBLltTP8FKZ0QSc4INm/eCloCDD9QQtxFxcuXNmis0o6eXhsv8a1RwmOVN1ZzlIIvYJSya8zLj9c+BplBzOaUhPSSMwhhFFmi7yz5V1ZegYIvIBCeUWBkczl3flaroCVwCErO07K5nIIqsiAIXEmgZpUcsrmcgjQT0xR0ccDkXTCSsMIoMspsNmfIZ7KSla+4PVnL5nLOH4FYzp8cKkDMkOcZmdFpT14JP6fnzkkfn9lczqgjE4SRk8/oDK5KWyyCtGi/rtZc8layC6NI5vU8ak9GddqHLdqLPyCSTC7xnJFeHPjZXM61m/6hr2q12q0uJP8wimy8P/ob/njjCz8J6MTAPckZH0caGF+qlOvlJ9M+daSFUWSZbMaNA/0YRuoHxUuJddXDKIrjQ31O/4bRWLkwjCxDX0sGW4wzY0mMMLm0avfBuCITavyKPgr2I9nMFHTDqGDf8ULnO6+xIqkyfj4eMor/AFtOTv6ln1ecVWvrXaPVZ7V0coo5xj8MZdPK/43Xf+QwcllaM60TMrchtH7jZvfvNMEdIMmykNcC3BHj27gDJh3HRweIVcreKyW+0oRXwQhk+XLtB094cP4T9RJUOo6PDlDCTFQYHA1SKAiCQAwJ66I7VElcEFWVDLKAG1zI8pVsH7wwiiyj4M0Ugi6MIoMuC/ElnUAzNttigIB3DNnHLrKUJXTxHD0IJRoI4gtaGIXyl7Ux/lLtw2aoZw2jyMIotFC481GwTR0ZnVOoC1AOjPZlFMwZBbAD4WEUWYCuFf6CQGYCZy8IQjMLzP8hh13AJOfoKqmHCvyM66OMZaKshdGoXTd+TrjMTf3n+DQRKCMyhiSd4jiN0RlDiNXRAdQbXDExvjv/MTr2Lh3HxyxQlCipFKkOkQ7tC4LQ6PMg2b9p51KA7+TUl+ilRZx/8ULZcbIqGT8SHbsaJplctsrgyVzhGm0fdK/rmIX24ce1UfbCKLIwAsKUHadhtA8bbowL40vdAm3XMRSLjb3Ll2s/zQEKEvzchhxWqJYUbHvRkSrhayFcX+HHt0PahzufxCbgDVN6cEq6+XqyTOpIxNmCDw6AJ2DHznjF95LOuPhN30OrHrc77nrQjZXTLaPj6PgBsJWWSdeR8ZDUScqBA14uWVbSQWY8naQesh7QMTF9PVmK7J7PyejmeULd5evJciI6aX/oANjBMKWHdN3TKZM6STlwAJk0TFUnqZe0WckPMkmdpBw4gEwapqqT1EvarOQHmaROUg4cQCYNU9VJ6iVtpv0oNkhCJKBcLmuRJptICSsQ3cUiuugA4GkIxICmwl3ggKvoBp4GdADoEin6KVeH5iGpU9AjZ7340gZqLqn5fOZL5VFHdwK6hTBUuoul4fMvvmT88LHYCMdJ3WiAI5W5VeJBB8qokPhu/M0d9kt94HjokSdKJSrovJLtc75KWzVa+3NuH20DRls7ikEHRimjWCU6EpV40AFk0jAefaq8tA9ffzntYQvwtpMldCBJ83glOvxKPOgAMmkYjz5VXtqH6oGSXBCERqlq6YUfoJQa18ajT5UXWx57T9kjV5Gz+voGShJbUjGZ46CH3JLAb1yeef5F6+3rT5Kn8ekemO6B6R74s+oBchS5ipw1mYaNSXoos+Ljx7Kbtoz9uAF/GqZ7YLoHpnvgT9kD5CZyFLlqsu0I3Zowuf4DL8B6nbPxY8Mt27ZLTOvKAl0VnblRL7gbQxevwBqVFc3LwfN4SQlDUEIr6Insrkq8cnSpjqsDs5xeJfrk7cX99ErZww9Q6ZnG401FZ9re+ONL/wBT6dup6Izn63+APT5IbNm6zchJ6zds5mnj/k8/G5w0jTp0wehKDyIgYvIik65bt8GeWPOMvaDDQhJgb3+/8T8Y5IuIS2oJBZ8XHL2MPSeaohd1ODxL8Zw8N+gAeBoq0cvYK/pCB0jbop6iF3XK2EPcATqAq6Rulehl7BV9oQOkTLlqij5hnZSes8VtPHqKN2Ff2C3AhHVSvgrqcWAXKwkEeSBJ8jh0wNeTZYo+3b7RzplwX4yqEMVxjX4F4lrpHTpQoBb9UE/QqQLw+Z9L9Pb1GTmH3EMOevGljUZOmlx+wZqs4kcwmvRES14kNMDT+JXztm07bJ2cPv3MCy4JPvbEWnt0DDxVhlZOLkmbvM7qJ54y4NEx/pN2y+FPTbdPfbb6L6L/yo3frmiv5Pjuqi2l/NXq90fV74+6spQ3Pm3yz7RaflZP0k8sP3lfj8rXo5P0hc7qcXTILSQ5cg05h9zDQkszoE9JUy7DkdE8LYxaISsmTMbU+A4ZLOnc1+MSCbjUYoASY6O20/VYZlQvrsdS4EBcG7UBDfB0Sl+n9FCODs0Dckm8XD1JQzZZBwegA+BJgOYBOjilB1/3JXRwD8k6OJDkUQc8jTJZBwegA+BJgOYBOjilB1/3JXRwD8k6OJDkUQc8jdLXPV6uTjQk6V7Wl/AAX/elp1EHknVwADoA7oG6B2hJvFx9su3z8klb+EjWwQHoALgH6h6gJfFyde/Py1F6OUoPnu7loUPzkKyDA0kedcDTKJN1cAA6AB5D/C5D8wAdnNLDaD2Whw7NQ7IODiR51AFP+/8AAAD//1KXr1EAAAAGSURBVAMAJWBu5UJHr5wAAAAASUVORK5CYII=&quot; style=&quot;border: none;&quot; width=&quot;317&quot; /&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;To put it in â€œcorporateâ€� the key asks from this entire post are fairly simple:&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;ul style=&quot;margin-bottom: 0; margin-top: 0; padding-inline-start: 48px;&quot;&gt;&lt;li dir=&quot;ltr&quot; style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;Frontier AI labs should provide real usable access to their frontier models for OSS&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li dir=&quot;ltr&quot; style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;AI labs should be careful in teams using their models in house for security in advance of their own programs for â€œtrustedâ€� access.&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li dir=&quot;ltr&quot; style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;More flexibility on disclosure dates is needed given the impacts of AI&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;li dir=&quot;ltr&quot; style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre;&quot;&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;A pony with attached coffee cup holder^ (and unlimited coffee) (jk)&lt;/span&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;img height=&quot;423&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEi15MFTPGZuSMNHo49IIU5j_VRzmspBTC1tmgQqAaQXQQg5ParjUBwD4mnAFVu7dqRhY5ooP5tdcVHYH5Coj1feLdKiA4500T4JqiY4htTXotKPR9I2cUhtxDmvZz_-iyJ8AAfVUCRpeiAwsX8TU2ku8JbD4Zck-DpLKsubSHfw_XMjW_uU_YNe&quot; style=&quot;border: none;&quot; width=&quot;318&quot; /&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;The core of the issue remains that reviewer bandwidth is the scarcest thing in large open source projects. While tools like CodeRabbit are amazing (big shout-out to them for their OSS plans), they still don't capture everything. Thanks to the increasing frequency of  attempted (and sometimes successful) malicious PRs and takeovers of OSS projects, we can't just leave it to the robots to argue amongst themselves. &lt;/span&gt;&lt;a href=&quot;https://www.intel.com/content/www/us/en/developer/articles/community/maintainer-burnout-a-problem-what-are-we-to-do.html&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;Maintainer burnout is a real security risk&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt; that we as an industry are not prepared to handle.&lt;/span&gt;&lt;/p&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;img height=&quot;354&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEh3P0fXDp36Hx-5UwAkdHspwaiY__kTD_FZ7lcS2vfyGpqELU6kgmr7Aji6VgzVO3W6r6LJNH_EqrRm7Uml7mEKle-0te-GTJY-tHE6k_9wo34pBWX3xCjYQn-OOMqM0BlcfXTkuG_yu-FbpbBJF27mnS1jwXticq8to6X3S9uL5h6jjW8QPK38&quot; style=&quot;border: none;&quot; width=&quot;472&quot; /&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;While these suggestions are only a starting point, the big AI labs can do a number of things to mitigate some of the downsides. One is providing &lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;usable&lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt; non-time locked free accounts to the people whose software they used. For some of the bigger projects, the AI labs have staff who are able to do things like manage releases, but they're often too busy working on internal issues to have the time to work upstream. I think giving engineers something akin to Google's &quot;20% time&quot; (which some jokingly called 120% time) to give back to the projects they're using could help. &lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;I want to double down on the &quot;usable&quot; part. While Anthropic has done better than any of the other AI labs at trying to provide free tooling for OSS developers, their effort still doesnâ€™t do enough. In trying to use my Anthropic provided OSS maintainer account on a recent Spark security issue I tripped its safety guards and eventually ran out of tokens*. In practice, I've found paid inference on open weight models** to be more effective at addressing security issues. Most OSS maintainers, including myself, don't yet have usable direct free access to the frontier security models, meaning we're pushed into a reactive mode rather than pro-active. &lt;/span&gt;&lt;/p&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;img height=&quot;468&quot; src=&quot;https://blogger.googleusercontent.com/img/a/AVvXsEh8sCAdEex-eDvCwmtZHJ8etQKc8n7xxj74xGbSiTyyYrX2st9oTf8BqLaq8Iu2d7QVTm7P4IY5lRzS9ExC2NIAy-abkcrU6klKj-bA0ZewVl6NOiMnLspgIjDsVweTyag52xuA5Axj1CnlWYcuoNNhqxzXFNysqy7AWqKgH4Txm5lZGV8MotWY&quot; style=&quot;border: none;&quot; width=&quot;624&quot; /&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;For me these issues came into focus during the recent ASF Spark 3 and 4 patch releases, &lt;/span&gt;&lt;a href=&quot;https://spark.apache.org/downloads.html&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;available everywhere fine bits can be downloaded&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;. Most impactful was a security issue reported by Team X where because of a combination of a lack of communication in the initial disclosure and lack of flexibility on the surprise deadline once the release managers became aware, we had to scramble for extra reviewers on complex separate security issues.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;Team X reported security issues in the Apache Spark project around February 25th of this year. This report was (thankfully) accompanied by a human-reviewed proposed patch and &lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;no specific deadline in the initial report&lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;, which to be fair is better than we would get with a frontier model alone. So far, so good.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;However, out of band, Team X then gave a vendor who distributes and contributes to Apache Spark (not my current employer) 90 days to fix this, or Team X would disclose the issue publicly. While the vendor requested an initial extension to line up with the planned OSS release schedule, which Team X granted, the &lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; font-weight: 700; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;deadline was not communicated to the broader OSS group, including myself, until much later&lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;While working on the most recent releases, we got roughly 30 post-first-line filter security reports in the week before the first patch release candidate, a few of which turned out to be actionable. Part way through the release cycle, I became aware of the deadline from Team X and reached out to the head of security at the lab to further extend the deadline so we could address the outstanding security issues. Some of these issues were blocked on knowledge of specialized libraries I personally don't have. Initially Team X was unwilling to extend the deadline any further, but when I explained how our &lt;/span&gt;&lt;a href=&quot;https://www.apache.org/legal/release-policy.html&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;72-hour release voting normally works&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;, they were willing to grant one extra week if the vote failed. During this negotiation, the amazing ASF security folks found a reviewer for the complex issue I was wrangling. We were able to get known issues fixed and the release candidate vote thankfully passed, but that outcome was far from certain.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;If we had not been able to find reviewers for the encryption fix, we would have had to ship a partial release with a known broken encryption issue. At the end of the day, Team X has still not published the disclosures as of the end of September, strongly suggesting that they could have given us more time. As a release manager, this is of course frustrating and had personal impacts on my vacation coffee consumption being more necessary rather than enjoyed, but the impacts could have been much worse (not just missing out on limoncello).&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;I am not particularly anti-AI as far as things go. Having worked on &lt;/span&gt;&lt;a href=&quot;https://www.fighthealthinsurance.com/&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;making AI tools to help people appeal health insurance denials&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;, I think there are potential positive cases where AI can help people.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;I'd also love to give a shout-out to my point of contact at the frontier AI lab for helping unravel the game of telephone and getting me that extra week.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;I don't think this problem is unique to Team X, the process is broken and compounding the existing reviewer constraint. Reviewer bandwidth has almost always been the biggest bottleneck in OSS projects, and the decreasing cost to generate code will continue to push against this bottleneck. When issues are discovered in OSS projects that are not actively being exploited, requiring releases from the OSS project with a fixed timeline is counterproductive. Release cycles are likely going to keep getting longer (see &lt;/span&gt;&lt;a href=&quot;https://www.techradar.com/pro/linus-torvalds-says-huge-linux-kernel-updates-are-now-the-status-quo-and-its-all-thanks-to-ai&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;how big the latest Linux RCs are&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;), and we should allow time for this. For the humans driving the robots, when there is a public interest-reason like an active exploit, not just a conference or PR opportunity, for providing a disclosure deadline, make sure to include that in your initial report whenever possible.&lt;/span&gt;&lt;/p&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;I'd like to end by reiterating my request to all of the frontier AI labs: please offer truly usable free plans for OSS developers, be flexible with your disclosure deadlines when no active exploits exist. In addition, there are some amazing engineers at these labs, if you give your staff time to participate in OSS maintenance tasks it could make a big difference. There's a strong history of industry and OSS working together in both ASF and other projects from Spark to PyTorch to the Linux kernel. That and a pony with a coffee cup holder and a coffee, but I'll take more tokens in place of a pony (I still want the coffee though :p).&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;If you're coming to&lt;/span&gt;&lt;a href=&quot;https://communityovercode.apache.org/events/glasgow-2026/&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt; Glasgow for Community over Code&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;, I'll be around and looking to catch up with other OSS folks for their views on how we can try and make the externalities from AI less terrible, &lt;/span&gt;&lt;a href=&quot;https://lists.apache.org/thread/3c7kwkty4zxbbndgg45p2vs3dchkq5ob&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;including chatting with folks at the responsible AI round table&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;, and &lt;/span&gt;&lt;a href=&quot;https://web.cvent.com/event/ac71ce47-2b5f-424c-abfe-5b48255315fb/summary?RefId=XSummary&amp;amp;session=87074527-715c-46b0-b7a1-5b83d433098e&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;talking about how to make PySpark even faster.&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt; For SF based folks I'll be at &lt;/span&gt;&lt;a href=&quot;https://qconsf.com/presentation/nov2026/breaking-down-walls-python-big-data-ai-performance-transpilation-and-llms&quot; style=&quot;text-decoration: none;&quot;&gt;&lt;span style=&quot;color: #1155cc; font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;QConSF in November talking on using AI powered transpilation to make UDFs faster&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;. And for folks regardless of location I'm going to be doing a virtual talk end of October on dealing with AI security reports (I'll post on LI and blog when the registration link is available).&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;P.S.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;I am well aware this is a bit of a â€œdo as I say not as I didâ€� situation as I did report some Yahoo! Mail security vulnerabilities purely through side channels back in University almost two decades ago. But I was young and did not have the resources of a multi-billion dollar company behind me, and I would argue the security implications of unencrypted e-mail passwords are a bit different than data processing software with schema inference driven class instantiation vulnerabilities.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;* I try to use the account for other OSS work, not just security. It is more successful for non-security work.&lt;/span&gt;&lt;/p&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;** I don't currently have enough vRam to run these on my own hardware, but if anyone has a few hundred spare RTX5090s or DGXs and a few 20a circuits give me a call :p &lt;/span&gt;&lt;/p&gt;&lt;p dir=&quot;ltr&quot; style=&quot;line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;&quot;&gt;&lt;span style=&quot;font-family: Arial, sans-serif; font-size: 11pt; font-variant: normal; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;^ Did you know that's a real thing? I mean not the unlimited coffee, but they make coffee cup holders for saddles. In practice since I live in a city and do not have space for a pony let alone the skills, I ride a Tiger 900 (without coffee cup holder) and an LX150 (with coffee cup holder). If the AI labs are reading this: coffee cup holder for motorcycle with unlimited coffee is an acceptable substitute ğŸ˜›&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;</description>
	<pubDate>Tue, 29 Sep 2026 18:20:59 +0000</pubDate>
	<author>noreply@blogger.com (Holden Karau)</author>
</item>
<item>
	<title>Philip Wadler: How we could turn Edinburgh into a real cycling city</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-1919583558318387374</guid>
	<link>https://wadler.blogspot.com/2026/09/how-we-could-turn-edinburgh-into-real.html</link>
	<description>&lt;p&gt; &lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZyUmoYluKeaXQnLBnaA4z29ORnYav1KgwSPpThBWcGhMeJ7QI88SZ-OBPsHg3F2sE-NrnvpYA30NdVyzpzed9boDIYcf3hRU_IvJTaPxwTwsP-JeNqF698PzYd5JDzofWyFatmEUyMQzCAuZWKbtYFc2rR0bZL7yaI7KwZPnLtyCybYj-bE5Z/s1456/99d37910-4300-4cfc-8680-5d5a71170d64_1560x826.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;338&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZyUmoYluKeaXQnLBnaA4z29ORnYav1KgwSPpThBWcGhMeJ7QI88SZ-OBPsHg3F2sE-NrnvpYA30NdVyzpzed9boDIYcf3hRU_IvJTaPxwTwsP-JeNqF698PzYd5JDzofWyFatmEUyMQzCAuZWKbtYFc2rR0bZL7yaI7KwZPnLtyCybYj-bE5Z/w640-h338/99d37910-4300-4cfc-8680-5d5a71170d64_1560x826.webp&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;A series by award-winning engineer Corey Boyle in the Edinburgh Inquirer. Well reasoned and well written. Some of the cited facts blew me away. (&quot;&lt;span style=&quot;color: #363737;&quot;&gt;Road traffic is the single biggest killer of 5 to 29 year olds on earth, and kills around 500 children every day. In 2024, &lt;/span&gt;&lt;a href=&quot;https://www.transport.gov.scot/news/finalised-road-casualty-statistics-for-2024/&quot;&gt;161 people, three of them children&lt;/a&gt;&lt;span style=&quot;color: #363737;&quot;&gt;, died on Scotlandâ€™s roads, up 13% in a year. Twelve were in Edinburgh and the Lothians.&lt;/span&gt;&quot;) Spotted on &lt;a href=&quot;https://buttondown.com/edi.bike/archive/issue-164-28th-sep-26/?ref=edinburghminute.com&quot;&gt;edi.bike&lt;/a&gt;, via the &lt;a href=&quot;https://www.edinburghminute.com/the-edinburgh-minute-monday-28-september-2026/?ref=the-edinburgh-minute-newsletter&quot;&gt;Edinburgh Minute&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;color: #555555;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span&gt;Corey Boyleâ€™s three-part, evidence-rich series for the Edinburgh Inquirer on pushing the city towards a more sustainable future wrapped up this week with some practical approaches that could move the needle.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span&gt;Boyle also had a piece on World Car Free Day&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;a href=&quot;https://www.edinburghnews.scotsman.com/news/opinion/columnists/world-car-free-day-bogota-to-edinburgh-corey-boyle-9093817&quot; rel=&quot;noopener noreferrer nofollow&quot; target=&quot;_blank&quot;&gt;published in The Scotsman&lt;/a&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;this week.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span&gt;As always, itâ€™s been refreshing hearing a new and well-informed voice calling for change - links to all three chapters are below, on the Inquirerâ€™s SubStack:&lt;/span&gt;&lt;/div&gt;&lt;blockquote style=&quot;border: medium; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;&quot;&gt;&lt;/blockquote&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;Part 1&lt;/strong&gt; â€” ğŸŒ� &lt;a href=&quot;https://www.edinburghinquirer.co.uk/p/even-the-worlds-worst-cycle-lane&quot; rel=&quot;noopener noreferrer nofollow&quot; target=&quot;_blank&quot;&gt;Even â€˜the worldâ€™s worst cycle laneâ€™ works&lt;/a&gt;&lt;/div&gt;&lt;blockquote style=&quot;border: medium; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;&quot;&gt;&lt;/blockquote&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;Part 2&lt;/strong&gt; â€” ğŸš— &lt;a href=&quot;https://www.edinburghinquirer.co.uk/p/fewer-commuters-drive-here-than-in&quot; rel=&quot;noopener noreferrer nofollow&quot; target=&quot;_blank&quot;&gt;Fewer commuters drive here than in any other Scottish city&lt;/a&gt;&lt;/div&gt;&lt;strong&gt;Part 3&lt;/strong&gt; â€” ğŸš² &lt;a href=&quot;https://www.edinburghinquirer.co.uk/p/how-we-could-turn-edinburgh-into&quot; rel=&quot;noopener noreferrer nofollow&quot; target=&quot;_blank&quot;&gt;How we could turn Edinburgh into a real cycling city&lt;/a&gt;&lt;/blockquote&gt;&lt;p&gt;Here's a bit from the intro.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;The car was sold to us as freedom. For most of us, it has become the opposite, without us even realising.&lt;/p&gt;&lt;p&gt;There is a name for that. Psychologists call it &lt;a href=&quot;https://www.inderscienceonline.com/doi/abs/10.1504/IJENVH.2023.135446&quot;&gt;motonormativity, or â€œcar brainâ€�&lt;/a&gt;: the unconscious habit of excusing from driving the harm we would never accept from anything else. Once you see it, you see it everywhere.&lt;/p&gt;&lt;p&gt;Dr Patrick Miner at the University of Edinburgh has helped &lt;a href=&quot;https://doi.org/10.1016/j.jtrangeo.2024.103817&quot;&gt;calculate the human cost&lt;/a&gt;. The result? The car has killed somewhere between 60 and 80 million people, around the same as both world wars combined, and it injures roughly 102 million more every year.&lt;/p&gt;&lt;p&gt;Over three parts, I want to take apart the car-centric society we accept as normal, confront Edinburghâ€™s disgrace, its poverty, and show that a cycling city, where the bike is how most people make most journeys, is the healthier, fairer alternative.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Caption to the photo above.&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;span&gt;&lt;i&gt;T&lt;/i&gt;&lt;/span&gt;&lt;em&gt;he same 69 people, and the road each takes: one bus, 69 bikes, 60 cars. Sixty, because that is how many an average 69 people fill: Credit: We Ride Australia, 2012, recreating a City of MÃ¼nster original&lt;/em&gt; &lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;&lt;a href=&quot;https://www.edinburghinquirer.co.uk/p/how-we-could-turn-edinburgh-into&quot; rel=&quot;noopener noreferrer nofollow&quot; target=&quot;_blank&quot;&gt;&lt;/a&gt;</description>
	<pubDate>Mon, 28 Sep 2026 10:15:48 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>Philip Wadler: Stormtoops and Freikorps Again: A timely Time Warp tribute in memory of Tim Curry</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-8409095899966208271</guid>
	<link>https://wadler.blogspot.com/2026/09/stormtoops-and-freikorps-again-timely.html</link>
	<description>&lt;p&gt; &lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;A brilliant, and sadly timely, parody. Spotted via &lt;a href=&quot;https://boingboing.net/2026/09/21/marsh-family-time-warp-tim-curry.html?fbclid=IwY2xjawUnB_pleHRuA2FlbQIxMQBwZG9mBHNydGMGYXBwX2lkATAAAR77sTPZDPA9vxFSA65nToPwRQHZc4PtZ4nIZm3ZIfo7TV3bWB4iaa4srNPtsA_aem_FO3kOFV9jSr-k3Phfjf8dA&quot;&gt;Boing Boing&lt;/a&gt;.&lt;/p&gt;</description>
	<pubDate>Mon, 28 Sep 2026 08:50:05 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>Mark Jason Dominus: L-systems generate weevils, pizza toppings, and matriarchal lineages</title>
	<guid isPermaLink="false">tag:,2026:/misc/fibonacci-word-2</guid>
	<link>https://blog.plover.com/misc/fibonacci-word-2.html</link>
	<description>&lt;p&gt;The Fibonacci sequence is so-called because in Western mathematics is
first appears in connection with a problem from Fibonacci's 1202 book
&lt;em&gt;Liber Abaci&lt;/em&gt;. Fibonacci asks us to consider a rabbit farm where a
pair of baby rabits grows to adulthood in one month, and an adult pair
of rabbits gives birth to a new pair of babies after a month.
Representing an adult pair with &lt;code&gt;a&lt;/code&gt; and a baby pair with &lt;code&gt;b&lt;/code&gt;, we could
write this as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    b → a
    a → ab
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Starting with one pair of baby rabbits, and performing the indicated
substitutions month by month, we get:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    b
    a
    ab
    aba
    abaab
    abaababa
    abaababaabaab
    abaababaabaababaababa
    abaababaabaababaababaabaababaabaab
    abaababaabaababaababaabaababaabaababaababaabaababaababa
    …
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The total number of symbols (and pairs of rabbits) each month gives us
the Fibonacci sequence, as does the total number of &lt;code&gt;a&lt;/code&gt;s, or the total
number of &lt;code&gt;b&lt;/code&gt;s, in each line.&lt;/p&gt;

&lt;p&gt;A structure like this one, in which there are symbols, in this case
&lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, that are repeatedly replaced with strings of symbols, is
called an “L-system”, and this L-system in particular is the “Fibonacci L-system”.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/07-nested-shortlex.html&quot;&gt;Yesterday I happened to mention &lt;em&gt;Zyzzyva&lt;/em&gt;&lt;/a&gt;, which is a genus
of weevils that I can only assume was named by a biologist who wanted
to be last in the dictionary.  When I see a pattern like this I
instantly think of the Fibonacci L-system.  We can use the Fibonacci L-system to generate a sequence
of increasingly long weevil names:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    Yva
    Zva
    Zyva
    Zyzva
    Zyzzyva
    Zyzzyzyzva
    Zyzzyzyzzyzzyva
    Zyzzyzyzzyzzyzyzzyzyzva
    Zyzzyzyzzyzzyzyzzyzyzzyzzyzyzzyzzyva
    Zyzzyzyzzyzzyzyzzyzyzzyzzyzyzzyzzyzyzzyzyzzyzzyzyzzyzyzva
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or pizza toppings:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    eroni
    proni
    peroni
    peproni
    pepperoni
    peppepeproni
    peppepeppepperoni
    peppepeppeppepeppepeproni
    peppepeppeppepeppepeppeppepeppepperoni
    peppepeppeppepeppepeppeppepeppeppepeppepeppeppepeppepeproni
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or family matriarchs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    Grandma
    Grandmam
    Grandmamma
    Grandmammamam
    Grandmammamammamma
    Grandmammamammammamammamam
    Grandmammamammammamammamammammamammamma
    Grandmammamammammamammamammammamammammamammamammammamammamam
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I had to cheat a little here because there's an &lt;code&gt;a&lt;/code&gt; in the middle that
I didn't substitute.  If we don't skip that we get the unpronounceable 
“Grmammandmammamammamma”.&lt;/p&gt;

&lt;p&gt;A quick computer search suggests several other variations, including
both “mammaries” and “titties”, but to prevent this article from
devolving into juvenile sniggering, I'm going to cut the scroll here.&lt;/p&gt;

&lt;p&gt;Amazingly, there was
&lt;a href=&quot;https://blog.plover.com/math/fibonacci-word.html&quot;&gt;a previous article on a closely related topic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Street view of Philadelphia.  In the center is a lamp post with two red and blue Interstate highway shields affixed, indicating that entrances to I-76 West and I-676 East are ahead.&quot; class=&quot;center&quot; src=&quot;https://pic.blog.plover.com/misc/fibonacci-word-2/76676.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I tried to get Claude to help me with this article,
but it said the premise was too stupid so I had to do it
myself.&lt;/p&gt;</description>
	<pubDate>Fri, 25 Sep 2026 16:01:00 +0000</pubDate>
	<author>mjd@plover.com (Mark Dominus)</author>
</item>
<item>
	<title>Mark Jason Dominus: The road to epsilon-zero: “Shortlex” isn't actually a single order</title>
	<guid isPermaLink="false">tag:,2026:/math/ordinals/07-nested-shortlex</guid>
	<link>https://blog.plover.com/math/ordinals/07-nested-shortlex.html</link>
	<description>&lt;p&gt;Previously:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.plover.com/math/epsilon-zero.html&quot;&gt;Ordinal numbers and basic set theory &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/01-nim.html&quot;&gt;Ordinals as nim-heaps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/02-wellfoundedness.html&quot;&gt;Nim always ends, even with infinite ordinals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/03-track-tokens.html&quot;&gt;Infinite Nim as a coin-moving game&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/04-coordinates.html&quot;&gt;Coin-moving games with no coins&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/05-shortlex.html&quot;&gt;Productive programs and well-founded orders&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/06-nim-shortlex.html&quot;&gt;Shortlex order also orders sequences of numbers&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Something tricky happened &lt;a href=&quot;https://blog.plover.com/math/ordinals/05-shortlex.html&quot;&gt;a couple of articles ago&lt;/a&gt; that I
wish now I'd explained more carefully.  The recent articles talk about
“shortlex order”.  But “shortlex order” isn't really just one order.
It's actually a method for turning one order into another, fancier
one.&lt;/p&gt;

&lt;p&gt;The definition I gave said:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The rule to compare two strings in shortlex order is:&lt;/p&gt;
  
  &lt;ol&gt;
  &lt;li&gt;If the strings are different lengths, the shorter one comes first.&lt;/li&gt;
  &lt;li&gt;If they're the same length, compare them lexicographically.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;But there's something hiding in that “lexicographically”.  To compare strings lexicographically,
you have to compare them character-by-character, and that means you
already have to have some idea of the order of the characters
themselves.  If we don't notice that “sort these into lexicographic order” is
ambigous, it's only because a particular standard ordering of the
characters is implicit.&lt;/p&gt;

&lt;p&gt;But lexicographic and shortlex orderings aren't restricted to strings of
characters.  They can be used to extend an ordering on
any kind of thing to an ordering on sequences of that-kind-of-thing.
If the things are characters, they extend the ordering of the
characters to an ordering on strings, which are sequences of
characters. But the things don't have to be characters.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/06-nim-shortlex.html&quot;&gt;In the previous article&lt;/a&gt; we took the ordinary ordering on
finite numbers (note, not characters) and used the shortlex
constructin to produce an ordering on sequences of finite numbers:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Replace any &lt;em&gt;one&lt;/em&gt; sequence of [finite number] coordinates with one
  that is earlier &lt;em&gt;in shortlex order&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The phrase “in shortlex order” there is implicitly assuming the usual ordering
of finite numbers.  If we had decided to start with a different
ordering of finite numbers, the shortlex extension to
sequences would be different.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.plover.com/math/ordinals/05-shortlex.html&quot;&gt;A of couple of episodes ago&lt;/a&gt; we saw an ordering of the positive
numbers that put all the odd numbers before all the even numbers.  Since that basic
ordering puts &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%247%24&quot; /&gt;  before &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%244%24&quot; /&gt;, the shortlex extension of it would
put &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%e2%9f%a81%2c%203%2c%207%e2%9f%a9%24&quot; /&gt; before &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%e2%9f%a81%2c%203%2c%204%e2%9f%a9%24&quot; /&gt;. And because the basic ordering also
put &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%2433333%24&quot; /&gt;  before &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%244%24&quot; /&gt; the shortlex extension would put &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%e2%9f%a81%2c%0a33333%2c%207%e2%9f%a9%24&quot; /&gt; before &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%e2%9f%a81%2c%204%2c%207%e2%9f%a9%24&quot; /&gt;.&lt;/p&gt;

&lt;h2&gt;Digression&lt;/h2&gt;

&lt;p&gt;For wacky fun, put the list of words in the dictionary into a lexicographic order
that is extended from a different alphabetical order than the usual
one!&lt;/p&gt;

&lt;p&gt;For example, suppose the alphabet was in the order &lt;code&gt;QWERTY UIOP ASDF
GHJKL ZXCV BNM&lt;/code&gt;. The dictionary would start with “queer”, then all the
other &lt;code&gt;QU&lt;/code&gt; words up through “quandaries”, finish out the &lt;code&gt;Q&lt;/code&gt; section
with a few non-&lt;code&gt;Qu&lt;/code&gt; words like “Qantas”, and then continue with the &lt;code&gt;W&lt;/code&gt;
words.  The last word in the dictionary wouldn't be “zyzzyva”, it
would be “mnemonics”.&lt;/p&gt;

&lt;p&gt;Is your kid having trouble learning to alphabetize?  Try alphabetizing
this random list into &lt;code&gt;QWERTY&lt;/code&gt; order to help yourself feel
sympathetic:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;criteria dapple duskiest
  flapjack gnawing immerses irk puppet surveyor sweetens&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Remember, ‘c’ is no longer the third letter of the alphabet, it's now
22nd.&lt;/p&gt;

&lt;p&gt;Here they are in &lt;code&gt;QWERTY&lt;/code&gt; order instead:&lt;/p&gt;

&lt;blockquote class=&quot;spoiler&quot;&gt;
irk immerses puppet sweetens
surveyor duskiest dapple
flapjack gnawing criteria
&lt;/blockquote&gt;

&lt;h2&gt;Shortlex isn't an order, it's a method for elaborating a simpler order&lt;/h2&gt;

&lt;p&gt;Say we have some set of things, &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%24&quot; /&gt; and an order relation on &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%24&quot; /&gt;,
which we'll call &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt;.&lt;/p&gt;

&lt;p&gt;We use the symbol “&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt;” rather than “&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5clt%24&quot; /&gt;”  to emphasize that
this is not necessarily the usual &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5clt%24&quot; /&gt; ordering.  We still write
&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24a%5cprec%20b%24&quot; /&gt; to mean that &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24a%24&quot; /&gt;  comes before &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24b%24&quot; /&gt; in whatever order
&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt; represents, and analogously we can write &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24b%20%5csucc%20a%24&quot; /&gt; 
means the same: &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24b%24&quot; /&gt; comes after &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24a%24&quot; /&gt;.&lt;/p&gt;

&lt;p&gt;The notation &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%5e%5cast%24&quot; /&gt; means the set of all finite sequences of
elements of &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%24&quot; /&gt;.  For example, when &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%24&quot; /&gt; is a character set, &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%5e%5cast%24&quot; /&gt; is the
set of strings over that character set.  Elements of &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%24&quot; /&gt; are not at
all the same kind of things as elements of &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%5e%5cast%24&quot; /&gt;.  Elements of
&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%24&quot; /&gt; are something-or-others, maybe melons or something — let's say
melons, melons are fun — and &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24m_1%5cprec%20m_2%24&quot; /&gt; if melon &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24m_2%24&quot; /&gt; is rounder
than melon &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24m_1%24&quot; /&gt;. Elements of &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%5e%5cast%24&quot; /&gt; are sequences of melons.&lt;/p&gt;

&lt;p&gt;If &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24a%24&quot; /&gt; and &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24b%24&quot; /&gt; are two sequences, you can't sensibly ask whether
&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24a%5cprec%20b%24&quot; /&gt; or &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24b%5cprec%20a%24&quot; /&gt;, since it doesn't make to say that one
sequence is rounder than another.  &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt; is meaningful for comparing
melons, not sequences.&lt;/p&gt;

&lt;p&gt;We can't use &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt; to order elements of &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%5e%5cast%24&quot; /&gt;.
But we can lexicographicize &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt; to make a new ordering that makes sense for
&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%5e%5cast%24&quot; /&gt;.  To lexicographically compare two elements of &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%5e%5cast%24&quot; /&gt;, that's two finite
sequences of elements of &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24S%24&quot; /&gt;, whatever they are, do this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
  &lt;li&gt;Compare the &lt;strong&gt;corresponding elements&lt;/strong&gt; of each sequence, using
    &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt;, starting with the first elements&lt;/li&gt;
  &lt;li&gt;If two sequence elements are different at some posision, the sequence with the &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt;-earlier element
    comes earlier&lt;/li&gt;
  &lt;li&gt;If the two elements are the same, move on to the next
    corresponding elements and continue similarly.&lt;/li&gt;
  &lt;li&gt;If one sequence ends before the other, then that one comes earlier. &lt;br /&gt;
    If both end at the same time, the two sequences were exactly
    the same, so neither
    comes earlier.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;This turns &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt; into a lexicographic elaboration of &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt; that makes it
work on sequences.  This is the same transformation that turns an ordering
on characters into the derived ordering for strings.&lt;/p&gt;

&lt;p&gt;We can turn &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%24&quot; /&gt; into an ordering for sequences in another way,
the shortlex way, producing an order I'll write as &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%5e%5cast%24&quot; /&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
  &lt;li&gt;If the sequences are different lengths, the shorter one comes first.&lt;/li&gt;
  &lt;li&gt;If they're the same length, compare them lexicographically as above.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the same transformation that turns the regular ordering on digits (&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%240%5clt%0a1%5c%5cdots%5clt%209%24&quot; /&gt;) into the derived ordering for ordinary base-10 numerals.&lt;/p&gt;

&lt;p&gt;I thought we'd get to &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cepsilon_0%24&quot; /&gt; today, but it seems not.  Here's
a preview of the next article, though: We're not going to stop with
&lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cprec%5e%5cast%24&quot; /&gt;, we're going to go on to &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cleft%28%5cprec%5e%5cast%5cright%29%5e%5cast%24&quot; /&gt; and &lt;img src=&quot;https://chart.apis.google.com/chart?chf=bg,s,00000000&amp;amp;cht=tx&amp;amp;chl=%24%5cleft%28%5cleft%28%5cprec%5e%5cast%5cright%29%5e%5cast%5cright%29%5e%5cast%24&quot; /&gt;.&lt;/p&gt;

&lt;p&gt;
Claude wanted to help with this article, but I wouldn't let it.
&lt;/p&gt;</description>
	<pubDate>Fri, 25 Sep 2026 00:14:00 +0000</pubDate>
	<author>mjd@plover.com (Mark Dominus)</author>
</item>
<item>
	<title>Tweag I/O: Tricorder MCP server: feedback your agent won't forget</title>
	<guid isPermaLink="true">https://tweag.io/blog/2026-09-24-tricorder-mcp-server/</guid>
	<link>https://tweag.io/blog/2026-09-24-tricorder-mcp-server/</link>
	<description>&lt;p&gt;A little over two months ago, we released the first version of
&lt;a href=&quot;https://discourse.haskell.org/t/ann-tricorder-a-new-development-tool-for-haskell-and-llms/14208&quot;&gt;Tricorder&lt;/a&gt;, a Haskell development tool built for
developers of the modern era, where responsive and frictionless developer
experience is king, and AI usage in coding is on the rise. Since then,
we’ve shipped a steady stream of improvements based on user feedback.
One of our most recent releases is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder-mcp&lt;/code&gt;, a Model Context Protocol
(MCP) server that lets AI agents use Tricorder effectively on larger code
bases.&lt;/p&gt;

&lt;p&gt;While Tricorder is primarily developed for human use, it doubles as a bridge
to GHCi for AI agents. This article showcases &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder-mcp&lt;/code&gt; and how it makes
agentic development with Tricorder more reliable.&lt;/p&gt;

&lt;h2 id=&quot;for-the-uninitiated-what-is-tricorder&quot;&gt;For the uninitiated: what is Tricorder?&lt;/h2&gt;

&lt;video controls=&quot;&quot;&gt;
  &lt;source src=&quot;https://www.tweag.io/tricorder_overview.mp4&quot; type=&quot;video/mp4&quot; /&gt;

  Your browser doesn't support HTML video. [Download the video
  here.](tricorder_overview.mp4)
&lt;/video&gt;

&lt;p&gt;Tricorder is a CLI program that runs a lightweight daemon per project, each keeping
a GHCi session warm in the background. Instead of having your agent cold-call
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cabal build&lt;/code&gt; and wait through a slow compile after every edit, it can query that
daemon for the most up-to-date build state. Tricorder uses GHCi to recompile
incrementally as files change, keeping type errors, warnings, test results,
and eval comments available on demand. The daemon does the slow part once and
keeps it warm; the agent just asks what’s true right now.&lt;/p&gt;

&lt;p&gt;Tricorder exposes the build state through both a programmatic CLI interface
and a more human-friendly TUI interface that you might just leave
open in another terminal tab while working on your project.&lt;sup id=&quot;fnref:1&quot;&gt;&lt;a class=&quot;footnote&quot; href=&quot;https://www.tweag.io/rss.xml#fn:1&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;h2 id=&quot;context-matters&quot;&gt;Context matters&lt;/h2&gt;

&lt;p&gt;We’ve offered a &lt;a href=&quot;https://github.com/tweag/tricorder/blob/7e5c682107e0acc7b9d70a36fdf2b0c699e6e2cd/agent-plugins/tricorder/skills/tricorder-cli/SKILL.md&quot;&gt;ready-to-use agent skill&lt;/a&gt; for some time now, and
if you’ve tried it, you might agree it works really well. However, longer
sessions reveal its limitations, as the agent forgets how to use it properly.
This is especially the case when working in large codebases, where a lot of
code needs to be brought into the context window for the agent to do its job
effectively.&lt;/p&gt;

&lt;p&gt;The reason is the dreaded &lt;em&gt;context window&lt;/em&gt;: once the agent decides it needs a
skill, it reads the skill definition &lt;em&gt;once&lt;/em&gt; and reuses that original read
definition for the rest of the session. It is not automatically re-read unless
you prompt the agent to do so. That original read is just more content sitting
in the context window, and like everything else there, it’s subject to
compaction and context bloat. The agent never forgets the skill &lt;em&gt;exists&lt;/em&gt;, but
it can lose track of &lt;em&gt;how to use it&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;MCP tools don’t have this problem. An agent’s harness resends the full list of
available tools—alongside skills and built-in tools—on every single turn, as
concise descriptions the agent invokes directly with the right arguments.
There’s no one-time read to lose to compaction, so tools stay just as reliable
in a two-hour session as in a two-minute one.&lt;/p&gt;

&lt;p&gt;In effect, the agent doesn’t actually know how to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder&lt;/code&gt; CLI
program directly; it’s the MCP server exposing it as a tool that handles that,
turn after turn, without the instructions ever degrading.&lt;sup id=&quot;fnref:2&quot;&gt;&lt;a class=&quot;footnote&quot; href=&quot;https://www.tweag.io/rss.xml#fn:2&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;h2 id=&quot;words-have-meaning&quot;&gt;Words have meaning&lt;/h2&gt;

&lt;p&gt;The skill works by telling the agent how to invoke &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder&lt;/code&gt;: a page of prose
describing subcommands, flags, and output formats that the agent has to read,
remember, and reproduce correctly on every call. Forget &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--wait&lt;/code&gt;, or get a flag
name slightly wrong, and the agent doesn’t fail loudly. It just gets a stale or
subtly wrong answer, or gives up on Tricorder entirely and pulls out &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cabal&lt;/code&gt;
directly, and carries on.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder-mcp&lt;/code&gt; replaces that with typed tools: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Status&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TestResults&lt;/code&gt;,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Restart&lt;/code&gt;, and others, each coupled with a structured schema. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder-mcp&lt;/code&gt;
better surfaces the difference between “Tricorder found build errors” and
“Tricorder itself failed to run”. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test-results&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eval-comments&lt;/code&gt;
commands exit non-zero when they’re reporting errors or failing tests, and the server
still returns that as a normal result rather than an error, so the agent
doesn’t have to guess what a non-zero exit means.&lt;/p&gt;

&lt;h2 id=&quot;show-and-tell&quot;&gt;Show and tell&lt;/h2&gt;

&lt;p&gt;To better illustrate how easy it is for an agent to use Tricorder with this MCP
server, let’s use it in some actual work!&lt;/p&gt;

&lt;p&gt;Picking a simple enough task straight off of Tricorder’s own issue board: the
Tricorder daemon should quit after a period of inactivity. Currently, the
daemon will just hang around after you’re done with it until you happen to
remember to kill it. This is a bit of a problem, seeing as GHCi can gobble up a
sizeable chunk of memory after a few &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:add&lt;/code&gt;s and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:reload&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;I will use Claude Code for this demonstration. The full conversation is
available &lt;a href=&quot;https://gist.github.com/Gipphe/5873166b108e714dabdf94a528655e9e&quot;&gt;in a public Gist&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Our initial prompt looks like this:&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;❯ Currently, the Tricorder daemon will remain running in the background forever
once started, until the user themselves calls &lt;span class=&quot;sb&quot;&gt;`tricorder stop`&lt;/span&gt; or otherwise
kills the process. It would instead be preferable if the daemon would shut
down by itself after a certain amount of idle time.
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I prompt Claude to make the Tricorder daemon shut down after a configurable
period of inactivity. I also ask it to use the Tricorder MCP server to check
the build and test status along the way, and I make sure to mention some
edge-cases that it needs to address in its implementation.&lt;/p&gt;

&lt;p&gt;Claude provides us an overview of its plan, and begins exploring.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;● I'll start by loading the Tricorder MCP tools and checking the current build
  status, then explore the daemon/session architecture.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As Claude explores the repo to figure out how to achieve its goal, it runs
Tricorder through the MCP server, observing the build process for
any issues. It can view the build status itself, and as soon as the
build is finished it can view test results as well.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  Read 1 file, called tricorder 5 times, ran 2 shell commands
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After making a series of changes, Claude can then check in with Tricorder again
to observe the build process. Since Tricorder is running in the background, the
build is probably ready for Claude to check by the time it
gets around to doing so. Claude doesn’t have to wait around for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cabal build&lt;/code&gt;
to finish.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;● Good. Now let's build and check for type errors via Tricorder MCP.

  Read 1 file, called tricorder 5 times, ran 2 shell commands
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Claude can perform various actions using Tricorder as it goes along:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Check test results:&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;● tricorder - test_results (MCP)(wait: true)
  └  atelier-core:test:atelier-test  passed (214) (410ms)
       test/Driver.hs
         Config
           extractNestedConfig
             should use default value for non-object Values:              OK
             should fetch top-level property:                             OK
             ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Inspect daemon logs:&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;● tricorder - log_contents (MCP)
  └  [INFO] Starting tricorder v0.2.1.0 (ac7251c)
     [INFO] Starting session 1
     [INFO] GHCi started (session #1): 0 diagnostics
     [INFO] Running 4 test suite(s)
     ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;View module sources:&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;● tricorder - source (MCP)(modules: [&quot;Data.Time.Units&quot;])
  └  {-# LANGUAGE DeriveDataTypeable #-}
     {-# LANGUAGE GeneralizedNewtypeDeriving #-}
     -- |This module defines types for many useful time periods, as well as
     -- mechanisms for converting between them.
     module Data.Time.Units(
             TimeUnit(..)
           , Attosecond
           , Femtosecond
           , Picosecond
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Restart the daemon entirely, if the need arises:&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;● tricorder - restart (MCP)
  └
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A lot of these operations also support narrowing down the returned result
to reduce the size of the context window filled by Tricorder’s output.&lt;/p&gt;

&lt;p&gt;After a little bit of back and forth, checking the build along the way, Claude
considers its task complete.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;● Implementation is complete and verified (0 diagnostics, all 4 test suites
  passing including the new tests). Summary of changes:

  - Tricorder.Session.IdleTimeout (new): IdleTimeout newtype, defaults to 300s.
  - Session.Config / Session: added idle_timeout_seconds config field
    (default 300), threaded into Session and re-evaluated on every loadSession call
    (i.e. on config reload), per your request.
  ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Further refining Claude’s solution from here is as simple as ever, but
thoroughly sped up by Tricorder’s snappy build loop and output.&lt;/p&gt;

&lt;p&gt;This is a simple example of how you can use Tricorder through the
Tricorder MCP server with Claude or another coding agent. Admittedly, this task
could’ve been achieved without Tricorder just as easily, or by using the
Tricorder skill, but I hope this walkthrough demonstrates the various ways
an agent can use Tricorder to optimise its work.&lt;/p&gt;

&lt;h2 id=&quot;try-it-out&quot;&gt;Try it out!&lt;/h2&gt;

&lt;p&gt;If you don’t have Tricorder itself already, check out
&lt;a href=&quot;https://github.com/tweag/tricorder/blob/7e5c682107e0acc7b9d70a36fdf2b0c699e6e2cd/README.md#installation&quot;&gt;Tricorder’s README on how to get started&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want to try it out with an AI agent like Claude, Copilot, Codex, Cursor or
Grok? Check the guide on
&lt;a href=&quot;https://github.com/tweag/tricorder/blob/7e5c682107e0acc7b9d70a36fdf2b0c699e6e2cd/docs/using-with-coding-agents.md&quot;&gt;using Tricorder with coding agents&lt;/a&gt; for a
full suite of plugins that manage the MCP server binary for you, and can pick
between the MCP server and the skill intelligently.&lt;/p&gt;

&lt;p&gt;To use Tricorder’s MCP server directly without a plugin, see
&lt;a href=&quot;https://github.com/tweag/tricorder/blob/7e5c682107e0acc7b9d70a36fdf2b0c699e6e2cd/tricorder-mcp/README.md#installation&quot;&gt;the official installation instructions for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder-mcp&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Have any feedback or suggestions for Tricorder or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder-mcp&lt;/code&gt;? Let us know
either on &lt;a href=&quot;https://github.com/tweag/tricorder/issues&quot;&gt;the issue board&lt;/a&gt; or on
&lt;a href=&quot;https://discord.com/channels/1174731094726295632/1512407012187832370&quot;&gt;Tricorder’s Discord channel&lt;/a&gt;!&lt;/p&gt;

&lt;h2 id=&quot;the-road-ahead&quot;&gt;The road ahead&lt;/h2&gt;

&lt;p&gt;Next up for Tricorder is further improving and refining the tool’s outputs.
Much care has been given to the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder ui&lt;/code&gt; so far, for human
usage, but there is still a fair amount of work left to improve the
regular CLI interface in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder status&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tricorder test-results&lt;/code&gt;, et al.&lt;/p&gt;

&lt;p&gt;Keep up with Tricorder by starring &lt;a href=&quot;https://github.com/tweag/tricorder&quot;&gt;the repo on GitHub&lt;/a&gt; or through &lt;a href=&quot;https://discord.com/channels/1174731094726295632/1512407012187832370&quot;&gt;the
Discord channel&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot;&gt;

      &lt;p&gt;For more detail, you can watch the in-depth Tricorder presentation given at the
&lt;a href=&quot;https://www.youtube.com/watch?v=vDmyl0ZJRF8&amp;amp;t=6786s&quot;&gt;Haskell Foundation’s “Haskell and AI” workshop&lt;/a&gt;. &lt;a class=&quot;reversefootnote&quot; href=&quot;https://www.tweag.io/rss.xml#fnref:1&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot;&gt;

      &lt;p&gt;I am grossly simplifying the potential uses for MCP servers in this
article. They can be used for many different things, especially when
they define &lt;em&gt;resources&lt;/em&gt; and &lt;em&gt;prompts&lt;/em&gt; on top of the &lt;em&gt;tools&lt;/em&gt; we describe here.
At full scope, it’s helpful to think of MCP servers as AI-oriented APIs
over resources that have a well-defined set of operations you can perform. &lt;a class=&quot;reversefootnote&quot; href=&quot;https://www.tweag.io/rss.xml#fnref:2&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</description>
	<pubDate>Thu, 24 Sep 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>GHC Developer Blog: GHC 10.0.1-alpha1 is now available</title>
	<guid isPermaLink="true">http://haskell.org/ghc/blog/20260918-ghc-10.0.1-alpha1-released.html</guid>
	<link>http://haskell.org/ghc/blog/20260918-ghc-10.0.1-alpha1-released.html</link>
	<description>&lt;h1&gt;GHC 10.0.1-alpha1 is now available&lt;/h1&gt;
&lt;h4 class=&quot;text-muted&quot;&gt;wz1000 - 2026-09-18&lt;/h4&gt;

&lt;p&gt;The GHC developers are very pleased to announce the availability of the
first alpha prerelease of GHC 10.0.1. Binary distributions, source
distributions, and documentation are available at &lt;a href=&quot;https://downloads.haskell.org/ghc/10.0.1-alpha1&quot;&gt;downloads.haskell.org&lt;/a&gt; and
via &lt;a href=&quot;https://www.haskell.org/ghcup/&quot;&gt;GHCup&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;GHC 10.0 will bring a number of new features and improvements, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The default language edition is now &lt;code&gt;GHC2024&lt;/code&gt;
(&lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/pull/632&quot;&gt;GHC Proposal &lt;span&gt;#632&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26039&quot;&gt;#26039&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Type&lt;/code&gt; and &lt;code&gt;Constraint&lt;/code&gt; are now completely distinct types, just as much as
&lt;code&gt;Int&lt;/code&gt; and &lt;code&gt;Bool&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/24279&quot;&gt;#24279&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The new &lt;a href=&quot;https://downloads.haskell.org/ghc/10.0.1-alpha1/docs/users_guide/exts/qualified_strings.html&quot;&gt;QualifiedStrings&lt;/a&gt; language extension
(&lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0723-qualified-strings.rst&quot;&gt;GHC Proposal &lt;span&gt;#723&lt;/span&gt;&lt;/a&gt;) allows string literals to be
qualified using module qualification syntax, so that &lt;code&gt;M.&quot;my string&quot;&lt;/code&gt;
desugars to&lt;code&gt;M.fromString &quot;my string&quot;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The treatment of static forms has been simplified, implementing
&lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0732-simplify-static.rst&quot;&gt;GHC Proposal &lt;span&gt;#732&lt;/span&gt;&lt;/a&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26556&quot;&gt;#26556&lt;/a&gt;). The free term variables of
&lt;code&gt;(static e)&lt;/code&gt; must now be bound at top level.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;-fpolymorphic-specialisation&lt;/code&gt; is now (re)enabled by default (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/23559&quot;&gt;#23559&lt;/a&gt;),&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bytecode can now be written to disk with &lt;code&gt;-fwrite-byte-code&lt;/code&gt; and packaged
into bytecode libraries with &lt;code&gt;-bytecodelib&lt;/code&gt;, which the interpreter can use
to satisfy package dependencies (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26298&quot;&gt;#26298&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Executables can now be properly statically linked with the new
&lt;code&gt;-fully-static&lt;/code&gt; and &lt;code&gt;-static-external&lt;/code&gt; flags (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26434&quot;&gt;#26434&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SIMD support in the AArch64 native code generator (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26536&quot;&gt;#26536&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A new &lt;code&gt;poll&lt;/code&gt; I/O manager, which is now the default in the single-threaded
RTS on all POSIX platforms except macOS (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/merge_requests/9677&quot;&gt;!9677&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Version 2 of the &lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0540-jsem.rst&quot;&gt;GHC Jobserver Protocol&lt;/a&gt; for GHC’s &lt;code&gt;-jsem&lt;/code&gt;
jobserver on Linux and other POSIX platforms (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25087&quot;&gt;#25087&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27253&quot;&gt;#27253&lt;/a&gt;). This is
sadly a necessary breaking change. &lt;code&gt;-jsem&lt;/code&gt; requires cabal-install 3.18 or
later, and is otherwise ignored with a warning, falling back to sequential
compilation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changes to &lt;code&gt;base&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;GHC.Num.{BigNat,Integer,Natural}&lt;/code&gt; are no longer exposed
(&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/359&quot;&gt;CLC &lt;span&gt;#359&lt;/span&gt;&lt;/a&gt;) and GHC internals in &lt;code&gt;GHC.Num&lt;/code&gt; are deprecated
(&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/360&quot;&gt;CLC &lt;span&gt;#360&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Data.List.init&lt;/code&gt; and &lt;code&gt;Data.List.last&lt;/code&gt; now carry an &lt;code&gt;x-partial&lt;/code&gt; warning
(&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/292&quot;&gt;CLC &lt;span&gt;#292&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The minimum bootstrap compiler is now GHC 9.10, LLVM versions up to 23 are
supported (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26813&quot;&gt;#26813&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27764&quot;&gt;#27764&lt;/a&gt;), and the Debian 9, Debian 10, Ubuntu 18.04
and Ubuntu 20.04 bindists have been dropped in favour of a new Debian 13
bindist (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/25876&quot;&gt;#25876&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;… and many more&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A full accounting of changes can be found in the &lt;a href=&quot;https://downloads.haskell.org/ghc/10.0.1-alpha1/docs/users_guide/10.0.1-notes.html&quot;&gt;release notes&lt;/a&gt;.
See the &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/10.0&quot;&gt;migration guide&lt;/a&gt; for guidance on migrating programs to this release.
As always, GHC’s release status, including planned future releases, can be
found on the GHC Wiki &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/wikis/GHC-status&quot;&gt;status&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Given the many changes in this major release, we would very much appreciate
testing on downstream workloads.&lt;/p&gt;
&lt;p&gt;GHC development is sponsored by:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://juspay.com/&quot;&gt;Juspay&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://qbaylogic.com/&quot;&gt;QBayLogic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.channable.com/&quot;&gt;Channable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://haskell.foundation/&quot;&gt;Haskell Foundation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://serokell.io/&quot;&gt;Serokell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://well-typed.com/&quot;&gt;Well-Typed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.tweag.io/&quot;&gt;Tweag&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.dotcom-monitor.com/&quot;&gt;Dotcom-Monitor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.loadview-testing.com/&quot;&gt;LoadView&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://webhostingbuddy.com/&quot;&gt;Web Hosting Buddy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.findmyelectric.com/&quot;&gt;Find My Electric&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.sc.com&quot;&gt;Standard Chartered&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://upcloud.com&quot;&gt;UpCloud&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mercury.com&quot;&gt;Mercury&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We would like to thank these sponsors and other anonymous contributors
whose on-going financial and in-kind support has facilitated GHC maintenance
and release management over the years. Finally, this release would not have
been possible without the hundreds of open-source contributors whose work
comprise this release.&lt;/p&gt;
&lt;p&gt;As always, do give this release a try and open a &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/new&quot;&gt;ticket&lt;/a&gt; if you see
anything amiss.&lt;/p&gt;</description>
	<pubDate>Fri, 18 Sep 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Tweag I/O: Restoring type class support in Liquid Haskell</title>
	<guid isPermaLink="true">https://tweag.io/blog/2026-09-17-lh-type-classes/</guid>
	<link>https://tweag.io/blog/2026-09-17-lh-type-classes/</link>
	<description>&lt;p&gt;Over the last months, I’ve participated in a Google Summer of Code internship at
Tweag in collaboration with Haskell.org, with the goal of bringing type class
support back to Liquid Haskell.
In this post, I will share the major insights and contributions that resulted
from this project, walking through the main breaking points that I identified
and their solutions, before briefly discussing the current state of the project
and the limitations that emerged while expanding the test suite.&lt;/p&gt;

&lt;h2 id=&quot;what-is-type-class-support-anyway&quot;&gt;What is type class support, anyway?&lt;/h2&gt;

&lt;p&gt;Ask a Haskell programmer what makes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Monoid&lt;/code&gt; instance well-behaved, and the
answer will certainly involve laws: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&amp;gt;&lt;/code&gt; should be associative and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mempty&lt;/code&gt;
should act as an identity. In Haskell, it is common practice to describe
structures, many of them implemented as type classes, in terms of the properties
that should hold for them.&lt;/p&gt;

&lt;p&gt;However, the responsibility of checking that the instances are well-behaved
usually falls on the developer. This is where Liquid Haskell (LH) can be really
convenient, as it will delegate to an SMT solver the task of proving that the
properties hold for instances of a class.&lt;/p&gt;

&lt;p&gt;That feature used to exist back when LH targeted GHC 8, but as the library moved
on to GHC 9, it broke and its test suite was disabled. During my GSoC 2026
project, I worked on bringing it back; the work is documented in
&lt;a href=&quot;https://github.com/ucsd-progsys/liquidhaskell/pull/2716&quot;&gt;this PR&lt;/a&gt;. I am thrilled
to say that type classes are once again supported by LH, and that the test suite
has been re-enabled.&lt;/p&gt;

&lt;p&gt;Most of the machinery for working with type classes isn’t new. It was built
during the GHC 8 era, and credit goes to its original authors: Yiyun Liu, James
Parker and Niki Vazou. What the restoration required was a proper integration
with the new GHC 9 API, and making targeted adjustments that account for recent
developments that have taken place in LH since then.&lt;/p&gt;

&lt;h2 id=&quot;when-a-class-becomes-a-proof-obligation&quot;&gt;When a class becomes a proof obligation&lt;/h2&gt;

&lt;p&gt;Before we dive into the work that was done, let’s take a look at an example
(taken from
&lt;a href=&quot;https://github.com/ucsd-progsys/liquidhaskell/blob/b4c8be10336ea52fc44ea553dac577c87ea8f5ec/tests/typeclasses/pos/All.hs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;All.hs&lt;/code&gt;&lt;/a&gt;
in the test suite):&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kr&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Semigroup&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;cm&quot;&gt;{-@ mappend :: a -&amp;gt; a -&amp;gt; a @-}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mappend&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Semigroup&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;VSemigroup&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;cm&quot;&gt;{-@ lawAssociative :: v:a -&amp;gt; v':a -&amp;gt; v'':a -&amp;gt; {mappend (mappend v v') v'' == mappend v (mappend v' v'')} @-}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lawAssociative&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PNat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;S&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PNat&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Semigroup&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PNat&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;mappend&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Z&lt;/span&gt;     &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;mappend&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;S&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;S&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mappend&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;VSemigroup&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PNat&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lawAssociative&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Z&lt;/span&gt;     &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;lawAssociative&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;S&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lawAssociative&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are a few things worth noticing here. The laws that describe the behaviour
of a class are no longer an agreement invisible to the user, but through
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSemigroup&lt;/code&gt;, we actually have a way of allowing the user to provide a proof to
the specification declared as a method.&lt;/p&gt;

&lt;p&gt;For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PNat&lt;/code&gt;, the body of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lawAssociative&lt;/code&gt; serves as the proof that the law holds
for that instance. The recursive call on the first argument carries out the
induction: LH verifies the base case directly and the recursive call on the first
argument tells the solver which induction hypothesis to use for the successor
case.&lt;/p&gt;

&lt;p&gt;In order to accomplish this, LH mirrors what GHC already does: while GHC
compiles classes into record types, LH will generate a specification of the
respective dictionary; GHC turns methods into record selectors, and LH lifts them
into the refinement logic; and instances are elaborated into evidence by GHC,
whereas LH will check the class’s laws for a concrete type.&lt;/p&gt;

&lt;h2 id=&quot;what-broke-in-the-ghc-8-to-9-migration&quot;&gt;What broke in the GHC 8 to 9 migration&lt;/h2&gt;

&lt;p&gt;In order to do its work, Liquid Haskell hooks into GHC as a plugin: It
consumes GHC’s intermediate representation, calls its API, and even asks GHC to
elaborate expressions on its behalf. This closeness between the two makes their
integration seamless, but also puts pressure on the LH
maintainers to keep up with the changes made to the compiler.&lt;/p&gt;

&lt;p&gt;In particular, the release of the compiler’s version 9 brought many
changes to LH, but the feature to check type classes was left behind,
and thus the corresponding tests were guarded for earlier GHC versions:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;executable typeclass-pos
    ...

    -- Fails on GHC 9.0.1
    if impl(ghc &amp;lt; 9)
      other-modules: ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, the first thing to do was to run the broken tests and identify the errors.
The exploration was not as straightforward, since often an error would only show
up after solving a different error at an earlier stage in the pipeline.&lt;/p&gt;

&lt;p&gt;But looking back, errors can be classified in three buckets:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Name resolution&lt;/strong&gt;: Refinements that mention type class methods failed
because their names weren’t visible to the resolver.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Elaboration of expressions&lt;/strong&gt;: Methods (or laws) elaborated through GHC came
back incomplete, especially in subclasses; e.g. unbound symbols and wrong number
of binders.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Measure generation&lt;/strong&gt;: The generated measures had dictionary constructors of
the wrong shape, producing arity errors and mismatched types in the
constraints.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The way I approached these problems was to use the behavior of GHC 8 as an
oracle: I traced each error to find the pieces of code
where the behaviour diverged from the working implementation, and re-aligned the
two.&lt;/p&gt;

&lt;p&gt;I will address these issues in the same order in the sections that follow.&lt;/p&gt;

&lt;h2 id=&quot;teaching-the-resolver-about-class-methods&quot;&gt;Teaching the resolver about class methods&lt;/h2&gt;

&lt;p&gt;After the migration to GHC 9, Liquid Haskell introduced a significant change: the
&lt;a href=&quot;https://www.tweag.io/blog/2025-02-06-refactoring-lh/&quot;&gt;name resolution module&lt;/a&gt;.
This new phase in the compilation serves as an entry point through which all names
used by LH have to go. However, this new pipeline did not account for type class
methods.&lt;/p&gt;

&lt;p&gt;If you tried to run a test involving type classes after the introduction of name
resolution, even doing something as simple as mentioning an imported class
method in a specification, LH would throw an error. For example, this module
imports &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check&lt;/code&gt; and uses it in the specification of a class law:&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;{-@ LIQUID &quot;--typeclass&quot; @-}&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Example&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Check&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;MyCheck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MyCheck&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MyVCheck&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;cm&quot;&gt;{-@ lawValid :: v:a -&amp;gt; {check v} @-}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lawValid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When building this, Liquid Haskell could not resolve &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check&lt;/code&gt; in the specification:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Unknown logic name `check`
Cannot resolve name
  |
7 |     {-@ lawValid :: v:a -&amp;gt; {check v} @-}
  |                             ^^^^^
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The way to solve this consists of making LH aware of the existence of such class
methods during the name resolution phase. If the lookup in LH’s environment
fails, we can leverage GHC’s environment and check if there is a match for the
name we are interested in. If that is the case, the name can be registered in
the logic, as long as the entry’s parent is a class (thus making sure it is a class
method).&lt;/p&gt;

&lt;p&gt;For the time being, we assume that if the user wants to use class methods in
specifications, they will also want to activate type class checking. Therefore we
show an error message inviting the user to enable type class support:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Unknown logic name `check`
This is a class method, but typeclass support is disabled.
Enable it with {-@ LIQUID &quot;--typeclass&quot; @-}.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;where-did-dictionaries-go&quot;&gt;Where did dictionaries go?&lt;/h2&gt;

&lt;p&gt;When compiling code that uses a type class method, GHC inserts the dictionary
that corresponds to the instance on which it is being called. Similarly, in
order to reach a superclass method, it is necessary to first extract the parent
dictionary. GHC stores that parent dictionary as a field of the subclass
dictionary and generates a selector to retrieve it. For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$p1VSemigroup&lt;/code&gt;
is a compiler-generated selector&lt;sup id=&quot;fnref:1&quot;&gt;&lt;a class=&quot;footnote&quot; href=&quot;https://www.tweag.io/rss.xml#fn:1&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Semigroup&lt;/code&gt; dictionary stored
inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VSemigroup&lt;/code&gt; dictionary, so GHC produces
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mappend ($p1VSemigroup vsemigroupDict) x y&lt;/code&gt; instead of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mappend vsemigroupDict x y&lt;/code&gt;. This applies only to subclasses, since only they
have superclass dictionaries.&lt;/p&gt;

&lt;p&gt;For LH to check a specification referencing these methods, it hands the
refinement to GHC to obtain a Core expression. Therefore, GHC decides what
dictionaries to insert at each location. These dictionaries are the evidence LH
needs: they carry the implementations of methods and, for subclasses, the
dictionaries of their superclasses. However, there were some slight changes in
how those dictionaries are given: in GHC 8, the dictionaries used to be inlined
in the application, whereas GHC 9 uses let-bindings to pass those arguments.&lt;/p&gt;

&lt;p&gt;This was a breaking change, because the previous code was expecting abstract
syntax trees with a
different shape. In particular, the non-inlined dictionary bindings were ignored,
discarding important evidence in the elaborated expressions. The expressions
therefore referred to free variables, resulting in errors such as “Unbound
symbol”.&lt;/p&gt;

&lt;p&gt;The fix, then, was to not discard that evidence when elaborating the
expressions. As with the rest of the restoration, I used GHC 8 as the source of
truth and mimicked the behaviour it previously had: to inline the evidence instead.&lt;/p&gt;

&lt;p&gt;Once the elaborated Core expression is returned by GHC, a function will traverse
the tree to find evidence bindings (which can be easily identified via the GHC
API) and substitute their occurrences by their definitions.
This produces a valid expression with no unbound symbols.&lt;/p&gt;

&lt;h2 id=&quot;counting-fields-on-dictionary-constructors&quot;&gt;Counting fields on dictionary constructors&lt;/h2&gt;

&lt;p&gt;I previously mentioned how classes are transformed into record types: every
class declaration gives rise to the specification of a record that describes
what the shape of the dictionary is, its fields and their respective types. That
description is consumed in several places, most noticeably during measure
generation&lt;sup id=&quot;fnref:2&quot;&gt;&lt;a class=&quot;footnote&quot; href=&quot;https://www.tweag.io/rss.xml#fn:2&quot; rel=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;To use these record types, LH must determine the number of fields each
record has and reconstruct the shape of the corresponding GHC dictionary. This
must account for fields storing superclass dictionaries.&lt;/p&gt;

&lt;p&gt;GHC 9 had some changes on what fields were returned by the
GHC API, and the expected arities started
disagreeing in a few places because superclass fields were missing from the
information LH received. This caused a series of errors and panics: “GHC
gave back more/less binders than I expected”, “Liquid Type Mismatch”, “GHC and
Liquid specifications have different numbers of fields”, and so on.&lt;/p&gt;

&lt;p&gt;Previously, different phases had different counting rules. The solution required
finding the correct rule to decide the arity of constructors, and applying the
adjustment throughout the code base. In particular, it was necessary to always
count superclass dictionaries as fields for class dictionaries, by including the
data constructor’s
&lt;a href=&quot;https://hackage-content.haskell.org/package/ghc-9.14.1/docs/src/GHC.Core.DataCon.html#dataConTheta&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;theta&lt;/code&gt; information&lt;/a&gt;
 – the type class constraints that accompany the constructor. This ensures that
all constraints, especially subclass constraints, are correctly included.&lt;/p&gt;

&lt;p&gt;This was a targeted fix that only executes when working with type classes,
correctly solving the previous errors.&lt;/p&gt;

&lt;h2 id=&quot;known-limitations&quot;&gt;Known limitations&lt;/h2&gt;

&lt;p&gt;With the original tests passing again, I started adding new ones to cover some
scenarios that were missing. That surfaced a few remaining gaps that were already
present in GHC 8. Tickets were opened for each one, with small code examples to reproduce
the errors:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ucsd-progsys/liquidhaskell/issues/2727&quot;&gt;Issue #2727&lt;/a&gt;: For
a class such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;class M m where ret :: a -&amp;gt; m a&lt;/code&gt;, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m&lt;/code&gt; is a higher-kinded
parameter, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m&lt;/code&gt; will be elaborated as if it had kind &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Type&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Type -&amp;gt;
Type&lt;/code&gt;. This triggers a kind error during the verification process.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ucsd-progsys/liquidhaskell/issues/2743&quot;&gt;Issue #2743&lt;/a&gt;: Some
special classes, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Eq&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ord&lt;/code&gt;, and the numeric classes, are embedded
directly in the SMT solver. Their methods are translated into SMT primitives
instead of using the definitions supplied by their instances. As a result, LH
assumes that these instances have the standard behaviour of their classes and
cannot verify laws against their actual implementations.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ucsd-progsys/liquidhaskell/issues/2744&quot;&gt;Issue #2744&lt;/a&gt;: A
class is lifted into the LH logic only when at least one of its methods carries
an LH specification. Without one, its methods cannot be used in refinements,
and its instances are not checked against class specifications. It could be
worth eliminating this requirement to make type class support easier to use,
or documenting better how it is needed.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ucsd-progsys/liquidhaskell/issues/2745&quot;&gt;Issue #2745&lt;/a&gt;: When
a specification uses a class method whose constraints cannot be satisfied, LH
reports an unbound dictionary symbol instead of a clear error resembling
GHC’s error messages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the immediately identified issues. As type class support remains
experimental, we should expect some other issues to be uncovered still by
more tests to come.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;We now have a working test suite that runs the type class group on GHC 9: all
previously working tests pass again, unchanged, showing that the restoration was
done properly. A few more tests were added to check some specific critical
points in the pipeline. Negative tests were included to verify that LH fails
when that is expected behaviour.&lt;/p&gt;

&lt;p&gt;The user documentation was updated to match the implementation. Comments were
added as well, to explain some design choices and non-obvious steps in the code.&lt;/p&gt;

&lt;p&gt;The whole restoration fits in about 150 lines of code, a small number because
none of it is new machinery. Every change followed the same approach: find the
place where GHC 9 and Liquid Haskell’s pipeline had drifted apart, and re-align
them, trying to stay as close as possible to the GHC 8 implementation.&lt;/p&gt;

&lt;p&gt;I want to thank my mentor, Facundo Domínguez, for his guidance and for steering me in
the right direction as each new error came up; to the Haskell.org committee and
Google Summer of Code for making the project possible; and to Tweag, for their
work on Liquid Haskell and for hosting this post.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot;&gt;

      &lt;p&gt;The exact name is an implementation detail which may vary and is internal
to GHC. &lt;a class=&quot;reversefootnote&quot; href=&quot;https://www.tweag.io/rss.xml#fnref:1&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot;&gt;

      &lt;p&gt;Measures are functions that refine the types of data constructors and lift
them to the logic. Generating a measure for a data constructor (or a record
type) requires identifying the shape of that constructor. This is important
for type classes because of the transformation they undergo during
compilation into record types. &lt;a class=&quot;reversefootnote&quot; href=&quot;https://www.tweag.io/rss.xml#fnref:2&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;</description>
	<pubDate>Thu, 17 Sep 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Bryan O'Sullivan: Announcing jj-stack: stacked GitHub pull requests for Jujutsu</title>
	<guid isPermaLink="true">https://www.serpentine.com/posts/2026/announcing-jj-stack/</guid>
	<link>https://www.serpentine.com/posts/2026/announcing-jj-stack/</link>
	<description>&lt;p&gt;Iâ€™m pleased to announce &lt;code&gt;jj-stack&lt;/code&gt;, a new tool for managing stacked GitHub pull requests quickly
and safely with &lt;a href=&quot;https://www.jj-vcs.dev/&quot;&gt;Jujutsu&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Stacked changes let you (or your agent) split a large body of work into small, independently
reviewable changes without waiting for each one to be reviewed or merged.&lt;/p&gt;
&lt;p&gt;I built this because I wanted the stacked-review workflow I’d grown used to, with the local
editing experience I’ve found so delightful in Jujutsu.&lt;/p&gt;</description>
	<pubDate>Mon, 14 Sep 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Haskell Interlude: 86: Michael Hanus</title>
	<guid isPermaLink="false">Buzzsprout-19791134</guid>
	<link>https://haskell.foundation/podcast/86</link>
	<description>&lt;p&gt;In this episode, we sat down with Michael Hanus. Michael is a professor at the University of Kiel and is one of the founders of functional logic programming, the combination of logic and functional programming in a single formalism. Michael is also the creator of Curry, a functional-logic language that resembles Haskell, so you can dive right in if you're familiar with Haskell. Naturally, we talked about what functional logic programming is, how it improves over plain functional programming, and the challenges of semantics and implementation. We learned a lot - enjoy!&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://haskell.foundation/podcast/86&quot;&gt;Episode links and transcript&lt;/a&gt;&lt;/p&gt;</description>
	<pubDate>Fri, 11 Sep 2026 17:00:00 +0000</pubDate>
        <enclosure url="https://www.buzzsprout.com/1817535/episodes/19791134-86-michael-hanus.mp3" length="38890822" type="audio/mpeg"/>
</item>
<item>
	<title>Bryan O'Sullivan: How my e-reader lost its stripes</title>
	<guid isPermaLink="true">https://www.serpentine.com/posts/2026/x3-stripes/</guid>
	<link>https://www.serpentine.com/posts/2026/x3-stripes/</link>
	<description>&lt;p&gt;I’ve been a lifelong reader, which has proved a fragile habit in our era of endless trivial
distraction. While books and e-ink readers solve the problem of competition for attention, they
can’t compete with a phone for convenience. That tiny device in my pocket has made reading both
easier and more vulnerable to displacement whenever a notification pops up.&lt;/p&gt;
&lt;p&gt;A few days ago, I learned for the first time about a recent generation of &lt;em&gt;tiny&lt;/em&gt; e-ink readers.
Since they’re cheap, it was easy to give into curiosity: I bought an &lt;a href=&quot;https://crosspointreader.com/&quot;&gt;Xteink
X3&lt;/a&gt;. Not only is it astonishingly tiny, I could immediately
install the delightful open-source
&lt;a href=&quot;https://github.com/crosspoint-reader/crosspoint-reader&quot;&gt;CrossPoint&lt;/a&gt; firmware. I was very
quickly able to install a few books. I also appreciated being able to install custom fonts,
though I was a little surprised by their indifferent rendering (&lt;em&gt;foreshadowing…&lt;/em&gt;).&lt;/p&gt;</description>
	<pubDate>Thu, 10 Sep 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Gabriella Gonzalez: Dependent if expressions without dependent types</title>
	<guid isPermaLink="true">https://haskellforall.com/2026/09/dependent-if-expressions</guid>
	<link>https://haskellforall.com/2026/09/dependent-if-expressions</link>
	<description>Folklore trick for dollar-store dependent types</description>
	<pubDate>Wed, 02 Sep 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>Haskell Interlude: 85: Brent Yorgey</title>
	<guid isPermaLink="false">Buzzsprout-19705795</guid>
	<link>https://haskell.foundation/podcast/85/</link>
	<description>&lt;p&gt;In today’s episode, we’re joined by Brent Yorgey, Associate Professor of Computer Science at Hendrix College and the creator, among other things, of the Diagrams vector graphics library and Swarm, a 2D programming and resource gathering game. We talk about teaching, domain specific languages, and competitive coding.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://haskell.foundation/podcast/85/&quot;&gt;Episode links and transcript&lt;/a&gt;&lt;/p&gt;</description>
	<pubDate>Wed, 26 Aug 2026 17:00:00 +0000</pubDate>
        <enclosure url="https://www.buzzsprout.com/1817535/episodes/19705795-85-brent-yorgey.mp3" length="43061216" type="audio/mpeg"/>
</item>
<item>
	<title>Philip Wadler: The Gargantuan Lie That is Collapsing the World's Climate</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-4320414745532883598</guid>
	<link>https://wadler.blogspot.com/2026/08/the-gargantuan-lie-that-is-collapsing.html</link>
	<description>&lt;p&gt;&lt;img alt=&quot;&quot; class=&quot;attachment-large size-large&quot; height=&quot;426&quot; src=&quot;https://www.carbonbrief.org/wp-content/uploads/2024/09/AMCKAY_Credit-Emma-Mcintyre-1-1024x682.jpeg&quot; width=&quot;640&quot; /&gt; &lt;/p&gt;&lt;p&gt;Adam McKay, director of &lt;i&gt;Don't Look Up, &lt;/i&gt;&lt;a href=&quot;https://www.currentaffairs.org/news/the-massive-climate-lie-that-will-destroy-human-civilization&quot;&gt;points out&lt;/a&gt; what should be obvious but is easy to ignore.&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;Like all grand lies, “we have more time” takes many different forms, often focus-grouped. There are the lofty-sounding promises to reach “Net-zero emissions by 2050,” as if 24 years from now was soon enough. There are the queries of “What’s your carbon footprint?,” as if individual people with plastic bags or straws were the problem and not gigantic multinational corporations. There’s the accusation that anyone who expresses urgency is a “doomer.” There are those who treat climate as an abstraction to win political campaigns, first arguing that &lt;/span&gt;&lt;/span&gt;&lt;a href=&quot;https://www.currentaffairs.org/news/2022/07/can-a-boon-for-the-fossil-fuel-sector-really-be-called-a-climate-bill&quot;&gt;the Biden administration’s Inflation Reduction Act&lt;/a&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt; was a victory despite doing nowhere near enough to address the scale of climate collapse, and more recently that Democrats should &lt;/span&gt;&lt;/span&gt;&lt;a href=&quot;http://v/&quot;&gt;just stop talking about climate&lt;/a&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt; and focus on more “winnable” issues. Most damaging and common of all, there are those who simply don’t mention it.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;All of these distortions have robbed us of the most critical element people need to understand a threat and adjust their actions accordingly: &lt;/span&gt;&lt;em style=&quot;font-family: garamond-premier-pro; margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;context.&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;And make no mistake, when it comes to how much time we have, the data and the geological record are crystal clear. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;For some badly-needed context, the last three years, likely for the first time in &lt;/span&gt;&lt;/span&gt;&lt;a href=&quot;https://www.aljazeera.com/news/2024/1/9/2023-shatters-record-for-worlds-warmest-year-eu-climate-agency&quot;&gt;over 100,000 years,&lt;/a&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt; have averaged above the Paris Accords’ “do not cross” 1.5 degrees celsius planetary warming. &lt;/span&gt;&lt;span style=&quot;color: #444746; margin: 0px; padding: 0px;&quot;&gt;Only ten years ago, that was considered catastrophic, but now mainstream news tells us it’s just a number.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #444746; margin: 0px; padding: 0px;&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;Let’s do the context thing again: 2C will mean we have completely left the stable climate period known as &lt;/span&gt;&lt;/span&gt;&lt;a href=&quot;https://www.thegef.org/news/goodbye-forever-friendly-holocene&quot;&gt;the Holocene&lt;/a&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;. This is the 11,000-year-old era that has given us large-scale crop and livestock agriculture, which gave us cities, written language, modern science and medicine, and warm beds and showers.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;Before that, for hundreds of thousands of years in the Pleistocene era, humans (and our hominid ancestors) spent most of their time running from drought, fires, floods, extreme cold and heat, predators, and each other. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;The population was likely no more than a few million people spread across the whole planet. At one point we may have almost gone extinct, with just 10,000 &lt;/span&gt;&lt;em style=&quot;font-family: garamond-premier-pro; margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;Homo sapien&lt;/span&gt;&lt;/em&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;s clinging to life. But after the advent of the Holocene and its remarkably stable climate, in only 100 centuries the world population has risen to 8 billion.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;That is what we stand to lose. In fact, if our institutions, news and elected officials continue to feed and water the oil company-conceived mega-falsehood that climate breakdown is something just “our great-grandkids need to worry about,”  human civilization as we know it will collapse and &lt;/span&gt;&lt;/span&gt;&lt;a href=&quot;https://www.forbes.com/sites/roberthart/2021/11/09/1-billion-people-at-risk-from-potentially-lethal-heat-if-global-warming-passes-2c-scientists-warn/&quot;&gt;billions could die&lt;/a&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;. And we’re not talking about the far distant future. We are talking about collapse within years, not centuries. For real.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p style=&quot;background-color: white; font-family: garamond-premier-pro, serif; font-size: 21px; line-height: 1.6 !important; margin: 0px; padding: 0px; text-indent: 2.5em;&quot;&gt;&lt;span style=&quot;margin: 0px; padding: 0px;&quot;&gt;&lt;span style=&quot;color: #222222; margin: 0px; padding: 0px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;</description>
	<pubDate>Tue, 25 Aug 2026 18:29:33 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>Philip Wadler: There is No AI</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-7988354390333301093</guid>
	<link>https://wadler.blogspot.com/2026/08/there-is-no-ai.html</link>
	<description>&lt;p&gt; &lt;/p&gt;&lt;img height=&quot;359&quot; src=&quot;https://tse2.mm.bing.net/th/id/OIP.w5uVNkNk3bDeGq7P01w_LAHaEK?r=0&amp;amp;pid=Api&quot; width=&quot;640&quot; /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I've searched with little success for ideas about how we can handle AI so that it benefits rather than harms society. Here is a &lt;a href=&quot;https://www.newyorker.com/science/annals-of-artificial-intelligence/there-is-no-ai&quot;&gt;suggestion&lt;/a&gt; from Jaron Lanier.  It was published by the New Yorker in 2023, when GPT-4 was the latest model. Time named Lanier to its list of 100 most influential people in 2015. &lt;/div&gt;&lt;div&gt;&lt;p class=&quot;paywall&quot;&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p class=&quot;paywall&quot;&gt;A program like OpenAI’s GPT-4, which can write sentences to order, is something like a version of Wikipedia that includes much more data, mashed together using statistics. Programs that create images to order are something like a version of online image search, but with a system for combining the pictures. In both cases, it’s people who have written the text and furnished the images. The new programs mash up work &lt;a href=&quot;https://www.newyorker.com/tech/annals-of-technology/chatgpt-is-a-blurry-jpeg-of-the-web&quot;&gt;done by human minds&lt;/a&gt;. What’s innovative is that the mashup process has become guided and constrained, so that the results are usable and often striking. This is a significant achievement and worth celebrating—but it can be thought of as illuminating previously hidden concordances between human creations, rather than as the invention of a new mind.&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;As far as I can tell, my view flatters the technology. After all, what is civilization but social collaboration? Seeing A.I. as a way of working together, rather than as a technology for creating independent, intelligent beings, may make it less mysterious—less like &lt;span class=&quot;small&quot; style=&quot;line-height: inherit;&quot;&gt;hal&lt;/span&gt; 9000 or Commander Data. But that’s good, because mystery only makes mismanagement more likely.&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;...&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;This concept, which I’ve &lt;a class=&quot;external-link&quot; href=&quot;https://www.amazon.com/Who-Owns-Future-Jaron-Lanier/dp/1451654979/ref=nodl_?dplnkId=da962c54-3d95-4609-a813-7471d422d110&quot; rel=&quot;nofollow noopener&quot; target=&quot;_blank&quot;&gt;contributed&lt;/a&gt; to developing, is usually called “data dignity.” It appeared, long before the rise of big-model “A.I.,” as an alternative to the familiar arrangement in which people give their data for free in exchange for free services, such as internet searches or social networking. Data dignity is sometimes known as “data as labor” or “plurality research.” The familiar arrangement has turned out to have a dark side: because of “network effects,” a few platforms take over, eliminating smaller players, like local newspapers. Worse, since the immediate online experience is supposed to be free, the only remaining business is the hawking of influence. Users experience what seems to be a communitarian paradise, but they are targeted by stealthy and addictive algorithms that make people vain, irritable, and paranoid.&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;In a world with data dignity, digital stuff would typically be connected with the humans who want to be known for having made it. In some versions of the idea, people could get paid for what they create, even when it is filtered and recombined through big models, and tech hubs would earn fees for facilitating things that people want to do. Some people are horrified by the idea of capitalism online, but this would be a more honest capitalism. The familiar “free” arrangement has been a disaster.&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;...&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Consider what might happen if A.I.-driven tree-trimming robots are introduced. Human tree trimmers might find themselves devalued or even out of work. But the robots could eventually allow for a new type of indirect landscaping artistry. Some former workers, or others, might create inventive approaches—holographic topiary, say, that looks different from different angles—that find their way into the tree-trimming models. With data dignity, the models might create new sources of income, distributed through collective organizations. Tree trimming would become more multifunctional and interesting over time; there would be a community motivated to remain valuable. Each new successful introduction of an A.I. or robotic application could involve the inauguration of a new kind of creative work. In ways large and small, this could help ease the transition to an economy into which models are integrated.&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;...&lt;/span&gt;&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;There are also non-altruistic reasons for A.I. companies to embrace data dignity. The models are only as good as their inputs. It’s only through a system like data dignity that we can expand the models into new frontiers. Right now, it’s much easier to get an L.L.M. to write an essay than it is to ask the program to generate an interactive virtual-reality world, because there are very few virtual worlds in existence. Why not solve that problem by giving people who add more virtual worlds a chance for prestige and income?&lt;/p&gt;&lt;/blockquote&gt;&lt;p class=&quot;paywall&quot;&gt;&lt;/p&gt;&lt;p class=&quot;paywall&quot;&gt;&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/p&gt;&lt;br class=&quot;Apple-interchange-newline&quot; /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;</description>
	<pubDate>Tue, 25 Aug 2026 17:57:53 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>Chris Smith 2: How to (Almost) Trisect an Angle</title>
	<guid isPermaLink="false">https://medium.com/p/c5c6aaf40620</guid>
	<link>https://cdsmithus.medium.com/how-to-almost-trisect-an-angle-c5c6aaf40620?source=rss-18bd5acaea78------2</link>
	<description>&lt;p&gt;In 1837 Pierre Wantzel proved that no compass-and-straightedge construction can trisect every angle. The counterexample is 60°: trisecting it would mean constructing cos 20°, which is a root of an irreducible cubic, while a compass and straightedge can only build stacks of square roots: objects of degree 2, 4, 8, never 3. And since 60° is easy to construct, any general method for trisection would have to trisect that one, too.&lt;/p&gt;&lt;p&gt;None of this has stopped people trying. For well over a century, mathematics departments have received a steady stream of constructions from amateurs convinced they’d done the impossible, enough of them that Underwood Dudley filled a whole book, &lt;em&gt;The Trisectors&lt;/em&gt;, with them.&lt;/p&gt;&lt;p&gt;I used to be one of those amateurs. Well, I was twelve, and my construction never made it into any mathematician’s inbox. But I had the essentials: a method, a compass and straightedge, a very exasperated geometry teacher, and a conviction that the experts had given up too soon.&lt;/p&gt;&lt;h4&gt;What you can trisect&lt;/h4&gt;&lt;p&gt;So you can’t trisect an angle, but there are things you can trisect. Say the angle is AVB, with vertex V and measure θ, and mark A and B the same distance out from V. Draw the arc centered at V from A to B. Cutting that arc into three equal pieces is the same as cutting the angle into three, so the whole problem comes down to trisecting that one arc, and that arc is the one Wantzel showed is off limits. But it belongs to a whole family of arcs from A to B, some fatter, some flatter, and other members of the family can be trisected exactly.&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;https://cdn-images-1.medium.com/max/1024/1*MiNrmJbppGEwmtZ1L6vM4g.png&quot; /&gt;&lt;/figure&gt;&lt;p&gt;Pick one, trisect it, and read the result back as an approximate trisection of the angle. How wrong you are depends on how far apart the two arcs sit.&lt;/p&gt;&lt;h4&gt;Idea #1: Trisect the chord instead&lt;/h4&gt;&lt;p&gt;Start with the flattest member of the family. Push the arc’s center infinitely far back and the arc straightens into the segment AB itself. Segments can be trisected exactly, by a standard similar-triangles construction, or more slickly through the centroid of a triangle. Either way, trisect AB, draw rays through the two division points, and you have approximate trisectors. Call this &lt;strong&gt;chord trisection&lt;/strong&gt;.&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;https://cdn-images-1.medium.com/max/1024/1*Ttp7_wErMGaBfKmbDndLpA.png&quot; /&gt;&lt;/figure&gt;&lt;p&gt;For narrow angles, chord trisection works remarkably well. As the angle shrinks the arc flattens toward its chord, and the error vanishes like θ³/81 (in radians, plus higher-order terms). That cubic is fast: a 4° angle gets trisected to within a few ten-thousandths of a degree. Wide angles are another story. The arc bulges away from the chord, the chord’s trisection points crowd toward the sides of the angle, and as θ approaches 180° the middle third of the trisection swallows the whole angle.&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;https://cdn-images-1.medium.com/max/1024/1*SlV2iKbxEuP0VXx0Y9Mm5g.png&quot; /&gt;&lt;/figure&gt;&lt;p&gt;So chord trisection is wonderful for narrow angles and hopeless for wide ones.&lt;/p&gt;&lt;h4&gt;Idea #2: Trisect a semicircle instead&lt;/h4&gt;&lt;p&gt;This is the method I found that year in geometry class. Slide the arc’s center the other way, up from V as far as it will go, to the midpoint of AB. The arc swells into the semicircle on AB, pointing away from V. A semicircle is easy to trisect, because its thirds are 60° arcs, and you mark those off by stepping the radius along the arc with your compass. Rays from V through the two marks are your approximate trisectors. Call this &lt;strong&gt;semicircle trisection&lt;/strong&gt;.&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;https://cdn-images-1.medium.com/max/1024/1*ra4-y4Jqc_rBjLpIabobWg.png&quot; /&gt;&lt;/figure&gt;&lt;p&gt;You’d expect semicircle trisection to do well at wide angles, and it does. At θ = 180° the semicircle stops being a stand-in altogether: V lands at the midpoint of AB, the semicircle coincides with the angle’s own arc, and the trisection is exact. Narrow the angle from there and the true arc pulls away from the semicircle, so the error grows…&lt;/p&gt;&lt;p&gt;Then it does something surprising: it turns around, comes back to exactly zero at θ = 90°, then rises and falls once more on the way down to zero at θ = 0, never getting more than 1.226° from the true trisector anywhere along the way. The worst cases sit at arcsin(1/√3) ≈ 35° and its supplement near 145°.&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;https://cdn-images-1.medium.com/max/1024/1*p2kj376RuHWcb8UBWmSfQw.png&quot; /&gt;&lt;/figure&gt;&lt;p&gt;The zero at 0° is just collapse. A and B merge, the whole angle closes onto a single ray, and the constructed trisectors converge onto it along with everything else. The zero at 90° is the interesting one, and it turns out to be the inscribed angle theorem. A right angle inscribed in a circle subtends a diameter, so a 90° vertex V lies on the very circle the trisection marks were drawn on, and each 60° arc of that circle subtends exactly 30° at V.&lt;/p&gt;&lt;p&gt;You can see how I fell for this. The three cases I knew how to check — 0°, 90°, 180° — were all exact, and an error under 1.226° passes the eyeball test everywhere else. The weakness hides at small angles, which are the hardest to check on paper. As θ approaches 0, semicircle trisection hands back a ray a quarter of the way across the angle instead of a third. Less than a degree off, sure, but when the whole angle is a few degrees, that’s a low bar. The error remains a substantial fraction of the answer no matter how small θ gets.&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;https://cdn-images-1.medium.com/max/1024/1*m8LjrHAUy4ldOqV76X-yTA.png&quot; /&gt;&lt;/figure&gt;&lt;p&gt;We have two approximations, then, with opposite strengths. Chord trisection converges cubically for small angles but blows up for large ones. Semicircle trisection stays within about a degree everywhere but converges to the wrong ray as the angle shrinks. It would be nice to get the best of both worlds, wouldn’t it?&lt;/p&gt;&lt;h4&gt;Bridge: The exact residual trick&lt;/h4&gt;&lt;p&gt;Draw the arc centered at V through A and B, and suppose someone hands you a guess: a point G on the arc, with VG claimed to be a trisector. Set your compass to the chord distance from A to G. Starting at B, step backwards that distance twice along the arc toward A, and call the landing point H.&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;https://cdn-images-1.medium.com/max/1024/1*MTtE06mJVAYuOwSS0co2hg.png&quot; /&gt;&lt;/figure&gt;&lt;p&gt;Claim: the trisector of the (probably small) angle GVH adjacent to VG is exactly the trisector of AVB adjacent to VA.&lt;/p&gt;&lt;p&gt;Proof: Write the guess as arc(A→G) = θ/3 + ε, where ε is its unknown error. Each compass step from B moves an arc of θ/3 + ε, so after two steps H sits at θ − 2(θ/3 + ε) = θ/3 − 2ε from A. The residual arc from H to G is therefore exactly 3ε, and a third of the way from G toward H lands at θ/3 + ε − ε = θ/3, for either sign of ε.&lt;/p&gt;&lt;p&gt;The residual angle GVH is exactly three times the guess’s error, and nothing in the reduction is approximate. Trisecting AVB has been converted, exactly, into trisecting a much smaller angle.&lt;/p&gt;&lt;h4&gt;Putting the pieces together&lt;/h4&gt;&lt;p&gt;So here’s the plan. Feed any angle at all, 5° or 175°, to semicircle trisection, and it hands back a guess within 1.226° of the true trisector. The residual trick then turns that guarantee into a fresh problem: an angle of at most 3.68° whose trisector is exactly the ray we’re after. And 3.68° is chord territory, small enough for the cubic convergence to bite, handing us a remarkable 0.674 arcseconds of error: roughly the angle spanned by a penny viewed from three and a half miles away.&lt;/p&gt;&lt;figure&gt;&lt;img alt=&quot;&quot; src=&quot;https://cdn-images-1.medium.com/max/1024/1*XSWCXuC9io-wwd1FVn2eNw.png&quot; /&gt;&lt;/figure&gt;&lt;p&gt;Nothing stops you from running the residual trick again on the improved answer; each round cubes the error. But one pass already lands within an arcsecond, and iterating would be polishing a quantity that stopped mattering a step ago.&lt;/p&gt;&lt;img alt=&quot;&quot; height=&quot;1&quot; src=&quot;https://medium.com/_/stat?event=post.clientViewed&amp;amp;referrerSource=full_rss&amp;amp;postId=c5c6aaf40620&quot; width=&quot;1&quot; /&gt;</description>
	<pubDate>Sun, 23 Aug 2026 14:46:19 +0000</pubDate>
</item>
<item>
	<title>Abhinav Sarkar: Fast Haskell Scripts on GitHub Actions</title>
	<guid isPermaLink="false">https://abhinavsarkar.net/posts/fast-haskell-scripts-on-github-actions/</guid>
	<link>https://abhinavsarkar.net/posts/fast-haskell-scripts-on-github-actions/?mtm_campaign=feed</link>
	<description>&lt;p&gt;Magix is a neat tool that lets us run &lt;a href=&quot;https://haskell.org&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Haskell&lt;/a&gt; programs as scripts&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;. We put a shebang on top mentioning Magix, list the Haskell packages we need, and &lt;code&gt;./script.hs&lt;/code&gt; just works. This post is about running such a script fast(er) on &lt;a href=&quot;https://github.com/features/actions&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;GitHub Actions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This post was originally published on &lt;a href=&quot;https://abhinavsarkar.net/posts/fast-haskell-scripts-on-github-actions/?mtm_campaign=feed&quot;&gt;abhinavsarkar.net&lt;/a&gt;.&lt;/p&gt;
&lt;nav class=&quot;toc&quot;&gt;&lt;h2&gt;Contents&lt;/h2&gt;&lt;ol&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-setup&quot;&gt;The Setup&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-problem&quot;&gt;The Problem&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-idea&quot;&gt;The Idea&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-bundle&quot;&gt;The Bundle&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-github-actions-workflow&quot;&gt;The GitHub Actions Workflow&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-payoff&quot;&gt;The Payoff&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-conclusion&quot;&gt;The Conclusion&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/nav&gt;
&lt;h2 id=&quot;the-setup&quot;&gt;The Setup&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-setup&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As our example, we’ll take the &lt;a href=&quot;https://en.wikipedia.org/wiki/static_site_generator&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;static site generator&lt;/a&gt; (SSG) I wrote some time ago: &lt;a href=&quot;https://abhin4v.github.io/shake-blog/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;BlogShake&lt;/a&gt;. It is written as a single Haskell file. It uses &lt;a href=&quot;https://shakebuild.com/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Shake&lt;/a&gt; to build the website, &lt;a href=&quot;https://pandoc.org/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Pandoc&lt;/a&gt; to render posts, and &lt;a href=&quot;https://hackage.haskell.org/package/mustache&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Mustache&lt;/a&gt; for templates. The script starts with these Magix directives&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn2&quot; id=&quot;fnref2&quot;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode numberSource bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;#!/usr/bin/env magix&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;#!magix haskell&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;#!haskellPackages mustache pandoc shake feed yaml&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;#!ghcFlags -threaded -O2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Running the script is as simple as:&lt;/p&gt;
&lt;pre class=&quot;plain&quot;&gt;&lt;code&gt;./blog.hs&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/dschrempf/magix&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Magix&lt;/a&gt; compiles the script into an executable and runs it. Nothing else to install, no &lt;code&gt;cabal&lt;/code&gt;, or &lt;code&gt;nix-shell&lt;/code&gt; required.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://nixos.org&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Nix&lt;/a&gt; and &lt;a href=&quot;https://www.haskell.org/cabal/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Cabal&lt;/a&gt; can also run scripts by providing shebang directives&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn3&quot; id=&quot;fnref3&quot;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;, so why reach for Magix instead? The &lt;code&gt;nix-shell&lt;/code&gt; shebang reinterprets the script with &lt;code&gt;runhaskell&lt;/code&gt; on every run, which is slow. Cabal compiles the script, but it fetches dependencies from Hackage and builds them from source, leading to very slow first build and rebuilds. Magix compiles the script once into a binary executable and caches it for the next runs. It also fetches dependencies from the prebuilt Nix cache. So running via Magix is faster than either case. But when running on GitHub Actions, we have a problem.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-problem&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/features/actions&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;GitHub Actions&lt;/a&gt; gives us a fresh runner for every build, with no Nix store and no Magix cache. So every run, we have to install Nix and Magix, download all the dependencies, and compile the script. In one instance, the build from scratch took 105 seconds, with installation, dependency download, and compilation taking 92 seconds.&lt;/p&gt;
&lt;p&gt;That is a lot of wasted work because the script and its dependencies change rarely. The actual run itself takes only a few seconds once the executable exists.&lt;/p&gt;
&lt;h2 id=&quot;the-idea&quot;&gt;The Idea&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-idea&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;What if we could persist the compiled executable across runs? Magix’s build is deterministic: the same script and the same nixpkgs revision produces the same executable. If we could stash that executable somewhere durable, a cache hit could skip Nix and Magix installation, as well as the script compilation entirely, and just run the executable.&lt;/p&gt;
&lt;p&gt;Magix creates a Nix derivation for compiling the Haskell script with GHC and builds it. The resultant executable lives in Magix’s cache, as a symlink with a path like:&lt;/p&gt;
&lt;pre class=&quot;plain&quot;&gt;&lt;code&gt;$ readlink -f ~/.cache/magix/*blog-result
/nix/store/&amp;lt;hash&amp;gt;-blog&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://haskell.org/ghc/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;GHC&lt;/a&gt; statically links all the Haskell libraries into the executable. The only dynamic dependencies are a handful of libraries—zlib, libffi, gmp etc. So if we have the executable plus those few libraries as a self-contained unit, we don’t need the hundreds of other packages in the Nix store&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn4&quot; id=&quot;fnref4&quot;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-bundle&quot;&gt;The Bundle&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-bundle&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I wrote a small script, &lt;code&gt;magix-bundle.sh&lt;/code&gt;, with two subcommands:&lt;/p&gt;
&lt;pre class=&quot;plain&quot;&gt;&lt;code&gt;$ magix-bundle.sh bundle blog.hs     # creates a bundle
$ magix-bundle.sh run blog.hs build  # runs a bundled script&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The script works for any Magix script, not just &lt;code&gt;blog.hs&lt;/code&gt;. Let’s go through it.&lt;/p&gt;
&lt;p&gt;The script takes a command and a script file, derives the bundle name from the script file name, and resolves the directories it needs:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode numberSource bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;bu&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-euo&lt;/span&gt; pipefail&lt;/span&gt;
&lt;span id=&quot;cb7-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;cf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$#&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-lt&lt;/span&gt; 2 &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;usage: magix-bundle.sh &amp;lt;bundle|run&amp;gt; &amp;lt;script-file&amp;gt; [args...]&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;exit&lt;/span&gt; 1&lt;/span&gt;
&lt;span id=&quot;cb7-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;cf&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;va&quot;&gt;CMD&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$1&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;bu&quot;&gt;shift&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;va&quot;&gt;SCRIPT_FILE&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$1&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;bu&quot;&gt;shift&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$SCRIPT_FILE&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;error: script not found: &lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$SCRIPT_FILE&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;exit&lt;/span&gt; 1&lt;/span&gt;
&lt;span id=&quot;cb7-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;va&quot;&gt;NAME&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$SCRIPT_FILE&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;va&quot;&gt;NAME&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;${NAME&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;%&lt;/span&gt;.&lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;va&quot;&gt;HASH&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;sha256sum&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$SCRIPT_FILE&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;'{print $1}'&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;va&quot;&gt;MAGIX_CACHE&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;${MAGIX_CACHE_DIR&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:-&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;${XDG_CACHE_HOME&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:-&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$HOME&lt;/span&gt;/.cache&lt;span class=&quot;va&quot;&gt;}&lt;/span&gt;/magix&lt;span class=&quot;va&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb7-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;va&quot;&gt;DEST&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;${MAGIX_BUNDLE_DIR&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:-&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$HOME&lt;/span&gt;/.cache/magix-bundles&lt;span class=&quot;va&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$HASH&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;NAME&lt;/code&gt; is the script file name without its extension, and &lt;code&gt;HASH&lt;/code&gt; is a SHA-256 hash of the script contents; together they key the bundle directory. &lt;code&gt;MAGIX_CACHE&lt;/code&gt; mirrors how Magix itself resolves its cache directory: &lt;code&gt;$XDG_CACHE_HOME/magix&lt;/code&gt; or &lt;code&gt;~/.cache/magix&lt;/code&gt;, overridable with the &lt;code&gt;MAGIX_CACHE_DIR&lt;/code&gt; environment variable. Bundles live under &lt;code&gt;~/.cache/magix-bundles&lt;/code&gt; by default, overridable with the &lt;code&gt;MAGIX_BUNDLE_DIR&lt;/code&gt; env var.&lt;/p&gt;
&lt;h3 id=&quot;creating-the-bundle&quot;&gt;Creating the Bundle&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#creating-the-bundle&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;bundle&lt;/code&gt; function locates the script’s build result in Magix’s cache, copies the executable out of the Nix store, and gathers the libraries it links against:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode numberSource bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;copy_lib_deps()&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$1&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;deps&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;dep&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;base&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;va&quot;&gt;deps&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;ldd&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$src&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;awk&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;'/\/nix\/store\//{print $3}'&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;for&lt;/span&gt; dep &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;$deps&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$dep&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;-glibc-&lt;/span&gt;&lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;continue&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;esac&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$dep&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;error: dependency '&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$dep&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;' of '&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$src&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;' not found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;bu&quot;&gt;exit&lt;/span&gt; 1&lt;/span&gt;
&lt;span id=&quot;cb8-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;va&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$dep&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/lib/&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$base&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-L&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$dep&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/lib/&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$base&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ex&quot;&gt;copy_lib_deps&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$dep&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;done&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;bundle()&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/bin&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/lib&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-24&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;va&quot;&gt;RESULT&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-25&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;for&lt;/span&gt; f &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$MAGIX_CACHE&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/&quot;&lt;/span&gt;&lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;-&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$NAME&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;-result&quot;&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-26&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-L&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;continue&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-27&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;continue&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-28&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-z&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$RESULT&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-nt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$RESULT&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-29&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;va&quot;&gt;RESULT&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-30&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-31&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;done&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-32&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$RESULT&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-33&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;error: no build result for '&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$SCRIPT_FILE&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;' found in &lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$MAGIX_CACHE&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-34&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;exit&lt;/span&gt; 1&lt;/span&gt;
&lt;span id=&quot;cb8-35&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-36&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-37&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;va&quot;&gt;RESULT&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;readlink&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$RESULT&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-38&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;va&quot;&gt;BIN&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$RESULT&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/bin/.&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$NAME&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;-wrapped&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-39&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$BIN&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/bin/&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$NAME&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-40&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;chmod&lt;/span&gt; +w &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/bin/&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$NAME&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-41&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-41&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-42&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-42&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;copy_lib_deps&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$BIN&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-43&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-43&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-44&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-44&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-m&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-45&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-45&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ss&quot;&gt;x86_64&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-46&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-46&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;va&quot;&gt;INTERPRETER&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;/lib64/ld-linux-x86-64.so.2&lt;/span&gt;
&lt;span id=&quot;cb8-47&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-47&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;cf&quot;&gt;;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-48&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-48&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ss&quot;&gt;aarch64&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-49&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-49&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;va&quot;&gt;INTERPRETER&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;/lib/ld-linux-aarch64.so.1&lt;/span&gt;
&lt;span id=&quot;cb8-50&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-50&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;cf&quot;&gt;;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-51&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-51&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-52&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-52&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;error: unsupported architecture: &lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;uname&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-m&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-53&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-53&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;bu&quot;&gt;exit&lt;/span&gt; 1&lt;/span&gt;
&lt;span id=&quot;cb8-54&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-54&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;cf&quot;&gt;;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-55&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-55&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;esac&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-56&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-56&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-57&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-57&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ex&quot;&gt;patchelf&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-58&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-58&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;at&quot;&gt;--set-interpreter&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$INTERPRETER&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-59&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-59&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;at&quot;&gt;--set-rpath&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;ORIGIN/lib&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-60&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-60&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/bin/&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$NAME&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-61&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-61&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-62&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-62&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;for&lt;/span&gt; lib &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;/lib/&lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-63&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-63&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$lib&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;continue&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-64&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-64&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;chmod&lt;/span&gt; +w &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$lib&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-65&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-65&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;patchelf&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;--set-rpath&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;dt&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;ORIGIN&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$lib&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-66&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-66&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;done&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-67&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-67&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;bundle created at &lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-68&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb8-68&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here is what it does:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;It picks the latest &lt;code&gt;&amp;lt;hash&amp;gt;-&amp;lt;name&amp;gt;-result&lt;/code&gt; symlink for the script by modified time, skipping dangling ones&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn5&quot; id=&quot;fnref5&quot;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;It dereferences the symlink to a Nix store path, copies the compiled executable at &lt;code&gt;bin/.&amp;lt;name&amp;gt;-wrapped&lt;/code&gt; from the Nix store path into the bundle&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn6&quot; id=&quot;fnref6&quot;&gt;&lt;sup&gt;6&lt;/sup&gt;&lt;/a&gt;, and makes the copy writable.&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn7&quot; id=&quot;fnref7&quot;&gt;&lt;sup&gt;7&lt;/sup&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;It copies the dynamic library dependencies of the executable by calling the function &lt;code&gt;copy_lib_deps&lt;/code&gt;. &lt;code&gt;copy_lib_deps&lt;/code&gt; finds the dependencies with &lt;code&gt;ldd&lt;/code&gt;, and copies the ones that live in &lt;code&gt;/nix/store&lt;/code&gt; into the bundle’s &lt;code&gt;lib/&lt;/code&gt; directory, skipping glibc. Note that it does this recursively, copying the dependencies of dependencies as well.&lt;/li&gt;
&lt;li&gt;It rewrites the executable with &lt;code&gt;patchelf&lt;/code&gt;, setting the interpreter and the library search path for it. It also sets the library search path for libraries themselves so that transitive dependencies work as well.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The two &lt;code&gt;patchelf&lt;/code&gt; flags are the interesting part. The &lt;code&gt;--set-rpath&lt;/code&gt; flag makes the dynamic loader find the bundled libraries, so we don’t need &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt; or the Nix store at run time. The &lt;code&gt;--set-interpreter&lt;/code&gt; flag is needed because Nix rewrites each executable’s dynamic loader to point at its own glibc inside the Nix store. Since we don’t bundle glibc, we reset the interpreter to the host’s loader according to the system’s architecture&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn8&quot; id=&quot;fnref8&quot;&gt;&lt;sup&gt;8&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;bundling-glibc&quot;&gt;Bundling glibc?&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#bundling-glibc&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;We deliberately do not bundle glibc. Unlike the other libraries, glibc cannot simply be shipped alongside the executable&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn9&quot; id=&quot;fnref9&quot;&gt;&lt;sup&gt;9&lt;/sup&gt;&lt;/a&gt;. The dynamic loader needs to be at an absolute path and needs to be matched with the glibc version. So we simply don’t bundle it and rely on the host’s glibc. One caveat here is that the host’s glibc must be at least as new as the one the executable was built against. That works because glibc is backwards-compatible&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn10&quot; id=&quot;fnref10&quot;&gt;&lt;sup&gt;10&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;running-the-bundle&quot;&gt;Running the Bundle&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#running-the-bundle&quot;&gt;§&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The rest of the script is the &lt;code&gt;run&lt;/code&gt; subcommand and the command dispatch:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode numberSource bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;run()&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;local&lt;/span&gt; &lt;span class=&quot;va&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/bin/&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$NAME&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-f&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$bin&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;/lib&quot;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;error: no bundle for '&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$SCRIPT_FILE&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;' at &lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$DEST&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;exit&lt;/span&gt; 1&lt;/span&gt;
&lt;span id=&quot;cb9-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;bu&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$bin&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;cf&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$CMD&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ss&quot;&gt;bundle&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$#&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-gt&lt;/span&gt; 0 &lt;span class=&quot;bu&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;error: 'bundle' takes no extra arguments&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;bu&quot;&gt;exit&lt;/span&gt; 1&lt;/span&gt;
&lt;span id=&quot;cb9-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;fi&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ex&quot;&gt;bundle&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ss&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ex&quot;&gt;run&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;pp&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;error: unknown command '&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$CMD&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;' (expected 'bundle' or 'run')&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;bu&quot;&gt;exit&lt;/span&gt; 1&lt;/span&gt;
&lt;span id=&quot;cb9-22&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-23&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb9-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;cf&quot;&gt;esac&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;run&lt;/code&gt; computes the same SHA-256 hash of the script, checks that the bundle exists, and &lt;a href=&quot;https://en.wikipedia.org/wiki/Exec_(system_call)&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;&lt;code&gt;exec&lt;/code&gt;&lt;/a&gt;s the executable, passing the remaining arguments through untouched.&lt;/p&gt;
&lt;h2 id=&quot;the-github-actions-workflow&quot;&gt;The GitHub Actions Workflow&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-github-actions-workflow&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The workflow uses Nix, Magix and the bundler script to execute the Haskell script.&lt;/p&gt;
&lt;p&gt;First, we set the runner image and the nixpkgs branch we track&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn11&quot; id=&quot;fnref11&quot;&gt;&lt;sup&gt;11&lt;/sup&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode numberSource yaml&quot;&gt;&lt;code class=&quot;sourceCode yaml&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; Build blog&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;branches&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;at&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;workflow_dispatch&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; build&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; ubuntu-26.04&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb10-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;NIXPKGS_BRANCH&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; release-26.05&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The next two steps compute the cache key and restore the bundle if we have one:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode numberSource yaml&quot;&gt;&lt;code class=&quot;sourceCode yaml&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; checkout&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; actions/checkout@v6&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; main&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; resolve nixpkgs commit&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; nixpkgs&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;        run&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          set -euo pipefail&lt;/span&gt;
&lt;span id=&quot;cb11-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          SHA=$(git ls-remote https://github.com/NixOS/nixpkgs.git &quot;refs/heads/$NIXPKGS_BRANCH&quot; | cut -f1)&lt;/span&gt;
&lt;span id=&quot;cb11-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          echo &quot;sha=$SHA&quot; &amp;gt;&amp;gt; &quot;$GITHUB_OUTPUT&quot;&lt;/span&gt;
&lt;span id=&quot;cb11-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; cache blog bundle&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; cache-blog&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; actions/cache@v5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; ~/.cache/magix-bundles&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb11-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; magix-bundles-${{ hashFiles('blog.hs') }}-${{ steps.nixpkgs.outputs.sha }}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We resolve the nixpkgs branch to its latest commit with &lt;code&gt;git ls-remote&lt;/code&gt; at the start of the job, use that commit in the cache key, and pin the whole build to the same commit via &lt;code&gt;MAGIX_NIXPKGS_REF&lt;/code&gt;, as we see below.&lt;/p&gt;
&lt;p&gt;The cache key has two parts: the hash of the script and the resolved nixpkgs commit hash. When the branch moves or the script changes, the cache misses because of the key change, and we rebuild against the new commit and/or script. We also restore the previously cached bundle to &lt;code&gt;~/.cache/magix-bundles&lt;/code&gt;, if found.&lt;/p&gt;
&lt;p&gt;The next four steps run only on a cache miss:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode numberSource yaml&quot;&gt;&lt;code class=&quot;sourceCode yaml&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; install Nix&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; steps.cache-blog.outputs.cache-hit != 'true'&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; DeterminateSystems/nix-installer-action@v22&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-5&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;          extra-conf&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;ch&quot;&gt;|&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-6&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            extra-substituters = https://dschrempf-magix.cachix.org&lt;/span&gt;
&lt;span id=&quot;cb12-7&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            extra-trusted-public-keys = dschrempf-magix.cachix.org-1:cScG6NjZBiQvY7KjSPpQdUa9UXZVLz9rvUZHtuwdYwc=&lt;/span&gt;
&lt;span id=&quot;cb12-8&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-9&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; install magix&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-10&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; steps.cache-blog.outputs.cache-hit != 'true'&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-11&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; nix profile install github:dschrempf/magix&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-12&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-13&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; compile blog&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-14&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; steps.cache-blog.outputs.cache-hit != 'true'&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-15&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-16&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;MAGIX_NIXPKGS_REF&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; github:NixOS/nixpkgs/${{ steps.nixpkgs.outputs.sha }}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-17&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; ./blog.hs&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-18&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-19&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; create bundle&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-20&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; steps.cache-blog.outputs.cache-hit != 'true'&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-21&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb12-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; ./scripts/magix-bundle.sh bundle blog.hs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;install Nix&lt;/code&gt; adds Magix’s binary cache to Nix, so that we get the prebuilt packages for Magix. The &lt;code&gt;compile blog&lt;/code&gt; step runs &lt;code&gt;./blog.hs&lt;/code&gt; with no arguments&lt;a class=&quot;footnote-ref&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fn12&quot; id=&quot;fnref12&quot;&gt;&lt;sup&gt;12&lt;/sup&gt;&lt;/a&gt;: This causes Magix to compile the script, but Shake has nothing to build, so it exits immediately. The build is pinned to the same nixpkgs commit that keys the cache, via the &lt;code&gt;MAGIX_NIXPKGS_REF&lt;/code&gt; env variable used by Magix. Finally, &lt;code&gt;create bundle&lt;/code&gt; packages the compiled executable by running the bash script we saw earlier.&lt;/p&gt;
&lt;p&gt;The rest of the workflow runs on every run, cache hit or not:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode numberSource yaml&quot;&gt;&lt;code class=&quot;sourceCode yaml&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; build site&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb13-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;at&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;kw&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;at&quot;&gt; ENV=PROD ./scripts/magix-bundle.sh run blog.hs -j4 build&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I’ve cut down the rest of the workflow to the only step that is relevant to us: &lt;code&gt;build site&lt;/code&gt; that runs the bundled executable with the script’s arguments. Other steps are specific to BlogShake.&lt;/p&gt;
&lt;h2 id=&quot;the-payoff&quot;&gt;The Payoff&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-payoff&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;All of this work, what does it buy us? Here are two numbers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Build with no bundle: 1 min 45 sec.&lt;/li&gt;
&lt;li&gt;Build with cached bundle: 10 sec.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With the cached bundle, the runner just downloads it and runs the executable, skipping Nix and Magix installation and script compilation altogether.&lt;/p&gt;
&lt;h2 id=&quot;the-conclusion&quot;&gt;The Conclusion&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#the-conclusion&quot;&gt;§&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This post showed how to speed up a Haskell script running on GitHub Actions by caching a compact bundle of its compiled executable. The approach works for any Haskell script that runs on Linux: compile once using Magix, bundle the binary with its library dependencies, and let the cache do the rest. One caveat though: the bundler script uses the internal details of Magix, which may break if Magix changes how it works.&lt;/p&gt;
&lt;p&gt;The full source code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/abhin4v/shake-blog/blob/main/blog.hs&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;BlogShake Haskell script&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/abhin4v/shake-blog/blob/main/scripts/magix-bundle.sh&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Magix bundle script&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/abhin4v/shake-blog/blob/main/.github/workflows/build.yml&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;BlogShake GitHub Actions workflow&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p class=&quot;like-msg&quot;&gt;
If you have any questions or comments, please leave a comment below. If you liked this post, please share it. Thanks for reading!
&lt;/p&gt;
&lt;section class=&quot;footnotes footnotes-end-of-document&quot; id=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;At the point of writing this post, Magix supported Bash, Haskell, and Python.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn2&quot;&gt;&lt;p&gt;Because the executable is compiled once and run many times, we build the script with &lt;code&gt;-O2&lt;/code&gt;.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref2&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn3&quot;&gt;&lt;p&gt;The shebang-based alternatives look like this:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode bash noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;#! /usr/bin/env nix-shell&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;#! nix-shell -p &quot;haskellPackages.ghcWithPackages (p: [p.pandoc p.shake p.mustache p.feed p.yaml])&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;#! nix-shell -i runhaskell&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;for Nix, and:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode bash noNumberSource&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;#! /usr/bin/env cabal&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;{-&lt;/span&gt; cabal:&lt;/span&gt;
&lt;span id=&quot;cb4-3&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;build-depends:&lt;/span&gt; base, pandoc, shake, mustache, feed, yaml&lt;/span&gt;
&lt;span id=&quot;cb4-4&quot;&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#cb4-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;-}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;for Cabal.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref3&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn4&quot;&gt;&lt;p&gt;Why not cache the entire Nix store between runs? Because the size of the full Nix store closure required to build Haskell scripts is usually in GBs. Caching that per run would defeat the purpose of caching by taking way too much time to download the cache. You may also want to reach out for the &lt;a href=&quot;https://nix.dev/manual/nix/2.35/command-ref/new-cli/nix3-bundle.html&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Nix bundle&lt;/a&gt; feature, which produces self-contained compressed executables. But these executables are still too big: 45 MB compressed/178 MB uncompressed for BlogShake. Our approach in this post results in a 9 MB bundle, compressed.&lt;/p&gt;
&lt;p&gt;Another completely different option is to build a fully statically linked executable, which I wrote about in &lt;a href=&quot;https://abhinavsarkar.net/posts/nix-for-haskell-static-builds/?mtm_campaign=feed&quot;&gt;Nix for Haskell: Static Builds&lt;/a&gt;. However, that requires a custom toolchain, running which on GitHub Action is too complex and/or slow.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref4&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn5&quot;&gt;&lt;p&gt;A result symlink can be left dangling if the store path has been garbage-collected. We skip those and pick the newest live one.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref5&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn6&quot;&gt;&lt;p&gt;Magix wraps the built executable with &lt;code&gt;wrapProgram&lt;/code&gt;, which renames the real executable to &lt;code&gt;bin/.&amp;lt;name&amp;gt;-wrapped&lt;/code&gt; and puts a wrapper script in its place. We copy the real executable.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref6&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn7&quot;&gt;&lt;p&gt;Nix store files are read-only mode, and we are about to modify the file, so we make it writable.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref7&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn8&quot;&gt;&lt;p&gt;We hardcode the loader paths here, but it should work on most mainstream Linux distributions with glibc.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref8&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn9&quot;&gt;&lt;p&gt;Well, it can be actually. That’s what Nix bundle does. It copies the glibc in the Nix store to the bundle, and points the program interpreter at the bundled loader inside a &lt;a href=&quot;https://en.wikipedia.org/wiki/chroot&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;chroot&lt;/a&gt;.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref9&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn10&quot;&gt;&lt;p&gt;An executable built against glibc 2.42 runs fine on a host with glibc 2.43, but not the other way around.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref10&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn11&quot;&gt;&lt;p&gt;The glibc constraint dictates the runner image. Here we build against nixpkgs branch &lt;code&gt;release-26.05&lt;/code&gt;, which has glibc 2.42. Ubuntu 26.04, the GitHub runner image we use, ships with glibc 2.43. So they are compatible.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref11&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn12&quot;&gt;&lt;p&gt;That no-argument behavior is Shake-specific: with no actions given, Shake runs nothing, so a bare &lt;code&gt;./blog.hs&lt;/code&gt; compiles the script and exits. A general script won’t do that by default. If you adapt this for a non-Shake script, give it a mode that does nothing, say a &lt;code&gt;--compile&lt;/code&gt; flag, so running it bare just produces the executable. This step’s only job is to get Magix to build the script, not to run it.&lt;a class=&quot;footnote-back&quot; href=&quot;https://abhinavsarkar.net/posts/tags/haskell/feed.atom#fnref12&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;&lt;hr /&gt;&lt;p&gt;Thanks for reading this post via feed. Feeds are great, and you're great for using them. ♥&lt;/p&gt;&lt;p&gt;This post was originally published on &lt;a href=&quot;https://abhinavsarkar.net/posts/fast-haskell-scripts-on-github-actions/?mtm_campaign=feed&quot;&gt;abhinavsarkar.net&lt;/a&gt;.&lt;/p&gt;&lt;h3&gt;Like, repost, or comment on:&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a class=&quot;u-syndication&quot; href=&quot;https://fantastic.earth/@abnv/117071410353946821&quot; rel=&quot;syndication&quot; target=&quot;_blank&quot;&gt;Fediverse&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;u-syndication&quot; href=&quot;https://lobste.rs/s/guqk6b&quot; rel=&quot;syndication&quot; target=&quot;_blank&quot;&gt;Lobsters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;u-syndication&quot; href=&quot;https://www.reddit.com/r/haskell/comments/1vky99w/&quot; rel=&quot;syndication&quot; target=&quot;_blank&quot;&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&quot;u-syndication&quot; href=&quot;https://discourse.nixos.org/t//79502&quot; rel=&quot;syndication&quot; target=&quot;_blank&quot;&gt;Discourse&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://abhinavsarkar.net/posts/fast-haskell-scripts-on-github-actions/?mtm_campaign=feed#comment-container&quot;&gt;My website&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Read more of my &lt;a href=&quot;https://abhinavsarkar.net/posts/&quot;&gt;posts&lt;/a&gt; and &lt;a href=&quot;https://abhinavsarkar.net/notes/&quot;&gt;notes&lt;/a&gt;.&lt;/p&gt;&lt;img alt=&quot;&quot; src=&quot;https://anna.abhinavsarkar.net/matomo.php?idsite=1&amp;amp;rec=1&quot; style=&quot;border: 0;&quot; /&gt;</description>
	<pubDate>Mon, 10 Aug 2026 00:00:00 +0000</pubDate>
	<author>abhinav@abhinavsarkar.net (Abhinav Sarkar)</author>
</item>
<item>
	<title>Oskar Wickström: How Antithesis Found and Explained a Bombadil Race Condition</title>
	<guid isPermaLink="true">https://wickstrom.tech/2026-08-07-bombadil-race-condition-antithesis.html</guid>
	<link>https://wickstrom.tech/2026-08-07-bombadil-race-condition-antithesis.html</link>
	<description>&lt;p&gt;&lt;em&gt;Disclosure: I’m the original author and lead for the Bombadil project at Antithesis.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;a href=&quot;https://catala.dev/&quot;&gt;Carlos Catala&lt;/a&gt;, interning at Antithesis and working on testing our webapps, found a strange crash while I was out camping. The tests he works on runs Antithesis’ webapp &lt;em&gt;inside Antithesis&lt;/em&gt; and explores it using Bombadil. That surfaced a particular sequence of events leading up to the following crash (logs are simplified for readability):&lt;/p&gt; &lt;pre&gt;&lt;code&gt;state machine error: unhandled transition: Running(10) + ActionAccepted(Click(&quot;Filters&quot;))&lt;/code&gt;&lt;/pre&gt; &lt;p&gt;What does this mean? To understand it, we need to back up a little.&lt;/p&gt; &lt;p&gt;The heart of the browser &lt;em&gt;driver&lt;/em&gt; is a finite state machine, an abstract model of a tab in Chrome or Chromium, and how we’re interacting with it over CDP. This is necessary due to how you can only invoke certain commands in certain browser states, or it might hang or behave in other confounding ways. In our state machine, some transitions are valid, and those are handled in a big &lt;code&gt;match (state, event) { ... }&lt;/code&gt; expression that returns a new state. Invalid transitions are covered by the last branch that returns an &lt;code&gt;Err&lt;/code&gt; with a message like the above.&lt;/p&gt; &lt;p&gt;There’s another important component involved which is called the &lt;em&gt;runner&lt;/em&gt;. It decides the control flow of the test, and uses the driver to run the system under test. The driver emits a state, and the runner decides what action to take in that state. It hands back the action to the driver so that it can apply it. The browser driver actually pauses the JS runtime when capturing a state, and doesn’t resume execution until the driver has given back the next action.&lt;/p&gt; &lt;p&gt;Hence, the &lt;code&gt;ActionAccepted&lt;/code&gt; event (which is when the driver gets a new action from the runner) is not valid in the &lt;code&gt;Running&lt;/code&gt; state. It really is only valid in the &lt;code&gt;Paused&lt;/code&gt; state. How could this happen? The driver and runner are supposed to operate in lock-step, where the driver is only in control while the JS runtime is paused. This was the nut to crack.&lt;/p&gt; &lt;p&gt;When Carlos told me about this during my PTO, my camping brain was definitely not helpful, so I let it rest until I was back at work. Staring at logs and code also didn’t help much, except for a poorly conceived theory about what could be going on, mislead by the presence of &lt;a href=&quot;https://antithesis.com/docs/product/fault_injection/fault_types/#network-faults&quot;&gt;network faults&lt;/a&gt; in one test where we saw the bug. We tried but failed to reproduce this locally, so I decided to find a simpler test subject and, crucially, try to reproduce it in Antithesis.&lt;/p&gt; &lt;h2 id=&quot;a-simple-reproduction-with-causality-analysis&quot;&gt;A Simple Reproduction with Causality Analysis&lt;/h2&gt; &lt;p&gt;To reduce possible confusion I wanted to first see if I could trigger the same bug in Bombadil with a simpler system under test. I found a Dockerized version of &lt;a href=&quot;https://the-internet.herokuapp.com/&quot;&gt;the-internet&lt;/a&gt;, an example application by Sauce Labs made for automated browser testing. Based on what we had seen before, I wrote a very small specification that only performs three actions:&lt;/p&gt; &lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode typescript&quot;&gt;&lt;code class=&quot;sourceCode typescript&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;im&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;im&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;@antithesishq/bombadil/browser/defaults/properties&quot;&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt; &lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;im&quot;&gt;import&lt;/span&gt; { weighted } &lt;span class=&quot;im&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;@antithesishq/bombadil/browser&quot;&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt; &lt;span id=&quot;cb2-3&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;im&quot;&gt;import&lt;/span&gt; {&lt;/span&gt; &lt;span id=&quot;cb2-4&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt; back&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt;&lt;/span&gt; &lt;span id=&quot;cb2-5&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt; forward&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt;&lt;/span&gt; &lt;span id=&quot;cb2-6&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt; clicks&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt;&lt;/span&gt; &lt;span id=&quot;cb2-7&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;} &lt;span class=&quot;im&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;@antithesishq/bombadil/browser/defaults/actions&quot;&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt; &lt;span id=&quot;cb2-8&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt; &lt;span id=&quot;cb2-9&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;im&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;const&lt;/span&gt; navigation &lt;span class=&quot;op&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;weighted&lt;/span&gt;([&lt;/span&gt; &lt;span id=&quot;cb2-10&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt; [&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt; clicks]&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt;&lt;/span&gt; &lt;span id=&quot;cb2-11&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt; [&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt; back]&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt;&lt;/span&gt; &lt;span id=&quot;cb2-12&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt; [&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;,&lt;/span&gt; forward]&lt;/span&gt; &lt;span id=&quot;cb2-13&quot;&gt;&lt;a href=&quot;https://wickstrom.tech/feed.xml#cb2-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;])&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; &lt;p&gt;In addition to finding various bugs in the Ruby code, it also immediately reproduced the Bombadil bug! Time to dig in. I went through a few instances and started to see a pattern. In addition, this gave me an excellent opportunity to try out &lt;a href=&quot;https://antithesis.com/docs/product/debugging/causality_analysis/&quot;&gt;Causality Analysis&lt;/a&gt;, a mind-bending feature for learning what behavior increases the likelihood of a bug appearing. Here’s the report I got:&lt;/p&gt; &lt;figure&gt; &lt;img alt=&quot;The causality analysis report plots the likelihood and helps you understand what events caused a certain bug.&quot; src=&quot;https://wickstrom.tech/assets/causality-analysis.webp&quot; /&gt; &lt;figcaption&gt;The causality analysis report plots the likelihood and helps you understand what events caused a certain bug.&lt;/figcaption&gt; &lt;/figure&gt; &lt;p&gt;As you may see in the chart, there are three points where the probability increases markedly. Correlating those with the logs (which is easy, you see the bumps highlighted in the log too), I could find the chain leading up to the bug:&lt;/p&gt; &lt;ol type=&quot;1&quot;&gt; &lt;li&gt;Click a link that navigates to &lt;code&gt;/frames&lt;/code&gt; (probability goes to 20%)&lt;/li&gt; &lt;li&gt;On that page, click a link that navigates to &lt;code&gt;/iframe&lt;/code&gt; (probability goes to 47%)&lt;/li&gt; &lt;li&gt;Go back (I was a bit surprised that this didn’t bump probability noticeably)&lt;/li&gt; &lt;li&gt;Go forward (probability goes to 99.98%)&lt;/li&gt; &lt;/ol&gt; &lt;p&gt;After the last “Go forward” action, it’s a done deal. Digging into the details in between these points, I could then see how going back to a page which immediately throws an exception, after restoring from &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Glossary/bfcache&quot;&gt;bfcache&lt;/a&gt;, takes a different path than most page loads do.&lt;/p&gt; &lt;h2 id=&quot;the-bug&quot;&gt;The Bug&lt;/h2&gt; &lt;p&gt;When going back or forward in history, the state machine immediately transitions to &lt;code&gt;Running&lt;/code&gt;, waiting for confirmation by the browser to know what actually happened. Some pages prevent bfcache through JavaScript or HTTP headers, so a history navigation can also end up as a full page load with new HTTP requests. When the page is confirmed to have been restored from bfcache, the page and thus the state machine model skips the &lt;code&gt;Loading&lt;/code&gt; state entirely, and goes straight to &lt;code&gt;Running&lt;/code&gt;.&lt;/p&gt; &lt;p&gt;In this case, that was followed by the exception being thrown immediately (you can see it in the bottom of the screenshot above), which triggers another path: exceptions thrown while in &lt;code&gt;Running&lt;/code&gt; aren’t collected and reported at a later state capture, as is the case for the &lt;code&gt;Loading&lt;/code&gt; state — they immediately pause the runtime and trigger a new state read. This in turn let the runner pick a new action to perform. But the CDP event that confirms that the page was restored from bfcache hadn’t arrived yet.&lt;/p&gt; &lt;p&gt;This is a race between the asynchronous communication of runner and browser state machine, selecting the next action, and the CDP event sent by Chromium over a WebSocket confirming the bfcache restore. The unfortunate ordering that triggers the bug is:&lt;/p&gt; &lt;ol type=&quot;1&quot;&gt; &lt;li&gt;Back or forward, restoring from bfcache&lt;/li&gt; &lt;li&gt;Exception thrown on restored page, pausing the debugger and capturing a new state&lt;/li&gt; &lt;li&gt;Runner picking a new action and passing it back to the browser state machine&lt;/li&gt; &lt;li&gt;Confirmation of bfcache restoration coming in, transitioning state machine to &lt;code&gt;Running&lt;/code&gt;&lt;/li&gt; &lt;li&gt;State machine applying the &lt;code&gt;ActionAccepted&lt;/code&gt; event, which, as we noted in the beginning of this excursion, is not valid when in &lt;code&gt;Running&lt;/code&gt;&lt;/li&gt; &lt;/ol&gt; &lt;p&gt;This sequence causes the crash. It only happens with bfcache-enabled history navigation to a page with an exception being thrown before the confirmation from Chromium comes back. Antithesis’ deterministic fuzzing environment messes with process scheduling and action selection and discovers this sequence reliably.&lt;/p&gt; &lt;h2 id=&quot;in-conclusion&quot;&gt;In Conclusion&lt;/h2&gt; &lt;p&gt;I’m not going to describe the fix in too much detail, but it involves a more robust use of the already existing &lt;code&gt;Generation&lt;/code&gt; value in the browser state machine. It’s a monotonically increasing number that is used to detect stale timeouts and actions, so that the state machine can discard them. The browser state now carries the current generation, and that is threaded through the runner’s action selection process, so that a stale action (as in step 5 above) can be safely ignored. Additionally, some transitions now increment the generation in cases where all existing timeouts or actions are stale.&lt;/p&gt; &lt;p&gt;I’ve had bugs of this kind before, and understanding them from staring at logs, let alone reproducing them, has been painful. This, in contrast, feels a bit like cheating. Moreover, it extends to race conditions and complicated bugs in the webapps themselves, which makes me very excited about continued work on the Bombadil and Antithesis integration.&lt;/p&gt;</description>
	<pubDate>Thu, 06 Aug 2026 22:00:00 +0000</pubDate>
</item>
<item>
	<title>Philip Wadler: Just Work: The Gig Economy, Platform Capitalism and the Battle for Worker Rights</title>
	<guid isPermaLink="false">tag:blogger.com,1999:blog-9757377.post-994402424996852846</guid>
	<link>https://wadler.blogspot.com/2026/08/just-work-gig-economy-platform.html</link>
	<description>&lt;p&gt; &lt;/p&gt;&lt;img height=&quot;640&quot; src=&quot;https://tomhumberstone.com/media/pages/pieces/just-work/b069648aa0-1781785658/page1_1-3000x.webp&quot; width=&quot;429&quot; /&gt;&lt;p&gt;A &lt;a href=&quot;https://tomhumberstone.com/just-work&quot;&gt;comic&lt;/a&gt; by Tom Humberstone about gig workers and the need to organise, with loads of local Edinburgh flavour.&lt;/p&gt;</description>
	<pubDate>Wed, 05 Aug 2026 14:43:48 +0000</pubDate>
	<author>noreply@blogger.com (Philip Wadler)</author>
</item>
<item>
	<title>in Code: &quot;Five-Point Haskell&quot;: Unconditional Election (via Parametricity)</title>
	<guid isPermaLink="true">https://blog.jle.im/entry/five-point-haskell-part-2-unconditional-election.html</guid>
	<link>https://blog.jle.im/entry/five-point-haskell-part-2-unconditional-election.html</link>
	<description>&lt;p&gt;Welcome back to &lt;em&gt;&lt;a href=&quot;https://blog.jle.im/entries/series/+five-point-haskell.html&quot;&gt;Five-Point
Haskell&lt;/a&gt;&lt;/em&gt;! This is my attempt to codify principles of writing robust,
maintainable, correct, clear, and effective code in Haskell and to dispel common
bad practices (or, heresies) I have run into in my time.&lt;/p&gt;
&lt;p&gt;In the last post, we talked about &lt;a href=&quot;https://blog.jle.im/entry/five-point-haskell-part-1-total-depravity.html&quot;&gt;Total
Depravity&lt;/a&gt;, which is about treating any mentally tracked constraint or
condition as inevitably leading to a catastrophe and denouncing the reliance on
our flawed mental context windows.&lt;/p&gt;
&lt;p&gt;However, stopping here gives us an incomplete picture. Firstly, types aren’t
just about preventing bad behaviors. They’re about designing good code.
Secondly, there is only so much you can do by picking careful structures and
making invalid states unrepresentable. These are still human tools with human
flaws.&lt;/p&gt;
&lt;p&gt;The next point, to me, is about an aspect of the type system that I see
little coverage of, but is a doctrine of design that I reach for in almost
everything I write. It’s about leveraging the unyielding properties of math
&lt;em&gt;itself&lt;/em&gt; to take care of our fate, even when we are unable to structure
our types well.&lt;/p&gt;
&lt;p&gt;So, when writing Haskell, remember &lt;strong&gt;Unconditional
Election&lt;/strong&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Unconditional Election: The power of the &lt;code&gt;forall&lt;/code&gt; to elect or
reprobate instantiations and implementations through parametric polymorphism.
These properties aren’t based on any conditional ad-hoc aspect of types, but are
truly unconditional, predestined by universal quantification.&lt;/p&gt;
&lt;p&gt;Surrender your control to parametric polymorphism in all things. Embrace the
“free”-dom of “Free Theorems” from one of Haskell’s greatest unexpected
strengths: the type parameter.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;choice-is-a-prison&quot;&gt;Choice is a Prison&lt;/h2&gt;
&lt;h3 id=&quot;conditional-election&quot;&gt;Conditional Election&lt;/h3&gt;
&lt;p&gt;Learning Haskell can be a journey full of surprises, but this was one of the
ones that blew my mind the most.&lt;/p&gt;
&lt;p&gt;Let’s think of a polymorphic function in Java that takes a value of any type
and returns something of that same type:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode java&quot;&gt;&lt;code class=&quot;sourceCode java&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; T &lt;span class=&quot;fu&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;T x&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What could that function do?&lt;/p&gt;
&lt;p&gt;Well, it could do IO or throw an exception, mutate the input, or possibly be
non-terminating, but let’s assume all it (and every other example here) does is
purely return a value without mutation. What could it do?&lt;/p&gt;
&lt;p&gt;The answer: pretty much anything. It could return the same value it was
given, except if it is an &lt;code&gt;Integer&lt;/code&gt;, in which case it negates it:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode java&quot;&gt;&lt;code class=&quot;sourceCode java&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; T &lt;span class=&quot;fu&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;T x&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;kw&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;Integer&lt;/span&gt; i&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;valueOf&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;(-&lt;/span&gt;i&lt;span class=&quot;op&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;return&lt;/span&gt; x&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Same deal in TypeScript or pretty much any other typed language without
parametricity:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode typescript&quot;&gt;&lt;code class=&quot;sourceCode typescript&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt;(x&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; T)&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; T&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yet again, we have conditional election:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode typescript&quot;&gt;&lt;code class=&quot;sourceCode typescript&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt;(x&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; T)&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; T {&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;typeof&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;number&quot;&lt;/span&gt;) {&lt;/span&gt;
&lt;span id=&quot;cb4-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;cf&quot;&gt;return&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;x) &lt;span class=&quot;im&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;im&quot;&gt;as&lt;/span&gt; T&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;
&lt;span id=&quot;cb4-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;cf&quot;&gt;return&lt;/span&gt; x&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But how about Haskell?&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;foo ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(For the rest of this post, let’s ignore &lt;a href=&quot;https://www.cse.chalmers.se/~nad/publications/danielsson-popl2006-tr.pdf&quot;&gt;non-termination&lt;/a&gt;&lt;a class=&quot;footnote-ref&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;
and other escape hatches&lt;a class=&quot;footnote-ref&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fn2&quot; id=&quot;fnref2&quot;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Because Haskell has type erasure and no runtime reflection, the &lt;em&gt;only&lt;/em&gt;
possible implementation is simply:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;foo ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So, there is an invariant that appeared somehow in our code: a function of
type &lt;code&gt;forall a. a -&amp;gt; a&lt;/code&gt; &lt;em&gt;must&lt;/em&gt; leave its value
unchanged!&lt;/p&gt;
&lt;p&gt;But wait…says who? Did we insert some sort of &lt;code&gt;const&lt;/code&gt; compiler
annotation? Did we add some sort of annotation or pre- and post-condition that
the value cannot change? Are we relying on any sort of foreseeable property of
the value given?&lt;/p&gt;
&lt;p&gt;No, this behavior is actually &lt;em&gt;intrinsically&lt;/em&gt; fixed! We got this
theorem &lt;em&gt;for free&lt;/em&gt;. No need for any sort of work, no need for any
foreseen faithfulness. We didn’t even have to &lt;em&gt;write&lt;/em&gt; the function before
knowing all it possibly could be.&lt;/p&gt;
&lt;p&gt;This is the power of the &lt;code&gt;forall&lt;/code&gt;. The above
&lt;code&gt;foo :: a -&amp;gt; a&lt;/code&gt; can be considered “sugar” for:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;foo ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; a&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you don’t add a &lt;code&gt;forall a&lt;/code&gt;, it is implicitly added. Some
languages, like PureScript and Dhall, require the &lt;code&gt;forall&lt;/code&gt; in every
case to be explicit. You can think of the “forall” as like
&lt;code&gt;template &amp;lt;typename T&amp;gt;&lt;/code&gt; in &lt;code&gt;C++&lt;/code&gt;, a declaration of
what type variable is being quantified over.&lt;/p&gt;
&lt;p&gt;Anyway, let’s consider another type signature:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode java&quot;&gt;&lt;code class=&quot;sourceCode java&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;T x&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Even if we disallow IO (like getting the time, or system state), in Java (and
most other languages), this again could literally be anything. You can serialize
the object with &lt;code&gt;toString&lt;/code&gt;, or you can get its class using
&lt;code&gt;getClass&lt;/code&gt;…&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode java&quot;&gt;&lt;code class=&quot;sourceCode java&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;T x&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;T x&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;return&lt;/span&gt; x&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;bu&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;(&lt;/span&gt;T x&lt;span class=&quot;op&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;cf&quot;&gt;return&lt;/span&gt; x&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;getClass&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;getSimpleName&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In Haskell? A &lt;code&gt;forall a. a -&amp;gt; String&lt;/code&gt; cannot use its input! It
&lt;em&gt;must&lt;/em&gt; be a constant string!&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;foo ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo _ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- or&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo _ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;goodbye&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- or&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo _ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;i always ignore my input&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In fact, there is an isomorphism between &lt;code&gt;String&lt;/code&gt; and
&lt;code&gt;forall a. a -&amp;gt; String&lt;/code&gt; (fun exercise: write it!)&lt;/p&gt;
&lt;p&gt;You can “selectively” bring in capabilities using typeclasses:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;foo ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo _ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- or&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb11-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- or&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;reversed: &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;reverse&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; x)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But there are still properties you can enforce: the resulting string can
&lt;em&gt;only&lt;/em&gt; depend on the input as far as what is revealed in its
&lt;code&gt;Show&lt;/code&gt; instance. Any property not in its &lt;code&gt;Show&lt;/code&gt; instance
is off-limits.&lt;/p&gt;
&lt;p&gt;Alternatively, you can think of
&lt;code&gt;foo :: Show a =&amp;gt; a -&amp;gt; String&lt;/code&gt; as:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;foo ::&lt;/span&gt; (a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foo showVal x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and you can see that the only way you can ever inspect &lt;code&gt;a&lt;/code&gt; is
through the singular inspection-lens &lt;code&gt;a -&amp;gt; String&lt;/code&gt; that you are
given. No &lt;code&gt;getClass()&lt;/code&gt;, no back doors, etc.&lt;/p&gt;
&lt;h3 id=&quot;the-guessing-game&quot;&gt;The Guessing Game&lt;/h3&gt;
&lt;p&gt;One game Haskellers often play day-to-day is “guess the properties that the
&lt;code&gt;forall&lt;/code&gt; ensures” on different type signatures. Let’s try it out!&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;mystery ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;mystery&lt;/code&gt; &lt;em&gt;has&lt;/em&gt; to be &lt;code&gt;\x _ -&amp;gt; x&lt;/code&gt; — there is
no other option.&lt;/p&gt;
&lt;p&gt;How about:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb14&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb14-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb14-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;turmeric ::&lt;/span&gt; ((a, b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; c) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; c&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you think about it, the only option is:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb15&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb15-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb15-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;turmeric ::&lt;/span&gt; ((a, b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; c) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; c&lt;/span&gt;
&lt;span id=&quot;cb15-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb15-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;turmeric f x y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; f (x, y)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can go pretty far down this lane using Haskell as a &lt;a href=&quot;https://blog.jle.im/entry/the-baby-paradox-in-haskell.html#haskell-as-a-theorem-prover&quot;&gt;theorem
prover&lt;/a&gt;, in that the type signature represents a proposition and the
implementation represents a proof of that claim. But we’re not going to go down
that route for now, since most practical code is not theorem-proving.&lt;/p&gt;
&lt;p&gt;But let’s look at something a bit more structural. How about:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb16&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb16-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb16-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;theThing ::&lt;/span&gt; [a] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [a]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What could this do?&lt;/p&gt;
&lt;p&gt;Well, we know that all items from the result list &lt;em&gt;must&lt;/em&gt; be from the
input list. It must be a “subset” — but the ordering or multiplicity can change.
And more importantly, it &lt;em&gt;can’t&lt;/em&gt; depend on anything about the properties
of any &lt;code&gt;a&lt;/code&gt;. We also know that if the input is empty, so must be the
output.&lt;/p&gt;
&lt;p&gt;From this, we can derive what are called &lt;a href=&quot;https://people.mpi-sws.org/~dreyer/tor/papers/wadler.pdf&quot;&gt;free
theorems&lt;/a&gt; to look at properties that &lt;em&gt;any implementation&lt;/em&gt; must have.
Namely, &lt;em&gt;mapping&lt;/em&gt; a function over the list and calling
&lt;code&gt;theThing&lt;/code&gt; must be equivalent to calling &lt;code&gt;theThing&lt;/code&gt; and
then mapping:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb17&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb17-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;theThing &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; f&lt;/span&gt;
&lt;span id=&quot;cb17-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; theThing&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Can you see why? Think of any possible implementation — &lt;code&gt;reverse&lt;/code&gt;,
&lt;code&gt;take 3&lt;/code&gt;, etc. — and see how this must be the case. However, this is
&lt;em&gt;not&lt;/em&gt; true for i.e. &lt;code&gt;sort :: [Int] -&amp;gt; [Int]&lt;/code&gt;. Because
&lt;code&gt;sort&lt;/code&gt; depends on the actual properties of the items,
&lt;code&gt;map f&lt;/code&gt; could change the properties that &lt;code&gt;sort&lt;/code&gt; depends
on!&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb18&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb18-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb18-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb18-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb18-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb18-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb18-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb18-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb18-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Compared to an actual function with the polymorphic type, like
&lt;code&gt;take 3&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb19&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb19-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb19-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb19-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb19-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How about:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb20&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb20-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb20-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;doIt ::&lt;/span&gt; [a] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Think about what this &lt;em&gt;can’t&lt;/em&gt; do. It clearly selects a single item,
but:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;The single item cannot be determined based on any quality or merit of that
item — it can’t be the smallest, the largest, etc.; it has to depend purely on
the position in the list and the length of the list&lt;/li&gt;
&lt;li&gt;If given an empty list, it &lt;em&gt;must&lt;/em&gt; return &lt;code&gt;Nothing&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;And again we have the same free theorem,
&lt;code&gt;doIt . map f == fmap f . doIt&lt;/code&gt;. No matter how you implement
&lt;code&gt;doIt&lt;/code&gt;, it is guaranteed to commute with &lt;code&gt;map&lt;/code&gt; and
&lt;code&gt;fmap&lt;/code&gt;!&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb21&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb21-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- no free theorem: `minimumMay :: [Int] -&amp;gt; Maybe Int`&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; minimumMay &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb21-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; minimumMay &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb21-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- free theorem: `listToMaybe :: [a] -&amp;gt; Maybe a`&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; listToMaybe &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb21-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; listToMaybe &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb21-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- free theorem: `lastMay :: [a] -&amp;gt; Maybe a`&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; lastMay &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb21-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; lastMay &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb21-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s try another one:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb22&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb22-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb22-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;collapse ::&lt;/span&gt; [a] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What could this possibly do? Well, we can rule out things like
&lt;code&gt;sum&lt;/code&gt; because we can’t use any property of the values themselves. The
only things that this could return are constant functions and functions that
depend on the &lt;em&gt;length&lt;/em&gt; but not the &lt;em&gt;contents&lt;/em&gt; of the list. We also
have another free theorem, &lt;code&gt;collapse . map f == collapse&lt;/code&gt;: mapping a
function shouldn’t change the output, because none of the actual values
matter.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb23&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb23-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- no free theorem: `sum :: [Int] -&amp;gt; Int`&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb23-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;16&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;sum&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb23-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- free theorem: `length :: [a] -&amp;gt; Int`&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb23-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;length&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb23-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- free theorem: `const 10 :: [a] -&amp;gt; Int`&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;abs&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb23-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt; [&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb23-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How about something in the opposite direction:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb24&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb24-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;duper ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [a]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;From this type signature, we can conclude that the final list must contain
the &lt;em&gt;same item&lt;/em&gt;! The only possible inhabitants are
&lt;code&gt;replicate n&lt;/code&gt; for some &lt;code&gt;n&lt;/code&gt;, or &lt;code&gt;repeat&lt;/code&gt;.&lt;a class=&quot;footnote-ref&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fn3&quot; id=&quot;fnref3&quot;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Again, we have a free theorem: &lt;code&gt;map f . duper == duper . f&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb25&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb25-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- no free theorem: `take 3 . iterate (+1) :: Int -&amp;gt; [Int]`&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;iterate&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;negate&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb25-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;negate&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;take&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;iterate&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;6&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb25-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- free theorem: `replicate 3 :: Int -&amp;gt; [Int]`&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;replicate&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;negate&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb25-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;negate&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;replicate&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;[&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How about continuations?&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb26&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb26-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb26-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;consumeInt ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; r&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; r) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; r&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This might look exotic, but think about what it &lt;em&gt;must&lt;/em&gt; do. It receives
a function &lt;code&gt;Int -&amp;gt; r&lt;/code&gt; and must produce an &lt;code&gt;r&lt;/code&gt;. The only
way to produce an &lt;code&gt;r&lt;/code&gt; for &lt;em&gt;all possible&lt;/em&gt; &lt;code&gt;r&lt;/code&gt; is to
apply the given function to some fixed &lt;code&gt;Int&lt;/code&gt; chosen ahead of time.
This type is actually isomorphic to &lt;code&gt;Int&lt;/code&gt; itself: the only inhabitant
is &lt;code&gt;\f -&amp;gt; f x&lt;/code&gt; for some fixed &lt;code&gt;x&lt;/code&gt;.&lt;a class=&quot;footnote-ref&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fn4&quot; id=&quot;fnref4&quot;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb27&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb27-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;consumeInt ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; r&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; r) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; r&lt;/span&gt;
&lt;span id=&quot;cb27-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;consumeInt f &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; f &lt;span class=&quot;dv&quot;&gt;42&lt;/span&gt;     &lt;span class=&quot;co&quot;&gt;-- must be a fixed `Int`, cannot dynamically change&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;consumeString ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; r&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; r) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; r&lt;/span&gt;
&lt;span id=&quot;cb27-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;consumeString f &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; f &lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt;     &lt;span class=&quot;co&quot;&gt;-- must be a fixed `String`, cannot dynamically change&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What’s better than one type variable? How about two?&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb28&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb28-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;mapMaybe ::&lt;/span&gt; (a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [a] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [b]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because this has to work for &lt;em&gt;all&lt;/em&gt; &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;,
we know that the &lt;em&gt;only&lt;/em&gt; possible &lt;code&gt;b&lt;/code&gt;s you can find in your
result list are &lt;code&gt;b&lt;/code&gt;s that you get from the
&lt;code&gt;a -&amp;gt; Maybe b&lt;/code&gt; function. So, you can be sure that the
implementation doesn’t conjure out any arbitrary &lt;code&gt;b&lt;/code&gt; except for the
specific ones producible by your &lt;code&gt;a -&amp;gt; Maybe b&lt;/code&gt;. For example, if
you pass it a function that returns only even integers, the resulting list will
&lt;em&gt;only&lt;/em&gt; ever contain even integers!&lt;/p&gt;
&lt;p&gt;One final one, with a higher-kinded type variable:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb29&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb29-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;traverseIO ::&lt;/span&gt; (a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [a] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; [b]&lt;/span&gt;
&lt;span id=&quot;cb29-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- vs&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;fu&quot;&gt;traverse&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; (a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; f b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [a] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; f [b]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What invariant does the second add over the first? Even &lt;em&gt;if&lt;/em&gt; you only
ever plan on calling things with &lt;code&gt;IO&lt;/code&gt;, the second gives you a new
invariant: there won’t be any “stray” &lt;code&gt;IO&lt;/code&gt; actions other than what is
given in the &lt;code&gt;a -&amp;gt; f b&lt;/code&gt;. In the first one, you never know if the
resulting &lt;code&gt;IO&lt;/code&gt; action might include a &lt;code&gt;putStrLn &quot;hello&quot;&lt;/code&gt;
or a &lt;code&gt;launchMissiles&lt;/code&gt;. You definitely don’t want any functions doing
sneaky IO behind your back!&lt;/p&gt;
&lt;h3 id=&quot;the-more-you-surrender&quot;&gt;The More you Surrender&lt;/h3&gt;
&lt;p&gt;Practically, this becomes similar to the principle of least power, the idea
that you should use the tools with the least power necessary to do your job. Say
you &lt;em&gt;are&lt;/em&gt; writing a function that shuffles a list of items, important for
your business logic. You can encode exactly &lt;em&gt;what&lt;/em&gt; business logic is
being done by adding more and more parametricity.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If your type is &lt;code&gt;[Int] -&amp;gt; [Int]&lt;/code&gt;, you know your function has
pretty much no restriction on what it can do. It can even look at the machine
representation of your values.&lt;/li&gt;
&lt;li&gt;If your type is &lt;code&gt;Num a =&amp;gt; [a] -&amp;gt; [a]&lt;/code&gt;, you know that it can
possibly numerically transform the items in your list, or even conjure up new
items.&lt;/li&gt;
&lt;li&gt;If your type is &lt;code&gt;Ord a =&amp;gt; [a] -&amp;gt; [a]&lt;/code&gt;, you know that your
business logic is allowed to look at the ordering between items in the list, but
cannot return any items that weren’t in the original list.&lt;/li&gt;
&lt;li&gt;If your type is &lt;code&gt;[a] -&amp;gt; [a]&lt;/code&gt;, you know that your logic can
only affect the permutation and multiplicity of items in your list.&lt;/li&gt;
&lt;li&gt;If your type is &lt;code&gt;Foldable t =&amp;gt; t a -&amp;gt; [a]&lt;/code&gt;, you know that
results come from the input, but you can still reorder, duplicate, or drop
elements. Furthermore, the resulting permutation is “pre-determined” before you
receive any input items.&lt;/li&gt;
&lt;li&gt;If your type is &lt;code&gt;Functor f =&amp;gt; f Int -&amp;gt; f Int&lt;/code&gt;, if you call
with &lt;code&gt;[]&lt;/code&gt;, you know that the length and ordering of the result will
be preserved, and also any mappings of &lt;code&gt;Int&lt;/code&gt;s will be done
purely.&lt;/li&gt;
&lt;li&gt;If your type is &lt;code&gt;Monad m =&amp;gt; m a -&amp;gt; m a&lt;/code&gt;, if you call with
&lt;code&gt;[]&lt;/code&gt;, you know that the lengths of your results will always be
integer powers of the length of the input, and each input element will be
duplicated the same number of times. So, if you give it &lt;code&gt;[1,2,3]&lt;/code&gt;,
you know the result’s length has to be of the form &lt;code&gt;3^k&lt;/code&gt; and must
contain &lt;code&gt;3^(k-1)&lt;/code&gt; copies of &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, and
&lt;code&gt;3&lt;/code&gt;, in some order.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By switching from concrete types slowly to parametric types, you surrender
control of what your functions can do, and create stronger and stronger
guarantees. In other languages, or with refinement types, you might have to
explicitly declare a post-condition like “the final values must all come from
the original list”. With parametric polymorphism, this is already guaranteed and
elected, no matter what the implementation is.&lt;/p&gt;
&lt;p&gt;You can also play this game in the other direction: instead of writing a
post-condition like “the length of the list must be preserved”, you can try to
figure out the level of power you need to get that. For example, if you wanted
to ensure “the result must have the same number of &lt;code&gt;Int&lt;/code&gt; items (if
you give it a list), but the order can depend on the actual inspection of the
items”, how would you give that type?&lt;/p&gt;
&lt;p&gt;And (spoilers) why does &lt;code&gt;Traversable f =&amp;gt; f Int -&amp;gt; f Int&lt;/code&gt;
work? And, why, when you pass it a tree of &lt;code&gt;Int&lt;/code&gt;s, does it even
preserve the &lt;em&gt;shape&lt;/em&gt; of the tree while freely allowing shuffling between
actual leaves based on the &lt;code&gt;Int&lt;/code&gt; values themselves?&lt;/p&gt;
&lt;h3 id=&quot;its-only-natural&quot;&gt;It’s Only Natural&lt;/h3&gt;
&lt;p&gt;As an aside, did you wonder where I got those free theorems from? In the
examples above, they come from &lt;em&gt;naturality&lt;/em&gt;. Basically, any
&lt;code&gt;forall a. (Functor f, Functor g) =&amp;gt; f a -&amp;gt; g a&lt;/code&gt; corresponds to
a &lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Natural_transformation&quot;&gt;natural
transformation&lt;/a&gt;&lt;/em&gt; in category theory, and so must commute with any
&lt;code&gt;fmap&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Basically, if you have a natural transformation
&lt;code&gt;h :: forall a. F a -&amp;gt; G a&lt;/code&gt;, with &lt;code&gt;Functor F&lt;/code&gt; and
&lt;code&gt;Functor G&lt;/code&gt;, then we have:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb30&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb30-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb30-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;h &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; f&lt;/span&gt;
&lt;span id=&quot;cb30-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb30-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; h&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above examples, &lt;code&gt;forall a. [a] -&amp;gt; [a]&lt;/code&gt;,
&lt;code&gt;forall a. [a] -&amp;gt; Maybe a&lt;/code&gt;, etc. all arise from this. But you
might have to think carefully to see that &lt;code&gt;forall a. a -&amp;gt; [a]&lt;/code&gt; is
really &lt;code&gt;forall a. Identity a -&amp;gt; [a]&lt;/code&gt;. And can you think of the
Functor that gives us naturality for &lt;code&gt;forall a. [a] -&amp;gt; Int&lt;/code&gt;?&lt;a class=&quot;footnote-ref&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fn5&quot; id=&quot;fnref5&quot;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Maybe more surprising than the fact that these free theorems exist is the
fact that their root is intrinsically tied to a branch of math as obscure and
esoteric as “category theory”!&lt;/p&gt;
&lt;h2 id=&quot;add-a-type-variable&quot;&gt;Add a Type Variable&lt;/h2&gt;
&lt;p&gt;Let’s say I had a data type like:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb31&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb31-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    {&lt;span class=&quot;ot&quot;&gt; userId ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; userName ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; userAge ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And we have a function to process the user, like:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb32&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb32-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processUser ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How can we enforce that the &lt;code&gt;userId&lt;/code&gt; is not changed?&lt;/p&gt;
&lt;p&gt;Maybe if we were in C, we could have a &lt;code&gt;const&lt;/code&gt; field:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb33&quot;&gt;&lt;pre class=&quot;sourceCode c&quot;&gt;&lt;code class=&quot;sourceCode c&quot;&gt;&lt;span id=&quot;cb33-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;struct&lt;/span&gt; User &lt;span class=&quot;op&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;int&lt;/span&gt; userId&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;char&lt;/span&gt; userName&lt;span class=&quot;op&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;int&lt;/span&gt; userAge&lt;span class=&quot;op&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But, this applies to &lt;em&gt;all&lt;/em&gt; usage of the &lt;code&gt;User&lt;/code&gt; struct…what
if we only wanted to preserve this property on a single function? You can’t
declare struct-level &lt;code&gt;const&lt;/code&gt; on a single argument!&lt;/p&gt;
&lt;p&gt;Instead, we can enforce this by making &lt;code&gt;userId&lt;/code&gt;’s type
parameterized:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb34&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb34-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt; uid &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb34-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    {&lt;span class=&quot;ot&quot;&gt; userId ::&lt;/span&gt; uid&lt;/span&gt;
&lt;span id=&quot;cb34-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; userName ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb34-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; userAge ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb34-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    }&lt;/span&gt;
&lt;span id=&quot;cb34-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb34-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Guaranteed not to change the ID&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb34-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processUser ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt; uid &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt; uid)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Is this constraint enforced because we carefully designed the structure of
our type? Is it constrained because we added compiler annotations or refinement
types or static analysis? Not quite! It truly did come for free.&lt;/p&gt;
&lt;p&gt;Or, consider a checklist item:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb35&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb35-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb35-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb35-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb35-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  {&lt;span class=&quot;ot&quot;&gt; updated ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UTCTime&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb35-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb35-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ,&lt;span class=&quot;ot&quot;&gt; items ::&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Status&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;)]&lt;/span&gt;
&lt;span id=&quot;cb35-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb35-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What if I wanted to write a function that processed items without adding or
removing any? Just each item in-place?&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb36&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb36-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb36-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Invariant: Preserves the ordering of items, and their number.&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb36-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb36-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;updateItems ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How can we make sure all our implementations are &lt;em&gt;elected&lt;/em&gt; to only be
implementations that don’t modify the length of &lt;code&gt;items&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;Again the answer can be: add quantification!&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb37&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb37-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb37-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  {&lt;span class=&quot;ot&quot;&gt; updated ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UTCTime&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb37-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ,&lt;span class=&quot;ot&quot;&gt; items ::&lt;/span&gt; t (&lt;span class=&quot;dt&quot;&gt;Status&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb37-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  }&lt;/span&gt;
&lt;span id=&quot;cb37-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb37-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Guaranteed to preseve the same number of `items` (or, number of item&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb37-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- &quot;slots&quot;), but can still perform IO to get the new Status and String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb37-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;updateItems ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Traversable&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt; t)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For an example of a possible implementation:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb38&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb38-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;updateSingleItem ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Status&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Status&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb38-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb38-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;updateItems ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Traversable&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt; t)&lt;/span&gt;
&lt;span id=&quot;cb38-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;updateItems c0 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb38-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  newItems &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;traverse&lt;/span&gt; updateSingleItem (items c0)&lt;/span&gt;
&lt;span id=&quot;cb38-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  newUpdated &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; getCurrentTime&lt;/span&gt;
&lt;span id=&quot;cb38-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Checklist&lt;/span&gt; newUpdated newItems)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Try as you might, you can’t make an implementation that gives you a different
number of items than when you started (even though you can rearrange or replace
them).&lt;/p&gt;
&lt;p&gt;Note that we are &lt;em&gt;not&lt;/em&gt; adding type parameters for abstraction or to be
able to use “exotic checklists” (&lt;code&gt;Checklist Maybe&lt;/code&gt;). Instead, we are
intentionally using them universally quantified in functions that process them,
in order to take advantage of these automatically enforced properties.&lt;/p&gt;
&lt;p&gt;This intersects a lot with the &lt;a href=&quot;https://reasonablypolymorphic.com/blog/higher-kinded-data/&quot;&gt;Higher-Kinded
Data&lt;/a&gt; pattern. Maybe we &lt;em&gt;do&lt;/em&gt; have data we want to have multiple
structural versions of:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb39&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb39-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    {&lt;span class=&quot;ot&quot;&gt; userName ::&lt;/span&gt; f &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; userAge ::&lt;/span&gt; f &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    }&lt;/span&gt;
&lt;span id=&quot;cb39-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Identity&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NullableUser&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserParser&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Parser&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserDocs&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Const&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Doc&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb39-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserPrinter&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case, a function like&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb40&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb40-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb40-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processUser ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; f&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;will give you a different, unique guarantee for every “shape” your user
has:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;UserF Maybe&lt;/code&gt;, the quantification ensures that the
null-or-present property of each field is preserved&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;UserF Parser&lt;/code&gt;, it ensures that all of the “parsing” logic,
and the set of strings that are validly parsed, is preserved&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;UserF (Const Doc)&lt;/code&gt;, it ensures that the per-field
&lt;code&gt;Doc&lt;/code&gt;/documentation is never changed or updated.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For an example, we can write:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb41&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb41-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb41-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processUser ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; f&lt;/span&gt;
&lt;span id=&quot;cb41-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb41-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;processUser user &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb41-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb41-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    { userName &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;toUpper&lt;/span&gt;) (userName user)&lt;/span&gt;
&lt;span id=&quot;cb41-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb41-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    , userAge &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) (userAge user)&lt;/span&gt;
&lt;span id=&quot;cb41-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb41-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is guaranteed to keep nullable fields null if &lt;code&gt;UserF Maybe&lt;/code&gt;,
preserve all successful parses if &lt;code&gt;UserF Parser&lt;/code&gt;, and leave any
field-level documentation unchanged if &lt;code&gt;UserF (Const Doc)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;All of these properties are &lt;em&gt;mathematically&lt;/em&gt; enforced,
&lt;em&gt;unconditionally&lt;/em&gt;. It doesn’t depend on any foreseen property of the
types or values we use. These guarantees free us to be able to confidently use
these functions without fear of invariants breaking.&lt;/p&gt;
&lt;p&gt;This game becomes even stronger when you consider dependent typing, where we
can express more complex relationships between type variables. For example, in
the case where you have a phantom type (like in &lt;a href=&quot;https://blog.jle.im/entry/introduction-to-singletons-1.html&quot;&gt;this
singletons tutorial&lt;/a&gt;):&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb42&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb42-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;DoorState&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Opened&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Closed&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Locked&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Door&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;s ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;DoorState&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb42-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processDoor ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Door&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Door&lt;/span&gt; s)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;processDoor&lt;/code&gt;, by virtue of taking &lt;code&gt;forall s&lt;/code&gt;, &lt;em&gt;must
leave&lt;/em&gt; the door state unchanged! It can never open a closed door, unlock a
locked door, etc.&lt;/p&gt;
&lt;p&gt;For things like &lt;a href=&quot;https://blog.jle.im/entry/fixed-length-vector-types-in-haskell.html&quot;&gt;fixed
length vectors&lt;/a&gt;, where the length &lt;code&gt;n&lt;/code&gt; parameter is the size, what
invariant do you think is preserved in:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb43&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb43-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb43-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;something ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vector&lt;/span&gt; n a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vector&lt;/span&gt; n a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We know that the length of the result must be the same as the length of the
input. Furthermore, with the &lt;code&gt;forall a&lt;/code&gt;, we know that every item in
the result must come from the input, but we might rearrange or change the
multiplicity of the occurrences as long as they add to the same original total
number. This might be a good candidate for a function like
&lt;code&gt;reverse&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Or, consider:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb44&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb44-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb44-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;somethingElse ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vector&lt;/span&gt; n a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vector&lt;/span&gt; (n &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;From this, we know that the original vector &lt;em&gt;must&lt;/em&gt; be non-empty!
Because of how the types must flow for whatever &lt;code&gt;n&lt;/code&gt; you give it, this
requires &lt;code&gt;n &amp;gt;= 1&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;ranking-up&quot;&gt;Ranking Up&lt;/h2&gt;
&lt;p&gt;Now that you see how useful it is to use type parameters and
&lt;code&gt;forall&lt;/code&gt;, can we &lt;em&gt;use&lt;/em&gt; this fact at the meta-level even within
our code itself?&lt;/p&gt;
&lt;h3 id=&quot;ensuring-structural-preservation&quot;&gt;Ensuring structural preservation&lt;/h3&gt;
&lt;p&gt;Let’s say we want to map an IO function over every item in our
&lt;code&gt;UserF&lt;/code&gt;, and return a new one. We know that whatever IO function we
use &lt;em&gt;must&lt;/em&gt; leave the actual “result” type unchanged. So that means we
must take a &lt;code&gt;forall a. f a -&amp;gt; h (f a)&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb45&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb45-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;traverseUser&lt;/span&gt;
&lt;span id=&quot;cb45-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt; h&lt;/span&gt;
&lt;span id=&quot;cb45-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; a&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; f a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; h (g a))&lt;/span&gt;
&lt;span id=&quot;cb45-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; f&lt;/span&gt;
&lt;span id=&quot;cb45-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; h (&lt;span class=&quot;dt&quot;&gt;UserF&lt;/span&gt; g)&lt;/span&gt;
&lt;span id=&quot;cb45-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;traverseUser f u &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; f (userName u) &lt;span class=&quot;op&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; f (userAge u)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here again we use the trick above to generalize for all
&lt;code&gt;Applicative h&lt;/code&gt; instead of concretely &lt;code&gt;IO&lt;/code&gt;, so we can know
that the final action can’t sneak in stray IO.&lt;/p&gt;
&lt;h3 id=&quot;ensuring-lexically-confined-resources&quot;&gt;Ensuring lexically-confined
resources&lt;/h3&gt;
&lt;p&gt;We can also use this property in phantom types to enforce lexically-confined
resources. Let’s say we are simulating local variables in an
&lt;code&gt;IntMap&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb46&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb46-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt; getMemory ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IntMap&lt;/span&gt; v }&lt;/span&gt;
&lt;span id=&quot;cb46-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;initVar ::&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; v) &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;initVar x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; state &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \(&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; mp) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; IM.lookupMax mp &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; IM.insert &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; x mp)&lt;/span&gt;
&lt;span id=&quot;cb46-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (i, _) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; (i &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;), &lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; IM.insert (i &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) x mp)&lt;/span&gt;
&lt;span id=&quot;cb46-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;readVar ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; v) v&lt;/span&gt;
&lt;span id=&quot;cb46-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;readVar (&lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; i) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; gets ((&lt;span class=&quot;op&quot;&gt;IM.!&lt;/span&gt; i) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; getMemory)&lt;/span&gt;
&lt;span id=&quot;cb46-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;writeVar ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; v) ()&lt;/span&gt;
&lt;span id=&quot;cb46-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;writeVar (&lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; i) x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; modify (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; IM.insert i x &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; getMemory)&lt;/span&gt;
&lt;span id=&quot;cb46-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;runWithMemory ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; v) a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb46-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;runWithMemory &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;`evalState`&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; IM.empty)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(By the way, what do we gain from having the state be &lt;code&gt;IntMap v&lt;/code&gt;
parametric on &lt;code&gt;v&lt;/code&gt;? What guarantees/invariants do we get, what sort of
actions do we forbid the library itself from doing? Is it possible to have a
default-initialized variable?)&lt;/p&gt;
&lt;p&gt;We can run operations like:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb47&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb47-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;getFib ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;) &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb47-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;getFib n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb47-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; initVar &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb47-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; initVar &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb47-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    replicateM_ n &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb47-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        newSum &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;+&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; readVar a &lt;span class=&quot;op&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; readVar b&lt;/span&gt;
&lt;span id=&quot;cb47-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        writeVar a &lt;span class=&quot;op&quot;&gt;=&amp;lt;&amp;lt;&lt;/span&gt; readVar b&lt;/span&gt;
&lt;span id=&quot;cb47-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        writeVar b newSum&lt;/span&gt;
&lt;span id=&quot;cb47-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    readVar b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb48&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb48-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb48-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; runWithMemory (getFib &lt;span class=&quot;dv&quot;&gt;10&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb48-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb48-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;55&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But now our variables are not actually scoped. We could, for instance, run
&lt;code&gt;runWithMemory&lt;/code&gt; &lt;em&gt;inside&lt;/em&gt; itself:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb49&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb49-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;myAction ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;) a&lt;/span&gt;
&lt;span id=&quot;cb49-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;myAction &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;                 &lt;span class=&quot;co&quot;&gt;-- new memory starts out empty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb49-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  v &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; initVar &lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt;        &lt;span class=&quot;co&quot;&gt;-- memory is now (0, &quot;hello&quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb49-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;let&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; runWithMemory &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;  &lt;span class=&quot;co&quot;&gt;-- new memory starts out empty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb49-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        readVar v             &lt;span class=&quot;co&quot;&gt;-- runtime error, looking up '0' in empty map!&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb49-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;co&quot;&gt;-- ..&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb49-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;co&quot;&gt;-- ..&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now &lt;code&gt;readVar v&lt;/code&gt; will fail! Remember that &lt;code&gt;v&lt;/code&gt; is
&lt;code&gt;Var 0&lt;/code&gt;, but that &lt;code&gt;0&lt;/code&gt; key only has meaning in the outer
scope. In the inner scope, &lt;code&gt;0&lt;/code&gt; refers to a different
&lt;code&gt;IntMap&lt;/code&gt;, where it is undefined.&lt;/p&gt;
&lt;p&gt;We can also do something silly like returning a &lt;code&gt;Var&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb50&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb50-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; runWithMemory (initVar &lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb50-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And now that var exists outside its scope. Its binding is gone, so the name
no longer refers to anything meaningful.&lt;/p&gt;
&lt;p&gt;We can prevent this by associating every variable with the scope that created
it. Then we can ensure that &lt;code&gt;runWithMemory&lt;/code&gt; requires the scope
phantom to never be a part of the final output:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb51&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb51-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; s v &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt; getMemory ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IntMap&lt;/span&gt; v }&lt;/span&gt;
&lt;span id=&quot;cb51-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;initVar ::&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; s v) (&lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; s)&lt;/span&gt;
&lt;span id=&quot;cb51-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;readVar ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; s v) v&lt;/span&gt;
&lt;span id=&quot;cb51-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;writeVar ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Var&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; s v) ()&lt;/span&gt;
&lt;span id=&quot;cb51-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;runWithMemory ::&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; s&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; s v) a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb51-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;runWithMemory &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;`evalState`&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Memory&lt;/span&gt; IM.empty)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, a &lt;code&gt;Var s&lt;/code&gt; must come from a &lt;code&gt;Memory s v&lt;/code&gt; with the
&lt;em&gt;same&lt;/em&gt; scope &lt;code&gt;s&lt;/code&gt;. It is associated with that scope, and no
others. The &lt;code&gt;forall&lt;/code&gt; here ensures that the action being given cannot
unify with any external &lt;code&gt;s&lt;/code&gt;: the scope is freshly created by
&lt;code&gt;runWithMemory&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Right off the bat, this prevents passing variables into nested calls (the
first var’s &lt;code&gt;s&lt;/code&gt; is different than the inner scope’s &lt;code&gt;s&lt;/code&gt;),
but this also prevents variables from leaking. That’s because the result type
&lt;code&gt;a&lt;/code&gt; must be fully &lt;em&gt;independent&lt;/em&gt; of the &lt;code&gt;s&lt;/code&gt;, so
returning a &lt;code&gt;Var s&lt;/code&gt; is illegal, since that would require the
&lt;code&gt;a&lt;/code&gt; to depend on &lt;code&gt;s&lt;/code&gt;, which escapes the scope of the
&lt;code&gt;forall&lt;/code&gt;. (This is exactly how the &lt;code&gt;ST&lt;/code&gt; monad works in GHC
standard libraries, actually.)&lt;/p&gt;
&lt;p&gt;By requiring the &lt;em&gt;caller&lt;/em&gt; to give up control of the &lt;code&gt;s&lt;/code&gt;, we
ensure lexical confinement both of the library and of the user-given
continuation. Now this safety doesn’t come from carefully tracking where
variables came from. Instead, it is assured through the universality of the
&lt;code&gt;forall&lt;/code&gt; and the unconditional properties it enforces.&lt;/p&gt;
&lt;h2 id=&quot;habits-to-build&quot;&gt;Habits to Build&lt;/h2&gt;
&lt;p&gt;Let’s look at what it looks like to recognize this principle in practice, and
use it in your code. Let’s imagine we have a function that you can use to deploy
a new &lt;code&gt;Config&lt;/code&gt; in your environment:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb52&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb52-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;deployConfig ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But, deployment is a bit expensive. So we want to deduplicate our deploys:
deploying the same &lt;code&gt;Config&lt;/code&gt; twice would be a no-op. We can do this by
keeping a &lt;code&gt;Config&lt;/code&gt; in an &lt;code&gt;IORef&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb53&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb53-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | returns True if changed, otherwise False if already deployed&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;updateConfig ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IORef&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;updateConfig cache newConfig &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    oldConfig &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; readIORef cache&lt;/span&gt;
&lt;span id=&quot;cb53-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; oldConfig &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; newConfig&lt;/span&gt;
&lt;span id=&quot;cb53-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            deployConfig newConfig&lt;/span&gt;
&lt;span id=&quot;cb53-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            writeIORef cache newConfig&lt;/span&gt;
&lt;span id=&quot;cb53-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This &lt;em&gt;works&lt;/em&gt;, but after learning about the principles in this post,
that type signature should feel a little bit suspicious to you. Note that our
function never actually &lt;em&gt;inspects&lt;/em&gt; the &lt;code&gt;Config&lt;/code&gt; at all. The
logic is independent. Would there be any value in pulling out the caching logic
generically?&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb54&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb54-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;cachedUpdate ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; (a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IORef&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;cachedUpdate action cache newVal &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    oldVal &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; readIORef cache&lt;/span&gt;
&lt;span id=&quot;cb54-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; oldVal &lt;span class=&quot;op&quot;&gt;==&lt;/span&gt; newVal&lt;/span&gt;
&lt;span id=&quot;cb54-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;kw&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            action newVal&lt;/span&gt;
&lt;span id=&quot;cb54-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            writeIORef cache newVal&lt;/span&gt;
&lt;span id=&quot;cb54-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;updateConfig ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IORef&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;updateConfig &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; cachedUpdate deployConfig&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s presume that we never intend to reuse &lt;code&gt;cachedUpdate&lt;/code&gt;. So, we
just increased our total lines of code…and for what? What does
&lt;code&gt;cachedUpdate&lt;/code&gt; get us?&lt;/p&gt;
&lt;p&gt;Firstly, in the original monomorphic &lt;code&gt;updateConfig&lt;/code&gt;, written
directly against &lt;code&gt;Config&lt;/code&gt;, there is so much that could go wrong.
Maybe you could mis-handle the configuration or accidentally modify it. You
could set certain fields to fixed values. You might end up deploying a
configuration that was never passed in directly.&lt;/p&gt;
&lt;p&gt;In our &lt;code&gt;cachedUpdate&lt;/code&gt; implementation, we are &lt;em&gt;sure&lt;/em&gt; that
any &lt;code&gt;Config&lt;/code&gt; deployed will &lt;em&gt;only&lt;/em&gt; come &lt;em&gt;directly&lt;/em&gt; from
calls to &lt;code&gt;updateConfig&lt;/code&gt;. No other operations are possible.&lt;/p&gt;
&lt;p&gt;Secondly, the type signature of &lt;code&gt;cachedUpdate&lt;/code&gt; tells us a lot more
about what &lt;code&gt;cachedUpdate&lt;/code&gt;’s intended logic is and what exactly it can
support. Let’s say in the future, a new requirement comes: Deploy a “default”
configuration if &lt;code&gt;deployConfig&lt;/code&gt; ever fails.&lt;/p&gt;
&lt;p&gt;You &lt;em&gt;want&lt;/em&gt; something as drastic as this to require you to change your
types and your contracts. In fact, if a new requirement comes along and you are
able to implement it without changing your types, that should be extremely scary
to you, because you previously allowed way too many potentially invalid programs
to compile.&lt;/p&gt;
&lt;p&gt;If we were to add such a change (“deploy a default &lt;code&gt;Config&lt;/code&gt;”), it
&lt;em&gt;should&lt;/em&gt; have us go back to the type signature of
&lt;code&gt;cachedUpdate&lt;/code&gt; and see how it must change in order for us to support
it. That process of interrogation makes us think about what we actually want to
do and how it would fundamentally change our data flow.&lt;/p&gt;
&lt;p&gt;If you subscribe to &lt;a href=&quot;https://en.wikipedia.org/wiki/SOLID&quot;&gt;“SOLID”
programming&lt;/a&gt;, this should all remind you of “Dependency Inversion”.&lt;/p&gt;
&lt;p&gt;Basically: treat all monomorphic code with suspicion. It may be a symptom of
you trying to hold on to more control, when you should be letting go.&lt;/p&gt;
&lt;p&gt;Another example of the same instinct: say you have a function that broadcasts
a notification to a list of users:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb55&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb55-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;notifyUsers ::&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;User&lt;/span&gt;] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;How can this branch on the users? Really, in any way. It could inspect each
user, craft different messages for admins vs. regular users, silently skip
certain users, log personal data, vary behavior based on user properties…&lt;/p&gt;
&lt;p&gt;Compare that to:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb56&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb56-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;notifyAll ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Foldable&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; t recipient &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (recipient &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;notifyAll&lt;/code&gt; is parametric in &lt;code&gt;recipient&lt;/code&gt;. It cannot
inspect who it is notifying. It cannot skip recipients based on their properties
or treat admins differently from regular users. All of that “policy” logic is
forced to live in the caller. The broadcasting logic itself &lt;em&gt;must&lt;/em&gt; be
uniform: it calls &lt;code&gt;send&lt;/code&gt; indiscriminately on some fixed subset of the
elements. It can pick a subset to send to, but it can’t pick what gets included
in that subset based on the properties of the recipient.&lt;/p&gt;
&lt;h2 id=&quot;embracing-unconditional-election&quot;&gt;Embracing Unconditional Election&lt;/h2&gt;
&lt;p&gt;What sort of control are you trying to hang on to in life, in a way that puts
you in your own prison?&lt;/p&gt;
&lt;p&gt;To me, the fact that making code more polymorphic and giving up information
is valuable not just for abstraction, but for taking advantage of universal
properties, was a surprising one. But ever since I started writing Haskell, it’s
a fact that I take advantage of every day. So, next time you see the
opportunity, try thinking about what that parametric &lt;code&gt;forall&lt;/code&gt; can do
for you. Take advantage of the doctrine of Haskell predestination that arrives
from properties of logic determined before our universe ever existed.&lt;/p&gt;
&lt;h3 id=&quot;the-next-step&quot;&gt;The Next Step&lt;/h3&gt;
&lt;p&gt;Embracing Total Depravity and Unconditional Election should redefine your
relationship with your code. But not all code lives in the nice pure world where
we can cordon off effects. &lt;code&gt;forall a. [a] -&amp;gt; [a]&lt;/code&gt; is very
different than &lt;code&gt;forall a. [a] -&amp;gt; IO [a]&lt;/code&gt;, after all.&lt;/p&gt;
&lt;p&gt;To extend these boundaries to useful code, we have to deal with that boundary
between the world of the pure and the world where &lt;a href=&quot;https://xkcd.com/1312/&quot;&gt;things actually happen for real&lt;/a&gt;. We’ll explore
the nuances of that boundary in the next chapter of &lt;a href=&quot;https://blog.jle.im/entries/series/+five-point-haskell.html&quot;&gt;Five-Point
Haskell&lt;/a&gt;, &lt;strong&gt;Limited Atonement&lt;/strong&gt;.&lt;/p&gt;
&lt;h2 id=&quot;special-thanks&quot;&gt;Special Thanks&lt;/h2&gt;
&lt;p&gt;I am very humbled to be supported by an amazing community, who make it
possible for me to devote time to researching and writing these posts. Very
special thanks to my supporter at the “Amazing” level on &lt;a href=&quot;https://www.patreon.com/justinle/overview&quot;&gt;patreon&lt;/a&gt;, Josh Vera! :)&lt;/p&gt;
&lt;section class=&quot;footnotes footnotes-end-of-document&quot; id=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;This is the “Fast and Loose Reasoning” condition (Danielsson et
al.).&lt;a class=&quot;footnote-back&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn2&quot;&gt;&lt;p&gt;Excluding &lt;code&gt;unsafePerformIO&lt;/code&gt;,
&lt;code&gt;unsafeCoerce&lt;/code&gt;, etc.&lt;a class=&quot;footnote-back&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fnref2&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn3&quot;&gt;&lt;p&gt;If you disallow &lt;code&gt;repeat&lt;/code&gt;,
&lt;code&gt;forall a. a -&amp;gt; [a]&lt;/code&gt; is actually isomorphic to the natural
numbers!&lt;a class=&quot;footnote-back&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fnref3&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn4&quot;&gt;&lt;p&gt;This is actually the essence of Yoneda lemma I think, whatever
that is.&lt;a class=&quot;footnote-back&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fnref4&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn5&quot;&gt;&lt;p&gt;It’s &lt;code&gt;forall a. [a] -&amp;gt; Const Int a&lt;/code&gt;!&lt;a class=&quot;footnote-back&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fnref5&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Tue, 04 Aug 2026 18:15:09 +0000</pubDate>
</item>
<item>
	<title>in Code: LLMs Will Cheese Your Types: Fighting Back in Haskell</title>
	<guid isPermaLink="true">https://blog.jle.im/entry/llms-and-haskell-1-constraint-evading-behavior.html</guid>
	<link>https://blog.jle.im/entry/llms-and-haskell-1-constraint-evading-behavior.html</link>
	<description>&lt;p&gt;Sooo yes it’s true, I’ve been integrating LLMs and agentic coding tools in my
Haskell coding since the beginning of this year for a lot of my projects, both
personal and professional. I do all of my programming in Haskell, a language
with a very expressive type system that encourages “type-driven
development”.&lt;/p&gt;
&lt;p&gt;Working with LLMs on writing Haskell is a very unique experience; a lot of
similarities and patterns exist with “normal” agentic coding, but I think the
hot flame of LLMs meeting the cool stone of Haskell yields a lot of wholly
unique optimal paths, workflow quirks, and failure modes.&lt;/p&gt;
&lt;p&gt;This post will focus on understanding what I call &lt;strong&gt;constraint-evading
behavior&lt;/strong&gt; in LLMs as it relates to writing Haskell effectively with LLM
collaboration.&lt;/p&gt;
&lt;p&gt;Consider this Part 1 of a series. This post is about how to spot and
understand a very common failure mode of LLMs once you actually &lt;em&gt;are&lt;/em&gt;
writing “type-driven” Haskell.&lt;/p&gt;
&lt;h2 id=&quot;the-ideal-case-haskell-and-llms&quot;&gt;The Ideal Case: Haskell and LLMs&lt;/h2&gt;
&lt;p&gt;Now, my personal opinion and wishful hope is that Haskell &lt;em&gt;should&lt;/em&gt; be
to agentic software engineering what Lean is in agentic research mathematics: a
framework for LLMs to self-construct the scaffolding they need to guide
themselves to their correct goal.&lt;/p&gt;
&lt;p&gt;I don’t believe that “correctness at generation-time” is a plausible goal,
not today in 2026, and probably not any time soon. Motion towards correctness is
asymptotic. You might get close enough sometimes, but the long tail of
correctness is…long. Even the most full-vibed frontier-model projects have &lt;a href=&quot;https://bun.com/blog/bun-in-rust&quot;&gt;large suites of tests&lt;/a&gt; that exist to
guide the agent. Agentic coding in five years will not be “spit out the correct
program”, it will be “set up the best scaffolding that guides the
implementation”.&lt;/p&gt;
&lt;p&gt;Remember: the point of types in Haskell isn’t to catch bad code. It’s to
direct how you write and structure your code, guide you down the most productive
paths, help you concretely iterate on what design you want, and make the actual
code-writing time a smooth flow process.&lt;/p&gt;
&lt;p&gt;Each part of this is antithetical to how LLMs are trained to use types and
write code.&lt;/p&gt;
&lt;p&gt;To this end, I try to structure my codebases with the correct scaffolding to
help augment the intuition of path exploration: AI has to search which paths are
the “most promising”, so I structure my code-base to quickly kill off paths that
are most likely to be dead-ends or lead to unmaintainable code, and to channel
AI exploration along more promising paths. The greatest tools are types,
compilers, warnings, hints.&lt;/p&gt;
&lt;p&gt;A lot of these are the same things we teach to human coders, and are not very
different than what I write about regularly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href=&quot;https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/&quot;&gt;Parse,
Don’t Validate&lt;/a&gt;: set up your types to make invalid states unrepresentable. AI
will, by nature, NOT do this, even the latest frontier models. They usually slip
into defensive programming (adding &lt;code&gt;isNull&lt;/code&gt; checks everywhere,
boolean checks for constructors like &lt;code&gt;isNothing&lt;/code&gt; /
&lt;code&gt;isObject&lt;/code&gt; / &lt;code&gt;isArray&lt;/code&gt; instead of just pattern matching,
filtering lists for duplicates instead of using &lt;code&gt;Data.Set&lt;/code&gt;, adding
precondition assertions, etc.), and it’s usually up to the human driver to stop
and consciously create data types that model the domain correctly, structurally
prevent invalid states, enforce pre- and post-conditions via structurally
verified types and not boolean checks, avoid boolean blindness, etc.&lt;/p&gt;
&lt;p&gt;Either that, or dedicate an entire session or planning step to clarifying
these domain requirements in their types.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use warnings and linters enthusiastically:
&lt;code&gt;-Werror=incomplete-patterns&lt;/code&gt; of course, and the default
&lt;code&gt;-Werror&lt;/code&gt; usually covers a lot of AI failure modes (leaving in dead
code, leaving in arguments in functions for no reason and killing opportunities
for abstraction).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add robust test suites for things that cannot be tested within the types,
but also explicitly laying out integration tests: something which LLMs seem
pretty allergic to without proper prompting.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;From my own empirical observations, all of these things are against the
nature of how even frontier LLM models operate, embedded deeply from being
trained on terabytes of untyped Python and React slop.&lt;/p&gt;
&lt;p&gt;So, I’ve been gathering a list of what I call &lt;strong&gt;constraint-evading
behavior&lt;/strong&gt;: when the requirements and constraints are explicitly and
unambiguously stated, but LLM nature &lt;em&gt;desperately&lt;/em&gt; tries to circumvent
them because they cannot keep up a sustained fight against their deepest base
impulses.&lt;/p&gt;
&lt;h2 id=&quot;decisions-that-require-scrutiny&quot;&gt;Decisions that require scrutiny&lt;/h2&gt;
&lt;p&gt;There is a class of failure modes where AI makes a risky decision that a
human &lt;em&gt;might&lt;/em&gt; reasonably make in the rare case that it is justified. But,
usually, it will be making this decision because it’s the &lt;span class=&quot;llm&quot;&gt;“simplest approach”&lt;/span&gt;. It cannot separate questionable
decisions based on reasoned justification and questionable decisions based on
flawed heuristics like “simplicity” or effort.&lt;/p&gt;
&lt;p&gt;We have the general failure modes like this that people mention for all
programming languages:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“This test doesn’t pass, so let’s disable it”&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“Let’s feed this test junk data so it will
pass.”&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“Let’s have this function detect if it’s in a test
environment and behave differently if it is.”&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But here are some that I feel are specific to working with LLMs in
Haskell.&lt;/p&gt;
&lt;h3 id=&quot;disabling-warnings-for-escape-hatches&quot;&gt;Disabling Warnings for Escape
Hatches&lt;/h3&gt;
&lt;p&gt;A lot of the “type safety” of Haskell can be bypassed trivially by disabling
warnings, and a lot of the “escape hatches” within the language are disabled via
linting rules (&lt;code&gt;Prelude.error&lt;/code&gt;, &lt;code&gt;unsafeCoerce&lt;/code&gt;, etc.)&lt;/p&gt;
&lt;p&gt;LLMs will often add warning suppressors that straight-up disable warning or
lint checks.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“Let me add &lt;code&gt;-Wno-incomplete-patterns&lt;/code&gt; to this
file so that it can compile, because this pattern is inaccessible anyway in
normal operation”&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“Let me add &lt;code&gt;HLINT ignore&lt;/code&gt; to this build so
that I can bypass the hlint rule forbidding
&lt;code&gt;Prelude.error&lt;/code&gt;”&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s pretty straightforward to add post-edit hooks to forbid edits of this
pattern…but I think this is a good platonic example of what I mean by
“constraint-evading behavior”.&lt;/p&gt;
&lt;p&gt;Maybe sometimes you &lt;em&gt;should&lt;/em&gt; be disabling warnings in your files.
Maybe sometimes you &lt;em&gt;should&lt;/em&gt; be using &lt;code&gt;Prelude.error&lt;/code&gt;. A human
&lt;em&gt;might&lt;/em&gt; look at the situation at hand and think, “this is one of those
rare cases where &lt;code&gt;Prelude.error&lt;/code&gt; is correct”, or “this is one of
those rare cases where that warning is incorrect.”&lt;/p&gt;
&lt;p&gt;But should you trust an LLM to make that judgment call? Fuck no. 99% of the
time, it is only doing this as the easy way out. Yes, every once in a while it
will discover a legitimate reason, but has not properly weighted
&lt;code&gt;P(legitimate | attempted)&lt;/code&gt;. Most of the attempts will be as hacks,
and it will be more than happy to follow through with an attempt if it truly is
the &lt;span class=&quot;llm&quot;&gt;“simplest way”&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Understanding something as constraint-evading behavior doesn’t mean “ban this
behavior”. It is meant to help highlight situations where 99% of the time, it’s
the LLM taking the easy or fast way out instead of the correct one. In these
cases, it’s imperative that a &lt;em&gt;human&lt;/em&gt; is what is adding the warning
silencing or hlint ignore.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;llm&quot;&gt;“The simplest approach is…”&lt;/span&gt; is the worst thing you
ever want to see in a thought trace, because it’s a sure guaranteed sign that
they are about to spew the most ridiculous and awful code you’ve ever seen.&lt;/p&gt;
&lt;p&gt;Breaking down the matrix:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;P(not legitimate &amp;amp;&amp;amp; not attempted)&lt;/code&gt;: Correct avoidance
of problematic behavior&lt;/li&gt;
&lt;li&gt;&lt;code&gt;P(legitimate &amp;amp;&amp;amp; not attempted)&lt;/code&gt;: The noble struggle. The
rare case that you are really justified in this normally risky behavior, but out
of misguided principle you do not. This is a bias and failure mode more likely
to be hit by humans. Or at least one human (that’s me).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;P(not legitimate &amp;amp;&amp;amp; attempted)&lt;/code&gt;: The failure mode where
an LLM will choose this out of a flawed heuristic like simplicity or
effort.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;P(legitimate &amp;amp;&amp;amp; attempted)&lt;/code&gt;: The rare case that you are
justified in normally risky behavior, and the LLM was correct in attempting
it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As this matrix moves towards more favorable marginals, my stance here will
slowly change. But for now, the numbers I roughly see encourage continued
vigilance.&lt;/p&gt;
&lt;h3 id=&quot;ignoring-types-in-planned-code&quot;&gt;Ignoring types in planned code&lt;/h3&gt;
&lt;p&gt;Let’s say you plan your perfect types that match your domain exactly,
forbidding all invalid states, perfectly monotonic parsers. Then you go to
execute that plan. Unfortunately, LLMs will not hesitate to throw away your
carefully designed types.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“The plan says to use &lt;code&gt;NonEmpty Int&lt;/code&gt; as an
argument, but that would require changing too much. The simplest approach is to
just have it take &lt;code&gt;[Int]&lt;/code&gt; and check for empty lists”&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“We planned to use this existing enum, but it doesn’t have
a branch we need. Instead of adding a branch, we’ll have it take
&lt;code&gt;String&lt;/code&gt; instead.”&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“Instead of a structured data type, let’s just use
stringly encoded lists or records with separators we can parse out.”&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“We have to call &lt;code&gt;fooFunc&lt;/code&gt;, which returns an
&lt;code&gt;Int&lt;/code&gt;, so let’s have our function return an &lt;code&gt;Int&lt;/code&gt; instead
of a &lt;code&gt;Natural&lt;/code&gt; like our original plan”.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“The plan requires adding a field to this record, but to
keep things simple, let’s just take a &lt;code&gt;Data.Aeson.Object&lt;/code&gt; instead so
we can return whatever fields we want.”&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;llm&quot;&gt;“The plan was to have this function be
&lt;code&gt;Binary a =&amp;gt;&lt;/code&gt;, but this type we defined doesn’t have a
&lt;code&gt;Binary&lt;/code&gt; instance yet, so we’ll just use &lt;code&gt;Show a =&amp;gt;&lt;/code&gt;
instead. It’s the simplest approach.”&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is especially frustrating because often these plans and types were
chosen to enforce some domain invariant or guide the proper and correct
development, but LLMs will almost never hesitate before throwing away all of the
planned type safety.&lt;/p&gt;
&lt;p&gt;These are all reasonable things that a &lt;em&gt;human&lt;/em&gt; might reconsider during
the process of following out a plan. Maybe we originally wanted to use
&lt;code&gt;NonEmpty Int&lt;/code&gt;, but upon closer examination, we realized it does have
to be an &lt;code&gt;[Int]&lt;/code&gt;. This is the natural process of iterating on a
design, as you discover more truths about the domain.&lt;/p&gt;
&lt;p&gt;But, that call should be a discussed one, not an implicit one…it took thought
to make the original plan, so it should take thought to change the plan. Most of
the time AI makes these decisions, it isn’t out of discovered truths about the
domain, but rather because of needless heuristics to minimize effort, or a
misunderstanding of the intent and design of the original plan (especially if
after a compaction). Things that should be discussed explicitly, not done
implicitly. Outside of planning mode, LLMs aren’t seeking out the truth of the
domain, they’re seeking out the path of least resistance.&lt;/p&gt;
&lt;p&gt;Note that this is different than weakening types in &lt;em&gt;existing&lt;/em&gt;
functions. That’s a failure mode I rarely see in practice. Instead, when running
into a wall with the type of existing code, there’s another failure mode that’s
much more common…&lt;/p&gt;
&lt;h2 id=&quot;structural-type-abuse&quot;&gt;Structural Type Abuse&lt;/h2&gt;
&lt;p&gt;Sometimes AI will optimize preserving &lt;em&gt;existing&lt;/em&gt; types (especially
across package boundaries) instead of changing them.&lt;/p&gt;
&lt;h3 id=&quot;string-stuffing&quot;&gt;String Stuffing&lt;/h3&gt;
&lt;p&gt;I like to call this “string stuffing”. We like to make nice semantic types
that match our domain and only allow the creation of meaningful values…but LLMs
absolutely &lt;em&gt;love&lt;/em&gt; to find ways to twist these to save time. Strings, in
particular, are vulnerable because most Haskell types have &lt;code&gt;Show&lt;/code&gt;
instances.&lt;/p&gt;
&lt;p&gt;Consider a type for structured errors:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorEvent&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UnknownUser&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;DatabaseErrorCode&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;InvalidJSON&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;A.Value&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NetworkError&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeException&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Canceled&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CancelationReason&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb1-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And you have a function:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;handleRequest ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Request&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorEvent&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Response&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And we have to add a new handler for a new request type. Maybe this new
handler has a new type of error. Instead of adding a new structural error, LLMs
will find great joy in cleverly abusing the structure to invalidate the
domain.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;handleAddGroup ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;AddGroupRequest&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorEvent&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Response&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;handleAddGroup req&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; validGroup &lt;span class=&quot;fu&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- ..&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;UnknownUser&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Invalid group: &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;group&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb3-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; getGroup req&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;llm&quot;&gt;“Invalid groups are not a valid &lt;code&gt;ErrorEvent&lt;/code&gt;.
The simplest solution is to put the error in &lt;code&gt;UnknownUser&lt;/code&gt;, which can
take a group name.”&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;And yes, this depravity knows no bounds. You would be surprised by the
creative ways AI will discover to stuff your strings. These are all things I
have personally witnessed in frontier models.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;handleAddGroup ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;AddGroupRequest&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorEvent&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Response&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;handleAddGroup req&lt;/span&gt;
&lt;span id=&quot;cb4-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;co&quot;&gt;-- ...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;DatabaseErrorCode&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb4-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;handleAddGroup ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;AddGroupRequest&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorEvent&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Response&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;handleAddGroup req&lt;/span&gt;
&lt;span id=&quot;cb4-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;co&quot;&gt;-- ...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;InvalidJSON&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        A.object [&lt;span class=&quot;st&quot;&gt;&quot;errorType&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.=&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Invalid group&quot;&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;group&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;group&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb4-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; getGroup req&lt;/span&gt;
&lt;span id=&quot;cb4-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;handleAddGroup ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;AddGroupRequest&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorEvent&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Response&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;handleAddGroup req&lt;/span&gt;
&lt;span id=&quot;cb4-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;co&quot;&gt;-- ...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NetworkError&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        toException (&lt;span class=&quot;fu&quot;&gt;userError&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Invalid group: &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;group&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; getGroup req&lt;/span&gt;
&lt;span id=&quot;cb4-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;handleAddGroup ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;AddGroupRequest&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Either&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorEvent&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Response&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb4-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;handleAddGroup req&lt;/span&gt;
&lt;span id=&quot;cb4-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;co&quot;&gt;-- ...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Left&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Canceled&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;This&lt;/em&gt; type of failure mode is probably more egregious than the others
in that it is very rare that this is ever the intended behavior. The entire
reason we picked an ADT to describe our type is so that we can structurally
match on them later, treat them semantically, etc., and string stuffing to abuse
our structure has pretty much zero legitimate use-cases other than quickly
hacking a printf debug session. However, it truly is often &lt;span class=&quot;llm&quot;&gt;“the simplest solution”&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The main way I deal with this is to be very very careful of putting abusable
fields like &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;A.Value&lt;/code&gt;, &lt;code&gt;Int&lt;/code&gt;,
&lt;code&gt;SomeException&lt;/code&gt; in my data types…just a single field or branch that
has an abusable field, AI &lt;em&gt;will&lt;/em&gt; find it, and you &lt;em&gt;will&lt;/em&gt; feel very
stupid for missing it. But hey, the whole point of using properly structured
values was to avoid stuffing things into &lt;code&gt;String&lt;/code&gt; too, right? The fix
for this is a fix that helps human coders, too.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorEvent&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UnknownUser&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;UserName&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;DatabaseErrorCode&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ErrorCode&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;InvalidJSON&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ParseError&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NetworkError&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NetworkException&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Canceled&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;CancelationReason&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;field-abuse&quot;&gt;Field Abuse&lt;/h3&gt;
&lt;p&gt;This isn’t just limited to sum types. AI will often stuff data into record
value fields that are strings or lists.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Report&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Report&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    {&lt;span class=&quot;ot&quot;&gt; reportName ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; reportAuthors ::&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb6-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; reportDate ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Day&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    , &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;llm&quot;&gt;“I need to specify the affiliations of the authors in this
report. The simplest solution is to add this to the list of
&lt;code&gt;reportAuthors&lt;/code&gt; after the authors.”&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;AI will also stuff sentinel values everywhere: instead of changing the type
to take &lt;code&gt;Maybe Day&lt;/code&gt;, it might add &lt;code&gt;ModifiedJulianDay 0&lt;/code&gt;
for missing days.&lt;/p&gt;
&lt;p&gt;There’s also the dual, where the AI will be happy to &lt;em&gt;use&lt;/em&gt; existing
record fields in overloaded ways instead of adding a new field.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Targets&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Targets&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    {&lt;span class=&quot;ot&quot;&gt; fooTarget ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ,&lt;span class=&quot;ot&quot;&gt; barTarget ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    , &lt;span class=&quot;co&quot;&gt;-- ...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s say you need to add a new feature or code path that requires a new
target for a &lt;code&gt;baz&lt;/code&gt; service.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;llm&quot;&gt;“I need to get a new target…instead of adding a new field
to &lt;code&gt;Targets&lt;/code&gt;, let’s re-use &lt;code&gt;barTarget&lt;/code&gt;. That’s the
cleanest approach.”&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It will optimize keeping existing types instead of extending them to match
your domain as your domain expands, especially if those existing types cross a
library boundary.&lt;/p&gt;
&lt;p&gt;I believe there are three heuristics at play that drive this behavior.&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;&lt;p&gt;The heuristic to avoid extra risk in modifying upstream types across
library boundaries with heavy dependencies. This might be very risky behavior in
an untyped language like Python, where each type change might introduce new
regressions that are not immediately obvious. So, avoiding upstream type changes
avoids potential regression.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The heuristic to avoid extra &lt;em&gt;work&lt;/em&gt; in modifying upstream types
across library boundaries and compilation units. In Haskell, however, upstream
type changes &lt;em&gt;force&lt;/em&gt; you to address each possible regression point
downstream. So changing an upstream type will require you to address every place
it is used, which can be time-consuming and avoided by LLMs, especially if
compilation is expensive, or multiple new typeclass instances might need to be
added.&lt;/p&gt;
&lt;p&gt;I have literally seen LLMs say &lt;span class=&quot;llm&quot;&gt;“Adding this field would
require adding typeclass instances on several other types, which would be a huge
change. The simplest approach would be…”&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;However, updates require work &lt;em&gt;by design&lt;/em&gt;: API changes &lt;em&gt;should&lt;/em&gt;
require lots of thought, and the compiler enforcing that is the whole point.&lt;/p&gt;
&lt;p&gt;The big irony here is that these updates and changes are largely mechanical
in nature, and are exactly the boilerplatey task that LLMs are optimally good
for. These are the reasonable one-shots. So it’s kind of funny when you let an
agentic coder take on a “self-directed” mode, it refuses to use “itself” in the
way that a real human would for these smaller tasks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The heuristic to avoid extra risk in touching data types that might
already be used in prod code or databases. Config files that might have to be
updated to new schemas, inter-op with existing services that might not be easily
deployed in sync, working with data at rest…all of these are real risks you have
to manage when changing data types. In practice, a lot of our type changes will
&lt;em&gt;not&lt;/em&gt; be relevant to any of these concerns, but it is understandable that
the LLM would develop an instinct to blanket-avoid them.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These are also the types of failure modes that are most difficult to catch
during code review. Diff views will analyze that code has changed, so code that
&lt;em&gt;didn’t&lt;/em&gt; change is especially difficult for human monkey brains to spot,
with no green or red bright highlighting. You must be especially vigilant to
catch code that &lt;em&gt;did not&lt;/em&gt; change but &lt;em&gt;should&lt;/em&gt; have.&lt;/p&gt;
&lt;h3 id=&quot;resisting-new-types&quot;&gt;Resisting New Types&lt;/h3&gt;
&lt;p&gt;Sometimes AI &lt;em&gt;does&lt;/em&gt; modify existing sum types, but doesn’t quite adapt
existing code correctly.&lt;/p&gt;
&lt;p&gt;For example, if your domain has a specific meaningful universe:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Alaska&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Arkansas&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Arizona&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processState ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We might want to start supporting countries alongside US states. An LLM might
recognize that the domain needs to be expanded, but it might expand it
flatly:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Region&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Canada&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Mexico&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Alaska&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Arkansas&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Arizona&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processRegion ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Region&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb9-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;processRegion &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Canada&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Mexico&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    st &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; processState st&lt;/span&gt;
&lt;span id=&quot;cb9-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processState ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Region&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb9-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;processState &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Canada&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb9-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Mexico&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb9-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Alaska&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- actual logic&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The real solution would be to have all your pattern matches strictly reduce
the space of what they cover (and be monotonically decreasing), and to be
suspicious of “ignored case matches” that have dummy values like
&lt;code&gt;pure ()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Region&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Canada&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Mexico&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;USState&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Alaska&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Arkansas&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Arizona&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processRegion ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Region&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb10-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;processRegion &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Canada&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Mexico&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;USState&lt;/span&gt; st &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; processState st&lt;/span&gt;
&lt;span id=&quot;cb10-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;processState ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IO&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb10-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;processState &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Alaska&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;co&quot;&gt;-- actual logic&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All things that are code smells in normal human code (not necessarily wrong,
but invite further scrutiny), but are maybe amplified in the age of LLMs because
of a mis-tuned heuristic on not defining new types and instead trying to re-use
or abuse existing types.&lt;/p&gt;
&lt;p&gt;So, if there is some pressure against &lt;em&gt;modifying&lt;/em&gt; types, there might
be an even greater pressure against &lt;em&gt;adding&lt;/em&gt; types.&lt;/p&gt;
&lt;h2 id=&quot;meditations&quot;&gt;Meditations&lt;/h2&gt;
&lt;p&gt;None of these behaviors are blanket-wrong, but they usually signal that the
LLM is under stress or duress and attempting to find ways to take the easy or
“low-effort” path over the correct one. All of them are worth human intervention
and guidance as soon as possible, at least until the day where
&lt;code&gt;P(legitimate | attempted)&lt;/code&gt; approaches 1.&lt;/p&gt;
&lt;p&gt;Will there be a day when LLMs can generate the correct types to match the
domain, and resist their tendency to “defensive-program” their way into
correctness? Maybe. But I have rarely ever had Opus 4.8 crank out a sufficient
domain model for any non-trivial product. And, when I do reach a plan I find
sufficient, a few compactions later and all of the original motivations seem to
get washed out.&lt;/p&gt;
&lt;p&gt;There might be a way to uber-prompt all of these issues away, but I feel that
effectively using LLMs isn’t necessarily something you can address from the
prompt level: it’s something that demands constant vigilance and care. I’ve
found automated hooks (like forbidding warning-disabling, detecting hlint
bypassing…ask claude for help writing these lol) also help me flag areas that
need attention immediately.&lt;/p&gt;
&lt;p&gt;Who knows, maybe all of these things will be solved within a year. But I
still think of software development as something that’s worth scrutinizing for
anything of importance. As failure modes like these become less common…the long
tail of correctness, I predict, will remain long.&lt;/p&gt;
&lt;p&gt;Anyway, that’s it for &lt;em&gt;this&lt;/em&gt; topic, but if I find the time I’ll
continue on with some other topics I’ve been thinking about during my Haskell
and LLM adventures:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;Effective ways to plan out Haskell code and approaches, ways to encourage
the best possible types&lt;/li&gt;
&lt;li&gt;Structuring your libraries mechanically for the best build-and-test rapid
development cycle&lt;/li&gt;
&lt;li&gt;Starting and maintaining full “vibe-coded” Haskell projects (when
correctness is not critical) and the advantages over untyped vibes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let me know if there are any you’d like to see first, or if there are other
aspects of Haskell LLM usage you might like me to address!&lt;/p&gt;
&lt;p&gt;And hey, since we’re here, why not train your agentic friend to take these
ideas to heart?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;https://blog.jle.im/entry/llms-and-haskell-1-constraint-evading-behavior.html&amp;gt;

Read this post and make me a Claude Code skill that reviews a Haskell diff for
the constraint-evading compromises it describes: suppressed warnings, string
and field stuffing, and weakened types that differ from any recorded plans.
Some of these hide in code that did not change but should have, so the skill
should start from the functions that changed and evaluate how they use or abuse
the types involved, but also spot type changes that look suspicious.&lt;/code&gt;&lt;/pre&gt;</description>
	<pubDate>Wed, 22 Jul 2026 16:10:56 +0000</pubDate>
</item>
<item>
	<title>in Code: Extreme Haskell: Typed Expression EDSLs (Part 1)</title>
	<guid isPermaLink="true">https://blog.jle.im/entry/extreme-haskell-typed-expression-edsls-1.html</guid>
	<link>https://blog.jle.im/entry/extreme-haskell-typed-expression-edsls-1.html</link>
	<description>&lt;p&gt;I always say, inside every Haskeller there are two wolves, living on opposite
ends of the Haskell Fancy Code Spectrum. Are you going to write “simple
Haskell”, using basic GHC 2010 tools and writing universal Haskell that every
introductory course offers, trying to keep the code as immediately
understandable and accessible? Or are you going to pile in all of the Haskell
type system and evaluation tricks you can find and turn on all the extensions,
and go full fancy?&lt;/p&gt;
&lt;p&gt;In my &lt;a href=&quot;https://blog.jle.im/entry/levels-of-type-safety-haskell-lists.html&quot;&gt;Seven
Levels of Type Safety&lt;/a&gt; post, I described different extremes of type safety
and fancy code. I talked about how writing effective code was finding the
correct compromise for the level of communication and safety you need.&lt;/p&gt;
&lt;p&gt;But this is not that kind of blog post. This is the kind of blog post where
we celebrate terrifying type-safety, facetious fanciness, and masochistic
meta-analysis. This series is about what happens when we dare to go full fancy.
Let’s write code that is so inscrutable, so painful and torturous to write, yet
so &lt;em&gt;undeniably useful&lt;/em&gt; that you can’t help but try to throw it into every
single thing you write and will feel a gnawing emptiness in your soul until you
do.&lt;/p&gt;
&lt;p&gt;As our example, let’s write a type-safe method to specify your program as a
series of states, with triggered transitions between them: a type-safe state
machine graph using a type-safe lambda calculus. We want to specify this in a
way that we can write once and then:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;be interpretable in a type-safe way within Haskell.&lt;/li&gt;
&lt;li&gt;be inspectable with visualizable control flow.&lt;/li&gt;
&lt;li&gt;be compilable to multiple actual back-ends, letting you run the same
function under multiple implementations.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This exact thing is something I’ve needed and used multiple times now in
projects. I want to specify one program graph within Haskell, but in a way that
can compile both in C and javascript while also being visualizable and
interactively explorable.&lt;/p&gt;
&lt;p&gt;Once you go down this road, everything you ever write will feel woefully
unsafe and limited. And everything you want to write will be hopelessly
inscrutable by normal humans and borderline unusable. But such is the curse we
all bear. Turn around now, you have been warned.&lt;/p&gt;
&lt;p&gt;This post will build up the embedded typed expression language. Part 2 will
use that expression language to define typed state machines with embedded
predicates and visualize them, and Part 3 will compile those machines to
different languages and verify they execute identically, with some live
demos.&lt;/p&gt;
&lt;p&gt;All of the code here is &lt;a href=&quot;https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/flake.nix&quot;&gt;available
online&lt;/a&gt;, and if you check out the repo and run &lt;code&gt;nix develop&lt;/code&gt; you
should be able to load it all in ghci:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode bash&quot;&gt;&lt;code class=&quot;sourceCode bash&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;$&lt;/span&gt; cd code-samples/typed-sm-lc&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;$&lt;/span&gt; nix develop&lt;/span&gt;
&lt;span id=&quot;cb1-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;$&lt;/span&gt; ghci&lt;/span&gt;
&lt;span id=&quot;cb1-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb1-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ex&quot;&gt;ghci&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; :load ExprStage1.hs&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;the-lambda-calculus&quot;&gt;The Lambda Calculus&lt;/h2&gt;
&lt;p&gt;Let’s derive a way to express an algorithm or expression in Haskell that can
be reified and analyzed within Haskell, and eventually be a form we can compile
to different backends, interpret in Haskell, or generate Graphviz visualizations
for.&lt;/p&gt;
&lt;h3 id=&quot;a-first-pass&quot;&gt;A First Pass&lt;/h3&gt;
&lt;p&gt;One basic thing we can do is start with:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage1.hs#L18-L34&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb2-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb2-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Map&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb2-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Map&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb2-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb2-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Show&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And you can write &lt;code&gt;(\x -&amp;gt; x * 3) 5&lt;/code&gt; as:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage1.hs#L36-L37&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;fifteen ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fifteen &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;))) &lt;span class=&quot;ot&quot;&gt;`EApply`&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The strings in &lt;code&gt;ELambda&lt;/code&gt; introduce variables, and
&lt;code&gt;EVar&lt;/code&gt; refers to the bound variable. As you can see, this is…pretty
untyped. We could easily write something that is meaningless:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage1.hs#L67-L69&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;badTypeExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;badTypeExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb4-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Of course, GHC can typecheck our code if we literally write
&lt;code&gt;\x -&amp;gt; x + 3&lt;/code&gt; and reject &lt;code&gt;1 &amp;amp;&amp;amp; 2&lt;/code&gt;. But we
aren’t trying to build opaque Haskell code here, we’re trying to represent our
expression as an ADT that we can analyze &lt;em&gt;within&lt;/em&gt; the language.&lt;/p&gt;
&lt;p&gt;If we want a record projection and one labeled choice with a case analysis
over it:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage1.hs#L39-L62&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;recordExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;recordExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        ( &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            M.fromList&lt;/span&gt;
&lt;span id=&quot;cb5-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;              [ (&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;)),&lt;/span&gt;
&lt;span id=&quot;cb5-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                (&lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;found&quot;&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb5-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;              ]&lt;/span&gt;
&lt;span id=&quot;cb5-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        )&lt;/span&gt;
&lt;span id=&quot;cb5-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;
&lt;span id=&quot;cb5-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb5-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;sumExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sumExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb5-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( M.fromList&lt;/span&gt;
&lt;span id=&quot;cb5-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        [ (&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt;, (&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))),&lt;/span&gt;
&lt;span id=&quot;cb5-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          (&lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt;, (&lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb5-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        ]&lt;/span&gt;
&lt;span id=&quot;cb5-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb5-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, for the entire point of &lt;code&gt;Expr&lt;/code&gt;, we can write a function to
pretty-print it, using the &lt;em&gt;&lt;a href=&quot;https://hackage.haskell.org/package/prettyprinter&quot;&gt;prettyprinter&lt;/a&gt;&lt;/em&gt;
library:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage1.hs#L253-L299&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppPrim ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb6-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppPrim &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty n&lt;/span&gt;
&lt;span id=&quot;cb6-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty (&lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; s)&lt;/span&gt;
&lt;span id=&quot;cb6-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppOp ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb6-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppOp &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&amp;lt;=&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&amp;amp;&amp;amp;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppExpr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb6-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppExpr paren &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; p &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ppPrim p&lt;/span&gt;
&lt;span id=&quot;cb6-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty v&lt;/span&gt;
&lt;span id=&quot;cb6-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; n body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; PP.pretty n &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;-&amp;gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; body&lt;/span&gt;
&lt;span id=&quot;cb6-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb6-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; o x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppOp o &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; y&lt;/span&gt;
&lt;span id=&quot;cb6-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    PP.encloseSep &lt;span class=&quot;st&quot;&gt;&quot;{ &quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; }&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      [PP.pretty k &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;=&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; v &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; (k, v) &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; M.toList xs]&lt;/span&gt;
&lt;span id=&quot;cb6-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; e k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; e &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;.&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; PP.pretty k&lt;/span&gt;
&lt;span id=&quot;cb6-32&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; tag x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-33&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; PP.pretty tag &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb6-34&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt; x hs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-35&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-36&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      PP.sep&lt;/span&gt;
&lt;span id=&quot;cb6-37&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        [ &lt;span class=&quot;st&quot;&gt;&quot;case&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;of&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-38&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        , PP.encloseSep &lt;span class=&quot;st&quot;&gt;&quot;{ &quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; }&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;; &quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-39&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            [ PP.pretty tag &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; PP.pretty n &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;-&amp;gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; body&lt;/span&gt;
&lt;span id=&quot;cb6-40&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; (tag, (n, body)) &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; M.toList hs&lt;/span&gt;
&lt;span id=&quot;cb6-41&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-41&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            ]&lt;/span&gt;
&lt;span id=&quot;cb6-42&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-42&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        ]&lt;/span&gt;
&lt;span id=&quot;cb6-43&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-43&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-44&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-44&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap&lt;/span&gt;
&lt;span id=&quot;cb6-45&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-45&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; paren &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; PP.parens&lt;/span&gt;
&lt;span id=&quot;cb6-46&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-46&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-47&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-47&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-48&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-48&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;prettyExpr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb6-49&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb6-49&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;prettyExpr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; prettyExpr fifteen&lt;/span&gt;
&lt;span id=&quot;cb7-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(\x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;) &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; prettyExpr badTypeExample&lt;/span&gt;
&lt;span id=&quot;cb7-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; prettyExpr recordExample&lt;/span&gt;
&lt;span id=&quot;cb7-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;{ label &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;found&quot;&lt;/span&gt;, value &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt; }&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt;value &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; prettyExpr sumExample&lt;/span&gt;
&lt;span id=&quot;cb7-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb7-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Found&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt; { &lt;span class=&quot;dt&quot;&gt;Found&lt;/span&gt; value &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; value &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;; &lt;span class=&quot;dt&quot;&gt;Missing&lt;/span&gt; message &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, we can write a quick typechecker for this using a greedy type-checking
algorithm (&lt;a href=&quot;https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage1.hs#L77-L89&quot;&gt;written
out here&lt;/a&gt;), which is a fun exercise, but it’s beyond the point of this post.
For our purposes, we’re going to write the in-Haskell evaluator, which is one
sure-fire evidential/constructive way to prove an expression was valid
after-the-fact.&lt;/p&gt;
&lt;p&gt;So…how can you “evaluate” this to 15, within Haskell? What would the type
even be? The best we can do at this point is make the entire thing monadic by
returning &lt;code&gt;Maybe&lt;/code&gt; or &lt;code&gt;Either&lt;/code&gt;, and split out the
expressions we write from the values we can actually evaluate to:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage1.hs#L228-L330&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVString&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb8-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVRecord&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Map&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb8-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVChoice&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eval ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Map&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eval env &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; p &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; evalPrim p&lt;/span&gt;
&lt;span id=&quot;cb8-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; M.lookup v env&lt;/span&gt;
&lt;span id=&quot;cb8-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; n body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; (\x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; eval (M.insert n x env) body))&lt;/span&gt;
&lt;span id=&quot;cb8-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; eval env f &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; f' &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; eval env x &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; f'&lt;/span&gt;
&lt;span id=&quot;cb8-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; o x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    u &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb8-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    v &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb8-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (u, v) &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; o &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb8-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb8-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb8-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; o &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb8-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-32&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVRecord&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;traverse&lt;/span&gt; (eval env) xs&lt;/span&gt;
&lt;span id=&quot;cb8-33&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; e k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-34&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVRecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env e&lt;/span&gt;
&lt;span id=&quot;cb8-35&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    M.lookup k xs&lt;/span&gt;
&lt;span id=&quot;cb8-36&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; tag x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVChoice&lt;/span&gt; tag &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb8-37&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt; x hs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-38&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVChoice&lt;/span&gt; tag payload &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb8-39&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (n, body) &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; M.lookup tag hs&lt;/span&gt;
&lt;span id=&quot;cb8-40&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb8-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    eval (M.insert n payload env) body&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This would properly evaluate:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; eval M.empty fifteen&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;15&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can also produce closures as values:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb10&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb10-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage1.hs#L64-L65&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;plusThree ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb10-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb10-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;plusThree &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb11&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb11-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; for_ (eval M.empty plusThree) \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;print&lt;/span&gt; (f (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb11-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;not a function&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb11-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This kind of works if you remember to thread everything through
&lt;code&gt;Maybe&lt;/code&gt; (or &lt;code&gt;Either&lt;/code&gt;) or what have you. But this is not
ideal. You should be able to know, at compile-time, that your &lt;code&gt;Expr&lt;/code&gt;
is valid. After all, you want to be able to create one “valid”
&lt;code&gt;Expr&lt;/code&gt;, and run it at every context. It’s useless to you if every
single time you used an &lt;code&gt;Expr&lt;/code&gt;, you had to manually handle the
&lt;code&gt;Nothing&lt;/code&gt; case. Your diagram generator, your Haskell runner, your
code generator, will always be in &lt;code&gt;Either&lt;/code&gt; even though you know your
&lt;code&gt;Expr&lt;/code&gt; is valid, via tests or something. We want GHC to reject badly
typed expressions, so we never need to unwrap or handle a &lt;code&gt;Nothing&lt;/code&gt;
or &lt;code&gt;Left&lt;/code&gt;!&lt;/p&gt;
&lt;p&gt;No, no, this is not okay and not acceptable. We should be able to verify in
the types if an &lt;code&gt;Expr&lt;/code&gt; is valid.&lt;/p&gt;
&lt;h2 id=&quot;type-indexed-expressions&quot;&gt;Type-Indexed Expressions&lt;/h2&gt;
&lt;h3 id=&quot;just-add-the-index&quot;&gt;Just Add the Index&lt;/h3&gt;
&lt;p&gt;The next step you’ll see in posts online is to add a phantom index type to
&lt;code&gt;Expr&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L19-L49&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; t&lt;/span&gt;
&lt;span id=&quot;cb12-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; t&lt;/span&gt;
&lt;span id=&quot;cb12-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb12-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; b&lt;/span&gt;
&lt;span id=&quot;cb12-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb12-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; a b c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; c&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(We’ll explain each part of this declaration eventually)&lt;/p&gt;
&lt;p&gt;A phantom type is a type parameter that doesn’t represent any actual
&lt;em&gt;value&lt;/em&gt; “contained” inside the data type, but just serves to “tag” or
distinguish values for the compiler to reject or unify things in useful
ways.&lt;/p&gt;
&lt;p&gt;This introduces several new language features, so bear with me as I break
them down.&lt;/p&gt;
&lt;p&gt;First, we use &lt;code&gt;-XTypeData&lt;/code&gt; to define a data kind: &lt;code&gt;Ty&lt;/code&gt;
is a kind with types &lt;code&gt;TInt :: Ty&lt;/code&gt;, &lt;code&gt;TBool :: Ty&lt;/code&gt;, etc. And
in &lt;code&gt;Expr t&lt;/code&gt;, we have an expression tagged with &lt;code&gt;t&lt;/code&gt;, which
describes the result type. (In fact, &lt;em&gt;all&lt;/em&gt; data types are automatically
promoted to the type level with &lt;code&gt;-XDataKinds&lt;/code&gt;. You might see the
&lt;code&gt;'Nothing&lt;/code&gt; quote prefix syntax in cases where it’s ambiguous if
you’re talking about the data constructor or the type constructor, like
&lt;code&gt;'[]&lt;/code&gt; and &lt;code&gt;'(,)&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;For example, because we have &lt;code&gt;EPrim :: Prim t -&amp;gt; Expr t&lt;/code&gt;, and
&lt;code&gt;PInt 3 :: Prim TInt&lt;/code&gt;, we have
&lt;code&gt;EPrim (PInt 3) :: Expr TInt&lt;/code&gt;: a primitive 3 is an expression
describing an integer.&lt;/p&gt;
&lt;p&gt;And because
&lt;code&gt;EOp :: Op a b c -&amp;gt; Expr a -&amp;gt; Expr b -&amp;gt; Expr c&lt;/code&gt;, and
&lt;code&gt;OLte :: Op TInt TInt TBool&lt;/code&gt;, we have&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So we can write an operation on two &lt;code&gt;Expr&lt;/code&gt;s that typecheck how
we’d expect:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb14&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb14-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb14-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;)) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb14-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb14-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We also have
&lt;code&gt;EOp OAnd :: Expr TBool -&amp;gt; Expr TBool -&amp;gt; Expr TBool&lt;/code&gt;, which
means the compiler will reject our previous &lt;code&gt;1 &amp;amp;&amp;amp; 2&lt;/code&gt;
example:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb15&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb15-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb15-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb15-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb15-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb15-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;Couldn't&lt;/span&gt; match &lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; ‘&lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;’ with ‘&lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;’&lt;/span&gt;
&lt;span id=&quot;cb15-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb15-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Expected&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb15-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Actual&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can write lambdas in this system too:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb16&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb16-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb16-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L51-L55&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb16-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb16-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;fifteen ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb16-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fifteen &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb16-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb16-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;))))&lt;/span&gt;
&lt;span id=&quot;cb16-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb16-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Because of &lt;code&gt;Ty&lt;/code&gt;, we can also make a new indexed data type with
phantoms of “fully resolved” values:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb17&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb17-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L115-L119&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVString&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb17-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; b)) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is what we want to eventually &lt;code&gt;eval&lt;/code&gt; into, as we can
guarantee ourselves to get a value of the correct type based on the
&lt;code&gt;Ty&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb18&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb18-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb18-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eValueToInt ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb18-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb18-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eValueToInt &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb18-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb18-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And GHC will verify this as a total pattern match because &lt;code&gt;EVInt&lt;/code&gt;
is the only possible way to create an &lt;code&gt;EValue TInt&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;singletons-and-existentials&quot;&gt;Singletons and Existentials&lt;/h3&gt;
&lt;p&gt;We’ll keep our bound variables stored as an ambient map of variable names to
their evaluated values for now. But, to do this, we need to turn the
heterogeneous &lt;code&gt;EValue t&lt;/code&gt; into the homogeneous
&lt;code&gt;Map String SomeValue&lt;/code&gt; by wrapping the type variable as an
existential type.&lt;/p&gt;
&lt;p&gt;You might notice we have a &lt;a href=&quot;https://blog.jle.im/entries/series/+introduction-to-singletons.html&quot;&gt;singleton&lt;/a&gt;
for our &lt;code&gt;Ty&lt;/code&gt; type, &lt;code&gt;STy&lt;/code&gt;, that pops up in multiple
situations.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb19&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb19-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L27-L31&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STBool&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb19-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STFun&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Firstly, it might help to recognize the general pattern where
&lt;code&gt;STy&lt;/code&gt; (the singleton) appears. It &lt;em&gt;usually&lt;/em&gt; pops up whenever
we have existentially scoped variables, like in
&lt;code&gt;data SomeValue = forall t. SomeValue (STy t) (EValue t)&lt;/code&gt;. In this
case, the &lt;code&gt;t&lt;/code&gt; is completely lost to the outside world, and
&lt;code&gt;STy t&lt;/code&gt; is used to allow us to recover a runtime witness to what
&lt;code&gt;t&lt;/code&gt; was, after pattern matching on &lt;code&gt;STy&lt;/code&gt;. This is the
&lt;em&gt;dependent sum&lt;/em&gt; pattern, and is similar to how &lt;code&gt;Typeable&lt;/code&gt; is
used in &lt;em&gt;Data.Dynamic&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In our case, because variables are still stored ambiently in the environment
and validated at runtime, we &lt;em&gt;do&lt;/em&gt; need singletons to implement
&lt;code&gt;eval&lt;/code&gt; . The type &lt;code&gt;Expr t&lt;/code&gt; only specifies the type of the
result, but the type information of the ambient variables is not available. So,
you can write &lt;code&gt;EVar STInt &quot;myVar&quot; :: Expr TInt&lt;/code&gt;, but:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;myVar&lt;/code&gt; might not be a variable in scope at all, so
&lt;code&gt;eval&lt;/code&gt; will fail at runtime&lt;/li&gt;
&lt;li&gt;&lt;code&gt;myVar&lt;/code&gt; might be in scope, but might be a &lt;code&gt;TString&lt;/code&gt;
and not a &lt;code&gt;TInt&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first case is easy enough to deal with (&lt;code&gt;M.lookup&lt;/code&gt; returns
&lt;code&gt;Nothing&lt;/code&gt;), but the second one is a little more subtle. Let’s say we
&lt;em&gt;do&lt;/em&gt; have a &lt;code&gt;SomeValue&lt;/code&gt; under our key &lt;code&gt;myVar&lt;/code&gt;…how
do we make sure it has the correct type?&lt;/p&gt;
&lt;h3 id=&quot;runtime-type-equality&quot;&gt;Runtime Type Equality&lt;/h3&gt;
&lt;p&gt;We can do ad-hoc pattern matching on &lt;code&gt;EValue&lt;/code&gt;, but that won’t get
us too far. Mostly because some of the &lt;code&gt;EValue&lt;/code&gt; constructors actually
don’t have enough information for us to validate their actual type (try it!
&lt;code&gt;EVFun&lt;/code&gt; will give you a lot of trouble). So, what we can do is write
a function that takes two &lt;code&gt;STy&lt;/code&gt; at runtime and &lt;em&gt;unifies&lt;/em&gt; them
conditionally if they are the same. We’ll write a function
&lt;code&gt;sameTy :: STy a -&amp;gt; STy b -&amp;gt; Maybe (a :~: b)&lt;/code&gt;, where&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb20&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb20-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb20-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; (:~:) ::&lt;/span&gt; k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb20-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; a &lt;span class=&quot;op&quot;&gt;:~:&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;pattern matching&lt;/em&gt; on a value of type &lt;code&gt;a :~: b&lt;/code&gt; will reveal
that &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are the same type variable, because the
only way to construct it is with &lt;code&gt;Refl :: a :~: a&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With that, we can write &lt;code&gt;sameTy&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb21&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb21-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L133-L143&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;sameTy ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:~:&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb21-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sameTy &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STBool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STBool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STFun&lt;/span&gt; a b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;STFun&lt;/span&gt; c d &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameTy a c&lt;/span&gt;
&lt;span id=&quot;cb21-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameTy b d&lt;/span&gt;
&lt;span id=&quot;cb21-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb21-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There’s a typeclass in &lt;em&gt;base&lt;/em&gt; (or rather, a “kindclass”),
&lt;code&gt;TestEquality&lt;/code&gt;, that encapsulates this pattern:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb22&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb22-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb22-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TestEquality&lt;/span&gt; f &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb22-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;    testEquality ::&lt;/span&gt; f a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; f b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:~:&lt;/span&gt; b)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In fact, we can write our &lt;code&gt;sameTy&lt;/code&gt; as an instance:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb23&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb23-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L130-L131&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TestEquality&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb23-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  testEquality &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sameTy&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We’re now at a higher fanciness level than before. But you might see the
problem here: &lt;code&gt;EVar STInt &quot;x&quot;&lt;/code&gt;. &lt;code&gt;x&lt;/code&gt; might not be defined,
and it also might not have the correct type. Soooo yes, we still have issues
here.&lt;/p&gt;
&lt;p&gt;But now at least we can write &lt;code&gt;eval&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb24&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb24-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L145-L176&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eval ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Map&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeValue&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; t)&lt;/span&gt;
&lt;span id=&quot;cb24-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eval env &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; n)&lt;/span&gt;
&lt;span id=&quot;cb24-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb24-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; s) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVString&lt;/span&gt; s)&lt;/span&gt;
&lt;span id=&quot;cb24-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; t v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;SomeValue&lt;/span&gt; t' v' &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; M.lookup v env&lt;/span&gt;
&lt;span id=&quot;cb24-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameTy t t'&lt;/span&gt;
&lt;span id=&quot;cb24-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; v'&lt;/span&gt;
&lt;span id=&quot;cb24-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; ta n body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; eval (M.insert n (&lt;span class=&quot;dt&quot;&gt;SomeValue&lt;/span&gt; ta x) env) body&lt;/span&gt;
&lt;span id=&quot;cb24-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; g &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env f&lt;/span&gt;
&lt;span id=&quot;cb24-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    x' &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb24-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    g x'&lt;/span&gt;
&lt;span id=&quot;cb24-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; o x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; o &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb24-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb24-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb24-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb24-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb24-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb24-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb24-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb24-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb24-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-32&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb24-33&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb24-34&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb24-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; b))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This does seem to work:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb25&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb25-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; for_ (eval M.empty fifteen) \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;print&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb25-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb25-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;15&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Our system also allows us to produce closures and functions as values:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb26&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb26-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb26-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L63-L64&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb26-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb26-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb26-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;plusThree ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb26-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb26-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;plusThree &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb27&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb27-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; for_ (eval M.empty plusThree) \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; for_ (f (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;)) \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;print&lt;/span&gt; x    &lt;span class=&quot;co&quot;&gt;-- compiler-verified to always be EVInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb27-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We have a type-safe &lt;code&gt;eval&lt;/code&gt; now that will create a value of the
type we want. But we still have the same errors when looking at variables:
variables can still not be defined, or be defined as the wrong type.&lt;/p&gt;
&lt;h3 id=&quot;pretty-printing&quot;&gt;Pretty-Printing&lt;/h3&gt;
&lt;p&gt;One nice consequence of this type-index method is that if you choose to
consume them into an untyped target, you can do it more or less in the same way
as the non-indexed untyped data.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb28&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb28-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L87-L113&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppPrim ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb28-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppPrim &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty n&lt;/span&gt;
&lt;span id=&quot;cb28-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; b &lt;span class=&quot;kw&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;true&quot;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty (&lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; s)&lt;/span&gt;
&lt;span id=&quot;cb28-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppOp ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; a b c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb28-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppOp &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&amp;lt;=&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&amp;amp;&amp;amp;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppExpr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb28-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppExpr paren &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; p &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ppPrim p&lt;/span&gt;
&lt;span id=&quot;cb28-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; _ v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty v&lt;/span&gt;
&lt;span id=&quot;cb28-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; _ n body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; PP.pretty n &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;-&amp;gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; body&lt;/span&gt;
&lt;span id=&quot;cb28-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb28-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; o x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppOp o &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; y&lt;/span&gt;
&lt;span id=&quot;cb28-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap&lt;/span&gt;
&lt;span id=&quot;cb28-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; paren &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; PP.parens&lt;/span&gt;
&lt;span id=&quot;cb28-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb28-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;prettyExpr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb28-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb28-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;prettyExpr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And they render the same way:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb29&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb29-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; prettyExpr fifteen&lt;/span&gt;
&lt;span id=&quot;cb29-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(\x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;) &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; prettyExpr plusThree&lt;/span&gt;
&lt;span id=&quot;cb29-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;\x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; prettyExpr badVariable&lt;/span&gt;
&lt;span id=&quot;cb29-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb29-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(\x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;) true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;still-not-fully-verified&quot;&gt;Still Not Fully Verified&lt;/h3&gt;
&lt;p&gt;Implicit in the previous section was the admission of failure: this system
lets us use indexed types to help propagate unification (the result types of
&lt;code&gt;OLte&lt;/code&gt;, &lt;code&gt;OAnd&lt;/code&gt;, &lt;code&gt;OPlus&lt;/code&gt;, etc.), but it can’t
prevent all ill-defined programs from compiling.&lt;/p&gt;
&lt;p&gt;The issue is &lt;code&gt;EVar&lt;/code&gt;: its type
&lt;code&gt;EVar :: STy t -&amp;gt; String -&amp;gt; Expr t&lt;/code&gt; lets us bind &lt;em&gt;any&lt;/em&gt;
variable name as &lt;em&gt;any&lt;/em&gt; type, and it’ll still typecheck. Even with the
help of everything we have, we can just straight-up declare a reference to an
unbound variable&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb30&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb30-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb30-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L71-L72&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb30-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb30-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;unboundVariable ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb30-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;unboundVariable &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;missing&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This typechecks in GHC even though it’s meaningless in the domain. We can
also reference the variable under any &lt;em&gt;type&lt;/em&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb31&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb31-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage2.hs#L57-L61&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;badVariable ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;badVariable &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STBool&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;))))&lt;/span&gt;
&lt;span id=&quot;cb31-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb31-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s because &lt;code&gt;EVar&lt;/code&gt; can freely take any &lt;code&gt;STy&lt;/code&gt; without
any restriction, and no association with the binder name, so there’s no way for
GHC to stop us.&lt;/p&gt;
&lt;p&gt;So, again, we cannot create a &lt;em&gt;fully&lt;/em&gt; type-checked &lt;code&gt;Expr&lt;/code&gt;.
We still have to deal with &lt;em&gt;most&lt;/em&gt; of the same errors. This is noble, but
clearly not good enough. We have to go deeper.&lt;/p&gt;
&lt;h2 id=&quot;typed-records-and-sums&quot;&gt;Typed Records and Sums&lt;/h2&gt;
&lt;p&gt;A quick detour: you might have noticed that this past implementation dropped
records and sums. Before we move on, let’s go ahead and add those. Introducing
records and sums at the same time as type-indexed &lt;code&gt;Expr&lt;/code&gt; is a bit
&lt;em&gt;too&lt;/em&gt; much of a jump to fit into a single section.&lt;/p&gt;
&lt;p&gt;Let’s add sums and records, which can use pretty similar mechanisms (via
duality) for implementation.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb32&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb32-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L31-L79&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;)]&lt;/span&gt;
&lt;span id=&quot;cb32-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;)]&lt;/span&gt;
&lt;span id=&quot;cb32-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STBool&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STRecord&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; as)&lt;/span&gt;
&lt;span id=&quot;cb32-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STSum&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; as)&lt;/span&gt;
&lt;span id=&quot;cb32-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb32-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STFun&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;Ty&lt;/code&gt; now includes &lt;code&gt;TRecord [(Symbol, Ty)]&lt;/code&gt; and
&lt;code&gt;TSum [(Symbol, Ty)]&lt;/code&gt;, which represent the field names and
constructor payloads (&lt;code&gt;Symbol&lt;/code&gt; being a type-level string). So, for
example, &lt;code&gt;TRecord [&quot;value&quot; ::: TInt, &quot;label&quot; ::: TString]&lt;/code&gt; would be
the type of a record with ordered fields &lt;code&gt;value&lt;/code&gt; and
&lt;code&gt;label&lt;/code&gt; of integers and strings, respectively.
&lt;code&gt;TSum [&quot;Found&quot; ::: TInt, &quot;Missing&quot; ::: TString]&lt;/code&gt; would be the type of
a sum between &lt;code&gt;Found&lt;/code&gt; containing an integer and &lt;code&gt;Missing&lt;/code&gt;
containing a string. Note we take a page out of &lt;a href=&quot;https://hackage.haskell.org/package/vinyl&quot;&gt;vinyl&lt;/a&gt; by defining the type
alias &lt;code&gt;(:::) = '(,)&lt;/code&gt; to make things syntactically nicer.&lt;/p&gt;
&lt;h3 id=&quot;record-access&quot;&gt;Record Access&lt;/h3&gt;
&lt;p&gt;We need the fields and types at the type level because we have to answer what
the &lt;code&gt;Expr&lt;/code&gt; phantom type of field access is. If we had an
&lt;code&gt;x :: Expr (TRecord [&quot;value&quot; ::: TInt, &quot;label&quot; ::: TString])&lt;/code&gt;, we
want the type of &lt;code&gt;x.value&lt;/code&gt; to be &lt;code&gt;Expr TInt&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To do this, we need to have a &lt;em&gt;value&lt;/em&gt; in our &lt;code&gt;Expr&lt;/code&gt; for
field access that can “point” at a specific field in the type. One way to do
that is to take a field of type &lt;code&gt;Index&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb33&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb33-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L47-L49&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; [k] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; (x &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs) x&lt;/span&gt;
&lt;span id=&quot;cb33-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb33-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; xs x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; (y &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs) x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can read this as: “&lt;code&gt;IZ&lt;/code&gt; is an index to the head of the
type-level list, and &lt;code&gt;IS n&lt;/code&gt; is an index to the n-th item of the
tail”. So, &lt;code&gt;IS IZ&lt;/code&gt; is an index into the second element,
&lt;code&gt;IS (IS IZ)&lt;/code&gt; is an index into the third, etc.&lt;/p&gt;
&lt;p&gt;If we have &lt;code&gt;[&quot;value&quot; ::: TInt, &quot;label&quot; ::: TString]&lt;/code&gt;, then we have
values:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb34&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb34-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;    ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;] (&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb34-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb34-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;] (&lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that the way this is constructed, it’s impossible for
&lt;code&gt;IS (IS IZ) :: Index [&quot;value&quot; ::: TInt, &quot;label&quot; ::: TString] _&lt;/code&gt; to
typecheck as anything!&lt;/p&gt;
&lt;p&gt;In this way, we have a well-typed field accessor syntax, which takes an
&lt;code&gt;Expr&lt;/code&gt; of a record of fields and indexes it to get an
&lt;code&gt;Expr&lt;/code&gt; of the type at that index:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb35&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb35-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb35-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L105-L105&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb35-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb35-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb35-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb35-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; as) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; as (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Which is typed as:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb36&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb36-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb36-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(&lt;span class=&quot;ot&quot;&gt;`EAccess`&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;)&lt;span class=&quot;ot&quot;&gt;    ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;]) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb36-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb36-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(&lt;span class=&quot;ot&quot;&gt;`EAccess`&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;)&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;]) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To &lt;em&gt;create&lt;/em&gt; an &lt;code&gt;Expr&lt;/code&gt; of a record, we can use
&lt;code&gt;Rec&lt;/code&gt; from &lt;em&gt;&lt;a href=&quot;https://hackage.haskell.org/package/vinyl&quot;&gt;vinyl&lt;/a&gt;&lt;/em&gt; or
&lt;code&gt;NP&lt;/code&gt; from &lt;em&gt;&lt;a href=&quot;https://hackage.haskell.org/package/sop-core&quot;&gt;sop-core&lt;/a&gt;&lt;/em&gt;: a
heterogeneous list indexed by a type-level list.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb37&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb37-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L43-L45&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb37-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb37-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; (k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [k] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb37-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; f '[]&lt;/span&gt;
&lt;span id=&quot;cb37-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb37-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  (:&amp;amp;) ::&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; f xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; f (x &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you haven’t seen &lt;code&gt;Rec&lt;/code&gt; before, basically
&lt;code&gt;Rec f [a,b,c]&lt;/code&gt; is a tuple of &lt;code&gt;f a&lt;/code&gt;, &lt;code&gt;f b&lt;/code&gt;, and
&lt;code&gt;f c&lt;/code&gt;. For example:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb38&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb38-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t &lt;span class=&quot;dt&quot;&gt;Identity&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Identity&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Identity&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb38-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Identity&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;]&lt;/span&gt;
&lt;span id=&quot;cb38-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t &lt;span class=&quot;dt&quot;&gt;Const&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Const&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;y&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb38-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb38-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Const&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt;) [x1, x2]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Keeping &lt;code&gt;Rec f as&lt;/code&gt; instead of a direct heterogeneous list of
&lt;code&gt;a&lt;/code&gt;s lets us store more interesting things than just
&lt;code&gt;Type&lt;/code&gt;-kinded things. For example, since our lists here are lists of
&lt;code&gt;(Symbol, Ty)&lt;/code&gt;, we can create a container to hold fields:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb39&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb39-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L109-L110&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb39-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb39-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EField&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt; (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The field constructor keeps a &lt;code&gt;KnownSymbol l&lt;/code&gt; constraint, so the
type-level label is still available later when we need to render it:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb40&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb40-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb40-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t &lt;span class=&quot;dt&quot;&gt;EField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb40-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb40-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;found&quot;&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb40-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb40-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb40-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb40-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So, we can create
&lt;code&gt;Expr (TRecord [&quot;value&quot; ::: TInt, &quot;label&quot; ::: TString])&lt;/code&gt; by taking a
&lt;code&gt;Rec&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb41&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb41-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb41-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L104-L104&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb41-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb41-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb41-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb41-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt; as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; as)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can make this a little more ergonomic by using
&lt;code&gt;-XRequiredTypeArguments&lt;/code&gt; (as of GHC 9.10) to get rid of the
&lt;code&gt;-XTypeApplication&lt;/code&gt; ugliness:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb42&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb42-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L115-L150&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eField ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt; (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb42-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eField l &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l&lt;/span&gt;
&lt;span id=&quot;cb42-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;makeRecordExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;])&lt;/span&gt;
&lt;span id=&quot;cb42-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;makeRecordExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( eField &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb42-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; eField &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;found&quot;&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb42-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;
&lt;span id=&quot;cb42-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;recordExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb42-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;recordExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; makeRecordExample &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;sum-injection-and-case-analysis&quot;&gt;Sum Injection and Case Analysis&lt;/h3&gt;
&lt;p&gt;We also need a type-level list witness for &lt;em&gt;sum&lt;/em&gt; types, because we
need to be able to implement the correct continuations for pattern matches: How
do we know &lt;em&gt;what&lt;/em&gt; thing to handle in each pattern match, unless the sum
type has that information in its type?&lt;/p&gt;
&lt;p&gt;Luckily due to the magic of duality, we can use the same tools, for the most
part! We can inject into a sum with an &lt;code&gt;Index&lt;/code&gt;, let’s say for a
&lt;code&gt;Expr (TSum [&quot;Found&quot; ::: TInt, &quot;Missing&quot; ::: TString])&lt;/code&gt;: sum type
with &lt;code&gt;Found&lt;/code&gt; containing an integer and &lt;code&gt;Missing&lt;/code&gt;
containing a string:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb43&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb43-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb43-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L106-L106&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb43-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb43-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb43-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb43-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; as (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; as)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb44&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb44-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb44-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;        ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;])&lt;/span&gt;
&lt;span id=&quot;cb44-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb44-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;)&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;])&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb45&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb45-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L181-L182&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb45-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb45-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;makeSumExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;])&lt;/span&gt;
&lt;span id=&quot;cb45-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb45-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;makeSumExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And we can re-use &lt;code&gt;Rec&lt;/code&gt; to define a type that can &lt;em&gt;handle&lt;/em&gt;
a &lt;code&gt;[&quot;Found&quot; ::: TInt, &quot;Missing&quot; ::: TString]&lt;/code&gt; sum:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb46&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb46-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L112-L133&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; b (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb46-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eHandler ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; b (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb46-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb46-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eHandler l &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb47&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb47-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t eHandler &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb47-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; (&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb47-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb47-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;         &lt;span class=&quot;co&quot;&gt;-- ^ result           ^ payload&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And we can put these into a &lt;code&gt;Rec&lt;/code&gt; to handle each option in the
list:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb48&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb48-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb48-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t eHandler &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb48-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb48-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;           &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; eHandler &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb48-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb48-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;           &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb48-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb48-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;) [&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And that’s exactly what a case statement is:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb49&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb49-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L107-L107&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb49-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb49-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb49-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; as) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; b) as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb50&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb50-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L184-L191&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb50-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb50-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;sumExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb50-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sumExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb50-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb50-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    makeSumExample&lt;/span&gt;
&lt;span id=&quot;cb50-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( eHandler &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb50-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; eHandler &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb50-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb50-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb50-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that we’re still using string binders, so there’s still an element of
unsafety here… we say that the variable name is &lt;code&gt;&quot;value&quot;&lt;/code&gt; and that it
is a &lt;code&gt;TInt&lt;/code&gt;, but when we later refer to the variable with
&lt;code&gt;EVar STInt &quot;value&quot;&lt;/code&gt;, it isn’t type-checked that later references use
the same type. The compiler would be just as happy with
&lt;code&gt;EVar STString &quot;value&quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here’s an example demonstrating both failure modes: the first handler
references a variable that doesn’t exist, and the second handler references a
variable that does exist as an incorrect type! How unfortunate.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb51&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb51-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L202-L209&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;badCaseBranchExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;badCaseBranchExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb51-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( eHandler &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;missing&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb51-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; eHandler &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb51-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb51-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;runtime-equality-for-records-and-sums&quot;&gt;Runtime Equality for Records and
Sums&lt;/h3&gt;
&lt;p&gt;One complication is that we need to update the &lt;code&gt;TestEquality&lt;/code&gt;
instance for &lt;code&gt;STy&lt;/code&gt;. The record and sum labels are type-level
&lt;code&gt;Symbol&lt;/code&gt;s, so we compare those with the &lt;code&gt;sameSymbol&lt;/code&gt; (kind
of like &lt;code&gt;testEquality&lt;/code&gt; for any &lt;code&gt;KnownSymbol&lt;/code&gt; instance) and
then compare the payload types recursively.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb52&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb52-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L284-L324&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TestEquality&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    testEquality &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sameTy&lt;/span&gt;
&lt;span id=&quot;cb52-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TestEquality&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    testEquality &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sameField&lt;/span&gt;
&lt;span id=&quot;cb52-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;sameTy ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STy&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:~:&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb52-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sameTy &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STInt&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STBool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STBool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STString&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;; _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STRecord&lt;/span&gt; as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;STRecord&lt;/span&gt; bs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameFields as bs&lt;/span&gt;
&lt;span id=&quot;cb52-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STSum&lt;/span&gt; as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;STSum&lt;/span&gt; bs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameFields as bs&lt;/span&gt;
&lt;span id=&quot;cb52-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;STFun&lt;/span&gt; a b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;STFun&lt;/span&gt; c d &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameTy a c&lt;/span&gt;
&lt;span id=&quot;cb52-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameTy b d&lt;/span&gt;
&lt;span id=&quot;cb52-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;sameFields ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; ys &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (xs &lt;span class=&quot;op&quot;&gt;:~:&lt;/span&gt; ys)&lt;/span&gt;
&lt;span id=&quot;cb52-32&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sameFields &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-33&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sameFields (x &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; xs) (y &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; ys) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-34&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameField x y&lt;/span&gt;
&lt;span id=&quot;cb52-35&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameFields xs ys&lt;/span&gt;
&lt;span id=&quot;cb52-36&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-37&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sameFields _ _ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-38&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-39&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;sameField ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (x &lt;span class=&quot;op&quot;&gt;:~:&lt;/span&gt; y)&lt;/span&gt;
&lt;span id=&quot;cb52-40&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sameField (&lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l tx) (&lt;span class=&quot;dt&quot;&gt;STyField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;m ty) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb52-41&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-41&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameSymbol (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l) (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;m)&lt;/span&gt;
&lt;span id=&quot;cb52-42&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-42&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameTy tx ty&lt;/span&gt;
&lt;span id=&quot;cb52-43&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb52-43&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;the-full-eval&quot;&gt;The Full Eval&lt;/h3&gt;
&lt;p&gt;Before we write the final &lt;code&gt;eval&lt;/code&gt;, let’s practice using
&lt;code&gt;Index&lt;/code&gt; and &lt;code&gt;Rec&lt;/code&gt; together. If we have an index
&lt;code&gt;Index as a&lt;/code&gt; that picks out a value &lt;code&gt;a&lt;/code&gt; in
&lt;code&gt;as&lt;/code&gt;, then we can pick out the &lt;code&gt;f a&lt;/code&gt; from a
&lt;code&gt;Rec f as&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb53&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb53-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L60-L63&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;indexRec ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; xs x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; f xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; f x&lt;/span&gt;
&lt;span id=&quot;cb53-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;indexRec &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \(x &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; _) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb53-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb53-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;IS&lt;/span&gt; i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; \(_ &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; xs) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; indexRec i xs&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We also can recursively iterate a function over each item, assuming the
function &lt;code&gt;forall x. f x -&amp;gt; g x&lt;/code&gt;: that is, we can turn a
&lt;code&gt;Rec f as&lt;/code&gt; into a &lt;code&gt;Rec g as&lt;/code&gt; assuming our function is
polymorphic over each &lt;code&gt;x&lt;/code&gt;, and only depends on the shape of
&lt;code&gt;f&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb54&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb54-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L65-L67&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;traverseRec ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt; m &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; (&lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; x&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; m (g x)) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; f xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; m (&lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; g xs)&lt;/span&gt;
&lt;span id=&quot;cb54-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;traverseRec _ &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb54-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;traverseRec f (x &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; xs) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; f x &lt;span class=&quot;op&quot;&gt;&amp;lt;*&amp;gt;&lt;/span&gt; traverseRec f xs&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With that, we can write our full &lt;code&gt;eval&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb55&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb55-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L326-L369&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eval ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Map&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SomeValue&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; t)&lt;/span&gt;
&lt;span id=&quot;cb55-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eval env &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; n)&lt;/span&gt;
&lt;span id=&quot;cb55-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb55-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; s) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVString&lt;/span&gt; s)&lt;/span&gt;
&lt;span id=&quot;cb55-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; t v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;SomeValue&lt;/span&gt; t' v' &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; M.lookup v env&lt;/span&gt;
&lt;span id=&quot;cb55-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;Refl&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; sameTy t t'&lt;/span&gt;
&lt;span id=&quot;cb55-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; v'&lt;/span&gt;
&lt;span id=&quot;cb55-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; ta n body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; eval (M.insert n (&lt;span class=&quot;dt&quot;&gt;SomeValue&lt;/span&gt; ta x) env) body&lt;/span&gt;
&lt;span id=&quot;cb55-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; g &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env f&lt;/span&gt;
&lt;span id=&quot;cb55-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    x' &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb55-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    g x'&lt;/span&gt;
&lt;span id=&quot;cb55-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; o x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; o &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb55-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb55-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb55-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb55-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb55-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb55-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb55-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb55-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb55-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-32&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb55-33&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env y&lt;/span&gt;
&lt;span id=&quot;cb55-34&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; b))&lt;/span&gt;
&lt;span id=&quot;cb55-35&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-36&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVRecord&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; traverseRec (evalField env) xs&lt;/span&gt;
&lt;span id=&quot;cb55-37&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; e i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-38&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVRecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env e&lt;/span&gt;
&lt;span id=&quot;cb55-39&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; indexRec i xs &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-40&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; v&lt;/span&gt;
&lt;span id=&quot;cb55-41&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-41&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; i x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-42&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-42&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVSum&lt;/span&gt; i &lt;span class=&quot;op&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb55-43&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-43&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt; x hs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;do&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-44&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-44&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVSum&lt;/span&gt; i v &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; eval env x&lt;/span&gt;
&lt;span id=&quot;cb55-45&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-45&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; indexRec i hs &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb55-46&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb55-46&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt; t n body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; eval (M.insert n (&lt;span class=&quot;dt&quot;&gt;SomeValue&lt;/span&gt; t v) env) body&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;ergonomics-of-records-and-sums&quot;&gt;Ergonomics of Records and Sums&lt;/h3&gt;
&lt;p&gt;Note that we could also choose to implement records and sums using row types
indexed by the name of the field itself, instead of an ordered list of tuples.
This would have the advantage of making, for example,
&lt;code&gt;{ value :: Int, label :: String }&lt;/code&gt; the same type as
&lt;code&gt;{ label :: String, value :: Int }&lt;/code&gt;. However, I personally prefer the
style of building things inductively (&lt;code&gt;:&amp;amp;&lt;/code&gt;/&lt;code&gt;RNil&lt;/code&gt; and
&lt;code&gt;IS&lt;/code&gt;/&lt;code&gt;IZ&lt;/code&gt;), it makes the type errors and constructions a
lot easier to work with and reason with.&lt;/p&gt;
&lt;p&gt;However, we can get a little bit of the best of both worlds by using
typeclasses to auto-insert the &lt;code&gt;Index&lt;/code&gt; witnesses into a list.&lt;/p&gt;
&lt;p&gt;First, we can write a typeclass that searches a type-level list of fields and
produces the right &lt;code&gt;Index&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb56&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb56-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L51-L58&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;l ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;) (&lt;span class=&quot;ot&quot;&gt;xs ::&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;)]) (&lt;span class=&quot;ot&quot;&gt;a ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; l xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  listIx ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; xs (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb56-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs) a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  listIx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{-# OVERLAPPABLE #-}&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l xs a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l (m &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; b &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs) a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb56-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  listIx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IS&lt;/span&gt; (listIx &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The FunDep &lt;code&gt;l xs -&amp;gt; a&lt;/code&gt; lets us use this like a function: for a
label &lt;code&gt;l&lt;/code&gt; and a list &lt;code&gt;xs&lt;/code&gt;, we should be able to uniquely
determine the &lt;code&gt;a&lt;/code&gt; type it singles out, if it exists. So we have an
instance of
&lt;code&gt;ListIx &quot;value&quot; [&quot;value&quot; ::: TInt, &quot;label&quot; ::: TString] TInt&lt;/code&gt;, where
the label &lt;code&gt;&quot;value&quot;&lt;/code&gt; and the list uniquely determines the result type
&lt;code&gt;TInt&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We can now have a helper function that we can call like
&lt;code&gt;eAccess &quot;value&quot;&lt;/code&gt; using GHC 9.10’s
&lt;code&gt;RequiredTypeArguments&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb57&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb57-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb57-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L118-L123&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb57-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb57-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb57-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb57-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eAccess ::&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb57-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb57-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;l ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb57-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb57-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (&lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l, &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l as a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb57-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb57-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; as) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb57-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb57-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb57-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb57-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eAccess l e &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l e (listIx &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That lets us write the field name directly, and the compiler will generate
the &lt;code&gt;Index&lt;/code&gt; automatically for us:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb58&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb58-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L167-L176&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb58-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb58-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;namedAccessExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb58-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;namedAccessExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb58-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  eAccess&lt;/span&gt;
&lt;span id=&quot;cb58-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb58-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb58-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        ( eField &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb58-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; eField &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;found&quot;&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb58-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb58-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        )&lt;/span&gt;
&lt;span id=&quot;cb58-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb58-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here,
&lt;code&gt;eAccess &quot;value&quot; :: Expr (TRecord [&quot;value&quot; ::: TInt, &quot;label&quot; ::: TString]) -&amp;gt; Expr TInt&lt;/code&gt;,
its result type uniquely determined by the types in the record fields.&lt;/p&gt;
&lt;p&gt;The same trick works for sum injections, so we can write the constructor name
directly:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb59&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb59-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L125-L179&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb59-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb59-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eChoice ::&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb59-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;l ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb59-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (&lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l, &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l as a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb59-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb59-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; as)&lt;/span&gt;
&lt;span id=&quot;cb59-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eChoice l e &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l (listIx &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l) e&lt;/span&gt;
&lt;span id=&quot;cb59-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb59-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;namedChoiceExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;])&lt;/span&gt;
&lt;span id=&quot;cb59-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb59-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;namedChoiceExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; eChoice &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;not here&quot;&lt;/span&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here,
&lt;code&gt;eChoice &quot;Missing&quot; :: Expr TString -&amp;gt; Expr (TSum [&quot;Found&quot; ::: TInt, &quot;Missing&quot; ::: TString])&lt;/code&gt;,
because the constructor name &lt;code&gt;&quot;Missing&quot;&lt;/code&gt; uniquely determines the
payload type inside that sum.&lt;/p&gt;
&lt;h3 id=&quot;pretty-printing-records-and-sums&quot;&gt;Pretty-Printing Records and Sums&lt;/h3&gt;
&lt;p&gt;To pretty-print, we finally use that &lt;code&gt;KnownSymbol&lt;/code&gt; constraint
we’ve been tracking this entire time. We can use
&lt;code&gt;symbolVal :: KnownSymbol s =&amp;gt; p s -&amp;gt; String&lt;/code&gt; to get the string
&lt;em&gt;value&lt;/em&gt; from the type-level string.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb60&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb60-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage3.hs#L237-L282&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppPrim ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb60-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppPrim &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty n&lt;/span&gt;
&lt;span id=&quot;cb60-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; b &lt;span class=&quot;kw&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;true&quot;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty (&lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; s)&lt;/span&gt;
&lt;span id=&quot;cb60-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppOp ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; a b c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb60-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppOp &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&amp;lt;=&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&amp;amp;&amp;amp;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppFields ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann]&lt;/span&gt;
&lt;span id=&quot;cb60-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppFields &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; []&lt;/span&gt;
&lt;span id=&quot;cb60-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppFields (&lt;span class=&quot;dt&quot;&gt;EField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l x &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; xs) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)) &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;=&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; x) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; ppFields xs&lt;/span&gt;
&lt;span id=&quot;cb60-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppHandlers ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; b) xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann]&lt;/span&gt;
&lt;span id=&quot;cb60-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppHandlers &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; []&lt;/span&gt;
&lt;span id=&quot;cb60-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppHandlers (&lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l _ n body &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; xs) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)) &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; PP.pretty n &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;-&amp;gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; body) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; ppHandlers xs&lt;/span&gt;
&lt;span id=&quot;cb60-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppExpr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb60-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppExpr paren &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; p &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ppPrim p&lt;/span&gt;
&lt;span id=&quot;cb60-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; _ v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty v&lt;/span&gt;
&lt;span id=&quot;cb60-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; _ n body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;\\&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; PP.pretty n &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;-&amp;gt;&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; body&lt;/span&gt;
&lt;span id=&quot;cb60-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb60-32&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; o x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppOp o &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; y&lt;/span&gt;
&lt;span id=&quot;cb60-33&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.encloseSep &lt;span class=&quot;st&quot;&gt;&quot;{ &quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; }&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; (ppFields xs)&lt;/span&gt;
&lt;span id=&quot;cb60-34&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l e _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; e &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;.&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l))&lt;/span&gt;
&lt;span id=&quot;cb60-35&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l _ x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)) &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb60-36&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt; x hs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-37&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-38&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      PP.sep&lt;/span&gt;
&lt;span id=&quot;cb60-39&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        [ &lt;span class=&quot;st&quot;&gt;&quot;case&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;of&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-40&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        , PP.encloseSep &lt;span class=&quot;st&quot;&gt;&quot;{ &quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; }&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;; &quot;&lt;/span&gt; (ppHandlers hs)&lt;/span&gt;
&lt;span id=&quot;cb60-41&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-41&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        ]&lt;/span&gt;
&lt;span id=&quot;cb60-42&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-42&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-43&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-43&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap&lt;/span&gt;
&lt;span id=&quot;cb60-44&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-44&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; paren &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; PP.parens&lt;/span&gt;
&lt;span id=&quot;cb60-45&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-45&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-46&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-46&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-47&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-47&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;prettyExpr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb60-48&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb60-48&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;prettyExpr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;capturing-variables&quot;&gt;Capturing Variables&lt;/h2&gt;
&lt;p&gt;In order to have &lt;code&gt;EVar&lt;/code&gt; be type-safe, the environment itself needs
to be a part of the &lt;code&gt;Expr&lt;/code&gt; type, and you should only be able to use
&lt;code&gt;EVar&lt;/code&gt; if the &lt;code&gt;Expr&lt;/code&gt; enforces it. &lt;code&gt;ELambda&lt;/code&gt;
would, therefore, introduce the new variable to the environment.&lt;/p&gt;
&lt;p&gt;We’ll have:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb61&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb61-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb61-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L80-L80&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb61-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb61-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb61-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb61-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So a value of type &lt;code&gt;Expr [&quot;x&quot; ::: TInt, &quot;y&quot; ::: TBool] t&lt;/code&gt; is an
expression with free variables &lt;code&gt;x&lt;/code&gt; of type &lt;code&gt;Int&lt;/code&gt; and
&lt;code&gt;y&lt;/code&gt; of type &lt;code&gt;Bool&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Surprise! That small detour to add records and sums to our language actually
ended up being a smooth precursor to all of the techniques we will be using to
solve for variable binders.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ELambda&lt;/code&gt; would therefore take an &lt;code&gt;Expr&lt;/code&gt; with a free
variable and turn it into an &lt;code&gt;Expr&lt;/code&gt; of a function type. We also keep
a &lt;code&gt;KnownSymbol&lt;/code&gt; constraint for the name so that we can recover the
name when we render the expression.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb62&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb62-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb62-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L83-L83&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb62-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb62-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb62-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb62-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (n &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a '&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; vs) b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;EVar&lt;/code&gt; then becomes exactly like &lt;code&gt;EAccess&lt;/code&gt;! We “index”
into the environment of the &lt;code&gt;Expr vs a&lt;/code&gt; using &lt;code&gt;Index&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb63&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb63-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb63-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L82-L82&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb63-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb63-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb63-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb63-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; vs (n &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; t) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs t&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So it is legal to have
&lt;code&gt;EVar IZ :: Expr [&quot;x&quot; ::: TInt, &quot;y&quot; ::: TBool] TInt&lt;/code&gt;, and also it is
automatically inferred to be a &lt;code&gt;TInt&lt;/code&gt;. But we could &lt;em&gt;not&lt;/em&gt;
write &lt;code&gt;EVar IZ :: Expr [] TInt&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;And finally, our whole &lt;code&gt;Expr&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb64&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb64-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L80-L89&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb64-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb64-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb64-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs t&lt;/span&gt;
&lt;span id=&quot;cb64-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; vs (n &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; t) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs t&lt;/span&gt;
&lt;span id=&quot;cb64-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (n &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a '&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; vs) b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb64-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs b&lt;/span&gt;
&lt;span id=&quot;cb64-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; a b c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs c&lt;/span&gt;
&lt;span id=&quot;cb64-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt; vs) as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; as)&lt;/span&gt;
&lt;span id=&quot;cb64-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; as) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; as (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs a&lt;/span&gt;
&lt;span id=&quot;cb64-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; as (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; as)&lt;/span&gt;
&lt;span id=&quot;cb64-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb64-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; as) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; vs b) as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Just like with record access, we can use the &lt;code&gt;ListIx&lt;/code&gt; class to
write a named helper:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb65&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb65-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L47-L120&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; (&lt;span class=&quot;ot&quot;&gt;l ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;) (&lt;span class=&quot;ot&quot;&gt;xs ::&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;)]) (&lt;span class=&quot;ot&quot;&gt;a ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; l xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  listIx ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; xs (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb65-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a '&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs) a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  listIx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{-# OVERLAPPABLE #-}&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l xs a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l (m &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; b '&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs) a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  listIx &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IS&lt;/span&gt; (listIx &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)&lt;/span&gt;
&lt;span id=&quot;cb65-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb65-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eVar ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; n vs a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs a&lt;/span&gt;
&lt;span id=&quot;cb65-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb65-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eVar n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; (listIx &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Adding in our other &lt;code&gt;-XRequiredTypeArguments&lt;/code&gt; helpers:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb66&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb66-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L91-L117&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eLambda ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (n &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a '&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; vs) b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb66-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eLambda n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n&lt;/span&gt;
&lt;span id=&quot;cb66-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eField ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt; vs (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb66-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eField l &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l&lt;/span&gt;
&lt;span id=&quot;cb66-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eAccess ::&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (&lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l, &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l as a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; as) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs a&lt;/span&gt;
&lt;span id=&quot;cb66-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eAccess l e &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l e (listIx &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)&lt;/span&gt;
&lt;span id=&quot;cb66-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eChoice ::&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (&lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l, &lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; l as a) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; as)&lt;/span&gt;
&lt;span id=&quot;cb66-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eChoice l e &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l (listIx &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l) e&lt;/span&gt;
&lt;span id=&quot;cb66-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eHandler ::&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (&lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; n, &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l) &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; (n &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a '&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; vs) b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; vs b (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;
&lt;span id=&quot;cb66-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb66-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eHandler n l &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And we get something that is &lt;em&gt;truly&lt;/em&gt; type-safe: all expressions are
well-typed, and all variables are ensured to be bound!&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb67&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb67-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L139-L171&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;fifteen ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fifteen &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (eLambda &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; (eVar &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;))))&lt;/span&gt;
&lt;span id=&quot;cb67-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb67-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;recordExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;recordExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( eAccess&lt;/span&gt;
&lt;span id=&quot;cb67-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        ( &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            ( eField &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb67-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; eField &lt;span class=&quot;st&quot;&gt;&quot;label&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;found&quot;&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb67-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            )&lt;/span&gt;
&lt;span id=&quot;cb67-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        )&lt;/span&gt;
&lt;span id=&quot;cb67-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;
&lt;span id=&quot;cb67-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb67-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;sumExample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sumExample &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (eChoice &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb67-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( eHandler &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (eVar &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb67-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; eHandler &lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;))&lt;/span&gt;
&lt;span id=&quot;cb67-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb67-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;what-we-gained&quot;&gt;What we gained&lt;/h3&gt;
&lt;p&gt;Now all of the previous “bad” examples we gave are finally
compiler-verified:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb68&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb68-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb68-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;Couldn't&lt;/span&gt; match &lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; (n0 &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs0&lt;/span&gt;
&lt;span id=&quot;cb68-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                     with&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; '[]&lt;/span&gt;
&lt;span id=&quot;cb68-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Expected&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; '[] (n0 &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb68-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Actual&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; ((n0 &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs0) (n0 &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb68-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[&lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb68-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;Couldn't&lt;/span&gt; match &lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; ‘&lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;’ with ‘&lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;’&lt;/span&gt;
&lt;span id=&quot;cb68-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;Expected&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; '[&lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;] (&lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb68-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;Actual&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; '[&lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;] (&lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb68-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (&lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;))&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; '[&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;]))&lt;/span&gt;
&lt;span id=&quot;cb68-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      ( &lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;)) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb68-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;IZ&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb68-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      )&lt;/span&gt;
&lt;span id=&quot;cb68-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;Couldn't&lt;/span&gt; match &lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; (n0 &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs0&lt;/span&gt;
&lt;span id=&quot;cb68-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;                     with&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; '[]&lt;/span&gt;
&lt;span id=&quot;cb68-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb68-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;Couldn't&lt;/span&gt; match &lt;span class=&quot;kw&quot;&gt;type&lt;/span&gt; ‘&lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;’ with ‘&lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;’&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And using the &lt;code&gt;ListIx&lt;/code&gt; helpers, those same failures show up as
failed index searches:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb69&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb69-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t (eVar &lt;span class=&quot;st&quot;&gt;&quot;missing&quot;&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb69-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;No&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; for ‘&lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;missing&quot;&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;’&lt;/span&gt;
&lt;span id=&quot;cb69-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t (eVar &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[&lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb69-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;No&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; for ‘&lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;’&lt;/span&gt;
&lt;span id=&quot;cb69-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      (eChoice &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;))&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; '[&lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;]))&lt;/span&gt;
&lt;span id=&quot;cb69-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      ( eHandler &lt;span class=&quot;st&quot;&gt;&quot;value&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Found&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (eVar &lt;span class=&quot;st&quot;&gt;&quot;missing&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb69-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; eHandler &lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;Missing&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (eVar &lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;)))&lt;/span&gt;
&lt;span id=&quot;cb69-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      )&lt;/span&gt;
&lt;span id=&quot;cb69-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;No&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; for ‘&lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;missing&quot;&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;’&lt;/span&gt;
&lt;span id=&quot;cb69-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt;interactive&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb69-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    • &lt;span class=&quot;dt&quot;&gt;No&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; for ‘&lt;span class=&quot;dt&quot;&gt;ListIx&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;message&quot;&lt;/span&gt; '[] &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;’&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Expressions that are not well-typed in our domain language are now rejected
by GHC!&lt;a class=&quot;footnote-ref&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fn1&quot; id=&quot;fnref1&quot;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;(As an exercise, can understand why those errors are what they are? Why they
all contain &lt;code&gt;ListIx _ '[]&lt;/code&gt;? For more ergonomics there is stuff we can
do with &lt;code&gt;TypeError&lt;/code&gt; machinery to make the messages a little prettier;
the &lt;em&gt;vinyl&lt;/em&gt; library does a lot to make error messages a bit better, like
in &lt;code&gt;HasField&lt;/code&gt; &lt;a href=&quot;https://hackage.haskell.org/package/vinyl/docs/Data-Vinyl-Derived.html#t:FieldType&quot;&gt;instance&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Note that this is sometimes done using straight &lt;a href=&quot;https://en.wikipedia.org/wiki/De_Bruijn_index&quot;&gt;De Bruijn indices&lt;/a&gt;:
&lt;code&gt;Expr :: [Ty] -&amp;gt; Type&lt;/code&gt;, so we don’t use any names but just the
direct index, but the point of this exercise is to be &lt;em&gt;borderline&lt;/em&gt;
unbearable to write, and &lt;em&gt;not&lt;/em&gt; to be &lt;em&gt;actually&lt;/em&gt; unbearable to
write.&lt;/p&gt;
&lt;h3 id=&quot;eval-with-a-typed-environment&quot;&gt;Eval with a typed environment&lt;/h3&gt;
&lt;p&gt;To actually write &lt;em&gt;eval&lt;/em&gt; now, we need to have a type-safe environment
to store these variables. In order to &lt;code&gt;eval&lt;/code&gt; an &lt;code&gt;Expr vs&lt;/code&gt;,
we need &lt;code&gt;EValue&lt;/code&gt;s for each &lt;code&gt;v&lt;/code&gt; in &lt;code&gt;vs&lt;/code&gt;. So for
&lt;code&gt;Expr [&quot;x&quot; ::: TInt, &quot;y&quot; ::: TBool]&lt;/code&gt;, we need to store a
&lt;code&gt;TInt&lt;/code&gt; and a &lt;code&gt;TBool&lt;/code&gt;. We can once again re-purpose
&lt;code&gt;Rec&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb70&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb70-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb70-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L130-L131&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb70-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb70-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb70-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb70-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValueField&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb70-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb70-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValueField&lt;/span&gt; '(l, a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb71&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb71-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb71-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t (&lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValueField&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;y&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;])&lt;/span&gt;
&lt;span id=&quot;cb71-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb71-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValueField&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;, &lt;span class=&quot;st&quot;&gt;&quot;y&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And so we can finally write:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb72&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb72-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L122-L271&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TBool&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVString&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TString&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVRecord&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValueField&lt;/span&gt; as &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TRecord&lt;/span&gt; as)&lt;/span&gt;
&lt;span id=&quot;cb72-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVSum&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Index&lt;/span&gt; as (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;TSum&lt;/span&gt; as)&lt;/span&gt;
&lt;span id=&quot;cb72-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb72-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;eval ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValueField&lt;/span&gt; vs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EValue&lt;/span&gt; t&lt;/span&gt;
&lt;span id=&quot;cb72-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;eval env &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; n) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; n&lt;/span&gt;
&lt;span id=&quot;cb72-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; b&lt;/span&gt;
&lt;span id=&quot;cb72-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; s) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVString&lt;/span&gt; s&lt;/span&gt;
&lt;span id=&quot;cb72-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; indexRec i env &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; v&lt;/span&gt;
&lt;span id=&quot;cb72-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; eval (&lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; env) body&lt;/span&gt;
&lt;span id=&quot;cb72-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; eval env f &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; g &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; g (eval env x)&lt;/span&gt;
&lt;span id=&quot;cb72-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; o x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; (o, eval env x, eval env y) &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;+&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb72-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb72-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;&amp;lt;=&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb72-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (&lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVBool&lt;/span&gt; (a &lt;span class=&quot;op&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb72-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVRecord&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; mapRec (\(&lt;span class=&quot;dt&quot;&gt;EField&lt;/span&gt; x) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt; (eval env x)) xs&lt;/span&gt;
&lt;span id=&quot;cb72-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; e i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; eval env e &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVRecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; indexRec i xs &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt; v &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; v&lt;/span&gt;
&lt;span id=&quot;cb72-32&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; i x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;EVSum&lt;/span&gt; i (eval env x)&lt;/span&gt;
&lt;span id=&quot;cb72-33&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt; x hs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; eval env x &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-34&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVSum&lt;/span&gt; i y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; indexRec i hs &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb72-35&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb72-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt; h &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; eval (&lt;span class=&quot;dt&quot;&gt;EVField&lt;/span&gt; y &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; env) h&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At least, we are here. A type-safe EDSL where only AST’s that can be validly
evaluated are legal to represent in Haskell. At least, we can embrace the
freedom of not having to carefully construct your terms. You can relax now. The
compiler and the types have your back.&lt;/p&gt;
&lt;p&gt;Even
&lt;code&gt;EVFun :: (EValue a -&amp;gt; EValue b) -&amp;gt; EValue (a :-&amp;gt; b)&lt;/code&gt; has a
total &lt;code&gt;EValue a -&amp;gt; EValue b&lt;/code&gt;, making the closure evaluation also
fully total.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb73&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb73-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb73-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L145-L147&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb73-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb73-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb73-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb73-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;plusThree ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] (&lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;TInt&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb73-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb73-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;plusThree &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb73-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb73-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  eLambda &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; (eVar &lt;span class=&quot;st&quot;&gt;&quot;x&quot;&lt;/span&gt;) (&lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb74&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb74-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb74-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; eval &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; plusThree &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb74-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb74-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;EVFun&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; f (&lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;4&lt;/span&gt;) &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb74-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb74-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;dt&quot;&gt;EVInt&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;print&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb74-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb74-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;pretty-printing-scoped-expressions&quot;&gt;Pretty-Printing Scoped
Expressions&lt;/h3&gt;
&lt;p&gt;Now to get to the entire utility of this abstraction: inspecting and
consuming the structure. For our new structure, we took out the string name from
&lt;code&gt;EVar&lt;/code&gt; in lieu of an index. This is intentional, so that we keep the
“responsibility” of storing the string name at the &lt;code&gt;ELambda&lt;/code&gt;
constructor and not have &lt;code&gt;EVar&lt;/code&gt; redundantly store it.&lt;/p&gt;
&lt;p&gt;However, this means that for pretty-printing, we will need to track the
variable names in the environment as we descend into lambdas. This is done very
similar to how it was done in &lt;code&gt;eval&lt;/code&gt;, but instead of tracking and
indexing out the evaluated &lt;code&gt;EValue&lt;/code&gt;s, we track and index out the
string names of each variable instead.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb75&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb75-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb75-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L182-L183&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb75-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb75-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb75-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb75-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Symbol&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Ty&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb75-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb75-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;KnownSymbol&lt;/span&gt; l &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; (l &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb76&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb76-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb76-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt;t &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;world&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb76-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb76-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; [&lt;span class=&quot;st&quot;&gt;&quot;hello&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; a, &lt;span class=&quot;st&quot;&gt;&quot;world&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;:::&lt;/span&gt; b]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb77&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb77-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- source: https://github.com/mstksg/inCode/tree/master/code-samples/typed-sm-lc/ExprStage4.hs#L185-L245&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-3&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppPrim ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Prim&lt;/span&gt; t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb77-4&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppPrim &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-5&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PInt&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty n&lt;/span&gt;
&lt;span id=&quot;cb77-6&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PBool&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;if&lt;/span&gt; b &lt;span class=&quot;kw&quot;&gt;then&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;true&quot;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-7&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;PString&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty (&lt;span class=&quot;fu&quot;&gt;show&lt;/span&gt; s)&lt;/span&gt;
&lt;span id=&quot;cb77-8&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-9&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppOp ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Op&lt;/span&gt; a b c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb77-10&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppOp &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-11&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OPlus&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-12&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OTimes&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-13&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OLte&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&amp;lt;=&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-14&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;OAnd&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&amp;amp;&amp;amp;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-15&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-16&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppFields ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; vs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;ExprField&lt;/span&gt; vs) xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann]&lt;/span&gt;
&lt;span id=&quot;cb77-17&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppFields _ &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; []&lt;/span&gt;
&lt;span id=&quot;cb77-18&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppFields names (&lt;span class=&quot;dt&quot;&gt;EField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l x &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; xs) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-19&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  (PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)) &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;=&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr names &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; x) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; ppFields names xs&lt;/span&gt;
&lt;span id=&quot;cb77-20&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-21&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppHandlers ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; vs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;ExprHandler&lt;/span&gt; vs b) xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [&lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann]&lt;/span&gt;
&lt;span id=&quot;cb77-22&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppHandlers _ &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; []&lt;/span&gt;
&lt;span id=&quot;cb77-23&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppHandlers names (&lt;span class=&quot;dt&quot;&gt;EHandler&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l body &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; xs) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-24&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ( PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l))&lt;/span&gt;
&lt;span id=&quot;cb77-25&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n))&lt;/span&gt;
&lt;span id=&quot;cb77-26&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;-&amp;gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-27&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr (&lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; names) &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; body&lt;/span&gt;
&lt;span id=&quot;cb77-28&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  )&lt;/span&gt;
&lt;span id=&quot;cb77-29&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; ppHandlers names xs&lt;/span&gt;
&lt;span id=&quot;cb77-30&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-31&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ppExpr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rec&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; vs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bool&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; vs t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb77-32&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-32&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ppExpr names paren &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \&lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-33&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-33&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EPrim&lt;/span&gt; p &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; ppPrim p&lt;/span&gt;
&lt;span id=&quot;cb77-34&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-34&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EVar&lt;/span&gt; i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; indexRec i names &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-35&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-35&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n))&lt;/span&gt;
&lt;span id=&quot;cb77-36&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-36&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ELambda&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n body &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-37&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-37&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-38&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-38&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;st&quot;&gt;&quot;\\&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n))&lt;/span&gt;
&lt;span id=&quot;cb77-39&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-39&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;-&amp;gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-40&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-40&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr (&lt;span class=&quot;dt&quot;&gt;NameField&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;n &lt;span class=&quot;op&quot;&gt;:&amp;amp;&lt;/span&gt; names) &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; body&lt;/span&gt;
&lt;span id=&quot;cb77-41&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-41&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EApply&lt;/span&gt; f x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-42&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-42&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; ppExpr names &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr names &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb77-43&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-43&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EOp&lt;/span&gt; o x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-44&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-44&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; ppExpr names &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppOp o &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr names &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; y&lt;/span&gt;
&lt;span id=&quot;cb77-45&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-45&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ERecord&lt;/span&gt; xs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-46&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-46&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    PP.encloseSep &lt;span class=&quot;st&quot;&gt;&quot;{ &quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; }&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;, &quot;&lt;/span&gt; (ppFields names xs)&lt;/span&gt;
&lt;span id=&quot;cb77-47&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-47&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EAccess&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l e _ &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-48&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-48&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ppExpr names &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; e &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;.&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l))&lt;/span&gt;
&lt;span id=&quot;cb77-49&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-49&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;EChoice&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l _ x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-50&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-50&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; PP.pretty (symbolVal (&lt;span class=&quot;dt&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;l)) &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr names &lt;span class=&quot;dt&quot;&gt;True&lt;/span&gt; x&lt;/span&gt;
&lt;span id=&quot;cb77-51&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-51&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;ECase&lt;/span&gt; x hs &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-52&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-52&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-53&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-53&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      PP.sep&lt;/span&gt;
&lt;span id=&quot;cb77-54&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-54&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        [ &lt;span class=&quot;st&quot;&gt;&quot;case&quot;&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; ppExpr names &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;&amp;lt;+&amp;gt;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;of&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-55&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-55&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        , PP.encloseSep &lt;span class=&quot;st&quot;&gt;&quot;{ &quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot; }&quot;&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;; &quot;&lt;/span&gt; (ppHandlers names hs)&lt;/span&gt;
&lt;span id=&quot;cb77-56&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-56&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        ]&lt;/span&gt;
&lt;span id=&quot;cb77-57&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-57&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-58&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-58&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    wrap&lt;/span&gt;
&lt;span id=&quot;cb77-59&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-59&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; paren &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; PP.parens&lt;/span&gt;
&lt;span id=&quot;cb77-60&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-60&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;op&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-61&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-61&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-62&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-62&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;prettyExpr ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Expr&lt;/span&gt; '[] t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;PP.Doc&lt;/span&gt; ann&lt;/span&gt;
&lt;span id=&quot;cb77-63&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb77-63&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;prettyExpr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ppExpr &lt;span class=&quot;dt&quot;&gt;RNil&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;False&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb78&quot;&gt;&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;&lt;span id=&quot;cb78-1&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb78-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;ghci&lt;span class=&quot;op&quot;&gt;&amp;gt;&lt;/span&gt; prettyExpr fifteen&lt;/span&gt;
&lt;span id=&quot;cb78-2&quot;&gt;&lt;a href=&quot;http://feeds.feedburner.com/incodeblog#cb78-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;(\x &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; x &lt;span class=&quot;op&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&lt;/span&gt;) &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;the-next-step&quot;&gt;The Next Step&lt;/h2&gt;
&lt;p&gt;Well, we set out with a simple goal: an expression language that lives within
Haskell that we can inspect and interrogate within the language, but where it
was impossible to construct (in Haskell) a term that did not type-check (in the
domain language).&lt;/p&gt;
&lt;p&gt;But, honestly, why does it matter that our expression language has to reject
invalid domain-level terms at the Haskell level? Why couldn’t we go the way of
other expression DSLs in Haskell, where we settle with “untyped” terms being
expressible in Haskell, and validated using a separate &lt;code&gt;typeCheck&lt;/code&gt;
function to validate terms at runtime?&lt;/p&gt;
&lt;p&gt;I don’t know. But really, maybe this is another way of taking the old &lt;a href=&quot;https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/&quot;&gt;Parse,
Don’t Validate&lt;/a&gt; adage to the extreme. Why allow yourself to construct invalid
terms of your domain inside Haskell? Why use a “validate” function
(&lt;code&gt;isValid&lt;/code&gt;) when you can just make invalid terms impossible to
construct?&lt;/p&gt;
&lt;p&gt;But…no. There is no other choice. We CANNOT allow invalid domain terms to be
constructible. If we have the ability to do better, we MUST. With great power
comes great responsibility. And if we compromise here, how can we trust
ourselves not to compromise when it really matters?&lt;/p&gt;
&lt;p&gt;One must imagine the stubborn typer happy.&lt;/p&gt;
&lt;p&gt;In Part 2, we’ll use our new EDSL to specify visualizable state machines and
programs within Haskell that are type-checked to be correct, and what it looks
like to actually &lt;em&gt;use&lt;/em&gt; these within Haskell. And in Part 3, we’ll start
“compiling” them to different language targets and different backends, with the
assurance that our generated programs are all synchronized and
self-consistent.&lt;/p&gt;
&lt;h3 id=&quot;a-note-on-ai-coding&quot;&gt;A Note on AI Coding&lt;/h3&gt;
&lt;p&gt;I guess I’m going to have to start mentioning this in every post.
&lt;em&gt;But&lt;/em&gt;, I really do feel like this “extreme type safety” approach is more
critical than ever, in the age of agentic coding and LLM. I’ve been using LLMs
in my daily coding for many months now at this point, and one common pattern
I’ve noticed: when I start with a design with very clear, very strict types,
LLMs excel. They make much fewer errors, and the type system provides more
immediate feedback on their progress, without needing hundreds of defensive
&lt;code&gt;x != null&lt;/code&gt;-style guard pollution.&lt;/p&gt;
&lt;p&gt;Once I can express what I want in the language of extreme “invalid states
unrepresentable” types, LLM agents no longer feel like agents of chaotic
spaghetti extruding unmaintainable code. Instead, it feels like…seeding a
crystal and watching it grow into a beautiful, shimmering lattice. It feels like
the language they yearn to speak. And the more expressive your types, the more
beautiful the crystalline structure in the end.&lt;/p&gt;
&lt;p&gt;Honestly, humans might have problems writing and using this code, but LLMs
definitely don’t, if properly scaffolded! Since early 2026, at least, for me.
We’ll explore a bit more about this once we have more to work with in Part
2.&lt;/p&gt;
&lt;h2 id=&quot;special-thanks&quot;&gt;Special Thanks&lt;/h2&gt;
&lt;p&gt;I am very humbled to be supported by an amazing community, who make it
possible for me to devote time to researching and writing these posts. Very
special thanks to my supporter at the “Amazing” level on &lt;a href=&quot;https://www.patreon.com/justinle/overview&quot;&gt;patreon&lt;/a&gt;, Josh Vera! :)&lt;/p&gt;
&lt;section class=&quot;footnotes footnotes-end-of-document&quot; id=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;&lt;p&gt;Excluding &lt;code&gt;_|_&lt;/code&gt; in Haskell-land (recursion or
&lt;code&gt;undefined&lt;/code&gt;), unfortunately.&lt;a class=&quot;footnote-back&quot; href=&quot;http://feeds.feedburner.com/incodeblog#fnref1&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Tue, 07 Jul 2026 17:11:34 +0000</pubDate>
</item>
<item>
	<title>Gabriella Gonzalez: Mechanized type inference for record concatenation</title>
	<guid isPermaLink="true">https://haskellforall.com/2026/07/mechanized-type-inference-for-record-concatenation</guid>
	<link>https://haskellforall.com/2026/07/mechanized-type-inference-for-record-concatenation</link>
	<description>Type inference algorithm for biased record concatenation</description>
	<pubDate>Tue, 07 Jul 2026 00:00:00 +0000</pubDate>
</item>

</channel>
</rss>
