<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:base="https://functional.computer/" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    
    <title>samir : coffee → nonsense</title>
    <link>https://functional.computer/</link>
    <atom:link href="https://functional.computer/rss.xml" rel="self" type="application/rss+xml" />
    <description>Samir Talwar&#39;s blog.</description>
    <language>en</language>
    <item>
      <title>Power gravitates left</title>
      <link>https://functional.computer/blog/power-gravitates-left</link><description>&lt;p&gt;Last week, I attended the first half of &lt;a href=&quot;https://ruthmalan.com/&quot;&gt;Ruth Malan&lt;/a&gt;’s &lt;a href=&quot;https://www.ruthmalan.com/Bredemeyer/Technical_Leadership_Masterclass_Overview.htm&quot;&gt;Technical Leadership Masterclass&lt;/a&gt;, in which we spent a lot of time discussing &lt;strong&gt;power&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Ruth asked us to draw multiple diagrams of the current organizations in which we work. I drew 4, and one looked a lot like this:&lt;/p&gt;
&lt;p class=&quot;asset&quot;&gt;
  &lt;img src=&quot;https://functional.computer/assets/blog/2026/2026-11-10--power-gravitates-left/hybrid-organisational-model.jpeg&quot; alt=&quot;The hybrid organisational model, described below&quot; title=&quot;The hybrid organisational model, described below&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;This is a diagram from &lt;em&gt;High Output Management&lt;/em&gt;, by Andrew S. Grove, CEO emeritus of Intel. (&lt;a href=&quot;https://functional.computer/blog/reading-about-management&quot;&gt;I re-read this book a few months ago&lt;/a&gt;, and you can scroll to the end of this article for a short review.)&lt;/p&gt;
