
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title>Inner Product</title>
	<subtitle>Thoughts on software development from Inner Product.</subtitle>
	<link href="https://inner-product.com/feed/feed.xml" rel="self"/>
	<link href="https://inner-product.com/"/>
	<updated>2024-04-24T00:00:00Z</updated>
	<id>https://inner-product.com/</id>
	<author>
		<name>Your Name Here</name>
		<email>youremailaddress@example.com</email>
	</author>
	
	<entry>
		<title>How to Write an Abstract</title>
		<link href="https://inner-product.com/posts/how-to-write-an-abstract/"/>
		<updated>2018-04-19T00:00:00Z</updated>
		<id>https://inner-product.com/posts/how-to-write-an-abstract/</id>
		<content type="html">&lt;p&gt;How do you write an abstract for a (industry) conference talk proposal? I like to use a three paragraph structure. The first paragraph gives a quick overview of what attendees can expect to learn if they attend the talk. The second paragraph gives motivation and background—why should attendees care about what I have to say? The third and final paragraph goes into more detail on the points I intend to cover.&lt;/p&gt;
&lt;p&gt;An abstract is a sales pitch to conference organizers and attendees. Conference organizers need to be convinced to accept the talk, and conference attendees need a reason to attend the talk. Luckily these two groups are looking for the same thing: evidence I have something interesting and coherent to say. Both are also short of time. I hope the organisers spend a bit more time on the abstract than can reasonably be expected of attendees but the quicker I can sell either group the better.&lt;/p&gt;
&lt;p&gt;This explains why I want the first paragraph to overview my talk—so attendees who only read that far can decide if they want to attend. The organisers want a bit more detail, so the job in the remaining paragraphs is to convince them that the problem I’m addressing is important (and hence will get an audience) and that I have something to say about it. That’s all I need in an abstract, so three paragraphs should suffice.&lt;/p&gt;
&lt;p&gt;If you have read this far you might want an example of the three paragraph structure. If so, you might wish to look over the previous three paragraphs…&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Solving the Rubik&#39;s Cube with Group Theory and Functional Programming</title>
		<link href="https://inner-product.com/posts/rubiks-intro/"/>
		<updated>2019-05-06T17:00:00Z</updated>
		<id>https://inner-product.com/posts/rubiks-intro/</id>
		<content type="html">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In this series of blog posts, we’ll develop a Rubik’s cube solution from first principles, using group theory and functional programming in Scala. There are already many existing solutions; optimal solutions can be computed in a fraction of a second, and many tutorials are available online that allow humans to solve a Rubik’s cube within 20 seconds. Our goal here will be to develop a solution we can really &lt;em&gt;understand&lt;/em&gt;; a solution that’s both simple to express as a program and easy to teach to a human.&lt;/p&gt;
&lt;p&gt;That gives us two constraints:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We’ll avoid directly translating speedcubing methods. On one hand, human solutions rely on cube algorithms that have been computed ahead of time and memorized. Simply porting one of these solutions doesn’t really count as “understanding” the solution. On the other hand, human solutions also rely on intuition and different types of moves that are context-dependent. This is very messy to implement as a program.&lt;/li&gt;
&lt;li&gt;We won’t search for an optimal solution, or even one with a low move-count. Optimal solutions require deeper mathematical background and large tables of algorithms. Even if a human can be said to understand such a solution, they wouldn’t be able to calculate and execute it.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Instead, we’ll learn about the minimal building blocks we need to invent Rubik’s cube algorithms ourselves. These will be simple and general enough so that we should be able to apply them to any similar mechanical combination puzzle (or “twisty puzzle”).&lt;/p&gt;
&lt;h2 id=&quot;overview-of-topics-covered&quot;&gt;Overview of topics covered&lt;/h2&gt;
&lt;p&gt;Our solution will borrow ideas from math and functional programming. The math will help us model our problem and understand our solution. The functional programming concepts will help us express our solution in elegant terms and translate the math into our problem domain. We’ll also borrow &lt;a href=&quot;https://www.speedsolving.com/wiki/index.php/Main_Page&quot;&gt;ideas and vernacular&lt;/a&gt; from twisty puzzle enthusiasts, modifying the math idioms to be more cube-friendly.&lt;/p&gt;
&lt;h4 id=&quot;permutations&quot;&gt;Permutations&lt;/h4&gt;
&lt;p&gt;Every Rubik’s Cube scramble is essentially just a &lt;em&gt;permutation&lt;/em&gt; of its stickers and pieces. The Rubik’s Cube is an example of what’s called a “Permutation Puzzle”. You have a small set of scrambling moves (the six face turns), which you need to combine in order to unscramble the greater puzzle.&lt;/p&gt;
&lt;p&gt;We’ll learn about permutations, and how they can be be decomposed into smaller permutations and disjoint cycles. We can decompose larger permutations (like scrambles) into smaller cycles and transpositions. We can systematically break puzzles down into smaller problems that are more manageable.&lt;/p&gt;
&lt;h4 id=&quot;group-theory&quot;&gt;Group Theory&lt;/h4&gt;
&lt;p&gt;As it happens, the scrambles of a Rubik’s Cube have an underlying mathematical structure called a &lt;a href=&quot;https://en.wikipedia.org/wiki/Group_(mathematics)&quot;&gt;“group”&lt;/a&gt; (The “Rubik’s Group”). This is useful because mathematicians have already been studying groups for centuries. Groups have structure; they’re composed of simpler subgroups that interact with each other. These simpler groups and their relationships are well understood by mathematicians, and we can use existing theorems and techniques to inform how we model both the Rubik’s Cube and its solution.&lt;/p&gt;
&lt;p&gt;Additionally, every mathematical group is analogous to a group of permutations, so any problem involving groups structure can be expressed in terms of permutations. Permutations are computationally simple to work with, and we can re-use everything we’ve learned about permutations to work with groups in the abstract.&lt;/p&gt;
&lt;p&gt;Along the way, we’ll learn about subgroups, direct products, group actions, and homomorphism. This isn’t technically required to understand the solution, but it still serves as great example of how math can reduce the amount of work to be done by puzzle solvers and programmers alike. This section is for the nerd inside all of us that wants to really grok how things work.&lt;/p&gt;
&lt;h4 id=&quot;functional-programming&quot;&gt;Functional Programming&lt;/h4&gt;
&lt;p&gt;We’ll tie this all together with functional programming: permutations are a special type of function called a bijection, which means they always have an inverse. We’ll see how these functions can be composed with their inverses in specific ways that allow us to create custom permutations with desirable effects. This will be important, because once we break down a scramble into smaller cycles and transpositions, we’ll need to be able to invent algorithms that solve those smaller problems.&lt;/p&gt;
&lt;p&gt;We’ll also learn about how groups relate to existing FP typeclasses that we might already be familiar with (Semigroups and Monoids). This is important because it’ll allow us to leverage techniques and libraries already used and taught by the FP community. In short groups allow us to leverage existing ideas from both Math and Functional Programming to simplify our approach to crafting a solution.&lt;/p&gt;
&lt;h2 id=&quot;list-of-articles%3A&quot;&gt;List of Articles:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://inner-product.com/posts/permutation-functions&quot;&gt;Part 1: Permutations as Functions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://inner-product.com/posts/define-group&quot;&gt;Part 2: The Group Typeclass&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Part 3: Cycles, Swaps, and Permutations (upcoming)&lt;/li&gt;
&lt;li&gt;Part 4: Cube Algorithm DSL as a Group (upcoming)&lt;/li&gt;
&lt;li&gt;Part 5: Combinations, Conjugates, and Commutators (upcoming)&lt;/li&gt;
&lt;li&gt;Part 6: Modeling Rubik’s Cube State in Scala (upcoming)&lt;/li&gt;
&lt;li&gt;Part 7: Putting it all together (upcoming)&lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<title>Permutations as Functions</title>
		<link href="https://inner-product.com/posts/permutation-functions/"/>
		<updated>2019-05-06T17:00:01Z</updated>
		<id>https://inner-product.com/posts/permutation-functions/</id>
		<content type="html">&lt;p&gt;This is the first part of a &lt;a href=&quot;https://inner-product.com/posts/rubiks-intro&quot;&gt;series&lt;/a&gt; of posts detailing the theory behind Rubik’s cube solutions and tying it in to functional programming concepts. Permutations will be a major building block of our solution. In this post, we’ll show how they can be composed, inverted, and treated like functions.&lt;/p&gt;
&lt;h2 id=&quot;scrambles-of-a-string&quot;&gt;Scrambles of a String&lt;/h2&gt;
&lt;p&gt;You might have learned to think of permutations as rearrangements of elements in a collection. In Scala, every &lt;code&gt;SeqLike&lt;/code&gt; has a &lt;a href=&quot;https://www.scala-lang.org/api/current/scala/collection/SeqLike.html#permutations:Iterator%5BRepr%5D&quot;&gt;&lt;code&gt;.permutations&lt;/code&gt;&lt;/a&gt; method that will return an iterator over every permutation of its elements. Consider this example from the REPL:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;ABC&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;permutations&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toList&lt;br /&gt;res0&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ABC&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ACB&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; BAC&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; BCA&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; CAB&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; CBA&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We get a list of six strings, all possible permutations of “ABC”. What might not be immediately obvious is that each of these permutations can be identified with a function of type &lt;code&gt;Char =&amp;gt; Char&lt;/code&gt; that goes from characters in the string &lt;code&gt;&amp;quot;ABC&amp;quot;&lt;/code&gt; back to the same string. For example, consider the fourth element, &lt;code&gt;&amp;quot;BCA&amp;quot;&lt;/code&gt;. It permutes &lt;code&gt;&amp;quot;ABC&amp;quot;&lt;/code&gt; by sending &lt;code&gt;&#39;A&#39;&lt;/code&gt; to &lt;code&gt;&#39;B&#39;&lt;/code&gt;, &lt;code&gt;&#39;B&#39;&lt;/code&gt; to &lt;code&gt;&#39;C&#39;&lt;/code&gt;, and &lt;code&gt;&#39;C&#39;&lt;/code&gt; to &lt;code&gt;&#39;A&#39;&lt;/code&gt;. We can encode this directly as a partial function in scala:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; bca&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Char&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Char&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;A&#39;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;B&#39;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;B&#39;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;C&#39;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;C&#39;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;A&#39;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This can permute the characters of our original string via &lt;code&gt;.map&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ABC&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &quot;BCA&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So in a sense &lt;code&gt;&amp;quot;BCA&amp;quot;&lt;/code&gt; is not just a permutation of &lt;code&gt;&amp;quot;ABC&amp;quot;&lt;/code&gt;; it represents the function that permutes &lt;code&gt;&amp;quot;ABC&amp;quot;&lt;/code&gt; into &lt;code&gt;&amp;quot;BCA&amp;quot;&lt;/code&gt;. This function permutes other strings too:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&quot;BCA&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &quot;CAB&quot;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token string&quot;&gt;&quot;CAB&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &quot;ABC&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Even more generally, &lt;code&gt;bca&lt;/code&gt; can be thought of as a function permuting any set of three elements. It sends the first to the second, the second to the third, and the third back to the first.&lt;/p&gt;
&lt;h2 id=&quot;bijections-from-a-set-to-itself&quot;&gt;Bijections from a Set to Itself&lt;/h2&gt;
&lt;p&gt;So, we can use functions of type &lt;code&gt;A =&amp;gt; A&lt;/code&gt; to model permutations. However, not every function of this type is a valid permutation. Notice how every letter in a string is still present after it’s scrambled. For our function to preserve this property, it needs to pair off all the elements of its domain and range in an exactly one-to-one fashion.&lt;/p&gt;
&lt;p&gt;Such functions are called &lt;em&gt;bijective&lt;/em&gt; functions, or &lt;em&gt;bijections&lt;/em&gt;. It’s “bi” in the sense of “bidirectional”; we can invert our function to send elements from the range back to the domain. Permutations are a special case of bijections where the parameter and result types are the same, so inverting a permutation gives you a function of the same type.&lt;/p&gt;
&lt;p&gt;We can construct the inverse of a permutation, but it will be much easier to do so with a &lt;code&gt;Map&lt;/code&gt; implementation, versus representing a permutation as a &lt;code&gt;A =&amp;gt; A&lt;/code&gt; function.&lt;/p&gt;
&lt;h2 id=&quot;implementing-permutations-with-map%5Ba%2C-a%5D&quot;&gt;Implementing Permutations with &lt;code&gt;Map[A, A]&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Our initial encoding via a &lt;code&gt;(Char =&amp;gt; Char)&lt;/code&gt; function was a bit opaque. We’ll want want to print, compare, and transform our permutations, so to enable this, we’ll model our original six permutations using &lt;code&gt;Map&lt;/code&gt;. For simplicity, let’s use a type inhabited by only three values:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Point &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Product &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; Serializable&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; A &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Point&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; B &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Point&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; C &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Point&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; allPoints &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Set&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A permutation is then a &lt;code&gt;Map[Point, Point]&lt;/code&gt;. Here’s a lightweight implementation using a type alias and some constructors:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; Perm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Point&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Perm &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pairs&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Point&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    require&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pairs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toSet &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; allPoints&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Domain must be &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;allPoints&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    require&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pairs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toSet &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; allPoints&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Range must be &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;allPoints&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;.&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    Map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pairs&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; _&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Point &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Point&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;perm&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; perm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;swap&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our first constructor constrains our values to valid permutations as discussed earlier. Our second constructor allows us to lift regular functions into our &lt;code&gt;Perm&lt;/code&gt; type. Inverting a permutation is done simply by swapping all the tuples in its map.&lt;/p&gt;
&lt;p&gt;With this, we can re-implement our original &lt;code&gt;bca&lt;/code&gt; example:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; bca &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;  A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Scala, &lt;code&gt;Map&lt;/code&gt; &lt;a href=&quot;https://www.scala-lang.org/api/current/scala/collection/Map.html#apply(key:K):V&quot;&gt;inherits from&lt;/a&gt; &lt;code&gt;Function1&lt;/code&gt;, so we can still pass this as a function in &lt;code&gt;.map&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res0&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or compose it with itself to perform successive permutations:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca compose bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res1&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca andThen bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res2&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But unlike a regular function, computing its inverse is trivial:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res3&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As seen in the first section, there are only six permutations of three elements. Below we use our model to implement all six of them:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; abc &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;  A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; acb &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;  A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; bac &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;  A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; bca &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;  A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cab &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;  A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cba &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;  A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; allPerms &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Set&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;abc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; acb&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bac&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bca&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cab&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cba&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can compose and sequence these permutations like we can functions. Notice that since we’ve instantiated all possible permutations, the resulting permutation will be equal to one we’ve already computed:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;acb compose bac&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cab&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; bcaTwice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca andThen bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;bcaTwice&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bcaTwice &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cab&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cabThrice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cab andThen cab andThen cab&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;cabThrice&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cabThrice &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; abc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The same is true when inverting functions. For example, when we invert &lt;code&gt;bca&lt;/code&gt;, we get &lt;code&gt;bac&lt;/code&gt;, and vice versa:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cab&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cab&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There two are inverses of each other. This makes sense if you look at their effect on a list. &lt;code&gt;bca&lt;/code&gt; cycles the elements to the left, while &lt;code&gt;cab&lt;/code&gt; cycles the elements to the right:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res4&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cab&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res5&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;C&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Another way to say this is that composing &lt;code&gt;bca&lt;/code&gt; with &lt;code&gt;cab&lt;/code&gt; results in the identity permutation, which sends every point back to itself. In our example, that &lt;code&gt;abc&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;abc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res6&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Point&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Same list&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca compose cab&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; abc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bca andThen cab&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; abc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A permutation can also be its own inverse, as in these examples:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;acb&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; acb&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bac&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; bac&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cba&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; cba&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each of these permutations swaps two elements, so it makes sense that swapping the elements twice results in no action. Lastly, the identity permutation is always its own inverse:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;abc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; abc&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our original six permutations have gone from scrambled strings to scrambling functions, which can compare, compose, and invert. Now that’s functional programming! In the &lt;a href=&quot;https://inner-product.com/posts/define-group&quot;&gt;next post&lt;/a&gt;, we’ll see that permutations are essential to an algebraic structure called a Group.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The code used in this post is available in a scastie worksheet:&lt;/em&gt;&lt;br /&gt;
&lt;a href=&quot;https://scastie.scala-lang.org/stewSquared/nNx923P0QyOiBaebwLZyJg&quot;&gt;https://scastie.scala-lang.org/stewSquared/nNx923P0QyOiBaebwLZyJg&lt;/a&gt;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>The Group Typeclass</title>
		<link href="https://inner-product.com/posts/define-group/"/>
		<updated>2019-05-06T17:00:02Z</updated>
		<id>https://inner-product.com/posts/define-group/</id>
		<content type="html">&lt;p&gt;In the &lt;a href=&quot;https://inner-product.com/posts/permutation-functions&quot;&gt;previous post&lt;/a&gt; of this &lt;a href=&quot;https://inner-product.com/posts/rubiks-intro&quot;&gt;series&lt;/a&gt;, we showed that the permutations of a set are functions that can be inverted or composed to give rise to different permutations of the same set. Given these operations, sets of permutations form an algebraic structure called a group.&lt;/p&gt;
&lt;p&gt;In this post, we’ll define what a group is, explore its structure, and discuss some examples of groups. We’ll use the &lt;code&gt;Group&lt;/code&gt; typeclass and show it’s relationship to other typeclasses. We’ll also show how permutations have a group structure, and that any other group can be described by a permutation group.&lt;/p&gt;
&lt;h2 id=&quot;an-intuition-for-monoids-and-groups&quot;&gt;An Intuition for Monoids and Groups&lt;/h2&gt;
&lt;p&gt;You might be familiar with the &lt;a href=&quot;https://typelevel.org/cats/typeclasses/semigroup.html&quot;&gt;&lt;code&gt;Semigroup&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;https://typelevel.org/cats/typeclasses/monoid.html&quot;&gt;&lt;code&gt;Monoid&lt;/code&gt;&lt;/a&gt; typeclasses. Even if you’re not familiar with them by these names, you’re probably very familiar with the concepts they abstract. In a broad sense monoids and semigroups allow us to combine two elements of some type to get another element of the same type. You can add numbers, concatenate strings, merge dictionaries, or compose functions. We’ll cover some examples and more formal definition in the next section.&lt;/p&gt;
&lt;p&gt;These typeclasses are extremely useful; libraries like Spire and Algebird use them extensively for numerical and algebraic computation. But sometimes, they’re too general. A more specific abstraction can give us a greater ability to reason about the sets of elements that these combining operations act on. Groups are one such abstraction.&lt;/p&gt;
&lt;p&gt;Groups extend monoids to allow decomposing elements. If you’ve used used operations like subtraction, division, factoring, inverting, or undo, then you’ve already worked with groups whether you realize it or not. Consider how heavily you use cancellation to solve or simplify algebraic equations. This is the kind of reasoning power that groups enable. The &lt;a href=&quot;https://typelevel.org/cats/api/cats/kernel/Group.html&quot;&gt;&lt;code&gt;Group&lt;/code&gt;&lt;/a&gt; typeclass abstracts this ability, formalizing what it means to cancel out elements from a combination.&lt;/p&gt;
&lt;p&gt;Groups are less general than monoids, since not all monoids have a group structure with an operation that can be reversed. But while we lose general applicability of our typeclass, we gain an increased ability to reason about our elements.&lt;/p&gt;
&lt;h2 id=&quot;semigroups-and-monoids&quot;&gt;Semigroups and Monoids&lt;/h2&gt;
&lt;h4 id=&quot;semigroup&quot;&gt;Semigroup&lt;/h4&gt;
&lt;p&gt;A semigroup is a set with an associative binary operation. It lets you combine any two elements to get another element of the same type. It’s extremely general; instances are already defined out of the box for many different types. Here are some examples using Cats:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implicits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;Semigroup&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res0: Int = 3&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Semigroup&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Group&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Theory&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res1: String = &quot;GroupTheory&quot;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Semigroup&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token char&quot;&gt;&#39;A&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token char&quot;&gt;&#39;A&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;B&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res2: Map[Char, Int] = Map(&#39;A&#39; -&gt; 15, &#39;B&#39; -&gt; 5)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cats provides a simplified syntax that we’ll use from here on out:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res4: Int = 6&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Group&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Theory&quot;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res5: String = &quot;GroupTheory&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note the fact that the combining operation must be associative, which means the following equality must hold for any &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;z&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; z &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; z&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The operation is also not necessarily commutative; &lt;code&gt;x |+| y&lt;/code&gt; is not necessarily equal to &lt;code&gt;y |+| x&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// true for `Int`&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Group&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Theory&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Theory&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Group&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// but not for `String`&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4 id=&quot;monoid&quot;&gt;Monoid&lt;/h4&gt;
&lt;p&gt;Monoid extends semigroup with the concept of an identity element. Combining an element with the identity results in that same element, (eg., adding zero). This is true whether combining from the left or right:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;assert&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Monoid instances from Cats give us the identity element of a set via &lt;code&gt;.empty&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res6: Int = 0&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res7: String = &quot;&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the context of collections, this allows reducing an arbitrary list down to a single element to give a very general definition of a sum:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Monoid[Int].combineAll(1, 2, 3, 4)
// res8: Int = 10
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Addition isn’t the only way to define a monoid for integers. We could provide our own definition that uses multiplication, where the identity is &lt;code&gt;1&lt;/code&gt; instead of &lt;code&gt;0&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;IntProductMonoid extends Monoid[Int] {
  def empty = 1
  def combine(x: Int, y: Int) = x * y
}

IntProductMonoid.combine(3, 5)
// res9: Int = 12
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Note: both the terms “multiplication” and “addition” are used to describe the combining operator of a group, with the latter being reserved for commutative operations. We can disambiguate with “integer multiplication” and “integer addition”, but in this post, we’ll follow Cats and use the the more neutral term, “combination”, denoted &lt;code&gt;|+|&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;(Note that without the identity element, we wouldn’t be able to reduce an empty list. eg., in folds, we would have to provide a default case.)&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://typelevel.org/cats/typeclasses/semigroup.html&quot;&gt;Semigroups&lt;/a&gt; and &lt;a href=&quot;https://typelevel.org/cats/typeclasses/monoid.html&quot;&gt;Monoids&lt;/a&gt; are both already well-documented in Cats’ page on &lt;a href=&quot;https://typelevel.org/cats/typeclasses&quot;&gt;Type-Classes&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;group%3A-monoid-with-inverse&quot;&gt;Group: Monoid with Inverse&lt;/h2&gt;
&lt;p&gt;As monoids extend semigroups with identity elements, groups extend monoids with the concept of inverse elements.&lt;/p&gt;
&lt;p&gt;A group requires that every element has a unique inverse. You can combine an element with its inverse to “cancel out” to the identity element. While it’s not as heavily used or documented, Cats does provide a &lt;a href=&quot;https://typelevel.org/cats/api/cats/kernel/Group.html&quot;&gt;definition&lt;/a&gt;. The &lt;code&gt;Group[A]&lt;/code&gt; typeclass includes an additional method, &lt;code&gt;def inverse(a: A): A&lt;/code&gt;, that maps each element to its inverse. It essentially looks like this:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; empty&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token comment&quot;&gt;// identity&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cats provides instances out of the box for various numerical types. They can be used like so:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res0&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; Group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;remove&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;res1&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The existence of inverse elements allows us to implement &lt;code&gt;remove&lt;/code&gt; which is the inverse operation to &lt;code&gt;combine&lt;/code&gt;. It’s analogous to subtraction, where subtracting &lt;code&gt;b&lt;/code&gt; from &lt;code&gt;a&lt;/code&gt; really just means adding negative &lt;code&gt;b&lt;/code&gt; to &lt;code&gt;a&lt;/code&gt;. Removing means combining inverse &lt;code&gt;b&lt;/code&gt; with &lt;code&gt;a&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; remove&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cats also provides an operator syntax for &lt;code&gt;remove&lt;/code&gt; that’s reminiscent of subtraction:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;res2&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;br /&gt;res3&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that our integer multiplication monoid from above isn’t also a group given division, since the inverses (reciprocal fractions) are no longer integers. To form a group with multiplication, we’d need rational numbers or some sort of &lt;code&gt;Fraction&lt;/code&gt; type. Floating point division doesn’t quite work here because of rounding. Since the mapping isn’t one-to-one. We get in trouble when we assume it works like a true inverse. In the following example, if division truly gave you the inverse, multiplication and division by &lt;code&gt;3.0&lt;/code&gt; would cancel out, and we’d get &lt;code&gt;0.1&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3.0&lt;/span&gt;&lt;br /&gt;res0&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.10000000000000002&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So a group is a monoid with a inverses. This sounds simple, but it actually implies a lot of structure and laws for the set of &lt;code&gt;A&lt;/code&gt;, especially when the type is inhabited by a finite number of elements, or the operation is non-commutative.&lt;/p&gt;
&lt;p&gt;To understand some of this structure, we’ll need to discuss permutations. If you aren’t already familiar with permutations, and specifically how a permutation is actually a bijective function, go back and read &lt;a href=&quot;https://inner-product.com/posts/permutation-functions&quot;&gt;this post&lt;/a&gt; before continuing on.&lt;/p&gt;
&lt;h2 id=&quot;permutation-groups&quot;&gt;Permutation Groups&lt;/h2&gt;
&lt;p&gt;Every element of a group can be viewed as a permutation. To illustrate this, we’ll need an example of a small finite group. We’ll use the integers modulo 6 under addition:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Mod6 &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Mod6 &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; mod6 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mod6 &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mod6&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mod6 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Mod6Group &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Mod6&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; empty &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; m&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this implementation, there are only 6 ways to construct an valid instance of &lt;code&gt;Mod6&lt;/code&gt; using our apply method. In formal terms, the &lt;code&gt;Mod6&lt;/code&gt; group has order 6:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; elements &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt; to &lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;distinct&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toList&lt;br /&gt;elements&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Mod6&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Mod6&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can map over all these elements to get a “combination table” of sorts (Analogous to multiplication tables):&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; mod6Table &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; elements&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; elements&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;mod6Table&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Mod6&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; mod6Table&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mkString&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; foreach println&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, each element uniquely determines a row, each of which contains all six elements in a unique order. That is to say, each element corresponds with a unique permutation of the set. If we were to write that out more concretely using our approach from the previous post, our permutations would look something like this:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; twoMod6 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is no coincidence. Notice that we partially apply an element to get each row via this anonymous function: &lt;code&gt;(m |+| _)&lt;/code&gt;. Since each element has an inverse, this function can be inverted: &lt;code&gt;(_ |-| m)&lt;/code&gt;. So this function is actually a bijective function from a set of elements to itself, which, as we learned in the previous post, is a permutation by definition.&lt;/p&gt;
&lt;p&gt;This holds for any group. Its elements correspond to a set of permutations because of the inverse. Furthermore, those permutations have a combining operation that gives them the same group structure (ie., its combination table looks identical). In more formal terms, every group is isomorphic to some &lt;em&gt;permutation group&lt;/em&gt;. This fact is known as &lt;a href=&quot;https://en.wikipedia.org/wiki/Cayley%27s_theorem&quot;&gt;Cayley’s Theorem&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This is useful because permutations are often simpler to work with in calculations than a naive implementation of a group that they’re isomorphic to. If we have a group, we can implement its data types and operations in terms of permutations. Furthermore, every finite permutation group can be decomposed into a unique product of finite simple groups. These simple groups and their properties are well understood and can help reveal the structure of groups in the problem domain.&lt;/p&gt;
&lt;h2 id=&quot;example%3A-permutations-of-three-elements&quot;&gt;Example: Permutations of Three Elements&lt;/h2&gt;
&lt;p&gt;Let’s look at another concrete example of Cayley’s Theorem. Imagine you have a tile in the shape of an equilateral triangle. Consider the ways you can flip and rotate this tile so that it fits into its original outline. These different orientations of the tile are called the symmetries of a triangle.&lt;/p&gt;
&lt;p&gt;These transformations form a group where the combining operation is sequential application of of transformations. For example, you can rotate the triangle clockwise, then flip it 180 degrees across its vertical axis, or you can perform this action in a single flip that switches the top corner with the left. The first two transformations combine to give a third transformation. Note that this operation is not commutative; flipping vertically then rotating, isn’t the same as rotating first then flipping vertically.&lt;/p&gt;
&lt;p&gt;All in all, there are six symmetries of a triangle, as shown in this image:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/posts/s3-diagram.jpg&quot; alt=&quot;S3 Diagram&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The three corners are labeled so we can reference any of these transformations by reading off the labels from the triangle.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ABC&lt;/code&gt; &lt;code&gt;ACB&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;BCA&lt;/code&gt; &lt;code&gt;BAC&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;CAB&lt;/code&gt; &lt;code&gt;CBA&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;We’ve seen these six words before. These are precisely the six permutations of the string &lt;code&gt;&amp;quot;ABC&amp;quot;&lt;/code&gt; that we worked with in our previous post. We can represent our group of symmetries using the permutations we’ve already defined. These permutations themselves form a group under function composition:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; PermGroup &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Group&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Perm&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; empty &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; C &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; inverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;swap&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Perm&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;compose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s use this group definition to create a combination table analogous to the one we made for &lt;code&gt;Mod6&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; perms &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;abc&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; acb&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bac&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bca&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cab&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cba&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; permsTable &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; perms&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; perms&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our permutations are represented by &lt;code&gt;Map&lt;/code&gt; instances that have a large &lt;code&gt;toString&lt;/code&gt;, so we’ll simplify our table by instead using the index of each permutation from the above list:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;scala&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; permsTable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;perms&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;indexOf&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;mkString&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; foreach println&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We’ve taken two different groups, the integers under addition modulo six, and the symmetries of an equilateral triangle, and found a way to represent them using permutations. The combination table for either shows that each element can be combined with every other element to get any of the six elements. We’ll use this in future posts where we use permutations to translate Rubik’s cube state and algorithms.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Tips for Teaching Scala</title>
		<link href="https://inner-product.com/posts/tips-for-teaching-scala/"/>
		<updated>2019-06-20T00:00:00Z</updated>
		<id>https://inner-product.com/posts/tips-for-teaching-scala/</id>
		<content type="html">&lt;p&gt;I recently gave a talk on teaching Scala. I first gave the talk at the &lt;a href=&quot;https://www.meetup.com/Scala-in-the-City/events/258763565/&quot;&gt;Scala in the City&lt;/a&gt; meetup, which was a dry run for the version I gave at &lt;a href=&quot;https://portal.klewel.com/watch/webcast/scala-days-2019/talk/6/&quot;&gt;Scala Days&lt;/a&gt;.  Take a look at &lt;a href=&quot;https://noelwelsh.com/downloads/tips-for-teaching-scala.pdf&quot;&gt;my slides&lt;/a&gt; if this is of interest to you. My talk centered around five tips for teaching. Here I give a quick rundown of the tips and some references for further reading.&lt;/p&gt;
&lt;p&gt;Before getting into the tips a bit of context is useful. When programmers discuss how to teach programming the discussion almost always focuses on the choice of programming language. Someone will say Python, someone else will advocate for Javascript, and there will always be someone who is adamant that &lt;a href=&quot;https://en.wikipedia.org/wiki/QBasic&quot;&gt;QBasic&lt;/a&gt; is the One True Way because that’s what they learned with. While I think language is important there are two other really important factors that rarely make it into these conversations: curriculum and pedagogy. Curriculum is what you teach and pedagogy is how you teach. I know from my own experience, and from reading the literature, that these two factors can make a huge difference. For this reason my tips focus on curriculum and pedagogy in equal measure. Let’s get on to them.&lt;/p&gt;
&lt;h2 id=&quot;notional-machines&quot;&gt;Notional Machines&lt;/h2&gt;
&lt;p&gt;My first tip is to teach some kind of “notional machine”, which means a simplified machine model that students can understand programs in terms of. This is most appropriate for beginning learners. The first thought that might come to mind you read this is some kind of &lt;a href=&quot;https://en.wikipedia.org/wiki/Von_Neumann_architecture&quot;&gt;von Neumann machine&lt;/a&gt; with registers, a stack, and heap. In functional programming the notional machine is much simpler: it’s algebraic substitution. Substitution is very easy to use (my primary school age children can do it) and allows for compositional reasoning. More on notional machines in &lt;a href=&quot;http://cs.brown.edu/~sk/Publications/Papers/Published/kf-prog-paradigms-and-beyond/&quot;&gt;Programming Paradigms and Beyond&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;programming-strategies&quot;&gt;Programming Strategies&lt;/h2&gt;
&lt;p&gt;Programming strategies are, as far as I know, something unique to how we at &lt;a href=&quot;https://underscore.io/&quot;&gt;Underscore&lt;/a&gt; and &lt;a href=&quot;https://inner-product.com/&quot;&gt;Inner Product&lt;/a&gt; teach programming. That said, they are heavily inspired by the design recipes in &lt;a href=&quot;http://htdp.org/&quot;&gt;How to Design Programs&lt;/a&gt;. The core idea is to allow students to move from problem to solution in a systematic and repeatable way. Programming strategies allow code to written faster with fewer bugs, and by standardizing techniques result in more readable code. They would take many words to explain but the &lt;a href=&quot;https://noelwelsh.com/downloads/tips-for-teaching-scala.pdf&quot;&gt;slides&lt;/a&gt; give some lengthy examples of the main strategies and my colleague Adam’s &lt;a href=&quot;https://arosien.github.io/talks/systematic-software.html&quot;&gt;slides&lt;/a&gt; describe a larger example.&lt;/p&gt;
&lt;h2 id=&quot;more-than-code&quot;&gt;More Than Code&lt;/h2&gt;
&lt;p&gt;This tip is really a reminder that there is more to programming than writing the code. At least two areas need to be taught as well: debugging and tool use. There is a lot of implicit knowledge here, such as interpretering Scala’s error messages. These areas can be explicitly taught. &lt;a href=&quot;https://software-carpentry.org/lessons/&quot;&gt;Software Carpentry&lt;/a&gt;, for example, has some lessons on using the shell and Git that are probably very good. However you might not want to take time away from other topics to do this.&lt;/p&gt;
&lt;p&gt;Another way to teach them is implicitly, by demonstrating them as part of other lessons. Live coding is a great way to do this! You’re bound to make errors, so you can then demonstrate error recovery and debugging, and you’ll naturally be using your tools as you live code. One pro tip: when you hit an error and your brain freezes that is an excellent opportunity to get the students to solve the error. It makes for a better lesson if they’re engaged with the problem and it gives you a chance to reboot.&lt;/p&gt;
&lt;h2 id=&quot;shut-up&quot;&gt;Shut Up&lt;/h2&gt;
&lt;p&gt;This tip is something I had to learn the hard way. People need time to think. They need to grapple with problems and fail before they can succeed. When I was a new teacher I was so keen to help that I’d jump in the moment I saw someone struggling. I was so fast they wouldn’t get a chance to learn. Whenever there was silence—because a student was figuring something out, for example—I’d rush to fill it with words.&lt;/p&gt;
&lt;p&gt;I’ve learned to slow down a bit, let students move at their own pace, and when I help I try to ask probing questions (“which strategy are you using here?”, “what do you think is going wrong?”, and so on) rather than addressing the problem directly. It’s an unfortunate truth that we can’t do the learning for the students. The struggle is real and we must let the student experience that for themselves. (Thanks to Anna Shipman for pointing out the error of my ways!)&lt;/p&gt;
&lt;h2 id=&quot;peer-learning&quot;&gt;Peer Learning&lt;/h2&gt;
&lt;p&gt;My final tip is to encourage students to engage in teaching—teaching and learning are in many ways the same thing! For programming tasks, &lt;a href=&quot;https://tuple.app/pair-programming-guide&quot;&gt;pair programming&lt;/a&gt; or &lt;a href=&quot;https://en.wikipedia.org/wiki/Mob_programming&quot;&gt;mob programming&lt;/a&gt; can work really well. For more general tasks I like to play what I call “the hypothesis game”. The game works like this: the teacher asks a question and gets students to vote on an answer. Students should then turn to the person next to them and explain how they arrived at their answer. Students can vote again, to see if they have changed their mind. Finally they should receive the correct answer and an explanation. By explaining their thoughts the student has to try to make a coherent story, which quickly exposes inconsistencies and errors in their mental model. They are then in an ideal state of mind to receive the correct explanation. When working alone the venerable tool of &lt;a href=&quot;https://en.wikipedia.org/wiki/Rubber_duck_debugging&quot;&gt;rubber ducking&lt;/a&gt; can be used to simulate an audience.&lt;/p&gt;
&lt;h2 id=&quot;further-resources&quot;&gt;Further Resources&lt;/h2&gt;
&lt;p&gt;The above five tips are far from the final word on teaching programming, so I want to throw in a few more resources that have influenced me.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Everything &lt;a href=&quot;http://third-bit.com/&quot;&gt;Greg Wilson&lt;/a&gt; does is great, but if you only read one thing of his make it &lt;a href=&quot;http://teachtogether.tech/&quot;&gt;Teaching Tech Together&lt;/a&gt;. It’s short, to the point, and very good. His paper (with &lt;a href=&quot;https://kclpure.kcl.ac.uk/portal/neil.c.c.brown.html&quot;&gt;Neil Brown&lt;/a&gt;) &lt;a href=&quot;https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1006023&quot;&gt;“Ten Quick Tips for Teaching Programming”&lt;/a&gt; is also a good read and, with ten tips, has twice the value of this blog post!&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://visible-learning.org/&quot;&gt;Visible Learning&lt;/a&gt; collects the work of John Hattie, particularly his large meta-analyses and &lt;a href=&quot;https://visible-learning.org/hattie-ranking-influences-effect-sizes-learning-achievement/&quot;&gt;rankings&lt;/a&gt; of the literature on pedagogical techniques. Although there is some debate on the correctness of his effect size calculations, I believe the overall rankings are in general correct, and they provide for me a very quick way to find the most impactful teaching techniques to study.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;a href=&quot;https://racket-lang.org/people.html&quot;&gt;PLT&lt;/a&gt; research group provided my first introduction to programming language theory, as distinct from programming, and to teaching programming. I’ve already mentioned &lt;a href=&quot;http://htdp.org/&quot;&gt;How to Design Programs&lt;/a&gt;. &lt;a href=&quot;https://programbydesign.org/&quot;&gt;Program by Design&lt;/a&gt; and &lt;a href=&quot;https://www.bootstrapworld.org/&quot;&gt;Bootstrap&lt;/a&gt; are other inspirations.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;Teaching programming is a distinct skill from programming, and one that I have only just scratched the surface of. I hope the above helps you become a better teacher, whether you’re a senior developer teaching juniors, helping out at a program such as &lt;a href=&quot;https://scalabridge.org/&quot;&gt;ScalaBridge&lt;/a&gt;, or teaching yourself.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Type Classes vs Records of Functions</title>
		<link href="https://inner-product.com/posts/type-classes-vs-records-of-functions/"/>
		<updated>2019-07-02T00:00:00Z</updated>
		<id>https://inner-product.com/posts/type-classes-vs-records-of-functions/</id>
		<content type="html">&lt;p&gt;Type classes and records of functions are two very similar tools that are available in languages like Haskell, Scala, and Rust. Given the similarity the question arises of which we should use. In this blog post I discuss when I think one language mechanism should be preferred over the other.&lt;/p&gt;
