<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Csongor Kiss</title>
  <id>https://kcsongor.github.io/</id>
  <link rel="self" href="https://blog.csongor.co.uk/feed.xml" />
  <link rel="alternate" href="https://blog.csongor.co.uk/" />
  <updated>2026-11-05T12:00:00.000Z</updated>
  <author><name>Csongor Kiss</name></author>
  <entry>
      <title>Write your own kernel optimiser</title>
      <id>https://blog.csongor.co.uk/cachet-grids-and-tiles/</id>
      <link href="https://blog.csongor.co.uk/cachet-grids-and-tiles/" />
      <published>2026-11-05T12:00:00.000Z</published>
      <updated>2026-11-05T12:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Well-scheduled kernels cannot go wrong*</summary>
      <content type="html">&lt;p&gt;Well-scheduled kernels cannot go wrong*&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://blog.csongor.co.uk/cachet-grids-and-tiles/&quot;&gt;Read the article&lt;/a&gt;&lt;/p&gt;</content>
    </entry>
<entry>
      <title>It&apos;s a trap: testing coding agents on a language with surprising semantics</title>
      <id>https://blog.csongor.co.uk/loom/</id>
      <link href="https://blog.csongor.co.uk/loom/" />
      <published>2026-10-25T16:35:00.000Z</published>
      <updated>2026-10-25T16:35:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>A case study: how coding agents investigate unfamiliar runtime rules, and what an assumptions log changes.</summary>
      <content type="html">&lt;p&gt;A case study: how coding agents investigate unfamiliar runtime rules, and what an assumptions log changes.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://blog.csongor.co.uk/loom/&quot;&gt;Read the article&lt;/a&gt;&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Trait-Constrained Enums in Rust</title>
      <id>https://kcsongor.github.io/2025/06/10/gadts-in-rust</id>
      <link href="https://blog.csongor.co.uk/gadts-in-rust/" />
      <published>2025-06-10T00:00:00.000Z</published>
      <updated>2025-11-07T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Simulating Haskell-style GADTs with phantom witnesses and specialisation.</summary>
      <content type="html">&lt;p&gt;Rust doesn’t have GADTs (generalised algebraic data types), but we can get
surprisingly close with some creative type-level tricks.&lt;/p&gt;
&lt;p&gt;This post might look like a departure from my usual (in the sense of &lt;em&gt;typical&lt;/em&gt;,
not &lt;em&gt;frequent&lt;/em&gt;) Haskell posts since we’ll be writing Rust today. Don’t let that
fool you; we’ll just be writing Haskell in Rust.&lt;/p&gt;
&lt;p&gt;GADTs are a Haskell feature that let
constructors carry richer type information. They can enforce constraints or
refine type parameters per constructor — which is what we’ll achieve here in
Rust.&lt;/p&gt;
&lt;p&gt;As this post is mainly for Rust programmers, I’ll start by motivating why GADTs
are useful.
For that, we’ll build a small expression language and see where plain algebraic
data types fall short.
Then we’ll introduce GADTs to fix the problem, first through type refinement and
then with per-constructor constraints.
After that, we’ll move to Rust and reconstruct both mechanisms: type equality
witnesses (a known trick) and constraint witnesses (the new bit this post is
really about). You’ll know when we’ve switched from Haskell to Rust — the syntax
gets ugly.&lt;/p&gt;
&lt;h2 id=&quot;a-simple-expression-language&quot;&gt;A simple expression language&lt;/h2&gt;
&lt;p&gt;Let’s start with a small expression language, encoded as a Haskell datatype. It
supports defining integer literals, and adding them together.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; LitInt&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Add&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Rust equivalent of this is an enum with two constructors and more parentheses
(and explicit heap indirection).&lt;/p&gt;
&lt;p&gt;We can evaluate expressions recursively:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval b&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Evaluating &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)))&lt;/span&gt;&lt;/code&gt; yields &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;12&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This is not a very useful expression language, so let’s add another literal type
and another binary operator:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; LitInt&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Add&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; LitBool&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Or&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can write expressions like &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Or&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; False&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; True&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;.
But we can also write &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; False&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;, which shouldn’t
type-check!&lt;/p&gt;
&lt;p&gt;Worse, we’re in trouble when writing the return type of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ???&lt;/span&gt;&lt;/code&gt;.
What it should return depends on the input, but the input type doesn’t contain
enough information.&lt;/p&gt;
&lt;h2 id=&quot;more-precise-types-with-gadts&quot;&gt;More precise types with GADTs&lt;/h2&gt;
&lt;p&gt;GADTs let us say more about each constructor’s result type.
We can extend our &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;/code&gt; definition so that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; only exists for integers, and
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Or&lt;/span&gt;&lt;/code&gt; only for booleans.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GADTs&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  LitBool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Or&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bool&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt; is now &lt;em&gt;parameterised&lt;/em&gt; (this would read &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; in Rust),
and each constructor specifies the type of expression it builds.  &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;/code&gt; takes
an &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt; and produces an &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/code&gt;, and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; combines two &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/code&gt;s into
another &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;As a result, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; False&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt; is rejected at compile time
because the second operand has the wrong type.&lt;/p&gt;
&lt;p&gt;The evaluation function can now have a precise type:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Or&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval b&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;/code&gt; now takes an expression of any type, and returns a value of that type.
When pattern matching on &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt;, if we see a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;/code&gt;, we learn that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; is
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt;, so the result must be an integer.
In the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; branch, both sub-expressions are &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/code&gt;, so &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;/code&gt; produces two
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt;s which can be added together.&lt;/p&gt;
&lt;p&gt;In other words, we not only &lt;em&gt;restrict&lt;/em&gt; what types of expressions can be used
when constructing &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt;, but also &lt;em&gt;learn&lt;/em&gt; type information when destructuring
it.&lt;/p&gt;
&lt;p&gt;So far, every constructor fixes a concrete return type.
But what if we wanted to support other types that can also be added together?&lt;/p&gt;
&lt;h2 id=&quot;more-flexible-types-with-gadts&quot;&gt;More flexible types with GADTs&lt;/h2&gt;
&lt;p&gt;Let’s say we want to support &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Double&lt;/span&gt;&lt;/code&gt;s in our language too:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  LitDouble&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Double&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Double&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Doubles, being numeric values, can also be added together, but the current &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt;
constructor only works on integers. We can relax this by constraining the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt;
type parameter just in this constructor:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;     ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  LitDouble&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Double&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Double&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Num&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;/code&gt; part is a &lt;em&gt;type class constraint&lt;/em&gt; in Haskell, equivalent to a
trait bound in Rust.
In other words, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; can now take any two expressions of the same type, as long as that type
supports numeric operations.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;/code&gt; function simply gains one extra case:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitDouble&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Or&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval b&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; now supports &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitDouble&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1.0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitDouble&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 2.0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;.
Crucially, the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt; constraint is attached to just the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; constructor, not
the entire type. It’s still possible to construct &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitBool&lt;/span&gt;&lt;/code&gt; values, even though
booleans don’t support addition.&lt;/p&gt;
&lt;p&gt;Each &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; value carries evidence that its type parameter &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; satisfies &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Num&lt;/span&gt;&lt;/code&gt;.
When we pattern match on &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt;, the type checker brings that constraint into
scope automatically, allowing us to use &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt; in the corresponding branch.&lt;/p&gt;
&lt;p&gt;This is a subtle but powerful idea: constraints can be local to a constructor.
Even the precise return types we saw earlier are another form of locality —
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;/code&gt; locally records that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; is equal to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next, we’ll rebuild the expression language in Rust, and see how to emulate both
of these features: constructor-local type equalities and constructor-local
constraints.
To start adopting the Rust nomenclature, we’ll build an enum whose constructors
are trait-constrained.&lt;/p&gt;
&lt;h2 id=&quot;encoding-gadts-in-rust&quot;&gt;Encoding GADTs in Rust&lt;/h2&gt;
&lt;p&gt;We’ll be encoding both properties of GADTs:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Constructor-local type equalities&lt;/strong&gt; — like &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LitInt&lt;/span&gt;&lt;/code&gt; refining &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constructor-local constraints&lt;/strong&gt; — like &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; requiring &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We’ll start with the first one, since the idea is already well known in the Rust community.&lt;/p&gt;
&lt;p&gt;As a baseline, here’s the simple expression language, with the promised
parentheses and heap indirections:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    LitBool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    Or&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As things stand, we have the same issues as the original Haskell version, namely that we can construct invalid combinations, and can’t give a precise type to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;type-equality-witnesses&quot;&gt;Type equality witnesses&lt;/h3&gt;
&lt;p&gt;In Haskell, specifying the return type of a GADT allowed us to express a type
equality which the typechecker could then automatically use to unify the type
variable with the concrete type.&lt;/p&gt;
&lt;p&gt;This relies on the type checker’s ability to make progress with locally learned
information, which Rust doesn’t natively support. Our encoding will instead
rely on an explicit &lt;em&gt;witness&lt;/em&gt; of type equality, which we then use where Haskell
would use the GADT constraint.&lt;/p&gt;
&lt;aside id=&quot;margin-user-content-fn-is-invariance&quot; data-footnote-id=&quot;user-content-fn-is-invariance&quot; tabindex=&quot;-1&quot; class=&quot;callout footnote-sidenote&quot; data-callout=&quot;note&quot; data-pagefind-ignore=&quot;all&quot; role=&quot;note&quot;&gt;&lt;p class=&quot;callout-title&quot;&gt;&lt;a href=&quot;#user-content-fnref-is-invariance&quot; aria-label=&quot;Back to reference 1&quot;&gt;1&lt;/a&gt;&lt;/p&gt;&lt;div class=&quot;callout-content&quot;&gt;
&lt;p&gt;Using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; would make &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;B&lt;/span&gt;&lt;/code&gt; invariant, which is slightly more robust if lifetimes are involved. For this post, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&gt;&lt;/span&gt;&lt;/code&gt; is simpler and works fine. &lt;/p&gt;
&lt;/div&gt;&lt;/aside&gt;&lt;p&gt;In Rust, we can encode this concept as a zero-sized type:  &lt;sup&gt;&lt;a href=&quot;#user-content-fn-is-invariance&quot; id=&quot;user-content-fnref-is-invariance&quot; data-footnote-ref=&quot;&quot; aria-describedby=&quot;footnote-label&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; core&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;marker&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; Self &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;        Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; is our equality witness: a value of type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; is only constructible when &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; and
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;B&lt;/span&gt;&lt;/code&gt; are equal. &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;/code&gt; is the only &lt;em&gt;safe&lt;/em&gt; way to construct values of type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If you’ve seen this trick before, you can safely skim this part. The more
interesting bit is how to do the same thing for &lt;strong&gt;trait bounds&lt;/strong&gt;, not just type
equalities.&lt;/p&gt;
&lt;p&gt;We can now write &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; where each constructor stores a type equality witness:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    LitBool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    Or&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 42&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt; has type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;, because the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;()&lt;/span&gt;&lt;/code&gt; constructor forces the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; variable to unify with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;i64&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;typechecks, but&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;    // wrong type, expected an Expr&amp;#x3C;i64&gt; but got Expr&amp;#x3C;bool&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; false&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;doesn’t.&lt;/p&gt;
&lt;p&gt;This machinery allows us to &lt;em&gt;restrict&lt;/em&gt; the types of expressions that can be used
in &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt;, but how do we &lt;em&gt;learn&lt;/em&gt; type information?&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; expr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ???&lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt; // n is of type &apos;i64&apos;, we need to return &apos;A&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the Haskell version, the type equality bound by the GADT constructor is a
native language feature that the typechecker knows about, so it freely converts
between &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt; under a pattern match.&lt;/p&gt;
&lt;p&gt;In Rust, we created a custom encoding of type equality, and the typechecker
doesn’t (and shouldn’t, in general) use it to unify types.&lt;/p&gt;
&lt;p&gt;This means that we need to write a function that actually performs the conversion:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        unsafe&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;intrinsics&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;transmute_unchecked&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;transmute_unchecked&lt;/span&gt;&lt;/code&gt; is a very unsafe function in general, but in our case, we
only invoke it when we have a type equality witness available (which can only be
constructed via &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;/code&gt;), so we &lt;em&gt;know&lt;/em&gt; the types &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;B&lt;/span&gt;&lt;/code&gt; are actually equal.&lt;/p&gt;
&lt;p&gt;With this, we can now use the equality witnesses in the constructors to rewrite
the results into the desired &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; expr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt; // i64 -&gt; A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; left&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; right&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Or&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; left&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; right&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ||&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;trait-constraint-witnesses&quot;&gt;Trait constraint witnesses&lt;/h3&gt;
&lt;p&gt;The type equality witnesses from the previous section are relatively simple,
because the only thing we need to record about our type parameter is that it’s
equal to a known type in the local context.&lt;/p&gt;
&lt;p&gt;Trait implementations are more complicated, because we need to know how certain
functionality is implemented for our type parameter.&lt;/p&gt;
&lt;p&gt;Haskell’s GADTs store references to type class dictionaries in their
constructors — essentially dynamic dispatch.
While Rust supports dynamic dispatch via &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;dyn&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Trait&lt;/span&gt;&lt;/code&gt;, it’s severely limited
(requiring “object safe” traits), so we’ll need a different approach.&lt;/p&gt;
&lt;p&gt;We’ll start with a similar witness idea, but this time, the witness will record
the fact that a trait implementation exists for a type.&lt;/p&gt;
&lt;p&gt;We’ll define a witness for the existence of a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt;-like capability, corresponding
to the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Num&lt;/span&gt;&lt;/code&gt; constraint in the Haskell version.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Sized&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    _phantom&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ops&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; Self &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _phantom&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; can_add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ops&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;()&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; can only be constructed (via &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;can_add&lt;/span&gt;&lt;/code&gt;) if &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;/code&gt; supports the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; operation
with result type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;/code&gt;. This mirrors the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;/code&gt; constraint on the Haskell side.&lt;/p&gt;
&lt;p&gt;We can now extend our expression type with a constructor that carries this
constraint witness:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;enum&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; i64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    LitDouble&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;f64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; f64&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    LitBool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    Or&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This version of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;/code&gt; is the Rust analogue of the final Haskell GADT.
The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; constructor now carries a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; witness that proves
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; implements &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So far this handles the &lt;em&gt;construction&lt;/em&gt; side of the story, but not the
&lt;em&gt;destruction&lt;/em&gt; side. When we pattern match on an &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;, Rust doesn’t know
that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; satisfies the constraint carried by &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; expr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt; // cannot add `A` to `A`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To recover that information, we’ll need to encode it in a trait that can
selectively enable the operation based on the presence of a witness.&lt;/p&gt;
&lt;h3 id=&quot;using-specialisation-to-recover-constraints&quot;&gt;Using specialisation to recover constraints&lt;/h3&gt;
&lt;p&gt;We now want to &lt;em&gt;use&lt;/em&gt; the information stored in &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CanAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; when pattern
matching on an expression. In Haskell, this happens automatically:
matching on &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; brings the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Num&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt; constraint into scope.
Rust has no mechanism for this, so we’ll need an indirection.&lt;/p&gt;
&lt;p&gt;We’ll introduce a helper trait &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MaybeAdd&lt;/span&gt;&lt;/code&gt; that acts like a type class dictionary.
It provides an operation &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;maybe_add&lt;/span&gt;&lt;/code&gt;, which only exists when the type supports
addition. We’ll use specialisation to make that conditional.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;feature&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;specialization&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;trait&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MaybeAdd&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; maybe_add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rhs&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We define a &lt;em&gt;default&lt;/em&gt; implementation for all types:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MaybeAdd&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    default &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; maybe_add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _rhs&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; Self&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;        unreachable!&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;no Add implementation for this type&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and a &lt;em&gt;specialised&lt;/em&gt; implementation for types that implement &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;impl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ops&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Output&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MaybeAdd&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; for&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; maybe_add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rhs&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; Self&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; Self&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;        self &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rhs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this machinery, we can now use the constraint witness inside &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    match&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; expr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitDouble&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitBool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;maybe_add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Or&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ||&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rather than directly using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;/code&gt; (which we can’t, since &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; isn’t known to
implement &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ops&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; in this context), we delegate to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;maybe_add&lt;/span&gt;&lt;/code&gt;, which
uses specialisation to select the correct implementation at monomorphisation
time.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;rust&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;#[&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; eval_test&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; expr_int &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitInt&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;can_add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    assert_eq!&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;expr_int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 7&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    let&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; expr_double &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitDouble&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        let&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;LitDouble&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Is&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;refl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;        Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;can_add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Box&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    assert_eq!&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;expr_double&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;why-this-works&quot;&gt;Why this works&lt;/h3&gt;
&lt;p&gt;If you’re coming from Haskell, it might be surprising that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;/code&gt; works at all.
In Haskell, type class resolution is coupled with evidence generation: when the
compiler decides that a type satisfies a constraint, it also produces a
reference to the corresponding dictionary. If Rust worked the same, then that
algorithm would pick the catch-all default implementation of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MaybeAdd&lt;/span&gt;&lt;/code&gt; under
the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; arm of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;eval&lt;/span&gt;&lt;/code&gt;, because at that point, no information is known
about the type (and our &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CanAdd&lt;/span&gt;&lt;/code&gt; witness is invisible to the typechecker).&lt;/p&gt;
&lt;p&gt;However, Rust’s specialisation works differently. During type checking, the
compiler only checks that &lt;em&gt;some&lt;/em&gt; implementation of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MaybeAdd&lt;/span&gt;&lt;/code&gt; exists — it
doesn’t commit to which one.
This step is &lt;strong&gt;proof-irrelevant&lt;/strong&gt;: the fact that a trait is implemented matters,
but not which implementation it resolves to.&lt;/p&gt;
&lt;p&gt;The actual selection happens later, during &lt;strong&gt;monomorphisation&lt;/strong&gt;, once all type
parameters are concrete. At that point, the specialiser sees that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; i64&lt;/span&gt;&lt;/code&gt; (or
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; f64&lt;/span&gt;&lt;/code&gt;, etc.) and picks the more specific implementation that performs real
addition. The default &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;unreachable!&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;()&lt;/span&gt;&lt;/code&gt; version is never instantiated, precisely
because our witness mechanism disallows constructing expressions that try to add
values without &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;/code&gt; implementations.&lt;/p&gt;
&lt;p&gt;This is the crucial distinction between Haskell and Rust: in Haskell, dictionary
resolution is part of type checking; in Rust, it’s deferred until code
generation. The specialiser makes the final decision once it knows the concrete
types, and because our witness types restrict what can actually be constructed,
the correct implementation is always chosen.&lt;/p&gt;
&lt;aside id=&quot;margin-user-content-fn-zero-cost&quot; data-footnote-id=&quot;user-content-fn-zero-cost&quot; tabindex=&quot;-1&quot; class=&quot;callout footnote-sidenote&quot; data-callout=&quot;note&quot; data-pagefind-ignore=&quot;all&quot; role=&quot;note&quot;&gt;&lt;p class=&quot;callout-title&quot;&gt;&lt;a href=&quot;#user-content-fnref-zero-cost&quot; aria-label=&quot;Back to reference 2&quot;&gt;2&lt;/a&gt;&lt;/p&gt;&lt;div class=&quot;callout-content&quot;&gt;
&lt;p&gt;Luckily the cost analysis of abstractions doesn’t include developer ergonomics. &lt;/p&gt;
&lt;/div&gt;&lt;/aside&gt;&lt;p&gt;In effect, Rust’s specialisation system lets us recover local constraint
learning at compile time, without runtime dictionaries or dynamic dispatch.
Everything is resolved statically and erased before code generation.
A truly zero-cost abstraction!&lt;sup&gt;&lt;a href=&quot;#user-content-fn-zero-cost&quot; id=&quot;user-content-fnref-zero-cost&quot; data-footnote-ref=&quot;&quot; aria-describedby=&quot;footnote-label&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;h2 id=&quot;limitations&quot;&gt;Limitations&lt;/h2&gt;
&lt;p&gt;This technique has a few obvious caveats.&lt;/p&gt;
&lt;p&gt;First, it relies on &lt;strong&gt;specialisation&lt;/strong&gt;, which is still unstable and only
available on nightly Rust. The feature also has some unsound edge cases that
the compiler can’t currently detect, though this particular usage is benign
because it doesn’t overlap implementations in unsafe ways.&lt;/p&gt;
&lt;p&gt;Second, the design doesn’t generalise to &lt;strong&gt;existential types&lt;/strong&gt; — Rust simply
has no equivalent. We can simulate type refinement (as with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Expr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;), but not
“forgetting” type information safely.&lt;/p&gt;
&lt;p&gt;Finally, while the runtime cost is zero, the cognitive cost certainly isn’t.
The type signatures are verbose, the ergonomics are questionable, and the
amount of ceremony required to recover what Haskell gives you by default is
non-trivial.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Until now we’ve been preoccupied with whether or not we could.
Now it’s time to stop and think if we should.&lt;/p&gt;
&lt;section data-footnotes=&quot;&quot; class=&quot;footnotes&quot; data-all-sidenotes=&quot;&quot;&gt;&lt;h2 class=&quot;sr-only&quot; id=&quot;footnote-label&quot;&gt;Footnotes&lt;/h2&gt;
&lt;ol&gt;
&lt;li id=&quot;user-content-fn-is-invariance&quot; data-sidenote=&quot;margin-user-content-fn-is-invariance&quot;&gt;
&lt;p&gt;Using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt; would make &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;B&lt;/span&gt;&lt;/code&gt; invariant, which is slightly more robust if lifetimes are involved. For this post, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;rust&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;PhantomData&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; B&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&gt;&lt;/span&gt;&lt;/code&gt; is simpler and works fine. &lt;a href=&quot;#user-content-fnref-is-invariance&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 1&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;user-content-fn-zero-cost&quot; data-sidenote=&quot;margin-user-content-fn-zero-cost&quot;&gt;
&lt;p&gt;Luckily the cost analysis of abstractions doesn’t include developer ergonomics. &lt;a href=&quot;#user-content-fnref-zero-cost&quot; data-footnote-backref=&quot;&quot; aria-label=&quot;Back to reference 2&quot; class=&quot;data-footnote-backref&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;</content>
    </entry>
