<?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>Sandy Maguire: Musically Horizontal Monads</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/horizontal-monads/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/horizontal-monads/index.html</link>
	<description>&lt;p&gt;Sorry for the delay! My household has had a couple of chaotic days of everyone-being-very-very-sick and house guests and 4:30am airport drop-offs. But we’re back! This blog post got written in several parts over several days, so it might be a little disjointed. Apologies.&lt;/p&gt;
&lt;p&gt;Anyway…&lt;/p&gt;
&lt;p&gt;In the &lt;a href=&quot;https://reasonablypolymorphic.com/blog/musical-spans/&quot;&gt;last post&lt;/a&gt; we discussed a way of attaching durations to instantaneous musical events: just wrap them with a duration, stupid! Then we wrote a 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;spanning ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; (), &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Span&lt;/span&gt; a))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which takes an event stream of stopping points, as well as an event stream of starting points&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;. It then spans each starting event until the next stop or start. The stops are there so that we can add silence if necessary.&lt;/p&gt;
&lt;p&gt;Having a function like &lt;code&gt;spanning&lt;/code&gt; around is cool because it means we can just emit starting and stopping events from whatever upstream process we might like, and it will automatically turn them into “rhythm.” And of course, that upstream process is welcome to derive the stop stream from the start stream (or vice versa), if you don’t want to have to think about it.&lt;/p&gt;
&lt;p&gt;Anyway, that’s all and good. But musicians have a lot of intellectual moment behind &lt;em&gt;not&lt;/em&gt; thinking about music that way. Instead they usually just think of having durations intrinsically attached to notes — that is to say, they conceive of the note and the duration at the same time. Since we have an explicit desideratum here of making it easy to get good results, we ought to provide some more conventional ways of thinking about writing lines.&lt;/p&gt;
&lt;p&gt;Writing lines is usually done as “pick a note and a duration, then after that duration, pick another note and another duration. Rinse and repeat.” Of course, there’s some wiggle room where you can instead choose to &lt;em&gt;not&lt;/em&gt; select a note and leave silence in its place. Depending on how functional-programming-pilled you are, this either sounds like a monoid or a monad. Let’s explore both ideas, starting with the monoid.&lt;/p&gt;
&lt;p&gt;A horizontal monoid seems like it ought to sequence one piece of music before another, in a temporal-sense. That is, &lt;code&gt;a &amp;lt;&amp;gt; b&lt;/code&gt; should play &lt;code&gt;a&lt;/code&gt;, and then once &lt;code&gt;a&lt;/code&gt; is done, it should play &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Recall that &lt;code&gt;Signal&lt;/code&gt; already has &lt;code&gt;Semigroup/Monoid&lt;/code&gt; instances, but looking at the type, they necessarily can’t be the right thing:&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;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a) [(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; a)]&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;deriving&lt;/span&gt; stock &lt;span class=&quot;dt&quot;&gt;Functor&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;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;Ap&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you’re unfamiliar with the &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Data-Monoid.html#t:Ap&quot;&gt;&lt;code&gt;Ap&lt;/code&gt;&lt;/a&gt; newtype, it lifts the &lt;code&gt;Semigroup&lt;/code&gt; and &lt;code&gt;Monoid&lt;/code&gt; instances over the &lt;code&gt;Applicative&lt;/code&gt; structure. That is, we’ve effectively derived the following instances:&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;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Semigroup&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Semigroup&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a) &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&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;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; liftA2 (&lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;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&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;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Monoid&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Monoid&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a) &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&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;fu&quot;&gt;mempty&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;fu&quot;&gt;mempty&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The clue that this is doing the wrong thing for our purposes is the &lt;code&gt;Semigroup a =&amp;gt;&lt;/code&gt; constraint. If we were putting one &lt;code&gt;Signal a&lt;/code&gt; after another &lt;em&gt;in time&lt;/em&gt;, we’d never need to combine &lt;code&gt;a&lt;/code&gt;s, and thus we would have no need for a &lt;code&gt;Semigroup a&lt;/code&gt; instance. Thus, this instance &lt;em&gt;must&lt;/em&gt; be doing a point-wise zip of times. Which is all to say that this is no good to us.&lt;/p&gt;
&lt;p&gt;What we instead must do is build a wrapper type which provides the correct temporal-sequencing monoid. Let’s take a moment to pour over our primitives and see if any of them are feasible for actually making that happen.&lt;/p&gt;
&lt;p&gt;The relevant primitive happens to be &lt;code&gt;switchS&lt;/code&gt;:&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;switchS ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (a, &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; b) &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;Signal&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which long-time readers might recall has &lt;a href=&quot;https://reasonablypolymorphic.com/blog/yampa-switching/&quot;&gt;seen some action on this blog&lt;/a&gt; in the past. The idea is that we will run the first &lt;code&gt;Signal&lt;/code&gt; until the first occurrence of the &lt;code&gt;Event c&lt;/code&gt;, then use that in order to switch to the given continuation.&lt;/p&gt;
&lt;p&gt;So &lt;code&gt;switchS&lt;/code&gt; gives us the “do one thing after another” semantics we want (notice its lack of a &lt;code&gt;Semigroup a&lt;/code&gt; constraint!) This happens to be most of the work done, now it’s just a matter of accumulating the rest of the bits we need: the actual notes we’re playing, the start points, and the stop points. Let’s package all of that up into a little datatype:&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;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt;&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;  {&lt;span class=&quot;ot&quot;&gt; hs_out ::&lt;/span&gt; a&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;ot&quot;&gt; hs_starts ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Event&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;  ,&lt;span class=&quot;ot&quot;&gt; hs_stops ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; ()&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&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;deriving&lt;/span&gt; stock (&lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Generic&lt;/span&gt;)&lt;/span&gt;
&lt;span id=&quot;cb5-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-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;Semigroup&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Monoid&lt;/span&gt;) via &lt;span class=&quot;dt&quot;&gt;Generically&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt; a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A horizontal structure then is some sort of newtype over &lt;code&gt;Signal (HState a)&lt;/code&gt;. As it happens, writing the monadic horizontal structure is going to be a little more informative at this point, since we can lift the monoidal version out directly. But we can immediately put &lt;code&gt;HState&lt;/code&gt; to work!&lt;/p&gt;
&lt;p&gt;In the exact style of &lt;a href=&quot;https://reasonablypolymorphic.com/blog/yampa-switching/&quot;&gt;Yampa Switching&lt;/a&gt;, we will use the &lt;a href=&quot;https://hackage-content.haskell.org/package/mtl-2.3.2/docs/Control-Monad-Cont.html#t:Cont&quot;&gt;&lt;code&gt;Cont&lt;/code&gt;inuation monad&lt;/a&gt;, which allows us to bind over the &lt;code&gt;a&lt;/code&gt; in some function &lt;code&gt;(a -&amp;gt; b) -&amp;gt; b&lt;/code&gt; — in our case, this will be the &lt;code&gt;(a -&amp;gt; Signal b) -&amp;gt; Signal b&lt;/code&gt; that curries out of &lt;code&gt;switchS&lt;/code&gt;. This is already well-treaded territory on this blog, so you can go read the Yampa Switching post if you’re curious about the exact details here.&lt;/p&gt;
&lt;p&gt;As a first approximation, wrapping this up gives us the type:&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;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; o a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&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;  {&lt;span class=&quot;ot&quot;&gt; unHorizontal ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Cont&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt; o)) a&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&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, this is somewhat unsatisfactory. &lt;code&gt;Signal&lt;/code&gt;s are covariant in their output (here &lt;code&gt;HState o&lt;/code&gt;), but &lt;code&gt;Cont&lt;/code&gt; is &lt;em&gt;invariant&lt;/em&gt; in that parameter. Blinding moving forward with &lt;code&gt;Horizontal&lt;/code&gt; as written would mean we wouldn’t have an operation &lt;code&gt;mapOut :: (o -&amp;gt; o') -&amp;gt; Horizontal o a -&amp;gt; Horizontal o' a&lt;/code&gt; which in practice turns out to be quite a limitation indeed!&lt;/p&gt;
&lt;p&gt;Instead we can do a little bit of a &lt;a href=&quot;https://hackage-content.haskell.org/package/kan-extensions-5.2.8/docs/Data-Functor-Yoneda.html#t:Yoneda&quot;&gt;Yoneda&lt;/a&gt;-style encoding, where we hold onto a function that maps from &lt;code&gt;o&lt;/code&gt; into the eventual result of the invariant &lt;code&gt;r&lt;/code&gt; parameter. Check this thing out:&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;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; f o a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Yo&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;  {&lt;span class=&quot;ot&quot;&gt; unYo ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; b&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (o &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;Cont&lt;/span&gt; (f b) a&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&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;kw&quot;&gt;deriving&lt;/span&gt; stock &lt;span class=&quot;dt&quot;&gt;Functor&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;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;Ap&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; f o) a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The idea is that we delay building up our &lt;code&gt;Cont&lt;/code&gt; until you hand us a way of mapping from &lt;code&gt;o&lt;/code&gt; into whatever type &lt;code&gt;b&lt;/code&gt; you eventually end up wanting. We usually use a trick like this to get a free &lt;em&gt;functor&lt;/em&gt;, but here we’re using it to get a free &lt;em&gt;bifunctor&lt;/em&gt;:&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;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Bifunctor&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; f) &lt;span class=&quot;kw&quot;&gt;where&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;  bimap f g (&lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; y) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; g &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; y &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; k &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; f&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;Applicative&lt;/code&gt; and &lt;code&gt;Monad&lt;/code&gt; instances also come already-paid-for, but we must write them out ourselves:&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;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; r o) &lt;span class=&quot;kw&quot;&gt;where&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;fu&quot;&gt;pure&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&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;  liftA2 f (&lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; liftA2 f (a k) (b k)&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&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;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Monad&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; r o) &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://reasonablypolymorphic.com#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; m &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; m k &lt;span class=&quot;op&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; \a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; unYo (f a) k&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This concludes our diversion into writing &lt;code&gt;Yo&lt;/code&gt;. Popping the stack: recall we’re still trying to write a monadic horizontal structure. Which we can now write out as &lt;code&gt;Horizontal r o a&lt;/code&gt; (with &lt;code&gt;r&lt;/code&gt; being left intentionally unmotivated for the time being), getting all our relevant instances for free:&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;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r o a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&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;  {&lt;span class=&quot;ot&quot;&gt; unHorizontal ::&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;forall&lt;/span&gt; b&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; (o &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;Cont&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt; b)) a&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;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Bifunctor&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;    via &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Compose&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt;)&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 class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Monad&lt;/span&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;    via &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Compose&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt;) o&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;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;)&lt;/span&gt;
&lt;span id=&quot;cb10-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb10-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    via &lt;span class=&quot;dt&quot;&gt;Yo&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Compose&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt;) o a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Take a moment to appreciate how &lt;em&gt;amazing&lt;/em&gt; GHC is here; each of those deriving vias is lifting an unrelated type through a polytype &lt;code&gt;forall b&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; through a &lt;code&gt;Compose&lt;/code&gt;, the latter of which doesn’t have most instances we’re actually intersted in!&lt;a class=&quot;footnote-ref&quot; href=&quot;https://reasonablypolymorphic.com#fn2&quot; id=&quot;fnref2&quot;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Our primary introduction form for &lt;code&gt;Horizontal&lt;/code&gt; is &lt;code&gt;mkHorizontal&lt;/code&gt;, which acts as one &lt;code&gt;Signal o&lt;/code&gt; for some amount of time, before rendering a continuation of type &lt;code&gt;a&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;{-# LANGUAGE ApplicativeDo #-}&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&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;ot&quot;&gt;mkHorizontal ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; o &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&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;Horizontal&lt;/span&gt; r o 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;mkHorizontal so t a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \k &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; cont &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; switchS &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;cb11-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  o &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; so&lt;/span&gt;
&lt;span id=&quot;cb11-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  start &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; atS &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb11-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  stop &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; atS t ()&lt;/span&gt;
&lt;span id=&quot;cb11-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ea &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; atS t a&lt;/span&gt;
&lt;span id=&quot;cb11-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-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;HState&lt;/span&gt; (k o) start stop, ea)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that we issue a start event at time 0, and both a stop event and switch event at time &lt;code&gt;t&lt;/code&gt;. From this we can derive &lt;code&gt;fill&lt;/code&gt; and &lt;code&gt;hit&lt;/code&gt;, both of which use a constant value across their &lt;code&gt;Signal o&lt;/code&gt;, differing only in what they return as a monadic continuation:&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;fill ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&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;Horizontal&lt;/span&gt; r a 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;fill t a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; mkHorizontal (&lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; a) t a&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&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;hit ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&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;Horizontal&lt;/span&gt; r a ()&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;hit t &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; void &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; fill t&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And of course, the &lt;code&gt;mapOut&lt;/code&gt; function we had to invent the &lt;code&gt;Yo&lt;/code&gt; machinery in order to provide:&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;mapOut ::&lt;/span&gt; (o &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; o') &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r o a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r o' 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;mapOut &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;flip&lt;/span&gt; bimap &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;a-musical-aside&quot;&gt;A Musical Aside&lt;/h2&gt;
&lt;p&gt;It’s probably worth taking a moment to discuss &lt;em&gt;what exactly the hell&lt;/em&gt; is going on with all of this crazy yoneda continuation monad stuff. What does it &lt;em&gt;buy&lt;/em&gt; us?&lt;/p&gt;
&lt;p&gt;Lets write a little motif:&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;motif ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r &lt;span class=&quot;dt&quot;&gt;Note&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Note&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;motif 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;cb14-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  hit (&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;) n&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;  hit (&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 class=&quot;op&quot;&gt;$&lt;/span&gt; n &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;cb14-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  hit (&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 class=&quot;op&quot;&gt;$&lt;/span&gt; n &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;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;fu&quot;&gt;pure&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; n &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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;notice that this emits notes relative to its input. In somewhat-standard notation, &lt;code&gt;motif F&lt;/code&gt; looks like this&lt;a class=&quot;footnote-ref&quot; href=&quot;https://reasonablypolymorphic.com#fn3&quot; id=&quot;fnref3&quot;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Motif&quot; src=&quot;https://reasonablypolymorphic.com/images/music4/out1.cropped.svg&quot; /&gt;&lt;figcaption&gt;Motif&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;using this triangle symbol to indicate the continuation. But now check this out. We can bind the result in order to build a musical sequence; &lt;code&gt;motif F &amp;gt;&amp;gt;= motif&lt;/code&gt; looks like this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Sequencing Motif&quot; src=&quot;https://reasonablypolymorphic.com/images/music4/out2.cropped.svg&quot; /&gt;&lt;figcaption&gt;Sequencing Motif&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;This happens to be the underlying machinery in Tymoczko’s work on sequences (chapter 4 of &lt;a href=&quot;https://dmitri.mycpanel.princeton.edu/tonality-an-owners-manual.html&quot;&gt;Tonality&lt;/a&gt;), though it is not presented as such (and I suspect he didn’t have monads in mind when writing it.)&lt;/p&gt;
&lt;h2 id=&quot;back-to-haskell&quot;&gt;Back to Haskell&lt;/h2&gt;
&lt;p&gt;Revitalized with a real connection to real musical ideas, we press on through the mires of abstract nonsense. The most pressing problem remaining is how do we eliminate &lt;code&gt;Horizontal&lt;/code&gt;? By means of &lt;code&gt;unsafeRunHorizontal&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;unsafeRunHorizontal&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; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r o a&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; (a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; o)&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;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt; o)&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;unsafeRunHorizontal (&lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; f) k&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;  &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; runCont (f &lt;span class=&quot;fu&quot;&gt;id&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;  &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; (\o &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt; o &lt;span class=&quot;dt&quot;&gt;NoEvent&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NoEvent&lt;/span&gt;) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; k&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which isn’t actually unsafe, but &lt;em&gt;will be&lt;/em&gt; due to a few additions to our API in a second. But a nicer eliminator is &lt;code&gt;horizontal&lt;/code&gt;, which as originally promised, calls &lt;code&gt;spanning&lt;/code&gt; on our behalf:&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;ot&quot;&gt;horizontal ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r o a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Span&lt;/span&gt; o))&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;horizontal h&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;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;flip&lt;/span&gt; runSF&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;      ( unsafeRunHorizontal h&lt;/span&gt;
&lt;span id=&quot;cb16-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-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;const&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-6&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;pure&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-7&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;error&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;impossible: horizontal sampled too late&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      )&lt;/span&gt;
&lt;span id=&quot;cb16-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; proc (&lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt; o starts stops) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; spanning &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; (stops, o &lt;span class=&quot;op&quot;&gt;&amp;lt;$&lt;/span&gt; starts)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we use &lt;code&gt;unsafeRunHorizontal&lt;/code&gt; and pass it a dummy continuation, since we know we’re never going to actually sample it past the duration of our &lt;code&gt;Horizontal&lt;/code&gt;. Then we take the output &lt;code&gt;HState&lt;/code&gt;, sample &lt;code&gt;o&lt;/code&gt; &lt;em&gt;only at the start points&lt;/em&gt;, and pass the stop points through unmolested.&lt;/p&gt;
&lt;p&gt;It can also be useful to turn a &lt;code&gt;Horizontal&lt;/code&gt; directly into an event stream, ignoring its spans:&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;ot&quot;&gt;getHorizontal ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r o a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; o)&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;getHorizontal &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;fmap&lt;/span&gt; getSpan) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; horizontal&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;putting-everything-to-rest&quot;&gt;Putting Everything to Rest&lt;/h2&gt;
&lt;p&gt;The final two mysteries remaining are the unmotivated &lt;code&gt;r&lt;/code&gt; parameter in &lt;code&gt;Horizontal r o a&lt;/code&gt;, and why &lt;code&gt;unsafeRunHorizontal&lt;/code&gt; is unsafe.&lt;/p&gt;
&lt;p&gt;Recall that musicians often think about periods of silence, or &lt;em&gt;rests.&lt;/em&gt; While we could model rests as a &lt;code&gt;Nothing&lt;/code&gt; value in a &lt;code&gt;Maybe&lt;/code&gt;-typed &lt;code&gt;Horizontal&lt;/code&gt;, this would get rather in the way of our nice combinators! In particular, nothing says we must interpret a &lt;code&gt;Horizontal&lt;/code&gt; as a discrete function; if it’s continuous, then the &lt;code&gt;Nothing&lt;/code&gt; value would make sense. But if we’re exporting discrete function, then the &lt;code&gt;Nothing&lt;/code&gt;s never actually show up and the &lt;code&gt;Just&lt;/code&gt;s are unnecessary.&lt;/p&gt;
&lt;p&gt;So here’s the trick; this phantom &lt;code&gt;r&lt;/code&gt; type in &lt;code&gt;Horizontal r o a&lt;/code&gt; corresponds to “are there any rests in this value?” We can use a &lt;code&gt;DataKinds&lt;/code&gt; to get ourselves a type-level value to play with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;data Restful = HasRests

type Horizontal :: Restful -&amp;gt; Type -&amp;gt; Type -&amp;gt; Type

type role Horizontal nominal representational representational&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our new &lt;code&gt;rest&lt;/code&gt; combinator is the only thing that uses the &lt;code&gt;r&lt;/code&gt; parameter:&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;rest ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HasRests&lt;/span&gt; a ()&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;rest t &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; cont &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; switchS &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;cb19-3&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  stop &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; atS &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ()&lt;/span&gt;
&lt;span id=&quot;cb19-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ea &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; atS t ()&lt;/span&gt;
&lt;span id=&quot;cb19-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-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;dt&quot;&gt;HState&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;impossible: rest can't be sampled&quot;&lt;/span&gt;) &lt;span class=&quot;dt&quot;&gt;NoEvent&lt;/span&gt; stop&lt;/span&gt;
&lt;span id=&quot;cb19-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;       , ea&lt;/span&gt;
&lt;span id=&quot;cb19-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb19-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;       )&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that &lt;code&gt;rest&lt;/code&gt; sets a stop point at t=0, and doesn’t create any new start points. Furthermore, the actual value of type &lt;code&gt;a&lt;/code&gt; here is an &lt;code&gt;error&lt;/code&gt; value. It claims to be “impossible,” which we will enforce momentarily.&lt;/p&gt;
&lt;p&gt;Something peculiar here is that we only have a single type of kind &lt;code&gt;Restful&lt;/code&gt;, namely &lt;code&gt;HasRests&lt;/code&gt;. How can we differentiate &lt;code&gt;Horizontal&lt;/code&gt;s that use &lt;code&gt;rest&lt;/code&gt; from those that don’t? Our old friend &lt;a href=&quot;https://wiki.haskell.org/Monad/ST#An_explanation_in_Haskell-Cafe&quot;&gt;the ST trick&lt;/a&gt; comes knocking, requiring that &lt;code&gt;r&lt;/code&gt; remain fully polymorphic:&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;switchHorizontal&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; r&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r o a)&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;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;Signal&lt;/span&gt; o)&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;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;HState&lt;/span&gt; o)&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;switchHorizontal h &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; unsafeRunHorizontal h&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now &lt;code&gt;switchHorizontal&lt;/code&gt; can give us a continuous function &lt;em&gt;only when the &lt;code&gt;Horizontal&lt;/code&gt; doesn’t contain any &lt;code&gt;rest&lt;/code&gt;s!&lt;/em&gt; Usage of &lt;code&gt;unsafeRunHorizontal&lt;/code&gt; makes no such guarantees, and thus will crash with the impossible error when used on a &lt;code&gt;rest&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that &lt;code&gt;horizontal&lt;/code&gt; has no such compunctions; it samples its function only at the designated start points, which &lt;code&gt;rest&lt;/code&gt; doesn’t provide. And since our semigroup/applicative instances are all temporally end-to-end, nothing can introduce a start point inside of a &lt;code&gt;rest&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;our-temporal-monoid&quot;&gt;Our Temporal Monoid&lt;/h2&gt;
&lt;p&gt;We began all of this looking for a temporal monoid whose multiplication would glue horizontal structures end-to-end. We then switched over to making a monad that did the same thing, but we don’t need to give up on our dreams of monoids!&lt;/p&gt;
&lt;p&gt;In fact, &lt;code&gt;Horizontal&lt;/code&gt; is already the monoid we want, with a minor niggle in that it wants a semigroup/monoid over the continuation value. If we tie that to be &lt;code&gt;()&lt;/code&gt; everything falls out for free:&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;kw&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HM&lt;/span&gt; r o &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;HM&lt;/span&gt; {&lt;span class=&quot;ot&quot;&gt; unHM ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r o () }&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;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;Semigroup&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Monoid&lt;/span&gt;)&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;co&quot;&gt;-- Get a functor instance via our Bifunctor&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-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;Functor&lt;/span&gt; via &lt;span class=&quot;dt&quot;&gt;Data.Bifunnctor.Flip.Flip&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Horizontal&lt;/span&gt; r) ()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I don’t actually think this type is worth using, since it means our introduction forms need to be lifted into this newtype. But it’s nice to know that we could do such a thing if we wanted to!&lt;/p&gt;
&lt;h2 id=&quot;next-time&quot;&gt;Next Time&lt;/h2&gt;
&lt;p&gt;That’s all I have to say about rhythm for now. I don’t consider this to be the last word, but it’s as far as I’ve figured anything out. I’ve been reading a ton of textbooks on the topic, and the author who seems to be the &lt;em&gt;least&lt;/em&gt; confused about it is &lt;a href=&quot;https://en.wikipedia.org/wiki/Peter_Westergaard's_tonal_theory#Operations_on_rhythm&quot;&gt;Peter Westergaard&lt;/a&gt;. His formulation is in terms of “if you want a listener to mentally parse a hierarchical structure out of your notes, what properties would make that parsing easy?”&lt;/p&gt;
&lt;p&gt;That’s a thing I’d like to explore further, but I’m on a &lt;a href=&quot;https://nano2.org/&quot;&gt;publishing deadline&lt;/a&gt; here, so I can’t take too much time to do research in between blog posts. Next time we’ll discuss &lt;a href=&quot;https://dmitri.mycpanel.princeton.edu/tonality-an-owners-manual.html&quot;&gt;Tymoczko’s theory of harmony&lt;/a&gt;, which will dramatically power up our ability to make something that sounds like &lt;em&gt;music.&lt;/em&gt; Until then!&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;Writing that sentence out makes it clear that I got the arguments backwards. Oops!&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;li id=&quot;fn2&quot;&gt;&lt;p&gt;As a little aside, if you’re not &lt;code&gt;deriving&lt;/code&gt; 90%+ of your instances, you should start. Good places to look for opportunities involve the wrappers in &lt;code&gt;Data.Monoid&lt;/code&gt; and &lt;code&gt;GHC.Generics&lt;/code&gt;.&lt;a class=&quot;footnote-back&quot; href=&quot;https://reasonablypolymorphic.com#fnref2&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&quot;fn3&quot;&gt;&lt;p&gt;In glorious SVG. Sorry for the shitty image quality in previous posts!&lt;a class=&quot;footnote-back&quot; href=&quot;https://reasonablypolymorphic.com#fnref3&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Sun, 08 Nov 2026 10:55:00 +0000</pubDate>
</item>
<item>
	<title>Sandy Maguire: From Instantaneous Events to Musical Spans</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/musical-spans/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/musical-spans/index.html</link>
	<description>&lt;p&gt;We’ve been thinking a lot about musical structures over these last few posts. Today we’re going to look into some rhythmic concerns, and in particular, figure out how to turn a discrete signal of instantaneous events into something that &lt;em&gt;actually looks like music.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Recall that we’ve been working with a &lt;code&gt;Signal a&lt;/code&gt; type, which is a continuous function of &lt;code&gt;Time -&amp;gt; a&lt;/code&gt;, packaged along with a set of “interesting times” that we might be interested in looking at. We smuggle &lt;em&gt;discrete&lt;/em&gt; events into these interesting times. But what makes a time interesting?&lt;/p&gt;
&lt;p&gt;One immediate answer, musically, is “when something happens.” That something could be a point in the meter, a chord change, the beginning of a note, the end of a note, a modulation, the return of a motif, every time an A is played, etc. Essentially, whenever you might think a time is interesting :)&lt;/p&gt;
&lt;p&gt;Of these, we’re primarily going to focus on the beginning and end of notes today. Since once we’ve sorted that bit out, we have the better part of a musical MVP. It’s not going to be anywhere close to the &lt;a href=&quot;https://reasonablypolymorphic.com/blog/music-overview/&quot;&gt;vision&lt;/a&gt;, but it’s a necessary step along the path.&lt;/p&gt;
&lt;p&gt;So what we’ve built so far is an &lt;code&gt;Event&lt;/code&gt; type, which demarks instantaneous and momentary things. We have a &lt;code&gt;Signal&lt;/code&gt; type which models things-over-time. It seems likely that in order to play notes we’re going to want a &lt;code&gt;Signal&lt;/code&gt; of &lt;code&gt;Event&lt;/code&gt;s of something. Let’s say &lt;a href=&quot;https://en.wikipedia.org/wiki/MIDI&quot;&gt;MIDI notes&lt;/a&gt; for now, since it’s an obvious sort of “computer music” interface.&lt;/p&gt;
&lt;p&gt;The problem of course is that &lt;code&gt;Event&lt;/code&gt;s are instantaneous, but audible things are not; they necessarily have a duration. I played around with data structures in the style of &lt;a href=&quot;https://en.wikipedia.org/wiki/Interval_tree&quot;&gt;interval trees&lt;/a&gt; for a &lt;a href=&quot;https://reasonablypolymorphic.com/blog/more-algebraic-music/&quot;&gt;long time&lt;/a&gt; before coming to the conclusion that that way lies folly.&lt;/p&gt;
&lt;p&gt;Instead what seems to be the play is to explicitly model &lt;code&gt;Span&lt;/code&gt;s:&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;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Span&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Span&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;ot&quot;&gt; duration ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&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;ot&quot;&gt; getSpan ::&lt;/span&gt; a&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&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;deriving&lt;/span&gt; stock (&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;Functor&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Foldable&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Traversable&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Generic&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and then just generate them in our &lt;code&gt;Signal&lt;/code&gt;. So rather than talk about &lt;code&gt;Event MIDINote&lt;/code&gt;, we instead use &lt;code&gt;Event (Span MIDINote)&lt;/code&gt;. This is very much in the style of “regular old music scores” where there is &lt;em&gt;explicit syntax&lt;/em&gt; for encoding the span of a note:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Rhythmic symbols&quot; src=&quot;https://reasonablypolymorphic.com/images/music3/out1.cropped.png&quot; /&gt;&lt;figcaption&gt;Rhythmic symbols&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;If you’re not familiar with music notation, the top line contains (after the funny “c” thing) the symbols for sounded notes. The first symbol takes up one unit of time, the next takes half that, and so on and so forth. The bottom line, in corresponding columns, are the same durations, except for silences rather than for sounded notes.&lt;/p&gt;
&lt;p&gt;Anyway, the point here is namely that musicians are very much in the habit of thinking about “how long does this note sound for?” So attaching the &lt;code&gt;Span&lt;/code&gt; to the note we want to play isn’t a very far-fetched idea at all.&lt;/p&gt;
&lt;p&gt;That being said…&lt;/p&gt;
&lt;p&gt;It turns out to be a &lt;em&gt;drag&lt;/em&gt; to have to always be sticking durations on things. I suspect the pain is already internalized in some sort of Stockholm Syndrome, because every written notation and every computer program for music requires you to write down the damn durations.&lt;/p&gt;
&lt;p&gt;As it happens, writing down the durations in advance is not very compositional. For example, if we stretch the time domain of our &lt;code&gt;Signal&lt;/code&gt; (which is very reasonable and possible, since it’s just a function &lt;code&gt;Time -&amp;gt; a&lt;/code&gt;), we’d expect the note durations to stretch too! But they can’t, since we’d have the type:&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;stretch ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Rational&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which you’ll notice is polymorphic in &lt;code&gt;a&lt;/code&gt;. Of course, we could do some ad-hoc bullshit where we stick in a typeclass constraint for “stretchable things” but this is the way of madness.&lt;/p&gt;
&lt;p&gt;What if, instead, we were to &lt;em&gt;derive&lt;/em&gt; the durations? After all, we already know &lt;em&gt;when&lt;/em&gt; everything &lt;em&gt;is&lt;/em&gt;, temporally-speaking. The first type that comes to mind for something like this would be:&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;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a)  (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Span&lt;/span&gt; a))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;that is, a function over &lt;code&gt;Signal&lt;/code&gt;s, which takes an input stream of events and outputs a stream of events with a duration attached to them. What duration? Presumably until the next event! While this is a perfectly fine type (and isn’t very hard to implement), it has the obvious downside that it can’t represent silence. If a sound spans until the next sound, then there’s nowhere to stick any silence!&lt;/p&gt;
&lt;p&gt;What I ended up with was to take &lt;em&gt;two&lt;/em&gt; input event streams:&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;spanning ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; (), &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Span&lt;/span&gt; a))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where the &lt;code&gt;Event ()&lt;/code&gt; stream indicates &lt;em&gt;changes&lt;/em&gt; and the &lt;code&gt;Event a&lt;/code&gt; stream indicates &lt;em&gt;beginnings of notes.&lt;/em&gt; Thus a note spans until the next &lt;code&gt;Event a&lt;/code&gt; or &lt;code&gt;Event ()&lt;/code&gt; — whichever comes first.&lt;/p&gt;
&lt;p&gt;The implementation is not very tricky. We use our primitive&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;ev2ev ::&lt;/span&gt; ([(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, a)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, b)]) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; b)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and zip together the events with the tail of the events, taking the difference in their times. All that’s left is to combine the event streams, which is exactly what the &lt;code&gt;Alternative&lt;/code&gt; instance on &lt;code&gt;Event&lt;/code&gt; does:&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;spanning ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; (), &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Span&lt;/span&gt; a))&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;spanning &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; proc (end, a) &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;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;let&lt;/span&gt; merged &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;&amp;lt;$&amp;gt;&lt;/span&gt; a &lt;span class=&quot;op&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;&amp;lt;$&lt;/span&gt; end&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;  ev2ev&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;    (\es &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; catMaybes&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;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;zipWith&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;          (\(t1, ema) (t2, _) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; ema &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&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;Just&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; (t1, &lt;span class=&quot;dt&quot;&gt;Span&lt;/span&gt; (t2 &lt;span class=&quot;op&quot;&gt;-&lt;/span&gt; t1) a)&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;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;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&gt;
&lt;span id=&quot;cb6-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          es&lt;/span&gt;
&lt;span id=&quot;cb6-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-12&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;drop&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; es&lt;/span&gt;
&lt;span id=&quot;cb6-13&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ) &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; merged&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I don’t know about you, but I think this is &lt;a href=&quot;https://www.youtube.com/watch?v=S1gHv6jdE3E&quot;&gt;pretty cool&lt;/a&gt;. For two different reasons. The first is that we’ve accidentally just derived the notion of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Part_(music)&quot;&gt;musical voice&lt;/a&gt;, which is to say a logical grouping of musical events into something that could be sung by a person, or played on an instrument. Voices and (most) instruments only make one note at a time, which is exactly the postcondition of &lt;code&gt;spanning&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The other cool thing here is that now we can change note lengths in the same way we can change chords and modulations — namely by doing transformations in &lt;code&gt;SF&lt;/code&gt;. You can imagine a &lt;code&gt;Signal Tension&lt;/code&gt; which describes how much tension the composer wants you to feel through the journey of the song. By deriving an event stream out of the modulating &lt;code&gt;Tension&lt;/code&gt;, where events happen frequently in high tension situations, and less often under low tension, we can automatically &lt;em&gt;drive note durations&lt;/em&gt; from our &lt;code&gt;Tension&lt;/code&gt; parameter! I haven’t used a lot of DAWs, but my understanding is (and the clankers agree) that almost(?) no DAWs support duration as an automation. So we might be in terra nova over here!&lt;/p&gt;
&lt;p&gt;Perhaps you’re starting to get a taste that maybe all of this &lt;a href=&quot;https://reasonablypolymorphic.com/blog/music-overview/&quot;&gt;“solving music”&lt;/a&gt; isn’t such a pipe dream after all. There’s clearly still a huge distance to travel before we’re anywhere close to anything useful, but the primitives do seem to be pulling their weight.&lt;/p&gt;
&lt;p&gt;Of course, nobody wants to call &lt;code&gt;spanning&lt;/code&gt; by hand, so next time we’ll see about finding nicer ways of generating horizontal structures.&lt;/p&gt;</description>
	<pubDate>Thu, 05 Nov 2026 08:59: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>Sandy Maguire: Musical Signal Functions</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/music-arrows/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/music-arrows/index.html</link>
	<description>&lt;p&gt;We’ve been chatting for a few days about my vision for a constructive model of music. So far it’s been rather loosey-goosey; lots of vibes, some semantics, but nothing in the way of implementation. Today let’s get our boots muddy and do enough implementation that we have something concrete to talk about.&lt;/p&gt;
&lt;h2 id=&quot;events-and-signals&quot;&gt;Events and Signals&lt;/h2&gt;
&lt;p&gt;In particular, in &lt;a href=&quot;https://reasonablypolymorphic.com/blog/music-signals/&quot;&gt;Musical Signals&lt;/a&gt; we discussed a &lt;code&gt;Signal&lt;/code&gt; type that is a continuous function of time, equipped with a set of interesting times that we actually care about. In addition, we proposed an &lt;code&gt;Event&lt;/code&gt; type that we will use in conjunction with &lt;code&gt;Signal&lt;/code&gt;s to signify sparse, discrete functions over time.&lt;/p&gt;
&lt;p&gt;Let’s begin with &lt;code&gt;Event&lt;/code&gt;. It’s just a little newtype wrapper around &lt;code&gt;Maybe&lt;/code&gt;:&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;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MkEvent&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;ot&quot;&gt; eventToMaybe ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; a&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;deriving&lt;/span&gt; stock (&lt;span class=&quot;dt&quot;&gt;Foldable&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Traversable&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Generic&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;kw&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;newtype&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;dt&quot;&gt;Functor&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Monad&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;Alternative&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;)&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;&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;ot&quot;&gt;{-# COMPLETE Event, NoEvent #-}&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;&lt;span class=&quot;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Event&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;Event&lt;/span&gt; a&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;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MkEvent&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Just&lt;/span&gt; a)&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;&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;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NoEvent&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt; ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a&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;kw&quot;&gt;pattern&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;NoEvent&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;MkEvent&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-14&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-15&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;event ::&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;Event&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; b&lt;/span&gt;
&lt;span id=&quot;cb1-16&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb1-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;event b f &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;maybe&lt;/span&gt; b f &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; eventToMaybe&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As discussed in the &lt;a href=&quot;https://reasonablypolymorphic.com/blog/music-signals/#fn1&quot;&gt;footnote&lt;/a&gt; on that page, perhaps we want to not export the &lt;code&gt;Event&lt;/code&gt; pattern, the &lt;code&gt;MkEvent&lt;/code&gt; constructor, or the &lt;code&gt;Applicative&lt;/code&gt;/&lt;code&gt;Monad&lt;/code&gt; instances. That seems likely like the way to go, but I haven’t yet put much thought into whether it solves all the problems. So let’s export them for now (because they are genuinely useful!) and circle back at some point in the future.&lt;/p&gt;
&lt;p&gt;My idea for implementing &lt;code&gt;Signal&lt;/code&gt; is to keep around a bunch of continuous segments of function, each of which might have a time-of-interest at the exact instant that the segment starts. A &lt;code&gt;Segment&lt;/code&gt; is defined thusly:&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;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Segment&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;ot&quot;&gt; segVal ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Maybe&lt;/span&gt; a&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; segment ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a&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;co&quot;&gt;-- ^ Must be meaningfully defined for the *entirety* of time!&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;  }&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; stock &lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The obvious &lt;code&gt;Applicative Segment&lt;/code&gt; instance has the wrong semantics here, so we don’t provide it. The problem being that &lt;code&gt;segVal&lt;/code&gt; corresponds to a value of the function at a time of interest. Which is to say, that if &lt;code&gt;segVal&lt;/code&gt; is ever &lt;code&gt;Just&lt;/code&gt;, it must &lt;em&gt;remain&lt;/em&gt; &lt;code&gt;Just&lt;/code&gt; as we &lt;code&gt;ap&lt;/code&gt; our way through it! But &lt;code&gt;Applicative&lt;/code&gt; isn’t powerful enough to do that, because filling in the missing data would require us to know what &lt;code&gt;Time&lt;/code&gt; we should be sampling the function at.&lt;/p&gt;
&lt;p&gt;We address the problem with a new &lt;code&gt;combine&lt;/code&gt; 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;co&quot;&gt;-- | Like 'liftA2' for 'Segment', when we have a concrete time to sample at.&lt;/span&gt;&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;combine ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&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; 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; &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; c&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;combine t f (&lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; a fa) (&lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; b fb) &lt;span class=&quot;ot&quot;&gt;=&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;dt&quot;&gt;Segment&lt;/span&gt;&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;    ( asum&lt;/span&gt;
&lt;span id=&quot;cb3-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;       [ liftA2 f a b&lt;/span&gt;
&lt;span id=&quot;cb3-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;       , &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; (f (fa t)) b&lt;/span&gt;
&lt;span id=&quot;cb3-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb3-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;       , &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;flip&lt;/span&gt; f (fb t)) a&lt;/span&gt;
&lt;span id=&quot;cb3-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#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://reasonablypolymorphic.com#cb3-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (liftA2 f fa fb)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;asum&lt;/code&gt; here attempts to fill in the interesting value by combining the &lt;code&gt;Maybe&lt;/code&gt;s. If that fails, it then tries to fill it in by fmapping over either individual interesting value.&lt;/p&gt;
&lt;p&gt;Collecting our &lt;code&gt;Segment&lt;/code&gt;s into a &lt;code&gt;Signal&lt;/code&gt; requires a few helper functions: &lt;code&gt;merge&lt;/code&gt; takes two sorted lists of pairs, and computes a sorted list of &lt;a href=&quot;https://hackage.haskell.org/package/strict-0.5.1/docs/Data-Strict-These.html#t:These&quot;&gt;&lt;code&gt;These&lt;/code&gt;&lt;/a&gt;. The idea being that if both lists have a key at the same index, we keep both of them around. If only one has a value, we tag it with which list it came from:&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;co&quot;&gt;-- | Specialized 'Data.Align.align' for sorted pairs.&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;ot&quot;&gt;merge ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; [(a, b)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(a, c)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(a, &lt;span class=&quot;dt&quot;&gt;These&lt;/span&gt; b c)]&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;merge [] ys       &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;fmap&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;That&lt;/span&gt;) ys&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;merge (x &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs) [] &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;fmap&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;This&lt;/span&gt;) (x &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs)&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;merge xx&lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;((tx, x) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; xs) yy&lt;span class=&quot;op&quot;&gt;@&lt;/span&gt;((ty, y) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; ys) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&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;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;compare&lt;/span&gt; tx ty &lt;span class=&quot;kw&quot;&gt;of&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;    &lt;span class=&quot;dt&quot;&gt;LT&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (tx, &lt;span class=&quot;dt&quot;&gt;This&lt;/span&gt; x) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; merge xs yy&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;    &lt;span class=&quot;dt&quot;&gt;GT&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (ty, &lt;span class=&quot;dt&quot;&gt;That&lt;/span&gt; y) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; merge xx ys&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;    &lt;span class=&quot;dt&quot;&gt;EQ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (tx, &lt;span class=&quot;dt&quot;&gt;These&lt;/span&gt; x y) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; merge xs ys&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We use &lt;code&gt;merge&lt;/code&gt; to define &lt;code&gt;joining&lt;/code&gt;, which does a &lt;code&gt;scanl&lt;/code&gt; of two sorted lists of &lt;code&gt;Segment&lt;/code&gt;s. The idea here is that the two lists are effectively interval trees, and the intervals are not guaranteed to line up. So we use a &lt;code&gt;scanl&lt;/code&gt; hold onto the old value of each until it gets replaced with a new value.&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;joining&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;&lt;span class=&quot;ot&quot;&gt;    ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Ord&lt;/span&gt; a&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;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; b&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;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; c&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;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(a, &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; b)]&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;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(a, &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; c)]&lt;/span&gt;
&lt;span id=&quot;cb5-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(a, (&lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; b, &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; c))]&lt;/span&gt;
&lt;span id=&quot;cb5-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;joining a0 b0 as bs&lt;/span&gt;
&lt;span id=&quot;cb5-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-9&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;drop&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-10&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;scanl&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    (\(_, (a, b)) (t, th) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; th &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-13&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;This&lt;/span&gt; a'     &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (t, (a', open b))&lt;/span&gt;
&lt;span id=&quot;cb5-14&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;That&lt;/span&gt; b'     &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (t, (open a, b'))&lt;/span&gt;
&lt;span id=&quot;cb5-15&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dt&quot;&gt;These&lt;/span&gt; a' b' &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (t, (a', b'))&lt;/span&gt;
&lt;span id=&quot;cb5-16&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    )&lt;/span&gt;
&lt;span id=&quot;cb5-17&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    ( &lt;span class=&quot;fu&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;unused initial time argument&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-18&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    , (a0, b0)&lt;/span&gt;
&lt;span id=&quot;cb5-19&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#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://reasonablypolymorphic.com#cb5-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; merge as bs&lt;/span&gt;
&lt;span id=&quot;cb5-21&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-22&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;open ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb5-23&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb5-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;open s &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; s { segVal &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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With all that ceremony out of the way, we’re ready to define &lt;code&gt;Signal&lt;/code&gt;s, which contain an original &lt;code&gt;Time -&amp;gt; a&lt;/code&gt; function with no interesting timepoints, and then a sorted list of new &lt;code&gt;Segment&lt;/code&gt;s.&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;co&quot;&gt;-- | A piecewise function, built out of 'Segment' intervals.&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;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a&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;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt;&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;dt&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a)&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;co&quot;&gt;-- ^ The function before we have switched to any 'Segment'.&lt;/span&gt;&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;dt&quot;&gt;Time&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; a)]&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;co&quot;&gt;-- ^ Sorted by time.&lt;/span&gt;&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;kw&quot;&gt;deriving&lt;/span&gt; stock &lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Of course, this is not the only possible implementation of &lt;code&gt;Signal&lt;/code&gt;, nor am I claiming it’s the best one. It’s just one that I came up with that satisfied all the laws and wasn’t slower than molasses. I’ve also got a relatively-fresh new baby, so a lot of this got written on very little sleep. Anyway…&lt;/p&gt;
&lt;p&gt;Despite &lt;code&gt;Segment&lt;/code&gt; not admitting an &lt;code&gt;Applicative&lt;/code&gt; instance, &lt;code&gt;Signal&lt;/code&gt; does, because it keeps track of where each &lt;code&gt;Segment&lt;/code&gt; begins.&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;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&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;  &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; a) &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;  liftA2 f (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a as) (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; b bs)&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;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (liftA2 f a b)&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;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;uncurry&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (t,) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;uncurry&lt;/span&gt; (combine t f))&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;$&lt;/span&gt; joining (&lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;Segment&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; b) as bs&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can show that &lt;code&gt;Signal&lt;/code&gt; preserves the &lt;a href=&quot;https://reasonablypolymorphic.com/blog/music-signals/&quot;&gt;semantics we gave&lt;/a&gt; by giving projections out of it into the semantic domain:&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;sample ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a)&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;sample (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; fa []) t &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; fa t&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;sample (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; fa ((t0, sa) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; as)) t &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&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;kw&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;compare&lt;/span&gt; t t0 &lt;span class=&quot;kw&quot;&gt;of&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;dt&quot;&gt;LT&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; fa t&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;dt&quot;&gt;EQ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&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;kw&quot;&gt;case&lt;/span&gt; sa &lt;span class=&quot;kw&quot;&gt;of&lt;/span&gt;&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;dt&quot;&gt;Segment&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Nothing&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; f t&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;dt&quot;&gt;Segment&lt;/span&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;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;dt&quot;&gt;GT&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; sample (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (segment sa) as) t&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This function is the continuous-&lt;code&gt;μ&lt;/code&gt; given by our semantics. The discrete-&lt;code&gt;μ&lt;/code&gt; is called &lt;code&gt;values&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;values ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, a)]&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;values (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; _ as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; mapMaybe (&lt;span class=&quot;fu&quot;&gt;traverse&lt;/span&gt; segVal) as&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which returns a sorted list of interesting times (and their associated values.) In the &lt;code&gt;Signal (Event a)&lt;/code&gt; case, we have a more useful function, which gets only those values which &lt;em&gt;actually contain a discrete value&lt;/em&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;https://reasonablypolymorphic.com#cb10-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;events ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Time&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;events &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; mapMaybe (&lt;span class=&quot;fu&quot;&gt;traverse&lt;/span&gt; eventToMaybe) &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; values&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id=&quot;arrows&quot;&gt;Arrows&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Signal&lt;/code&gt;s are nice representations of interesting-things-over-time. But the mental model of &lt;a href=&quot;https://reasonablypolymorphic.com/blog/musical-vision/&quot;&gt;abstract modular synthesizers&lt;/a&gt; is not about interesting-things-over-time. It’s about ways of &lt;em&gt;transforming and combining&lt;/em&gt; interesting-things-over-time.&lt;/p&gt;
&lt;p&gt;Haskell has some deep-cut notation for this, namely the &lt;code&gt;-XArrows&lt;/code&gt; extension and its related &lt;code&gt;proc&lt;/code&gt; notation. Long-time readers of the blog will know that we’re &lt;a href=&quot;https://reasonablypolymorphic.com/blog/arrowized-frp/&quot;&gt;no&lt;/a&gt; &lt;a href=&quot;https://reasonablypolymorphic.com/blog/arrows-to-arrows/&quot;&gt;strangers&lt;/a&gt; to &lt;a href=&quot;https://reasonablypolymorphic.com/blog/reviewing-again/&quot;&gt;arrows&lt;/a&gt; here!&lt;/p&gt;
&lt;p&gt;But as a &lt;em&gt;very&lt;/em&gt; quick recap, given instances of &lt;code&gt;Category&lt;/code&gt; and &lt;code&gt;Arrow&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;kw&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Category&lt;/span&gt; a &lt;span class=&quot;kw&quot;&gt;where&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;ot&quot;&gt;  id  ::&lt;/span&gt; a x x&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;ot&quot;&gt;  (.) ::&lt;/span&gt; a y z &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a x y &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a x z&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&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;kw&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Category&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Arrow&lt;/span&gt; a &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb11-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  arr   ::&lt;/span&gt; (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 b c&lt;/span&gt;
&lt;span id=&quot;cb11-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb11-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;  (***) ::&lt;/span&gt; a b c &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a b' c' &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a (b, b') (c, c')&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;we gain access to &lt;code&gt;proc&lt;/code&gt; notation, which is very convenient for composing arrow values:&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;proc input &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt;&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; a &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; foo &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; input&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; b &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; bar &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; (input, a)&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;kw&quot;&gt;let&lt;/span&gt; output &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; qux a b&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; returnA &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; output&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This notation is syntactic sugar for the egregious:&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;  arr qux&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;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;***&lt;/span&gt; bar&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;op&quot;&gt;.&lt;/span&gt; arr (\i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;snd&lt;/span&gt; i, i))&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;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;***&lt;/span&gt; foo&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;&lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; arr (\i &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; (i, i))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which I think we can all agree is &lt;em&gt;slightly&lt;/em&gt; less readable than the &lt;code&gt;proc&lt;/code&gt; notation.&lt;/p&gt;
&lt;p&gt;The rough vibes here is that &lt;code&gt;Category a&lt;/code&gt; abstracts the notion of what a function is. Rather than using Haskell &lt;code&gt;-&amp;gt;&lt;/code&gt; arrows, we can instead use &lt;code&gt;a&lt;/code&gt;-arrows. The &lt;code&gt;Category&lt;/code&gt; methods &lt;code&gt;(.)&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt; do exactly what you’d expect — composition and identity of &lt;code&gt;a&lt;/code&gt;-arrows.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Arrow a&lt;/code&gt; class extends this with some way of turning arbitrary Haskell arrows into &lt;code&gt;a&lt;/code&gt;-arrows, as well as a way to run two different &lt;code&gt;a&lt;/code&gt;-arrows on the &lt;code&gt;fst&lt;/code&gt; and &lt;code&gt;snd&lt;/code&gt; of a (Haskell) product type. The exact semantics of what that looks like depends on your &lt;code&gt;a&lt;/code&gt;-arrow type (much like the exact semantics of &lt;code&gt;do&lt;/code&gt; notation depend on your choice of a monad!)&lt;/p&gt;
&lt;p&gt;Anyway, to pull back to musical transformations. If we were to find some sort of music-aware &lt;code&gt;Arrow&lt;/code&gt; instance, &lt;code&gt;proc&lt;/code&gt;-notation is exactly what we’d want in order to “run patches” in our abstract modular synthesizer. That is to say, it provides the plumbing for moving music in and out of transformations. As a teaser for what’s to come, here’s a little example from my sandbox:&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;co&quot;&gt;-- A horizontal pattern describing an unfolding dyadic voice-leading that&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;co&quot;&gt;-- descends by fifth, before swapping the voices by moving away from one&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;co&quot;&gt;-- another.&lt;/span&gt;&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;divergence &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; replicateH &lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;foldMap&lt;/span&gt; dnote&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;  [ voiceLead &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;3&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;  , expand &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;&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&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&gt;
&lt;span id=&quot;cb14-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;song &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; horizontal &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; resize &lt;span class=&quot;fl&quot;&gt;0.25&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; proc 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;cb14-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;co&quot;&gt;-- Compute an abstract voice leading over time&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  vl &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; liftH msumE &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; divergence &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; i&lt;/span&gt;
&lt;span id=&quot;cb14-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-13&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;co&quot;&gt;-- Apply that voice leading to the first two notes of a triad&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb14-14&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ns &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; replicateH &lt;span class=&quot;dv&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; with &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; applyVoiceLeading vl &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; chord triad&lt;/span&gt;
&lt;span id=&quot;cb14-15&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb14-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  returnA &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; ns&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which describes this this nice little dyad expansion:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Dyad Expansion&quot; src=&quot;https://reasonablypolymorphic.com/images/music2/out1.cropped.png&quot; /&gt;&lt;figcaption&gt;Dyad Expansion&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;If we replace the last line with:&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;co&quot;&gt;-- Convert the &quot;polyphony&quot; into &quot;counterpoint&quot;&lt;/span&gt;&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;kw&quot;&gt;let&lt;/span&gt; (lo &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; hi &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 class=&quot;ot&quot;&gt;=&lt;/span&gt; toV ns&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&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;co&quot;&gt;-- Clamp the lo voice so it stays within its register&lt;/span&gt;&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;  lo' &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; liftH &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; clampOctave (&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;fl&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; lo&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;&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;  &lt;span class=&quot;co&quot;&gt;-- Convert the &quot;counterpoint&quot; back into &quot;polyphony&quot;&lt;/span&gt;&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;  returnA &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; fromV &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; lo' &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; hi &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;we get this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Clamped Dyad Expansion&quot; src=&quot;https://reasonablypolymorphic.com/images/music2/out2.cropped.png&quot; /&gt;&lt;figcaption&gt;Clamped Dyad Expansion&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;and of course we can also clamp the upper voice:&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;co&quot;&gt;-- Clamp the voices so they stay within their registers&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;  lo' &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; liftH &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; clampOctave (&lt;span class=&quot;op&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;9&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;fl&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; lo&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;  hi' &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; liftH &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; clampOctave (&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;dv&quot;&gt;7&lt;/span&gt;) &lt;span class=&quot;fl&quot;&gt;0.5&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; hi&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&gt;
&lt;span id=&quot;cb16-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;co&quot;&gt;-- Convert the &quot;counterpoint&quot; back into &quot;polyphony&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb16-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb16-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  returnA &lt;span class=&quot;op&quot;&gt;-&amp;lt;&lt;/span&gt; fromV &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; lo' &lt;span class=&quot;op&quot;&gt;:&amp;gt;&lt;/span&gt; hi' &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;and we get this:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Doubly-Clamped Dyad Expansion&quot; src=&quot;https://reasonablypolymorphic.com/images/music2/out3.cropped.png&quot; /&gt;&lt;figcaption&gt;Doubly-Clamped Dyad Expansion&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Now I’ll be the first to admit that this is a completely unusable DSL. But it does actually work! And if you squint, you can sort of see the modular synthesizer idea at play. Inside the proc notation, we are sampling all of our &lt;code&gt;Signal&lt;/code&gt;s simultaneously, but routing the output of (eg) the voice leading into the input of &lt;code&gt;replicateH $ with id&lt;/code&gt;, which is what builds out the rhythmic pattern. Then we take the output of that, separate it into &lt;code&gt;Signal&lt;/code&gt;s of its constituent voices, and feed those into &lt;code&gt;clampOctave&lt;/code&gt;, which moves a &lt;code&gt;Signal&lt;/code&gt;’s notes into the given range of scale degrees.&lt;/p&gt;
&lt;p&gt;Something cool but not pictured here, &lt;code&gt;clampOctave&lt;/code&gt; takes a second argument (the &lt;code&gt;0.5&lt;/code&gt;) which tells it to look &lt;em&gt;into the future&lt;/em&gt; and not perform any displacement if it would need to move again within that 0.5 of a measure. This is some sort of “retro-hysteresis” where signals can depend on their future values! Which sounds crazy when you’re thinking about wiring up synthesizers, but much less crazy when you’re sitting down writing out a score. Of course the notes in your score can depend on notes that happen later on in the song! One of the many benefits of &lt;em&gt;explicitly modeling time&lt;/em&gt; in &lt;code&gt;Signal&lt;/code&gt;s.&lt;/p&gt;
&lt;h2 id=&quot;signal-functions&quot;&gt;Signal Functions&lt;/h2&gt;
&lt;p&gt;Hopefully you’re excited to see how we go about doing any of this acausal wizardry stuff. But it all starts from getting &lt;code&gt;proc&lt;/code&gt; notation working. Which, recall, means we need a type to give an &lt;code&gt;Arrow&lt;/code&gt; instance to.&lt;/p&gt;
&lt;p&gt;Behold, the signal function (&lt;code&gt;SF&lt;/code&gt;):&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;newtype&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; a b &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt;&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;  {&lt;span class=&quot;ot&quot;&gt; runSF ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; b&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&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;  &lt;span class=&quot;kw&quot;&gt;deriving&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt;, &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt;) via &lt;span class=&quot;dt&quot;&gt;WrappedArrow&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; a&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;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;Ap&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; a) b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which you will note is &lt;em&gt;literally a function between &lt;code&gt;Signal&lt;/code&gt;s.&lt;/em&gt; Its &lt;code&gt;Category&lt;/code&gt; instance comes immediately from Haskell functions’ instance:&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;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Category&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; &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;fu&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;id&lt;/span&gt;&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;SF&lt;/span&gt; g &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; f &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (g &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; f)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;Arrow&lt;/code&gt; instance is similarly straightforward — separate out the product, run the functions on each half, then use the applicative instance to combine them back into a single &lt;code&gt;Signal&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;https://reasonablypolymorphic.com#cb19-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;Arrow&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&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;  arr &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt;&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;  &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; f &lt;span class=&quot;op&quot;&gt;***&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; g &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;$&lt;/span&gt; \sg &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://reasonablypolymorphic.com#cb19-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    liftA2 (,) (f &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;fst&lt;/span&gt; sg) (g &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;snd&lt;/span&gt; sg)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s really all there is to it! We can give some means of introducing &lt;code&gt;SF&lt;/code&gt;s:&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;constS ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; x 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;constS &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; &lt;span class=&quot;op&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;const&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;as well as a few primitives whose definition we don’t have time to get into:&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;co&quot;&gt;-- | Create a single occurrence of an Event at the given time.&lt;/span&gt;&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;at ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&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;SF&lt;/span&gt; x (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; 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&gt;
&lt;span id=&quot;cb21-4&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Convert a discrete stream into a continuous one, by holding the value of&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-5&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- the last event seen.&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-6&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;hold ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) a&lt;/span&gt;
&lt;span id=&quot;cb21-7&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-8&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Like 'hold', but backwards in time. Hold the value of the /next/ event that&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-9&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- will occur.&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-10&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;fhold ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) a&lt;/span&gt;
&lt;span id=&quot;cb21-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Stateful folds over Events&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-13&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;scanE ::&lt;/span&gt; (s &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; (s, b)) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; s &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb21-14&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-15&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Full-scale rewrites of Event streams&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-16&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;ev2ev ::&lt;/span&gt; ([(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, a)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, b)]) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; a) (&lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; b)&lt;/span&gt;
&lt;span id=&quot;cb21-17&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-18&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- | Run the first SF until the 'Event' occurs, then switch into the&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-19&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;co&quot;&gt;-- continuation.&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-20&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb21-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;switch ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; a (b, &lt;span class=&quot;dt&quot;&gt;Event&lt;/span&gt; c) &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; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; a b) &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;SF&lt;/span&gt; a b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(for completeness’ sake, there ought to be time-reverse versions of &lt;code&gt;scanE&lt;/code&gt; and &lt;code&gt;switch&lt;/code&gt; too!)&lt;/p&gt;
&lt;p&gt;This seems like a good place to leave it for now. In the next post we’ll discuss how to turn all of this abstract machinery into something that actually resembles music.&lt;/p&gt;</description>
	<pubDate>Wed, 04 Nov 2026 12:42:00 +0000</pubDate>
</item>
<item>
	<title>Sandy Maguire: What Composing Music Could Look Like</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/musical-vision/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/musical-vision/index.html</link>
	<description>&lt;p&gt;In the last post, we discussed the &lt;a href=&quot;https://reasonablypolymorphic.com/blog/music-signals/&quot;&gt;algebraic structure of horizontal structures&lt;/a&gt;. But I realized I haven’t actually communicated the argument behind &lt;em&gt;why we might care&lt;/em&gt; about that algebraic structure. So rather than diving directly into the technical stuff I had planned for today, let’s hash that out first.&lt;/p&gt;
&lt;p&gt;If you recall, where we ended up was an abstract type &lt;code&gt;Signal a&lt;/code&gt; which I claim is a precursor to horizontal musical structures, with the following common algebras:&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;Signal&lt;/span&gt; a&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;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&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;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&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;&lt;span class=&quot;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Foldable&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&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;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Semigroup&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Semigroup&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a)&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;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Semigroup&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Monoid&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It’s worth considering what each of these ideas means, musically. And once we’ve sorted that out, it’s worth thinking about what other musical structure we might want on top.&lt;/p&gt;
&lt;p&gt;We can think about a &lt;code&gt;Signal a&lt;/code&gt; as being a continuous function of &lt;code&gt;a&lt;/code&gt;s over time, combined with a set of times that we’re &lt;em&gt;actually interested in.&lt;/em&gt; So what exactly does that mean? The continuous function part is just there to fill in the details when we change our minds about what times we want to look at.&lt;/p&gt;
&lt;p&gt;With our &lt;code&gt;Functor&lt;/code&gt; instance, we can do per-note (or per-whatever is held in the horizontal structure) transformations. For example, &lt;code&gt;fmap (+ 1)&lt;/code&gt; transforms&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;C-D-E-F&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out1.cropped.png&quot; /&gt;&lt;figcaption&gt;C-D-E-F&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;into&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;D-E-F-G&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out2.cropped.png&quot; /&gt;&lt;figcaption&gt;D-E-F-G&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;But it also does more interesting things; &lt;code&gt;fmap (!! 1)&lt;/code&gt; (where &lt;code&gt;!! n&lt;/code&gt; selects voice &lt;code&gt;n&lt;/code&gt;) transforms&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Inversions of C&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out3.cropped.png&quot; /&gt;&lt;figcaption&gt;Inversions of C&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;into&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;E-G-C-E&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out4.cropped.png&quot; /&gt;&lt;figcaption&gt;E-G-C-E&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Furthermore, nobody says the &lt;code&gt;a&lt;/code&gt; in &lt;code&gt;Signal a&lt;/code&gt; need be something that is musically meaningful. A &lt;code&gt;Signal&lt;/code&gt; is just a function from time to something; so we could do something crazy like building a &lt;code&gt;Signal DayName&lt;/code&gt; that is a week-long signal moving from &lt;code&gt;Monday&lt;/code&gt; to &lt;code&gt;Sunday&lt;/code&gt; at a rate of one a day. We could then use the &lt;code&gt;Functor&lt;/code&gt; instance to transform these labels into something more musically meaningful — perhaps into motifs or numbers of repeats, or something like that.&lt;/p&gt;
&lt;p&gt;Our &lt;code&gt;Semigroup&lt;/code&gt; instance gives us a &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; operation, which combines things that happen at the same time. What it means to “combine things” depends on the things being combined. But as a little vibe check, we can use &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; to smush together two little motifs:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;C-x-E-x&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out5.cropped.png&quot; /&gt;&lt;figcaption&gt;C-x-E-x&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;figure&gt;
&lt;img alt=&quot;C-F-G-x&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out6.cropped.png&quot; /&gt;&lt;figcaption&gt;C-F-G-x&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;into&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;C-F-EG-x&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out7.cropped.png&quot; /&gt;&lt;figcaption&gt;C-F-EG-x&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;This example one is very vibes, so don’t read TOO much into it. The important part is that “things that happen at the same time get smushed together.” We’ll discuss in MUCH MORE DETAIL sometime in the future what sorts of smushing might be musically meaningful.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Applicative&lt;/code&gt; structure is very similar to the &lt;code&gt;Semigroup&lt;/code&gt;, except that instead of smushing things together, we can run a function that depends on the values in both things. This one is unfortunately impossible to show in standard musical notation, and wiring up some custom visualization isn’t a good use of my nanowrimo time.&lt;/p&gt;
&lt;p&gt;But let’s use our IMAGINATION instead. Imagine a voice-leading that looks like this (realized over a C-major triad):&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Abstract voice leading&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out8.cropped.png&quot; /&gt;&lt;figcaption&gt;Abstract voice leading&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;We can use our applicative instance to retarget the voice leading over any other chord. For example, given this one:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;C-D-B&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out9.cropped.png&quot; /&gt;&lt;figcaption&gt;C-D-B&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Invoking &lt;code&gt;liftA2 applyVoiceLeading&lt;/code&gt; over these two musical pieces gives us this remarkable chord progression:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Something odd&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out10.cropped.png&quot; /&gt;&lt;figcaption&gt;Something odd&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;If you’re familiar with lots of harmony, you can see that both progressions are structurally equivalent — a &lt;a href=&quot;https://en.wikipedia.org/wiki/Neo-Riemannian_theory#Triadic_transformations_and_voice_leading&quot;&gt;neo-Riemannian P transform&lt;/a&gt;, followed by an efficient voice leading from CM to E#M, followed by the outer two voices switching pitch classes by moving in opposite directions.&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;&lt;/p&gt;
&lt;p&gt;Of course, nothing requires us to use the &lt;em&gt;same&lt;/em&gt; chord throughout. We can apply our same voice leading pattern to a series of changing chords, for example:&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Changing chords&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out11.cropped.png&quot; /&gt;&lt;figcaption&gt;Changing chords&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;which results in&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Voice leading over changing chords&quot; src=&quot;https://reasonablypolymorphic.com/images/music1/out12.cropped.png&quot; /&gt;&lt;figcaption&gt;Voice leading over changing chords&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;You’ll notice we’re getting &lt;em&gt;very interesting&lt;/em&gt; results without doing much work at all. I picked some random voice leadings and some random chords, and the computer did all of the work of figuring out what notes I’m gesturing at.&lt;/p&gt;
&lt;p&gt;Some other things you could do with this: instead of voice leadings, put scalar modulations. Or note durations! Or put voice numberings and project chords down to individual voices (which is different than the functor case, because here we can &lt;em&gt;change&lt;/em&gt; which voice gets to sound.) Better yet, do all of the above, continuously, at different time scales.&lt;/p&gt;
&lt;p&gt;What I think this ends up looking like in practice is something akin to a modular synthesizer. You can plug the outputs of different signals into the inputs of others. But unlike any modular synthesizer you’ve ever played with, here our signals are allowed to carry macroharmonies, voice leadings, modulations, tension, motifs, rhythm, registral expansion, dynamics, timbre, or even just arbitrary mathematical functions.&lt;/p&gt;
&lt;p&gt;That’s probably enough for now. In the next post we’ll actually get around to looking at the implementation of &lt;code&gt;Signal&lt;/code&gt; and then giving better composable semantics around it.&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;I extracted all the moves here from Tymoczko’s masterpiece &lt;a href=&quot;https://dmitri.mycpanel.princeton.edu/tonality-an-owners-manual.html&quot;&gt;Tonality: An Owner’s Manual&lt;/a&gt;.&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, 03 Nov 2026 15:30:00 +0000</pubDate>
</item>
<item>
	<title>Sandy Maguire: Musical Signals</title>
	<guid isPermaLink="true">https://reasonablypolymorphic.com/blog/music-signals/index.html</guid>
	<link>https://reasonablypolymorphic.com/blog/music-signals/index.html</link>
	<description>&lt;p&gt;The &lt;a href=&quot;https://reasonablypolymorphic.com/blog/music-overview/&quot;&gt;last blog&lt;/a&gt; post served as an overview of my music project — what it is, why it is, and roughly what success will look like. Today we’ll get into some of the nitty gritty details that will help put the project onto a firm footing.&lt;/p&gt;
&lt;p&gt;In order to have any chance of &lt;a href=&quot;https://www.youtube.com/watch?v=rlyqoYoUumc&quot;&gt;not confusing ourselves&lt;/a&gt;, we must first be precise about what exactly it is we’re talking about. That means we’ll have to model out some types, and, importantly &lt;em&gt;what those types mean.&lt;/em&gt; Recall that one of our &lt;a href=&quot;https://reasonablypolymorphic.com/blog/music-overview/&quot;&gt;desiderata&lt;/a&gt; is that whatever it is we’re building must be capable of expressing any music from any culture from any time period, including those times and cultures which do not yet exist.&lt;/p&gt;
&lt;p&gt;The trick then is to make a series of &lt;em&gt;extensible&lt;/em&gt; types, each one providing some aspect of functionality, and letting people compose them together for themselves. Future users can add their own types with their own functionality, and reuse as much of the system that is meaningful to reuse.&lt;/p&gt;
&lt;p&gt;By my reckoning, music is made up of some combination of the following:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;&lt;em&gt;some notion of time,&lt;/em&gt; generally considered to be rhythmic content, or “horizontal structures.”&lt;/li&gt;
&lt;li&gt;&lt;em&gt;some notion of simultaneity,&lt;/em&gt; generally considered to be chords, polyphony, or “vertical structures.”&lt;/li&gt;
&lt;li&gt;&lt;em&gt;things that can happen,&lt;/em&gt; generally considered to be musical content of some sort. Perhaps this is pitches (as in musical scores), or waveforms (as in musical playback.)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As a placeholder to get our bearings, let’s imagine the first two elements as types:&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;Horizontal&lt;/span&gt; a&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;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Vertical&lt;/span&gt; a&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and postulate both a &lt;code&gt;Pitch&lt;/code&gt; and &lt;code&gt;Waveform&lt;/code&gt; type for item three:&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;Pitch&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;type&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Waveform&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By composing these types in different ways, different ideas of music fall out. Several examples:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;&lt;code&gt;Horizontal ()&lt;/code&gt; describes purely time-based structures; eg. rhythmic ideas&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Horizontal Pitch&lt;/code&gt; is a simple tune, the sort you can whistle&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Vertical Pitch&lt;/code&gt; is a single chord.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Horizontal (Vertical Pitch)&lt;/code&gt; is what you get at a campfire from someone who only knows how to play chords on the guitar. In the jargon, this is “polyphonic” music.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Vertical (Horizontal Pitch)&lt;/code&gt; is multiple independent tunes happening simultaneously. In the jargon, this is &lt;a href=&quot;https://en.wikipedia.org/wiki/Counterpoint&quot;&gt;“contrapuntal” music&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Vertical (Horizontal Waveform)&lt;/code&gt; is what you get in a &lt;a href=&quot;https://en.wikipedia.org/wiki/Digital_audio_workstation&quot;&gt;DAW&lt;/a&gt; — independent tracks, each of which lets you move recorded samples along its own timeline.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Of course, this is all vibes right now, since we haven’t discussed what exactly any of these types &lt;em&gt;actually are.&lt;/em&gt; But this should be enough to wet your whistle; if we can deliver types that match these vibes, we clearly have an amazing starting point.&lt;/p&gt;
&lt;p&gt;As a point of note, this first step is where I went wrong in my &lt;a href=&quot;https://reasonablypolymorphic.com/blog/more-algebraic-music/&quot;&gt;last public attempt&lt;/a&gt;. There I postulated that &lt;em&gt;all&lt;/em&gt; music is what here would be described as &lt;code&gt;Vertical (Horizontal a)&lt;/code&gt;. And I gave unsatisfying answers for what &lt;code&gt;Vertical&lt;/code&gt; and &lt;code&gt;Horizontal&lt;/code&gt; are, to boot!&lt;/p&gt;
&lt;p&gt;Shame. Sorrow.&lt;/p&gt;
&lt;p&gt;Anyway. Let’s leave &lt;code&gt;Vertical&lt;/code&gt; alone for now, and focus our attention on &lt;code&gt;Horizontal&lt;/code&gt;, since it seems to be the crux of the interesting stuff here. However, rather than using up the musically-relevant name &lt;code&gt;Horizontal&lt;/code&gt;, let’s go with &lt;code&gt;Signal&lt;/code&gt;, since we’re going to be building up this idea in several pieces.&lt;/p&gt;
&lt;p&gt;By our vibes, a &lt;code&gt;Signal&lt;/code&gt; is a time-based structure. Which is to say, it ought to describe two things: 1) what happens, and 2) when it happens. That’s a pretty big constraint on the search space; the only other question is whether it ought to be continuous or discrete. In the continuous case, we have:&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;dt&quot;&gt;Signal&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt; &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;and in the discrete:&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;dt&quot;&gt;Signal&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, a)]  &lt;span class=&quot;co&quot;&gt;-- ordered by monotonically ascending Time&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where &lt;code&gt;μ&lt;/code&gt; should be read as “the meaning of.” That is to say, we’re trying to pin down the &lt;em&gt;semantics&lt;/em&gt; of &lt;code&gt;Signal&lt;/code&gt; without saying anything about &lt;em&gt;how we’re actually going to implement this thing.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This is my first point of confusion.&lt;/strong&gt; Composition (in the programming sense) is significantly better with the &lt;code&gt;Time -&amp;gt; a&lt;/code&gt; denotation, but when I &lt;em&gt;listen to music&lt;/em&gt; I “hear” it “discretely.” I think. Maybe. Like I said, I am confused.&lt;/p&gt;
&lt;p&gt;Rather than choose between these two options, I am &lt;strong&gt;cheating&lt;/strong&gt; and choosing BOTH. HA HA HA. This is intellectually dishonest, but it’s a decision that needs to be made early on, and making progress seems to be dependent on choosing &lt;em&gt;something.&lt;/em&gt; Hopefully when everything has clarified down the road, we can return to this decision and make a better choice.&lt;/p&gt;
&lt;p&gt;Sorry, let me back up. What the hell am I talking about? I have the advantage of knowing where I’m going, so I imagine it’s pretty disjointed without that. Concretely, I am torn between a few things:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;The discrete choice &lt;em&gt;vibes better&lt;/em&gt; with my classical piano training, where “music” is rendered as a score with discrete symbols on a page&lt;/li&gt;
&lt;li&gt;I have a strong bias towards being able to generate these symbols on a page with this system I’m making&lt;/li&gt;
&lt;li&gt;But the discrete choice doesn’t admit nearly as much algebraic structure. For example, it seems very desirable to have an applicative structure on &lt;code&gt;Signal&lt;/code&gt;. The usual list applicative is seems musically meaningless (taking the cartesian product of all the notes in a piece? yeah ok). What seems more likely is a pointwise “zippy” applicative. But in a discrete framing, &lt;em&gt;there isn’t enough data&lt;/em&gt; to combine two &lt;code&gt;Signal&lt;/code&gt; structures that are out of phase with one another.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So, the unsatisfying solution here is to just do both. Rather than picking between a discrete and a continuous representation, just keep both around:&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;Signal&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That is, represent the &lt;code&gt;Signal&lt;/code&gt; structure as a continuous function out of time, &lt;em&gt;but also keep track of a set of interesting times!&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We get quite a lot of nice algebraic structure on top of this thing:&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;dt&quot;&gt;Signal&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a, &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Time&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;&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;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Functor&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&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;fu&quot;&gt;fmap&lt;/span&gt; f a) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; first (&lt;span class=&quot;fu&quot;&gt;fmap&lt;/span&gt; f) (μ a)&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&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;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Applicative&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&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://reasonablypolymorphic.com#cb6-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  μ (&lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (&lt;span class=&quot;fu&quot;&gt;const&lt;/span&gt; a, &lt;span class=&quot;fu&quot;&gt;mempty&lt;/span&gt;)&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;  μ (liftA2 f a b) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (liftA2 f &lt;span class=&quot;op&quot;&gt;***&lt;/span&gt; (&lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;)) (μ a) (μ b)&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&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;kw&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Semigroup&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Semigroup&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a) &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-11&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  μ (a &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; b) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; μ a &lt;span class=&quot;op&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; μ b&lt;/span&gt;
&lt;span id=&quot;cb6-12&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#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://reasonablypolymorphic.com#cb6-13&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;Monoid&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Monoid&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a) &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-14&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  μ &lt;span class=&quot;fu&quot;&gt;mempty&lt;/span&gt; &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-15&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-16&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-16&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;Foldable&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-17&quot;&gt;&lt;a href=&quot;https://reasonablypolymorphic.com#cb6-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  μ (&lt;span class=&quot;fu&quot;&gt;foldMap&lt;/span&gt; f a) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;uncurry&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;foldMap&lt;/span&gt; (μ a)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;which I always take as a sign that I’m on the right track.&lt;/p&gt;
&lt;p&gt;Armed with &lt;code&gt;Signal&lt;/code&gt;, we can give some rudimentary combinators on it. For example, we can lift functions:&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;lift ::&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Time&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;Signal&lt;/span&gt; a&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;μ (lift f) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (f, &lt;span class=&quot;fu&quot;&gt;mempty&lt;/span&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;and we can generate stepwise signals:&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;stepwise ::&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, a)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a&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;μ (stepwise a0 []) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;pure&lt;/span&gt; a0&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;μ (stepwise a0 ((t0, a) &lt;span class=&quot;op&quot;&gt;:&lt;/span&gt; as)) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; \t &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://reasonablypolymorphic.com#cb8-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;kw&quot;&gt;case&lt;/span&gt; t &lt;span class=&quot;op&quot;&gt;&amp;lt;&lt;/span&gt; t0 &lt;span class=&quot;kw&quot;&gt;of&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;dt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; a0&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;dt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; μ (stepwise a as)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Attentive readers will notice that nothing yet ever actually introduces any times-of-interest into a &lt;code&gt;Signal&lt;/code&gt;. Here is where my earlier belly-aching comes back to bite. I’d LIKE VERY MUCH to be able to say “discrete &lt;code&gt;Signal&lt;/code&gt;s have a different type than continuous ones.” So maybe we add an &lt;code&gt;Event&lt;/code&gt; type:&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;dt&quot;&gt;Event&lt;/span&gt; a) &lt;span class=&quot;ot&quot;&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;and then say we can make &lt;em&gt;discrete&lt;/em&gt; signals:&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;discrete ::&lt;/span&gt; [(&lt;span class=&quot;dt&quot;&gt;Time&lt;/span&gt;, a)] &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&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;μ (discrete as) &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; (\t &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;fu&quot;&gt;lookup&lt;/span&gt; t as, Set.fromList &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;fst&lt;/span&gt; as)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;with the implicit promise that if you have a &lt;code&gt;Signal (Event a)&lt;/code&gt; &lt;em&gt;then it must be a discrete &lt;code&gt;Signal&lt;/code&gt;!&lt;/em&gt; Unfortunately, this is not the sort of promise we can keep; it’s trivial to construct a densely packed &lt;code&gt;Signal&lt;/code&gt; of &lt;code&gt;Event&lt;/code&gt;s:&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;notActuallyDiscrete ::&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; a &lt;span class=&quot;ot&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Signal&lt;/span&gt; (&lt;span class=&quot;dt&quot;&gt;Event&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;μ notActuallyDiscrete &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;dt&quot;&gt;Just&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;Alas. Shame. Sorrow.&lt;/p&gt;
&lt;p&gt;Nevertheless, we are just going to &lt;strong&gt;ignore&lt;/strong&gt; this issue, and &lt;strong&gt;pretend&lt;/strong&gt; like any &lt;code&gt;Signal (Event a)&lt;/code&gt; must be discrete and sparse! I am eminently aware of how disappointed &lt;a href=&quot;http://conal.net/&quot;&gt;Conal&lt;/a&gt; would be by my motivated reasoning here. I’m disappointed in myself! Unfortunately I don’t have a better answer other than to split up the continuous and discrete cases, but that throws away a bunch of the compositionality that I want. &lt;strong&gt;I am confident there is a better solution here&lt;/strong&gt; but damned if I know what it is.&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;&lt;/p&gt;
&lt;p&gt;Anyway, I’m writing this in the darkness at 5am before my family awakens, but I hear stirring so it’s probably time to stop. If you have any lovely ideas about how to solve this discrete/continuous discrepancy, I’d love to hear them.&lt;/p&gt;
&lt;p&gt;Next we’ll look at signal functions, and give to-my-knowledge a novel application of &lt;code&gt;Arrow&lt;/code&gt;s for music.&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;One titillating idea is to simply not expose any introductory forms for &lt;code&gt;Event&lt;/code&gt;. I haven’t explored this much, since it would mean we don’t get an applicative functor over &lt;code&gt;Event&lt;/code&gt;. Thinking about it now, it’d still be fine to expose the &lt;code&gt;Nothing&lt;/code&gt;-analogous constructor, so we could still give &lt;code&gt;mempty&lt;/code&gt;. And perhaps this is the first motivating case I’ve ever found for giving only a &lt;a href=&quot;https://hackage-content.haskell.org/package/semigroupoids-6.0.2/docs/Data-Functor-Apply.html#g:2&quot;&gt;&lt;code&gt;Apply&lt;/code&gt;&lt;/a&gt; instance instead of a full &lt;code&gt;Applicative&lt;/code&gt;.&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, 03 Nov 2026 05:01: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>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 id=&quot;toc&quot;&gt;&lt;h3&gt;Contents&lt;/h3&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>Well-Typed.Com: Real-Time Telemetry with Eventlog Live</title>
	<guid isPermaLink="false">http://www.well-typed.com/blog/2026/09/real-time-telemetry-with-eventlog-live</guid>
	<link>https://well-typed.com/blog/2026/09/real-time-telemetry-with-eventlog-live</link>
	<description>&lt;p&gt;Well-Typed are happy to announce &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live&quot;&gt;Eventlog Live&lt;/a&gt;, a program that streams real-time telemetry from any Haskell application to any observability platform that supports the OpenTelemetry protocol, such as &lt;a href=&quot;https://grafana.com/&quot;&gt;Grafana Cloud&lt;/a&gt;, &lt;a href=&quot;https://prometheus.io/docs/guides/opentelemetry/&quot;&gt;Prometheus&lt;/a&gt;, or local viewers such as &lt;a href=&quot;https://github.com/ymtdzzz/otel-tui&quot;&gt;&lt;code&gt;otel-tui&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/CtrlSpice/otel-desktop-viewer&quot;&gt;&lt;code&gt;otel-desktop-viewer&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this blog post, we’ll show a variety of ways to use Eventlog Live. For each showcase, we’ll link to the relevant instructions using Eventlog Live version &lt;code&gt;0.10.0.0&lt;/code&gt; as well as the script that was used to generate the showcase. If you would like to use Eventlog Live, we recommend following the up-to-date instructions in &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live#readme&quot;&gt;the README of the latest release of Eventlog Live&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;tile is-ancestor&quot;&gt;
&lt;div class=&quot;tile is-child box&quot;&gt;
  &lt;p class=&quot;is-sr-only&quot;&gt;Heap Profiles by Closure Type&lt;/p&gt;
  &lt;img alt=&quot;An animated GIF that shows a screen recording of a Grafana dashboard called Heap Profiles. It shows the heap profile of the example program called 'oddball', which has highly volatile memory usage. The dashboard has several configuration options, which let you select the service name and instance ID. For the example, the selected service name and instance ID are 'oddball' and a generated UUID, respectively. The dashboard is configured to show the last 5 minutes of data and refresh every 5 seconds. The dashboard is split into two columns. The left column contains a stacked area graph labelled 'Heap Profile' that shows a heap profile broken down by closure type. Below, there are three buttons labelled 'Start', 'Stop', and 'Census'. The right column contains six visualisations in a two-by-three grid. In order, these are labelled 'Heap Size', 'Memory Management', 'Heap Copied per GC by Generation', 'Heap Fragmentation by Generation', 'Heap Slop by Generation', and 'Heap Allocation by Capability'. The 'Heap Size' visualisation is a bar chart that shows three metrics. The 'Allocated from OS' metric shows the current size of the heap measured in megablocks, which is the unit in which the GHC RTS requests memory from the OS. The 'Allocated' metric shows the current size of the heap measured in blocks, which is the subunit that the GHC RTS uses to dole out memory to the application. The 'Live' metric shows the current size of the live heap, measured in live blocks. All three measurements are visualised in bytes. The 'Memory Management' visualisation shows three metrics that track the interaction with the OS: 'Current', 'Needed', and 'Returned'. The 'Current' metric shows the number of megablocks currently held by the RTS. The 'Needed' metric shows the number of megablocks that the RTS currently needs. If 'Needed' exceeds 'Current', the RTS is about to request more memory from the OS. If 'Needed' is less than 'Current', the RTS should return the excess megablocks to the OS. If this happens, the number of returned megablocks is shown in the 'Returned' metric. The 'Heap Copied per GC by Generation', 'Heap Fragmentation by Generation', and 'Heap Slop by Generation' are bar charts that show the number of bytes copied during GC, the amount of heap fragmentation found during GC, and the amount of slop bytes found during GC, respectively. These metrics are broken down by heap generations. In the example, these are Generation 0 and Generation 1. The 'Heap Allocation by Capability' visualisation is a bar chart that shows the number of bytes allocated by each capability. This metric is broken down by capability, but in the example there is only one Capability.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-pipe-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
  &lt;p class=&quot;box&quot;&gt;
    The Grafana Heap Profiles dashboard, which shows a heap profile broken down by closure type (&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-options-heap-prof&quot;&gt;&lt;code&gt;-hT&lt;/code&gt;&lt;/a&gt;) as well as a variety of metrics for memory usage. For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live--the-basic-version&quot;&gt;Eventlog Live – The Basic Version&lt;/a&gt;. This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-pipe.sh&quot;&gt;&lt;code&gt;oddball-with-pipe&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
  &lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-oddball-with-pipe-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Heap Profiles by Closure Type.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-pipe-2026-09-21-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box notification&quot;&gt;
      The Grafana Heap Profiles dashboard, which shows a heap profile broken down by closure type (&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-options-heap-prof&quot;&gt;&lt;code&gt;-hT&lt;/code&gt;&lt;/a&gt;) as well as a variety of metrics for memory usage. For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live--the-basic-version&quot;&gt;Eventlog Live – The Basic Version&lt;/a&gt;. This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-pipe.sh&quot;&gt;&lt;code&gt;oddball-with-pipe&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;h2 id=&quot;eventlog-live&quot;&gt;Eventlog Live&lt;/h2&gt;
&lt;p&gt;Eventlog Live analyses your application’s &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/runtime_control.html#rts-eventlog&quot;&gt;eventlog&lt;/a&gt; and exports it via the OpenTelemetry protocol.
Eventlog Live is &lt;em&gt;lightweight&lt;/em&gt;, runs side-by-side with your application in only a few megabytes of memory, and requires &lt;em&gt;no instrumentation&lt;/em&gt; other than the &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/phases.html#ghc-flag-threaded&quot;&gt;&lt;code&gt;-threaded&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/phases.html#ghc-flag-rtsopts-none-some-all-ignore-ignoreAll&quot;&gt;&lt;code&gt;-rtsopts&lt;/code&gt;&lt;/a&gt; build options.&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;
All you need is somewhere to send your data.
For instance, if you have a Grafana Cloud&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; account, adding telemetry to your application is as easy as:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode sh&quot;&gt;&lt;code class=&quot;sourceCode bash&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;co&quot;&gt;# Create a pipe for the eventlog&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;va&quot;&gt;EVENTLOG_PIPE&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;/tmp/eventlog.pipe&quot;&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;fu&quot;&gt;mkfifo&lt;/span&gt; &lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;${EVENTLOG_PIPE}&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&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;co&quot;&gt;# Start your application&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;ex&quot;&gt;your-application&lt;/span&gt; +RTS &lt;span class=&quot;at&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-ol&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;${EVENTLOG_PIPE}&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-hT&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;--eventlog-flush-interval&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;1 &lt;span class=&quot;at&quot;&gt;-RTS&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;&amp;amp;&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;&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;co&quot;&gt;# Start eventlog-live-otlp&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 class=&quot;va&quot;&gt;OTEL_SERVICE_NAME&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;your-application&quot;&lt;/span&gt;                                              &lt;span class=&quot;dt&quot;&gt;\&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 class=&quot;va&quot;&gt;OTEL_RESOURCE_ATTRIBUTES&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;service.instance.id=&lt;/span&gt;&lt;span class=&quot;va&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;fu&quot;&gt;uuidgen&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;dt&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;va&quot;&gt;OTEL_EXPORTER_OTLP_ENDPOINT&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;https://otlp-gateway-&amp;lt;YOUR_REGION&amp;gt;.grafana.net/otlp&quot;&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;\&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;va&quot;&gt;OTEL_EXPORTER_OTLP_HEADERS&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;Authorization=Basic%20&amp;lt;YOUR_AUTHORIZATION_HEADER&amp;gt;&quot;&lt;/span&gt;    &lt;span class=&quot;dt&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;&lt;span class=&quot;ex&quot;&gt;eventlog-live-otlp&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;--eventlog-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;${EVENTLOG_PIPE}&lt;/span&gt;&lt;span class=&quot;st&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;-hT&lt;/span&gt; &lt;span class=&quot;at&quot;&gt;--eventlog-flush-interval&lt;/span&gt;&lt;span class=&quot;op&quot;&gt;=&lt;/span&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Out of the box, Eventlog Live supports &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hT&quot;&gt;&lt;code&gt;-hT&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hi&quot;&gt;&lt;code&gt;-hi&lt;/code&gt;&lt;/a&gt; heap profiles&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; as well as a variety of memory usage metrics, cost-centre stack profiles, log messages, productivity, detailed thread &amp;amp; capability usage traces. Each of these features is showcased below using an example Grafana dashboard. These dashboards can be found in the repository under &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo-grafana-cloud/grafana-dashboards&quot;&gt;&lt;code&gt;demo-grafana-cloud/grafana-dashboards&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/config/grafana-dashboards&quot;&gt;&lt;code&gt;demo/config/grafana-dashboards&lt;/code&gt;&lt;/a&gt;. Click any showcase for more information.&lt;/p&gt;
&lt;div class=&quot;tile is-vertical is-ancestor&quot;&gt;
  &lt;div class=&quot;tile&quot;&gt;
    &lt;div class=&quot;tile is-child box&quot;&gt;
      &lt;p class=&quot;title is-6&quot;&gt;Heap Profiles by Closure Type&lt;/p&gt;
      &lt;img alt=&quot;This is the same animated GIF as under Heap Profiles by Closure Type.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-pipe-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&quot;tile is-child box&quot;&gt;
      &lt;p class=&quot;title is-6&quot;&gt;Heap Profiles by Info Table&lt;/p&gt;
      &lt;img alt=&quot;An animated GIF that shows a screen recording of the same Heap Profiles dashboard as under Heap Profiles by Closure Type, except that the categories for each heap profile sample show more detailed information based on the info tables.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-hi-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
      &lt;p class=&quot;is-sr-only&quot;&gt;
        The Grafana Heap Profiles dashboard, which shows a variety of metrics for memory usage and a heap profile broken down by info table (&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-options-heap-prof&quot;&gt;&lt;code&gt;-hi&lt;/code&gt;&lt;/a&gt;). For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live-with-heap-profiling-by-info-table&quot;&gt;Eventlog Live with Heap Profiling by Info Table&lt;/a&gt;. This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-hi.sh&quot;&gt;&lt;code&gt;oddball-with-hi&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;tile&quot;&gt;
    &lt;div class=&quot;tile is-child box&quot;&gt;
      &lt;p class=&quot;title is-6&quot;&gt;Cost-Centre Stack Profiles&lt;/p&gt;
      &lt;img alt=&quot;An animated GIF that shows a screen recording of a Grafana dashboard called Cost-Centre Stack Profiles. The dashboard has several configuration options, which let you select the service name and instance ID. For the example, the selected service name and instance ID are 'jumpy-jump' and a generated UUID, respectively. The dashboard is configured to show the last 6 hours of data and refresh every 5 seconds. The dashboard has one visualisation, which is split into two columns. The left column shows a table of symbols together with their sample count. The top two samples are IDLE and SYSTEM, followed by uniformIntegralM, an ever-changing list of ten functions called jumpyJump0 to jumpyJump9, and, occasionally, PROFILING:OVERHEAD_OF. The right column shows the stack profile as a flame graph. (Technically, an icicle chart, since the spikes go downward, but I don't think people use that word much anymore.) The flame graph is dominated by the IDLE category, which is sampled, roughly, two orders of magnitude more often than every other category.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/jumpy-jump-with-cost-centre-profiler-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
      &lt;p class=&quot;is-sr-only&quot;&gt;
        The Grafana Cost-Centre Stack Profiles dashboard, which shows the cost-centre stack samples collected by GHC's built-in profiler in a flame graph.
        For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live-with-cost-centre-stack-profiling&quot;&gt;Eventlog Live with Cost-Centre Stack Profiling&lt;/a&gt;.
        This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/jumpy-jump/jumpy-jump-with-cost-centre-profiler.sh&quot;&gt;&lt;code&gt;jumpy-jump-with-cost-centre-profiler&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
      &lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;tile is-child box&quot;&gt;
      &lt;p class=&quot;title is-6&quot;&gt;Logs&lt;/p&gt;
      &lt;img alt=&quot;An animated GIF that shows a screen recording of a Grafana dashboard called Logs. The dashboard has several configuration options, which let you select the service name and instance ID. For the example, the selected service name and instance ID are 'oddball' and a generated UUID, respectively. The dashboard is configured to show the last 6 hours of data and refresh every 5 seconds. The dashboard has one visualisation, which shows a list of log messages with their time and severity. Each log message has the TRACE severity and is some variant of 'Summing 313172 numbers', except the first two messages which are the thread labels for the timer and IOManager threads emitted by the RTS.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-pipe-logs-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
      &lt;p class=&quot;is-sr-only&quot;&gt;
        The Grafana Logs dashboard, which shows log messages emitted using &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Debug-Trace.html#v:traceEventIO&quot;&gt;&lt;code&gt;traceEventIO&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Debug-Trace.html#v:traceMarkerIO&quot;&gt;&lt;code&gt;traceMarkerIO&lt;/code&gt;&lt;/a&gt;, and &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/GHC-Conc-Sync.html#v:threadLabel&quot;&gt;&lt;code&gt;threadLabel&lt;/code&gt;&lt;/a&gt;.
        This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-pipe.sh&quot;&gt;&lt;code&gt;oddball-with-pipe&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
      &lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;tile is-child box&quot;&gt;
      &lt;p class=&quot;title is-6&quot;&gt;Logs as Markers&lt;/p&gt;
      &lt;img alt=&quot;An animated GIF that shows a screen recording of the same Heap Profiles dashboard as under Heap Profiles by Info Table. However, in the configuration options a switch labelled UserMarker is enabled and the field labelled Filter UserMarker contains the regular expression 'Summing \d+ numbers', and each visualisation shows dotted red lines at the times that correspond to these markers.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-hi-logs-as-markers-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
      &lt;p class=&quot;is-sr-only&quot;&gt;
        The Grafana Heap Profiles dashboard, which shows dotted red lines on each visualisation at the times that correspond to the markers, emitted using &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Debug-Trace.html#v:traceMarkerIO&quot;&gt;&lt;code&gt;traceMarkerIO&lt;/code&gt;&lt;/a&gt;, that match the regular expression &lt;code&gt;Summing \d+ numbers&lt;/code&gt;.
        This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-hi.sh&quot;&gt;&lt;code&gt;oddball-with-hi&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;tile&quot;&gt;
    &lt;div class=&quot;tile is-child box&quot;&gt;
      &lt;p class=&quot;title is-6&quot;&gt;Productivity&lt;/p&gt;
      &lt;img alt=&quot;An animated GIF that shows a screen recording of a Grafana dashboard called Threads. The dashboard has several configuration options, which let you select the service name and instance ID. For the example, the selected service name and instance ID are 'oddball' and a generated UUID, respectively. The dashboard is configured to show the last 5 minutes of data and refresh every 5 seconds. The dashboard is split into two columns. In the left column, there is a bar chart labelled  'Productivity by Capability', which shows the ratio between executing user code and garbage collection as a percentage, split out by capability and summed over the entire execution. Oddball uses only one capability, Capability 0. From the bar chart, we can see that this capability is about 80% productive during the first few seconds, then nosedives to about 65%, and stays there for the remainder of the animation. In the right column, there is a stacked area graph labelled 'Capability Usage', which shows the capability usage over a sliding window, split out into executing user code (labelled 'Mutator'), garbage collection (labelled 'GC'), and idle time (labelled 'Idle'). While this affirms the image shown by the previous visualisation, it reveals that oddball is idle the vast majority of the time.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-pipe-threads-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
      &lt;p class=&quot;is-sr-only&quot;&gt;
        The Grafana Threads dashboard, which shows the cumulative productivity per capability, as well as the overall productivity over a sliding window.
        This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-pipe.sh&quot;&gt;&lt;code&gt;oddball-with-pipe&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
      &lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;tile is-child box&quot;&gt;
      &lt;p class=&quot;title is-6&quot;&gt;Thread &amp;amp; Capability Usage&lt;/p&gt;
      &lt;img alt=&quot;A screenshot that shows the Grafana Trace Explorer, which is currently exploring a thread usage trace for thread 3. Each span is listed with its type (either Running or Blocked) and its duration. One particular span is selected, which unfolds the entry and reveals more information. Notably, it reveals that thread 3 was blocked during this span due to a foreign function call.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/spectral-norm-thread-usage-2026-09-21.png&quot; width=&quot;1280&quot; /&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-oddball-with-hi-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Heap Profiles by Info Table.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-hi-2026-09-21-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box notification&quot;&gt;
      The Grafana Heap Profiles dashboard, which shows a variety of metrics for memory usage and a heap profile broken down by info table (&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-options-heap-prof&quot;&gt;&lt;code&gt;-hi&lt;/code&gt;&lt;/a&gt;). For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live-with-heap-profiling-by-info-table&quot;&gt;Eventlog Live with Heap Profiling by Info Table&lt;/a&gt;. This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-hi.sh&quot;&gt;&lt;code&gt;oddball-with-hi&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-jumpy-jump-with-cost-centre-profiler-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Cost-Centre Stack Profiler.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/jumpy-jump-with-cost-centre-profiler-2026-09-21-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box notification&quot;&gt;
      The Grafana Cost-Centre Stack Profiles dashboard, which shows the cost-centre stack samples collected by GHC's built-in profiler in a flame graph.
      For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live-with-cost-centre-stack-profiling&quot;&gt;Eventlog Live with Cost-Centre Stack Profiling&lt;/a&gt;.
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/jumpy-jump/jumpy-jump-with-cost-centre-profiler.sh&quot;&gt;&lt;code&gt;jumpy-jump-with-cost-centre-profiler&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-oddball-with-pipe-logs-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Logs.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-pipe-logs-2026-09-21-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box notification&quot;&gt;
      The Grafana Logs dashboard, which shows log messages emitted using &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Debug-Trace.html#v:traceEventIO&quot;&gt;&lt;code&gt;traceEventIO&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Debug-Trace.html#v:traceMarkerIO&quot;&gt;&lt;code&gt;traceMarkerIO&lt;/code&gt;&lt;/a&gt;, and &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/GHC-Conc-Sync.html#v:threadLabel&quot;&gt;&lt;code&gt;threadLabel&lt;/code&gt;&lt;/a&gt;.
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-pipe.sh&quot;&gt;&lt;code&gt;oddball-with-pipe&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-oddball-with-hi-logs-as-markers-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Logs as Markers.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-hi-logs-as-markers-2026-09-21-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box notification&quot;&gt;
      The Grafana Heap Profiles dashboard, which shows dotted red lines on each visualisation at the times that correspond to the markers, emitted using &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Debug-Trace.html#v:traceMarkerIO&quot;&gt;&lt;code&gt;traceMarkerIO&lt;/code&gt;&lt;/a&gt;, that match the regular expression &lt;code&gt;Summing \d+ numbers&lt;/code&gt;.
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-hi.sh&quot;&gt;&lt;code&gt;oddball-with-hi&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-oddball-with-pipe-threads-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Productivity.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-pipe-threads-2026-09-21-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box notification&quot;&gt;
      The Grafana Threads dashboard, which shows the cumulative productivity per capability, as well as the overall productivity over a sliding window.
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-pipe.sh&quot;&gt;&lt;code&gt;oddball-with-pipe&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-spectral-norm-thread-usage-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same image as under Thread &amp;amp; Capability Usage.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/spectral-norm-thread-usage-2026-09-21.png&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box notification&quot;&gt;
      The Tempo trace explorer, which shows a detailed trace of the scheduling of one thread, which includes the durations for each running/blocked segment as well as the reason for each time the thread becomes blocked.
      These traces are not enabled by default, as their volume easily overwhelms most endpoints, and must be enabled using a &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live#configuration-files&quot;&gt;configuration file&lt;/a&gt;.
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/spectral-norm/spectral-norm.sh&quot;&gt;&lt;code&gt;spectral-norm&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;p&gt;Eventlog Live’s OTLP exporter is configured using OpenTelemetry’s environment variables. The output of &lt;code&gt;eventlog-live-otlp --help&lt;/code&gt; has &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/eventlog-live/data/help.footer.txt&quot;&gt;comprehensive documentation on its support for these environment variables&lt;/a&gt;.
By default, Eventlog Live aggregates telemetry data over 1 second intervals and exports every 30 seconds, but these intervals can be configured for each telemetry signal using &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live#configuration-files&quot;&gt;configuration files&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;dynamic-control-with-eventlog-socket&quot;&gt;Dynamic Control with Eventlog Socket&lt;/h2&gt;
&lt;p&gt;If you instrument your application with &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-socket/#readme&quot;&gt;&lt;code&gt;eventlog-socket&lt;/code&gt;&lt;/a&gt;, you’ll be able to start/stop profiling while your application is running using a REST API, which you can connect, e.g., to buttons on your Grafana dashboard, as we’ve done on the example Heap Profiles dashboard.&lt;/p&gt;
&lt;div class=&quot;tile is-ancestor&quot;&gt;
&lt;div class=&quot;tile is-child box&quot;&gt;
  &lt;p class=&quot;is-sr-only&quot;&gt;Dynamic Control with Eventlog Socket&lt;/p&gt;
  &lt;img alt=&quot;An animated GIF that shows a screen recording of the same Grafana Heap Profiles dashboard that was described above.  It continues to show the heap profile of the 'oddball' example program.  This recording illustrates the usage of the 'Start' and 'Stop' buttons.   At the start of the recording, the 'Heap Profile' visualisation shows a history of volatile and spiky memory usage.  After a few refreshes, the 'Stop' button is pressed, the message 'Requested Heap Profiling Stop' appears in the top-right corner, and the 'Heap Profile' visualisation flatlines.   A few refreshes later, the 'Start' button is pressed, the message 'Requested Heap Profiling Start' appears in the top-right corner, and the 'Heap Profile' visualisation resumes.  The 'Census' button can be used to request a single heap census, though this isn't shown in the recording.  The visualisations in the right column are unaffected by the 'Start' and 'Stop' button, and continue to update during the entire recording.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-control-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
  &lt;p class=&quot;box&quot;&gt;
    Using the buttons on the Grafana Heap Profiles dashboard. When “Stop” is pressed, the heap profile flatlines. When “Start” is pressed, the heap profile resumes. The “Census” button can be used to request a single heap census. For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live-with-eventlog-socket&quot;&gt;Eventlog Live with Eventlog Socket&lt;/a&gt;. This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-hT.sh&quot;&gt;&lt;code&gt;oddball-with-hT&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
  &lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-oddball-control-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Dynamic Control with Eventlog Socket.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-control-2026-09-21-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box&quot;&gt;
      Using the buttons on the Grafana Heap Profiles dashboard. When “Stop” is pressed, the heap profile flatlines. When “Start” is pressed, the heap profile resumes. The “Census” button can be used to request a single heap census. For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live-with-eventlog-socket&quot;&gt;Eventlog Live with Eventlog Socket&lt;/a&gt;. This showcase uses the  &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/examples/oddball/oddball-with-hT.sh&quot;&gt;&lt;code&gt;oddball-with-hT&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;p&gt;Eventlog Socket lets your application write its eventlog over sockets and uses the other direction of communication for control messages. Eventlog Socket’s control protocol is generic. It isn’t bound to any specific application or library. Any library can register its own &lt;a href=&quot;https://hackage-content.haskell.org/package/eventlog-socket-0.1.3.0/docs/GHC-Eventlog-Socket.html#g:control_commands&quot;&gt;control commands&lt;/a&gt; under its own namespace,&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; using Haskell IO actions as callbacks:&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;registerMyEventlogSocketSupport &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;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;  myPackageNamespace &lt;span class=&quot;ot&quot;&gt;&amp;lt;-&lt;/span&gt; registerNamespace &lt;span class=&quot;st&quot;&gt;&quot;my-package-name&quot;&lt;/span&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;  registerCommand myPackageNamespace (&lt;span class=&quot;dt&quot;&gt;CommandId&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;) myCommandCallback1&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;  registerCommand myPackageNamespace (&lt;span class=&quot;dt&quot;&gt;CommandId&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;) myCommandCallback2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Any command that your application registers with Eventlog Socket can be invoked using Eventlog Live’s REST API.&lt;/p&gt;
&lt;p&gt;For security reasons, Eventlog Socket’s control protocol and Eventlog Live’s REST API are hidden behind the &lt;code&gt;+control&lt;/code&gt; feature flag, which is disabled by default. If you develop a library that (optionally) uses Eventlog Socket’s control protocol, we recommend using the same &lt;code&gt;+control&lt;/code&gt; feature flag.&lt;/p&gt;
&lt;h2 id=&quot;call-stack-profiles-with-ghc-stack-profiler&quot;&gt;Call-Stack Profiles with GHC Stack Profiler&lt;/h2&gt;
&lt;p&gt;If you instrument your application with &lt;a href=&quot;https://hackage.haskell.org/package/ghc-stack-profiler/#readme&quot;&gt;&lt;code&gt;ghc-stack-profiler&lt;/code&gt;&lt;/a&gt;, you’ll be able to use its lightweight call-stack profiler.&lt;/p&gt;
&lt;div class=&quot;tile is-ancestor&quot;&gt;
&lt;div class=&quot;tile is-child box&quot;&gt;
  &lt;p class=&quot;is-sr-only&quot;&gt;Call-Stack Profiles with GHC Stack Profiler&lt;/p&gt;
  &lt;img alt=&quot;An animated GIF that shows a screen recording of a Grafana dashboard called Call-Stack Profiles. This dashboard has the same layout as described under Cost-Centre Stack Profiler, but the profiles are slightly different. Notably, the IDLE, SYSTEM, and PROFILING:OVERHEAD_OF categories are not recorded by GHC Stack Profiler. The most prevalent category is 'other' followed by the same ever-changing list of ten functions called jumpyJump0 to jumpyJump9. The flame graph is significantly spikier. The IDLE category isn't recorded and does not dominate the flame graph, which leaves a lot more room for the deeply nested calls jumping between the various jumpyJump functions.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/jumpy-jump-with-ghc-stack-profiler-2026-09-21.webp&quot; width=&quot;1280&quot; /&gt;
  &lt;p class=&quot;box&quot;&gt;
    The Grafana Call-Stack Profiles dashboard, which shows the call-stack samples collected by &lt;code&gt;ghc-stack-profiler&lt;/code&gt; in a flame graph. For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live-with-ghc-stack-profiler&quot;&gt;Eventlog Live with GHC Stack Profiler&lt;/a&gt;. This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/74a1c45451b1e3dac7a0c2ab826df22a7a7fe471/examples/jumpy-jump/jumpy-jump-with-ghc-stack-profiler.sh&quot;&gt;&lt;code&gt;jumpy-jump-with-ghc-stack-profiler&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
  &lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-jumpy-jump-with-ghc-stack-profiler-2026-09-21&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
  &lt;figure&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Call-Stack Profiles with GHC Stack Profiler.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/jumpy-jump-with-ghc-stack-profiler-2026-09-21-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;figcaption class=&quot;box&quot;&gt;
      The Grafana Call-Stack Profiles dashboard, which shows the call-stack samples collected by &lt;code&gt;ghc-stack-profiler&lt;/code&gt; in a flame graph. For detailed instructions, see &lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live-0.10.0.0#eventlog-live-with-ghc-stack-profiler&quot;&gt;Eventlog Live with GHC Stack Profiler&lt;/a&gt;. This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/74a1c45451b1e3dac7a0c2ab826df22a7a7fe471/examples/jumpy-jump/jumpy-jump-with-ghc-stack-profiler.sh&quot;&gt;&lt;code&gt;jumpy-jump-with-ghc-stack-profiler&lt;/code&gt;&lt;/a&gt; example with the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt;.
    &lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;p&gt;GHC Stack Profiler samples GHC’s runtime call-stack, rather than a virtual cost-centre stack, and writes these samples to the eventlog. Based on &lt;a href=&quot;https://hackage.haskell.org/package/ghc-stack-profiler-0.5.0.0#benchmarks&quot;&gt;our benchmarks&lt;/a&gt;, GHC Stack Profiler has virtually no overhead when it’s not running and we observed 2-8% overhead while it was running, compared to GHC’s built-in cost-centre stack profiler, where we observed 54-128% overhead while it wasn’t running and 98-136% overhead while it was running.&lt;/p&gt;
&lt;p&gt;If you’ve also instrumented your application with Eventlog Socket, you’ll be able to start/stop GHC Stack Profiler while your application is running using the same REST API, which you can connect, e.g., to buttons on your Grafana dashboard, as we’ve done on the example Call-Stack Profiles dashboard.&lt;/p&gt;
&lt;p&gt;GHC Stack Profiler can also be used without Eventlog Live, e.g., by &lt;a href=&quot;https://hackage.haskell.org/package/ghc-stack-profiler-0.5.0.0#ghc-stack-profiler-with-speedscope&quot;&gt;exporting the profiles to speedscope&lt;/a&gt;. We’ll discuss GHC Stack Profiler in detail in an upcoming blog post.&lt;/p&gt;
&lt;h2 id=&quot;local-viewers&quot;&gt;Local Viewers&lt;/h2&gt;
&lt;p&gt;Eventlog Live does not yet include a built-in telemetry viewer, as it was primarily developed to monitor long-running processes in production. We are currently developing a built-in telemetry viewer, but the prototype is not yet ready for publication. In the meantime, some developers may feel that using either Grafana Cloud or the Docker Compose setup in &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo/docker-compose.yml&quot;&gt;&lt;code&gt;demo/docker-compose.yml&lt;/code&gt;&lt;/a&gt; is too heavyweight for local development. Fortunately, the OpenTelemetry ecosystem has several lightweight telemetry viewers, such as &lt;a href=&quot;https://github.com/ymtdzzz/otel-tui&quot;&gt;&lt;code&gt;otel-tui&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://github.com/CtrlSpice/otel-desktop-viewer&quot;&gt;&lt;code&gt;otel-desktop-viewer&lt;/code&gt;&lt;/a&gt;, which are general purpose telemetry viewers that use a text and browser-based UI, respectively.&lt;/p&gt;
&lt;div class=&quot;tile is-ancestor&quot;&gt;
  &lt;div class=&quot;tile is-child box&quot;&gt;
    &lt;p class=&quot;is-sr-only&quot;&gt;Eventlog Live with otel-tui&lt;/p&gt;
    &lt;img alt=&quot;An animated GIF that shows a screen recording of a terminal session. The terminal is split into three screens. On the left, taking up the majority of the terminal, is a screen labelled 'otel-tui'. On the right, split vertically, are two screens labelled 'oddball' and 'eventlog-live-otlp'. The 'otel-tui' screen is itself split into three subscreens. A narrow column on the left, labelled 'Metrics', lists all incoming metrics by name. These names correspond to the names described under Heap Profile by Closure Type, except formatted in camel-case and prefixed with 'ghc_eventlog_'. Currently, the metric named 'ghc_eventlog_HeapLive' is selected. The wide column on the right is split vertically. The top row, labelled 'Details', shows the exact structure of the selected OTLP message for the selected metric. The bottom row, labelled 'Chart', shows a text rendering of a line chart that shows the values of the 'ghc_eventlog_HeapLive' metric. This shows a spiky line, which jumps between 10-30MB, though the Y-axis is labelled in bytes rather than megabytes. The 'oddball' screen shows the output of the oddball program, which consists of many lines saying 'Generating X random numbers' and 'Sum: Y' for ever-changing values of X and Y. The 'eventlog-live-otlp' screen shows the debug output of Eventlog Live, which consists of many lines saying '[DEBUG] Received X events.', '[DEBUG] Exported Y metrics.', and '[DEBUG] Exported Z logs.', for ever-changing values of X, Y, and Z.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-otel-tui-2026-09-22.webp&quot; width=&quot;1280&quot; /&gt;
    &lt;p class=&quot;box&quot;&gt;
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/74a1c45451b1e3dac7a0c2ab826df22a7a7fe471/examples/oddball/oddball-with-otel-tui.sh&quot;&gt;&lt;code&gt;oddball-with-otel-tui&lt;/code&gt;&lt;/a&gt; example with &lt;a href=&quot;https://github.com/ymtdzzz/otel-tui&quot;&gt;&lt;code&gt;otel-tui&lt;/code&gt;&lt;/a&gt; version 0.7.4.
    This example creates a screen session that combines &lt;code&gt;otel-tui&lt;/code&gt; with &lt;code&gt;oddball&lt;/code&gt; and &lt;code&gt;eventlog-live-otlp&lt;/code&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;tile is-child box&quot;&gt;
    &lt;p class=&quot;is-sr-only&quot;&gt;Eventlog Live with otel-desktop-viewer&lt;/p&gt;
    &lt;img alt=&quot;An animated GIF that shows a screen recording of the browser-based otel-desktop-viewer application. The website is split into three columns. The left column has three tabs labelled 'Traces', 'Metrics', and 'Logs'. Currently, the 'Metrics' tab is selected, which lists all incoming metrics by name and application. These names correspond to the names described under Heap Profile by Closure Type, except formatted in camel-case and prefixed with 'ghc_eventlog_'. Currently, the metric named 'ghc_eventlog_HeapProfSample (oddball)' is selected. The right column lists all series in this metric. The 'ghc_eventlog_HeapProfSample' metric is the heap profile, broken down by closure type, so the various series correspond to the various closure types. Only the top few series are selected. Amongst these are built-in closure types such as FUN, FUN_2_0, STACK, and BLACKHOLE, as well as user-defined function types such as ghc-bignum:GHC.Num.Integer.IS. The series are constantly reordered, so they're a bit hard to read. The middle column, which takes up about half of the website, shows a chart with the selected series. While ten series are selected, only two lines are visible. STACK and ghc-bignum:GHC.Num.Integer.IS. This is because these samples represent the vast majority of the heap size used by the oddball program. The remaining lines, presumably, all hug the X-axis, though this isn't visually clear.&quot; class=&quot;js-modal-trigger is-clickable has-ratio&quot; height=&quot;691&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-otel-desktop-viewer-2026-09-22.webp&quot; width=&quot;1280&quot; /&gt;
    &lt;p class=&quot;box&quot;&gt;
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/74a1c45451b1e3dac7a0c2ab826df22a7a7fe471/examples/oddball/oddball-with-otel-desktop-viewer.sh&quot;&gt;&lt;code&gt;oddball-with-otel-desktop-viewer&lt;/code&gt;&lt;/a&gt; example with &lt;a href=&quot;https://github.com/CtrlSpice/otel-desktop-viewer&quot;&gt;&lt;code&gt;otel-desktop-viewer&lt;/code&gt;&lt;/a&gt; version 0.5.0.
    &lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-oddball-with-otel-tui-2026-09-22&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Eventlog Live with otel-tui.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-otel-tui-2026-09-22-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box&quot;&gt;
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/74a1c45451b1e3dac7a0c2ab826df22a7a7fe471/examples/oddball/oddball-with-otel-tui.sh&quot;&gt;&lt;code&gt;oddball-with-otel-tui&lt;/code&gt;&lt;/a&gt; example with &lt;a href=&quot;https://github.com/ymtdzzz/otel-tui&quot;&gt;&lt;code&gt;otel-tui&lt;/code&gt;&lt;/a&gt; version 0.7.4.
      This example creates a screen session that combines &lt;code&gt;otel-tui&lt;/code&gt; with &lt;code&gt;oddball&lt;/code&gt; and &lt;code&gt;eventlog-live-otlp&lt;/code&gt;.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;modal&quot; id=&quot;modal-oddball-with-otel-desktop-viewer-2026-09-22&quot;&gt;
  &lt;div class=&quot;modal-background&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;modal-content&quot;&gt;
    &lt;img alt=&quot;This is the same animated GIF as under Eventlog Live with otel-desktop-viewer.&quot; class=&quot;has-ratio&quot; height=&quot;1382&quot; src=&quot;https://well-typed.com/blog/aux/images/real-time-telemetry-with-eventlog-live/oddball-with-otel-desktop-viewer-2026-09-22-large.webp&quot; width=&quot;2560&quot; /&gt;
    &lt;p class=&quot;box&quot;&gt;
      This showcase uses the &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/74a1c45451b1e3dac7a0c2ab826df22a7a7fe471/examples/oddball/oddball-with-otel-desktop-viewer.sh&quot;&gt;&lt;code&gt;oddball-with-otel-desktop-viewer&lt;/code&gt;&lt;/a&gt; example with &lt;a href=&quot;https://github.com/CtrlSpice/otel-desktop-viewer&quot;&gt;&lt;code&gt;otel-desktop-viewer&lt;/code&gt;&lt;/a&gt; version 0.5.0.
    &lt;/p&gt;
  &lt;/div&gt;
  &lt;button class=&quot;modal-close is-large&quot;&gt;&lt;/button&gt;
&lt;/div&gt;
&lt;h2 id=&quot;future-work&quot;&gt;Future Work&lt;/h2&gt;
&lt;p&gt;We are currently developing a built-in telemetry viewer that is purpose-built for GHC’s telemetry, with the intention of making Eventlog Live more useful for short development sessions. We’re also planning to create a library for writing structured log messages to the eventlog, expand the analyses supported by Eventlog Live, and expand the Eventlog Socket protocol to permit command arguments. Moreover, we’re always open to suggestions and feature requests from the community.&lt;/p&gt;
&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;
&lt;p&gt;This work has been performed in collaboration with Mercury, who have a long-term commitment to the scalability and robustness of the Haskell ecosystem. Well-Typed are always interested in projects and looking for funding to improve 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;If your application is built with GHC 9.2 or older, it must also be built with &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/phases.html#ghc-flag-eventlog&quot;&gt;&lt;code&gt;-eventlog&lt;/code&gt;&lt;/a&gt;. Since GHC 9.4, the eventlog is enabled by default.&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;The repository contains &lt;a href=&quot;https://github.com/well-typed/eventlog-live/blob/eventlog-live-v0.10.0.0/demo-grafana-cloud#readme&quot;&gt;a self-contained demo for using Eventlog Live with Grafana Cloud&lt;/a&gt;. Unfortunately, as Grafana Cloud changes its user interface frequently, that demo may not be fully up-to-date.&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;Eventlog Live is developed and tested with the &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hT&quot;&gt;&lt;code&gt;-hT&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hi&quot;&gt;&lt;code&gt;-hi&lt;/code&gt;&lt;/a&gt; heap profile breakdowns, but the &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hm&quot;&gt;&lt;code&gt;-hm&lt;/code&gt;&lt;/a&gt;/&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hd&quot;&gt;&lt;code&gt;-hd&lt;/code&gt;&lt;/a&gt;/&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hy&quot;&gt;&lt;code&gt;-hy&lt;/code&gt;&lt;/a&gt;/&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-he&quot;&gt;&lt;code&gt;-he&lt;/code&gt;&lt;/a&gt; heap profile breakdowns should work. The &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hc&quot;&gt;&lt;code&gt;-hc&lt;/code&gt;&lt;/a&gt;/&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hb&quot;&gt;&lt;code&gt;-hb&lt;/code&gt;&lt;/a&gt;/&lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html#rts-flag-hr&quot;&gt;&lt;code&gt;-hr&lt;/code&gt;&lt;/a&gt; heap profile breakdowns require additional support, which is planned but not yet implemented.&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;We strongly recommend using your package name.&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;/ol&gt;
&lt;/section&gt;</description>
	<pubDate>Thu, 24 Sep 2026 00:00:00 +0000</pubDate>
</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>Mark Jason Dominus: Noting another house repair chore victory</title>
	<guid isPermaLink="false">tag:,2026:/brain/silicone-2</guid>
	<link>https://blog.plover.com/brain/silicone-2.html</link>
	<description>&lt;p&gt;&lt;a href=&quot;https://blog.plover.com/brain/silicone.html&quot;&gt;Yesterday I reported about how much I despise home repair&lt;/a&gt;, how I feel
I'm really bad at it, how it sometimes goes terribly wrong for me, and
how my work diary had demonstrated that a seeming failure was catually
a glorious success.&lt;/p&gt;

&lt;p&gt;I included this picture of our (formerly!) leaky laundry tub, and it
reminded me of something else.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://pic.blog.plover.com/brain/silicone-2/tub.jpg&quot;&gt;&lt;img class=&quot;center&quot; src=&quot;https://pic.blog.plover.com/brain/silicone-2/tub-th-640.jpg&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we first moved into the house, more than four years ago now, that
black outflow hose was unsecured.  If you weren't careful — and
sometimes even if you were — it would thrash around while the washer
was emptying, and sometimes spray water everywhere.&lt;/p&gt;

&lt;p&gt;In the first week or two after we moved in, there was a long list of
minor problems like that. I found some store online that was selling a
fix for the thrashing hose. The fix, which you can see in the picture,
is a stiff but flexible rod with a collar at each end.  You affix the
collars around the top and bottom ends of the hose, then bend the
flexible rod to force the hose to point in the direction you want.
Then the hose can't thrash.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.plover.com/misc/evaluation.html&quot;&gt;I observed a while back&lt;/a&gt; that the worst
projects are also the ones that are easiest to remember.  These are
the ones that drag on for months, requiring endless adjustments,
different approaches, and a lot of thought and toil.  Or they're the
ones that fail completely leaving some unfinished damage that reminds
me every day that what I did didn't work.&lt;/p&gt;

&lt;p&gt;Here's an example in the other direction.  I didn't know how to solve
this problem, but I found a solution, I executed it, and because the
solution has worked flawlessly for more than four years I completely
forgot about it until today when I was looking at this photo.&lt;/p&gt;

&lt;p&gt;The work diary can help with this sort of thing.  If I had a rollup
page for laundry room issues I would be able to open it and see a list
of all the things I had gotten right over the years, instead of
forgetting them and only remembering the stuff that went wrong.&lt;/p&gt;

&lt;p&gt;Claude advised that it would be better to write “hub”
instead of “tub”, but I opted not to take its advice.&lt;/p&gt;

&lt;h2&gt;Addendum&lt;/h2&gt;

&lt;p&gt;Another example that occurs to me: in our house it's my job to unclog
toilets.  How many times over the years has a toilet clogged and I
unfussily unclogged it with the plunger?  Too many to count.  And
there have been other occasions when the plunger didn't work, so I
would shrug, and go downstairs to get
&lt;a href=&quot;https://blog.plover.com/misc/toilet-snake.html&quot;&gt;the toilet auger&lt;/a&gt;.  I don't remember
these many episodes because to me they were no big deal.&lt;/p&gt;

&lt;p&gt;I should try to remember that no matter how incompetent I feel in
general, just &lt;em&gt;owning&lt;/em&gt; a toilet auger puts me way ahead of a lot of
other people.&lt;/p&gt;</description>
	<pubDate>Wed, 23 Sep 2026 13:46:00 +0000</pubDate>
	<author>mjd@plover.com (Mark Dominus)</author>
</item>
<item>
	<title>Well-Typed.Com: Haskell ecosystem activities report: Juneâ€“August 2026</title>
	<guid isPermaLink="false">http://www.well-typed.com/blog/2026/09/haskell-ecosystem-report-june-august-2026</guid>
	<link>https://well-typed.com/blog/2026/09/haskell-ecosystem-report-june-august-2026</link>
	<description>&lt;p&gt;This is the thirty-second edition of our Haskell ecosystem activities report,
which describes the work Well-Typed are doing on GHC, Cabal, HLS and other parts
of the core Haskell toolchain. The current edition covers roughly the months of
June, July and August 2026.&lt;/p&gt;
&lt;p&gt;You can find the previous editions collected under the
&lt;a href=&quot;https://well-typed.com/blog/tags/haskell-ecosystem-report&quot;&gt;haskell-ecosystem-report tag&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;sponsorship&quot;&gt;Sponsorship&lt;/h2&gt;
&lt;p&gt;We offer &lt;a href=&quot;https://well-typed.com/ecosystem/&quot;&gt;Haskell Ecosystem Support Packages&lt;/a&gt; to provide commercial
users with support from Well-Typed’s experts while investing in the Haskell
community and its technical ecosystem including through the work described in
this report. To find out more, read our &lt;a href=&quot;https://well-typed.com/blog/2025/06/haskell-ecosystem-support-packages&quot;&gt;announcement of these
packages&lt;/a&gt; in partnership with
the Haskell Foundation. We need funding to continue this essential maintenance work!&lt;/p&gt;
&lt;p&gt;Many thanks to our Haskell Ecosystem Supporters: &lt;a href=&quot;https://www.sc.com/&quot;&gt;Standard Chartered&lt;/a&gt;,
&lt;a href=&quot;https://www.channable.com/&quot;&gt;Channable&lt;/a&gt; and &lt;a href=&quot;https://qbaylogic.com/&quot;&gt;QBayLogic&lt;/a&gt;,
as well as to our other clients who also contribute to making this work possible:
&lt;a href=&quot;https://www.anduril.com/&quot;&gt;Anduril&lt;/a&gt;, &lt;a href=&quot;https://juspay.in/&quot;&gt;Juspay&lt;/a&gt; and &lt;a href=&quot;https://mercury.com/&quot;&gt;Mercury&lt;/a&gt;;
and to the &lt;a href=&quot;https://opencollective.com/haskell-language-server&quot;&gt;HLS Open Collective&lt;/a&gt; for
supporting HLS release management.&lt;/p&gt;
&lt;h2 id=&quot;highlights&quot;&gt;Highlights&lt;/h2&gt;

&lt;h3 id=&quot;known-key-names-and-reinstallable-base&quot;&gt;Known-key names and reinstallable &lt;code&gt;base&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;We have long been working to decouple GHC from &lt;code&gt;base&lt;/code&gt;, to make it easier to
upgrade the compiler independently from the (standard) libraries.
A key restriction that prevented disentangling &lt;code&gt;base&lt;/code&gt; from GHC was GHC’s
treatment of &lt;em&gt;known entities&lt;/em&gt;. These are things in the standard libraries
that GHC needs to know something about (e.g. &lt;code&gt;Show&lt;/code&gt; for &lt;code&gt;deriving&lt;/code&gt;, &lt;code&gt;Monad&lt;/code&gt; for desugaring &lt;code&gt;do&lt;/code&gt;
notation, etc). This is detailed in &lt;a href=&quot;https://blog.haskell.org/making-ghc-upgrades-easy/#3-3-known-entities&quot;&gt;section 3.3&lt;/a&gt;
of &lt;a href=&quot;https://blog.haskell.org/making-ghc-upgrades-easy/&quot;&gt;a recent blog post by Simon Peyton Jones&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Rodrigo and Sam recently overhauled GHC’s treatment of known entities, in
collaboration with Simon (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/15899&quot;&gt;!15899&lt;/a&gt;), finally landing the patch in August. This
allowed known entities to be defined completely outside of the compiler for the
first time, without hard-coding the precise module that defines them.&lt;/p&gt;
&lt;p&gt;As part of this change, it was also necessary to rework some implementation
details of &lt;code&gt;Typeable&lt;/code&gt;, namely the &lt;code&gt;KindRep&lt;/code&gt; datatype. Sam opened
&lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/433&quot;&gt;CLC proposal &lt;span&gt;#433&lt;/span&gt;&lt;/a&gt;
to get approval on this change, which required an accompanying change to the &lt;code&gt;binary&lt;/code&gt;
package, which Rodrigo implemented in &lt;a href=&quot;https://github.com/haskell/binary/pull/244&quot;&gt;&lt;span&gt;#244&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;aarch64-code-generation-and-sub-word-handling&quot;&gt;AArch64 code generation and sub-word handling&lt;/h3&gt;
&lt;p&gt;Andreas has been hunting down a slew of correctness bugs in the AArch64 native
code generator, mostly related to the handling of sub-word values. Issues
include sub-word handling across C calls (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27430&quot;&gt;#27430&lt;/a&gt;), in atomic loads/stores
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27533&quot;&gt;#27533&lt;/a&gt;), in conversions (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27537&quot;&gt;#27537&lt;/a&gt;), in &lt;code&gt;if&lt;/code&gt; conditionals (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27538&quot;&gt;#27538&lt;/a&gt;), in &lt;code&gt;%i2f64&lt;/code&gt;
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27539&quot;&gt;#27539&lt;/a&gt;) and the use of invalid immediates (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27550&quot;&gt;#27550&lt;/a&gt;). These were all fixed in
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16255&quot;&gt;!16255&lt;/a&gt;, with a lot of the testing infrastructure being provided by Sam’s
overhaul of &lt;code&gt;test-primops&lt;/code&gt;
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/test-primops/-/merge_requests/43&quot;&gt;&lt;span&gt;!43&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;
&lt;h3 id=&quot;coercion-zapping&quot;&gt;Coercion zapping&lt;/h3&gt;
&lt;p&gt;Extremely poor compiler performance for programs involving significant
type-level computation has long plagued the community (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/8095&quot;&gt;#8095&lt;/a&gt;, recently
re-reported as &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27336&quot;&gt;#27336&lt;/a&gt;). One major cause for this is blow-ups in coercion sizes
due to the representation of accumulated type family reduction steps in Core,
GHC’s typed intermediate language.&lt;/p&gt;
&lt;p&gt;This problem resisted many attempts over the years, with promising approaches
being brought down by performance regressions on certain types of programs (e.g.
singleton-heavy programs).
However, the future is bright for type-level wizards: MR &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16378&quot;&gt;!16378&lt;/a&gt; implementing
coercion zapping promises to bring the story to a satisfying conclusion.&lt;/p&gt;
&lt;p&gt;This MR has an unusual history. In July, Sylvain Henry put up a &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/8095#note_685609&quot;&gt;proof-of-concept patch&lt;/a&gt;
that was generated using Claude Fable. This patch was a carefully balanced
“best of” previous approaches over the years, combining elements from MRs by
Ben (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/611&quot;&gt;!611&lt;/a&gt;), Adam (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6476&quot;&gt;!6476&lt;/a&gt;), Sam (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/7787&quot;&gt;!7787&lt;/a&gt;) and Richard Eisenberg (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/7909&quot;&gt;!7909&lt;/a&gt;), each of
which made significant progress on some programs but struggled to overcome
significant compiler performance regressions on other programs.&lt;/p&gt;
&lt;p&gt;Sam took over this patch and rewrote it from the ground up, doing a deep
dive into GHC’s type family rewriter, and finding
several related optimisation opportunities (e.g. &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16479&quot;&gt;!16479&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The net result is a patch that significantly improves compilation speed of
type-family-heavy programs without regressing elsewhere. This patch is under
final review and we hope to land it soon. Performance results
from GHC’s testsuite:&lt;/p&gt;
&lt;table&gt;
&lt;colgroup&gt;
&lt;col style=&quot;width: 23%;&quot; /&gt;
&lt;col style=&quot;width: 40%;&quot; /&gt;
&lt;col style=&quot;width: 36%;&quot; /&gt;
&lt;/colgroup&gt;
&lt;thead&gt;
&lt;tr class=&quot;header&quot;&gt;
&lt;th&gt;Test case&lt;/th&gt;
&lt;th&gt;Compiler allocations&lt;/th&gt;
&lt;th&gt;Compilation time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&quot;odd&quot;&gt;
&lt;td&gt;T8095&lt;/td&gt;
&lt;td&gt;  768 MB ⇝   128 MB (−83%)&lt;/td&gt;
&lt;td&gt;2.432s ⇝ 0.160s (−93%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
&lt;td&gt;T9872a&lt;/td&gt;
&lt;td&gt;1 656 MB ⇝ 1 406 MB (−15%)&lt;/td&gt;
&lt;td&gt;0.971s ⇝ 0.494s (−49%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
&lt;td&gt;T12545&lt;/td&gt;
&lt;td&gt;  698 MB ⇝   425 MB (−39%)&lt;/td&gt;
&lt;td&gt;0.648s ⇝ 0.387s (−40%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
&lt;td&gt;T13386&lt;/td&gt;
&lt;td&gt;  428 MB ⇝    35 MB (−92%)&lt;/td&gt;
&lt;td&gt;0.259s ⇝ 0.104s (−60%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
&lt;td&gt;T27336&lt;/td&gt;
&lt;td&gt;  499 MB ⇝    36 MB (−93%)&lt;/td&gt;
&lt;td&gt;1.188s ⇝ 0.110s (−91%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
&lt;td&gt;CoOpt_Singletons&lt;/td&gt;
&lt;td&gt;  634 MB ⇝   596 MB ( −6%)&lt;/td&gt;
&lt;td&gt;0.484s ⇝ 0.360s (−26%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;odd&quot;&gt;
&lt;td&gt;LargeRecord&lt;/td&gt;
&lt;td&gt;1 227 MB ⇝   967 MB (−21%)&lt;/td&gt;
&lt;td&gt;0.872s ⇝ 0.617s (−29%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&quot;even&quot;&gt;
&lt;td&gt;FamAppCachePerf&lt;/td&gt;
&lt;td&gt;  496 MB ⇝   192 MB (−61%)&lt;/td&gt;
&lt;td&gt;0.675s ⇝ 0.191s (−72%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id=&quot;exception-annotations&quot;&gt;Exception annotations&lt;/h3&gt;
&lt;p&gt;Ben’s &lt;a href=&quot;https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0330-exception-backtraces.rst&quot;&gt;GHC proposal &lt;span&gt;#330&lt;/span&gt;&lt;/a&gt; and accompanying &lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/164&quot;&gt;CLC proposal &lt;span&gt;#164&lt;/span&gt;&lt;/a&gt;
proposed a deep rework of exception backtraces, providing several different
backtrace mechanisms (e.g. &lt;code&gt;HasCallStack&lt;/code&gt;, profiling cost centres, IPE).&lt;/p&gt;
&lt;p&gt;However, the implementation of these proposals remained patchy, as detailed in
&lt;a href=&quot;https://well-typed.com/blog/2026/05/lay-annotation-land/&quot;&gt;Edsko’s recent blog post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The team made several follow-up improvements to mechanisms for
attaching information to exceptions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ben fixed &lt;code&gt;WhileHandling&lt;/code&gt; dropping the &lt;code&gt;ExceptionContext&lt;/code&gt; of the exception
it is handling (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16275&quot;&gt;!16275&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27456&quot;&gt;#27456&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Sam made sure &lt;code&gt;SomeException&lt;/code&gt; does not drop the &lt;code&gt;ExceptionContext&lt;/code&gt; of the
exception it is storing (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16274&quot;&gt;!16274&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27455&quot;&gt;#27455&lt;/a&gt;), with accompanying &lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/432&quot;&gt;CLC proposal &lt;span&gt;#432&lt;/span&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Wolfgang added &lt;code&gt;rethrowSTM&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16501&quot;&gt;!16501&lt;/a&gt;), incorporated the re-export into the
&lt;code&gt;stm&lt;/code&gt; submodule (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16616&quot;&gt;!16616&lt;/a&gt;), and upstreamed it to &lt;code&gt;stm&lt;/code&gt; (&lt;a href=&quot;https://github.com/haskell/stm/pull/100&quot;&gt;&lt;span&gt;#100&lt;/span&gt;&lt;/a&gt;),
with accompanying &lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/436&quot;&gt;CLC proposal &lt;span&gt;#436&lt;/span&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Zubin made &lt;code&gt;annotateSTM&lt;/code&gt; use &lt;code&gt;catchSTM#&lt;/code&gt; rather than &lt;code&gt;catch#&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16508&quot;&gt;!16508&lt;/a&gt;),
with accompanying &lt;a href=&quot;https://github.com/haskell/core-libraries-committee/issues/437&quot;&gt;CLC proposal &lt;span&gt;#437&lt;/span&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;trees-that-grow&quot;&gt;Trees That Grow&lt;/h3&gt;
&lt;p&gt;Over a long period, GHC has been gradually migrating to the Trees That Grow
representation for its internal AST datatypes, and improving the modularity of
the AST within the GHC codebase (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/19218&quot;&gt;#19218&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/21592&quot;&gt;#21592&lt;/a&gt;). Once complete, this will make
it possible to expose the AST in a package that does not depend on the rest of
the &lt;code&gt;ghc&lt;/code&gt; library, and unblocks doing the same for the parser. As well as
improving modularity, this will make it easier to use in tools that parse
Haskell syntax, and may unlock future simplifications to &lt;code&gt;template-haskell&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Alex and Rodrigo have been working on the next steps in that process,
reorganising the codebase so that there will be no modules in the
&lt;code&gt;Language.Haskell.*&lt;/code&gt; namespace depending on modules in the &lt;code&gt;GHC.*&lt;/code&gt; namespace.
This included decoupling &lt;code&gt;Language.Haskell.Syntax&lt;/code&gt; from &lt;code&gt;GHC.Hs.Doc&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/15601&quot;&gt;!15601&lt;/a&gt;)
and from &lt;code&gt;GHC.Types.SourceText&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/15573&quot;&gt;!15573&lt;/a&gt;), representing string literals in
the AST with &lt;code&gt;Text&lt;/code&gt; rather than &lt;code&gt;FastString&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/21628&quot;&gt;#21628&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/12846&quot;&gt;!12846&lt;/a&gt;), and decoupling
&lt;code&gt;ModuleName&lt;/code&gt;s used in the AST from those in GHC (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16546&quot;&gt;!16546&lt;/a&gt;). As part of the
original migration strategy, various orphan instances were introduced, which Alex has
been cleaning up (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27755&quot;&gt;#27755&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16299&quot;&gt;!16299&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16610&quot;&gt;!16610&lt;/a&gt;).&lt;/p&gt;
&lt;h3 id=&quot;gitlab-infrastructure-work&quot;&gt;GitLab infrastructure work&lt;/h3&gt;
&lt;p&gt;Keeping Haskell’s infrastructure available has been increasingly challenging over the
past couple of months due to the continued rise in LLM-driven crawling. See
for comparison the &lt;a href=&quot;https://people.kernel.org/monsieuricon/creepy-crawlies&quot;&gt;&lt;em&gt;Creepy crawlies&lt;/em&gt; blog post by Konstantin Ryabitsev&lt;/a&gt;,
detailing the challenges faced by the git hosting infrastructure for the Linux
kernel.&lt;/p&gt;
&lt;p&gt;The GHC GitLab instance relies on &lt;a href=&quot;https://github.com/TecharoHQ/anubis&quot;&gt;Anubis&lt;/a&gt;
to reduce crawler traffic. In early August, Magnus traced a fresh surge in load
to a crawler that was bypassing Anubis entirely. Blocking it cut the load on the
GitLab instance dramatically.&lt;/p&gt;
&lt;p&gt;Other important infrastructure items:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Magnus and Ben upgraded the instance’s PostgreSQL database, after which Ben
carried the GitLab upgrade itself through to the latest release in a single
fourteen-hour session. Besides clearing a long-standing backlog item, this
made it possible to prioritise traffic from logged-in users.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Magnus upgraded the host running the GitLab container, and applied patches
to the GitLab instance (e.g. to close certain security vulnerabilities).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;ghc&quot;&gt;GHC&lt;/h2&gt;
&lt;h3 id=&quot;releases&quot;&gt;Releases&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Andreas posted an &lt;a href=&quot;https://discourse.haskell.org/t/2026-06-09-informal-ghc-release-status-update-of-a-sort/14250&quot;&gt;informal GHC release status update&lt;/a&gt; on Discourse in June.
GHC release progress is officially tracked on the &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/wikis/GHC-Status&quot;&gt;GHC release status page&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Zubin worked on 9.14.2, cutting a &lt;a href=&quot;https://www.haskell.org/ghc/blog/20260730-ghc-9.14.2-rc1-released.html&quot;&gt;first release candidate&lt;/a&gt; at the end of July, and a &lt;a href=&quot;https://www.haskell.org/ghc/blog/20260917-ghc-9.14.2-rc2-released.html&quot;&gt;second release candidate&lt;/a&gt; in September. This work fixed a significant number of bugs, many of them blockers for the next major release, 10.0, which Zubin will prepare in due course.&lt;/li&gt;
&lt;li&gt;Magnus prepared &lt;a href=&quot;https://www.haskell.org/ghc/blog/20260731-ghc-9.12.5-rc3-released.html&quot;&gt;release candidates for 9.12.5&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Andreas continued work backporting patches to the 9.10.4 branch.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;frontend&quot;&gt;Frontend&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Magnus renamed &lt;code&gt;ZonkAny&lt;/code&gt; to &lt;code&gt;UnusedType&lt;/code&gt; and added pretty-printing
logic for it (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16212&quot;&gt;!16212&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27390&quot;&gt;#27390&lt;/a&gt;), so that it no longer leaks confusingly
in HLS output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Magnus refactored the error reporting code for
&lt;code&gt;ExplicitLevelImports&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16195&quot;&gt;!16195&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Adam marked various language extension flags as deprecated (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16145&quot;&gt;!16145&lt;/a&gt;),
implementing part of the categorisation of language extensions (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27329&quot;&gt;#27329&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sam relaxed representation-polymorphism restrictions on newtype
constructor patterns (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16342&quot;&gt;!16342&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/20363&quot;&gt;#20363&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sam fixed a couple of issues with mis-oriented coercions produced by GHC
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16474&quot;&gt;!16474&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27639&quot;&gt;#27639&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16207&quot;&gt;!16207&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27374&quot;&gt;#27374&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sam fixed a crash in the &lt;code&gt;Data.Data&lt;/code&gt; instance for &lt;code&gt;HsCtxt&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16142&quot;&gt;!16142&lt;/a&gt;,
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27359&quot;&gt;#27359&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sam reviewed a patch adding explanations for unsolved &lt;code&gt;Typeable&lt;/code&gt;
constraints (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16351&quot;&gt;!16351&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;core-simplifier&quot;&gt;Core &amp;amp; simplifier&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Andreas stopped the specialiser looping on recursive dictionaries
in &lt;code&gt;interestingDict&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16559&quot;&gt;!16559&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27705&quot;&gt;#27705&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zubin landed various fixes for “Entered absent arg” bugs
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16519&quot;&gt;!16519&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27627&quot;&gt;#27627&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16503&quot;&gt;!16503&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27626&quot;&gt;#27626&lt;/a&gt;), along with fixes for the interaction of
absent fillers with speculative evaluation of dictionaries (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16219&quot;&gt;!16219&lt;/a&gt;),
following an extensive investigation of the crash reported in &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/25924&quot;&gt;#25924&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Following on from Simon Peyton Jones’ earlier refactor of &lt;code&gt;GHC.Core.Utils.mkTick&lt;/code&gt;
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/15494&quot;&gt;!15494&lt;/a&gt;), which Sam helped with, Sam and Magnus tied up several loose ends:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;preserving tick ordering in &lt;code&gt;tickTickedExpr&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16601&quot;&gt;!16601&lt;/a&gt;), fixing a
regression in which source notes were moved past each other;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;allowing more ticks in argument position in &lt;code&gt;rebuild_app&lt;/code&gt;
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16558&quot;&gt;!16558&lt;/a&gt;);&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;stopping ticks being dropped around variables of type &lt;code&gt;IO ()&lt;/code&gt;
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16123&quot;&gt;!16123&lt;/a&gt;), fixing &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27182&quot;&gt;#27182&lt;/a&gt; and &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27225&quot;&gt;#27225&lt;/a&gt;;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;stopping &lt;code&gt;mkTick&lt;/code&gt; in Core Prep from
breaking ANF (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16198&quot;&gt;!16198&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;driver&quot;&gt;Driver&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Rodrigo made some improvements to the structure and caching of downsweep
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16330&quot;&gt;!16330&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27053&quot;&gt;#27053&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27461&quot;&gt;#27461&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;During this work, Rodrigo and Sam found an opportunity to clean up some
legacy Backpack logic, establishing that &lt;code&gt;implicitRequirementsShallow&lt;/code&gt; can
never reach a &lt;code&gt;HoleUnit&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16346&quot;&gt;!16346&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rodrigo deduplicated and shared &lt;code&gt;Module&lt;/code&gt;s in interface files (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16216&quot;&gt;!16216&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16213&quot;&gt;!16213&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27401&quot;&gt;#27401&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes shared &lt;code&gt;UnitInfo&lt;/code&gt;s across the &lt;code&gt;UnitEnv&lt;/code&gt; using a &lt;code&gt;UnitIndex&lt;/code&gt;
and an external unit database cache (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16115&quot;&gt;!16115&lt;/a&gt;), and arranged for the
&lt;code&gt;UnitIndexCache&lt;/code&gt; to be reused after initialising multiple home units (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16598&quot;&gt;!16598&lt;/a&gt;,
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27748&quot;&gt;#27748&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes added the &lt;code&gt;backendInfoTableMapValidity&lt;/code&gt; backend predicate
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16234&quot;&gt;!16234&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes fixed the order of &lt;code&gt;PackageDBFlag&lt;/code&gt;s for the interactive home unit
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16591&quot;&gt;!16591&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27640&quot;&gt;#27640&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes diagnosed the quadratic behaviour of &lt;code&gt;ghc-pkg list&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27275&quot;&gt;#27275&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zubin carried out a substantial overhaul of the loader (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16493&quot;&gt;!16493&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16582&quot;&gt;!16582&lt;/a&gt;,
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16583&quot;&gt;!16583&lt;/a&gt;) to allow more control over unloading stale linkables, which is
useful for HLS to efficiently evaluate expressions as files change in the
IDE (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27606&quot;&gt;#27606&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zubin made &lt;code&gt;UniqDFM.alter&lt;/code&gt; preserve insertion order (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16385&quot;&gt;!16385&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wolfgang finished up a patch sorting &lt;code&gt;Usage&lt;/code&gt;s by fingerprint, to ensure
determinism (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/15484&quot;&gt;!15484&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;code-generation&quot;&gt;Code generation&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Andreas fixed common block elimination silently dropping a procedure’s info
table (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16592&quot;&gt;!16592&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27722&quot;&gt;#27722&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;ghci-and-bytecode&quot;&gt;GHCi and bytecode&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Wolfgang added &lt;code&gt;--show-byte-code&lt;/code&gt;, the counterpart of &lt;code&gt;--show-iface&lt;/code&gt; for
bytecode files (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16386&quot;&gt;!16386&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/26909&quot;&gt;#26909&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;rts&quot;&gt;RTS&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Duncan continued his sustained work to overhaul RTS I/O managers and the
scheduler. One of the main goals is to bring the non-threaded RTS in line
with the threaded one (e.g. &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27335&quot;&gt;#27335&lt;/a&gt;), and to improve maintainability across
I/O manager implementations.
As part of this effort, Duncan landed a collection of I/O manager refactors
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16217&quot;&gt;!16217&lt;/a&gt;), preparing the ground for fixing idle GC and deadlock detection on the
non-threaded RTS. Duncan also brought the POSIX and Win32 ticker implementations
more in line (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16023&quot;&gt;!16023&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Andreas fixed pointer arithmetic in the RTS linker’s
&lt;code&gt;flushInstructionCacheRISCV64&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16419&quot;&gt;!16419&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27569&quot;&gt;#27569&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;build-system&quot;&gt;Build system&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sam made Hadrian check for &lt;code&gt;cabal-install &amp;gt;= 3.12&lt;/code&gt; upfront (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16132&quot;&gt;!16132&lt;/a&gt;,
&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27317&quot;&gt;#27317&lt;/a&gt;), rather than failing obscurely later on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;David fixed the &lt;code&gt;ghc-internal&lt;/code&gt; &lt;code&gt;.def&lt;/code&gt; file name (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16190&quot;&gt;!16190&lt;/a&gt;), removed
unused wrapper scripts from the Windows bindist (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16138&quot;&gt;!16138&lt;/a&gt;), and made Hadrian
avoid response files when the command line is short enough (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16134&quot;&gt;!16134&lt;/a&gt;),
as response files were impeding developer workflows (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27230&quot;&gt;#27230&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;David stopped &lt;code&gt;ghc-toolchain&lt;/code&gt; throwing an exception when candidate executables are
not found (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16173&quot;&gt;!16173&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27369&quot;&gt;#27369&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;testsuite&quot;&gt;Testsuite&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sam made the test driver normalise call-stack line numbers when referring to
locations in libraries (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16247&quot;&gt;!16247&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27387&quot;&gt;#27387&lt;/a&gt;), so that tests are no longer
sensitive to irrelevant internal changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Andreas made the &lt;code&gt;process009&lt;/code&gt; test use &lt;code&gt;SIGKILL&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16429&quot;&gt;!16429&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zubin made various improvements to the JUnit output, most importantly
showing diffs in the GitLab summary (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16309&quot;&gt;!16309&lt;/a&gt;) and reporting fragile failures as
skipped (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16250&quot;&gt;!16250&lt;/a&gt;). He also landed assorted testsuite fixes (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16413&quot;&gt;!16413&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;ci-and-infrastructure&quot;&gt;CI and infrastructure&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Magnus worked on keeping nightly CI passing (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16296&quot;&gt;!16296&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27127&quot;&gt;#27127&lt;/a&gt;),
adding a retry when fetching test metrics (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16281&quot;&gt;!16281&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;David made the &lt;code&gt;lint-changelog&lt;/code&gt; job check for the &lt;code&gt;no-changelog&lt;/code&gt;
label in the script rather than in &lt;code&gt;rules&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16177&quot;&gt;!16177&lt;/a&gt;), which avoids having to
re-run the entire pipeline when applying the &lt;code&gt;no-changelog&lt;/code&gt; label.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zubin arranged for tool messages to be reported as errors or warnings depending
on the tool’s exit code, rather than always as errors (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16170&quot;&gt;!16170&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/issues/27370&quot;&gt;#27370&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;process-library&quot;&gt;&lt;code&gt;process&lt;/code&gt; library&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Zubin fixed link errors on certain macOS configurations by allowing
&lt;code&gt;posix_spawn_file_actions_addchdir&lt;/code&gt; to be missing when linking against
&lt;code&gt;process&lt;/code&gt; on Darwin (&lt;a href=&quot;https://github.com/haskell/process/pull/377&quot;&gt;&lt;span&gt;#377&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/haskell/process/issues/376&quot;&gt;&lt;span&gt;#376&lt;/span&gt;&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;cabal&quot;&gt;Cabal&lt;/h2&gt;
&lt;h3 id=&quot;releases-1&quot;&gt;Releases&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The 3.18.1.0 release was managed by Artem Pelenitsyn, with release engineering
help from Mikolaj (&lt;a href=&quot;https://github.com/haskell/cabal/pull/12104&quot;&gt;&lt;span&gt;#12104&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/haskell/cabal/pull/12128&quot;&gt;&lt;span&gt;#12128&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/haskell/cabal/pull/12016&quot;&gt;&lt;span&gt;#12016&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/haskell/cabal/pull/12010&quot;&gt;&lt;span&gt;#12010&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/haskell/cabal/pull/12017&quot;&gt;&lt;span&gt;#12017&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/haskell/cabal/pull/12108&quot;&gt;&lt;span&gt;#12108&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/haskell/cabal/pull/12113&quot;&gt;&lt;span&gt;#12113&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/haskell/cabal/pull/12182&quot;&gt;&lt;span&gt;#12182&lt;/span&gt;&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;recompilation-checking&quot;&gt;Recompilation checking&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sam stopped &lt;code&gt;Cabal&lt;/code&gt; from writing &lt;code&gt;setup-config&lt;/code&gt; twice
(&lt;a href=&quot;https://github.com/haskell/cabal/pull/11949&quot;&gt;&lt;span&gt;#11949&lt;/span&gt;&lt;/a&gt;), fixing access-denied
failures (&lt;a href=&quot;https://github.com/haskell/cabal/issues/11619&quot;&gt;&lt;span&gt;#11619&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sam made &lt;code&gt;cabal-install&lt;/code&gt; fall back to the external setup method on a
Cabal-library mismatch (&lt;a href=&quot;https://github.com/haskell/cabal/pull/11950&quot;&gt;&lt;span&gt;#11950&lt;/span&gt;&lt;/a&gt;),
fixing stuck rebuilds of &lt;code&gt;build-type: Custom&lt;/code&gt; packages
(&lt;a href=&quot;https://github.com/haskell/cabal/issues/11942&quot;&gt;&lt;span&gt;#11942&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sam allowed preserving the local build artifacts across minor Cabal version
bumps without causing recompilation or errors
(&lt;a href=&quot;https://github.com/haskell/cabal/pull/12289&quot;&gt;&lt;span&gt;#12289&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tobias switched pre-processor recompilation checks to use file monitoring
(&lt;a href=&quot;https://github.com/haskell/cabal/pull/12021&quot;&gt;&lt;span&gt;#12021&lt;/span&gt;&lt;/a&gt;, with a preparatory
refactoring in &lt;a href=&quot;https://github.com/haskell/cabal/pull/12065&quot;&gt;&lt;span&gt;#12065&lt;/span&gt;&lt;/a&gt;).
This fixes an issue with build tools not being re-run when their version
changes (&lt;a href=&quot;https://github.com/haskell/cabal/issues/11411&quot;&gt;&lt;span&gt;#11411&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;haskell-language-server&quot;&gt;Haskell Language Server&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Andrea added whole-project loading
(&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/5002&quot;&gt;&lt;span&gt;#5002&lt;/span&gt;&lt;/a&gt;),
as described in his &lt;a href=&quot;https://well-typed.com/blog/2026/09/whole-project-loading-for-haskell-ide-tooling/&quot;&gt;recent blog post&lt;/a&gt;.
This builds on the groundwork Andrea laid in &lt;code&gt;hie-bios&lt;/code&gt;: new load modes for
upfront loading of a cradle or project
(&lt;a href=&quot;https://github.com/haskell/hie-bios/pull/508&quot;&gt;&lt;span&gt;#508&lt;/span&gt;&lt;/a&gt;) and the
&lt;code&gt;componentsToLoad&lt;/code&gt; field
(&lt;a href=&quot;https://github.com/haskell/hie-bios/pull/515&quot;&gt;&lt;span&gt;#515&lt;/span&gt;&lt;/a&gt;), both reviewed and
merged by Hannes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes and Zubin pushed &lt;a href=&quot;https://github.com/guibou&quot;&gt;Guibou&lt;/a&gt;’s module-to-filename resolution over the finish line
and applied further optimisations to put HLS’s import resolution on par with GHC’s
(&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/4600&quot;&gt;&lt;span&gt;#4600&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zubin made HLS unload linkables when any transitive dependency changes
(&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/5035&quot;&gt;&lt;span&gt;#5035&lt;/span&gt;&lt;/a&gt;),
and stopped HLS unloading all bytecode whenever new bytecode is generated
(&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/5033&quot;&gt;&lt;span&gt;#5033&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes set the &lt;code&gt;FileCache&lt;/code&gt; hook before typechecking
(&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/5030&quot;&gt;&lt;span&gt;#5030&lt;/span&gt;&lt;/a&gt;),
fixing &lt;a href=&quot;https://github.com/haskell/haskell-language-server/issues/4675&quot;&gt;&lt;span&gt;#4675&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes ensured the &lt;code&gt;DocMap&lt;/code&gt; is never computed with stale data
(&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/4936&quot;&gt;&lt;span&gt;#4936&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes tracked down multi-component session failures when expanding response
files (&lt;a href=&quot;https://github.com/haskell/haskell-language-server/issues/5004&quot;&gt;&lt;span&gt;#5004&lt;/span&gt;&lt;/a&gt;),
fixed by whole-project loading.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes substantially improved HLS’s error messages
(&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/5029&quot;&gt;&lt;span&gt;#5029&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;google-summer-of-code-projects&quot;&gt;Google Summer of Code projects&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Magnus, Hannes and Andreas mentored the &lt;a href=&quot;https://gist.github.com/Aster89/69a57c364ec2d0cc89f474cc3d194371&quot;&gt;2026 GSoC project by Enrico Maria De Angelis&lt;/a&gt;,
adding back the case-split plugin to HLS (&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/5014&quot;&gt;&lt;span&gt;#5014&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zubin and Hannes mentored the &lt;a href=&quot;https://gist.github.com/vidit-od/032f03afac51d0e3507b2b9d1bd9a277&quot;&gt;2026 GSoC project by Vidit Odedra&lt;/a&gt;,
adding go-to-definition for third-party dependencies to HLS (&lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/4973&quot;&gt;&lt;span&gt;#4973&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://github.com/haskell/haskell-language-server/pull/5025&quot;&gt;&lt;span&gt;#5025&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;haskell-syntax-highlighting&quot;&gt;Haskell syntax highlighting&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Hannes transferred the VS Code syntax highlighting extension to the
Haskell community (&lt;a href=&quot;https://github.com/haskell/vscode-haskell-syntax-highlighting/pull/256&quot;&gt;&lt;span&gt;#256&lt;/span&gt;&lt;/a&gt;),
fixing a mishap that had caused
the extension to disappear from the VS Code marketplace, breaking syntax
highlighting for many users.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;runtime-observability&quot;&gt;Runtime observability&lt;/h2&gt;
&lt;h3 id=&quot;haskell-debugger&quot;&gt;Haskell Debugger&lt;/h3&gt;
&lt;p&gt;Rodrigo continued to lead development of the
&lt;a href=&quot;https://github.com/well-typed/haskell-debugger&quot;&gt;Haskell Debugger&lt;/a&gt; (&lt;code&gt;hdb&lt;/code&gt;), an
interactive step-through debugger for Haskell, with contributions from Andrea
and Hannes. It also gained a logo, designed by George Thomas (&lt;a href=&quot;https://github.com/well-typed/haskell-debugger/pull/326&quot;&gt;&lt;span&gt;#326&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Hannes and Rodrigo added support for the future release of GHC 10.2 (&lt;a href=&quot;https://github.com/well-typed/haskell-debugger/pull/332&quot;&gt;&lt;span&gt;#332&lt;/span&gt;&lt;/a&gt;, &lt;a href=&quot;https://github.com/well-typed/haskell-debugger/pull/359&quot;&gt;&lt;span&gt;#359&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;hdb&lt;/code&gt; has two mechanisms for listing threads, decoding stacks and collecting
exception information: external compiler commands where GHC supports them,
and a fallback that inspects the heap directly on older GHCs. Rodrigo
separated the two into distinct modules selected by a Cabal version
conditional, rather than interleaving them (&lt;a href=&quot;https://github.com/well-typed/haskell-debugger/pull/356&quot;&gt;&lt;span&gt;#356&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rodrigo made the &lt;code&gt;singleThread&lt;/code&gt; flag of a &lt;em&gt;continue&lt;/em&gt; request optional, as
the Debug Adapter Protocol specifies (&lt;a href=&quot;https://github.com/haskell-debugger/dap/pull/38&quot;&gt;&lt;span&gt;#38&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hannes made the stack frame decoding logic use the source locations that
&lt;code&gt;StackAnnotation&lt;/code&gt; carries from &lt;code&gt;ghc-internal-9.1402&lt;/code&gt; onwards
(&lt;a href=&quot;https://github.com/well-typed/haskell-debugger/pull/333&quot;&gt;&lt;span&gt;#333&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Andrea made each stack frame report the variables bound in its own scope
(&lt;a href=&quot;https://github.com/well-typed/haskell-debugger/pull/355&quot;&gt;&lt;span&gt;#355&lt;/span&gt;&lt;/a&gt;,
&lt;a href=&quot;https://github.com/well-typed/haskell-debugger/issues/160&quot;&gt;&lt;span&gt;#160&lt;/span&gt;&lt;/a&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;live-monitoring&quot;&gt;Live monitoring&lt;/h3&gt;
&lt;p&gt;Wen continued to develop
&lt;a href=&quot;https://github.com/well-typed/eventlog-live/&quot;&gt;&lt;code&gt;eventlog-live&lt;/code&gt;&lt;/a&gt;, which allows
GHC’s eventlog to be monitored as a program runs, streaming telemetry data
from the runtime system to an external observability platform.
One focus was on improving the OpenTelemetry integration.&lt;/p&gt;
&lt;p&gt;Releases 0.6.0.0 through 0.9.0.0 came out this quarter. See the
&lt;a href=&quot;https://hackage.haskell.org/package/eventlog-live/changelog&quot;&gt;&lt;code&gt;eventlog-live&lt;/code&gt; changelog&lt;/a&gt;
for more details.&lt;/p&gt;
&lt;h3 id=&quot;stack-and-ipe-profiling&quot;&gt;Stack and IPE profiling&lt;/h3&gt;
&lt;p&gt;Hannes and Wen worked on packages supporting stack profiling functionality, with
a release of &lt;code&gt;ghc-stack-profiler&lt;/code&gt; 0.4.0.0 (&lt;a href=&quot;https://hackage.haskell.org/package/ghc-stack-profiler/changelog&quot;&gt;changelog&lt;/a&gt;) and &lt;code&gt;ghc-stack-annotations&lt;/code&gt; 0.2.0.0
(&lt;a href=&quot;https://hackage.haskell.org/package/ghc-stack-annotations/changelog&quot;&gt;changelog&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Wen rewrote &lt;code&gt;ipedb&lt;/code&gt;’s LSM-tree database API
(&lt;a href=&quot;https://github.com/well-typed/ipedb/pull/18&quot;&gt;&lt;span&gt;#18&lt;/span&gt;&lt;/a&gt;) and released 0.2.0.0/0.2.0.1
with other improvements (see the &lt;a href=&quot;https://hackage.haskell.org/package/ipedb/changelog&quot;&gt;changelog&lt;/a&gt;).&lt;/p&gt;</description>
	<pubDate>Fri, 18 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>GHC Developer Blog: GHC 9.14.2-rc2 is now available</title>
	<guid isPermaLink="true">http://haskell.org/ghc/blog/20260917-ghc-9.14.2-rc2-released.html</guid>
	<link>http://haskell.org/ghc/blog/20260917-ghc-9.14.2-rc2-released.html</link>
	<description>&lt;h1&gt;GHC 9.14.2-rc2 is now available&lt;/h1&gt;
&lt;h4 class=&quot;text-muted&quot;&gt;wz1000 - 2026-09-17&lt;/h4&gt;

&lt;p&gt;The GHC developers are very pleased to announce the availability
of the second 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-rc2&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/20260730-ghc-9.14.2-rc1-released.html&quot;&gt;previous announcement of 9.14.2-rc1&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The changes since the first release candidate are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Several fixes to the specialiser and demand analyser for miscompilations
causing incorrect runtime results, crashes, or hangs
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27626&quot;&gt;#27626&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27627&quot;&gt;#27627&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27629&quot;&gt;#27629&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27703&quot;&gt;#27703&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27704&quot;&gt;#27704&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27705&quot;&gt;#27705&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27717&quot;&gt;#27717&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for a number of runtime system bugs:
&lt;ul&gt;
&lt;li&gt;a missing memory barrier in the parallel garbage collector, which could
crash or silently corrupt the heap on weakly-ordered architectures such
as AArch64 (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27477&quot;&gt;#27477&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;a use-after-free with TSOs (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26716&quot;&gt;#26716&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26717&quot;&gt;#26717&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;an “unknown/strange object” crash in the compacting collector when
collecting the large data constructors that the bytecode interpreter
allocates (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27649&quot;&gt;#27649&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;a segfault when querying the label of an unlabelled thread (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27618&quot;&gt;#27618&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;a crash when capturing or resuming a delimited continuation that adjusts
the asynchronous exception masking state (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27651&quot;&gt;#27651&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;stack corruption when returning large unboxed tuples in GHCi (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27633&quot;&gt;#27633&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Further fixes to the AArch64 native code generator, addressing bugs that
caused incorrect runtime results from subword operations
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27430&quot;&gt;#27430&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27533&quot;&gt;#27533&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27537&quot;&gt;#27537&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27538&quot;&gt;#27538&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27539&quot;&gt;#27539&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27550&quot;&gt;#27550&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;toException&lt;/code&gt; for &lt;code&gt;SomeException&lt;/code&gt; no longer drops the attached
&lt;code&gt;ExceptionContext&lt;/code&gt;, and a &lt;code&gt;catchSTM&lt;/code&gt; handler no longer breaks &lt;code&gt;retry&lt;/code&gt; or
asynchronous exception delivery (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27455&quot;&gt;#27455&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27657&quot;&gt;#27657&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Reduced memory usage with multiple home units
(&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26423&quot;&gt;#26423&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27308&quot;&gt;#27308&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27500&quot;&gt;#27500&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27748&quot;&gt;#27748&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Fixes for panics and spurious out-of-scope errors involving type arguments
in pattern synonym right-hand sides (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27440&quot;&gt;#27440&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27583&quot;&gt;#27583&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27586&quot;&gt;#27586&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;The pattern-match checker now recognises &lt;code&gt;considerAccessible&lt;/code&gt; under ticks,
and no longer emits a spurious &lt;code&gt;-Wincomplete-uni-patterns&lt;/code&gt; warning under
&lt;code&gt;-finfo-table-map&lt;/code&gt; (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27314&quot;&gt;#27314&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27360&quot;&gt;#27360&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;The deprecation warning for static forms, added in rc1, no longer fires on
free type variables (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27664&quot;&gt;#27664&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for a regression preventing modules from being loaded into GHCi after
startup (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27202&quot;&gt;#27202&lt;/a&gt;, &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27640&quot;&gt;#27640&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;A fix for representation-polymorphism checks producing a coercion that
fails Core Lint (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27639&quot;&gt;#27639&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Support for LLVM up to version 23 (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/27764&quot;&gt;#27764&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Updates for libffi (3.8.0), &lt;code&gt;directory&lt;/code&gt; (1.3.11.0), &lt;code&gt;file-io&lt;/code&gt; (0.1.6),
&lt;code&gt;haskeline&lt;/code&gt; (0.8.5.0) and &lt;code&gt;os-string&lt;/code&gt; (2.0.10).&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-rc2/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 1 October 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, 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 id=&quot;toc&quot;&gt;&lt;h3&gt;Contents&lt;/h3&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>GHC Developer Blog: GHC 9.12.5-rc3 is now available</title>
	<guid isPermaLink="true">http://haskell.org/ghc/blog/20260731-ghc-9.12.5-rc3-released.html</guid>
	<link>http://haskell.org/ghc/blog/20260731-ghc-9.12.5-rc3-released.html</link>
	<description>&lt;h1&gt;GHC 9.12.5-rc3 is now available&lt;/h1&gt;
&lt;h4 class=&quot;text-muted&quot;&gt;mangoiv - 2026-07-31&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-rc3.&lt;/p&gt;
&lt;p&gt;The third release candidate was necessary to mitigate a critical bug on HEAD that needed to be backported. The bugfix is included in this RC.&lt;/p&gt;
&lt;p&gt;See the &lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/-/merge_requests/16249&quot;&gt;backport MR.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;See also the previous announcement of 9.12.5-rc2: https://www.haskell.org/ghc/blog/20260624-ghc-9.12.5-rc2-released.html&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-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;This release candidate will have a two-week testing period. If all goes well
the final release will be available the week of 13 August 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>Fri, 31 Jul 2026 00:00:00 +0000</pubDate>
</item>
<item>
	<title>GHC Developer Blog: GHC 9.14.2-rc1 is now available</title>
	<guid isPermaLink="true">http://haskell.org/ghc/blog/20260730-ghc-9.14.2-rc1-released.html</guid>
	<link>http://haskell.org/ghc/blog/20260730-ghc-9.14.2-rc1-released.html</link>
	<description>&lt;h1&gt;GHC 9.14.2-rc1 is now available&lt;/h1&gt;
&lt;h4 class=&quot;text-muted&quot;&gt;wz1000 - 2026-07-30&lt;/h4&gt;

&lt;p&gt;The GHC developers are very pleased to announce the availability
of the 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-rc1&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 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;Fixed 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;Fixed demand analysis giving an absent demand to an argument that was still
used by the function’s stable unfolding, which could cause a run-time 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 NGG 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 sub-word
widths&lt;/li&gt;
&lt;li&gt;Fixed 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;Fixed 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;Fixed 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;Fixed 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;Fixed 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;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;) and
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;)&lt;/li&gt;
&lt;li&gt;Fixed 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;Fixed negative type literals causing the compiler to hang (&lt;a href=&quot;https://gitlab.haskell.org/ghc/ghc/issues/26861&quot;&gt;#26861&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Fixed 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;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;Fixed split sections support 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;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;Fixes for the Wasm backend, and the RISC-V and PowerPC native code generators&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 13 August 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, 30 Jul 2026 00:00:00 +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>
<item>
	<title>Joey Hess: no LLM code in dependencies</title>
	<guid isPermaLink="true">http://joeyh.name/blog/entry/no_LLM_code_in_dependencies/</guid>
	<link>http://joeyh.name/blog/entry/no_LLM_code_in_dependencies/</link>
	<description>&lt;p&gt;I've spent about 100 hours of work over the past month to make sure
git-annex can build without dependencies that contain LLM generated code.
At least so far.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://git-annex.branchable.com/no_llm_code/&quot;&gt;https://git-annex.branchable.com/no_llm_code/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Needing to review a program's whole dependency tree on an ongoing basis is
apparently what programming has come to?&lt;/p&gt;

&lt;p&gt;I've found some real stinkers. Large LLM generated changes being reverted
in the next release without any explanation. An incoherent 1489 line
commit message with 10,000 lines of changes to a 26,000 LOC code base.
A LLM prompt to copy code from another project that seems to have only
avoided being copyright infringement due to luck.&lt;/p&gt;

&lt;p&gt;I now have additional information about the quality of dependencies
which will surely influence future decisions. As far as I
can see, that's the only positive benefit of this work.&lt;/p&gt;

&lt;p&gt;I realize that I am probably trying to hold back the tide at this point.
That appears to be why Software Freedom Conservancy
&lt;a href=&quot;https://sfconservancy.org/llm-gen-ai/llm-backed-generative-ai-recommendations.html&quot;&gt;punted&lt;/a&gt;,
and I doubt that the FSF will do any better.&lt;/p&gt;

&lt;p&gt;As these dominos fall, I am reconsidering my participation in these
communities. But I continue my work and support my users.&lt;/p&gt;

&lt;p&gt;It may seem easy to prompt a LLM with&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Add fourmolu config and restyled&lt;/p&gt;

&lt;p&gt;neat&lt;/p&gt;

&lt;p&gt;format a module&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;And commit the result and call yourself a 10xer.
But please consider the broader impact of your actions.
(In the above case, that project lost my further collaboration on it.)&lt;/p&gt;</description>
	<pubDate>Thu, 02 Jul 2026 17:58:19 +0000</pubDate>
</item>
<item>
	<title>Chris Reade: Diagrams for Penrose Tiles</title>
	<guid isPermaLink="false">http://readerunner.wordpress.com/?p=81</guid>
	<link>https://readerunner.wordpress.com/2021/09/13/diagrams-for-penrose-tiles/</link>
	<description>&lt;figure&gt;
&lt;img alt=&quot;Figure: leftFilledSun6&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2026/06/leftfilledsun6new.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure: leftFilledSun6&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;&lt;em&gt;Update June 2026&lt;/em&gt; This blog is about drawing finite regions of the infinite non-periodic tessellations of Roger Penrose’s kite and dart tiles. It was first written before developing a Haskell package (PenroseKiteDart) now available on Hackage. More info and developments can be seen at the end. I have made small updates to this blog to keep it compatible with later developments (and it is no longer a complete literate Haskell file).&lt;/p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;As part of a collaboration with Stephen Huggett, working on some mathematical properties of Penrose tilings, I recognised the need for quick renderings of tilings. I thought &lt;em&gt;Haskell diagrams&lt;/em&gt; would be helpful here, and that turned out to be an excellent choice. Two dimensional vectors were well-suited to describing tiling operations and these are included as part of the diagrams package.&lt;/p&gt;
&lt;p&gt;The Haskell below uses the Haskell Diagrams package to draw tilings with kites and darts. It also implements &lt;code&gt;compChoices&lt;/code&gt; and &lt;code&gt;decompPatch&lt;/code&gt; which are used for constructing tilings (explained below).&lt;/p&gt;
&lt;p&gt;Firstly, these 5 lines are needed in Haskell to use the diagrams package:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE NoMonomorphismRestriction #-}&lt;/span&gt;
    &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE FlexibleContexts          #-}&lt;/span&gt;
    &lt;span style=&quot;color: green;&quot;&gt;{-# LANGUAGE TypeFamilies              #-}&lt;/span&gt;

    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; &lt;span&gt;Diagrams.Prelude&lt;/span&gt;
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;import&lt;/span&gt; &lt;span&gt;Diagrams.Backend.SVG.CmdLine&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and we will also import a module for half tiles (explained later)&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; &lt;span&gt;HalfTile&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;legal-tilings&quot;&gt;Legal tilings&lt;/h2&gt;
&lt;p&gt;These are the kite and dart tiles.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure: Kite and Dart&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2021/03/kitedart.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure: Kite and Dart&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The red line marking here on the right hand copies, is purely to illustrate rules about how tiles can be put together for legal (non-periodic) tilings. Obviously edges can only be put together when they have the same length. If all the tiles are marked with red lines as illustrated on the right, the vertices where tiles meet must all have a red line or none must have a red line at that vertex. This prevents us from forming a simple rombus by placing a kite top at the base of a dart and thus enabling periodic tilings.&lt;/p&gt;
&lt;p&gt;All edges are powers of the golden section &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; which we write as &lt;code&gt;phi&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;phi&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt;
    &lt;span&gt;phi&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 class=&quot;hs-num&quot;&gt;1.0&lt;/span&gt; &lt;span&gt;+&lt;/span&gt; &lt;span&gt;sqrt&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;5.0&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span&gt;/&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;2.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So if the shorter edges are unit length, then the longer edges have length &lt;code&gt;phi&lt;/code&gt;. We also have the interesting property of the golden section that &lt;img alt=&quot;phi^2 = phi + 1&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=phi%5E2+%3D+phi+%2B+1&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and so&lt;/p&gt;
&lt;p&gt;&lt;img alt=&quot;1/phi = phi-1&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=1%2Fphi+%3D+phi-1&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;, &lt;img alt=&quot;phi^3 = 2phi +1&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=phi%5E3+%3D+2phi+%2B1&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; and &lt;img alt=&quot;1/phi^2 = 2-phi&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=1%2Fphi%5E2+%3D+2-phi&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;All angles in the figures are multiples of &lt;code&gt;tt&lt;/code&gt; which is &lt;code&gt;36 deg&lt;/code&gt; or &lt;code&gt;1/10 turn&lt;/code&gt;. We use &lt;code&gt;ttangle&lt;/code&gt; to express such angles (e.g 180 degrees is &lt;code&gt;ttangle 5&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;ttangle&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Int&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Angle&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt;
    &lt;span&gt;ttangle&lt;/span&gt; &lt;span&gt;n&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;fromIntegral&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt; &lt;span&gt;`mod`&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;*^&lt;/span&gt;&lt;span&gt;tt&lt;/span&gt;
            &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt; &lt;span&gt;tt&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&gt;/&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;10&lt;/span&gt; &lt;span&gt;@@&lt;/span&gt; &lt;span&gt;turn&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;pieces&quot;&gt;Pieces&lt;/h2&gt;
&lt;p&gt;In order to implement &lt;code&gt;compChoices&lt;/code&gt; and &lt;code&gt;decompPatch&lt;/code&gt;, we need to work with half tiles. We now define these in the separately imported module &lt;code&gt;HalfTile&lt;/code&gt; with constructors for Left Dart, Right Dart, Left Kite, Right Kite&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; &lt;span&gt;HalfTile&lt;/span&gt; &lt;span&gt;rep&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;-- defined in HalfTile module&lt;/span&gt;
        &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;LD&lt;/span&gt; &lt;span&gt;rep&lt;/span&gt; 
        &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; &lt;span&gt;RD&lt;/span&gt; &lt;span&gt;rep&lt;/span&gt;
        &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; &lt;span&gt;LK&lt;/span&gt; &lt;span&gt;rep&lt;/span&gt;
        &lt;span style=&quot;color: red;&quot;&gt;|&lt;/span&gt; &lt;span&gt;RK&lt;/span&gt; &lt;span&gt;rep&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;rep&lt;/code&gt; is a type variable allowing for different representations. However, here, we want to use a more specific type which we call &lt;code&gt;Piece&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; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;HalfTile&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;V2&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here the half tiles have a simple 2D vector representation to provide orientation and scale. (&lt;em&gt;Update 2026&lt;/em&gt;: Originally a single vector was used to represent the join edge of a half tile but now we use a list of two vectors instead).&lt;/p&gt;
&lt;p&gt;The list of two two-dimensional vectors represents two edges of the half tile (excluding the &lt;em&gt;join&lt;/em&gt; edge where half tiles come together). The origin for a dart is the tip, and the origin for a kite is the acute angle tip (marked in the figure with a red dot).&lt;/p&gt;
&lt;p&gt;These are the only 4 pieces we use. They are defined oriented with the join along the x axis. In the figure they are rotated 90 degrees so the join edges are vertical (and they have also been labelled).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;ldart&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;rdart&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;lkite&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;rkite&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt;
    &lt;span&gt;ldart&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&gt;v'&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;v&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;v'&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;where&lt;/span&gt; &lt;span&gt;v&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;unitX&lt;/span&gt;
                                   &lt;span&gt;v'&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt;&lt;span&gt;*^&lt;/span&gt;&lt;span&gt;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ttangle&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&gt;v&lt;/span&gt;
    &lt;span&gt;rdart&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&gt;v'&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;v&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;v'&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;where&lt;/span&gt; &lt;span&gt;v&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;unitX&lt;/span&gt;
                                   &lt;span&gt;v'&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt;&lt;span&gt;*^&lt;/span&gt;&lt;span&gt;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ttangle&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;v&lt;/span&gt;
    &lt;span&gt;lkite&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&gt;v'&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;v&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;v'&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;where&lt;/span&gt; &lt;span&gt;v&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt;&lt;span&gt;*^&lt;/span&gt;&lt;span&gt;unitX&lt;/span&gt;
                                   &lt;span&gt;v'&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ttangle&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;v&lt;/span&gt;
    &lt;span&gt;rkite&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&gt;v'&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;v&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;v'&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;where&lt;/span&gt; &lt;span&gt;v&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt;&lt;span&gt;*^&lt;/span&gt;&lt;span&gt;unitX&lt;/span&gt;
                                   &lt;span&gt;v'&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ttangle&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&gt;v&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure: Four HalfTile Pieces&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2026/06/newpiecesfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure: Four HalfTile Pieces&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Perhaps confusingly, we regard left and right of a dart differently from left and right of a kite when viewed from the origin. The diagram shows the right dart before the left dart and the left kite before the right kite. Thus in a complete tile, going clockwise round the origin the right dart comes before the left dart, but the left kite comes before the right kite.&lt;/p&gt;
&lt;p&gt;When it comes to drawing pieces, for the simplest case, we just want to draw the two tile edges of each piece (and not the join edge). These are the &lt;code&gt;drawnEdges&lt;/code&gt; of the tile. We can also use the &lt;code&gt;joinVector&lt;/code&gt; of the tile (which is simply the vector sum of the drawn edges) The drawnEdges are ordered starting from the origin of each piece.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;drawnEdges&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&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;&lt;span&gt;V2&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    &lt;span&gt;joinVector&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;V2&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now drawing lines for the 2 outer edges of a piece we have&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;drawPiece&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;drawPiece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;strokeLine&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;fromOffsets&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;drawnEdges&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or just the join edge&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;drawJoin&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;drawJoin&lt;/span&gt; &lt;span&gt;piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;strokeLine&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;fromOffsets&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;joinVector&lt;/span&gt; &lt;span&gt;piece&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or drawing all 3 edges round a piece&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;drawRoundPiece&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;drawRoundPiece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;strokeLoop&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;closeLine&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;fromOffsets&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;drawnEdges&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sometimes we want two drawn edges but a faint dashed line for the join&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;drawjPiece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;drawjPiece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;drawPiece&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;lw&lt;/span&gt; &lt;span&gt;ultraThin&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;joinDashing&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;drawJoin&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;where&lt;/span&gt; &lt;span&gt;joinDashing&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;normalized&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0.003&lt;/span&gt;  &lt;span&gt;`atLeast`&lt;/span&gt; &lt;span&gt;output&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To fill half tile pieces, we can use &lt;code&gt;fillOnlyPiece&lt;/code&gt; which fills without showing edges of a half tile (by using line width none).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;fillOnlyPiece&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Colour&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;fillOnlyPiece&lt;/span&gt; &lt;span&gt;col&lt;/span&gt; &lt;span&gt;piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;drawRoundPiece&lt;/span&gt; &lt;span&gt;piece&lt;/span&gt; &lt;span&gt;#&lt;/span&gt; &lt;span&gt;fc&lt;/span&gt; &lt;span&gt;col&lt;/span&gt; &lt;span&gt;#&lt;/span&gt; &lt;span&gt;lw&lt;/span&gt; &lt;span&gt;none&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We also use &lt;code&gt;fillPieceDK&lt;/code&gt; which fills darts and kites with given colours and also draws edges using drawPiece.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;fillPieceDK&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Colour&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Colour&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;fillPieceDK&lt;/span&gt; &lt;span&gt;dcol&lt;/span&gt; &lt;span&gt;kcol&lt;/span&gt; &lt;span&gt;piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;drawPiece&lt;/span&gt; &lt;span&gt;piece&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span&gt;fillOnlyPiece&lt;/span&gt; &lt;span&gt;col&lt;/span&gt; &lt;span&gt;piece&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
            &lt;span&gt;col&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;case&lt;/span&gt; &lt;span&gt;piece&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;of&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: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;&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&gt;dcol&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: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;&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&gt;dcol&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: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;&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&gt;kcol&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: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;&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&gt;kcol&lt;/span&gt;     &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For an alternative fill operation on whole tiles, we calculated a list of the 4 tile edges of a completed half-tile piece clockwise from the origin of the tile. This allows colour filling a whole tile, but it does rely on prior transformations preserving angles as it uses angles in the definition (not shown).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;wholeTileEdges&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&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;&lt;span&gt;V2&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To fill whole tiles with colours, darts with &lt;code&gt;dcol&lt;/code&gt; and kites with &lt;code&gt;kcol&lt;/code&gt; we can now use &lt;code&gt;leftFillPieceDK&lt;/code&gt;. This uses only the left pieces to identify the whole tile and ignores right pieces so that a tile is not filled twice and uses &lt;code&gt;wholeTileEdges&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;leftFillPieceDK&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Colour&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Colour&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;leftFillPieceDK&lt;/span&gt; &lt;span&gt;dcol&lt;/span&gt; &lt;span&gt;kcol&lt;/span&gt; &lt;span&gt;c&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;case&lt;/span&gt; &lt;span&gt;c&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;of&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: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;&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;&lt;span&gt;strokeLoop&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;glueLine&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;fromOffsets&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;wholeTileEdges&lt;/span&gt; &lt;span&gt;c&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;  &lt;span&gt;#&lt;/span&gt; &lt;span&gt;fc&lt;/span&gt; &lt;span&gt;dcol&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: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;&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;&lt;span&gt;strokeLoop&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;glueLine&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;fromOffsets&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;wholeTileEdges&lt;/span&gt; &lt;span&gt;c&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;  &lt;span&gt;#&lt;/span&gt; &lt;span&gt;fc&lt;/span&gt; &lt;span&gt;kcol&lt;/span&gt;
      &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;_&lt;/span&gt;      &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;mempty&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By making &lt;code&gt;Pieces&lt;/code&gt; transformable we can reuse generic transform operations. These 4 lines of code are required to do this&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; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;instance&lt;/span&gt; &lt;span&gt;N&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;HalfTile&lt;/span&gt; &lt;span&gt;a&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;N&lt;/span&gt; &lt;span&gt;a&lt;/span&gt;
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;type&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;instance&lt;/span&gt; &lt;span&gt;V&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;HalfTile&lt;/span&gt; &lt;span&gt;a&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;V&lt;/span&gt; &lt;span&gt;a&lt;/span&gt;
    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;instance&lt;/span&gt; &lt;span&gt;Transformable&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;Transformable&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;HalfTile&lt;/span&gt; &lt;span&gt;a&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;where&lt;/span&gt;
        &lt;span&gt;transform&lt;/span&gt; &lt;span&gt;t&lt;/span&gt; &lt;span&gt;ht&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;fmap&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;transform&lt;/span&gt; &lt;span&gt;t&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span&gt;ht&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So we can scale and rotate a piece by an angle (positive rotations are in the anticlockwise direction) but we cannote translate until they are located (as Patches).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;scale&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt;
    &lt;span&gt;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Angle&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;patches&quot;&gt;Patches&lt;/h2&gt;
&lt;p&gt;A patch is a list of located pieces (each with a 2D point)&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; &lt;span&gt;Patch&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;Located&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To turn a whole patch into a diagram using some function &lt;code&gt;pd&lt;/code&gt; for drawing the pieces, we use &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;    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span&gt;Drawable&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
        &lt;span&gt;drawWith&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;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;&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&gt;a&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;

    &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;instance&lt;/span&gt; &lt;span&gt;Drawable&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
        &lt;span&gt;drawWith&lt;/span&gt; &lt;span&gt;pd&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;position&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;fmap&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;viewLoc&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;mapLoc&lt;/span&gt; &lt;span&gt;pd&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here &lt;code&gt;mapLoc&lt;/code&gt; applies a function to the piece in a located piece – producing a located diagram in this case, and &lt;code&gt;viewLoc&lt;/code&gt; returns the pair of point and diagram from a located diagram. Finally &lt;code&gt;position&lt;/code&gt; forms a single diagram from the list of pairs of points and diagrams. (The use of a class definition here anticipates more instances in later developments).&lt;/p&gt;
&lt;p&gt;We now define the most common special cases:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;draw&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Drawable&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;draw&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;drawWith&lt;/span&gt; &lt;span&gt;drawPiece&lt;/span&gt;
    
    &lt;span&gt;drawj&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Drawable&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;drawj&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;drawWith&lt;/span&gt; &lt;span&gt;drawjPiece&lt;/span&gt;

    &lt;span&gt;fillDK&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Drawable&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span&gt;Colour&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Colour&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;fillDK&lt;/span&gt; &lt;span&gt;c1&lt;/span&gt; &lt;span&gt;c2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;drawWith&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;fillPieceDK&lt;/span&gt; &lt;span&gt;c1&lt;/span&gt; &lt;span&gt;c2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Patches are automatically inferred to be transformable, so we can also scale a patch, translate a patch by a vector, and rotate a patch by an angle (for example).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;scale&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt;
    &lt;span&gt;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Angle&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt;
    &lt;span&gt;translate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;V2&lt;/span&gt; &lt;span&gt;Double&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As an aid to creating patches with 5-fold rotational symmetry, we combine 5 copies of a basic patch (rotated by multiples of &lt;code&gt;ttangle 2&lt;/code&gt; successively).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;penta&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt;
    &lt;span&gt;penta&lt;/span&gt; &lt;span&gt;p&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;concatMap&lt;/span&gt; &lt;span&gt;copy&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0&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: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt; &lt;span&gt;copy&lt;/span&gt; &lt;span&gt;n&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ttangle&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&gt;*&lt;/span&gt;&lt;span&gt;n&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;p&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This must be used with care to avoid nonsense patches. But two special cases are&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;sun&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;star&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt;         
    &lt;span&gt;sun&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt;  &lt;span&gt;penta&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;rkite&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;origin&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;lkite&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;origin&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    &lt;span&gt;star&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;penta&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;rdart&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;origin&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;ldart&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;origin&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This figure shows some example patches, drawn with &lt;code&gt;draw&lt;/code&gt; The first is a &lt;code&gt;star&lt;/code&gt; and the second is a &lt;code&gt;sun&lt;/code&gt;.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure: Tile Patches&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2021/03/tilepatches.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure: Tile Patches&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The tools so far for creating patches may seem limited (and do not help with ensuring legal tilings), but there is an even bigger problem.&lt;/p&gt;
&lt;h2 id=&quot;correct-tilings&quot;&gt;Correct Tilings&lt;/h2&gt;
&lt;p&gt;Unfortunately, correct tilings – that is, tilings which can be extended to infinity – are not as simple as just legal tilings. It is not enough to have a legal tiling, because an apparent (legal) choice of placing one tile can have non-local consequences, causing a conflict with a choice made far away in a patch of tiles, resulting in a patch which cannot be extended. This suggests that constructing correct patches is far from trivial.&lt;/p&gt;
&lt;p&gt;The infinite number of possible infinite tilings do have some remarkable properties. Any finite patch from one of them, will occur in all the others (infinitely many times) and within a relatively small radius of any point in an infinite tiling. (For details of this see links at the end).&lt;/p&gt;
&lt;p&gt;This is why we need a different approach to constructing larger patches. There are two significant processes used for creating patches, namely &lt;code&gt;inflate&lt;/code&gt; (also called &lt;em&gt;compose&lt;/em&gt;) and &lt;code&gt;decompose&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To understand these processes, take a look at the following figure.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure: Experiment&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2021/03/experiment.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure: Experiment&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;Here the &lt;em&gt;small&lt;/em&gt; pieces have been drawn in an unusual way. The edges have been drawn with dashed lines, but long edges of kites have been emphasised with a solid line and the join edges of darts marked with a red line. From this you may be able to make out a patch of larger scale kites and darts. This is an inflated patch arising from the smaller scale patch. Conversely, the larger kites and darts decompose to the smaller scale ones.&lt;/p&gt;
&lt;h2 id=&quot;decomposition&quot;&gt;Decomposition&lt;/h2&gt;
&lt;p&gt;Since the rule for decomposition is uniquely determined, we can express it as a simple function on patches.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;decompPatch&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Patch&lt;/span&gt;
    &lt;span&gt;decompPatch&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;concatMap&lt;/span&gt; &lt;span&gt;decompPiece&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where the function &lt;code&gt;decompPiece&lt;/code&gt; acts on located pieces and produces a list of the smaller located pieces contained in the piece. For example, a larger right dart will produce both a smaller right dart and a smaller left kite. Decomposing a located piece also takes care of the location, scale and rotation of the new pieces. (&lt;em&gt;Revised version 2026 avoiding use of angles&lt;/em&gt;).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;decompPiece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Located&lt;/span&gt; &lt;span&gt;Piece&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;&lt;span&gt;Located&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    &lt;span&gt;decompPiece&lt;/span&gt; &lt;span&gt;lp&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;case&lt;/span&gt; &lt;span&gt;viewLoc&lt;/span&gt; &lt;span&gt;lp&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;of&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&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&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z2&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: red;&quot;&gt;-&amp;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&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;p&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&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;negate&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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;where&lt;/span&gt; &lt;span&gt;s1&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;phi&lt;/span&gt;&lt;span style=&quot;color: green;&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&gt;*^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;
              &lt;span&gt;s2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;sumV&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&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&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z2&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: red;&quot;&gt;-&amp;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&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;p&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&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;negate&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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;where&lt;/span&gt; &lt;span&gt;s1&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;phi&lt;/span&gt;&lt;span style=&quot;color: green;&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&gt;*^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;
              &lt;span&gt;s2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;sumV&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&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&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z2&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: red;&quot;&gt;-&amp;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&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;p&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&gt;s4&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;negate&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;s3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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;where&lt;/span&gt; &lt;span&gt;j&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;sumV&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
              &lt;span&gt;s1&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;phi&lt;/span&gt;&lt;span style=&quot;color: green;&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&gt;*^&lt;/span&gt; &lt;span&gt;j&lt;/span&gt;
              &lt;span&gt;s2&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 class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span&gt;phi&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt;
              &lt;span&gt;s3&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;phi&lt;/span&gt;&lt;span style=&quot;color: green;&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&gt;*^&lt;/span&gt; &lt;span&gt;j&lt;/span&gt;
              &lt;span&gt;s4&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 class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span&gt;phi&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&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&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z2&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: red;&quot;&gt;-&amp;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&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;p&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&gt;s4&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;negate&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;s3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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;where&lt;/span&gt; &lt;span&gt;j&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;sumV&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
              &lt;span&gt;s1&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;phi&lt;/span&gt;&lt;span style=&quot;color: green;&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&gt;*^&lt;/span&gt; &lt;span&gt;j&lt;/span&gt;
              &lt;span&gt;s2&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 class=&quot;hs-num&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span&gt;phi&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt;
              &lt;span&gt;s3&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;phi&lt;/span&gt;&lt;span style=&quot;color: green;&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&gt;*^&lt;/span&gt; &lt;span&gt;j&lt;/span&gt;
              &lt;span&gt;s4&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 class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-&lt;/span&gt;&lt;span&gt;phi&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;
      &lt;span&gt;other&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;error&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;&quot;decompPiece: &quot;&lt;/span&gt; &lt;span&gt;++&lt;/span&gt; &lt;span&gt;show&lt;/span&gt; &lt;span&gt;other&lt;/span&gt; &lt;span&gt;++&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;&quot;/n&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is illustrated in the following figure for the cases of a right dart and a right kite.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure: Decomposition and Composition&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2026/06/decompexplainnewfig.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure: Decomposition and Composition&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p&gt;The symmetric diagrams for left pieces are easy to work out from these, so they are not illustrated.&lt;/p&gt;
&lt;p&gt;With the &lt;code&gt;decompPatch&lt;/code&gt; operation we can start with a simple correct patch, and decompose repeatedly to get more and more detailed patches. (Each decomposition scales the tiles down by a factor of &lt;img alt=&quot;1/phi&quot; class=&quot;latex&quot; src=&quot;https://s0.wp.com/latex.php?latex=1%2Fphi&amp;amp;bg=ffffff&amp;amp;fg=444444&amp;amp;s=0&amp;amp;c=20201002&quot; /&gt; but we can rescale at any time.)&lt;/p&gt;
&lt;p&gt;This figure illustrates how each piece decomposes with 4 decomposition steps below each one.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure: Four Decompositions of Pieces&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2026/06/fourdecompsnew.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure: Four Decompositions of Pieces&lt;/figcaption&gt;&lt;/figure&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;thePieces&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;ldart&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;rdart&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;lkite&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;rkite&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;  
    &lt;span&gt;fourDecomps&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;hsep&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;fmap&lt;/span&gt; &lt;span&gt;decomps&lt;/span&gt; &lt;span&gt;thePieces&lt;/span&gt; &lt;span&gt;#&lt;/span&gt; &lt;span&gt;lw&lt;/span&gt; &lt;span&gt;thin&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
        &lt;span&gt;decomps&lt;/span&gt; &lt;span&gt;pc&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;vsep&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;fmap&lt;/span&gt; &lt;span&gt;drawj&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;take&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;5&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span&gt;decompositionsP&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;pc&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;origin&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have made use of the fact that we can create an infinite list of finer and finer decompositions of any patch, using:&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;decompositionsP&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Patch&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;&lt;span&gt;Patch&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    &lt;span&gt;decompositionsP&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;iterate&lt;/span&gt; &lt;span&gt;decompPatch&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We could get the n-fold decomposition of a patch as just the nth item in a list of decompositions.&lt;/p&gt;
&lt;p&gt;For example, here is an infinite list of decomposed versions of &lt;code&gt;sun&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;suns&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;decompositionsP&lt;/span&gt; &lt;span&gt;sun&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The coloured tiling shown at the beginning is simply 6 decompositions of &lt;code&gt;sun&lt;/code&gt; displayed using &lt;code&gt;leftFillPieceDK&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;leftFilledSun6&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;leftFilledSun6&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;drawWith&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;leftFillPieceDK&lt;/span&gt; &lt;span&gt;red&lt;/span&gt; &lt;span&gt;darkviolet&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;)&lt;/span&gt; &lt;span&gt;sun6&lt;/span&gt; &lt;span&gt;#&lt;/span&gt; &lt;span&gt;lw&lt;/span&gt; &lt;span&gt;thin&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The earlier figure illustrating larger kites and darts emphasised from the smaller ones is also &lt;code&gt;suns!!6&lt;/code&gt; but this time pieces are drawn with &lt;code&gt;experiment&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;experimentFig&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;drawWith&lt;/span&gt; &lt;span&gt;experiment&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;suns&lt;/span&gt;&lt;span&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&gt;#&lt;/span&gt; &lt;span&gt;lw&lt;/span&gt; &lt;span&gt;thin&lt;/span&gt;

    &lt;span&gt;experiment&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;experiment&lt;/span&gt; &lt;span&gt;pc&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;emph&lt;/span&gt; &lt;span&gt;pc&lt;/span&gt; &lt;span&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;drawRoundPiece&lt;/span&gt; &lt;span&gt;pc&lt;/span&gt; &lt;span&gt;#&lt;/span&gt; &lt;span&gt;dashingN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.002&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0.002&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt; &lt;span&gt;#&lt;/span&gt; &lt;span&gt;lw&lt;/span&gt; &lt;span&gt;ultraThin&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;where&lt;/span&gt; &lt;span&gt;emph&lt;/span&gt; &lt;span&gt;pc&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;case&lt;/span&gt; &lt;span&gt;pc&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;of&lt;/span&gt;
              &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;LD&lt;/span&gt; &lt;span&gt;v&lt;/span&gt;&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;&lt;span&gt;strokeLine&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;fromOffsets&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;v&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;#&lt;/span&gt; &lt;span&gt;lc&lt;/span&gt; &lt;span&gt;red&lt;/span&gt;   &lt;span style=&quot;color: green;&quot;&gt;-- emphasise join edge of darts in red&lt;/span&gt;
              &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;RD&lt;/span&gt; &lt;span&gt;v&lt;/span&gt;&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;&lt;span&gt;strokeLine&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;fromOffsets&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;v&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;#&lt;/span&gt; &lt;span&gt;lc&lt;/span&gt; &lt;span&gt;red&lt;/span&gt; 
              &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;LK&lt;/span&gt; &lt;span&gt;v&lt;/span&gt;&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;&lt;span&gt;strokeLine&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;fromOffsets&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;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ttangle&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;v&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;-- emphasise long edge for kites&lt;/span&gt;
              &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;RK&lt;/span&gt; &lt;span&gt;v&lt;/span&gt;&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;&lt;span&gt;strokeLine&lt;/span&gt; &lt;span&gt;.&lt;/span&gt; &lt;span&gt;fromOffsets&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;rotate&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ttangle&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&gt;v&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;compose-choices&quot;&gt;Compose Choices&lt;/h2&gt;
&lt;p&gt;You might expect composition (also called &lt;em&gt;inflation&lt;/em&gt;) to be a kind of inverse to decomposition, but it is a bit more complicated than that. With our current representation of pieces, we can only compose single pieces. This amounts to embedding the piece into a larger piece that matches how the larger piece decomposes. There is thus a choice at each composition step as to which of several possibilities we select as the larger half-tile. We represent this choice as a list of alternatives. This list should not be confused with a Patch. It only makes sense to select one of the alternatives giving a new single piece.&lt;/p&gt;
&lt;p&gt;The earlier diagram illustrating how decompositions are calculated also shows the two choices for embedding a right dart into either a right kite or a larger right dart. There will be two symmetric choices for a left dart, and three choices for left and right kites.&lt;/p&gt;
&lt;p&gt;Once again we work with located pieces to ensure the resulting larger piece contains the original in its original position in a decomposition. (&lt;em&gt;Revised version 2026 avoiding use of angles&lt;/em&gt;).&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;compChoices&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Located&lt;/span&gt; &lt;span&gt;Piece&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;&lt;span&gt;Located&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    &lt;span&gt;compChoices&lt;/span&gt; &lt;span&gt;lp&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;case&lt;/span&gt; &lt;span&gt;viewLoc&lt;/span&gt; &lt;span&gt;lp&lt;/span&gt; &lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;of&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&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&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s2&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: red;&quot;&gt;-&amp;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&gt;negate&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;  &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;p&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;where&lt;/span&gt; &lt;span&gt;z1&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;phi&lt;/span&gt;&lt;span&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&gt;*^&lt;/span&gt; &lt;span&gt;sumV&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
                &lt;span&gt;z2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&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&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s2&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: red;&quot;&gt;-&amp;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&gt;negate&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;  &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;p&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;where&lt;/span&gt; &lt;span&gt;z1&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;phi&lt;/span&gt;&lt;span&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&gt;*^&lt;/span&gt; &lt;span&gt;sumV&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
                &lt;span&gt;z2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&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&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s2&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: red;&quot;&gt;-&amp;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&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;  &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;p&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&gt;negate&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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&gt;negate&lt;/span&gt; &lt;span&gt;z&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z&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;where&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt;
                &lt;span&gt;z&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt; &lt;span&gt;^+^&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;phi&lt;/span&gt;&lt;span&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&gt;*^&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt;
                &lt;span&gt;z2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;sumV&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;
                &lt;span&gt;z3&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt; &lt;span&gt;^+^&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;z2&lt;/span&gt;
      &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&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&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s2&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: red;&quot;&gt;-&amp;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&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;z2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;  &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;p&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&gt;negate&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;z3&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z1&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&gt;negate&lt;/span&gt; &lt;span&gt;z&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt; &lt;span&gt;.+^&lt;/span&gt; &lt;span&gt;z&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;where&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt;
                &lt;span&gt;z&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;s1&lt;/span&gt; &lt;span&gt;^+^&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;phi&lt;/span&gt;&lt;span&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&gt;*^&lt;/span&gt; &lt;span&gt;s2&lt;/span&gt;
                &lt;span&gt;z2&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;sumV&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt;&lt;span&gt;s1&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;,&lt;/span&gt;&lt;span&gt;s2&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt; &lt;span&gt;^-^&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt;
                &lt;span&gt;z3&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;z1&lt;/span&gt; &lt;span&gt;^+^&lt;/span&gt; &lt;span&gt;phi&lt;/span&gt; &lt;span&gt;*^&lt;/span&gt; &lt;span&gt;z2&lt;/span&gt;
      &lt;span&gt;other&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;error&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;&quot;compChoices: &quot;&lt;/span&gt; &lt;span&gt;++&lt;/span&gt; &lt;span&gt;show&lt;/span&gt; &lt;span&gt;other&lt;/span&gt; &lt;span&gt;++&lt;/span&gt; &lt;span style=&quot;color: teal;&quot;&gt;&quot;/n&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As the result is a list of alternatives, we need to select one to do further inflations. We can express all the alternatives after n steps as &lt;code&gt;compNChoices n&lt;/code&gt; where&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;compNChoices&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Int&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span&gt;Located&lt;/span&gt; &lt;span&gt;Piece&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;&lt;span&gt;Located&lt;/span&gt; &lt;span&gt;Piece&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    &lt;span&gt;compNChoices&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt; &lt;span&gt;lp&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;lp&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;]&lt;/span&gt;
    &lt;span&gt;compNChoices&lt;/span&gt; &lt;span&gt;n&lt;/span&gt; &lt;span&gt;lp&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;do&lt;/span&gt;
        &lt;span&gt;lp'&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span&gt;inflate&lt;/span&gt; &lt;span&gt;lp&lt;/span&gt;
        &lt;span&gt;inflations&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span style=&quot;color: green;&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&gt;lp'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This figure illustrates 5 consecutive choices for composing a left dart to produce a left kite. On the left, the finishing piece is shown with the starting piece embedded, and on the right the 5-fold decomposition of the result is shown.&lt;/p&gt;
&lt;figure&gt;
&lt;img alt=&quot;Figure: Five Composition Choices&quot; src=&quot;https://readerunner.wordpress.com/wp-content/uploads/2021/03/fiveinflate.png?w=625&quot; /&gt;&lt;figcaption&gt;Figure: Five Composition Choices&lt;/figcaption&gt;&lt;/figure&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;fiveCompChoices&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;hsep&lt;/span&gt; &lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt; &lt;span&gt;$&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;[&lt;/span&gt; &lt;span&gt;draw&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&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span&gt;draw&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 style=&quot;color: red;&quot;&gt;,&lt;/span&gt; &lt;span&gt;draw&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;(&lt;/span&gt;&lt;span&gt;decompositionsP&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&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 style=&quot;color: blue; font-weight: bold;&quot;&gt;where&lt;/span&gt;
      &lt;span&gt;ld&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;ldart&lt;/span&gt; &lt;span&gt;`at`&lt;/span&gt; &lt;span&gt;origin&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&gt;compChoices&lt;/span&gt; &lt;span&gt;ld&lt;/span&gt;  &lt;span&gt;!!&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;
      &lt;span&gt;rk&lt;/span&gt;  &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;compChoices&lt;/span&gt; &lt;span&gt;lk&lt;/span&gt;  &lt;span&gt;!!&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;
      &lt;span&gt;rk'&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;compChoices&lt;/span&gt; &lt;span&gt;rk&lt;/span&gt;  &lt;span&gt;!!&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;2&lt;/span&gt;
      &lt;span&gt;ld'&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;compChoices&lt;/span&gt; &lt;span&gt;rk'&lt;/span&gt; &lt;span&gt;!!&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;0&lt;/span&gt;
      &lt;span&gt;lk'&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;compChoices&lt;/span&gt; &lt;span&gt;ld'&lt;/span&gt; &lt;span&gt;!!&lt;/span&gt;&lt;span class=&quot;hs-num&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, at the end of this haskell program we choose which figure to draw as output.&lt;/p&gt;
&lt;pre class=&quot;sourceCode haskell&quot;&gt;&lt;code class=&quot;sourceCode haskell&quot;&gt;    &lt;span&gt;fig&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;::&lt;/span&gt; &lt;span&gt;Diagram&lt;/span&gt; &lt;span&gt;B&lt;/span&gt;
    &lt;span&gt;fig&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;leftFilledSun6&lt;/span&gt;

    &lt;span&gt;main&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;=&lt;/span&gt; &lt;span&gt;mainWith&lt;/span&gt; &lt;span&gt;fig&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s it. But, &lt;em&gt;What about composing whole patches?&lt;/em&gt;, I hear you ask. Unfortunately we need to answer questions like what pieces are adjacent to a piece in a patch and whether there is a corresponding other half for a piece. These cannot be done with our simple vector representations. We would need some form of planar graph representation, which is much more involved. That is another story which can be found in these subsequent blogs.&lt;/p&gt;
&lt;ul&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;li&gt;&lt;a href=&quot;https://readerunner.wordpress.com/2024/04/08/penrosekitedart-user-guides/&quot;&gt;PenroseKiteDart User Guide&lt;/a&gt; for the PenroseKiteDart package.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Source code for library version of the above code (as part of the PenroseKiteDart package on &lt;a href=&quot;https://hackage.haskell.org/package/PenroseKiteDart&quot;&gt;Hackage&lt;/a&gt;) can be seen at &lt;a href=&quot;https://github.com/chrisreade/PenroseKiteDart&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Many thanks to Stephen Huggett for his inspirations concerning the tilings.&lt;/p&gt;
&lt;h2 id=&quot;further-reading-on-penrose-tilings&quot;&gt;Further reading on Penrose Tilings&lt;/h2&gt;
&lt;p&gt;As well as the Wikipedia entry &lt;a href=&quot;https://en.wikipedia.org/wiki/Penrose_tiling&quot;&gt;Penrose Tilings&lt;/a&gt; I recommend two articles in Scientific American from 2005 by David Austin &lt;a href=&quot;http://www.ams.org/publicoutreach/feature-column/fcarc-penrose&quot;&gt;Penrose Tiles Talk Across Miles&lt;/a&gt; and &lt;a href=&quot;http://www.ams.org/publicoutreach/feature-column/fcarc-ribbons&quot;&gt;Penrose Tilings Tied up in Ribbons&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;There is also a very interesting article by Roger Penrose himself: Penrose R &lt;em&gt;Tilings and quasi-crystals; a non-local growth problem?&lt;/em&gt; in Aperiodicity and Order 2, edited by Jarich M, Academic Press, 1989.&lt;/p&gt;
&lt;p&gt;More information about the diagrams package can be found from the home page &lt;a href=&quot;https://diagrams.github.io&quot;&gt;Haskell diagrams&lt;/a&gt;&lt;/p&gt;</description>
	<pubDate>Tue, 30 Jun 2026 10:40:55 +0000</pubDate>
</item>
<item>
	<title>Haskell Interlude: 84: Sylvain Henry</title>
	<guid isPermaLink="false">Buzzsprout-19412450</guid>
	<link></link>
	<description>&lt;p&gt;In this episode of the Haskell Interlude, we are joined by Sylvain Henry, one of the all-time top contributors to GHC. He tells us about his work on GHC, the bignum library, modularization, and the secret to becoming a top contributor!&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description>
	<pubDate>Mon, 29 Jun 2026 09:00:00 +0000</pubDate>
        <enclosure url="https://www.buzzsprout.com/1817535/episodes/19412450-84-sylvain-henry.mp3" length="37162978" type="audio/mpeg"/>
</item>
<item>
	<title>Jeremy Gibbons: Progress on the book</title>
	<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=440</guid>
	<link>https://patternsinfp.wordpress.com/2026/06/27/progress-on-the-book/</link>
	<description>&lt;figure class=&quot;wp-block-image size-large&quot;&gt;&lt;a href=&quot;https://patternsinfp.wordpress.com/wp-content/uploads/2026/06/cover-kekub.jpg&quot;&gt;&lt;img alt=&quot;&quot; class=&quot;wp-image-448&quot; height=&quot;1023&quot; src=&quot;https://patternsinfp.wordpress.com/wp-content/uploads/2026/06/cover-kekub.jpg?w=724&quot; width=&quot;724&quot; /&gt;&lt;/a&gt;&lt;/figure&gt;



&lt;p class=&quot;wp-block-paragraph&quot;&gt;So, evidently I failed to fulfill my ambition to blog regularly about the contents of my planned book on Patterns in Functional Programming. But I have been making progress. I had the privilege of another sabbatical 2024-2025, in which I managed to draft the entire book. &lt;/p&gt;



&lt;p class=&quot;wp-block-paragraph&quot;&gt;It’s in short chapters, following the example set by &lt;a href=&quot;https://www.cs.cornell.edu/~kozen/&quot;&gt;Dexter Kozen&lt;/a&gt; in his lovely books: the idea is that each chapter is roughly one lecture’s worth of material. I had 40 to 50 chapter ideas, and 40 to 50 weeks in my sabbatical, so diligently stuck to one chapter per week for a yearâ€”if this week’s chapter wasn’t finished by the end of the week, it was put aside anyway in order to move on to the next chapter the following week. &lt;/p&gt;



&lt;p class=&quot;wp-block-paragraph&quot;&gt;That did mean that although I ended the year with a draft of the entire book, it did have many gaps and to-dos remaining. Reality hit at the end of my sabbatical in October 2025, and it has taken me the best part of another year around actual responsibilities to fill in most of the gaps and knock off most of the to-dos. I have also had the benefit of a number of readers (thank you, everyone!), with many helpful comments to implement.&lt;/p&gt;



&lt;p class=&quot;wp-block-paragraph&quot;&gt;But I have just yesterday sent a complete polished version to the publisher, Cambridge University Press, for the input of a professional copyeditor. I still have to construct an index, write solutions for the many exercises (which may appear separately from the book itself, in order to keep the length and hence the cost down), and make my own proof-reading pass. I am hoping to complete those tasks and implement the copyeditor’s eventual corrections over the summer, and that the book will finally appear before the end of 2026.&lt;/p&gt;



&lt;p class=&quot;wp-block-paragraph&quot;&gt;The image is my mock-up of the cover of the book. The painting is &lt;a href=&quot;https://en.vasarely.hu/artworks/15371/&quot;&gt;Kekub&lt;/a&gt; by &lt;a href=&quot;https://en.wikipedia.org/wiki/Victor_Vasarely&quot;&gt;Victor Vasarely&lt;/a&gt;. There is a &lt;a href=&quot;https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/pfp-toc-provisional.pdf&quot;&gt;provisional table of contents&lt;/a&gt; on my webpage.&lt;/p&gt;</description>
	<pubDate>Sat, 27 Jun 2026 10:13:08 +0000</pubDate>
</item>
<item>
	<title>Brent Yorgey: Proving the Fundamental Theorem of Arithmetic in Agda</title>
	<guid isPermaLink="true">http://byorgey.github.io/blog/posts/2026/06/26/FTA.lagda.html</guid>
	<link>http://byorgey.github.io/blog/posts/2026/06/26/FTA.lagda.html</link>
	<description>&lt;h1&gt;Proving the Fundamental Theorem of Arithmetic in Agda&lt;/h1&gt;

&lt;div class=&quot;info&quot;&gt;
  Posted on June 26, 2026
  
  
  &lt;br /&gt;
  Tagged &lt;a href=&quot;https://byorgey.github.io/tag/agda.html&quot; rel=&quot;tag&quot; title=&quot;All pages tagged 'agda'.&quot;&gt;agda&lt;/a&gt;, &lt;a href=&quot;https://byorgey.github.io/tag/arithmetic.html&quot; rel=&quot;tag&quot; title=&quot;All pages tagged 'arithmetic'.&quot;&gt;arithmetic&lt;/a&gt;, &lt;a href=&quot;https://byorgey.github.io/tag/theorem.html&quot; rel=&quot;tag&quot; title=&quot;All pages tagged 'theorem'.&quot;&gt;theorem&lt;/a&gt;, &lt;a href=&quot;https://byorgey.github.io/tag/proof.html&quot; rel=&quot;tag&quot; title=&quot;All pages tagged 'proof'.&quot;&gt;proof&lt;/a&gt;, &lt;a href=&quot;https://byorgey.github.io/tag/Agda.html&quot; rel=&quot;tag&quot; title=&quot;All pages tagged 'Agda'.&quot;&gt;Agda&lt;/a&gt;
  
&lt;/div&gt;

&lt;a href=&quot;https://share.joinmastodon.org/#text=Proving the Fundamental Theorem of Arithmetic in Agda https://byorgey.github.io/blog/posts/2026/06/26/FTA.lagda.html&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Share on &lt;/a&gt;
&lt;section&gt;
&lt;p&gt;&lt;strong&gt;tl;dr&lt;/strong&gt;: This is a fully commentated, from-scratch proof of the
Fundamental Theorem of Arithmetic in
&lt;a href=&quot;https://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.HomePage&quot;&gt;Agda&lt;/a&gt;,
intended for those who already know a bit of Agda but might benefit
from reading and working through a larger example. See the
Introduction and the &lt;a href=&quot;https://byorgey.github.io/blog/rss#table-of-contents&quot;&gt;Table of Contents&lt;/a&gt; below for more details.&lt;/p&gt;
&lt;section class=&quot;level2&quot; id=&quot;introduction&quot;&gt;
&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Earlier this spring, I was idly brainstorming potential final projects
for my &lt;a href=&quot;https://hendrix-cs.github.io/csci365/&quot;&gt;Functional Programming&lt;/a&gt;
students. Having just &lt;a href=&quot;http://ozark.hendrix.edu/~yorgey/forest/00EG/index.xml&quot;&gt;taught my Discrete Math students the
Fundamental Theorem of
Arithmetic&lt;/a&gt;, I
wondered whether formalizing&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-0&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-0&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;I mean formalizing it &lt;em&gt;from scratch&lt;/em&gt;: of
course, the FTA is &lt;a href=&quot;https://agda.github.io/agda-stdlib/v2.3/Data.Nat.Primality.Factorisation.html&quot;&gt;already in the Agda standard library&lt;/a&gt;. As I later
learned, it was added to the standard library by &lt;a href=&quot;https://github.com/Taneb&quot;&gt;Nathan van Doorn,
aka Taneb&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt; it in
&lt;a href=&quot;https://wiki.portal.chalmers.se/agda/pmwiki.php?n=Main.HomePage&quot;&gt;Agda&lt;/a&gt;
could make a nice project.&lt;/p&gt;
&lt;p&gt;So I decided to spend about an hour trying to prove it in Agda,
to gauge the level of the project. At the end of an hour, I had
learned two things: (1) proving the Fundamental Theorem of Arithmetic is &lt;em&gt;not&lt;/em&gt; an appropriate project for
my students (who had only had a few weeks’ practice with Agda); (2) I
was not going to be able to stop until I finished the proof myself!&lt;/p&gt;
&lt;p&gt;Over the next week or so, I finished the proof completely from
scratch—without using anything from the standard library, and without
looking up any reference material. I based it only on my experience
in Agda, knowledge of the relevant proofs on an informal level, and
Agda techniques I’ve picked up along the way (from &lt;em&gt;e.g.&lt;/em&gt; Conor McBride, Jacques
Carette, colleagues at Penn, and elsewhere).&lt;/p&gt;
&lt;p&gt;I decided to publish the proof, with extra commentary, in the hopes
that it can be useful as an intermediate-level reference. That is,
perhaps you’ve learned some basic Agda (if not, I suggest &lt;a href=&quot;https://agda.readthedocs.io/en/latest/getting-started/a-taste-of-agda.html&quot;&gt;this
tutorial to
start&lt;/a&gt;)
and have some basic familiarity
with the Curry-Howard correspondence, but would benefit from seeing an
example of a fully worked out, medium-sized proof.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-1&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-1&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;Another good
source of information along these lines is &lt;a href=&quot;https://jesper.sikanda.be/posts/formalize-all-the-things.html&quot;&gt;this post by Jesper
Cockx&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt; The resulting blog
post is extremely long, but I make no apologies for that—if you want
an entertaining 5-minute read, you should look elsewhere!&lt;/p&gt;
&lt;p&gt;The entire blog post and proof is &lt;a href=&quot;https://github.com/byorgey/blog/blob/main/posts/2026/06/26/FTA.lagda.md&quot;&gt;available as a literate Agda
document&lt;/a&gt;. &lt;strong&gt;Even better, I have also published &lt;a href=&quot;http://ozark.hendrix.edu/~yorgey/pub/FTA-holes.lagda.md&quot;&gt;another version of this
blog post with holes in place of almost all the proofs&lt;/a&gt;.&lt;/strong&gt; For a
maximal learning experience, I suggest downloading it and trying to
fill in the holes yourself as you go along.&lt;/p&gt;
&lt;p&gt;Below is a table of contents. Depending on your background, you may
of course choose to skip some sections. For example, if you have
already had a good deal of practice dealing with basic natural number
arithmetic, equality, and inequality in Agda, you might wish to skip
over those sections.&lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;table-of-contents&quot;&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#half-of-the-fundamental-theorem-of-arithmetic-constructively&quot;&gt;(Half of) The Fundamental Theorem of Arithmetic (Constructively)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#preliminaries&quot;&gt;Preliminaries&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#basic-logic&quot;&gt;Basic logic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#equality&quot;&gt;Equality&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#natural-numbers&quot;&gt;Natural numbers&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#no-confusion&quot;&gt;No confusion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#decidable-equality&quot;&gt;Decidable equality&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#addition&quot;&gt;Addition&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#multiplication&quot;&gt;Multiplication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#inequality&quot;&gt;Inequality&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#relationships-among-equality-and-inequality&quot;&gt;Relationships among equality and inequality&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#arithmetic-and-inequality&quot;&gt;Arithmetic and inequality&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#divisibility-primes-and-composites&quot;&gt;Divisibility, primes, and composites&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#nontrivial-divisors-come-in-pairs&quot;&gt;Nontrivial divisors come in pairs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#division&quot;&gt;Division&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#absolute-difference&quot;&gt;Absolute difference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#quotient-and-remainder-are-unique&quot;&gt;Quotient and remainder are unique&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#the-division-algorithm-take-1&quot;&gt;The division algorithm, take 1&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#construct-the-evidence-you-would-like-to-pattern-match-on&quot;&gt;Construct the evidence you would like to pattern-match on&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#well-founded-induction&quot;&gt;Well-founded induction&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#accessibility&quot;&gt;Accessibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#well-founded-induction-defined&quot;&gt;Well-founded induction, defined&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#less-than-is-well-founded&quot;&gt;Less-than is well-founded&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#the-division-algorithm&quot;&gt;The division algorithm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#primality-testing&quot;&gt;Primality testing&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#counting-up&quot;&gt;Counting up&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#primality-testing-by-trial-division&quot;&gt;Primality testing by trial division&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#lists&quot;&gt;Lists&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#the-fundamental-theorem-of-arithmetic&quot;&gt;The Fundamental Theorem of Arithmetic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#further-directions&quot;&gt;Further Directions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;half-of-the-fundamental-theorem-of-arithmetic-constructively&quot;&gt;
&lt;h2&gt;(Half of) The Fundamental Theorem of Arithmetic (Constructively)&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic&quot;&gt;Fundamental Theorem of
Arithmetic&lt;/a&gt;
(&lt;em&gt;FTA&lt;/em&gt; for short) states that any natural number &lt;span class=&quot;math inline&quot;&gt;\(n \geq 1\)&lt;/span&gt; can be
written as a product of zero or more primes, and moreover that this
product is unique up to permutation.&lt;/p&gt;
&lt;p&gt;For now, we are only going to prove the &lt;em&gt;existence&lt;/em&gt; part (I may write
another blog post with the uniqueness proof later). Since a &lt;em&gt;constructive&lt;/em&gt;
existence proof is really an algorithm for constructing the thing that
is claimed to exist, this can also be seen as a &lt;em&gt;formally verified
factorization program&lt;/em&gt;: put any number in, get a prime factorization
out. Writing a prime factorization program is not hard, of course;
it’s the formal verification part that is interesting!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Stop!&lt;/strong&gt; Before reading on, if you want to get the most out of this tutorial, I
&lt;strong&gt;strongly recommend &lt;a href=&quot;http://ozark.hendrix.edu/~yorgey/pub/FTA-holes.lagda.md&quot;&gt;downloading the version with
holes&lt;/a&gt;&lt;/strong&gt;
and trying to complete as many of the proofs as you can before reading mine!&lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;preliminaries&quot;&gt;
&lt;h2&gt;Preliminaries&lt;/h2&gt;
&lt;p&gt;We will often make use of &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; to stand for arbitrary
sets/types, so we use a &lt;code&gt;variable&lt;/code&gt; declaration to tell Agda that it
should implicitly quantify them whenever they show up as free
variables. That way we don’t have to write &lt;code&gt;{A B : Set} → ...&lt;/code&gt; all the time.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb1&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb1-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb1-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;variable&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb1-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb1-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  A B &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;basic-logic&quot;&gt;
&lt;h2&gt;Basic logic&lt;/h2&gt;
&lt;p&gt;Since we’re building this completely from scratch, we start with some
types to represent basic logical building blocks (via the
&lt;a href=&quot;https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence&quot;&gt;Curry-Howard correspondence&lt;/a&gt;). First, the “top” type &lt;code&gt;⊤&lt;/code&gt; to stand for truth, &lt;em&gt;i.e.&lt;/em&gt; a proposition with trivial evidence:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb2&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb2-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb2-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; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb2-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb2-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  tt &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ⊤&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;tt&lt;/code&gt; is declared to be the one and only value of type &lt;code&gt;⊤&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that some things we define here—such as &lt;code&gt;⊤&lt;/code&gt;—will have the same
names as they do in the &lt;a href=&quot;https://agda.github.io/agda-stdlib/v2.3/&quot;&gt;Agda standard library&lt;/a&gt;. However, many things
won’t, since I either didn’t know the standard name and made up my
own, or (in a few cases) did know the standard name but didn’t like
it, and made up my own anyway.&lt;/p&gt;
&lt;p&gt;Next, the “bottom” type &lt;code&gt;⊥&lt;/code&gt; with no constructors, representing
falsity, along with a corresponding elimination principle, &lt;code&gt;absurd&lt;/code&gt;. The elimination
principle says that anything follows from &lt;code&gt;⊥&lt;/code&gt; (“&lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Principle_of_explosion&quot;&gt;ex falso
quodlibet&lt;/a&gt;&lt;/em&gt;”), and is implemented using Agda’s absurd pattern,
written &lt;code&gt;()&lt;/code&gt;. If Agda can tell that there are no possible
constructors which could give rise to a value of a certain type, we
can pattern-match on it with &lt;code&gt;()&lt;/code&gt;, and are absolved of providing a right-hand side for the definition in that case.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb3&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb3-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb3-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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://byorgey.github.io/blog/rss#cb3-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;absurd &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ⊥ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; A&lt;/span&gt;
&lt;span id=&quot;cb3-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb3-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;absurd &lt;span class=&quot;ot&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can now define negation as an implication to &lt;code&gt;⊥&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb4&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb4-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb4-1&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;Set&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb4-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb4-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;¬ P &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; P &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ⊥&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Dependent pairs are next: a pair of values where the &lt;em&gt;type&lt;/em&gt; of the
second component can depend on the &lt;em&gt;value&lt;/em&gt; of the first. That is, a value of type &lt;code&gt;Σ A B&lt;/code&gt; is a value &lt;code&gt;a&lt;/code&gt; of type
&lt;code&gt;A&lt;/code&gt; paired with a value of type &lt;code&gt;B a&lt;/code&gt;. Via Curry-Howard, this is used
to represent existential quantification: a (constructive) proof of
&lt;span class=&quot;math inline&quot;&gt;\(\exists a : A.\; B(a)\)&lt;/span&gt; is a value &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; of type &lt;span class=&quot;math inline&quot;&gt;\(A\)&lt;/span&gt; (the &lt;em&gt;witness&lt;/em&gt;)
paired with a proof that &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; has property &lt;span class=&quot;math inline&quot;&gt;\(B\)&lt;/span&gt; (&lt;em&gt;i.e.&lt;/em&gt; a value of type &lt;span class=&quot;math inline&quot;&gt;\(B(a)\)&lt;/span&gt;).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb5&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb5-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb5-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixr&lt;/span&gt; &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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb5-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;ot&quot;&gt;(&lt;/span&gt;A &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;B &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb5-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb5-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;,&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B a &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Σ A B&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We also define a projection function (we only end up needing &lt;code&gt;fst&lt;/code&gt;;
defining &lt;code&gt;snd&lt;/code&gt; is left as an exercise for the reader&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-2&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-2&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;The &lt;em&gt;definition&lt;/em&gt;
of &lt;code&gt;snd&lt;/code&gt; is trivial; writing down its &lt;em&gt;type&lt;/em&gt; is a worthwhile
exercise.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;), along with a type of non-dependent pairs, corresponding
to logical conjunction (and).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb6&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb6-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb6-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fst &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;∀&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;A B&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Σ A B &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; A&lt;/span&gt;
&lt;span id=&quot;cb6-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb6-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fst &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a , &lt;span class=&quot;ot&quot;&gt;_)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; a&lt;/span&gt;
&lt;span id=&quot;cb6-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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://byorgey.github.io/blog/rss#cb6-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixr&lt;/span&gt; &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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb6-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;×&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;A B &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb6-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb6-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;A × B &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Σ A &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, we define a disjoint (tagged) union type corresponding to
logical disjunction (or).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb7&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb7-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb7-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixr&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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb7-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;ot&quot;&gt;_&lt;/span&gt;⊎&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;A B &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb7-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb7-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  inj₁ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; A ⊎ B&lt;/span&gt;
&lt;span id=&quot;cb7-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb7-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  inj₂ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; B &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; A ⊎ B&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;equality&quot;&gt;
&lt;h2&gt;Equality&lt;/h2&gt;
&lt;p&gt;Next, we write down the standard equality (&lt;em&gt;aka&lt;/em&gt; identity, &lt;em&gt;aka&lt;/em&gt; path) type, with a single
constructor &lt;code&gt;refl&lt;/code&gt; that witnesses when its two arguments are
identical.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-3&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-3&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;It still seems somewhat magical to me that this seemingly
too-simple definition encapsulates everything we want in an equality
relation (well, &lt;a href=&quot;https://martinescardo.github.io/TypeTopology/UnivalenceFromScratch.html&quot;&gt;almost everything&lt;/a&gt;).&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt; We also define a convenient
synonym for inequality.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb8&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb8-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb8-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infix&lt;/span&gt; &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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb8-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;ot&quot;&gt;_&lt;/span&gt;≡&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb8-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  refl &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; a ≡ a&lt;/span&gt;
&lt;span id=&quot;cb8-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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://byorgey.github.io/blog/rss#cb8-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≢&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb8-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb8-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;x ≢ y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ¬ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ≡ y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Besides reflexivity, equality enjoys various properties that we will
need: symmetry, transitivity, and congruence (&lt;em&gt;i.e.&lt;/em&gt;, we can apply any
function to both sides of an equation).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb9&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb9-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb9-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sym &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y ≡ x&lt;/span&gt;
&lt;span id=&quot;cb9-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb9-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sym refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb9-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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://byorgey.github.io/blog/rss#cb9-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;trans &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y ≡ z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ z&lt;/span&gt;
&lt;span id=&quot;cb9-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb9-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;trans refl y≡z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; y≡z&lt;/span&gt;
&lt;span id=&quot;cb9-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb9-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb9-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb9-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;cong &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;f &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; f x ≡ f y&lt;/span&gt;
&lt;span id=&quot;cb9-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb9-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;cong &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since we will spend a good amount of time reasoning about equality, it
is worthwhile building up some machinery for writing more readable
equality proofs. Instead of writing, say,&lt;/p&gt;
&lt;pre class=&quot;text&quot;&gt;&lt;code&gt;trans p (trans q (trans (sym r) s))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;we will be able to instead write equality proofs like so:&lt;/p&gt;
&lt;pre class=&quot;text&quot;&gt;&lt;code&gt;begin
  v      ≡[ p ⟩≡
  w      ≡[ q ⟩≡
  x      ≡⟨ r ]≡
  y      ≡[ s ⟩≡
  z      ∎&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The intention is that this proof shows &lt;code&gt;v ≡ z&lt;/code&gt;, by first using &lt;code&gt;p&lt;/code&gt; to
show that &lt;code&gt;v ≡ w&lt;/code&gt;, then &lt;code&gt;q&lt;/code&gt; to show &lt;code&gt;w ≡ x&lt;/code&gt;, and so on. This notation
is one of my favorite applications of Agda’s &lt;a href=&quot;https://agda.readthedocs.io/en/latest/language/mixfix-operators.html&quot;&gt;mixfix operator
syntax&lt;/a&gt;,
and has several benefits:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We can avoid nested parentheses when chaining uses of transitivity.&lt;/li&gt;
&lt;li&gt;We can automatically apply symmetry by using a left-pointing
instead of right-pointing operator.&lt;/li&gt;
&lt;li&gt;We get to explicitly mention (and have Agda check for us) all the
intermediate values, making it easier to write the proof
incrementally, and much easier for humans to read.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is one of the places where I deliberately chose different
operator names than the standard library, which uses &lt;code&gt;_≡⟨_⟩_&lt;/code&gt; and
&lt;code&gt;_≡⟨_⟨_&lt;/code&gt;. The operator names I decided to use are &lt;a href=&quot;https://personal.cis.strath.ac.uk/conor.mcbride/PolyTest.pdf&quot;&gt;inspired by Conor
McBride&lt;/a&gt;. I just like the way they look better.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb12&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb12-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infix&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; begin&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://byorgey.github.io/blog/rss#cb12-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;begin&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y&lt;/span&gt;
&lt;span id=&quot;cb12-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;begin x≡y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; x≡y&lt;/span&gt;
&lt;span id=&quot;cb12-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixr&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;ot&quot;&gt;_&lt;/span&gt;⟩≡&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≡[&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;⟩≡&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ≡ y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y ≡ z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ≡ z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ≡[ x≡y ⟩≡ y≡z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; trans x≡y y≡z&lt;/span&gt;
&lt;span id=&quot;cb12-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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://byorgey.github.io/blog/rss#cb12-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixr&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;ot&quot;&gt;_&lt;/span&gt;]≡&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≡⟨&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;]≡&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y ≡ x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y ≡ z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ≡ z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb12-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ≡⟨ y≡x ]≡ y≡z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym y≡x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y≡z&lt;/span&gt;
&lt;span id=&quot;cb12-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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://byorgey.github.io/blog/rss#cb12-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixr&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;∎&lt;/span&gt;
&lt;span id=&quot;cb12-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;∎ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ x&lt;/span&gt;
&lt;span id=&quot;cb12-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb12-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ∎ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, a few &lt;a href=&quot;https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Prelude.html#t:Applicative&quot;&gt;Applicative&lt;/a&gt;-like operators for more conveniently
writing common forms of congruence. For example, instead of writing
&lt;code&gt;cong f x≡y&lt;/code&gt;, we can write &lt;code&gt;f $≡ x≡y&lt;/code&gt;; or to use congruence on both
arguments of a two-place function at once, we can write &lt;code&gt;f $≡ x≡y ≡$≡ z≡w&lt;/code&gt;. (These operators were also inspired by Conor.)&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb13&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb13-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixl&lt;/span&gt; &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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;$≡&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;f &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; f x ≡ f y&lt;/span&gt;
&lt;span id=&quot;cb13-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;f $≡ x≡y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; cong f x≡y&lt;/span&gt;
&lt;span id=&quot;cb13-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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://byorgey.github.io/blog/rss#cb13-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixl&lt;/span&gt; &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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≡$&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;f g &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; f ≡ g &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; f x ≡ g x&lt;/span&gt;
&lt;span id=&quot;cb13-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;f≡g ≡$ x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; cong &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; h &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; h x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; f≡g&lt;/span&gt;
&lt;span id=&quot;cb13-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixl&lt;/span&gt; &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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb13-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≡$≡&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;f g &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; f ≡ g &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; f x ≡ g y&lt;/span&gt;
&lt;span id=&quot;cb13-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb13-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;f≡g ≡$≡ x≡y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;f≡g ≡$ &lt;span class=&quot;ot&quot;&gt;_)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(_&lt;/span&gt; $≡ x≡y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;natural-numbers&quot;&gt;
&lt;h2&gt;Natural numbers&lt;/h2&gt;
&lt;p&gt;Of course, we will need a type to represent the natural numbers. We
can also tell Agda that our natural number type should correspond to its
built-in notion of natural numbers, so we can use numeric literals
like &lt;code&gt;2 : ℕ&lt;/code&gt; instead of having to write &lt;code&gt;suc (suc zero)&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb14&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb14-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb14-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; &lt;span class=&quot;dt&quot;&gt;Set&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://byorgey.github.io/blog/rss#cb14-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  zero &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;/span&gt;
&lt;span id=&quot;cb14-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb14-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  suc &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ&lt;/span&gt;
&lt;span id=&quot;cb14-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb14-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;pp&quot;&gt;{-# BUILTIN NATURAL ℕ #-}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;section class=&quot;level3&quot; id=&quot;no-confusion&quot;&gt;
&lt;h3&gt;No confusion&lt;/h3&gt;
&lt;p&gt;For our natural number type—and often, for any algebraic data type—we
need to know that the constructors are&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;&lt;em&gt;disjoint&lt;/em&gt;, meaning that different constructors always generate
different values (so it’s a contradiction to have an equality
between values built with different constructors); and&lt;/li&gt;
&lt;li&gt;&lt;em&gt;injective&lt;/em&gt;, meaning that if we have an equality between values built
with the same constructor, we can decompose it into equalities between
the components.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We can prove both of these simultaneously using a property called “no
confusion”. This property and its name is well-known in the
literature; for example, see
&lt;a href=&quot;http://strictlypositive.org/concon.ps.gz&quot;&gt;McBridge&lt;/a&gt; or &lt;a href=&quot;https://link.springer.com/chapter/10.1007/3-540-61780-9_64&quot;&gt;Cornes +
Terrasse&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For natural numbers &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;, the type &lt;code&gt;NoConf m n&lt;/code&gt; should be
thought of as the type of evidence that &lt;code&gt;m ≡ n&lt;/code&gt;, based on looking at
the top-level constructors of &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;. If &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; have different
constructors, then no evidence can possibly show that they are equal,
so &lt;code&gt;NoConf m n = ⊥&lt;/code&gt; in that case. If &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; are both zero, then
they are evidently equal, so &lt;code&gt;NoConf 0 0 = ⊤&lt;/code&gt;. Otherwise, if &lt;code&gt;m&lt;/code&gt; and
&lt;code&gt;n&lt;/code&gt; are both successors, &lt;code&gt;NoConf m n&lt;/code&gt; reduces to a proof of equality
between their predecessors.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb15&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb15-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb15-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;NoConf &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb15-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb15-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;NoConf zero zero &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://byorgey.github.io/blog/rss#cb15-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;NoConf zero &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ⊥&lt;/span&gt;
&lt;span id=&quot;cb15-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb15-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;NoConf &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc m&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; zero &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://byorgey.github.io/blog/rss#cb15-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;NoConf &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc m&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; m ≡ n&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can prove the no confusion lemma for our natural number type,
which says that &lt;code&gt;NoConf m n&lt;/code&gt; always holds whenever &lt;code&gt;m ≡ n&lt;/code&gt;.
Since &lt;code&gt;m ≡ n&lt;/code&gt;, we only have to deal with the cases when &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;
are both zero or both a successor—but this also justifies assigning a
type of &lt;code&gt;⊥&lt;/code&gt; to the cases when the constructors do not match. &lt;code&gt;noConf&lt;/code&gt;
can therefore be used to strip &lt;code&gt;suc&lt;/code&gt; from both sides of an equation,
&lt;em&gt;or&lt;/em&gt; to derive a contradiction when we have an equation between
non-matching constructors.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb16&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb16-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb16-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;noConf &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≡ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; NoConf m n&lt;/span&gt;
&lt;span id=&quot;cb16-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb16-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;noConf &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; tt&lt;/span&gt;
&lt;span id=&quot;cb16-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb16-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;noConf &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc m&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As an aside, this definition of the no confusion property uses a
technique I like: defining a &lt;em&gt;type&lt;/em&gt; starting with a capital letter,
then defining a &lt;em&gt;term&lt;/em&gt; that returns that type starting with a
lowercase letter. This pattern will come up again later. Sometimes
we define named types in this way just for convenience, say, to be
able to refer to the type multiple times in a concise way; or, as in
the above case, sometimes the type is actually defined via some
nontrivial computation.&lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;level3&quot; id=&quot;decidable-equality&quot;&gt;
&lt;h3&gt;Decidable equality&lt;/h3&gt;
&lt;p&gt;We can now show how to decide equality of natural numbers. We first
define a simple type representing decidability in general: &lt;code&gt;Dec P&lt;/code&gt; represents
either a proof of &lt;code&gt;P&lt;/code&gt;, or a proof of &lt;code&gt;¬ P&lt;/code&gt;.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-4&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-4&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;You may be aware that the
&lt;a href=&quot;https://en.wikipedia.org/wiki/Law_of_excluded_middle&quot;&gt;law of excluded middle&lt;/a&gt;, which
says that &lt;span class=&quot;math inline&quot;&gt;\(P \lor \neg P\)&lt;/span&gt; for all propositions &lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt;, is rejected in constructive logic. However, even
though &lt;span class=&quot;math inline&quot;&gt;\(P \lor \neg P\)&lt;/span&gt; does not hold for &lt;em&gt;all&lt;/em&gt; &lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt;, it can still hold
for certain specific propositions. Propositions &lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt; for which &lt;span class=&quot;math inline&quot;&gt;\(P \lor
\neg P\)&lt;/span&gt; holds constructively are called &lt;em&gt;decidable&lt;/em&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt; (The &lt;a href=&quot;https://agda.github.io/agda-stdlib/v2.3/Relation.Nullary.Decidable.Core.html#1966&quot;&gt;standard library
version is much more sophisticated&lt;/a&gt;, but this simple version will do
just fine.)&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb17&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb17-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb17-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; Dec &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;P &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb17-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb17-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  yes &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; P &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Dec P&lt;/span&gt;
&lt;span id=&quot;cb17-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb17-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  no &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ¬ P &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Dec P&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can then prove that for any natural numbers &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, we can decide
whether &lt;code&gt;x ≡ y&lt;/code&gt;. Notice the several different uses of the no
confusion lemma: two to handle impossible situations, and one to strip
&lt;code&gt;suc&lt;/code&gt; off both sides of an equality.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb18&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb18-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb18-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≟&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Dec &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ≡ y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb18-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb18-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero ≟ zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; yes refl&lt;/span&gt;
&lt;span id=&quot;cb18-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb18-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero ≟ suc y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; no noConf&lt;/span&gt;
&lt;span id=&quot;cb18-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb18-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;suc x ≟ zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; no noConf&lt;/span&gt;
&lt;span id=&quot;cb18-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb18-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;suc x ≟ suc y &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; x ≟ y&lt;/span&gt;
&lt;span id=&quot;cb18-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb18-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; yes x≡y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; yes &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc $≡ x≡y&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://byorgey.github.io/blog/rss#cb18-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; no x≢y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; no &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; sx≡sy &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x≢y &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;noConf sx≡sy&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;addition&quot;&gt;
&lt;h2&gt;Addition&lt;/h2&gt;
&lt;p&gt;We next turn to defining addition (by pattern-matching on the
left-hand argument), along with several properties of
addition we will need: zero is a right identity for addition; we can
pull out a &lt;code&gt;suc&lt;/code&gt; from the right-hand argument; and addition is
commutative, associative, and left-cancellable.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb19&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb19-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixl&lt;/span&gt; &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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;+&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &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://byorgey.github.io/blog/rss#cb19-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero + y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; y&lt;/span&gt;
&lt;span id=&quot;cb19-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;suc x + y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;+0 &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n + &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ≡ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero +0 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb19-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; +0 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n +0&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;+suc&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + suc y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ≡ suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero +suc y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb19-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; +suc y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x +suc y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-comm &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x + y ≡ y + x&lt;/span&gt;
&lt;span id=&quot;cb19-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-comm zero y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sym &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y +0&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-16&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-comm &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;+-comm x y&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y +suc x&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-17&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-18&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-assoc &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; + z ≡ x + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y + z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-19&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-assoc zero y z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb19-20&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-assoc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;+-assoc x y z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-21&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb19-22&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-cancelˡ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x + y ≡ x + z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y ≡ z&lt;/span&gt;
&lt;span id=&quot;cb19-23&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-cancelˡ zero y z x+y≡x+z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; x+y≡x+z&lt;/span&gt;
&lt;span id=&quot;cb19-24&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb19-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+-cancelˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y z x+y≡x+z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; +-cancelˡ x y z &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;noConf x+y≡x+z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;multiplication&quot;&gt;
&lt;h2&gt;Multiplication&lt;/h2&gt;
&lt;p&gt;Multiplication is next: we start by defining the multiplication
operation (by pattern-matching on the left-hand argument) and proving
a few lemmas about multiplying by known arguments on the right. The
proof of &lt;code&gt;*suc&lt;/code&gt; is the most involved proof we have seen yet, but it
ultimately just comes down to algebra, and we can make good use of our
notation for writing chained equality proofs.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb20&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb20-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;infixl&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;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ&lt;/span&gt;
&lt;span id=&quot;cb20-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero * y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; zero&lt;/span&gt;
&lt;span id=&quot;cb20-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;suc x * y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; y + x * y&lt;/span&gt;
&lt;span id=&quot;cb20-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;*0 &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n * &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;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero *0 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb20-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; *0 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; n *0&lt;/span&gt;
&lt;span id=&quot;cb20-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;*1 &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n * &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; ≡ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; *1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb20-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; *1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n *1&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;*suc&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * suc y ≡ x + x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero *suc y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb20-16&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; *suc y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb20-17&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  begin&lt;/span&gt;
&lt;span id=&quot;cb20-18&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + x * suc y               ≡[ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y +&lt;span class=&quot;ot&quot;&gt;_)&lt;/span&gt; $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x *suc y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb20-19&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;             ≡⟨ +-assoc y x &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ]≡&lt;/span&gt;
&lt;span id=&quot;cb20-20&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y + x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; + x * y             ≡[ &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;+&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ +-comm y x ≡$ x * y ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb20-21&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; + x * y             ≡[ +-assoc x &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb20-22&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb20-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  x + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y + x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;             ∎&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We prove some standard properties of multiplication: commutativity,
distributivity over addition, associativity. Again, the proofs mostly
consist of a whole bunch of algebra, using the special notation for
building chained equality proofs.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb21&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb21-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-comm &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x * y ≡ y * x&lt;/span&gt;
&lt;span id=&quot;cb21-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-comm zero y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sym &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y *0&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-comm &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; begin&lt;/span&gt;
&lt;span id=&quot;cb21-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + x * y                   ≡[ y +&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ *-comm x y ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + y * x                   ≡⟨ y *suc x ]≡&lt;/span&gt;
&lt;span id=&quot;cb21-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y * suc x                   ∎&lt;/span&gt;
&lt;span id=&quot;cb21-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-distribˡ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x * &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y + z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ≡ x * y + x * z&lt;/span&gt;
&lt;span id=&quot;cb21-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-distribˡ zero y z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb21-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-distribˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; begin&lt;/span&gt;
&lt;span id=&quot;cb21-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + z + x * &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y + z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;         ≡[ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y + z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; +&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ *-distribˡ x y z ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + z + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * y + x * z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;     ≡[ +-assoc y &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;z + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * y + x * z&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;   ≡⟨ y +&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ +-assoc z &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ]≡&lt;/span&gt;
&lt;span id=&quot;cb21-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;z + x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; + x * z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;   ≡[ y +&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ &lt;span class=&quot;ot&quot;&gt;(_&lt;/span&gt;+&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ +-comm z &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ≡$ x * z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;x * y + z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; + x * z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;   ≡[ y +&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ +-assoc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-16&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * y + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;z + x * z&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;   ≡⟨ +-assoc y &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ]≡&lt;/span&gt;
&lt;span id=&quot;cb21-17&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y + x * y + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;z + x * z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;     ∎&lt;/span&gt;
&lt;span id=&quot;cb21-18&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-18&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-19&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-19&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-distribʳ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; * z ≡ x * z + y * z&lt;/span&gt;
&lt;span id=&quot;cb21-20&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-20&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-distribʳ x y z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; begin&lt;/span&gt;
&lt;span id=&quot;cb21-21&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-21&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; * z                 ≡[ *-comm &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-22&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-22&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  z * &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;                 ≡[ *-distribˡ z &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-23&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-23&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  z * x + z * y               ≡[ &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;+&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ *-comm z &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ≡$≡ *-comm z &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-24&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-24&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  x * z + y * z               ∎&lt;/span&gt;
&lt;span id=&quot;cb21-25&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-25&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-26&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-26&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-assoc &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; * z ≡ x * &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y * z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb21-27&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-27&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-assoc zero y z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb21-28&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-28&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-assoc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; begin&lt;/span&gt;
&lt;span id=&quot;cb21-29&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-29&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y + x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; * z             ≡[ *-distribʳ y &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-30&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-30&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y * z + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; * z         ≡[ y * z +&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ *-assoc x &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb21-31&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb21-31&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  y * z + x * &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y * z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;         ∎&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, we prove that multiplication is left-cancellative. This
proof is somewhat tricky—in the case that &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;z&lt;/code&gt; are all
successors, we need to use the induction hypothesis (&lt;em&gt;i.e.&lt;/em&gt; a
recursive call to &lt;code&gt;*-cancelˡ&lt;/code&gt;) on &lt;code&gt;x&lt;/code&gt; and the
predecessors of &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt;, using the fact that + is
left-cancellative to construct the required input equality.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb22&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb22-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-cancelˡ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ≢ x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x * y ≡ x * z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y ≡ z&lt;/span&gt;
&lt;span id=&quot;cb22-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-cancelˡ zero y z x≢0 xy≡xz &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x≢0 refl&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-cancelˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; zero zero x≢0 xy≡xz &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb22-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-cancelˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; zero &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; x≢0 xy≡xz &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;noConf &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x *0&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; xy≡xz&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-cancelˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; zero x≢0 xy≡xz &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;noConf &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;trans xy≡xz &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x *0&lt;span class=&quot;ot&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*-cancelˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; x≢0 xy≡xz &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc $≡&lt;/span&gt;
&lt;span id=&quot;cb22-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt; *-cancelˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y z x≢0&lt;/span&gt;
&lt;span id=&quot;cb22-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt; +-cancelˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x * z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt; begin&lt;/span&gt;
&lt;span id=&quot;cb22-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          suc x + suc x * y   ≡⟨ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; *suc y ]≡&lt;/span&gt;
&lt;span id=&quot;cb22-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          suc x * suc y       ≡[ xy≡xz ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb22-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          suc x * suc z       ≡[ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; *suc z ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb22-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          suc x + suc x * z   ∎&lt;/span&gt;
&lt;span id=&quot;cb22-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb22-16&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb22-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;inequality&quot;&gt;
&lt;h2&gt;Inequality&lt;/h2&gt;
&lt;p&gt;Next, we give a standard definition of the “less than or equal to”
relation on natural numbers. Note that the structure of a proof of &lt;span class=&quot;math inline&quot;&gt;\(x \leq
y\)&lt;/span&gt; exactly matches the structure of &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; itself.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb23&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb23-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb23-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;≤&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb23-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb23-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  zle &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; zero ≤ n&lt;/span&gt;
&lt;span id=&quot;cb23-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb23-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  sle &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; suc m ≤ suc n&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We also prove some standard properties of &lt;span class=&quot;math inline&quot;&gt;\(\leq\)&lt;/span&gt;: it is reflexive and
transitive, and is related to &lt;code&gt;suc&lt;/code&gt; in various ways.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb24&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb24-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-refl &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤ m&lt;/span&gt;
&lt;span id=&quot;cb24-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-refl &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; zle&lt;/span&gt;
&lt;span id=&quot;cb24-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-refl &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc m&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle ≤-refl&lt;/span&gt;
&lt;span id=&quot;cb24-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-trans &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y ≤ z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ z&lt;/span&gt;
&lt;span id=&quot;cb24-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-trans zle y≤z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; zle&lt;/span&gt;
&lt;span id=&quot;cb24-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x≤y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle y≤z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤-trans x≤y y≤z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-sucr &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤ suc n&lt;/span&gt;
&lt;span id=&quot;cb24-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-sucr zle &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; zle&lt;/span&gt;
&lt;span id=&quot;cb24-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-sucr &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle m≤n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤-sucr m≤n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-sucl &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; suc m ≤ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤ n&lt;/span&gt;
&lt;span id=&quot;cb24-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-sucl &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle sm≤n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤-sucr sm≤n&lt;/span&gt;
&lt;span id=&quot;cb24-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb24-16&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-pred &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; suc x ≤ suc y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ y&lt;/span&gt;
&lt;span id=&quot;cb24-17&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb24-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-pred &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle sx≤sy&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sx≤sy&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For convenience, we define &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt; in terms of &lt;span class=&quot;math inline&quot;&gt;\(\leq\)&lt;/span&gt;, and prove a few
properties: any number is less than its successor, and &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt; is
transitive and non-reflexive.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb25&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb25-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;lt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;x &amp;lt; y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc x ≤ y&lt;/span&gt;
&lt;span id=&quot;cb25-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;lt;suc &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; suc x&lt;/span&gt;
&lt;span id=&quot;cb25-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;lt;suc zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle zle&lt;/span&gt;
&lt;span id=&quot;cb25-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;lt;suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &amp;lt;suc&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-trans &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y &amp;lt; z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; z&lt;/span&gt;
&lt;span id=&quot;cb25-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x&amp;lt;y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle y&amp;lt;z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x&amp;lt;y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤-sucr y&amp;lt;z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;x≮x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ¬ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &amp;lt; x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;x≮x &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb25-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb25-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;x≮x &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x&amp;lt;x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x≮x x&amp;lt;x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;section class=&quot;level3&quot; id=&quot;relationships-among-equality-and-inequality&quot;&gt;
&lt;h3&gt;Relationships among equality and inequality&lt;/h3&gt;
&lt;p&gt;Of course, equality, &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(\leq\)&lt;/span&gt; have various relationships that we
will need. First, equality implies &lt;span class=&quot;math inline&quot;&gt;\(\leq\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb26&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb26-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb26-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≡→≤ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ y&lt;/span&gt;
&lt;span id=&quot;cb26-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb26-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≡→≤ refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤-refl&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, &lt;span class=&quot;math inline&quot;&gt;\(x &amp;lt; y\)&lt;/span&gt; implies that &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(y\)&lt;/span&gt; are &lt;em&gt;not&lt;/em&gt; related by &lt;span class=&quot;math inline&quot;&gt;\(\equiv\)&lt;/span&gt; or
&lt;span class=&quot;math inline&quot;&gt;\(\geq\)&lt;/span&gt;. The first lemma in particular—that &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt; implies &lt;span class=&quot;math inline&quot;&gt;\(\not\equiv\)&lt;/span&gt;—gets used quite a bit. Note that it can be read in two equivalent
ways: on the surface, it is a way to turn a proof of &lt;span class=&quot;math inline&quot;&gt;\(x &amp;lt; y\)&lt;/span&gt; into a
proof of &lt;span class=&quot;math inline&quot;&gt;\(x \not\equiv y\)&lt;/span&gt;; but
since &lt;span class=&quot;math inline&quot;&gt;\(x \not\equiv y\)&lt;/span&gt; is really an abbreviation for &lt;span class=&quot;math inline&quot;&gt;\((x \equiv y) \to \bot\)&lt;/span&gt;, it can be
used to derive a contradiction if we have proofs that &lt;span class=&quot;math inline&quot;&gt;\(x &amp;lt; y\)&lt;/span&gt; and
also &lt;span class=&quot;math inline&quot;&gt;\(x \equiv y\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb27&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb27-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb27-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;→≢ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≢ y&lt;/span&gt;
&lt;span id=&quot;cb27-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb27-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;→≢ x&amp;lt;y refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; x≮x x&amp;lt;y&lt;/span&gt;
&lt;span id=&quot;cb27-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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;https://byorgey.github.io/blog/rss#cb27-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;→≱ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ¬ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y ≤ x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb27-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb27-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;→≱ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x&amp;lt;y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle y≤x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &amp;lt;→≱ x&amp;lt;y y≤x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If &lt;span class=&quot;math inline&quot;&gt;\(x \leq y\)&lt;/span&gt; but they are not equal, then &lt;span class=&quot;math inline&quot;&gt;\(x &amp;lt; y\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb28&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb28-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb28-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤≢→&amp;lt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≢ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; y&lt;/span&gt;
&lt;span id=&quot;cb28-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb28-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤≢→&amp;lt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; zle x≢y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x≢y refl&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://byorgey.github.io/blog/rss#cb28-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤≢→&amp;lt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; zle x≢y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle zle&lt;/span&gt;
&lt;span id=&quot;cb28-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb28-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤≢→&amp;lt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x≤y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; x≢y &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤≢→&amp;lt; x≤y &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; m≡n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x≢y &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc $≡ m≡n&lt;span class=&quot;ot&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We will need a form of transitivity that says if &lt;span class=&quot;math inline&quot;&gt;\(x \leq y\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(y &amp;lt;
z\)&lt;/span&gt;, then &lt;span class=&quot;math inline&quot;&gt;\(x &amp;lt; z\)&lt;/span&gt;, as well as the other way around.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb29&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb29-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb29-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-&amp;lt;-trans &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y &amp;lt; z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; z&lt;/span&gt;
&lt;span id=&quot;cb29-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb29-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤-&amp;lt;-trans x≤y &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle y&amp;lt;z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x≤y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle y&amp;lt;z&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://byorgey.github.io/blog/rss#cb29-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb29-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb29-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-≤-trans &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y ≤ z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; z&lt;/span&gt;
&lt;span id=&quot;cb29-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb29-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-≤-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x&amp;lt;y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y≤z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle x&amp;lt;y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y≤z&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, a very specific lemma we will need: if a number is not
equal to either 0 or 1, then it must be greater than or equal to 2.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb30&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb30-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb30-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;¬01-is-≥2 &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a ≢ &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;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a ≢ &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;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; ≤ a&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb30-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;¬01-is-≥2 zero a≢0 a≢1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a≢0 refl&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb30-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;¬01-is-≥2 &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc zero&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; a≢0 a≢1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a≢1 refl&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb30-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb30-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;¬01-is-≥2 &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc a&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; a≢0 a≢1 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle zle&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level3&quot; id=&quot;arithmetic-and-inequality&quot;&gt;
&lt;h3&gt;Arithmetic and inequality&lt;/h3&gt;
&lt;p&gt;The last lemmas we need relate arithmetic operations and inequality.
First, adding and multiplying cannot make anything smaller (unless we
multiply by zero, of course).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb31&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb31-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb31-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤+ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb31-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤+ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; zle&lt;/span&gt;
&lt;span id=&quot;cb31-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb31-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤+ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sle ≤+&lt;/span&gt;
&lt;span id=&quot;cb31-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb31-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb31-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤* &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ≢ &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;ot&quot;&gt;→&lt;/span&gt; y ≤ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb31-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤* &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; x≢0 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x≢0 refl&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb31-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb31-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤* &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; x≢0 &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤+&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As a result, if we know that one thing is equal to a sum or product of
other things, we can conclude something about their relative sizes.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb32&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb32-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb32-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+→≤ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x + y ≡ z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ z&lt;/span&gt;
&lt;span id=&quot;cb32-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb32-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+→≤ refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤+&lt;/span&gt;
&lt;span id=&quot;cb32-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb32-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb32-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+→&amp;lt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x + y ≡ z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; z&lt;/span&gt;
&lt;span id=&quot;cb32-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb32-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;+→&amp;lt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; x+y≡z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; +→≤ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x +suc y&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; x+y≡z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb32-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb32-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb32-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;*→≤ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y ≢ &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;ot&quot;&gt;→&lt;/span&gt; y * x ≡ z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≤ z&lt;/span&gt;
&lt;span id=&quot;cb32-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb32-8&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;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; y≢0 refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤* y≢0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;divisibility-primes-and-composites&quot;&gt;
&lt;h2&gt;Divisibility, primes, and composites&lt;/h2&gt;
&lt;p&gt;With the preliminaries out of the way, we can finally get on with the
meat of the problem—and we finally get to make use of a dependent pair! A &lt;em&gt;constructive&lt;/em&gt; proof that &lt;code&gt;a&lt;/code&gt; divides
&lt;code&gt;b&lt;/code&gt; is a specific natural number witness &lt;code&gt;k&lt;/code&gt;, along with a proof that &lt;code&gt;k * a ≡ b&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb33&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb33-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb33-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;∣&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb33-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb33-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;a ∣ b &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Σ ℕ &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; k &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; k * a ≡ b&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Proofs of divisibility are unique—that is, for given &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; there
is at most one value of &lt;span class=&quot;math inline&quot;&gt;\(k\)&lt;/span&gt; such that &lt;span class=&quot;math inline&quot;&gt;\(ka = b\)&lt;/span&gt;. We won’t need this,
but it follows easily from the fact that multiplication is
cancellative. More interesting is the fact that divisibility is
&lt;em&gt;decidable&lt;/em&gt;—that is, for given numbers &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; we can calculate
either a proof that &lt;span class=&quot;math inline&quot;&gt;\(a \mid b\)&lt;/span&gt;, or a proof that &lt;span class=&quot;math inline&quot;&gt;\(\neg (a \mid b)\)&lt;/span&gt;.
This will play a starring role later on—to factor a number we need to
be able to try potential divisors and find out whether they work—but proving it is not easy!
It will take us several hundred more lines of Agda to get there.&lt;/p&gt;
&lt;p&gt;In any case, using this notion of divisibility, we can now define prime and
composite numbers. A number &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is defined to be prime if it is at
least two, and every &lt;span class=&quot;math inline&quot;&gt;\(2 \leq d &amp;lt; n\)&lt;/span&gt; does &lt;em&gt;not&lt;/em&gt; divide &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb34&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb34-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb34-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;Prime &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb34-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb34-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;Prime n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; ≤ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; × &lt;span class=&quot;ot&quot;&gt;(∀&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d &amp;lt; n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; ≤ d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ¬ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d ∣ n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One could equivalently define primality by saying that any divisor of
&lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; must be equal to &lt;span class=&quot;math inline&quot;&gt;\(1\)&lt;/span&gt; or &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;; I just decided I liked this
formulation better, especially because it directly matches up with the
way we will test a number for primality later.&lt;/p&gt;
&lt;p&gt;A composite number is one that has a nontrivial divisor—that is, a number &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;
such that &lt;span class=&quot;math inline&quot;&gt;\(2 \leq d &amp;lt; n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; divides &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-5&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-5&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;Note
that we could easily prove that if &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is prime then &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is not
composite, and likewise if &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is composite then it is not prime, but
we won’t end up needing these lemmas.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb35&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb35-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb35-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;Composite &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb35-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb35-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;Composite n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Σ ℕ &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; d &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; ≤ d × d &amp;lt; n × d ∣ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Unlike proofs of divisibility, proofs of &lt;code&gt;Composite n&lt;/code&gt; are &lt;em&gt;not&lt;/em&gt;
unique. For example, we could prove &lt;code&gt;Composite 12&lt;/code&gt; by showing that
&lt;span class=&quot;math inline&quot;&gt;\(2\)&lt;/span&gt; is a nontrivial divisor of &lt;span class=&quot;math inline&quot;&gt;\(12\)&lt;/span&gt;, or by showing that &lt;span class=&quot;math inline&quot;&gt;\(3\)&lt;/span&gt; is.
Although this does not matter from a purely logical point of view, it
matters computationally; in general, we care which specific proof of
&lt;code&gt;Composite n&lt;/code&gt; we have.&lt;/p&gt;
&lt;section class=&quot;level3&quot; id=&quot;nontrivial-divisors-come-in-pairs&quot;&gt;
&lt;h3&gt;Nontrivial divisors come in pairs&lt;/h3&gt;
&lt;p&gt;Before moving on to other things, we will prove a lemma about
composite numbers. If &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is composite, by definition it has a
nontrivial divisor &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt;; but this means it must also have a second
nontrivial divisor &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; such that &lt;span class=&quot;math inline&quot;&gt;\(ab = n\)&lt;/span&gt;. This fact seems almost
trivial to us. Indeed, it’s easy to show that if &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; has a divisor
&lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt;, then it must have another divisor &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; such that &lt;span class=&quot;math inline&quot;&gt;\(ab = n\)&lt;/span&gt;. The
tricky part is showing that if &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; is a &lt;em&gt;nontrivial&lt;/em&gt; divisor, then &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt;
is &lt;em&gt;also&lt;/em&gt; nontrivial. The proof relies on much of the infrastructure
we have built up about natural numbers, multiplication, and
inequality.&lt;/p&gt;
&lt;p&gt;First, we define a type representing two factors of a number &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;: a
pair of proofs that &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is composite (&lt;em&gt;i.e.&lt;/em&gt; two nontrivial divisors
of &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;), along with a proof that the product of those divisors is &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb36&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb36-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb36-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;FactorsOf &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb36-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb36-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;FactorsOf n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Σ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;Composite n × Composite n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{(&lt;/span&gt;f₁ , f₂&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; fst f₁ * fst f₂ ≡ n&lt;span class=&quot;ot&quot;&gt;})&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, we prove that if &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is composite, then it has two nontrivial
factors. We begin by pattern-matching on the proof that &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is
composite, which consists of a divisor &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt;, evidence that &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; is
nontrivial (&lt;em&gt;i.e.&lt;/em&gt; &lt;span class=&quot;math inline&quot;&gt;\(2 \leq a\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(a &amp;lt; n\)&lt;/span&gt;), and a proof that &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; is a
divisor of &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, which itself consists of a number &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; paired with a
proof that &lt;span class=&quot;math inline&quot;&gt;\(ba = n\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb37&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb37-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb37-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;factorsOf &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Composite n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; FactorsOf n&lt;/span&gt;
&lt;span id=&quot;cb37-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb37-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;factorsOf n &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a , 2≤a , a&amp;lt;n , b , ba≡n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To construct the proof of &lt;code&gt;FactorsOf n&lt;/code&gt;, we need two proofs of
&lt;code&gt;Composite n&lt;/code&gt; along with a proof that the product of the two divisors
is &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;. We already have a proof that &lt;span class=&quot;math inline&quot;&gt;\(ba = n\)&lt;/span&gt;, so we use that, with
&lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; as the second divisor (replicating the corresponding proof of
&lt;code&gt;Composite n&lt;/code&gt;), and &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; as the first. Proving that &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; is a divisor of
&lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is easy: &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; is the witness, and proving that &lt;span class=&quot;math inline&quot;&gt;\(ab = n\)&lt;/span&gt; is easy
since we already know &lt;span class=&quot;math inline&quot;&gt;\(ba = n\)&lt;/span&gt; and multiplication is commutative. The
only thing left is to prove that &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; is nontrivial, &lt;em&gt;i.e.&lt;/em&gt; that &lt;span class=&quot;math inline&quot;&gt;\(2
\leq b\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(b &amp;lt; n\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb38&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb38-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb38-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;b , 2≤b , b&amp;lt;n , a , trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;*-comm a b&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ba≡n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a , 2≤a , a&amp;lt;n , b , ba≡n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; , ba≡n&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First, we need a lemma that &lt;span class=&quot;math inline&quot;&gt;\(0 &amp;lt; n\)&lt;/span&gt;, which follows because &lt;span class=&quot;math inline&quot;&gt;\(0 &amp;lt; 1 &amp;lt; a
&amp;lt; n\)&lt;/span&gt; (remember that a proof of &lt;span class=&quot;math inline&quot;&gt;\(1 &amp;lt; a\)&lt;/span&gt; is actually defined to be the
same thing as a proof of &lt;span class=&quot;math inline&quot;&gt;\(2 \leq a\)&lt;/span&gt;).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb39&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb39-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb39-1&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;cb39-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb39-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  0&amp;lt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; n&lt;/span&gt;
&lt;span id=&quot;cb39-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb39-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  0&amp;lt;n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &amp;lt;-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle zle&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&amp;lt;-trans 2≤a a&amp;lt;n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, we tackle &lt;span class=&quot;math inline&quot;&gt;\(2 \leq b\)&lt;/span&gt;, by showing that &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; can’t possibly be &lt;span class=&quot;math inline&quot;&gt;\(0\)&lt;/span&gt;
or &lt;span class=&quot;math inline&quot;&gt;\(1\)&lt;/span&gt; (using our previous lemma that anything not equal to 0 or 1
must be greater than or equal to 2).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb40&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb40-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb40-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  2≤b &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; ≤ b&lt;/span&gt;
&lt;span id=&quot;cb40-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb40-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  2≤b &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ¬01-is-≥2 b&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; were &lt;span class=&quot;math inline&quot;&gt;\(0\)&lt;/span&gt;, then &lt;span class=&quot;math inline&quot;&gt;\(ba = n\)&lt;/span&gt; would imply &lt;span class=&quot;math inline&quot;&gt;\(0 = n\)&lt;/span&gt;, but we know
&lt;span class=&quot;math inline&quot;&gt;\(0 &amp;lt; n\)&lt;/span&gt;, so this is a contradiction.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb41&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb41-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb41-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; b≡0 &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &amp;lt;→≢ 0&amp;lt;n&lt;/span&gt;
&lt;span id=&quot;cb41-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb41-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;begin&lt;/span&gt;
&lt;span id=&quot;cb41-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb41-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;                     ≡[ refl ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb41-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb41-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; * a                 ≡⟨ &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;*&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ b≡0 ≡$ a ]≡&lt;/span&gt;
&lt;span id=&quot;cb41-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb41-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        b * a                 ≡[ ba≡n ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb41-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb41-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        n                     ∎&lt;/span&gt;
&lt;span id=&quot;cb41-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb41-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb41-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb41-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; were &lt;span class=&quot;math inline&quot;&gt;\(1\)&lt;/span&gt;, then &lt;span class=&quot;math inline&quot;&gt;\(ba = n\)&lt;/span&gt; would imply &lt;span class=&quot;math inline&quot;&gt;\(a = n\)&lt;/span&gt;, but we know
&lt;span class=&quot;math inline&quot;&gt;\(a &amp;lt; n\)&lt;/span&gt;, so this is also a contradiction.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb42&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb42-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; b≡1 &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &amp;lt;→≢ a&amp;lt;n&lt;/span&gt;
&lt;span id=&quot;cb42-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;begin&lt;/span&gt;
&lt;span id=&quot;cb42-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        a                     ≡⟨ a *1 ]≡&lt;/span&gt;
&lt;span id=&quot;cb42-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        a * &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;                 ≡[ *-comm a &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb42-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; * a                 ≡⟨ &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;*&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ b≡1 ≡$ a ]≡&lt;/span&gt;
&lt;span id=&quot;cb42-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        b * a                 ≡[ ba≡n ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb42-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        n                     ∎&lt;/span&gt;
&lt;span id=&quot;cb42-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb42-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb42-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, we prove &lt;span class=&quot;math inline&quot;&gt;\(b &amp;lt; n\)&lt;/span&gt;, by showing &lt;span class=&quot;math inline&quot;&gt;\(b \leq n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(b \neq n\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb43&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb43-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb43-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  b&amp;lt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; b &amp;lt; n&lt;/span&gt;
&lt;span id=&quot;cb43-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb43-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  b&amp;lt;n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤≢→&amp;lt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;math inline&quot;&gt;\(b \leq n\)&lt;/span&gt; since &lt;span class=&quot;math inline&quot;&gt;\(ba = n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; is not zero (if &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; were zero it
would contradict the fact that &lt;span class=&quot;math inline&quot;&gt;\(2 \leq a\)&lt;/span&gt;).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb44&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb44-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb44-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;*→≤ &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; a≡0 &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &amp;lt;→≢ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&amp;lt;-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle zle&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; 2≤a&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym a≡0&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;*-comm a b&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ba≡n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;math inline&quot;&gt;\(b \neq n\)&lt;/span&gt;, since &lt;span class=&quot;math inline&quot;&gt;\(b = n\)&lt;/span&gt; together with &lt;span class=&quot;math inline&quot;&gt;\(ba = n\)&lt;/span&gt; would imply &lt;span class=&quot;math inline&quot;&gt;\(a = 1\)&lt;/span&gt;
(since multiplication is cancellative), but &lt;span class=&quot;math inline&quot;&gt;\(2 \leq a\)&lt;/span&gt; so it cannot
equal 1.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb45&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb45-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; b≡n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &amp;lt;→≢ 2≤a&lt;/span&gt;
&lt;span id=&quot;cb45-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym&lt;/span&gt;
&lt;span id=&quot;cb45-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;*-cancelˡ n a &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&amp;lt;→≢ 0&amp;lt;n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb45-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;begin&lt;/span&gt;
&lt;span id=&quot;cb45-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            n * a             ≡⟨ &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;*&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ b≡n ≡$ a ]≡&lt;/span&gt;
&lt;span id=&quot;cb45-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            b * a             ≡[ ba≡n ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb45-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            n                 ≡⟨ n *1 ]≡&lt;/span&gt;
&lt;span id=&quot;cb45-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;            n * &lt;span class=&quot;dv&quot;&gt;1&lt;/span&gt;             ∎&lt;/span&gt;
&lt;span id=&quot;cb45-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;          &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb45-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;        &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb45-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb45-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb45-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;division&quot;&gt;
&lt;h2&gt;Division&lt;/h2&gt;
&lt;p&gt;Let’s start working our way towards proving that divisibility is
decidable. To check whether &lt;span class=&quot;math inline&quot;&gt;\(d \mid n\)&lt;/span&gt;, the usual idea would be to
divide &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; by &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; and check whether we get a remainder of zero. So we
need to formalize this notion of division with remainder.&lt;/p&gt;
&lt;p&gt;Specifically, when we divide &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; by &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, we expect to get a &lt;em&gt;quotient&lt;/em&gt; &lt;span class=&quot;math inline&quot;&gt;\(q\)&lt;/span&gt;
and a &lt;em&gt;remainder&lt;/em&gt; &lt;span class=&quot;math inline&quot;&gt;\(r\)&lt;/span&gt;, such that &lt;span class=&quot;math inline&quot;&gt;\(r + qd = n\)&lt;/span&gt;, and &lt;span class=&quot;math inline&quot;&gt;\(0 \leq r &amp;lt; d\)&lt;/span&gt;.
The first condition, &lt;span class=&quot;math inline&quot;&gt;\(r + qd = n\)&lt;/span&gt;, just defines what we mean by division: &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is
&lt;span class=&quot;math inline&quot;&gt;\(q\)&lt;/span&gt; times &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, plus a remainder of &lt;span class=&quot;math inline&quot;&gt;\(r\)&lt;/span&gt;. The second condition will ensure
that the result is unique. We wouldn’t want to divide &lt;span class=&quot;math inline&quot;&gt;\(17\)&lt;/span&gt; by &lt;span class=&quot;math inline&quot;&gt;\(2\)&lt;/span&gt; and
end up with a quotient of &lt;span class=&quot;math inline&quot;&gt;\(6\)&lt;/span&gt; and a remainder of &lt;span class=&quot;math inline&quot;&gt;\(5\)&lt;/span&gt;; the remainder should be as small as possible.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;DivMod&lt;/code&gt; type simply encodes these requirements.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb46&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb46-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb46-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; DivMod &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n d q r &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb46-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb46-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  DM &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;r + q * d ≡ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;r &amp;lt; d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d q r&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can prove a few lemmas about &lt;code&gt;DivMod&lt;/code&gt;. First, whenever we have
&lt;code&gt;DivMod n d q r&lt;/code&gt;, then &lt;code&gt;d&lt;/code&gt; must be positive, since &lt;span class=&quot;math inline&quot;&gt;\(r &amp;lt; d\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(r\)&lt;/span&gt; is
a natural number.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb47&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb47-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb47-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;divMod→0&amp;lt;d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n d q r &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d q r &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; d&lt;/span&gt;
&lt;span id=&quot;cb47-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb47-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;divMod→0&amp;lt;d &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;DM &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; r&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤-&amp;lt;-trans zle r&amp;lt;d&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can also show that for nonzero &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, the remainder is zero if and only if &lt;span class=&quot;math inline&quot;&gt;\(d
\mid n\)&lt;/span&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb48&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb48-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb48-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;mod0→divides &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;q &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d q &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; d ∣ n&lt;/span&gt;
&lt;span id=&quot;cb48-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb48-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;mod0→divides n d &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;q&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;DM eq &lt;span class=&quot;ot&quot;&gt;_)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; q , eq&lt;/span&gt;
&lt;span id=&quot;cb48-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb48-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb48-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb48-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;divides→mod0 &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; d ∣ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Σ ℕ &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; q &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d q &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb48-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb48-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;divides→mod0 n d 0&amp;lt;d &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;q , qd≡n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; q , &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;DM qd≡n 0&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We would also like to show that if the remainder when dividing &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; by
&lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; is &lt;em&gt;not&lt;/em&gt; zero, then &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; does &lt;em&gt;not&lt;/em&gt; divide &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;. This is &lt;em&gt;almost&lt;/em&gt;
the contrapositive of &lt;code&gt;divides→mod0&lt;/code&gt;—which would be trivial to
show—but not quite: I said “the” remainder, but actually we don’t yet
know that the quotient and remainder are unique! Perhaps we could get
a remainder of 0 and some other remainder for the same &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, by
choosing different quotients?&lt;/p&gt;
&lt;p&gt;Of course, quotients and remainders &lt;em&gt;are&lt;/em&gt; unique: that is, if &lt;span class=&quot;math inline&quot;&gt;\(q_1,
r_1\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(q_2, r_2\)&lt;/span&gt; both satisfy the properties to be the quotient and
remainder of &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; divided by &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, then in fact &lt;span class=&quot;math inline&quot;&gt;\(q_1 = q_2\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(r_1 =
r_2\)&lt;/span&gt;. But how can we prove this? The usual idea is to look at the
difference &lt;span class=&quot;math inline&quot;&gt;\(r_1 - r_2 = dq_1 - dq_2\)&lt;/span&gt;, which is divisible by &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;; but
since &lt;span class=&quot;math inline&quot;&gt;\(r_1 &amp;lt; d\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(r_2 &amp;lt; d\)&lt;/span&gt;, the only way for the difference &lt;span class=&quot;math inline&quot;&gt;\(r_1 -
r_2\)&lt;/span&gt; to be divisible by &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; is if in fact &lt;span class=&quot;math inline&quot;&gt;\(r_1 - r_2 = 0\)&lt;/span&gt;. From here
we can also derive &lt;span class=&quot;math inline&quot;&gt;\(q_1 = q_2\)&lt;/span&gt; via algebra.&lt;/p&gt;
&lt;p&gt;Subtraction, eh? In order to formalize this, it seems as though we might
need to define the integers… but there is a better way!&lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;absolute-difference&quot;&gt;
&lt;h2&gt;Absolute difference&lt;/h2&gt;
&lt;p&gt;The previous informal argument mentioned the difference &lt;span class=&quot;math inline&quot;&gt;\(r_1 - r_2\)&lt;/span&gt;.
But we could just as easily have talked about &lt;span class=&quot;math inline&quot;&gt;\(r_2 - r_1\)&lt;/span&gt; instead, and
the same argument would work just as well. This observation shows
that we do not actually care about the (signed) &lt;em&gt;difference&lt;/em&gt; between
&lt;span class=&quot;math inline&quot;&gt;\(r_1\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(r_2\)&lt;/span&gt;, but only the &lt;em&gt;distance&lt;/em&gt; between them. This means we
can just stick to our well-loved natural numbers, and define a commutative
&lt;em&gt;absolute difference&lt;/em&gt; function which computes the nonnegative distance
between its two arguments, like so:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb49&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb49-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb49-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∥&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;-&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;∥ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ&lt;/span&gt;
&lt;span id=&quot;cb49-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb49-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∥ zero - y ∥ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; y&lt;/span&gt;
&lt;span id=&quot;cb49-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb49-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∥ suc x - zero ∥ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc x&lt;/span&gt;
&lt;span id=&quot;cb49-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb49-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∥ suc x - suc y ∥ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ∥ x - y ∥&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Of course, we will need a lot of small lemmas about the properties of
this operation. We can start by proving that the distance between two
numbers is 0 if and only if they are equal:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb50&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb50-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb50-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff0 &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ≡ ∥ x - x ∥&lt;/span&gt;
&lt;span id=&quot;cb50-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb50-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff0 zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb50-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb50-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff0 &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; diff0 x&lt;/span&gt;
&lt;span id=&quot;cb50-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb50-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb50-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb50-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff0→≡ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ≡ ∥ x - y ∥ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ y&lt;/span&gt;
&lt;span id=&quot;cb50-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb50-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff0→≡ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; eq &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; eq&lt;/span&gt;
&lt;span id=&quot;cb50-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb50-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff0→≡ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; eq &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; suc $≡ diff0→≡ eq&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, the distance between any number and 0 is the number itself, and
the distance function is commutative.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb51&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb51-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∥x-0∥≡x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ∥ x - &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ∥ ≡ x&lt;/span&gt;
&lt;span id=&quot;cb51-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∥x-0∥≡x zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb51-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∥x-0∥≡x &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb51-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb51-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-comm &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ∥ x - y ∥ ≡ ∥ y - x ∥&lt;/span&gt;
&lt;span id=&quot;cb51-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-comm &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb51-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-comm &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb51-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-comm &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb51-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb51-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-comm &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; diff-comm &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A key lemma supporting the argument outlined in the previous section
is that if &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(y\)&lt;/span&gt; are both less than &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, so is their absolute difference.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb52&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb52-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb52-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-&amp;lt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; d &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y &amp;lt; d &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ∥ x - y ∥ &amp;lt; d&lt;/span&gt;
&lt;span id=&quot;cb52-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb52-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-&amp;lt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; x&amp;lt;d y&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; y&amp;lt;d&lt;/span&gt;
&lt;span id=&quot;cb52-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb52-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-&amp;lt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; x&amp;lt;d y&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; x&amp;lt;d&lt;/span&gt;
&lt;span id=&quot;cb52-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb52-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-&amp;lt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; x&amp;lt;d y&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; diff-&amp;lt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&amp;lt;-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &amp;lt;suc&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; x&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&amp;lt;-trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y &amp;lt;suc&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; y&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can also cancel the same thing being added to both sides, or factor out
the same thing being multiplied on both sides.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb53&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb53-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-cancelˡ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a b c &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ∥ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a + b&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; - &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a + c&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ∥ ≡ ∥ b - c ∥&lt;/span&gt;
&lt;span id=&quot;cb53-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-cancelˡ zero b c &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb53-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-cancelˡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc a&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; b c &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; diff-cancelˡ a b c&lt;/span&gt;
&lt;span id=&quot;cb53-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-distribʳ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ∥ x * d - y * d ∥ ≡ ∥ x - y ∥ * d&lt;/span&gt;
&lt;span id=&quot;cb53-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-distribʳ zero y d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb53-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-distribʳ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; zero d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ∥x-0∥≡x &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d + x * d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb53-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;diff-distribʳ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; begin&lt;/span&gt;
&lt;span id=&quot;cb53-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ∥ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d + x * d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; - &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d + y * d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ∥         ≡[ diff-cancelˡ d &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x * d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;y * d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb53-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ∥ x * d - y * d ∥                     ≡[ diff-distribʳ x y d ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb53-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb53-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  ∥ x - y ∥ * d                         ∎&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another key lemma is that if &lt;span class=&quot;math inline&quot;&gt;\(w + x = y + z\)&lt;/span&gt;, then &lt;span class=&quot;math inline&quot;&gt;\(\|w - y\| = \|x -
z\|\)&lt;/span&gt; (&lt;code&gt;sub₂&lt;/code&gt; below). Personally, I found this quite tricky to prove.
The best approach I found was to first prove the simpler lemma that
&lt;span class=&quot;math inline&quot;&gt;\(x + y = z\)&lt;/span&gt; implies &lt;span class=&quot;math inline&quot;&gt;\(x = \| z - y \|\)&lt;/span&gt; (&lt;code&gt;sub₁&lt;/code&gt;), which can then be used
in several places in the proof of &lt;code&gt;sub₂&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb54&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb54-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sub₁ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x + y ≡ z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x ≡ ∥ z - y ∥&lt;/span&gt;
&lt;span id=&quot;cb54-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sub₁ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;z&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; diff0 y&lt;/span&gt;
&lt;span id=&quot;cb54-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sub₁ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc z&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; x+y≡z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  begin&lt;/span&gt;
&lt;span id=&quot;cb54-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    suc x                     ≡⟨ suc $≡ x +0 ]≡&lt;/span&gt;
&lt;span id=&quot;cb54-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x + &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;               ≡[ x+y≡z ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb54-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    suc z                     ∎&lt;/span&gt;
&lt;span id=&quot;cb54-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sub₁ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc z&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; x+y≡z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  sub₁ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;z&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;noConf &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc $≡ sym &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x +suc y&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; x+y≡z&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sub₂ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;w x y z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; w + x ≡ y + z &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ∥ w - y ∥ ≡ ∥ x - z ∥&lt;/span&gt;
&lt;span id=&quot;cb54-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sub₂ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;z&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; w+x≡y+z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sub₁ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym w+x≡y+z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sub₂ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc w&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;zero&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;z&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; w+x≡y+z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sub₁ w+x≡y+z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;diff-comm &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;z&lt;span class=&quot;ot&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb54-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb54-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;sub₂ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc w&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;z&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; w+x≡y+z &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sub₂ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;w&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;noConf w+x≡y+z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;quotient-and-remainder-are-unique&quot;&gt;
&lt;h2&gt;Quotient and remainder are unique&lt;/h2&gt;
&lt;p&gt;We can now return to prove that quotient and remainder are unique.
First, we show that zero is the only multiple of &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; which is less than &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb55&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb55-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb55-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∣&amp;lt;→0 &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;d x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; d ∣ x &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; x &amp;lt; d &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ≡ x&lt;/span&gt;
&lt;span id=&quot;cb55-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb55-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∣&amp;lt;→0 &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;zero , ad≡x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; x&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ad≡x&lt;/span&gt;
&lt;span id=&quot;cb55-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb55-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;∣&amp;lt;→0 &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc a , ad≡x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; x&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&amp;lt;→≱ x&amp;lt;d &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;+→≤ ad≡x&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And now for the main event: if we have both &lt;code&gt;DivMod n d q₁ r₁&lt;/code&gt; and
&lt;code&gt;DivMod n d q₂ r₂&lt;/code&gt;, then in fact the &lt;code&gt;q&lt;/code&gt;s and &lt;code&gt;r&lt;/code&gt;s must be the same.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb56&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb56-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb56-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;divModUnique &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n d q₁ r₁ q₂ r₂ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d q₁ r₁ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d q₂ r₂ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;q₁ ≡ q₂&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; × &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;r₁ ≡ r₂&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb56-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb56-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;divModUnique &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;d&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;q₁&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;r₁&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;q₂&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;r₂&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; dm&lt;span class=&quot;ot&quot;&gt;@(&lt;/span&gt;DM r₁+q₁d≡n r₁&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;DM r₂+q₂d≡n r₂&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; q₁≡q₂ , r₁≡r₂&lt;/span&gt;
&lt;span id=&quot;cb56-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb56-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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since &lt;span class=&quot;math inline&quot;&gt;\(r_1 + q_1d = n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(r_2 + q_2d = n\)&lt;/span&gt;, by transitivity and
symmetry we have &lt;span class=&quot;math inline&quot;&gt;\(r_1 + q_1d = r_2 + q_2d\)&lt;/span&gt;; then by the &lt;code&gt;sub₂&lt;/code&gt; lemma,
&lt;span class=&quot;math inline&quot;&gt;\(\|r_1 - r_2\| = \|q_1d - q_2d\|\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb57&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb57-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb57-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  rem-diff &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ∥ r₁ - r₂ ∥ ≡ ∥ q₁ * d - q₂ * d ∥&lt;/span&gt;
&lt;span id=&quot;cb57-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb57-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  rem-diff &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sub₂ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;r₁&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;trans r₁+q₁d≡n &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym r₂+q₂d≡n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next, we can show that &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; divides the absolute difference &lt;span class=&quot;math inline&quot;&gt;\(\|r_1 -
  r_2\|\)&lt;/span&gt;, by factoring it out of &lt;span class=&quot;math inline&quot;&gt;\(\|q_1 d - q_2 d\|\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb58&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb58-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb58-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  d∣r₁-r₂ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; d ∣ ∥ r₁ - r₂ ∥&lt;/span&gt;
&lt;span id=&quot;cb58-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb58-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  d∣r₁-r₂ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ∥ q₁ - q₂ ∥ ,&lt;/span&gt;
&lt;span id=&quot;cb58-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb58-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;begin&lt;/span&gt;
&lt;span id=&quot;cb58-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb58-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      ∥ q₁ - q₂ ∥ * d         ≡⟨ diff-distribʳ q₁ q₂ d ]≡&lt;/span&gt;
&lt;span id=&quot;cb58-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb58-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      ∥ q₁ * d - q₂ * d ∥     ≡⟨ rem-diff ]≡&lt;/span&gt;
&lt;span id=&quot;cb58-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb58-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      ∥ r₁ - r₂ ∥             ∎&lt;/span&gt;
&lt;span id=&quot;cb58-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb58-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can then put three lemmas together to conclude &lt;span class=&quot;math inline&quot;&gt;\(r_1 = r_2\)&lt;/span&gt;: first,
since &lt;span class=&quot;math inline&quot;&gt;\(r_1\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(r_2\)&lt;/span&gt; are both less than &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, so is their absolute
difference; since &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; also divides the absolute difference, the
absolute difference must be zero; and finally, an absolute difference
of zero means &lt;span class=&quot;math inline&quot;&gt;\(r_1\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(r_2\)&lt;/span&gt; must be equal.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb59&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb59-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb59-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  r₁≡r₂ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; r₁ ≡ r₂&lt;/span&gt;
&lt;span id=&quot;cb59-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb59-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  r₁≡r₂ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; diff0→≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;∣&amp;lt;→0 d∣r₁-r₂ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;diff-&amp;lt; r₁&amp;lt;d r₂&amp;lt;d&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;From here, proving &lt;span class=&quot;math inline&quot;&gt;\(q_1 = q_2\)&lt;/span&gt; just requires some algebra.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb60&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb60-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  dq₁≡dq₂ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; d * q₁ ≡ d * q₂&lt;/span&gt;
&lt;span id=&quot;cb60-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  dq₁≡dq₂ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; +-cancelˡ r₁ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d * q₁&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d * q₂&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;begin&lt;/span&gt;
&lt;span id=&quot;cb60-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      r₁ + d * q₁             ≡[ r₁ +&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ *-comm d q₁ ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb60-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      r₁ + q₁ * d             ≡[ r₁+q₁d≡n ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb60-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      n                       ≡⟨ r₂+q₂d≡n ]≡&lt;/span&gt;
&lt;span id=&quot;cb60-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      r₂ + q₂ * d             ≡[ &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;+&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ sym r₁≡r₂ ≡$≡ *-comm q₂ d ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb60-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      r₁ + d * q₂             ∎&lt;/span&gt;
&lt;span id=&quot;cb60-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb60-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  q₁≡q₂ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; q₁ ≡ q₂&lt;/span&gt;
&lt;span id=&quot;cb60-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb60-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  q₁≡q₂ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; *-cancelˡ d q₁ q₂ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&amp;lt;→≢ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;divMod→0&amp;lt;d dm&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; dq₁≡dq₂&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, we can use uniqueness of quotients and remainders to show the
lemma we wanted about divisibility and remainders: if &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; divided by
&lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; has some nonzero number as remainder, then &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; does not divide
&lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;. If &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; did divide &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, then we know we would get a remainder of
&lt;span class=&quot;math inline&quot;&gt;\(0\)&lt;/span&gt;; but since remainders are unique, we can’t have both a zero and
nonzero remainder.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb61&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb61-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb61-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;modS→¬divides &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;q r &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d q &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc r&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ¬ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d ∣ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb61-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb61-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;modS→¬divides n d dm d∣n &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; divides→mod0 n d &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;divMod→0&amp;lt;d dm&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; d∣n&lt;/span&gt;
&lt;span id=&quot;cb61-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb61-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; q₂ , dm₂ &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; divModUnique dm dm₂&lt;/span&gt;
&lt;span id=&quot;cb61-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb61-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; q₁≡q₂ , &lt;span class=&quot;ot&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;the-division-algorithm-take-1&quot;&gt;
&lt;h2&gt;The division algorithm, take 1&lt;/h2&gt;
&lt;p&gt;So, given natural numbers &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, how do we compute the quotient
and remainder? We can write down a type &lt;code&gt;DivAlg&lt;/code&gt; that expresses what
we want: given some &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, &lt;code&gt;DivAlg n d&lt;/code&gt; represents the result of
the division algorithm, that is, a pair of numbers &lt;span class=&quot;math inline&quot;&gt;\((q,r)\)&lt;/span&gt; such that
&lt;code&gt;DivMod n d q r&lt;/code&gt; holds.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb62&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb62-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb62-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;DivAlg &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb62-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb62-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;DivAlg n d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Σ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;ℕ × ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;q , r&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d q r &lt;span class=&quot;ot&quot;&gt;})&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then we want a function with a type something like &lt;code&gt;(n d : ℕ) → DivAlg n d&lt;/code&gt; (actually this type is not quite correct—can you see why?).
How can we write something with this type?&lt;/p&gt;
&lt;p&gt;One simple idea, expressed imperatively, is to start with &lt;span class=&quot;math inline&quot;&gt;\(q = 0\)&lt;/span&gt;.
Now, as long as &lt;span class=&quot;math inline&quot;&gt;\(n \geq d\)&lt;/span&gt;, subtract &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; from &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; and add one to
&lt;span class=&quot;math inline&quot;&gt;\(q\)&lt;/span&gt;—if we can find &lt;span class=&quot;math inline&quot;&gt;\(q\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(r\)&lt;/span&gt; such that &lt;span class=&quot;math inline&quot;&gt;\(r + qd = n - d\)&lt;/span&gt;, then &lt;span class=&quot;math inline&quot;&gt;\(r +
(q+1)d = n\)&lt;/span&gt;. Eventually, &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; must land in the range &lt;span class=&quot;math inline&quot;&gt;\(0 \leq n &amp;lt; d\)&lt;/span&gt;,
in which case it will be the remainder, and the current value of &lt;span class=&quot;math inline&quot;&gt;\(q\)&lt;/span&gt;
will be the quotient.&lt;/p&gt;
&lt;section class=&quot;level3&quot; id=&quot;construct-the-evidence-you-would-like-to-pattern-match-on&quot;&gt;
&lt;h3&gt;Construct the evidence you would like to pattern-match on&lt;/h3&gt;
&lt;p&gt;That’s the idea, but turning this into a verified constructive algorithm will take some
work. First, let’s formalize the idea of testing whether &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is less
than &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, and decreasing it by &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; if not. We’d rather not actually
deal with subtraction, so the idea is to generate either a proof that
&lt;span class=&quot;math inline&quot;&gt;\(n &amp;lt; d\)&lt;/span&gt;, or another number &lt;span class=&quot;math inline&quot;&gt;\(n'\)&lt;/span&gt; along with a proof that &lt;span class=&quot;math inline&quot;&gt;\(n' + d = n\)&lt;/span&gt;.
We encapsulate this in the following type &lt;code&gt;Cmp&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb63&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb63-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb63-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; Cmp &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb63-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb63-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  LT &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; n &amp;lt; d &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Cmp n d&lt;/span&gt;
&lt;span id=&quot;cb63-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb63-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  GE &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n′ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n′ + d ≡ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Cmp n d&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;Cmp n d&lt;/code&gt; represents the result of comparing &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, and is
equivalent to having either &lt;span class=&quot;math inline&quot;&gt;\(n &amp;lt; d\)&lt;/span&gt; or &lt;span class=&quot;math inline&quot;&gt;\(n \geq d\)&lt;/span&gt;, but expressed in a
form that is more directly useful to us. &lt;strong&gt;Construct the evidence you
would like to pattern-match on!&lt;/strong&gt; That is, in general, evidence for a
proposition &lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt; can take many logically equivalent forms, and you
should pick the form that will make your life easiest at the &lt;em&gt;use
site&lt;/em&gt;, even if it means you have to work harder to &lt;em&gt;construct&lt;/em&gt; it in
the first place. You can write standalone lemmas for constructing
your evidence; but pattern-matching it will happen in the middle of
some bigger proof which ought not to be cluttered by calls to
conversion lemmas.&lt;/p&gt;
&lt;p&gt;To construct evidence for &lt;code&gt;Cmp n d&lt;/code&gt;, we write the function
&lt;code&gt;decreaseBy?&lt;/code&gt; which decides whether we can decrease &lt;code&gt;n&lt;/code&gt; by &lt;code&gt;d&lt;/code&gt; or not.
Writing this function is a bit more work than writing something
of type &lt;code&gt;(n d : ℕ) → (n &amp;lt; d) ⊎ (d ≤ n)&lt;/code&gt;, but our work will pay off later!&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb64&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb64-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb64-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;decreaseBy?&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Cmp n d&lt;/span&gt;
&lt;span id=&quot;cb64-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb64-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero decreaseBy? zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; GE &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; refl&lt;/span&gt;
&lt;span id=&quot;cb64-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb64-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero decreaseBy? suc d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; LT &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle zle&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb64-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb64-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;suc n decreaseBy? zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; GE &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; +0&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb64-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb64-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;suc n decreaseBy? suc d &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; n decreaseBy? d&lt;/span&gt;
&lt;span id=&quot;cb64-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb64-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; LT n&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; LT &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle n&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb64-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb64-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; GE n′ n′+d≡n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; GE n′ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n′ +suc d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc $≡ n′+d≡n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can also write a helper function &lt;code&gt;incDivMod&lt;/code&gt; which encodes the
observation from before, that if &lt;span class=&quot;math inline&quot;&gt;\(r + qd = n-d\)&lt;/span&gt;, then &lt;span class=&quot;math inline&quot;&gt;\(r + (q+1)d =
n\)&lt;/span&gt;. Of course we don’t actually want to use subtraction, so instead
of writing &lt;span class=&quot;math inline&quot;&gt;\(n-d\)&lt;/span&gt;, we work in terms of an &lt;span class=&quot;math inline&quot;&gt;\(n'\)&lt;/span&gt; such that &lt;span class=&quot;math inline&quot;&gt;\(n' + d = n\)&lt;/span&gt;.
Proving this requires only some straightforward algebra.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb65&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb65-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;incDivMod &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n′ n d q r &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; n′ + d ≡ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n′ d q r &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivMod n d &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc q&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; r&lt;/span&gt;
&lt;span id=&quot;cb65-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;incDivMod &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n′&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;d&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;q&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;r&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; n′+d≡n &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;DM r+qd≡n′ r&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; DM r+d+qd≡n r&amp;lt;d&lt;/span&gt;
&lt;span id=&quot;cb65-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-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;cb65-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  r+d+qd≡n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; r + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d + q * d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ≡ n&lt;/span&gt;
&lt;span id=&quot;cb65-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  r+d+qd≡n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; begin&lt;/span&gt;
&lt;span id=&quot;cb65-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    r + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d + q * d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;           ≡⟨ +-assoc r &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ]≡&lt;/span&gt;
&lt;span id=&quot;cb65-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;r + d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; + q * d           ≡[ &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;+&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ +-comm r &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ≡$ q * d ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb65-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;d + r&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; + q * d           ≡[ +-assoc d &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb65-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    d + &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;r + q * d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;           ≡[ d +&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ r+qd≡n′ ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb65-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    d + n′                    ≡[ +-comm d &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb65-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    n′ + d                    ≡[ n′+d≡n ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb65-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb65-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    n                         ∎&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now it seems like we have everything we need to write the division
algorithm as a recursive algorithm: given &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt;, check whether
&lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; can be decreased by &lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; or not. If not, we can return &lt;span class=&quot;math inline&quot;&gt;\(q = 0\)&lt;/span&gt; and
&lt;span class=&quot;math inline&quot;&gt;\(r = n\)&lt;/span&gt;. Otherwise, recurse on &lt;span class=&quot;math inline&quot;&gt;\(n - d\)&lt;/span&gt;, returning the same remainder
and an incremented quotient from whatever the recursive call returns, using
&lt;code&gt;incDivMod&lt;/code&gt; to discharge the proof obligation. It’s just a few lines
of code, right?&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb66&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb66-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb66-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;module&lt;/span&gt; DivModBad &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#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;https://byorgey.github.io/blog/rss#cb66-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;pp&quot;&gt;{-# NON_TERMINATING #-}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb66-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  divAlg &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivAlg n d&lt;/span&gt;
&lt;span id=&quot;cb66-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb66-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  divAlg n d &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; n decreaseBy? d&lt;/span&gt;
&lt;span id=&quot;cb66-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb66-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; LT n&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; , n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;DM &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n +0&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; n&amp;lt;d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb66-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb66-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; GE n′ n′+d≡n &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; divAlg n′ d&lt;/span&gt;
&lt;span id=&quot;cb66-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb66-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;q , r&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , dm &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc q , r&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;incDivMod n′+d≡n dm&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Well, as you can see, it &lt;em&gt;is&lt;/em&gt; just a few lines of code, but all is not
well: although this function typechecks, Agda can’t tell that it is
terminating! (I added the &lt;code&gt;NON_TERMINATING&lt;/code&gt; pragma so I could include
this bad version of &lt;code&gt;divAlg&lt;/code&gt; in the code without causing an error.)
The problem is that the recursive call to &lt;code&gt;divAlg&lt;/code&gt; is on &lt;code&gt;n′&lt;/code&gt;,
which is not a subterm of &lt;code&gt;n&lt;/code&gt;, but instead comes from the call to
&lt;code&gt;decreaseBy?&lt;/code&gt;. Agda has no way of knowing whether the result from
some random function call is going to end up being smaller than the
original input.&lt;/p&gt;
&lt;p&gt;Now, you and I can both see that this function does indeed terminate,
but we just need a way to convince Agda of this fact… right?&lt;/p&gt;
&lt;p&gt;…have you spotted the flaw? Remember how I mentioned that the type &lt;code&gt;(n d : ℕ) → DivAlg n d&lt;/code&gt; is not quite right? In fact, the above bad
implementation of &lt;code&gt;divMod&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; terminating, and Agda is quite right to
be worried! In particular, the function recurses infinitely when given an input of &lt;span class=&quot;math inline&quot;&gt;\(d
= 0\)&lt;/span&gt;, since it will keep subtracting &lt;span class=&quot;math inline&quot;&gt;\(0\)&lt;/span&gt; from &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; forever. This makes sense, of
course: everyone knows you can’t divide by zero &lt;em&gt;because it makes the
universe go into infinite recursion&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The correct type for &lt;code&gt;divAlg&lt;/code&gt; is &lt;code&gt;(n d : ℕ) → (0 &amp;lt; d) → DivAlg n d&lt;/code&gt;,
but we’re still going to have trouble convincing Agda that our
algorithm is terminating. In order to do so, we need to take a detour
through &lt;em&gt;well-founded induction&lt;/em&gt;.&lt;/p&gt;
&lt;/section&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;well-founded-induction&quot;&gt;
&lt;h2&gt;Well-founded induction&lt;/h2&gt;
&lt;p&gt;Normally, Agda only allows functions that are &lt;em&gt;structurally
recursive&lt;/em&gt;—that is, functions which make recursive calls on syntactic
subterms of their inputs. (Agda’s termination checking is a bit more
sophisticated than that, but that’s the basic idea.) However, we can
use basic structural induction to bootstrap our way into more exotic
forms. In particular, we are going to define something called
&lt;em&gt;well-founded induction&lt;/em&gt;.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-6&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-6&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;Note that I will use the terms “recursion”
and “induction” more or less interchangeably. In some contexts, people
make a distinction between the terms (typically a &lt;em&gt;recursion
principle&lt;/em&gt; is a less-dependently-typed version of an &lt;em&gt;induction
principle&lt;/em&gt;), but in this context, &lt;em&gt;inductive proofs&lt;/em&gt; correspond, via
Curry-Howard, to (suitably restricted) &lt;em&gt;recursive functions&lt;/em&gt;, so
“induction” and “recursion” describe the same thing from a logical and
computational viewpoint, respectively.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The idea of well-founded induction starts with the general idea of a
&lt;em&gt;relation&lt;/em&gt;. A relation on &lt;code&gt;A&lt;/code&gt; is just a function that takes two
values of type &lt;code&gt;A&lt;/code&gt; and produces a type, representing evidence that the
two values are related (according to whatever kind of relationship we
have in mind).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb67&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb67-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb67-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;Rel &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set₁&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb67-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb67-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;Rel A &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We have already seen quite a few relations, such as equality, &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt;,
&lt;span class=&quot;math inline&quot;&gt;\(\leq\)&lt;/span&gt;, and divisibility.&lt;/p&gt;
&lt;p&gt;Suppose we’re writing a recursive function, with some relation &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt;
in mind, and for a given input &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; we’re only allowed to make
recursive calls on values &lt;span class=&quot;math inline&quot;&gt;\(y\)&lt;/span&gt; such that &lt;span class=&quot;math inline&quot;&gt;\(y \prec x\)&lt;/span&gt;. If &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt; is the
“is a syntactic subterm of” relation, then we get structural
recursion as usual. But what if &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt; is some other relation? What
needs to be true about &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt; for this to make sense? In particular, how
can we be sure that the function won’t get stuck in infinite recursion?&lt;/p&gt;
&lt;p&gt;One’s first instinct might be to say that &lt;span class=&quot;math inline&quot;&gt;\(y \prec x\)&lt;/span&gt; needs to imply that
&lt;span class=&quot;math inline&quot;&gt;\(y\)&lt;/span&gt; is “smaller than” &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; somehow. But what does “smaller than” mean?
And in fact, “smaller than” doesn’t always work: for example, if we are
writing a function over the rational numbers, or even just the
integers, the usual “smaller than” relation does &lt;em&gt;not&lt;/em&gt; guarantee our
function will terminate; it’s possible to continue choosing smaller
and smaller rational numbers or integers forever.&lt;/p&gt;
&lt;p&gt;The key idea is exactly that this can’t happen: it’s not possible to
have an infinite chain of values where each is related to the
previous. That is, there should be no left-infinite chains &lt;span class=&quot;math inline&quot;&gt;\(\dots
\prec y_3 \prec y_2 \prec y_1 \prec x\)&lt;/span&gt;. Then we are guaranteed that if we keep making
recursive calls on values that are related by &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt; to the previous
value, we will have to stop eventually: after some finite number of calls
we will hit a value with nothing else related to it.&lt;/p&gt;
&lt;p&gt;A relation &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt; with this “no left-infinite chains” property is
called &lt;em&gt;well-founded&lt;/em&gt;. But how do we encode this idea in Agda?&lt;/p&gt;
&lt;section class=&quot;level3&quot; id=&quot;accessibility&quot;&gt;
&lt;h3&gt;Accessibility&lt;/h3&gt;
&lt;p&gt;Instead of thinking negatively (&lt;em&gt;no&lt;/em&gt; left-infinite
chains), the key is to think positively: &lt;em&gt;all&lt;/em&gt; chains to the left of
&lt;em&gt;every&lt;/em&gt; value are finite. Call a value &lt;em&gt;accessible&lt;/em&gt; if all chains
leading to it are finite. Another way to say this is that a value is
accessible if every value related to it is also accessible:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb68&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb68-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb68-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; Acc &lt;span class=&quot;ot&quot;&gt;(_&lt;/span&gt;≺&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; Rel A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb68-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb68-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  acc &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; y ≺ x &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Acc &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≺&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Acc &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≺&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;&lt;code&gt;Acc _≺_ x&lt;/code&gt; defines what it means for a particular value &lt;code&gt;x&lt;/code&gt; to be
accessible with respect to a relation &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt;. There is only one
constructor, &lt;code&gt;acc&lt;/code&gt;, which requires &lt;code&gt;(y : A) → y ≺ x → Acc _≺_ y&lt;/code&gt;—that
is, for every value &lt;code&gt;y&lt;/code&gt; of type &lt;code&gt;A&lt;/code&gt;, if &lt;code&gt;y&lt;/code&gt; is related to &lt;code&gt;x&lt;/code&gt;, then
&lt;code&gt;y&lt;/code&gt; is accessible. In other words, &lt;code&gt;x&lt;/code&gt; is accessible if and only if every &lt;code&gt;y ≺ x&lt;/code&gt;
is accessible.&lt;/p&gt;
&lt;p&gt;This is definitely tricky to wrap your head around! At this point
you may have two objections:&lt;/p&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;&lt;p&gt;What about base cases? Shouldn’t we have another constructor which
says &lt;code&gt;x&lt;/code&gt; is accessible if &lt;em&gt;nothing&lt;/em&gt; is related to it? Actually,
the &lt;code&gt;acc&lt;/code&gt; constructor already says that! If nothing is related to
&lt;code&gt;x&lt;/code&gt;, then &lt;code&gt;(y : A) → y ≺ x → Acc _≺_ y&lt;/code&gt; is trivially true: we can
easily promise anything we want as the output of a function if we
know it can never be called. Every natural number less than zero is a
purple flying weasel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Doesn’t this just run into the same problem as before with
left-infinite chains? If we consider the “is one less than”
relation on the integers, isn’t &lt;span class=&quot;math inline&quot;&gt;\(2\)&lt;/span&gt; accessible because &lt;span class=&quot;math inline&quot;&gt;\(1\)&lt;/span&gt; is
accessible because &lt;span class=&quot;math inline&quot;&gt;\(0\)&lt;/span&gt; is accessible because &lt;span class=&quot;math inline&quot;&gt;\(-1\)&lt;/span&gt; is accessible
because … ?&lt;/p&gt;
&lt;p&gt;There is something a bit subtle going on here: recursive data types
in Agda (unlike Haskell) are interpreted according to a
&lt;em&gt;least fixed point&lt;/em&gt; semantics. Put in plain terms, the only values
of a data type are those which can be built by applications of a
&lt;em&gt;finite&lt;/em&gt; number of constructors. So in fact, the “no left-infinite
chain” condition is foundationally built into the way Agda data
types work!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The idea is that we can turn well-founded induction into structural
induction by &lt;em&gt;pattern-matching on &lt;code&gt;Acc&lt;/code&gt; proofs&lt;/em&gt;! Starting with some
&lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt;, any &lt;span class=&quot;math inline&quot;&gt;\(y \prec x\)&lt;/span&gt; has an accessibility proof which is a subterm of
the accessibility proof for &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt;. This is a bit exotic though: if we
pattern-match on the &lt;code&gt;acc&lt;/code&gt; for &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; we get a &lt;em&gt;function&lt;/em&gt; that yields an
accessibility proof for each &lt;span class=&quot;math inline&quot;&gt;\(y \prec x\)&lt;/span&gt;; calling that function
produces another accessibility proof, which &lt;em&gt;counts as a structural
subterm of the original&lt;/em&gt;. This seems a bit strange until you think of
a value of type &lt;code&gt;Acc&lt;/code&gt; like an big, arbitrarily-branching tree of
finite depth; each node contains a function which really just stores
all the subtrees. In other words, a function of type &lt;code&gt;(y : A) → y ≺ x → Acc _≺_ y&lt;/code&gt; can be thought of as a giant tuple of &lt;code&gt;Acc&lt;/code&gt; values, one
for each &lt;code&gt;y \prec x&lt;/code&gt;.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-7&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-7&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;I am not sure exactly how Agda handles this
internally, but I assume this is well-trodden ground for designers of
proof assistants.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;section class=&quot;level3&quot; id=&quot;well-founded-induction-defined&quot;&gt;
&lt;h3&gt;Well-founded induction, defined&lt;/h3&gt;
&lt;p&gt;Given the definition of accessible elements, we can now give the
definition of a well-founded relation: a relation on &lt;code&gt;A&lt;/code&gt; is well-founded if
&lt;em&gt;every&lt;/em&gt; value of type &lt;code&gt;A&lt;/code&gt; is accessible.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb69&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb69-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb69-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;WellFounded &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; Rel A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb69-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb69-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;WellFounded &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≺&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Acc &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≺&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;We can now write down the &lt;em&gt;principle of well-founded induction&lt;/em&gt;. This
is also quite tricky to wrap your brain around, so we’ll go through it
slowly. Previously, we were just talking about whether functions terminated or
not; but the reason this is important is that a function might be
calculating a &lt;em&gt;proof&lt;/em&gt;. A function which purports to calculate a proof
but sometimes goes into infinite recursion is a charlatan, and not
really a proof at all.&lt;/p&gt;
&lt;p&gt;So instead of thinking about termination, let’s switch to thinking
about proofs. Given a proposition &lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt;, we want to prove that
&lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt; holds for every value of type &lt;span class=&quot;math inline&quot;&gt;\(A\)&lt;/span&gt;. The idea is that when trying to
prove &lt;span class=&quot;math inline&quot;&gt;\(P(y)\)&lt;/span&gt; for a particular &lt;span class=&quot;math inline&quot;&gt;\(y\)&lt;/span&gt;, we get to assume that &lt;span class=&quot;math inline&quot;&gt;\(P(z)\)&lt;/span&gt; holds
(&lt;em&gt;i.e.&lt;/em&gt; we get to make recursive calls) for all &lt;span class=&quot;math inline&quot;&gt;\(z \prec y\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Here, then, is the statement of the principle of well-founded induction, with
each argument broken out on a separate line so we can explain them as
we go.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb70&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb70-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb70-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;wf-ind &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb70-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb70-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;P &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;P&lt;/code&gt; represents an arbitrary proposition on &lt;code&gt;A&lt;/code&gt;; our goal is to show &lt;code&gt;P&lt;/code&gt; holds for
every value of type &lt;code&gt;A&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb71&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb71-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb71-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;{_&lt;/span&gt;≺&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; Rel A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;An arbitrary relation.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb72&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb72-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb72-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  WellFounded &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≺&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;A proof that &lt;code&gt;≺&lt;/code&gt; is a well-founded relation.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb73&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb73-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb73-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;z &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; z ≺ y &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; P z&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; P y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;This is the trickiest
argument to understand. Intuitively, it says “For any
&lt;code&gt;y&lt;/code&gt;, if we know &lt;code&gt;P z&lt;/code&gt; holds for all &lt;code&gt;z ≺ y&lt;/code&gt;, then we can show &lt;code&gt;P y&lt;/code&gt;
also holds.”&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb74&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb74-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb74-1&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;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; P x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;The principle of well-founded induction says that all of this is
enough to show that &lt;code&gt;P x&lt;/code&gt; holds for &lt;em&gt;all&lt;/em&gt; &lt;code&gt;x : A&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, how do we implement this? If we try something
straightforward, as in &lt;code&gt;wf-ind-bad&lt;/code&gt; below—just call &lt;code&gt;ind&lt;/code&gt; on &lt;code&gt;x&lt;/code&gt;, then call &lt;code&gt;wf-ind&lt;/code&gt; recursively
to fill in the proofs for &lt;code&gt;P z&lt;/code&gt;—of course it does not work; Agda cannot
tell that this is terminating.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-8&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-8&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;As an aside, Agda complains that this definition of &lt;code&gt;wf-ind-bad&lt;/code&gt;
is not terminating—which makes sense—but it continues to complain even
when I include the &lt;code&gt;NON_TERMINATING&lt;/code&gt; pragma, which I don’t
understand. Perhaps this has been fixed in a more recent version of Agda.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt; And this makes sense, because we are
not even using the fact that the relation is well-founded at all!&lt;/p&gt;
&lt;pre class=&quot;plain&quot;&gt;&lt;code&gt;module WFIndBad where

  {-# NON_TERMINATING #-}
  wf-ind-bad : {P : A → Set} {_≺_ : Rel A} → WellFounded _≺_ → ((y : A) → ((z : A) → z ≺ y → P z) → P y) → (x : A) → P x
  wf-ind-bad wf ind x = ind x (λ z Rzx → wf-ind-bad wf ind z)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead, the right idea is to use the fact that &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt; is
well-founded to generate an initial proof of accessibility for the
input &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt;, and then &lt;em&gt;pattern match on accessibility proofs&lt;/em&gt; alongside
the values as we recurse. Every time we make a recursive call on some
&lt;span class=&quot;math inline&quot;&gt;\(y \prec x\)&lt;/span&gt;, we can just pattern-match on the accessibility proof for
&lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; to get an accessibility proof for &lt;span class=&quot;math inline&quot;&gt;\(y\)&lt;/span&gt;, so Agda will be able to see
that the whole thing is &lt;em&gt;structurally&lt;/em&gt; recursive on the accessibility
proofs.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb76&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb76-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb76-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;wf-ind &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;P&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{_&lt;/span&gt;≺&lt;span class=&quot;ot&quot;&gt;_}&lt;/span&gt; wf ind x &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; go x &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;wf x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb76-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb76-2&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;cb76-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb76-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  go &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Acc &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;≺&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; x &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; P x&lt;/span&gt;
&lt;span id=&quot;cb76-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb76-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  go x &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;acc f&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ind x &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; z z≺x &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; go z &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;f z z≺x&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level3&quot; id=&quot;less-than-is-well-founded&quot;&gt;
&lt;h3&gt;Less-than is well-founded&lt;/h3&gt;
&lt;p&gt;Now that we have well-founded induction under our belts, let’s show
that the less-than relation on natural numbers is well-founded. This
corresponds to what is often called “strong induction”.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-9&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-9&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;Incidentally,
we could probably have gotten away with directly defining a principle
of strong natural number induction, without bothering with the full
generality of well-founded induction, but this way is more fun and
interesting!&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt; To prove that &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt; is well-founded, we of course must
show that every natural number is accessible under &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt;. However, if
we directly try to prove &lt;code&gt;(m : ℕ) → (Acc _&amp;lt;_) m&lt;/code&gt;, we run into a
variant of the exact same problem we have been dealing with: to prove
that &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; is accessible we need to know that &lt;em&gt;every&lt;/em&gt; &lt;span class=&quot;math inline&quot;&gt;\(k &amp;lt; m\)&lt;/span&gt; is also
accessible, but again, we cannot show this directly by
recursion/induction, since &lt;span class=&quot;math inline&quot;&gt;\(k\)&lt;/span&gt; may not be a structural subterm of &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;What we need is the usual trick for proving strong induction from weak
induction: instead of proving that &lt;span class=&quot;math inline&quot;&gt;\(P(x)\)&lt;/span&gt; holds for all &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt;, we prove
that &lt;span class=&quot;math inline&quot;&gt;\((\downarrow P)(x)\)&lt;/span&gt; holds for all &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt;, where &lt;span class=&quot;math inline&quot;&gt;\(\downarrow P\)&lt;/span&gt; is
the “downward closure” of &lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt;. That is, &lt;span class=&quot;math inline&quot;&gt;\((\downarrow P)(x)\)&lt;/span&gt; says that
&lt;span class=&quot;math inline&quot;&gt;\(P(x)\)&lt;/span&gt; holds for &lt;em&gt;all&lt;/em&gt; &lt;span class=&quot;math inline&quot;&gt;\(y &amp;lt; x\)&lt;/span&gt;.&lt;span class=&quot;sidenote-wrapper&quot;&gt;&lt;label class=&quot;margin-toggle sidenote-number&quot; for=&quot;sn-10&quot;&gt;&lt;/label&gt;&lt;input class=&quot;margin-toggle&quot; id=&quot;sn-10&quot; type=&quot;checkbox&quot; /&gt;&lt;span class=&quot;sidenote&quot;&gt;Note that another way to define
&lt;span class=&quot;math inline&quot;&gt;\((\downarrow P)(x)\)&lt;/span&gt; is that &lt;span class=&quot;math inline&quot;&gt;\(P(y)\)&lt;/span&gt; holds for all &lt;span class=&quot;math inline&quot;&gt;\(y \sim x\)&lt;/span&gt;, where &lt;span class=&quot;math inline&quot;&gt;\(\sim\)&lt;/span&gt;
is the &lt;em&gt;transitive closure&lt;/em&gt; of the predecessor relation. As an
advanced exercise, generalize &lt;span class=&quot;math inline&quot;&gt;\(\downarrow_\prec P\)&lt;/span&gt; to be defined relative to
the transitive closure of any relation &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt;, and then prove that &lt;span class=&quot;math inline&quot;&gt;\(\prec\)&lt;/span&gt; is well-founded
if and only if its transitive closure is. For more along
these lines, see this &lt;a href=&quot;https://boarders.github.io/posts/well_founded_induction.html&quot;&gt;very cool (but much more abstract) post on
well-founded induction by Callan McGill&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb77&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb77-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb77-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;↓ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb77-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb77-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;↓ P n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;k &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;k &amp;lt; n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; P k&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can prove, for all natural numbers &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt;, that every natural
number up to and including &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; is accessible under the &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt; relation. Zero is accessible
because nothing is less than it; everything up to the successor of &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; is accessible because by induction we know everything up to &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; is, and anything less than the successor of &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; must in fact be &lt;span class=&quot;math inline&quot;&gt;\(\leq m\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb78&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb78-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb78-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-acc &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;m &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ↓ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;Acc &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;lt;&lt;span class=&quot;ot&quot;&gt;_)&lt;/span&gt; m&lt;/span&gt;
&lt;span id=&quot;cb78-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb78-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-acc zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;λ&lt;/span&gt; k &lt;span class=&quot;ot&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb78-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb78-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-acc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc m&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; zero &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle le&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; acc &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; y &lt;span class=&quot;ot&quot;&gt;())&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb78-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb78-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-acc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc m&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc k&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle le&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; acc &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; y y&amp;lt;sk &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &amp;lt;-acc m y &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&amp;lt;-≤-trans y&amp;lt;sk le&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, to show that any natural number &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is accessible—&lt;em&gt;i.e.&lt;/em&gt; that &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt;
is well-founded—we can use the fact that all numbers less than the successor of &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;
are accessible, and just project out accessibility for &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; itself.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb79&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb79-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb79-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-wf &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; WellFounded &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;lt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb79-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb79-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&amp;lt;-wf n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &amp;lt;-acc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &amp;lt;suc&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;the-division-algorithm&quot;&gt;
&lt;h2&gt;The division algorithm&lt;/h2&gt;
&lt;p&gt;Finally, we can define the division algorithm, via well-founded
induction! The definition is very similar to our first attempt, but we use the principle of well-founded induction with &lt;span class=&quot;math inline&quot;&gt;\(&amp;lt;\)&lt;/span&gt;. Note that neither &lt;code&gt;divAlg&lt;/code&gt; nor its helper function &lt;code&gt;go&lt;/code&gt; is directly recursive. Instead, &lt;code&gt;go&lt;/code&gt; takes an induction hypothesis as an argument, which we call instead, providing an extra proof that the subject of the induction hypothesis is in fact less than the original input. &lt;code&gt;wf-ind&lt;/code&gt; takes care of the actual recursion.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb80&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb80-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb80-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;divAlg &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n d &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivAlg n d&lt;/span&gt;
&lt;span id=&quot;cb80-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb80-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;divAlg n d 0&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; wf-ind &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;P &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;λ&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivAlg n d&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &amp;lt;-wf go n&lt;/span&gt;
&lt;span id=&quot;cb80-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb80-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;cb80-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb80-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  go &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;n′ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; n′ &amp;lt; n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivAlg n′ d&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; DivAlg n d&lt;/span&gt;
&lt;span id=&quot;cb80-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb80-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  go n IH &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; n decreaseBy? d&lt;/span&gt;
&lt;span id=&quot;cb80-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb80-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; LT n&amp;lt;d &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; , n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , DM &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n +0&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; n&amp;lt;d&lt;/span&gt;
&lt;span id=&quot;cb80-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb80-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; GE n′ n′+d≡n &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; IH n′ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;+→&amp;lt; 0&amp;lt;d n′+d≡n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb80-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb80-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;q , r&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , dm &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc q , r&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , incDivMod n′+d≡n dm&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using the division algorithm, we can also finally decide whether one
number divides another: zero divides zero; zero does not divide any
successor since that would imply there is some &lt;span class=&quot;math inline&quot;&gt;\(k\)&lt;/span&gt; such that &lt;span class=&quot;math inline&quot;&gt;\(k\)&lt;/span&gt; times
zero is nonzero, which is absurd; and if &lt;span class=&quot;math inline&quot;&gt;\(x\)&lt;/span&gt; is a successor, we can
apply the division algorithm and check the remainder, applying some
previous lemmas that say what zero and nonzero remainders tells us
about divisibility.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb81&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb81-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb81-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;∣?&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x y &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Dec &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ∣ y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb81-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb81-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero ∣? zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; yes &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; , refl&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb81-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb81-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;zero ∣? &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc y&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; no &lt;span class=&quot;ot&quot;&gt;λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a , eq&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; absurd &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;noConf &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;*-comm a zero&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; eq&lt;span class=&quot;ot&quot;&gt;))}&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb81-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb81-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ∣? y &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; divAlg y &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle zle&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb81-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb81-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;q , zero&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , dm &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; yes &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;mod0→divides y &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; dm&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb81-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb81-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;q , suc r&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , dm &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; no &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;modS→¬divides y &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc x&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; dm&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;primality-testing&quot;&gt;
&lt;h2&gt;Primality testing&lt;/h2&gt;
&lt;p&gt;To test a number for primality, we are just going to use
straightforward, naive trial division. The straightforward way of
doing this is by starting at &lt;span class=&quot;math inline&quot;&gt;\(2\)&lt;/span&gt; and counting &lt;em&gt;up&lt;/em&gt;—but this is a
problem, because when pattern-matching on natural numbers we most
naturally count &lt;em&gt;down&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Well, remember—build the evidence you want to pattern-match on! Let’s
develop some machinery for counting up instead of down.&lt;/p&gt;
&lt;section class=&quot;level3&quot; id=&quot;counting-up&quot;&gt;
&lt;h3&gt;Counting up&lt;/h3&gt;
&lt;p&gt;The problem starts with &lt;span class=&quot;math inline&quot;&gt;\(\leq\)&lt;/span&gt;: a proof of &lt;span class=&quot;math inline&quot;&gt;\(m \leq n\)&lt;/span&gt; starts with a
base case representing evidence that &lt;span class=&quot;math inline&quot;&gt;\(0 \leq k\)&lt;/span&gt;, then every
constructor application of &lt;code&gt;sle&lt;/code&gt; increments both sides by one.
Pattern-matching on a proof of &lt;span class=&quot;math inline&quot;&gt;\(m \leq n\)&lt;/span&gt; thus either reveals that &lt;span class=&quot;math inline&quot;&gt;\(m
= 0\)&lt;/span&gt;, or that &lt;span class=&quot;math inline&quot;&gt;\(m' \leq n'\)&lt;/span&gt; where &lt;span class=&quot;math inline&quot;&gt;\(m'\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(n'\)&lt;/span&gt; are the predecessors of
&lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;: in other words, it facilitates counting &lt;em&gt;down&lt;/em&gt;, just
like matching directly on &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; would.&lt;/p&gt;
&lt;p&gt;However, there is an alternative way to define the &lt;span class=&quot;math inline&quot;&gt;\(\leq\)&lt;/span&gt; relation,
which we will call &lt;span class=&quot;math inline&quot;&gt;\(\leq'\)&lt;/span&gt;.
We can choose &lt;em&gt;reflexivity&lt;/em&gt; of &lt;span class=&quot;math inline&quot;&gt;\(\leq'\)&lt;/span&gt; as a base case, that is, &lt;span class=&quot;math inline&quot;&gt;\(n \leq'
n\)&lt;/span&gt; for any &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;. We can then &lt;em&gt;decrement&lt;/em&gt; the left-hand side every time
we apply another constructor. Like so:&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb82&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb82-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb82-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;≤′&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb82-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb82-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  lerefl &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; n ≤′ n&lt;/span&gt;
&lt;span id=&quot;cb82-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb82-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  lesuc &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; suc m ≤′ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤′ n&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Pattern-matching on a proof of &lt;span class=&quot;math inline&quot;&gt;\(m \leq' n\)&lt;/span&gt; thus facilitates counting
&lt;em&gt;up&lt;/em&gt; from &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; to &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, just like we wanted!&lt;/p&gt;
&lt;p&gt;We can also prove a few lemmas about properties of &lt;span class=&quot;math inline&quot;&gt;\(\leq'\)&lt;/span&gt;. For
example, the two axioms that define the usual &lt;span class=&quot;math inline&quot;&gt;\(\leq\)&lt;/span&gt; can be proved as
lemmas.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb83&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb83-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb83-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤′-suc &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤′ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; suc m ≤′ suc n&lt;/span&gt;
&lt;span id=&quot;cb83-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb83-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤′-suc lerefl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; lerefl&lt;/span&gt;
&lt;span id=&quot;cb83-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb83-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤′-suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;lesuc m≤′n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; lesuc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤′-suc m≤′n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb83-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb83-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb83-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb83-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;0≤′ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; ≤′ n&lt;/span&gt;
&lt;span id=&quot;cb83-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb83-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;0≤′ zero &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; lerefl&lt;/span&gt;
&lt;span id=&quot;cb83-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb83-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;0≤′ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; lesuc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤′-suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;0≤′ n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can also prove that &lt;span class=&quot;math inline&quot;&gt;\(m \leq n\)&lt;/span&gt; implies &lt;span class=&quot;math inline&quot;&gt;\(m \leq' n\)&lt;/span&gt;. (The converse
is true as well, and can be proved as an easy exercise, but we won’t need it.)&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb84&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb84-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb84-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤→≤′ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤ n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ≤′ n&lt;/span&gt;
&lt;span id=&quot;cb84-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb84-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤→≤′ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; n&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; zle &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; 0≤′ n&lt;/span&gt;
&lt;span id=&quot;cb84-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb84-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;≤→≤′ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle m≤n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤′-suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤→≤′ m≤n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So, we can pattern-match on a proof of &lt;span class=&quot;math inline&quot;&gt;\(m \leq' n\)&lt;/span&gt; to count up from &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt; to
&lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, but this isn’t quite good enough: while counting, we won’t remember the relationship of the
intermediate values to the original &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt;. We would like to be able to
count up through some interval, from some starting &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; to ending &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt;,
knowing all along the way that the values we count are contained in
the interval.&lt;/p&gt;
&lt;p&gt;To this end, we can create a data
type &lt;code&gt;i ∈[ a ⋯ b ]&lt;/code&gt; which represents a stage in counting from &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; to
&lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt;. The base case is when &lt;span class=&quot;math inline&quot;&gt;\(i = b\)&lt;/span&gt;; otherwise &lt;code&gt;i ∈[ a ⋯ b ]&lt;/code&gt; when &lt;span class=&quot;math inline&quot;&gt;\(a
\leq i\)&lt;/span&gt; and also &lt;code&gt;suc i ∈[ a ⋯ b ]&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb85&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb85-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb85-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;∈[&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;⋯&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;] &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb85-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb85-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  stop &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;a b &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; a ≤ b &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; b ∈[ a ⋯ b ]&lt;/span&gt;
&lt;span id=&quot;cb85-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb85-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  step &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;a i b &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; a ≤ i &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; suc i ∈[ a ⋯ b ] &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; i ∈[ a ⋯ b ]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can then write a function which “constructs a loop”—that is,
starting from a proof of &lt;span class=&quot;math inline&quot;&gt;\(a \leq' b\)&lt;/span&gt;, it builds
a value of type &lt;code&gt;a ∈[ a ⋯ b ]&lt;/code&gt; which represents a reified loop from
&lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; to &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt;. By pattern-matching on this value we can successively
increment from &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; up to &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt;, with the appropriate guarantees along
the way.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb86&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb86-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb86-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;loop &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a b &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a ≤′ b&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; a ∈[ a ⋯ b ]&lt;/span&gt;
&lt;span id=&quot;cb86-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb86-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;loop a b a≤′b &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; mid a a b ≤-refl a≤′b&lt;/span&gt;
&lt;span id=&quot;cb86-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb86-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;cb86-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb86-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  mid &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a i b &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;a ≤ i&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;i ≤′ b&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; i ∈[ a ⋯ b ]&lt;/span&gt;
&lt;span id=&quot;cb86-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb86-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  mid a i b a≤i lerefl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; stop a≤i&lt;/span&gt;
&lt;span id=&quot;cb86-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb86-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  mid a i b a≤i &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;lesuc i≤′b&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; step a≤i &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;mid a &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc i&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; b &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤-sucr a≤i&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; i≤′b&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally, we need as a simple lemma the fact that if &lt;code&gt;i ∈[ a ⋯ b ]&lt;/code&gt;
then in fact &lt;span class=&quot;math inline&quot;&gt;\(i \leq b\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb87&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb87-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb87-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;top &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;i a b &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; i ∈[ a ⋯ b ] &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; i ≤ b&lt;/span&gt;
&lt;span id=&quot;cb87-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb87-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;top &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;stop &lt;span class=&quot;ot&quot;&gt;_)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤-refl&lt;/span&gt;
&lt;span id=&quot;cb87-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb87-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;top &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;step &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; s&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ≤-sucl &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;top s&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level3&quot; id=&quot;primality-testing-by-trial-division&quot;&gt;
&lt;h3&gt;Primality testing by trial division&lt;/h3&gt;
&lt;p&gt;First, a lemma about downward closure: if we know &lt;span class=&quot;math inline&quot;&gt;\((\downarrow P)(m)\)&lt;/span&gt;,
and we know &lt;span class=&quot;math inline&quot;&gt;\(P(m)\)&lt;/span&gt;, then we know &lt;span class=&quot;math inline&quot;&gt;\((\downarrow P)(1 + m)\)&lt;/span&gt;. In other
words, if &lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt; holds for everything less than &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt;, we can extend it by
one by providing a proof that &lt;span class=&quot;math inline&quot;&gt;\(P\)&lt;/span&gt; also holds for &lt;span class=&quot;math inline&quot;&gt;\(m\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb88&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb88-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb88-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;extend &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;P &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;↓ P&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; m &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; P m &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;↓ P&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc m&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb88-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb88-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;extend &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; m&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; soFar Pm j &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;j ≟ m&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb88-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb88-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; yes refl &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Pm&lt;/span&gt;
&lt;span id=&quot;cb88-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb88-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; no j≢m &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle j≤m&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; soFar j &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤≢→&amp;lt; j≤m j≢m&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can define primality testing itself, via trial division. We
&lt;code&gt;loop&lt;/code&gt; from &lt;span class=&quot;math inline&quot;&gt;\(2\)&lt;/span&gt; up to &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, testing each number to see if it divides
&lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, keeping track along the way of the fact that all the numbers less
than our current trial divisor do not divide &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;. If the next divisor
does divide &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, we return it as proof that &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is composite. If it
does not, we &lt;code&gt;extend&lt;/code&gt; our accumulating proof of all the numbers that do
not divide &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, and proceed to the next. If we reach &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;, our
accumulated proof tells us that none of the numbers less than &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;
divide it, which is proof that &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is prime.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb89&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb89-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;prime? &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; ≤ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Prime n ⊎ Composite n&lt;/span&gt;
&lt;span id=&quot;cb89-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;prime? n 2≤n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; trialDiv &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;loop &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;≤→≤′ 2≤n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; noDivisorsUpTo2&lt;/span&gt;
&lt;span id=&quot;cb89-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-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;cb89-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  NoDivisorsUpTo &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  NoDivisorsUpTo &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ↓ &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; # &lt;span class=&quot;ot&quot;&gt;→&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 class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ¬ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;# ∣ n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  noDivisorsUpTo2 &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; NoDivisorsUpTo &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  noDivisorsUpTo2 &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc zero&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle &lt;span class=&quot;ot&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  noDivisorsUpTo2 &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc j&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle &lt;span class=&quot;ot&quot;&gt;()))&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  trialDiv &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; m ∈[ &lt;span class=&quot;dv&quot;&gt;2&lt;/span&gt; ⋯ n ] &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; NoDivisorsUpTo m &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; Prime n ⊎ Composite n&lt;/span&gt;
&lt;span id=&quot;cb89-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  trialDiv &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;stop 2≤n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; soFar &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; inj₁ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;2≤n , soFar&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  trialDiv &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;m&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;step 2≤m next&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; soFar &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; m ∣? n&lt;/span&gt;
&lt;span id=&quot;cb89-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; yes m∣n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; inj₂ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;m , 2≤m , top next , m∣n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb89-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb89-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; no pf &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; trialDiv next &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;extend soFar &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; pf&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;lists&quot;&gt;
&lt;h2&gt;Lists&lt;/h2&gt;
&lt;p&gt;Before we are able to state the Fundamental Theorem of Arithmetic, we need to build up a data type for lists, along with some standard list manipulation functions. First, we define the type of lists and the standard &lt;code&gt;foldr&lt;/code&gt; function.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb90&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb90-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb90-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;kw&quot;&gt;data&lt;/span&gt; List &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;A &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;kw&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb90-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb90-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  [] &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; List A&lt;/span&gt;
&lt;span id=&quot;cb90-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb90-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;∷&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; List A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; List A&lt;/span&gt;
&lt;span id=&quot;cb90-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb90-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb90-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb90-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foldr &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; List A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; B&lt;/span&gt;
&lt;span id=&quot;cb90-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb90-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foldr &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;amp;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; z [] &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; z&lt;/span&gt;
&lt;span id=&quot;cb90-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb90-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;foldr &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;amp;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; z &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ∷ xs&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; x &amp;amp; foldr &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;&amp;amp;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; z xs&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can define concatenation and product via &lt;code&gt;foldr&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb91&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb91-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb91-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;++&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; List A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; List A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; List A&lt;/span&gt;
&lt;span id=&quot;cb91-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb91-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;xs ++ ys &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; foldr &lt;span class=&quot;ot&quot;&gt;(_&lt;/span&gt;∷&lt;span class=&quot;ot&quot;&gt;_)&lt;/span&gt; ys xs&lt;/span&gt;
&lt;span id=&quot;cb91-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb91-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb91-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb91-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;product &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; List ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; ℕ&lt;/span&gt;
&lt;span id=&quot;cb91-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb91-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;product &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; foldr &lt;span class=&quot;ot&quot;&gt;_&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We will need &lt;code&gt;All&lt;/code&gt;, which expresses that some predicate holds of all
the elements of a list. In fact, &lt;code&gt;All&lt;/code&gt; is manifestly an instance of
&lt;code&gt;foldr&lt;/code&gt; as well, but we would need a universe-polymorphic version of &lt;code&gt;foldr&lt;/code&gt; for that, so we just write it manually.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb92&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb92-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb92-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;All &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;P &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; List A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb92-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb92-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;All P [] &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; ⊤&lt;/span&gt;
&lt;span id=&quot;cb92-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb92-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;All P &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ∷ xs&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; P x × All P xs&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, we just need a couple lemmas about concatenation: first, that if &lt;code&gt;P&lt;/code&gt; holds for all the elements in &lt;code&gt;xs&lt;/code&gt; and all the elements in &lt;code&gt;ys&lt;/code&gt;, then it holds for all the elements in &lt;code&gt;xs ++ ys&lt;/code&gt;; and second, that &lt;code&gt;product&lt;/code&gt; distributes over concatenation (&lt;em&gt;i.e.&lt;/em&gt; it is a homomorphism from the monoid of lists under concatenation to the monoid of natural numbers under multiplication).&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb93&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb93-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb93-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;All-++ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;P &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;xs ys &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; List A&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; All P xs &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; All P ys &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; All P &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;xs ++ ys&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb93-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb93-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;All-++ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;xs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; []&lt;span class=&quot;ot&quot;&gt;}&lt;/span&gt; Pxs Pys &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Pys&lt;/span&gt;
&lt;span id=&quot;cb93-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb93-3&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;All-++ &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;xs &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; ∷ &lt;span class=&quot;ot&quot;&gt;_}&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;Px , Pxs&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; Pys &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Px , All-++ Pxs Pys&lt;/span&gt;
&lt;span id=&quot;cb93-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb93-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;&lt;/span&gt;
&lt;span id=&quot;cb93-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb93-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;product-++ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;xs ys &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; List ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; product &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;xs ++ ys&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ≡ product xs * product ys&lt;/span&gt;
&lt;span id=&quot;cb93-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb93-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;product-++ [] ys &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; sym &lt;span class=&quot;ot&quot;&gt;(_&lt;/span&gt; +0&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb93-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb93-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;product-++ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x ∷ xs&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ys &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; trans &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;x *&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ product-++ xs ys&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sym &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;*-assoc x &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;product xs&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;product ys&lt;span class=&quot;ot&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;the-fundamental-theorem-of-arithmetic&quot;&gt;
&lt;h2&gt;The Fundamental Theorem of Arithmetic&lt;/h2&gt;
&lt;p&gt;Finally, we can put all the pieces together to state and prove (one
half of) the Fundamental Theorem of Arithmetic! &lt;code&gt;FTA n&lt;/code&gt; says that for
some positive integer &lt;code&gt;n&lt;/code&gt;, we can find a list of natural numbers which
are all prime, and whose product is &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb94&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb94-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb94-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;FTA &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dt&quot;&gt;Set&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb94-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb94-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;FTA n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; Σ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;List ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; ps &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; All Prime ps × product ps ≡ n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To prove that this holds for all positive integers, we can again use
well-founded induction: in the base case, if &lt;span class=&quot;math inline&quot;&gt;\(n = 1\)&lt;/span&gt;, the empty list
suffices. Otherwise, we can decide whether &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is prime. If so, the
singleton list containing &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; fits the bill. Otherwise, &lt;span class=&quot;math inline&quot;&gt;\(n = ab\)&lt;/span&gt; where
&lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt; are both nontrivial divisors of &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;; by the induction
hypothesis, both can be factored into primes, and the list we want for
&lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is simply the concatenation of the factorizations for &lt;span class=&quot;math inline&quot;&gt;\(a\)&lt;/span&gt; and &lt;span class=&quot;math inline&quot;&gt;\(b\)&lt;/span&gt;.&lt;/p&gt;
&lt;div class=&quot;sourceCode&quot; id=&quot;cb95&quot;&gt;&lt;pre class=&quot;sourceCode agda&quot;&gt;&lt;code class=&quot;sourceCode agda&quot;&gt;&lt;span id=&quot;cb95-1&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-1&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fta &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; FTA n&lt;/span&gt;
&lt;span id=&quot;cb95-2&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-2&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;fta n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; wf-ind &lt;span class=&quot;ot&quot;&gt;{&lt;/span&gt;P &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(λ&lt;/span&gt; n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; FTA n&lt;span class=&quot;ot&quot;&gt;)}&lt;/span&gt; &amp;lt;-wf go n&lt;/span&gt;
&lt;span id=&quot;cb95-3&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-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;cb95-4&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-4&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  go &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;n′ &lt;span class=&quot;ot&quot;&gt;:&lt;/span&gt; ℕ&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; n′ &amp;lt; n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; n′ &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; FTA n′&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; &lt;span class=&quot;dv&quot;&gt;0&lt;/span&gt; &amp;lt; n &lt;span class=&quot;ot&quot;&gt;→&lt;/span&gt; FTA n&lt;/span&gt;
&lt;span id=&quot;cb95-5&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-5&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  go &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc zero&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; IH 0&amp;lt;n &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; [] , &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;tt , refl&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb95-6&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-6&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  go &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; IH 0&amp;lt;n &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; prime? &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle zle&lt;span class=&quot;ot&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb95-7&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-7&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; inj₁ P &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ∷ []&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;P , tt&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc $≡ &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;n *1&lt;span class=&quot;ot&quot;&gt;))))&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb95-8&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-8&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; inj₂ C &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; factorsOf &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; C&lt;/span&gt;
&lt;span id=&quot;cb95-9&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-9&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;((&lt;/span&gt;a , sle &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; , a&amp;lt;n , &lt;span class=&quot;ot&quot;&gt;_)&lt;/span&gt; , &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;b , sle &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; , b&amp;lt;n , &lt;span class=&quot;ot&quot;&gt;_))&lt;/span&gt; , ab≡n&lt;/span&gt;
&lt;span id=&quot;cb95-10&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-10&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      &lt;span class=&quot;kw&quot;&gt;with&lt;/span&gt; IH a a&amp;lt;n &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle zle&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb95-11&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-11&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;         &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; IH b b&amp;lt;n &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;sle zle&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;cb95-12&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-12&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;  &lt;span class=&quot;ot&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; ps₁ , Pps₁ , prod₁ &lt;span class=&quot;ot&quot;&gt;|&lt;/span&gt; ps₂ , Pps₂ , prod₂ &lt;span class=&quot;ot&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;ps₁ ++ ps₂&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; , &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;All-++ Pps₁ Pps₂&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt; ,&lt;/span&gt;
&lt;span id=&quot;cb95-13&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-13&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;    begin&lt;/span&gt;
&lt;span id=&quot;cb95-14&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-14&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      product &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;ps₁ ++ ps₂&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;      ≡[ product-++ ps₁ ps₂ ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb95-15&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-15&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      product ps₁ * product ps₂ ≡[ &lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt;*&lt;span class=&quot;ot&quot;&gt;_&lt;/span&gt; $≡ prod₁ ≡$≡ prod₂ ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb95-16&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-16&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      a * b                     ≡[ ab≡n ⟩≡&lt;/span&gt;
&lt;span id=&quot;cb95-17&quot;&gt;&lt;a href=&quot;https://byorgey.github.io/blog/rss#cb95-17&quot; tabindex=&quot;-1&quot;&gt;&lt;/a&gt;      suc &lt;span class=&quot;ot&quot;&gt;(&lt;/span&gt;suc n&lt;span class=&quot;ot&quot;&gt;)&lt;/span&gt;               ∎&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;section class=&quot;level2&quot; id=&quot;further-directions&quot;&gt;
&lt;h2&gt;Further Directions&lt;/h2&gt;
&lt;p&gt;That concludes our tour of the Fundamental Theorem of Arithmetic in Agda! If you’ve worked through the whole thing and
completed the proofs mostly on your own, congratulations! If you want
more practice, there are a lot of directions you could take this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Doing trial division all the way up to &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt; is silly; we can stop
when we get to &lt;span class=&quot;math inline&quot;&gt;\(\sqrt n\)&lt;/span&gt;. I think this would make for a nice
exercise (in fact, Taneb’s version in the Agda stdlib does this).&lt;/li&gt;
&lt;li&gt;Define “&lt;span class=&quot;math inline&quot;&gt;\(d\)&lt;/span&gt; is a proper divisor of &lt;span class=&quot;math inline&quot;&gt;\(n\)&lt;/span&gt;” to mean that that &lt;span class=&quot;math inline&quot;&gt;\(d \mid n\)&lt;/span&gt;
and &lt;span class=&quot;math inline&quot;&gt;\(2 \leq d &amp;lt; n\)&lt;/span&gt;, then show that the “is a proper divisor of” relation
is well-founded, and prove &lt;code&gt;fta&lt;/code&gt; via well-founded induction on that
relation instead. This might streamline some parts of the proof;
I’m not sure.&lt;/li&gt;
&lt;li&gt;The other half of the FTA says that the prime factorization is
&lt;em&gt;unique up to permutation&lt;/em&gt;. To prove this, one has to define
what it means for one list to be a permutation of another, and show that
if there are two different prime factorizations then one must be a
permutation of the other (if &lt;span class=&quot;math inline&quot;&gt;\(p\)&lt;/span&gt; is a prime from the first
factorization, then it divides the other factorization as well,
which means it must be equal to one of the primes in the other
factorization). I may write up this proof in a follow-up post.&lt;/li&gt;
&lt;/ul&gt;
&lt;/section&gt;

&lt;/section&gt;


&lt;section id=&quot;isso-thread&quot;&gt;
  &amp;lt;noscript&amp;gt;Javascript needs to be activated to view comments.&amp;lt;/noscript&amp;gt;
&lt;/section&gt;</description>
	<pubDate>Fri, 26 Jun 2026 00:00:00 +0000</pubDate>
</item>

</channel>
</rss>