&lt;h2 id=&quot;a-who-of-what%3F&quot;&gt;A Who of What?&lt;/h2&gt;
&lt;p&gt;Before we get into the details, lets just clear up the terminology. I’m going to assume you know what a type class is. A record is the Haskell term for what in Scala we’d call a &lt;code&gt;class&lt;/code&gt; (or &lt;code&gt;trait&lt;/code&gt;, or &lt;code&gt;case class&lt;/code&gt;) and in Rust a &lt;code&gt;struct&lt;/code&gt;. Basically it’s a data structure where the fields (the bits that store the data) have names. So a record of functions is a data structure with named fields where the fields hold functions. In OO parlance fields that hold functions are methods, so we’re basically talking about objects.&lt;/p&gt;
&lt;p&gt;An object, or record of functions, is very similar to a type class. In fact if we forget about the compile-time dispatch that type classes bring it’s the same thing. This similarity is why the question arises as to which we should use.&lt;/p&gt;
&lt;h2 id=&quot;composition&quot;&gt;Composition&lt;/h2&gt;
&lt;p&gt;Type class composition is one nice feature that records of functions don’t provide. The basic idea is that if we define the base elements and the composition rules, the compiler will automatically apply the composition rules to construct a type class instance.&lt;/p&gt;
&lt;p&gt;For example, if we have a &lt;code&gt;Monoid&lt;/code&gt; type class such as&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; identity&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and instances&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; intMonoid &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; y&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; identity&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; optionMonoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; m&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;m&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; None&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;None&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;None&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;    None&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; None&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; identity&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; None&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;we don’t have to explicitly define &lt;code&gt;Monoid[Option[Int]]&lt;/code&gt;. The compiler will construct the instance for us. No such automatic composition takes place with records of functions.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://inner-product.com/posts/type-classes-vs-records-of-functions/#fn1&quot; id=&quot;fnref1&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;So here is our first criteria for deciding if we should use type classes or records of functions: will type class composition bring us a lot of value? If not, perhaps we shouldn’t use a type class.&lt;/p&gt;
&lt;h2 id=&quot;comprehensibility&quot;&gt;Comprehensibility&lt;/h2&gt;
&lt;p&gt;You’ll sometimes see functional programmers ranting about “lawless type classes”. This combines two of (some) functional programmers favourite things: impenetrable jargon and getting bent out of shape. However, there is some point to this discussion.&lt;br /&gt;
Type class “laws” are statement, usually expressed in maths, of some properties that must hold for a type class instance to be considered valid. For example, for &lt;code&gt;Monoid&lt;/code&gt; the laws are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the binary operation must be associative, so &lt;code&gt;combine(x, combine(y, z)) == combine(combine(x, y), z)&lt;/code&gt;; and&lt;/li&gt;
&lt;li&gt;the identity must be a commutative … umm … identity, so &lt;code&gt;combine(a, identity) == a == combine(identity, a)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Defining the semantics is important because it’s these semantics that allow us to implement systems using type classes and know they’ll work as we expect. Let’s address this from two different angles.&lt;/p&gt;
&lt;p&gt;One way of seeing the importance of clearly defined semantics is to consider how many possible type class instances we could implement for &lt;code&gt;Monoid&lt;/code&gt; if we disregarded the laws. Almost anything would be valid. For example, we could define&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; intMonoid &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    x &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; identity&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or indeed any other &lt;code&gt;combine&lt;/code&gt; and &lt;code&gt;identity&lt;/code&gt; that match the type signature. If this was common place how would we ever understand what code using type classes will do? Type classes seem fairly magical in the first place. We need laws to allow us to build understanding when they are being used.&lt;/p&gt;
&lt;p&gt;As another example, monoids are really &lt;a href=&quot;https://github.com/twitter/algebird&quot;&gt;useful&lt;/a&gt; in &lt;a href=&quot;https://arxiv.org/abs/1304.7544&quot;&gt;data science&lt;/a&gt; because they allow us to easily parallelize jobs. Remember associativity says that &lt;code&gt;a + (b + c) == (a + b) + c&lt;/code&gt;. If we consider a pair of brackets to be a machine, if the operation we want to perform is a monoid it doesn’t matter how we distribute work across those machines so long as retain the ordering of the data (and if the operation is commutative, which most are, the order doesn’t matter at all). Here the clearly defined semantics allow to guarantee that our distributed system will be correct.&lt;/p&gt;
&lt;p&gt;Some type classes don’t have laws. This is what a “lawless type class” is (sadly, not some lovable functional programming rogue that steals from the rich to give to the poor.)&lt;/p&gt;
&lt;p&gt;Now, are lawless type classes bad? Not necessarily! Lawlessness (gosh, I hate this pompous terminology) is a proxy for the real issue, which is program understanding. If a type class has laws then you have some principles you can use to reason about your program. A lawless type class might still have principles you can use to reason about code. For example, the 0.9 branch of &lt;a href=&quot;https://www.creativescala.org/doodle/&quot;&gt;Doodle&lt;/a&gt; has a &lt;code&gt;Renderer&lt;/code&gt; type class. Its role is to render (i.e. draw) a picture to some kind of canvas. There aren’t any useful laws you can write about this without introducing a huge amount of mathematical machinery to describe the effect of rendering. However it’s still fairly easy to understand what this type class does.&lt;/p&gt;
&lt;p&gt;So here is our second criteria: if we can clearly define the semantics of an interface it could be a type class. If the interface is just a blob of functions that “do stuff”, like we might find inside the implementation of an interpreter, it probably should not be a type class.&lt;/p&gt;
&lt;h2 id=&quot;multiple-instances&quot;&gt;Multiple Instances&lt;/h2&gt;
&lt;p&gt;Type classes use types to choose type class instances. In other words they dispatch on type. Different languages have different rules for where they find type class instances, but in all cases that I know of the compiler will search outside the usual lexical scope for instances. If there are multiple instances for the same type it could be difficult to work out which one is used. Haskell forbids this. Scala allows multiple instances but if two or more are in scope code will not compile. I’m not sure what the situation is with Rust.&lt;/p&gt;
&lt;p&gt;Where multiple instances are allowed there are clear rules for which instance is being used, but that doesn’t mean it’s easy for the programmer to work out what the compiler will do. For example, the choice of instance might depend on imports that are far away from the code being viewed, or on complicated priority rules.&lt;/p&gt;
&lt;p&gt;This gives us another criteria for choosing between type classes and records of functions: if we could want multiple instances for the same type we’re probably better using records of functions than type classes. This is really another facet of “Comprehensibility” above. Using multiple instances can lead to confusion when we’re relying on the compiler to choose instances for us.&lt;/p&gt;
&lt;h2 id=&quot;convenience&quot;&gt;Convenience&lt;/h2&gt;
&lt;p&gt;The final issue I want to consider is convenience. A library’s API is the user interface through with the programmer interacts with it, and using type classes can make that API easier to use. For example, in Doodle making the &lt;code&gt;Renderer&lt;/code&gt; a type class means the user doesn’t have to explicitly specify the &lt;code&gt;Renderer&lt;/code&gt; when calling the &lt;code&gt;draw&lt;/code&gt; method on a picture. This makes drawing easier. The user needs to know less about the library to get something working.&lt;/p&gt;
&lt;p&gt;So my final criteria for choosing between type classes and records of functions: will using type classes make the code easier to use for the end user?&lt;/p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;We’ve seen a few different reasons for choosing between type classes and records of functions. In summary they are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if automatic composition will be useful then consider type classes;&lt;/li&gt;
&lt;li&gt;if semantics are not well defined don’t use a type class;&lt;/li&gt;
&lt;li&gt;if multiple instances are likely a type class is probably the wrong choice; and&lt;/li&gt;
&lt;li&gt;if it will make your code substantially easier to use a type class might be the right choice.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Hopefully this will make the decision easier for you.&lt;/p&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;There are some languages and libraries that allow adding and removing fields from records at runtime. They are generally known as “extensible records”. To the best of my knowledge no such system is in widespread use. OO languages allow compile-time composition (usually called extension or inheritance). Neither system allows automatic type-driven composition, which is the magic that type classes bring. &lt;a href=&quot;https://inner-product.com/posts/type-classes-vs-records-of-functions/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
	</entry>
	
	<entry>
		<title>Optional Actions</title>
		<link href="https://inner-product.com/posts/optional-actions/"/>
		<updated>2019-11-18T00:00:00Z</updated>
		<id>https://inner-product.com/posts/optional-actions/</id>
		<content type="html">&lt;p&gt;Sometimes, amongst required actions, we need to &lt;em&gt;optionally&lt;/em&gt; do something. In this post we’ll see how we can use &lt;code&gt;cats&lt;/code&gt; to concisely handle this situation.&lt;/p&gt;
&lt;h2 id=&quot;the-basic-scenario&quot;&gt;The Basic Scenario&lt;/h2&gt;
&lt;p&gt;Here’s some code that approximates the basic scenario:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  y  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There’s an immediate issue: we don’t have an &lt;code&gt;else&lt;/code&gt; for our &lt;code&gt;if&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Did you also know that this is allowed in Scala? An &lt;code&gt;if&lt;/code&gt; without an else returns &lt;code&gt;Unit&lt;/code&gt;. &lt;strong&gt;*Sigh*&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let’s complete the expression with a placeholder:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  y  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// TODO&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, as written, if the optional action is executed, we &lt;em&gt;won’t&lt;/em&gt; know if it succeeded or failed. Take a moment to see why…&lt;/p&gt;
&lt;h2 id=&quot;avoiding-unexamined-effects&quot;&gt;Avoiding Unexamined Effects&lt;/h2&gt;
&lt;p&gt;Do you see why the action can fail and we won’t know it? Say the &lt;code&gt;optionalAction&lt;/code&gt; returns a &lt;code&gt;Future&lt;/code&gt;, then &lt;code&gt;y&lt;/code&gt; is assigned that &lt;code&gt;Future&lt;/code&gt;. And a &lt;code&gt;Future&lt;/code&gt; isn’t the result itself, it merely represents some future value. We need to wait until the &lt;code&gt;Future&lt;/code&gt; completes to know if it succeeded or not.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  y  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// `y` is the action itself, not the successful value of the action!&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is true of any monad we use in our expression. If our actions return &lt;code&gt;Either&lt;/code&gt;, a value of type &lt;code&gt;Either&lt;/code&gt; doesn’t tell us if it suceeded or failed. We need to know if the value is a &lt;code&gt;Left&lt;/code&gt; or a &lt;code&gt;Right&lt;/code&gt;. And so on for other monads.&lt;/p&gt;
&lt;p&gt;For any monad, how do we know if some action succeeded? If it does succeed, we’ll get a value on the left-hand side of the &lt;code&gt;&amp;lt;-&lt;/code&gt; in our for-comprehension:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// switch from using `=` (value binding) to `&amp;lt;-` (monadic binding)&lt;/span&gt;&lt;br /&gt;  y &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Obligatory reminder: a for-comprehension is just syntactic sugar for&lt;br /&gt;
nested &lt;code&gt;flatMap&lt;/code&gt; calls. So &lt;code&gt;y &amp;lt;- someAction&lt;/code&gt; is really &lt;code&gt;someAction.flatMap(y =&amp;gt; ???)&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The only way &lt;code&gt;y&lt;/code&gt; can have a value is if &lt;code&gt;optionalAction&lt;/code&gt; succeeded, or the &lt;code&gt;else&lt;/code&gt; clause’s action was successful.&lt;/p&gt;
&lt;p&gt;Let’s take care of the &lt;code&gt;else&lt;/code&gt; case, when &lt;code&gt;x.isAmazing&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;. We need to provide some (monadic) value that has a type compatible with &lt;code&gt;optionalAction&lt;/code&gt;, in order for the expression to type check. What’s the type of &lt;code&gt;optionalAction&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;Since we want to be monad-agnostic, let’s call the monad &lt;code&gt;F&lt;/code&gt;. &lt;code&gt;optionalAction&lt;/code&gt; will return an &lt;code&gt;F&lt;/code&gt;, and the &lt;code&gt;else&lt;/code&gt; branch also needs to return an &lt;code&gt;F&lt;/code&gt;. But &lt;code&gt;F&lt;/code&gt; is a container type, it holds a value. It doesn’t really matter what our &lt;code&gt;else&lt;/code&gt; clause returns, so let’s choose &lt;code&gt;()&lt;/code&gt; (“Unit”). We “lift” a Unit into the current monad via &lt;a href=&quot;https://typelevel.org/cats/api/cats/Applicative.html#pure%5BA%5D(x:A):F%5BA%5D&quot;&gt;&lt;code&gt;pure&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implicits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_ &lt;span class=&quot;token comment&quot;&gt;// for extension methods like pure&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  y &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pure&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;F&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// lift a value into an effect F&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do we need &lt;code&gt;y&lt;/code&gt;? No! We can “throw it away” by naming it &lt;code&gt;_&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;pure&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;F&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Luckily for us, this “if condition, run the action, else pure Unit” pattern is a helper method named &lt;a href=&quot;https://typelevel.org/cats/api/cats/Applicative.html#whenA%5BA%5D(cond:Boolean)(f:=%3EF%5BA%5D):F%5BUnit%5D&quot;&gt;&lt;code&gt;whenA&lt;/code&gt;&lt;/a&gt;!&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;Applicative&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; Applicative&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;F&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;whenA&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;NOTE: There is a form of &lt;code&gt;whenA&lt;/code&gt; that acts as an extension method on an effect, but the effect is not lazily evaluated, which may be necessary with effects like &lt;code&gt;Future&lt;/code&gt;. So please ignore the above ugliness.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;To summarize so far:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We need to evaluate optional effects, otherwise we won’t know if they succeeded or failed.&lt;/li&gt;
&lt;li&gt;If we have a boolean condition, we can use the &lt;code&gt;whenA&lt;/code&gt; combinator to choose between an optional action and an “empty” effect that returns &lt;code&gt;()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;optional-actions-via-the-option-type&quot;&gt;Optional Actions via the &lt;code&gt;Option&lt;/code&gt; type&lt;/h2&gt;
&lt;p&gt;There’s an alternate way of encoding optional actions, let’s use the &lt;code&gt;Option&lt;/code&gt; type! Instead of checking a &lt;code&gt;Boolean&lt;/code&gt; value to conditionally evaluate an action of type &lt;code&gt;F[A]&lt;/code&gt;, what if the action itself was contained within an &lt;code&gt;Option&lt;/code&gt;? Then it’s optional, right?&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x    &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  optA  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// has type Option[F[Unit]]&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; None&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// TODO: extract result of opt&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We could use the &lt;a href=&quot;https://www.scala-lang.org/api/current/scala/Option$.html#when%5BA%5D(cond:Boolean)(a:=%3EA):Option%5BA%5D&quot;&gt;&lt;code&gt;Option.when&lt;/code&gt;&lt;/a&gt; factory method to replace the &lt;code&gt;if (predicate) Some(action)&lt;/code&gt;/&lt;code&gt;else None&lt;/code&gt; code:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x    &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  optA  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;when&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// TODO: extract result of opt&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have a value of type &lt;code&gt;Option[F[Unit]]&lt;/code&gt;, but our expression requires actions to be in the &lt;code&gt;F&lt;/code&gt; monad. Is there a way to transform an &lt;code&gt;Option&lt;/code&gt; of &lt;code&gt;F&lt;/code&gt; into an &lt;code&gt;F&lt;/code&gt; of &lt;code&gt;Option&lt;/code&gt;? Yes! That’s &lt;a href=&quot;https://typelevel.org/cats/api/cats/Traverse.html#sequence%5BG%5B_%5D,A%5D(fga:F%5BG%5BA%5D%5D)(implicitevidence$2:cats.Applicative%5BG%5D):G%5BF%5BA%5D%5D&quot;&gt;&lt;code&gt;sequence&lt;/code&gt;&lt;/a&gt; (which is equivalent to &lt;code&gt;traverse&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implicits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  x    &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; requiredAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  optA  &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;when&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isAmazing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;optionalAction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  _    &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; optA&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sequence &lt;span class=&quot;token comment&quot;&gt;// Option[F[Unit]] =&gt; F[Option[Unit]]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; x&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://typelevel.org/cats/typeclasses/traverse.html&quot;&gt;&lt;code&gt;traverse&lt;/code&gt;&lt;/a&gt; is everywhere!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;WARNING: Putting a &lt;code&gt;Future&lt;/code&gt; within an &lt;code&gt;Option&lt;/code&gt; &lt;em&gt;doesn’t&lt;/em&gt; optionally execute the &lt;code&gt;Future&lt;/code&gt;, because &lt;code&gt;Future&lt;/code&gt; is eagerly scheduled. So you can’t use this technique with &lt;code&gt;Future&lt;/code&gt;. Use &lt;a href=&quot;https://typelevel.org/cats-effect/datatypes/io.html&quot;&gt;&lt;code&gt;cats.effect.IO&lt;/code&gt;&lt;/a&gt; instead.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;Optionally performing actions is a common need, and they have a regular structure we can abstract over using &lt;code&gt;cats&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://typelevel.org/cats/api/cats/Applicative.html#whenA%5BA%5D(cond:Boolean)(f:=%3EF%5BA%5D):F%5BUnit%5D&quot;&gt;&lt;code&gt;Applicative.whenA&lt;/code&gt;&lt;/a&gt; lets us construct an optional action from a predicate and a way to construct that action. It’s built from an &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; conditional and using &lt;code&gt;pure&lt;/code&gt; to construct an empty action in case the predicate fails.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://typelevel.org/cats/api/cats/Traverse.html#sequence%5BG%5B_%5D,A%5D(fga:F%5BG%5BA%5D%5D)(implicitevidence$2:cats.Applicative%5BG%5D):G%5BF%5BA%5D%5D&quot;&gt;&lt;code&gt;Traverse.sequence&lt;/code&gt;&lt;/a&gt; lets us transform an optional action (&lt;code&gt;Option[F[A]]&lt;/code&gt;) into an action that produces an optional value (&lt;code&gt;F[Option[A]]&lt;/code&gt;), because our for-comprehension is using the &lt;code&gt;F&lt;/code&gt; monad, not &lt;code&gt;Option&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At the same time, we need to avoid pitfalls like constructing optional actions without ensuring they are a part of the entire computation. We also showed how we don’t necessarily care about the &lt;em&gt;value&lt;/em&gt; produced by an optional action, only that the action succeeded. In a for-comprehension, this means:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;evaluating the action with &lt;code&gt;&amp;lt;-&lt;/code&gt;; and&lt;/li&gt;
&lt;li&gt;naming the result &lt;code&gt;_&lt;/code&gt; (to forget it)&lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<title>Leveraging Monoids for More Powerful Summarization</title>
		<link href="https://inner-product.com/posts/leveraging-monoids/"/>
		<updated>2019-12-17T00:00:00Z</updated>
		<id>https://inner-product.com/posts/leveraging-monoids/</id>
		<content type="html">&lt;p&gt;During our &lt;a href=&quot;https://www.inner-product.com/services/#training&quot;&gt;training and mentoring&lt;/a&gt;, we often hear students say “I’m not sure what to do here” when faced with trying to choose what function to call in order to solve their problem. First, it’s completely normal; nobody instantly knows the answer. To try to help, one piece of advice we’ve found to be useful is in the form of a heuristic:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The solution is usually either a &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;flatMap&lt;/code&gt;, or &lt;code&gt;fold&lt;/code&gt;.&lt;sup&gt;*&lt;/sup&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this post we’ll be discussing the power of &lt;code&gt;fold&lt;/code&gt;s: how they are used to summarize a data structure, how summarization can be decomposed and abstracted, and how that abstraction can be leveraged to provide better performance in a generic way.&lt;/p&gt;
&lt;p&gt;&lt;small&gt;&lt;sup&gt;*&lt;/sup&gt;And if it isn’t one of those, it’s probably &lt;code&gt;traverse&lt;/code&gt;.&lt;/small&gt;&lt;/p&gt;
&lt;h2 id=&quot;decomposing-summarization&quot;&gt;Decomposing Summarization&lt;/h2&gt;
&lt;p&gt;If a &lt;code&gt;List&lt;/code&gt; contains numbers, we can compute the sum of the elements using a &lt;em&gt;fold&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foldLeft&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; sum &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res0: Int = 6&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;foldLeft&lt;/code&gt; has a rather generic signature, so let’s factor out the necessary knowledge to summarize our &lt;code&gt;List&lt;/code&gt; of &lt;code&gt;Int&lt;/code&gt;s:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We need a starting value to initialize our summary, in case the list is empty: &lt;code&gt;ifEmpty: Int&lt;/code&gt;; and&lt;/li&gt;
&lt;li&gt;We need a way to combine the previous summary to the next element: &lt;code&gt;combine: (Int, Int) =&amp;gt; Int&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s write a helper function &lt;code&gt;summarize&lt;/code&gt; to name these parts, and just delegate to &lt;code&gt;foldLeft&lt;/code&gt; as the implementation:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; summarize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ifEmpty&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; combine&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  is&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foldLeft&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ifEmpty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;summarize&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res1: Int = 6&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But how could we summarize &lt;em&gt;any&lt;/em&gt; list, not just a list of numbers? Let’s allow the caller to choose the element type, and adjust the &lt;code&gt;ifEmpty&lt;/code&gt; and &lt;code&gt;combine&lt;/code&gt; helpers:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; summarize2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;as&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ifEmpty&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; combine&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  as&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foldLeft&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ifEmpty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;summarize2&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; _ &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; _&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res2: Int = 6&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the caller can summarize a list any way they want. But it’s annoying to have to supply &lt;code&gt;ifEmpty&lt;/code&gt; and &lt;code&gt;combine&lt;/code&gt; for every call; those parameters will (almost) always be the same for every type &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;capturing-common-behavior-in-a-typeclass&quot;&gt;Capturing Common Behavior in a Typeclass&lt;/h2&gt;
&lt;p&gt;Let’s factor those two helpers into a typeclass:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// n.b. exactly the same as cats.Monoid&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a1&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a2&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then implicitly provide the &lt;code&gt;Monoid&lt;/code&gt; containing our &lt;code&gt;empty&lt;/code&gt; and &lt;code&gt;combine&lt;/code&gt; helpers:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; summarize3&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;as&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; M&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  as&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foldLeft&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;M&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; M&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s create an &lt;code&gt;Monoid[Int]&lt;/code&gt; instance so we can use it in &lt;code&gt;summarize3&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; intMonoid&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i1&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; d2&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; i1 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; d2&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally we’re back where we started:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;summarize3&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res3: Int = 6&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;summarize3&lt;/code&gt; will now work for lists of any type &lt;code&gt;A&lt;/code&gt;, as long as &lt;code&gt;A&lt;/code&gt; has a &lt;code&gt;Monoid[A]&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;deriving-foldmap&quot;&gt;Deriving &lt;code&gt;foldMap&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;But what if we have a list of some type that &lt;em&gt;doesn’t&lt;/em&gt;, or can’t, have a &lt;code&gt;Monoid&lt;/code&gt;? To compute a summary for it, we’ll need to transform our list into a type that &lt;em&gt;does&lt;/em&gt; have a &lt;code&gt;Monoid&lt;/code&gt;. Is there a way to do this?&lt;/p&gt;
&lt;p&gt;There is, and it’s a common strategy in functional programming: we ask for help from the caller, since &lt;code&gt;foldMap&lt;/code&gt; itself can’t know what to do. If the caller provides us with a function &lt;code&gt;A =&amp;gt; B&lt;/code&gt;, and &lt;em&gt;&lt;code&gt;B&lt;/code&gt;&lt;/em&gt; has a &lt;code&gt;Monoid&lt;/code&gt;, then we can summarize. Aha!&lt;/p&gt;
&lt;p&gt;This function is usually called &lt;a href=&quot;https://typelevel.org/cats/api/cats/Foldable.html#foldMap%5BA,B%5D(fa:F%5BA%5D)(f:A=%3EB)(implicitB:cats.Monoid%5BB%5D):B&quot;&gt;&lt;code&gt;foldMap&lt;/code&gt;&lt;/a&gt;, because we’re both &lt;code&gt;fold&lt;/code&gt;-ing (the list) and &lt;code&gt;map&lt;/code&gt;-ping the elements (the &lt;code&gt;A =&amp;gt; B&lt;/code&gt; function):&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Summarize a List[A] as a B, if B has a Monoid.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; foldMap&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;as&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; M&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; B &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  as&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foldLeft&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;M&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; M&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If &lt;code&gt;A&lt;/code&gt; already has a &lt;code&gt;Monoid&lt;/code&gt;, then we can summarize using &lt;code&gt;foldMap&lt;/code&gt; by &lt;em&gt;not&lt;/em&gt; transforming the data. That is, we transform it with the &lt;code&gt;identity&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;as&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  foldMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;as&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;identity&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res4: Int = 15&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It’s not very exciting, but we can count the length of a list too (implicitly using the &lt;code&gt;Monoid[Int]&lt;/code&gt; to “increment”):&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; count&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;as&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  foldMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;as&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_ &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;//               ^&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;//               increment count by 1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;//               for each element&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;count&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res5: Int = 5&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;computing-the-mean-with-foldmap&quot;&gt;Computing the Mean with &lt;code&gt;foldMap&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;What can you do with the sum and the count of a list? Compute the mean!&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; l &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;l&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res6: Int = 15&lt;/span&gt;&lt;br /&gt;count&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;l&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res7: Int = 5&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toDouble &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; count&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;mean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;l&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res8: Double = 3.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It works!&lt;/p&gt;
&lt;h2 id=&quot;computing-the-mean-with-foldmap%3A-improved!&quot;&gt;Computing the Mean with &lt;code&gt;foldMap&lt;/code&gt;: Improved!&lt;/h2&gt;
&lt;p&gt;For the performance-minded, there’s an issue with the computation of the mean: it processes the list twice, once for the sum and once for the count. Could we compute the mean in only one pass?&lt;/p&gt;
&lt;p&gt;One way to think about how we could do this would be to fill in, as best we can, the parameters to &lt;em&gt;one&lt;/em&gt; call to &lt;code&gt;foldMap&lt;/code&gt;, in order to compute the answer we’re looking for. That is, we need &lt;code&gt;foldMap&lt;/code&gt; to return the sum AND the count:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; onePassMean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// need sum AND count from foldMap&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; count&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    foldMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  sum&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toDouble &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; count&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If &lt;code&gt;foldMap&lt;/code&gt; is to return an &lt;code&gt;(Int, Int)&lt;/code&gt; tuple, the &lt;code&gt;A =&amp;gt; B&lt;/code&gt; mapping function–the &lt;code&gt;???&lt;/code&gt; above–needs to return &lt;code&gt;(Int, Int)&lt;/code&gt;, and &lt;code&gt;foldMap&lt;/code&gt; also requires a &lt;code&gt;Monoid[(Int, Int)]&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; onePassMean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; count&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    foldMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;//                ^&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;//                needs to produce (Int, Int)&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  sum&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toDouble &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; count&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// error: could not find implicit value for parameter M: repl.Session.App.Monoid[(Int, Int)]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;//     foldMap(is)(i =&gt; (???): (Int, Int))&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;//     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We know the values of components of the tuple: the first needs to be the sum and the second needs to be the count. When &lt;code&gt;foldMap&lt;/code&gt; uses the &lt;code&gt;Monoid[(Int, Int)]&lt;/code&gt; to combine tuples, it needs to work like this:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; sumAndCount &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// sumAndCount: (Int, Int) = (10, 4)&lt;/span&gt;&lt;br /&gt;                 &lt;span class=&quot;token comment&quot;&gt;// ^  ^&lt;/span&gt;&lt;br /&gt;                 &lt;span class=&quot;token comment&quot;&gt;// ^  previous count&lt;/span&gt;&lt;br /&gt;                 &lt;span class=&quot;token comment&quot;&gt;// previous sum&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; toAdd &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// toAdd: (Int, Int) = (5, 1)&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token comment&quot;&gt;// ^  ^&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token comment&quot;&gt;// ^  increment the count by 1 &lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token comment&quot;&gt;// increment the sum by 5&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; totals &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// totals: (Int, Int) = (15, 5)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This generalizes to any tuple: combining two tuples produces one tuple with combined components. Sounds familiar? Here’s the &lt;code&gt;Monoid&lt;/code&gt; we need! (if there exists a &lt;code&gt;Monoid&lt;/code&gt; for the each component of the tuple)&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; tuple2Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; MA&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MB&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;// the empty tuple is a tuple of empty values from each Monoid&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; empty&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MA&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MB&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;// combine A fields via MA, combine B fields via MB&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ab1&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ab2&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ab1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ab2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a2&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MA&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; MB&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;tuple2Monoid&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;combine&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res11: (Int, Int) = (15, 5)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can compute our mean with one pass over the data:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; onePassMean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; count&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    foldMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;//                ^  ^&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;//                ^  increment count by 1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;//                increment sum by i&lt;/span&gt;&lt;br /&gt;  sum&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toDouble &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; count&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;onePassMean&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;l&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res12: Double = 3.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Summarizing a &lt;code&gt;List&lt;/code&gt; is a &lt;em&gt;fold&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The initial value and combining operation of elements for a summary can be abstracted over by a &lt;a href=&quot;https://typelevel.org/cats/typeclasses/monoid.html&quot;&gt;&lt;code&gt;Monoid&lt;/code&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;foldMap&lt;/code&gt; summarizes a &lt;code&gt;List&lt;/code&gt; by requiring each element be able to be transformed into something that has a &lt;code&gt;Monoid&lt;/code&gt;. &lt;a href=&quot;https://typelevel.org/cats/api/cats/Foldable.html#foldMap%5BA,B%5D(fa:F%5BA%5D)(f:A=%3EB)(implicitB:cats.Monoid%5BB%5D):B&quot;&gt;&lt;code&gt;foldMap&lt;/code&gt;&lt;/a&gt; is available in cats as part of the &lt;a href=&quot;https://typelevel.org/cats/typeclasses/foldable.html&quot;&gt;&lt;code&gt;Foldable&lt;/code&gt;&lt;/a&gt; typeclass, which models foldable (“summarizable”) types like &lt;code&gt;List&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We can derive a typeclass instance for &lt;em&gt;tuples&lt;/em&gt; of monoids, which then gives us a way to combine values “in parallel”. &lt;a href=&quot;https://typelevel.org/cats/api/cats/instances/TupleInstances.html&quot;&gt;Typeclass instances for tuples&lt;/a&gt; are included in cats, and are usually imported into scope via the wildcard &lt;code&gt;import cats.implicits._&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// The same example as above, but using cats.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implicits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foldMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// res13: (Int, Int) = (15, 5)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Further reading:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.cs.nott.ac.uk/~pszgmh/fold.pdf&quot;&gt;A tutorial on the universality and&lt;br /&gt;
expressiveness of fold&lt;/a&gt; by Graham Hutton.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/Gabriel439/slides/blob/master/munihac/foldmap.md&quot;&gt;Beautiful folds&lt;/a&gt; by Gabriel Gonzalez.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://twitter.github.io/algebird/&quot;&gt;Algebird&lt;/a&gt;, a library from Twitter for building aggregations, used in systems like Summingbird.&lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<title>Inner Product&#39;s Best of 2019</title>
		<link href="https://inner-product.com/posts/best-of-2019/"/>
		<updated>2019-12-31T00:00:00Z</updated>
		<id>https://inner-product.com/posts/best-of-2019/</id>
		<content type="html">&lt;p&gt;Here’s a sample of the best writing, talks, and people we’ve come across in 2019. We hope you enjoy them as much as we did.&lt;/p&gt;