<entry>
      <title>Announcing generic-optics (&amp; generic-lens 2.0.0.0)</title>
      <id>https://kcsongor.github.io/2020/02/11/generic-lens-2</id>
      <link href="https://blog.csongor.co.uk/generic-lens-2/" />
      <published>2020-02-11T00:00:00.000Z</published>
      <updated>2020-02-11T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>generic-lens gets a new interface.</summary>
      <content type="html">&lt;p&gt;I’m happy to announce a new library,
&lt;a href=&quot;https://hackage.haskell.org/package/generic-optics&quot;&gt;generic-optics&lt;/a&gt;,
accompanied by version 2.0.0.0 of &lt;a href=&quot;https://hackage.haskell.org/package/generic-lens&quot;&gt;generic-lens&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;background&quot;&gt;Background&lt;/h2&gt;
&lt;p&gt;A few months ago, the folks at Well-Typed &lt;a href=&quot;https://www.well-typed.com/blog/2019/09/announcing-the-optics-library/&quot;&gt;announced the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;
library&lt;/a&gt;,
which aims to improve on the user experience compared to the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; library.
Oleg Grenrus has written an excellent &lt;a href=&quot;http://oleg.fi/gists/posts/2020-01-25-case-study-migration-from-lens-to-optics.html&quot;&gt;migration guide&lt;/a&gt;
from &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;, so please have a look there for some more background.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; is essentially a port of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; that is
compatible with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;, and is designed to be a drop-in replacement
for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt;. This means that if you’re already using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt;
with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; and decide to migrate to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;, you should be able to replace the
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; dependency with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; and expect things to just work.&lt;/p&gt;
&lt;h2 id=&quot;examples&quot;&gt;Examples&lt;/h2&gt;
&lt;p&gt;To explain why I’m so excited about &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;, I’m going to
compare a real-life workflow between &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First, language pragmas and imports:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; DataKinds&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; TypeApplications&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; DeriveGeneric&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Data.Generics.Product&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GHC.Generics&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the module &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Data.Generics.Product&lt;/span&gt;&lt;/code&gt; is shared between
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;When using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; with the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; library, we would import&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Control.Lens&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;, the import becomes&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Optics.Core&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we define a simple record:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MyRecord&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MyRecord&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bool&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  deriving&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Show&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;myRecord1 &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MyRecord&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;myRecord1 &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MyRecord&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With either library, we can view the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; field using
the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;field&lt;/span&gt;&lt;/code&gt; lens:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;lens|optics&gt; myRecord1 ^. field @&quot;a&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we ask what the type of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;field &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;/code&gt; is in GHCi, we already see
the advantage of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;’s opaque representation.&lt;/p&gt;
&lt;p&gt;Compare&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;lens&gt; :t field @&quot;a&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;field @&quot;a&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  :: (HasField &quot;a&quot; s t a b, Functor f) =&gt; (a -&gt; f b) -&gt; s -&gt; f t&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;with&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;optics&gt; :t field @&quot;a&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;field @&quot;a&quot; :: HasField &quot;a&quot; s t a b =&gt; Lens s t a b&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let us use the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;typed&lt;/span&gt;&lt;/code&gt; lens, which performs a type-directed lookup in
a product type, as long as there is a unique field with that type:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;lens|optics&gt; myRecord1 ^. typed @(Bool, Int)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(False,2)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When the type of the field is not unique (such as if we tried to retrieve a field
of type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt;), both &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; provides a helpful type error:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;lens|optics&gt; myRecord1 ^. typed @Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;#x3C;interactive&gt; error:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    • The type MyRecord contains multiple values of type Int.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      The choice of value is thus ambiguous. The offending constructors are:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      • MyRecord&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For situation likes this, both libraries provide a traversal called
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;types&lt;/span&gt;&lt;/code&gt; that focuses on all values of the given type.&lt;/p&gt;
&lt;p&gt;Let’s see what happens if we replace &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;typed&lt;/span&gt;&lt;/code&gt; with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;types&lt;/span&gt;&lt;/code&gt; in the above
example when using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;lens&gt; myRecord1 ^. types @Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;#x3C;interactive&gt;:43:14-23: error:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    • No instance for (Monoid Int) arising from a use of ‘types’&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This error is rather puzzling. Unless we know what’s going on under
the hood, it’s not obvious where the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Monoid&lt;/span&gt;&lt;/code&gt; constraint is coming from.&lt;/p&gt;
&lt;p&gt;Compare this with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;optics&gt; myRecord1 ^. types @Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;#x3C;interactive&gt;:32:1-23: error:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    • A_Traversal cannot be used as A_Getter&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Right! &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;types &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt; is a traversal, but &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;^.&lt;/span&gt;&lt;/code&gt; takes a getter!
Arguably this is a more helpful message. Consulting the documentation
of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;, we find the combinator we’re looking for: &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;^..&lt;/span&gt;&lt;/code&gt;, which returns
all the values focused on by a traversal:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;lens|optics&gt; myRecord1 ^.. types @Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;[0,1,2]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This now of course works in both libraries.&lt;/p&gt;
&lt;p&gt;To summarise, using the two libraries should be nearly identical as
long as everything goes well and we’re not hitting type errors.
Where &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; (but really, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; itself) shines is when things
do not go all that well, in which case the resulting error messages are a lot more
comprehensible.&lt;/p&gt;
&lt;h2 id=&quot;differences&quot;&gt;Differences&lt;/h2&gt;
&lt;p&gt;The above was just to give a little taste of using
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt;. The interface of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; is
intended to be largely identical to that of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;labels&quot;&gt;Labels&lt;/h3&gt;
&lt;p&gt;At the time of writing, the main difference is the support for overloaded
labels in &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt;, which allows writing&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;lens&gt; import Data.Generics.Labels ()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;lens&gt; myRecord1 ^. #a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I intend to add support for this for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; too, but it
isn’t implemented yet.&lt;/p&gt;
&lt;h2 id=&quot;changes-in-generic-lens&quot;&gt;Changes in generic-lens&lt;/h2&gt;
&lt;p&gt;To support this new interface, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; itself has undergone a major
reorganisation. I thought this was a good opportunity to clean some things
up and change the interface at places, which ultimately resulted in a new major
version bump.&lt;/p&gt;
&lt;p&gt;Most notably, GHC versions below 8.4 are no longer
supported. &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; (and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; too) promises good
performance by making sure that the generic overhead is eliminated at
compile time.  Doing so requires really careful coding practices, and
GHC’s optimiser changes between every version, which meant that
certain tricks that worked for 8.2 didn’t work for 8.6 and vice
versa. The result was horrible CPP macros to enable certain hacks on
certain versions of GHC. In the end, I decided it wasn’t worth the
effort to maintain these hacks for older versions of the compiler.&lt;/p&gt;
&lt;p&gt;I intend to write a blog post in the near future describing some of these
hacks, as they are quite interesting and potentially educational.&lt;/p&gt;
&lt;p&gt;For a more comprehensive list of changes, refer to the
&lt;a href=&quot;https://github.com/kcsongor/generic-lens/blob/master/generic-lens/ChangeLog.md&quot;&gt;changelog&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Thanks for reading this blog post, and I’m hope you’re as excited
about &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; as I am! Since this release required a major refactoring
and moving things around, it is possible that some documentation is out of date, or
certain functions are not exported from where you would expect. If you find anything
that looks off, please either open a pull request or let me know on the issue tracker!&lt;/p&gt;
&lt;p&gt;Finally, if you find &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; or &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;optics&lt;/span&gt;&lt;/code&gt; useful,
consider &lt;a href=&quot;https://github.com/sponsors/kcsongor&quot;&gt;buying me a coffee&lt;/a&gt;!&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Opaque constraint synonyms</title>
      <id>https://kcsongor.github.io/2019/09/25/opaque-constraint-synonyms</id>
      <link href="https://blog.csongor.co.uk/opaque-constraint-synonyms/" />
      <published>2019-09-25T00:00:00.000Z</published>
      <updated>2019-09-25T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Hiding internal details of constraint synonyms.</summary>
      <content type="html">&lt;p&gt;The list of type class constraints in a function signature can