&lt;p&gt;I did not intend to copy this diagram from Grove’s work, and only realised I had afterwards. It shows the “hybrid organisational model”, which is a compromise between &lt;em&gt;mission-oriented&lt;/em&gt; and &lt;em&gt;functional&lt;/em&gt; models.&lt;/p&gt;
&lt;p&gt;On the right are the mission-oriented &lt;em&gt;business units&lt;/em&gt;, who provide the &lt;strong&gt;value&lt;/strong&gt;. For most companies, this means it talks about the people who do the work for customers who pay the money. The diagram shows three “business groups” labelled “component”, “microcomputer”, and “system”.&lt;/p&gt;
&lt;p&gt;On the left are the &lt;em&gt;functional units&lt;/em&gt;, who provide &lt;strong&gt;support&lt;/strong&gt;: these are the people who make sure that the people on the right can do their jobs effectively. The diagram shows “sales”, “manufacturing”, technology development”, and “administration”, and it might also include marketing, accounting, legal, compliance, IT, and management… there’s a lot of support work.&lt;/p&gt;
&lt;p&gt;And on top is the &lt;em&gt;executive office&lt;/em&gt;, who govern both.&lt;/p&gt;
&lt;p&gt;In the book, Grove explains that “some two-thirds of Intel’s employees work in the functional units, indicating their enormous importance” (at the time of writing, in the 1980s, but I don’t see a reason why this would have changed much).&lt;/p&gt;
&lt;p&gt;And it’s true, they are very important. But I’m not sure they’re &lt;em&gt;more&lt;/em&gt; important than the mission-oriented units; that is, the people who provide the value externally, and therefore the ones who facilitate the money, and the continual salaries of everyone involved.&lt;/p&gt;
&lt;h2&gt;Where does the power lie?&lt;/h2&gt;
&lt;p&gt;I’ve worked in many organisations, and all of them have followed this hybrid model; as Grove points out, it’s rediscovered time and time again, often through sweat and tears, because leaning too hard one way or another does not work.&lt;/p&gt;
&lt;p&gt;And in almost all of them, power is imbalanced.&lt;/p&gt;
&lt;p&gt;Of course, we expect the executives to retain much of the power; they answer to the board of directors or whomever gets to decide the fate of the company, and so they need to maintain at least some high-level authority to match their responsibility. While you might question whether this is actually good idea (and I’d encourage it), I think it stems from the typical company governance structure, and so changing it would require changing that entirely.&lt;/p&gt;
&lt;p&gt;(There are some alternatives, such as &lt;a href=&quot;https://maximumfun.org/co-op/&quot;&gt;worker-owned co-operatives&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;It doesn’t just gravitate up, though. It also gravitates left, from the &lt;strong&gt;valuable&lt;/strong&gt; business units to the &lt;strong&gt;supporting&lt;/strong&gt; functional units. I have had so many conversations that end with “but our Legal team will never allow it”, or “we’ll never get this schedule passed Compliance”, or “Accounting will hate this”.&lt;/p&gt;
&lt;p&gt;And I don’t think there’s anything in the average corporate structure that insists upon this. It’s just… human nature, I guess?&lt;/p&gt;
&lt;p&gt;I would really like to see more decisions being &lt;em&gt;made&lt;/em&gt; by the people who know the business best, with &lt;em&gt;support&lt;/em&gt; from their peers in those functional units (and I include management, which is certainly a support role in my book).&lt;/p&gt;
&lt;p&gt;I try, in my role as a new manager, to ensure that people feel they have control and authority over the areas for which they are responsible. I’m part of a consultancy, which means the consultants are directly responsible for making our clients happy, and I am there to support them. Which means that if they feel they can better serve our clients by spending some money on a trip to visit them, or to attend a conference, or to buy a piece of hardware… I say “Yes!”&lt;/p&gt;
&lt;p&gt;And I would much prefer to not have to say “yes”, but instead, in the model of &lt;a href=&quot;https://www.reinventingorganizations.com/&quot;&gt;Reinventing Organisations&lt;/a&gt;, to simply be notified by the consultant that they’ve used their corporate credit card, and why they thought it was important.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Addendum:&lt;/strong&gt; a review of &lt;em&gt;High Output Management&lt;/em&gt;&lt;/h2&gt;
&lt;p&gt;The book is full of sensible advice for someone new to leadership, especially on 1-on-1 meetings and dealing with interpersonal crises. I really appreciated its unwavering attitude to dealing with problems head-on, fairly, and uncompromisingly.&lt;/p&gt;
&lt;p&gt;However, it’s got some problems too. The advice is very process-heavy and prescriptive, and may not be the best choice in your situation. It’s hard to tell what’s generally a good idea, and what may not apply because of a different in organisations.&lt;/p&gt;
&lt;p&gt;And, of course, it was written in the 1980s, so caveats abound here:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;It’s full of casual sexism (most notably using “he” as a placeholder for someone of any gender).&lt;/li&gt;
  &lt;li&gt;Grove asks no questions of the prison-industrial complex in the USA when using it as an unnecessary example.&lt;/li&gt;
  &lt;li&gt;It assumes a fairly basic economic model that we know to be way too simple.&lt;/li&gt;
  &lt;li&gt;Email is new and exciting; Grove, unsurprisingly, did not predict the rise of the Web and everything that came after.&lt;/li&gt;
  &lt;li&gt;In 2026, much of the praise of technology feels… off.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Oh, and in the revised edition, Ben Horowitz, one-half of the supervillain duo known as &lt;em&gt;Andreessen Horowitz&lt;/em&gt;, wrote the foreword. It is not good.&lt;/p&gt;
&lt;p&gt;I would recommend reading it despite these issues, in the spirit of taking what is useful, and discarding what is useless. But not, as many suggest, as your first management book. &lt;a href=&quot;https://functional.computer/blog/reading-about-management&quot;&gt;Pick something a little newer.&lt;/a&gt;&lt;/p&gt;
</description><pubDate>Tue, 10 Nov 2026 12:30:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/power-gravitates-left</guid>
    </item>
    <item>
      <title>Reading about management</title>
      <link>https://functional.computer/blog/reading-about-management</link><description>&lt;p&gt;Today, I started a new job at &lt;a href=&quot;https://www.tweag.io/&quot;&gt;Tweag&lt;/a&gt;. It’s my first official management role in a little while (though I’ve been subbing in, off and on), so a few weeks ago, I thought I’d ask a group of friends, ex-colleagues from my first job at TIM Group, if they could recommend me any reading material. I’d like to start with the right mindset and a little bit of advice, and I find reading always helps.&lt;/p&gt;
&lt;p&gt;My friends replied, and they really delivered.&lt;/p&gt;
&lt;h2&gt;First-hand knowledge&lt;/h2&gt;
&lt;p&gt;First of all, the material that people wrote themselves:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Joe Schmetzer wrote up his entire &lt;a href=&quot;https://exubero.com/2026/06/26/joes-lightweight-engineering-management-approach/&quot;&gt;Lightweight Engineering Management Approach&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Tony Tsui put together a slide deck on &lt;a href=&quot;https://docs.google.com/presentation/d/13IX0voyBGKV4VglU3lLCcVjMpaDrC6Q5&quot;&gt;Performance Review KPIs&lt;/a&gt;, based on the TIM Group performance framework&lt;/li&gt;
  &lt;li&gt;Douglas Squirrel and Jeffrey Fredrick have slides on &lt;a href=&quot;https://docs.google.com/presentation/d/1h_0-6XXOhCEX-GzCGCnOeagw1N0gL5qLFr0AB_iJuvw&quot;&gt;Your First 30 Days as a Manager&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;
    based on my question, Squirrel and Jeffrey also made two episodes of the &lt;a href=&quot;https://agileconversations.com/troubleshooting-agile-podcast/&quot;&gt;Troubleshooting Agile podcast&lt;/a&gt;, which include links in the show notes to even more books and references (though I’ve included them below for completeness):
    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;https://overcast.fm/+AALRKGuuE2Y&quot;&gt;You Never Forget Your First Time! Stories For The New Manager&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://overcast.fm/+AALRKGDJ23w&quot;&gt;Bozo the Client? Managing In A Consultancy&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Books&lt;/h2&gt;
&lt;p&gt;Here’s the books they recommended, in no particular order:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    by Johanna Rothman:
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.jrothman.com/books/modern-management-made-easy-a-three-volume-set/&quot;&gt;Modern Management Made Easy&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.jrothman.com/books/behind-closed-doors-secrets-of-great-management/&quot;&gt;Behind Closed Doors&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;by Gerald Weinberg, &lt;a href=&quot;https://geraldmweinberg.com/Site/Technical_Leader.html&quot;&gt;Becoming a Technical Leader&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;
    by &lt;a href=&quot;https://www.jrothman.com/books/modern-management-made-easy-a-three-volume-set/&quot;&gt;Roger Schwarz&lt;/a&gt;:
    &lt;ul&gt;
      &lt;li&gt;Smart Leaders, Smarter Teams&lt;/li&gt;
      &lt;li&gt;The Skilled Facilitator&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;by Michael Watkins, &lt;a href=&quot;https://store.hbr.org/product/the-first-90-days-updated-and-expanded-proven-strategies-for-getting-up-to-speed-faster-and-smarter/11323&quot;&gt;The First 90 Days&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;by Stephen Bungay, &lt;a href=&quot;https://www.goodreads.com/book/show/9973202-the-art-of-action&quot;&gt;The Art of Action&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;by David Allen, &lt;a href=&quot;https://gettingthingsdone.com/books/&quot;&gt;Getting Things Done&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;by Kenneth Blanchard and Spencer Johnson, &lt;a href=&quot;https://en.wikipedia.org/wiki/The_One_Minute_Manager&quot;&gt;The One-Minute Manager&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;by Mike Rother, &lt;a href=&quot;https://en.wikipedia.org/wiki/Toyota_Kata&quot;&gt;Toyota Kata&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;by Michael Lopp, &lt;a href=&quot;https://randsinrepose.com/books/&quot;&gt;Managing Humans&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;by Andy Grove, &lt;a href=&quot;https://en.wikipedia.org/wiki/High_Output_Management&quot;&gt;High Output Management&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;
    by Xavier Amador, of the &lt;a href=&quot;https://leapinstitute.org/leap-books/&quot;&gt;LEAP Institute&lt;/a&gt;:
    &lt;ul&gt;
      &lt;li&gt;I’m Right, You’re Wrong, Now What?&lt;/li&gt;
      &lt;li&gt;I am Not Sick, I Don’t Need Help!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I’ve read a few of these, but most are new to me.&lt;/p&gt;
&lt;h2&gt;Frameworks&lt;/h2&gt;
&lt;p&gt;They also recommended some frameworks:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.csu.edu.au/__data/assets/pdf_file/0008/917018/Eight-Behaviors-for-Smarter-Teams-2.pdf&quot;&gt;Eight Behaviors for Smarter Teams&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Situational_leadership_theory&quot;&gt;Situational leadership theory&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://availagility.co.uk/2021/03/10/what-is-backbriefing/&quot;&gt;Backbriefing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://playbooks.equalexperts.com/advice-process/how-the-ap-works/step-by-step-guide&quot;&gt;The Advice Process&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://reach.crownandreach.com/posts/mini-pitches&quot;&gt;Mini-Pitches&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;I’ve got a lot of reading to do&lt;/h2&gt;
&lt;p&gt;I’m looking forward to it, even if it’s gonna take me years.&lt;/p&gt;
</description><pubDate>Tue, 01 Sep 2026 15:00:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/reading-about-management</guid>
    </item>
    <item>
      <title>Fairness in disjunctions</title>
      <link>https://functional.computer/blog/fairness-in-disjunctions</link><description>&lt;p&gt;When we last left our little logic DSL, &lt;a href=&quot;https://functional.computer/blog/monadic-logic&quot;&gt;it had grown a monad&lt;/a&gt;.&lt;/p&gt;
&lt;ol class=&quot;series-list&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/i-relent&quot;&gt;I relent&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview&quot;&gt;Solving the technical interview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview-explained&quot;&gt;Solving the technical interview, explained&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/monadic-logic&quot;&gt;Monadic logic&lt;/a&gt;&lt;/li&gt;
  &lt;li aria-current=&quot;page&quot;&gt;Fairness in disjunctions&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;With that working, let’s turn our attention to another piece of functionality expected from a logic language: fairness. That is, if we create a disjunction between two goals (&lt;code&gt;g ||| h&lt;/code&gt;), we should sample from the successes of &lt;em&gt;both&lt;/em&gt;, not yield all the results from &lt;code&gt;g&lt;/code&gt; before turning to &lt;code&gt;h&lt;/code&gt;. This means that implementing &lt;code&gt;(|||)&lt;/code&gt; in terms of &lt;code&gt;(&amp;#x3C;|&gt;)&lt;/code&gt;, which concatenates two lists, is not ideal.&lt;/p&gt;
&lt;p&gt;Imagine a stream, &lt;code&gt;odds&lt;/code&gt;, and another one, &lt;code&gt;evens&lt;/code&gt;. &lt;code&gt;odds&lt;/code&gt; unifies a variable with the numbers &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt;, etc. and &lt;code&gt;evens&lt;/code&gt; yields &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;4&lt;/code&gt;, &lt;code&gt;6&lt;/code&gt;...&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;odds&lt;/span&gt; v = foldr1 (|||) $ map (&#92;n -&gt; v === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; n &amp;#x26;&amp;#x26;&amp;#x26; bool falsity truth (odd n)) [&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;..]
&lt;span class=&quot;hljs-title&quot;&gt;evens&lt;/span&gt; v = foldr1 (|||) $ map (&#92;n -&gt; v === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; n &amp;#x26;&amp;#x26;&amp;#x26; bool falsity truth (even n)) [&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;..]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can create a goal, &lt;code&gt;numbers&lt;/code&gt;, which combines the two as a disjunction:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;numbers&lt;/span&gt; = &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  v &amp;#x3C;- fresh
  odds v ||| evens v
  pure v
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And when we run it:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;λ take &lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt; . map (&#92;(v, s) -&gt; walk s v) $ run numbers
[&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;9&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;11&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;13&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;15&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;17&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;19&lt;/span&gt;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There’s no fairness. It’s just odd numbers.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(&amp;#x3C;|&gt;)&lt;/code&gt; is the &lt;strong&gt;alternation&lt;/strong&gt; operator, though. Perhaps we could make it alternate?&lt;/p&gt;
&lt;h2&gt;An alternating stream&lt;/h2&gt;
&lt;p&gt;Let’s make a new data type, wrapping lists, and call it &lt;code&gt;Stream&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; a = &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; { &lt;span class=&quot;hljs-title&quot;&gt;unStream&lt;/span&gt; :: [&lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt;] }&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Show&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Functor&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Applicative&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Monad&lt;/span&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We will automatically delegate &lt;code&gt;Functor&lt;/code&gt;, &lt;code&gt;Applicative&lt;/code&gt;, and &lt;code&gt;Monad&lt;/code&gt;, but we’ll implement &lt;code&gt;Alternative&lt;/code&gt; ourselves. Our alternation function will take an element from the left stream, &lt;code&gt;xs&lt;/code&gt;, swap the streams, and recurse. (If it’s empty, it will just return the right stream, &lt;code&gt;ys&lt;/code&gt;.)&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Alternative&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
  empty = &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; empty
  &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; xs &amp;#x3C;|&gt; &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; ys = &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; $ alternate xs ys
    &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    alternate [] ys = ys
    alternate (x : xs) ys = x : alternate ys xs
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that we call &lt;code&gt;alternate ys xs&lt;/code&gt; on the last line, so &lt;code&gt;ys&lt;/code&gt; becomes &lt;code&gt;xs&lt;/code&gt; and &lt;code&gt;xs&lt;/code&gt; becomes &lt;code&gt;ys&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We also implement &lt;code&gt;MonadPlus&lt;/code&gt; because otherwise other things complain. &lt;code&gt;MonadPlus&lt;/code&gt; is really just a legacy way of expressing &lt;code&gt;Alternative + Monad&lt;/code&gt;, and so it doesn’t need any implemenetation.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;MonadPlus&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s try it out:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;λ &lt;span class=&quot;hljs-string&quot;&gt;&quot;hello&quot;&lt;/span&gt; &amp;#x3C;|&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;world&quot;&lt;/span&gt; :: [&lt;span class=&quot;hljs-type&quot;&gt;Char&lt;/span&gt;]
&lt;span class=&quot;hljs-string&quot;&gt;&quot;helloworld&quot;&lt;/span&gt;
λ &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;hello&quot;&lt;/span&gt; &amp;#x3C;|&gt; &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;world&quot;&lt;/span&gt;
&lt;span class=&quot;hljs-string&quot;&gt;&quot;hweolrllod&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Seems to work!&lt;/p&gt;
&lt;p&gt;We need to update &lt;code&gt;Goal&lt;/code&gt; to use &lt;code&gt;Stream&lt;/code&gt; rather than &lt;code&gt;[]&lt;/code&gt;, and a couple of other boring tweaks which I will elide here:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a = &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;StateT&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;atom&lt;/span&gt;) &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; a)&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Functor&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Applicative&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Alternative&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Monad&lt;/span&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And with that, we alternate. Lets try &lt;code&gt;numbers&lt;/code&gt; again.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;λ take &lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt; . map (&#92;(v, s) -&gt; walk s v) $ run numbers
[&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;6&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;9&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s more like it.&lt;/p&gt;
&lt;h2&gt;Three’s a crowd&lt;/h2&gt;
&lt;p&gt;At least, until we combine more than two streams. For example, alternating 3 streams, we quickly see that it’s unfair.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;λ &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;abcde&quot;&lt;/span&gt; &amp;#x3C;|&gt; &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;fghij&quot;&lt;/span&gt; &amp;#x3C;|&gt; &lt;span class=&quot;hljs-type&quot;&gt;Stream&lt;/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;klmno&quot;&lt;/span&gt;
&lt;span class=&quot;hljs-string&quot;&gt;&quot;akflbmgncohdiej&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We’d expect &lt;code&gt;&quot;afkbglchmdinejo&quot;&lt;/code&gt;, but &lt;code&gt;o&lt;/code&gt; appears much earlier, because we alternate the first two streams &lt;em&gt;and then&lt;/em&gt; alternate them with the third, which leads to &lt;strong&gt;bias&lt;/strong&gt;. Unfortunately, µKanren doesn’t have a solution for this; we’ll have to deal with it.&lt;/p&gt;
&lt;p&gt;In practice, it may not matter. I hope to apply this language to problems that have effects dwarfing the computation, and so it might be prudent to e.g. run disjunctions in parallel, and yield in whatever order the results arrive.&lt;/p&gt;
&lt;h2&gt;&lt;code&gt;LogicT&lt;/code&gt;, and effectful, interleaving streams&lt;/h2&gt;
&lt;p&gt;
  It is worth mentioning the paper &lt;a href=&quot;https://functional.computer/assets/documents/LogicT.pdf&quot;&gt;Backtracking, Interleaving, and Terminating Monad Transformers&lt;/a&gt; at this point, which introduces &lt;code&gt;LogicT&lt;/code&gt; (provided in the &lt;a href=&quot;https://hackage.haskell.org/package/logict&quot;&gt;&lt;code&gt;logict&lt;/code&gt;&lt;/a&gt; package) and its associated combinators. &lt;code&gt;Logic&lt;/code&gt; (which is &lt;code&gt;LogicT&lt;/code&gt; over &lt;code&gt;Identity&lt;/code&gt;) can be slotted in place of &lt;code&gt;Stream&lt;/code&gt; in this exercise, with similar implementations of &lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt; and &lt;code&gt;|||&lt;/code&gt;. This handles some other aspects of fairness over conjunection, but still leaves a bias for simple computations.
&lt;/p&gt;
&lt;p&gt;&lt;code&gt;LogicT&lt;/code&gt;, in contrast to &lt;code&gt;Logic&lt;/code&gt;, offers a way to encode effects in the transformations, which can be very useful in expressing logical combinations within a Haskell eDSL. For the kind of language I am writing, it’s less useful, but still fascinating, and I encourage you to explore it.&lt;/p&gt;
</description><pubDate>Thu, 06 Aug 2026 06:00:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/fairness-in-disjunctions</guid>
    </item>
    <item>
      <title>LLMs can&#39;t program</title>
      <link>https://functional.computer/blog/llms-cant-program</link><description>&lt;p&gt;The word “generative” in “generative AI” refers to what it &lt;em&gt;does&lt;/em&gt;, not what it is &lt;em&gt;useful for&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;But that’s the conclusion. Let’s start from the beginning.&lt;/p&gt;
&lt;h2&gt;Years of ignoring the world&lt;/h2&gt;
&lt;p&gt;I have wondered, for a long time now, why I haven’t written on the topic of generative AI for programming. After all, everyone else is.&lt;/p&gt;
&lt;p&gt;I recently realised that that’s why. Everyone else is. I have nothing to add, except that I find it personally distateful. (And if you were wondering what my bias is, now you know.) &lt;a href=&quot;https://functional.computer/blog/teaching-a-machine-to-code&quot;&gt;I have long been an advocate of machine-assisted work&lt;/a&gt;, and in the past, &lt;a href=&quot;https://functional.computer/blog/city-planning&quot;&gt;I even claimed programming will be fully automated&lt;/a&gt;, though I no longer believe this… and yet, I never felt the need to even bother &lt;em&gt;trying&lt;/em&gt;. A large language model was obviously so detached from an &lt;em&gt;understanding&lt;/em&gt; of the code that I didn’t believe it could ever be useful. And, of course, I was seeing the evidence reinforcing my beliefs all over social media.&lt;/p&gt;
&lt;p&gt;So I mostly ignored it, even when my colleagues were experimenting with outsourcing their own jobs to a chatbot. I installed Cursor last year when I was asked to, and tried it out. I was told to “treat it like an intern”. I asked it to help me debug a failing CI pipeline. It misquoted the documentation at me and then doubled down when I argued with it. So I &lt;del&gt;fired&lt;/del&gt; &lt;ins&gt;uninstalled&lt;/ins&gt; it and carried on with my work as usual. For a year, no one asked me to try it again.&lt;/p&gt;
&lt;h2&gt;In which I learn to be one with the agent… or not&lt;/h2&gt;
&lt;p&gt;Recently, I was asked to attend a two-day course in “spec-driven development with agentic AI”. If you’ve not heard of it, spec-driven development is the idea that waterfall software development is good, actually, as long as you do it fast enough.&lt;/p&gt;
&lt;p&gt;
  The principle is fairly simple: write down every decision in text files, never rely on the agent’s context (so that instances of agents become fungible), and work in discrete (though very large) steps. Start with a roadmap, broken down into high-level goals, and then generate a spec for the first goal, implement, repeat. Classic top-down &lt;a href=&quot;https://en.wikipedia.org/wiki/Theory_X_and_Theory_Y&quot;&gt;&lt;em&gt;Theory X&lt;/em&gt; management&lt;/a&gt;, with the programmer acting as the manager. (To be fair, if Theory X was ever useful for something, it’d be for dictating to a machine.)
&lt;/p&gt;
&lt;p&gt;So each of us, in the workshop, built a little habit-tracking app with either Claude Code or OpenAI Codex. It went quick. I had Codex generate a 5000-word specification, prompt me for clarification, and update it until &lt;em&gt;it&lt;/em&gt; (the agent, not me) decided it had a high confidence of being worthwhile.&lt;/p&gt;
&lt;p&gt;(What it means for an agent to have “high confidence”, I have no idea. As far as I can tell, the bullshit machine works better if you ask it to bullshit itself too.)&lt;/p&gt;
&lt;p&gt;I had a UI and some basic functionality within, I don’t know, 30 minutes? It was an atrocious UI, which is not necessarily a problem: it allowed me to reject a design and come up with something new. But while staring at the awful design, I was a bit confused.&lt;/p&gt;
&lt;p&gt;You see, I’d asked the chatbot to also create screenshot-based approval tests so I could see the changes as diffs. And it did. But they looked nothing like the actual UI. So I scrolled through the approval test code, and discovered that it was not actually running the application and taking screenshots, but instead generating some random nice-looking screenshots with code, rendering them as PNGs, and saving them to disk.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Strike one.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I proceeded anyway, ignoring the tests. The bot was not bad at redesigning, though I found myself really wanting a UX designer by my side, not something that could only say “yes”. It turns out tracking habits is not a solved problem; the general problem is infinitely complex, and constraining it in such a way that it is still useful is fascinating in its own right, even setting aside the computer. But time was marching on, and so I accepted a design that was, well, &lt;em&gt;fine, I guess&lt;/em&gt;, and continued with step 2 on my roadmap: &lt;em&gt;authentication&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I asked the agent to generate a spec, and it complied. It even asked me if I wanted to use passkeys, which I had not considered, and for the first time, I was intrigued. I didn’t know how to implement authentication with passkeys. This was a learning opportunity for me! Of course I agreed, and once the spec was refined, I asked the bot to implement it.&lt;/p&gt;
&lt;p&gt;It did. At least, it said it did. I launched the webapp, signed in as a user, and found two issues. First of all, it claimed that it would transfer habits recorded as an anonymous user to the known user upon sign-in… and it did not. But the more worrying issue was that there was no passkey prompt. Nothing. I typed a username, I was signed in.&lt;/p&gt;
&lt;p&gt;It turns out that chatbots are good at chat. It &lt;em&gt;claimed&lt;/em&gt; to have implemented authentication. Actually, it had basically written:&lt;/p&gt;
&lt;pre data-lang=&quot;typescript&quot;&gt;&lt;code class=&quot;hljs language-typescript&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;hljs-title function_&quot;&gt;authenticate&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-attr&quot;&gt;user&lt;/span&gt;: &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt;&lt;/span&gt;): bool {
  &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;true&lt;/span&gt;;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I am fairly sure this is not compliant with the WebAuthN/Passkeys specification, though I haven’t read it to be sure.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Strike two.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;At this point, I had a hunch. I restarted the application. All data was lost. Either it wasn’t really saving to a SQLite database, as was specified and claimed, or it was truncating the database on startup. I didn’t bother to investigate. Either way, it was:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Strike three.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Total cost: around $1000 in tokens, mostly to generate Markdown files which were interpreted as suggestions, at best.&lt;/p&gt;
&lt;p&gt;I did not do much on the second day of the workshop. If not for the trainer, who was great, and quite receptive to feedback, I may not have showed up.&lt;/p&gt;
&lt;h2&gt;And yet, a pleasant surprise&lt;/h2&gt;
&lt;p&gt;There were two moments where I saw some potential, though.&lt;/p&gt;
&lt;p&gt;Firstly, as we’ve discussed, I found it very useful that the chatbot suggested passkeys. I hadn’t thought of it. It was the right choice, IMO, and I’m glad it didn’t pass me by.&lt;/p&gt;
&lt;p&gt;Secondly, I vividly recall asking the bot, “what’s missing from this spec?” and it found a few issues that I thought were important, including what to do with persisted data in older formats. (Turns out it didn’t matter, it was throwing away the data, but still, a valid concern.)&lt;/p&gt;
&lt;p&gt;These two moments made me realise these tools aren’t a &lt;em&gt;total&lt;/em&gt; waste of time.&lt;/p&gt;
&lt;h2&gt;Agents are not people&lt;/h2&gt;
&lt;p&gt;The chatbots are &lt;em&gt;great&lt;/em&gt; at chatting. They can chat for pages. I’m sure they’re incentivised to do so by their makers; after all, you pay by the token.&lt;/p&gt;
&lt;p&gt;We call this category of tools “generative AI”. Setting aside “AI” for a moment (because if you get me started on the word “intelligence” and what it means, we’re going to go completely off the rails), a commercial LLM is designed to generate. And generate, it shall. Anything you like. Prose, poetry, code.&lt;/p&gt;
&lt;p&gt;At no point have I ever met something generated by one of these tools that I didn’t detest. Articles are obsequious, vacuous, and typically &lt;strong&gt;boring&lt;/strong&gt;. Length is a virtue (Pascal would have cried). Poetry reads like the stuff I wrote when I was 14. (I didn’t get better, I stopped.) Ask an LLM to generate short fiction, and you’ll quickly learn it was trained on the worst of internet fanfic.&lt;/p&gt;
&lt;p&gt;Of course, code is no exception.&lt;/p&gt;
&lt;p&gt;The machine-generated specifications are ridiculously large, and they feel &lt;em&gt;designed&lt;/em&gt; to be ignored; they’re so long and dull that I am not sure I believe that any human has &lt;em&gt;ever&lt;/em&gt; thoroughly read one of these. I certainly didn’t; I skimmed them and only realised the issues much later.&lt;/p&gt;
&lt;p&gt;(If you haven’t read &lt;a href=&quot;https://functional.computer/assets/documents/Royce-Waterfall.pdf&quot;&gt;Royce’s 1970 paper on waterfall software development&lt;/a&gt;, including the flaws and how to resolve them, I highly recommend it.)&lt;/p&gt;
&lt;p&gt;And the code? The code is trash. Not even slightly trash; it’s beyond repair. I am no longer shocked that I hear a new story every week about catastrophic bugs being introduced by LLMs, at a rate far greater than humans ever could. We don’t review the code any more, apparently; and how could you? Humans can handle reviewing about 200 lines of code before losing it:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Reviewers are most effective at reviewing small amounts of code. Anything below 200 lines produces a relatively high rate of defects, often several times the average. After that the results trail off considerably; no review larger than 250 lines produced more than 37 defects per 1000 lines of code.&lt;/p&gt;
  &lt;p&gt;— &lt;a href=&quot;https://functional.computer/assets/documents/SmartBear-PeerCodeReview.pdf&quot;&gt;The Best Kept Secrets of Peer Code Review&lt;/a&gt;, SmartBear&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We used to tolerate the occasional 1000-line pull request, even though we knew it was a terrible idea. Nowadays, I hear that in some organisations, agents are making changesets of tens or hundreds of thousands of lines of code, hour by hour. With any instruction treated as a suggestion (including “run the tests”, or even “ensure that code is covered by tests”), there’s absolutely no way to verify that they’re doing anything meaningful at all. The code has become the purpose; we’ve totally given up on solving problems.&lt;/p&gt;
&lt;p&gt;Perhaps the best argument for commercial LLMs is that &lt;a href=&quot;https://functional.computer/blog/programming-peaked&quot;&gt;quality is going downhill anyway&lt;/a&gt;, and has been for a decade, so why not accelerate it? I’ve heard this from others, too; I asked a few proponents of “vibe” coding and they have all basically made the point that people are simply copying and pasting from Stack Overflow without thinking anyway, so what’s the difference?&lt;/p&gt;
&lt;p&gt;People have been “vibe” coding long before LLMs, and will do it long afterwards.&lt;/p&gt;
&lt;p&gt;I can’t help those programmers, and I don’t see the value in furthering this. It feels to me like we’re missing the value of these tools. Because there is some value, if you ignore the thought leaders.&lt;/p&gt;
&lt;h2&gt;Agents are machines, and good at what machines do&lt;/h2&gt;
&lt;p&gt;As far as I can tell, people want to treat coding agents like people because they talk like people. (People on LinkedIn, who are only about 50% deserving of the term, but still.)&lt;/p&gt;
&lt;p&gt;They’re not people. They are machines with a text front-end. You know, like Google Search.&lt;/p&gt;
&lt;p&gt;And they are good at things that machines are good at. Yes, they can generate infinite output for relatively cheap. This is not valuable. It has not been valuable since someone wrote the first &lt;code&gt;GOTO 10&lt;/code&gt;. (And the loop a million times cheaper.)&lt;/p&gt;
&lt;p&gt;An LLM is a tool for mediocrity. It shines where humans are particularly bad: identifying slight discrepancies or anomalies, spotting missing information, exhaustive search.&lt;/p&gt;
&lt;p&gt;I could see use cases for LLMs in programming, especially in review. Some potential prompts come to mind:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;“identify areas of the code that probably need changing but weren’t”&lt;/li&gt;
  &lt;li&gt;“spot edge cases that are not covered by tests”&lt;/li&gt;
  &lt;li&gt;“identify discrepancies between this implementation and the requirements”&lt;/li&gt;
  &lt;li&gt;“check the comments to make sure they are updated according to code changes”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A review bot that is prompted will do much better than the average disinterested human looking at the diff.&lt;/p&gt;
&lt;p&gt;Humans are, typically, terrible at code review. The bot wins simply because it would actually consume all the input. And unlike generation, it’s additive: it doesn’t prevent a human code review, it just gives you instant feedback &lt;em&gt;before&lt;/em&gt; you share the code with your colleagues, just like a linter.&lt;/p&gt;
&lt;p&gt;It’s a linter that works &lt;strong&gt;if the dice roll well&lt;/strong&gt;. This is obviously not great, and if we can do something with static analysis that works 100% of the time, that’s better; let’s do that. A mechanical code coverage tool or fuzzer will do a better job most of the time. But for cases which are quite subjective or rely on a rudimentary understanding of the written word, I’ll take it.&lt;/p&gt;
&lt;p&gt;I have seen a couple of instances of this kind of thing, and if it were to become standard practice, I don’t think I’d mind. And based on my somewhat out-of-date understanding of neural networks (which peaked around the time of the “Attention is all you need” paper), I expect this would be possible with smaller, local models; you wouldn’t need the power of Anthropic’s GPU farm.&lt;/p&gt;
&lt;p&gt;There are companies using LLMs to find anomalies in logs and alert a human who can investigate. Some pentesters use LLMs to try a ton of stuff which probably won’t work, but might. These tools don’t have to be perfect. Anomaly detection is useful if it is conservative and misses stuff; penetration testing just needs to get one success, even if there’s a thousand failures. These are machines. They can bang on walls until the walls break.&lt;/p&gt;
&lt;p&gt;And of course, they have replaced search engines for many people. This will work for a while, until the well of stolen data dries up. But sure, go ahead. They’ll generate a function for you, or rewrite some YAML into slightly less awful YAML. It might even work. It’s not as good as a blog post or Stack Overflow answer, because there’s no context (a timestamp, the author’s reputation, the problem at hand), but I guess it’ll do.&lt;/p&gt;
&lt;h2&gt;So will I do it?&lt;/h2&gt;
&lt;p&gt;Probably not.&lt;/p&gt;
&lt;p&gt;I can see the use cases of these kind of tools. I cannot forgive the harm that they are causing to our society in destroying what was left of civil discourse, obliterating entire job markets in the ever-growing quest for mediocrity, and turning people I respected into slop merchants. (And there’s an argument to be made that the environmental impact is awful too, but I think it’s harder to prove.)&lt;/p&gt;
&lt;p&gt;I switched away from Google many years ago and I’m happy. My search queries tend to find answers. I don’t need a bot to tell me how to write a function.&lt;/p&gt;
&lt;p&gt;I’ve had the ick since 2023, at least, and it’s not going away. Perhaps when OpenAI and Anthropic are dead and buried, I’ll be able to dislodge it.&lt;/p&gt;
&lt;p&gt;And anyway, code review is stupid. Work with your team and your customers/users, not against them.&lt;/p&gt;
</description><pubDate>Mon, 13 Jul 2026 07:00:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/llms-cant-program</guid>
    </item>
    <item>
      <title>Monadic logic</title>
      <link>https://functional.computer/blog/monadic-logic</link><description>&lt;p&gt;It is a truth universally acknowledged that a data structure in possession of information, must be in want of an extra type parameter.&lt;/p&gt;
&lt;p&gt;In the last couple of posts, we looked at implementing a simple logic DSL in Haskell.&lt;/p&gt;
&lt;ol class=&quot;series-list&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/i-relent&quot;&gt;I relent&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview&quot;&gt;Solving the technical interview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview-explained&quot;&gt;Solving the technical interview, explained&lt;/a&gt;&lt;/li&gt;
  &lt;li aria-current=&quot;page&quot;&gt;Monadic logic&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/fairness-in-disjunctions&quot;&gt;Fairness in disjunctions&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It works, but the syntax is a little un-Haskell-like. We can do better, simply by &lt;del&gt;shoehorning in&lt;/del&gt; &lt;ins&gt;elevating it into&lt;/ins&gt; a monad.&lt;/p&gt;
&lt;p&gt;We start with the basics, except we’ll rename &lt;code&gt;Knowledge&lt;/code&gt; to &lt;code&gt;Subst&lt;/code&gt; (short for “substitutions”), as is more traditional in µKanren:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;{-# LANGUAGE GHC2024 #-}&lt;/span&gt;
&lt;span class=&quot;hljs-meta&quot;&gt;{-# LANGUAGE BlockArguments #-}&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Control.Applicative
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Control.Monad.State
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.Char &lt;span class=&quot;hljs-keyword&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; Char
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.List &lt;span class=&quot;hljs-keyword&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; List
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.Maybe (&lt;span class=&quot;hljs-title&quot;&gt;listToMaybe&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Prelude &lt;span class=&quot;hljs-keyword&quot;&gt;hiding&lt;/span&gt; (&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; System.Environment (&lt;span class=&quot;hljs-title&quot;&gt;getArgs&lt;/span&gt;)

&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Enum&lt;/span&gt;)&lt;/span&gt;

&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom&lt;/span&gt;
  = &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; atom
  | &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;)

&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Subst&lt;/span&gt; atom = [(&lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;atom&lt;/span&gt;)]&lt;/span&gt;

&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; a = &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Subst&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt;)&lt;/span&gt;

&lt;span class=&quot;hljs-title&quot;&gt;emptyState&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;emptyState&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;) []
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Consider the &lt;code&gt;Lesson&lt;/code&gt;, which we will rename here to its more conventional name, &lt;code&gt;Goal&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;type Goal atom = GoalState atom -&gt; [GoalState atom]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can make it carry an extra value around:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;type Goal atom a = GoalState atom -&gt; [(GoalState atom, a)]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you are a Haskell programmer, you might recognise this as very similar to the &lt;code&gt;State&lt;/code&gt; monad, except the return type is a list. It is, in fact, the same as &lt;code&gt;StateT (GoalState atom) []&lt;/code&gt;. For example, the &lt;code&gt;Control.Monad.State.runStateT&lt;/code&gt; function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;runStateT :: StateT s m a -&gt; s -&gt; m (a, s)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;specialises to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;runStateT :: StateT (GoalState atom) [] a -&gt; GoalState atom -&gt; [(a, GoalState atom)]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Except for the order of the tuple, this is a function that takes a &lt;code&gt;StateT (GoalState atom) [] a&lt;/code&gt; and returns the exact data type of our &lt;code&gt;Goal&lt;/code&gt;. So let’s use it:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a = &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;StateT&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;atom&lt;/span&gt;) [] a)&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Functor&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Applicative&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Alternative&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Monad&lt;/span&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And as &lt;code&gt;Goal&lt;/code&gt; is now a wrapper around an exisiting monad type, and we can claim all of the benefits with &lt;code&gt;deriving newtype&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Each output of the goal (which you may remember can be zero, one, or many, represented by a list here) also carries a value, which allows us to pass stuff around. This will be useful later.&lt;/p&gt;
&lt;p&gt;We also derive &lt;code&gt;Alternative&lt;/code&gt; as a replacement for &lt;code&gt;Monoid&lt;/code&gt;; we will use &lt;code&gt;empty&lt;/code&gt; instead of &lt;code&gt;mempty&lt;/code&gt; and &lt;code&gt;(&amp;#x3C;|&gt;)&lt;/code&gt; instead of &lt;code&gt;(&amp;#x3C;&gt;)&lt;/code&gt;. For lists, they are equivalent.&lt;/p&gt;
&lt;p&gt;We’ll need to modify the combinators to fit the new type signature, starting with the basics:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom ()
&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; = empty

&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom ()
&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; = pure ()

&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom () -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom ()
&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; g) = &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  state &amp;#x3C;- get
  &lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt; evalStateT g state &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt;
    [] -&gt; pure ()
    _ -&gt; empty
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note how it’s much clearer now that &lt;code&gt;negate&lt;/code&gt; is working on the current version of the state, and therefore can only negate things that come before, not after.&lt;/p&gt;
&lt;p&gt;Equality works the same way except that it wraps the behavior in &lt;code&gt;StateT&lt;/code&gt; (and therefore has to pass &lt;code&gt;subst&lt;/code&gt; around).&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;infix&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt; ===

(===) :: &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; atom. (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; atom) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom ()
&lt;span class=&quot;hljs-title&quot;&gt;x&lt;/span&gt; === y = &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; $ &lt;span class=&quot;hljs-type&quot;&gt;StateT&lt;/span&gt; &#92;(&lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; vars subst) -&gt;
  maybe mempty (&#92;newSubst -&gt; pure ((), &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; vars newSubst)) $
    unifyTerm (walk subst x) (walk subst y) subst
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    unifyTerm (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x&#39;) (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y&#39;) subst
      | x&#39; == y&#39; = &lt;span class=&quot;hljs-type&quot;&gt;Just&lt;/span&gt; subst
      | otherwise = &lt;span class=&quot;hljs-type&quot;&gt;Nothing&lt;/span&gt;
    unifyTerm (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) y&#39; subst = unifyVar v y&#39; subst
    unifyTerm x&#39; (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) subst = unifyVar v x&#39; subst
    unifyVar v t subst
      | occurs v t = &lt;span class=&quot;hljs-type&quot;&gt;Nothing&lt;/span&gt;
      | otherwise = &lt;span class=&quot;hljs-type&quot;&gt;Just&lt;/span&gt; (pure (v, t) &amp;#x3C;&gt; subst)
    occurs _ (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; _) = &lt;span class=&quot;hljs-type&quot;&gt;False&lt;/span&gt;
    occurs v (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; tv) = tv == v
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt; and &lt;code&gt;|||&lt;/code&gt; are almost the same:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;infixr&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt; &amp;#x26;&amp;#x26;&amp;#x26;

(&amp;#x26;&amp;#x26;&amp;#x26;) :: &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a
(&amp;#x26;&amp;#x26;&amp;#x26;) = (&gt;&gt;)

&lt;span class=&quot;hljs-keyword&quot;&gt;infixr&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt; |||

(|||) :: &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a
(|||) = (&amp;#x3C;|&gt;)

&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; atom) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom] -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom ()
&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; x = foldr ((|||) . (=== x)) falsity
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And &lt;code&gt;fresh&lt;/code&gt; (previously &lt;code&gt;consider&lt;/code&gt;) is where it gets interesting. Rather than accepting a lambda, &lt;code&gt;fresh&lt;/code&gt; can return a value.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;fresh&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom (&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom)
&lt;span class=&quot;hljs-title&quot;&gt;fresh&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; nextVar subst &amp;#x3C;- get
  put $ &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; (succ nextVar) subst
  pure (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; nextVar)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can do the same for &lt;code&gt;understand&lt;/code&gt;, which I am renaming to &lt;code&gt;current&lt;/code&gt; (and doesn’t have a name in µKanren):&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;current&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom (&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom)
&lt;span class=&quot;hljs-title&quot;&gt;current&lt;/span&gt; term = &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; _ subst &amp;#x3C;- get
  pure $ walk subst term
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;climb&lt;/code&gt; and &lt;code&gt;learn&lt;/code&gt; get renamed to &lt;code&gt;walk&lt;/code&gt; and &lt;code&gt;run&lt;/code&gt;, and &lt;code&gt;run&lt;/code&gt; is modified to return the value as well as the &lt;code&gt;subst&lt;/code&gt;.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;walk&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Subst&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;walk&lt;/span&gt; _ term@&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; {} = term
&lt;span class=&quot;hljs-title&quot;&gt;walk&lt;/span&gt; subst (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) =
  maybe (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) (walk subst) $ lookup v subst

&lt;span class=&quot;hljs-title&quot;&gt;run&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom a -&gt; [(a, &lt;span class=&quot;hljs-type&quot;&gt;Subst&lt;/span&gt; atom)]
&lt;span class=&quot;hljs-title&quot;&gt;run&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; goal) =
  map (&#92;(x, &lt;span class=&quot;hljs-type&quot;&gt;GoalState&lt;/span&gt; _ subst) -&gt; (x, subst)) $ runStateT goal emptyState
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And now that we have a logic system working, we can start defining our solution for the &lt;code&gt;N&lt;/code&gt; queens problem.&lt;/p&gt;
&lt;p&gt;We start with &lt;code&gt;add&lt;/code&gt;, which uses our monadic version of &lt;code&gt;current&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;add&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; atom, &lt;span class=&quot;hljs-type&quot;&gt;Num&lt;/span&gt; atom) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; atom -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; atom ()
&lt;span class=&quot;hljs-title&quot;&gt;add&lt;/span&gt; x y z = &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  x &amp;#x3C;- current x
  y &amp;#x3C;- current y
  z &amp;#x3C;- current z
  &lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt; (x, y, z) &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt;
    (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y, z) -&gt; z === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (x + y)
    (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x, y, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; z) -&gt; y === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (z - x)
    (x, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; z) -&gt; x === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (z - y)
    _ -&gt; falsity
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And now we can define &lt;code&gt;queens&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;queens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Goal&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; [(&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;)]
&lt;span class=&quot;hljs-title&quot;&gt;queens&lt;/span&gt; n = queens&#39; n []
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    valid = map &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; [&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; .. pred n]
    queens&#39; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; _ = pure []
    queens&#39; m threatened = &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
      rank &amp;#x3C;- fresh
      file &amp;#x3C;- fresh
      diag1 &amp;#x3C;- fresh
      diag2 &amp;#x3C;- fresh
      rank `within` valid
      negate (rank `within` threatenedRanks)
      file `within` valid
      negate (file `within` threatenedFiles)
      add diag1 rank file
      negate (diag1 `within` threatenedDiag1)
      add rank file diag2
      negate (diag2 `within` threatenedDiag2)
      positions &amp;#x3C;- queens&#39; (pred m) ((rank, file, diag1, diag2) : threatened)
      pure $ (rank, file) : positions
      &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
        (threatenedRanks, threatenedFiles, threatenedDiag1, threatenedDiag2) = &lt;span class=&quot;hljs-type&quot;&gt;List&lt;/span&gt;.unzip4 threatened
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have rewritten the whole thing in &lt;code&gt;do&lt;/code&gt; notation, using bindings to allocate &lt;code&gt;fresh&lt;/code&gt; variables. And we’ve lost all the &lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt; chaining because it’s just &lt;code&gt;&gt;&gt;&lt;/code&gt;, which means it can be replaced with a newline.&lt;/p&gt;
&lt;p&gt;Check out the new return type. We also return the lists of positions (ranks and files), which we can use for a much more pleasant way of solving this problem.&lt;/p&gt;
&lt;p&gt;We define our &lt;code&gt;Position&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Ord&lt;/span&gt;)
&lt;span class=&quot;hljs-class&quot;&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Show&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
  show (&lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; rank file) =
    &lt;span class=&quot;hljs-type&quot;&gt;Char&lt;/span&gt;.chr (rank + &lt;span class=&quot;hljs-type&quot;&gt;Char&lt;/span&gt;.ord &lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;) : show (file + &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And now we can rewrite &lt;code&gt;solveQueens&lt;/code&gt; to take advantage of our new return value. We have the terms, but they are &lt;code&gt;TVar&lt;/code&gt;s, so we need to walk over &lt;code&gt;subst&lt;/code&gt; to get the actual &lt;code&gt;TAtom&lt;/code&gt; values out. We no longer need to guess the variable names.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;findQueen&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Subst&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;) -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt;
&lt;span class=&quot;hljs-title&quot;&gt;findQueen&lt;/span&gt; subst (rankTerm, fileTerm) = &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; rank file
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; rank = walk subst rankTerm
    &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; file = walk subst fileTerm

&lt;span class=&quot;hljs-title&quot;&gt;solveQueens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt;]
&lt;span class=&quot;hljs-title&quot;&gt;solveQueens&lt;/span&gt; n = map (findQueen subst) positionTerms
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    (positionTerms, subst) = head $ run (queens n)

&lt;span class=&quot;hljs-title&quot;&gt;main&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;IO&lt;/span&gt; ()
&lt;span class=&quot;hljs-title&quot;&gt;main&lt;/span&gt; = &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  args &amp;#x3C;- getArgs
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; n = maybe &lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt; read $ listToMaybe args
  print $ solveQueens n
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There we go. Add a type parameter, get a free monad, and much more understandable code.&lt;/p&gt;
&lt;hr /&gt;
&lt;ol class=&quot;series-list&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/i-relent&quot;&gt;I relent&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview&quot;&gt;Solving the technical interview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview-explained&quot;&gt;Solving the technical interview, explained&lt;/a&gt;&lt;/li&gt;
  &lt;li aria-current=&quot;page&quot;&gt;Monadic logic&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/fairness-in-disjunctions&quot;&gt;Fairness in disjunctions&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</description><pubDate>Wed, 24 Jun 2026 07:00:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/monadic-logic</guid>
    </item>
    <item>
      <title>Solving the technical interview, explained</title>
      <link>https://functional.computer/blog/solving-the-technical-interview-explained</link><description>&lt;p&gt;Let’s talk about &lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview&quot;&gt;Solving the technical interview&lt;/a&gt;, and how it all works.&lt;/p&gt;
&lt;p&gt;You probably want to read that first. And as it’s based on stories by Aphyr, read those too. And there’s a riff by Richard Towers.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://aphyr.com/posts/340-reversing-the-technical-interview&quot;&gt;Reversing the technical interview&lt;/a&gt;, by Aphyr&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://aphyr.com/posts/341-hexing-the-technical-interview&quot;&gt;Hexing the technical interview&lt;/a&gt;, by Aphyr&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://aphyr.com/posts/342-typing-the-technical-interview&quot;&gt;Typing the technical interview&lt;/a&gt;, by Aphyr&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.richard-towers.com/2023/03/11/typescripting-the-technical-interview.html&quot;&gt;Typescripting the technical interview&lt;/a&gt;, by Richard Towers&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview&quot;&gt;Solving the technical interview&lt;/a&gt;, by me&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Once you’re done with all that, let’s talk about how the last one works.&lt;/p&gt;
&lt;p&gt;We are solving the &lt;a href=&quot;https://en.wikipedia.org/wiki/Eight_queens_puzzle&quot;&gt;eight queens puzzle&lt;/a&gt;, or rather, a generalization called “&lt;code&gt;N&lt;/code&gt; queens”. As our interviewer explains:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;“Well,” you stammer, “imagine a chessboard. The problem is called &lt;code&gt;N&lt;/code&gt;-Queens. For a given chessboard of size &lt;code&gt;N&lt;/code&gt;×&lt;code&gt;N&lt;/code&gt;, you need to find a way to place &lt;code&gt;N&lt;/code&gt; queens on that board, safely.”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this context, “safely” means that the queen is not threatened by another.&lt;/p&gt;
&lt;p&gt;
  Our witch, who seems to have been to the same school of witchcraft as Gytha Ogg, has decided to write Haskell. The source file for the article is actually &lt;a href=&quot;https://codeberg.org/ooble/self/src/branch/main/content/blog/2026/2026-05-23--solving-the-technical-interview.md.lhs&quot;&gt;a literate Haskell file&lt;/a&gt;, processed with &lt;a href=&quot;https://github.com/sol/markdown-unlit&quot;&gt;&lt;code&gt;markdown-unlit&lt;/code&gt;&lt;/a
  &gt;, and therefore can be both compiled to a binary with &lt;code&gt;ghc -pgmL markdown-unlit&lt;/code&gt;, and rendered to HTML.
&lt;/p&gt;
&lt;h2&gt;Preamble&lt;/h2&gt;
&lt;p&gt;She starts with some preamble. She sets the language “edition” to &lt;code&gt;GHC2024&lt;/code&gt;, which is, effectively, &lt;a href=&quot;https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/control.html#extension-GHC2024&quot;&gt;a group of language extensions&lt;/a&gt; to enable. It doesn’t include &lt;code&gt;BlockArguments&lt;/code&gt; though, so she turns that on explicitly. (&lt;code&gt;BlockArguments&lt;/code&gt; lets us avoid the &lt;code&gt;$&lt;/code&gt; when the last argument to a function is a &lt;code&gt;do&lt;/code&gt; block or a lambda expression.)&lt;/p&gt;
&lt;p&gt;She also disables the “name shadowing” warning, because it’s useful to re-use names in certain contexts.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;{-# LANGUAGE GHC2024 #-}&lt;/span&gt;
&lt;span class=&quot;hljs-meta&quot;&gt;{-# LANGUAGE BlockArguments #-}&lt;/span&gt;
&lt;span class=&quot;hljs-meta&quot;&gt;{-# OPTIONS_GHC -Wno-name-shadowing #-}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;She then imports a bunch of stuff. Boring, but necessary.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; Main &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Control.Monad ((&gt;=&gt;))
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.Char &lt;span class=&quot;hljs-keyword&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; Char
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.Maybe (&lt;span class=&quot;hljs-title&quot;&gt;listToMaybe&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.List &lt;span class=&quot;hljs-keyword&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; List
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Prelude &lt;span class=&quot;hljs-keyword&quot;&gt;hiding&lt;/span&gt; (&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; System.Environment (&lt;span class=&quot;hljs-title&quot;&gt;getArgs&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our witch has a plan: she is implementing an embedded DSL for logic programming called &lt;a href=&quot;https://functional.computer/assets/documents/microKanren.pdf&quot;&gt;µKanren&lt;/a&gt;. This is a very small DSL, based on &lt;a href=&quot;https://minikanren.org/&quot;&gt;miniKanren&lt;/a&gt; but implementable in very few lines of code.&lt;/p&gt;
&lt;h2&gt;Primitives&lt;/h2&gt;
&lt;p&gt;She defines a couple of small data structures. First, a variable, which is just a wrapper around an integer. (It doesn’t really matter what it is, but generating new integers is easier than generating e.g. new strings.)&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Enum&lt;/span&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then a term. A term can be an atom (which is any data type; it’s parameterised) or a variable. There can be other kinds of terms, but we don’t need them here.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a&lt;/span&gt;
  = &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; a
  | &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;She also defines a &lt;code&gt;Knowledge&lt;/code&gt;, which is better known as a &lt;code&gt;Substitution&lt;/code&gt; in µKanren.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; a = [(&lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This &lt;code&gt;Knowledge&lt;/code&gt; is modelled as an association list: we insert pairs of mapping from variables to terms. In reality though, it’s a set of tree structures. Looking up a variable might yield an atom, but it might instead point to another variable, which points to another term, which… you get the idea. The witch traverses this tree with a &lt;code&gt;climb&lt;/code&gt; function (&lt;code&gt;walk&lt;/code&gt; in µKanren):&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;climb&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;climb&lt;/span&gt; _ term@&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; {} = term
&lt;span class=&quot;hljs-title&quot;&gt;climb&lt;/span&gt; knowledge (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) =
  maybe (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) (climb knowledge) $ lookup v knowledge
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Together, &lt;code&gt;Knowledge&lt;/code&gt; and &lt;code&gt;climb&lt;/code&gt; are a very simple implementation of a logic system. If we can record a piece of knowledge, we can combine it with others to derive new information.&lt;/p&gt;
&lt;h2&gt;Lessons, or Goals&lt;/h2&gt;
&lt;p&gt;She then defines a &lt;code&gt;Lesson&lt;/code&gt; (a &lt;code&gt;Goal&lt;/code&gt;), and some state. The &lt;code&gt;State&lt;/code&gt; stores two pieces of information: the next free variable, and the current knowledge in the system. At first, it starts with the variable &lt;code&gt;0&lt;/code&gt;, and no knowledge (and therefore an empty list).&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;Lesson&lt;/code&gt; is really a function from some state to a list of possible states. This means that any given lesson takes the current state, and might return zero, one, or many new states. Zero states means that we’ve hit the end of the road, and should try another path. If we have many, we can evaluate all of them to see where they might lead.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a = &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]&lt;/span&gt;

&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a = &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt;)&lt;/span&gt;

&lt;span class=&quot;hljs-title&quot;&gt;emptyState&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;emptyState&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;) []
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, let’s define a lesson. Our witch’s first lesson is &lt;code&gt;falsity&lt;/code&gt;, or &lt;code&gt;zero&lt;/code&gt;, or &lt;code&gt;bottom&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; = mempty
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This always discards the state and produces no new states. The implementation, &lt;code&gt;mempty&lt;/code&gt;, might look surprising; let’s break it down.&lt;/p&gt;
&lt;p&gt;We could define &lt;code&gt;falsity&lt;/code&gt; like this; it would be the clearest implementation.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; _ = []
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can replace the wildcard with &lt;code&gt;const&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; = const []
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, a list, &lt;code&gt;[State a]&lt;/code&gt;, is a &lt;code&gt;Monoid (State a)&lt;/code&gt;, and therefore we can use &lt;code&gt;mempty&lt;/code&gt; to construct the empty list.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; = const mempty
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This isn’t better, except that &lt;code&gt;Monoid b =&gt; Monoid (a -&gt; b)&lt;/code&gt;; i.e. if a type is a monoid, then any function that returns that type is also a monoid. There’s only one possible implementation of this: it must be &lt;code&gt;mempty = const mempty&lt;/code&gt;, and therefore we can define &lt;code&gt;falsity = mempty&lt;/code&gt;, as in the original definition:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; = mempty
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;She then implements &lt;code&gt;truth&lt;/code&gt;, or &lt;code&gt;one&lt;/code&gt;, or &lt;code&gt;top&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; = pure
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Breaking this down similarly:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; state = [state]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can replace &lt;code&gt;[state]&lt;/code&gt; with &lt;code&gt;pure state&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; state = pure state
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Haskell, functions are values, which means we can use “eta reduction” to simplify. If &lt;code&gt;f x = g x&lt;/code&gt;, then &lt;code&gt;f = g&lt;/code&gt;. We remove the argument from both sides to recover the original definition:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; = pure
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These definitions are terse and somewhat confusing on purpose; our witch is messing with the interviewer. In code designed to be more readable, you might use one of the other options.&lt;/p&gt;
&lt;p&gt;The next primitive she defines is &lt;code&gt;negate&lt;/code&gt;, which converts a success (one or more output states) into a failure (zero output states), and a failure into a success.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt; lesson state = &lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt; lesson state &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt;
  [] -&gt; pure state
  _ -&gt; mempty
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is hopefully relatively clear if you remember that &lt;code&gt;lesson&lt;/code&gt; is a &lt;code&gt;Lesson&lt;/code&gt; function, and can accept a state as input. &lt;code&gt;negate lesson&lt;/code&gt; is also a &lt;code&gt;Lesson&lt;/code&gt; function, and it therefore takes an extra parameter on the left-hand side too.&lt;/p&gt;
&lt;h2&gt;Equality, and unification&lt;/h2&gt;
&lt;p&gt;The most complicated part of µKanren is equality, so let’s tackle it piece by piece.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Equality (&lt;code&gt;===&lt;/code&gt;) yields a &lt;code&gt;Lesson&lt;/code&gt;, which means it accepts a state (&lt;code&gt;State vars knowledge&lt;/code&gt;) as its extra parameter.&lt;/li&gt;
  &lt;li&gt;It uses this &lt;code&gt;knowledge&lt;/code&gt; to &lt;code&gt;climb&lt;/code&gt; for both the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; terms, traversing the tree as far as it can for each.&lt;/li&gt;
  &lt;li&gt;We then “unify” these terms. &lt;code&gt;unifyTerm&lt;/code&gt; returns a &lt;code&gt;Maybe (Knowledge a)&lt;/code&gt;; we use &lt;code&gt;Just&lt;/code&gt; to represent success, and &lt;code&gt;Nothing&lt;/code&gt; to represent failure.&lt;/li&gt;
  &lt;li&gt;If it succeeds, &lt;code&gt;===&lt;/code&gt; will recombine the knowledge with the existing state.&lt;/li&gt;
  &lt;li&gt;If it fails, it returns &lt;code&gt;mempty&lt;/code&gt; (an empty list), which means this lesson goes no further.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(===) :: &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; a. (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; a) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(x === y) (&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; vars knowledge) =
  maybe mempty (pure . &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; vars) $
    unifyTerm (climb knowledge x) (climb knowledge y)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;unifyTerm&lt;/code&gt; will unify two terms to figure out if (a) new knowledge is gained, (b) the existing knowledge is true, or (c) the knowledge is false.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;If the resulting terms are atoms, and equal, it succeeds with our original knowledge. Nothing new has been learned, but everything we knew is still valid.&lt;/li&gt;
  &lt;li&gt;If they’re atoms but unequal, it fails. We’ve tried to unify two values that don’t match up, and so our knowledge has led us down the wrong path. We discard it.&lt;/li&gt;
  &lt;li&gt;If one of the terms is a variable, it calls &lt;code&gt;unifyVar&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;    unifyTerm (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x&#39;) (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y&#39;)
      | x&#39; == y&#39; = &lt;span class=&quot;hljs-type&quot;&gt;Just&lt;/span&gt; knowledge
      | otherwise = &lt;span class=&quot;hljs-type&quot;&gt;Nothing&lt;/span&gt;
    unifyTerm (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) y&#39; = unifyVar v y&#39;
    unifyTerm x&#39; (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) = unifyVar v x&#39;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;unifyVar&lt;/code&gt; produces new knowledge.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Unifying a variable with a term checks if the variable “occurs” in the term. If it does, it fails, because this would create a cycle.&lt;/li&gt;
  &lt;li&gt;If there’s no cycles, it prefixes the knowledge with something new: the pair, &lt;code&gt;(v, t)&lt;/code&gt;. This is the only way to introduce new knowledge in µKanren.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;    unifyVar v t
      | occurs v t = &lt;span class=&quot;hljs-type&quot;&gt;Nothing&lt;/span&gt;
      | otherwise = &lt;span class=&quot;hljs-type&quot;&gt;Just&lt;/span&gt; (pure (v, t) &amp;#x3C;&gt; knowledge)
    occurs _ (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; _) = &lt;span class=&quot;hljs-type&quot;&gt;False&lt;/span&gt;
    occurs v (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; tv) = tv == v
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We connect it all together, and put the unification functions in a &lt;code&gt;where&lt;/code&gt; block so they can access &lt;code&gt;knowledge&lt;/code&gt; without having to pass it around.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(===) :: &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; a. (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; a) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(x === y) (&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; vars knowledge) =
  maybe mempty (pure . &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; vars) $
    unifyTerm (climb knowledge x) (climb knowledge y)
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    unifyTerm (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x&#39;) (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y&#39;)
      | x&#39; == y&#39; = &lt;span class=&quot;hljs-type&quot;&gt;Just&lt;/span&gt; knowledge
      | otherwise = &lt;span class=&quot;hljs-type&quot;&gt;Nothing&lt;/span&gt;
    unifyTerm (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) y&#39; = unifyVar v y&#39;
    unifyTerm x&#39; (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) = unifyVar v x&#39;
    unifyVar v t
      | occurs v t = &lt;span class=&quot;hljs-type&quot;&gt;Nothing&lt;/span&gt;
      | otherwise = &lt;span class=&quot;hljs-type&quot;&gt;Just&lt;/span&gt; (pure (v, t) &amp;#x3C;&gt; knowledge)
    occurs _ (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; _) = &lt;span class=&quot;hljs-type&quot;&gt;False&lt;/span&gt;
    occurs v (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; tv) = tv == v
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;unify&lt;/code&gt; is the heart of how we build knowledge. It’s a popular algorithm; you’ll see something similar in most type-checkers too. (And for some reason, it’s rarely written down, and when it is, &lt;a href=&quot;https://www.norvig.com/unify-bug.pdf&quot;&gt;there’s always a bug&lt;/a&gt;. The µKanren &lt;code&gt;unify&lt;/code&gt; doesn’t bother with an &lt;code&gt;occurs&lt;/code&gt; check. This is fine in certain cases, but dangerous in others.)&lt;/p&gt;
&lt;h2&gt;Combinators&lt;/h2&gt;
&lt;p&gt;Next, the witch defines her &lt;code&gt;Lesson&lt;/code&gt; combinators.&lt;/p&gt;
&lt;h3&gt;Conjunction and disjunction&lt;/h3&gt;
&lt;p&gt;She starts with conjunction, &lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt; (&lt;code&gt;conj&lt;/code&gt; in µKanren), which combines two lessons, producing only knowledge that holds under both:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(&amp;#x26;&amp;#x26;&amp;#x26;) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(&amp;#x26;&amp;#x26;&amp;#x26;) = (&gt;=&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is probably unclear, so let’s derive it. She wants something like this, which calls &lt;code&gt;a&lt;/code&gt; with the input state, and for each new state, calls &lt;code&gt;b&lt;/code&gt;, then concatenates all the results.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(&amp;#x26;&amp;#x26;&amp;#x26;) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt; &amp;#x26;&amp;#x26;&amp;#x26; b = &#92;state -&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; newStates = a state &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; concatMap b newStates
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can inline &lt;code&gt;newStates&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(&amp;#x26;&amp;#x26;&amp;#x26;) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt; &amp;#x26;&amp;#x26;&amp;#x26; b = &#92;state -&gt; concatMap b (a state)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, &lt;code&gt;concatMap&lt;/code&gt; is also known as &lt;code&gt;&gt;&gt;=&lt;/code&gt; (“bind”):&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(&amp;#x26;&amp;#x26;&amp;#x26;) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt; &amp;#x26;&amp;#x26;&amp;#x26; b = &#92;state -&gt; a state &gt;&gt;= b
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a pretty common pattern. So common that there’s a special operator for it, “Kleisli composition”:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(&gt;=&gt;): &lt;span class=&quot;hljs-type&quot;&gt;Monad&lt;/span&gt; m =&gt; (a -&gt; m b) -&gt; (b -&gt; m c) -&gt; (a -&gt; m c)
&lt;span class=&quot;hljs-title&quot;&gt;f&lt;/span&gt; &gt;=&gt; g = &#92;x -&gt; f x &gt;&gt;= g
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So let’s use it:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(&amp;#x26;&amp;#x26;&amp;#x26;) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt; &amp;#x26;&amp;#x26;&amp;#x26; b = a &gt;=&gt; b
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we can use eta reduction:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(&amp;#x26;&amp;#x26;&amp;#x26;) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(&amp;#x26;&amp;#x26;&amp;#x26;) = (&gt;=&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It’s worth noting that while &lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt; goes from left to right, it should be commutative; i.e. &lt;code&gt;a &amp;#x26;&amp;#x26;&amp;#x26; b&lt;/code&gt; should always get the same result (or something indistinguishable) as &lt;code&gt;b &amp;#x26;&amp;#x26;&amp;#x26; a&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next, &lt;code&gt;|||&lt;/code&gt;, disjunction (&lt;code&gt;disj&lt;/code&gt; in µKanren), which, given two lessons, lets us try both.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(|||) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(|||) = (&amp;#x3C;&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Again, let’s derive it. The way it works is to apply the state to both, and then concatenate the results together with &lt;code&gt;&amp;#x3C;&gt;&lt;/code&gt;. This is our main way of getting more than one state as an output. We can implement that:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(|||) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt; ||| b = &#92;state -&gt; a state &amp;#x3C;&gt; b state
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;&amp;#x3C;&gt;&lt;/code&gt; comes from the &lt;code&gt;Semigroup&lt;/code&gt; typeclass, and there’s an implementation for functions too:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Semigroup&lt;/span&gt; b =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Semigroup&lt;/span&gt; (&lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt; -&gt; &lt;span class=&quot;hljs-title&quot;&gt;b&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
  f &amp;#x3C;&gt; g = &#92;x -&gt; f x &amp;#x3C;&gt; g x
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In other words, concatenating two functions is the same as calling the two functions with the same argument and concatenating the results. You can see how the pattern is the same as in our &lt;code&gt;|||&lt;/code&gt; combinator, so let’s use it:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(|||) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt; ||| b = a &amp;#x3C;&gt; b
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And, again, eta reduction lets us drop the parameters:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(|||) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(|||) = (&amp;#x3C;&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The boolean logic laws hold:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;falsity &amp;#x26;&amp;#x26;&amp;#x26; x&lt;/code&gt; always fails (yields no results)&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;truth &amp;#x26;&amp;#x26;&amp;#x26; x&lt;/code&gt; is the same as &lt;code&gt;x&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;falsity ||| x&lt;/code&gt; is the same as &lt;code&gt;x&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;truth ||| x&lt;/code&gt; always succeeds (yields at least one result)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both operators are commutative and can therefore be flipped.&lt;/p&gt;
&lt;p&gt;We can derive the usual truth tables, too. For example, here’s the table for &lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt;:&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt;&lt;/th&gt;
      &lt;th&gt;falsity&lt;/th&gt;
      &lt;th&gt;truth&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;falsity&lt;/td&gt;
      &lt;td&gt;falsity&lt;/td&gt;
      &lt;td&gt;falsity&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;truth&lt;/td&gt;
      &lt;td&gt;falsity&lt;/td&gt;
      &lt;td&gt;truth&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;I’ll leave &lt;code&gt;|||&lt;/code&gt; as an exercise.&lt;/p&gt;
&lt;h3&gt;Variables&lt;/h3&gt;
&lt;p&gt;The final combinator in µKanren is &lt;code&gt;consider&lt;/code&gt;, known as &lt;code&gt;fresh&lt;/code&gt; in µKanren because it creates a “fresh” variable.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;consider&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a) -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;consider&lt;/span&gt; f (&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; nextVar knowledge) =
  f (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; nextVar) (&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; (succ nextVar) knowledge)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Given a lesson that accepts an extra term, we construct one, &lt;code&gt;TVar nextVar&lt;/code&gt;, using the current variable in the state. We increment the variable counter with &lt;code&gt;succ&lt;/code&gt; to make sure that the variable is not re-used.&lt;/p&gt;
&lt;h3&gt;Understanding&lt;/h3&gt;
&lt;p&gt;The witch also defines one extra combinator here, &lt;code&gt;understand&lt;/code&gt;, which takes a term, and a function which accepts that term. It passes it through after calling &lt;code&gt;climb knowledge term&lt;/code&gt; to figure out as much as possible about the term. This is useful when you need to perform an operation on existing knowledge to derive something new, but it breaks some assumptions about how &lt;code&gt;Knowledge&lt;/code&gt; works. Because it digs into the knowledge before it’s complete, it might not find what it’s looking for. It therefore makes &lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt; asymmetric, because earlier operations can influence later ones.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;understand&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a) -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;understand&lt;/span&gt; term f state@(&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; _ knowledge) =
  f (climb knowledge term) state
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Learning, or running&lt;/h2&gt;
&lt;p&gt;Finally, we stick it all together with &lt;code&gt;learn&lt;/code&gt; (&lt;code&gt;run&lt;/code&gt; in µKanren):&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;learn&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;learn&lt;/span&gt; lesson =
  map (&#92;(&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; _ knowledge) -&gt; knowledge) $ lesson emptyState
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once we’ve created a &lt;code&gt;Lesson&lt;/code&gt; by combining as many as we’d like, we call it with &lt;code&gt;emptyState&lt;/code&gt; to get it started, and then get the &lt;code&gt;Knowledge&lt;/code&gt; out of the state at the end. This function therefore handles the state so callers don’t have to know about it.&lt;/p&gt;
&lt;h2&gt;Helpers&lt;/h2&gt;
&lt;p&gt;We’ve now implemented µKanren. Let’s use it. The witch defines an &lt;code&gt;add&lt;/code&gt; relation which uses &lt;code&gt;understand&lt;/code&gt; to dive into the knowledge and get some values out. It takes three parameters, &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;z&lt;/code&gt;, and defines &lt;code&gt;z&lt;/code&gt; as &lt;code&gt;x + y&lt;/code&gt;. This means that if we know &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, we can unify them accordingly, &lt;em&gt;but&lt;/em&gt; we can also go backwards, unifying &lt;code&gt;y&lt;/code&gt; with &lt;code&gt;z - x&lt;/code&gt; and &lt;code&gt;x&lt;/code&gt; with &lt;code&gt;z - y&lt;/code&gt;. Addition and subtraction are part of the same relation.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;add&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; a, &lt;span class=&quot;hljs-type&quot;&gt;Num&lt;/span&gt; a) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;add&lt;/span&gt; x y z = understand x &#92;x -&gt; understand y &#92;y -&gt; understand z &#92;z -&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt; (x, y, z) &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt;
    (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y, z) -&gt; z === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (x + y)
    (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x, y, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; z) -&gt; y === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (z - x)
    (x, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; z) -&gt; x === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (z - y)
    _ -&gt; falsity
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;She also defines &lt;code&gt;within&lt;/code&gt;, a wrapper around &lt;code&gt;===&lt;/code&gt; which checks if a term can be unified with any of a list of terms:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; a) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a] -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; x = foldr ((|||) . (=== x)) falsity
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is probably a bit confusing. Let’s try it this way:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; a) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a] -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; x xs = foldr (|||) falsity $ map (=== x) xs
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It first transforms all terms in the list, &lt;code&gt;xs&lt;/code&gt;, to unifications between the terms and the input term, &lt;code&gt;x&lt;/code&gt;. It then uses &lt;code&gt;foldr&lt;/code&gt; to insert &lt;code&gt;|||&lt;/code&gt; between each element in the list, which will find the disjunction of all of them, using &lt;code&gt;falsity&lt;/code&gt; as the base case. So if any term in &lt;code&gt;xs&lt;/code&gt; unifies with &lt;code&gt;x&lt;/code&gt;, this will succeed.&lt;/p&gt;
&lt;p&gt;If we expand it out, it might help:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; x) [&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;]
  = foldr ((|||) . (=== &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; x)) falsity [&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;]
  = foldr (|||) falsity $ map (=== &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; x) [&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;]
  = foldr (|||) falsity $ [&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt; === &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; x, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt; === &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; x, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt; === &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; x]
  = &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt; === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x ||| (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt; === &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; x ||| (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt; === &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; x ||| falsity))
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Solving the puzzle&lt;/h2&gt;
&lt;p&gt;Now, let’s define &lt;code&gt;queens&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;queens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;hljs-title&quot;&gt;queens&lt;/span&gt; n = queens&#39; n []
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We simply delegate to a helper with an extra argument, an empty list. This list will build up the existing variables so we can unify them with new ones. We’ll call it &lt;code&gt;threatened&lt;/code&gt; as it’s going to track which squares are threatened.&lt;/p&gt;
&lt;p&gt;An &lt;code&gt;N&lt;/code&gt;×&lt;code&gt;N&lt;/code&gt; chessboard has &lt;code&gt;N&lt;/code&gt; ranks and &lt;code&gt;N&lt;/code&gt; files, so let’s make sure that our rank and file are within those values. We create new variables for each of these:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    valid = map &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; [&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; .. pred n]
    queens&#39; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; _ = truth
    queens&#39; m threatened = consider &#92;rank -&gt; consider &#92;file -&gt; consider &#92;diag1 -&gt; consider &#92;diag2 -&gt;
      (rank `within` valid)
        &amp;#x26;&amp;#x26;&amp;#x26; (file `within` valid)
        &lt;span class=&quot;hljs-comment&quot;&gt;-- &amp;#x26;&amp;#x26;&amp;#x26; ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We also compute two diagonals, &lt;code&gt;diag1&lt;/code&gt; and &lt;code&gt;diag2&lt;/code&gt;. &lt;code&gt;diag1&lt;/code&gt; is &lt;code&gt;file - rank&lt;/code&gt; and &lt;code&gt;diag2&lt;/code&gt; is &lt;code&gt;rank + file&lt;/code&gt;.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;        &lt;span class=&quot;hljs-comment&quot;&gt;-- ...&lt;/span&gt;
        &amp;#x26;&amp;#x26;&amp;#x26; add diag1 rank file
        &amp;#x26;&amp;#x26;&amp;#x26; add rank file diag2
        &lt;span class=&quot;hljs-comment&quot;&gt;-- &amp;#x26;&amp;#x26;&amp;#x26; ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, we ensure that our new variables are not within our list of threatened variables:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;        &lt;span class=&quot;hljs-comment&quot;&gt;-- ...&lt;/span&gt;
        &amp;#x26;&amp;#x26;&amp;#x26; negate (rank `within` threatenedRanks)
        &amp;#x26;&amp;#x26;&amp;#x26; negate (file `within` threatenedFiles)
        &amp;#x26;&amp;#x26;&amp;#x26; negate (diag1 `within` threatenedDiag1)
        &amp;#x26;&amp;#x26;&amp;#x26; negate (diag2 `within` threatenedDiag2)
        &lt;span class=&quot;hljs-comment&quot;&gt;-- &amp;#x26;&amp;#x26;&amp;#x26; ...&lt;/span&gt;
      &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
        (threatenedRanks, threatenedFiles, threatenedDiag1, threatenedDiag2) = &lt;span class=&quot;hljs-type&quot;&gt;List&lt;/span&gt;.unzip4 threatened
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And we recurse until we hit zero.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;    queens&#39; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; _ = truth
    queens&#39; m threatened = consider &#92;rank -&gt; consider &#92;file -&gt; consider &#92;diag1 -&gt; consider &#92;diag2 -&gt;
        &lt;span class=&quot;hljs-comment&quot;&gt;-- ...&lt;/span&gt;
        &amp;#x26;&amp;#x26;&amp;#x26; queens&#39; (pred m) ((rank, file, diag1, diag2) : threatened)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This ensures that &lt;code&gt;rank&lt;/code&gt; and &lt;code&gt;file&lt;/code&gt; are within bounds, and that no rank, file, or diagonal for a given queen will match (unify) those for another queen.&lt;/p&gt;
&lt;p&gt;Our &lt;code&gt;queens&lt;/code&gt; function will consider every possibility, thanks to the &lt;code&gt;within&lt;/code&gt; relation, which is really a lot of disjunctions in a trenchcoat.&lt;/p&gt;
&lt;p&gt;Because &lt;code&gt;&amp;#x26;&amp;#x26;&amp;#x26;&lt;/code&gt; is logically commutative, but in practice always runs the left-hand side first, we move the negations up so they’re earlier, as these cut off branches. By moving them earlier, they happen faster.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;queens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;hljs-title&quot;&gt;queens&lt;/span&gt; n = queens&#39; n []
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    valid = map &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; [&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; .. pred n]
    queens&#39; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; _ = truth
    queens&#39; m threatened = consider &#92;rank -&gt; consider &#92;file -&gt; consider &#92;diag1 -&gt; consider &#92;diag2 -&gt;
      (rank `within` valid)
        &amp;#x26;&amp;#x26;&amp;#x26; negate (rank `within` threatenedRanks)
        &amp;#x26;&amp;#x26;&amp;#x26; (file `within` valid)
        &amp;#x26;&amp;#x26;&amp;#x26; negate (file `within` threatenedFiles)
        &amp;#x26;&amp;#x26;&amp;#x26; add diag1 rank file
        &amp;#x26;&amp;#x26;&amp;#x26; negate (diag1 `within` threatenedDiag1)
        &amp;#x26;&amp;#x26;&amp;#x26; add rank file diag2
        &amp;#x26;&amp;#x26;&amp;#x26; negate (diag2 `within` threatenedDiag2)
        &amp;#x26;&amp;#x26;&amp;#x26; queens&#39; (pred m) ((rank, file, diag1, diag2) : threatened)
      &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
        (threatenedRanks, threatenedFiles, threatenedDiag1, threatenedDiag2) = &lt;span class=&quot;hljs-type&quot;&gt;List&lt;/span&gt;.unzip4 threatened
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Interpretation&lt;/h2&gt;
&lt;p&gt;This &lt;code&gt;queens n&lt;/code&gt; relation solves it, but we need to get the output. We define a &lt;code&gt;Position&lt;/code&gt; which takes a rank and file and renders it as a chess position:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Ord&lt;/span&gt;)
&lt;span class=&quot;hljs-class&quot;&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Show&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
  show (&lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; rank file) =
    &lt;span class=&quot;hljs-type&quot;&gt;Char&lt;/span&gt;.chr (rank + &lt;span class=&quot;hljs-type&quot;&gt;Char&lt;/span&gt;.ord &lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;) : show (file + &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then we transform knowledge into a list of positions, using the fact that we know that for each queen &lt;code&gt;m&lt;/code&gt;, the rank will be the variable &lt;code&gt;m * 4&lt;/code&gt;, and the file will be &lt;code&gt;m * 4 + 1&lt;/code&gt;. (This is a bad idea in general, but it works here.)&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;findQueens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt;]
&lt;span class=&quot;hljs-title&quot;&gt;findQueens&lt;/span&gt; _ &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; = []
&lt;span class=&quot;hljs-title&quot;&gt;findQueens&lt;/span&gt; knowledge n =
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; offset = &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; (fromIntegral (n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;) * &lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;)
      &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; rank = climb knowledge (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; offset)
      &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; file = climb knowledge (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; (succ offset))
   &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; rank file : findQueens knowledge (n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To solve it, we call &lt;code&gt;learn (queens n)&lt;/code&gt;, take the first result with &lt;code&gt;head&lt;/code&gt;, and then call &lt;code&gt;findQueens&lt;/code&gt; on the result. We sort the output because it’s prettier, but it’s not necessary.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;solveQueens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt;]
&lt;span class=&quot;hljs-title&quot;&gt;solveQueens&lt;/span&gt; n = &lt;span class=&quot;hljs-type&quot;&gt;List&lt;/span&gt;.sort (findQueens knowledge n)
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    knowledge = head $ learn (queens n)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our &lt;code&gt;main&lt;/code&gt; function takes an optional argument, parses it, and defaults to &lt;code&gt;8&lt;/code&gt; if it’s not set, then calls &lt;code&gt;solveQueens&lt;/code&gt;:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;main&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;IO&lt;/span&gt; ()
&lt;span class=&quot;hljs-title&quot;&gt;main&lt;/span&gt; = &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  args &amp;#x3C;- getArgs
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; n = maybe &lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt; read $ listToMaybe args
  print $ solveQueens n
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We compile with &lt;code&gt;ghc -O2 -o interview Interview.hs&lt;/code&gt;, and run with &lt;code&gt;./interview&lt;/code&gt;. It takes about half a second on my computer to produce:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[a1,b5,c8,d6,e3,f7,g2,h4]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And there you have it. Solving the eight queens puzzle with unification, and witchcraft.&lt;/p&gt;
&lt;hr /&gt;
&lt;ol class=&quot;series-list&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/i-relent&quot;&gt;I relent&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview&quot;&gt;Solving the technical interview&lt;/a&gt;&lt;/li&gt;
  &lt;li aria-current=&quot;page&quot;&gt;Solving the technical interview, explained&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/monadic-logic&quot;&gt;Monadic logic&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/fairness-in-disjunctions&quot;&gt;Fairness in disjunctions&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</description><pubDate>Thu, 28 May 2026 15:30:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/solving-the-technical-interview-explained</guid>
    </item>
    <item>
      <title>Solving the technical interview</title>
      <link>https://functional.computer/blog/solving-the-technical-interview</link><description>&lt;p&gt;&lt;em&gt;With apologies to Aphyr.&lt;/em&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;You heave a sigh and get up from your desk, stopping by the kitchenette for a tea and a biscuit on the way to the meeting room. Recruiting for this role has not been going well, and your manager is starting to get nervous.&lt;/p&gt;
&lt;p&gt;Nervous is bad. Nervous means more scrutiny, more questions, more micro-management. And nervous means you get nervous too. You can feel your heart beat faster as you walk over to the interview room. You can see a sillhouette through the frosted glass door; they must already be waiting for you.&lt;/p&gt;
&lt;p&gt;You take a deep breath. The last few candidates have all been a little… odd. Let’s hope this one makes life a bit easier for you. You could use a win.&lt;/p&gt;
&lt;p&gt;You stride in. “Um, hello.”&lt;/p&gt;
&lt;p&gt;“Hello, dear! It’s lovely to meet you.”&lt;/p&gt;
&lt;p&gt;You pause. Seated at the conference table is an older woman, full of smiles, unapologetically beaming at you, half a biscuit in her hand, a laptop in front of her. As she opens it, you notice that it seems to have seen better days; its keyboard is covered in cake crumbs, the trackpad has been worn smooth, the hinge creaks, and a fan roars to life.&lt;/p&gt;
&lt;p&gt;You relax a little. Perhaps this won’t be like &lt;a href=&quot;https://aphyr.com/posts/342-typing-the-technical-interview&quot;&gt;the one last month&lt;/a&gt;. Or &lt;a href=&quot;https://www.richard-towers.com/2023/03/11/typescripting-the-technical-interview.html&quot;&gt;the one last week&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;“I’m ready when you are, sonny jim.”&lt;/p&gt;
&lt;p&gt;You try not to appear taken aback by her familiarity, as you stammer out the start of the problem statement.&lt;/p&gt;
&lt;p&gt;“Are you familiar with chess?”&lt;/p&gt;
&lt;p&gt;She frowns at you. The light flickers. You meet her eyes, and are whisked away. A horse, rearing, its rider vaulting over a warrior with a pike. A man of God speaks, his voice booming, a tower crumbles. A queen stands over a king, with her dagger pointed at his neck, her face steeled even as tears form in her eyes.&lt;/p&gt;
&lt;p&gt;Was it a moment, or a lifetime?&lt;/p&gt;
&lt;p&gt;“Yes, I am. Intimately, you might say.” She offers you a wink. You’re not sure why.&lt;/p&gt;
&lt;p&gt;“Well,” you stammer, “imagine a chessboard. The problem is called &lt;code&gt;N&lt;/code&gt;-Queens. For a given chessboard of size &lt;code&gt;N&lt;/code&gt;×&lt;code&gt;N&lt;/code&gt;, you need to find a way to place &lt;code&gt;N&lt;/code&gt; queens on that board, safely. Don’t worry about actually solving—”&lt;/p&gt;
&lt;p&gt;“Safely, you say? Surely that depends on the queen?”&lt;/p&gt;
&lt;p&gt;“Well… um. In this situation, imagine they all move like chess queens, and they want to capture each other. You need to find a way to place them all so none of them can reach another.”&lt;/p&gt;
&lt;p&gt;“Yes, I’ve dealt with royalty before. Best to keep them in separate castles. But if you can’t, let’s at least be clever about the seating plan.”&lt;/p&gt;
&lt;p&gt;She starts typing:&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-meta&quot;&gt;{-# LANGUAGE GHC2024 #-}&lt;/span&gt;
&lt;span class=&quot;hljs-meta&quot;&gt;{-# LANGUAGE BlockArguments #-}&lt;/span&gt;
&lt;span class=&quot;hljs-meta&quot;&gt;{-# OPTIONS_GHC -Wno-name-shadowing #-}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“Er… what’s this?”&lt;/p&gt;
&lt;p&gt;“We want a fair game, don’t we? No arguments, no subterfuge.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;module&lt;/span&gt; Main &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;

&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Control.Monad ((&gt;=&gt;))
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.Char &lt;span class=&quot;hljs-keyword&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; Char
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.Maybe (&lt;span class=&quot;hljs-title&quot;&gt;listToMaybe&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Data.List &lt;span class=&quot;hljs-keyword&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; List
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; Prelude &lt;span class=&quot;hljs-keyword&quot;&gt;hiding&lt;/span&gt; (&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt; System.Environment (&lt;span class=&quot;hljs-title&quot;&gt;getArgs&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Suddenly, a memory, and not a positive one. “Hang on, is this Haskell? Um… I must warn you we haven’t had great experiences with all of that type-level trickery here.”&lt;/p&gt;
&lt;p&gt;“Oh, very good. No, I don’t hold to that myself, either. You know where you are with variables, I always say. Types should be kept simple. Code is for humans to read, as they say!”&lt;/p&gt;
&lt;p&gt;A weight on your chest starts to ease up. You hadn’t realised it was there.&lt;/p&gt;
&lt;p&gt;“Ah, phew. Please, carry on.”&lt;/p&gt;
&lt;p&gt;“Well, speaking of variables, let’s make some.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; &lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;newtype&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Enum&lt;/span&gt;)&lt;/span&gt;

&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a&lt;/span&gt;
  = &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; a
  | &lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You start to interrupt, but she’s typing furiously now, and the clacking of the keyboard (why does it sound like a typewriter?) drowns out your question.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; a = [(&lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;She stops, for a moment. “A tree of knowledge. And one must climb it.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;climb&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;climb&lt;/span&gt; _ term@&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; {} = term
&lt;span class=&quot;hljs-title&quot;&gt;climb&lt;/span&gt; knowledge (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) =
  maybe (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) (climb knowledge) $ lookup v knowledge
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“Climb until the branch yields fruit. No more.”&lt;/p&gt;
&lt;p&gt;“Er… how does this—”&lt;/p&gt;
&lt;p&gt;Her eyes snap upwards, meeting yours. A woman, naked, reaches toward an apple, hanging from a tree. She takes it, bites it. Her eyes widen. Oblivion.&lt;/p&gt;
&lt;p&gt;You stop talking.&lt;/p&gt;
&lt;p&gt;“One cannot simply &lt;em&gt;have&lt;/em&gt; knowledge, of course. We learn.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a = &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a]&lt;/span&gt;

&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a = &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;a&lt;/span&gt;)&lt;/span&gt;

&lt;span class=&quot;hljs-title&quot;&gt;emptyState&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;emptyState&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;) []
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“Hang on, what’s a state?”&lt;/p&gt;
&lt;p&gt;She smiles at you. “Haskell is a functional programming language. No mutation. When you learn, you are not the same person as before. The program isn’t either. To learn, it must transition, from state to state. You’re learning Haskell right now, aren’t you? And you won’t be the same person at the end of this conversation as you were at the beginning, let me tell you!”&lt;/p&gt;
&lt;p&gt;You nod, your mouth shut. It seems safer.&lt;/p&gt;
&lt;p&gt;“The first lesson is that if what we know is wrong, we know nothing.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;falsity&lt;/span&gt; = mempty
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“The second: even if we don’t learn anything new, we know what we know.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;truth&lt;/span&gt; = pure
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“And of course, some lessons are not worth learning.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;negate&lt;/span&gt; lesson state = &lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt; lesson state &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt;
  [] -&gt; pure state
  _ -&gt; mempty
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“I think I follow,” you manage to mumble. “If it teaches us something, it is false, and if it claims to be a dead end, we were right all along?”&lt;/p&gt;
&lt;p&gt;She beams at you, and takes her hands from the keyboard, leaning down to rummage around in a huge handbag by her feet. After a brief moment, she produces a metal box, and opens it up.&lt;/p&gt;
&lt;p&gt;“Very good, sonny. Would you like a piece of ginger cake? I baked it this morning.”&lt;/p&gt;
&lt;p&gt;You take the piece of cake. She waits, expectantly. You take a bite.&lt;/p&gt;
&lt;p&gt;It’s delicious.&lt;/p&gt;
&lt;p&gt;“Now, my boy, let’s talk about what it means for things to be the same.”&lt;/p&gt;
&lt;p&gt;Her fingers blur, faster than you thought possible. Cake crumbs fly through the air.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(===) :: &lt;span class=&quot;hljs-keyword&quot;&gt;forall&lt;/span&gt; a. (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; a) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(x === y) (&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; vars knowledge) =
  maybe mempty (pure . &lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; vars) $
    unifyTerm (climb knowledge x) (climb knowledge y)
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    unifyTerm (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x&#39;) (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y&#39;)
      | x&#39; == y&#39; = &lt;span class=&quot;hljs-type&quot;&gt;Just&lt;/span&gt; knowledge
      | otherwise = &lt;span class=&quot;hljs-type&quot;&gt;Nothing&lt;/span&gt;
    unifyTerm (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) y&#39; = unifyVar v y&#39;
    unifyTerm x&#39; (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; v) = unifyVar v x&#39;
    unifyVar v t
      | occurs v t = &lt;span class=&quot;hljs-type&quot;&gt;Nothing&lt;/span&gt;
      | otherwise = &lt;span class=&quot;hljs-type&quot;&gt;Just&lt;/span&gt; (pure (v, t) &amp;#x3C;&gt; knowledge)
    occurs _ (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; _) = &lt;span class=&quot;hljs-type&quot;&gt;False&lt;/span&gt;
    occurs v (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; tv) = tv == v
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;She cracks her knuckles. “My joints aren’t what they used to be, you know.&lt;/p&gt;
&lt;p&gt;“Atoms are atoms; they’re the same or they ain’t, and if they ain’t… you’ve gone wrong. But variables… if you know that a variable is a term, then you’ve learned something new.”&lt;/p&gt;
&lt;p&gt;“Unless it… occurs?”&lt;/p&gt;
&lt;p&gt;“Yes, that’s right. If you don’t check what you already know, you’ll end up going in circles. And when you start thinking in circles, you’ll never stop.”&lt;/p&gt;
&lt;p&gt;“Right, and—”&lt;/p&gt;
&lt;p&gt;She cuts you off with a raised palm.&lt;/p&gt;
&lt;p&gt;“Almost there. Now, where was I? Some lessons follow on from others, building on prior knowledge. Fortunately, Haskell gives us an operator for that.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(&amp;#x26;&amp;#x26;&amp;#x26;) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(&amp;#x26;&amp;#x26;&amp;#x26;) = (&gt;=&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“And if two things might be true, you can explore them one by one. So let’s stick them together.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;(|||) :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
(|||) = (&amp;#x3C;&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;She grins. “Remember, a lesson returns a list. So we can have multiple possible truths.”&lt;/p&gt;
&lt;p&gt;Your brow furrows. You remember why you’re here. “So about these queens…”&lt;/p&gt;
&lt;p&gt;“Hush, sonny. Almost there. Now, we introduce a variable when we want to consider something new. And we make sure never to consider that variable twice; it’s gone, and its successor takes its place.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;consider&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a) -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;consider&lt;/span&gt; f (&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; nextVar knowledge) =
  f (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; nextVar) (&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; (succ nextVar) knowledge)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“And sometimes we need to check our understanding during a lesson.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;understand&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; (&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a) -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;understand&lt;/span&gt; term f state@(&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; _ knowledge) =
  f (climb knowledge term) state
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“And of course, we would like to learn.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;learn&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; a]
&lt;span class=&quot;hljs-title&quot;&gt;learn&lt;/span&gt; lesson =
  map (&#92;(&lt;span class=&quot;hljs-type&quot;&gt;State&lt;/span&gt; _ knowledge) -&gt; knowledge) $ lesson emptyState
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;She leans back in her chair, satisfied.&lt;/p&gt;
&lt;p&gt;“You see?”&lt;/p&gt;
&lt;p&gt;“Well… not really. Where does it figure out where the queens go on a chessboard?”&lt;/p&gt;
&lt;p&gt;“Oh, I see, you want the details. Alright, let’s go. I imagine we’ll need to add some numbers.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;add&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; a, &lt;span class=&quot;hljs-type&quot;&gt;Num&lt;/span&gt; a) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;add&lt;/span&gt; x y z = understand x &#92;x -&gt; understand y &#92;y -&gt; understand z &#92;z -&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt; (x, y, z) &lt;span class=&quot;hljs-keyword&quot;&gt;of&lt;/span&gt;
    (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y, z) -&gt; z === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (x + y)
    (&lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; x, y, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; z) -&gt; y === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (z - x)
    (x, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; y, &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; z) -&gt; x === &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; (z - y)
    _ -&gt; falsity
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A plus. Alright. This is unorthodox, but at least you understand the plus symbol. But a minus?&lt;/p&gt;
&lt;p&gt;“What’s that subtraction doing there?”&lt;/p&gt;
&lt;p&gt;“Addition, subtraction, what’s the difference?” She pauses, waiting for you to acknowledge her little joke, and you smile, weakly, your eyes telling a different story. She doesn’t notice, instead cracking her knuckles and starting to type again.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; :: (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt; a) =&gt; &lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Term&lt;/span&gt; a] -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; a
&lt;span class=&quot;hljs-title&quot;&gt;within&lt;/span&gt; x = foldr ((|||) . (=== x)) falsity
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“We’re ready.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;queens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Lesson&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;
&lt;span class=&quot;hljs-title&quot;&gt;queens&lt;/span&gt; n = queens&#39; n []
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    valid = map &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; [&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; .. pred n]
    queens&#39; &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; _ = truth
    queens&#39; m threatened = consider &#92;rank -&gt; consider &#92;file -&gt; consider &#92;diag1 -&gt; consider &#92;diag2 -&gt;
      (rank `within` valid)
        &amp;#x26;&amp;#x26;&amp;#x26; negate (rank `within` threatenedRanks)
        &amp;#x26;&amp;#x26;&amp;#x26; (file `within` valid)
        &amp;#x26;&amp;#x26;&amp;#x26; negate (file `within` threatenedFiles)
        &amp;#x26;&amp;#x26;&amp;#x26; add diag1 rank file
        &amp;#x26;&amp;#x26;&amp;#x26; negate (diag1 `within` threatenedDiag1)
        &amp;#x26;&amp;#x26;&amp;#x26; add rank file diag2
        &amp;#x26;&amp;#x26;&amp;#x26; negate (diag2 `within` threatenedDiag2)
        &amp;#x26;&amp;#x26;&amp;#x26; queens&#39; (pred m) ((rank, file, diag1, diag2) : threatened)
      &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
        (threatenedRanks, threatenedFiles, threatenedDiag1, threatenedDiag2) = &lt;span class=&quot;hljs-type&quot;&gt;List&lt;/span&gt;.unzip4 threatened
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“Happy now?”&lt;/p&gt;
&lt;p&gt;You furrow your brows, trying to process it. There’s a lot of ampersands, but they seem to do the right thing.&lt;/p&gt;
&lt;p&gt;“And this… solves it, somehow?”&lt;/p&gt;
&lt;p&gt;There’s no computation, apart from a couple of additions. And one of those might actually be a subtraction, who knows?&lt;/p&gt;
&lt;p&gt;“Eat your cake, my boy. I can see you’re the sort who needs their hand held. You want to know where the queens are?”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-class&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; = &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;hljs-keyword&quot;&gt;deriving&lt;/span&gt; stock (&lt;span class=&quot;hljs-type&quot;&gt;Eq&lt;/span&gt;, &lt;span class=&quot;hljs-type&quot;&gt;Ord&lt;/span&gt;)
&lt;span class=&quot;hljs-class&quot;&gt;
&lt;span class=&quot;hljs-keyword&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Show&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;&lt;/span&gt;
  show (&lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; rank file) =
    &lt;span class=&quot;hljs-type&quot;&gt;Char&lt;/span&gt;.chr (rank + &lt;span class=&quot;hljs-type&quot;&gt;Char&lt;/span&gt;.ord &lt;span class=&quot;hljs-string&quot;&gt;&#39;a&#39;&lt;/span&gt;) : show (file + &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You relax. Finally, something normal. Haskell’s a bit weird, but this code would be relatively at home in JavaScript too.&lt;/p&gt;
&lt;p&gt;She pauses. “We’ll cheat a little. We know how our algorithm allocates variables, so let’s use that to get the rank and file.”&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;findQueens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Knowledge&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt;]
&lt;span class=&quot;hljs-title&quot;&gt;findQueens&lt;/span&gt; _ &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; = []
&lt;span class=&quot;hljs-title&quot;&gt;findQueens&lt;/span&gt; knowledge n =
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; offset = &lt;span class=&quot;hljs-type&quot;&gt;Var&lt;/span&gt; (fromIntegral (n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;) * &lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;)
      &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; rank = climb knowledge (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; offset)
      &lt;span class=&quot;hljs-type&quot;&gt;TAtom&lt;/span&gt; file = climb knowledge (&lt;span class=&quot;hljs-type&quot;&gt;TVar&lt;/span&gt; (succ offset))
   &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt; rank file : findQueens knowledge (n - &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“And then, we solve. Is just one answer alright?” She doesn’t wait for you to respond.&lt;/p&gt;
&lt;pre data-lang=&quot;haskell&quot;&gt;&lt;code class=&quot;hljs language-haskell&quot;&gt;&lt;span class=&quot;hljs-title&quot;&gt;solveQueens&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;Int&lt;/span&gt; -&gt; [&lt;span class=&quot;hljs-type&quot;&gt;Position&lt;/span&gt;]
&lt;span class=&quot;hljs-title&quot;&gt;solveQueens&lt;/span&gt; n = &lt;span class=&quot;hljs-type&quot;&gt;List&lt;/span&gt;.sort (findQueens knowledge n)
  &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;
    knowledge = head $ learn (queens n)

&lt;span class=&quot;hljs-title&quot;&gt;main&lt;/span&gt; :: &lt;span class=&quot;hljs-type&quot;&gt;IO&lt;/span&gt; ()
&lt;span class=&quot;hljs-title&quot;&gt;main&lt;/span&gt; = &lt;span class=&quot;hljs-keyword&quot;&gt;do&lt;/span&gt;
  args &amp;#x3C;- getArgs
  &lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; n = maybe &lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt; read $ listToMaybe args
  print $ solveQueens n
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“Happy now, young lad?”&lt;/p&gt;
&lt;p&gt;You realise that you never introduced yourself, she never asked your name. It doesn’t matter any more. You squeeze your eyes shut and take a breath, then open them.&lt;/p&gt;
&lt;p&gt;“Can we run it?” you ask, tentatively.&lt;/p&gt;
&lt;p&gt;She taps &lt;code&gt;ghc -O2 -o interview Interview.hs &amp;#x26;&amp;#x26; ./interview&lt;/code&gt; into a terminal. It chugs for a second, the laptop fans groaning, and it spits out:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[a1,b5,c8,d6,e3,f7,g2,h4]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;“Does that look right to you, my boy?”&lt;/p&gt;
&lt;p&gt;You nod, your heart beating faster and faster. You don’t remember what you said, but you must have wrapped it up, because the next thing you know, you are hovering over your desk, your still-full mug of tea in your hand, wondering what you did to deserve this.&lt;/p&gt;
&lt;p&gt;You glance down. There’s an entire slice of cake waiting for you on your desk.&lt;/p&gt;
&lt;p&gt;You sigh, sit down, and break off a piece.&lt;/p&gt;
&lt;hr /&gt;
&lt;ol class=&quot;series-list&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/i-relent&quot;&gt;I relent&lt;/a&gt;&lt;/li&gt;
  &lt;li aria-current=&quot;page&quot;&gt;Solving the technical interview&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview-explained&quot;&gt;Solving the technical interview, explained&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/monadic-logic&quot;&gt;Monadic logic&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/fairness-in-disjunctions&quot;&gt;Fairness in disjunctions&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</description><pubDate>Sat, 23 May 2026 07:00:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/solving-the-technical-interview</guid>
    </item>
    <item>
      <title>I relent</title>
      <link>https://functional.computer/blog/i-relent</link><description>&lt;p&gt;Every year or so, I start writing a programming language.&lt;/p&gt;
&lt;p&gt;I never make it very far before giving up, or just getting bored. But I keep coming back to the underlying problems, trying again and again.&lt;/p&gt;
&lt;p&gt;Last year, I wrote &lt;a href=&quot;https://codeberg.org/ooble/tremuloides&quot;&gt;a little database&lt;/a&gt; which is missing a programming language. So I figured I should give it another go. But I want this database to be able to compute backwards as well as forwards, so a functional language wasn’t enough. I need a logic language.&lt;/p&gt;
&lt;p&gt;And I started to write one, called &lt;a href=&quot;https://codeberg.org/ooble/bonkers&quot;&gt;Bonkers&lt;/a&gt;, based on &lt;a href=&quot;https://functional.computer/assets/documents/microKanren.pdf&quot;&gt;µKanren&lt;/a&gt;. It goes forwards, and it often goes backwards. Don’t worry about it too much. But eventually, I put it down. I didn’t stop thinking about it, though. Lots of ideas have been swimming around in my head for months.&lt;/p&gt;
&lt;p&gt;
  This year, I got the bug again. I started writing a little logic programming language based on Haskell’s &lt;a href=&quot;https://hackage.haskell.org/package/logict&quot;&gt;&lt;code&gt;LogicT&lt;/code&gt;&lt;/a
  &gt;. And then I thought about writing a &lt;code&gt;unify&lt;/code&gt; function, realized I’d probably already written it. So I searched my computer and found out that not only had I implemented that function, but a lot of other stuff I didn’t want to recreate.
&lt;/p&gt;
&lt;p&gt;I threw out the new codebase and instead refactored the old one to use &lt;code&gt;LogicT&lt;/code&gt;. (It seems a little slower than my own µKanren implementation, but also more flexible and correct.)&lt;/p&gt;
&lt;p&gt;I seem to keep coming back to the same idea. So I am writing it down. I, Samir, am going to take this programming language seriously, and try and make it useful. I’ll make a logic programming language that can be embedded in something else (through linking, calling out to another binary, WebAssembly, whatever).&lt;/p&gt;
&lt;p&gt;And I think I’ve got a few interesting features planned that I haven’t seen in other programming languages. I plan on writing about them, either as I implement them, or as a precursor to doing so.&lt;/p&gt;
&lt;p&gt;I’m excited about the idea that I have something to work on again. I hope you’ll find it interesting too.&lt;/p&gt;
&lt;hr /&gt;
&lt;ol class=&quot;series-list&quot;&gt;
  &lt;li aria-current=&quot;page&quot;&gt;I relent&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview&quot;&gt;Solving the technical interview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/solving-the-technical-interview-explained&quot;&gt;Solving the technical interview, explained&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/monadic-logic&quot;&gt;Monadic logic&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/fairness-in-disjunctions&quot;&gt;Fairness in disjunctions&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</description><pubDate>Tue, 21 Apr 2026 07:00:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/i-relent</guid>
    </item>
    <item>
      <title>Cleave clients from servers</title>
      <link>https://functional.computer/blog/cleave-clients-from-servers</link><description>&lt;p&gt;I was once in an interview for a Silicon Valley job. The interviewer, an American, asked me how I found out about the job. I told them that a recruiter contacted me. They replied, “London is a city of middlemen.”&lt;/p&gt;
&lt;p&gt;
  It’s true, London is full of people who will sell you their contact book. It’s their asset: who to talk to, and who not to bother with. But compared to Silicon Valley, we&lt;sup&gt;&lt;a href=&quot;https://functional.computer/blog/cleave-clients-from-servers#fn-london&quot; id=&quot;fnref-london&quot; data-footnote-ref=&quot;&quot; aria-describedby=&quot;footnote-label&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; are but tiny little babies.
&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;I watch shows via Netflix, sometimes.&lt;/p&gt;
&lt;p&gt;I like the shows. I fucking hate the Netflix app. I don’t like that it hides the controls behind random taps. I despise the constant pushing of new stuff.&lt;/p&gt;
&lt;p&gt;But if I want to watch a show on Netflix on my phone, I have no choice. I must use the app. I may not use something which lets me, I don’t know, put the playback controls on the side of the screen.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;I use Patreon a lot. I love that I can pay my favourite creators to get extras, like a comic strip a day early, or a show I like without adverts.&lt;/p&gt;
&lt;p&gt;I fucking hate the Patreon app.&lt;/p&gt;
&lt;p&gt;But I also hate newsletters via email, so I begrudgingly use it.&lt;/p&gt;
&lt;p&gt;What I want is for my Patreon subscriptions to show up in my feed reader, NetNewsWire. But it’s locked down; Patreon does not and never will support RSS or Atom, or even a workable API.&lt;/p&gt;
&lt;p&gt;There’s one exception: podcasts are delivered over RSS. They &lt;strong&gt;have to&lt;/strong&gt; be: if it’s not RSS, it’s not a podcast. (A “Spotify exclusive” is not a podcast.) So they resort to dark patterns: the podcast shows up in the app as soon as it’s published, but only in the feed an hour or two later.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;I like Apple Fitness. It helps me get off my bum.&lt;/p&gt;
&lt;p&gt;I even like the app.&lt;/p&gt;
&lt;p&gt;I do not like the Apple Watch. But I just replaced my 5-year-old one, because the battery was dying, and it’s the one thing that makes me get off my bum.&lt;/p&gt;
&lt;p&gt;I’m beholden to the hardware. The software won’t work with another heart rate tracker. I need &lt;em&gt;this&lt;/em&gt; one.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;We shall not talk about Facebook.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;And I’m lucky, because I don’t have to rely on a medical device with locked-down firmware, or a wheelchair that requires a paid subscription to drive. (And even though it was the future of juicing, I neglected to buy a Juicero.)&lt;/p&gt;
&lt;p&gt;Everywhere I look, there is a “software” company, typically based in northern California, offering me something good with one hand, ready to handcuff me with the other, as soon as I reach forward.&lt;/p&gt;
&lt;p&gt;They insert themselves in the middle, and leech until both sides are dry. They don’t sell software, they sell access, just like the recruiters in London, but on a scale so different, it’s barely recognisable.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Let’s get some fucking APIs instead. APIs that separate the client from the server, so that I can watch, or listen, or read, or &lt;strong&gt;live&lt;/strong&gt; the way I choose.&lt;/p&gt;
&lt;p&gt;And I know, you need to break the APIs to innovate. You can’t have an API for streaming music or video before you have streaming music or video. There’ll always be this tension. But we’ve swung too far the other way. Commodities that won’t commoditise because they lock in the consumption, force you to do it on their terms.&lt;/p&gt;
&lt;p&gt;We at least need regulation to say that APIs must be available, and you can’t block clients with your Terms of Service. Any competition authority worth its paperwork needs to identify companies in dominant positions and &lt;strong&gt;force&lt;/strong&gt; them to a higher standard, to provide fair access to any consumer.&lt;/p&gt;
&lt;p&gt;With APIs come de facto standards. A market leader emerges, and others copy their API. One client, multiple services. That’s the beginning of federation.&lt;/p&gt;
&lt;p&gt;Cleave clients from servers. Bring back user agents.&lt;/p&gt;
&lt;p&gt;That’s the promise of the web, and it’s a broken promise.&lt;/p&gt;
&lt;section data-footnotes=&quot;&quot; class=&quot;footnotes&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;fn-london&quot;&gt;
      &lt;p&gt;I may have left London, but I am still a Londoner. Fight me. &lt;a href=&quot;https://functional.computer/blog/cleave-clients-from-servers#fnref-london&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;/ol&gt;
&lt;/section&gt;
</description><pubDate>Thu, 12 Feb 2026 08:00:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/cleave-clients-from-servers</guid>
    </item>
    <item>
      <title>So I&#39;m that age, apparently</title>
      <link>https://functional.computer/blog/thirty-seven</link><description>&lt;p&gt;I’m 37.&lt;/p&gt;
&lt;p&gt;Apparently I’m now at that age where I feel the need to make sweeping statements about life, the universe, and everything.&lt;/p&gt;
&lt;p&gt;So here’s my wisdom, collected over the last year or so. Perhaps I’ll do this every year.&lt;/p&gt;
&lt;h3&gt;Life&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;The three most dangerous words in the English language are “they”, “should”, and “is”.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/talking-in-the-second-person&quot;&gt;When someone talks about “you”, they are usually talking about themselves in the second person.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Reality may have a left-wing bias, but perception has a right-wing bias.&lt;/li&gt;
  &lt;li&gt;Anyone who tells you about a perfect system for life has someone else (a wife, a mother, etc.) taking care of all the messy bits, while they remain oblivious. Often intentionally.&lt;/li&gt;
  &lt;li&gt;“I’m tired and I don’t know” is a valid response.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Learning&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Those who don’t ask stupid questions give stupid answers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Work&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/lessons-from-a-failed-startup--do-less-and-do-it-better&quot;&gt;Do less, and do it better.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;It’s harder and more important to finish things than to start things.&lt;/li&gt;
  &lt;li&gt;But it’s more fun to start things.&lt;/li&gt;
  &lt;li&gt;If in doubt, organise, read, procrastinate. &lt;a href=&quot;https://functional.computer/blog/im-not-fucking-about&quot;&gt;It’s not fucking about, it’s internalising.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/responsibility-and-authority&quot;&gt;Responsibility only works if it’s paired with authority.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Sales and marketing are often harder, more important, and less understood than technology.&lt;/li&gt;
  &lt;li&gt;The people are more important than the product. The product is more important than the technology. (But they’re still important.)&lt;/li&gt;
  &lt;li&gt;Technology is the most expensive solution to a problem, and the hardest to change.&lt;/li&gt;
  &lt;li&gt;The only 10x developer is the developer that makes the 10 people around them twice as good.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Software&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;The watchmakers were correct. They’re “complications”, not “features”.&lt;/li&gt;
  &lt;li&gt;Legacy code is tough. Legacy data is impossible.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://functional.computer/blog/the-cardinal-sin-of-software-architecture&quot;&gt;The worst kind of accidental complexity in software&lt;/a&gt; is the unnecessary distribution, replication, or restructuring of state, both in space and time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Here’s to another year&lt;/h2&gt;
&lt;p&gt;I made it around the sun once more. I hope to continue doing so.&lt;/p&gt;
&lt;p&gt;If you want to talk about any of the above, ask me to elaborate, or challenge me on something, you know where to find me.&lt;/p&gt;
</description><pubDate>Sat, 10 Jan 2026 08:00:00 +0000</pubDate>
      <dc:creator>Samir Talwar</dc:creator>
      <guid>https://functional.computer/blog/thirty-seven</guid>
    </item>
  </channel>
</rss>