&lt;p&gt;– &lt;a href=&quot;https://www.linkedin.com/in/arosien/&quot;&gt;Adam&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/mmynsted/&quot;&gt;Mark&lt;/a&gt;, &lt;a href=&quot;https://noelwelsh.com/&quot;&gt;Noel&lt;/a&gt;, and &lt;a href=&quot;https://www.linkedin.com/in/stewart-stewart-1a268563/&quot;&gt;Stewart&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;talks&quot;&gt;Talks&lt;/h2&gt;
&lt;p&gt;Many of our favorite talks took place at &lt;a href=&quot;https://www.thestrangeloop.com/2019/sessions.html&quot;&gt;Strange Loop&lt;/a&gt;, but sadly none of us could attend. For all the conferences that make their talks accessible to the public, thank you!&lt;/p&gt;
&lt;h3 id=&quot;intro-to-cats-effect%2C-gavin-bisesi&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=83pXEdCpY4A&quot;&gt;Intro to Cats-Effect&lt;/a&gt;, Gavin Bisesi&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Adam: Gavin provides a thorough and illuminating tour of &lt;a href=&quot;https://typelevel.org/cats-effect/&quot;&gt;&lt;code&gt;cats-effect&lt;/code&gt;&lt;/a&gt; for any Scala developer.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/83pXEdCpY4A&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;h3 id=&quot;performance-matters%2C-emery-berger&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=r-TLSBdHe1A&quot;&gt;Performance Matters&lt;/a&gt;, Emery Berger&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Mark: While I spend a lot of time ignoring performance in order to model correct and reasonable code,  performance can not be ignored forever. Making changes to improve performance can have surprising consequences. Everybody should watch this talk before making performance enhancements. It is far more than Stabilizer. The lessons learned go beyond any particular language.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/r-TLSBdHe1A&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;h3 id=&quot;scale-by-the-bay-2019%3A-paul-cleary%2C-re-programming-the-programmer%2C-from-actors-to-fp%2C-paul-cleary&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=PJ7ozyLnaEY&quot;&gt;Scale By The Bay 2019: Paul Cleary, Re-programming the programmer, from Actors to FP&lt;/a&gt;, Paul Cleary&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Adam: Paul shares the real story of how he and his team redesigned and refactored a system to use the Typelevel ecosystem of libraries. It’s not just a technical story, it’s a story of the process the team went through to mentally switch gears to a more principled and functional approach.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/PJ7ozyLnaEY&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;h3 id=&quot;probabilistic-scripts-for-automating-common-sense-tasks%2C-alexander-lew&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=MiiWzJE0fEA&quot;&gt;Probabilistic scripts for automating common-sense tasks&lt;/a&gt;, Alexander Lew&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Mark: An accessible and practical talk about using probabilistic scripts for automation. It describes common challenges and how to address them. It does this in a useful way.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/MiiWzJE0fEA&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;h3 id=&quot;finding-bugs-without-running-or-even-looking-at-code%2C-jay-parlar&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=FvNRlE4E9QQ&quot;&gt;Finding bugs without running or even looking at code&lt;/a&gt;, Jay Parlar&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Mark: This talk is fascinating. The only thing better than eliminating defects at compile time is before writing any code at all! Jay describes compelling examples from his experience that really motivate Alloy, TLA+, and the ideas in general.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/FvNRlE4E9QQ&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;h2 id=&quot;videos&quot;&gt;Videos&lt;/h2&gt;
&lt;h3 id=&quot;the-%E2%80%9Creal-programmer%E2%80%9D-myth%2C-maciek-gorywoda&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=8WxNOMqjn5s&quot;&gt;The “real programmer” myth&lt;/a&gt;, Maciek Gorywoda&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Adam: A brilliant exposition of one of the worst parts of our (programming) culture.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/8WxNOMqjn5s&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;h3 id=&quot;nan-gates-and-flip-flops%2C-tom-7&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=5TFDG-y-EHs&quot;&gt;NaN Gates and Flip FLOPS&lt;/a&gt;, Tom 7&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Adam: If you ever feel like you’re too big of a nerd, then this video will let you know that you’re fine, really, you’re just fine. It’s also hilarious.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/5TFDG-y-EHs&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;h2 id=&quot;writing&quot;&gt;Writing&lt;/h2&gt;
&lt;h3 id=&quot;parse%2C-don%E2%80%99t-validate%2C-alexis-king&quot;&gt;&lt;a href=&quot;https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/&quot;&gt;Parse, don’t validate&lt;/a&gt;, Alexis King&lt;/h3&gt;
&lt;p&gt;Definitely our favorite post of the year.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Noel: This blog post clearly articulates a concept I feel is at the core of effectively using types. It gives a name to something I have unconciously understood but have never been able to so plainly and concisely communicate.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We attempted a similar subject at ScalaDays Berlin 2018 entitled &lt;a href=&quot;https://www.youtube.com/watch?v=w7FuQiSi48w&quot;&gt;“Strings are Evil”&lt;/a&gt;, inspired by Changlin Li’s 2018 NEScala talk &lt;a href=&quot;https://www.youtube.com/watch?v=Csj3lzsr0_I&quot;&gt;“Moving Beyond Defensive Programming”&lt;/a&gt;, but Alexis’ post has the best exposition.&lt;/p&gt;
&lt;h3 id=&quot;yes!-and-%E2%80%A6%2C-tom-critchlow&quot;&gt;&lt;a href=&quot;https://tomcritchlow.com/2019/11/18/yes-and/&quot;&gt;Yes! and …&lt;/a&gt;, Tom Critchlow&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Noel: “[F]or knowledge work, the performance of the work is the work” is the line in “Yes! and…”, a blog post inspired by a &lt;a href=&quot;https://www.amazon.com/Impro-Improvisation-Theatre-Keith-Johnstone/dp/0878301178&quot;&gt;book&lt;/a&gt;, that told me I needed to pay attention to this. This strongly resonates with my expererience. It’s not enough to do the work; the work must be seen to have value. The ideas in this post, and its follow ups, are particularly relevant for consultants but apply for anyone who works in any but the smallest organizations.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;using-a-hard-disk-as-a-listening-device%2C-andrew-kwong%2C-wenyuan-xu%2C-and-kevin-fu&quot;&gt;&lt;a href=&quot;https://spqr.eecs.umich.edu/papers/Kwong-HDDphone-IEEE-SP-2019.pdf&quot;&gt;Using a hard disk as a listening device&lt;/a&gt;, Andrew Kwong, Wenyuan Xu, and Kevin Fu&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Mark: It is fun to see how people can use things in unexpected ways.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;other-recommendations&quot;&gt;Other Recommendations&lt;/h2&gt;
&lt;h3 id=&quot;scalabridge-london&quot;&gt;&lt;a href=&quot;https://www.scalabridgelondon.org/&quot;&gt;ScalaBridge London&lt;/a&gt;&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Noel: “Best of” lists often imply passive media consumption, so I wanted to add something that has definitely required active engagement this year. ScalaBridge London aims to increase diversity within the (London) Scala community. Our main way of addressing this is by teaching Scala. We’ve been running for about a year and at least two of our students have been hired as Scala developers in that time. Being involved with ScalaBridge London for me has been a great experience on so many levels. It’s enabled me to get further into teaching and start dipping my toe in sociology, it’s some of the most meaningful work I do, and it’s just plain fun to hang out with fun people.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;lse-podcasts&quot;&gt;&lt;a href=&quot;http://www.lse.ac.uk/lse-player&quot;&gt;LSE Podcasts&lt;/a&gt;&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Noel: I’m unclear how I ever did housework without podcasts. I listen to a lot of podcasts but those from the LSE are the ones that I think about the most. There is fantastic stuff here that helps me better understand the world and myself.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;the-unison-language&quot;&gt;&lt;a href=&quot;https://www.unisonweb.org/docs/tour/&quot;&gt;The Unison language&lt;/a&gt;&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Mark: It has some exciting innovations. Can’t wait to really try this. Watch the videos. &lt;a href=&quot;https://www.youtube.com/watch?v=gCWtkvDQ2ZI&quot;&gt;Here&lt;/a&gt; is one from Strange Loop 2019 but there are others that are good also. Search Youtube.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;creative-coding&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Creative_coding&quot;&gt;Creative Coding&lt;/a&gt;&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Noel: One of the themes of my year was creative coding. Although there are a lot of resources for creative coding I’m not a fan of the imperative approach the majority take, which I think obscures the essential ideas. Instead I’ll link to three books that  address the intersection of maths and art in different ways: &lt;a href=&quot;https://press.princeton.edu/books/hardcover/9780691161730/creating-symmetry&quot;&gt;Creating Symmetry&lt;/a&gt;, &lt;a href=&quot;https://press.princeton.edu/books/hardcover/9780691175331/the-seduction-of-curves&quot;&gt;The Seduction of Curves&lt;/a&gt;, and &lt;a href=&quot;https://press.princeton.edu/books/hardcover/9780691165288/mathematics-and-art&quot;&gt;Mathematics and Art: A Cultural History&lt;/a&gt; (notably all published by Princeton Press).  I got Creating Symmetry a few years back but this is the year when I spent the most time with it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;turtles-in-minecraft&quot;&gt;&lt;a href=&quot;http://www.computercraft.info/wiki/Turtle&quot;&gt;Turtles in Minecraft&lt;/a&gt;&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Mark: These turtles are little robots with a programmable, on-board computer. They can be programmed to perform necessary but tedious work so the player can focus on other activities. The turtles use Lua for reasons I have yet to determine. I think mining and other turtles might be good motivation for learning programming. Hopefully my son and I can dig into this, so to speak, when he returns this summer. These are not from 2019, but I just learned about them this year.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;for-all-the-family&quot;&gt;For all the Family&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Noel: The &lt;a href=&quot;https://www.turingtumble.com/&quot;&gt;Turing Tumble&lt;/a&gt; is elegant and accessible. &lt;a href=&quot;https://twitter.com/Gingerhazing&quot;&gt;Noelle Stevenson&lt;/a&gt; doesn’t just have a great name but tells great stories in comics and cartoons.&lt;/p&gt;
&lt;/blockquote&gt;
</content>
	</entry>
	
	<entry>
		<title>Building Serverless Scala Services with GraalVM</title>
		<link href="https://inner-product.com/posts/serverless-scala-services-with-graalvm/"/>
		<updated>2020-02-06T00:00:00Z</updated>
		<id>https://inner-product.com/posts/serverless-scala-services-with-graalvm/</id>
		<content type="html">&lt;p&gt;A recent project has involved serverless web services in Scala, which led me to investigate using GraalVM’s Native Image tool to create native executables. In this blog post I describe the steps necessary to build a native executable from a simple &lt;a href=&quot;https://http4s.org/&quot;&gt;http4s&lt;/a&gt; web service. There is also &lt;a href=&quot;https://github.com/inner-product/http4s-native-image&quot;&gt;complete code&lt;/a&gt; accompanying this example.&lt;/p&gt;