sometimes get out of hand. In these situations, we can introduce a
type synonym (thanks to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ConstraintKinds&lt;/span&gt;&lt;/code&gt;) to avoid repetition.&lt;/p&gt;
&lt;p&gt;Say we want to group together the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Read&lt;/span&gt;&lt;/code&gt; constraints:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Serialise&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Read&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Serialise&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt; can be used anywhere where we require both constraints:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;roundtrip &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Serialise&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;roundtrip &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; read &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; show&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is great, because it means we no longer have to spell out &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Read&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;
whenever we need both, and we also improved readability, because
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Serialise&lt;/span&gt;&lt;/code&gt; conveys some additional domain-specific meaning.&lt;/p&gt;
&lt;p&gt;There’s a problem with this, however. If we ask GHCi about the type of
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;roundtrip&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :t roundtrip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;roundtrip :: (Show a, Read a) =&gt; a -&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;it will eagerly expand the type synonym, removing all traces of
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Serialise&lt;/span&gt;&lt;/code&gt;. Of course this is a well known problem of type
synonyms, so we generally avoid them in favour of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;newtype&lt;/span&gt;&lt;/code&gt;s.&lt;/p&gt;
&lt;p&gt;But there’s no analogous construction for constraints. Or is there?&lt;/p&gt;
&lt;h2 id=&quot;constraints-newtypes-kind-of&quot;&gt;Constraints newtypes (kind of)&lt;/h2&gt;
&lt;p&gt;To begin, we’re going to drop the type synonym in favour of the
“constraint synonym” technique, which is essentially the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Read&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Serialise&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Read&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Serialise&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In other words, we introduce a new type class with the required
superclass constraints, and a single catchall instance.&lt;/p&gt;
&lt;p&gt;So far, the status quo hasn’t improved though. GHC is quite renitent:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :t roundtrip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;roundtrip :: (Show a, Read a) =&gt; a -&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This happens because the compiler sees that there’s only one matching
instance, so it’s safe to pick that one, and it will do so. This point
is the important one: that there’s only one instance. So, if we could somehow
trick GHC into thinking that there are other options, then maybe it wouldn’t
be so eager to expand our constraints.&lt;/p&gt;
&lt;p&gt;So, we create an empty data type, only to be used internally:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Opaque&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, we satisfy the superclass constraints&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Read&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Opaque&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  readsPrec &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; undefined&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Show&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Opaque&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  showsPrec &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; undefined&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that these two instances only exist so that the constraint is
satisfied, but since the type is internal, the actual functions are
never going to be invoked.&lt;/p&gt;
&lt;p&gt;Finally, the key ingredient: an overlapping instance for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Serialise&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Opaque&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;OVERLAPPING&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Serialise&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Opaque&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, every time GHC sees a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Serialise&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt; constraint, it will no longer
be able to pick the catchall instance, in case &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; gets instantiated
to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Opaque&lt;/span&gt;&lt;/code&gt; later. Of course, this won’t happen, because we don’t
export &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Opaque&lt;/span&gt;&lt;/code&gt;, but it’s good enough for GHC.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :t roundtrip&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;roundtrip :: Serialise a =&gt; a -&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;a-real-world-example&quot;&gt;A real world example&lt;/h2&gt;
&lt;p&gt;You might say that the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Read&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt; example is perhaps overly
simplistic. I came up with this technique to solve a very real problem
in the &lt;a href=&quot;http://hackage.haskell.org/package/generic-lens-1.2.0.0&quot;&gt;generic-lens&lt;/a&gt; library.
This problem shows up at many places in the library, but to pick one, consider the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AsType&lt;/span&gt;&lt;/code&gt;
class:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; AsType&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a s &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  _Typed &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Prism&apos;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The exact meaning of the class is irrelevant here (but see the
&lt;a href=&quot;http://hackage.haskell.org/package/generic-lens-1.2.0.0/docs/Data-Generics-Sum-Typed.html#v:_Typed&quot;&gt;documentation&lt;/a&gt; if you’re interested). What
matters is that there’s a catchall instance defined for all types
(using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GHC.Generics&lt;/span&gt;&lt;/code&gt;), which in turn requires a large number of other constraints and predicates
to hold. Since this catchall instance is the only one defined by the library, asking for the
type of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;_Typed&lt;/span&gt;&lt;/code&gt; in GHCi eagerly expands the constraints to those of the instance.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t _Typed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;_Typed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ErrorUnlessOne&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;        a s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CollectPartialType&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;TupleToList&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;      Defined&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;TypeError&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ...&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (()&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Constraint&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Generic&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;      ListTuple&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;TupleToList&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GAsType&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;TupleToList&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;      Data.Profunctor.Choice.Choice&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Applicative&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;     p a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Not great. All the internal implementation details leak out. By
employing the opaque constraint trick above, we can define overlapping
instances for the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AsType&lt;/span&gt;&lt;/code&gt; class, which results in the following type signature:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t _Typed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;_Typed &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; AsType&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Prism&apos;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which is much nicer!&lt;/p&gt;
&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;
&lt;p&gt;I wrote most of this post a while time ago, but never published it.
Thanks to &lt;a href=&quot;https://twitter.com/rob_rix&quot;&gt;Rob Rix&lt;/a&gt; for bringing up this
topic and thus reminding me to publish it. It’s good to see library authors
care about the user experience of their library down to this level of detail,
and I hope this technique will be useful for many others!&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Tripping up type inference</title>
      <id>https://kcsongor.github.io/2019/09/18/ambiguous-tags</id>
      <link href="https://blog.csongor.co.uk/ambiguous-tags/" />
      <published>2019-09-18T00:00:00.000Z</published>
      <updated>2019-09-18T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Improving code readability by enforcing type annotations.</summary>
      <content type="html">&lt;p&gt;One of the main selling points of Haskell is that despite (or because)
of its strong static type system, it frees us from the burden of having
to spell out tedious type signatures everywhere.&lt;/p&gt;
&lt;p&gt;Type inference is a blessing, but sometimes it can also be a
curse. Inference too good can hinder the readability of code, because
the compiler knows what the type of an identifier is even when we
don’t. It’s not just readability though: correctness
can be imperilled too.&lt;/p&gt;
&lt;p&gt;As an example, consider the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tagged&lt;/span&gt;&lt;/code&gt; type, which allows
us to attach type information to some other type.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;newtype&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Tagged&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; k&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkTagged&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then we might want to define a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Person&lt;/span&gt;&lt;/code&gt; type consisting of a first
name and a last name, both of type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;String&lt;/span&gt;&lt;/code&gt;, tagged by (type-level)
symbols accordingly:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Person&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkPerson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;firstName&quot;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;lastName&quot;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can then construct values of this type:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Person&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkPerson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Joseph&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Knecht&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And here is the problem. Since both fields are constructed just with
the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;/code&gt; constructor, nothing is stopping us from mixing up the
field names if we misremember the ordering:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Person&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkPerson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Knecht&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Joseph&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We would wish to get a type error, but GHC happily infers that
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Joseph&quot;&lt;/span&gt;&lt;/code&gt; indeed has type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tagged&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;String&lt;/span&gt;&lt;/code&gt; for any &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt;,
thus it fits perfectly into the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;lastName&quot;&lt;/span&gt;&lt;/code&gt; field.&lt;/p&gt;
&lt;p&gt;We can fix this example by providing explicit type applications to
the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;/code&gt; constructor. Then, mixing up the order &lt;em&gt;is&lt;/em&gt; a type error.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Person&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkPerson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; @&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;lastName&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Knecht&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; @&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;firstName&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Joseph&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;results in:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    • Couldn&apos;t match type ‘&quot;lastName&quot;’ with ‘&quot;firstName&quot;’&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works, but these annotations are entirely optional, and if we
forget about them, we’re in trouble once again.&lt;/p&gt;
&lt;p&gt;To summarise, the problem is that GHC can infer the type of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Joseph&quot;&lt;/span&gt;&lt;/code&gt;,
and due to the generality of the result, it can also unify
it with any arbitrary tag.&lt;/p&gt;
&lt;p&gt;So the question is this: how do we stop GHC from inferring the type of
expressions like &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MkTagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Joseph&quot;&lt;/span&gt;&lt;/code&gt;? In other words, how do we enforce
that the tag must be provided by explicit type annotation?&lt;/p&gt;
&lt;h2 id=&quot;an-ambiguous-smart-constructor&quot;&gt;An ambiguous smart constructor&lt;/h2&gt;
&lt;p&gt;We’re going to write a smart constructor that can only be invoked by
explicit type annotation of the tag type.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; forall&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Tagged&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;???&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkTagged&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What to put in the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;???&lt;/span&gt;&lt;/code&gt; hole? The idea is that we want &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; in this
type to be &lt;em&gt;ambiguous&lt;/em&gt;, in other words, it should be impossible to
infer &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; even if we know what &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tagged&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;???&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt; is. If it can’t be
inferred, then GHC will insist that we specify a type annotation at
the use site for what &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; should be.&lt;/p&gt;
&lt;p&gt;The obvious thing to plug into &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;???&lt;/span&gt;&lt;/code&gt; would be &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; itself, but that
doesn’t work of course, because from knowing &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tagged&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t a&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; can be
trivially inferred.
For example, when given a value of type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tagged&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;firstName&quot;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/code&gt;,
we can infer that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; must be &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;firstName&quot;&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;As always (at least this seems to be a recurring theme here on my
blog), we reach for type families to solve this problem. In
particular, we define a rather funny-looking variant of the identity
type family, which I’m going to call &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Ambiguous&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Ambiguous&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; k&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; j &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Ambiguous&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first thing that might strike you is the kind signature:
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Ambiguous&lt;/span&gt;&lt;/code&gt; takes an argument of kind &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;k&lt;/span&gt;&lt;/code&gt;, and returns something of
kind &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;j&lt;/span&gt;&lt;/code&gt;. It helps to think of these kind parameters as additional
&lt;em&gt;inputs&lt;/em&gt; to the type family.&lt;/p&gt;
&lt;p&gt;That is, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Ambiguous&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;firstName&quot;&lt;/span&gt;&lt;/code&gt; will get stuck:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! Ambiguous &quot;firstName&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Ambiguous &quot;firstName&quot; :: j&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= Ambiguous &quot;firstName&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;because GHC doesn’t know at which &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;j&lt;/span&gt;&lt;/code&gt; we want to evaluate the type
family (and indeed, in principle this choice could change the
behaviour of the type family, since in GHC, type families are not
parametric).&lt;/p&gt;
&lt;p&gt;In order to properly reduce the family, we must provide the result
kind as an input, like so:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! (Ambiguous &quot;firstName&quot; :: Symbol)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(Ambiguous &quot;firstName&quot; :: Symbol) :: Symbol&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= &quot;firstName&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let us plug this type family into the type of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged&lt;/span&gt;&lt;/code&gt;, and see what happens.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; forall&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Tagged&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Ambiguous&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkTagged&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, when GHC’s given &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Ambiguous&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t&lt;/span&gt;&lt;/code&gt;, it can’t work out what &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt;
is. Why? Suppose we know that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Ambiguous&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;/code&gt;, that is, we
expect it to reduce to a symbol. That still doesn’t tell us anything
about the kind of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt;! According to the kind signature of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Ambiguous&lt;/span&gt;&lt;/code&gt;, the
kind of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; could be &lt;em&gt;anything&lt;/em&gt;. Indeed, the only way to disambiguate this
is to provide the kind of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt;. As the signature of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged&lt;/span&gt;&lt;/code&gt; does
not have an explicit kind annotation on &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt;, the only way to provide
the kind of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; is to provide &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; itself (since only visibly quantified
variables can be applied with visible type applications).&lt;/p&gt;
&lt;p&gt;Now, the following code&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Person&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkPerson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Joseph&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Knecht&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;results in the error:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    • Couldn&apos;t match type ‘Ambiguous t0’ with ‘&quot;firstName&quot;’&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To fix it, we now &lt;em&gt;must&lt;/em&gt; provide type applications:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Person&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;joseph &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MkPerson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;firstName&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Joseph&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mkTagged &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;lastName&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Knecht&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
    </entry>
<entry>
      <title>Most underrated vim features: C-a</title>
      <id>https://kcsongor.github.io/2019/09/12/underrated-vim-c-a</id>
      <link href="https://blog.csongor.co.uk/underrated-vim-c-a/" />
      <published>2019-09-12T00:00:00.000Z</published>
      <updated>2019-09-12T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>In which I present a hidden gem in the vim text editor.</summary>
      <content type="html">&lt;p&gt;The aim of this series of blog posts is to shed light on some of the
darker corners of the vim text editor that I have encountered over the
years. Each post will focus on one particular feature, and should take
no longer than a couple of minutes to read.&lt;/p&gt;
&lt;p&gt;Today, I’d like to talk about the &lt;code&gt;&amp;#x3C;C-a&gt;&lt;/code&gt; key sequence (that is,
control+a). It is extremely simple: pressing &lt;code&gt;&amp;#x3C;C-a&gt;&lt;/code&gt; searches the
current line (starting at the cursor position) for a number, then
increments it.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;this is a number: 10.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;^&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;&amp;#x3C;C-a&gt;&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;this is a number: 11.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                   ^&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;^&lt;/code&gt; marks the cursor position.&lt;/p&gt;
&lt;p&gt;Its inverse is &lt;code&gt;&amp;#x3C;C-x&gt;&lt;/code&gt;, which decrements the number.
We can also specify a count, for example &lt;code&gt;20&amp;#x3C;C-x&gt;&lt;/code&gt; will result in:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;this is a number: -9.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                   ^&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hexadecimal and binary numbers are supported too.
For example, to convert &lt;code&gt;192&lt;/code&gt; to hex, we can do&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;this is a hexadecimal number: 0x0.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;^&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;192&amp;#x3C;C-a&gt;&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;this is a hexadecimal number: 0xc0.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 ^&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
    </entry>
<entry>
      <title>Global Implicit Parameters</title>
      <id>https://kcsongor.github.io/2019/07/11/global-implicit-parameters</id>
      <link href="https://blog.csongor.co.uk/global-implicit-parameters/" />
      <published>2019-07-11T00:00:00.000Z</published>
      <updated>2019-07-11T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Exploiting GHC internals to define top-level implicit parameters.</summary>
      <content type="html">&lt;p&gt;Implicit parameters (enabled with the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ImplicitParams&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/code&gt; pragma) provide a way to dynamically bind variables in Haskell.&lt;/p&gt;
&lt;p&gt;For example, the following function can be called in any context where &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;/code&gt; is bound:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foo &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foo &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bar &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bar &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; let&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; foo&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unlike type classes, implicit parameters are bound locally. But what
if we want to bind one in the global scope? This would allow a global
“default” value, which could then be shadowed locally.&lt;/p&gt;
&lt;p&gt;Unfortunately, the following is syntactically invalid:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 21&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We turn to the &lt;a href=&quot;https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#implicit-parameter-bindings&quot;&gt;GHC User Manual&lt;/a&gt;,
only to be further discouraged:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;A group of implicit-parameter bindings may occur anywhere a normal group of Haskell bindings can occur, except at top level.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Of course, we won’t let mere syntactic restrictions to get in our way.&lt;/p&gt;
&lt;h2 id=&quot;under-the-hood&quot;&gt;Under the hood&lt;/h2&gt;
&lt;p&gt;Since global binding of implicit parameters is officially not possible,
we need to turn to unofficial methods.
To begin, we pass the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;ddump&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;tc&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;trace&lt;/span&gt;&lt;/code&gt; flag
to GHC and recompile the module containing &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foo&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bar&lt;/span&gt;&lt;/code&gt;.
This makes GHC dump information about what it’s doing during typechecking
the module. There is quite a lot of output, but one line looks interesting:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;canEvNC:cls ghc-prim-0.5.3:GHC.Classes.IP [&quot;x&quot;, Int]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Good software engineering practice dictates code reuse, and we all
know that GHC is a well-engineered piece of software. Therefore, it is
not surprising to find that implicit parameters are implemented by
piggybacking off of type class resolution with some additional rules
to disregard issues like global coherence.&lt;/p&gt;
&lt;p&gt;As the above line suggests, implicit parameter resolution is desugared into
the resolution of the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GHC.Classes.IP&lt;/span&gt;&lt;/code&gt; type class from &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;ghc&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;prim&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Even though this module is &lt;a href=&quot;http://hackage.haskell.org/package/ghc-prim-0.5.3&quot;&gt;not documented&lt;/a&gt;, we
can import it and ask GHCi for more information:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; IP&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  ip &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MINIMAL&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; ip #-}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It looks like GHC generates instances of the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;IP&lt;/span&gt;&lt;/code&gt; class on the fly
whenever it sees a binder for an implicit parameter. The name
of the parameter is represented as a type-level symbol. The functional
dependency allows the variable’s type to be resolved just from its name.&lt;/p&gt;
&lt;p&gt;Let’s try to write an instance for this class by hand:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- ?x = 21&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; IP&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;x&quot;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  ip &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 21&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;GHC happily accepts this definition. Indeed, we can now write&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;baz &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;baz &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which evaluates to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;21&lt;/span&gt;&lt;/code&gt;, by picking up the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;/code&gt; variable from the
top-level scope. As expected, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; in&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; foo&lt;/span&gt;&lt;/code&gt; still evaluates to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;10&lt;/span&gt;&lt;/code&gt;, as it
&lt;em&gt;shadows&lt;/em&gt; the top-level binding.&lt;/p&gt;
&lt;h2 id=&quot;barewords&quot;&gt;Barewords&lt;/h2&gt;
&lt;p&gt;Perhaps this is a good place to stop. But we can go further:
above, we defined only the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;/code&gt; variable. It turns out
that we can define an instance for &lt;em&gt;all&lt;/em&gt; symbols at once:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; KnownSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; IP&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  ip &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; symbolVal &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Proxy&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This instance brings all possible implicit variables into scope, and
assigns their name their value by reflecting the symbol into a string.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bye &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bye &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;thanks &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot; &quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ++&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;for &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot; &quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ++&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;reading&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which &lt;em&gt;almost&lt;/em&gt; feels like writing Perl!&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Detecting the undetectable: custom type errors for stuck type families</title>
      <id>https://kcsongor.github.io/2018/11/29/report-stuck-families</id>
      <link href="https://blog.csongor.co.uk/report-stuck-families/" />
      <published>2018-11-29T00:00:00.000Z</published>
      <updated>2018-11-30T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Stuck type families are notorious for producing puzzling type errors. In this post I present a technique for detecting and reporting them.</summary>
      <content type="html">&lt;p&gt;Custom type errors are a great way to improve the usability of Haskell
libraries that utilise some of the more recent language extensions.
Yet anyone who has written or used one of these libraries will know
that despite the authors’ best efforts, there are still many occasions
where a wall of text jumps out, leaving us puzzled as to what went wrong.&lt;/p&gt;
&lt;p&gt;This post is about one particular class of such errors that have been
troubling users of many modern Haskell libraries: stuck type
families.&lt;/p&gt;
&lt;p&gt;The following type error perfectly illustrates the problem. It is an
actual error &lt;a href=&quot;https://github.com/kcsongor/generic-lens/issues/73&quot;&gt;reported&lt;/a&gt;
on the issue tracker of the
&lt;a href=&quot;http://hackage.haskell.org/package/generic-lens&quot;&gt;generic-lens&lt;/a&gt;
library.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;• No instance for (Data.Generics.Product.Types.HasTypes&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                     (Data.Generics.Product.Types.Snd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                        (Data.Generics.Product.Types.InterestingOr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                           Description&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                           (Data.Generics.Product.Types.InterestingOr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                              Description&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                              (Data.Generics.Product.Types.Interesting&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 Description&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 (Rep Text)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 Name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 &apos;[Text, Sirname, None, Description])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                              (M1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 S&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 (&apos;MetaSel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    (&apos;Just &quot;name&quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    &apos;NoSourceUnpackedness&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    &apos;NoSourceStrictness&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    &apos;DecidedLazy)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 (Rec0 Name))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                              Name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                           (M1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                              C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                              (&apos;MetaCons &quot;M&quot; &apos;PrefixI &apos;False)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                              (S1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 (&apos;MetaSel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    &apos;Nothing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    &apos;NoSourceUnpackedness&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    &apos;NoSourceStrictness&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                    &apos;DecidedLazy)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                                 (Rec0 Multiple)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                           Name))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                     Description&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;                     Name)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    arising from a use of ‘types’&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Can you spot the problem? Even if you know what to look for,
it takes a good few seconds to locate the culprit. The goal of this
post is to turn the above into the following:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;• No instance for Generic Text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;   arising from a traversal over Description.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How could we possibly identify a lack of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt; instance from the
above? Let us have a closer look at that large type error. It is a
nested chain function of calls, such as &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Snd&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Interesting&lt;/span&gt;&lt;/code&gt;, which
are type families leaking out of the library’s implementation. The
reason we see these type families (as opposed to the result they
evaluate to), is because the computation is &lt;em&gt;stuck&lt;/em&gt;. The culprit is
the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Text&lt;/span&gt;&lt;/code&gt; part somewhere in the middle.&lt;/p&gt;
&lt;p&gt;It turns out that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; is an associated type family of the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt;
class:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Generic&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Rep&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Type&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thus, the reason &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Text&lt;/span&gt;&lt;/code&gt; is not defined is that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Text&lt;/span&gt;&lt;/code&gt; has no
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt; instance. Clearly, it’s unreasonable to expect users to keep
such implementation details in mind and hunt for unreduced occurrences
of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; in their type errors to find out what the issue is!&lt;/p&gt;
&lt;p&gt;Yet, reporting this is not so easy. To explain why, we need to
understand the behaviour of type families.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;As things stand today, the associated family &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; is not actually
connected to the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt; class as far as the type checker is
concerned.  This is why unreduced occurrences will not result in error
messages mentioning anything about &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt; in the first place.
&lt;a href=&quot;https://arxiv.org/abs/1706.09715&quot;&gt;Constrained type families&lt;/a&gt; offer a solution to this problem, but
they are not (yet) implemented in GHC.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;type-family-evaluation-semantics&quot;&gt;Type family evaluation semantics&lt;/h2&gt;
&lt;p&gt;The reduction of type families is driven by the constraint solver. To
the best of my knowledge, there is no formal specification for their
semantics, so I’m not going to attempt to give a comprehensive account
here either. Instead, let us just make some key observations about
how type families reduce.&lt;/p&gt;
&lt;p&gt;A type involving a type family is said to be &lt;em&gt;stuck&lt;/em&gt; if none of the
type family’s equations can be selected for the provided
arguments. Since &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Text&lt;/span&gt;&lt;/code&gt;s have no &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt; instance, there is
consequently no &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Text&lt;/span&gt;&lt;/code&gt; instance defined either. Thus, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Text&lt;/span&gt;&lt;/code&gt;
is stuck.&lt;/p&gt;
&lt;p&gt;How does “stuckness” propagate up a chain of function calls? Consider
the following type family:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Foo&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Foo&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;No matter what we pass in as the argument, the single equation will
always match. This means that even if we pass in a stuck type, such as
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Text&lt;/span&gt;&lt;/code&gt;, the equation can reduce to the right hand side (and get
stuck afterwards):&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! Foo (Rep Text)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= Rep Text&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In other words, we can think of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Foo&lt;/span&gt;&lt;/code&gt; as a type family that’s “lazy”
in its argument. Now consider the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bar&lt;/span&gt;&lt;/code&gt; type family:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bar&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Bar&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Maybe&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Maybe&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Bar&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, we first check if the argument is &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Maybe&lt;/span&gt;&lt;/code&gt;, in which case &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Maybe&lt;/span&gt;&lt;/code&gt;
is returned, otherwise we pick the second equation. Perhaps
surprisingly, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bar&lt;/span&gt;&lt;/code&gt; behaves the same as &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Foo&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! Bar (Rep Text)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= Rep Text&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two equations of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bar&lt;/span&gt;&lt;/code&gt; &lt;em&gt;agree&lt;/em&gt; with each other, because the first
one is a substitution instance of the second. GHC recognises this, and
decides that it is safe to drop the first equation in favour of the
second one.&lt;/p&gt;
&lt;p&gt;We can of course write disagreeing equations:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T2&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FooBar&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  FooBar&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  FooBar&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This time, notice that the first equation is not a substitution
instance of the second: it returns something other than the argument.&lt;/p&gt;
&lt;p&gt;GHC won’t optimise this case away anymore, and now instance matching
will have to consider both equations. A given equation matches, if the
argument unifies with the pattern, and is apart from all of the preceding
patterns (i.e. doesn’t match any of them).
The important thing here is that a stuck type is &lt;em&gt;not&lt;/em&gt; apart from any
other type, but neither does it match any other type. This means that&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! FooBar (Rep Text)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= FooBar (Rep Text)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FooBar&lt;/span&gt;&lt;/code&gt; gets stuck just when its argument does. We can think of
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FooBar&lt;/span&gt;&lt;/code&gt; as a type family that is “strict” in its argument.&lt;/p&gt;
&lt;p&gt;If we pass in a non-stuck value, evaluation proceeds as normal:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! FooBar Maybe&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= Maybe&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Maybe&lt;/span&gt;&lt;/code&gt; is apart from &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T1&lt;/span&gt;&lt;/code&gt; (they are different ground types), and
it unifies with the catch-all pattern &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So, if a type family that inspects its argument is given a stuck type,
then the resulting type will be stuck itself. Notice that we can’t
proceed any further: there is no way to detect if the argument was
stuck or not. This is why the type error above is so impenetrable.
If we ignore our argument like &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Foo&lt;/span&gt;&lt;/code&gt; does, then it just slips by, but
if we try to do something with it like &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FooBar&lt;/span&gt;&lt;/code&gt; does, we get stuck.&lt;/p&gt;
&lt;p&gt;Of course, I wouldn’t have written down all of these low-level details
about type family reduction if they didn’t lead to a solution!&lt;/p&gt;
&lt;h2 id=&quot;custom-type-errors&quot;&gt;Custom type errors&lt;/h2&gt;
&lt;p&gt;The mechanism of custom type errors is quite simple. The constraint
solver proceeds normally, reducing all type family equations and
solving all type class instances. If at the end, there are any
constraints of the form &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;TypeError&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ...&lt;/span&gt;&lt;/code&gt;, then the payload of the error
gets printed, otherwise any unsolved constraints are reported.&lt;/p&gt;
&lt;p&gt;As an example&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foo &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; TypeError&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Ouch&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foo &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;yields&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;• Ouch&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;even though &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;10&lt;/span&gt;&lt;/code&gt; clearly doesn’t have type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;()&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We want to produce a custom type error when the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; type family gets
stuck, and we’d like to continue normally otherwise. As discussed
above, there is no way to branch on whether a type family is stuck or
not.&lt;/p&gt;
&lt;p&gt;However, we now have all the necessary pieces: all we need to do is
to make sure that when &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; gets stuck, we leave a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;TypeError&lt;/span&gt;&lt;/code&gt; in the
residual constraints. To do this, we’re going to wrap the call to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; in
another type family, which will get stuck just when &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; is stuck. When
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; reduces, our wrapper reduces too. The additional piece is that
the wrapper will also hold a type error as its argument, which will
reside in the unsolved constraint in the stuck case, but disappear otherwise.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Break&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Constraint&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Type&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Type&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Constraint&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Break&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ((),&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Break&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ _  &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Break&lt;/span&gt;&lt;/code&gt; is the wrapper family. It takes a constraint, which will be
our type error. Then it forces its argument by testing against
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T1&lt;/span&gt;&lt;/code&gt;. Note that in both equations, the type family reduces to the
trivial constraint &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;()&lt;/span&gt;&lt;/code&gt;, but in the first case, we use &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;((),&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ())&lt;/span&gt;&lt;/code&gt; (a
tuple of two trivial constraints) to ensure that the equations
don’t optimise away, like they did with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bar&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, we introduce a type family to construct a custom error message:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; NoGeneric&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  NoGeneric&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; TypeError&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;No instance for &quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;:&amp;#x3C;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ShowType&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, consider what happens when we call &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Break&lt;/span&gt;&lt;/code&gt; with the stuck
argument &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Text&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! Break (NoGeneric Int) (Rep Text)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= Break (TypeError ...) (Rep Text)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;the type gets stuck, with a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;TypeError&lt;/span&gt;&lt;/code&gt; inside! However, when
called with a type where &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; is defined, such as &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bool&lt;/span&gt;&lt;/code&gt;, the type reduces
to the unit constraint, no mention of the type error.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! Break (NoGeneric Bool) (Rep Bool)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= () :: Constraint&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And with this, we can report errors for any stuck type family.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bar &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Break&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;NoGeneric&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Text&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Text&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bar &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;yields&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;• No instance for Generic Text&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;• In the expression: bar&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Using this technique, we can place custom type errors right where our
stuck type families are, and provide more contextual information about
what went wrong. We can even generalise the above to the following type family:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Any&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; k&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Assert&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;err &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Constraint&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;break &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Type&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Type&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; k&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; k &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Assert&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Any&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Assert&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ _ k &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; k&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which we can use at any point in a computation, not just in
constraints. &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Assert&lt;/span&gt;&lt;/code&gt; takes a type error, a potentially stuck
computation, and a value. If the computation is stuck, then the custom
error is presented, otherwise the value is passed through without any
errors. Here, strictness is forced by the same &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;T1&lt;/span&gt;&lt;/code&gt; trick, but this
time, to ensure that the right hand sides are also different, we
return the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Any&lt;/span&gt;&lt;/code&gt; type family in the first case.&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Parsing type-level strings in Haskell</title>
      <id>https://kcsongor.github.io/2018/11/28/symbol-parsing-haskell</id>
      <link href="https://blog.csongor.co.uk/symbol-parsing-haskell/" />
      <published>2018-11-28T00:00:00.000Z</published>
      <updated>2018-11-28T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>A technique for manipulating Symbols in GHC for type-level programming.</summary>
      <content type="html">&lt;p&gt;Haskell, as implemented in GHC, has a very rich language for expressing computations in types. Thanks to the
&lt;a href=&quot;https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html?highlight=datakinds#datatype-promotion&quot;&gt;DataKinds&lt;/a&gt;
extension, any inductively defined data type can be used not only at the term level, but also at the type level.
A notable exception are strings, which provide the main theme for today’s blog post.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;String&lt;/span&gt;&lt;/code&gt; type in Haskell is defined as a list of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Char&lt;/span&gt;&lt;/code&gt;s. However,
the type-level equivalent, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Symbol&lt;/span&gt;&lt;/code&gt;, is defined as a primitive in GHC,
presumably for efficiency. After all, the type checker passes these
types around, and the simpler their structure, the less potential work
the constraint solver needs to do.&lt;/p&gt;
&lt;p&gt;The problem is this: since &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Symbol&lt;/span&gt;&lt;/code&gt; is defined as a primitive, there
is no way to pattern match on its structure, and the only way to
interact with them are by using the built-in primitive operations,
namely appending and (efficient, constant-time) comparison.&lt;/p&gt;
&lt;p&gt;In this blog post, I will show how these primitives can be used to
recover the ability to do arbitrary introspection of these type-level
string literals, thereby enabling a whole range of applications where
statically known information can be exploited.&lt;/p&gt;
&lt;p&gt;The technique presented here was inspired by Daniel Winograd-Cort’s
&lt;a href=&quot;https://github.com/kcsongor/generic-lens/pull/69&quot;&gt;pull request for the generic-lens library&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;All of this is packaged into the
&lt;a href=&quot;https://github.com/kcsongor/symbols&quot;&gt;symbols&lt;/a&gt; library.&lt;/p&gt;
&lt;h1 id=&quot;motivation&quot;&gt;Motivation&lt;/h1&gt;
&lt;p&gt;I have &lt;a href=&quot;/purescript-safe-printf&quot;&gt;written&lt;/a&gt; about type-level symbol
parsing in PureScript to implement a type-safe &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;printf&lt;/span&gt;&lt;/code&gt;
function. (There, I achieved symbol decomposition by patching the
compiler, but no such thing is required here.)&lt;/p&gt;
&lt;p&gt;Reusing that example, we will be able to write&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t printf &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Wurble %d %d %s&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;printf &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Wurble %d %d %s&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; printf &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Wurble %d %d %s&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 20&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;foo&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Wurble 10 20 foo&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The implementation of the printf example using the technique described in this blog post can be found on
&lt;a href=&quot;https://github.com/kcsongor/symbols/blob/master/src/Data/Symbol/Examples/Printf.hs&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id=&quot;primitives&quot;&gt;Primitives&lt;/h1&gt;
&lt;p&gt;First, let’s have a look at the primitives GHC provides for
manipulating type of kind &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Symbol&lt;/span&gt;&lt;/code&gt;, namely &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;/code&gt; and
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CmpSymbol&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;These functions are implemented in the compiler, and exported from the
&lt;a href=&quot;http://hackage.haskell.org/package/base-4.12.0.0/docs/GHC-TypeLits.html&quot;&gt;GHC.TypeLits&lt;/a&gt; module:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;m &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;n &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; CmpSymbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;m &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;n &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Ordering&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that there is no &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Uncons&lt;/span&gt;&lt;/code&gt; primitive that returns the head (first
character) and the tail of the symbol. It turns out that we can
implement &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Uncons&lt;/span&gt;&lt;/code&gt; using the two primitives above.&lt;/p&gt;
&lt;h2 id=&quot;appendsymbol&quot;&gt;AppendSymbol&lt;/h2&gt;
&lt;p&gt;The fact that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;/code&gt; is a type family suggests a rather
straightforward semantics. It appends two symbols together resulting
in a third one:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;bar&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;foobar&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is to say, it should only go in one way, so to speak.&lt;/p&gt;
&lt;p&gt;However, if we have a look at the
&lt;a href=&quot;https://github.com/ghc/ghc/blob/1c2c2d3dfd4c36884b22163872feb87122b4528d/compiler/typecheck/TcTypeNats.hs#L835&quot;&gt;implementation&lt;/a&gt;
in GHC, we can see that there’s more going on. There are special rules
for the interaction of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;/code&gt; constraints with equality
constraints. In concrete terms, GHC will solve the following
constraint:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(AppendSymbol &quot;foo&quot; b ~ &quot;foobar&quot;) =&gt; (b ~ &quot;bar&quot;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is, if we know a prefix of a symbol, we can decompose it to get
the matching suffix. Morally, the actual signature of
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;/code&gt; would be closer to&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; m n &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r m &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n, r n &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; m&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But this can’t be expressed today in GHC (type family dependencies
only allow the inputs to be decided solely by the result, and no
such combination of inputs and outputs are allowed), so &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;/code&gt;
really is a lot more powerful than what the type system would like to admit!&lt;/p&gt;
&lt;p&gt;Even with the ability to decompose symbols, there is a problem,
however. This decomposition only works if we &lt;em&gt;know&lt;/em&gt; what the prefix
is. And in general, we need to know two out of the three symbols
involved in the constraint to get the third.&lt;/p&gt;
&lt;p&gt;As a result, the following won’t work:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bad &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; prefix suffix &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;hello world&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; suffix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bad &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t bad&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bad &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; prefix suffix &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;hello world&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; suffix&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;that is, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;suffix&lt;/span&gt;&lt;/code&gt; is unsolved.&lt;/p&gt;
&lt;p&gt;We might think that we can just try all possible characters as
potential prefixes until one matches, but that would require
backtracking in the constraint solver, and GHC’s constraint solver
doesn’t backtrack.&lt;/p&gt;
&lt;p&gt;That is, trying a prefix that doesn’t match results in an unsolvable
constraint:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bad&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;a&quot;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; suffix &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;hello world&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; suffix&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bad&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t bad&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bad&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;a&quot;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; suffix &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;hello world&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; suffix&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But since we can’t backtrack, there is no way to try a different
character once we’ve committed to a particular prefix.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;If we knew&lt;/em&gt; what the first character was, we could strip it off
and get the remaining symbol this way, which would allow us to
treat Symbols as a list of characters essentially.&lt;/p&gt;
&lt;h2 id=&quot;cmpsymbol&quot;&gt;CmpSymbol&lt;/h2&gt;
&lt;p&gt;It turns out that we can simply use alphabetical ordering to find out
what the first character of a string is. &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CmpSymbol&lt;/span&gt;&lt;/code&gt; compares two symbols,
and returns one of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LT&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;EQ&lt;/span&gt;&lt;/code&gt;, or &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GT&lt;/span&gt;&lt;/code&gt; as a result.&lt;/p&gt;
&lt;p&gt;Observe that for any string longer than one, it’s always true that the
string follows its first character alphabetically, and precedes any
character after its first one. As an example, consider the string
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;hello world&quot;&lt;/span&gt;&lt;/code&gt;, whose first character is &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h&lt;/span&gt;&lt;/code&gt;, and the letter after
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h&lt;/span&gt;&lt;/code&gt; is &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;i&lt;/span&gt;&lt;/code&gt;. Then we have&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&quot;h&quot; &amp;#x3C; &quot;hello world&quot; &amp;#x3C; &quot;i&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For strings of length one, they will simply return &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;EQ&lt;/span&gt;&lt;/code&gt; when compared
with their first character (themselves).&lt;/p&gt;
&lt;h1 id=&quot;decomposition&quot;&gt;Decomposition&lt;/h1&gt;
&lt;p&gt;We now put the pieces together to implement an uncons function for
symbols. First, we need &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Head&lt;/span&gt;&lt;/code&gt;, a function that returns the first
character of a symbol. Second, we will use &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Head&lt;/span&gt;&lt;/code&gt; to interact with
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;/code&gt; to retrieve the tail of the symbol. Doing this
repeatedly will allow us to turn a symbol into a list of characters,
which in turn can be consumed by ordinary type families.&lt;/p&gt;
&lt;h2 id=&quot;head&quot;&gt;Head&lt;/h2&gt;
&lt;p&gt;So, to find out what the first character of a symbol is, we just
need to find the last character in the ASCII table that precedes
our symbol. To do this reasonably efficiently, we use binary search.
Since indexing into a type-level list takes linear time, we use a
balanced binary search tree instead. Recall that symbol comparisons
are constant-time, so the whole operation is constant time (as we’re
working with a fixed size alphabet), so this optimisation simply
improves the constant factor by an order of magnitude.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Tree&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Leaf&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  deriving&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Show&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The printable subset of the ASCII character set can be encoded as the
following tree:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Chars&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;!&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;!&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;#&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;#&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;$&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;$&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;%&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;%&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&amp;#x26;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&amp;#x26;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&apos;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&apos;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;(&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;(&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;)&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;)&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;*&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;+&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;,&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;-&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;-&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;.&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;/&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;0&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;1&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;2&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;2&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;3&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;3&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;4&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;4&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;5&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;5&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;6&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;6&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;7&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;7&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;8&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;8&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;9&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;9&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;:&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&amp;#x3C;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&amp;#x3C;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;=&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;=&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;?&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;?&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;@&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;@&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;A&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;A&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;B&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;B&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;C&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;C&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;D&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;D&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;E&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;E&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;F&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;F&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;G&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;G&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;H&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;H&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;I&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;I&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;J&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;J&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;K&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;K&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;L&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;L&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;M&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;M&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;N&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;N&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;O&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)))))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;O&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;P&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;P&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Q&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Q&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;R&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;R&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;S&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;S&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;T&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;T&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;U&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;U&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;V&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;V&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;W&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;W&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;X&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;X&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Y&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Y&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;Z&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Z&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;[&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;[&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;]&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;]&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;^&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;^&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;_&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;_&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;`&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;`&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;a&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;b&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;c&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;d&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;d&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;e&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;e&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;f&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;f&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;g&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;g&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;h&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;h&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;i&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;i&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;j&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;j&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;k&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;k&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;l&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;l&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;m&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;m&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;n&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;n&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;o&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;o&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;p&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;p&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;q&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;q&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;r&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;s&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;s&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;t&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;t&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;u&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;u&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;v&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;v&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;w&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;x&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;y&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;z&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;z&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;{&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;{&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;|&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;|&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;}&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;}&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;~&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;~&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;~&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)))))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(I generated this structure with the help of other type families, but
found that inlining the result into the source file results in much
faster lookups.)&lt;/p&gt;
&lt;p&gt;Note that each node contains two consecutive characters: this is so
that we can easily decide when to stop: when the first element is less
than, and the second element is greater than our input string.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lookup&lt;/span&gt;&lt;/code&gt; type family (and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lookup2&lt;/span&gt;&lt;/code&gt;, to make up for a lack of local
declarations in type families) implements a standard binary search.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; LookupTable&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Tree&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Lookup&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;xs &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; LookupTable&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Lookup&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Node&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; l &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&apos;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;cl&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; cr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Lookup2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CmpSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; cl x&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;CmpSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; cr x&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x cl l r&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; family&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Lookup2&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; ol or x cl l r &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Lookup2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;EQ&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ _ cl _ _     &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; cl &lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- character matches&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Lookup2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LT&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ cl _ r   &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; cl &lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- found the right node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Lookup2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ _ cl _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Leaf&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; cl &lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- we&apos;re at the rightmost node (~)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Lookup2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ x _ _ r      &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Lookup&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x r &lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- go right&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Lookup2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _ x _ l _      &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Lookup&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x l &lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- go left&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Head&lt;/span&gt;&lt;/code&gt; is just a lookup in the binary tree.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Head&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Lookup&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Chars&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :kind! Head &quot;Wurble&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= &quot;W&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;uncons&quot;&gt;Uncons&lt;/h2&gt;
&lt;p&gt;Next, we need to interact the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;/code&gt; constraint with
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Head&lt;/span&gt;&lt;/code&gt;. We now turn to a type class, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Uncons&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Uncons&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;sym &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  uncons &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;sym&lt;/span&gt;&lt;/code&gt; is our symbol, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h&lt;/span&gt;&lt;/code&gt; is the head, and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; is the tail.  It would
be nice to have a functional dependency &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;sym &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; h t&lt;/span&gt;&lt;/code&gt;, but
unfortunately we can’t make that pass, as recall that the backwards
dependencies of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;/code&gt; are essentially hidden from the type
system.&lt;/p&gt;
&lt;p&gt;We write a single instance, which sets up the right constraints:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; h &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Head&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;	 ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; h t &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;	 )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Uncons&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym h t &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  uncons &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First, we write &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Head&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym&lt;/span&gt;&lt;/code&gt;, which unifies &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h&lt;/span&gt;&lt;/code&gt; with the first
element of the symbol using the binary lookup defined
previously. Then, the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;AppendSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; h t &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym&lt;/span&gt;&lt;/code&gt; constraint will trigger
the solution of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt;, due to the now known prefix &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;uncons&lt;/span&gt;&lt;/code&gt; member is not necessary for things to work out, but it
helps illustrate the working of the type class in the REPL:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :t uncons @&quot;foo&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;uncons @&quot;foo&quot; :: Proxy &apos;(&quot;f&quot;, &quot;oo&quot;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we can write the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Listify&lt;/span&gt;&lt;/code&gt; class to recursively break down a
symbol into a list of characters:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Listify&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;sym &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;result &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  listify &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;OVERLAPPING&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} nil &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; &apos;[]&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Listify&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; nil &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  listify &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Uncons&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym h t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt; 	 ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Listify&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t result&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; result&apos; &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; result&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;	 )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Listify&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym result&apos; &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  listify &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Proxy&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; :t listify @&quot;Hello&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;listify @&quot;Hello&quot; :: Proxy &apos;[&quot;H&quot;, &quot;e&quot;, &quot;l&quot;, &quot;l&quot;, &quot;o&quot;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And with this, we can parse anything we’d like.&lt;/p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Of course all of the above could be done a lot more efficiently with
compiler support, and there’s no reason for that not to happen at some
point in the future. This post is just a proof of concept that
something like this is already possible today, and the presented
technique is suitable for some lightweight applications. For anything
larger scale, Template Haskell is probably much better suited for the
job today.&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Deriving Bifunctor with Generics</title>
      <id>https://kcsongor.github.io/2017/12/31/generic-deriving-bifunctor</id>
      <link href="https://blog.csongor.co.uk/generic-deriving-bifunctor/" />
      <published>2017-12-31T00:00:00.000Z</published>
      <updated>2018-01-01T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>A techinque for approximating Generic2 with only the Generic type class.</summary>
      <content type="html">&lt;p&gt;Recently, I’ve been experimenting with deriving various type class instances
generically, and seeing how far we can go before having to resort to
TemplateHaskell.  This post is a showcase of one such experiment: deriving
&lt;a href=&quot;https://hackage.haskell.org/package/bifunctors&quot;&gt;Bifunctor&lt;/a&gt;, a type class that ranges
over types of kind &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; *&lt;/span&gt;&lt;/code&gt;, something &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GHC.Generics&lt;/span&gt;&lt;/code&gt; is known not to be
well suited for. The accompanying source code can be found in &lt;a href=&quot;https://gist.github.com/kcsongor/a8cb718f676c6ca1d999bfc56def9b7b&quot;&gt;this gist&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The problem&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://hackage.haskell.org/package/base-4.10.1.0/docs/GHC-Generics.html&quot;&gt;GHC.Generics&lt;/a&gt;
module defines two representations: &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic1&lt;/span&gt;&lt;/code&gt;. The former is used to describe
types of kind &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;/code&gt;, while the latter is used for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; *&lt;/span&gt;&lt;/code&gt;.
For example, the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic1&lt;/span&gt;&lt;/code&gt; representation is used in the &lt;a href=&quot;http://hackage.haskell.org/package/generic-deriving-1.12/docs/Generics-Deriving-Functor.html&quot;&gt;generic-deriving&lt;/a&gt; package’s Functor derivation.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GFunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gmap &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f b&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then instances are defined for the generic building blocks. Whenever we have a
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GFunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;, we can turn that into a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Functor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With this, it’s possible to derive many useful instances of classes that range
over &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;/code&gt; or &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; *&lt;/span&gt;&lt;/code&gt;. However, there’s no &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic2&lt;/span&gt;&lt;/code&gt;, so if we try to adapt &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;deriving&lt;/span&gt;&lt;/code&gt;’s
Functor approach to Bifunctors, we’ll run into problems.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  bimap &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p a c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p b d&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The type parameter &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p&lt;/span&gt;&lt;/code&gt; takes two arguments, but the generic &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep1&lt;/span&gt;&lt;/code&gt;
representations are strictly &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; *&lt;/span&gt;&lt;/code&gt; (in the case of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt;, the type parameter
is phantom — it’s only there so that much of the structure of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep1&lt;/span&gt;&lt;/code&gt;
can be shared, and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep1&lt;/span&gt;&lt;/code&gt; requires &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; *&lt;/span&gt;&lt;/code&gt;). This means that even if we
defined a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GBifunctor&lt;/span&gt;&lt;/code&gt;, we would need to require a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep2&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt; which
we could then turn into a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p&lt;/span&gt;&lt;/code&gt;. Alas, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep2&lt;/span&gt;&lt;/code&gt; doesn’t exist.&lt;/p&gt;
&lt;p&gt;Indeed, the deriving mechanism in the bifunctors package uses TH.&lt;/p&gt;
&lt;h2 id=&quot;the-solution&quot;&gt;The solution&lt;/h2&gt;
&lt;p&gt;The solution is inspired by how lenses implement polymorphic updates. The idea
is that a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lens&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s t a b&lt;/span&gt;&lt;/code&gt;  focuses on the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; inside some structure &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;/code&gt;, and if
we swap that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; with a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;/code&gt;, we get a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since we’re talking about Bifunctors now, we need two more type variables:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s t a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s x &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t x&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; will be the generic representations, which means they are of kind
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; *&lt;/span&gt;&lt;/code&gt;. However, we’re going to be using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt; instead of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic1&lt;/span&gt;&lt;/code&gt;, so
the type parameter &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;/code&gt; is not used.&lt;/p&gt;
&lt;p&gt;Unlike the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GFunctor&lt;/span&gt;&lt;/code&gt; class, which looked exactly like &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Functor&lt;/span&gt;&lt;/code&gt;, this one is a
lot different from &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;/code&gt;. Also important to note that &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;gbimap&lt;/span&gt;&lt;/code&gt;’s type
signature is more polymorphic than that of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bimap&lt;/span&gt;&lt;/code&gt;, so we need to ensure that
our instances are properly parametric.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;In an earlier version of this class, I had functional dependencies on the
class that expressed this interrelation between the type variables, but I had to
lose them so that more interesting instances could be defined (more on this
later).&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;the-boring-instances&quot;&gt;The boring instances&lt;/h3&gt;
&lt;p&gt;The first instance simply looks through the metadata node.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s t a b c d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;M1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; k m s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;M1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; k m t&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap f g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; M1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; gbimap f g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; unM1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A sum &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:+:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;/code&gt; can be turned into &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:+:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&apos;&lt;/span&gt;&lt;/code&gt; if we can turn &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l&lt;/span&gt;&lt;/code&gt; into &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l&apos;&lt;/span&gt;&lt;/code&gt; and
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;r&lt;/span&gt;&lt;/code&gt; into &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;r&apos;&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; l l&apos; a b c d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r r&apos; a b c d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;:+:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l&apos; &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;:+:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap f g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;L1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; l&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; L1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;gbimap f g l&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap f g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;R1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; R1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;gbimap f g r&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And similarly, for products.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; l l&apos; a b c d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r r&apos; a b c d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;:*:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l&apos; &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;:*:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap f g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:*:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; gbimap f g l &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:*:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; gbimap f g r&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The last boring instance is for unit types, these are trivially Bifunctors.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; U1&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; U1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap _ _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; id&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;incoherent-instances&quot;&gt;Incoherent instances&lt;/h3&gt;
&lt;p&gt;With all of the gluing out of the way, we can now get to the meat of the
problem: the actual fields in the constructors. When considering a field, we
have 3 cases:&lt;/p&gt;
&lt;p&gt;The field is of type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt;, and we apply the first function to turn it into a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap f _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;K1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; K1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similarly, if it’s a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c&lt;/span&gt;&lt;/code&gt;, we turn it into a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;d&lt;/span&gt;&lt;/code&gt; using the second function.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap _ g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;K1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; K1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;g a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, the field is neither &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt;, nor &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c&lt;/span&gt;&lt;/code&gt;, so we just leave it alone.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; x&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap _ _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; id&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that these instances need to be defined with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/code&gt; pragmas.
This is required because neither of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d&lt;/span&gt;&lt;/code&gt; is
more specific than the other.&lt;/p&gt;
&lt;p&gt;However, in our case, this is not a problem, because we’re going to invoke
instance resolution with polymorphic arguments, so there will be exactly one
instance that matches.&lt;/p&gt;
&lt;h3 id=&quot;default-signatures&quot;&gt;Default signatures&lt;/h3&gt;
&lt;p&gt;We can now revise our original class definition, and add a default signature
(&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;DefaultSignatures&lt;/span&gt;&lt;/code&gt;). This will make &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;/code&gt; derivable with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;DeriveAnyClass&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  bimap &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p a c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p b d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  default&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; bimap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    ::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p a c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;       ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p b d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;       ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p a c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p b d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;       )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p a c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; p b d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  bimap f g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; to &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; gbimap f g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; from&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note the line &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p a c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rep&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;p b d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d&lt;/span&gt;&lt;/code&gt;. Here’s where we
establish the relationship between the types. This now allows us to derive a
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;/code&gt; instance for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Either&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;deriving&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; instance&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bifunctor&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Either&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For example, when looking at the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Left&lt;/span&gt;&lt;/code&gt; constructor, the compiler will try to
find an instance for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d&lt;/span&gt;&lt;/code&gt;. There is exactly
one instance that matches this, so our incoherent instance will not bite us.
This is important: if instead we wanted an instance for a concrete type, say,
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Either&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/code&gt;, all of our incoherent instances would match, and an arbitrary
one would be picked. However, we avoid this problem by ensuring that the
instance is derived for the aformentioned polymorphic form.&lt;/p&gt;
&lt;p&gt;With this, we have a correct implementation of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bimap&lt;/span&gt;&lt;/code&gt; for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Either&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; bimap show (+ 10) (Left 10)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Left &quot;10&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; bimap show (+ 10) (Right 10)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Right 20&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even better, compiled with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;O1&lt;/span&gt;&lt;/code&gt;, all of the overhead from using generics is
optimised away:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;$fBifunctorEither_$cbimap&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  = \ @ a_a3EL @ b_a3EM @ c_a3EN @ d_a3EO f_X1EN g_X1EP eta_B1 -&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      case eta_B1 of {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        Left g1_a3X5 -&gt; Left (f_X1EN g1_a3X5);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;        Right g1_a3X8 -&gt; Right (g_X1EP g1_a3X8)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;a-few-more-instances&quot;&gt;A few more instances&lt;/h3&gt;
&lt;p&gt;The above deriving mechanism is naive: it only looks at fields whose types is
exactly &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; or &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;/code&gt;. But we can do better: what if the field is a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Maybe&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/code&gt;?
Surely we can turn that into a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Maybe&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;/code&gt;. Or if it’s an &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Either&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b&lt;/span&gt;&lt;/code&gt;, we can turn that into
an &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Either&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c d&lt;/span&gt;&lt;/code&gt;, since it has a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;/code&gt; instance.&lt;/p&gt;
&lt;p&gt;The following three instances do exactly that.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f a c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f b d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap f g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;K1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; K1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bimap f g a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Functor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap _ g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;K1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; K1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;fmap g a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Functor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap f _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;K1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; K1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;fmap f a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f a a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f b b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap f _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;K1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; K1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bimap f f a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; {-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;INCOHERENT&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-} &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GBifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f c c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Rec0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f d d&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b c d &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  gbimap _ g &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;K1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; K1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bimap g g b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can derive even more interesting &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Bifunctor&lt;/span&gt;&lt;/code&gt; instances.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Maybe&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Either&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; T2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Maybe&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  deriving&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Bifunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;We have seen a technique for approximating a hypothetical &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic2&lt;/span&gt;&lt;/code&gt;
representation with only using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;/code&gt;. Of course there was nothing specific
about the number 2, we can easily generalise this to any fixed number of
parameters.&lt;/p&gt;
&lt;p&gt;I’m planning on writing a post about a further generalisation of
this idea, which allows us to talk about types that have an arbitrary number type
parameters (unlike here, where it’s a fixed number), which I used in the
&lt;a href=&quot;https://hackage.haskell.org/package/generic-lens&quot;&gt;generic-lens&lt;/a&gt; library, to
allow for type changing lenses over any type parameter (thanks to the more
elaborate extra machinery, there is no need for incoherent instance
resolution).&lt;/p&gt;
&lt;p&gt;It would be interesting to see how far this can be pushed before hitting a
roadblock that would truly require a bespoke &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GenericN&lt;/span&gt;&lt;/code&gt; representation.&lt;/p&gt;
&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;
&lt;p&gt;Thanks to &lt;a href=&quot;https://github.com/adituv&quot;&gt;@adituv&lt;/a&gt; for pointing out that two instances were missing.&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Announcing generic-lens 0.5.0.0</title>
      <id>https://kcsongor.github.io/2017/12/10/generic-lens</id>
      <link href="https://blog.csongor.co.uk/generic-lens/" />
      <published>2017-12-10T00:00:00.000Z</published>
      <updated>2017-12-10T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Deriving lenses generically</summary>
      <content type="html">&lt;p&gt;The &lt;a href=&quot;https://hackage.haskell.org/package/generic-lens&quot;&gt;generic-lens&lt;/a&gt; library
provides utilities for deriving various optics for your datatypes,
using &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GHC.Generics&lt;/span&gt;&lt;/code&gt;. In this post I’ll go over some of the features and
provide examples of using them.&lt;/p&gt;
&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;/h2&gt;
&lt;p&gt;Lenses have proven to be an exteremely powerful tool in the Haskell ecosystem.
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generic&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lens&lt;/span&gt;&lt;/code&gt; uses &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;GHC.Generics&lt;/span&gt;&lt;/code&gt; to derive lenses and prisms on the fly, only
when they are needed. These optics are highly polymorphic, and can be used with
all types that are of the right shape. Extra care has been taken to keep type
errors readable.&lt;/p&gt;
&lt;h3 id=&quot;examples&quot;&gt;Examples&lt;/h3&gt;
&lt;p&gt;To get started, we will need the following extensions:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; DataKinds&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;        #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; DeriveGeneric&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FlexibleContexts&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; TypeApplications&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; TypeFamilies&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;     #-}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the following imports&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Control.Lens&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Data.Generics.Product&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; GHC.Generics&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Consider the following datatype:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Human&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Human&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    {&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; name    &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    ,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; age     &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    ,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; address &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    ,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; other   &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; deriving&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;field&quot;&gt;field&lt;/h4&gt;
&lt;p&gt;We can access the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;name&lt;/span&gt;&lt;/code&gt; field:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Human &quot;John&quot; 18 &quot;London&quot; True ^. field @&quot;name&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&quot;John&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can update fields too, even changing types where possible (when the type of
the field is a type parameter of the datatype):&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Human &quot;John&quot; 18 &quot;London&quot; True &amp;#x26; field @&quot;other&quot; %~ show&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Human {name = &quot;John&quot;, age = 18, address = &quot;London&quot;, other = &quot;True&quot;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In case of sum types, it only makes sense to have a lens on the fields that
appear in every constructor. Trying to use &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;field&lt;/span&gt;&lt;/code&gt; to get a lens for a partial
field is a type error.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Note that the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;field&lt;/span&gt;&lt;/code&gt; lens works with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;DuplicateRecordFields&lt;/span&gt;&lt;/code&gt;, which means that
record fields can actually be shared, and we can get a reusuble lens for all
cases without code duplication.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h4 id=&quot;typed&quot;&gt;typed&lt;/h4&gt;
&lt;p&gt;We can directly reference a field by its type, as long as the type is unique in
the structure.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Human &quot;John&quot; 18 &quot;London&quot; True ^. typed @Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;True&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Human &quot;John&quot; 18 &quot;London&quot; True ^. typed @String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;#x3C;interactive&gt;:34:34: error:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    • The type Human Bool contains multiple values of type [Char].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      The choice of value is thus ambiguous. The offending constructors are:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      • Human&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    • In the second argument of ‘(^.)’, namely ‘typed @String’&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      In the expression: Human &quot;John&quot; 18 &quot;London&quot; True ^. typed @String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      In an equation for ‘it’:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;          it = Human &quot;John&quot; 18 &quot;London&quot; True ^. typed @String&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;position&quot;&gt;position&lt;/h4&gt;
&lt;p&gt;When the above two fail, and we have a product type, we can specify the field
of interest by its position.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MyTuple&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; MyTuple&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a b &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;deriving&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; MyTuple 10 20 &amp;#x26; position @1 .~ &quot;hello&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;MyTuple &quot;hello&quot; 20&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;super-row-polymorphism&quot;&gt;super (row polymorphism)&lt;/h4&gt;
&lt;p&gt;Given two records, where the set of fields of one is the subset of that of the
other, we can talk about a structural subtype relationship. The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;super&lt;/span&gt;&lt;/code&gt; lens
allows us to treat the subtype as the supertype - without forgetting the
original structure.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Small&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Small&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    {&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; small &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; deriving&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Large&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Large&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    {&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; small &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    ,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; large &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    }&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; deriving&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Generic&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;smallFun &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Small&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Small&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;smallFun &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Small&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; n&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Small&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;n &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Here, we need the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; DuplicateRecordFields&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/code&gt; extension in
addition to the previous ones.)&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Large 10 &quot;foo&quot; &amp;#x26; super %~ smallFun&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Large {small = 20, large = &quot;foo&quot;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or we can simply upcast:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Large 10 &quot;foo&quot; ^. super :: Small&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Small {small = 10}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Small 10 ^. super :: Large&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;#x3C;interactive&gt;:53:13: error:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    • The type &apos;Small&apos; is not a subtype of &apos;Large&apos;.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      The following fields are missing from &apos;Small&apos;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;      • large&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;_ctor&quot;&gt;_Ctor&lt;/h4&gt;
&lt;p&gt;We can also obtain prisms that focus on individual constructors:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Human &quot;John&quot; 18 &quot;London&quot; True ^? _Ctor @&quot;Human&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Just (&quot;John&quot;,18,&quot;London&quot;,True)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&gt;&gt;&gt; Human &quot;John&quot; 18 &quot;London&quot; True ^? _Ctor @&quot;Human&quot; . position @3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Just &quot;London&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;mtl&quot;&gt;mtl&lt;/h3&gt;
&lt;p&gt;So far, we haven’t provided any type signatures. Indeed, everything can be
inferred by the compiler. However, because these combinators are highly
polymorphic, it might be interesting to use them in a polymorphic context.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;MonadReader&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; env m&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; HasField&apos;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;username&quot;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; env &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; m &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; view &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;field &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;username&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This function is now polymorphic not just in the monad stack it will eventually
run in, but also in the type of the environment.&lt;/p&gt;
&lt;p&gt;The type of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;field&lt;/span&gt;&lt;/code&gt; is&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;field &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; HasField&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; field s t a b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Lens&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s t a b&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;HasField&apos;&lt;/span&gt;&lt;/code&gt; (similarly to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lens&apos;&lt;/span&gt;&lt;/code&gt;) is a type synonym for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;HasField&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; field s s a a&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For a more comprehensive overview and more examples, please have a look at the
library on &lt;a href=&quot;https://hackage.haskell.org/package/generic-lens&quot;&gt;hackage&lt;/a&gt;, or on
&lt;a href=&quot;https://github.com/kcsongor/generic-lens&quot;&gt;github&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;performance&quot;&gt;Performance&lt;/h2&gt;
&lt;p&gt;An important question when evaluating such high-level abstractions is whether
the abstraction comes at the cost of performance. Fortunately, GHC optimises
away all of the overhead of the generic transformations, leaving us with code
that is equivalent to what we would’ve written manually.&lt;/p&gt;
&lt;p&gt;This can be verified by comparing the generated core of both the manually
written lens and the generated one. However, it happened multiple times during
development that a small change (such as eta-reduction) broke the optimisation.
Joachim Breitner’s excellent
&lt;a href=&quot;https://github.com/nomeata/inspection-testing&quot;&gt;inspection-testing&lt;/a&gt; tool, which
is now integrated into the automated test suite, is making sure that the
optimisation happens by automatically doing this comparison. This tool has been
invaluable in ensuring the performance guarantees, without having to manually
inspect the generated core after every single commit. The tests can be found
&lt;a href=&quot;https://github.com/kcsongor/generic-lens/blob/master/test/Spec.hs&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;It’s important to mention that as of this release, only the lenses are
optimised away completely, the prisms still have some leftover overhead. This
is planned to be fixed in a future release.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;quick-note-migration&quot;&gt;Quick note (migration)&lt;/h2&gt;
&lt;p&gt;In case you were already using the library, there are some breaking changes in &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;0.0&lt;/span&gt;&lt;/code&gt;.
Namely, all the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Has&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;*&lt;/span&gt;&lt;/code&gt; classes have been extended from 3 type parameters to 5.
Auxiliary constraint synonyms are provided, and migration should be relatively simple:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; HasField&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; field a record &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;becomes&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;f &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; HasField&apos;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; field record a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Notice the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;&apos;&lt;/span&gt;&lt;/code&gt; at the end of the class name, and the swapping of the last two arguments.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;
&lt;p&gt;Thanks to Matthew Pickering for useful comments on a draft of this post.&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Well-typed printfs cannot go wrong</title>
      <id>https://kcsongor.github.io/2017/09/25/purescript-safe-printf</id>
      <link href="https://blog.csongor.co.uk/purescript-safe-printf/" />
      <published>2017-09-25T00:00:00.000Z</published>
      <updated>2017-09-25T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Writing a simple parser of type-level symbols in PureScript 0.12</summary>
      <content type="html">&lt;p&gt;One of the classic examples that keeps coming up when talking about dependently
typed programming languages is the “safe” &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;printf&lt;/span&gt;&lt;/code&gt; function — one that ensures
that the number and type of arguments match the requirement in the format
specification.&lt;/p&gt;
&lt;p&gt;In languages like Idris, this is just a function that takes a format string,
and returns the type of arguments required for constructing the formatted
output string.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  format &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;A number: %d, and a string: %s&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Other languages, like rust, solve this by various means of metaprogramming:
writing a program (macro) that runs at compile-time, generating the program to
be executed at runtime.&lt;/p&gt;
&lt;p&gt;What these two approaches have in common is that they both operate on strings
that are statically available to the compiler. The aim of this post is to show
another way of achieving the same result, with tools that are available in
PureScript — a strongly-typed functional language, with no dependent types.&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The problem&lt;/h2&gt;
&lt;p&gt;We want to write a program that takes a format string, some number of
arguments, and returns the result of inserting the arguments at their specified
places in the format string, and does all this in a type-safe way.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t format &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Wurble %d %d %s&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; format &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Wurble %d %d %s&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 20&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;foo&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Wurble 10 20 foo&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; format &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;Wurble %d %d %s&quot;&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 10&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 20&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 30&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; found&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Could&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; not match type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  with type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;    Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;while trying to match type &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  with type &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;/code&gt; symbol before the string is the proxy syntax introduced in 0.12
which provides a concise way of passing types around. The format strings
are actually type-level literals — but more on this later.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Crucially, we need to compute a type from some input, but because PureScript
has no dependent types, values and functions in the traditional sense are not
available for evaluation at compile-time. However, there is a way to interact
with the compiler: via the type-checker.&lt;/p&gt;
&lt;p&gt;The solution therefore is to encode this computation in the types, and have the
type-checker evaluate it for us as part of type-checking. Luckily, PureScript
allows string literals in types (these are types whose kind is &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Symbol&lt;/span&gt;&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Thus, constructing our &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;printf&lt;/span&gt;&lt;/code&gt; function comprises two steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;parse the input &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Symbol&lt;/span&gt;&lt;/code&gt; into a list of format tokens&lt;/li&gt;
&lt;li&gt;generate the function from the format list that will then assemble the output string&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;type-level-parsing&quot;&gt;Type-level parsing&lt;/h2&gt;
&lt;p&gt;For the sake of simplicity, we’re going to focus on two types of format
specifiers: decimals (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;d&lt;/span&gt;&lt;/code&gt;) and strings (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;We represent these cases with a &lt;em&gt;custom kind&lt;/em&gt;, which is like a regular
algebraic datatype, but lifted to the type-level. This means that these
constructors can be used &lt;em&gt;in types&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;foreign&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; import&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; kind Specifier&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foreign import data D   &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Specifier&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;foreign&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; import&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; data S   &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Specifier&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;foreign&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; import&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; data Lit &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Specifier&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course, apart from the format specifiers &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;d&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;/code&gt;, everything else is a
literal, which we account for by wrapping them in the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;/code&gt; type constructor.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;foreign&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; import&lt;/span&gt;&lt;/code&gt; bit means that we’re introducing types here that have no
constructors. That is to say, it’s impossible to construct a value of type &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;D&lt;/span&gt;&lt;/code&gt;
and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;S&lt;/span&gt;&lt;/code&gt;.  We’ll see later how it is still possible to carry these types around
in terms (hint: proxies).&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Furthermore, we need a way of representing a sequence of these specifiers, for
which we introduce another kind:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;foreign&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; import&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; kind FList&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foreign import data FNil  &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FList&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;foreign&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; import&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; data FCons &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Specifier&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FList&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FList&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this, we can now write types like &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; D&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot; foo&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FNil&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;,
corresponding to the string &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;d foo&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Kind-polymorphism is not supported by the current version (0.12) of PureScript,
so we can’t define a parametric type-level list once and for all — we need a
new one for each type we want to store in lists. With this, and some syntactic
sugar, we would be able to write (as we can in Haskell today) &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;D&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;/code&gt;.
This limitation is likely to be removed in a future version of the compiler.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;With these building blocks defined, now we have a vocabulary for talking about
the parser itself: it is a function that takes a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Symbol&lt;/span&gt;&lt;/code&gt; as an input, and
returns a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FList&lt;/span&gt;&lt;/code&gt;. We encode the computation in the following type class:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;string &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;format &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FList&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; string &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; format&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The functional dependency &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;string &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; format&lt;/span&gt;&lt;/code&gt; states that the input &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;string&lt;/span&gt;&lt;/code&gt;
determines the ouput &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;format&lt;/span&gt;&lt;/code&gt;. This bit is crucial, as this is what tells the
compiler that knowing &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;string&lt;/span&gt;&lt;/code&gt; is sufficient in determining what the value of
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;format&lt;/span&gt;&lt;/code&gt; is. It is then our task to ensure that this dependency indeed holds,
when writing out the instances.&lt;/p&gt;
&lt;p&gt;To deconstruct the input symbol, we use the following type class available in 0.12:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ConsSymbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;head &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;                 (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;tail &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;                 (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;sym &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;                 sym &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; head tail, head tail &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; sym&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The interesting functional dependency here is the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;sym &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; head tail&lt;/span&gt;&lt;/code&gt;, which,
given some symbol, deconstructs it into its &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;head&lt;/span&gt;&lt;/code&gt; (the first character) and
its &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;tail&lt;/span&gt;&lt;/code&gt; — the rest.&lt;/p&gt;
&lt;p&gt;The parser is like a state machine, with the following legal states:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;State 1: found a non-&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;/code&gt; character&lt;/li&gt;
&lt;li&gt;State 2: found a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;/code&gt; character&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One possible way of representing these states is by having a separate type class
to deal with each.&lt;/p&gt;
&lt;p&gt;Since in our simplified example, we know that the specifier symbols can
only be single characters, we can define the second state as:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;head &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;out &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Specifier&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; head &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; out&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is, it takes a symbol, and returns the matching specifier. The
implementation is straightforward:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; parse2D &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse2&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;d&quot;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; D&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; parse2S &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse2&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;s&quot;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; S&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a partial function, which means that format strings that contain
unsupported specifier tokens will simply fail to compile.&lt;/p&gt;
&lt;p&gt;The first state is more complicated, as it can consume an arbitrary number of
characters, so we pass it the remaining string (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;tail&lt;/span&gt;&lt;/code&gt;) as well.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;head &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;tail &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;out &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FList&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; head tail &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; out&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Parse1&lt;/span&gt;&lt;/code&gt; represents the parsing state where we have the current character
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;head&lt;/span&gt;&lt;/code&gt;, the rest of the input string &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;tail&lt;/span&gt;&lt;/code&gt;, and we know that the previous
character was not a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The first case is when the tail is empty. In this case, we just return the
current character as the literal in a singleton list:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; parse1Nil &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FNil&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The second case is more interesting. This is when we find a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;/code&gt;, so we need to
invoke the other function, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Parse2&lt;/span&gt;&lt;/code&gt;, which handles parsing the specifier
itself. To do that, we use &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ConsSymbol&lt;/span&gt;&lt;/code&gt; to split our current tail &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;/code&gt; into
its head &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h&lt;/span&gt;&lt;/code&gt; and tail &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt;. &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;h&lt;/span&gt;&lt;/code&gt; contains the format specifier, which we pass on
to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Parse2&lt;/span&gt;&lt;/code&gt;. Then, recursively invoke &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Parse&lt;/span&gt;&lt;/code&gt; on &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t&lt;/span&gt;&lt;/code&gt; to parse the rest of
the input. In addition to returning &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;spec&lt;/span&gt;&lt;/code&gt; consed to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rest&lt;/span&gt;&lt;/code&gt;, we also put
an empty string literal at the head of the output list: this is to maintain
the invariant that the head of the output list always contains a string literal.
This invariant will be useful for the last case…&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; instance parse1Pc &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ConsSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; h t s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse2&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; h spec&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t rest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse1&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;%&quot;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; spec rest&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;…when we match any other character, i.e. other than &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;/code&gt;. Since we’re in
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Parse1&lt;/span&gt;&lt;/code&gt;, that means that the current character needs to be in a string
literal. For this, we first recursively parse the tail &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;/code&gt; into
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; acc&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;/code&gt;. The reason we want to know that at the head of parsing
the remaining string is a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;/code&gt; is so that we can prepend the current character
to that literal — we need to rebuild long string literals
character-by-character after all. This is where the invariant from the previous
two cases is useful: we don’t have to handle the cases where the head is not
a &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;/code&gt;, because the recursive calls guarantee that it is. &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;acc&lt;/span&gt;&lt;/code&gt; is thus the
tail of the string literal we’re currently parsing, so we put it together
with the current character by &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ConsSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; o acc rest&lt;/span&gt;&lt;/code&gt; (recall that this type
class can both construct and deconstruct symbols via its functional
dependencies). Then we simply return &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rest&lt;/span&gt;&lt;/code&gt; along with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;r&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; instance parse1Other &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; acc&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ConsSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; o acc rest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; o s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rest&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; r&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how these instances actually overlap. In the third case, we can
easily imagine a particular instantiation of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;o&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;r&lt;/span&gt;&lt;/code&gt; such that it
matches the instance head in the second case. In other words, when the current
character is &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;%&lt;/span&gt;&lt;/code&gt;, both &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;parse1Pc&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;parse1Other&lt;/span&gt;&lt;/code&gt; match (because
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;parse1Other&lt;/span&gt;&lt;/code&gt; is more general).&lt;/p&gt;
&lt;p&gt;To make sure that the instances are selected in the order we want them to be,
we use instance chains. That is, by writing &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; A&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; else instance &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;B&lt;/span&gt;&lt;/code&gt; we
tell the compiler to try to match instance &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;A&lt;/span&gt;&lt;/code&gt; first, and if it fails, then try
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;B&lt;/span&gt;&lt;/code&gt;. This is a new feature in PureScript 0.12, and a very powerful one — it
allows us to avoid the overlapping instance problem for good.&lt;/p&gt;
&lt;p&gt;Finally, we need to actually kick off the parser. We do this by invoking it
in the first state.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; parseNil &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  Parse&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FNil&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; instance parseCons &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ConsSymbol&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; h t string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse1&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; h t fl&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; string fl&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-the-sausage-gets-made-computing-the-output-type&quot;&gt;How the sausage gets made: computing the output type&lt;/h2&gt;
&lt;p&gt;But how do we know how many arguments we need to pass to the formatter? It
depends on the format string! No surprises here: just like all the previous
type-level computations, this one will also be encoded in a type class with a
functional dependency.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FormatF&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;format &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FList&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; fun &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; format &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; fun &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  formatF &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; @&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;format &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; fun&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;/code&gt; symbol is special syntax, and in this case, it means that the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;formatF&lt;/span&gt;&lt;/code&gt;
function takes an &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FList&lt;/span&gt;&lt;/code&gt; (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;format&lt;/span&gt;&lt;/code&gt;) as an input. But because &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FList&lt;/span&gt;&lt;/code&gt; is a
&lt;em&gt;custom kind&lt;/em&gt;, it has no value-level inhabitants. So, how can we still get
something whose type mentions &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;format&lt;/span&gt;&lt;/code&gt;? This is what &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;/code&gt; does — it’s a proxy
for a type. Its value is isomorphic to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Unit&lt;/span&gt;&lt;/code&gt;, and carries no information,
other than its type. Notice that it works for any kind — indeed, proxies are
currently a special-cased type in PureScript, in that they are kind-polymorphic.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Thus &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;formatF&lt;/span&gt;&lt;/code&gt; takes a format list, and an accumulator string, and returns some
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;fun&lt;/span&gt;&lt;/code&gt; — this type depends on the actual format list.&lt;/p&gt;
&lt;p&gt;Starting with the base case, when there’s nothing to print, simply just
return the accumulated formatted string.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; formatFNil &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FormatF&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FNil&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  formatF _ str &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; str&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When the head of the list is &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;D&lt;/span&gt;&lt;/code&gt;, we know that we will need an &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt; argument,
and the rest of the function’s type can be computed by recursing on the tail of
the list. As for the implementation, since the return type is now refined to
be of the form &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; fun&lt;/span&gt;&lt;/code&gt;, we are allowed to construct a lambda that takes
the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;/code&gt;, and appends it to the end of the accumulator, then recurses on the
rest. The implementation of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;S&lt;/span&gt;&lt;/code&gt; is identical, and is omitted for brevity.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; formatFConsD &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  FormatF&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rest fun&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FormatF&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FCons&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; D&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rest&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; fun&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  formatF _ str&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    =&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; \&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;i &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; formatF &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rest &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;str &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; show i&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Handling literals (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Lit&lt;/span&gt;&lt;/code&gt;) is left as an exercise for the reader.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Finally, as a matter of convenience, we can wrap the above type classes into
one, that serves as a bridge between the parser and the formatter, as such:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Format&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;string &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Symbol&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; fun &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; string &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; fun &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  format &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; @&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;string &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; fun&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; formatFFormat &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Parse&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; string format&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  ,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; FormatF&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; format fun&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  )&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Format&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; string fun &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  format _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; formatF &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;format &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And that’s it! It might be instructional to try and work out &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;FormatF&lt;/span&gt;&lt;/code&gt;’s
instance resolution for a few simple examples by hand, to get a better idea why
this works. A fully working implementation of the code in this post can be
found &lt;a href=&quot;https://github.com/kcsongor/purescript-safe-printf&quot;&gt;on github&lt;/a&gt;.&lt;/p&gt;</content>
    </entry>
<entry>
      <title>Time travel in Haskell for dummies</title>
      <id>https://kcsongor.github.io/2015/10/02/time-travel-in-haskell-for-dummies</id>
      <link href="https://blog.csongor.co.uk/time-travel-in-haskell-for-dummies/" />
      <published>2015-10-02T00:00:00.000Z</published>
      <updated>2015-10-02T00:00:00.000Z</updated>
      <author><name>Csongor Kiss</name></author>
      <summary>Some interesting consequences of lazy evaluation.</summary>
      <content type="html">&lt;p&gt;Browsing Hackage the other day, I came across the &lt;a href=&quot;https://hackage.haskell.org/package/tardis-0.3.0.0/docs/Control-Monad-Tardis.html&quot;&gt;Tardis
Monad&lt;/a&gt;.
Reading its description, it turns out that the Tardis monad is capable of
sending state back in time. Yep. Back in time.&lt;/p&gt;
&lt;h2 id=&quot;how&quot;&gt;How?&lt;/h2&gt;
&lt;p&gt;No, it’s not the reification of &lt;a href=&quot;https://en.wikipedia.org/wiki/Tachyon&quot;&gt;some hypothetical time-travelling
particle&lt;/a&gt;, rather a really clever way of
exploiting Haskell’s laziness.&lt;/p&gt;
&lt;p&gt;In this rather lengthy post, I’ll showcase some interesting consequences of
lazy evaluation and the way to work ourselves up from simple examples to ’time
travelling’ craziness through different levels of abstraction.&lt;/p&gt;
&lt;h2 id=&quot;the-repmax-problem&quot;&gt;The repMax problem&lt;/h2&gt;
&lt;p&gt;Imagine you had a list, and you wanted to replace all the elements of the list
with the largest element, by only passing the list once.  You might say
something like “Easier said than done, how do I know the largest element
without having passed the list before?”&lt;/p&gt;
&lt;p&gt;Let’s start from the beginning:
— First, you ask the future for the largest element of the list, (don’t worry,
this will make sense in a bit) let’s call this value &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep&lt;/span&gt;&lt;/code&gt; (as in the value we
replace stuff with).&lt;/p&gt;
&lt;p&gt;Walking through the list, you do two things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;replace the current element with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;’return’ the larger of the current element and the largest element of the remaining list.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When only one element remains, replace it with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep&lt;/span&gt;&lt;/code&gt;, and return what was there
originally. (this is the base case)&lt;/p&gt;
&lt;p&gt;Right, at the moment, we haven’t acquired the skill of seeing the future, so we
just write the rest of the function with that bit left out.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;repMax &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;repMax &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rep &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;repMax &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rep &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;repMax &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;l &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; ls&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rep &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;m&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rep &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; ls&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  where&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; ls&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; repMax ls rep&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;        m&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; max m l&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, it takes a list, and the rep element, and returns (Int, [Int])&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;repMax &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 6&lt;/span&gt;&lt;/code&gt;
gives us &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;])&lt;/span&gt;&lt;/code&gt; which is exactly what we wanted: the elements are
replaced with rep and we also have the largest element.
Now, all we need to do is use that largest element as &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep&lt;/span&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;doRepMax &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;doRepMax xs &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; xs&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  where&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;largest&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; xs&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; repMax xs largest&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;wait-what&quot;&gt;Wait, what?&lt;/h2&gt;
&lt;p&gt;This can be done thanks to lazy evaluation. Haskell systems use so-called
’thunks’ for values that are yet to be evaluated. When you say &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;min &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;, the
expression will form a thunk and not be evaluated until it really needs to be.
Here, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep&lt;/span&gt;&lt;/code&gt; can be thought of as a reference to a thunk. When we tell GHC to put
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;largest&lt;/span&gt;&lt;/code&gt; in all slots of the list, it will in fact put a reference to the same
thunk in those slots, not the actual data. As we pass the list, this thunk is
building up with nested &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;max&lt;/span&gt;&lt;/code&gt; expressions. For &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;/code&gt;, will end up with a
thunk: &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;max &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;max &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;max &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/code&gt;.
A reference to this thunk will be placed everywhere in the list. By the time we
finished traversing the list, the thunk will be finished too, and can be
evaluated. (Before finishing, the thunk has the form similar to
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;max &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;_something_&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt; where &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;_something_&lt;/span&gt;&lt;/code&gt; is the max of the rest of the list.
This obivously can not be evaluated at this point)&lt;/p&gt;
&lt;p&gt;How about generalising this idea to other data structures?&lt;/p&gt;
&lt;p&gt;There’s an old saying in the world of lists&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;“Everything’s a fold”.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Indeed, we could easily rewrite our doRepMax function using a fold:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foldmax &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Ord&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Num&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foldmax ls &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; ls&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  where&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;ls&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; largest&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; foldl &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(\(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;largest &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; max a c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ([],&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; ls&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Brilliant! Now we can use this technique on everything that is Foldable!
Or can we?&lt;/p&gt;
&lt;p&gt;Taking a look at the type signature of the generalised &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foldl&lt;/span&gt;&lt;/code&gt; (from
Data.Foldable): &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Data.Foldable.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;foldl &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Foldable&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;/code&gt; we realise that the returned value’s structure &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;/code&gt; is independent from
that of the input &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t a&lt;/span&gt;&lt;/code&gt;. The reason we could get away with this in our fold
example was that we knew we were dealing with a list, so we used the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;/code&gt;
operator explicitly to restore the structure.&lt;/p&gt;
&lt;p&gt;No problem! There exists a type class that does just what we want, that is it
lets us fold it while keeping its structure.
This magical class is called &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Traversable&lt;/span&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;{-# &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;LANGUAGE&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; DeriveFunctor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;             DeriveFoldable&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;             DeriveTraversable&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; #-}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Tree&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Empty&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Leaf&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Node&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  deriving&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Functor&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Foldable&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Traversable&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;— Thankfully, GHC is clever enough to derive Traversable for us from this
data definiton. (But it wouldn’t be too difficult to do by hand anyway)&lt;/p&gt;
&lt;p&gt;Traversable data structures can do a really neat trick (among many others):
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mapAccumR &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Traversable&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This function is like combining a map with a fold (and so all Traversables also
need to be Functors and Foldables).  We take a function &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/code&gt;,
an initial &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; and a Traversable of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;/code&gt;s (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t b&lt;/span&gt;&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;The elements will be changed with their respective &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c&lt;/span&gt;&lt;/code&gt;s. (the one calculated by
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;/code&gt;) So &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c&lt;/span&gt;&lt;/code&gt; is a perfect place for us to put our &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;rep&lt;/span&gt;&lt;/code&gt; (the
largest element in this case)&lt;/p&gt;
&lt;p&gt;Apart from the final Traversable &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;t c&lt;/span&gt;&lt;/code&gt;, it also returns the accumulated &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt;s
(that’s where we return the largest).&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generalMax &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Traversable&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Num&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Ord&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generalMax t &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; xs&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;largest&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; xs&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; mapAccumR &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(\&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;max a b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; largest&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This generalisation gives us new options!  What we’ve been doing so far is
we’ve used &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;/code&gt; and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c&lt;/span&gt;&lt;/code&gt; as the same types (as, say Ints).&lt;/p&gt;
&lt;p&gt;For instance, if we want to replace all the elements with the average of them,
then we can accumulate the sum and the count of elements in a tuple (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; will
then take the role of this tuple) and &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c&lt;/span&gt;&lt;/code&gt; will be the sum divided by the count,
for which we’re going to ask the future again!&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generalAvg &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Traversable&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Integral&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;generalAvg t &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; xs&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  where&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;    avg &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;`div`&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;    ((&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; xs&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;      =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; mapAccumR &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(\(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; ((&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; avg&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And so on, we can do all sorts of interesting things in a single traversal of
our data structures.&lt;/p&gt;
&lt;h2 id=&quot;states-travelling-back-in-time&quot;&gt;States travelling back in time&lt;/h2&gt;
&lt;hr&gt;
&lt;h5 id=&quot;what-are-states-anyway&quot;&gt;What are states anyway?&lt;/h5&gt;
&lt;p&gt;In Haskell, whenever we want to write functions that operate on some sort
of environment or state, we write these functions in the following form:
statefulFunction :: b -&gt; c -&gt; d -&gt; s -&gt; (a, s)
that is, we take some arguments (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;c&lt;/span&gt;&lt;/code&gt;, &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;d&lt;/span&gt;&lt;/code&gt; here), a state &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;/code&gt;, and
return a new, possibly modified state along with some value &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt;.
Now, this involves writing a lot of boilerplate code, both in the type
signatures and in the actual code that is using the state.&lt;/p&gt;
&lt;p&gt;For example, using the state as a counter:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;statefulFunction arg1 arg2 arg3 counter &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;arg1 &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; arg2 &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; arg3&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; counter &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bindStatefulFunctions &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;bindStatefulFunctions f1 f2 &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; \&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;initialState &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; updatedState&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f1 initialState&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  in&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; f2 result updatedState&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that f2 takes an extra &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt;, that’s the output of the first function.
That’s why this function is called bind, we bind the output of the first
function to the input of the second while passing the modified state.&lt;/p&gt;
&lt;p&gt;The State monad essentially does something like the above code, but hides
it all and makes the state passing implicit. Also, being a monad, gives us
the all so convenient do notation!&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s a&lt;/span&gt;&lt;/code&gt; is basically just a type synonym for &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;, so our
previous example could be written as
&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;statefulFunction &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; d &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; State&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s a&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;and bindStatefulFunctions we get for free from State (known as &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&gt;=&lt;/span&gt;&lt;/code&gt; for monads)&lt;/p&gt;
&lt;p&gt;Now we can do:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;statefulFunction arg1 arg2 arg3 &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  counter &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; get&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  put &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;counter &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  return &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;arg1 &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; arg2 &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; arg3&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Did you know that Haskell is also the best imperative language?)
Notice how the state is not explicitly passed as an argument (thus our
function is partially applied), but is bound to counter by the get function.
Put then puts the updated counter back in the state. Return then just makes
sure that what we get out of is wrapped back in the State monad.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;The nice thing about the State monad is that all the computations we do
within it are essentially just partially applied functions, so they can’t be
evaluated until provided with an initial state, which will then magically
flow through the pipeline of computations, each doing their respective
modifications in the meantime.&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mapAccumR&lt;/span&gt;&lt;/code&gt; does a series of stateful computations (in nature, but it’s not
using the State monad), where it takes a value and a state, then returns a
new value with a modified state. (Accum refers to the fact that this state
can be used as an accumulator as we traverse the data)&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;mapAccumR &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Traversable&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t b &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; t c&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;a&lt;/span&gt;&lt;/code&gt; is that state here, that is what we used to store the largest element.
This state, however, travels forward in time, so to speak, as we go through
the list. The trick we do only happens at the end, when we feed it its own
output. We can do so thanks to lazy evaluation.&lt;/p&gt;
&lt;p&gt;So the State monad passes its &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s&lt;/span&gt;&lt;/code&gt; from computation to computation, that’s
how these computations are bound.&lt;/p&gt;
&lt;p&gt;Imagine using the same laziness self-feeding trick, but for passing the
state:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;reverseBind stateful1 stateful2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;  =&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; \&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;s &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&apos;&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  where&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&apos;&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; stateful1 s&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;        (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; s&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; stateful2 x s&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So first we run stateful1 &lt;strong&gt;with the state modified by stateful2&lt;/strong&gt;!
Then we run stateful2 with stateful1’s output. Finally, we return the
state after running stateful1 along with the value &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;x&apos;&lt;/span&gt;&lt;/code&gt; from stateful2.
Note that because of the way this binding is done, stateful1’s ouput state
will actually be the &lt;em&gt;past&lt;/em&gt; of stateful1. (That is, whatever we do with the
state in stateful1, will be visible to the computations preceding stateful1,
just like how stateful2’s effects are seen in stateful1. Lazy evaluation
rocks!)&lt;/p&gt;
&lt;p&gt;Coming from an imperative background, this can be thought of as stateful1
putting forward references to the values it uses from the state, and once
those values are actually calculated in the future, stateful1 will be able
to do whatever it wanted. These references are not explicit though as they
would be in C (using pointers, for example), but implicitly placed there
by GHC as thunks.&lt;/p&gt;
&lt;p&gt;That also means whatever we do with these values has to be done lazily. (an
example below)&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;The above code is a modified version of the monadic binding found in the
rev-state package (which is in turn a modification of the original State
monad by reversing the flow of state).&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;finally-the-time-machine-tardis&quot;&gt;Finally, the time machine, TARDIS&lt;/h2&gt;
&lt;p&gt;So we have the State monad, of which the state flows forwards, then we have
the Rev-State, which sends the state backwards. So what do we get if we
combine these two? Yes, a time machine! Also known as the Tardis monad: it
is in fact a combination of the State and Rev-State monads with some nice
functions to deal with the bidirectional states.&lt;/p&gt;
&lt;p&gt;I say states, because naturally, we have data coming from the future and
data coming from the past, and those make two (a backwards travelling and a
forwards travelling state).&lt;/p&gt;
&lt;p&gt;These could be of different types, say we can send Strings back in time and
Ints to the future.&lt;/p&gt;
&lt;h3 id=&quot;a-single-pass-assembler-an-example&quot;&gt;A single-pass assembler: an example&lt;/h3&gt;
&lt;p&gt;Writing an assembler is relatively straightforward. We go through a list of
assembly instructions and turn them into their binary equivalent for the given
CPU architecture.&lt;/p&gt;
&lt;p&gt;However, there are some instructions that we can’t immediately convert.
One of such instructions is a label for branching. (jumps)
For these labels, we need a symbol table.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; qualified&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Data.Map.Strict&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; as&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; M&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; SymTable&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; M.Map&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Addr&lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt; -- map label names to their addresses&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Add&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;            |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Mov&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;            |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ToLabel&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;            |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ToAddr&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Addr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;            |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Label&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; String&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;            |&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Err&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;            deriving&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Instr&lt;/span&gt;&lt;/code&gt; is a rather rudimentary representation of assembly instructions, but it
does the job for us now.&lt;/p&gt;
&lt;p&gt;What we want to have is a function that takes a list of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Instr&lt;/span&gt;&lt;/code&gt;s and returns
a list of &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)]&lt;/span&gt;&lt;/code&gt; and also replace all the &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ToLabel&lt;/span&gt;&lt;/code&gt;s with &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ToAddr&lt;/span&gt;&lt;/code&gt;s
that point to the address of the label. If the label is never defined, we
put an &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Err&lt;/span&gt;&lt;/code&gt; there. (In real life, you would use some ExceptT monad transformer
to handle such errors.)&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;runAssembler &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Jumping to a label that is already defined is easy, we look it up in our
SymTable and convert &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ToLabel&lt;/span&gt;&lt;/code&gt; to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ToAddr&lt;/span&gt;&lt;/code&gt;. This sounds like an application
of the State monad, doesn’t it?
When we encounter a label definition, just add it to the state (&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;SymTable&lt;/span&gt;&lt;/code&gt;).
Done!&lt;/p&gt;
&lt;p&gt;The problem arises from the fact that some labels might be defined after they
are used. The ‘else’ block of an if statement will typically be done like this.
Implementing this in C, you could remember these positions and at the end, fill
in the gaps with the knowledge you have acquired. Thunks, anyone?&lt;/p&gt;
&lt;p&gt;I’ll just use a Rev-State monad and send these definitions back in time.
Simple enough, right?&lt;/p&gt;
&lt;p&gt;So at this point, we can see that we will need both types of these states:
one that’s travelling forward and one that is going backwards. And that is
exactly what the Tardis monad is!&lt;/p&gt;
&lt;div class=&quot;callout&quot; data-callout=&quot;note&quot; data-collapsible=&quot;false&quot;&gt;&lt;div class=&quot;callout-title&quot;&gt;&lt;div class=&quot;callout-title-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;1em&quot; height=&quot;1em&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;line x1=&quot;18&quot; y1=&quot;2&quot; x2=&quot;22&quot; y2=&quot;6&quot;&gt;&lt;/line&gt;&lt;path d=&quot;M7.5 20.5 19 9l-4-4L3.5 16.5 2 22z&quot;&gt;&lt;/path&gt;&lt;/svg&gt;&lt;/div&gt;&lt;div class=&quot;callout-title-text&quot;&gt;Note&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;callout-content&quot;&gt;&lt;p&gt;Labels will not be turned into any binary, instead
the next actual instruction’s address will be used.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Assembler&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Tardis&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; SymTable&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; SymTable&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Right, our &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;runAssembler&lt;/span&gt;&lt;/code&gt; function will run some &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;assemble&lt;/span&gt;&lt;/code&gt; function
in the Tardis monad. (That is, it will give it the initial states and extract
the final value at the end).&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;runAssembler asm &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; instructions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  where&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;instructions&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; _&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;          =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; runTardis &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;assemble &lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; asm&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;M.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; M.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;assemble&lt;/span&gt;&lt;/code&gt; function turns a list of instructions to &lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)]&lt;/span&gt;&lt;/code&gt;
in the Assembler monad (which is a synonym for Tardis SymTable SymTable).
What’s that 0 doing there, you ask?&lt;/p&gt;
&lt;p&gt;We need to keep track of the address we will use for the next instruction.
This is because of labels. When we encounter a regular instruction, we put
that at the provided address, then increment that address by 1. If a label
comes around, we put it in the State then continue without incrementing the
address.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;assemble &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Assembler&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;assemble _ &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; return &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;[]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- label found, update state then go on&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;assemble addr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Label&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; label &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; is&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  modifyBackwards &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;M.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;insert label addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt; -- send to past&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  modifyForwards &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;M.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;insert label addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;  -- send to future&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  assemble addr is&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- assemble the rest of the instructions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- jump to label found, replace with&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- jump to address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- then do the rest starting at (addr + 1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;assemble addr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ToLabel&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; label &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; is&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  bw &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; getFuture&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  fw &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; getPast&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;  let&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; union &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; M.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;union bw fw &lt;/span&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- take union of the two symbol tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;      this &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; case&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; M.&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;lookup label union &lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;                Just&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&apos; &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;-&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; ToAddr&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; a&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;                Nothing&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; -&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; Err&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  rest &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; assemble &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;addr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; is&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  return &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; this &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- regular instruction found,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- assign it to the address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-subtle-italic&quot;&gt;-- then do the rest starting at (addr + 1)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;assemble addr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;instr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; is&apos;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt; do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  rest &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&amp;#x3C;-&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; assemble &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;addr &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; is&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;  return &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; rest&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we come up with some test instructions:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes csongor-monochrome-light csongor-monochrome-dark has-language-label&quot; style=&quot;--shiki-light:#0a0a0a;--shiki-dark:#eeeeee;--shiki-light-bg:#ffffff;--shiki-dark-bg:#0a0a0a; overflow-x: auto;--file-name-offset: -0.75rem;&quot; tabindex=&quot;0&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;code-language-label&quot;&gt;haskell&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;input &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Instr&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-foreground&quot;&gt;input &lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         ToLabel&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;my_label&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         Mov&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         Mov&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         Label&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;my_label&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         Label&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;second_label&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         Mov&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         ToLabel&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; &quot;second_label&quot;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;         Mov&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;…and we can try running the assembler on this data:&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-foreground&quot;&gt; runAssembler input&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code class=&quot;shiki-inline&quot; data-language=&quot;haskell&quot;&gt;&lt;span class=&quot;tk-subtle&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt; [(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ToAddr&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Mov&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Mov&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Mov&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;ToAddr&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;),(&lt;/span&gt;&lt;span class=&quot;tk-accent&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;tk-strong-bold&quot;&gt;Mov&lt;/span&gt;&lt;span class=&quot;tk-subtle&quot;&gt;)]&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Yay! Just what we wanted!&lt;/p&gt;
&lt;h3 id=&quot;io-doesnt-mix-with-the-future-the-past-is-fine&quot;&gt;IO doesn’t mix with the future! (The past is fine)&lt;/h3&gt;
&lt;p&gt;Be careful about what you do with the state coming from the future.
Everything has to be lazily passed through.&lt;/p&gt;
&lt;p&gt;You might be tempted to use the TardisT monad transformer to interleave IO
effects in your time-travelling code. Most IO computations, however are
strict.&lt;/p&gt;
&lt;p&gt;Let’s say you want to get the label from the future and print its address.
IO’s print will try to evaluate its argument (which is a partial thunk at this
point). It will block the thread until the evaluation is completed, which will
result in the program breaking, as the thread block prevents it from
progressing further. In this case, I’d advise the use of a Writer monad which
has a lazy mechanism, and the results can be printed at the end using IO.&lt;/p&gt;
&lt;h3 id=&quot;thanks&quot;&gt;Thanks&lt;/h3&gt;
&lt;p&gt;Thanks for reading this lengthy post, in which we saw how we can mimic the use
of references in pure Haskell code (altough time-travel is an arguably better
name for this). This comes at a price though: accumulating
unevaluated thunks can use up quite a bit of memory, so be careful if you want
to use these techniques in a memory critical environment.&lt;/p&gt;
&lt;p&gt;If you find any bugs or mistakes, please make sure to let me know!&lt;/p&gt;</content>
    </entry>
</feed>