&lt;h2 id=&quot;why-native-image%3F&quot;&gt;Why Native Image?&lt;/h2&gt;
&lt;p&gt;Before going into the details it’s worth going over a bit of background. &lt;a href=&quot;https://www.graalvm.org/&quot;&gt;GraalVM&lt;/a&gt; is a new Java virtual machine and associated tools that brings increased performance compared to the older Hotspot VM. &lt;a href=&quot;https://www.graalvm.org/docs/reference-manual/native-image/&quot;&gt;Native Image&lt;/a&gt; is one of the most interesting tools that comes with GraalVM. It compiles JVM bytecode into native executables. This promises reduced startup time and decreased memory consumption compared to running on a full JVM. This is particularly attractive for serverless computing as startup time is an issue and billing often increases with memory consumption.&lt;/p&gt;
&lt;h2 id=&quot;overview-of-the-process&quot;&gt;Overview of the Process&lt;/h2&gt;
&lt;p&gt;Building an executable with Native Image is not hard but there is a bit of setup involved the first time. The main steps are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install GraalVM locally (this is optional, but it is faster to test this way);&lt;/li&gt;
&lt;li&gt;Writing code (the fun part!);&lt;/li&gt;
&lt;li&gt;one-off configuration so that GraalVM can handle uses of reflection and &lt;code&gt;Unsafe&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;creating a Docker image to build the executable (optional if you are developing on the same platform you’re deploying to); and&lt;/li&gt;
&lt;li&gt;creating a Docker image to deploy the executable (again, optional).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let’s look at each in turn.&lt;/p&gt;
&lt;h2 id=&quot;installing-graalvm&quot;&gt;Installing GraalVM&lt;/h2&gt;
&lt;p&gt;Installing GraalVM is our local computer is optional (if we go the Docker route, as we do later in this post) but advised as it is much faster to test things locally before invoking Docker. The part of GraalVM we’re interested in, &lt;a href=&quot;https://www.graalvm.org/docs/reference-manual/native-image/&quot;&gt;Native Image&lt;/a&gt;, is an addon to the main distribution. You need to install GraalVM first and then use the tool it provides to add Native Image. I used &lt;a href=&quot;https://sdkman.io/&quot;&gt;sdkman&lt;/a&gt; to install GraalVM, so I can run it alongside Hotspot (I need to continue to use Java 8 to build libraries I maintain.)&lt;/p&gt;
&lt;p&gt;With sdkman installed the magic to install GraalVM is&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Show available JVMs&lt;/span&gt;&lt;br /&gt;sdk list &lt;span class=&quot;token function&quot;&gt;java&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;# Install GraalVM 19.3.1 running on JDK 11&lt;/span&gt;&lt;br /&gt;sdk &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;java&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;19.3&lt;/span&gt;.1.r11-grl &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once you have GraalVM adding Native Image is just a matter of running&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;gu &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; native-image&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Test everything is installed by running &lt;code&gt;native-image --version&lt;/code&gt;. You should see output similar to &lt;code&gt;GraalVM Version 19.3.1 CE&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;writing-the-code&quot;&gt;Writing the Code&lt;/h2&gt;
&lt;p&gt;The next step is to write the code that we’ll compile to a native executable. For my case I wrote a &lt;a href=&quot;https://github.com/inner-product/http4s-native-image&quot;&gt;very simple web service&lt;/a&gt; using &lt;a href=&quot;https://http4s.org/&quot;&gt;http4s&lt;/a&gt; and Scala 2.13.&lt;/p&gt;
&lt;p&gt;From this code we want to generate a JAR file that Native Image will then compile to an executable. In my case I used the &lt;a href=&quot;https://github.com/sbt/sbt-assembly&quot;&gt;sbt-assembly&lt;/a&gt; sbt plugin to generate a single fat JAR. Running the &lt;code&gt;assembly&lt;/code&gt; task in sbt produces a single JAR that I can pass to Native Image with the following command:&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;native-image &lt;span class=&quot;token parameter variable&quot;&gt;--verbose&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-jar&lt;/span&gt; target/scala-2.13/http4s-native-image-assembly-0.1.0-SNAPSHOT.jar ping-server&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;--verbose&lt;/code&gt; flag is not strictly needed but I found it useful for debugging. If you try this using my code it should just work and produce an executable. If you try on your own code, however, you will likely run into some errors because Native Image does not support all the features of the JVM. In the next section I discuss the problems I encoutered and how I fixed them.&lt;/p&gt;
&lt;h2 id=&quot;graalvm-configuration&quot;&gt;GraalVM Configuration&lt;/h2&gt;
&lt;p&gt;This step is perhaps the most important. Native Image has various &lt;a href=&quot;https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md&quot;&gt;limitations&lt;/a&gt; which we must work around. The lack of reflection is likely to be an issue for any sizable application. Scala code does not make much use of reflection. Unfortunately we often rely on Java infrastructure that does use reflection. This is the case for http4s, which relys on a Java logging library that uses reflection. For Scala 2.13 there is also an issue with its use of &lt;code&gt;Unsafe&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To use reflection with Native Image we must tell Native Image ahead of time which classes will be reflected so it can generate the appropriate data. There are various ways to do this but the most modular is to create files called &lt;code&gt;reflect-config.json&lt;/code&gt; in &lt;code&gt;src/main/resources/META-INF/native-image/package-name/project-name&lt;/code&gt; replacing &lt;code&gt;package-name&lt;/code&gt; and &lt;code&gt;project-name&lt;/code&gt; with appropriate names. &lt;a href=&quot;https://github.com/inner-product/http4s-native-image/blob/master/src/main/resources/META-INF/native-image/co.innerproduct/ping/reflect-config.json&quot;&gt;This is my example.&lt;/a&gt; The format is reasonably self explanatory. If we do this we can package our configuration with our code and Native Image will automatically find it. Otherwise we must provide additional command line arguments telling Native Image where to find our configuration.&lt;/p&gt;
&lt;p&gt;We can configure other Native Image settings by creating a file called &lt;code&gt;native-image.properties&lt;/code&gt; in the same location. This is important to get Scala 2.13 working with Native Image. Scala 2.13 has some uses of &lt;code&gt;Unsafe&lt;/code&gt;, encapsulated in &lt;code&gt;scala.runtime.Statics&lt;/code&gt;, that Native Image does not recognise and hence cannot compile. To get around this create a &lt;a href=&quot;https://github.com/inner-product/http4s-native-image/blob/master/src/main/resources/META-INF/native-image/org.scala-lang/scala-lang/native-image.properties&quot;&gt;file&lt;/a&gt; &lt;code&gt;src/main/resources/META-INF/native-image/org.scala-lang/scala-lang/native-image.properties&lt;/code&gt; containing&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Args = --initialize-at-build-time=scala.runtime.Statics$VM
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This initializes the object &lt;code&gt;scala.runtime.Statics&lt;/code&gt; at compile-time and copies the object into the native executable’s heap, getting around the problem.&lt;/p&gt;
&lt;p&gt;We can set other Native Image commands this way, which again allows for an encapsulated and modular build. Here’s what I set for &lt;code&gt;co.innerproduct/ping&lt;/code&gt;, which applies to the code I created.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Args = -H:+ReportExceptionStackTraces --allow-incomplete-classpath --no-fallback
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These parameters, in order, are used to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;report more informative traces in case of errors;&lt;/li&gt;
&lt;li&gt;allow compilation of code that does not contain all classes that are referenced in the code, which is important for uses of reflection; and&lt;/li&gt;
&lt;li&gt;fail the build instead of generating fallback code when Native Image cannot resolve uses of reflection or other issues.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;More information can be in the &lt;a href=&quot;https://www.graalvm.org/docs/reference-manual/native-image/#image-generation-options&quot;&gt;Native Image documentation&lt;/a&gt; and if you use code that makes heavy use of reflection you might want to use the &lt;a href=&quot;https://github.com/oracle/graal/blob/master/substratevm/CONFIGURE.md&quot;&gt;Native Image agent&lt;/a&gt; to automatically generate your configuration.&lt;/p&gt;
&lt;p&gt;A final issue I ran into was a complaint about Janino. I don’t understand what is causing this but adding Janino as a dependency to my project (and hence including it in my fat JAR) solved the issue.&lt;/p&gt;
&lt;p&gt;With all the above in place the code should compile and, several hundreds of thousands of milliseconds later, we’ll have a owkring executable. In my case I can connect to port 8080 with my web browser and see the server is indeed working.&lt;/p&gt;
&lt;h2 id=&quot;cross-building-using-docker&quot;&gt;Cross-Building Using Docker&lt;/h2&gt;
&lt;p&gt;I’m building on a Mac laptop but I want to deploy on Linux. Native Image does not support cross-building, so to build a Linux executable I must run Native Image within a Docker container.&lt;/p&gt;
&lt;p&gt;Oracle provides Docker images with GraalVM, but not with Native Image. It’s a simple matter to create an image with Native Image using the Oracle images as a base. Here’s the &lt;code&gt;Dockerfile&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ARG GRAAL_VERSION=19.3.1-java11
FROM oracle/graalvm-ce:${GRAAL_VERSION}
WORKDIR /opt/native-image
RUN gu install native-image
ENTRYPOINT [&amp;quot;native-image&amp;quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I can build this image using the command, from within the directory that contains the &lt;code&gt;Dockerfile&lt;/code&gt; (which is &lt;code&gt;docker&lt;/code&gt; in my code; this directory contains nothing else to keep the resulting image as small as possible.)&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; build &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; inner-product/graalvm-native-image &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now I can use the image I just made to cross-build a Linux executable. I created a sbt task to do this, so that my task could depend on the local of the fat JAR built by &lt;code&gt;sbt-assembly&lt;/code&gt;. Here’s the code for this task&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;lazy&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; nativeImage &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  taskKey&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;File&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Build a standalone executable using GraalVM Native Image&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;nativeImage &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;sbt&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;Keys&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;streams&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; assemblyFatJar &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; assembly&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; assemblyFatJarPath &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; assemblyFatJar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getParent&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; assemblyFatJarName &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; assemblyFatJar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getName&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; outputPath &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;baseDirectory&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;out&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getAbsolutePath&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; outputName &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;ping-server&quot;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; nativeImageDocker &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;inner-product/graalvm-native-image&quot;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cmd &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&quot;docker run&lt;br /&gt;     | --volume &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;assemblyFatJarPath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;:/opt/assembly&lt;br /&gt;     | --volume &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;outputPath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;:/opt/native-image&lt;br /&gt;     | &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;nativeImageDocker&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&lt;br /&gt;     | --static&lt;br /&gt;     | -jar /opt/assembly/&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;assemblyFatJarName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&lt;br /&gt;     | &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;outputName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;stripMargin&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;filter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_ &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token char&quot;&gt;&#39;\n&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; log &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; streams&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log&lt;br /&gt;  log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;info&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Building native image from &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;assemblyFatJarName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;debug&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cmd&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cmd&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;!&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;log&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; file&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;outputPath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;outputName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Native image command failed:\n &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Exception&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Native image command failed&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now I can simply run the &lt;code&gt;nativeImage&lt;/code&gt; task from within sbt and, after a substantial pause, I will have a Linux executable called &lt;code&gt;ping&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;There’s really only one thing to note about this command: I passed the &lt;code&gt;--static&lt;/code&gt; parameter to Native Image. This creates a statically linked executable that has no dependencies on external libraries. This can be important when deploying as discussed in the next section.&lt;/p&gt;
&lt;h2 id=&quot;creating-a-deployment-docker-image&quot;&gt;Creating a Deployment Docker Image&lt;/h2&gt;
&lt;p&gt;The final step is to create a Docker image in which I can deploy my code. Because I built a statically linked executable I don’t need very much external support, so I can create a very small image. Alpine Linux is a distribution created specifically for the use case of building small Docker images. I can create my Docker image using Alpine Linux and the executable I built earlier with the following Dockerfile.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;FROM alpine:3.11.3
COPY ping-server /opt/ping-server/ping-server
RUN chmod +x /opt/ping-server/ping-server
EXPOSE 8080
ENTRYPOINT [&amp;quot;/opt/ping-server/ping-server&amp;quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We build this image with the following command, assuming the Dockerfile is in the current directory which also contains the executable we previously built.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; build &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; inner-product/ping-server &lt;span class=&quot;token builtin class-name&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can run the resulting image with using the &lt;code&gt;docker run&lt;/code&gt; command.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; run &lt;span class=&quot;token parameter variable&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8080&lt;/span&gt;:8080/tcp inner-product/ping-server&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now visit &lt;code&gt;http://localhost:8080/ping/hello&lt;/code&gt; and you should see a result!&lt;/p&gt;
&lt;p&gt;Finally stop the Docker using the container id printed when you ran it above.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;docker&lt;/span&gt; stop &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;container-id&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;We’ve seen that creating an executable from Scala code using GraalVM Native Image is quite straightforward. There is a bit of one-off cost to get everything setup, but you can copy the work I’ve done and, if you’re just using http4s and other Typelevel projects, it is unlikely you’ll have to do additional configuration. Compilation is not fast, but this is something you could offload to a CI/CD server. The resulting executable is easy to deploy.&lt;/p&gt;
&lt;p&gt;One of the attractions of native executables is faster startup and reduced memory consumption compared to the standard JVM. This, along with deployment, is something I’ll look at in the next in this series.&lt;/p&gt;
&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;
&lt;p&gt;I relied on lots of different resources to get this all working. I’ve linked to documentation above where it is relevant but there were a few other sources I used:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/scala/bug/issues/11634&quot;&gt;This Scala bug report&lt;/a&gt; contained the solution to getting Scala 2.13 working with GraalVM Native Image.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://blog.softwaremill.com/small-fast-docker-images-using-graalvms-native-image-99c0bc92e70b&quot;&gt;This blog post&lt;/a&gt; from SoftwareMill was a useful reference for the Docker part of the puzzle.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Thanks Dale and Adam!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Benchmarking Web Services using GraalVM Native Image</title>
		<link href="https://inner-product.com/posts/benchmarking-graalvm-native-image/"/>
		<updated>2020-02-17T00:00:00Z</updated>
		<id>https://inner-product.com/posts/benchmarking-graalvm-native-image/</id>
		<content type="html">&lt;p&gt;In a &lt;a href=&quot;https://inner-product.com/posts/serverless-scala-services-with-graalvm/&quot;&gt;previous&lt;/a&gt; post I discussed the steps needed to compile a &lt;a href=&quot;https://http4s.org/&quot;&gt;http4s&lt;/a&gt; web service to an executable with &lt;a href=&quot;https://www.graalvm.org/docs/reference-manual/native-image/&quot;&gt;GraalVM Native Image&lt;/a&gt;. It’s natural to ask what tradeoffs are made by going this route instead of using the JVM. In this post I present several benchmark results I obtained comparing the Native Image executable to the running on the JVM. Read on to find out which comes out on top!&lt;/p&gt;
&lt;p&gt;My goal was to compare executables created with GraalVM Native Image with the same code running on the Java 11 JVM with the GraalVM JIT compiler enabled.&lt;/p&gt;
&lt;p&gt;Before we benchmark we need to decide what we are measuring. The nature of the application determines the performance goal we should optimize for. Two main cases arise in serverless computing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;infrequently called services, which prioritize startup time and memory consumption; and&lt;/li&gt;
&lt;li&gt;frequently called services, which prioritize long-term performance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I investigated both cases.&lt;/p&gt;
&lt;p&gt;A quick note about my results. My benchmarking environment (also known as my laptop) doesn’t allow me to control for many sources of noise that can affect performance measurements.  As a result only large performance differences should be considered valid; small differences might be due to factors I didn’t control for. I haven’t run any statistical tests on my data, partly because I wasn’t able to collect suitable data in some cases and partly because I wasn’t interested in the small differences I would need statistical tests to confirm.&lt;/p&gt;
&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;/h2&gt;
&lt;p&gt;My main results are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Native Image has orders of magnitude faster startup time and lower memory consumption than the JVM;&lt;/li&gt;
&lt;li&gt;a warmed up JVM has faster response time than Native Image except in the tails, where the JVM response time is much higher.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also have some additional results from further experiments I ran.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It seems the 10000 requests is sufficient to warm up the JVM for peak performance.&lt;/li&gt;
&lt;li&gt;The tail response times of the JVM seemed to really get out of hand on some runs. I’m not sure what caused this. My guess is garbage collection pauses, or perhaps interference from something else on my system, but I haven’t investigated. Native Image responses in the tail seemed to be much more stable.&lt;/li&gt;
&lt;li&gt;Attempting very simple tuning of the JVM didn’t yield noticable results.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;startup-time-and-memory-consumption&quot;&gt;Startup Time and Memory Consumption&lt;/h2&gt;
&lt;p&gt;Startup time is one of the most important benchmarks for serverless endpoints that are called infrequently. An infrequently used endpoint will not be already running, so the user must wait for it to start before they will receive a response. It is well established that response time is inversely correlated with user satisfaction so we must care about this for any system that interacts with users.&lt;/p&gt;
&lt;p&gt;Memory consumption is another major consideration as all major cloud providers also charge for RAM, usually in 256MB chunks.&lt;/p&gt;
&lt;p&gt;To investigate these issues ran an experiment that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;started a server using the Native Image and the JVM;&lt;/li&gt;
&lt;li&gt;measured the time it took for the server to respond to its first HTTP request; and&lt;/li&gt;
&lt;li&gt;measured memory consumption (resident set size) after this response.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I repeated this process 100 times for each of Native Image and the JVM. The complete process is given by &lt;a href=&quot;https://github.com/inner-product/http4s-native-image/blob/master/startup-benchmark.sh&quot;&gt;this shell script&lt;/a&gt; (this is reproducible research!) Here’s a graph of time to first response.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/img/benchmarking-graal-native-image/startup-time.png&quot; alt=&quot;GraalVM vs Native Image Time to First Response&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Here’s memory consumption after the first response.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/img/benchmarking-graal-native-image/resident-set-size.png&quot; alt=&quot;GraalVM vs Native Image Resident Set Size after First Response&quot; /&gt;&lt;/p&gt;
&lt;p&gt;I’m not confident that my experimental setup will be able to distinguish millisecond differences but this doesn’t matter because &lt;strong&gt;Native Image is orders of magnitude faster to start and uses orders of magnitude less memory&lt;/strong&gt; than the JVM.&lt;/p&gt;
&lt;h2 id=&quot;response-time-under-sustained-load&quot;&gt;Response Time Under Sustained Load&lt;/h2&gt;
&lt;p&gt;Native Image is faster at starting up, but what about response times under load? I expected the JVM would eventually outperform Native Image if the JIT compiler was given enough data to work with. To test this I used the &lt;a href=&quot;https://httpd.apache.org/docs/2.4/programs/ab.html&quot;&gt;ab&lt;/a&gt; load testing tool to send requests to each server. I sent requests in batches of 10000 with a maximum concurrency of 50. The full experimental setup is given in &lt;a href=&quot;https://github.com/inner-product/http4s-native-image/blob/master/sustained-benchmark.sh&quot;&gt;this script&lt;/a&gt;. The graph below shows the zero to 99-percentile response times for Native Image, the cold JVM (the first 10000 requests) and a warm JVM (after 70000 requests).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/img/benchmarking-graal-native-image/lower-sustained-response-time.png&quot; alt=&quot;0 to 99 percentile response times measured over 10000 requests for Native Image, cold JVM, and warm JVM&quot; /&gt;&lt;/p&gt;
&lt;p&gt;This graph shows the 95 to 100-percentile respone times for the same setup.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/img/benchmarking-graal-native-image/upper-sustained-response-time.png&quot; alt=&quot;95 to 100 percentile response times over 10000 requests for Native Image, cold JVM, and warm JVM&quot; /&gt;&lt;/p&gt;
&lt;p&gt;There are a few conclusions I draw from these results:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Native Image continues to outperform the JVM until the JVM is warm;&lt;/li&gt;
&lt;li&gt;a warm JVM has a modest response time improvement over Native Image; except&lt;/li&gt;
&lt;li&gt;the tail response times (roughly 99th-percentile) for the JVM are much worse than Native Image.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In summary Native Image has much more stable performance than the JVM. It’s performance from start is better and it is less affected by large pauses in the tail of the response time distribution.&lt;/p&gt;
&lt;h2 id=&quot;jvm-performance-over-time-and-jvm-tuning&quot;&gt;JVM Performance Over Time and JVM Tuning&lt;/h2&gt;
&lt;p&gt;The results from the previous experiment intrigued me. I decided to investigate two questions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;How many requests does it take to warm up the JVM?&lt;/li&gt;
&lt;li&gt;Can I improve the tail performance of the JVM with some simple tuning?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To answer the first question I recorded percentile response times using ab with the same setup as before, but this time for each block of 10000 requests from the start to 80000 total requests. The results are graphed below. I only graphed up to the 95th percentile response time as I didn’t want the tails to obscure the rest of the data.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/img/benchmarking-graal-native-image/warming-sustained-response-time.png&quot; alt=&quot;0 to 95 percentile response times over batches of 10000 requests&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The results show no large performance increase after 10000 requests. I feel confident that the JVM is warmed up after the first batch of 10000 requests, but I don’t know if I could use fewer requests to achieve the same result. I considered investigating this but decided this was going too far from what I set out to do. It only takes a few seconds to warm up the JVM with my current setup so this isn’t a big issue for me.&lt;/p&gt;
&lt;p&gt;To answer the second questions I tried two different things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;setting a 200ms pause time goal for the &lt;a href=&quot;https://www.oracle.com/technical-resources/articles/java/g1gc.html&quot;&gt;G1GC&lt;/a&gt; garbage collector; and&lt;/li&gt;
&lt;li&gt;changing some compiler settings that Chris Thalinger used to optimize GraalVM services in his &lt;a href=&quot;https://www.youtube.com/watch?v=ldk8CL0fygE&quot;&gt;ScalaDays talk&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Complete settings are given in &lt;a href=&quot;https://github.com/inner-product/http4s-native-image/blob/master/jvm-tuning-benchmark.sh&quot;&gt;the shell script&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here are the results.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/img/benchmarking-graal-native-image/lower-tuned-response-time.png&quot; alt=&quot;0 to 99 percentile response times for various settings of JVM tuning options&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/img/benchmarking-graal-native-image/upper-tuned-response-time.png&quot; alt=&quot;95 to 100 percentile response times for various settings of JVM tuning options&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Although there appears to be a small difference in response time between the various JVM settings I don’t think my experimental setup is sensitive enough to reliably pick up these small effects.&lt;/p&gt;
&lt;h2 id=&quot;conclusions-and-open-questions&quot;&gt;Conclusions and Open Questions&lt;/h2&gt;
&lt;p&gt;Let’s have a quick recap. The result that is best supported by the evidence is that the &lt;strong&gt;executables created by GraalVM Native Image start up much faster and use much less memory than the JVM&lt;/strong&gt;. My experiments have also shown that &lt;strong&gt;Native Image has faster responses time than a cold JVM&lt;/strong&gt; but &lt;strong&gt;a warm JVM has faster response times than Native Image, except above the 98th percentile&lt;/strong&gt;. My conclusion is that &lt;strong&gt;Native Image executables are preferred over the JVM&lt;/strong&gt; for serverless applications, particularly those where startup time is important or they are IO-bound not CPU-bound.&lt;/p&gt;
&lt;p&gt;I also showed that &lt;strong&gt;10000 requests are sufficient to warm up the JVM&lt;/strong&gt; in my case, but I wasn’t able to show any improvements from simple tuning of the JVM.&lt;/p&gt;
&lt;p&gt;There are several open questions. I’m most curious about what is causing the long tail response times I saw on the JVM. The JVM provides plenty of tools to investgate this. My suspicion is it is garbage collection pauses. Using the &lt;code&gt;-XX:+PrintGC&lt;/code&gt; flag would be a simple way to investigate this hypothesis. Improving the pause times, assuming it is GC, is more challenging. My first step here would be trying the &lt;a href=&quot;https://wiki.openjdk.java.net/display/zgc/Main&quot;&gt;ZGC&lt;/a&gt; garbage collector, which is designed to produce pause times less than 10ms. ZGC is not currently available for MacOS, but is available on Linux.&lt;/p&gt;
&lt;p&gt;It is an open question how far my results generalize to other applications. I am reasonably confident that short-lived web services—the typical serverless application—will behave in a similar way to my benchmarks. My expectation is that CPU-bound tasks will benefit from additional optimizations in the JVM and that memory-intensive applications will benefit from the more advanced garbage collectors the JVM brings.&lt;/p&gt;
&lt;p&gt;Finally, everything I’ve done—the code, the experiments, and the analysis—is available in &lt;a href=&quot;https://github.com/inner-product/http4s-native-image/&quot;&gt;the repository&lt;/a&gt; so you can try my experiments with your code or machines. Let me know if you do! I’m very curious to hear how the results translate to different domains.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>A Sketch of Research on Teaching Programming</title>
		<link href="https://inner-product.com/posts/sketch-curriculum-pedagogy/"/>
		<updated>2020-03-13T00:00:00Z</updated>
		<id>https://inner-product.com/posts/sketch-curriculum-pedagogy/</id>
		<content type="html">&lt;p&gt;In preparation for a talk at &lt;a href=&quot;https://nescala.io/&quot;&gt;NEScala&lt;/a&gt; I wrote this quick survey on the research on curriculum and pedagogy for teaching programming (curriculum means what we teach, and pedagogy is how we teach it). My goals are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;give a framework for thinking about curriculum and pedagogy;&lt;/li&gt;
&lt;li&gt;give some specific tips for teaching programming;&lt;/li&gt;
&lt;li&gt;point out where I believe more research is needed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;My survey is based on the published literature, but it’s not a comprehensive document. I’m not an academic and I don’t have the time (sadly) to read all the relevant literature. However I’ve linked to papers I found interesting or important so you can explore further if something grabs your interest. There is a lot of interesting research out there and the tiny bit I’ve read has made my teaching much better.&lt;/p&gt;
&lt;h2 id=&quot;what-do-we-know-about-teaching%3F&quot;&gt;What Do We Know About Teaching?&lt;/h2&gt;
&lt;p&gt;We know a lot about teaching, which has been studied for a very long time. To handle this vast literature we can turn to literature surveys. Perhaps the largest surveys are those by &lt;a href=&quot;https://visible-learning.org/&quot;&gt;John Hattie&lt;/a&gt; which aggregates some 1200 meta-surveys which in total aggregate results over some 300 million students. Hattie ranks different techniques according to effect size. There have been various criticisms of Hattie’s rankings but I don’t believe these criticisms effect the general results, so by looking at the higher ranked methods we can get some good ideas as to effective techniques.&lt;/p&gt;
&lt;p&gt;Here are the top ten techniques according to the 2018 survey:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Collective teacher efficacy&lt;/li&gt;
&lt;li&gt;Self-reported grades&lt;/li&gt;
&lt;li&gt;Teacher estimates of achievement&lt;/li&gt;
&lt;li&gt;Cognitive task analysis&lt;/li&gt;
&lt;li&gt;Response to intervention&lt;/li&gt;
&lt;li&gt;Piagetian programs&lt;/li&gt;
&lt;li&gt;Jigsaw method&lt;/li&gt;
&lt;li&gt;Conceptual change programs&lt;/li&gt;
&lt;li&gt;Prior ability&lt;/li&gt;
&lt;li&gt;Strategy to integrate with prior knowledge&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;There are 122 techniques with effect size at or higher than the average effect size of 0.4. Two questions come to my mind from looking over the list:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Which specific techniques have high effect size and are applicable to teaching programming in my context?&lt;/li&gt;
&lt;li&gt;What are the general priniciples behind the effective techniques?&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We’ll get onto the second question in a bit, when we look at techniques specific to teaching programming. For now let’s look at what effective methods have in common. I see a few broad themes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Explicating and correcting students’ knowledge (e.g. #8 Conceptual change programs, #18 Summarization, any technique using discussion). It’s clear that for learning to take place students need to actively engage with their knowledge. This could be writing notes, completing formative assessment, or writing programs. It’s my experience (and I’m sure the experience of many teachers) that students will believe they know something after you have explained it to them but when they come to actually use their knowledge they discover many holes in it. Anything that requires students to engage with their knowledge will show these holes and give them the opportunity to fix them.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Learning with other students (e.g. #7 Jigsaw method, #15 Classroom discussion, #27 Reciprocal teaching). There are many effective strategies that involve learning with other students. It’s possible that the effectiveness of these methods isn’t due to the social aspect, but because discussion and so on forces them to actively engage with their knowledge, but I believe learning with a social component is more effective than learning alone.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Explicit learning strategies (meta-cognition; e.g. #4 Cognitive task analysis, #14 Transfer strategies, #17 Deliberate practice). There is a lot of implicit knowledge in programming, as in other domains. For example, strategies for moving from problem statement to working code is usually left implicit. Making this knowledge explicit seems to help learning.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now we’ve seen some general principles let’s look specifically at programming.&lt;/p&gt;
&lt;h2 id=&quot;what-do-we-know-about-teaching-programming%3F&quot;&gt;What Do We Know About Teaching Programming?&lt;/h2&gt;
&lt;p&gt;We know a lot less about teaching programming than we do about teaching in general, but we still know a fair bit! The general result seems to be that what we know about learning and teaching transfers to programming. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Peer learning (learning with other students) in various forms works well. There are many different approaches. Some examples include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pair programming. This is a good practice to use: students get some exposure to a way of working they may encounter in industry, and mentors may already be experienced in pair programming and hence less training is required.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://files.software-carpentry.org/training-course/2012/08/porter-halving-fail-peer-instruction-2013.pdf&quot;&gt;Peer instruction&lt;/a&gt; is a structured method of discussion suitable for teaching that predominately uses lectures.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.04174&quot;&gt;Structured peer learning&lt;/a&gt; is a techinque that uses students teachers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;There are many techniques for explicating knowledge in programming. Programming assigments are perhaps the most obvious way to do this, but there are other techniques. We can ask students to trace through programs (e.g. &lt;a href=&quot;http://cs.brown.edu/~sk/Publications/Papers/Published/tfk-eval-trace-rec-subst-nm/paper.pdf&quot;&gt;tracing recursion&lt;/a&gt;) or ask them to solve &lt;a href=&quot;https://www.researchgate.net/profile/Petri_Ihantola/publication/230859647_How_Do_Students_Solve_Parsons_Programming_Problems_-_An_Analysis_of_Interaction_Traces/links/00b49532c151a61b9f000000.pdf&quot;&gt;“Parsons problems”&lt;/a&gt;. All of these methods help students to develop a mental model of how programs work (a so-called “notional machine” that describes some abstract machine that executes the code). Working away from code allows the exercises to focus on particular issues that may be obscured by code, particularly when students are not yet proficient with the syntax.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/pdf/1911.00046.pdf&quot;&gt;Programming strategies&lt;/a&gt; and &lt;a href=&quot;https://htdp.org/&quot;&gt;design recipes&lt;/a&gt; are examples of explicit learning strategies. Giving students explicit strategies allows them to reason about program construction and this seems to be helpful. However this is a fairly new area of study and I think there is much more that can be done here.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are a lot of choices we can make about a curriculum. Usually the choices are dictated in some part by what is needed to get a job (in a bootcamp) or to support later courses (in a University). We have a bit more freedom at &lt;a href=&quot;https://scalabridgelondon.org/&quot;&gt;ScalaBridge London&lt;/a&gt; so what I’ve done is basically walking the line between what I find fun and what I think is best for the students. Briefly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We teach functional programming. Substitution is our computing model of choice.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We focus mostly on problem solving and Scala language features. We teach the FP subset of Scala except implicits, and strategies such as structural recursion. We have much less emphasis on producing large systems and preparing students for employment. This is perhaps not a good thing, given most of our students are interested in jobs as programmers.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One feature of our curriculum is introducing programming by creating pictures. This approach is known as “media computation” (see, for example, &lt;a href=&quot;http://andreaforte.net/ForteGuzdialMotivation.pdf&quot;&gt;this paper&lt;/a&gt; and &lt;a href=&quot;https://cs.brynmawr.edu/~dxu/xu257SIGCSE2018.pdf&quot;&gt;this paper&lt;/a&gt;) in the literature. The evidence suggests this helps increase diversity. Our students certainly seem to find it enjoyable.&lt;/p&gt;
&lt;p&gt;Finally, we should note there is much more to programming than just programming. For example, there is tool use (git, editor or IDE, and so on), workflow, debugging, testing, and so on. We don’t teach this explicitly. Our approach is to teach parts of this implicitly by having experienced developers work with small groups of students. Some parts (e.g. testing) we don’t teach at all.&lt;/p&gt;
&lt;p&gt;Bootcamps also offer interview training. Possibly we should do the same.&lt;/p&gt;
&lt;h2 id=&quot;what-holes-are-there-in-our-knowledge%3F&quot;&gt;What Holes Are There in Our Knowledge?&lt;/h2&gt;
&lt;p&gt;In my opinion there are several gaps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We don’t know enough about teaching and learning functional programming.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We don’t know much about post-college learners, such as bootcamp or ScalaBridge attendees, though the &lt;a href=&quot;https://par.nsf.gov/servlets/purl/10107751&quot;&gt;research literature&lt;/a&gt; is growing.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have two beliefs about functional programing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;it has a much simpler model than alternatives; and&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;it is amenable to systematic program construction.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Substitution (more formally, “the substitution model of evaluation”) is how we reason about functional programs. It is simple to use—we learned how to do this in grade school—and this same simplicity is why experienced FP developers like using it. The same simplicity applies to learning FP. The model is easy to work with, and a minimal FP language (something like Scheme, for example) can be very small.&lt;/p&gt;
&lt;p&gt;Functional programming has an underlying mathematical model that makes it, for example, possible to generate code from type declarations. These same strategies can be used by humans to generate code, and you are a (typed) functional programmer you have possibly had the experience where the code just about writes itself once the types are in place. We can (and do!) teach these strategies explicitly, drawing inspiration from &lt;a href=&quot;https://htdp.org/&quot;&gt;How to Design Programs&lt;/a&gt;. There are other strategies (e.g. the connection between free structures and Church encodings) that are outside the scope of ScalaBridge but we found important in our commercial work.&lt;/p&gt;
&lt;p&gt;Finally we don’t know much about the needs of the people who attend ScalaBridge. My suspicion is that ScalaBridge attendees are very similar to bootcamp attendees, which suggests that we should consider doing some of the things that bootcamps do (interview preparation and portfolio development, for example.)&lt;/p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;p&gt;As well as the references embedded in the post there are a few papers that I found to be good overviews:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;http://cs.brown.edu/~sk/Publications/Papers/Published/kf-prog-paradigms-and-beyond/&quot;&gt;Programming Paradigms and Beyond&lt;/a&gt; is a nice overview of the concept of a notional machine, how it is useful for teaching, and raises some open issues in programming education.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;https://royalsociety.org/-/media/policy/projects/computing-education/literature-review-overview-research-field.pdf&quot;&gt;Computing Education: An Overview of Research in the Field&lt;/a&gt; is a recent (2016) paper that does exactly what the title claims, with a focus on high school education.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally, if you are interested in exploring the literature here a few tips. Firstly, the computer science education field is pretty terrible in general about releasing their papers in an open way. Most of them are locked behind various paywalls. There are two strategies to combat this: searching the wonderful world wide web for a PDF in the wild or contacting the authors directly. Most academics will be delighted (and somewhat shocked) to have interest in their research and will happily send you a PDF. &lt;a href=&quot;https://scholar.google.com/&quot;&gt;Google Scholar&lt;/a&gt; is the best search engine I know of for papers. It will also link you to papers that cite the papers you’re looking at, and the citations from the current paper. This is a great way to quickly explore the literature.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Thoughts on Recent Online-Only Conferences</title>
		<link href="https://inner-product.com/posts/online-only-conference-report/"/>
		<updated>2020-05-05T00:00:00Z</updated>
		<id>https://inner-product.com/posts/online-only-conference-report/</id>
		<content type="html">&lt;p&gt;Noel and I recently spoke at the &lt;a href=&quot;https://nescala.io/&quot;&gt;Northeast Scala Symposium 2020&lt;/a&gt; (a.k.a. NEScala) and &lt;a href=&quot;https://scala.love/conf/&quot;&gt;Scala Love&lt;/a&gt; conferences. What was unusual for us, as both speakers and attendees, was that both these conferences were online-only!&lt;/p&gt;
&lt;p&gt;I’d like to share our thoughts on what worked and didn’t, and what didn’t seem to change at all in this new remote world.&lt;/p&gt;
&lt;h2 id=&quot;march%3A-northeast-scala-symposium-2020&quot;&gt;March: &lt;a href=&quot;https://nescala.io/&quot;&gt;Northeast Scala Symposium 2020&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The Northeast Scala Symposium 2020 was to be hosted in New York City from March 12-14, and consists of three days of single-track conferences: the Typelevel Summit, the main NESCala conference, and finally the NEScala &lt;a href=&quot;https://en.wikipedia.org/wiki/Unconference&quot;&gt;unconference&lt;/a&gt;. In addition, &lt;a href=&quot;https://nescala.io/#sponsors&quot;&gt;Inner Product was a Gold Sponsor&lt;/a&gt;, our first time sponsoring a conference. Why do I say “was to be hosted”? Two days before the conference was set to begin the organizers made the decision that the conference had to convert to online-only, as the coronavirus had started to explode in the United States. I can’t imagine the stress the organizers felt.&lt;/p&gt;
&lt;center&gt;
&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;tl;dr: NE Scala is still on, but we’re going online-only. 1/6&lt;/p&gt;&amp;mdash; Northeast Scala Symposium (@nescalas) &lt;a href=&quot;https://twitter.com/nescalas/status/1237485050234617856?ref_src=twsrc%5Etfw&quot;&gt;March 10, 2020&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async=&quot;&quot; src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/center&gt;
&lt;p&gt;At first, I was upset, since I was scheduled to fly from Seattle to New York the next day and now had to cancel my flight and hotel. But in hindsight, their decision was wise.&lt;/p&gt;
&lt;p&gt;Thursday’s track, the Typelevel Summit, came quickly, and the organizers had a plan. Luckily for me I could immediately be a canary, as I had the first talk! (&lt;em&gt;You Didn’t Know It, but You Really Want to Learn Sequent Calculus&lt;/em&gt;: &lt;a href=&quot;https://t.co/pBAkXeL8Fe?amp=1&quot;&gt;video&lt;/a&gt;, &lt;a href=&quot;https://arosien.github.io/sequentish/slides.html&quot;&gt;slides&lt;/a&gt;)&lt;/p&gt;
&lt;center&gt;
 &lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/wpSMzDiAjmc&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/center&gt;
&lt;p&gt;The talks proceded in Zoom, with questions arriving for speakers via the Zoom Q&amp;amp;A panel, Zoom chat itself, and sometimes in the conference Slack channels. As a sponsor, I especially liked the custom background image supplied by NEScala that speakers were encouraged to display:&lt;/p&gt;
&lt;center&gt;
&lt;img src=&quot;https://camo.githubusercontent.com/566f8915341de5b98332e089f5c8826e5f386479/68747470733a2f2f676973742e6769746875622e636f6d2f7279616e2d77696c6c69616d732f34653266323166613164333439333637346163353263373636613032653633372f7261772f7a6f6f6d2d686f7374732e706e67&quot; width=&quot;30%&quot; /&gt;
&lt;br /&gt;
&lt;small&gt;Speaker view with sponsors background during NEScala. Image stolen from &lt;a href=&quot;https://gist.github.com/ryan-williams/4e2f21fa1d3493674ac52c766a02e637&quot;&gt;Ryan Williams&#39;s NEScala post-mortem&lt;/a&gt;.&lt;/small&gt;
&lt;/center&gt;
&lt;p&gt;Ryan Williams’ &lt;a href=&quot;https://gist.github.com/ryan-williams/4e2f21fa1d3493674ac52c766a02e637&quot;&gt;post-mortem&lt;/a&gt; goes into great detail about running the conference. I encourage anyone interested in more effective conferences to take a look.&lt;/p&gt;
&lt;p&gt;On a sad note, I didn’t attend the Unconference on day 3. I was too tired!&lt;/p&gt;
&lt;p&gt;Of the many talks, these stood out for me:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;implicit def bias = stereotypes |+| prejudices&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/unyagami&quot;&gt;Jeferson David Ossa&lt;/a&gt; (&lt;a href=&quot;https://www.youtube.com/watch?v=dtOvYhuRBjY&quot;&gt;video&lt;/a&gt;, &lt;a href=&quot;https://speakerdeck.com/jedossa/implicit-def-bias-equals-stereotypes-plus-prejudices&quot;&gt;slides&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;An important break-down of our conscious and unconscious biases, and what we can do about it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;em&gt;1000+ Compile-time Errors Later and Running Smoothly in Prod&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/Gentmen&quot;&gt;Kevin Meredith&lt;/a&gt; (&lt;a href=&quot;https://www.youtube.com/watch?v=hXQJChR2ii0&quot;&gt;video&lt;/a&gt;, &lt;a href=&quot;https://docs.google.com/presentation/d/1J-Ok0gMC_E86mwV-I_-GdUmv2YPoAluaUABJkqflZ7U/edit?usp=sharing&quot;&gt;slides&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;A quick ten-minute summary of an upgrade &lt;em&gt;gone right&lt;/em&gt;, benefitting from the solid foundations of static typing and great &lt;a href=&quot;https://typelevel.org/&quot;&gt;Typelevel libraries&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Streams: Your New Favorite Primitive&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/LiquidSloshalot&quot;&gt;Ryan Peters&lt;/a&gt; (&lt;a href=&quot;https://slides.rpeters.dev/fs2-streams/index.html&quot;&gt;slides&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;A superb introduction to streams from a first-time speaker.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Comonads and the Game of Life&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/r_l_mark&quot;&gt;Rebecca Mark&lt;/a&gt; (&lt;a href=&quot;https://speakerdeck.com/rlmark/comonads-and-the-game-of-life&quot;&gt;slides&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Poetics! Automatons! Comonads! This talk has everything, including hidden jokes in the diagrams. A must see!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Bringing Scala to a Diverse Group of Students&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/elissavetvee&quot;&gt;Elissavet Velli&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/noelwelsh&quot;&gt;Noel Welsh&lt;/a&gt;, and &lt;a href=&quot;https://twitter.com/yifan_xing_e&quot;&gt;Yifan Xing&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;A three-person panel about Elissavet’s and Yifan’s path into Scala, with moderation and thoughts by Noel. Real talk, no platitudes. Repeated at Scala Love a month later!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Understanding Scala’s Type System&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/bvenners&quot;&gt;Bill Venners&lt;/a&gt; (&lt;a href=&quot;https://p199.p4.n0.cdn.getcloudapp.com/items/wbumzglK/UnderstandingScalasTypeSystemNEScala2020.pdf?v=12f7d13ccb9e812b17504c9aa19ffdc7&quot;&gt;slides&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;Great explanations and diagrams.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Scala eDSLs for Domain-Specific Business Logic&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/elefthei&quot;&gt;Eleftherios “Lef” Ioannidis&lt;/a&gt; (&lt;a href=&quot;https://github.com/elefthei/nescala2020-edsl&quot;&gt;slides&lt;/a&gt;)
&lt;ul&gt;
&lt;li&gt;The first talk I’ve seen that was live-coding in a notebook!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;april%3A-scala-love&quot;&gt;April: &lt;a href=&quot;https://scala.love/conf/&quot;&gt;Scala Love&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For me, Scala Love appeared out of nowhere. I saw the CFP (Call For Proposals) a while ago, then promptly forgot about it until the selected speakers started being announced. What a line up! &lt;em&gt;Everybody&lt;/em&gt; was speaking, from Martin Odersky, to Scala old-timers, to Scala newcomers, to first-time speakers. It had two main tracks, and then added an extra track, curated and staffed by JetBrains. In a facinating innovation that I hadn’t seen before, it was a mixed &lt;em&gt;time zone&lt;/em&gt; conference to accomodate speaker locality (9am-11pm CEST / 12am-2pm PDT). And it was free!&lt;/p&gt;
&lt;p&gt;With all these positive attributes, it wasn’t a surprise that so many people attended. During Martin’s keynote that came in the middle of the event–afternoon for CEST, early morning for PDT–there were around 550 people watching the Twitch feed! Most talks had over 100 people, which is a lot compared to most in-person conferences!&lt;/p&gt;
&lt;p&gt;Some of my favorite talks from Scala Love:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;A Scala 3 Update&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/odersky&quot;&gt;Martin Odersky&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;Finally convinced me of the goodness of Scala 3. Also, he admitted before the talk he didn’t get to see the other talks because he was busy finishing his slides! He’s just like us!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;center&gt;
&lt;blockquote class=&quot;twitter-tweet&quot; data-partner=&quot;tweetdeck&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;somebody showed up for &lt;a href=&quot;https://twitter.com/scala_love?ref_src=twsrc%5Etfw&quot;&gt;@scala_love&lt;/a&gt;! &lt;a href=&quot;https://twitter.com/odersky?ref_src=twsrc%5Etfw&quot;&gt;@odersky&lt;/a&gt; &lt;a href=&quot;https://t.co/BXxYH5GLow&quot;&gt;pic.twitter.com/BXxYH5GLow&lt;/a&gt;&lt;/p&gt;&amp;mdash; Adam Rosien (@arosien) &lt;a href=&quot;https://twitter.com/arosien/status/1251502696080609282?ref_src=twsrc%5Etfw&quot;&gt;April 18, 2020&lt;/a&gt;&lt;/blockquote&gt;
&lt;/center&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Counts, Combinations, Dice, and D&amp;amp;D&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/runarorama&quot;&gt;Rúnar Bjarnason&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;An &lt;a href=&quot;https://github.com/runarorama/dice&quot;&gt;irresistable subject&lt;/a&gt; to a kid who loved Dungeons and Dragons, but also included some algebraic structures &lt;a href=&quot;https://github.com/runarorama/scala-mset&quot;&gt;related to multisets&lt;/a&gt; that were new to me.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Cats Effect 3: What, When, Why&lt;/em&gt;, &lt;a href=&quot;https://twitter.com/djspiewak&quot;&gt;Daniel Spiewak&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;As a &lt;a href=&quot;https://typelevel.org/cats-effect&quot;&gt;&lt;code&gt;cats-effect&lt;/code&gt;&lt;/a&gt; user who is also &lt;a href=&quot;https://www.inner-product.com/services/training/essential-effects/&quot;&gt;creating a course for effects&lt;/a&gt;, I wanted to know more about the limitations of the current design, and where it is going. It was at the same time as &lt;a href=&quot;https://twitter.com/mpilquist&quot;&gt;Michael Pilquist&lt;/a&gt;’s &lt;em&gt;Scodec for Scala 3&lt;/em&gt;. I wish I could have attended both!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;as-a-speaker&quot;&gt;As a Speaker&lt;/h2&gt;
&lt;p&gt;As a speaker it’s &lt;em&gt;very&lt;/em&gt; difficult to gauge the audience’s reactions when you can’t see or hear them (laugh at your jokes). Speakers need lots of feedback to give engaging and meaningful talks, and most speakers didn’t check in with their audience very often, or at all. We need to explore how to get this signal back to the speakers: encourage more activity in chat, run periodic structured queries to the audience, mass video broadcasting so everyone can see everyone else, and so on.&lt;/p&gt;
&lt;p&gt;At the same time, attendees at an in-person conference have no, or vastly reduced, distractions compared to an online conference. Most online attendees were at home or work, which means you don’t get their full attention as there is an expectation they will do parts of their usual work or home routine. To balance this I think it might be better for online conferences to run shorter days—say 3 hours instead of a whole day—as it is much easier to block out the time for a shorter period.&lt;/p&gt;
&lt;p&gt;Both conferences had per-track moderators that helped speakers set up and get comfortable, and then gave introductions and helped moderate questions during and after the talk. As a speaker, these helpers felt more involved than in-person conferences, probably because they had so much more to deal with: screen-sharing, multiple chat systems, and so on. They did great.&lt;/p&gt;
&lt;h2 id=&quot;as-an-attendee&quot;&gt;As an Attendee&lt;/h2&gt;
&lt;p&gt;As an attendee, the first issue that came up for me after hearing a talk was, “how do I clap?”, to let them know I appreciated their talk. I think everyone immediately had similar thoughts. As an audience we didn’t have many options available, so we started putting clapping emoji (👏) into the chat rooms. I think it worked well, and it was exciting to see the chat start scrolling quickly with clapping everywhere:&lt;/p&gt;
&lt;center&gt;
&lt;a href=&quot;https://camo.githubusercontent.com/346704ca2a405cc357a6699e31a43aed49a9c9a8/68747470733a2f2f676973742e6769746875622e636f6d2f7279616e2d77696c6c69616d732f34653266323166613164333439333637346163353263373636613032653633372f7261772f74616c6b732d6368616e6e656c2e676966&quot;&gt;&lt;img alt=&quot;example chat with applause during NEScala&quot; src=&quot;https://camo.githubusercontent.com/346704ca2a405cc357a6699e31a43aed49a9c9a8/68747470733a2f2f676973742e6769746875622e636f6d2f7279616e2d77696c6c69616d732f34653266323166613164333439333637346163353263373636613032653633372f7261772f74616c6b732d6368616e6e656c2e676966&quot; height=&quot;30%&quot; width=&quot;30%&quot; /&gt;&lt;/a&gt;
&lt;br /&gt;
&lt;small&gt;Example chat with applause during NEScala. Image stolen from &lt;a href=&quot;https://gist.github.com/ryan-williams/4e2f21fa1d3493674ac52c766a02e637&quot;&gt;Ryan Williams&#39;s NEScala post-mortem&lt;/a&gt;.&lt;/small&gt;
&lt;/center&gt;
&lt;p&gt;Secondly, in a “normal” conference setting there is time, and most importantly, &lt;em&gt;space&lt;/em&gt;, between talks for people to meet and talk. Conferences had breakout rooms, but I think we, as the attendees, didn’t know how to use them. Maybe we need more structure imposed on us, since we don’t have the physical cues available, with facilitators to keep the conversations flowing?&lt;/p&gt;
&lt;p&gt;Last, a bit of a minor benefit of being online: I liked that the attendees could closely experience the pre-talk setup time between the organizers and speakers, instead of merely becoming introduced to the speaker when their talk began. It humanizes the difficult tasks of a conference, and helps the audience connect with the people making it happen.&lt;/p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;Online-only conferences &lt;em&gt;will&lt;/em&gt; be the new normal, and they can be as good, or even better in some regards, than the traditional in-person form. For example, in a world where speakers don’t have to travel, conferences have access to so many more wonderful and talented people. And conferences can be much larger for the same reason, no one has to travel in order to attend.&lt;/p&gt;
&lt;p&gt;I didn’t mention it earlier, but the organizers, moderators and helpers for both NEScala and Scala Love were fantastic! Everyone was very responsive across all the various pieces of software we were using. After the conference, I was happy to see many thank-you’s to them on social media.&lt;/p&gt;
&lt;p&gt;Let us know what you think about our new world!&lt;/p&gt;
&lt;h2 id=&quot;appendix%3A-conference-details-to-compare-and-contrast&quot;&gt;Appendix: Conference Details to Compare and Contrast&lt;/h2&gt;
&lt;p&gt;NEScala:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CFP&lt;/strong&gt;: community-curated (attendees voted on the submitted talks)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Duration&lt;/strong&gt;: 3 days&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tracks&lt;/strong&gt;: 1&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cost&lt;/strong&gt;: $120&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Video&lt;/strong&gt;: Zoom&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Chat&lt;/strong&gt;: Slack, Zoom&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Scala Love:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CFP&lt;/strong&gt;: organizer-curated&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Duration&lt;/strong&gt;: 1 day (14 hours!)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tracks&lt;/strong&gt;: 2 + 1 (extra track managed by JetBrains)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cost&lt;/strong&gt;: $0&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Video&lt;/strong&gt;: Zoom (registered attendees), Twitch (public)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Chat&lt;/strong&gt;: Slack, Zoom, Twitch&lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<title>Functional Programming Demystification at Seattle&#39;s Scala Meetup</title>
		<link href="https://inner-product.com/posts/scala-at-the-sea-may-2020/"/>
		<updated>2020-05-20T00:00:00Z</updated>
		<id>https://inner-product.com/posts/scala-at-the-sea-may-2020/</id>
		<content type="html">&lt;p&gt;&lt;a href=&quot;https://www.meetup.com/Seattle-Scala-User-Group/events/frbgdrybchbqb/&quot;&gt;Last night&lt;/a&gt; &lt;a href=&quot;https://twitter.com/arosien&quot;&gt;I&lt;/a&gt; was the speaker at Seattle’s Scala meetup, &lt;a href=&quot;https://www.meetup.com/Seattle-Scala-User-Group/&quot;&gt;Scala at the Sea&lt;/a&gt;. The topic was “Functional Programming Demystification”:&lt;/p&gt;
&lt;div class=&quot;ml-4&quot;&gt;
Functional programming is full of Fancy Words&amp;trade;. Let&#39;s collect a list of them to demystify together. Adam will facilitate with live-coding, and will also try to make good jokes.
&lt;p&gt;Here’s some terms to bootstrap us: &lt;em&gt;effect&lt;/em&gt;, &lt;em&gt;free monad&lt;/em&gt;, &lt;em&gt;functor&lt;/em&gt;, &lt;em&gt;property-based testing&lt;/em&gt;, &lt;em&gt;refinement type&lt;/em&gt;, &lt;em&gt;stream&lt;/em&gt;, etc.&lt;/p&gt;
&lt;p&gt;We will have plenty of time for Q&amp;amp;A during this session.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;I also shared our newly available public course, &lt;a href=&quot;https://www.inner-product.com/services/training/essential-effects/&quot;&gt;Essential Effects&lt;/a&gt;, with the group.&lt;/p&gt;
&lt;p&gt;We didn’t get to all of the terms we collected, but we did cover &lt;em&gt;functor&lt;/em&gt;, &lt;em&gt;monad&lt;/em&gt;, and &lt;em&gt;effects&lt;/em&gt;, with a side-topic about typeclasses, before we ran out of time.&lt;/p&gt;
&lt;p&gt;I’ve posted &lt;a href=&quot;https://gist.github.com/arosien/76af9e989d5c9dd53a24112ffb16c38e&quot;&gt;my notes and code&lt;/a&gt; on Github.&lt;/p&gt;
&lt;p&gt;We’ve moved online via Zoom for our monthly gatherings, and it was good to reconnect with everyone there. However, as a speaker it is really difficult to talk to an audience you can’t see, and having only Q&amp;amp;A and text-based chat to interact is very frustrating. I hope we as a community can find more interactive ways of gathering together. We need to at least see each other’s faces!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Addendum to Alex N&#39;s blog post on Scala warnings</title>
		<link href="https://inner-product.com/posts/nowarn-addendum/"/>
		<updated>2020-05-29T00:00:00Z</updated>
		<id>https://inner-product.com/posts/nowarn-addendum/</id>
		<content type="html">&lt;p&gt;Alexandru Nedelcu recently wrote &lt;a href=&quot;https://alexn.org/blog/2020/05/26/scala-fatal-warnings.html&quot;&gt;Fatal Warnings and Linting in Scala&lt;/a&gt;, where amongst other things, I learned about the new &lt;a href=&quot;https://www.scala-lang.org/api/2.13.2/scala/annotation/nowarn.html&quot;&gt;&lt;code&gt;@nowarn&lt;/code&gt; annotation&lt;/a&gt; in Scala 2.13, which allows you to selectively disable compiler warnings.&lt;/p&gt;
&lt;p&gt;Of course, you be shouldn’t be locally suppressing compiler warnings very often. Why might you do this?&lt;/p&gt;
&lt;p&gt;In my case, I’m writing exercises for our &lt;a href=&quot;https://www.inner-product.com/services/training/essential-effects/&quot;&gt;upcoming “Essential Effects” course&lt;/a&gt; (sorry, I couldn’t resist the plug!). When writing exercises, you want to provide a good code skeleton, so the student can concentrate on the essential details of the problem. I usually mark the the sections the student needs to implement with the &lt;code&gt;???&lt;/code&gt; method. For example:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;effect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; HelloWorld &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; IOApp &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;args&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ExitCode&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    helloWorld&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ExitCode&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Success&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; helloWorld&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// TODO: print &quot;Hello World&quot; to the console&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our friend the compiler helpfully reminds us &lt;code&gt;dead code following this construct&lt;/code&gt; regarding the line with the &lt;code&gt;???&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But I don’t want my students to have these warnings, because they become errors, because I’ve also enabled &lt;code&gt;-Xfatal-warnings&lt;/code&gt;, which turns all compiler warnings into errors. Alex’s article goes into more detail about why you should also enable &lt;code&gt;-Xfatal-warnings&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So it’s a bit of a conundrum: we don’t want warnings, because they are probably errors, but we now have errors–due to warnings–that we don’t want to bother the student with.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;@nowarn&lt;/code&gt; annotation let’s us locally suppress the warning:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;effect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; HelloWorld &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; IOApp &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;args&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ExitCode&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    helloWorld&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ExitCode&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Success&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token annotation punctuation&quot;&gt;@annotation.nowarn&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; helloWorld&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// TODO: print &quot;Hello World&quot; to the console&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But what if our diligent student completes the exercise? Now there aren’t actually any compiler warnings, but the code is marked with &lt;code&gt;@nowarn&lt;/code&gt;. In my mind, the programmer should be notified of this, but unfortunately, there is no warning about this condition by default! We wouldn’t want any future revisions of this code, which might introduce warnings, to not have those warnings surfaced.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;effect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; HelloWorld &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; IOApp &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;args&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ExitCode&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    helloWorld&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ExitCode&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Success&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token annotation punctuation&quot;&gt;@annotation.nowarn&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// There&#39;s no warning being suppressed!&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; helloWorld&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To avoid this situation, you need to enable the &lt;code&gt;-Wunused:nowarn&lt;/code&gt; compiler flag. Once added, if there exist any &lt;code&gt;@nowarn&lt;/code&gt; annotations, but no warnings were actually suppressed, you’ll see this warning from the compiler: &lt;code&gt;@nowarn annotation does not suppress any warnings&lt;/code&gt;. Now the programmer can remove the annotation. Huzzah!&lt;/p&gt;
&lt;p&gt;In summary, if you’re on Scala 2.13 and are using &lt;code&gt;@nowarn&lt;/code&gt;, please enable the &lt;code&gt;-Wunused:nowarn&lt;/code&gt; compiler flag!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>What Functional Programming Is, What it Isn&#39;t, and Why it Matters</title>
		<link href="https://inner-product.com/posts/fp-what-and-why/"/>
		<updated>2020-06-10T00:00:00Z</updated>
		<id>https://inner-product.com/posts/fp-what-and-why/</id>
		<content type="html">&lt;p&gt;The programming world is moving towards functional programming (FP). More developers are using languages with an explicit bias towards FP, such as Scala and Haskell, while object-oriented (OO) languages and their communities adopt FP features and practices. (A striking example of the latter is the rise of Typescript and React in the Javascript community.) So what is FP and what does it mean to write code in a functional style? It’s common to view functional programming as a collection of language features, such as first class functions, or to define it as a programming style using immutable data and pure functions. (Pure functions always return the same output given the same input.) This was my view when I started down the FP route, but I now believe the true goals of FP are enabling local reasoning and composition. Language features and programming style are in service of these goals. In this post I attempt to explain the meaning and value of local reasoning and composition.&lt;/p&gt;
&lt;h2 id=&quot;what-functional-programming-is&quot;&gt;What Functional Programming Is&lt;/h2&gt;
&lt;p&gt;I believe that functional programming is a hypothesis about software quality: that software that can be understood before it is run and is built of small reusable components is easier to write and maintain. The first property is known as local reasoning, and the second as composition. Let’s address each in turn.&lt;/p&gt;
&lt;p&gt;Local reasoning means we can understand pieces of code in isolation. When we see the expression &lt;code&gt;1 + 1&lt;/code&gt; we know what it means regardless of the weather, the database, or the current status of our Kubernetes cluster. None of these external events can change it. This is a trivial and slightly silly example, but it illustrates a point. A goal of functional programming is to extend this ability across our code base.&lt;/p&gt;
&lt;p&gt;It can help to understand local reasoning by looking at what it is not. Shared mutable state is out because relying on shared state means that other code can change what our code does without our knowledge. It means no global mutable configuration, as found in many web frameworks and graphics libraries for example, as any random code can change that configuration. Metaprogramming has to be carefully controlled. No &lt;a href=&quot;https://en.wikipedia.org/wiki/Monkey_patch&quot;&gt;monkey patching&lt;/a&gt;, for example, as again it allows other code to change our code in non-obvious ways. As we can see, adapting code to enable local reasoning can mean quite some sweeping changes. However if we work in a language that embraces functional programming this style of programming is the default.&lt;/p&gt;
&lt;p&gt;Composition means building big things out of smaller things. Numbers are compositional. We can take any number and add one, giving us a new number. Lego is also compositional. We compose Lego by sticking it together. In the particular sense we’re using composition we also require the original elements we combine don’t change in any way when they are composed. When we create by &lt;code&gt;2&lt;/code&gt; by adding &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; we get a new result but we don’t change what &lt;code&gt;1&lt;/code&gt; means.&lt;/p&gt;
&lt;p&gt;We can find compositional ways to model common programming tasks once we start looking for them. React components are one example familiar to many front-end developers: a component can consist of many components. HTTP routes can be modelled in a compositional way. A route is a function from an HTTP request to a handler function or a value indicating the route did not match. We can combine routes as a logical or: try this route or, if it doesn’t match, try this other route. Processing pipelines are another example that often use sequential composition: perform this pipeline stage and then this other pipeline stage.&lt;/p&gt;
&lt;h3 id=&quot;types&quot;&gt;Types&lt;/h3&gt;
&lt;p&gt;Types are not strictly part of functional programming but statically typed FP is the most popular form of FP and sufficiently important to warrant a mention. Types help compilers generate efficient code but types in FP are as much for the programmer as they are the compiler. Types express properties of programs, and the type checker automatically ensures that these properties hold. They can tell us, for example, what a function accepts and what it returns, or that a value is optional. We can also use types to express our beliefs about a program and the type checker will tell us if those beliefs are incorrect. For example, we can use types to tell the compiler we do not expect an error at a particular point in our code and the type checker will let us know if have made an incorrect assumption. In this way types are another tool for reasoning about code.&lt;/p&gt;
&lt;p&gt;Type systems push programs towards particular designs, as to work effectively with the type checker requires designing code in a way the type checker can understand. As modern type systems come to other languages they naturally tend to shift programmers in those languages towards a FP style of coding.&lt;/p&gt;
&lt;h2 id=&quot;what-functional-programming-isn%E2%80%99t&quot;&gt;What Functional Programming Isn’t&lt;/h2&gt;
&lt;p&gt;In my view functional programming is not about immutability, or keeping to “the substitution model of evaluation”, and so on. These are tools in service of the goals of enabling local reasoning and composition, but they are not the goals themselves. Code that is immutable always allows local reasoning, for example, but it is not necessary to avoid mutation to still have local reasoning. Here is an example of summing a collection of numbers. First we have the code in Typescript:&lt;/p&gt;
&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;number&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; total &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;    numbers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; total &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; total &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; total&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here’s the same function in Scala:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;numbers&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; total &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;br /&gt;  numbers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foreach&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; total &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; total &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  total&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In both implementations we mutate &lt;code&gt;total&lt;/code&gt;. This is ok though! We cannot tell from the outside that this is done, and therefore all users of &lt;code&gt;sum&lt;/code&gt; can still use local reasoning. Inside &lt;code&gt;sum&lt;/code&gt; we have to be careful when we reason about &lt;code&gt;total&lt;/code&gt; but this block of code is small enough that it shouldn’t cause any problems.&lt;/p&gt;
&lt;p&gt;In this case we can reason about our code despite the mutation, but neither the Typescript nor the Scala compiler can determine that this is ok. Both languages allow mutation but it’s up to us to use it appropriately. A more expressive type system, perhaps with features like Rust’s, would be able to tell that &lt;code&gt;sum&lt;/code&gt; doesn’t allow mutation to be observed by other parts of the system&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://inner-product.com/posts/fp-what-and-why/#fn1&quot; id=&quot;fnref1&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt;. Another approach, which is the one taken by Haskell, is to disallow all mutation and thus guarantee it cannot cause problems.&lt;/p&gt;
&lt;p&gt;Mutation also interferes with composition. For example, if a value relies on internal state then composing it may produce unexpected results. Consider generators in Javascript. They maintain internal state that is used to generate the next value. If we have two generators we might want to combine them into one generator that yields values from the two inputs. Here’s the code in Typescript:&lt;/p&gt;
&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;type Gen&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Generator&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; never&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;infinite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Gen&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;number&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; index&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; zip&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;left&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Gen&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;right&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Gen&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;b&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Gen&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;zipIt&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; l &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; left&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; right&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;l&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;done &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;done&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;l&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; result&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;zipIt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works if we pass two distinct generators to &lt;code&gt;zip&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token function&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;infinite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;infinite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [0, 0]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However if we pass the same generator twice we get a surprising result.&lt;/p&gt;
&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; inf &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;infinite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token function&quot;&gt;zip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;inf&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; inf&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [0, 1]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The usual functional programming solution is to avoid mutable state but we can envisage other possibilities. For example, an &lt;a href=&quot;https://en.wikipedia.org/wiki/Effect_system&quot;&gt;effect tracking system&lt;/a&gt; would allow us to avoid combining two generators that use the same memory region. These systems are still research projects, however.&lt;/p&gt;
&lt;p&gt;So in my opinion immutability (and purity, referential transparency, and no doubt more fancy words that I have forgotten) have become associated with functional programming because they guarantee local reasoning and composition, and until recently we didn’t have the language tools to automatically distinguish safe uses of mutation from those that cause problems. Restricting ourselves to immutability is the easiest way to ensure the desirable properties of functional programming, but as languages evolve this might come to be regarded as a historical artifact.&lt;/p&gt;
&lt;h2 id=&quot;why-it-matters&quot;&gt;Why It Matters&lt;/h2&gt;
&lt;p&gt;I have described local reasoning and composition but have not discussed their benefits. Why are they are desirable? The answer is that they make efficient use of knowledge. Let me expand on this.&lt;/p&gt;
&lt;p&gt;We care about local reasoning because it allows our ability to understand code to scale with the size of the code base. We can understand module A and module B in isolation, and our understanding does not change when we bring them together in the same program. By definition if both A and B allow local reasoning there is no way that B (or any other code) can change our understanding of A, and vice versa. If we don’t have local reasoning every new line of code can force us to revisit the rest of the code base to understand what has changed. This means it becomes exponentially harder to understand code as it grows in size as the number of interactions (and hence possible behaviours) grows exponentially. We can say that local reasoning is compositional. Our understanding of module A calling module B is just our understanding of A, our understanding of B, and whatever calls A makes to B.&lt;/p&gt;
&lt;p&gt;We introduced numbers and Lego as examples of composition. They have an interesting property in common: the operations that we can use to combine them (for example, addition, subtraction, and so on for numbers; for Lego the operation is “sticking bricks together”) give us back the same kind of thing. A number multiplied by a number is a number. Two bits of Lego stuck together is still Lego. This property is called closure: when you combine things you end up with the same kind of thing. Closure means you can apply the combining operations (sometimes called combinators) an arbitrary number of times. No matter how many times you add one to a number you still have a number and can still add or subtract or multiply or…you get the idea. If we understand module A, and the combinators that A provides are closed, we can build very complex structures using A without having to learn new concepts! This is also one reason functional programmers tend to like abstractions such a monads (beyond liking fancy words): they allow us to use one mental model in lots of different contexts.&lt;/p&gt;
&lt;p&gt;In a sense local reasoning and composition are two sides of the same coin. Local reasoning is compositional; composition allows local reasoning. Both make code easier to understand.&lt;/p&gt;
&lt;h2 id=&quot;the-evidence-for-functional-programming&quot;&gt;The Evidence for Functional Programming&lt;/h2&gt;
&lt;p&gt;I’ve made arguments in favour of functional programming and I admit I am biased—I do believe it is a better way to develop code than imperative programming. However, is there any evidence to back up my claim? There has not been much research on the effectiveness of functional programming, but there has been a reasonable amount done on static typing. I feel static typing, particularly using modern type systems, serves as a good proxy for functional programming so let’s look at the evidence there.&lt;/p&gt;
&lt;p&gt;In the corners of the Internet I frequent the common refrain is that &lt;a href=&quot;https://danluu.com/empirical-pl/&quot;&gt;static typing has neglible effect on productivity&lt;/a&gt;. I decided to look into this and was surprised that the majority of the results I found support the claim that static typing increases productivity. For example, the literature review in &lt;a href=&quot;https://web.cs.unlv.edu/stefika/documents/MerlinDissertation.pdf&quot;&gt;this dissertation&lt;/a&gt; (section 2.3, p16–19) shows a majority of results in favour of static typing, in particular the most recent studies. However the majority of these studies are very small and use relatively inexperienced developers—which is noted in the review by Dan Luu that I linked. My belief is that functional programming comes into its own on larger systems. Furthermore, programming languages, like all tools, require proficiency to use effectively. I’m not convinced very junior developers have sufficient skill to demonstrate a significant difference between languages.&lt;/p&gt;
&lt;p&gt;To me the most useful evidence of the effectiveness of functional programming is that industry is adopting functional programming en masse. Consider, say, the widespread and growing adoption of Typescript and React. If we are to argue that FP as embodied by Typescript or React has no value we are also arguing that the thousands of Javascript developers who have switched to using them are deluded. At some point this argument becomes untenable.&lt;/p&gt;
&lt;p&gt;This doesn’t mean we’ll all be using Haskell in five years. More likely we’ll see something like the shift to object-oriented programming of the nineties: Smalltalk was the paradigmatic example of OO, but it was more familiar languages like C++ and Java that brought OO to the mainstream. In the case of FP this probably means languages like Scala, Swift, Kotlin, or Rust, and mainstream languages like Javascript and Java continuing to adopt more FP features.&lt;/p&gt;
&lt;h2 id=&quot;final-words&quot;&gt;Final Words&lt;/h2&gt;
&lt;p&gt;I’ve given my opinion on functional programming—that the real goals are local reasoning and composition, and programming practices like immutability are in service of these. Other people may disagree with this definition, and that’s ok. Words are defined by the community that uses them, and meanings change over time.&lt;/p&gt;
&lt;p&gt;Functional programming emphasises formal reasoning, and there are some implications that I want to briefly touch on. Later articles may expand on these points.&lt;/p&gt;
&lt;p&gt;Firstly, I find that FP is most valuable in the large. For a small system it is possible to keep all the details in our head. It’s when a program becomes too large for anyone to understand all of it that local reasoning really shows its value. This is not to say that FP should not be used for small projects, but rather that if you are, say, switching from an imperative style of programming you shouldn’t expect to see the benefit when working on toy projects.&lt;/p&gt;
&lt;p&gt;The formal models that underlie functional programming allow systematic construction of code. This is in some ways the reverse of reasoning: instead of taking code and deriving properties and start from some properties and derive code. This sounds very academic but is in fact very practical and how I, for example, develop most of my code.&lt;/p&gt;
&lt;p&gt;Finally, reasoning is not the only way to understand code. It’s valuable to the limitations of reasoning, other methods for gaining understanding, and using a variety of strategies depending on the situation.&lt;/p&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;The example I gave is fairly simple. A compiler that used &lt;a href=&quot;https://en.wikipedia.org/wiki/Escape_analysis&quot;&gt;escape analysis&lt;/a&gt; could recognize that no reference to &lt;code&gt;total&lt;/code&gt; is possible outside &lt;code&gt;sum&lt;/code&gt; and hence &lt;code&gt;sum&lt;/code&gt; is pure (or referentially transparent). Escape analysis is a well studied technique. In the general case the problem is a lot harder. We’d often like to know that a value is only referenced once at various points in our program, and hence we can mutate that value without changes being observable in other parts of the program. This might be used, for example, to pass an accumulator through various processing stages. To do this requires a programming language with what is called a &lt;a href=&quot;https://en.wikipedia.org/wiki/Substructural_type_system&quot;&gt;substructural type system&lt;/a&gt;. Rust has such a system, with affine types. Linear types are in development for Haskell. &lt;a href=&quot;https://inner-product.com/posts/fp-what-and-why/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
	</entry>
	
	<entry>
		<title>Scoring Ten-pin Bowling with Algebraic Data and Finite State Machines</title>
		<link href="https://inner-product.com/posts/bowling-in-fp/"/>
		<updated>2020-06-18T00:00:00Z</updated>
		<id>https://inner-product.com/posts/bowling-in-fp/</id>
		<content type="html">&lt;p&gt;I recently led a training session where we implemented the &lt;a href=&quot;https://github.com/davegurnell/bowling-case-study/&quot;&gt;rules for scoring ten-pin bowling&lt;/a&gt; in Scala.  It makes for a good case study. It’s small enough that you can pick up the rules in a few minutes, but the dependencies between frames makes calculating the score non-trivial. I decided to implement &lt;a href=&quot;https://github.com/noelwelsh/bowling-case-study/&quot;&gt;my own solution&lt;/a&gt; which turned into an interesting exercise in algebraic data and finite state machines. In this post I’ll describe my implementation and my process for developing it.&lt;/p&gt;
&lt;p&gt;For my implementation I solely focused on scoring the game. I didn’t implement any parsing code, as that part of the problem didn’t interest me.&lt;/p&gt;
&lt;!--more--&gt;
&lt;h2 id=&quot;the-data&quot;&gt;The Data&lt;/h2&gt;
&lt;p&gt;The core of my approach is getting the data structure right. Once they’re in place the rest of the code is relatively straightforward. This approach relies on some foundational features of functional programming, namely algebraic data types and structural recursion. Lets have a quick diversion into these topics.&lt;/p&gt;
&lt;h3 id=&quot;algebraic-data-types-and-structural-recursion&quot;&gt;Algebraic Data Types and Structural Recursion&lt;/h3&gt;
&lt;p&gt;“Algebraic data type” is a fancy phrase that functional programmers use to refer to data that is modelled in terms of logical ands and logical ors. Here are some examples:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a &lt;code&gt;User&lt;/code&gt; is a name &lt;em&gt;and&lt;/em&gt; an email address &lt;em&gt;and&lt;/em&gt; a password;&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;Result&lt;/code&gt; is a success &lt;em&gt;or&lt;/em&gt; a failure;&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;List&lt;/code&gt; of &lt;code&gt;A&lt;/code&gt; is:
&lt;ul&gt;
&lt;li&gt;the empty list (conventionally called nil) &lt;em&gt;or&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;a pair (conventionally called cons) containing a head of type &lt;code&gt;A&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; a tail of type &lt;code&gt;List&lt;/code&gt; of &lt;code&gt;A&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If a language has support for algebraic data types, once we have a description of data such as the examples above we can directly translate it into code. Let’s use the example of the list as it is the most complex.&lt;/p&gt;
&lt;p&gt;Here’s how we can define it in Scala.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Nil&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Cons&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here’s the same thing in Typescript.&lt;/p&gt;
&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;type Nil&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;nil&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;type Cons&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;cons&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;tail&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;type List&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Nil&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Cons&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Nil &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;nil&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; Cons &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;tail&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;cons&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;tail&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; tail &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here’s Rust.&lt;/p&gt;
&lt;pre class=&quot;language-rust&quot;&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token class-name&quot;&gt;Nil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token class-name&quot;&gt;Cons&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each language requires some language specific knowledge in the implementation. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;in Scala we can choose between covariance and invariance;&lt;/li&gt;
&lt;li&gt;in Typescript we need to define constructors seperately;&lt;/li&gt;
&lt;li&gt;in Rust we must wrap recursion in a &lt;code&gt;Box&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The general concept, however, applies to all these languages and we can transfer knowledge from one language to another. To avoid writing all the code three times for the rest of this post I’ll be sticking to Scala.&lt;/p&gt;
&lt;p&gt;Given an algebraic data type we can implement &lt;em&gt;any&lt;/em&gt; transformation on that type using structural recursion (also known as a fold or a catamorphism)&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://inner-product.com/posts/bowling-in-fp/#fn1&quot; id=&quot;fnref1&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt;. The rules, informally, for structural recursion are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;each case (logical or) in the data must have a case in the structural recursion; and&lt;/li&gt;
&lt;li&gt;if the data we are processing is recursive the function we are defining must be recursive at the same place.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Structural recursion cannot solve everything for us—we must add problem-specific code to fill out the implementation—but it gives us a substantial help.&lt;/p&gt;
&lt;p&gt;Here’s one way we could write the structural recursion skeleton for a list in Scala.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; transform&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; SomeResultType &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Nil&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Problem specific&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Cons&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Problem specific but *must* include the recursion transform(t)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we want to calculate the length of a list we can start with the skeleton&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Nil&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Problem specific&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Cons&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Problem specific but *must* include the recursion length(t)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and fill out the problem specific parts&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Nil&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Cons&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;h&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;br /&gt;      &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any (yes, really, any) other method we can write that transforms a list to something else (or even another list) is going to have the same skeleton. So in summary, all we have to do is work out how to model our data using logical ands and ors and then we immediately get for free:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the representation of that data in code; and&lt;/li&gt;
&lt;li&gt;a generic template for transforming that data into anything.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Diversion over! Let’s get back to bowling.&lt;/p&gt;
&lt;h2 id=&quot;bowling-as-an-algebraic-data-type&quot;&gt;Bowling as an Algebraic Data Type&lt;/h2&gt;
&lt;p&gt;From reading the rules of bowling we can pull out a reasonably simple structure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A game consists of 10 frames&lt;/li&gt;
&lt;li&gt;Each frame can be a strike, a spare, or an open frame where
&lt;ul&gt;
&lt;li&gt;an open frame is two rolls that sum to less than ten;&lt;/li&gt;
&lt;li&gt;a spare is one roll that is less than ten (the second rule is implied by the first roll); and&lt;/li&gt;
&lt;li&gt;a strike doesn’t need any additional information.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is the model I started with but as I worked on it I realised the true model is more complicated because the final frame may have up to two bonus rolls. Hence I changed the model to&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A game consists of 9 frames and 1 final frame&lt;/li&gt;
&lt;li&gt;A frame can be a strike, a spare, or an open frame where
&lt;ul&gt;
&lt;li&gt;an open frame is two rolls that sum to less than ten;&lt;/li&gt;
&lt;li&gt;a spare is one roll that is less than ten (the second roll is implied by the first roll); and&lt;/li&gt;
&lt;li&gt;a strike doesn’t need any additional information.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A final frame can be a strike, a spare, or an open frame which have the same definition as above and also
&lt;ul&gt;
&lt;li&gt;a spare final frame has one bonus roll; and&lt;/li&gt;
&lt;li&gt;a strike final frame has two bonus rolls.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These definitions fit the criteria for an algebraic data type (they consist of logical ands and ors) and therefore translate to code in a straightforward way. Rather than paste a big lump of code I’ll just link to &lt;a href=&quot;https://github.com/noelwelsh/bowling-case-study/blob/master/src/main/scala/code/Frame.scala&quot;&gt;Frame&lt;/a&gt;, &lt;a href=&quot;https://github.com/noelwelsh/bowling-case-study/blob/master/src/main/scala/code/FinalFrame.scala&quot;&gt;FinalFrame&lt;/a&gt;, and &lt;a href=&quot;https://github.com/noelwelsh/bowling-case-study/blob/master/src/main/scala/code/Game.scala&quot;&gt;Game&lt;/a&gt; in the code repository. Note that not all the invariants can be expressed in the type system. For example, we cannot express the criteria that the rolls in an open frame must sum to less than 10. The problem specification says we only need to consider valid data, but I put some dynamic checks in the “smart constructors” on the companion objects. This turned out to be useful as it caught some errors in my tests (which I’ll talk about in a bit.)&lt;/p&gt;
&lt;p&gt;Now that we have defined the data we just need to write a structural recursion over the &lt;code&gt;Game&lt;/code&gt; type. Well, not quite. The scoring rules have dependencies between frames. For example, if a frame is a strike the next two rolls are added to the score for that frame. We need to keep around the information about pending frames—frames that have yet to be scored—while we process the data.&lt;/p&gt;
&lt;p&gt;Reading through the rules we can extract the following.&lt;/p&gt;
&lt;p&gt;The pending frames can be&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a strike;&lt;/li&gt;
&lt;li&gt;a spare; or&lt;/li&gt;
&lt;li&gt;a strike and a strike .&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is another algebraic data type.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Pending&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Strike &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Pending&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Spare &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Pending&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; StrikeAndStrike &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Pending&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we score a frame in a game we must calculate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the score for this frame if it is not pending futures frames;&lt;/li&gt;
&lt;li&gt;the score for any pending frames that are now complete; and&lt;/li&gt;
&lt;li&gt;the pending frames after this frame.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this way the scoring algorithm is a finite state machine (FSM). The pending frames are the current state of the FSM, the current frame is the output, and we output the next state (the updated pending frames) in addition to a score.&lt;/p&gt;
&lt;p&gt;It’s useful to wrap the &lt;code&gt;Pending&lt;/code&gt; information up with the score calculated so far, which gives all the information to calculate the total score so far. I called this &lt;code&gt;State&lt;/code&gt;. Note that &lt;code&gt;Pending&lt;/code&gt; is wrapped in an &lt;code&gt;Option&lt;/code&gt;; there may be no frames for which the score is pending.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; State&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;    score&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;    pending&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Pending&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; next&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;additionalScore&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newPending&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Pending&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; State &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;copy&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;      score &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; score &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; additionalScore&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;      pending &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newPending&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this definition the scoring function has type &lt;code&gt;(State, Frame) =&amp;gt; State&lt;/code&gt;, which is exactly the type of the transition function of a FSM. We can calculate the score of a &lt;code&gt;List[Frame]&lt;/code&gt; by passing this function as the second argument to &lt;code&gt;foldLeft&lt;/code&gt;, with the initial state forming the first argument. In code this is&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;frames&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foldLeft&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;initialState&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;transitionFunction&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The transition function, the scoring algorithm, is a structural recursion over the &lt;code&gt;Frame&lt;/code&gt; as well as the &lt;code&gt;State&lt;/code&gt;. &lt;a href=&quot;https://github.com/noelwelsh/bowling-case-study/blob/master/src/main/scala/code/Game.scala#L7-L105&quot;&gt;The code is lengthy&lt;/a&gt;, but it isn’t hard to write and a good deal of it is generated by the IDE (in my case, Metals with Doom Emacs.)&lt;/p&gt;
&lt;h3 id=&quot;testing&quot;&gt;Testing&lt;/h3&gt;
&lt;p&gt;Testing was important. The scoring rules aren’t amenable to much support from the type system (though now I think about it I could have expressed the rules in a different way that would have given me more compiler support) which means testing is the next best way to ensure the code is correct. This is an excellent application for property-based testing, for which I used &lt;a href=&quot;http://scalacheck.org/&quot;&gt;ScalaCheck&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I defined a few different &lt;a href=&quot;https://github.com/noelwelsh/bowling-case-study/blob/master/src/test/scala/code/Generators.scala&quot;&gt;generators&lt;/a&gt; for the various types of frames. For example here is how I generate open frames.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; genOpen&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Gen&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Frame&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    misses &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; Gen&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;choose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    hits &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; misses&lt;br /&gt;    roll1 &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; Gen&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;choose&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; hits&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    roll2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; hits &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; roll1&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; Frame&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;roll1&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; roll2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These generators enabled me to &lt;a href=&quot;https://github.com/noelwelsh/bowling-case-study/blob/master/src/test/scala/code/GameSpec.scala&quot;&gt;test&lt;/a&gt; both the examples given in the instructions and examples generated at random. I found quite a few errors with these tests, both in my scoring algorithm and in how I was generating data. Luckily they were all very easy to diagnose. As the scoring algorithm was very explicit it was easy to work out what I had done wrong (which was usually forgetting to include a roll somewhere).&lt;/p&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;I hope this article has given an insight in how I approached this case study. In summary there are three important components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the core of my approach is to model the data correctly, as I know once I have the data model in place almost all of the rest of the code follows from it;&lt;/li&gt;
&lt;li&gt;recognising the scoring algorithm was a finite state machine was another insight I needed to model it cleanly; and&lt;/li&gt;
&lt;li&gt;using property-based testing allowed me to achieve a high degree of confidence in my implementation without a great deal of effort.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I have presented my process as if I moved straight from problem to implementation. This was &lt;em&gt;not&lt;/em&gt; the case. It was a highly iterative process, and I changed the data model at least three times as I came to better understand the problem. I also interleaved developing the tests with the code under test.&lt;/p&gt;
&lt;p&gt;Of course my approach is the only one. There is a write-up of a &lt;a href=&quot;https://ronjeffries.com/xprog/articles/acsbowling/&quot;&gt;TDD approach in C#&lt;/a&gt; which may make an interesting contrast to mine.&lt;/p&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Although this is well known in programming language theory I haven’t been able to find a reference that has a chance of being comprehensible to the average programmer. I think the first place to state this result is &lt;a href=&quot;https://www.sciencedirect.com/science/article/pii/0167642390900237&quot;&gt;Data Structures and Program Transformation&lt;/a&gt;, but this uses the Bird-Meertens formalism which I find very hard to read. &lt;a href=&quot;https://www.cs.nott.ac.uk/~pszgmh/fold.pdf&quot;&gt;A tutorial on the universality and expressiveness of fold&lt;/a&gt; only considers folds on list, but uses Haskell and more standard mathematical notation. I imagine this is still quite obscure for most but it is an improvement! &lt;a href=&quot;https://inner-product.com/posts/bowling-in-fp/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
	</entry>
	
	<entry>
		<title>Techniques for Understanding Code</title>
		<link href="https://inner-product.com/posts/techniques-for-understanding-code/"/>
		<updated>2020-07-07T00:00:00Z</updated>
		<id>https://inner-product.com/posts/techniques-for-understanding-code/</id>
		<content type="html">&lt;p&gt;Building an understanding of code is one of the main tasks in software development. Whenever we want to answer a question about code—what is this doing? why doesn’t it work? how can we make it faster?—this is what we’re doing. I have found it valuable to consciously surface the strategy I use to answer these questions, and have categorized my approaches into three groups:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;reasoning about code;&lt;/li&gt;
&lt;li&gt;inspecting running code; and&lt;/li&gt;
&lt;li&gt;referring to an authoritative source.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In this post I will discuss these different ways of understanding code and their benefits and drawbacks.&lt;/p&gt;
&lt;h2 id=&quot;three-ways-to-understand-code&quot;&gt;Three Ways to Understand Code&lt;/h2&gt;
&lt;p&gt;Let’s start by describing the three methods for understanding code.&lt;/p&gt;
&lt;h3 id=&quot;reasoning&quot;&gt;Reasoning&lt;/h3&gt;
&lt;p&gt;Reasoning means applying logic to some model of a programming language’s semantics. This sounds very formal, and that can be the case using say, an operational or denotational semantics. However most reasoning is informal. I’m sure the majority of programmers can reason about code (quick check: what does the program &lt;code&gt;1 + 1&lt;/code&gt; evaluate to?) but would be hard pressed to specify the model and inference rules they use.&lt;/p&gt;
&lt;p&gt;Reasoning takes place at many levels. For example, when I reason about code I might work at a low level that is easily reducible to a formal semantics. More often I work at a higher level, where I’m thinking about, say, transformations on algebraic data types instead of expressions and values.&lt;/p&gt;
&lt;p&gt;Regardless of how it is done, reasoning requires a model and rules for inference.&lt;/p&gt;
&lt;h3 id=&quot;observation&quot;&gt;Observation&lt;/h3&gt;
&lt;p&gt;Another way we can understand code is by observing its behavior as it runs. There are many ways to do this. Just running the program and looking at its output is probably most common, but other methods include using a debugger, inspecting logs, or runnning tests.&lt;/p&gt;
&lt;h3 id=&quot;appeal-to-authority&quot;&gt;Appeal to Authority&lt;/h3&gt;
&lt;p&gt;A final way we can understand code is by turning to a trusted source. For most programmers this means the searching the Internet, perhaps using a site like Stack Overflow or a specialist forum or mailing list. It can also mean consulting a colleague or even, as a last resort, reading the fine manual.&lt;/p&gt;
&lt;h2 id=&quot;the-advantages-of-reasoning&quot;&gt;The Advantages of Reasoning&lt;/h2&gt;
&lt;p&gt;Of the three methods my preference is to use reasoning. Reasoning can make statements that hold for all possible program runs. Let’s use the following code example to discuss this further. First the code is shown in Typescript.&lt;/p&gt;
&lt;pre class=&quot;language-javascript&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;shout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;phrase&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; string &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;phrase&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now here it is using Scala.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; shout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;phrase&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;phrase&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toUpperCase&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;!&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally an example of using it.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;shout&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello, reader&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// Returns HELLO, READER!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here are some of the properties of this code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the string returned by &lt;code&gt;shout&lt;/code&gt; will always end with an exclamation mark!&lt;/li&gt;
&lt;li&gt;the code cannot fail unless passed &lt;code&gt;null&lt;/code&gt; (or &lt;code&gt;undefined&lt;/code&gt; in the case of Typescript.)&lt;/li&gt;
&lt;li&gt;if the input is all in upper case the output will be one character longer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are other properties we could define but hopefully the above are sufficient to give you an idea of what I mean. We can tell these properties are true without running the code and they hold for all possible execution—an infinite number of cases.&lt;/p&gt;
&lt;p&gt;Neither observation nor appealing to authority can prove statements about programs. Observation can only tell us about the properties of the program when run with the particular input it is given. If we see that the output of &lt;code&gt;shout(&amp;quot;Hello, reader&amp;quot;)&lt;/code&gt; is &lt;code&gt;&amp;quot;HELLO, READER!&amp;quot;&lt;/code&gt; then we might guess as to what &lt;code&gt;shout&lt;/code&gt; is doing. More observations can increase our confidence. However we cannot ever be certain that we are correct by observation alone. Appealing to authority—perhaps by reading the documentation for the &lt;code&gt;shout&lt;/code&gt; function—may describe what the function does but that description could be incorrect. The problem with trust is that it, well, relies on trust. Particularly when trusting randos on the Internet we must be cautious.&lt;/p&gt;
&lt;p&gt;I find reasoning more efficient than other methods. If I have a good model of the domain I can reason my way out of most problems with just a little thinking. Observation requires I run a program, which usually takes more to setup, and consulting others requires I interrupt a colleague or trawl through hundreds of Internet search results.&lt;/p&gt;
&lt;p&gt;Reasoning is also amenable to automation. The most accessible form of automated reasoning is probably type checking but linters and similar tools are other examples. Although there are systems that can construct tests they are not anywhere near as widely available as type systems. We might consider code reviews a kind of automated code review, but the feedback loop is much slower.&lt;/p&gt;
&lt;h2 id=&quot;the-limit-of-reasoning&quot;&gt;The Limit of Reasoning&lt;/h2&gt;
&lt;p&gt;There are theoretical and practical limits to the power of reasoning.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Halting_problem&quot;&gt;The halting problem&lt;/a&gt; demonstrates fundamental limits to the power of reasoning. There are some properties that we simply cannot prove for all programs. (The &lt;a href=&quot;https://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorems&quot;&gt;incompleteness theorems&lt;/a&gt; express the same idea from a mathematical perspective.) However this is not usually what causes problems in practice. In my (admittedly limited) experience, for the working programmer the limits more often come from the cost of reasoning, reasoning across system boundaries, and issues with assumptions built into models. Let’s address each of these in turn.&lt;/p&gt;
&lt;p&gt;The first issue is perhaps the most important: reasoning can be just too expensive. The cost is usually that of learning. Most reasoning we do is informal, and the cost here is in acquiring a reliable mental model. All of us have limits on what we have time to learn and must choose to specialise to an extent. When reasoning formally we may already have a model but even then most of us do not have expertise to use a formal model efficiently. Some systems are too complex to reasonably build a model for. Attempting to build a cycle accurate CPU model, for example, is likely to be wasted effort. It’s much simpler to measure actual program performance.&lt;/p&gt;
&lt;p&gt;System boundaries also limit our ability to reason. We can only formally reason up to the boundaries of our system; when we interact with the outside world all bets are off. I imagine many developers have had the experience of interacting with a web service that doesn’t adhere to its own specification. It doesn’t matter what the documentation says, and it doesn’t matter how we represent remote systems with types in our code; if the real world is different we must adapt. The only way we can reliably determine a remote system’s behavior is by interacting with it and seeing what happens.&lt;/p&gt;
&lt;p&gt;Finally, all formal models are built on assumptions. For example, when we say that the program &lt;code&gt;1 + 1&lt;/code&gt; always evaluates to &lt;code&gt;2&lt;/code&gt; we are making assumptions such as: arithmetic won’t overflow, we aren’t going to run into &lt;a href=&quot;https://en.wikipedia.org/wiki/Pentium_FDIV_bug&quot;&gt;CPU bugs&lt;/a&gt;, and we don’t have to worry about &lt;a href=&quot;https://en.wikipedia.org/wiki/Soft_error#Cosmic_rays_creating_energetic_neutrons_and_protons&quot;&gt;cosmic rays&lt;/a&gt;. Usually this is fine, but there are occasions where it is not. It is up to us to decide when our assumptions should be challenged.&lt;/p&gt;
&lt;h2 id=&quot;combining-reasoning%2C-observation%2C-and-trust&quot;&gt;Combining Reasoning, Observation, and Trust&lt;/h2&gt;
&lt;p&gt;I’ve presented reasoning, observation, and appeal to authority as alternatives but the truth is that they are complimentary. For example, we can take observations as a starting point for reasoning, and usually when debugging this is what we do. We can use reasoning to suggest optimizations that we then confirm with actual performance measurements. We implicitly rely on trust even in formal reasoning: trust that the model we work with is correct, the tools we are using are free from bugs, and those who taught us to reason did a good job. In my experience the skill is in realising which combination of techniques is appropriate in a given situation. Let me give two examples.&lt;/p&gt;
&lt;p&gt;I don’t fully understand the Scala &lt;a href=&quot;https://www.scala-sbt.org/&quot;&gt;sbt&lt;/a&gt; build tool. When I do have to work with sbt I know I’m going to have to rely on reading documentation and trial and error—which is to say appeal to authority and observation. If my goal is to work with a plugin I’ll usually rely on that plugin’s documentation. If I’m working with something core to sbt I’ll go straight to the sbt documentation. I won’t usually search the web as a first choice because I don’t find it particularly reliable. One non-goal is developing a complete mental model of sbt. I don’t mind if this happens but I don’t have to modify my builds often enough that I think this worthwhile. Hence my reading is usually very task oriented—how do I do this?—versus understanding why things work the way they do.&lt;/p&gt;
&lt;p&gt;In contrast I’ve recently been learning React and Typescript (hence the Typescript examples in recent posts.) Here my goal is to build a mental model. To this end I have read through most of the documentation with a focus on conceptual material. When I encounter a surprise when programming I actively try to inspect my mental model to see how it should be revised. This slows me down to start with but the time I spend learning is paid back every time I use the model I’ve learned.&lt;/p&gt;
&lt;p&gt;Finally, I’ve noticed that some programmers have an over reliance on a particular technique for understanding, usually searching the web. I think it is important to build reliable mental models in our core skills, so if you are the type of person who jumps onto Google whenever you encounter a problem try instead to reason about it first, and then think how you need to adjust your mental model so you understand the cause of the error. I believe it will make you a better programmer in the long run.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>What is an Effect?</title>
		<link href="https://inner-product.com/posts/what-is-an-effect/"/>
		<updated>2020-07-20T00:00:00Z</updated>
		<id>https://inner-product.com/posts/what-is-an-effect/</id>
		<content type="html">&lt;p&gt;&lt;em&gt;The following post is an excerpt of the first chapter of the companion book to our new course &lt;a href=&quot;https://inner-product.com/services/training/essential-effects&quot;&gt;Essential Effects&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We often use the term &lt;em&gt;effect&lt;/em&gt; when talking about the behavior of our code, like “What is the &lt;em&gt;effect&lt;/em&gt; of that operation?” or, when debugging, “Doing that shouldn’t have an &lt;em&gt;effect&lt;/em&gt;, what’s going on?”, where “what’s going on?” is most likely replaced with an expletive. But what is an effect? Can we talk about effects in precise ways, in order to write better programs that we can better understand?&lt;/p&gt;
&lt;p&gt;To explore what effects are, and how we can leverage them, we’ll distinguish two aspects of code: computing values and interacting with the environment. At the same time, we’ll talk about how transparent, or not, our code can be in describing these aspects, and what we as programmers can do about it.&lt;/p&gt;
&lt;h2 id=&quot;the-substitution-model-of-evaluation&quot;&gt;The Substitution Model of Evaluation&lt;/h2&gt;
&lt;p&gt;Let’s start with the first aspect, computing values. As a programmer, we write some code, say a method, and it computes a value that gets returned to the caller of that method:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; plusOne&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;  i &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; plusOne&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;plusOne&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here are some of the things we can say about this code:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;plusOne&lt;/code&gt; is a method that takes an &lt;code&gt;Int&lt;/code&gt; argument and produces an &lt;code&gt;Int&lt;/code&gt; value. We often talk about the type signature, or just signature, of a method. &lt;code&gt;plusOne&lt;/code&gt; has the type signature &lt;code&gt;Int =&amp;gt; Int&lt;/code&gt;, pronounced &amp;quot;&lt;em&gt;&lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Int&lt;/code&gt;&lt;/em&gt;&amp;quot; or &amp;quot;&lt;em&gt;&lt;code&gt;plusOne&lt;/code&gt; is a function from &lt;code&gt;Int&lt;/code&gt; to &lt;code&gt;Int&lt;/code&gt;&lt;/em&gt;&amp;quot;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;x&lt;/code&gt; is a &lt;em&gt;value&lt;/em&gt;. It is defined as the result of &lt;em&gt;evaluating&lt;/em&gt; the &lt;em&gt;expression&lt;/em&gt; &lt;code&gt;plusOne(plusOne(12))&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let’s use substitution to evaluate this code. We start with the expression &lt;code&gt;plusOne(plusOne(12))&lt;/code&gt; and substituting each (sub-)expression with its definition, recursively repeating until there are no more sub-expressions:&lt;/p&gt;
&lt;p&gt;TIP: We’re displaying the substitution process as a “diff” you might see in a code review. The original expression is prefixed with &lt;code&gt;-&lt;/code&gt;, and the result of substitution is prefixed with &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Replace the inner &lt;code&gt;plusOne(12)&lt;/code&gt; with its definition:&lt;/p&gt;
&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = plusOne(plusOne(12))&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = plusOne(12 + 1))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Replace &lt;code&gt;12 + 1&lt;/code&gt; with &lt;code&gt;13&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = plusOne(12 + 1))&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = plusOne(13))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Replace &lt;code&gt;plusOne(13)&lt;/code&gt; with its definition:&lt;/p&gt;
&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = plusOne(13))&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = 13 + 1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Replace &lt;code&gt;13 + 1&lt;/code&gt; with &lt;code&gt;14&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = 13 + 1&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = 14&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It is important to notice some particular properties of this example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;To understand what &lt;code&gt;plusOne&lt;/code&gt; does, you &lt;em&gt;don’t&lt;/em&gt; have to look anywhere except the (literal) definition of &lt;code&gt;plusOne&lt;/code&gt;. There are no references to anything outside of it. This is sometimes referred to as &lt;em&gt;local reasoning&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Under substitution, programs mean the same thing if they evaluate to the same value. &lt;code&gt;13 + 1&lt;/code&gt; &lt;em&gt;means&lt;/em&gt; exactly the same thing as &lt;code&gt;14&lt;/code&gt;. So does &lt;code&gt;plusOne(12 + 1)&lt;/code&gt;, or even &lt;code&gt;(12 + 1) + 1&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To quote myself while teaching an introductory course on functional programming, “[substitution] is so stupid, even a computer can do it!”. It would be fantastic if all programs were as self-contained as &lt;code&gt;plusOne&lt;/code&gt;, so we humans could use substitution, just like the machine, to evaluate code. But not all code is compatible with substitution.&lt;/p&gt;
&lt;h3 id=&quot;when-does-substitution-break-down%3F&quot;&gt;When does substitution break down?&lt;/h3&gt;
&lt;p&gt;Take a minute to think of some examples of expressions where substitution doesn’t work. What about them breaks substitution?&lt;/p&gt;
&lt;p&gt;Here are a few examples you might have thought of:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;When printing to the console.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;println&lt;/code&gt; function prints a string to the console, and has the return type &lt;code&gt;Unit&lt;/code&gt;. If we apply substitution,&lt;/p&gt;
&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = println(&quot;Hello world!&quot;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = ()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;the meaning–the &lt;em&gt;effect&lt;/em&gt;–of the first expression is very different from the second expression. Nothing is printed in the latter. Using substitution doesn’t do what we intend.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;When reading values from the outside world.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If we apply substitution,&lt;/p&gt;
&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val name = readLine&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val name = &amp;lt;whatever you typed in the console&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;name&lt;/code&gt; evaluates to whatever &lt;em&gt;particular&lt;/em&gt; string was read from the console, but that particular string is &lt;em&gt;not&lt;/em&gt; the same as the evaluation of the expression &lt;code&gt;readLine&lt;/code&gt;. The expression &lt;code&gt;readLine&lt;/code&gt; could evaluate to something else.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;When expressions refer to mutable variables.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If we interact with mutable variables, the value of an expression depends any possible change to the variable. In the following example, if any code changes the value of &lt;code&gt;i&lt;/code&gt;, then that would change the evaluation of &lt;code&gt;x&lt;/code&gt; as well.&lt;/p&gt;
&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;var i = 12&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = { i += 1; i }&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val x = 13&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(This example is very similar to the previous one.)&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;dealing-with-side-effects&quot;&gt;Dealing with Side-effects&lt;/h2&gt;
&lt;p&gt;The second aspect of effects, after computing values, is interacting with the environment. And as we’ve seen, this can break substitution. Environments can change, they are non-deterministic, so expressions involving them do not necessarily evaluate to the same value. If we use mutable state, if we perform hidden side-effects–if we break substitution–is all lost? Not at all.&lt;/p&gt;
&lt;p&gt;One way we can maintain the ability to reason about code is to localize the “impure” code that breaks substitution. To the outside world, the code will look–and evaluate–as if substitution is taking place. But inside the boundary, there be dragons:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ints&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; sum &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  ints&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foreach&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; sum &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  sum&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;We’ve used a mutable variable. The horrors! But nothing outside of &lt;code&gt;sum&lt;/code&gt; can ever affect it. Its existence is localized to a single invocation.&lt;/li&gt;
&lt;li&gt;When we evaluate the expression that uses &lt;code&gt;sum&lt;/code&gt;, we get a deterministic answer. Substitution works at this level.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We’ve optimized, in a debatable way, code to compute the sum of a list, so instead of using an immutable fold over the list we’re updating a local variable. From the caller’s point to view, substitution is maintained. Within the impure code, we can’t leverage the reasoning that substitution gives us, so to prove to ourselves the code behaved we’d have to use other techniques that are outside the scope of this book.&lt;/p&gt;
&lt;p&gt;Localization is a nice trick, but won’t work for everything that breaks substitution. We need side-effects to actually do something in our programs, but side-effects are unsafe! What can we do?&lt;/p&gt;
&lt;h2 id=&quot;the-effect-pattern&quot;&gt;The Effect Pattern&lt;/h2&gt;
&lt;p&gt;If we impose some conditions, we can tame the side-effects into something safer; we’ll call these &lt;strong&gt;effects&lt;/strong&gt;. There are two parts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The type of the program should tell us what kind of effects the program will perform, in addition to the type of the value it will produce.&lt;/strong&gt;&lt;br /&gt;
One problem with impure code is we can’t &lt;em&gt;see&lt;/em&gt; that it is impure! From the outside it looks like a method or block of code. By giving the effect a type we can distinguish it from other code. At the same time, we continue to track the type of the result of the computation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;If the behavior we want relies upon some externally-visible side-effect, we separate describing the effects we want to happen from actually making them happen. We can freely substitute the description of effects up until the point we run them.&lt;/strong&gt;&lt;br /&gt;
This idea is exactly the same as the localization idea, except that instead of performing the side-effect at the &lt;em&gt;innermost&lt;/em&gt; layer of code and hiding it from the outer layers, we delay the the side-effect so it executes &lt;em&gt;outside&lt;/em&gt; of any evaluation, ensuring substitution still holds within.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We’ll call these conditions the &lt;em&gt;Effect Pattern&lt;/em&gt;, and apply it to studying and describing the effects we use every day, and to new kinds of effects.&lt;/p&gt;
&lt;h3 id=&quot;example%3A-is-option-an-effect%3F&quot;&gt;Example: Is &lt;code&gt;Option&lt;/code&gt; an Effect?&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;Option&lt;/code&gt; type represents the &lt;em&gt;optionality&lt;/em&gt; of a value: we have some value, or we have none. In Scala it is encoded as an &lt;em&gt;algebraic data type&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Some&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; None &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Nothing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is &lt;code&gt;Option[A]&lt;/code&gt; an effect? Let’s check the criteria:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Does &lt;code&gt;Option[A]&lt;/code&gt; tell us what kind of effects the program will perform, in addition to the type of the value it will produce?&lt;/strong&gt;&lt;br /&gt;
&lt;em&gt;Yes&lt;/em&gt;: if we have a value of type &lt;code&gt;Option[A]&lt;/code&gt;, we know the effect is optionality from the name &lt;code&gt;Option&lt;/code&gt;, and we know it may produce a value of type &lt;code&gt;A&lt;/code&gt; from the type parameter &lt;code&gt;A&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Are externally-visible side-effects required?&lt;/strong&gt;&lt;br /&gt;
&lt;em&gt;Not really&lt;/em&gt;. The &lt;code&gt;Option&lt;/code&gt; algebraic data type is an interface representing optionality that maintains substitution. We can replace a method call with its implementation and the meaning of the program won’t change.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;There is one exception–pun intended–where an externally-visible side-effect might occur:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; a&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; None &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; NoSuchElementException&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;None.get&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Calling &lt;code&gt;get&lt;/code&gt; on a &lt;code&gt;None&lt;/code&gt; is a programmer error, and raises an exception which in turn may result in a stack trace being printed. However this side-effect is not core to the concept of exceptions, it is just the implementation of the default exception handler. The essence of exceptions is non-local control flow: a jump to an exception handler in the dynamic scope. This is not an externally-visible side effect.&lt;/p&gt;
&lt;p&gt;With these two criteria satisfied, we can say &lt;em&gt;yes&lt;/em&gt;, &lt;code&gt;Option[A]&lt;/code&gt; is an effect!&lt;/p&gt;
&lt;p&gt;It may seem strange to call &lt;code&gt;Option&lt;/code&gt; an effect since it doesn’t perform any side-effects. The point of the first condition of the Effect Pattern is that the type should make the presence of an effect &lt;em&gt;visible&lt;/em&gt;. A traditional alternative to &lt;code&gt;Option&lt;/code&gt; would be to use a &lt;code&gt;null&lt;/code&gt; value, but then how could you tell that a value of type &lt;code&gt;A&lt;/code&gt; could be &lt;code&gt;null&lt;/code&gt; or not? Some types which could have a &lt;code&gt;null&lt;/code&gt; value are not intended to have the concept of a missing value. &lt;code&gt;Option&lt;/code&gt; makes this distinction apparent.&lt;/p&gt;
&lt;h3 id=&quot;example%3A-is-future-an-effect%3F&quot;&gt;Example: Is &lt;code&gt;Future&lt;/code&gt; an Effect?&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;Future&lt;/code&gt; is known to have issues that aren’t easily seen. For example, look at this code, where we reference the same &lt;code&gt;Future&lt;/code&gt; to run it twice:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; print &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Future&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; twice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  print&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_ &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; print&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What output is produced?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Hello World!
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is only printed once! Why is that?&lt;/p&gt;
&lt;p&gt;The reason is that the &lt;code&gt;Future&lt;/code&gt; is scheduled to be run immediately upon construction. So the side-effect will happen (almost) immediately, even when other “descriptive” operations–the subsequent &lt;code&gt;print&lt;/code&gt; in the &lt;code&gt;flatMap&lt;/code&gt;—happen later. That is, we describe performing &lt;code&gt;print&lt;/code&gt; twice, but the side-effect is only executed once!&lt;/p&gt;
&lt;p&gt;Compare this to what happens when we substitute the definition of &lt;code&gt;print&lt;/code&gt; into &lt;code&gt;twice&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Replace the first reference to &lt;code&gt;print&lt;/code&gt; with its definition:&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token unchanged&quot;&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val print = Future(println(&quot;Hello World!&quot;))&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val twice =&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt;   print&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt;   Future(println(&quot;Hello World!&quot;))&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged&quot;&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token line&quot;&gt;     .flatMap(_ =&gt; print)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Replace the second reference to &lt;code&gt;print&lt;/code&gt; with its definition, and remove the definition of &lt;code&gt;print&lt;/code&gt; since it has been inlined.&lt;pre class=&quot;language-diff&quot;&gt;&lt;code class=&quot;language-diff&quot;&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val print = Future(println(&quot;Hello World!&quot;))&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token unchanged&quot;&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token line&quot;&gt; val twice =&lt;br /&gt;&lt;/span&gt;&lt;span class=&quot;token prefix unchanged&quot;&gt; &lt;/span&gt;&lt;span class=&quot;token line&quot;&gt;   Future(println(&quot;Hello World!&quot;))&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token deleted-sign deleted&quot;&gt;&lt;span class=&quot;token prefix deleted&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt;     .flatMap(_ =&gt; print)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token inserted-sign inserted&quot;&gt;&lt;span class=&quot;token prefix inserted&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;token line&quot;&gt;     .flatMap(_ =&gt; Future(println(&quot;Hello World!&quot;)))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We now have:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; twice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  Future&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_ &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Future&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Running it, we then see:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Hello World!
Hello World!
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is why we say &lt;code&gt;Future&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; an effect: when we do not separate effect description from execution, as per our Effect Pattern, substitution of expressions with their definitions doesn’t have the same meaning,&lt;/p&gt;
&lt;h2 id=&quot;capturing-arbitrary-side-effects-as-an-effect&quot;&gt;Capturing Arbitrary Side-Effects as an Effect&lt;/h2&gt;
&lt;p&gt;We’ve seen the &lt;code&gt;Option&lt;/code&gt; effect type, which doesn’t involve side-effects, and we’ve examined why &lt;code&gt;Future&lt;/code&gt; isn’t an effect. So what about an effect that does involve side-effects, but safely?&lt;/p&gt;
&lt;p&gt;This is the purpose of the &lt;code&gt;IO&lt;/code&gt; effect type in &lt;code&gt;cats.effect&lt;/code&gt;. It is a data type that allows us to capture &lt;em&gt;any&lt;/em&gt; side-effect, but in a safe way, following our Effect Pattern. We’ll first build our own version of &lt;code&gt;IO&lt;/code&gt; to understand how it works.&lt;/p&gt;
&lt;h3 id=&quot;a-simple-myio-data-type&quot;&gt;A simple &lt;code&gt;MyIO&lt;/code&gt; data type&lt;/h3&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; MyIO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unsafeRun&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; map&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MyIO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    MyIO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unsafeRun&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; flatMap&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; MyIO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MyIO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    MyIO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;unsafeRun&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsafeRun&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;When we construct an &lt;code&gt;MyIO&lt;/code&gt; value, we give it a &lt;em&gt;function&lt;/em&gt; which computes the result and may perform side-effects, but don’t invoke it yet. When we want to execute the side-effect, we call &lt;code&gt;unsafeRun&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We can compose our &lt;code&gt;MyIO&lt;/code&gt; value to produce new &lt;code&gt;MyIO&lt;/code&gt; values using &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt;. But notice we always create a new &lt;code&gt;MyIO&lt;/code&gt; value, with its own delayed &lt;code&gt;unsafeRun&lt;/code&gt; side-effect, to ensure no side-effects are executed until the outer &lt;code&gt;unsafeRun&lt;/code&gt; is invoked.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example, we might define printing to the console as an &lt;code&gt;MyIO&lt;/code&gt; value:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; putStr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MyIO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  MyIO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;s&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; hello &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; putStr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;hello!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; world &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; putStr&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;world!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; helloWorld &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; hello&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; world&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;helloWorld&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unsafeRun&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which outputs&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;hello!
world!
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;myio-as-an-effect&quot;&gt;&lt;code&gt;MyIO&lt;/code&gt; as an Effect&lt;/h3&gt;
&lt;p&gt;Let’s check &lt;code&gt;MyIO&lt;/code&gt; against our Effect Pattern:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Does the type of the program tell us…&lt;/strong&gt;&lt;br /&gt;
a. &lt;strong&gt;What &lt;em&gt;kind of effects&lt;/em&gt; the program will perform?&lt;/strong&gt;&lt;br /&gt;
An &lt;code&gt;MyIO&lt;/code&gt; represents a (possibly) &lt;em&gt;side-effecting computation&lt;/em&gt;.&lt;br /&gt;
b. &lt;strong&gt;What &lt;em&gt;type of value&lt;/em&gt; will it will produce?&lt;/strong&gt;&lt;br /&gt;
A value of type &lt;code&gt;A&lt;/code&gt;, if the computation is successful.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;When externally-visible side-effects are required, is the effect description separate from the execution?&lt;/strong&gt;&lt;br /&gt;
Externally-visible side-effects &lt;strong&gt;are required&lt;/strong&gt;: the &lt;code&gt;unsafeRun&lt;/code&gt; method of an &lt;code&gt;MyIO&lt;/code&gt; can do anything, including side-effects.&lt;br /&gt;
We describe &lt;code&gt;MyIO&lt;/code&gt; values by constructing them and by composing with methods like &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt;. The execution of the effect only happens when the &lt;code&gt;unsafeRun&lt;/code&gt; method is called.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Therefore, &lt;code&gt;MyIO&lt;/code&gt; is an effect!&lt;/p&gt;
&lt;p&gt;By satisfying the Effect Pattern we know the &lt;code&gt;MyIO&lt;/code&gt; effect type is safe to use, even when programming with side-effects. At any point before we invoke &lt;code&gt;unsafeRun&lt;/code&gt; we can rely on substitution, and therefore we can replace any expression with its value–and vice-versa–to safely refactor our code.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://typelevel.org/cats-effect/datatypes/io.html&quot;&gt;&lt;code&gt;cats.effect.IO&lt;/code&gt;&lt;/a&gt; data type uses very similar techniques to isolate the captured side-effect from any methods used for composition.&lt;/p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;The substitution model of evaluation gives us local reasoning and fearless refactoring.&lt;/li&gt;
&lt;li&gt;Interacting with the environment can break substitution. We can localize these side-effects so they don’t affect evaluation.&lt;/li&gt;
&lt;li&gt;The Effect Pattern is a set of conditions that makes the presence of effects more visible while ensuring substitution is maintained. An effect’s type tells us what kind of effects the program will perform, in addition to the type of the value it will produce. Effects separate describing what we want to happen from actually making them happen. We can freely substitute the description of effects up until the point we run them.&lt;/li&gt;
&lt;li&gt;We created the &lt;code&gt;MyIO[A]&lt;/code&gt; effect, which delayed the side-effect until the &lt;code&gt;unsafeRun&lt;/code&gt; method is called. We produced new &lt;code&gt;MyIO&lt;/code&gt; values with the &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;flatMap&lt;/code&gt; combinators. &lt;a href=&quot;https://typelevel.org/cats-effect&quot;&gt;&lt;code&gt;cats-effect&lt;/code&gt;&lt;/a&gt; and other implementations of the &lt;code&gt;IO&lt;/code&gt; monad allow us to safely program and refactor our programs, even in the presence of side-effects.&lt;/li&gt;
&lt;/ol&gt;
</content>
	</entry>
	
	<entry>
		<title>Context Bounds are Implicitly Passed, are Subtype Bounds?</title>
		<link href="https://inner-product.com/posts/subtype-bounds/"/>
		<updated>2020-11-06T00:00:00Z</updated>
		<id>https://inner-product.com/posts/subtype-bounds/</id>
		<content type="html">&lt;h2 id=&quot;the-question&quot;&gt;The question&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;What is it that makes &lt;code&gt;Functor.widen&lt;/code&gt; safe? I think I remember someone explaining it at some point but I can’t remember. Something about “all functors are covariant” or something like that.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;– &lt;a href=&quot;https://gitter.im/typelevel/cats?at=5ebc53cd2cf0da0ad9feadaf&quot;&gt;@Billzabob in gitter.im/typelevel/cats&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;what-does-functor.widen-do%3F&quot;&gt;What does &lt;code&gt;Functor.widen&lt;/code&gt; do?&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Functor.widen&lt;/code&gt; lets you transform some container &lt;code&gt;F[A]&lt;/code&gt; into &lt;code&gt;F[B]&lt;/code&gt;, if &lt;code&gt;A&lt;/code&gt; is a subtype of &lt;code&gt;B&lt;/code&gt;. For example, a list of cats can also be viewed as a list of animals:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implicits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cats&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Cat&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; animals&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Animal&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;widen&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Naturally, &lt;code&gt;Cat&lt;/code&gt; is a subtype of &lt;code&gt;Animal&lt;/code&gt; in these examples.)&lt;/p&gt;
&lt;p&gt;Now, using &lt;code&gt;widen&lt;/code&gt; on a &lt;code&gt;List&lt;/code&gt; is actually redundant, as &lt;code&gt;List&lt;/code&gt; is marked as &lt;em&gt;covariant&lt;/em&gt;: it is defined as &lt;code&gt;List[+A]&lt;/code&gt;. The &lt;code&gt;+&lt;/code&gt; before the type parameter &lt;code&gt;A&lt;/code&gt; means covariant, and if something is covariant it means you can replace some &lt;code&gt;F[A]&lt;/code&gt; with &lt;code&gt;F[B]&lt;/code&gt;, if &lt;code&gt;A&lt;/code&gt; is a subtype of &lt;code&gt;B&lt;/code&gt;. So we could have instead written:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cats&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Cat&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; animals&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Animal&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cats &lt;span class=&quot;token comment&quot;&gt;// no widen necessary, because List[+A]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We might actually use &lt;code&gt;widen&lt;/code&gt; on a type that &lt;em&gt;isn’t&lt;/em&gt; marked as covariant, such as &lt;code&gt;cats.data.EitherT&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;EitherT&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implicits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cat&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; EitherT&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Throwable&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Cat&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; animal&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; EitherT&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Throwable&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Animal&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cat&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;widen&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;how-is-functor.widen-implemented%3F&quot;&gt;How is &lt;code&gt;Functor.widen&lt;/code&gt; implemented?&lt;/h2&gt;
&lt;p&gt;Here’s the definition from Cats:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; widen&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fa&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; fa&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;asInstanceOf&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We see the implementation is a type cast that is guarded by the subtype constraint &lt;code&gt;B &amp;gt;: A&lt;/code&gt;: type &lt;code&gt;B&lt;/code&gt; must be a supertype of type &lt;code&gt;A&lt;/code&gt;. The compiler won’t allow the use of &lt;code&gt;widen&lt;/code&gt; where the subtype relationship doesn’t hold.&lt;/p&gt;
&lt;h2 id=&quot;back-to-the-question&quot;&gt;Back to the Question&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;What is it that makes &lt;code&gt;Functor.widen&lt;/code&gt; safe?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I replied–correctly–that &lt;code&gt;widen&lt;/code&gt; is safe because:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;the signature requires the super type witness: &lt;code&gt;def widen[A, B &amp;gt;: A](fa: F[A]): F[B]&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;where I conflate the term “witness” (a &lt;em&gt;value&lt;/em&gt; that “proves” some condition holds) with the presence of the subtype bound &lt;code&gt;B &amp;gt;: A&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;However, I then misspoke:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;also remember that &lt;code&gt;[B &amp;gt;: A]&lt;/code&gt; is actually passed as an implicit value &lt;code&gt;ev: A &amp;lt;:&amp;lt; B&lt;/code&gt;, and &lt;code&gt;&amp;lt;:&amp;lt;[A, B] extends Function1[A, B]&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I thought that subtype bounds were syntactic sugar, just as &lt;a href=&quot;https://docs.scala-lang.org/tutorials/FAQ/context-bounds.html&quot;&gt;context bounds&lt;/a&gt; are, but I was wrong! Recall that context bounds are a way to succinctly write typeclass instance constraints, so&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; something&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Monoid context bound&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;is desugared and equivalent to&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; something&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; m&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Monoid&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// implicit Monoid typeclass instance&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is an analogous implicitly passed value for &lt;em&gt;subtype&lt;/em&gt; bounds, but the subtype bound &lt;strong&gt;is not&lt;/strong&gt; syntactic sugar for it. (I thought it was.) That is, the type signature&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; widen&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fa&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;is &lt;em&gt;equivalent&lt;/em&gt; to the type signature&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; widen&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fa&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; ev&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;but the former is &lt;em&gt;not&lt;/em&gt; converted to the latter by the compiler,&lt;br /&gt;
as is the case for context bounds. (&amp;quot;&lt;code&gt;B&lt;/code&gt; is a supertype of &lt;code&gt;A&lt;/code&gt;&amp;quot; is equivalent to &amp;quot;&lt;code&gt;A&lt;/code&gt; is a subtype of &lt;code&gt;B&lt;/code&gt;&amp;quot;; &lt;code&gt;ev&lt;/code&gt; stands for “evidence”.)&lt;/p&gt;
&lt;h2 id=&quot;what-is-this-%3C%3A%3C-type%3F&quot;&gt;What is this &lt;code&gt;&amp;lt;:&amp;lt;&lt;/code&gt; type?&lt;/h2&gt;
&lt;p&gt;The higher-kinded type &lt;code&gt;&amp;lt;:&amp;lt;[A, B]&lt;/code&gt; represents the subtype relationship between two types, where &lt;code&gt;A&lt;/code&gt; is a subtype of &lt;code&gt;B&lt;/code&gt;. Types with two parameters may be written &lt;em&gt;infix&lt;/em&gt;, so the previous type is usually written as &lt;code&gt;A &amp;lt;:&amp;lt; B&lt;/code&gt;. If you have a value of this type, then the subtype relationship holds.&lt;/p&gt;
&lt;p&gt;As shown above, you can “summon” an implicit value (named &lt;code&gt;ev&lt;/code&gt; above) of type &lt;code&gt;A &amp;lt;:&amp;lt; B&lt;/code&gt; if type &lt;code&gt;A&lt;/code&gt; is a subtype of &lt;code&gt;B&lt;/code&gt;. The compiler will then supply the value if it exists.&lt;/p&gt;
&lt;p&gt;What’s the point of having this alternate representation of the subtype relationship? If an &lt;code&gt;A&lt;/code&gt; is a subtype of &lt;code&gt;B&lt;/code&gt;, we can rely on the compiler to “automatically” cast an &lt;code&gt;A&lt;/code&gt; into a &lt;code&gt;B&lt;/code&gt;. Like a typeclass instance, you only need it if you’re going to use it, which begs the question, what can you &lt;em&gt;do&lt;/em&gt; with a &lt;code&gt;A &amp;lt;:&amp;lt; B&lt;/code&gt;?&lt;/p&gt;
&lt;h2 id=&quot;using-a-%3C%3A%3C-value&quot;&gt;Using a &lt;code&gt;&amp;lt;:&amp;lt;&lt;/code&gt; value&lt;/h2&gt;
&lt;p&gt;It turns out that &lt;code&gt;A &amp;lt;:&amp;lt; B&lt;/code&gt; is a subtype of the function type &lt;code&gt;A =&amp;gt; B&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Function1&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This makes sense: subtyping &lt;em&gt;means&lt;/em&gt; we can transform a value of (sub-)type &lt;code&gt;A&lt;/code&gt; into a value of (super-)type &lt;code&gt;B&lt;/code&gt;. And the value &lt;code&gt;ev: A &amp;lt;:&amp;lt; B&lt;/code&gt; &lt;em&gt;is&lt;/em&gt; the function that can do that.&lt;/p&gt;
&lt;p&gt;It’s not common to use this value explicitly, but the fact that it exists can help demystify covariance.&lt;/p&gt;
&lt;h2 id=&quot;covariance-without-subtypes&quot;&gt;Covariance without subtypes&lt;/h2&gt;
&lt;p&gt;Covariance is usually explained in terms of containers and subtypes. That is, the covariant &lt;code&gt;List[A]&lt;/code&gt; can be cast to a &lt;code&gt;List[B]&lt;/code&gt; if &lt;code&gt;A&lt;/code&gt; is a subtype of &lt;code&gt;B&lt;/code&gt;. (Cue list of cats and animals example.)&lt;/p&gt;
&lt;p&gt;What if we could “turn off” the covariant &lt;code&gt;+&lt;/code&gt; annotation on &lt;code&gt;List&lt;/code&gt;, but still perform the same conversion of the container? How might we implement that ourselves? Well, to convert one list into another, we can use &lt;code&gt;map&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cats&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Cat&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; animals&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Animal&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We need to replace &lt;code&gt;???&lt;/code&gt; with a function that converts a &lt;code&gt;Cat&lt;/code&gt; into an &lt;code&gt;Animal&lt;/code&gt;. That’s our implicit-subtype-evidence-function thing!&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; cats&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Cat&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; ev&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Cat &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; Animal &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; implicitly&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; animals&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Animal&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ev&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead of viewing covariance as the ability to convert containers of subtypes into containers of supertypes, we can recast the former definition in terms of &lt;code&gt;map&lt;/code&gt; with the “subtype evidence” function. Covariance is a more general phenomenon: the ability to &lt;code&gt;map&lt;/code&gt;; that is, a (covariant) &lt;code&gt;Functor&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;back-to-functor.widen&quot;&gt;Back to &lt;code&gt;Functor.widen&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;We can rewrite &lt;code&gt;Functor.widen&lt;/code&gt; to explicitly convert every element using the subtype evidence, rather than using the type casting machinery of the compiler:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; explicitWiden&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fa&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; ev&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;B&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  fa&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ev&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Remember, &lt;code&gt;F&lt;/code&gt; is a &lt;code&gt;Functor&lt;/code&gt;, so there is a &lt;code&gt;map&lt;/code&gt; method available.)&lt;/p&gt;
&lt;p&gt;The actual implementation of &lt;code&gt;Functor.widen&lt;/code&gt; doesn’t use this definition, as it’s unnecessary to actually perform the sub- to supertype conversion given the semantics of Scala. So instead the implementation does a cast. But I find it very illuminating to know they are equivalent!&lt;/p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;It is always possible to convert a value of a subtype to its supertype. This doesn’t require any extra code at runtime, only at compile-time.&lt;/li&gt;
&lt;li&gt;You can get &lt;em&gt;evidence&lt;/em&gt; of the subtype relation as an implicit parameter. This evidence has type &lt;code&gt;&amp;lt;:&amp;lt;[A, B]&lt;/code&gt;, which is most often written infix as &lt;code&gt;A &amp;lt;:&amp;lt; B&lt;/code&gt;. The &lt;code&gt;&amp;lt;:&amp;lt;[A, B]&lt;/code&gt; type extends &lt;code&gt;Function1[A, B]&lt;/code&gt;, because one can always tranform subtypes into supertypes.&lt;/li&gt;
&lt;li&gt;Containers of a subtype can be transformed into containers of its supertype, if you can &lt;code&gt;map&lt;/code&gt; over the container. The usual defintion of covariance emphasizes subtypes, but the ability to &lt;code&gt;map&lt;/code&gt; is a more general, and useful, definition.&lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<title>Concurrent state machines</title>
		<link href="https://inner-product.com/posts/concurrent-state-machines/"/>
		<updated>2021-01-05T00:00:00Z</updated>
		<id>https://inner-product.com/posts/concurrent-state-machines/</id>
		<content type="html">&lt;p&gt;Noel recently tweeted about state machines,&lt;/p&gt;
&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;Finite state machines are really a very useful abstraction. This is the kind of stuff that some dismiss as theoretical nonsense taught by time wasting CS departments, but from UIs to distributed systems life gets easier if you know and use FSMs.&lt;/p&gt;&amp;mdash; Noel Welsh (@noelwelsh) &lt;a href=&quot;https://twitter.com/noelwelsh/status/1338450292359245825?ref_src=twsrc%5Etfw&quot;&gt;December 14, 2020&lt;/a&gt;&lt;/blockquote&gt; &lt;script async=&quot;&quot; src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;p&gt;and that reminded me of &lt;strong&gt;concurrent&lt;/strong&gt; state machines! Those must be even cooler!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The following is an excerpt from a chapter in &lt;a href=&quot;https://essentialeffects.dev/&quot;&gt;my upcoming book&lt;/a&gt;, “Essential Effects”&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;concurrent-state-machines&quot;&gt;Concurrent state machines&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Ref&lt;/code&gt; and &lt;code&gt;Deferred&lt;/code&gt; are the building blocks of concurrency within Cats Effect. With &lt;code&gt;Ref&lt;/code&gt; we can ensure atomic updates of shared state, and &lt;code&gt;Deferred&lt;/code&gt; gives us the ability to serialize the execution of an effect with respect to some newly-produced state. Together we can build larger and more complex concurrent behaviors. One technique to do this is to create a &lt;em&gt;concurrent state machine&lt;/em&gt;.&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://inner-product.com/posts/concurrent-state-machines/#fn1&quot; id=&quot;fnref1&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;To build one we:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Define an interface whose methods return effects.&lt;/li&gt;
&lt;li&gt;Implement the interface by building a state machine where:
&lt;ol&gt;
&lt;li&gt;state (with type &lt;code&gt;S&lt;/code&gt;) is atomically managed via a &lt;code&gt;Ref[IO, S]&lt;/code&gt; value;&lt;/li&gt;
&lt;li&gt;each interface method is implemented by a state transition function affecting the &lt;code&gt;Ref&lt;/code&gt;; and&lt;/li&gt;
&lt;li&gt;any state-dependent blocking behavior is controlled via &lt;code&gt;Deferred&lt;/code&gt; values.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As an example, we’ll follow this recipe to build a structure called a &lt;em&gt;countdown latch&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id=&quot;example%3A-countdown-latch&quot;&gt;Example: countdown latch&lt;/h2&gt;
&lt;p&gt;The behavior we’d like to model is &lt;em&gt;to block subsequent effects until a certain number of (possibly concurrent) effects have occurred&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The metaphor of a &lt;em&gt;latch&lt;/em&gt; is used because a latch is used to keep a door closed until the latch is opened. The term &lt;em&gt;countdown&lt;/em&gt; refers to the algorithm for how the latch is opened: a counter is decremented, and when the counter reaches zero, the latch opens.&lt;/p&gt;
&lt;p&gt;There are two logical roles that concurrently coordinate through the shared latch:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;readers&lt;/em&gt; wait for the latch to open; and&lt;/li&gt;
&lt;li&gt;&lt;em&gt;writers&lt;/em&gt; decrement the latch counter.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The latch itself is responsible for “opening” when its counter reaches zero.&lt;/p&gt;
&lt;p&gt;Let’s fulfill step one of our recipe (“define an interface whose methods return effects”) by encapsulating the actions of the two roles as methods on a shared &lt;code&gt;CountdownLatch&lt;/code&gt; interface:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; CountdownLatch &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; await&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; decrement&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;Readers will &lt;code&gt;await&lt;/code&gt; the opening of the latch. The caller will be &lt;em&gt;blocked&lt;/em&gt; and no value will be produced until the latch opens.&lt;/li&gt;
&lt;li&gt;Writers will &lt;code&gt;decrement&lt;/code&gt; the latch counter, which may open the latch.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A reader will be waiting for the latch to open, perhaps denoting a set of prerequite actions have occurred:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; actionWithPrerequisites&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latch&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; CountdownLatch&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;waiting for prerequisites&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;debug&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; latch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;await &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;    result &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;action&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;debug &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; result&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;We block until the &lt;code&gt;latch&lt;/code&gt; opens.&lt;/li&gt;
&lt;li&gt;Once the &lt;code&gt;latch&lt;/code&gt; opens, we can run the action.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;(In these examples, &lt;code&gt;debug&lt;/code&gt; is an custom extension method on an &lt;code&gt;IO&lt;/code&gt; that is essentially defined as &lt;code&gt;flatTap(a =&amp;gt; IO(println(a)))&lt;/code&gt;, printing the value produced to the console.)&lt;/p&gt;
&lt;p&gt;At the same time, a writer is fulfilling one or more of those prerequisites:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; runPrerequisite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latch&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; CountdownLatch&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    result &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;prerequisite&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;debug&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; latch&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decrement &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; result&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;Once the prerequisite action is completed, we &lt;code&gt;decrement&lt;/code&gt; the latch.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Other code would run each of these roles concurrently:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; prepareAndRun &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    latch &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; CountdownLatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;actionWithPrerequisites&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latch&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; runPrerequisite&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;latch&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;parTupled&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It’s important to note that the two effects are &lt;em&gt;only&lt;/em&gt; communicating through the shared &lt;code&gt;CountdownLatch&lt;/code&gt;. They don’t directly know anything about each other.&lt;/p&gt;
&lt;p&gt;When we run it we would see output like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[ioapp-compute-1] waiting for prerequisites
[ioapp-compute-2] prerequisite
[ioapp-compute-1] action
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s implement it! A &lt;code&gt;CountdownLatch&lt;/code&gt; will be in one of two states:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;outstanding&lt;/em&gt;: we have &lt;code&gt;n&lt;/code&gt; outstanding &lt;code&gt;decrement()&lt;/code&gt; operations to expect; or&lt;/li&gt;
&lt;li&gt;&lt;em&gt;done&lt;/em&gt;: we have invoked &lt;code&gt;decrement()&lt;/code&gt; &lt;code&gt;n&lt;/code&gt; (or more) times.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We’ll encode the state–step 2.1 of our recipe–as an &lt;em&gt;algebraic data type&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; State&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Outstanding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; whenDone&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Deferred&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;IO&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; State&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Done&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; State&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For each method of the interface, the behavior of the latch will depend on its current state:&lt;/p&gt;
&lt;p&gt;When a reader calls &lt;code&gt;await()&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If our state is &lt;code&gt;Outstanding(n, whenDone)&lt;/code&gt;, there are &lt;code&gt;n&lt;/code&gt; outstanding &lt;code&gt;decrement&lt;/code&gt; calls, so block the caller via &lt;code&gt;whenDone.get&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If our state is &lt;code&gt;Done()&lt;/code&gt;, do nothing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When a writer calls &lt;code&gt;decrement()&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If our state is &lt;code&gt;Outstanding(n, whenDone)&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;n&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;, this is the last &lt;code&gt;decrement()&lt;/code&gt;. Transition to &lt;code&gt;Done&lt;/code&gt; and unblock any blocked &lt;code&gt;await()&lt;/code&gt; calls via &lt;code&gt;whenDone.complete()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Otherwise decrement &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If our state is &lt;code&gt;Done()&lt;/code&gt;, do nothing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://inner-product.com/img/latch.png&quot; alt=&quot;Concurrent state machine for a countdown latch that opens after  events. A  holds the current state.&quot; /&gt;&lt;/p&gt;
&lt;p&gt;When we construct the &lt;code&gt;CountdownLatch&lt;/code&gt; we’ll control concurrent access to the state with a &lt;code&gt;Ref&lt;/code&gt; and create a &lt;code&gt;Deferred&lt;/code&gt; to control our blocking behavior. We’ll then translate the state transitions into code almost exactly as previously described:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; CountdownLatch &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; cs&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ContextShift&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;IO&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;CountdownLatch&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      whenDone &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; Deferred&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;IO&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;      state &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; Ref&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;IO&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;of&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;State&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Outstanding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; whenDone&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; CountdownLatch &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; await&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;        state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatMap &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;3&gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Outstanding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; whenDone&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; whenDone&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;4&gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Done&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;                   &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unit&lt;br /&gt;        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; decrement&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;        state&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;modify &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;5&gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Outstanding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; whenDone&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Done&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; whenDone&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;complete&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;6&gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Outstanding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; whenDone&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;            Outstanding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; whenDone&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unit &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;7&gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Done&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Done&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;unit&lt;br /&gt;        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatten &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;8&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;We create a &lt;code&gt;Deferred[IO, Unit]&lt;/code&gt; that we’ll use to block and unblock &lt;code&gt;await()&lt;/code&gt; callers.&lt;/li&gt;
&lt;li&gt;We enforce atomic access to the current state with a &lt;code&gt;Ref[IO, State]&lt;/code&gt; that we initialize to &lt;code&gt;Outstanding&lt;/code&gt; with &lt;code&gt;n&lt;/code&gt; expected decrements.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;await()&lt;/code&gt; never changes the state, so we only act on the value from &lt;code&gt;state.get&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If decrements are outstanding, we return a blocking effect that unblocks when the &lt;code&gt;Deferred&lt;/code&gt; is completed.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;decrement()&lt;/code&gt; always changes the state, so we use &lt;code&gt;Ref.modify&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This is the last decrement, so we transition to &lt;code&gt;Done&lt;/code&gt; and return an effect that completes the &lt;code&gt;Deferred&lt;/code&gt; to unblock anyone who has invoked &lt;code&gt;await()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We decrement the counter and return an effect which does nothing.&lt;/li&gt;
&lt;li&gt;Our use of the &lt;code&gt;state.modify&lt;/code&gt; method returns an &lt;code&gt;IO[IO[Unit]]&lt;/code&gt;, so we &lt;code&gt;flatten&lt;/code&gt; it.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Voilà!&lt;/p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;We built a &lt;em&gt;countdown latch&lt;/em&gt; to model blocking subsequent effects until a certain number of (possibly concurrent) effects have occurred. We followed the &lt;em&gt;concurrent state machine&lt;/em&gt; recipe:&lt;/p&gt;
&lt;p&gt;1. &lt;strong&gt;Define an interface whose methods return effects.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We defined:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; CountdownLatch &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;	&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; await&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;	&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; decrement&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;2. &lt;strong&gt;Implement the interface by building a state machine where:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;2.1. &lt;strong&gt;state (with type &lt;code&gt;S&lt;/code&gt;) is atomically managed via a &lt;code&gt;Ref[IO, S]&lt;/code&gt; value:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We initialize our state into a &lt;code&gt;Ref&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;Ref&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;IO&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;of&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;State&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Outstanding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; whenDone&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where &lt;code&gt;State&lt;/code&gt; is the algebraic data type&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; State&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Outstanding&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; whenDone&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Deferred&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;IO&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; State&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Done&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; State&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;2.2. &lt;strong&gt;each interface method is implemented by a state transition function affecting the &lt;code&gt;Ref&lt;/code&gt;; and&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We implement state-dependent behavior by pattern matching on the current state provided by the &lt;code&gt;Ref&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Ref.modify&lt;/code&gt; lets us set the new state and return additional effects to be run.&lt;/p&gt;
&lt;p&gt;2.3. &lt;strong&gt;any state-dependent blocking behavior is controlled via &lt;code&gt;Deferred&lt;/code&gt; values.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We block when invoking &lt;code&gt;await&lt;/code&gt; in the &lt;code&gt;Outstanding&lt;/code&gt; state, and unblock any “await-ers” when invoking &lt;code&gt;decrement&lt;/code&gt; if the counter reaches zero.&lt;/p&gt;
&lt;p&gt;Blocking and unblocking are controlled by the &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;complete&lt;/code&gt; methods of the &lt;code&gt;whenDone: Deferred[IO, Unit]&lt;/code&gt; value.&lt;/p&gt;
&lt;p&gt;By following this recipe, you too can build all sorts of control structures: mutexes, barriers, and more.&lt;/p&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Fabio Labella introduced this technique and has popularized it through his talks and public commentary. You can watch his talks and learn more at &lt;a href=&quot;https://systemfw.org/&quot;&gt;https://systemfw.org&lt;/a&gt;. &lt;a href=&quot;https://inner-product.com/posts/concurrent-state-machines/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
	</entry>
	
	<entry>
		<title>Recent and Upcoming Public Appearances</title>
		<link href="https://inner-product.com/posts/early-2021-public-appearances/"/>
		<updated>2021-02-11T00:00:00Z</updated>
		<id>https://inner-product.com/posts/early-2021-public-appearances/</id>
		<content type="html">&lt;p&gt;We’ve had a few recent public appearances in the beginning of 2021, and more are upcoming:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Adam introduced the Cats Effect library and the release of &lt;a href=&quot;https://essentialeffects.dev/&quot;&gt;his new book, &lt;em&gt;Essential Effects&lt;/em&gt;&lt;/a&gt;, for the &lt;a href=&quot;https://www.meetup.com/SF-Scala/events/275583573/&quot;&gt;SF&lt;/a&gt; and &lt;a href=&quot;https://www.meetup.com/Singapore-Scala-Programmers/events/275785450/&quot;&gt;Singapore&lt;/a&gt; Scala meetup groups. You can watch either version &lt;a href=&quot;https://www.youtube.com/watch?v=9TR--8gAcZ8&quot;&gt;here&lt;/a&gt; or &lt;a href=&quot;https://us02web.zoom.us/rec/play/or0k6KaezfmDtifcD3U_c_SsBJVD-mgujVt4HvEgUNJl7eRJ7e27fvaf8r6AUjp_S8OA-CHZm_nJcTMU.7RLht6ZEG_BTzxfZ?continueMode=true&amp;amp;_x_zm_rtaid=f1Cdv9PpQw67NCK5vo71HQ.1613070891392.96866f92ce4ab3f2a9439a89d50136b8&amp;amp;_x_zm_rhtaid=727&quot;&gt;here&lt;/a&gt;. (Adam likes the second recording better because he had more practice.)&lt;/li&gt;
&lt;li&gt;Adam was on &lt;a href=&quot;https://open.spotify.com/show/4PbqD6YLB2hY5pFJWfDgeN&quot;&gt;The Scala Logs&lt;/a&gt; podcast talking about the release of &lt;a href=&quot;https://essentialeffects.dev/&quot;&gt;&lt;em&gt;Essential Effects&lt;/em&gt;&lt;/a&gt; and a bunch of other nerdy things. &lt;a href=&quot;https://open.spotify.com/episode/169kHgsVpYzyK3ixy6KQ9q&quot;&gt;Take a listen!&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Noel will be presenting at the &lt;a href=&quot;https://inthecity.scala.love/&quot;&gt;Scala Love in the City&lt;/a&gt; conference on Saturday, February 13, 2021. He’ll be presenting &lt;a href=&quot;https://embed.emamo.com/event/scala-love-in-the-city/r/speaker/noel-welsh&quot;&gt;Finite State Machines for Functional Software Machinery&lt;/a&gt;, we hope you can attend!
&lt;blockquote&gt;
&lt;p&gt;Finite state machines (FSMs) are one of the simplest models of computation, but it’s this simplicity that makes them so useful. They are expressive enough to cover a wide range of practical problems while remaining easy to reason about. I believe they should be more widely used, and the only reason they aren’t is because many developers only know them in the context of a dusty theory of computation course. This talk sets out to change this!&lt;/p&gt;
&lt;p&gt;I’ll first (re)introduce finite state machines as a model of computation. I’ll show that they are very simple, which makes them easy to understand and therefore create and debug. We’ll then see how finite state machines appear just about everywhere. We’ll see examples in the Scala standard library, and in application code from web services and user interfaces. We’ll finish by discussing implementation techniques and further applications.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content>
	</entry>
	
	<entry>
		<title>Fire-and-Forget in Cats Effect</title>
		<link href="https://inner-product.com/posts/fire-and-forget-in-cats-effect/"/>
		<updated>2021-03-09T00:00:00Z</updated>
		<id>https://inner-product.com/posts/fire-and-forget-in-cats-effect/</id>
		<content type="html">&lt;p&gt;I’ve noticed that often a lot of similar questions get asked within a few days of each other. Recently those questions have been about “fire-and-forget”, how it’s done in Cats Effect, and what issues one should be aware of. So I wrote up a little post with some background and techniques. Hope you enjoy it!&lt;/p&gt;
&lt;p&gt;If you want to learn more about Cats Effect and concurrency, check out my book, &lt;a href=&quot;https://essentialeffects.dev/&quot;&gt;&lt;em&gt;Essential Effects&lt;/em&gt;&lt;/a&gt;!&lt;/p&gt;
&lt;h2 id=&quot;what-is-%E2%80%9Cfire-and-forget%E2%80%9D%3F&quot;&gt;What is “fire-and-forget”?&lt;/h2&gt;
&lt;p&gt;To understand &lt;em&gt;fire-and-forget&lt;/em&gt;, let’s first distinguish it from a more typical method or function call whose purpose is to compute a value: a &lt;em&gt;synchronous&lt;/em&gt; computation. We invoke the function, and can proceed once the value is produced.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;Thread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;currentThread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;] doSomething(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; is a very nice number&quot;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; doSomethingElse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;Thread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;currentThread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;] doSomethingElse()&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; doBoth &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;Thread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;currentThread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;] doSomething(12) produced: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;result&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  doSomethingElse&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice: all &lt;code&gt;println&lt;/code&gt; outputs show the same thread.&lt;/p&gt;
&lt;p&gt;Next let’s break down &lt;em&gt;fire-and-forget&lt;/em&gt;. What’s &lt;em&gt;fire&lt;/em&gt;?&lt;br /&gt;
It means to start a computation without waiting for its result, along with a way to access its &lt;em&gt;eventual&lt;/em&gt; result: an &lt;em&gt;asynchronous&lt;/em&gt; computation. (The term &lt;em&gt;fire&lt;/em&gt; comes from the military, where a missile is launched. Eek!)&lt;/p&gt;
&lt;p&gt;What would the type signature of such an asynchronous thing be? It requires the necessary inputs, but immediately returns to the caller. We’re not returning the “actual” result of the computation–we need to merely signal “ok, we’re starting, here is a value that lets you act on the eventual result”. We’ll model this as a trait with a &lt;code&gt;fire&lt;/code&gt; method that returns an “eventual result” signal (as a function):&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Asynchronous &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; EventualResult&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; fire&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; EventualResult&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can implement this interface using Scala’s &lt;code&gt;Future&lt;/code&gt; type:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Asynchronous &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;concurrent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;concurrent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;duration&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; global &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Asynchronous &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; ec &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;concurrent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ExecutionContext&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;global&lt;br /&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; fire&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; EventualResult&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; res &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Promise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;completeWith&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Future&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// start the computation&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Await&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;res&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;future&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Duration&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Inf&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;// wait on demand&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We then convert our synchronous code to use our &lt;code&gt;Asynchronous&lt;/code&gt; interface:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; doBothAsync &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; await &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Asynchronous&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;global&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fire&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; await&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;Thread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;currentThread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;] doSomething(12) produced: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;result&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  doSomethingElse&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice: The &lt;code&gt;println&lt;/code&gt; for &lt;code&gt;doSomething&lt;/code&gt; happens in a different thread than &lt;code&gt;doSomethingElse&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, we need to &lt;em&gt;forget&lt;/em&gt;. That is, after we start the computation (&lt;em&gt;fire&lt;/em&gt;), we don’t do anything with our handler to the eventual result. We “forget” it:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Asynchronous &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; EventualResult&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; fire&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; EventualResult&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; fireAndForget&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    fire&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// ignore the eventual result handler&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Asynchronous &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;concurrent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;concurrent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;duration&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; global &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Asynchronous &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; ec &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;concurrent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ExecutionContext&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;global&lt;br /&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; fire&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; EventualResult&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; res &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Promise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;completeWith&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Future&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// start the computation&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; Await&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;res&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;future&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Duration&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Inf&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// wait on demand&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;fireAndForget&lt;/code&gt; method invokes &lt;code&gt;fire&lt;/code&gt;, which starts the asynchronous computation, but then ignores the returned &lt;code&gt;EventualResult&lt;/code&gt;, meaning we won’t wait for any result at all.&lt;/p&gt;
&lt;p&gt;Let’s update our example:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; doBothFireAndForget &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  Asynchronous&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;global&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fireAndForget&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  doSomethingElse&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice: we see the same asynchronous execution as the “fire”-only example, but don’t print out any intermediate results because we choose to not to wait for them.&lt;/p&gt;
&lt;h2 id=&quot;translating-to-cats-effect&quot;&gt;Translating to Cats Effect&lt;/h2&gt;
&lt;p&gt;In Cats Effect, there are three parts to &lt;em&gt;fire-and-forget&lt;/em&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Define the &lt;em&gt;synchronous&lt;/em&gt; computation as an effect.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Fork&lt;/em&gt; the execution of the effect so it runs asynchronously (fire).&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Ignore&lt;/em&gt; the return value of the fork (forget).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let’s do it:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;effect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;cats&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;implicits&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;_&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Something &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; IOApp &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;args&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ExitCode&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    doBoth&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;as&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ExitCode&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Success&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;Thread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;currentThread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;] doSomething(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;i&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; is a very nice number&quot;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; doSomethingElse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token id function&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token expression&quot;&gt;Thread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;currentThread&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;getName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;] doSomethingElse()&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; doBoth&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;start &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt; &amp;lt;3&gt;&lt;/span&gt;&lt;br /&gt;      i &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; doSomethingElse&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; i&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Something&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;main&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Array&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;empty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;We change the signature from returning &lt;code&gt;String&lt;/code&gt; to returning &lt;code&gt;IO[String]&lt;/code&gt;, to signal the definition of a (synchronous) effect.&lt;/li&gt;
&lt;li&gt;We &lt;code&gt;start&lt;/code&gt; the &lt;code&gt;IO&lt;/code&gt; effect value, forking its execution. It returns an &lt;code&gt;IO[Fiber[IO, String]]&lt;/code&gt; (&lt;code&gt;IO[FiberIO[String]]&lt;/code&gt; in Cats Effect 3) that lets you manage the asynchronous execution and eventual result.&lt;/li&gt;
&lt;li&gt;We also ignore the &lt;code&gt;Fiber&lt;/code&gt; produced by &lt;code&gt;start&lt;/code&gt; by “anonymizing” its name as &lt;code&gt;_&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;is-it-safe-to-%E2%80%9Cforget%E2%80%9D%3F&quot;&gt;Is it safe to “forget”?&lt;/h2&gt;
&lt;p&gt;When we &lt;em&gt;fire-and-forget&lt;/em&gt; using Cats Effect, we’re deliberately not keeping track of the forked computation. Is there a cost to this–is it safe?&lt;/p&gt;
&lt;p&gt;To answer, we need to recall what we &lt;em&gt;can&lt;/em&gt; do with the fiber of the forked effect: we can wait for its completion (via &lt;code&gt;join&lt;/code&gt;) or cancel it (via &lt;code&gt;cancel&lt;/code&gt;). Since we really want asychronous execution, we will ignore &lt;code&gt;join&lt;/code&gt;, leaving &lt;code&gt;cancel&lt;/code&gt;: would it ever be necessary to &lt;code&gt;cancel&lt;/code&gt; a computation that we &lt;em&gt;fired&lt;/em&gt; and &lt;em&gt;forgot&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;It certainly could be important to do so! Consider a &lt;em&gt;long-lived&lt;/em&gt; task, like an event-handling loop. We might think “oh, that effect runs forever, it doesn’t need to be canceled”, and therefore 🚀 + 🙈: &lt;em&gt;fire-and-forget&lt;/em&gt;. But cancelation might be needed! That forked effect exists in some context; it might be important to ensure that it is cancelled if its “parent” context is cancelled:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; businessLogic &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; longLivedEffect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;start&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; doSomeOtherStuff&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example perhaps &lt;code&gt;longLivedEffect&lt;/code&gt; should be cancelled when &lt;code&gt;businessLogic&lt;/code&gt; completes, but &lt;em&gt;fire-and-forget&lt;/em&gt; precludes that possibility. We could instead &lt;em&gt;link&lt;/em&gt; the lifetime of that long-lived effect to the surrounding business logic. One way is to use &lt;code&gt;guarantee&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; businessLogic &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    fiber &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; longLivedEffect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;start&lt;br /&gt;    _ &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; doSomeOtherStuff&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;guarantee&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fiber&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cancel&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;Cancel the fiber once the other logic completes successfully, with an error, or is cancelled.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;But using &lt;code&gt;guarantee&lt;/code&gt; doesn’t scale to multiple effects–for example, we want the fiber to be cancelled if &lt;em&gt;any&lt;/em&gt; subsequent effect inside &lt;code&gt;businessLogic&lt;/code&gt; is cancelled. We can instead model the lifecycle of an effect as a &lt;a href=&quot;https://typelevel.org/cats-effect/docs/std/resource&quot;&gt;&lt;em&gt;resource&lt;/em&gt;&lt;/a&gt;: a data structure that allows us to define a static region where state is used, along with effects for state acquisition and release:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; longLived&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Resource&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;IO&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  longLivedEffect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;background &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;1&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; businessLogic &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  longLived&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;use &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; join &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &amp;lt;2&gt;&lt;/span&gt;&lt;br /&gt;    doSomeOtherStuff&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ol&gt;
&lt;li&gt;Instead of calling &lt;code&gt;start&lt;/code&gt; to fork the execution, &lt;code&gt;background&lt;/code&gt; returns a &lt;code&gt;Resource&lt;/code&gt; that manages the forked fiber for you. The managed state is itself an action–an &lt;code&gt;IO[Unit]&lt;/code&gt; that lets you &lt;code&gt;join&lt;/code&gt; the underlying fiber (if you want to).&lt;/li&gt;
&lt;li&gt;Once we have a &lt;code&gt;Resource&lt;/code&gt;, we &lt;code&gt;use&lt;/code&gt; it to gain access to the managed state, where we can then perform our own effects. This defines a &lt;em&gt;static region&lt;/em&gt; where the state is available, and outside this region we are guaranteed the state has been properly acquired and released. In the case of the resource returned by &lt;code&gt;background&lt;/code&gt;, the state acquisition effect is “fork a fiber” and the release effect is “cancel the fiber”.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;The pattern of &lt;em&gt;fire-and-forget&lt;/em&gt; is really built out of three concepts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;defining &lt;em&gt;synchronous&lt;/em&gt; effects;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;forking&lt;/em&gt; an effect to run asynchronously; and&lt;/li&gt;
&lt;li&gt;&lt;em&gt;controlling&lt;/em&gt; the asynchronously executing effect, either waiting for its eventual result or canceling it.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you have short-lived effects that you want to run asynchronously, by all means fire them off and forget about them! But also know that execution is always in &lt;em&gt;some&lt;/em&gt; context, and you have techniques like &lt;code&gt;background&lt;/code&gt; to make the asynchronous lifecycle more visible.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>A Case Study in Incrementally Improving Code</title>
		<link href="https://inner-product.com/posts/a-case-study-in-incrementally-improving-code/"/>
		<updated>2021-10-09T00:00:00Z</updated>
		<id>https://inner-product.com/posts/a-case-study-in-incrementally-improving-code/</id>
		<content type="html">&lt;p&gt;In this article I’m going to go through the process of improving some code. I’m mentoring a new developer who is applying for their first job. They were asked to complete some tasks on &lt;a href=&quot;https://www.codility.com/&quot;&gt;Codility&lt;/a&gt; as the first step of the interview process. To get used to the platform they did the first example task, and I advised them on some changes. I’m writing up here the progression from their code to (what I think is) better code. (Since this is the example task, not a task used to assess applicants, I think this is ok to publically post.)&lt;/p&gt;
&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;
&lt;p&gt;First, the Codility problem:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Write a function:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Solution &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.&lt;/p&gt;
&lt;p&gt;For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. Given A = [1, 2, 3], the function should return 4. Given A = [−1, −3], the function should return 1.&lt;/p&gt;
&lt;p&gt;Write an efficient algorithm for the following assumptions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;N is an integer within the range [1…100,000];&lt;/li&gt;
&lt;li&gt;each element of array A is an integer within the range [−1,000,000…1,000,000].&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;setup&quot;&gt;Setup&lt;/h2&gt;
&lt;p&gt;I created the interface below so that I could run all the variations through the same test harness. It’s not part of the specification from Codility or the student’s original code.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;trait&lt;/span&gt; Solution &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;initial-solution&quot;&gt;Initial Solution&lt;/h2&gt;
&lt;p&gt;Here’s the student’s initial solution:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Solution1 &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Solution &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; tolis&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; b &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; Nil &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; hs &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tolis&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;hs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;filter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_ &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sorted&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;//b.sorted&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isEmpty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tolis&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;code-cleanup&quot;&gt;Code Cleanup&lt;/h2&gt;
&lt;p&gt;There are several issues with the initial solution. Let’s start with the easiest ones:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;confusing naming (what does &lt;code&gt;tolis&lt;/code&gt; mean?)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;var&lt;/code&gt; is not necessary (it could be a &lt;code&gt;val&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;messy formatting&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are fairly small points but they are easy for an interviewer to complain about. A lot of jobs, particularly entry level jobs, receive many applicants and interviewers are often looking for reasons to reject candidates. We don’t want to give them an easy reason to reject us!&lt;/p&gt;
&lt;p&gt;Here’s the code after a quick clean up.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Solution2 &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Solution &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;numbers&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;br /&gt;      numbers &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; Nil &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; xs &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;xs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;xs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; clean&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;filter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_ &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sorted&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;clean&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isEmpty&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;clean&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;head &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;clean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;testing-the-solution&quot;&gt;Testing the Solution&lt;/h2&gt;
&lt;p&gt;Before we move on to deeper issues, I want to create a test suite so we can be sure we don’t break anything during refactoring.&lt;br /&gt;
To test this function we could create a few hand-crafted cases, the programmer equivalent of banging together sticks to make fire, or we could generate test cases from a specification. A fairly simple way to generate test cases is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;create a many negative number as we like&lt;/li&gt;
&lt;li&gt;create a sequence of positive numbers, and remove one of the numbers&lt;/li&gt;
&lt;li&gt;join the two sets of numbers and shuffle&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With this construction we know the result should be the number we removed.&lt;/p&gt;
&lt;p&gt;Once we’ve setup the test suite we can proceed. I used &lt;a href=&quot;https://scalameta.org/munit/&quot;&gt;MUnit&lt;/a&gt; and its ScalaCheck integration to do the above.&lt;/p&gt;
&lt;h2 id=&quot;partial-and-total-functions&quot;&gt;Partial and Total Functions&lt;/h2&gt;
&lt;p&gt;Let’s now move on to deeper issues. I don’t like the implementation of &lt;code&gt;findLowest&lt;/code&gt;. There is some input for which it will crash—namely the empty list. In FP jargon we’d say it is a &lt;em&gt;partial function&lt;/em&gt;, not a &lt;em&gt;total function&lt;/em&gt;. The empty list case checked before it’s called, but it easy for future modifications to break this.  We could use, say, Cats’ &lt;code&gt;NonEmptyList&lt;/code&gt; type to express that this function only works with non-empty lists, but it’s not really appropriate to add a dependency in this context. We can, instead, rewrite &lt;code&gt;findLowest&lt;/code&gt; to be a total function.&lt;/p&gt;
&lt;p&gt;We can make &lt;code&gt;findLowest&lt;/code&gt; a total function by adding an extra parameter, which is the current guess for the lowest number. With this we can write &lt;code&gt;findLowest&lt;/code&gt; as a standard structural recursion and the compiler will stop complaining about our incomplete match. Here’s the code (written with Scala 3 syntax).&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Solution3 &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Solution &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; numbers&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;      numbers &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Nil &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; result&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt; xs &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; x then findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; xs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; result&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; clean&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;toList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;filter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_ &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sorted&lt;br /&gt;&lt;br /&gt;    findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; clean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;performance&quot;&gt;Performance&lt;/h2&gt;
&lt;p&gt;The requirements state they want an “efficient algorithm”. I don’t think they really mean that, but optimizing code can be fun and in this case there are some easy wins to be had. I’m going to look at two types of optimization:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;data representation, where we change how we store data to be more efficient; and&lt;/li&gt;
&lt;li&gt;algorithmic optimization, where we change the structure of the code to do less work.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The code mostly uses the &lt;code&gt;List&lt;/code&gt; datatype, which is a singly linked list. This is a poor choice for performance as it involves a lot of pointer chasing and random memory access is slow on modern computers. &lt;code&gt;List&lt;/code&gt; is appropriate when want to reason about shared data, and hence use immutable data, but in this code the data is never shared outside the method so that is not a concern.&lt;/p&gt;
&lt;p&gt;From the algorithmic perspective we are doing a lot of work:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;there is an &lt;a href=&quot;https://en.wikipedia.org/wiki/Big_O_notation&quot;&gt;O(n)&lt;/a&gt; traversal of the input to convert to a &lt;code&gt;List&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;the filtering operation is at least O(n) and may be more depending on how the filtered result is constructed;&lt;/li&gt;
&lt;li&gt;sorting is O(n log n); and&lt;/li&gt;
&lt;li&gt;the final traversal to find the lowest missing number is O(n).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;My first change is mostly concerned with data representation. By working purely with arrays we use a more cache-friendly data structure, and we can also sort in-place which avoids some allocation. Here’s the code.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;java&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;Arrays&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Solution4 &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Solution &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; idx&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; numbers&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; idx &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; numbers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length then result&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; numbers&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; then&lt;br /&gt;        findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; idx &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; numbers&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; result&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; clean&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;filter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;_ &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    Arrays&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sort&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;clean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    findLowest&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; clean&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The next step is mostly algorithmic optimization. We don’t need to sort the array, or even filter it. All we need to do is construct a data structure that tells us what numbers are present. This requires just one O(n) traversal through the input. We only need a single bit to represent presence or absence for each positive integer. The specification tells us the input will not be higher than 1,000,000. Hence we can use a bit-set consuming no more than about 125kB, which should easily fit into the L2 cache and might even squeeze into L1 cache. Once we have constructed the bit set we need a single O(n) traversal to find the lowest missing number. Here’s the code. Note I used &lt;code&gt;java.util.BitSet&lt;/code&gt; instead of &lt;code&gt;scala.collection.mutable.BitSet&lt;/code&gt; because it was a bit clearer on a quick glance which were the methods I wanted.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;java&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;Arrays&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;java&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;BitSet&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Solution5 &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Solution &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; solution&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; populateBitSet&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;        bitSet&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; BitSet&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;        idx&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;        numbers&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; BitSet &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; idx &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; numbers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length then bitSet&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; elt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; numbers&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;idx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; elt &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; then populateBitSet&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bitSet&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; idx &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; numbers&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;          bitSet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;set&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;elt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;          populateBitSet&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bitSet&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; idx &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; numbers&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; bitSet &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; populateBitSet&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;BitSet&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1000000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; bitSet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nextClearBit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    result&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I setup a quick &lt;a href=&quot;https://github.com/openjdk/jmh&quot;&gt;JMH&lt;/a&gt; benchmark to compare implementations. I was only looking for big improvements, so I’m only reporting results below for the first solution, and &lt;code&gt;Solution4&lt;/code&gt; and &lt;code&gt;Solution5&lt;/code&gt; above. As you can see the combination of data representation and algorithmic improvements yield a speed up a bit over ten times compared to the original. That’s pretty good for some fairly simple changes!&lt;/p&gt;
&lt;p&gt;[info] CodilityBenchmark.benchSolution1  thrpt    3  741.060 ± 32.291  ops/s&lt;br /&gt;
[info] CodilityBenchmark.benchSolution4  thrpt    3  1956.945 ± 62.053  ops/s&lt;br /&gt;
[info] CodilityBenchmark.benchSolution5  thrpt    3  8406.225 ± 751.966  ops/s&lt;/p&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;The process of improving the code was reasonably straight forward. The most important improvements, in my opinion, are the ones that were done first. As an interviewer I want to see code that pays attention to clarity, as I think that’s one of the most important factors in successfully growing a large code base. The optimizations I performed require some level of knowledge of data structures, computer architecture, and algorithmic complexity. All these things should be covered in a computer science course but those who haven’t studied CS can find equivalents online. My optimizations don’t require a deep level of knowledge of, for example x86-64 architecture. All these optimizations can be reasoned about with a fairly coarse machine model.&lt;/p&gt;
&lt;p&gt;All the code is on &lt;a href=&quot;https://github.com/noelwelsh/codility&quot;&gt;Github&lt;/a&gt; if you want go further, or just see how I setup the tests and benchmarks. I hope it is useful!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Functional Programming is Based</title>
		<link href="https://inner-product.com/posts/fp-is-based/"/>
		<updated>2023-03-13T00:00:00Z</updated>
		<id>https://inner-product.com/posts/fp-is-based/</id>
		<content type="html">&lt;p&gt;Function programming is based&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://inner-product.com/posts/fp-is-based/#fn1&quot; id=&quot;fnref1&quot;&gt;[1]&lt;/a&gt;&lt;/sup&gt;; that is, based on principles that allow a &lt;em&gt;systematic&lt;/em&gt; and &lt;em&gt;repeatable&lt;/em&gt; process for creating software. In this post I’m going illustrate this process with an example of summing the elements of a list, inspired by &lt;a href=&quot;https://news.ycombinator.com/item?id=35031092&quot;&gt;this conversation&lt;/a&gt;. We’ll mostly be looking at &lt;em&gt;algebraic data types&lt;/em&gt; and &lt;em&gt;structural recursion&lt;/em&gt; (which often uses &lt;em&gt;pattern matching&lt;/em&gt;, but is not synonymous with it).&lt;/p&gt;
&lt;p&gt;For me, a working functional programmer, this process is one of the main advantages of FP. It means I can spend my mental cycles on understanding the problem, knowing that once I have done so the implementation follows in a straightforward way. The inverse also holds: if someone uses these principles to write code I can easily work out what problem it solves.&lt;/p&gt;
&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;/h2&gt;
&lt;p&gt;The main concepts we’ll discuss are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;algebraic data types, which builds up data; and&lt;/li&gt;
&lt;li&gt;structural recursion, which tears down data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As the description suggests, they go together. If we use an algebraic data type to define data, we can then use structural recursion to manipulate that data.&lt;/p&gt;
&lt;p&gt;We’ll also see two reasoning principles for structural recursion, and we’ll take a quick look at the process for converting a function into a tail recursive form.&lt;/p&gt;
&lt;p&gt;Functional programming has many of these concepts, which form the functional programming equivalent of &lt;a href=&quot;https://en.wikipedia.org/wiki/Software_design_pattern&quot;&gt;design patterns&lt;/a&gt; in the object-oriented world. Like OO design patterns, they exist above the code and the representation in code often uses several language features. Unlike OO patterns they usually have a formal definition. Formality adds precision, but formal definitions are not very approachable. In my presentation below I’ll be quite informal.&lt;/p&gt;
&lt;h2 id=&quot;algebraic-data-types&quot;&gt;Algebraic Data Types&lt;/h2&gt;
&lt;p&gt;Algebraic data types are data where individual elements are combined using logical ands and logical ors. For example, we could say a &lt;code&gt;User&lt;/code&gt; data type consists of a name &lt;em&gt;and&lt;/em&gt; an email address &lt;em&gt;and&lt;/em&gt; a status. We might say a &lt;code&gt;UserStatus&lt;/code&gt; is active &lt;em&gt;or&lt;/em&gt; banned &lt;em&gt;or&lt;/em&gt; suspended. All languages I know of that have some way of declaring data using logical ands, but most lack logical ors&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://inner-product.com/posts/fp-is-based/#fn2&quot; id=&quot;fnref2&quot;&gt;[2]&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;A (singly linked) list is one of the simplest examples of an interesting algebraic data type. A &lt;code&gt;List&lt;/code&gt; with elements of type &lt;code&gt;A&lt;/code&gt; is either:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the empty &lt;code&gt;List&lt;/code&gt;, or&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;Pair&lt;/code&gt; with a head of type &lt;code&gt;A&lt;/code&gt; and a tail of type &lt;code&gt;List&lt;/code&gt; of &lt;code&gt;A&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Notice in this example we have both an &lt;em&gt;and&lt;/em&gt; and an &lt;em&gt;or&lt;/em&gt;, and that the definition of list is recursive: a list contains a list in the tail of the &lt;code&gt;Pair&lt;/code&gt; case.&lt;/p&gt;
&lt;p&gt;There is a direct translation of this structure into code. In Scala 3 we have the following patterns:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;A&lt;/code&gt; is a &lt;code&gt;B&lt;/code&gt; or &lt;code&gt;C&lt;/code&gt; we write&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; A &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; B&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; C&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;A&lt;/code&gt; is a &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt; we write&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://inner-product.com/posts/fp-is-based/#fn3&quot; id=&quot;fnref3&quot;&gt;[3]&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; B&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; C&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Taken together, and adding the syntax for generic types, we can write the definition of &lt;code&gt;List&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The point to note is that this Scala definition &lt;em&gt;follows directly&lt;/em&gt; from the abstract definition of &lt;code&gt;List&lt;/code&gt; and the patterns for algebraic data types in Scala. We could write a program to do the translation, if we so wanted.&lt;/p&gt;
&lt;p&gt;Other languages with algebraic data types follow the same principles, with their own language specific quirks. For example,&lt;br /&gt;
in O’Caml we write type declarations backwards, with the generic type variable before the concrete type it applies to.&lt;/p&gt;
&lt;pre class=&quot;language-ocaml&quot;&gt;&lt;code class=&quot;language-ocaml&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token type-variable function&quot;&gt;&#39;a&lt;/span&gt; list &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Empty &lt;br /&gt;  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Pair &lt;span class=&quot;token keyword&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;token type-variable function&quot;&gt;&#39;a&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token type-variable function&quot;&gt;&#39;a&lt;/span&gt; list&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In Rust we must add indirection for the recursion in the form of a &lt;code&gt;Box&lt;/code&gt;, and worse, indent by 4 spaces instead of 2.&lt;/p&gt;
&lt;pre class=&quot;language-rust&quot;&gt;&lt;code class=&quot;language-rust&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;token type-definition class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token class-name&quot;&gt;Empty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token class-name&quot;&gt;Pair&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Box&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can also express algebraic data types in languages that don’t directly support them. In this case we have to rely on conventions instead of language support.&lt;/p&gt;
&lt;h2 id=&quot;structural-recursion&quot;&gt;Structural Recursion&lt;/h2&gt;
&lt;p&gt;Structural recursion is the complement to algebraic data types. Algebraic data types tell us how to construct data. Structural recursion tells us how to transform that data into something else. In other words, how to deconstruct data. Any time we are working with an algebraic data type we can use structural recursion.&lt;/p&gt;
&lt;p&gt;Structural recursion is often implemented using pattern matching, but pattern matching is not synonymous with structural recursion. We can implement structural recursion in other ways, and implement things that are not structural recursion using pattern matching. I’m going to use pattern matching in the example here, but keep this point in mind.&lt;/p&gt;
&lt;p&gt;Given we have two patterns for structural recursion, ands and ors, we need two patterns for structural recursion. Once again, I’m using Scala 3 here.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If &lt;code&gt;A&lt;/code&gt; is a &lt;code&gt;B&lt;/code&gt; or &lt;code&gt;C&lt;/code&gt; we write&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;anA &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; B &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; C &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If &lt;code&gt;A&lt;/code&gt; is a &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt; we write&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;anA &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The right-hand side of the &lt;code&gt;match&lt;/code&gt; expressions, which I’ve filled in with &lt;code&gt;???&lt;/code&gt;, is where we implement our problem specific functionality.&lt;/p&gt;
&lt;p&gt;There is one very important additional rule: when the data is recursive, the method we’re implementing is recursive in the same place.&lt;/p&gt;
&lt;h2 id=&quot;an-example&quot;&gt;An Example&lt;/h2&gt;
&lt;p&gt;Let’s see how structural recursion applies to implementing a method to sum the element in a list of integers. We start by defining the method header.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first step is to recognize that &lt;code&gt;list&lt;/code&gt; is an algebraic data type, and therefore we can use structural recursion to convert this list into the &lt;code&gt;Int&lt;/code&gt; output we’re after. We start by filling out the structural recursion skeleton.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a direct application of the two structural recursion patterns to the &lt;code&gt;List&lt;/code&gt; algebraic data type. However, I’ve forgotten one thing, the recursion rule! &lt;code&gt;List&lt;/code&gt; is recursive in the &lt;code&gt;tail&lt;/code&gt; of &lt;code&gt;Pair&lt;/code&gt;, so &lt;code&gt;sum&lt;/code&gt; should be recursive there. Let’s add that, giving us&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I’ve kept the &lt;code&gt;???&lt;/code&gt; in place, indicating that we haven’t quite finished yet. This is as far as structural recursion on its own will take us, but we can finish the implementation with two reasoning principles.&lt;/p&gt;
&lt;h2 id=&quot;reasoning-about-structural-recursion&quot;&gt;Reasoning about Structural Recursion&lt;/h2&gt;
&lt;p&gt;To finish the implementation we can use two principles for reasoning about structural recursion:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;we can consider each case independently; and&lt;/li&gt;
&lt;li&gt;we can assume any recursive calls will return the correct value.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Starting with the code&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;we’ll first look at the case for &lt;code&gt;Empty&lt;/code&gt;. Remember we can consider it independently of the &lt;code&gt;Pair&lt;/code&gt; case. So we simply need to ask ourselves “what is the &lt;code&gt;sum&lt;/code&gt; of the empty list?” Zero is the only sensible answer here.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we move on to the &lt;code&gt;Pair&lt;/code&gt; case, where we can use the reasoning principle for recursion: assume it returns the value it should, which is the sum of the tail of the list. We can do this because the recursion comes from the recursion rule for structural recursion. So long as we take care of the non-recursive parts, the recursion is guaranteed to be correct.&lt;/p&gt;
&lt;p&gt;Given this, we ask ourselves “if we have the sum of the tail of the list, what should sum of the tail and the head be?” The answer is to add the head to the sum of the tail.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; head &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With that we are done!&lt;/p&gt;
&lt;p&gt;Notice that every step of the development of this method is justified by a general principle. This is what I mean by functional programming providing a systematic and repeatable process. Systematic because the process guides us along every step, and repeatable because we can apply the same process in many situations.&lt;/p&gt;
&lt;h2 id=&quot;tail-recursion&quot;&gt;Tail Recursion&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;sum&lt;/code&gt; method is not tail recursive, but once again there is a process to transform it into a tail recursive version. The steps of the transform&lt;sup class=&quot;footnote-ref&quot;&gt;&lt;a href=&quot;https://inner-product.com/posts/fp-is-based/#fn4&quot; id=&quot;fnref4&quot;&gt;[4]&lt;/a&gt;&lt;/sup&gt; are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Identify what will summarize the data we’ve already seen at any point in the process. Here that is the partial sum of the list elements we’ve seen so far.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add an additional method parameter to hold this value, usually called an accumulator.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; accum&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; head &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Change any base cases to return the accumulator.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; accum&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; accum&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; head &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Change any recursive cases to update the accumulator and perform the recursion in tail position.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;list&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; accum&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  list &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Empty &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; accum&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Pair&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;head&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; tail&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; sum&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tail&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; head &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; accum&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This explanation could do with more detail. I’ve skipped over the definition of tail position, for example. My goal here is to illustrate the process to someone who has already used it but is perhaps cannot articulate the underlying steps, rather than teach the process to someone who has no prior knowledge.&lt;/p&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;We’ve seen the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;algebraic data types for modelling data expressed in terms or logical ands and ors;&lt;/li&gt;
&lt;li&gt;structural recursion for transforming algebraic data types;&lt;/li&gt;
&lt;li&gt;reasoning principles for completing structural recursions; and&lt;/li&gt;
&lt;li&gt;conversion to tail recursive form.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is a lot in this simple example. The most important point is that there is a process that explains every step of creating the method we’re after. This same process scales up to complex problems like compilers and &lt;a href=&quot;https://github.com/creativescala/doodle/blob/main/image/shared/src/main/scala/doodle/image/Image.scala&quot;&gt;graphics&lt;/a&gt;. This is a contrast to how I was taught imperative programming, and I think most programmers think about code, which is as a random collection of language features that combine in some ineffable way to produce working programs. In the context of the conversation that motivated this post, there is no need to even think about things like early returns. They simply aren’t a concept that is needed when you can produce working code using the patterns I’ve described.&lt;/p&gt;
&lt;p&gt;The second point is applicable to languages that are adding “functional programming” features. For example, Python has recently added pattern matching. The real benefit of pattern matching, in my opinion, is not as a language feature on its own but as a tool for implementing structural recursion. This usually comes with compiler support for checking that all cases have patterns, known as exhaustivity checking. It is a mistake to think of functional programming as a collection of language features. The language features are in service to deeper ideas.&lt;/p&gt;
&lt;p&gt;The third point is that functional programming is full of great stuff like this. The academic community has mostly done a terrible job communicating it to industry. For example, I believe algebraic data types and structural recursion were first explored in the antecedents of &lt;a href=&quot;https://ris.utwente.nl/ws/files/6142047/db-utwente-40501F46.pdf&quot;&gt;Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire&lt;/a&gt;. This paper has some of the most opaque notation I’ve ever read, and does a incredibly poor job of conveying incredibly interesting ideas. A notable exception is &lt;a href=&quot;https://htdp.org/&quot;&gt;How to Design Programs&lt;/a&gt;, which presents algebraic data types and structural recursion in a way that completely new programmers can understand. I believe that making these patterns more accessible is the next step in the growth of functional programming.&lt;/p&gt;
&lt;hr class=&quot;footnotes-sep&quot; /&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn1&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;“Based” is a term for something that is good or true, according to my children. &lt;a href=&quot;https://inner-product.com/posts/fp-is-based/#fnref1&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn2&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;Go is a great example of the problems caused by the absence of logical ors. Go functions return a success &lt;em&gt;and&lt;/em&gt; an error value. The logical design would be to have function returning success &lt;em&gt;or&lt;/em&gt; error values. Go’s design forces it to include a null value, the billion dollar mistake, because there needs to be some value to return for the success case when an error means there is no sensible value to return. Algebraic data types were fully formed by &lt;a href=&quot;https://smlfamily.github.io/history/SML-history.pdf&quot;&gt;1983 when Standard ML was created&lt;/a&gt;, while development on Go started 24 years later in 2007. One of the “primary considerations” for Go is &lt;a href=&quot;https://go.dev/talks/2012/splash.article#TOC_6.&quot;&gt;“it must be modern”&lt;/a&gt;. &lt;a href=&quot;https://inner-product.com/posts/fp-is-based/#fnref2&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn3&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;There is an annoying irregularity in Scala 3’s syntax. For a case inside an &lt;code&gt;enum&lt;/code&gt; we write what is given, but outside an &lt;code&gt;enum&lt;/code&gt; we use a &lt;code&gt;final case class&lt;/code&gt; instead. &lt;a href=&quot;https://inner-product.com/posts/fp-is-based/#fnref3&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;fn4&quot; class=&quot;footnote-item&quot;&gt;&lt;p&gt;The full transform is known as continuation passing style and is a bit more complex than what I present here. &lt;a href=&quot;https://inner-product.com/posts/fp-is-based/#fnref4&quot; class=&quot;footnote-backref&quot;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content>
	</entry>
	
	<entry>
		<title>Understanding Virtual Machine Dispatch through Duality</title>
		<link href="https://inner-product.com/posts/understanding-vm-dispatch/"/>
		<updated>2023-12-19T00:00:00Z</updated>
		<id>https://inner-product.com/posts/understanding-vm-dispatch/</id>
		<content type="html">&lt;p&gt;For the next edition of &lt;a href=&quot;https://www.scalawithcats.com/&quot;&gt;Scala with Cats&lt;/a&gt; I’m writing a section on implementing interpreters. In my research I ended up going fairly deep down the rabbit hole of optimizations, in which virtual machine dispatch is a major area. There are many different approaches to dispatch, and I struggled to relate them until I realized they were all variations on a basic structure that resulted from applying the principle of duality. Duality is one of the major themes of the book, so I was happy to make this discovery. However I think going deep into optimization is not appropriate for the book so I’m writing this up here instead.&lt;/p&gt;
&lt;p&gt;I’m going to first describe duality, then give context on interpreters and virtual machines, before moving on to the application of duality in virtual machine dispatch.&lt;/p&gt;
&lt;h2 id=&quot;duality&quot;&gt;Duality&lt;/h2&gt;
&lt;p&gt;Duality, in mathematics, means making a direct correspondence between one structure and another structure. This in turn allows us to apply what we know from one structure to the other. There are many dualities in programming, which allows us to see different code structures as alternative implementations of some underlying concept. For example, function calls are dual to function returns. If we have a function that returns a value, like &lt;code&gt;f&lt;/code&gt; in&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; f&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;we can, instead of returning that value, pass it to another function that does what we wanted to do with the value that was returned.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; f&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; a &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Asynchronous code using callbacks looks like this second style, and if you’re a programming language theory person you will recognize it as continuation passing style.&lt;/p&gt;
&lt;p&gt;Duality doesn’t mean the two things are identical. Although the two examples above compute the same result they differ in other ways. The obvious difference is that in the second example the function &lt;code&gt;f&lt;/code&gt; has two parameters. There is another difference. Function calls consume a stack frame, which a subsequent return frees. If we replace returns with calls we’ll never free any stack frames and so overflow the stack. Solving this leads us to tail calls.&lt;/p&gt;
&lt;p&gt;A tail call is a function call that doesn’t consume a stack frame. A tail call can only replace a normal function call if the call is in tail position. A function call is in tail position if the result of the call is immediately returned. So the call to &lt;code&gt;f&lt;/code&gt; below is in tail position.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However the call to &lt;code&gt;h&lt;/code&gt; below is not, because the result is used in the subsequent addition.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; h&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; h&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Tail calls will be important is what comes later. To make them explicit I’ll going to invent a &lt;code&gt;tailcall&lt;/code&gt; keyword to explicitly indicate them. So I’ll write&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; tailcall f&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;to explicitly indicate that &lt;code&gt;f&lt;/code&gt; is being called using a tail call.&lt;/p&gt;
&lt;p&gt;There is another duality that we’ll need: that between data and functions. Wherever we have data, we can replace it with a function that does whatever it is we want to do with that data. For example, imagine choosing a contact to message from a list of contacts on your mobile phone. Instead of representing each contact as data containing a name, phone number, and so on, we can represent them as the action that initiates a message with them. We can extend this further to say the duality is between describing something in terms of what it is and in terms of what we can do with it. Describing what something is gives us data, while describing what we can do with it gives us functions (or objects, or codata depending on the terminology you want to use.) Again, this doesn’t mean that the two approaches are identical. They can achieve the same goal, but do it in different ways and have different properties.&lt;/p&gt;
&lt;h2 id=&quot;bytecode-interpreters-and-virtual-machines&quot;&gt;Bytecode Interpreters and Virtual Machines&lt;/h2&gt;
&lt;p&gt;There are two common ways to implement an interpreter: by directly interpreting an abstract syntax tree, known as a tree-walking interpreter, or by compiling the abstract syntax tree to a linear sequence of instructions and then interpreting these instructions. This later case is known as a bytecode interpreter, as the instruction often, but not always, take up a single byte.&lt;/p&gt;
&lt;p&gt;The instructions for a bytecode interpreter describe actions of some virtual machine. There are two common types of virtual machine: register machines and stack machines, of which stack machines are the most common. If we consider the code that implements each instruction as a function, in a stack machine these functions receive parameters from and return results to a stack. An instruction to add two integers would therefore pop two values from the stack, add them, and push the result back. Stack machines are simple to implement, but as this short example illustrates there can be considerable inefficiencies in moving values to and from the stack.&lt;/p&gt;
&lt;p&gt;Imagine we’re implementing an interpreter for simple arithmetic. Our language consists of literals, addition, subtraction, multiplication, and division. In Scala we might define stack machine bytecode as&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; ByteCode &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Lit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Add&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Sub&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Mul&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Div&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that the majority of operations have no parameters: these values come from the stack. In a register machine, the operations would have to specify registers where they find their parameters and the register where they store results.&lt;/p&gt;
&lt;h2 id=&quot;interpreter-dispatch&quot;&gt;Interpreter Dispatch&lt;/h2&gt;
&lt;p&gt;The core of a bytecode interpreter is instruction dispatch: choosing an instruction and then choosing the action to take given the instruction. Instruction dispatch has three components:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;instruction fetch, which chooses the instruction to execute;&lt;/li&gt;
&lt;li&gt;the actual dispatch, the chooses the action to take given the instruction that has been fetched; and&lt;/li&gt;
&lt;li&gt;some kind of loop to continue the above two processes until the end of the program, if any, is reached.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Switch dispatch is the most common form of dispatch. This is named after the C language &lt;code&gt;switch&lt;/code&gt; statement used to implement the dispatch component. Instruction fetch is often just an array reference, and the loop is usually an infinite &lt;code&gt;while&lt;/code&gt; or &lt;code&gt;for&lt;/code&gt; loop. In a functional language the loop would usually be a tail recursive function and the switch would usually be implemented by a pattern match over the instructions. More abstractly, instructions are an algebraic data type and switch dispatch is a structurally recursive loop.&lt;/p&gt;
&lt;p&gt;Here’s a complete interpreter in Scala illustrating switch dispatch.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// The stack&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; stack&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// The program, a sequence of instructions&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; instructions&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ByteCode&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// sp is the *stack pointer*&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// Its value is the index of the first free location in the stack&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;//&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// ip is the *instruction pointer*&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// Its value is the index of the current instruction in instructions&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; dispatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ip&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// This simple language has no control flow, so we stop when&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// we reach the end of the instructions&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;// Fetch instruction&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; ins &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;// Dispatch&lt;/span&gt;&lt;br /&gt;    ins &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Lit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;        stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; value&lt;br /&gt;        loop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        loop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sub &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        loop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Mul &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        loop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Div &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        loop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;dispatch-through-the-lens-of-duality&quot;&gt;Dispatch Through the Lens of Duality&lt;/h2&gt;
&lt;p&gt;The usual presentation of alternatives to switch dispatch (direct threading, indirect threading, and so on; we’ll see them in a moment) in my opinion confuses the code level realization and the conceptual level. For example, discussion of direct threading often talks about labels-as-values, a GCC extension. The differences between dispatch methods only became clear to me when I saw them as different realizations of the three components making up dispatch that utilized the dualities I’ve described earlier.&lt;/p&gt;
&lt;p&gt;Starting with switch dispatch described above, let’s replace the bytecode instructions with functions that carry out the instructions, using the duality between data and functions. This gives us code like the following (where I’m leaving out bits that are unchanged from the previous code.)&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; instructions&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Op&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; sp&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// An operation is a function () =&gt; Unit&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Op &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Function0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Lit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; value&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Add &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Sub &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Mul &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Div &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; dispatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;// Fetch instruction&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; ins &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;// Dispatch&lt;/span&gt;&lt;br /&gt;    ins&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token comment&quot;&gt;// Loop&lt;/span&gt;&lt;br /&gt;    loop&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is known as subroutine threading in the literature. Notice that instructions can have parameters, like the &lt;code&gt;Lit&lt;/code&gt; instruction above, and access the &lt;code&gt;sp&lt;/code&gt; variable in their lexical environment. This means we need closures, or some equivalent representation. The literature often talks about how instruction parameters can be stored on the instruction stack alongside the instructions, but this detail, while important for performance, is not important to understanding the core of the concept.&lt;/p&gt;
&lt;p&gt;Now we will utilize the duality between calls and returns to replace the returns in the instruction functions with (tail) calls to the next instruction. This moves the instruction fetch and dispatch into the instructions, giving us what is known as direct threading. Now the loop is implicit, spread throughout the instruction as a sequence of tail calls. The follow code illustrates direct threaded dispatch (where I’m again just showing the important bits.)&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; instructions&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Op&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; sp&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; ip&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// An operation is a function () =&gt; Unit&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;sealed&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Op &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Function0&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Lit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; value&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall instructions&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Add &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall instructions&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Sub &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall instructions&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Mul &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall instructions&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Div &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; Op &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall instructions&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is one variant left, which is to keep the instructions as bytecode data but use tail calls in instruction dispatch. This gives us indirect threaded dispatch.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; instructions&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Array&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;ByteCode&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; sp&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; ip&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; lit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; value&lt;br /&gt;  sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall dispatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instruction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall dispatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instruction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; sub&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall dispatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instruction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall dispatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instruction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; div&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  sp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  ip &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; ip &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; instructions&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;size then stack&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sp &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; tailcall dispatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instruction&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ip&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; dispatch&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;instruction&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ByteCode&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  instruction &lt;span class=&quot;token keyword&quot;&gt;match&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Lit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;      tailcall lit&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Add &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;      tailcall add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Sub &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;      tailcall sub&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Mul &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;      tailcall mul&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; Op&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Div &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;      tailcall div&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;We’ve seen the four main variants of dispatch can all be explained by considering dualities between data and functions for instructions and calls and returns for looping:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;data / call and return: switch dispatch&lt;/li&gt;
&lt;li&gt;data / tail call: indirect threaded dispatch&lt;/li&gt;
&lt;li&gt;function / call and return: subroutine dispatch&lt;/li&gt;
&lt;li&gt;function / tail call: direct threaded dispatch&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To me using duality to understand these different techniques is very satisfying. It means I can keep one mental model that is realized in different ways depending on the programming language features available. For example, if I have full tail calls (or some equivalent, like labels-as-values) then I can implement direct threaded dispatch. Without full tail calls, perhaps I use subroutine threaded dispatch instead (which I can view of as trampolining direct threaded dispatch, but that’s a slightly different story.) In this conceptual approach resonates with you, I encourage you to signup for the &lt;a href=&quot;https://www.scalawithcats.com/&quot;&gt;book’s newsletter&lt;/a&gt;, where I’m sharing copies of the second edition.&lt;/p&gt;
&lt;p&gt;You may also wonder about performance. I’m my experiments subroutine dispatch is about sixty thousand times (yes, really) faster than switch dispatch for a very simple benchmark. If you’re interested in the code, I have a &lt;a href=&quot;https://github.com/scalawithcats/stack-machine&quot;&gt;repository&lt;/a&gt; with all the experiments I tried. I’m still exploring these performance differences, but my best guess is that subroutine threaded dispatch is easier for the Hotspot compiler to optimize, which brings far larger performance improvements than anything that interpreter optimizations can bring. Writing an interpreter to work with a JIT compiler is itself an interesting problem, and one that seems under-explored, but that’s a topic for a different post.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Essential Effects book updated!</title>
		<link href="https://inner-product.com/posts/essential-effects-now-with-cats-effect-3/"/>
		<updated>2024-03-14T00:00:00Z</updated>
		<id>https://inner-product.com/posts/essential-effects-now-with-cats-effect-3/</id>
		<content type="html">&lt;p&gt;Hi there, and happy π day!&lt;/p&gt;
&lt;p&gt;I just finished updating “Essential Effects” to Cats Effect 3. This has been the #1 most important task for me, along with being the most important request from readers. Next I will be making the final edits while adding an additional case study.&lt;/p&gt;
&lt;p&gt;If you’ve purchased the book already, updated copies have been sent out. If you haven’t gotten the book yet, hopefully this update will make you more interested. There’s &lt;a href=&quot;https://essentialeffects.dev/essential-effects-excerpt.pdf&quot;&gt;an excerpt&lt;/a&gt; available if you want to take a look, containing an extra discount code. (Shh, it’s &lt;a href=&quot;https://innerproductllc.gumroad.com/l/essentialeffects/ee-ce3&quot;&gt;&lt;code&gt;ee-ce3&lt;/code&gt;&lt;/a&gt;!)&lt;/p&gt;
&lt;p&gt;As always, the main site is &lt;a href=&quot;https://essentialeffects.dev/&quot;&gt;essentialeffects.dev&lt;/a&gt;. Let me know what you think!&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Direct-style Effects Explained</title>
		<link href="https://inner-product.com/posts/direct-style-effects/"/>
		<updated>2024-04-24T00:00:00Z</updated>
		<id>https://inner-product.com/posts/direct-style-effects/</id>
		<content type="html">&lt;p&gt;Direct-style effects, also known as algebraic effects and effect handlers, are the next big thing in programming languages. They are already available in &lt;a href=&quot;https://www.unison-lang.org/docs/fundamentals/abilities/&quot;&gt;Unison&lt;/a&gt; and &lt;a href=&quot;https://github.com/ocaml-multicore/ocaml-effects-tutorial&quot;&gt;OCaml&lt;/a&gt;, are coming to &lt;a href=&quot;https://www.youtube.com/watch?v=0Fm0y4K4YO8&quot;&gt;Scala&lt;/a&gt;, and I’m seeing discussion about them in other &lt;a href=&quot;https://without.boats/blog/coroutines-and-effects/&quot;&gt;closely-related-to-industry contexts&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The goal is very simple: to allow us to write code in a natural style without monads, but still get the benefits of reasoning and composition that monads bring. At the same time I see &lt;a href=&quot;https://twitter.com/debasishg/status/1780636969841914279&quot;&gt;some&lt;/a&gt; &lt;a href=&quot;https://twitter.com/channingwalton/status/1780517826505166989&quot;&gt;confusion&lt;/a&gt; about direct-style effects. In this post I want to address this confusion by explaining the what, the why, and the how of direct-style effects using a Scala 3 implementation as an example.&lt;/p&gt;
&lt;p&gt;There is quite a bit going on here. First we’ll talk about the problem we’re trying to solve and the constraints we’re operating under. Then we’ll look at a simple implementation in Scala 3 and describe the language feature, contextual functions, that enables it. Next up we’ll see some shortcomings of this implementation and see how they can solved by two language features, one well known (delimited continuations) and one in development (type system innovations). Finally I’ll give some pointers to more about information on this topic.&lt;/p&gt;
&lt;!-- more --&gt;
&lt;h2 id=&quot;what-we-care-about&quot;&gt;What We Care About&lt;/h2&gt;
&lt;p&gt;When we argue for one programming style over alternatives we are making a value judgement about programming. It is helpful to be explicit about what those values are. As I’ve written &lt;a href=&quot;https://noelwelsh.com/posts/what-and-why-fp/&quot;&gt;elsewhere&lt;/a&gt;, I believe the core values of functional programming are &lt;strong&gt;reasoning&lt;/strong&gt; and &lt;strong&gt;composition&lt;/strong&gt;. Side effects stop us achieving both of these, but every useful program must interact with the world in some way. (If you’re uncertain what is meant by a side effect, &lt;a href=&quot;https://www.creativescala.org/creative-scala/substitution/index.html&quot;&gt;this chapter of Creative Scala&lt;/a&gt; goes into detail.) Therefore, replacing side effects with something in keeping with these core principles is considered an important problem in functional programming. Solutions to this problem are called &lt;strong&gt;effect systems&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Nota bene: in this post I use the term &lt;em&gt;side effect&lt;/em&gt; for uncontrolled effects, and just &lt;em&gt;effect&lt;/em&gt; for effects that are controlled in a more desirable way.&lt;/p&gt;
&lt;p&gt;Monads are the most common effect system in modern functional programming, but this doesn’t mean they are the only approach. Older versions of Haskell used streams. The &lt;a href=&quot;https://wiki.clean.cs.ru.nl/Language_features&quot;&gt;Clean&lt;/a&gt; language uses uniqueness types, which are very closely related to the affine types seen in Rust’s borrow checker. Most current research work focuses on what are called &lt;strong&gt;algebraic effects&lt;/strong&gt; and &lt;strong&gt;effect handlers&lt;/strong&gt;. It’s this kind of approach we will be exploring, though we have some background to get through first.&lt;/p&gt;
&lt;p&gt;Now we known why effect systems are interesting, let’s look at some of the design choices in effect systems.&lt;/p&gt;
&lt;h2 id=&quot;the-design-space-of-effect-systems&quot;&gt;The Design Space of Effect Systems&lt;/h2&gt;
&lt;p&gt;Reasoning and composition are non-negotiable criteria for any effect system. There are other criteria that are desirable, however. Here we will look at the style in which code is written, the separation between description and action, and some of the nuances in how effect systems can help us reason about and compose effectful code.&lt;/p&gt;
&lt;h3 id=&quot;direct-and-monadic-style&quot;&gt;Direct and Monadic Style&lt;/h3&gt;
&lt;p&gt;The style of code that we have to write to use the effect system is a major determinant of how usable the system is. If an effect system requires too much work from the programmer it is unusable in practice, no matter what other properties it has. Here we will look at &lt;strong&gt;direct style&lt;/strong&gt;, which is code as we want to write it, and &lt;strong&gt;monadic style&lt;/strong&gt;, which is code as monadic effect systems force us to write it.&lt;/p&gt;
&lt;p&gt;Direct style code is code as it is usually written. You call functions, they return results, and you use those results in further computations. Here’s the kind of code we write in direct style.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; c &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; doSomething2&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; d &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; doSomething3&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We don’t need to say much about direct style, other than that it is desirable to write in this style.&lt;/p&gt;
&lt;p&gt;As most Scala programmers will have experienced, we must write code in a different style if we are to use monads. In monadic style the code above ends up looking something like&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;flatMap&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is considered annoying enough that languages that support monads usually provide special syntax for them. In Scala we can write&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  b &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  c &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  d &lt;span class=&quot;token keyword&quot;&gt;&amp;lt;-&lt;/span&gt; doSomething&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;yield&lt;/span&gt; d&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This isn’t too bad. Lots of developers have written code like this. However, it’s still a different style of coding that has been learned, and hence a barrier to entry. It’s also a whole program transform. Once one part of our code start using monads, it is usually the case that all of our code has to be transformed to monadic style. So ideally an alternative effect system would allow us to continue to write in direct style.&lt;/p&gt;
&lt;h3 id=&quot;description-and-action&quot;&gt;Description and Action&lt;/h3&gt;
&lt;p&gt;Any effect system must have a separation between describing the effects that should occur, and actually carrying out those effects. This is a requirement of composition. Consider perhaps the simplest effect in any programming language: printing to the console. In Scala we can accomplish this as a side effect with &lt;code&gt;println&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;OMG, it&#39;s an effect&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Imagine we want to compose the effect of printing to the console with the effect that changes the color of the text on the console. With the &lt;code&gt;println&lt;/code&gt; side effect we cannot do this. Once we call &lt;code&gt;println&lt;/code&gt; the output is already printed; there is no opportunity to change the color.&lt;/p&gt;
&lt;p&gt;Let me be clear that the goal is &lt;em&gt;composition&lt;/em&gt;. We can certainly use two side effects that happen to occur in the correct order to get the output with the color we want.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;\u001b[91m&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Color code for bright red text&lt;/span&gt;&lt;br /&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;OMG, it&#39;s an effect&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However this is not the same thing as composing an effect that combines these two effects. For example, the example above doesn’t reset the foreground color so all subsequent output will be bright red. This is the classic problem of side effects: they have “action at a distance” meaning one part of the program can change the meaning of another part of the program. This in turns means we cannot reason locally, nor can we build programs in a compositional way.&lt;/p&gt;
&lt;p&gt;What we really want is to write code like&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;Effect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;OMG, it&#39;s an effect&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foregroundBrightRed&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which limits the foreground colour to just the given text. We can only do if we have a separation between describing the effect, as we have done above, and actually running it.&lt;/p&gt;
&lt;h3 id=&quot;reasoning-and-composing-with-effects&quot;&gt;Reasoning and Composing with Effects&lt;/h3&gt;
&lt;p&gt;Effect systems should help us reason about what code does. Take for example, the following method signature:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; cthulhuFhtagn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What happens when we call this method? Returning &lt;code&gt;Unit&lt;/code&gt; suggests it has some side-effect, but what is that side-effect? It could print to the console, raise an exception, or wake a Great Old One to destroy the Earth. We cannot tell.&lt;/p&gt;
&lt;p&gt;Using the &lt;code&gt;IO&lt;/code&gt; monad is similar. If we instead see the method signature&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; cthulhuFhtagn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;we again don’t know what effects will occur but we do have some way to manipulate those effects. We can attempt to cancel the effects, for example, by writing&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;IO&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;canceled &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; cthulhuFhtagn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or instead recover from errors using &lt;code&gt;handleError&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It’s important to note that we can do this manipulation of effects in a composable way. For instance, we can pass the &lt;code&gt;IO&lt;/code&gt; to some other method that chooses how to manipulate it.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; cancelOrRecover&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;effect&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// Continue only if the stars are right&lt;/span&gt;&lt;br /&gt;  IO&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;realTimeInstant&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;time &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; starsAreRight&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;time&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ifM&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; effect&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;handleError&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; IO&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;cancel &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; effect &lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;cancelOrRecover&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cthulhuFhtagn&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We cannot do this in the first case that uses side-effects.&lt;/p&gt;
&lt;p&gt;Before we get into effect systems, there another issue I want to quickly deal with, which is composition of effects. One criticism of &lt;code&gt;IO&lt;/code&gt; is that it lumps all effects into one type. We might want to be more precise, and say, for example, &lt;em&gt;this&lt;/em&gt; method requires logging and database access, while &lt;em&gt;that&lt;/em&gt; method reads from the keyboard and prints to the screen. Monad transformers are one way to achieve this, but they are difficult to use. A more common alternative is tagless final. The method signature&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; cthulhuFhtagn&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;_&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; WakeGreatOldOne&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; F&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;indicates this method requires a &lt;code&gt;WakeGreatOldOne&lt;/code&gt; effect, which we might use to decide to not call the method. Tagless final is also inconvenient, but not so inconvenient to stop it becoming relatively common in the Scala world.&lt;/p&gt;
&lt;h2 id=&quot;direct-style-effect-systems-in-scala-3&quot;&gt;Direct-style Effect Systems in Scala 3&lt;/h2&gt;
&lt;p&gt;Let’s now implement a direct-style effect system in Scala 3. This requires some machinery that is new in Scala 3. Since that’s probably unfamiliar to many readers we’re going to start with an example, explain the programming techniques behind it, and then explain the concepts it embodies.&lt;/p&gt;
&lt;p&gt;Our example is a simple effect system for printing to the console. The implementation is below. You can save this in a file (called, say, &lt;code&gt;Print.scala&lt;/code&gt;) and run it with &lt;code&gt;scala-cli&lt;/code&gt; with the command &lt;code&gt;scala-cli Print.scala&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;//&gt; using scala 3&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// For convenience, so we don&#39;t have to write Console.type everywhere.&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; Console &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Console &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;print&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;/** Insert a prefix before `print` */&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; prefix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;first&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    Print &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      first&lt;br /&gt;      print&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;/** Use red foreground color when printing */&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; red&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    Print &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RED&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; print&lt;br /&gt;      Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RESET&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      result&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Print &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; print&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;msg&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; c&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Console&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    c&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;msg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;msg&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; c&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Console&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    c&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;msg&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; run&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;print&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;given&lt;/span&gt; c&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Console &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Console&lt;br /&gt;    print&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;/** Constructor for `Print` values */&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; body&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Console &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    body&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@main&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; go&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// Declare some `Prints`&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; message&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello from direct-style land!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// Composition&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; red&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Amazing!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;prefix&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&gt; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;red&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// Make some output&lt;/span&gt;&lt;br /&gt;  Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;message&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;red&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;Print[A]&lt;/code&gt; is a description: a program that when run may print to the console and also compute a value of type &lt;code&gt;A&lt;/code&gt;. It is implemented as a &lt;a href=&quot;https://docs.scala-lang.org/scala3/reference/contextual/context-functions.html&quot;&gt;context function&lt;/a&gt;. You can think of a context function as a normal function with &lt;code&gt;given&lt;/code&gt; (implicit) parameters. In our case a &lt;code&gt;Print[A]&lt;/code&gt; is a context function with a &lt;code&gt;Console&lt;/code&gt; given parameter. (&lt;code&gt;Console&lt;/code&gt; is a type in the Scala standard library.)&lt;/p&gt;
&lt;p&gt;Context function types have a special rule that makes constructing them easier: a normal expression will be converted to an expression that produces a context function if the type of the expression is a context function. Let’s unpack that by seeing how it works in practice. In the example above we have the line&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; message&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello from direct-style land!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;Print.println&lt;/code&gt; is an expression with type &lt;code&gt;Unit&lt;/code&gt;, not a context function type. However &lt;code&gt;Print[Unit]&lt;/code&gt; is a context function type. This type annotation causes &lt;code&gt;Print.println&lt;/code&gt; to be converted to a context function type. You can check this yourself by removing the type annotation:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; message &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Hello from direct-style land!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will not compile.&lt;/p&gt;
&lt;p&gt;We use the same trick with &lt;code&gt;Print.apply&lt;/code&gt;, which is a general purpose constructor. You can call &lt;code&gt;apply&lt;/code&gt; with any expression and it will be converted to a context function. (As far as I know it is not essential to use &lt;code&gt;inline&lt;/code&gt;, but all the examples I learned from do this so I do it as well. I assume it is an optimization.)&lt;/p&gt;
&lt;p&gt;Running a &lt;code&gt;Print[A]&lt;/code&gt; uses another bit of special sauce: if there is given value of the correct type in scope of a context function, that given value will be automatically applied to the function. This is also what makes direct-style composition, an example of which is shown below, work. The calls to &lt;code&gt;Print.print&lt;/code&gt; are in a context where a &lt;code&gt;Console&lt;/code&gt; is available, and so will be evaluated once the surrounding context function is run.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; red&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Print&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  Print &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RED&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; print&lt;br /&gt;    Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;RESET&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    result&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s the mechanics of how direct-style effect systems work in Scala: it all comes down to context functions.&lt;/p&gt;
&lt;p&gt;Notice what we have in these examples: we write code in the natural direct style, but we still have an informative type, &lt;code&gt;Print[A]&lt;/code&gt;, that helps us reason about effects and we can compose together values of type &lt;code&gt;Print[A]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I’m going to deal with composition of different effects and more in just a bit. First though, I want describe the concepts behind what we’ve done.&lt;/p&gt;
&lt;p&gt;Notice in direct-style effects we split effects into two parts: context functions that define the effects we need, and the actual implementation of those effects. In the literature these are called algebraic effects and effect handlers respectively. This is an important difference from &lt;code&gt;IO&lt;/code&gt;, where the same type indicates the need for effects and provides the implementation of those effects.&lt;/p&gt;
&lt;p&gt;Also notice that we use the argument type of context functions to indicate the effects we need, rather the result type as in monadic effects. This difference avoids the &lt;a href=&quot;https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/&quot;&gt;“colored function”&lt;/a&gt; problem with monads. We can think of the arguments as specifying requirements on the environment or context in which the context functions, hence the name.&lt;/p&gt;
&lt;p&gt;Now let’s look at composition of effects, and effects that modify control flow.&lt;/p&gt;
&lt;h3 id=&quot;composition-of-direct-style-effects&quot;&gt;Composition of Direct-Style Effects&lt;/h3&gt;
&lt;p&gt;Direct-style effects compose in a straightforward way: we just add additional parameters to our context function. Here’s a simple example that defines another effect, &lt;code&gt;Sample&lt;/code&gt;, for producing random values, and then builds a program that requires both &lt;code&gt;Print&lt;/code&gt; and &lt;code&gt;Sample&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First we define the effect, using the same pattern as before.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;Random&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// A `Sample[A]` is a description of an effect that, when run, will generate a&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token comment&quot;&gt;// values of type `A` possibly using a random number generator&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; Sample&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Random &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Sample &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// This runs a `Sample[A]` producing a value of type `A`. By default it uses&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// the global random number generator, but the user can pass in a different&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// generator as the first argument.&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; run&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;sample&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Sample&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; random&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Random &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Random&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;given&lt;/span&gt; r&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Random &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; random&lt;br /&gt;    sample&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// Utility to use inside a `Sample[A]` to produce a random `Int`&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; int&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; r&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Random&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nextInt&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// Utility to use inside a `Sample[A]` to produce a random `Double`&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; double&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; r&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Random&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;nextDouble&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token comment&quot;&gt;// Constructs a `Sample[A]`.&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; body&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Random &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Sample&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    body&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can use both &lt;code&gt;Print&lt;/code&gt; and &lt;code&gt;Sample&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; printSample&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Console&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Random&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  Print &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Sample &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; Sample&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;int &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;    Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Print&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Sample&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;printSample&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;effects-that-change-control-flow&quot;&gt;Effects That Change Control Flow&lt;/h3&gt;
&lt;p&gt;So far, the effects we’ve looked at have very simple control flow. In fact they don’t alter the control flow at all. Many interesting effects, such as error handling and concurrency, require manipulation of the program’s control flow. How do we handle this in our model?&lt;/p&gt;
&lt;p&gt;We need a slight extension to accomodate this: when the user program calls an effect handler method, the effect handler is passed not just that method’s arguments but also also a &lt;strong&gt;continuation&lt;/strong&gt; that it can resume when the effect is complete. What’s a continuation? It represents the “rest of the program”: a value that can be invoked to continue execution from the point that called the effect handler. Cooperative threads, fibers, generators, and coroutines are all examples of abstractions that use a form of continuations.&lt;/p&gt;
&lt;p&gt;Continuations can be implemented as a program transform, but for performance we ideally want runtime support. This is why &lt;a href=&quot;https://github.com/scala-native/scala-native/blob/main/nativelib/src/main/scala-3/scala/scalanative/runtime/Continuations.scala&quot;&gt;Scala Native is getting continuations&lt;/a&gt;. On the JVM, &lt;a href=&quot;https://cr.openjdk.org/~rpressler/loom/Loom-Proposal.html&quot;&gt;Project Loom&lt;/a&gt; adds them.&lt;/p&gt;
&lt;p&gt;Scala 3 does not yet expose a continuation API, but it does have non-local exits in &lt;code&gt;scala.util.boundary&lt;/code&gt; that can express a few interesting things. Here’s an example implementing error-handling in the style of exceptions.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;//&gt; using scala 3&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;boundary&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;scala&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;boundary&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;break&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Label&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; Error&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; label&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Label&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; raise&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    break&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Error&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;object&lt;/span&gt; Raise &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; apply&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; body&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Error&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    body&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; raise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; e&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Error&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;raise&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; run&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;raise&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; A &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    boundary&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token keyword&quot;&gt;given&lt;/span&gt; error&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Error&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; Error&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;A&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;br /&gt;      raise&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token annotation punctuation&quot;&gt;@main&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; go&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Unit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; program&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;    Raise &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token comment&quot;&gt;// This early return is difficult to write in a purely functional style&lt;/span&gt;&lt;br /&gt;      List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;foreach&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; then Raise&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;raise&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Found 3&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;token string&quot;&gt;&quot;No 3 found&quot;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; result &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;program&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;result&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that we still have the separation between description and action. The &lt;code&gt;program&lt;/code&gt; isn’t run until we call &lt;code&gt;Raise.run&lt;/code&gt;, and the control-flow exits at the point where it is run, not at the point where it is defined.&lt;/p&gt;
&lt;p&gt;Using direct-style effects we can write programs that would have to use &lt;code&gt;traverse&lt;/code&gt; or other combinators in monadic style. Here’s an example that produces an &lt;code&gt;Option[List[Int]]&lt;/code&gt; from a &lt;code&gt;List[Int]&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; traverse&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  Raise &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;br /&gt;    Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;br /&gt;      List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;map&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; then Raise&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;raise&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;None&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Raise&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;traverse&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the equivalent of the following program using Cats:&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; traverseCats&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Option&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;List&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;Int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  List&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;traverse&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; then None &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; Some&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You might wonder how monads implement effects that play with control flow without requiring runtime support. The answer is that monads require the user to explicitly specify the control-flow. This is exactly what &lt;code&gt;flatMap&lt;/code&gt; does: it expresses what should happen in what order, and by giving the monad this information as a chain of &lt;code&gt;flatMaps&lt;/code&gt; it can evaluate them in the order that makes sense for the particular monad implementation. In fact monads are &lt;a href=&quot;https://dl.acm.org/doi/10.1145/174675.178047&quot;&gt;equivalent to delimited continuations&lt;/a&gt;. So direct-style effects and monad effects can be seen as just two different syntaxes for writing the same thing.&lt;/p&gt;
&lt;h2 id=&quot;capturing%2C-types%2C-and-effects&quot;&gt;Capturing, Types, and Effects&lt;/h2&gt;
&lt;p&gt;What we’ve seen so far suggests that effects are straightforward to implement and use, and they are for the most part. However there is at least one wrinkle that we need to be aware of: capturing effects.&lt;/p&gt;
&lt;p&gt;In the following code we capture a &lt;code&gt;Error[String]&lt;/code&gt; in a closure, and then attempt to call the &lt;code&gt;raise&lt;/code&gt; method on that &lt;code&gt;Error&lt;/code&gt; outside of the block where it is valid. This leads to a runtime exception.&lt;/p&gt;
&lt;pre class=&quot;language-scala&quot;&gt;&lt;code class=&quot;language-scala&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; capture&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;br /&gt;  Raise &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; error &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; then &lt;span class=&quot;token string&quot;&gt;&quot;Nothing to see here&quot;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;raise&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Hahahahaha!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; error&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;token keyword&quot;&gt;val&lt;/span&gt; closure &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Raise&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;capture&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;br /&gt;println&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;closure&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is this a serious flaw in the entire foundation of direct-style effects? No! What we’ve seen so far is only the portion of the effect system that is currently in Scala 3. &lt;a href=&quot;https://dotty.epfl.ch/docs/reference/experimental/cc&quot;&gt;Capture checking&lt;/a&gt;, which is still experimental, rules out this kind of bug. The &lt;a href=&quot;https://dl.acm.org/doi/10.1145/3618003&quot;&gt;Capturing Types&lt;/a&gt; paper has all the technical details.&lt;/p&gt;
&lt;p&gt;Capture checking in fact goes further than the examples we’ve seen so far. It tracks capability usage in the dynamic scope of the program. It can be used to implement composition of different effects. We saw an example earlier where we constructed a context function with type &lt;code&gt;(Console, Random) ?=&amp;gt; Unit&lt;/code&gt;. You might have given this type a bit of side-eye as it doesn’t use the type aliases we used for the two effects on their own (&lt;code&gt;Print&lt;/code&gt; and &lt;code&gt;Sample&lt;/code&gt;.) With capture checking the type system works out these types for us. It can also be use for resource checking, such as ensuring all open files are closed or that region-based memory management is implemented without leaks.&lt;/p&gt;
&lt;h2 id=&quot;conclusions-and-further-reading&quot;&gt;Conclusions and Further Reading&lt;/h2&gt;
&lt;p&gt;We’ve seen that direct-style effect allow us to write code in a natural direct style, while still retaining useful types that help with reasoning and allowing composition of effects. The implementation is a combination of:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;context functions;&lt;/li&gt;
&lt;li&gt;continuations; and&lt;/li&gt;
&lt;li&gt;type system improvements&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;which together allow us to express effects in direct-style as operations on effect handlers.&lt;/p&gt;
&lt;p&gt;Overall, I’m pretty excited by direct-style effects in general, and direct-style effects in Scala in particular. I think they are much more ergonomic than monadic effects, which in turn makes them accessible to a wider range of programmers. I’m also excited to have access to continuations, and presumably tail calls, in more languages. Tail calls are really useful for certain problems, such as &lt;a href=&quot;https://noelwelsh.com/posts/understanding-vm-dispatch/&quot;&gt;virtual machine dispatch&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I’m also excited to see Scala continuing to evolve. Scala has always been a language of innovation, and these changes are nothing more than a continuation (pun-intended) of that heritage. I’m also excited to see more investment in Scala Native. I think it’s only in Scala Native that the developers will have the flexibility to implement the runtime support needed for a full effect system, and also to really maximise its advantages by providing things like region based memory management. I also think Scala Native is important for Scala’s industrial adoption in use cases like serverless, so I see more investment in Scala Native as a &lt;em&gt;big&lt;/em&gt; win for the community.&lt;/p&gt;
&lt;p&gt;If you’d like to read more about direct-style effects here are some suggestions, which are a mix of accessible introductions and academic papers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.unison-lang.org/docs/fundamentals/abilities/for-monadically-inclined/&quot;&gt;Abilities for the monadically inclined&lt;/a&gt;, a very nice post from the Unison team that covers a lot of the same material from a different perspective.&lt;/li&gt;
&lt;li&gt;Dean Wampler’s post &lt;a href=&quot;https://medium.com/scala-3/scala-3-what-is-direct-style-d9c1bcb1f810&quot;&gt;What is “Direct Style”&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;https://github.com/lampepfl/gears&quot;&gt;Gears&lt;/a&gt; concurrency library being developed for Scala 3.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/rcardin/raise4s/tree/main&quot;&gt;Raise4s&lt;/a&gt;, a simple example of error handling as a direct-style effect.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.eff-lang.org/handlers-tutorial.pdf&quot;&gt;An Introduction to Algebraic Effects and Handlers&lt;/a&gt; is a tutorial from a more theoretic perspective.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://denotational.co.uk/publications/kammar-lindley-oury-handlers-in-action.pdf&quot;&gt;Handlers in Action&lt;/a&gt; is one of the more readable academic papers on implementing effect handlers.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://dl.acm.org/doi/10.1145/3618003&quot;&gt;Capturing Types&lt;/a&gt; describes the extensions to the Scala type system that are necessary for correctly implementing effects.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally, if you’ve found this interesting I think you’ll love my book, &lt;a href=&quot;https://scalawithcats.com/&quot;&gt;Functional Programming Strategies&lt;/a&gt;. It covers lots of the concepts in this post, such as continuation-passing style and interpreters, and a whole lot more.&lt;/p&gt;
</content>
	</entry>
</feed>
